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, "rb");
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, "rb");
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 const struct got_error *
772 got_ref_cmp_by_commit_timestamp_descending(void *arg, int *cmp,
773 struct got_reference *ref1, struct got_reference *ref2)
775 const struct got_error *err;
776 struct got_repository *repo = arg;
777 struct got_object_id *id1 = NULL, *id2 = NULL;
778 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
782 if (ref1->committer_time == 0) {
783 err = got_ref_resolve(&id1, repo, ref1);
786 err = got_object_open_as_commit(&commit1, repo, id1);
789 ref1->committer_time =
790 got_object_commit_get_committer_time(commit1);
793 if (ref2->committer_time == 0) {
794 err = got_ref_resolve(&id2, repo, ref2);
797 err = got_object_open_as_commit(&commit2, repo, id2);
800 ref2->committer_time =
801 got_object_commit_get_committer_time(commit2);
804 if (ref1->committer_time < ref2->committer_time)
806 else if (ref2->committer_time < ref1->committer_time)
812 got_object_commit_close(commit1);
814 got_object_commit_close(commit2);
818 const struct got_error *
819 got_reflist_insert(struct got_reflist_entry **newp, struct got_reflist_head *refs,
820 struct got_reference *ref, got_ref_cmp_cb cmp_cb, void *cmp_arg)
822 const struct got_error *err;
823 struct got_reflist_entry *new, *re;
828 new = malloc(sizeof(*new));
830 return got_error_from_errno("malloc");
835 * We must de-duplicate entries on insert because packed-refs may
836 * contain redundant entries. On-disk refs take precedence.
837 * This code assumes that on-disk revs are read before packed-refs.
838 * We're iterating the list anyway, so insert elements sorted by name.
840 * Many callers will provide paths in a somewhat sorted order.
841 * Iterating backwards from the tail of the list should be more
842 * efficient than traversing through the entire list each time
843 * an element is inserted.
845 re = TAILQ_LAST(refs, got_reflist_head);
847 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
855 } else if (cmp < 0) {
856 TAILQ_INSERT_AFTER(refs, re, new, entry);
859 re = TAILQ_PREV(re, got_reflist_head, entry);
862 TAILQ_INSERT_HEAD(refs, new, entry);
866 static const struct got_error *
867 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
868 const char *subdir, struct got_repository *repo,
869 got_ref_cmp_cb cmp_cb, void *cmp_arg)
871 const struct got_error *err = NULL;
875 while (subdir[0] == '/')
878 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
879 return got_error_from_errno("asprintf");
881 d = opendir(path_subdir);
887 struct got_reference *ref;
895 if (strcmp(dent->d_name, ".") == 0 ||
896 strcmp(dent->d_name, "..") == 0)
899 err = got_path_dirent_type(&type, path_subdir, dent);
905 err = open_ref(&ref, path_refs, subdir, dent->d_name,
910 struct got_reflist_entry *new;
911 err = got_reflist_insert(&new, refs, ref,
913 if (err || new == NULL /* duplicate */)
920 if (asprintf(&child, "%s%s%s", subdir,
921 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
922 err = got_error_from_errno("asprintf");
925 err = gather_on_disk_refs(refs, path_refs, child, repo,
940 const struct got_error *
941 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
942 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
944 const struct got_error *err;
945 char *packed_refs_path, *path_refs = NULL;
946 char *abs_namespace = NULL;
947 char *buf = NULL, *ondisk_ref_namespace = NULL;
950 struct got_reference *ref;
951 struct got_reflist_entry *new;
953 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
954 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
955 if (path_refs == NULL) {
956 err = got_error_from_errno("get_refs_dir_path");
959 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
962 err = got_reflist_insert(&new, refs, ref, cmp_cb, cmp_arg);
963 if (err || new == NULL /* duplicate */)
965 if (err && err->code != GOT_ERR_NOT_REF)
968 /* Try listing a single reference. */
969 const char *refname = ref_namespace;
970 path_refs = get_refs_dir_path(repo, refname);
971 if (path_refs == NULL) {
972 err = got_error_from_errno("get_refs_dir_path");
975 err = open_ref(&ref, path_refs, "", refname, 0);
977 if (err->code != GOT_ERR_NOT_REF)
979 /* Try to look up references in a given namespace. */
981 err = got_reflist_insert(&new, refs, ref,
983 if (err || new == NULL /* duplicate */)
991 /* Canonicalize the path to eliminate double-slashes if any. */
992 if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
993 err = got_error_from_errno("asprintf");
996 len = strlen(abs_namespace) + 1;
999 err = got_error_from_errno("malloc");
1002 err = got_canonpath(abs_namespace, buf, len);
1005 ondisk_ref_namespace = buf;
1006 while (ondisk_ref_namespace[0] == '/')
1007 ondisk_ref_namespace++;
1008 if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
1009 ondisk_ref_namespace += 5;
1010 else if (strcmp(ondisk_ref_namespace, "refs") == 0)
1011 ondisk_ref_namespace = "";
1014 /* Gather on-disk refs before parsing packed-refs. */
1016 path_refs = get_refs_dir_path(repo, "");
1017 if (path_refs == NULL) {
1018 err = got_error_from_errno("get_refs_dir_path");
1021 err = gather_on_disk_refs(refs, path_refs,
1022 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
1028 * The packed-refs file may contain redundant entries, in which
1029 * case on-disk refs take precedence.
1031 packed_refs_path = got_repo_get_path_packed_refs(repo);
1032 if (packed_refs_path == NULL) {
1033 err = got_error_from_errno("got_repo_get_path_packed_refs");
1037 f = fopen(packed_refs_path, "r");
1038 free(packed_refs_path);
1040 size_t linesize = 0;
1044 if (fstat(fileno(f), &sb) == -1) {
1045 err = got_error_from_errno2("fstat", packed_refs_path);
1049 linelen = getline(&line, &linesize, f);
1050 if (linelen == -1) {
1053 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1056 if (linelen > 0 && line[linelen - 1] == '\n')
1057 line[linelen - 1] = '\0';
1058 err = parse_packed_ref_line(&ref, NULL, line,
1063 if (ref_namespace) {
1065 name = got_ref_get_name(ref);
1066 if (!got_path_is_child(name,
1068 strlen(ref_namespace))) {
1073 err = got_reflist_insert(&new, refs, ref,
1075 if (err || new == NULL /* duplicate */)
1083 free(abs_namespace);
1087 if (f && fclose(f) == EOF && err == NULL)
1088 err = got_error_from_errno("fclose");
1093 got_ref_list_free(struct got_reflist_head *refs)
1095 struct got_reflist_entry *re;
1097 while ((re = TAILQ_FIRST(refs))) {
1098 TAILQ_REMOVE(refs, re, entry);
1099 got_ref_close(re->ref);
1106 got_ref_is_symbolic(struct got_reference *ref)
1108 return (ref->flags & GOT_REF_IS_SYMBOLIC);
1111 const struct got_error *
1112 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1114 if (ref->flags & GOT_REF_IS_SYMBOLIC)
1115 return got_error(GOT_ERR_BAD_REF_TYPE);
1117 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
1121 const struct got_error *
1122 got_ref_change_symref(struct got_reference *ref, const char *refname)
1126 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1127 return got_error(GOT_ERR_BAD_REF_TYPE);
1129 new_name = strdup(refname);
1130 if (new_name == NULL)
1131 return got_error_from_errno("strdup");
1133 free(ref->ref.symref.ref);
1134 ref->ref.symref.ref = new_name;
1138 const struct got_error *
1139 got_ref_change_symref_to_ref(struct got_reference *symref,
1140 struct got_object_id *id)
1142 if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1143 return got_error(GOT_ERR_BAD_REF_TYPE);
1145 symref->ref.ref.name = symref->ref.symref.name;
1146 memcpy(symref->ref.ref.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1147 symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1151 const struct got_error *
1152 got_ref_write(struct got_reference *ref, struct got_repository *repo)
1154 const struct got_error *err = NULL, *unlock_err = NULL;
1155 const char *name = got_ref_get_name(ref);
1156 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1157 struct got_lockfile *lf = NULL;
1162 path_refs = get_refs_dir_path(repo, name);
1163 if (path_refs == NULL) {
1164 err = got_error_from_errno2("get_refs_dir_path", name);
1168 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1169 err = got_error_from_errno("asprintf");
1173 err = got_opentemp_named(&tmppath, &f, path);
1176 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1178 err = got_path_dirname(&parent, path);
1181 err = got_path_mkdir(parent);
1185 err = got_opentemp_named(&tmppath, &f, path);
1190 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1191 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1192 if (n != strlen(ref->ref.symref.ref) + 6) {
1193 err = got_ferror(f, GOT_ERR_IO);
1197 char hex[SHA1_DIGEST_STRING_LENGTH];
1198 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
1199 sizeof(hex)) == NULL) {
1200 err = got_error(GOT_ERR_BAD_REF_DATA);
1203 n = fprintf(f, "%s\n", hex);
1204 if (n != sizeof(hex)) {
1205 err = got_ferror(f, GOT_ERR_IO);
1210 if (ref->lf == NULL) {
1211 err = got_lockfile_lock(&lf, path, -1);
1216 /* XXX: check if old content matches our expectations? */
1218 if (stat(path, &sb) != 0) {
1219 if (errno != ENOENT) {
1220 err = got_error_from_errno2("stat", path);
1223 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1226 if (fchmod(fileno(f), sb.st_mode) != 0) {
1227 err = got_error_from_errno2("fchmod", tmppath);
1231 if (rename(tmppath, path) != 0) {
1232 err = got_error_from_errno3("rename", tmppath, path);
1238 if (stat(path, &sb) == -1) {
1239 err = got_error_from_errno2("stat", path);
1242 ref->mtime = sb.st_mtime;
1244 if (ref->lf == NULL && lf)
1245 unlock_err = got_lockfile_unlock(lf, -1);
1247 if (fclose(f) == EOF && err == NULL)
1248 err = got_error_from_errno("fclose");
1253 if (unlink(tmppath) != 0 && err == NULL)
1254 err = got_error_from_errno2("unlink", tmppath);
1257 return err ? err : unlock_err;
1260 static const struct got_error *
1261 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1263 const struct got_error *err = NULL, *unlock_err = NULL;
1264 struct got_lockfile *lf = NULL;
1265 FILE *f = NULL, *tmpf = NULL;
1266 char *line = NULL, *packed_refs_path, *tmppath = NULL;
1267 size_t linesize = 0;
1268 struct got_reflist_head refs;
1269 int found_delref = 0;
1271 /* The packed-refs file does not cotain symbolic references. */
1272 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1273 return got_error(GOT_ERR_BAD_REF_DATA);
1277 packed_refs_path = got_repo_get_path_packed_refs(repo);
1278 if (packed_refs_path == NULL)
1279 return got_error_from_errno("got_repo_get_path_packed_refs");
1281 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
1285 if (delref->lf == NULL) {
1286 err = got_lockfile_lock(&lf, packed_refs_path, -1);
1291 f = fopen(packed_refs_path, "r");
1293 err = got_error_from_errno2("fopen", packed_refs_path);
1298 struct got_reference *ref;
1299 struct got_reflist_entry *new;
1301 linelen = getline(&line, &linesize, f);
1302 if (linelen == -1) {
1305 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1308 if (linelen > 0 && line[linelen - 1] == '\n')
1309 line[linelen - 1] = '\0';
1310 err = parse_packed_ref_line(&ref, NULL, line, 0);
1316 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1317 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1318 sizeof(delref->ref.ref.sha1)) == 0) {
1324 err = got_reflist_insert(&new, &refs, ref,
1325 got_ref_cmp_by_name, NULL);
1326 if (err || new == NULL /* duplicate */)
1333 struct got_reflist_entry *re;
1337 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1338 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1339 err = got_ferror(f, GOT_ERR_IO);
1343 TAILQ_FOREACH(re, &refs, entry) {
1344 char hex[SHA1_DIGEST_STRING_LENGTH];
1346 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1347 sizeof(hex)) == NULL) {
1348 err = got_error(GOT_ERR_BAD_REF_DATA);
1351 n = fprintf(tmpf, "%s ", hex);
1352 if (n != sizeof(hex)) {
1353 err = got_ferror(f, GOT_ERR_IO);
1356 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1357 if (n != strlen(re->ref->ref.ref.name) + 1) {
1358 err = got_ferror(f, GOT_ERR_IO);
1363 if (fflush(tmpf) != 0) {
1364 err = got_error_from_errno("fflush");
1368 if (fstat(fileno(f), &sb) != 0) {
1369 if (errno != ENOENT) {
1370 err = got_error_from_errno2("fstat",
1374 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1377 if (fchmod(fileno(tmpf), sb.st_mode) != 0) {
1378 err = got_error_from_errno2("fchmod", tmppath);
1382 if (rename(tmppath, packed_refs_path) != 0) {
1383 err = got_error_from_errno3("rename", tmppath,
1389 if (delref->lf == NULL && lf)
1390 unlock_err = got_lockfile_unlock(lf, -1);
1392 if (fclose(f) == EOF && err == NULL)
1393 err = got_error_from_errno("fclose");
1397 if (fclose(tmpf) == EOF && err == NULL)
1398 err = got_error_from_errno("fclose");
1401 free(packed_refs_path);
1403 got_ref_list_free(&refs);
1404 return err ? err : unlock_err;
1407 static const struct got_error *
1408 delete_loose_ref(struct got_reference *ref, struct got_repository *repo)
1410 const struct got_error *err = NULL, *unlock_err = NULL;
1411 const char *name = got_ref_get_name(ref);
1412 char *path_refs = NULL, *path = NULL;
1413 struct got_lockfile *lf = NULL;
1415 path_refs = get_refs_dir_path(repo, name);
1416 if (path_refs == NULL) {
1417 err = got_error_from_errno2("get_refs_dir_path", name);
1421 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1422 err = got_error_from_errno("asprintf");
1426 if (ref->lf == NULL) {
1427 err = got_lockfile_lock(&lf, path, -1);
1432 /* XXX: check if old content matches our expectations? */
1434 if (unlink(path) != 0)
1435 err = got_error_from_errno2("unlink", path);
1437 if (ref->lf == NULL && lf)
1438 unlock_err = got_lockfile_unlock(lf, -1);
1442 return err ? err : unlock_err;
1445 const struct got_error *
1446 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1448 const struct got_error *err = NULL;
1449 struct got_reference *ref2;
1451 if (ref->flags & GOT_REF_IS_PACKED) {
1452 err = delete_packed_ref(ref, repo);
1456 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1458 if (err->code == GOT_ERR_NOT_REF)
1463 err = delete_loose_ref(ref2, repo);
1464 got_ref_close(ref2);
1467 err = delete_loose_ref(ref, repo);
1471 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1473 if (err->code == GOT_ERR_NOT_REF)
1478 err = delete_packed_ref(ref2, repo);
1479 got_ref_close(ref2);
1484 const struct got_error *
1485 got_ref_unlock(struct got_reference *ref)
1487 const struct got_error *err;
1488 err = got_lockfile_unlock(ref->lf, -1);
1493 struct got_reflist_object_id_map {
1494 struct got_object_idset *idset;
1497 struct got_reflist_object_id_map_entry {
1498 struct got_reflist_head refs;
1501 static const struct got_error *
1502 add_object_id_map_entry(struct got_object_idset *idset,
1503 struct got_object_id *id, struct got_reflist_entry *re)
1505 const struct got_error *err = NULL;
1506 struct got_reflist_object_id_map_entry *ent;
1507 struct got_reflist_entry *new;
1509 ent = got_object_idset_get(idset, id);
1511 ent = malloc(sizeof(*ent));
1513 return got_error_from_errno("malloc");
1515 TAILQ_INIT(&ent->refs);
1516 err = got_object_idset_add(idset, id, ent);
1521 err = got_reflist_entry_dup(&new, re);
1525 TAILQ_INSERT_TAIL(&ent->refs, new, entry);
1529 const struct got_error *
1530 got_reflist_object_id_map_create(struct got_reflist_object_id_map **map,
1531 struct got_reflist_head *refs, struct got_repository *repo)
1533 const struct got_error *err = NULL;
1534 struct got_object_idset *idset;
1535 struct got_object_id *id = NULL;
1536 struct got_reflist_entry *re;
1538 idset = got_object_idset_alloc();
1540 return got_error_from_errno("got_object_idset_alloc");
1542 *map = malloc(sizeof(**map));
1544 got_object_idset_free(idset);
1545 return got_error_from_errno("malloc");
1547 (*map)->idset = idset;
1549 TAILQ_FOREACH(re, refs, entry) {
1550 struct got_tag_object *tag = NULL;
1552 err = got_ref_resolve(&id, repo, re->ref);
1556 err = add_object_id_map_entry(idset, id, re);
1560 if (strstr(got_ref_get_name(re->ref), "/tags/") == NULL) {
1566 err = got_object_open_as_tag(&tag, repo, id);
1568 if (err->code != GOT_ERR_OBJ_TYPE)
1570 /* Ref points at something other than a tag. */
1578 err = add_object_id_map_entry(idset,
1579 got_object_tag_get_object_id(tag), re);
1580 got_object_tag_close(tag);
1590 got_reflist_object_id_map_free(*map);
1596 struct got_reflist_head *
1597 got_reflist_object_id_map_lookup(struct got_reflist_object_id_map *map,
1598 struct got_object_id *id)
1600 struct got_reflist_object_id_map_entry *ent;
1601 ent = got_object_idset_get(map->idset, id);
1607 static const struct got_error *
1608 free_id_map_entry(struct got_object_id *id, void *data, void *arg)
1610 struct got_reflist_object_id_map_entry *ent = data;
1612 got_ref_list_free(&ent->refs);
1618 got_reflist_object_id_map_free(struct got_reflist_object_id_map *map)
1620 got_object_idset_for_each(map->idset, free_id_map_entry, NULL);
1621 got_object_idset_free(map->idset);