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>
19 #include <sys/stat.h>
21 #include <errno.h>
22 #include <ctype.h>
23 #include <dirent.h>
24 #include <limits.h>
25 #include <sha1.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <util.h>
31 #include <zlib.h>
32 #include <time.h>
33 #include <libgen.h>
35 #include "got_error.h"
36 #include "got_object.h"
37 #include "got_repository.h"
38 #include "got_reference.h"
39 #include "got_opentemp.h"
40 #include "got_path.h"
42 #include "got_lib_sha1.h"
43 #include "got_lib_delta.h"
44 #include "got_lib_inflate.h"
45 #include "got_lib_object.h"
46 #include "got_lib_object_idset.h"
47 #include "got_lib_lockfile.h"
49 #ifndef nitems
50 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
51 #endif
53 #define GOT_REF_HEADS "heads"
54 #define GOT_REF_TAGS "tags"
55 #define GOT_REF_REMOTES "remotes"
57 /*
58 * We do not resolve tags yet, and don't yet care about sorting refs either,
59 * so packed-refs files we write contain a minimal header which disables all
60 * packed-refs "traits" supported by Git.
61 */
62 #define GOT_PACKED_REFS_HEADER "# pack-refs with:"
64 /* A symbolic reference. */
65 struct got_symref {
66 char *name;
67 char *ref;
68 };
70 #define GOT_REF_RECURSE_MAX 20
72 /* A non-symbolic reference (there is no better designation). */
73 struct got_ref {
74 char *name;
75 u_int8_t sha1[SHA1_DIGEST_LENGTH];
76 };
78 /* A reference which points to an arbitrary object. */
79 struct got_reference {
80 unsigned int flags;
81 #define GOT_REF_IS_SYMBOLIC 0x01
82 #define GOT_REF_IS_PACKED 0x02
84 union {
85 struct got_ref ref;
86 struct got_symref symref;
87 } ref;
89 struct got_lockfile *lf;
90 };
92 static const struct got_error *
93 alloc_ref(struct got_reference **ref, const char *name,
94 struct got_object_id *id, int flags)
95 {
96 const struct got_error *err = NULL;
98 *ref = calloc(1, sizeof(**ref));
99 if (*ref == NULL)
100 return got_error_from_errno("calloc");
102 memcpy((*ref)->ref.ref.sha1, id->sha1, sizeof((*ref)->ref.ref.sha1));
103 (*ref)->flags = flags;
104 (*ref)->ref.ref.name = strdup(name);
105 if ((*ref)->ref.ref.name == NULL) {
106 err = got_error_from_errno("strdup");
107 got_ref_close(*ref);
108 *ref = NULL;
110 return err;
113 static const struct got_error *
114 alloc_symref(struct got_reference **ref, const char *name,
115 const char *target_ref, int flags)
117 const struct got_error *err = NULL;
119 *ref = calloc(1, sizeof(**ref));
120 if (*ref == NULL)
121 return got_error_from_errno("calloc");
123 (*ref)->flags = GOT_REF_IS_SYMBOLIC | flags;
124 (*ref)->ref.symref.name = strdup(name);
125 if ((*ref)->ref.symref.name == NULL) {
126 err = got_error_from_errno("strdup");
127 got_ref_close(*ref);
128 *ref = NULL;
129 return err;
131 (*ref)->ref.symref.ref = strdup(target_ref);
132 if ((*ref)->ref.symref.ref == NULL) {
133 err = got_error_from_errno("strdup");
134 got_ref_close(*ref);
135 *ref = NULL;
137 return err;
140 static const struct got_error *
141 parse_symref(struct got_reference **ref, const char *name, const char *line)
143 if (line[0] == '\0')
144 return got_error(GOT_ERR_BAD_REF_DATA);
146 return alloc_symref(ref, name, line, 0);
149 static const struct got_error *
150 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
152 struct got_object_id id;
154 if (strncmp(line, "ref: ", 5) == 0) {
155 line += 5;
156 return parse_symref(ref, name, line);
159 if (!got_parse_sha1_digest(id.sha1, line))
160 return got_error(GOT_ERR_BAD_REF_DATA);
162 return alloc_ref(ref, name, &id, 0);
165 static const struct got_error *
166 parse_ref_file(struct got_reference **ref, const char *name,
167 const char *absname, const char *abspath, int lock)
169 const struct got_error *err = NULL;
170 FILE *f;
171 char *line = NULL;
172 size_t linesize = 0;
173 ssize_t linelen;
174 struct got_lockfile *lf = NULL;
176 if (lock) {
177 err = got_lockfile_lock(&lf, abspath);
178 if (err) {
179 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
180 err = got_error_not_ref(name);
181 return err;
185 f = fopen(abspath, "rb");
186 if (f == NULL) {
187 if (errno != ENOTDIR && errno != ENOENT)
188 err = got_error_from_errno2("fopen", abspath);
189 else
190 err = got_error_not_ref(name);
191 if (lock)
192 got_lockfile_unlock(lf);
193 return err;
196 linelen = getline(&line, &linesize, f);
197 if (linelen == -1) {
198 if (feof(f))
199 err = NULL; /* ignore empty files (could be locks) */
200 else {
201 if (errno == EISDIR)
202 err = got_error(GOT_ERR_NOT_REF);
203 else if (ferror(f))
204 err = got_ferror(f, GOT_ERR_IO);
205 else
206 err = got_error_from_errno2("getline", abspath);
208 if (lock)
209 got_lockfile_unlock(lf);
210 goto done;
212 while (linelen > 0 && line[linelen - 1] == '\n') {
213 line[linelen - 1] = '\0';
214 linelen--;
217 err = parse_ref_line(ref, absname, line);
218 if (lock) {
219 if (err)
220 got_lockfile_unlock(lf);
221 else {
222 if (*ref)
223 (*ref)->lf = lf;
224 else
225 got_lockfile_unlock(lf);
228 done:
229 free(line);
230 if (fclose(f) != 0 && err == NULL) {
231 err = got_error_from_errno("fclose");
232 if (*ref) {
233 if (lock)
234 got_ref_unlock(*ref);
235 got_ref_close(*ref);
236 *ref = NULL;
239 return err;
242 static int
243 is_well_known_ref(const char *refname)
245 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
246 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
247 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
248 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
251 static char *
252 get_refs_dir_path(struct got_repository *repo, const char *refname)
254 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
255 return strdup(got_repo_get_path_git_dir(repo));
257 return got_repo_get_path_refs(repo);
260 static int
261 is_valid_ref_name(const char *name)
263 const char *s, *seg;
264 const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
265 const char *forbidden_seq[] = { "//", "..", "@{" };
266 const char *lfs = GOT_LOCKFILE_SUFFIX;
267 const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
268 size_t i;
270 if (name[0] == '@' && name[1] == '\0')
271 return 0;
273 s = name;
274 seg = s;
275 if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
276 return 0;
277 while (*s) {
278 for (i = 0; i < nitems(forbidden); i++) {
279 if (*s == forbidden[i])
280 return 0;
282 for (i = 0; i < nitems(forbidden_seq); i++) {
283 if (s[0] == forbidden_seq[i][0] &&
284 s[1] == forbidden_seq[i][1])
285 return 0;
287 if (iscntrl((unsigned char)s[0]))
288 return 0;
289 if (s[0] == '.' && s[1] == '\0')
290 return 0;
291 if (*s == '/') {
292 const char *nextseg = s + 1;
293 if (nextseg[0] == '\0' || nextseg[0] == '.' ||
294 nextseg[0] == '/')
295 return 0;
296 if (seg <= s - lfs_len &&
297 strncmp(s - lfs_len, lfs, lfs_len) == 0)
298 return 0;
299 seg = nextseg;
301 s++;
304 if (seg <= s - lfs_len &&
305 strncmp(s - lfs_len, lfs, lfs_len) == 0)
306 return 0;
308 return 1;
311 const struct got_error *
312 got_ref_alloc(struct got_reference **ref, const char *name,
313 struct got_object_id *id)
315 if (!is_valid_ref_name(name))
316 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
318 return alloc_ref(ref, name, id, 0);
321 const struct got_error *
322 got_ref_alloc_symref(struct got_reference **ref, const char *name,
323 struct got_reference *target_ref)
325 if (!is_valid_ref_name(name))
326 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
328 return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
331 static const struct got_error *
332 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
333 const char *line)
335 struct got_object_id id;
336 const char *name;
338 *ref = NULL;
340 if (line[0] == '#' || line[0] == '^')
341 return NULL;
343 if (!got_parse_sha1_digest(id.sha1, line))
344 return got_error(GOT_ERR_BAD_REF_DATA);
346 if (abs_refname) {
347 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
348 return NULL;
349 name = abs_refname;
350 } else
351 name = line + SHA1_DIGEST_STRING_LENGTH;
353 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED);
356 static const struct got_error *
357 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
358 int nsubdirs, const char *refname)
360 const struct got_error *err = NULL;
361 char *abs_refname;
362 char *line;
363 size_t len;
364 const char delim[3] = {'\0', '\0', '\0'};
365 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
367 *ref = NULL;
369 if (ref_is_absolute)
370 abs_refname = (char *)refname;
371 do {
372 line = fparseln(f, &len, NULL, delim, 0);
373 if (line == NULL) {
374 if (feof(f))
375 break;
376 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
377 break;
379 for (i = 0; i < nsubdirs; i++) {
380 if (!ref_is_absolute &&
381 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
382 refname) == -1)
383 return got_error_from_errno("asprintf");
384 err = parse_packed_ref_line(ref, abs_refname, line);
385 if (!ref_is_absolute)
386 free(abs_refname);
387 if (err || *ref != NULL)
388 break;
390 free(line);
391 if (err)
392 break;
393 } while (*ref == NULL);
395 return err;
398 static const struct got_error *
399 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
400 const char *name, int lock)
402 const struct got_error *err = NULL;
403 char *path = NULL;
404 char *absname = NULL;
405 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
406 int ref_is_well_known = (subdir[0] == '\0' && is_well_known_ref(name));
408 *ref = NULL;
410 if (ref_is_absolute || ref_is_well_known) {
411 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
412 return got_error_from_errno("asprintf");
413 absname = (char *)name;
414 } else {
415 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
416 subdir[0] ? "/" : "", name) == -1)
417 return got_error_from_errno("asprintf");
419 if (asprintf(&absname, "refs/%s%s%s",
420 subdir, subdir[0] ? "/" : "", name) == -1) {
421 err = got_error_from_errno("asprintf");
422 goto done;
426 err = parse_ref_file(ref, name, absname, path, lock);
427 done:
428 if (!ref_is_absolute && !ref_is_well_known)
429 free(absname);
430 free(path);
431 return err;
434 const struct got_error *
435 got_ref_open(struct got_reference **ref, struct got_repository *repo,
436 const char *refname, int lock)
438 const struct got_error *err = NULL;
439 char *path_refs = NULL;
440 const char *subdirs[] = {
441 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
442 };
443 size_t i;
444 int well_known = is_well_known_ref(refname);
445 struct got_lockfile *lf = NULL;
447 *ref = NULL;
449 path_refs = get_refs_dir_path(repo, refname);
450 if (path_refs == NULL) {
451 err = got_error_from_errno2("get_refs_dir_path", refname);
452 goto done;
455 if (well_known) {
456 err = open_ref(ref, path_refs, "", refname, lock);
457 } else {
458 char *packed_refs_path;
459 FILE *f;
461 /* Search on-disk refs before packed refs! */
462 for (i = 0; i < nitems(subdirs); i++) {
463 err = open_ref(ref, path_refs, subdirs[i], refname,
464 lock);
465 if ((err && err->code != GOT_ERR_NOT_REF) || *ref)
466 goto done;
469 packed_refs_path = got_repo_get_path_packed_refs(repo);
470 if (packed_refs_path == NULL) {
471 err = got_error_from_errno(
472 "got_repo_get_path_packed_refs");
473 goto done;
476 if (lock) {
477 err = got_lockfile_lock(&lf, packed_refs_path);
478 if (err)
479 goto done;
481 f = fopen(packed_refs_path, "rb");
482 free(packed_refs_path);
483 if (f != NULL) {
484 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
485 refname);
486 if (!err) {
487 if (fclose(f) != 0) {
488 err = got_error_from_errno("fclose");
489 got_ref_close(*ref);
490 *ref = NULL;
491 } else if (*ref)
492 (*ref)->lf = lf;
496 done:
497 if (!err && *ref == NULL)
498 err = got_error_not_ref(refname);
499 if (err && lf)
500 got_lockfile_unlock(lf);
501 free(path_refs);
502 return err;
505 void
506 got_ref_close(struct got_reference *ref)
508 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
509 free(ref->ref.symref.name);
510 free(ref->ref.symref.ref);
511 } else
512 free(ref->ref.ref.name);
513 free(ref);
516 struct got_reference *
517 got_ref_dup(struct got_reference *ref)
519 struct got_reference *ret;
521 ret = calloc(1, sizeof(*ret));
522 if (ret == NULL)
523 return NULL;
525 ret->flags = ref->flags;
526 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
527 ret->ref.symref.name = strdup(ref->ref.symref.name);
528 if (ret->ref.symref.name == NULL) {
529 free(ret);
530 return NULL;
532 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
533 if (ret->ref.symref.ref == NULL) {
534 free(ret->ref.symref.name);
535 free(ret);
536 return NULL;
538 } else {
539 ret->ref.ref.name = strdup(ref->ref.ref.name);
540 if (ret->ref.ref.name == NULL) {
541 free(ret);
542 return NULL;
544 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
545 sizeof(ret->ref.ref.sha1));
548 return ret;
551 const struct got_error *
552 got_reflist_entry_dup(struct got_reflist_entry **newp,
553 struct got_reflist_entry *re)
555 const struct got_error *err = NULL;
556 struct got_reflist_entry *new;
558 *newp = NULL;
560 new = malloc(sizeof(*new));
561 if (new == NULL)
562 return got_error_from_errno("malloc");
564 new->ref = got_ref_dup(re->ref);
565 if (new->ref == NULL) {
566 err = got_error_from_errno("got_ref_dup");
567 free(new);
568 return err;
571 *newp = new;
572 return NULL;
575 static const struct got_error *
576 resolve_symbolic_ref(struct got_reference **resolved,
577 struct got_repository *repo, struct got_reference *ref)
579 struct got_reference *nextref;
580 const struct got_error *err;
582 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
583 if (err)
584 return err;
586 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
587 err = resolve_symbolic_ref(resolved, repo, nextref);
588 else
589 *resolved = got_ref_dup(nextref);
591 got_ref_close(nextref);
592 return err;
595 static const struct got_error *
596 ref_resolve(struct got_object_id **id, struct got_repository *repo,
597 struct got_reference *ref, int recursion)
599 const struct got_error *err;
601 if (recursion <= 0)
602 return got_error_msg(GOT_ERR_RECURSION,
603 "reference recursion limit reached");
605 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
606 struct got_reference *resolved = NULL;
607 err = resolve_symbolic_ref(&resolved, repo, ref);
608 if (err == NULL)
609 err = ref_resolve(id, repo, resolved, --recursion);
610 if (resolved)
611 got_ref_close(resolved);
612 return err;
615 *id = calloc(1, sizeof(**id));
616 if (*id == NULL)
617 return got_error_from_errno("calloc");
618 memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
619 return NULL;
622 const struct got_error *
623 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
624 struct got_reference *ref)
626 return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
629 char *
630 got_ref_to_str(struct got_reference *ref)
632 char *str;
634 if (ref->flags & GOT_REF_IS_SYMBOLIC)
635 return strdup(ref->ref.symref.ref);
637 str = malloc(SHA1_DIGEST_STRING_LENGTH);
638 if (str == NULL)
639 return NULL;
641 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
642 SHA1_DIGEST_STRING_LENGTH) == NULL) {
643 free(str);
644 return NULL;
647 return str;
650 const char *
651 got_ref_get_name(struct got_reference *ref)
653 if (ref->flags & GOT_REF_IS_SYMBOLIC)
654 return ref->ref.symref.name;
656 return ref->ref.ref.name;
659 const char *
660 got_ref_get_symref_target(struct got_reference *ref)
662 if (ref->flags & GOT_REF_IS_SYMBOLIC)
663 return ref->ref.symref.ref;
665 return NULL;
668 const struct got_error *
669 got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
670 struct got_reference* re2)
672 const char *name1 = got_ref_get_name(re1);
673 const char *name2 = got_ref_get_name(re2);
675 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
676 return NULL;
679 const struct got_error *
680 got_ref_cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
681 struct got_reference *ref2)
683 const struct got_error *err = NULL;
684 struct got_repository *repo = arg;
685 struct got_object_id *id1, *id2 = NULL;
686 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
687 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
688 time_t time1, time2;
690 *cmp = 0;
692 err = got_ref_resolve(&id1, repo, ref1);
693 if (err)
694 return err;
695 err = got_object_open_as_tag(&tag1, repo, id1);
696 if (err) {
697 if (err->code != GOT_ERR_OBJ_TYPE)
698 goto done;
699 /* "lightweight" tag */
700 err = got_object_open_as_commit(&commit1, repo, id1);
701 if (err)
702 goto done;
703 time1 = got_object_commit_get_committer_time(commit1);
704 } else
705 time1 = got_object_tag_get_tagger_time(tag1);
707 err = got_ref_resolve(&id2, repo, ref2);
708 if (err)
709 goto done;
710 err = got_object_open_as_tag(&tag2, repo, id2);
711 if (err) {
712 if (err->code != GOT_ERR_OBJ_TYPE)
713 goto done;
714 /* "lightweight" tag */
715 err = got_object_open_as_commit(&commit2, repo, id2);
716 if (err)
717 goto done;
718 time2 = got_object_commit_get_committer_time(commit2);
719 } else
720 time2 = got_object_tag_get_tagger_time(tag2);
722 /* Put latest tags first. */
723 if (time1 < time2)
724 *cmp = 1;
725 else if (time1 > time2)
726 *cmp = -1;
727 else
728 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
729 done:
730 free(id1);
731 free(id2);
732 if (tag1)
733 got_object_tag_close(tag1);
734 if (tag2)
735 got_object_tag_close(tag2);
736 if (commit1)
737 got_object_commit_close(commit1);
738 if (commit2)
739 got_object_commit_close(commit2);
740 return err;
743 static const struct got_error *
744 insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
745 struct got_reference *ref, struct got_repository *repo,
746 got_ref_cmp_cb cmp_cb, void *cmp_arg)
748 const struct got_error *err;
749 struct got_reflist_entry *new, *re;
750 int cmp;
752 *newp = NULL;
754 new = malloc(sizeof(*new));
755 if (new == NULL)
756 return got_error_from_errno("malloc");
757 new->ref = ref;
758 *newp = new;
760 /*
761 * We must de-duplicate entries on insert because packed-refs may
762 * contain redundant entries. On-disk refs take precedence.
763 * This code assumes that on-disk revs are read before packed-refs.
764 * We're iterating the list anyway, so insert elements sorted by name.
766 * Many callers will provide paths in a somewhat sorted order.
767 * Iterating backwards from the tail of the list should be more
768 * efficient than traversing through the entire list each time
769 * an element is inserted.
770 */
771 re = TAILQ_LAST(refs, got_reflist_head);
772 while (re) {
773 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
774 if (err)
775 return err;
776 if (cmp == 0) {
777 /* duplicate */
778 free(new);
779 *newp = NULL;
780 return NULL;
781 } else if (cmp < 0) {
782 TAILQ_INSERT_AFTER(refs, re, new, entry);
783 return NULL;
785 re = TAILQ_PREV(re, got_reflist_head, entry);
788 TAILQ_INSERT_HEAD(refs, new, entry);
789 return NULL;
792 static const struct got_error *
793 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
794 const char *subdir, struct got_repository *repo,
795 got_ref_cmp_cb cmp_cb, void *cmp_arg)
797 const struct got_error *err = NULL;
798 DIR *d = NULL;
799 char *path_subdir;
801 while (subdir[0] == '/')
802 subdir++;
804 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
805 return got_error_from_errno("asprintf");
807 d = opendir(path_subdir);
808 if (d == NULL)
809 goto done;
811 for (;;) {
812 struct dirent *dent;
813 struct got_reference *ref;
814 char *child;
815 int type;
817 dent = readdir(d);
818 if (dent == NULL)
819 break;
821 if (strcmp(dent->d_name, ".") == 0 ||
822 strcmp(dent->d_name, "..") == 0)
823 continue;
825 err = got_path_dirent_type(&type, path_subdir, dent);
826 if (err)
827 break;
829 switch (type) {
830 case DT_REG:
831 err = open_ref(&ref, path_refs, subdir, dent->d_name,
832 0);
833 if (err)
834 goto done;
835 if (ref) {
836 struct got_reflist_entry *new;
837 err = insert_ref(&new, refs, ref, repo,
838 cmp_cb, cmp_arg);
839 if (err || new == NULL /* duplicate */)
840 got_ref_close(ref);
841 if (err)
842 goto done;
844 break;
845 case DT_DIR:
846 if (asprintf(&child, "%s%s%s", subdir,
847 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
848 err = got_error_from_errno("asprintf");
849 break;
851 err = gather_on_disk_refs(refs, path_refs, child, repo,
852 cmp_cb, cmp_arg);
853 free(child);
854 break;
855 default:
856 break;
859 done:
860 if (d)
861 closedir(d);
862 free(path_subdir);
863 return err;
866 const struct got_error *
867 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
868 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
870 const struct got_error *err;
871 char *packed_refs_path, *path_refs = NULL;
872 char *abs_namespace = NULL;
873 char *buf = NULL, *ondisk_ref_namespace = NULL;
874 FILE *f = NULL;
875 struct got_reference *ref;
876 struct got_reflist_entry *new;
878 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
879 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
880 if (path_refs == NULL) {
881 err = got_error_from_errno("get_refs_dir_path");
882 goto done;
884 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
885 if (err)
886 goto done;
887 err = insert_ref(&new, refs, ref, repo,
888 cmp_cb, cmp_arg);
889 if (err || new == NULL /* duplicate */)
890 got_ref_close(ref);
891 if (err && err->code != GOT_ERR_NOT_REF)
892 goto done;
893 } else {
894 /* Try listing a single reference. */
895 const char *refname = ref_namespace;
896 path_refs = get_refs_dir_path(repo, refname);
897 if (path_refs == NULL) {
898 err = got_error_from_errno("get_refs_dir_path");
899 goto done;
901 err = open_ref(&ref, path_refs, "", refname, 0);
902 if (err) {
903 if (err->code != GOT_ERR_NOT_REF)
904 goto done;
905 /* Try to look up references in a given namespace. */
906 } else {
907 err = insert_ref(&new, refs, ref, repo,
908 cmp_cb, cmp_arg);
909 if (err || new == NULL /* duplicate */)
910 got_ref_close(ref);
911 return err;
915 if (ref_namespace) {
916 size_t len;
917 /* Canonicalize the path to eliminate double-slashes if any. */
918 if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
919 err = got_error_from_errno("asprintf");
920 goto done;
922 len = strlen(abs_namespace) + 1;
923 buf = malloc(len);
924 if (buf == NULL) {
925 err = got_error_from_errno("malloc");
926 goto done;
928 err = got_canonpath(abs_namespace, buf, len);
929 if (err)
930 goto done;
931 ondisk_ref_namespace = buf;
932 while (ondisk_ref_namespace[0] == '/')
933 ondisk_ref_namespace++;
934 if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
935 ondisk_ref_namespace += 5;
936 else if (strcmp(ondisk_ref_namespace, "refs") == 0)
937 ondisk_ref_namespace = "";
940 /* Gather on-disk refs before parsing packed-refs. */
941 free(path_refs);
942 path_refs = get_refs_dir_path(repo, "");
943 if (path_refs == NULL) {
944 err = got_error_from_errno("get_refs_dir_path");
945 goto done;
947 err = gather_on_disk_refs(refs, path_refs,
948 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
949 cmp_cb, cmp_arg);
950 if (err)
951 goto done;
953 /*
954 * The packed-refs file may contain redundant entries, in which
955 * case on-disk refs take precedence.
956 */
957 packed_refs_path = got_repo_get_path_packed_refs(repo);
958 if (packed_refs_path == NULL) {
959 err = got_error_from_errno("got_repo_get_path_packed_refs");
960 goto done;
963 f = fopen(packed_refs_path, "r");
964 free(packed_refs_path);
965 if (f) {
966 char *line;
967 size_t len;
968 const char delim[3] = {'\0', '\0', '\0'};
969 for (;;) {
970 line = fparseln(f, &len, NULL, delim, 0);
971 if (line == NULL) {
972 if (feof(f))
973 break;
974 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
975 goto done;
977 err = parse_packed_ref_line(&ref, NULL, line);
978 free(line);
979 if (err)
980 goto done;
981 if (ref) {
982 if (ref_namespace) {
983 const char *name;
984 name = got_ref_get_name(ref);
985 if (!got_path_is_child(name,
986 ref_namespace,
987 strlen(ref_namespace))) {
988 got_ref_close(ref);
989 continue;
992 err = insert_ref(&new, refs, ref, repo,
993 cmp_cb, cmp_arg);
994 if (err || new == NULL /* duplicate */)
995 got_ref_close(ref);
996 if (err)
997 goto done;
1001 done:
1002 free(abs_namespace);
1003 free(buf);
1004 free(path_refs);
1005 if (f && fclose(f) != 0 && err == NULL)
1006 err = got_error_from_errno("fclose");
1007 return err;
1010 void
1011 got_ref_list_free(struct got_reflist_head *refs)
1013 struct got_reflist_entry *re;
1015 while ((re = TAILQ_FIRST(refs))) {
1016 TAILQ_REMOVE(refs, re, entry);
1017 free(re);
1022 int
1023 got_ref_is_symbolic(struct got_reference *ref)
1025 return (ref->flags & GOT_REF_IS_SYMBOLIC);
1028 const struct got_error *
1029 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1031 if (ref->flags & GOT_REF_IS_SYMBOLIC)
1032 return got_error(GOT_ERR_BAD_REF_TYPE);
1034 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
1035 return NULL;
1038 const struct got_error *
1039 got_ref_change_symref(struct got_reference *ref, const char *refname)
1041 char *new_name;
1043 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1044 return got_error(GOT_ERR_BAD_REF_TYPE);
1046 new_name = strdup(refname);
1047 if (new_name == NULL)
1048 return got_error_from_errno("strdup");
1050 free(ref->ref.symref.ref);
1051 ref->ref.symref.ref = new_name;
1052 return NULL;
1055 const struct got_error *
1056 got_ref_change_symref_to_ref(struct got_reference *symref,
1057 struct got_object_id *id)
1059 if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1060 return got_error(GOT_ERR_BAD_REF_TYPE);
1062 symref->ref.ref.name = symref->ref.symref.name;
1063 memcpy(symref->ref.ref.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1064 symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1065 return NULL;
1068 const struct got_error *
1069 got_ref_write(struct got_reference *ref, struct got_repository *repo)
1071 const struct got_error *err = NULL, *unlock_err = NULL;
1072 const char *name = got_ref_get_name(ref);
1073 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1074 struct got_lockfile *lf = NULL;
1075 FILE *f = NULL;
1076 size_t n;
1077 struct stat sb;
1079 path_refs = get_refs_dir_path(repo, name);
1080 if (path_refs == NULL) {
1081 err = got_error_from_errno2("get_refs_dir_path", name);
1082 goto done;
1085 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1086 err = got_error_from_errno("asprintf");
1087 goto done;
1090 err = got_opentemp_named(&tmppath, &f, path);
1091 if (err) {
1092 char *parent;
1093 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1094 goto done;
1095 err = got_path_dirname(&parent, path);
1096 if (err)
1097 goto done;
1098 err = got_path_mkdir(parent);
1099 free(parent);
1100 if (err)
1101 goto done;
1102 err = got_opentemp_named(&tmppath, &f, path);
1103 if (err)
1104 goto done;
1107 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1108 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1109 if (n != strlen(ref->ref.symref.ref) + 6) {
1110 err = got_ferror(f, GOT_ERR_IO);
1111 goto done;
1113 } else {
1114 char hex[SHA1_DIGEST_STRING_LENGTH];
1115 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
1116 sizeof(hex)) == NULL) {
1117 err = got_error(GOT_ERR_BAD_REF_DATA);
1118 goto done;
1120 n = fprintf(f, "%s\n", hex);
1121 if (n != sizeof(hex)) {
1122 err = got_ferror(f, GOT_ERR_IO);
1123 goto done;
1127 if (ref->lf == NULL) {
1128 err = got_lockfile_lock(&lf, path);
1129 if (err)
1130 goto done;
1133 /* XXX: check if old content matches our expectations? */
1135 if (stat(path, &sb) != 0) {
1136 if (errno != ENOENT) {
1137 err = got_error_from_errno2("stat", path);
1138 goto done;
1140 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1143 if (fchmod(fileno(f), sb.st_mode) != 0) {
1144 err = got_error_from_errno2("fchmod", tmppath);
1145 goto done;
1148 if (rename(tmppath, path) != 0) {
1149 err = got_error_from_errno3("rename", tmppath, path);
1150 goto done;
1152 free(tmppath);
1153 tmppath = NULL;
1154 done:
1155 if (ref->lf == NULL && lf)
1156 unlock_err = got_lockfile_unlock(lf);
1157 if (f) {
1158 if (fclose(f) != 0 && err == NULL)
1159 err = got_error_from_errno("fclose");
1161 free(path_refs);
1162 free(path);
1163 if (tmppath) {
1164 if (unlink(tmppath) != 0 && err == NULL)
1165 err = got_error_from_errno2("unlink", tmppath);
1166 free(tmppath);
1168 return err ? err : unlock_err;
1171 static const struct got_error *
1172 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1174 const struct got_error *err = NULL, *unlock_err = NULL;
1175 struct got_lockfile *lf = NULL;
1176 FILE *f = NULL, *tmpf = NULL;
1177 char *packed_refs_path, *tmppath = NULL;
1178 struct got_reflist_head refs;
1179 int found_delref = 0;
1181 /* The packed-refs file does not cotain symbolic references. */
1182 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1183 return got_error(GOT_ERR_BAD_REF_DATA);
1185 TAILQ_INIT(&refs);
1187 packed_refs_path = got_repo_get_path_packed_refs(repo);
1188 if (packed_refs_path == NULL)
1189 return got_error_from_errno("got_repo_get_path_packed_refs");
1191 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
1192 if (err)
1193 goto done;
1195 if (delref->lf == NULL) {
1196 err = got_lockfile_lock(&lf, packed_refs_path);
1197 if (err)
1198 goto done;
1201 f = fopen(packed_refs_path, "r");
1202 if (f == NULL) {
1203 err = got_error_from_errno2("fopen", packed_refs_path);
1204 goto done;
1206 for (;;) {
1207 char *line;
1208 size_t len;
1209 const char delim[3] = {'\0', '\0', '\0'};
1210 struct got_reference *ref;
1211 struct got_reflist_entry *new;
1213 line = fparseln(f, &len, NULL, delim, 0);
1214 if (line == NULL) {
1215 if (feof(f))
1216 break;
1217 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1218 goto done;
1220 err = parse_packed_ref_line(&ref, NULL, line);
1221 free(line);
1222 if (err)
1223 goto done;
1224 if (ref == NULL)
1225 continue;
1227 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1228 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1229 sizeof(delref->ref.ref.sha1)) == 0) {
1230 found_delref = 1;
1231 got_ref_close(ref);
1232 continue;
1235 err = insert_ref(&new, &refs, ref, repo,
1236 got_ref_cmp_by_name, NULL);
1237 if (err || new == NULL /* duplicate */)
1238 got_ref_close(ref);
1239 if (err)
1240 goto done;
1243 if (found_delref) {
1244 struct got_reflist_entry *re;
1245 size_t n;
1246 struct stat sb;
1248 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1249 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1250 err = got_ferror(f, GOT_ERR_IO);
1251 goto done;
1254 TAILQ_FOREACH(re, &refs, entry) {
1255 uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
1257 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1258 sizeof(hex)) == NULL) {
1259 err = got_error(GOT_ERR_BAD_REF_DATA);
1260 goto done;
1262 n = fprintf(tmpf, "%s ", hex);
1263 if (n != sizeof(hex)) {
1264 err = got_ferror(f, GOT_ERR_IO);
1265 goto done;
1267 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1268 if (n != strlen(re->ref->ref.ref.name) + 1) {
1269 err = got_ferror(f, GOT_ERR_IO);
1270 goto done;
1274 if (fflush(tmpf) != 0) {
1275 err = got_error_from_errno("fflush");
1276 goto done;
1279 if (fstat(fileno(f), &sb) != 0) {
1280 if (errno != ENOENT) {
1281 err = got_error_from_errno2("fstat",
1282 packed_refs_path);
1283 goto done;
1285 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1288 if (fchmod(fileno(tmpf), sb.st_mode) != 0) {
1289 err = got_error_from_errno2("fchmod", tmppath);
1290 goto done;
1293 if (rename(tmppath, packed_refs_path) != 0) {
1294 err = got_error_from_errno3("rename", tmppath,
1295 packed_refs_path);
1296 goto done;
1299 done:
1300 if (delref->lf == NULL && lf)
1301 unlock_err = got_lockfile_unlock(lf);
1302 if (f) {
1303 if (fclose(f) != 0 && err == NULL)
1304 err = got_error_from_errno("fclose");
1306 if (tmpf) {
1307 unlink(tmppath);
1308 if (fclose(tmpf) != 0 && err == NULL)
1309 err = got_error_from_errno("fclose");
1311 free(tmppath);
1312 free(packed_refs_path);
1313 got_ref_list_free(&refs);
1314 return err ? err : unlock_err;
1317 static const struct got_error *
1318 delete_loose_ref(struct got_reference *ref, struct got_repository *repo)
1320 const struct got_error *err = NULL, *unlock_err = NULL;
1321 const char *name = got_ref_get_name(ref);
1322 char *path_refs = NULL, *path = NULL;
1323 struct got_lockfile *lf = NULL;
1325 path_refs = get_refs_dir_path(repo, name);
1326 if (path_refs == NULL) {
1327 err = got_error_from_errno2("get_refs_dir_path", name);
1328 goto done;
1331 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1332 err = got_error_from_errno("asprintf");
1333 goto done;
1336 if (ref->lf == NULL) {
1337 err = got_lockfile_lock(&lf, path);
1338 if (err)
1339 goto done;
1342 /* XXX: check if old content matches our expectations? */
1344 if (unlink(path) != 0)
1345 err = got_error_from_errno2("unlink", path);
1346 done:
1347 if (ref->lf == NULL && lf)
1348 unlock_err = got_lockfile_unlock(lf);
1350 free(path_refs);
1351 free(path);
1352 return err ? err : unlock_err;
1355 const struct got_error *
1356 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1358 const struct got_error *err = NULL;
1359 struct got_reference *ref2;
1361 if (ref->flags & GOT_REF_IS_PACKED) {
1362 err = delete_packed_ref(ref, repo);
1363 if (err)
1364 return err;
1366 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1367 if (err) {
1368 if (err->code == GOT_ERR_NOT_REF)
1369 return NULL;
1370 return err;
1373 err = delete_loose_ref(ref2, repo);
1374 got_ref_close(ref2);
1375 return err;
1376 } else {
1377 err = delete_loose_ref(ref, repo);
1378 if (err)
1379 return err;
1381 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1382 if (err) {
1383 if (err->code == GOT_ERR_NOT_REF)
1384 return NULL;
1385 return err;
1388 err = delete_packed_ref(ref2, repo);
1389 got_ref_close(ref2);
1390 return err;
1394 const struct got_error *
1395 got_ref_unlock(struct got_reference *ref)
1397 const struct got_error *err;
1398 err = got_lockfile_unlock(ref->lf);
1399 ref->lf = NULL;
1400 return err;
1403 struct got_reflist_object_id_map {
1404 struct got_object_idset *idset;
1407 struct got_reflist_object_id_map_entry {
1408 struct got_reflist_head refs;
1411 static const struct got_error *
1412 add_object_id_map_entry(struct got_object_idset *idset,
1413 struct got_object_id *id, struct got_reflist_entry *re)
1415 const struct got_error *err = NULL;
1416 struct got_reflist_object_id_map_entry *ent;
1417 struct got_reflist_entry *new;
1419 ent = got_object_idset_get(idset, id);
1420 if (ent == NULL) {
1421 ent = malloc(sizeof(*ent));
1422 if (ent == NULL)
1423 return got_error_from_errno("malloc");
1425 TAILQ_INIT(&ent->refs);
1426 err = got_object_idset_add(idset, id, ent);
1427 if (err)
1428 return err;
1431 err = got_reflist_entry_dup(&new, re);
1432 if (err)
1433 return err;
1435 TAILQ_INSERT_TAIL(&ent->refs, new, entry);
1436 return NULL;
1439 const struct got_error *
1440 got_reflist_object_id_map_create(struct got_reflist_object_id_map **map,
1441 struct got_reflist_head *refs, struct got_repository *repo)
1443 const struct got_error *err = NULL;
1444 struct got_object_idset *idset;
1445 struct got_object_id *id = NULL;
1446 struct got_reflist_entry *re;
1448 idset = got_object_idset_alloc();
1449 if (idset == NULL)
1450 return got_error_from_errno("got_object_idset_alloc");
1452 *map = malloc(sizeof(**map));
1453 if (*map == NULL) {
1454 got_object_idset_free(idset);
1455 return got_error_from_errno("malloc");
1457 (*map)->idset = idset;
1459 TAILQ_FOREACH(re, refs, entry) {
1460 struct got_tag_object *tag = NULL;
1462 err = got_ref_resolve(&id, repo, re->ref);
1463 if (err)
1464 goto done;
1466 err = add_object_id_map_entry(idset, id, re);
1467 if (err)
1468 goto done;
1470 if (strstr(got_ref_get_name(re->ref), "/tags/") == NULL) {
1471 free(id);
1472 id = NULL;
1473 continue;
1476 err = got_object_open_as_tag(&tag, repo, id);
1477 if (err) {
1478 if (err->code != GOT_ERR_OBJ_TYPE)
1479 goto done;
1480 /* Ref points at something other than a tag. */
1481 err = NULL;
1482 tag = NULL;
1483 free(id);
1484 id = NULL;
1485 continue;
1488 err = add_object_id_map_entry(idset,
1489 got_object_tag_get_object_id(tag), re);
1490 got_object_tag_close(tag);
1491 if (err)
1492 goto done;
1494 free(id);
1495 id = NULL;
1497 done:
1498 free(id);
1499 if (err) {
1500 got_reflist_object_id_map_free(*map);
1501 *map = NULL;
1503 return err;
1506 struct got_reflist_head *
1507 got_reflist_object_id_map_lookup(struct got_reflist_object_id_map *map,
1508 struct got_object_id *id)
1510 struct got_reflist_object_id_map_entry *ent;
1511 ent = got_object_idset_get(map->idset, id);
1512 if (ent)
1513 return &ent->refs;
1514 return NULL;
1517 static const struct got_error *
1518 free_id_map_entry(struct got_object_id *id, void *data, void *arg)
1520 struct got_reflist_object_id_map_entry *ent = data;
1522 got_ref_list_free(&ent->refs);
1523 free(ent);
1524 return NULL;
1527 void
1528 got_reflist_object_id_map_free(struct got_reflist_object_id_map *map)
1530 got_object_idset_for_each(map->idset, free_id_map_entry, NULL);
1531 got_object_idset_free(map->idset);
1532 free(map);