Blob


1 /*
2 * Copyright (c) 2018, 2019 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>
26 #include <time.h>
28 #include "got_error.h"
29 #include "got_object.h"
30 #include "got_repository.h"
31 #include "got_reference.h"
33 #include "got_lib_sha1.h"
34 #include "got_lib_path.h"
35 #include "got_lib_delta.h"
36 #include "got_lib_inflate.h"
37 #include "got_lib_object.h"
39 #ifndef nitems
40 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
41 #endif
43 #define GOT_REF_HEADS "heads"
44 #define GOT_REF_TAGS "tags"
45 #define GOT_REF_REMOTES "remotes"
47 /* A symbolic reference. */
48 struct got_symref {
49 char *name;
50 char *ref;
51 };
53 /* A non-symbolic reference (there is no better designation). */
54 struct got_ref {
55 char *name;
56 u_int8_t sha1[SHA1_DIGEST_LENGTH];
57 };
59 /* A reference which points to an arbitrary object. */
60 struct got_reference {
61 unsigned int flags;
62 #define GOT_REF_IS_SYMBOLIC 0x01
64 union {
65 struct got_ref ref;
66 struct got_symref symref;
67 } ref;
68 };
70 static const struct got_error *
71 parse_symref(struct got_reference **ref, const char *name, const char *line)
72 {
73 struct got_symref *symref;
74 char *symref_name;
75 char *symref_ref;
77 if (line[0] == '\0')
78 return got_error(GOT_ERR_NOT_REF);
80 symref_name = strdup(name);
81 if (symref_name == NULL)
82 return got_error_from_errno();
83 symref_ref = strdup(line);
84 if (symref_ref == NULL) {
85 const struct got_error *err = got_error_from_errno();
86 free(symref_name);
87 return err;
88 }
90 *ref = calloc(1, sizeof(**ref));
91 if (*ref == NULL)
92 return got_error_from_errno();
93 (*ref)->flags |= GOT_REF_IS_SYMBOLIC;
94 symref = &((*ref)->ref.symref);
95 symref->name = symref_name;
96 symref->ref = symref_ref;
97 return NULL;
98 }
100 static const struct got_error *
101 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
103 uint8_t digest[SHA1_DIGEST_LENGTH];
104 char *ref_name;
106 if (strncmp(line, "ref: ", 5) == 0) {
107 line += 5;
108 return parse_symref(ref, name, line);
111 ref_name = strdup(name);
112 if (ref_name == NULL)
113 return got_error_from_errno();
115 if (!got_parse_sha1_digest(digest, line))
116 return got_error(GOT_ERR_NOT_REF);
118 *ref = calloc(1, sizeof(**ref));
119 if (*ref == NULL)
120 return got_error_from_errno();
121 (*ref)->ref.ref.name = ref_name;
122 memcpy(&(*ref)->ref.ref.sha1, digest, SHA1_DIGEST_LENGTH);
123 return NULL;
126 static const struct got_error *
127 parse_ref_file(struct got_reference **ref, const char *name,
128 const char *abspath)
130 const struct got_error *err = NULL;
131 FILE *f = fopen(abspath, "rb");
132 char *line;
133 size_t len;
134 const char delim[3] = {'\0', '\0', '\0'};
136 if (f == NULL)
137 return got_error(GOT_ERR_NOT_REF);
139 line = fparseln(f, &len, NULL, delim, 0);
140 if (line == NULL) {
141 err = got_error(GOT_ERR_NOT_REF);
142 goto done;
145 err = parse_ref_line(ref, name, line);
146 done:
147 free(line);
148 fclose(f);
149 return err;
152 static int
153 is_well_known_ref(const char *refname)
155 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
156 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
157 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
158 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
161 static char *
162 get_refs_dir_path(struct got_repository *repo, const char *refname)
164 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
165 return strdup(got_repo_get_path_git_dir(repo));
167 return got_repo_get_path_refs(repo);
170 static const struct got_error *
171 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
172 const char *line)
174 uint8_t digest[SHA1_DIGEST_LENGTH];
175 char *name;
177 *ref = NULL;
179 if (line[0] == '#' || line[0] == '^')
180 return NULL;
182 if (!got_parse_sha1_digest(digest, line))
183 return got_error(GOT_ERR_NOT_REF);
185 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
186 return NULL;
188 name = strdup(abs_refname);
189 if (name == NULL)
190 return got_error_from_errno();
192 *ref = calloc(1, sizeof(**ref));
193 if (*ref == NULL)
194 return got_error_from_errno();
195 (*ref)->ref.ref.name = name;;
196 memcpy(&(*ref)->ref.ref.sha1, digest, SHA1_DIGEST_LENGTH);
197 return NULL;
200 static const struct got_error *
201 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
202 int nsubdirs, const char *refname)
204 const struct got_error *err = NULL;
205 char *abs_refname;
206 char *line;
207 size_t len;
208 const char delim[3] = {'\0', '\0', '\0'};
209 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
211 if (ref_is_absolute)
212 abs_refname = (char *)refname;
213 do {
214 line = fparseln(f, &len, NULL, delim, 0);
215 if (line == NULL) {
216 err = got_error(GOT_ERR_NOT_REF);
217 break;
219 for (i = 0; i < nsubdirs; i++) {
220 if (!ref_is_absolute &&
221 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
222 refname) == -1)
223 return got_error_from_errno();
224 err = parse_packed_ref_line(ref, abs_refname, line);
225 if (!ref_is_absolute)
226 free(abs_refname);
227 if (err || *ref != NULL)
228 break;
230 free(line);
231 if (err)
232 break;
233 } while (*ref == NULL);
235 return err;
238 static const struct got_error *
239 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
240 const char *refname)
242 const struct got_error *err = NULL;
243 char *path_ref;
244 char *normpath;
246 if (asprintf(&path_ref, "%s/%s/%s", path_refs, subdir, refname) == -1)
247 return got_error_from_errno();
249 normpath = got_path_normalize(path_ref);
250 if (normpath == NULL) {
251 err = got_error(GOT_ERR_NOT_REF);
252 goto done;
255 err = parse_ref_file(ref, refname, normpath);
256 done:
257 free(path_ref);
258 free(normpath);
259 return err;
262 const struct got_error *
263 got_ref_open(struct got_reference **ref, struct got_repository *repo,
264 const char *refname)
266 const struct got_error *err = NULL;
267 char *path_refs = NULL;
268 const char *subdirs[] = {
269 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
270 };
271 int i, well_known = is_well_known_ref(refname);
273 if (!well_known) {
274 char *packed_refs_path;
275 FILE *f;
277 packed_refs_path = got_repo_get_path_packed_refs(repo);
278 if (packed_refs_path == NULL)
279 return got_error_from_errno();
281 f = fopen(packed_refs_path, "rb");
282 free(packed_refs_path);
283 if (f != NULL) {
284 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
285 refname);
286 fclose(f);
287 if (err == NULL)
288 goto done;
292 path_refs = get_refs_dir_path(repo, refname);
293 if (path_refs == NULL) {
294 err = got_error_from_errno();
295 goto done;
298 if (!well_known) {
299 for (i = 0; i < nitems(subdirs); i++) {
300 err = open_ref(ref, path_refs, subdirs[i], refname);
301 if (err == NULL)
302 goto done;
306 err = open_ref(ref, path_refs, "", refname);
307 done:
308 free(path_refs);
309 return err;
312 void
313 got_ref_close(struct got_reference *ref)
315 if (ref->flags & GOT_REF_IS_SYMBOLIC)
316 free(ref->ref.symref.name);
317 else
318 free(ref->ref.ref.name);
319 free(ref);
322 struct got_reference *
323 got_ref_dup(struct got_reference *ref)
325 struct got_reference *ret;
327 ret = calloc(1, sizeof(*ret));
328 if (ret == NULL)
329 return NULL;
331 ret->flags = ref->flags;
332 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
333 ret->ref.symref.name = strdup(ref->ref.symref.name);
334 if (ret->ref.symref.name == NULL) {
335 free(ret);
336 return NULL;
338 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
339 if (ret->ref.symref.ref == NULL) {
340 free(ret->ref.symref.name);
341 free(ret);
342 return NULL;
344 } else {
345 ref->ref.ref.name = strdup(ref->ref.ref.name);
346 if (ref->ref.ref.name == NULL) {
347 free(ret);
348 return NULL;
350 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
351 SHA1_DIGEST_LENGTH);
354 return ret;
357 static const struct got_error *
358 resolve_symbolic_ref(struct got_reference **resolved,
359 struct got_repository *repo, struct got_reference *ref)
361 struct got_reference *nextref;
362 const struct got_error *err;
364 err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
365 if (err)
366 return err;
368 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
369 err = resolve_symbolic_ref(resolved, repo, nextref);
370 else
371 *resolved = got_ref_dup(nextref);
373 got_ref_close(nextref);
374 return err;
377 const struct got_error *
378 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
379 struct got_reference *ref)
381 const struct got_error *err;
383 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
384 struct got_reference *resolved = NULL;
385 err = resolve_symbolic_ref(&resolved, repo, ref);
386 if (err == NULL)
387 err = got_ref_resolve(id, repo, resolved);
388 free(resolved);
389 return err;
392 *id = calloc(1, sizeof(**id));
393 if (*id == NULL)
394 return got_error_from_errno();
395 memcpy((*id)->sha1, ref->ref.ref.sha1, SHA1_DIGEST_LENGTH);
396 return NULL;
399 char *
400 got_ref_to_str(struct got_reference *ref)
402 char *str;
404 if (ref->flags & GOT_REF_IS_SYMBOLIC)
405 return strdup(ref->ref.symref.ref);
407 str = malloc(SHA1_DIGEST_STRING_LENGTH);
408 if (str == NULL)
409 return NULL;
411 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
412 SHA1_DIGEST_STRING_LENGTH) == NULL) {
413 free(str);
414 return NULL;
417 return str;
420 const char *
421 got_ref_get_name(struct got_reference *ref)
423 if (ref->flags & GOT_REF_IS_SYMBOLIC)
424 return ref->ref.symref.name;
426 return ref->ref.ref.name;