2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/types.h>
18 #include <sys/queue.h>
35 #include "got_error.h"
36 #include "got_object.h"
37 #include "got_repository.h"
38 #include "got_reference.h"
39 #include "got_opentemp.h"
42 #include "got_lib_sha1.h"
43 #include "got_lib_delta.h"
44 #include "got_lib_inflate.h"
45 #include "got_lib_object.h"
46 #include "got_lib_object_idset.h"
47 #include "got_lib_lockfile.h"
50 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
53 #define GOT_REF_HEADS "heads"
54 #define GOT_REF_TAGS "tags"
55 #define GOT_REF_REMOTES "remotes"
58 * We do not resolve tags yet, and don't yet care about sorting refs either,
59 * so packed-refs files we write contain a minimal header which disables all
60 * packed-refs "traits" supported by Git.
62 #define GOT_PACKED_REFS_HEADER "# pack-refs with:"
64 /* A symbolic reference. */
70 #define GOT_REF_RECURSE_MAX 20
72 /* A non-symbolic reference (there is no better designation). */
75 u_int8_t sha1[SHA1_DIGEST_LENGTH];
78 /* A reference which points to an arbitrary object. */
79 struct got_reference {
81 #define GOT_REF_IS_SYMBOLIC 0x01
82 #define GOT_REF_IS_PACKED 0x02
86 struct got_symref symref;
89 struct got_lockfile *lf;
92 /* Cached timestamp for got_ref_cmp_by_commit_timestamp_descending() */
93 time_t committer_time;
96 static const struct got_error *
97 alloc_ref(struct got_reference **ref, const char *name,
98 struct got_object_id *id, int flags, time_t mtime)
100 const struct got_error *err = NULL;
102 *ref = calloc(1, sizeof(**ref));
104 return got_error_from_errno("calloc");
106 memcpy((*ref)->ref.ref.sha1, id->sha1, sizeof((*ref)->ref.ref.sha1));
107 (*ref)->flags = flags;
108 (*ref)->ref.ref.name = strdup(name);
109 (*ref)->mtime = mtime;
110 if ((*ref)->ref.ref.name == NULL) {
111 err = got_error_from_errno("strdup");
118 static const struct got_error *
119 alloc_symref(struct got_reference **ref, const char *name,
120 const char *target_ref, int flags)
122 const struct got_error *err = NULL;
124 *ref = calloc(1, sizeof(**ref));
126 return got_error_from_errno("calloc");
128 (*ref)->flags = GOT_REF_IS_SYMBOLIC | flags;
129 (*ref)->ref.symref.name = strdup(name);
130 if ((*ref)->ref.symref.name == NULL) {
131 err = got_error_from_errno("strdup");
136 (*ref)->ref.symref.ref = strdup(target_ref);
137 if ((*ref)->ref.symref.ref == NULL) {
138 err = got_error_from_errno("strdup");
145 static const struct got_error *
146 parse_symref(struct got_reference **ref, const char *name, const char *line)
149 return got_error(GOT_ERR_BAD_REF_DATA);
151 return alloc_symref(ref, name, line, 0);
154 static const struct got_error *
155 parse_ref_line(struct got_reference **ref, const char *name, const char *line,
158 struct got_object_id id;
160 if (strncmp(line, "ref: ", 5) == 0) {
162 return parse_symref(ref, name, line);
165 if (!got_parse_sha1_digest(id.sha1, line))
166 return got_error(GOT_ERR_BAD_REF_DATA);
168 return alloc_ref(ref, name, &id, 0, mtime);
171 static const struct got_error *
172 parse_ref_file(struct got_reference **ref, const char *name,
173 const char *absname, const char *abspath, int lock)
175 const struct got_error *err = NULL;
180 struct got_lockfile *lf = NULL;
184 err = got_lockfile_lock(&lf, abspath, -1);
186 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
187 err = got_error_not_ref(name);
192 f = fopen(abspath, "rb");
194 if (errno != ENOTDIR && errno != ENOENT)
195 err = got_error_from_errno2("fopen", abspath);
197 err = got_error_not_ref(name);
199 got_lockfile_unlock(lf, -1);
202 if (fstat(fileno(f), &sb) == -1) {
203 err = got_error_from_errno2("fstat", abspath);
207 linelen = getline(&line, &linesize, f);
210 err = NULL; /* ignore empty files (could be locks) */
213 err = got_error(GOT_ERR_NOT_REF);
215 err = got_ferror(f, GOT_ERR_IO);
217 err = got_error_from_errno2("getline", abspath);
220 got_lockfile_unlock(lf, -1);
223 while (linelen > 0 && line[linelen - 1] == '\n') {
224 line[linelen - 1] = '\0';
228 err = parse_ref_line(ref, absname, line, sb.st_mtime);
231 got_lockfile_unlock(lf, -1);
236 got_lockfile_unlock(lf, -1);
241 if (fclose(f) == EOF && err == NULL) {
242 err = got_error_from_errno("fclose");
245 got_ref_unlock(*ref);
254 is_well_known_ref(const char *refname)
256 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
257 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
258 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
259 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
263 get_refs_dir_path(struct got_repository *repo, const char *refname)
265 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
266 return strdup(got_repo_get_path_git_dir(repo));
268 return got_repo_get_path_refs(repo);
272 got_ref_name_is_valid(const char *name)
275 const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
276 const char *forbidden_seq[] = { "//", "..", "@{" };
277 const char *lfs = GOT_LOCKFILE_SUFFIX;
278 const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
281 if (name[0] == '@' && name[1] == '\0')
286 if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
289 for (i = 0; i < nitems(forbidden); i++) {
290 if (*s == forbidden[i])
293 for (i = 0; i < nitems(forbidden_seq); i++) {
294 if (s[0] == forbidden_seq[i][0] &&
295 s[1] == forbidden_seq[i][1])
298 if (iscntrl((unsigned char)s[0]))
300 if (s[0] == '.' && s[1] == '\0')
303 const char *nextseg = s + 1;
304 if (nextseg[0] == '\0' || nextseg[0] == '.' ||
307 if (seg <= s - lfs_len &&
308 strncmp(s - lfs_len, lfs, lfs_len) == 0)
315 if (seg <= s - lfs_len &&
316 strncmp(s - lfs_len, lfs, lfs_len) == 0)
322 const struct got_error *
323 got_ref_alloc(struct got_reference **ref, const char *name,
324 struct got_object_id *id)
326 if (!got_ref_name_is_valid(name))
327 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
329 return alloc_ref(ref, name, id, 0, 0);
332 const struct got_error *
333 got_ref_alloc_symref(struct got_reference **ref, const char *name,
334 struct got_reference *target_ref)
336 if (!got_ref_name_is_valid(name))
337 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
339 return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
342 static const struct got_error *
343 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
344 const char *line, time_t mtime)
346 struct got_object_id id;
351 if (line[0] == '#' || line[0] == '^')
354 if (!got_parse_sha1_digest(id.sha1, line))
355 return got_error(GOT_ERR_BAD_REF_DATA);
358 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
362 name = line + SHA1_DIGEST_STRING_LENGTH;
364 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED, mtime);
367 static const struct got_error *
368 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
369 int nsubdirs, const char *refname, time_t mtime)
371 const struct got_error *err = NULL;
376 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
381 abs_refname = (char *)refname;
383 linelen = getline(&line, &linesize, f);
387 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
390 if (linelen > 0 && line[linelen - 1] == '\n')
391 line[linelen - 1] = '\0';
392 for (i = 0; i < nsubdirs; i++) {
393 if (!ref_is_absolute &&
394 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
396 return got_error_from_errno("asprintf");
397 err = parse_packed_ref_line(ref, abs_refname, line,
399 if (!ref_is_absolute)
401 if (err || *ref != NULL)
406 } while (*ref == NULL);
412 static const struct got_error *
413 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
414 const char *name, int lock)
416 const struct got_error *err = NULL;
418 char *absname = NULL;
419 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
420 int ref_is_well_known = (subdir[0] == '\0' && is_well_known_ref(name));
424 if (!got_ref_name_is_valid(name))
425 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
427 if (ref_is_absolute || ref_is_well_known) {
428 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
429 return got_error_from_errno("asprintf");
430 absname = (char *)name;
432 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
433 subdir[0] ? "/" : "", name) == -1)
434 return got_error_from_errno("asprintf");
436 if (asprintf(&absname, "refs/%s%s%s",
437 subdir, subdir[0] ? "/" : "", name) == -1) {
438 err = got_error_from_errno("asprintf");
443 err = parse_ref_file(ref, name, absname, path, lock);
445 if (!ref_is_absolute && !ref_is_well_known)
451 const struct got_error *
452 got_ref_open(struct got_reference **ref, struct got_repository *repo,
453 const char *refname, int lock)
455 const struct got_error *err = NULL;
456 char *path_refs = NULL;
457 const char *subdirs[] = {
458 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
461 int well_known = is_well_known_ref(refname);
462 struct got_lockfile *lf = NULL;
466 path_refs = get_refs_dir_path(repo, refname);
467 if (path_refs == NULL) {
468 err = got_error_from_errno2("get_refs_dir_path", refname);
473 err = open_ref(ref, path_refs, "", refname, lock);
475 char *packed_refs_path;
478 /* Search on-disk refs before packed refs! */
479 for (i = 0; i < nitems(subdirs); i++) {
480 err = open_ref(ref, path_refs, subdirs[i], refname,
482 if ((err && err->code != GOT_ERR_NOT_REF) || *ref)
486 packed_refs_path = got_repo_get_path_packed_refs(repo);
487 if (packed_refs_path == NULL) {
488 err = got_error_from_errno(
489 "got_repo_get_path_packed_refs");
494 err = got_lockfile_lock(&lf, packed_refs_path, -1);
498 f = fopen(packed_refs_path, "rb");
499 free(packed_refs_path);
502 if (fstat(fileno(f), &sb) == -1) {
503 err = got_error_from_errno2("fstat",
507 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
508 refname, sb.st_mtime);
510 if (fclose(f) == EOF) {
511 err = got_error_from_errno("fclose");
520 if (!err && *ref == NULL)
521 err = got_error_not_ref(refname);
523 got_lockfile_unlock(lf, -1);
529 got_ref_close(struct got_reference *ref)
531 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
532 free(ref->ref.symref.name);
533 free(ref->ref.symref.ref);
535 free(ref->ref.ref.name);
539 struct got_reference *
540 got_ref_dup(struct got_reference *ref)
542 struct got_reference *ret;
544 ret = calloc(1, sizeof(*ret));
548 ret->flags = ref->flags;
549 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
550 ret->ref.symref.name = strdup(ref->ref.symref.name);
551 if (ret->ref.symref.name == NULL) {
555 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
556 if (ret->ref.symref.ref == NULL) {
557 free(ret->ref.symref.name);
562 ret->ref.ref.name = strdup(ref->ref.ref.name);
563 if (ret->ref.ref.name == NULL) {
567 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
568 sizeof(ret->ref.ref.sha1));
574 const struct got_error *
575 got_reflist_entry_dup(struct got_reflist_entry **newp,
576 struct got_reflist_entry *re)
578 const struct got_error *err = NULL;
579 struct got_reflist_entry *new;
583 new = malloc(sizeof(*new));
585 return got_error_from_errno("malloc");
587 new->ref = got_ref_dup(re->ref);
588 if (new->ref == NULL) {
589 err = got_error_from_errno("got_ref_dup");
598 const struct got_error *
599 got_ref_resolve_symbolic(struct got_reference **resolved,
600 struct got_repository *repo, struct got_reference *ref)
602 struct got_reference *nextref;
603 const struct got_error *err;
605 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
609 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
610 err = got_ref_resolve_symbolic(resolved, repo, nextref);
612 *resolved = got_ref_dup(nextref);
614 got_ref_close(nextref);
618 static const struct got_error *
619 ref_resolve(struct got_object_id **id, struct got_repository *repo,
620 struct got_reference *ref, int recursion)
622 const struct got_error *err;
625 return got_error_msg(GOT_ERR_RECURSION,
626 "reference recursion limit reached");
628 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
629 struct got_reference *resolved = NULL;
630 err = got_ref_resolve_symbolic(&resolved, repo, ref);
632 err = ref_resolve(id, repo, resolved, --recursion);
634 got_ref_close(resolved);
638 *id = calloc(1, sizeof(**id));
640 return got_error_from_errno("calloc");
641 memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
645 const struct got_error *
646 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
647 struct got_reference *ref)
649 return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
653 got_ref_to_str(struct got_reference *ref)
657 if (ref->flags & GOT_REF_IS_SYMBOLIC)
658 return strdup(ref->ref.symref.ref);
660 str = malloc(SHA1_DIGEST_STRING_LENGTH);
664 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
665 SHA1_DIGEST_STRING_LENGTH) == NULL) {
674 got_ref_get_name(struct got_reference *ref)
676 if (ref->flags & GOT_REF_IS_SYMBOLIC)
677 return ref->ref.symref.name;
679 return ref->ref.ref.name;
683 got_ref_get_symref_target(struct got_reference *ref)
685 if (ref->flags & GOT_REF_IS_SYMBOLIC)
686 return ref->ref.symref.ref;
692 got_ref_get_mtime(struct got_reference *ref)
697 const struct got_error *
698 got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
699 struct got_reference* re2)
701 const char *name1 = got_ref_get_name(re1);
702 const char *name2 = got_ref_get_name(re2);
704 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
708 const struct got_error *
709 got_ref_cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
710 struct got_reference *ref2)
712 const struct got_error *err = NULL;
713 struct got_repository *repo = arg;
714 struct got_object_id *id1, *id2 = NULL;
715 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
716 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
721 err = got_ref_resolve(&id1, repo, ref1);
724 err = got_object_open_as_tag(&tag1, repo, id1);
726 if (err->code != GOT_ERR_OBJ_TYPE)
728 /* "lightweight" tag */
729 err = got_object_open_as_commit(&commit1, repo, id1);
732 time1 = got_object_commit_get_committer_time(commit1);
734 time1 = got_object_tag_get_tagger_time(tag1);
736 err = got_ref_resolve(&id2, repo, ref2);
739 err = got_object_open_as_tag(&tag2, repo, id2);
741 if (err->code != GOT_ERR_OBJ_TYPE)
743 /* "lightweight" tag */
744 err = got_object_open_as_commit(&commit2, repo, id2);
747 time2 = got_object_commit_get_committer_time(commit2);
749 time2 = got_object_tag_get_tagger_time(tag2);
751 /* Put latest tags first. */
754 else if (time1 > time2)
757 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
762 got_object_tag_close(tag1);
764 got_object_tag_close(tag2);
766 got_object_commit_close(commit1);
768 got_object_commit_close(commit2);
772 static const struct got_error *
773 get_committer_time(struct got_reference *ref, struct got_repository *repo)
775 const struct got_error *err = NULL;
777 struct got_commit_object *commit = NULL;
778 struct got_tag_object *tag = NULL;
779 struct got_object_id *id = NULL;
781 err = got_ref_resolve(&id, repo, ref);
785 err = got_object_get_type(&obj_type, repo, id);
790 case GOT_OBJ_TYPE_COMMIT:
791 err = got_object_open_as_commit(&commit, repo, id);
794 ref->committer_time =
795 got_object_commit_get_committer_time(commit);
797 case GOT_OBJ_TYPE_TAG:
798 err = got_object_open_as_tag(&tag, repo, id);
801 ref->committer_time = got_object_tag_get_tagger_time(tag);
804 /* best effort for other object types */
805 ref->committer_time = got_ref_get_mtime(ref);
811 got_object_commit_close(commit);
813 got_object_tag_close(tag);
817 const struct got_error *
818 got_ref_cmp_by_commit_timestamp_descending(void *arg, int *cmp,
819 struct got_reference *ref1, struct got_reference *ref2)
821 const struct got_error *err = NULL;
822 struct got_repository *repo = arg;
826 if (ref1->committer_time == 0) {
827 err = get_committer_time(ref1, repo);
831 if (ref2->committer_time == 0) {
832 err = get_committer_time(ref2, repo);
837 if (ref1->committer_time < ref2->committer_time)
839 else if (ref2->committer_time < ref1->committer_time)
845 const struct got_error *
846 got_reflist_insert(struct got_reflist_entry **newp, struct got_reflist_head *refs,
847 struct got_reference *ref, got_ref_cmp_cb cmp_cb, void *cmp_arg)
849 const struct got_error *err;
850 struct got_reflist_entry *new, *re;
855 new = malloc(sizeof(*new));
857 return got_error_from_errno("malloc");
862 * We must de-duplicate entries on insert because packed-refs may
863 * contain redundant entries. On-disk refs take precedence.
864 * This code assumes that on-disk revs are read before packed-refs.
865 * We're iterating the list anyway, so insert elements sorted by name.
867 * Many callers will provide paths in a somewhat sorted order.
868 * Iterating backwards from the tail of the list should be more
869 * efficient than traversing through the entire list each time
870 * an element is inserted.
872 re = TAILQ_LAST(refs, got_reflist_head);
874 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
882 } else if (cmp < 0) {
883 TAILQ_INSERT_AFTER(refs, re, new, entry);
886 re = TAILQ_PREV(re, got_reflist_head, entry);
889 TAILQ_INSERT_HEAD(refs, new, entry);
893 const struct got_error *
894 got_reflist_sort(struct got_reflist_head *refs,
895 got_ref_cmp_cb cmp_cb, void *cmp_arg)
897 const struct got_error *err = NULL;
898 struct got_reflist_entry *re, *tmp, *new;
899 struct got_reflist_head sorted;
903 TAILQ_FOREACH_SAFE(re, refs, entry, tmp) {
904 struct got_reference *ref = re->ref;
905 TAILQ_REMOVE(refs, re, entry);
907 err = got_reflist_insert(&new, &sorted, ref, cmp_cb, cmp_arg);
908 if (err || new == NULL /* duplicate */)
914 TAILQ_CONCAT(refs, &sorted, entry);
918 static const struct got_error *
919 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
920 const char *subdir, struct got_repository *repo,
921 got_ref_cmp_cb cmp_cb, void *cmp_arg)
923 const struct got_error *err = NULL;
927 while (subdir[0] == '/')
930 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
931 return got_error_from_errno("asprintf");
933 d = opendir(path_subdir);
939 struct got_reference *ref;
947 if (strcmp(dent->d_name, ".") == 0 ||
948 strcmp(dent->d_name, "..") == 0)
951 err = got_path_dirent_type(&type, path_subdir, dent);
957 err = open_ref(&ref, path_refs, subdir, dent->d_name,
962 struct got_reflist_entry *new;
963 err = got_reflist_insert(&new, refs, ref,
965 if (err || new == NULL /* duplicate */)
972 if (asprintf(&child, "%s%s%s", subdir,
973 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
974 err = got_error_from_errno("asprintf");
977 err = gather_on_disk_refs(refs, path_refs, child, repo,
992 const struct got_error *
993 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
994 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
996 const struct got_error *err;
997 char *packed_refs_path, *path_refs = NULL;
998 char *abs_namespace = NULL;
999 char *buf = NULL, *ondisk_ref_namespace = NULL;
1002 struct got_reference *ref;
1003 struct got_reflist_entry *new;
1005 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
1006 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
1007 if (path_refs == NULL) {
1008 err = got_error_from_errno("get_refs_dir_path");
1011 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
1014 err = got_reflist_insert(&new, refs, ref, cmp_cb, cmp_arg);
1015 if (err || new == NULL /* duplicate */)
1017 if (err && err->code != GOT_ERR_NOT_REF)
1020 /* Try listing a single reference. */
1021 const char *refname = ref_namespace;
1022 path_refs = get_refs_dir_path(repo, refname);
1023 if (path_refs == NULL) {
1024 err = got_error_from_errno("get_refs_dir_path");
1027 err = open_ref(&ref, path_refs, "", refname, 0);
1029 if (err->code != GOT_ERR_NOT_REF)
1031 /* Try to look up references in a given namespace. */
1033 err = got_reflist_insert(&new, refs, ref,
1035 if (err || new == NULL /* duplicate */)
1041 if (ref_namespace) {
1043 /* Canonicalize the path to eliminate double-slashes if any. */
1044 if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
1045 err = got_error_from_errno("asprintf");
1048 len = strlen(abs_namespace) + 1;
1051 err = got_error_from_errno("malloc");
1054 err = got_canonpath(abs_namespace, buf, len);
1057 ondisk_ref_namespace = buf;
1058 while (ondisk_ref_namespace[0] == '/')
1059 ondisk_ref_namespace++;
1060 if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
1061 ondisk_ref_namespace += 5;
1062 else if (strcmp(ondisk_ref_namespace, "refs") == 0)
1063 ondisk_ref_namespace = "";
1066 /* Gather on-disk refs before parsing packed-refs. */
1068 path_refs = get_refs_dir_path(repo, "");
1069 if (path_refs == NULL) {
1070 err = got_error_from_errno("get_refs_dir_path");
1073 err = gather_on_disk_refs(refs, path_refs,
1074 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
1080 * The packed-refs file may contain redundant entries, in which
1081 * case on-disk refs take precedence.
1083 packed_refs_path = got_repo_get_path_packed_refs(repo);
1084 if (packed_refs_path == NULL) {
1085 err = got_error_from_errno("got_repo_get_path_packed_refs");
1089 f = fopen(packed_refs_path, "r");
1090 free(packed_refs_path);
1092 size_t linesize = 0;
1096 if (fstat(fileno(f), &sb) == -1) {
1097 err = got_error_from_errno2("fstat", packed_refs_path);
1101 linelen = getline(&line, &linesize, f);
1102 if (linelen == -1) {
1105 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1108 if (linelen > 0 && line[linelen - 1] == '\n')
1109 line[linelen - 1] = '\0';
1110 err = parse_packed_ref_line(&ref, NULL, line,
1115 if (ref_namespace) {
1117 name = got_ref_get_name(ref);
1118 if (!got_path_is_child(name,
1120 strlen(ref_namespace))) {
1125 err = got_reflist_insert(&new, refs, ref,
1127 if (err || new == NULL /* duplicate */)
1135 free(abs_namespace);
1139 if (f && fclose(f) == EOF && err == NULL)
1140 err = got_error_from_errno("fclose");
1145 got_ref_list_free(struct got_reflist_head *refs)
1147 struct got_reflist_entry *re;
1149 while ((re = TAILQ_FIRST(refs))) {
1150 TAILQ_REMOVE(refs, re, entry);
1151 got_ref_close(re->ref);
1158 got_ref_is_symbolic(struct got_reference *ref)
1160 return (ref->flags & GOT_REF_IS_SYMBOLIC);
1163 const struct got_error *
1164 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1166 if (ref->flags & GOT_REF_IS_SYMBOLIC)
1167 return got_error(GOT_ERR_BAD_REF_TYPE);
1169 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
1173 const struct got_error *
1174 got_ref_change_symref(struct got_reference *ref, const char *refname)
1178 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1179 return got_error(GOT_ERR_BAD_REF_TYPE);
1181 new_name = strdup(refname);
1182 if (new_name == NULL)
1183 return got_error_from_errno("strdup");
1185 free(ref->ref.symref.ref);
1186 ref->ref.symref.ref = new_name;
1190 const struct got_error *
1191 got_ref_change_symref_to_ref(struct got_reference *symref,
1192 struct got_object_id *id)
1194 if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1195 return got_error(GOT_ERR_BAD_REF_TYPE);
1197 symref->ref.ref.name = symref->ref.symref.name;
1198 memcpy(symref->ref.ref.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1199 symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1203 const struct got_error *
1204 got_ref_write(struct got_reference *ref, struct got_repository *repo)
1206 const struct got_error *err = NULL, *unlock_err = NULL;
1207 const char *name = got_ref_get_name(ref);
1208 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1209 struct got_lockfile *lf = NULL;
1214 path_refs = get_refs_dir_path(repo, name);
1215 if (path_refs == NULL) {
1216 err = got_error_from_errno2("get_refs_dir_path", name);
1220 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1221 err = got_error_from_errno("asprintf");
1225 err = got_opentemp_named(&tmppath, &f, path);
1228 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1230 err = got_path_dirname(&parent, path);
1233 err = got_path_mkdir(parent);
1237 err = got_opentemp_named(&tmppath, &f, path);
1242 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1243 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1244 if (n != strlen(ref->ref.symref.ref) + 6) {
1245 err = got_ferror(f, GOT_ERR_IO);
1249 char hex[SHA1_DIGEST_STRING_LENGTH];
1250 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
1251 sizeof(hex)) == NULL) {
1252 err = got_error(GOT_ERR_BAD_REF_DATA);
1255 n = fprintf(f, "%s\n", hex);
1256 if (n != sizeof(hex)) {
1257 err = got_ferror(f, GOT_ERR_IO);
1262 if (ref->lf == NULL) {
1263 err = got_lockfile_lock(&lf, path, -1);
1268 /* XXX: check if old content matches our expectations? */
1270 if (stat(path, &sb) != 0) {
1271 if (errno != ENOENT) {
1272 err = got_error_from_errno2("stat", path);
1275 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1278 if (fchmod(fileno(f), sb.st_mode) != 0) {
1279 err = got_error_from_errno2("fchmod", tmppath);
1283 if (rename(tmppath, path) != 0) {
1284 err = got_error_from_errno3("rename", tmppath, path);
1290 if (stat(path, &sb) == -1) {
1291 err = got_error_from_errno2("stat", path);
1294 ref->mtime = sb.st_mtime;
1296 if (ref->lf == NULL && lf)
1297 unlock_err = got_lockfile_unlock(lf, -1);
1299 if (fclose(f) == EOF && err == NULL)
1300 err = got_error_from_errno("fclose");
1305 if (unlink(tmppath) != 0 && err == NULL)
1306 err = got_error_from_errno2("unlink", tmppath);
1309 return err ? err : unlock_err;
1312 static const struct got_error *
1313 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1315 const struct got_error *err = NULL, *unlock_err = NULL;
1316 struct got_lockfile *lf = NULL;
1317 FILE *f = NULL, *tmpf = NULL;
1318 char *line = NULL, *packed_refs_path, *tmppath = NULL;
1319 size_t linesize = 0;
1320 struct got_reflist_head refs;
1321 int found_delref = 0;
1323 /* The packed-refs file does not cotain symbolic references. */
1324 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1325 return got_error(GOT_ERR_BAD_REF_DATA);
1329 packed_refs_path = got_repo_get_path_packed_refs(repo);
1330 if (packed_refs_path == NULL)
1331 return got_error_from_errno("got_repo_get_path_packed_refs");
1333 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
1337 if (delref->lf == NULL) {
1338 err = got_lockfile_lock(&lf, packed_refs_path, -1);
1343 f = fopen(packed_refs_path, "r");
1345 err = got_error_from_errno2("fopen", packed_refs_path);
1350 struct got_reference *ref;
1351 struct got_reflist_entry *new;
1353 linelen = getline(&line, &linesize, f);
1354 if (linelen == -1) {
1357 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1360 if (linelen > 0 && line[linelen - 1] == '\n')
1361 line[linelen - 1] = '\0';
1362 err = parse_packed_ref_line(&ref, NULL, line, 0);
1368 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1369 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1370 sizeof(delref->ref.ref.sha1)) == 0) {
1376 err = got_reflist_insert(&new, &refs, ref,
1377 got_ref_cmp_by_name, NULL);
1378 if (err || new == NULL /* duplicate */)
1385 struct got_reflist_entry *re;
1389 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1390 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1391 err = got_ferror(f, GOT_ERR_IO);
1395 TAILQ_FOREACH(re, &refs, entry) {
1396 char hex[SHA1_DIGEST_STRING_LENGTH];
1398 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1399 sizeof(hex)) == NULL) {
1400 err = got_error(GOT_ERR_BAD_REF_DATA);
1403 n = fprintf(tmpf, "%s ", hex);
1404 if (n != sizeof(hex)) {
1405 err = got_ferror(f, GOT_ERR_IO);
1408 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1409 if (n != strlen(re->ref->ref.ref.name) + 1) {
1410 err = got_ferror(f, GOT_ERR_IO);
1415 if (fflush(tmpf) != 0) {
1416 err = got_error_from_errno("fflush");
1420 if (fstat(fileno(f), &sb) != 0) {
1421 if (errno != ENOENT) {
1422 err = got_error_from_errno2("fstat",
1426 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1429 if (fchmod(fileno(tmpf), sb.st_mode) != 0) {
1430 err = got_error_from_errno2("fchmod", tmppath);
1434 if (rename(tmppath, packed_refs_path) != 0) {
1435 err = got_error_from_errno3("rename", tmppath,
1441 if (delref->lf == NULL && lf)
1442 unlock_err = got_lockfile_unlock(lf, -1);
1444 if (fclose(f) == EOF && err == NULL)
1445 err = got_error_from_errno("fclose");
1449 if (fclose(tmpf) == EOF && err == NULL)
1450 err = got_error_from_errno("fclose");
1453 free(packed_refs_path);
1455 got_ref_list_free(&refs);
1456 return err ? err : unlock_err;
1459 static const struct got_error *
1460 delete_loose_ref(struct got_reference *ref, struct got_repository *repo)
1462 const struct got_error *err = NULL, *unlock_err = NULL;
1463 const char *name = got_ref_get_name(ref);
1464 char *path_refs = NULL, *path = NULL;
1465 struct got_lockfile *lf = NULL;
1467 path_refs = get_refs_dir_path(repo, name);
1468 if (path_refs == NULL) {
1469 err = got_error_from_errno2("get_refs_dir_path", name);
1473 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1474 err = got_error_from_errno("asprintf");
1478 if (ref->lf == NULL) {
1479 err = got_lockfile_lock(&lf, path, -1);
1484 /* XXX: check if old content matches our expectations? */
1486 if (unlink(path) != 0)
1487 err = got_error_from_errno2("unlink", path);
1489 if (ref->lf == NULL && lf)
1490 unlock_err = got_lockfile_unlock(lf, -1);
1494 return err ? err : unlock_err;
1497 const struct got_error *
1498 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1500 const struct got_error *err = NULL;
1501 struct got_reference *ref2;
1503 if (ref->flags & GOT_REF_IS_PACKED) {
1504 err = delete_packed_ref(ref, repo);
1508 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1510 if (err->code == GOT_ERR_NOT_REF)
1515 err = delete_loose_ref(ref2, repo);
1516 got_ref_close(ref2);
1519 err = delete_loose_ref(ref, repo);
1523 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1525 if (err->code == GOT_ERR_NOT_REF)
1530 err = delete_packed_ref(ref2, repo);
1531 got_ref_close(ref2);
1536 const struct got_error *
1537 got_ref_unlock(struct got_reference *ref)
1539 const struct got_error *err;
1540 err = got_lockfile_unlock(ref->lf, -1);
1545 struct got_reflist_object_id_map {
1546 struct got_object_idset *idset;
1549 struct got_reflist_object_id_map_entry {
1550 struct got_reflist_head refs;
1553 static const struct got_error *
1554 add_object_id_map_entry(struct got_object_idset *idset,
1555 struct got_object_id *id, struct got_reflist_entry *re)
1557 const struct got_error *err = NULL;
1558 struct got_reflist_object_id_map_entry *ent;
1559 struct got_reflist_entry *new;
1561 ent = got_object_idset_get(idset, id);
1563 ent = malloc(sizeof(*ent));
1565 return got_error_from_errno("malloc");
1567 TAILQ_INIT(&ent->refs);
1568 err = got_object_idset_add(idset, id, ent);
1573 err = got_reflist_entry_dup(&new, re);
1577 TAILQ_INSERT_TAIL(&ent->refs, new, entry);
1581 const struct got_error *
1582 got_reflist_object_id_map_create(struct got_reflist_object_id_map **map,
1583 struct got_reflist_head *refs, struct got_repository *repo)
1585 const struct got_error *err = NULL;
1586 struct got_object_idset *idset;
1587 struct got_object_id *id = NULL;
1588 struct got_reflist_entry *re;
1590 idset = got_object_idset_alloc();
1592 return got_error_from_errno("got_object_idset_alloc");
1594 *map = malloc(sizeof(**map));
1596 got_object_idset_free(idset);
1597 return got_error_from_errno("malloc");
1599 (*map)->idset = idset;
1601 TAILQ_FOREACH(re, refs, entry) {
1602 struct got_tag_object *tag = NULL;
1604 err = got_ref_resolve(&id, repo, re->ref);
1608 err = add_object_id_map_entry(idset, id, re);
1612 if (strstr(got_ref_get_name(re->ref), "/tags/") == NULL) {
1618 err = got_object_open_as_tag(&tag, repo, id);
1620 if (err->code != GOT_ERR_OBJ_TYPE)
1622 /* Ref points at something other than a tag. */
1630 err = add_object_id_map_entry(idset,
1631 got_object_tag_get_object_id(tag), re);
1632 got_object_tag_close(tag);
1642 got_reflist_object_id_map_free(*map);
1648 struct got_reflist_head *
1649 got_reflist_object_id_map_lookup(struct got_reflist_object_id_map *map,
1650 struct got_object_id *id)
1652 struct got_reflist_object_id_map_entry *ent;
1653 ent = got_object_idset_get(map->idset, id);
1659 static const struct got_error *
1660 free_id_map_entry(struct got_object_id *id, void *data, void *arg)
1662 struct got_reflist_object_id_map_entry *ent = data;
1664 got_ref_list_free(&ent->refs);
1670 got_reflist_object_id_map_free(struct got_reflist_object_id_map *map)
1672 got_object_idset_for_each(map->idset, free_id_map_entry, NULL);
1673 got_object_idset_free(map->idset);