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_hash.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 struct got_object_id id;
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.id, id, sizeof((*ref)->ref.ref.id));
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,
156 time_t mtime, enum got_hash_algorithm algo)
158 struct got_object_id id;
160 if (strncmp(line, "ref: ", 5) == 0) {
162 return parse_symref(ref, name, line);
165 if (!got_parse_object_id(&id, line, algo))
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,
174 enum got_hash_algorithm algo)
176 const struct got_error *err = NULL;
181 struct got_lockfile *lf = NULL;
185 err = got_lockfile_lock(&lf, abspath, -1);
187 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
188 err = got_error_not_ref(name);
193 f = fopen(abspath, "rbe");
195 if (errno != ENOTDIR && errno != ENOENT)
196 err = got_error_from_errno2("fopen", abspath);
198 err = got_error_not_ref(name);
200 got_lockfile_unlock(lf, -1);
203 if (fstat(fileno(f), &sb) == -1) {
204 err = got_error_from_errno2("fstat", abspath);
208 linelen = getline(&line, &linesize, f);
211 err = NULL; /* ignore empty files (could be locks) */
214 err = got_error(GOT_ERR_NOT_REF);
216 err = got_ferror(f, GOT_ERR_IO);
218 err = got_error_from_errno2("getline", abspath);
221 got_lockfile_unlock(lf, -1);
224 while (linelen > 0 && line[linelen - 1] == '\n') {
225 line[linelen - 1] = '\0';
229 err = parse_ref_line(ref, absname, line, sb.st_mtime, algo);
232 got_lockfile_unlock(lf, -1);
237 got_lockfile_unlock(lf, -1);
242 if (fclose(f) == EOF && err == NULL) {
243 err = got_error_from_errno("fclose");
246 got_ref_unlock(*ref);
255 is_well_known_ref(const char *refname)
257 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
258 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
259 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
260 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
264 get_refs_dir_path(struct got_repository *repo, const char *refname)
266 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
267 return strdup(got_repo_get_path_git_dir(repo));
269 return got_repo_get_path_refs(repo);
272 const struct got_error *
273 got_ref_alloc(struct got_reference **ref, const char *name,
274 struct got_object_id *id)
276 if (!got_ref_name_is_valid(name))
277 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
279 return alloc_ref(ref, name, id, 0, 0);
282 const struct got_error *
283 got_ref_alloc_symref(struct got_reference **ref, const char *name,
284 struct got_reference *target_ref)
286 if (!got_ref_name_is_valid(name))
287 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
289 return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
292 static const struct got_error *
293 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
294 const char *line, time_t mtime, enum got_hash_algorithm algo)
296 struct got_object_id id;
301 if (line[0] == '#' || line[0] == '^')
304 if (!got_parse_object_id(&id, line, algo))
305 return got_error(GOT_ERR_BAD_REF_DATA);
308 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
312 name = line + SHA1_DIGEST_STRING_LENGTH;
314 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED, mtime);
317 static const struct got_error *
318 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
319 int nsubdirs, const char *refname, time_t mtime,
320 enum got_hash_algorithm algo)
322 const struct got_error *err = NULL;
327 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
332 abs_refname = (char *)refname;
334 linelen = getline(&line, &linesize, f);
338 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
341 if (linelen > 0 && line[linelen - 1] == '\n')
342 line[linelen - 1] = '\0';
343 for (i = 0; i < nsubdirs; i++) {
344 if (!ref_is_absolute &&
345 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
347 return got_error_from_errno("asprintf");
348 err = parse_packed_ref_line(ref, abs_refname, line,
350 if (!ref_is_absolute)
352 if (err || *ref != NULL)
357 } while (*ref == NULL);
363 static const struct got_error *
364 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
365 const char *name, int lock, enum got_hash_algorithm algo)
367 const struct got_error *err = NULL;
369 char *absname = NULL;
370 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
371 int ref_is_well_known = (subdir[0] == '\0' && is_well_known_ref(name));
375 if (!got_ref_name_is_valid(name))
376 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
378 if (ref_is_absolute || ref_is_well_known) {
379 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
380 return got_error_from_errno("asprintf");
381 absname = (char *)name;
383 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
384 subdir[0] ? "/" : "", name) == -1)
385 return got_error_from_errno("asprintf");
387 if (asprintf(&absname, "refs/%s%s%s",
388 subdir, subdir[0] ? "/" : "", name) == -1) {
389 err = got_error_from_errno("asprintf");
394 err = parse_ref_file(ref, name, absname, path, lock, algo);
396 if (!ref_is_absolute && !ref_is_well_known)
402 const struct got_error *
403 got_ref_open(struct got_reference **ref, struct got_repository *repo,
404 const char *refname, int lock)
406 const struct got_error *err = NULL;
407 char *packed_refs_path = NULL, *path_refs = NULL;
408 const char *subdirs[] = {
409 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
412 int well_known = is_well_known_ref(refname);
413 struct got_lockfile *lf = NULL;
417 path_refs = get_refs_dir_path(repo, refname);
418 if (path_refs == NULL) {
419 err = got_error_from_errno2("get_refs_dir_path", refname);
424 err = open_ref(ref, path_refs, "", refname, lock,
425 got_repo_get_object_format(repo));
429 /* Search on-disk refs before packed refs! */
430 for (i = 0; i < nitems(subdirs); i++) {
431 err = open_ref(ref, path_refs, subdirs[i], refname,
432 lock, got_repo_get_object_format(repo));
433 if ((err && err->code != GOT_ERR_NOT_REF) || *ref)
437 packed_refs_path = got_repo_get_path_packed_refs(repo);
438 if (packed_refs_path == NULL) {
439 err = got_error_from_errno(
440 "got_repo_get_path_packed_refs");
445 err = got_lockfile_lock(&lf, packed_refs_path, -1);
449 f = fopen(packed_refs_path, "rbe");
452 if (fstat(fileno(f), &sb) == -1) {
453 err = got_error_from_errno2("fstat",
457 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
458 refname, sb.st_mtime,
459 got_repo_get_object_format(repo));
461 if (fclose(f) == EOF) {
462 err = got_error_from_errno("fclose");
471 if (!err && *ref == NULL)
472 err = got_error_not_ref(refname);
474 got_lockfile_unlock(lf, -1);
475 free(packed_refs_path);
481 got_ref_close(struct got_reference *ref)
483 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
484 free(ref->ref.symref.name);
485 free(ref->ref.symref.ref);
487 free(ref->ref.ref.name);
491 struct got_reference *
492 got_ref_dup(struct got_reference *ref)
494 struct got_reference *ret;
496 ret = calloc(1, sizeof(*ret));
500 ret->flags = ref->flags;
501 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
502 ret->ref.symref.name = strdup(ref->ref.symref.name);
503 if (ret->ref.symref.name == NULL) {
507 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
508 if (ret->ref.symref.ref == NULL) {
509 free(ret->ref.symref.name);
514 ret->ref.ref.name = strdup(ref->ref.ref.name);
515 if (ret->ref.ref.name == NULL) {
519 memcpy(&ret->ref.ref.id, &ref->ref.ref.id,
520 sizeof(ret->ref.ref.id));
526 const struct got_error *
527 got_reflist_entry_dup(struct got_reflist_entry **newp,
528 struct got_reflist_entry *re)
530 const struct got_error *err = NULL;
531 struct got_reflist_entry *new;
535 new = malloc(sizeof(*new));
537 return got_error_from_errno("malloc");
539 new->ref = got_ref_dup(re->ref);
540 if (new->ref == NULL) {
541 err = got_error_from_errno("got_ref_dup");
550 const struct got_error *
551 got_ref_resolve_symbolic(struct got_reference **resolved,
552 struct got_repository *repo, struct got_reference *ref)
554 struct got_reference *nextref;
555 const struct got_error *err;
557 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
561 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
562 err = got_ref_resolve_symbolic(resolved, repo, nextref);
564 *resolved = got_ref_dup(nextref);
566 got_ref_close(nextref);
570 static const struct got_error *
571 ref_resolve(struct got_object_id **id, struct got_repository *repo,
572 struct got_reference *ref, int recursion)
574 const struct got_error *err;
577 return got_error_msg(GOT_ERR_RECURSION,
578 "reference recursion limit reached");
580 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
581 struct got_reference *resolved = NULL;
582 err = got_ref_resolve_symbolic(&resolved, repo, ref);
584 err = ref_resolve(id, repo, resolved, --recursion);
586 got_ref_close(resolved);
590 *id = calloc(1, sizeof(**id));
592 return got_error_from_errno("calloc");
593 memcpy(*id, &ref->ref.ref.id, sizeof(**id));
597 const struct got_error *
598 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
599 struct got_reference *ref)
601 return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
605 got_ref_to_str(struct got_reference *ref)
609 if (ref->flags & GOT_REF_IS_SYMBOLIC)
610 return strdup(ref->ref.symref.ref);
612 if (got_object_id_str(&str, &ref->ref.ref.id) != NULL)
619 got_ref_get_name(struct got_reference *ref)
621 if (ref->flags & GOT_REF_IS_SYMBOLIC)
622 return ref->ref.symref.name;
624 return ref->ref.ref.name;
628 got_ref_get_symref_target(struct got_reference *ref)
630 if (ref->flags & GOT_REF_IS_SYMBOLIC)
631 return ref->ref.symref.ref;
637 got_ref_get_mtime(struct got_reference *ref)
642 const struct got_error *
643 got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
644 struct got_reference* re2)
646 const char *name1 = got_ref_get_name(re1);
647 const char *name2 = got_ref_get_name(re2);
649 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
653 const struct got_error *
654 got_ref_cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
655 struct got_reference *ref2)
657 const struct got_error *err = NULL;
658 struct got_repository *repo = arg;
659 struct got_object_id *id1, *id2 = NULL;
660 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
661 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
666 err = got_ref_resolve(&id1, repo, ref1);
669 err = got_object_open_as_tag(&tag1, repo, id1);
671 if (err->code != GOT_ERR_OBJ_TYPE)
673 /* "lightweight" tag */
674 err = got_object_open_as_commit(&commit1, repo, id1);
677 time1 = got_object_commit_get_committer_time(commit1);
679 time1 = got_object_tag_get_tagger_time(tag1);
681 err = got_ref_resolve(&id2, repo, ref2);
684 err = got_object_open_as_tag(&tag2, repo, id2);
686 if (err->code != GOT_ERR_OBJ_TYPE)
688 /* "lightweight" tag */
689 err = got_object_open_as_commit(&commit2, repo, id2);
692 time2 = got_object_commit_get_committer_time(commit2);
694 time2 = got_object_tag_get_tagger_time(tag2);
696 /* Put latest tags first. */
699 else if (time1 > time2)
702 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
707 got_object_tag_close(tag1);
709 got_object_tag_close(tag2);
711 got_object_commit_close(commit1);
713 got_object_commit_close(commit2);
717 static const struct got_error *
718 get_committer_time(struct got_reference *ref, struct got_repository *repo)
720 const struct got_error *err = NULL;
722 struct got_commit_object *commit = NULL;
723 struct got_tag_object *tag = NULL;
724 struct got_object_id *id = NULL;
726 err = got_ref_resolve(&id, repo, ref);
730 err = got_object_get_type(&obj_type, repo, id);
735 case GOT_OBJ_TYPE_COMMIT:
736 err = got_object_open_as_commit(&commit, repo, id);
739 ref->committer_time =
740 got_object_commit_get_committer_time(commit);
742 case GOT_OBJ_TYPE_TAG:
743 err = got_object_open_as_tag(&tag, repo, id);
746 ref->committer_time = got_object_tag_get_tagger_time(tag);
749 /* best effort for other object types */
750 ref->committer_time = got_ref_get_mtime(ref);
756 got_object_commit_close(commit);
758 got_object_tag_close(tag);
762 const struct got_error *
763 got_ref_cmp_by_commit_timestamp_descending(void *arg, int *cmp,
764 struct got_reference *ref1, struct got_reference *ref2)
766 const struct got_error *err = NULL;
767 struct got_repository *repo = arg;
771 if (ref1->committer_time == 0) {
772 err = get_committer_time(ref1, repo);
776 if (ref2->committer_time == 0) {
777 err = get_committer_time(ref2, repo);
782 if (ref1->committer_time < ref2->committer_time)
784 else if (ref2->committer_time < ref1->committer_time)
787 return got_ref_cmp_by_name(arg, cmp, ref1, ref2);
792 const struct got_error *
793 got_reflist_insert(struct got_reflist_entry **newp,
794 struct got_reflist_head *refs, struct got_reference *ref,
795 got_ref_cmp_cb cmp_cb, void *cmp_arg)
797 const struct got_error *err;
798 struct got_reflist_entry *new, *re;
803 if (cmp_cb != got_ref_cmp_by_name && (ref->flags & GOT_REF_IS_PACKED)) {
805 * If we are not sorting elements by name then we must still
806 * detect collisions between a packed ref and an on-disk ref
807 * using the same name. On-disk refs take precedence and are
808 * already present on the list before packed refs get added.
810 TAILQ_FOREACH(re, refs, entry) {
811 err = got_ref_cmp_by_name(NULL, &cmp, re->ref, ref);
819 new = malloc(sizeof(*new));
821 return got_error_from_errno("malloc");
826 * We must de-duplicate entries on insert because packed-refs may
827 * contain redundant entries. On-disk refs take precedence.
828 * This code assumes that on-disk revs are read before packed-refs.
829 * We're iterating the list anyway, so insert elements sorted by name.
831 * Many callers will provide paths in a somewhat sorted order.
832 * Iterating backwards from the tail of the list should be more
833 * efficient than traversing through the entire list each time
834 * an element is inserted.
836 re = TAILQ_LAST(refs, got_reflist_head);
838 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
846 } else if (cmp < 0) {
847 TAILQ_INSERT_AFTER(refs, re, new, entry);
850 re = TAILQ_PREV(re, got_reflist_head, entry);
853 TAILQ_INSERT_HEAD(refs, new, entry);
857 const struct got_error *
858 got_reflist_sort(struct got_reflist_head *refs,
859 got_ref_cmp_cb cmp_cb, void *cmp_arg)
861 const struct got_error *err = NULL;
862 struct got_reflist_entry *re, *tmp, *new;
863 struct got_reflist_head sorted;
867 TAILQ_FOREACH_SAFE(re, refs, entry, tmp) {
868 struct got_reference *ref = re->ref;
869 TAILQ_REMOVE(refs, re, entry);
871 err = got_reflist_insert(&new, &sorted, ref, cmp_cb, cmp_arg);
872 if (err || new == NULL /* duplicate */)
878 TAILQ_CONCAT(refs, &sorted, entry);
882 static const struct got_error *
883 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
884 const char *subdir, struct got_repository *repo,
885 got_ref_cmp_cb cmp_cb, void *cmp_arg)
887 const struct got_error *err = NULL;
891 while (subdir[0] == '/')
894 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
895 return got_error_from_errno("asprintf");
897 d = opendir(path_subdir);
903 struct got_reference *ref;
911 if (strcmp(dent->d_name, ".") == 0 ||
912 strcmp(dent->d_name, "..") == 0)
915 err = got_path_dirent_type(&type, path_subdir, dent);
921 err = open_ref(&ref, path_refs, subdir, dent->d_name,
922 0, got_repo_get_object_format(repo));
926 struct got_reflist_entry *new;
927 err = got_reflist_insert(&new, refs, ref,
929 if (err || new == NULL /* duplicate */)
936 if (asprintf(&child, "%s%s%s", subdir,
937 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
938 err = got_error_from_errno("asprintf");
941 err = gather_on_disk_refs(refs, path_refs, child, repo,
956 const struct got_error *
957 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
958 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
960 const struct got_error *err;
961 char *packed_refs_path = NULL, *path_refs = NULL;
962 char *abs_namespace = NULL, *buf = NULL;
963 const char *ondisk_ref_namespace = NULL;
966 struct got_reference *ref;
967 struct got_reflist_entry *new;
969 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
970 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
971 if (path_refs == NULL) {
972 err = got_error_from_errno("get_refs_dir_path");
975 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0,
976 got_repo_get_object_format(repo));
979 err = got_reflist_insert(&new, refs, ref, cmp_cb, cmp_arg);
980 if (err || new == NULL /* duplicate */)
982 if (err && err->code != GOT_ERR_NOT_REF)
985 /* Try listing a single reference. */
986 const char *refname = ref_namespace;
987 path_refs = get_refs_dir_path(repo, refname);
988 if (path_refs == NULL) {
989 err = got_error_from_errno("get_refs_dir_path");
992 err = open_ref(&ref, path_refs, "", refname, 0,
993 got_repo_get_object_format(repo));
995 if (err->code != GOT_ERR_NOT_REF)
997 /* Try to look up references in a given namespace. */
999 err = got_reflist_insert(&new, refs, ref,
1001 if (err || new == NULL /* duplicate */)
1007 if (ref_namespace) {
1009 /* Canonicalize the path to eliminate double-slashes if any. */
1010 if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
1011 err = got_error_from_errno("asprintf");
1014 len = strlen(abs_namespace) + 1;
1017 err = got_error_from_errno("malloc");
1020 err = got_canonpath(abs_namespace, buf, len);
1023 ondisk_ref_namespace = buf;
1024 while (ondisk_ref_namespace[0] == '/')
1025 ondisk_ref_namespace++;
1026 if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
1027 ondisk_ref_namespace += 5;
1028 else if (strcmp(ondisk_ref_namespace, "refs") == 0)
1029 ondisk_ref_namespace = "";
1032 /* Gather on-disk refs before parsing packed-refs. */
1034 path_refs = get_refs_dir_path(repo, "");
1035 if (path_refs == NULL) {
1036 err = got_error_from_errno("get_refs_dir_path");
1039 err = gather_on_disk_refs(refs, path_refs,
1040 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
1046 * The packed-refs file may contain redundant entries, in which
1047 * case on-disk refs take precedence.
1049 packed_refs_path = got_repo_get_path_packed_refs(repo);
1050 if (packed_refs_path == NULL) {
1051 err = got_error_from_errno("got_repo_get_path_packed_refs");
1055 f = fopen(packed_refs_path, "re");
1057 size_t linesize = 0;
1061 if (fstat(fileno(f), &sb) == -1) {
1062 err = got_error_from_errno2("fstat", packed_refs_path);
1066 linelen = getline(&line, &linesize, f);
1067 if (linelen == -1) {
1070 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1073 if (linelen > 0 && line[linelen - 1] == '\n')
1074 line[linelen - 1] = '\0';
1075 err = parse_packed_ref_line(&ref, NULL, line,
1076 sb.st_mtime, got_repo_get_object_format(repo));
1080 if (ref_namespace) {
1082 name = got_ref_get_name(ref);
1083 if (!got_path_is_child(name,
1085 strlen(ref_namespace))) {
1090 err = got_reflist_insert(&new, refs, ref,
1092 if (err || new == NULL /* duplicate */)
1100 free(packed_refs_path);
1101 free(abs_namespace);
1105 if (f && fclose(f) == EOF && err == NULL)
1106 err = got_error_from_errno("fclose");
1111 got_ref_list_free(struct got_reflist_head *refs)
1113 struct got_reflist_entry *re;
1115 while ((re = TAILQ_FIRST(refs))) {
1116 TAILQ_REMOVE(refs, re, entry);
1117 got_ref_close(re->ref);
1123 got_ref_is_symbolic(struct got_reference *ref)
1125 return (ref->flags & GOT_REF_IS_SYMBOLIC);
1128 const struct got_error *
1129 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1131 if (ref->flags & GOT_REF_IS_SYMBOLIC)
1132 return got_error(GOT_ERR_BAD_REF_TYPE);
1134 memcpy(&ref->ref.ref.id, id, sizeof(ref->ref.ref.id));
1138 const struct got_error *
1139 got_ref_change_symref(struct got_reference *ref, const char *refname)
1143 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1144 return got_error(GOT_ERR_BAD_REF_TYPE);
1146 new_name = strdup(refname);
1147 if (new_name == NULL)
1148 return got_error_from_errno("strdup");
1150 free(ref->ref.symref.ref);
1151 ref->ref.symref.ref = new_name;
1155 const struct got_error *
1156 got_ref_change_symref_to_ref(struct got_reference *symref,
1157 struct got_object_id *id)
1159 if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1160 return got_error(GOT_ERR_BAD_REF_TYPE);
1162 symref->ref.ref.name = symref->ref.symref.name;
1163 memcpy(&symref->ref.ref.id, id, sizeof(symref->ref.ref.id));
1164 symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1168 const struct got_error *
1169 got_ref_write(struct got_reference *ref, struct got_repository *repo)
1171 const struct got_error *err = NULL, *unlock_err = NULL;
1172 const char *name = got_ref_get_name(ref);
1173 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1174 struct got_lockfile *lf = NULL;
1179 path_refs = get_refs_dir_path(repo, name);
1180 if (path_refs == NULL) {
1181 err = got_error_from_errno2("get_refs_dir_path", name);
1185 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1186 err = got_error_from_errno("asprintf");
1190 err = got_opentemp_named(&tmppath, &f, path, "");
1193 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1195 err = got_path_dirname(&parent, path);
1198 err = got_path_mkdir(parent);
1202 err = got_opentemp_named(&tmppath, &f, path, "");
1207 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1208 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1209 if (n != strlen(ref->ref.symref.ref) + 6) {
1210 err = got_ferror(f, GOT_ERR_IO);
1217 err = got_object_id_str(&hex, &ref->ref.ref.id);
1221 n = fprintf(f, "%s\n", hex);
1224 err = got_ferror(f, GOT_ERR_IO);
1229 if (ref->lf == NULL) {
1230 err = got_lockfile_lock(&lf, path, -1);
1235 /* XXX: check if old content matches our expectations? */
1237 if (stat(path, &sb) != 0) {
1238 if (errno != ENOENT) {
1239 err = got_error_from_errno2("stat", path);
1242 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1245 if (fchmod(fileno(f), sb.st_mode) != 0) {
1246 err = got_error_from_errno2("fchmod", tmppath);
1250 if (rename(tmppath, path) != 0) {
1251 err = got_error_from_errno3("rename", tmppath, path);
1257 if (stat(path, &sb) == -1) {
1258 err = got_error_from_errno2("stat", path);
1261 ref->mtime = sb.st_mtime;
1263 if (ref->lf == NULL && lf)
1264 unlock_err = got_lockfile_unlock(lf, -1);
1266 if (fclose(f) == EOF && err == NULL)
1267 err = got_error_from_errno("fclose");
1272 if (unlink(tmppath) == -1 && err == NULL)
1273 err = got_error_from_errno2("unlink", tmppath);
1276 return err ? err : unlock_err;
1279 static const struct got_error *
1280 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1282 const struct got_error *err = NULL, *unlock_err = NULL;
1283 struct got_lockfile *lf = NULL;
1284 FILE *f = NULL, *tmpf = NULL;
1285 char *line = NULL, *packed_refs_path, *tmppath = NULL;
1286 size_t linesize = 0;
1287 struct got_reflist_head refs;
1288 int found_delref = 0;
1290 /* The packed-refs file does not cotain symbolic references. */
1291 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1292 return got_error(GOT_ERR_BAD_REF_DATA);
1296 packed_refs_path = got_repo_get_path_packed_refs(repo);
1297 if (packed_refs_path == NULL)
1298 return got_error_from_errno("got_repo_get_path_packed_refs");
1300 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path, "");
1304 if (delref->lf == NULL) {
1305 err = got_lockfile_lock(&lf, packed_refs_path, -1);
1310 f = fopen(packed_refs_path, "re");
1312 err = got_error_from_errno2("fopen", packed_refs_path);
1317 struct got_reference *ref;
1318 struct got_reflist_entry *new;
1320 linelen = getline(&line, &linesize, f);
1321 if (linelen == -1) {
1324 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1327 if (linelen > 0 && line[linelen - 1] == '\n')
1328 line[linelen - 1] = '\0';
1329 err = parse_packed_ref_line(&ref, NULL, line, 0,
1330 got_repo_get_object_format(repo));
1336 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1337 got_object_id_cmp(&ref->ref.ref.id,
1338 &delref->ref.ref.id) == 0) {
1344 err = got_reflist_insert(&new, &refs, ref,
1345 got_ref_cmp_by_name, NULL);
1346 if (err || new == NULL /* duplicate */)
1353 struct got_reflist_entry *re;
1357 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1358 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1359 err = got_ferror(f, GOT_ERR_IO);
1363 TAILQ_FOREACH(re, &refs, entry) {
1367 err = got_object_id_str(&hex, &re->ref->ref.ref.id);
1371 n = fprintf(tmpf, "%s ", hex);
1374 err = got_ferror(f, GOT_ERR_IO);
1378 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1379 if (n != strlen(re->ref->ref.ref.name) + 1) {
1380 err = got_ferror(f, GOT_ERR_IO);
1385 if (fflush(tmpf) != 0) {
1386 err = got_error_from_errno("fflush");
1390 if (fstat(fileno(f), &sb) != 0) {
1391 if (errno != ENOENT) {
1392 err = got_error_from_errno2("fstat",
1396 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1399 if (fchmod(fileno(tmpf), sb.st_mode) != 0) {
1400 err = got_error_from_errno2("fchmod", tmppath);
1404 if (rename(tmppath, packed_refs_path) != 0) {
1405 err = got_error_from_errno3("rename", tmppath,
1413 if (delref->lf == NULL && lf)
1414 unlock_err = got_lockfile_unlock(lf, -1);
1416 if (fclose(f) == EOF && err == NULL)
1417 err = got_error_from_errno("fclose");
1419 if (tmppath && unlink(tmppath) == -1 && err == NULL)
1420 err = got_error_from_errno2("unlink", tmppath);
1421 if (tmpf && fclose(tmpf) == EOF && err == NULL)
1422 err = got_error_from_errno("fclose");
1424 free(packed_refs_path);
1426 got_ref_list_free(&refs);
1427 return err ? err : unlock_err;
1430 static const struct got_error *
1431 delete_loose_ref(struct got_reference *ref, struct got_repository *repo)
1433 const struct got_error *err = NULL, *unlock_err = NULL;
1434 const char *name = got_ref_get_name(ref);
1435 char *path_refs = NULL, *path = NULL;
1436 struct got_lockfile *lf = NULL;
1438 path_refs = get_refs_dir_path(repo, name);
1439 if (path_refs == NULL) {
1440 err = got_error_from_errno2("get_refs_dir_path", name);
1444 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1445 err = got_error_from_errno("asprintf");
1449 if (ref->lf == NULL) {
1450 err = got_lockfile_lock(&lf, path, -1);
1455 /* XXX: check if old content matches our expectations? */
1457 if (unlink(path) == -1)
1458 err = got_error_from_errno2("unlink", path);
1460 if (ref->lf == NULL && lf)
1461 unlock_err = got_lockfile_unlock(lf, -1);
1465 return err ? err : unlock_err;
1468 const struct got_error *
1469 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1471 const struct got_error *err = NULL;
1472 struct got_reference *ref2;
1474 if (ref->flags & GOT_REF_IS_PACKED) {
1475 err = delete_packed_ref(ref, repo);
1479 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1481 if (err->code == GOT_ERR_NOT_REF)
1486 err = delete_loose_ref(ref2, repo);
1487 got_ref_close(ref2);
1490 err = delete_loose_ref(ref, repo);
1494 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1496 if (err->code == GOT_ERR_NOT_REF)
1501 err = delete_packed_ref(ref2, repo);
1502 got_ref_close(ref2);
1507 const struct got_error *
1508 got_ref_unlock(struct got_reference *ref)
1510 const struct got_error *err;
1511 err = got_lockfile_unlock(ref->lf, -1);
1516 struct got_reflist_object_id_map {
1517 struct got_object_idset *idset;
1520 struct got_reflist_object_id_map_entry {
1521 struct got_reflist_head refs;
1524 static const struct got_error *
1525 add_object_id_map_entry(struct got_object_idset *idset,
1526 struct got_object_id *id, struct got_reflist_entry *re)
1528 const struct got_error *err = NULL;
1529 struct got_reflist_object_id_map_entry *ent;
1530 struct got_reflist_entry *new;
1532 ent = got_object_idset_get(idset, id);
1534 ent = malloc(sizeof(*ent));
1536 return got_error_from_errno("malloc");
1538 TAILQ_INIT(&ent->refs);
1539 err = got_object_idset_add(idset, id, ent);
1546 err = got_reflist_entry_dup(&new, re);
1550 TAILQ_INSERT_TAIL(&ent->refs, new, entry);
1554 const struct got_error *
1555 got_reflist_object_id_map_create(struct got_reflist_object_id_map **map,
1556 struct got_reflist_head *refs, struct got_repository *repo)
1558 const struct got_error *err = NULL;
1559 struct got_object_idset *idset;
1560 struct got_object_id *id = NULL;
1561 struct got_reflist_entry *re;
1563 idset = got_object_idset_alloc();
1565 return got_error_from_errno("got_object_idset_alloc");
1567 *map = malloc(sizeof(**map));
1569 got_object_idset_free(idset);
1570 return got_error_from_errno("malloc");
1572 (*map)->idset = idset;
1574 TAILQ_FOREACH(re, refs, entry) {
1575 struct got_tag_object *tag = NULL;
1577 err = got_ref_resolve(&id, repo, re->ref);
1581 err = add_object_id_map_entry(idset, id, re);
1585 if (strstr(got_ref_get_name(re->ref), "/tags/") == NULL) {
1591 err = got_object_open_as_tag(&tag, repo, id);
1593 if (err->code != GOT_ERR_OBJ_TYPE)
1595 /* Ref points at something other than a tag. */
1603 err = add_object_id_map_entry(idset,
1604 got_object_tag_get_object_id(tag), re);
1605 got_object_tag_close(tag);
1615 got_reflist_object_id_map_free(*map);
1621 struct got_reflist_head *
1622 got_reflist_object_id_map_lookup(struct got_reflist_object_id_map *map,
1623 struct got_object_id *id)
1625 struct got_reflist_object_id_map_entry *ent;
1626 ent = got_object_idset_get(map->idset, id);
1632 static const struct got_error *
1633 free_id_map_entry(struct got_object_id *id, void *data, void *arg)
1635 struct got_reflist_object_id_map_entry *ent = data;
1637 got_ref_list_free(&ent->refs);
1643 got_reflist_object_id_map_free(struct got_reflist_object_id_map *map)
1645 got_object_idset_for_each(map->idset, free_id_map_entry, NULL);
1646 got_object_idset_free(map->idset);