Blob


1 /*
2 * Copyright (c) 2018 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_reference.h"
32 #include "got_lib_sha1.h"
33 #include "got_lib_path.h"
34 #include "got_lib_delta.h"
35 #include "got_lib_zbuf.h"
36 #include "got_lib_object.h"
38 #define GOT_REF_HEADS "heads"
39 #define GOT_REF_TAGS "tags"
40 #define GOT_REF_REMOTES "remotes"
42 /* A symbolic reference. */
43 struct got_symref {
44 char *name;
45 char *ref;
46 };
48 /* A non-symbolic reference (there is no better designation). */
49 struct got_ref {
50 char *name;
51 u_int8_t sha1[SHA1_DIGEST_LENGTH];
52 };
54 /* A reference which points to an arbitrary object. */
55 struct got_reference {
56 unsigned int flags;
57 #define GOT_REF_IS_SYMBOLIC 0x01
59 union {
60 struct got_ref ref;
61 struct got_symref symref;
62 } ref;
63 };
65 static const struct got_error *
66 parse_symref(struct got_reference **ref, const char *name, const char *line)
67 {
68 struct got_symref *symref;
69 char *symref_name;
70 char *symref_ref;
72 if (line[0] == '\0')
73 return got_error(GOT_ERR_NOT_REF);
75 symref_name = strdup(name);
76 if (symref_name == NULL)
77 return got_error_from_errno();
78 symref_ref = strdup(line);
79 if (symref_ref == NULL) {
80 const struct got_error *err = got_error_from_errno();
81 free(symref_name);
82 return err;
83 }
85 *ref = calloc(1, sizeof(**ref));
86 if (*ref == NULL)
87 return got_error_from_errno();
88 (*ref)->flags |= GOT_REF_IS_SYMBOLIC;
89 symref = &((*ref)->ref.symref);
90 symref->name = symref_name;
91 symref->ref = symref_ref;
92 return NULL;
93 }
95 static const struct got_error *
96 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
97 {
98 uint8_t digest[SHA1_DIGEST_LENGTH];
99 char *ref_name;
101 if (strncmp(line, "ref: ", 5) == 0) {
102 line += 5;
103 return parse_symref(ref, name, line);
106 ref_name = strdup(name);
107 if (ref_name == NULL)
108 return got_error_from_errno();
110 if (!got_parse_sha1_digest(digest, line))
111 return got_error(GOT_ERR_NOT_REF);
113 *ref = calloc(1, sizeof(**ref));
114 if (*ref == NULL)
115 return got_error_from_errno();
116 (*ref)->ref.ref.name = ref_name;
117 memcpy(&(*ref)->ref.ref.sha1, digest, SHA1_DIGEST_LENGTH);
118 return NULL;
121 static const struct got_error *
122 parse_ref_file(struct got_reference **ref, const char *name,
123 const char *abspath)
125 const struct got_error *err = NULL;
126 FILE *f = fopen(abspath, "rb");
127 char *line;
128 size_t len;
129 const char delim[3] = {'\0', '\0', '\0'};
131 if (f == NULL)
132 return got_error(GOT_ERR_NOT_REF);
134 line = fparseln(f, &len, NULL, delim, 0);
135 if (line == NULL) {
136 err = got_error(GOT_ERR_NOT_REF);
137 goto done;
140 err = parse_ref_line(ref, name, line);
141 done:
142 free(line);
143 fclose(f);
144 return err;
147 static char *
148 get_refs_dir_path(struct got_repository *repo, const char *refname)
150 if (strcmp(refname, GOT_REF_HEAD) == 0 ||
151 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
152 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
153 strcmp(refname, GOT_REF_FETCH_HEAD) == 0 ||
154 strncmp(refname, "refs/", 5) == 0)
155 return got_repo_get_path_git_dir(repo);
157 return got_repo_get_path_refs(repo);
160 static const struct got_error *
161 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
162 const char *refname)
164 const struct got_error *err = NULL;
165 char *path_ref;
166 char *normpath;
168 if (asprintf(&path_ref, "%s/%s/%s", path_refs, subdir, refname) == -1)
169 return got_error_from_errno();
171 normpath = got_path_normalize(path_ref);
172 if (normpath == NULL) {
173 err = got_error(GOT_ERR_NOT_REF);
174 goto done;
177 err = parse_ref_file(ref, refname, normpath);
178 done:
179 free(path_ref);
180 free(normpath);
181 return err;
184 const struct got_error *
185 got_ref_open(struct got_reference **ref, struct got_repository *repo,
186 const char *refname)
188 const struct got_error *err = NULL;
189 char *path_refs = get_refs_dir_path(repo, refname);
191 if (path_refs == NULL) {
192 err = got_error_from_errno();
193 goto done;
196 /* XXX For now, this assumes that refs exist in the filesystem. */
198 err = open_ref(ref, path_refs, GOT_REF_HEADS, refname);
199 if (err != NULL)
200 err = open_ref(ref, path_refs, GOT_REF_TAGS, refname);
201 if (err != NULL)
202 err = open_ref(ref, path_refs, GOT_REF_REMOTES, refname);
203 if (err != NULL)
204 err = open_ref(ref, path_refs, "", refname);
205 done:
206 free(path_refs);
207 return err;
210 void
211 got_ref_close(struct got_reference *ref)
213 if (ref->flags & GOT_REF_IS_SYMBOLIC)
214 free(ref->ref.symref.name);
215 else
216 free(ref->ref.ref.name);
217 free(ref);
220 struct got_reference *
221 got_ref_dup(struct got_reference *ref)
223 struct got_reference *ret;
225 ret = calloc(1, sizeof(*ret));
226 if (ret == NULL)
227 return NULL;
229 ret->flags = ref->flags;
230 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
231 ret->ref.symref.name = strdup(ref->ref.symref.name);
232 if (ret->ref.symref.name == NULL) {
233 free(ret);
234 return NULL;
236 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
237 if (ret->ref.symref.ref == NULL) {
238 free(ret->ref.symref.name);
239 free(ret);
240 return NULL;
242 } else {
243 ref->ref.ref.name = strdup(ref->ref.ref.name);
244 if (ref->ref.ref.name == NULL) {
245 free(ret);
246 return NULL;
248 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
249 SHA1_DIGEST_LENGTH);
252 return ret;
255 static const struct got_error *
256 resolve_symbolic_ref(struct got_reference **resolved,
257 struct got_repository *repo, struct got_reference *ref)
259 struct got_reference *nextref;
260 const struct got_error *err;
262 err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
263 if (err)
264 return err;
266 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
267 err = resolve_symbolic_ref(resolved, repo, nextref);
268 else
269 *resolved = got_ref_dup(nextref);
271 got_ref_close(nextref);
272 return err;
275 const struct got_error *
276 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
277 struct got_reference *ref)
279 const struct got_error *err;
281 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
282 struct got_reference *resolved = NULL;
283 err = resolve_symbolic_ref(&resolved, repo, ref);
284 if (err == NULL)
285 err = got_ref_resolve(id, repo, resolved);
286 free(resolved);
287 return err;
290 *id = calloc(1, sizeof(**id));
291 if (*id == NULL)
292 return got_error_from_errno();
293 memcpy((*id)->sha1, ref->ref.ref.sha1, SHA1_DIGEST_LENGTH);
294 return NULL;
297 char *
298 got_ref_to_str(struct got_reference *ref)
300 char *str;
301 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
302 if (asprintf(&str, "ref: %s", ref->ref.symref.ref) == -1)
303 return NULL;
304 } else {
305 str = calloc(1, SHA1_DIGEST_STRING_LENGTH);
306 if (str == NULL)
307 return NULL;
308 str = got_sha1_digest_to_str(ref->ref.ref.sha1, str,
309 SHA1_DIGEST_STRING_LENGTH);
312 return str;