2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 #include <sys/types.h>
18 #include <sys/queue.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"
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"
50 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
53 #define GOT_REF_HEADS "heads"
54 #define GOT_REF_TAGS "tags"
55 #define GOT_REF_REMOTES "remotes"
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.
62 #define GOT_PACKED_REFS_HEADER "# pack-refs with:"
64 /* A symbolic reference. */
70 #define GOT_REF_RECURSE_MAX 20
72 /* A non-symbolic reference (there is no better designation). */
75 u_int8_t sha1[SHA1_DIGEST_LENGTH];
78 /* A reference which points to an arbitrary object. */
79 struct got_reference {
81 #define GOT_REF_IS_SYMBOLIC 0x01
82 #define GOT_REF_IS_PACKED 0x02
86 struct got_symref symref;
89 struct got_lockfile *lf;
92 /* Cached timestamp for got_ref_cmp_by_commit_timestamp_descending() */
93 time_t committer_time;
96 static const struct got_error *
97 alloc_ref(struct got_reference **ref, const char *name,
98 struct got_object_id *id, int flags, time_t mtime)
100 const struct got_error *err = NULL;
102 *ref = calloc(1, sizeof(**ref));
104 return got_error_from_errno("calloc");
106 memcpy((*ref)->ref.ref.sha1, id->sha1, sizeof((*ref)->ref.ref.sha1));
107 (*ref)->flags = flags;
108 (*ref)->ref.ref.name = strdup(name);
109 (*ref)->mtime = mtime;
110 if ((*ref)->ref.ref.name == NULL) {
111 err = got_error_from_errno("strdup");
118 static const struct got_error *
119 alloc_symref(struct got_reference **ref, const char *name,
120 const char *target_ref, int flags)
122 const struct got_error *err = NULL;
124 *ref = calloc(1, sizeof(**ref));
126 return got_error_from_errno("calloc");
128 (*ref)->flags = GOT_REF_IS_SYMBOLIC | flags;
129 (*ref)->ref.symref.name = strdup(name);
130 if ((*ref)->ref.symref.name == NULL) {
131 err = got_error_from_errno("strdup");
136 (*ref)->ref.symref.ref = strdup(target_ref);
137 if ((*ref)->ref.symref.ref == NULL) {
138 err = got_error_from_errno("strdup");
145 static const struct got_error *
146 parse_symref(struct got_reference **ref, const char *name, const char *line)
149 return got_error(GOT_ERR_BAD_REF_DATA);
151 return alloc_symref(ref, name, line, 0);
154 static const struct got_error *
155 parse_ref_line(struct got_reference **ref, const char *name, const char *line,
158 struct got_object_id id;
160 if (strncmp(line, "ref: ", 5) == 0) {
162 return parse_symref(ref, name, line);
165 if (!got_parse_sha1_digest(id.sha1, line))
166 return got_error(GOT_ERR_BAD_REF_DATA);
168 return alloc_ref(ref, name, &id, 0, mtime);
171 static const struct got_error *
172 parse_ref_file(struct got_reference **ref, const char *name,
173 const char *absname, const char *abspath, int lock)
175 const struct got_error *err = NULL;
180 struct got_lockfile *lf = NULL;
184 err = got_lockfile_lock(&lf, abspath, -1);
186 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
187 err = got_error_not_ref(name);
192 f = fopen(abspath, "rbe");
194 if (errno != ENOTDIR && errno != ENOENT)
195 err = got_error_from_errno2("fopen", abspath);
197 err = got_error_not_ref(name);
199 got_lockfile_unlock(lf, -1);
202 if (fstat(fileno(f), &sb) == -1) {
203 err = got_error_from_errno2("fstat", abspath);
207 linelen = getline(&line, &linesize, f);
210 err = NULL; /* ignore empty files (could be locks) */
213 err = got_error(GOT_ERR_NOT_REF);
215 err = got_ferror(f, GOT_ERR_IO);
217 err = got_error_from_errno2("getline", abspath);
220 got_lockfile_unlock(lf, -1);
223 while (linelen > 0 && line[linelen - 1] == '\n') {
224 line[linelen - 1] = '\0';
228 err = parse_ref_line(ref, absname, line, sb.st_mtime);
231 got_lockfile_unlock(lf, -1);
236 got_lockfile_unlock(lf, -1);
241 if (fclose(f) == EOF && err == NULL) {
242 err = got_error_from_errno("fclose");
245 got_ref_unlock(*ref);
254 is_well_known_ref(const char *refname)
256 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
257 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
258 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
259 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
263 get_refs_dir_path(struct got_repository *repo, const char *refname)
265 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
266 return strdup(got_repo_get_path_git_dir(repo));
268 return got_repo_get_path_refs(repo);
272 got_ref_name_is_valid(const char *name)
275 const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
276 const char *forbidden_seq[] = { "//", "..", "@{" };
277 const char *lfs = GOT_LOCKFILE_SUFFIX;
278 const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
281 if (name[0] == '@' && name[1] == '\0')
286 if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
289 for (i = 0; i < nitems(forbidden); i++) {
290 if (*s == forbidden[i])
293 for (i = 0; i < nitems(forbidden_seq); i++) {
294 if (s[0] == forbidden_seq[i][0] &&
295 s[1] == forbidden_seq[i][1])
298 if (iscntrl((unsigned char)s[0]))
300 if (s[0] == '.' && s[1] == '\0')
303 const char *nextseg = s + 1;
304 if (nextseg[0] == '\0' || nextseg[0] == '.' ||
307 if (seg <= s - lfs_len &&
308 strncmp(s - lfs_len, lfs, lfs_len) == 0)
315 if (seg <= s - lfs_len &&
316 strncmp(s - lfs_len, lfs, lfs_len) == 0)
322 const struct got_error *
323 got_ref_alloc(struct got_reference **ref, const char *name,
324 struct got_object_id *id)
326 if (!got_ref_name_is_valid(name))
327 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
329 return alloc_ref(ref, name, id, 0, 0);
332 const struct got_error *
333 got_ref_alloc_symref(struct got_reference **ref, const char *name,
334 struct got_reference *target_ref)
336 if (!got_ref_name_is_valid(name))
337 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
339 return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
342 static const struct got_error *
343 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
344 const char *line, time_t mtime)
346 struct got_object_id id;
351 if (line[0] == '#' || line[0] == '^')
354 if (!got_parse_sha1_digest(id.sha1, line))
355 return got_error(GOT_ERR_BAD_REF_DATA);
358 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
362 name = line + SHA1_DIGEST_STRING_LENGTH;
364 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED, mtime);
367 static const struct got_error *
368 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
369 int nsubdirs, const char *refname, time_t mtime)
371 const struct got_error *err = NULL;
376 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
381 abs_refname = (char *)refname;
383 linelen = getline(&line, &linesize, f);
387 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
390 if (linelen > 0 && line[linelen - 1] == '\n')
391 line[linelen - 1] = '\0';
392 for (i = 0; i < nsubdirs; i++) {
393 if (!ref_is_absolute &&
394 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
396 return got_error_from_errno("asprintf");
397 err = parse_packed_ref_line(ref, abs_refname, line,
399 if (!ref_is_absolute)
401 if (err || *ref != NULL)
406 } while (*ref == NULL);
412 static const struct got_error *
413 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
414 const char *name, int lock)
416 const struct got_error *err = NULL;
418 char *absname = NULL;
419 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
420 int ref_is_well_known = (subdir[0] == '\0' && is_well_known_ref(name));
424 if (!got_ref_name_is_valid(name))
425 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
427 if (ref_is_absolute || ref_is_well_known) {
428 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
429 return got_error_from_errno("asprintf");
430 absname = (char *)name;
432 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
433 subdir[0] ? "/" : "", name) == -1)
434 return got_error_from_errno("asprintf");
436 if (asprintf(&absname, "refs/%s%s%s",
437 subdir, subdir[0] ? "/" : "", name) == -1) {
438 err = got_error_from_errno("asprintf");
443 err = parse_ref_file(ref, name, absname, path, lock);
445 if (!ref_is_absolute && !ref_is_well_known)
451 const struct got_error *
452 got_ref_open(struct got_reference **ref, struct got_repository *repo,
453 const char *refname, int lock)
455 const struct got_error *err = NULL;
456 char *packed_refs_path = NULL, *path_refs = NULL;
457 const char *subdirs[] = {
458 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
461 int well_known = is_well_known_ref(refname);
462 struct got_lockfile *lf = NULL;
466 path_refs = get_refs_dir_path(repo, refname);
467 if (path_refs == NULL) {
468 err = got_error_from_errno2("get_refs_dir_path", refname);
473 err = open_ref(ref, path_refs, "", refname, lock);
477 /* Search on-disk refs before packed refs! */
478 for (i = 0; i < nitems(subdirs); i++) {
479 err = open_ref(ref, path_refs, subdirs[i], refname,
481 if ((err && err->code != GOT_ERR_NOT_REF) || *ref)
485 packed_refs_path = got_repo_get_path_packed_refs(repo);
486 if (packed_refs_path == NULL) {
487 err = got_error_from_errno(
488 "got_repo_get_path_packed_refs");
493 err = got_lockfile_lock(&lf, packed_refs_path, -1);
497 f = fopen(packed_refs_path, "rbe");
500 if (fstat(fileno(f), &sb) == -1) {
501 err = got_error_from_errno2("fstat",
505 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
506 refname, sb.st_mtime);
508 if (fclose(f) == EOF) {
509 err = got_error_from_errno("fclose");
518 if (!err && *ref == NULL)
519 err = got_error_not_ref(refname);
521 got_lockfile_unlock(lf, -1);
522 free(packed_refs_path);
528 got_ref_close(struct got_reference *ref)
530 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
531 free(ref->ref.symref.name);
532 free(ref->ref.symref.ref);
534 free(ref->ref.ref.name);
538 struct got_reference *
539 got_ref_dup(struct got_reference *ref)
541 struct got_reference *ret;
543 ret = calloc(1, sizeof(*ret));
547 ret->flags = ref->flags;
548 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
549 ret->ref.symref.name = strdup(ref->ref.symref.name);
550 if (ret->ref.symref.name == NULL) {
554 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
555 if (ret->ref.symref.ref == NULL) {
556 free(ret->ref.symref.name);
561 ret->ref.ref.name = strdup(ref->ref.ref.name);
562 if (ret->ref.ref.name == NULL) {
566 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
567 sizeof(ret->ref.ref.sha1));
573 const struct got_error *
574 got_reflist_entry_dup(struct got_reflist_entry **newp,
575 struct got_reflist_entry *re)
577 const struct got_error *err = NULL;
578 struct got_reflist_entry *new;
582 new = malloc(sizeof(*new));
584 return got_error_from_errno("malloc");
586 new->ref = got_ref_dup(re->ref);
587 if (new->ref == NULL) {
588 err = got_error_from_errno("got_ref_dup");
597 const struct got_error *
598 got_ref_resolve_symbolic(struct got_reference **resolved,
599 struct got_repository *repo, struct got_reference *ref)
601 struct got_reference *nextref;
602 const struct got_error *err;
604 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
608 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
609 err = got_ref_resolve_symbolic(resolved, repo, nextref);
611 *resolved = got_ref_dup(nextref);
613 got_ref_close(nextref);
617 static const struct got_error *
618 ref_resolve(struct got_object_id **id, struct got_repository *repo,
619 struct got_reference *ref, int recursion)
621 const struct got_error *err;
624 return got_error_msg(GOT_ERR_RECURSION,
625 "reference recursion limit reached");
627 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
628 struct got_reference *resolved = NULL;
629 err = got_ref_resolve_symbolic(&resolved, repo, ref);
631 err = ref_resolve(id, repo, resolved, --recursion);
633 got_ref_close(resolved);
637 *id = calloc(1, sizeof(**id));
639 return got_error_from_errno("calloc");
640 memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
644 const struct got_error *
645 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
646 struct got_reference *ref)
648 return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
652 got_ref_to_str(struct got_reference *ref)
656 if (ref->flags & GOT_REF_IS_SYMBOLIC)
657 return strdup(ref->ref.symref.ref);
659 str = malloc(SHA1_DIGEST_STRING_LENGTH);
663 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
664 SHA1_DIGEST_STRING_LENGTH) == NULL) {
673 got_ref_get_name(struct got_reference *ref)
675 if (ref->flags & GOT_REF_IS_SYMBOLIC)
676 return ref->ref.symref.name;
678 return ref->ref.ref.name;
682 got_ref_get_symref_target(struct got_reference *ref)
684 if (ref->flags & GOT_REF_IS_SYMBOLIC)
685 return ref->ref.symref.ref;
691 got_ref_get_mtime(struct got_reference *ref)
696 const struct got_error *
697 got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
698 struct got_reference* re2)
700 const char *name1 = got_ref_get_name(re1);
701 const char *name2 = got_ref_get_name(re2);
703 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
707 const struct got_error *
708 got_ref_cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
709 struct got_reference *ref2)
711 const struct got_error *err = NULL;
712 struct got_repository *repo = arg;
713 struct got_object_id *id1, *id2 = NULL;
714 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
715 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
720 err = got_ref_resolve(&id1, repo, ref1);
723 err = got_object_open_as_tag(&tag1, repo, id1);
725 if (err->code != GOT_ERR_OBJ_TYPE)
727 /* "lightweight" tag */
728 err = got_object_open_as_commit(&commit1, repo, id1);
731 time1 = got_object_commit_get_committer_time(commit1);
733 time1 = got_object_tag_get_tagger_time(tag1);
735 err = got_ref_resolve(&id2, repo, ref2);
738 err = got_object_open_as_tag(&tag2, repo, id2);
740 if (err->code != GOT_ERR_OBJ_TYPE)
742 /* "lightweight" tag */
743 err = got_object_open_as_commit(&commit2, repo, id2);
746 time2 = got_object_commit_get_committer_time(commit2);
748 time2 = got_object_tag_get_tagger_time(tag2);
750 /* Put latest tags first. */
753 else if (time1 > time2)
756 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
761 got_object_tag_close(tag1);
763 got_object_tag_close(tag2);
765 got_object_commit_close(commit1);
767 got_object_commit_close(commit2);
771 static const struct got_error *
772 get_committer_time(struct got_reference *ref, struct got_repository *repo)
774 const struct got_error *err = NULL;
776 struct got_commit_object *commit = NULL;
777 struct got_tag_object *tag = NULL;
778 struct got_object_id *id = NULL;
780 err = got_ref_resolve(&id, repo, ref);
784 err = got_object_get_type(&obj_type, repo, id);
789 case GOT_OBJ_TYPE_COMMIT:
790 err = got_object_open_as_commit(&commit, repo, id);
793 ref->committer_time =
794 got_object_commit_get_committer_time(commit);
796 case GOT_OBJ_TYPE_TAG:
797 err = got_object_open_as_tag(&tag, repo, id);
800 ref->committer_time = got_object_tag_get_tagger_time(tag);
803 /* best effort for other object types */
804 ref->committer_time = got_ref_get_mtime(ref);
810 got_object_commit_close(commit);
812 got_object_tag_close(tag);
816 const struct got_error *
817 got_ref_cmp_by_commit_timestamp_descending(void *arg, int *cmp,
818 struct got_reference *ref1, struct got_reference *ref2)
820 const struct got_error *err = NULL;
821 struct got_repository *repo = arg;
825 if (ref1->committer_time == 0) {
826 err = get_committer_time(ref1, repo);
830 if (ref2->committer_time == 0) {
831 err = get_committer_time(ref2, repo);
836 if (ref1->committer_time < ref2->committer_time)
838 else if (ref2->committer_time < ref1->committer_time)
841 return got_ref_cmp_by_name(arg, cmp, ref1, ref2);
846 const struct got_error *
847 got_reflist_insert(struct got_reflist_entry **newp,
848 struct got_reflist_head *refs, struct got_reference *ref,
849 got_ref_cmp_cb cmp_cb, void *cmp_arg)
851 const struct got_error *err;
852 struct got_reflist_entry *new, *re;
857 if (cmp_cb != got_ref_cmp_by_name && (ref->flags & GOT_REF_IS_PACKED)) {
859 * If we are not sorting elements by name then we must still
860 * detect collisions between a packed ref and an on-disk ref
861 * using the same name. On-disk refs take precedence and are
862 * already present on the list before packed refs get added.
864 TAILQ_FOREACH(re, refs, entry) {
865 err = got_ref_cmp_by_name(NULL, &cmp, re->ref, ref);
873 new = malloc(sizeof(*new));
875 return got_error_from_errno("malloc");
880 * We must de-duplicate entries on insert because packed-refs may
881 * contain redundant entries. On-disk refs take precedence.
882 * This code assumes that on-disk revs are read before packed-refs.
883 * We're iterating the list anyway, so insert elements sorted by name.
885 * Many callers will provide paths in a somewhat sorted order.
886 * Iterating backwards from the tail of the list should be more
887 * efficient than traversing through the entire list each time
888 * an element is inserted.
890 re = TAILQ_LAST(refs, got_reflist_head);
892 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
900 } else if (cmp < 0) {
901 TAILQ_INSERT_AFTER(refs, re, new, entry);
904 re = TAILQ_PREV(re, got_reflist_head, entry);
907 TAILQ_INSERT_HEAD(refs, new, entry);
911 const struct got_error *
912 got_reflist_sort(struct got_reflist_head *refs,
913 got_ref_cmp_cb cmp_cb, void *cmp_arg)
915 const struct got_error *err = NULL;
916 struct got_reflist_entry *re, *tmp, *new;
917 struct got_reflist_head sorted;
921 TAILQ_FOREACH_SAFE(re, refs, entry, tmp) {
922 struct got_reference *ref = re->ref;
923 TAILQ_REMOVE(refs, re, entry);
925 err = got_reflist_insert(&new, &sorted, ref, cmp_cb, cmp_arg);
926 if (err || new == NULL /* duplicate */)
932 TAILQ_CONCAT(refs, &sorted, entry);
936 static const struct got_error *
937 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
938 const char *subdir, struct got_repository *repo,
939 got_ref_cmp_cb cmp_cb, void *cmp_arg)
941 const struct got_error *err = NULL;
945 while (subdir[0] == '/')
948 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
949 return got_error_from_errno("asprintf");
951 d = opendir(path_subdir);
957 struct got_reference *ref;
965 if (strcmp(dent->d_name, ".") == 0 ||
966 strcmp(dent->d_name, "..") == 0)
969 err = got_path_dirent_type(&type, path_subdir, dent);
975 err = open_ref(&ref, path_refs, subdir, dent->d_name,
980 struct got_reflist_entry *new;
981 err = got_reflist_insert(&new, refs, ref,
983 if (err || new == NULL /* duplicate */)
990 if (asprintf(&child, "%s%s%s", subdir,
991 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
992 err = got_error_from_errno("asprintf");
995 err = gather_on_disk_refs(refs, path_refs, child, repo,
1010 const struct got_error *
1011 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
1012 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
1014 const struct got_error *err;
1015 char *packed_refs_path = NULL, *path_refs = NULL;
1016 char *abs_namespace = NULL, *buf = NULL;
1017 const char *ondisk_ref_namespace = NULL;
1020 struct got_reference *ref;
1021 struct got_reflist_entry *new;
1023 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
1024 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
1025 if (path_refs == NULL) {
1026 err = got_error_from_errno("get_refs_dir_path");
1029 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
1032 err = got_reflist_insert(&new, refs, ref, cmp_cb, cmp_arg);
1033 if (err || new == NULL /* duplicate */)
1035 if (err && err->code != GOT_ERR_NOT_REF)
1038 /* Try listing a single reference. */
1039 const char *refname = ref_namespace;
1040 path_refs = get_refs_dir_path(repo, refname);
1041 if (path_refs == NULL) {
1042 err = got_error_from_errno("get_refs_dir_path");
1045 err = open_ref(&ref, path_refs, "", refname, 0);
1047 if (err->code != GOT_ERR_NOT_REF)
1049 /* Try to look up references in a given namespace. */
1051 err = got_reflist_insert(&new, refs, ref,
1053 if (err || new == NULL /* duplicate */)
1059 if (ref_namespace) {
1061 /* Canonicalize the path to eliminate double-slashes if any. */
1062 if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
1063 err = got_error_from_errno("asprintf");
1066 len = strlen(abs_namespace) + 1;
1069 err = got_error_from_errno("malloc");
1072 err = got_canonpath(abs_namespace, buf, len);
1075 ondisk_ref_namespace = buf;
1076 while (ondisk_ref_namespace[0] == '/')
1077 ondisk_ref_namespace++;
1078 if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
1079 ondisk_ref_namespace += 5;
1080 else if (strcmp(ondisk_ref_namespace, "refs") == 0)
1081 ondisk_ref_namespace = "";
1084 /* Gather on-disk refs before parsing packed-refs. */
1086 path_refs = get_refs_dir_path(repo, "");
1087 if (path_refs == NULL) {
1088 err = got_error_from_errno("get_refs_dir_path");
1091 err = gather_on_disk_refs(refs, path_refs,
1092 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
1098 * The packed-refs file may contain redundant entries, in which
1099 * case on-disk refs take precedence.
1101 packed_refs_path = got_repo_get_path_packed_refs(repo);
1102 if (packed_refs_path == NULL) {
1103 err = got_error_from_errno("got_repo_get_path_packed_refs");
1107 f = fopen(packed_refs_path, "re");
1109 size_t linesize = 0;
1113 if (fstat(fileno(f), &sb) == -1) {
1114 err = got_error_from_errno2("fstat", packed_refs_path);
1118 linelen = getline(&line, &linesize, f);
1119 if (linelen == -1) {
1122 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1125 if (linelen > 0 && line[linelen - 1] == '\n')
1126 line[linelen - 1] = '\0';
1127 err = parse_packed_ref_line(&ref, NULL, line,
1132 if (ref_namespace) {
1134 name = got_ref_get_name(ref);
1135 if (!got_path_is_child(name,
1137 strlen(ref_namespace))) {
1142 err = got_reflist_insert(&new, refs, ref,
1144 if (err || new == NULL /* duplicate */)
1152 free(packed_refs_path);
1153 free(abs_namespace);
1157 if (f && fclose(f) == EOF && err == NULL)
1158 err = got_error_from_errno("fclose");
1163 got_ref_list_free(struct got_reflist_head *refs)
1165 struct got_reflist_entry *re;
1167 while ((re = TAILQ_FIRST(refs))) {
1168 TAILQ_REMOVE(refs, re, entry);
1169 got_ref_close(re->ref);
1176 got_ref_is_symbolic(struct got_reference *ref)
1178 return (ref->flags & GOT_REF_IS_SYMBOLIC);
1181 const struct got_error *
1182 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1184 if (ref->flags & GOT_REF_IS_SYMBOLIC)
1185 return got_error(GOT_ERR_BAD_REF_TYPE);
1187 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
1191 const struct got_error *
1192 got_ref_change_symref(struct got_reference *ref, const char *refname)
1196 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1197 return got_error(GOT_ERR_BAD_REF_TYPE);
1199 new_name = strdup(refname);
1200 if (new_name == NULL)
1201 return got_error_from_errno("strdup");
1203 free(ref->ref.symref.ref);
1204 ref->ref.symref.ref = new_name;
1208 const struct got_error *
1209 got_ref_change_symref_to_ref(struct got_reference *symref,
1210 struct got_object_id *id)
1212 if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1213 return got_error(GOT_ERR_BAD_REF_TYPE);
1215 symref->ref.ref.name = symref->ref.symref.name;
1216 memcpy(symref->ref.ref.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1217 symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1221 const struct got_error *
1222 got_ref_write(struct got_reference *ref, struct got_repository *repo)
1224 const struct got_error *err = NULL, *unlock_err = NULL;
1225 const char *name = got_ref_get_name(ref);
1226 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1227 struct got_lockfile *lf = NULL;
1232 path_refs = get_refs_dir_path(repo, name);
1233 if (path_refs == NULL) {
1234 err = got_error_from_errno2("get_refs_dir_path", name);
1238 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1239 err = got_error_from_errno("asprintf");
1243 err = got_opentemp_named(&tmppath, &f, path);
1246 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1248 err = got_path_dirname(&parent, path);
1251 err = got_path_mkdir(parent);
1255 err = got_opentemp_named(&tmppath, &f, path);
1260 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1261 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1262 if (n != strlen(ref->ref.symref.ref) + 6) {
1263 err = got_ferror(f, GOT_ERR_IO);
1267 char hex[SHA1_DIGEST_STRING_LENGTH];
1268 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
1269 sizeof(hex)) == NULL) {
1270 err = got_error(GOT_ERR_BAD_REF_DATA);
1273 n = fprintf(f, "%s\n", hex);
1274 if (n != sizeof(hex)) {
1275 err = got_ferror(f, GOT_ERR_IO);
1280 if (ref->lf == NULL) {
1281 err = got_lockfile_lock(&lf, path, -1);
1286 /* XXX: check if old content matches our expectations? */
1288 if (stat(path, &sb) != 0) {
1289 if (errno != ENOENT) {
1290 err = got_error_from_errno2("stat", path);
1293 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1296 if (fchmod(fileno(f), sb.st_mode) != 0) {
1297 err = got_error_from_errno2("fchmod", tmppath);
1301 if (rename(tmppath, path) != 0) {
1302 err = got_error_from_errno3("rename", tmppath, path);
1308 if (stat(path, &sb) == -1) {
1309 err = got_error_from_errno2("stat", path);
1312 ref->mtime = sb.st_mtime;
1314 if (ref->lf == NULL && lf)
1315 unlock_err = got_lockfile_unlock(lf, -1);
1317 if (fclose(f) == EOF && err == NULL)
1318 err = got_error_from_errno("fclose");
1323 if (unlink(tmppath) == -1 && err == NULL)
1324 err = got_error_from_errno2("unlink", tmppath);
1327 return err ? err : unlock_err;
1330 static const struct got_error *
1331 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1333 const struct got_error *err = NULL, *unlock_err = NULL;
1334 struct got_lockfile *lf = NULL;
1335 FILE *f = NULL, *tmpf = NULL;
1336 char *line = NULL, *packed_refs_path, *tmppath = NULL;
1337 size_t linesize = 0;
1338 struct got_reflist_head refs;
1339 int found_delref = 0;
1341 /* The packed-refs file does not cotain symbolic references. */
1342 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1343 return got_error(GOT_ERR_BAD_REF_DATA);
1347 packed_refs_path = got_repo_get_path_packed_refs(repo);
1348 if (packed_refs_path == NULL)
1349 return got_error_from_errno("got_repo_get_path_packed_refs");
1351 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
1355 if (delref->lf == NULL) {
1356 err = got_lockfile_lock(&lf, packed_refs_path, -1);
1361 f = fopen(packed_refs_path, "re");
1363 err = got_error_from_errno2("fopen", packed_refs_path);
1368 struct got_reference *ref;
1369 struct got_reflist_entry *new;
1371 linelen = getline(&line, &linesize, f);
1372 if (linelen == -1) {
1375 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1378 if (linelen > 0 && line[linelen - 1] == '\n')
1379 line[linelen - 1] = '\0';
1380 err = parse_packed_ref_line(&ref, NULL, line, 0);
1386 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1387 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1388 sizeof(delref->ref.ref.sha1)) == 0) {
1394 err = got_reflist_insert(&new, &refs, ref,
1395 got_ref_cmp_by_name, NULL);
1396 if (err || new == NULL /* duplicate */)
1403 struct got_reflist_entry *re;
1407 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1408 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1409 err = got_ferror(f, GOT_ERR_IO);
1413 TAILQ_FOREACH(re, &refs, entry) {
1414 char hex[SHA1_DIGEST_STRING_LENGTH];
1416 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1417 sizeof(hex)) == NULL) {
1418 err = got_error(GOT_ERR_BAD_REF_DATA);
1421 n = fprintf(tmpf, "%s ", hex);
1422 if (n != sizeof(hex)) {
1423 err = got_ferror(f, GOT_ERR_IO);
1426 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1427 if (n != strlen(re->ref->ref.ref.name) + 1) {
1428 err = got_ferror(f, GOT_ERR_IO);
1433 if (fflush(tmpf) != 0) {
1434 err = got_error_from_errno("fflush");
1438 if (fstat(fileno(f), &sb) != 0) {
1439 if (errno != ENOENT) {
1440 err = got_error_from_errno2("fstat",
1444 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1447 if (fchmod(fileno(tmpf), sb.st_mode) != 0) {
1448 err = got_error_from_errno2("fchmod", tmppath);
1452 if (rename(tmppath, packed_refs_path) != 0) {
1453 err = got_error_from_errno3("rename", tmppath,
1461 if (delref->lf == NULL && lf)
1462 unlock_err = got_lockfile_unlock(lf, -1);
1464 if (fclose(f) == EOF && err == NULL)
1465 err = got_error_from_errno("fclose");
1467 if (tmppath && unlink(tmppath) == -1 && err == NULL)
1468 err = got_error_from_errno2("unlink", tmppath);
1469 if (tmpf && fclose(tmpf) == EOF && err == NULL)
1470 err = got_error_from_errno("fclose");
1472 free(packed_refs_path);
1474 got_ref_list_free(&refs);
1475 return err ? err : unlock_err;
1478 static const struct got_error *
1479 delete_loose_ref(struct got_reference *ref, struct got_repository *repo)
1481 const struct got_error *err = NULL, *unlock_err = NULL;
1482 const char *name = got_ref_get_name(ref);
1483 char *path_refs = NULL, *path = NULL;
1484 struct got_lockfile *lf = NULL;
1486 path_refs = get_refs_dir_path(repo, name);
1487 if (path_refs == NULL) {
1488 err = got_error_from_errno2("get_refs_dir_path", name);
1492 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1493 err = got_error_from_errno("asprintf");
1497 if (ref->lf == NULL) {
1498 err = got_lockfile_lock(&lf, path, -1);
1503 /* XXX: check if old content matches our expectations? */
1505 if (unlink(path) == -1)
1506 err = got_error_from_errno2("unlink", path);
1508 if (ref->lf == NULL && lf)
1509 unlock_err = got_lockfile_unlock(lf, -1);
1513 return err ? err : unlock_err;
1516 const struct got_error *
1517 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1519 const struct got_error *err = NULL;
1520 struct got_reference *ref2;
1522 if (ref->flags & GOT_REF_IS_PACKED) {
1523 err = delete_packed_ref(ref, repo);
1527 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1529 if (err->code == GOT_ERR_NOT_REF)
1534 err = delete_loose_ref(ref2, repo);
1535 got_ref_close(ref2);
1538 err = delete_loose_ref(ref, repo);
1542 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1544 if (err->code == GOT_ERR_NOT_REF)
1549 err = delete_packed_ref(ref2, repo);
1550 got_ref_close(ref2);
1555 const struct got_error *
1556 got_ref_unlock(struct got_reference *ref)
1558 const struct got_error *err;
1559 err = got_lockfile_unlock(ref->lf, -1);
1564 struct got_reflist_object_id_map {
1565 struct got_object_idset *idset;
1568 struct got_reflist_object_id_map_entry {
1569 struct got_reflist_head refs;
1572 static const struct got_error *
1573 add_object_id_map_entry(struct got_object_idset *idset,
1574 struct got_object_id *id, struct got_reflist_entry *re)
1576 const struct got_error *err = NULL;
1577 struct got_reflist_object_id_map_entry *ent;
1578 struct got_reflist_entry *new;
1580 ent = got_object_idset_get(idset, id);
1582 ent = malloc(sizeof(*ent));
1584 return got_error_from_errno("malloc");
1586 TAILQ_INIT(&ent->refs);
1587 err = got_object_idset_add(idset, id, ent);
1594 err = got_reflist_entry_dup(&new, re);
1598 TAILQ_INSERT_TAIL(&ent->refs, new, entry);
1602 const struct got_error *
1603 got_reflist_object_id_map_create(struct got_reflist_object_id_map **map,
1604 struct got_reflist_head *refs, struct got_repository *repo)
1606 const struct got_error *err = NULL;
1607 struct got_object_idset *idset;
1608 struct got_object_id *id = NULL;
1609 struct got_reflist_entry *re;
1611 idset = got_object_idset_alloc();
1613 return got_error_from_errno("got_object_idset_alloc");
1615 *map = malloc(sizeof(**map));
1617 got_object_idset_free(idset);
1618 return got_error_from_errno("malloc");
1620 (*map)->idset = idset;
1622 TAILQ_FOREACH(re, refs, entry) {
1623 struct got_tag_object *tag = NULL;
1625 err = got_ref_resolve(&id, repo, re->ref);
1629 err = add_object_id_map_entry(idset, id, re);
1633 if (strstr(got_ref_get_name(re->ref), "/tags/") == NULL) {
1639 err = got_object_open_as_tag(&tag, repo, id);
1641 if (err->code != GOT_ERR_OBJ_TYPE)
1643 /* Ref points at something other than a tag. */
1651 err = add_object_id_map_entry(idset,
1652 got_object_tag_get_object_id(tag), re);
1653 got_object_tag_close(tag);
1663 got_reflist_object_id_map_free(*map);
1669 struct got_reflist_head *
1670 got_reflist_object_id_map_lookup(struct got_reflist_object_id_map *map,
1671 struct got_object_id *id)
1673 struct got_reflist_object_id_map_entry *ent;
1674 ent = got_object_idset_get(map->idset, id);
1680 static const struct got_error *
1681 free_id_map_entry(struct got_object_id *id, void *data, void *arg)
1683 struct got_reflist_object_id_map_entry *ent = data;
1685 got_ref_list_free(&ent->refs);
1691 got_reflist_object_id_map_free(struct got_reflist_object_id_map *map)
1693 got_object_idset_for_each(map->idset, free_id_map_entry, NULL);
1694 got_object_idset_free(map->idset);