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 <dirent.h>
21 #include <sha1.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <util.h>
26 #include <zlib.h>
27 #include <time.h>
29 #include "got_error.h"
30 #include "got_object.h"
31 #include "got_repository.h"
32 #include "got_reference.h"
34 #include "got_lib_sha1.h"
35 #include "got_lib_path.h"
36 #include "got_lib_delta.h"
37 #include "got_lib_inflate.h"
38 #include "got_lib_object.h"
40 #ifndef nitems
41 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
42 #endif
44 #define GOT_REF_HEADS "heads"
45 #define GOT_REF_TAGS "tags"
46 #define GOT_REF_REMOTES "remotes"
48 /* A symbolic reference. */
49 struct got_symref {
50 char *name;
51 char *ref;
52 };
54 /* A non-symbolic reference (there is no better designation). */
55 struct got_ref {
56 char *name;
57 u_int8_t sha1[SHA1_DIGEST_LENGTH];
58 };
60 /* A reference which points to an arbitrary object. */
61 struct got_reference {
62 unsigned int flags;
63 #define GOT_REF_IS_SYMBOLIC 0x01
65 union {
66 struct got_ref ref;
67 struct got_symref symref;
68 } ref;
69 };
71 static const struct got_error *
72 parse_symref(struct got_reference **ref, const char *name, const char *line)
73 {
74 struct got_symref *symref;
75 char *symref_name;
76 char *symref_ref;
78 if (line[0] == '\0')
79 return got_error(GOT_ERR_BAD_REF_DATA);
81 symref_name = strdup(name);
82 if (symref_name == NULL)
83 return got_error_from_errno();
84 symref_ref = strdup(line);
85 if (symref_ref == NULL) {
86 const struct got_error *err = got_error_from_errno();
87 free(symref_name);
88 return err;
89 }
91 *ref = calloc(1, sizeof(**ref));
92 if (*ref == NULL)
93 return got_error_from_errno();
94 (*ref)->flags |= GOT_REF_IS_SYMBOLIC;
95 symref = &((*ref)->ref.symref);
96 symref->name = symref_name;
97 symref->ref = symref_ref;
98 return NULL;
99 }
101 static const struct got_error *
102 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
104 uint8_t digest[SHA1_DIGEST_LENGTH];
105 char *ref_name;
107 if (strncmp(line, "ref: ", 5) == 0) {
108 line += 5;
109 return parse_symref(ref, name, line);
112 ref_name = strdup(name);
113 if (ref_name == NULL)
114 return got_error_from_errno();
116 if (!got_parse_sha1_digest(digest, line))
117 return got_error(GOT_ERR_BAD_REF_DATA);
119 *ref = calloc(1, sizeof(**ref));
120 if (*ref == NULL)
121 return got_error_from_errno();
122 (*ref)->ref.ref.name = ref_name;
123 memcpy(&(*ref)->ref.ref.sha1, digest, SHA1_DIGEST_LENGTH);
124 return NULL;
127 static const struct got_error *
128 parse_ref_file(struct got_reference **ref, const char *name,
129 const char *abspath)
131 const struct got_error *err = NULL;
132 FILE *f = fopen(abspath, "rb");
133 char *line;
134 size_t len;
135 const char delim[3] = {'\0', '\0', '\0'};
137 if (f == NULL)
138 return NULL;
140 line = fparseln(f, &len, NULL, delim, 0);
141 if (line == NULL) {
142 err = got_error(GOT_ERR_BAD_REF_DATA);
143 goto done;
146 err = parse_ref_line(ref, name, line);
147 done:
148 free(line);
149 fclose(f);
150 return err;
153 static int
154 is_well_known_ref(const char *refname)
156 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
157 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
158 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
159 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
162 static char *
163 get_refs_dir_path(struct got_repository *repo, const char *refname)
165 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
166 return strdup(got_repo_get_path_git_dir(repo));
168 return got_repo_get_path_refs(repo);
171 static const struct got_error *
172 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
173 const char *line)
175 uint8_t digest[SHA1_DIGEST_LENGTH];
176 char *name;
178 *ref = NULL;
180 if (line[0] == '#' || line[0] == '^')
181 return NULL;
183 if (!got_parse_sha1_digest(digest, line))
184 return got_error(GOT_ERR_BAD_REF_DATA);
186 if (abs_refname) {
187 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
188 return NULL;
190 name = strdup(abs_refname);
191 if (name == NULL)
192 return got_error_from_errno();
193 } else
194 name = strdup(line + SHA1_DIGEST_STRING_LENGTH);
196 *ref = calloc(1, sizeof(**ref));
197 if (*ref == NULL)
198 return got_error_from_errno();
199 (*ref)->ref.ref.name = name;;
200 memcpy(&(*ref)->ref.ref.sha1, digest, SHA1_DIGEST_LENGTH);
201 return NULL;
204 static const struct got_error *
205 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
206 int nsubdirs, const char *refname)
208 const struct got_error *err = NULL;
209 char *abs_refname;
210 char *line;
211 size_t len;
212 const char delim[3] = {'\0', '\0', '\0'};
213 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
215 *ref = NULL;
217 if (ref_is_absolute)
218 abs_refname = (char *)refname;
219 do {
220 line = fparseln(f, &len, NULL, delim, 0);
221 if (line == NULL)
222 break;
223 for (i = 0; i < nsubdirs; i++) {
224 if (!ref_is_absolute &&
225 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
226 refname) == -1)
227 return got_error_from_errno();
228 err = parse_packed_ref_line(ref, abs_refname, line);
229 if (!ref_is_absolute)
230 free(abs_refname);
231 if (err || *ref != NULL)
232 break;
234 free(line);
235 if (err)
236 break;
237 } while (*ref == NULL);
239 return err;
242 static const struct got_error *
243 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
244 const char *name)
246 const struct got_error *err = NULL;
247 char *path = NULL;
248 char *normpath = NULL;
249 char *absname = NULL;
250 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
251 int ref_is_well_known = is_well_known_ref(name);
253 *ref = NULL;
255 if (ref_is_absolute || ref_is_well_known) {
256 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
257 return got_error_from_errno();
258 absname = (char *)name;
259 } else {
260 if (asprintf(&path, "%s/%s/%s", path_refs, subdir, name) == -1)
261 return got_error_from_errno();
263 if (asprintf(&absname, "refs/%s/%s", subdir, name) == -1) {
264 err = got_error_from_errno();
265 goto done;
269 normpath = got_path_normalize(path);
270 if (normpath == NULL) {
271 err = got_error_from_errno();
272 goto done;
275 err = parse_ref_file(ref, absname, normpath);
276 done:
277 if (!ref_is_absolute && !ref_is_well_known)
278 free(absname);
279 free(path);
280 free(normpath);
281 return err;
284 const struct got_error *
285 got_ref_open(struct got_reference **ref, struct got_repository *repo,
286 const char *refname)
288 const struct got_error *err = NULL;
289 char *path_refs = NULL;
290 const char *subdirs[] = {
291 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
292 };
293 int i, well_known = is_well_known_ref(refname);
295 *ref = NULL;
297 if (!well_known) {
298 char *packed_refs_path;
299 FILE *f;
301 packed_refs_path = got_repo_get_path_packed_refs(repo);
302 if (packed_refs_path == NULL)
303 return got_error_from_errno();
305 f = fopen(packed_refs_path, "rb");
306 free(packed_refs_path);
307 if (f != NULL) {
308 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
309 refname);
310 fclose(f);
311 if (err || *ref)
312 goto done;
316 path_refs = get_refs_dir_path(repo, refname);
317 if (path_refs == NULL) {
318 err = got_error_from_errno();
319 goto done;
322 if (!well_known) {
323 for (i = 0; i < nitems(subdirs); i++) {
324 err = open_ref(ref, path_refs, subdirs[i], refname);
325 if (err || *ref)
326 goto done;
330 err = open_ref(ref, path_refs, "", refname);
331 if (err)
332 goto done;
333 if (*ref == NULL)
334 err = got_error_not_ref(refname);
335 done:
336 free(path_refs);
337 return err;
340 void
341 got_ref_close(struct got_reference *ref)
343 if (ref->flags & GOT_REF_IS_SYMBOLIC)
344 free(ref->ref.symref.name);
345 else
346 free(ref->ref.ref.name);
347 free(ref);
350 struct got_reference *
351 got_ref_dup(struct got_reference *ref)
353 struct got_reference *ret;
355 ret = calloc(1, sizeof(*ret));
356 if (ret == NULL)
357 return NULL;
359 ret->flags = ref->flags;
360 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
361 ret->ref.symref.name = strdup(ref->ref.symref.name);
362 if (ret->ref.symref.name == NULL) {
363 free(ret);
364 return NULL;
366 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
367 if (ret->ref.symref.ref == NULL) {
368 free(ret->ref.symref.name);
369 free(ret);
370 return NULL;
372 } else {
373 ref->ref.ref.name = strdup(ref->ref.ref.name);
374 if (ref->ref.ref.name == NULL) {
375 free(ret);
376 return NULL;
378 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
379 SHA1_DIGEST_LENGTH);
382 return ret;
385 static const struct got_error *
386 resolve_symbolic_ref(struct got_reference **resolved,
387 struct got_repository *repo, struct got_reference *ref)
389 struct got_reference *nextref;
390 const struct got_error *err;
392 err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
393 if (err)
394 return err;
396 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
397 err = resolve_symbolic_ref(resolved, repo, nextref);
398 else
399 *resolved = got_ref_dup(nextref);
401 got_ref_close(nextref);
402 return err;
405 const struct got_error *
406 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
407 struct got_reference *ref)
409 const struct got_error *err;
411 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
412 struct got_reference *resolved = NULL;
413 err = resolve_symbolic_ref(&resolved, repo, ref);
414 if (err == NULL)
415 err = got_ref_resolve(id, repo, resolved);
416 free(resolved);
417 return err;
420 *id = calloc(1, sizeof(**id));
421 if (*id == NULL)
422 return got_error_from_errno();
423 memcpy((*id)->sha1, ref->ref.ref.sha1, SHA1_DIGEST_LENGTH);
424 return NULL;
427 char *
428 got_ref_to_str(struct got_reference *ref)
430 char *str;
432 if (ref->flags & GOT_REF_IS_SYMBOLIC)
433 return strdup(ref->ref.symref.ref);
435 str = malloc(SHA1_DIGEST_STRING_LENGTH);
436 if (str == NULL)
437 return NULL;
439 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
440 SHA1_DIGEST_STRING_LENGTH) == NULL) {
441 free(str);
442 return NULL;
445 return str;
448 const char *
449 got_ref_get_name(struct got_reference *ref)
451 if (ref->flags & GOT_REF_IS_SYMBOLIC)
452 return ref->ref.symref.name;
454 return ref->ref.ref.name;
457 static const struct got_error *
458 insert_ref(struct got_reflist_head *refs, struct got_reference *ref,
459 struct got_repository *repo)
461 const struct got_error *err;
462 struct got_object_id *id;
463 struct got_reflist_entry *new, *re, *prev;
464 int cmp;
466 err = got_ref_resolve(&id, repo, ref);
467 if (err)
468 return err;
470 new = malloc(sizeof(*re));
471 if (new == NULL) {
472 free(id);
473 return got_error_from_errno();
475 new->ref = ref;
476 new->id = id;
478 /*
479 * We must de-duplicate entries on insert because packed-refs may
480 * contain redundant entries. On-disk refs take precedence.
481 * This code assumes that on-disk revs are read before packed-refs.
482 * We're iterating the list anyway, so insert elements sorted by name.
483 */
484 re = SIMPLEQ_FIRST(refs);
485 while (re) {
486 cmp = got_path_cmp(got_ref_get_name(re->ref),
487 got_ref_get_name(ref));
488 if (cmp == 0) {
489 free(ref); /* duplicate */
490 return NULL;
491 } else if (cmp > 0) {
492 if (prev)
493 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
494 else
495 SIMPLEQ_INSERT_HEAD(refs, new, entry);
496 return NULL;
497 } else {
498 prev = re;
499 re = SIMPLEQ_NEXT(re, entry);
503 SIMPLEQ_INSERT_TAIL(refs, new, entry);
504 return NULL;
507 static const struct got_error *
508 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
509 const char *subdir, struct got_repository *repo)
511 const struct got_error *err = NULL;
512 DIR *d = NULL;
513 char *path_subdir;
515 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
516 return got_error_from_errno();
518 d = opendir(path_subdir);
519 if (d == NULL)
520 goto done;
522 while (1) {
523 struct dirent *dent;
524 struct got_reference *ref;
525 char *child;
527 dent = readdir(d);
528 if (dent == NULL)
529 break;
531 if (strcmp(dent->d_name, ".") == 0 ||
532 strcmp(dent->d_name, "..") == 0)
533 continue;
535 switch (dent->d_type) {
536 case DT_REG:
537 err = open_ref(&ref, path_refs, subdir, dent->d_name);
538 if (err)
539 goto done;
540 if (ref) {
541 err = insert_ref(refs, ref, repo);
542 if (err)
543 goto done;
545 break;
546 case DT_DIR:
547 if (asprintf(&child, "%s%s%s", subdir,
548 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
549 err = got_error_from_errno();
550 break;
552 err = gather_on_disk_refs(refs, path_refs, child, repo);
553 free(child);
554 break;
555 default:
556 break;
559 done:
560 if (d)
561 closedir(d);
562 free(path_subdir);
563 return err;
566 const struct got_error *
567 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo)
569 const struct got_error *err;
570 char *packed_refs_path, *path_refs = NULL;
571 FILE *f = NULL;
572 struct got_reference *ref;
574 /* HEAD ref should always exist. */
575 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
576 if (path_refs == NULL) {
577 err = got_error_from_errno();
578 goto done;
580 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD);
581 if (err)
582 goto done;
583 err = insert_ref(refs, ref, repo);
584 if (err)
585 goto done;
587 /* Gather on-disk refs before parsing packed-refs. */
588 free(path_refs);
589 path_refs = get_refs_dir_path(repo, "");
590 if (path_refs == NULL) {
591 err = got_error_from_errno();
592 goto done;
594 err = gather_on_disk_refs(refs, path_refs, "", repo);
595 if (err)
596 goto done;
598 /*
599 * The packed-refs file may contain redundant entries, in which
600 * case on-disk refs take precedence.
601 */
602 packed_refs_path = got_repo_get_path_packed_refs(repo);
603 if (packed_refs_path == NULL) {
604 err = got_error_from_errno();
605 goto done;
608 f = fopen(packed_refs_path, "r");
609 free(packed_refs_path);
610 if (f) {
611 char *line;
612 size_t len;
613 const char delim[3] = {'\0', '\0', '\0'};
614 while (1) {
615 line = fparseln(f, &len, NULL, delim, 0);
616 if (line == NULL)
617 break;
618 err = parse_packed_ref_line(&ref, NULL, line);
619 if (err)
620 goto done;
621 if (ref) {
622 err = insert_ref(refs, ref, repo);
623 if (err)
624 goto done;
628 done:
629 free(path_refs);
630 if (f)
631 fclose(f);
632 return err;