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>
32 #include "got_compat.h"
34 #include "got_error.h"
35 #include "got_object.h"
36 #include "got_repository.h"
37 #include "got_reference.h"
38 #include "got_opentemp.h"
41 #include "got_lib_sha1.h"
42 #include "got_lib_delta.h"
43 #include "got_lib_inflate.h"
44 #include "got_lib_object.h"
45 #include "got_lib_object_idset.h"
46 #include "got_lib_lockfile.h"
49 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
52 #define GOT_REF_HEADS "heads"
53 #define GOT_REF_TAGS "tags"
54 #define GOT_REF_REMOTES "remotes"
57 * We do not resolve tags yet, and don't yet care about sorting refs either,
58 * so packed-refs files we write contain a minimal header which disables all
59 * packed-refs "traits" supported by Git.
61 #define GOT_PACKED_REFS_HEADER "# pack-refs with:"
63 /* A symbolic reference. */
69 #define GOT_REF_RECURSE_MAX 20
71 /* A non-symbolic reference (there is no better designation). */
74 u_int8_t sha1[SHA1_DIGEST_LENGTH];
77 /* A reference which points to an arbitrary object. */
78 struct got_reference {
80 #define GOT_REF_IS_SYMBOLIC 0x01
81 #define GOT_REF_IS_PACKED 0x02
85 struct got_symref symref;
88 struct got_lockfile *lf;
91 /* Cached timestamp for got_ref_cmp_by_commit_timestamp_descending() */
92 time_t committer_time;
95 static const struct got_error *
96 alloc_ref(struct got_reference **ref, const char *name,
97 struct got_object_id *id, int flags, time_t mtime)
99 const struct got_error *err = NULL;
101 *ref = calloc(1, sizeof(**ref));
103 return got_error_from_errno("calloc");
105 memcpy((*ref)->ref.ref.sha1, id->sha1, sizeof((*ref)->ref.ref.sha1));
106 (*ref)->flags = flags;
107 (*ref)->ref.ref.name = strdup(name);
108 (*ref)->mtime = mtime;
109 if ((*ref)->ref.ref.name == NULL) {
110 err = got_error_from_errno("strdup");
117 static const struct got_error *
118 alloc_symref(struct got_reference **ref, const char *name,
119 const char *target_ref, int flags)
121 const struct got_error *err = NULL;
123 *ref = calloc(1, sizeof(**ref));
125 return got_error_from_errno("calloc");
127 (*ref)->flags = GOT_REF_IS_SYMBOLIC | flags;
128 (*ref)->ref.symref.name = strdup(name);
129 if ((*ref)->ref.symref.name == NULL) {
130 err = got_error_from_errno("strdup");
135 (*ref)->ref.symref.ref = strdup(target_ref);
136 if ((*ref)->ref.symref.ref == NULL) {
137 err = got_error_from_errno("strdup");
144 static const struct got_error *
145 parse_symref(struct got_reference **ref, const char *name, const char *line)
148 return got_error(GOT_ERR_BAD_REF_DATA);
150 return alloc_symref(ref, name, line, 0);
153 static const struct got_error *
154 parse_ref_line(struct got_reference **ref, const char *name, const char *line,
157 struct got_object_id id;
159 if (strncmp(line, "ref: ", 5) == 0) {
161 return parse_symref(ref, name, line);
164 if (!got_parse_sha1_digest(id.sha1, line))
165 return got_error(GOT_ERR_BAD_REF_DATA);
167 return alloc_ref(ref, name, &id, 0, mtime);
170 static const struct got_error *
171 parse_ref_file(struct got_reference **ref, const char *name,
172 const char *absname, const char *abspath, int lock)
174 const struct got_error *err = NULL;
179 struct got_lockfile *lf = NULL;
183 err = got_lockfile_lock(&lf, abspath, -1);
185 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
186 err = got_error_not_ref(name);
191 f = fopen(abspath, "rbe");
193 if (errno != ENOTDIR && errno != ENOENT)
194 err = got_error_from_errno2("fopen", abspath);
196 err = got_error_not_ref(name);
198 got_lockfile_unlock(lf, -1);
201 if (fstat(fileno(f), &sb) == -1) {
202 err = got_error_from_errno2("fstat", abspath);
206 linelen = getline(&line, &linesize, f);
209 err = NULL; /* ignore empty files (could be locks) */
212 err = got_error(GOT_ERR_NOT_REF);
214 err = got_ferror(f, GOT_ERR_IO);
216 err = got_error_from_errno2("getline", abspath);
219 got_lockfile_unlock(lf, -1);
222 while (linelen > 0 && line[linelen - 1] == '\n') {
223 line[linelen - 1] = '\0';
227 err = parse_ref_line(ref, absname, line, sb.st_mtime);
230 got_lockfile_unlock(lf, -1);
235 got_lockfile_unlock(lf, -1);
240 if (fclose(f) == EOF && err == NULL) {
241 err = got_error_from_errno("fclose");
244 got_ref_unlock(*ref);
253 is_well_known_ref(const char *refname)
255 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
256 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
257 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
258 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
262 get_refs_dir_path(struct got_repository *repo, const char *refname)
264 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
265 return strdup(got_repo_get_path_git_dir(repo));
267 return got_repo_get_path_refs(repo);
270 const struct got_error *
271 got_ref_alloc(struct got_reference **ref, const char *name,
272 struct got_object_id *id)
274 if (!got_ref_name_is_valid(name))
275 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
277 return alloc_ref(ref, name, id, 0, 0);
280 const struct got_error *
281 got_ref_alloc_symref(struct got_reference **ref, const char *name,
282 struct got_reference *target_ref)
284 if (!got_ref_name_is_valid(name))
285 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
287 return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
290 static const struct got_error *
291 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
292 const char *line, time_t mtime)
294 struct got_object_id id;
299 if (line[0] == '#' || line[0] == '^')
302 if (!got_parse_sha1_digest(id.sha1, line))
303 return got_error(GOT_ERR_BAD_REF_DATA);
306 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
310 name = line + SHA1_DIGEST_STRING_LENGTH;
312 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED, mtime);
315 static const struct got_error *
316 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
317 int nsubdirs, const char *refname, time_t mtime)
319 const struct got_error *err = NULL;
324 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
329 abs_refname = (char *)refname;
331 linelen = getline(&line, &linesize, f);
335 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
338 if (linelen > 0 && line[linelen - 1] == '\n')
339 line[linelen - 1] = '\0';
340 for (i = 0; i < nsubdirs; i++) {
341 if (!ref_is_absolute &&
342 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
344 return got_error_from_errno("asprintf");
345 err = parse_packed_ref_line(ref, abs_refname, line,
347 if (!ref_is_absolute)
349 if (err || *ref != NULL)
354 } while (*ref == NULL);
360 static const struct got_error *
361 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
362 const char *name, int lock)
364 const struct got_error *err = NULL;
366 char *absname = NULL;
367 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
368 int ref_is_well_known = (subdir[0] == '\0' && is_well_known_ref(name));
372 if (!got_ref_name_is_valid(name))
373 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
375 if (ref_is_absolute || ref_is_well_known) {
376 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
377 return got_error_from_errno("asprintf");
378 absname = (char *)name;
380 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
381 subdir[0] ? "/" : "", name) == -1)
382 return got_error_from_errno("asprintf");
384 if (asprintf(&absname, "refs/%s%s%s",
385 subdir, subdir[0] ? "/" : "", name) == -1) {
386 err = got_error_from_errno("asprintf");
391 err = parse_ref_file(ref, name, absname, path, lock);
393 if (!ref_is_absolute && !ref_is_well_known)
399 const struct got_error *
400 got_ref_open(struct got_reference **ref, struct got_repository *repo,
401 const char *refname, int lock)
403 const struct got_error *err = NULL;
404 char *packed_refs_path = NULL, *path_refs = NULL;
405 const char *subdirs[] = {
406 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
409 int well_known = is_well_known_ref(refname);
410 struct got_lockfile *lf = NULL;
414 path_refs = get_refs_dir_path(repo, refname);
415 if (path_refs == NULL) {
416 err = got_error_from_errno2("get_refs_dir_path", refname);
421 err = open_ref(ref, path_refs, "", refname, lock);
425 /* Search on-disk refs before packed refs! */
426 for (i = 0; i < nitems(subdirs); i++) {
427 err = open_ref(ref, path_refs, subdirs[i], refname,
429 if ((err && err->code != GOT_ERR_NOT_REF) || *ref)
433 packed_refs_path = got_repo_get_path_packed_refs(repo);
434 if (packed_refs_path == NULL) {
435 err = got_error_from_errno(
436 "got_repo_get_path_packed_refs");
441 err = got_lockfile_lock(&lf, packed_refs_path, -1);
445 f = fopen(packed_refs_path, "rbe");
448 if (fstat(fileno(f), &sb) == -1) {
449 err = got_error_from_errno2("fstat",
453 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
454 refname, sb.st_mtime);
456 if (fclose(f) == EOF) {
457 err = got_error_from_errno("fclose");
466 if (!err && *ref == NULL)
467 err = got_error_not_ref(refname);
469 got_lockfile_unlock(lf, -1);
470 free(packed_refs_path);
476 got_ref_close(struct got_reference *ref)
478 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
479 free(ref->ref.symref.name);
480 free(ref->ref.symref.ref);
482 free(ref->ref.ref.name);
486 struct got_reference *
487 got_ref_dup(struct got_reference *ref)
489 struct got_reference *ret;
491 ret = calloc(1, sizeof(*ret));
495 ret->flags = ref->flags;
496 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
497 ret->ref.symref.name = strdup(ref->ref.symref.name);
498 if (ret->ref.symref.name == NULL) {
502 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
503 if (ret->ref.symref.ref == NULL) {
504 free(ret->ref.symref.name);
509 ret->ref.ref.name = strdup(ref->ref.ref.name);
510 if (ret->ref.ref.name == NULL) {
514 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
515 sizeof(ret->ref.ref.sha1));
521 const struct got_error *
522 got_reflist_entry_dup(struct got_reflist_entry **newp,
523 struct got_reflist_entry *re)
525 const struct got_error *err = NULL;
526 struct got_reflist_entry *new;
530 new = malloc(sizeof(*new));
532 return got_error_from_errno("malloc");
534 new->ref = got_ref_dup(re->ref);
535 if (new->ref == NULL) {
536 err = got_error_from_errno("got_ref_dup");
545 const struct got_error *
546 got_ref_resolve_symbolic(struct got_reference **resolved,
547 struct got_repository *repo, struct got_reference *ref)
549 struct got_reference *nextref;
550 const struct got_error *err;
552 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
556 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
557 err = got_ref_resolve_symbolic(resolved, repo, nextref);
559 *resolved = got_ref_dup(nextref);
561 got_ref_close(nextref);
565 static const struct got_error *
566 ref_resolve(struct got_object_id **id, struct got_repository *repo,
567 struct got_reference *ref, int recursion)
569 const struct got_error *err;
572 return got_error_msg(GOT_ERR_RECURSION,
573 "reference recursion limit reached");
575 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
576 struct got_reference *resolved = NULL;
577 err = got_ref_resolve_symbolic(&resolved, repo, ref);
579 err = ref_resolve(id, repo, resolved, --recursion);
581 got_ref_close(resolved);
585 *id = calloc(1, sizeof(**id));
587 return got_error_from_errno("calloc");
588 memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
592 const struct got_error *
593 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
594 struct got_reference *ref)
596 return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
600 got_ref_to_str(struct got_reference *ref)
604 if (ref->flags & GOT_REF_IS_SYMBOLIC)
605 return strdup(ref->ref.symref.ref);
607 str = malloc(SHA1_DIGEST_STRING_LENGTH);
611 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
612 SHA1_DIGEST_STRING_LENGTH) == NULL) {
621 got_ref_get_name(struct got_reference *ref)
623 if (ref->flags & GOT_REF_IS_SYMBOLIC)
624 return ref->ref.symref.name;
626 return ref->ref.ref.name;
630 got_ref_get_symref_target(struct got_reference *ref)
632 if (ref->flags & GOT_REF_IS_SYMBOLIC)
633 return ref->ref.symref.ref;
639 got_ref_get_mtime(struct got_reference *ref)
644 const struct got_error *
645 got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
646 struct got_reference* re2)
648 const char *name1 = got_ref_get_name(re1);
649 const char *name2 = got_ref_get_name(re2);
651 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
655 const struct got_error *
656 got_ref_cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
657 struct got_reference *ref2)
659 const struct got_error *err = NULL;
660 struct got_repository *repo = arg;
661 struct got_object_id *id1, *id2 = NULL;
662 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
663 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
668 err = got_ref_resolve(&id1, repo, ref1);
671 err = got_object_open_as_tag(&tag1, repo, id1);
673 if (err->code != GOT_ERR_OBJ_TYPE)
675 /* "lightweight" tag */
676 err = got_object_open_as_commit(&commit1, repo, id1);
679 time1 = got_object_commit_get_committer_time(commit1);
681 time1 = got_object_tag_get_tagger_time(tag1);
683 err = got_ref_resolve(&id2, repo, ref2);
686 err = got_object_open_as_tag(&tag2, repo, id2);
688 if (err->code != GOT_ERR_OBJ_TYPE)
690 /* "lightweight" tag */
691 err = got_object_open_as_commit(&commit2, repo, id2);
694 time2 = got_object_commit_get_committer_time(commit2);
696 time2 = got_object_tag_get_tagger_time(tag2);
698 /* Put latest tags first. */
701 else if (time1 > time2)
704 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
709 got_object_tag_close(tag1);
711 got_object_tag_close(tag2);
713 got_object_commit_close(commit1);
715 got_object_commit_close(commit2);
719 static const struct got_error *
720 get_committer_time(struct got_reference *ref, struct got_repository *repo)
722 const struct got_error *err = NULL;
724 struct got_commit_object *commit = NULL;
725 struct got_tag_object *tag = NULL;
726 struct got_object_id *id = NULL;
728 err = got_ref_resolve(&id, repo, ref);
732 err = got_object_get_type(&obj_type, repo, id);
737 case GOT_OBJ_TYPE_COMMIT:
738 err = got_object_open_as_commit(&commit, repo, id);
741 ref->committer_time =
742 got_object_commit_get_committer_time(commit);
744 case GOT_OBJ_TYPE_TAG:
745 err = got_object_open_as_tag(&tag, repo, id);
748 ref->committer_time = got_object_tag_get_tagger_time(tag);
751 /* best effort for other object types */
752 ref->committer_time = got_ref_get_mtime(ref);
758 got_object_commit_close(commit);
760 got_object_tag_close(tag);
764 const struct got_error *
765 got_ref_cmp_by_commit_timestamp_descending(void *arg, int *cmp,
766 struct got_reference *ref1, struct got_reference *ref2)
768 const struct got_error *err = NULL;
769 struct got_repository *repo = arg;
773 if (ref1->committer_time == 0) {
774 err = get_committer_time(ref1, repo);
778 if (ref2->committer_time == 0) {
779 err = get_committer_time(ref2, repo);
784 if (ref1->committer_time < ref2->committer_time)
786 else if (ref2->committer_time < ref1->committer_time)
789 return got_ref_cmp_by_name(arg, cmp, ref1, ref2);
794 const struct got_error *
795 got_reflist_insert(struct got_reflist_entry **newp,
796 struct got_reflist_head *refs, struct got_reference *ref,
797 got_ref_cmp_cb cmp_cb, void *cmp_arg)
799 const struct got_error *err;
800 struct got_reflist_entry *new, *re;
805 if (cmp_cb != got_ref_cmp_by_name && (ref->flags & GOT_REF_IS_PACKED)) {
807 * If we are not sorting elements by name then we must still
808 * detect collisions between a packed ref and an on-disk ref
809 * using the same name. On-disk refs take precedence and are
810 * already present on the list before packed refs get added.
812 TAILQ_FOREACH(re, refs, entry) {
813 err = got_ref_cmp_by_name(NULL, &cmp, re->ref, ref);
821 new = malloc(sizeof(*new));
823 return got_error_from_errno("malloc");
828 * We must de-duplicate entries on insert because packed-refs may
829 * contain redundant entries. On-disk refs take precedence.
830 * This code assumes that on-disk revs are read before packed-refs.
831 * We're iterating the list anyway, so insert elements sorted by name.
833 * Many callers will provide paths in a somewhat sorted order.
834 * Iterating backwards from the tail of the list should be more
835 * efficient than traversing through the entire list each time
836 * an element is inserted.
838 re = TAILQ_LAST(refs, got_reflist_head);
840 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
848 } else if (cmp < 0) {
849 TAILQ_INSERT_AFTER(refs, re, new, entry);
852 re = TAILQ_PREV(re, got_reflist_head, entry);
855 TAILQ_INSERT_HEAD(refs, new, entry);
859 const struct got_error *
860 got_reflist_sort(struct got_reflist_head *refs,
861 got_ref_cmp_cb cmp_cb, void *cmp_arg)
863 const struct got_error *err = NULL;
864 struct got_reflist_entry *re, *tmp, *new;
865 struct got_reflist_head sorted;
869 TAILQ_FOREACH_SAFE(re, refs, entry, tmp) {
870 struct got_reference *ref = re->ref;
871 TAILQ_REMOVE(refs, re, entry);
873 err = got_reflist_insert(&new, &sorted, ref, cmp_cb, cmp_arg);
874 if (err || new == NULL /* duplicate */)
880 TAILQ_CONCAT(refs, &sorted, entry);
884 static const struct got_error *
885 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
886 const char *subdir, struct got_repository *repo,
887 got_ref_cmp_cb cmp_cb, void *cmp_arg)
889 const struct got_error *err = NULL;
893 while (subdir[0] == '/')
896 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
897 return got_error_from_errno("asprintf");
899 d = opendir(path_subdir);
905 struct got_reference *ref;
913 if (strcmp(dent->d_name, ".") == 0 ||
914 strcmp(dent->d_name, "..") == 0)
917 err = got_path_dirent_type(&type, path_subdir, dent);
923 err = open_ref(&ref, path_refs, subdir, dent->d_name,
928 struct got_reflist_entry *new;
929 err = got_reflist_insert(&new, refs, ref,
931 if (err || new == NULL /* duplicate */)
938 if (asprintf(&child, "%s%s%s", subdir,
939 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
940 err = got_error_from_errno("asprintf");
943 err = gather_on_disk_refs(refs, path_refs, child, repo,
958 const struct got_error *
959 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
960 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
962 const struct got_error *err;
963 char *packed_refs_path = NULL, *path_refs = NULL;
964 char *abs_namespace = NULL, *buf = NULL;
965 const char *ondisk_ref_namespace = NULL;
968 struct got_reference *ref;
969 struct got_reflist_entry *new;
971 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
972 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
973 if (path_refs == NULL) {
974 err = got_error_from_errno("get_refs_dir_path");
977 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
980 err = got_reflist_insert(&new, refs, ref, cmp_cb, cmp_arg);
981 if (err || new == NULL /* duplicate */)
983 if (err && err->code != GOT_ERR_NOT_REF)
986 /* Try listing a single reference. */
987 const char *refname = ref_namespace;
988 path_refs = get_refs_dir_path(repo, refname);
989 if (path_refs == NULL) {
990 err = got_error_from_errno("get_refs_dir_path");
993 err = open_ref(&ref, path_refs, "", refname, 0);
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,
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);
1124 got_ref_is_symbolic(struct got_reference *ref)
1126 return (ref->flags & GOT_REF_IS_SYMBOLIC);
1129 const struct got_error *
1130 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1132 if (ref->flags & GOT_REF_IS_SYMBOLIC)
1133 return got_error(GOT_ERR_BAD_REF_TYPE);
1135 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
1139 const struct got_error *
1140 got_ref_change_symref(struct got_reference *ref, const char *refname)
1144 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1145 return got_error(GOT_ERR_BAD_REF_TYPE);
1147 new_name = strdup(refname);
1148 if (new_name == NULL)
1149 return got_error_from_errno("strdup");
1151 free(ref->ref.symref.ref);
1152 ref->ref.symref.ref = new_name;
1156 const struct got_error *
1157 got_ref_change_symref_to_ref(struct got_reference *symref,
1158 struct got_object_id *id)
1160 if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1161 return got_error(GOT_ERR_BAD_REF_TYPE);
1163 symref->ref.ref.name = symref->ref.symref.name;
1164 memcpy(symref->ref.ref.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1165 symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1169 const struct got_error *
1170 got_ref_write(struct got_reference *ref, struct got_repository *repo)
1172 const struct got_error *err = NULL, *unlock_err = NULL;
1173 const char *name = got_ref_get_name(ref);
1174 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1175 struct got_lockfile *lf = NULL;
1180 path_refs = get_refs_dir_path(repo, name);
1181 if (path_refs == NULL) {
1182 err = got_error_from_errno2("get_refs_dir_path", name);
1186 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1187 err = got_error_from_errno("asprintf");
1191 err = got_opentemp_named(&tmppath, &f, path, "");
1194 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1196 err = got_path_dirname(&parent, path);
1199 err = got_path_mkdir(parent);
1203 err = got_opentemp_named(&tmppath, &f, path, "");
1208 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1209 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1210 if (n != strlen(ref->ref.symref.ref) + 6) {
1211 err = got_ferror(f, GOT_ERR_IO);
1215 char hex[SHA1_DIGEST_STRING_LENGTH];
1216 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
1217 sizeof(hex)) == NULL) {
1218 err = got_error(GOT_ERR_BAD_REF_DATA);
1221 n = fprintf(f, "%s\n", hex);
1222 if (n != sizeof(hex)) {
1223 err = got_ferror(f, GOT_ERR_IO);
1228 if (ref->lf == NULL) {
1229 err = got_lockfile_lock(&lf, path, -1);
1234 /* XXX: check if old content matches our expectations? */
1236 if (stat(path, &sb) != 0) {
1237 if (errno != ENOENT) {
1238 err = got_error_from_errno2("stat", path);
1241 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1244 if (fchmod(fileno(f), sb.st_mode) != 0) {
1245 err = got_error_from_errno2("fchmod", tmppath);
1249 if (rename(tmppath, path) != 0) {
1250 err = got_error_from_errno3("rename", tmppath, path);
1256 if (stat(path, &sb) == -1) {
1257 err = got_error_from_errno2("stat", path);
1260 ref->mtime = sb.st_mtime;
1262 if (ref->lf == NULL && lf)
1263 unlock_err = got_lockfile_unlock(lf, -1);
1265 if (fclose(f) == EOF && err == NULL)
1266 err = got_error_from_errno("fclose");
1271 if (unlink(tmppath) == -1 && err == NULL)
1272 err = got_error_from_errno2("unlink", tmppath);
1275 return err ? err : unlock_err;
1278 static const struct got_error *
1279 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1281 const struct got_error *err = NULL, *unlock_err = NULL;
1282 struct got_lockfile *lf = NULL;
1283 FILE *f = NULL, *tmpf = NULL;
1284 char *line = NULL, *packed_refs_path, *tmppath = NULL;
1285 size_t linesize = 0;
1286 struct got_reflist_head refs;
1287 int found_delref = 0;
1289 /* The packed-refs file does not cotain symbolic references. */
1290 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1291 return got_error(GOT_ERR_BAD_REF_DATA);
1295 packed_refs_path = got_repo_get_path_packed_refs(repo);
1296 if (packed_refs_path == NULL)
1297 return got_error_from_errno("got_repo_get_path_packed_refs");
1299 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path, "");
1303 if (delref->lf == NULL) {
1304 err = got_lockfile_lock(&lf, packed_refs_path, -1);
1309 f = fopen(packed_refs_path, "re");
1311 err = got_error_from_errno2("fopen", packed_refs_path);
1316 struct got_reference *ref;
1317 struct got_reflist_entry *new;
1319 linelen = getline(&line, &linesize, f);
1320 if (linelen == -1) {
1323 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1326 if (linelen > 0 && line[linelen - 1] == '\n')
1327 line[linelen - 1] = '\0';
1328 err = parse_packed_ref_line(&ref, NULL, line, 0);
1334 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1335 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1336 sizeof(delref->ref.ref.sha1)) == 0) {
1342 err = got_reflist_insert(&new, &refs, ref,
1343 got_ref_cmp_by_name, NULL);
1344 if (err || new == NULL /* duplicate */)
1351 struct got_reflist_entry *re;
1355 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1356 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1357 err = got_ferror(f, GOT_ERR_IO);
1361 TAILQ_FOREACH(re, &refs, entry) {
1362 char hex[SHA1_DIGEST_STRING_LENGTH];
1364 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1365 sizeof(hex)) == NULL) {
1366 err = got_error(GOT_ERR_BAD_REF_DATA);
1369 n = fprintf(tmpf, "%s ", hex);
1370 if (n != sizeof(hex)) {
1371 err = got_ferror(f, GOT_ERR_IO);
1374 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1375 if (n != strlen(re->ref->ref.ref.name) + 1) {
1376 err = got_ferror(f, GOT_ERR_IO);
1381 if (fflush(tmpf) != 0) {
1382 err = got_error_from_errno("fflush");
1386 if (fstat(fileno(f), &sb) != 0) {
1387 if (errno != ENOENT) {
1388 err = got_error_from_errno2("fstat",
1392 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1395 if (fchmod(fileno(tmpf), sb.st_mode) != 0) {
1396 err = got_error_from_errno2("fchmod", tmppath);
1400 if (rename(tmppath, packed_refs_path) != 0) {
1401 err = got_error_from_errno3("rename", tmppath,
1409 if (delref->lf == NULL && lf)
1410 unlock_err = got_lockfile_unlock(lf, -1);
1412 if (fclose(f) == EOF && err == NULL)
1413 err = got_error_from_errno("fclose");
1415 if (tmppath && unlink(tmppath) == -1 && err == NULL)
1416 err = got_error_from_errno2("unlink", tmppath);
1417 if (tmpf && fclose(tmpf) == EOF && err == NULL)
1418 err = got_error_from_errno("fclose");
1420 free(packed_refs_path);
1422 got_ref_list_free(&refs);
1423 return err ? err : unlock_err;
1426 static const struct got_error *
1427 delete_loose_ref(struct got_reference *ref, struct got_repository *repo)
1429 const struct got_error *err = NULL, *unlock_err = NULL;
1430 const char *name = got_ref_get_name(ref);
1431 char *path_refs = NULL, *path = NULL;
1432 struct got_lockfile *lf = NULL;
1434 path_refs = get_refs_dir_path(repo, name);
1435 if (path_refs == NULL) {
1436 err = got_error_from_errno2("get_refs_dir_path", name);
1440 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1441 err = got_error_from_errno("asprintf");
1445 if (ref->lf == NULL) {
1446 err = got_lockfile_lock(&lf, path, -1);
1451 /* XXX: check if old content matches our expectations? */
1453 if (unlink(path) == -1)
1454 err = got_error_from_errno2("unlink", path);
1456 if (ref->lf == NULL && lf)
1457 unlock_err = got_lockfile_unlock(lf, -1);
1461 return err ? err : unlock_err;
1464 const struct got_error *
1465 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1467 const struct got_error *err = NULL;
1468 struct got_reference *ref2;
1470 if (ref->flags & GOT_REF_IS_PACKED) {
1471 err = delete_packed_ref(ref, repo);
1475 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1477 if (err->code == GOT_ERR_NOT_REF)
1482 err = delete_loose_ref(ref2, repo);
1483 got_ref_close(ref2);
1486 err = delete_loose_ref(ref, repo);
1490 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1492 if (err->code == GOT_ERR_NOT_REF)
1497 err = delete_packed_ref(ref2, repo);
1498 got_ref_close(ref2);
1503 const struct got_error *
1504 got_ref_unlock(struct got_reference *ref)
1506 const struct got_error *err;
1507 err = got_lockfile_unlock(ref->lf, -1);
1512 struct got_reflist_object_id_map {
1513 struct got_object_idset *idset;
1516 struct got_reflist_object_id_map_entry {
1517 struct got_reflist_head refs;
1520 static const struct got_error *
1521 add_object_id_map_entry(struct got_object_idset *idset,
1522 struct got_object_id *id, struct got_reflist_entry *re)
1524 const struct got_error *err = NULL;
1525 struct got_reflist_object_id_map_entry *ent;
1526 struct got_reflist_entry *new;
1528 ent = got_object_idset_get(idset, id);
1530 ent = malloc(sizeof(*ent));
1532 return got_error_from_errno("malloc");
1534 TAILQ_INIT(&ent->refs);
1535 err = got_object_idset_add(idset, id, ent);
1542 err = got_reflist_entry_dup(&new, re);
1546 TAILQ_INSERT_TAIL(&ent->refs, new, entry);
1550 const struct got_error *
1551 got_reflist_object_id_map_create(struct got_reflist_object_id_map **map,
1552 struct got_reflist_head *refs, struct got_repository *repo)
1554 const struct got_error *err = NULL;
1555 struct got_object_idset *idset;
1556 struct got_object_id *id = NULL;
1557 struct got_reflist_entry *re;
1559 idset = got_object_idset_alloc();
1561 return got_error_from_errno("got_object_idset_alloc");
1563 *map = malloc(sizeof(**map));
1565 got_object_idset_free(idset);
1566 return got_error_from_errno("malloc");
1568 (*map)->idset = idset;
1570 TAILQ_FOREACH(re, refs, entry) {
1571 struct got_tag_object *tag = NULL;
1573 err = got_ref_resolve(&id, repo, re->ref);
1577 err = add_object_id_map_entry(idset, id, re);
1581 if (strstr(got_ref_get_name(re->ref), "/tags/") == NULL) {
1587 err = got_object_open_as_tag(&tag, repo, id);
1589 if (err->code != GOT_ERR_OBJ_TYPE)
1591 /* Ref points at something other than a tag. */
1599 err = add_object_id_map_entry(idset,
1600 got_object_tag_get_object_id(tag), re);
1601 got_object_tag_close(tag);
1611 got_reflist_object_id_map_free(*map);
1617 struct got_reflist_head *
1618 got_reflist_object_id_map_lookup(struct got_reflist_object_id_map *map,
1619 struct got_object_id *id)
1621 struct got_reflist_object_id_map_entry *ent;
1622 ent = got_object_idset_get(map->idset, id);
1628 static const struct got_error *
1629 free_id_map_entry(struct got_object_id *id, void *data, void *arg)
1631 struct got_reflist_object_id_map_entry *ent = data;
1633 got_ref_list_free(&ent->refs);
1639 got_reflist_object_id_map_free(struct got_reflist_object_id_map *map)
1641 got_object_idset_for_each(map->idset, free_id_map_entry, NULL);
1642 got_object_idset_free(map->idset);