Blob


1 /*
2 * Copyright (c) 2017 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
20 #include <sha1.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <util.h>
25 #include <zlib.h>
27 #include "got_error.h"
28 #include "got_object.h"
29 #include "got_repository.h"
30 #include "got_refs.h"
31 #include "got_sha1.h"
33 #include "path.h"
36 static const struct got_error *
37 parse_symref(struct got_reference **ref, const char *name, const char *line)
38 {
39 struct got_symref *symref;
40 char *symref_name;
41 char *symref_ref;
43 if (line[0] == '\0')
44 return got_error(GOT_ERR_NOT_REF);
46 symref_name = strdup(name);
47 if (symref_name == NULL)
48 return got_error(GOT_ERR_NO_MEM);
49 symref_ref = strdup(line);
50 if (symref_ref == NULL) {
51 free(symref_name);
52 return got_error(GOT_ERR_NO_MEM);
53 }
55 *ref = calloc(1, sizeof(**ref));
56 (*ref)->flags |= GOT_REF_IS_SYMBOLIC;
57 symref = &((*ref)->ref.symref);
58 symref->name = symref_name;
59 symref->ref = symref_ref;
60 return NULL;
61 }
63 static const struct got_error *
64 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
65 {
66 uint8_t digest[SHA1_DIGEST_LENGTH];
67 char *ref_name;
69 if (strncmp(line, "ref: ", 5) == 0) {
70 line += 5;
71 return parse_symref(ref, name, line);
72 }
74 ref_name = strdup(name);
75 if (ref_name == NULL)
76 return got_error(GOT_ERR_NO_MEM);
78 if (!got_parse_sha1_digest(digest, line))
79 return got_error(GOT_ERR_NOT_REF);
81 *ref = calloc(1, sizeof(**ref));
82 (*ref)->ref.ref.name = ref_name;
83 memcpy(&(*ref)->ref.ref.sha1, digest, SHA1_DIGEST_LENGTH);
84 return NULL;
85 }
87 static const struct got_error *
88 parse_ref_file(struct got_reference **ref, const char *name,
89 const char *abspath)
90 {
91 const struct got_error *err = NULL;
92 FILE *f = fopen(abspath, "rb");
93 char *line;
94 size_t len;
95 const char delim[3] = {'\0', '\0', '\0'};
97 if (f == NULL)
98 return got_error(GOT_ERR_NOT_REF);
100 line = fparseln(f, &len, NULL, delim, 0);
101 if (line == NULL) {
102 err = got_error(GOT_ERR_NOT_REF);
103 goto done;
106 err = parse_ref_line(ref, name, line);
107 done:
108 free(line);
109 fclose(f);
110 return err;
113 static char *
114 get_refs_dir_path(struct got_repository *repo, const char *refname)
116 if (strcmp(refname, GOT_REF_HEAD) == 0 ||
117 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
118 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
119 strcmp(refname, GOT_REF_FETCH_HEAD) == 0 ||
120 strncmp(refname, "refs/", 5) == 0)
121 return got_repo_get_path_git_dir(repo);
123 return got_repo_get_path_refs(repo);
126 const struct got_error *
127 got_ref_open(struct got_reference **ref, struct got_repository *repo,
128 const char *refname)
130 const struct got_error *err = NULL;
131 char *path_ref = NULL;
132 char *normpath = NULL;
133 const char *parent_dir;
134 char *path_refs = get_refs_dir_path(repo, refname);
136 if (path_refs == NULL) {
137 err = got_error(GOT_ERR_NO_MEM);
138 goto done;
141 /* XXX For now, this assumes that refs exist in the filesystem. */
143 if (asprintf(&path_ref, "%s/%s", path_refs, refname) == -1) {
144 err = got_error(GOT_ERR_NO_MEM);
145 goto done;
148 normpath = got_path_normalize(path_ref);
149 if (normpath == NULL) {
150 err = got_error(GOT_ERR_NOT_REF);
151 goto done;
154 err = parse_ref_file(ref, refname, normpath);
155 done:
156 free(normpath);
157 free(path_ref);
158 free(path_refs);
159 return err;
162 void
163 got_ref_close(struct got_reference *ref)
165 if (ref->flags & GOT_REF_IS_SYMBOLIC)
166 free(ref->ref.symref.name);
167 else
168 free(ref->ref.ref.name);
169 free(ref);
172 struct got_reference *
173 got_ref_dup(struct got_reference *ref)
175 struct got_reference *ret = calloc(1, sizeof(*ret));
176 char *name = NULL;
177 char *symref = NULL;
179 if (ret == NULL)
180 return NULL;
182 ret->flags = ref->flags;
183 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
184 ret->ref.symref.name = strdup(ref->ref.symref.name);
185 if (ret->ref.symref.name == NULL) {
186 free(ret);
187 return NULL;
189 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
190 if (ret->ref.symref.ref == NULL) {
191 free(ret->ref.symref.name);
192 free(ret);
193 return NULL;
195 } else {
196 ref->ref.ref.name = strdup(ref->ref.ref.name);
197 if (ref->ref.ref.name == NULL) {
198 free(ret);
199 return NULL;
201 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
202 SHA1_DIGEST_LENGTH);
205 return ret;
208 static const struct got_error *
209 resolve_symbolic_ref(struct got_reference **resolved,
210 struct got_repository *repo, struct got_reference *ref)
212 struct got_reference *nextref;
213 const struct got_error *err;
215 err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
216 if (err)
217 return err;
219 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
220 err = resolve_symbolic_ref(resolved, repo, nextref);
221 else
222 *resolved = got_ref_dup(nextref);
224 got_ref_close(nextref);
225 return err;
228 const struct got_error *
229 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
230 struct got_reference *ref)
232 const struct got_error *err;
234 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
235 struct got_reference *resolved = NULL;
236 err = resolve_symbolic_ref(&resolved, repo, ref);
237 if (err == NULL)
238 err = got_ref_resolve(id, repo, resolved);
239 free(resolved);
240 return err;
243 *id = calloc(1, sizeof(**id));
244 if (*id == NULL)
245 return got_error(GOT_ERR_NO_MEM);
246 memcpy((*id)->sha1, ref->ref.ref.sha1, SHA1_DIGEST_LENGTH);
247 return NULL;