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>
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);
271 got_ref_name_is_valid(const char *name)
274 const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
275 const char *forbidden_seq[] = { "//", "..", "@{" };
276 const char *lfs = GOT_LOCKFILE_SUFFIX;
277 const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
280 if (name[0] == '@' && name[1] == '\0')
285 if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
288 for (i = 0; i < nitems(forbidden); i++) {
289 if (*s == forbidden[i])
292 for (i = 0; i < nitems(forbidden_seq); i++) {
293 if (s[0] == forbidden_seq[i][0] &&
294 s[1] == forbidden_seq[i][1])
297 if (iscntrl((unsigned char)s[0]))
299 if (s[0] == '.' && s[1] == '\0')
302 const char *nextseg = s + 1;
303 if (nextseg[0] == '\0' || nextseg[0] == '.' ||
306 if (seg <= s - lfs_len &&
307 strncmp(s - lfs_len, lfs, lfs_len) == 0)
314 if (seg <= s - lfs_len &&
315 strncmp(s - lfs_len, lfs, lfs_len) == 0)
321 const struct got_error *
322 got_ref_alloc(struct got_reference **ref, const char *name,
323 struct got_object_id *id)
325 if (!got_ref_name_is_valid(name))
326 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
328 return alloc_ref(ref, name, id, 0, 0);
331 const struct got_error *
332 got_ref_alloc_symref(struct got_reference **ref, const char *name,
333 struct got_reference *target_ref)
335 if (!got_ref_name_is_valid(name))
336 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
338 return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
341 static const struct got_error *
342 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
343 const char *line, time_t mtime)
345 struct got_object_id id;
350 if (line[0] == '#' || line[0] == '^')
353 if (!got_parse_sha1_digest(id.sha1, line))
354 return got_error(GOT_ERR_BAD_REF_DATA);
357 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
361 name = line + SHA1_DIGEST_STRING_LENGTH;
363 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED, mtime);
366 static const struct got_error *
367 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
368 int nsubdirs, const char *refname, time_t mtime)
370 const struct got_error *err = NULL;
375 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
380 abs_refname = (char *)refname;
382 linelen = getline(&line, &linesize, f);
386 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
389 if (linelen > 0 && line[linelen - 1] == '\n')
390 line[linelen - 1] = '\0';
391 for (i = 0; i < nsubdirs; i++) {
392 if (!ref_is_absolute &&
393 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
395 return got_error_from_errno("asprintf");
396 err = parse_packed_ref_line(ref, abs_refname, line,
398 if (!ref_is_absolute)
400 if (err || *ref != NULL)
405 } while (*ref == NULL);
411 static const struct got_error *
412 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
413 const char *name, int lock)
415 const struct got_error *err = NULL;
417 char *absname = NULL;
418 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
419 int ref_is_well_known = (subdir[0] == '\0' && is_well_known_ref(name));
423 if (!got_ref_name_is_valid(name))
424 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
426 if (ref_is_absolute || ref_is_well_known) {
427 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
428 return got_error_from_errno("asprintf");
429 absname = (char *)name;
431 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
432 subdir[0] ? "/" : "", name) == -1)
433 return got_error_from_errno("asprintf");
435 if (asprintf(&absname, "refs/%s%s%s",
436 subdir, subdir[0] ? "/" : "", name) == -1) {
437 err = got_error_from_errno("asprintf");
442 err = parse_ref_file(ref, name, absname, path, lock);
444 if (!ref_is_absolute && !ref_is_well_known)
450 const struct got_error *
451 got_ref_open(struct got_reference **ref, struct got_repository *repo,
452 const char *refname, int lock)
454 const struct got_error *err = NULL;
455 char *path_refs = NULL;
456 const char *subdirs[] = {
457 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
460 int well_known = is_well_known_ref(refname);
461 struct got_lockfile *lf = NULL;
465 path_refs = get_refs_dir_path(repo, refname);
466 if (path_refs == NULL) {
467 err = got_error_from_errno2("get_refs_dir_path", refname);
472 err = open_ref(ref, path_refs, "", refname, lock);
474 char *packed_refs_path;
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");
498 free(packed_refs_path);
501 if (fstat(fileno(f), &sb) == -1) {
502 err = got_error_from_errno2("fstat",
506 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
507 refname, sb.st_mtime);
509 if (fclose(f) == EOF) {
510 err = got_error_from_errno("fclose");
519 if (!err && *ref == NULL)
520 err = got_error_not_ref(refname);
522 got_lockfile_unlock(lf, -1);
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 new = malloc(sizeof(*new));
859 return got_error_from_errno("malloc");
864 * We must de-duplicate entries on insert because packed-refs may
865 * contain redundant entries. On-disk refs take precedence.
866 * This code assumes that on-disk revs are read before packed-refs.
867 * We're iterating the list anyway, so insert elements sorted by name.
869 * Many callers will provide paths in a somewhat sorted order.
870 * Iterating backwards from the tail of the list should be more
871 * efficient than traversing through the entire list each time
872 * an element is inserted.
874 re = TAILQ_LAST(refs, got_reflist_head);
876 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
884 } else if (cmp < 0) {
885 TAILQ_INSERT_AFTER(refs, re, new, entry);
888 re = TAILQ_PREV(re, got_reflist_head, entry);
891 TAILQ_INSERT_HEAD(refs, new, entry);
895 const struct got_error *
896 got_reflist_sort(struct got_reflist_head *refs,
897 got_ref_cmp_cb cmp_cb, void *cmp_arg)
899 const struct got_error *err = NULL;
900 struct got_reflist_entry *re, *tmp, *new;
901 struct got_reflist_head sorted;
905 TAILQ_FOREACH_SAFE(re, refs, entry, tmp) {
906 struct got_reference *ref = re->ref;
907 TAILQ_REMOVE(refs, re, entry);
909 err = got_reflist_insert(&new, &sorted, ref, cmp_cb, cmp_arg);
910 if (err || new == NULL /* duplicate */)
916 TAILQ_CONCAT(refs, &sorted, entry);
920 static const struct got_error *
921 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
922 const char *subdir, struct got_repository *repo,
923 got_ref_cmp_cb cmp_cb, void *cmp_arg)
925 const struct got_error *err = NULL;
929 while (subdir[0] == '/')
932 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
933 return got_error_from_errno("asprintf");
935 d = opendir(path_subdir);
941 struct got_reference *ref;
949 if (strcmp(dent->d_name, ".") == 0 ||
950 strcmp(dent->d_name, "..") == 0)
953 err = got_path_dirent_type(&type, path_subdir, dent);
959 err = open_ref(&ref, path_refs, subdir, dent->d_name,
964 struct got_reflist_entry *new;
965 err = got_reflist_insert(&new, refs, ref,
967 if (err || new == NULL /* duplicate */)
974 if (asprintf(&child, "%s%s%s", subdir,
975 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
976 err = got_error_from_errno("asprintf");
979 err = gather_on_disk_refs(refs, path_refs, child, repo,
994 const struct got_error *
995 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
996 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
998 const struct got_error *err;
999 char *packed_refs_path, *path_refs = NULL;
1000 char *abs_namespace = NULL;
1001 char *buf = NULL, *ondisk_ref_namespace = NULL;
1004 struct got_reference *ref;
1005 struct got_reflist_entry *new;
1007 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
1008 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
1009 if (path_refs == NULL) {
1010 err = got_error_from_errno("get_refs_dir_path");
1013 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
1016 err = got_reflist_insert(&new, refs, ref, cmp_cb, cmp_arg);
1017 if (err || new == NULL /* duplicate */)
1019 if (err && err->code != GOT_ERR_NOT_REF)
1022 /* Try listing a single reference. */
1023 const char *refname = ref_namespace;
1024 path_refs = get_refs_dir_path(repo, refname);
1025 if (path_refs == NULL) {
1026 err = got_error_from_errno("get_refs_dir_path");
1029 err = open_ref(&ref, path_refs, "", refname, 0);
1031 if (err->code != GOT_ERR_NOT_REF)
1033 /* Try to look up references in a given namespace. */
1035 err = got_reflist_insert(&new, refs, ref,
1037 if (err || new == NULL /* duplicate */)
1043 if (ref_namespace) {
1045 /* Canonicalize the path to eliminate double-slashes if any. */
1046 if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
1047 err = got_error_from_errno("asprintf");
1050 len = strlen(abs_namespace) + 1;
1053 err = got_error_from_errno("malloc");
1056 err = got_canonpath(abs_namespace, buf, len);
1059 ondisk_ref_namespace = buf;
1060 while (ondisk_ref_namespace[0] == '/')
1061 ondisk_ref_namespace++;
1062 if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
1063 ondisk_ref_namespace += 5;
1064 else if (strcmp(ondisk_ref_namespace, "refs") == 0)
1065 ondisk_ref_namespace = "";
1068 /* Gather on-disk refs before parsing packed-refs. */
1070 path_refs = get_refs_dir_path(repo, "");
1071 if (path_refs == NULL) {
1072 err = got_error_from_errno("get_refs_dir_path");
1075 err = gather_on_disk_refs(refs, path_refs,
1076 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
1082 * The packed-refs file may contain redundant entries, in which
1083 * case on-disk refs take precedence.
1085 packed_refs_path = got_repo_get_path_packed_refs(repo);
1086 if (packed_refs_path == NULL) {
1087 err = got_error_from_errno("got_repo_get_path_packed_refs");
1091 f = fopen(packed_refs_path, "re");
1092 free(packed_refs_path);
1094 size_t linesize = 0;
1098 if (fstat(fileno(f), &sb) == -1) {
1099 err = got_error_from_errno2("fstat", packed_refs_path);
1103 linelen = getline(&line, &linesize, f);
1104 if (linelen == -1) {
1107 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1110 if (linelen > 0 && line[linelen - 1] == '\n')
1111 line[linelen - 1] = '\0';
1112 err = parse_packed_ref_line(&ref, NULL, line,
1117 if (ref_namespace) {
1119 name = got_ref_get_name(ref);
1120 if (!got_path_is_child(name,
1122 strlen(ref_namespace))) {
1127 err = got_reflist_insert(&new, refs, ref,
1129 if (err || new == NULL /* duplicate */)
1137 free(abs_namespace);
1141 if (f && fclose(f) == EOF && err == NULL)
1142 err = got_error_from_errno("fclose");
1147 got_ref_list_free(struct got_reflist_head *refs)
1149 struct got_reflist_entry *re;
1151 while ((re = TAILQ_FIRST(refs))) {
1152 TAILQ_REMOVE(refs, re, entry);
1153 got_ref_close(re->ref);
1160 got_ref_is_symbolic(struct got_reference *ref)
1162 return (ref->flags & GOT_REF_IS_SYMBOLIC);
1165 const struct got_error *
1166 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1168 if (ref->flags & GOT_REF_IS_SYMBOLIC)
1169 return got_error(GOT_ERR_BAD_REF_TYPE);
1171 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
1175 const struct got_error *
1176 got_ref_change_symref(struct got_reference *ref, const char *refname)
1180 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1181 return got_error(GOT_ERR_BAD_REF_TYPE);
1183 new_name = strdup(refname);
1184 if (new_name == NULL)
1185 return got_error_from_errno("strdup");
1187 free(ref->ref.symref.ref);
1188 ref->ref.symref.ref = new_name;
1192 const struct got_error *
1193 got_ref_change_symref_to_ref(struct got_reference *symref,
1194 struct got_object_id *id)
1196 if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1197 return got_error(GOT_ERR_BAD_REF_TYPE);
1199 symref->ref.ref.name = symref->ref.symref.name;
1200 memcpy(symref->ref.ref.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1201 symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1205 const struct got_error *
1206 got_ref_write(struct got_reference *ref, struct got_repository *repo)
1208 const struct got_error *err = NULL, *unlock_err = NULL;
1209 const char *name = got_ref_get_name(ref);
1210 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1211 struct got_lockfile *lf = NULL;
1216 path_refs = get_refs_dir_path(repo, name);
1217 if (path_refs == NULL) {
1218 err = got_error_from_errno2("get_refs_dir_path", name);
1222 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1223 err = got_error_from_errno("asprintf");
1227 err = got_opentemp_named(&tmppath, &f, path);
1230 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1232 err = got_path_dirname(&parent, path);
1235 err = got_path_mkdir(parent);
1239 err = got_opentemp_named(&tmppath, &f, path);
1244 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1245 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1246 if (n != strlen(ref->ref.symref.ref) + 6) {
1247 err = got_ferror(f, GOT_ERR_IO);
1251 char hex[SHA1_DIGEST_STRING_LENGTH];
1252 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
1253 sizeof(hex)) == NULL) {
1254 err = got_error(GOT_ERR_BAD_REF_DATA);
1257 n = fprintf(f, "%s\n", hex);
1258 if (n != sizeof(hex)) {
1259 err = got_ferror(f, GOT_ERR_IO);
1264 if (ref->lf == NULL) {
1265 err = got_lockfile_lock(&lf, path, -1);
1270 /* XXX: check if old content matches our expectations? */
1272 if (stat(path, &sb) != 0) {
1273 if (errno != ENOENT) {
1274 err = got_error_from_errno2("stat", path);
1277 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1280 if (fchmod(fileno(f), sb.st_mode) != 0) {
1281 err = got_error_from_errno2("fchmod", tmppath);
1285 if (rename(tmppath, path) != 0) {
1286 err = got_error_from_errno3("rename", tmppath, path);
1292 if (stat(path, &sb) == -1) {
1293 err = got_error_from_errno2("stat", path);
1296 ref->mtime = sb.st_mtime;
1298 if (ref->lf == NULL && lf)
1299 unlock_err = got_lockfile_unlock(lf, -1);
1301 if (fclose(f) == EOF && err == NULL)
1302 err = got_error_from_errno("fclose");
1307 if (unlink(tmppath) != 0 && err == NULL)
1308 err = got_error_from_errno2("unlink", tmppath);
1311 return err ? err : unlock_err;
1314 static const struct got_error *
1315 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1317 const struct got_error *err = NULL, *unlock_err = NULL;
1318 struct got_lockfile *lf = NULL;
1319 FILE *f = NULL, *tmpf = NULL;
1320 char *line = NULL, *packed_refs_path, *tmppath = NULL;
1321 size_t linesize = 0;
1322 struct got_reflist_head refs;
1323 int found_delref = 0;
1325 /* The packed-refs file does not cotain symbolic references. */
1326 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1327 return got_error(GOT_ERR_BAD_REF_DATA);
1331 packed_refs_path = got_repo_get_path_packed_refs(repo);
1332 if (packed_refs_path == NULL)
1333 return got_error_from_errno("got_repo_get_path_packed_refs");
1335 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
1339 if (delref->lf == NULL) {
1340 err = got_lockfile_lock(&lf, packed_refs_path, -1);
1345 f = fopen(packed_refs_path, "re");
1347 err = got_error_from_errno2("fopen", packed_refs_path);
1352 struct got_reference *ref;
1353 struct got_reflist_entry *new;
1355 linelen = getline(&line, &linesize, f);
1356 if (linelen == -1) {
1359 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1362 if (linelen > 0 && line[linelen - 1] == '\n')
1363 line[linelen - 1] = '\0';
1364 err = parse_packed_ref_line(&ref, NULL, line, 0);
1370 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1371 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1372 sizeof(delref->ref.ref.sha1)) == 0) {
1378 err = got_reflist_insert(&new, &refs, ref,
1379 got_ref_cmp_by_name, NULL);
1380 if (err || new == NULL /* duplicate */)
1387 struct got_reflist_entry *re;
1391 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1392 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1393 err = got_ferror(f, GOT_ERR_IO);
1397 TAILQ_FOREACH(re, &refs, entry) {
1398 char hex[SHA1_DIGEST_STRING_LENGTH];
1400 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1401 sizeof(hex)) == NULL) {
1402 err = got_error(GOT_ERR_BAD_REF_DATA);
1405 n = fprintf(tmpf, "%s ", hex);
1406 if (n != sizeof(hex)) {
1407 err = got_ferror(f, GOT_ERR_IO);
1410 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1411 if (n != strlen(re->ref->ref.ref.name) + 1) {
1412 err = got_ferror(f, GOT_ERR_IO);
1417 if (fflush(tmpf) != 0) {
1418 err = got_error_from_errno("fflush");
1422 if (fstat(fileno(f), &sb) != 0) {
1423 if (errno != ENOENT) {
1424 err = got_error_from_errno2("fstat",
1428 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1431 if (fchmod(fileno(tmpf), sb.st_mode) != 0) {
1432 err = got_error_from_errno2("fchmod", tmppath);
1436 if (rename(tmppath, packed_refs_path) != 0) {
1437 err = got_error_from_errno3("rename", tmppath,
1443 if (delref->lf == NULL && lf)
1444 unlock_err = got_lockfile_unlock(lf, -1);
1446 if (fclose(f) == EOF && err == NULL)
1447 err = got_error_from_errno("fclose");
1451 if (fclose(tmpf) == EOF && err == NULL)
1452 err = got_error_from_errno("fclose");
1455 free(packed_refs_path);
1457 got_ref_list_free(&refs);
1458 return err ? err : unlock_err;
1461 static const struct got_error *
1462 delete_loose_ref(struct got_reference *ref, struct got_repository *repo)
1464 const struct got_error *err = NULL, *unlock_err = NULL;
1465 const char *name = got_ref_get_name(ref);
1466 char *path_refs = NULL, *path = NULL;
1467 struct got_lockfile *lf = NULL;
1469 path_refs = get_refs_dir_path(repo, name);
1470 if (path_refs == NULL) {
1471 err = got_error_from_errno2("get_refs_dir_path", name);
1475 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1476 err = got_error_from_errno("asprintf");
1480 if (ref->lf == NULL) {
1481 err = got_lockfile_lock(&lf, path, -1);
1486 /* XXX: check if old content matches our expectations? */
1488 if (unlink(path) != 0)
1489 err = got_error_from_errno2("unlink", path);
1491 if (ref->lf == NULL && lf)
1492 unlock_err = got_lockfile_unlock(lf, -1);
1496 return err ? err : unlock_err;
1499 const struct got_error *
1500 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1502 const struct got_error *err = NULL;
1503 struct got_reference *ref2;
1505 if (ref->flags & GOT_REF_IS_PACKED) {
1506 err = delete_packed_ref(ref, repo);
1510 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1512 if (err->code == GOT_ERR_NOT_REF)
1517 err = delete_loose_ref(ref2, repo);
1518 got_ref_close(ref2);
1521 err = delete_loose_ref(ref, repo);
1525 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1527 if (err->code == GOT_ERR_NOT_REF)
1532 err = delete_packed_ref(ref2, repo);
1533 got_ref_close(ref2);
1538 const struct got_error *
1539 got_ref_unlock(struct got_reference *ref)
1541 const struct got_error *err;
1542 err = got_lockfile_unlock(ref->lf, -1);
1547 struct got_reflist_object_id_map {
1548 struct got_object_idset *idset;
1551 struct got_reflist_object_id_map_entry {
1552 struct got_reflist_head refs;
1555 static const struct got_error *
1556 add_object_id_map_entry(struct got_object_idset *idset,
1557 struct got_object_id *id, struct got_reflist_entry *re)
1559 const struct got_error *err = NULL;
1560 struct got_reflist_object_id_map_entry *ent;
1561 struct got_reflist_entry *new;
1563 ent = got_object_idset_get(idset, id);
1565 ent = malloc(sizeof(*ent));
1567 return got_error_from_errno("malloc");
1569 TAILQ_INIT(&ent->refs);
1570 err = got_object_idset_add(idset, id, ent);
1577 err = got_reflist_entry_dup(&new, re);
1581 TAILQ_INSERT_TAIL(&ent->refs, new, entry);
1585 const struct got_error *
1586 got_reflist_object_id_map_create(struct got_reflist_object_id_map **map,
1587 struct got_reflist_head *refs, struct got_repository *repo)
1589 const struct got_error *err = NULL;
1590 struct got_object_idset *idset;
1591 struct got_object_id *id = NULL;
1592 struct got_reflist_entry *re;
1594 idset = got_object_idset_alloc();
1596 return got_error_from_errno("got_object_idset_alloc");
1598 *map = malloc(sizeof(**map));
1600 got_object_idset_free(idset);
1601 return got_error_from_errno("malloc");
1603 (*map)->idset = idset;
1605 TAILQ_FOREACH(re, refs, entry) {
1606 struct got_tag_object *tag = NULL;
1608 err = got_ref_resolve(&id, repo, re->ref);
1612 err = add_object_id_map_entry(idset, id, re);
1616 if (strstr(got_ref_get_name(re->ref), "/tags/") == NULL) {
1622 err = got_object_open_as_tag(&tag, repo, id);
1624 if (err->code != GOT_ERR_OBJ_TYPE)
1626 /* Ref points at something other than a tag. */
1634 err = add_object_id_map_entry(idset,
1635 got_object_tag_get_object_id(tag), re);
1636 got_object_tag_close(tag);
1646 got_reflist_object_id_map_free(*map);
1652 struct got_reflist_head *
1653 got_reflist_object_id_map_lookup(struct got_reflist_object_id_map *map,
1654 struct got_object_id *id)
1656 struct got_reflist_object_id_map_entry *ent;
1657 ent = got_object_idset_get(map->idset, id);
1663 static const struct got_error *
1664 free_id_map_entry(struct got_object_id *id, void *data, void *arg)
1666 struct got_reflist_object_id_map_entry *ent = data;
1668 got_ref_list_free(&ent->refs);
1674 got_reflist_object_id_map_free(struct got_reflist_object_id_map *map)
1676 got_object_idset_for_each(map->idset, free_id_map_entry, NULL);
1677 got_object_idset_free(map->idset);