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 "got_compat.h"
19 #include <sys/types.h>
20 #include <sys/queue.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_hash.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 struct got_object_id id;
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;
92 * Cached timestamp for got_ref_cmp_by_commit_timestamp_descending()
93 * and got_ref_cmp_tags().
95 time_t committer_time;
98 static const struct got_error *
99 alloc_ref(struct got_reference **ref, const char *name,
100 struct got_object_id *id, int flags, time_t mtime)
102 const struct got_error *err = NULL;
104 *ref = calloc(1, sizeof(**ref));
106 return got_error_from_errno("calloc");
108 memcpy(&(*ref)->ref.ref.id, id, sizeof((*ref)->ref.ref.id));
109 (*ref)->flags = flags;
110 (*ref)->ref.ref.name = strdup(name);
111 (*ref)->mtime = mtime;
112 if ((*ref)->ref.ref.name == NULL) {
113 err = got_error_from_errno("strdup");
120 static const struct got_error *
121 alloc_symref(struct got_reference **ref, const char *name,
122 const char *target_ref, int flags)
124 const struct got_error *err = NULL;
126 *ref = calloc(1, sizeof(**ref));
128 return got_error_from_errno("calloc");
130 (*ref)->flags = GOT_REF_IS_SYMBOLIC | flags;
131 (*ref)->ref.symref.name = strdup(name);
132 if ((*ref)->ref.symref.name == NULL) {
133 err = got_error_from_errno("strdup");
138 (*ref)->ref.symref.ref = strdup(target_ref);
139 if ((*ref)->ref.symref.ref == NULL) {
140 err = got_error_from_errno("strdup");
147 static const struct got_error *
148 parse_symref(struct got_reference **ref, const char *name, const char *line)
151 return got_error(GOT_ERR_BAD_REF_DATA);
153 return alloc_symref(ref, name, line, 0);
156 static const struct got_error *
157 parse_ref_line(struct got_reference **ref, const char *name, const char *line,
158 time_t mtime, enum got_hash_algorithm algo)
160 struct got_object_id id;
162 if (strncmp(line, "ref: ", 5) == 0) {
164 return parse_symref(ref, name, line);
167 if (!got_parse_object_id(&id, line, algo))
168 return got_error(GOT_ERR_BAD_REF_DATA);
170 return alloc_ref(ref, name, &id, 0, mtime);
173 static const struct got_error *
174 parse_ref_file(struct got_reference **ref, const char *name,
175 const char *absname, const char *abspath, int lock,
176 enum got_hash_algorithm algo)
178 const struct got_error *err = NULL;
183 struct got_lockfile *lf = NULL;
187 err = got_lockfile_lock(&lf, abspath, -1);
189 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
190 err = got_error_not_ref(name);
195 f = fopen(abspath, "rbe");
197 if (errno != ENOTDIR && errno != ENOENT)
198 err = got_error_from_errno2("fopen", abspath);
200 err = got_error_not_ref(name);
202 got_lockfile_unlock(lf, -1);
205 if (fstat(fileno(f), &sb) == -1) {
206 err = got_error_from_errno2("fstat", abspath);
210 linelen = getline(&line, &linesize, f);
213 err = NULL; /* ignore empty files (could be locks) */
216 err = got_error(GOT_ERR_NOT_REF);
218 err = got_ferror(f, GOT_ERR_IO);
220 err = got_error_from_errno2("getline", abspath);
223 got_lockfile_unlock(lf, -1);
226 while (linelen > 0 && line[linelen - 1] == '\n') {
227 line[linelen - 1] = '\0';
231 err = parse_ref_line(ref, absname, line, sb.st_mtime, algo);
234 got_lockfile_unlock(lf, -1);
239 got_lockfile_unlock(lf, -1);
244 if (fclose(f) == EOF && err == NULL) {
245 err = got_error_from_errno("fclose");
248 got_ref_unlock(*ref);
257 is_well_known_ref(const char *refname)
259 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
260 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
261 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
262 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
266 get_refs_dir_path(struct got_repository *repo, const char *refname)
268 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
269 return strdup(got_repo_get_path_git_dir(repo));
271 return got_repo_get_path_refs(repo);
274 const struct got_error *
275 got_ref_alloc(struct got_reference **ref, const char *name,
276 struct got_object_id *id)
278 if (!got_ref_name_is_valid(name))
279 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
281 return alloc_ref(ref, name, id, 0, 0);
284 const struct got_error *
285 got_ref_alloc_symref(struct got_reference **ref, const char *name,
286 struct got_reference *target_ref)
288 if (!got_ref_name_is_valid(name))
289 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
291 return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
294 static const struct got_error *
295 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
296 const char *line, time_t mtime, enum got_hash_algorithm algo)
298 struct got_object_id id;
303 if (line[0] == '#' || line[0] == '^')
306 if (!got_parse_object_id(&id, line, algo))
307 return got_error(GOT_ERR_BAD_REF_DATA);
310 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
314 name = line + SHA1_DIGEST_STRING_LENGTH;
316 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED, mtime);
319 static const struct got_error *
320 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
321 int nsubdirs, const char *refname, time_t mtime,
322 enum got_hash_algorithm algo)
324 const struct got_error *err = NULL;
329 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
334 abs_refname = (char *)refname;
336 linelen = getline(&line, &linesize, f);
340 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
343 if (linelen > 0 && line[linelen - 1] == '\n')
344 line[linelen - 1] = '\0';
345 for (i = 0; i < nsubdirs; i++) {
346 if (!ref_is_absolute &&
347 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
349 return got_error_from_errno("asprintf");
350 err = parse_packed_ref_line(ref, abs_refname, line,
352 if (!ref_is_absolute)
354 if (err || *ref != NULL)
359 } while (*ref == NULL);
365 static const struct got_error *
366 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
367 const char *name, int lock, enum got_hash_algorithm algo)
369 const struct got_error *err = NULL;
371 char *absname = NULL;
372 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
373 int ref_is_well_known = (subdir[0] == '\0' && is_well_known_ref(name));
377 if (!got_ref_name_is_valid(name))
378 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
380 if (ref_is_absolute || ref_is_well_known) {
381 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
382 return got_error_from_errno("asprintf");
383 absname = (char *)name;
385 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
386 subdir[0] ? "/" : "", name) == -1)
387 return got_error_from_errno("asprintf");
389 if (asprintf(&absname, "refs/%s%s%s",
390 subdir, subdir[0] ? "/" : "", name) == -1) {
391 err = got_error_from_errno("asprintf");
396 err = parse_ref_file(ref, name, absname, path, lock, algo);
398 if (!ref_is_absolute && !ref_is_well_known)
404 const struct got_error *
405 got_ref_open(struct got_reference **ref, struct got_repository *repo,
406 const char *refname, int lock)
408 const struct got_error *err = NULL;
409 char *packed_refs_path = NULL, *path_refs = NULL;
410 const char *subdirs[] = {
411 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
414 int well_known = is_well_known_ref(refname);
415 struct got_lockfile *lf = NULL;
419 path_refs = get_refs_dir_path(repo, refname);
420 if (path_refs == NULL) {
421 err = got_error_from_errno2("get_refs_dir_path", refname);
426 err = open_ref(ref, path_refs, "", refname, lock,
427 got_repo_get_object_format(repo));
431 /* Search on-disk refs before packed refs! */
432 for (i = 0; i < nitems(subdirs); i++) {
433 err = open_ref(ref, path_refs, subdirs[i], refname,
434 lock, got_repo_get_object_format(repo));
435 if ((err && err->code != GOT_ERR_NOT_REF) || *ref)
439 packed_refs_path = got_repo_get_path_packed_refs(repo);
440 if (packed_refs_path == NULL) {
441 err = got_error_from_errno(
442 "got_repo_get_path_packed_refs");
447 err = got_lockfile_lock(&lf, packed_refs_path, -1);
451 f = fopen(packed_refs_path, "rbe");
454 if (fstat(fileno(f), &sb) == -1) {
455 err = got_error_from_errno2("fstat",
459 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
460 refname, sb.st_mtime,
461 got_repo_get_object_format(repo));
463 if (fclose(f) == EOF) {
464 err = got_error_from_errno("fclose");
473 if (!err && *ref == NULL)
474 err = got_error_not_ref(refname);
476 got_lockfile_unlock(lf, -1);
477 free(packed_refs_path);
483 got_ref_close(struct got_reference *ref)
485 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
486 free(ref->ref.symref.name);
487 free(ref->ref.symref.ref);
489 free(ref->ref.ref.name);
493 struct got_reference *
494 got_ref_dup(struct got_reference *ref)
496 struct got_reference *ret;
498 ret = calloc(1, sizeof(*ret));
502 ret->flags = ref->flags;
503 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
504 ret->ref.symref.name = strdup(ref->ref.symref.name);
505 if (ret->ref.symref.name == NULL) {
509 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
510 if (ret->ref.symref.ref == NULL) {
511 free(ret->ref.symref.name);
516 ret->ref.ref.name = strdup(ref->ref.ref.name);
517 if (ret->ref.ref.name == NULL) {
521 memcpy(&ret->ref.ref.id, &ref->ref.ref.id,
522 sizeof(ret->ref.ref.id));
528 const struct got_error *
529 got_reflist_entry_dup(struct got_reflist_entry **newp,
530 struct got_reflist_entry *re)
532 const struct got_error *err = NULL;
533 struct got_reflist_entry *new;
537 new = malloc(sizeof(*new));
539 return got_error_from_errno("malloc");
541 new->ref = got_ref_dup(re->ref);
542 if (new->ref == NULL) {
543 err = got_error_from_errno("got_ref_dup");
552 const struct got_error *
553 got_ref_resolve_symbolic(struct got_reference **resolved,
554 struct got_repository *repo, struct got_reference *ref)
556 struct got_reference *nextref;
557 const struct got_error *err;
559 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
563 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
564 err = got_ref_resolve_symbolic(resolved, repo, nextref);
566 *resolved = got_ref_dup(nextref);
568 got_ref_close(nextref);
572 static const struct got_error *
573 ref_resolve(struct got_object_id **id, struct got_repository *repo,
574 struct got_reference *ref, int recursion)
576 const struct got_error *err;
579 return got_error_msg(GOT_ERR_RECURSION,
580 "reference recursion limit reached");
582 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
583 struct got_reference *resolved = NULL;
584 err = got_ref_resolve_symbolic(&resolved, repo, ref);
586 err = ref_resolve(id, repo, resolved, --recursion);
588 got_ref_close(resolved);
592 *id = calloc(1, sizeof(**id));
594 return got_error_from_errno("calloc");
595 memcpy(*id, &ref->ref.ref.id, sizeof(**id));
599 const struct got_error *
600 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
601 struct got_reference *ref)
603 return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
607 got_ref_to_str(struct got_reference *ref)
611 if (ref->flags & GOT_REF_IS_SYMBOLIC)
612 return strdup(ref->ref.symref.ref);
614 if (got_object_id_str(&str, &ref->ref.ref.id) != NULL)
621 got_ref_get_name(struct got_reference *ref)
623 if (ref->flags & GOT_REF_IS_SYMBOLIC)
624 return ref->ref.symref.name;
626 return ref->ref.ref.name;
630 got_ref_get_symref_target(struct got_reference *ref)
632 if (ref->flags & GOT_REF_IS_SYMBOLIC)
633 return ref->ref.symref.ref;
639 got_ref_get_mtime(struct got_reference *ref)
644 const struct got_error *
645 got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
646 struct got_reference* re2)
648 const char *name1 = got_ref_get_name(re1);
649 const char *name2 = got_ref_get_name(re2);
651 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
655 static const struct got_error *
656 get_committer_time(struct got_reference *ref, struct got_repository *repo)
658 const struct got_error *err = NULL;
660 struct got_commit_object *commit = NULL;
661 struct got_tag_object *tag = NULL;
662 struct got_object_id *id = NULL;
664 err = got_ref_resolve(&id, repo, ref);
668 err = got_object_get_type(&obj_type, repo, id);
673 case GOT_OBJ_TYPE_COMMIT:
674 err = got_object_open_as_commit(&commit, repo, id);
677 ref->committer_time =
678 got_object_commit_get_committer_time(commit);
680 case GOT_OBJ_TYPE_TAG:
681 err = got_object_open_as_tag(&tag, repo, id);
684 ref->committer_time = got_object_tag_get_tagger_time(tag);
687 /* best effort for other object types */
688 ref->committer_time = got_ref_get_mtime(ref);
694 got_object_commit_close(commit);
696 got_object_tag_close(tag);
700 const struct got_error *
701 got_ref_cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
702 struct got_reference *ref2)
704 const struct got_error *err = NULL;
705 struct got_repository *repo = arg;
709 if (ref1->committer_time == 0) {
710 err = get_committer_time(ref1, repo);
714 if (ref2->committer_time == 0) {
715 err = get_committer_time(ref2, repo);
720 /* Put latest tags first. */
721 if (ref1->committer_time < ref2->committer_time)
723 else if (ref1->committer_time > ref2->committer_time)
726 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
731 const struct got_error *
732 got_ref_cmp_by_commit_timestamp_descending(void *arg, int *cmp,
733 struct got_reference *ref1, struct got_reference *ref2)
735 const struct got_error *err = NULL;
736 struct got_repository *repo = arg;
740 if (ref1->committer_time == 0) {
741 err = get_committer_time(ref1, repo);
745 if (ref2->committer_time == 0) {
746 err = get_committer_time(ref2, repo);
751 if (ref1->committer_time < ref2->committer_time)
753 else if (ref2->committer_time < ref1->committer_time)
756 return got_ref_cmp_by_name(arg, cmp, ref1, ref2);
761 const struct got_error *
762 got_reflist_insert(struct got_reflist_entry **newp,
763 struct got_reflist_head *refs, struct got_reference *ref,
764 got_ref_cmp_cb cmp_cb, void *cmp_arg)
766 const struct got_error *err;
767 struct got_reflist_entry *new, *re;
772 if (cmp_cb != got_ref_cmp_by_name && (ref->flags & GOT_REF_IS_PACKED)) {
774 * If we are not sorting elements by name then we must still
775 * detect collisions between a packed ref and an on-disk ref
776 * using the same name. On-disk refs take precedence and are
777 * already present on the list before packed refs get added.
779 TAILQ_FOREACH(re, refs, entry) {
780 err = got_ref_cmp_by_name(NULL, &cmp, re->ref, ref);
788 new = malloc(sizeof(*new));
790 return got_error_from_errno("malloc");
795 * We must de-duplicate entries on insert because packed-refs may
796 * contain redundant entries. On-disk refs take precedence.
797 * This code assumes that on-disk revs are read before packed-refs.
798 * We're iterating the list anyway, so insert elements sorted by name.
800 * Many callers will provide paths in a somewhat sorted order.
801 * Iterating backwards from the tail of the list should be more
802 * efficient than traversing through the entire list each time
803 * an element is inserted.
805 re = TAILQ_LAST(refs, got_reflist_head);
807 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
815 } else if (cmp < 0) {
816 TAILQ_INSERT_AFTER(refs, re, new, entry);
819 re = TAILQ_PREV(re, got_reflist_head, entry);
822 TAILQ_INSERT_HEAD(refs, new, entry);
826 const struct got_error *
827 got_reflist_sort(struct got_reflist_head *refs,
828 got_ref_cmp_cb cmp_cb, void *cmp_arg)
830 const struct got_error *err = NULL;
831 struct got_reflist_entry *re, *tmp, *new;
832 struct got_reflist_head sorted;
836 TAILQ_FOREACH_SAFE(re, refs, entry, tmp) {
837 struct got_reference *ref = re->ref;
838 TAILQ_REMOVE(refs, re, entry);
840 err = got_reflist_insert(&new, &sorted, ref, cmp_cb, cmp_arg);
841 if (err || new == NULL /* duplicate */)
847 TAILQ_CONCAT(refs, &sorted, entry);
851 static const struct got_error *
852 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
853 const char *subdir, struct got_repository *repo,
854 got_ref_cmp_cb cmp_cb, void *cmp_arg)
856 const struct got_error *err = NULL;
860 while (subdir[0] == '/')
863 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
864 return got_error_from_errno("asprintf");
866 d = opendir(path_subdir);
872 struct got_reference *ref;
880 if (strcmp(dent->d_name, ".") == 0 ||
881 strcmp(dent->d_name, "..") == 0)
884 err = got_path_dirent_type(&type, path_subdir, dent);
890 err = open_ref(&ref, path_refs, subdir, dent->d_name,
891 0, got_repo_get_object_format(repo));
892 if (err && err->code == GOT_ERR_BAD_REF_NAME)
897 struct got_reflist_entry *new;
898 err = got_reflist_insert(&new, refs, ref,
900 if (err || new == NULL /* duplicate */)
907 if (asprintf(&child, "%s%s%s", subdir,
908 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
909 err = got_error_from_errno("asprintf");
912 err = gather_on_disk_refs(refs, path_refs, child, repo,
927 const struct got_error *
928 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
929 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
931 const struct got_error *err;
932 char *packed_refs_path = NULL, *path_refs = NULL;
933 char *abs_namespace = NULL, *buf = NULL;
934 const char *ondisk_ref_namespace = NULL;
937 struct got_reference *ref;
938 struct got_reflist_entry *new;
940 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
941 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
942 if (path_refs == NULL) {
943 err = got_error_from_errno("get_refs_dir_path");
946 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0,
947 got_repo_get_object_format(repo));
950 err = got_reflist_insert(&new, refs, ref, cmp_cb, cmp_arg);
951 if (err || new == NULL /* duplicate */)
953 if (err && err->code != GOT_ERR_NOT_REF)
956 /* Try listing a single reference. */
957 const char *refname = ref_namespace;
958 path_refs = get_refs_dir_path(repo, refname);
959 if (path_refs == NULL) {
960 err = got_error_from_errno("get_refs_dir_path");
963 err = open_ref(&ref, path_refs, "", refname, 0,
964 got_repo_get_object_format(repo));
966 if (err->code != GOT_ERR_NOT_REF)
968 /* Try to look up references in a given namespace. */
970 err = got_reflist_insert(&new, refs, ref,
972 if (err || new == NULL /* duplicate */)
980 /* Canonicalize the path to eliminate double-slashes if any. */
981 if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
982 err = got_error_from_errno("asprintf");
985 len = strlen(abs_namespace) + 1;
988 err = got_error_from_errno("malloc");
991 err = got_canonpath(abs_namespace, buf, len);
994 ondisk_ref_namespace = buf;
995 while (ondisk_ref_namespace[0] == '/')
996 ondisk_ref_namespace++;
997 if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
998 ondisk_ref_namespace += 5;
999 else if (strcmp(ondisk_ref_namespace, "refs") == 0)
1000 ondisk_ref_namespace = "";
1003 /* Gather on-disk refs before parsing packed-refs. */
1005 path_refs = get_refs_dir_path(repo, "");
1006 if (path_refs == NULL) {
1007 err = got_error_from_errno("get_refs_dir_path");
1010 err = gather_on_disk_refs(refs, path_refs,
1011 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
1017 * The packed-refs file may contain redundant entries, in which
1018 * case on-disk refs take precedence.
1020 packed_refs_path = got_repo_get_path_packed_refs(repo);
1021 if (packed_refs_path == NULL) {
1022 err = got_error_from_errno("got_repo_get_path_packed_refs");
1026 f = fopen(packed_refs_path, "re");
1028 size_t linesize = 0;
1032 if (fstat(fileno(f), &sb) == -1) {
1033 err = got_error_from_errno2("fstat", packed_refs_path);
1037 linelen = getline(&line, &linesize, f);
1038 if (linelen == -1) {
1041 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1044 if (linelen > 0 && line[linelen - 1] == '\n')
1045 line[linelen - 1] = '\0';
1046 err = parse_packed_ref_line(&ref, NULL, line,
1047 sb.st_mtime, got_repo_get_object_format(repo));
1051 if (ref_namespace) {
1053 name = got_ref_get_name(ref);
1054 if (!got_path_is_child(name,
1056 strlen(ref_namespace))) {
1061 err = got_reflist_insert(&new, refs, ref,
1063 if (err || new == NULL /* duplicate */)
1071 free(packed_refs_path);
1072 free(abs_namespace);
1076 if (f && fclose(f) == EOF && err == NULL)
1077 err = got_error_from_errno("fclose");
1082 got_ref_list_free(struct got_reflist_head *refs)
1084 struct got_reflist_entry *re;
1086 while ((re = TAILQ_FIRST(refs))) {
1087 TAILQ_REMOVE(refs, re, entry);
1088 got_ref_close(re->ref);
1094 got_ref_is_symbolic(struct got_reference *ref)
1096 return (ref->flags & GOT_REF_IS_SYMBOLIC);
1099 const struct got_error *
1100 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1102 if (ref->flags & GOT_REF_IS_SYMBOLIC)
1103 return got_error(GOT_ERR_BAD_REF_TYPE);
1105 memcpy(&ref->ref.ref.id, id, sizeof(ref->ref.ref.id));
1109 const struct got_error *
1110 got_ref_change_symref(struct got_reference *ref, const char *refname)
1114 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1115 return got_error(GOT_ERR_BAD_REF_TYPE);
1117 new_name = strdup(refname);
1118 if (new_name == NULL)
1119 return got_error_from_errno("strdup");
1121 free(ref->ref.symref.ref);
1122 ref->ref.symref.ref = new_name;
1126 const struct got_error *
1127 got_ref_change_symref_to_ref(struct got_reference *symref,
1128 struct got_object_id *id)
1130 if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1131 return got_error(GOT_ERR_BAD_REF_TYPE);
1133 symref->ref.ref.name = symref->ref.symref.name;
1134 memcpy(&symref->ref.ref.id, id, sizeof(symref->ref.ref.id));
1135 symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1139 const struct got_error *
1140 got_ref_write(struct got_reference *ref, struct got_repository *repo)
1142 const struct got_error *err = NULL, *unlock_err = NULL;
1143 const char *name = got_ref_get_name(ref);
1144 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1145 struct got_lockfile *lf = NULL;
1150 path_refs = get_refs_dir_path(repo, name);
1151 if (path_refs == NULL) {
1152 err = got_error_from_errno2("get_refs_dir_path", name);
1156 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1157 err = got_error_from_errno("asprintf");
1161 err = got_opentemp_named(&tmppath, &f, path, "");
1164 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1166 err = got_path_dirname(&parent, path);
1169 err = got_path_mkdir(parent);
1173 err = got_opentemp_named(&tmppath, &f, path, "");
1178 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1179 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1180 if (n != strlen(ref->ref.symref.ref) + 6) {
1181 err = got_ferror(f, GOT_ERR_IO);
1188 err = got_object_id_str(&hex, &ref->ref.ref.id);
1192 n = fprintf(f, "%s\n", hex);
1195 err = got_ferror(f, GOT_ERR_IO);
1200 if (ref->lf == NULL) {
1201 err = got_lockfile_lock(&lf, path, -1);
1206 /* XXX: check if old content matches our expectations? */
1208 if (stat(path, &sb) != 0) {
1209 if (errno != ENOENT) {
1210 err = got_error_from_errno2("stat", path);
1213 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1216 if (fchmod(fileno(f), sb.st_mode) != 0) {
1217 err = got_error_from_errno2("fchmod", tmppath);
1221 if (rename(tmppath, path) != 0) {
1222 err = got_error_from_errno3("rename", tmppath, path);
1228 if (stat(path, &sb) == -1) {
1229 err = got_error_from_errno2("stat", path);
1232 ref->mtime = sb.st_mtime;
1234 if (ref->lf == NULL && lf)
1235 unlock_err = got_lockfile_unlock(lf, -1);
1237 if (fclose(f) == EOF && err == NULL)
1238 err = got_error_from_errno("fclose");
1243 if (unlink(tmppath) == -1 && err == NULL)
1244 err = got_error_from_errno2("unlink", tmppath);
1247 return err ? err : unlock_err;
1250 static const struct got_error *
1251 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1253 const struct got_error *err = NULL, *unlock_err = NULL;
1254 struct got_lockfile *lf = NULL;
1255 FILE *f = NULL, *tmpf = NULL;
1256 char *line = NULL, *packed_refs_path, *tmppath = NULL;
1257 size_t linesize = 0;
1258 struct got_reflist_head refs;
1259 int found_delref = 0;
1261 /* The packed-refs file does not cotain symbolic references. */
1262 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1263 return got_error(GOT_ERR_BAD_REF_DATA);
1267 packed_refs_path = got_repo_get_path_packed_refs(repo);
1268 if (packed_refs_path == NULL)
1269 return got_error_from_errno("got_repo_get_path_packed_refs");
1271 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path, "");
1275 if (delref->lf == NULL) {
1276 err = got_lockfile_lock(&lf, packed_refs_path, -1);
1281 f = fopen(packed_refs_path, "re");
1283 err = got_error_from_errno2("fopen", packed_refs_path);
1288 struct got_reference *ref;
1289 struct got_reflist_entry *new;
1291 linelen = getline(&line, &linesize, f);
1292 if (linelen == -1) {
1295 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1298 if (linelen > 0 && line[linelen - 1] == '\n')
1299 line[linelen - 1] = '\0';
1300 err = parse_packed_ref_line(&ref, NULL, line, 0,
1301 got_repo_get_object_format(repo));
1307 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1308 got_object_id_cmp(&ref->ref.ref.id,
1309 &delref->ref.ref.id) == 0) {
1315 err = got_reflist_insert(&new, &refs, ref,
1316 got_ref_cmp_by_name, NULL);
1317 if (err || new == NULL /* duplicate */)
1324 struct got_reflist_entry *re;
1328 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1329 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1330 err = got_ferror(f, GOT_ERR_IO);
1334 TAILQ_FOREACH(re, &refs, entry) {
1338 err = got_object_id_str(&hex, &re->ref->ref.ref.id);
1342 n = fprintf(tmpf, "%s ", hex);
1345 err = got_ferror(f, GOT_ERR_IO);
1349 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1350 if (n != strlen(re->ref->ref.ref.name) + 1) {
1351 err = got_ferror(f, GOT_ERR_IO);
1356 if (fflush(tmpf) != 0) {
1357 err = got_error_from_errno("fflush");
1361 if (fstat(fileno(f), &sb) != 0) {
1362 if (errno != ENOENT) {
1363 err = got_error_from_errno2("fstat",
1367 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1370 if (fchmod(fileno(tmpf), sb.st_mode) != 0) {
1371 err = got_error_from_errno2("fchmod", tmppath);
1375 if (rename(tmppath, packed_refs_path) != 0) {
1376 err = got_error_from_errno3("rename", tmppath,
1384 if (delref->lf == NULL && lf)
1385 unlock_err = got_lockfile_unlock(lf, -1);
1387 if (fclose(f) == EOF && err == NULL)
1388 err = got_error_from_errno("fclose");
1390 if (tmppath && unlink(tmppath) == -1 && err == NULL)
1391 err = got_error_from_errno2("unlink", tmppath);
1392 if (tmpf && fclose(tmpf) == EOF && err == NULL)
1393 err = got_error_from_errno("fclose");
1395 free(packed_refs_path);
1397 got_ref_list_free(&refs);
1398 return err ? err : unlock_err;
1401 static const struct got_error *
1402 delete_loose_ref(struct got_reference *ref, struct got_repository *repo)
1404 const struct got_error *err = NULL, *unlock_err = NULL;
1405 const char *name = got_ref_get_name(ref);
1406 char *path_refs = NULL, *path = NULL;
1407 struct got_lockfile *lf = NULL;
1409 path_refs = get_refs_dir_path(repo, name);
1410 if (path_refs == NULL) {
1411 err = got_error_from_errno2("get_refs_dir_path", name);
1415 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1416 err = got_error_from_errno("asprintf");
1420 if (ref->lf == NULL) {
1421 err = got_lockfile_lock(&lf, path, -1);
1426 /* XXX: check if old content matches our expectations? */
1428 if (unlink(path) == -1)
1429 err = got_error_from_errno2("unlink", path);
1431 if (ref->lf == NULL && lf)
1432 unlock_err = got_lockfile_unlock(lf, -1);
1436 return err ? err : unlock_err;
1439 const struct got_error *
1440 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1442 const struct got_error *err = NULL;
1443 struct got_reference *ref2;
1445 if (ref->flags & GOT_REF_IS_PACKED) {
1446 err = delete_packed_ref(ref, repo);
1450 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1452 if (err->code == GOT_ERR_NOT_REF)
1457 err = delete_loose_ref(ref2, repo);
1458 got_ref_close(ref2);
1461 err = delete_loose_ref(ref, repo);
1465 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1467 if (err->code == GOT_ERR_NOT_REF)
1472 err = delete_packed_ref(ref2, repo);
1473 got_ref_close(ref2);
1478 const struct got_error *
1479 got_ref_unlock(struct got_reference *ref)
1481 const struct got_error *err;
1482 err = got_lockfile_unlock(ref->lf, -1);
1487 struct got_reflist_object_id_map {
1488 struct got_object_idset *idset;
1491 struct got_reflist_object_id_map_entry {
1492 struct got_reflist_head refs;
1495 static const struct got_error *
1496 add_object_id_map_entry(struct got_object_idset *idset,
1497 struct got_object_id *id, struct got_reflist_entry *re)
1499 const struct got_error *err = NULL;
1500 struct got_reflist_object_id_map_entry *ent;
1501 struct got_reflist_entry *new;
1503 ent = got_object_idset_get(idset, id);
1505 ent = malloc(sizeof(*ent));
1507 return got_error_from_errno("malloc");
1509 TAILQ_INIT(&ent->refs);
1510 err = got_object_idset_add(idset, id, ent);
1517 err = got_reflist_entry_dup(&new, re);
1521 TAILQ_INSERT_TAIL(&ent->refs, new, entry);
1525 const struct got_error *
1526 got_reflist_object_id_map_create(struct got_reflist_object_id_map **map,
1527 struct got_reflist_head *refs, struct got_repository *repo)
1529 const struct got_error *err = NULL;
1530 struct got_object_idset *idset;
1531 struct got_object_id *id = NULL;
1532 struct got_reflist_entry *re;
1534 idset = got_object_idset_alloc();
1536 return got_error_from_errno("got_object_idset_alloc");
1538 *map = malloc(sizeof(**map));
1540 got_object_idset_free(idset);
1541 return got_error_from_errno("malloc");
1543 (*map)->idset = idset;
1545 TAILQ_FOREACH(re, refs, entry) {
1546 struct got_tag_object *tag = NULL;
1548 err = got_ref_resolve(&id, repo, re->ref);
1552 err = add_object_id_map_entry(idset, id, re);
1556 if (strstr(got_ref_get_name(re->ref), "/tags/") == NULL) {
1562 err = got_object_open_as_tag(&tag, repo, id);
1564 if (err->code != GOT_ERR_OBJ_TYPE)
1566 /* Ref points at something other than a tag. */
1574 err = add_object_id_map_entry(idset,
1575 got_object_tag_get_object_id(tag), re);
1576 got_object_tag_close(tag);
1586 got_reflist_object_id_map_free(*map);
1592 struct got_reflist_head *
1593 got_reflist_object_id_map_lookup(struct got_reflist_object_id_map *map,
1594 struct got_object_id *id)
1596 struct got_reflist_object_id_map_entry *ent;
1597 ent = got_object_idset_get(map->idset, id);
1603 static const struct got_error *
1604 free_id_map_entry(struct got_object_id *id, void *data, void *arg)
1606 struct got_reflist_object_id_map_entry *ent = data;
1608 got_ref_list_free(&ent->refs);
1614 got_reflist_object_id_map_free(struct got_reflist_object_id_map *map)
1616 got_object_idset_for_each(map->idset, free_id_map_entry, NULL);
1617 got_object_idset_free(map->idset);