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_hash.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 struct got_object_id id;
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;
93 * Cached timestamp for got_ref_cmp_by_commit_timestamp_descending()
94 * and got_ref_cmp_tags().
96 time_t committer_time;
99 static const struct got_error *
100 alloc_ref(struct got_reference **ref, const char *name,
101 struct got_object_id *id, int flags, time_t mtime)
103 const struct got_error *err = NULL;
105 *ref = calloc(1, sizeof(**ref));
107 return got_error_from_errno("calloc");
109 memcpy(&(*ref)->ref.ref.id, id, sizeof((*ref)->ref.ref.id));
110 (*ref)->flags = flags;
111 (*ref)->ref.ref.name = strdup(name);
112 (*ref)->mtime = mtime;
113 if ((*ref)->ref.ref.name == NULL) {
114 err = got_error_from_errno("strdup");
121 static const struct got_error *
122 alloc_symref(struct got_reference **ref, const char *name,
123 const char *target_ref, int flags)
125 const struct got_error *err = NULL;
127 *ref = calloc(1, sizeof(**ref));
129 return got_error_from_errno("calloc");
131 (*ref)->flags = GOT_REF_IS_SYMBOLIC | flags;
132 (*ref)->ref.symref.name = strdup(name);
133 if ((*ref)->ref.symref.name == NULL) {
134 err = got_error_from_errno("strdup");
139 (*ref)->ref.symref.ref = strdup(target_ref);
140 if ((*ref)->ref.symref.ref == NULL) {
141 err = got_error_from_errno("strdup");
148 static const struct got_error *
149 parse_symref(struct got_reference **ref, const char *name, const char *line)
152 return got_error(GOT_ERR_BAD_REF_DATA);
154 return alloc_symref(ref, name, line, 0);
157 static const struct got_error *
158 parse_ref_line(struct got_reference **ref, const char *name, const char *line,
159 time_t mtime, enum got_hash_algorithm algo)
161 struct got_object_id id;
163 if (strncmp(line, "ref: ", 5) == 0) {
165 return parse_symref(ref, name, line);
168 if (!got_parse_object_id(&id, line, algo))
169 return got_error(GOT_ERR_BAD_REF_DATA);
171 return alloc_ref(ref, name, &id, 0, mtime);
174 static const struct got_error *
175 parse_ref_file(struct got_reference **ref, const char *name,
176 const char *absname, const char *abspath, int lock,
177 enum got_hash_algorithm algo)
179 const struct got_error *err = NULL;
184 struct got_lockfile *lf = NULL;
188 err = got_lockfile_lock(&lf, abspath, -1);
190 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
191 err = got_error_not_ref(name);
196 f = fopen(abspath, "rbe");
198 if (errno != ENOTDIR && errno != ENOENT)
199 err = got_error_from_errno2("fopen", abspath);
201 err = got_error_not_ref(name);
203 got_lockfile_unlock(lf, -1);
206 if (fstat(fileno(f), &sb) == -1) {
207 err = got_error_from_errno2("fstat", abspath);
211 linelen = getline(&line, &linesize, f);
214 err = NULL; /* ignore empty files (could be locks) */
217 err = got_error(GOT_ERR_NOT_REF);
219 err = got_ferror(f, GOT_ERR_IO);
221 err = got_error_from_errno2("getline", abspath);
224 got_lockfile_unlock(lf, -1);
227 while (linelen > 0 && line[linelen - 1] == '\n') {
228 line[linelen - 1] = '\0';
232 err = parse_ref_line(ref, absname, line, sb.st_mtime, algo);
235 got_lockfile_unlock(lf, -1);
240 got_lockfile_unlock(lf, -1);
245 if (fclose(f) == EOF && err == NULL) {
246 err = got_error_from_errno("fclose");
249 got_ref_unlock(*ref);
258 is_well_known_ref(const char *refname)
260 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
261 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
262 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
263 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
267 get_refs_dir_path(struct got_repository *repo, const char *refname)
269 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
270 return strdup(got_repo_get_path_git_dir(repo));
272 return got_repo_get_path_refs(repo);
275 const struct got_error *
276 got_ref_alloc(struct got_reference **ref, const char *name,
277 struct got_object_id *id)
279 if (!got_ref_name_is_valid(name))
280 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
282 return alloc_ref(ref, name, id, 0, 0);
285 const struct got_error *
286 got_ref_alloc_symref(struct got_reference **ref, const char *name,
287 struct got_reference *target_ref)
289 if (!got_ref_name_is_valid(name))
290 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
292 return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
295 static const struct got_error *
296 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
297 const char *line, time_t mtime, enum got_hash_algorithm algo)
299 struct got_object_id id;
304 if (line[0] == '#' || line[0] == '^')
307 if (!got_parse_object_id(&id, line, algo))
308 return got_error(GOT_ERR_BAD_REF_DATA);
311 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
315 name = line + SHA1_DIGEST_STRING_LENGTH;
317 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED, mtime);
320 static const struct got_error *
321 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
322 int nsubdirs, const char *refname, time_t mtime,
323 enum got_hash_algorithm algo)
325 const struct got_error *err = NULL;
330 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
335 abs_refname = (char *)refname;
337 linelen = getline(&line, &linesize, f);
341 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
344 if (linelen > 0 && line[linelen - 1] == '\n')
345 line[linelen - 1] = '\0';
346 for (i = 0; i < nsubdirs; i++) {
347 if (!ref_is_absolute &&
348 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
350 return got_error_from_errno("asprintf");
351 err = parse_packed_ref_line(ref, abs_refname, line,
353 if (!ref_is_absolute)
355 if (err || *ref != NULL)
360 } while (*ref == NULL);
366 static const struct got_error *
367 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
368 const char *name, int lock, enum got_hash_algorithm algo)
370 const struct got_error *err = NULL;
372 char *absname = NULL;
373 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
374 int ref_is_well_known = (subdir[0] == '\0' && is_well_known_ref(name));
378 if (!got_ref_name_is_valid(name))
379 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
381 if (ref_is_absolute || ref_is_well_known) {
382 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
383 return got_error_from_errno("asprintf");
384 absname = (char *)name;
386 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
387 subdir[0] ? "/" : "", name) == -1)
388 return got_error_from_errno("asprintf");
390 if (asprintf(&absname, "refs/%s%s%s",
391 subdir, subdir[0] ? "/" : "", name) == -1) {
392 err = got_error_from_errno("asprintf");
397 err = parse_ref_file(ref, name, absname, path, lock, algo);
399 if (!ref_is_absolute && !ref_is_well_known)
405 const struct got_error *
406 got_ref_open(struct got_reference **ref, struct got_repository *repo,
407 const char *refname, int lock)
409 const struct got_error *err = NULL;
410 char *packed_refs_path = NULL, *path_refs = NULL;
411 const char *subdirs[] = {
412 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
415 int well_known = is_well_known_ref(refname);
416 struct got_lockfile *lf = NULL;
420 path_refs = get_refs_dir_path(repo, refname);
421 if (path_refs == NULL) {
422 err = got_error_from_errno2("get_refs_dir_path", refname);
427 err = open_ref(ref, path_refs, "", refname, lock,
428 got_repo_get_object_format(repo));
432 /* Search on-disk refs before packed refs! */
433 for (i = 0; i < nitems(subdirs); i++) {
434 err = open_ref(ref, path_refs, subdirs[i], refname,
435 lock, got_repo_get_object_format(repo));
436 if ((err && err->code != GOT_ERR_NOT_REF) || *ref)
440 packed_refs_path = got_repo_get_path_packed_refs(repo);
441 if (packed_refs_path == NULL) {
442 err = got_error_from_errno(
443 "got_repo_get_path_packed_refs");
448 err = got_lockfile_lock(&lf, packed_refs_path, -1);
452 f = fopen(packed_refs_path, "rbe");
455 if (fstat(fileno(f), &sb) == -1) {
456 err = got_error_from_errno2("fstat",
460 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
461 refname, sb.st_mtime,
462 got_repo_get_object_format(repo));
464 if (fclose(f) == EOF) {
465 err = got_error_from_errno("fclose");
474 if (!err && *ref == NULL)
475 err = got_error_not_ref(refname);
477 got_lockfile_unlock(lf, -1);
478 free(packed_refs_path);
484 got_ref_close(struct got_reference *ref)
486 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
487 free(ref->ref.symref.name);
488 free(ref->ref.symref.ref);
490 free(ref->ref.ref.name);
494 struct got_reference *
495 got_ref_dup(struct got_reference *ref)
497 struct got_reference *ret;
499 ret = calloc(1, sizeof(*ret));
503 ret->flags = ref->flags;
504 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
505 ret->ref.symref.name = strdup(ref->ref.symref.name);
506 if (ret->ref.symref.name == NULL) {
510 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
511 if (ret->ref.symref.ref == NULL) {
512 free(ret->ref.symref.name);
517 ret->ref.ref.name = strdup(ref->ref.ref.name);
518 if (ret->ref.ref.name == NULL) {
522 memcpy(&ret->ref.ref.id, &ref->ref.ref.id,
523 sizeof(ret->ref.ref.id));
529 const struct got_error *
530 got_reflist_entry_dup(struct got_reflist_entry **newp,
531 struct got_reflist_entry *re)
533 const struct got_error *err = NULL;
534 struct got_reflist_entry *new;
538 new = malloc(sizeof(*new));
540 return got_error_from_errno("malloc");
542 new->ref = got_ref_dup(re->ref);
543 if (new->ref == NULL) {
544 err = got_error_from_errno("got_ref_dup");
553 const struct got_error *
554 got_ref_resolve_symbolic(struct got_reference **resolved,
555 struct got_repository *repo, struct got_reference *ref)
557 struct got_reference *nextref;
558 const struct got_error *err;
560 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
564 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
565 err = got_ref_resolve_symbolic(resolved, repo, nextref);
567 *resolved = got_ref_dup(nextref);
569 got_ref_close(nextref);
573 static const struct got_error *
574 ref_resolve(struct got_object_id **id, struct got_repository *repo,
575 struct got_reference *ref, int recursion)
577 const struct got_error *err;
580 return got_error_msg(GOT_ERR_RECURSION,
581 "reference recursion limit reached");
583 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
584 struct got_reference *resolved = NULL;
585 err = got_ref_resolve_symbolic(&resolved, repo, ref);
587 err = ref_resolve(id, repo, resolved, --recursion);
589 got_ref_close(resolved);
593 *id = calloc(1, sizeof(**id));
595 return got_error_from_errno("calloc");
596 memcpy(*id, &ref->ref.ref.id, sizeof(**id));
600 const struct got_error *
601 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
602 struct got_reference *ref)
604 return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
608 got_ref_to_str(struct got_reference *ref)
612 if (ref->flags & GOT_REF_IS_SYMBOLIC)
613 return strdup(ref->ref.symref.ref);
615 if (got_object_id_str(&str, &ref->ref.ref.id) != NULL)
622 got_ref_get_name(struct got_reference *ref)
624 if (ref->flags & GOT_REF_IS_SYMBOLIC)
625 return ref->ref.symref.name;
627 return ref->ref.ref.name;
631 got_ref_get_symref_target(struct got_reference *ref)
633 if (ref->flags & GOT_REF_IS_SYMBOLIC)
634 return ref->ref.symref.ref;
640 got_ref_get_mtime(struct got_reference *ref)
645 const struct got_error *
646 got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
647 struct got_reference* re2)
649 const char *name1 = got_ref_get_name(re1);
650 const char *name2 = got_ref_get_name(re2);
652 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
656 static const struct got_error *
657 get_committer_time(struct got_reference *ref, struct got_repository *repo)
659 const struct got_error *err = NULL;
661 struct got_commit_object *commit = NULL;
662 struct got_tag_object *tag = NULL;
663 struct got_object_id *id = NULL;
665 err = got_ref_resolve(&id, repo, ref);
669 err = got_object_get_type(&obj_type, repo, id);
674 case GOT_OBJ_TYPE_COMMIT:
675 err = got_object_open_as_commit(&commit, repo, id);
678 ref->committer_time =
679 got_object_commit_get_committer_time(commit);
681 case GOT_OBJ_TYPE_TAG:
682 err = got_object_open_as_tag(&tag, repo, id);
685 ref->committer_time = got_object_tag_get_tagger_time(tag);
688 /* best effort for other object types */
689 ref->committer_time = got_ref_get_mtime(ref);
695 got_object_commit_close(commit);
697 got_object_tag_close(tag);
701 const struct got_error *
702 got_ref_cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
703 struct got_reference *ref2)
705 const struct got_error *err = NULL;
706 struct got_repository *repo = arg;
710 if (ref1->committer_time == 0) {
711 err = get_committer_time(ref1, repo);
715 if (ref2->committer_time == 0) {
716 err = get_committer_time(ref2, repo);
721 /* Put latest tags first. */
722 if (ref1->committer_time < ref2->committer_time)
724 else if (ref1->committer_time > ref2->committer_time)
727 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
732 const struct got_error *
733 got_ref_cmp_by_commit_timestamp_descending(void *arg, int *cmp,
734 struct got_reference *ref1, struct got_reference *ref2)
736 const struct got_error *err = NULL;
737 struct got_repository *repo = arg;
741 if (ref1->committer_time == 0) {
742 err = get_committer_time(ref1, repo);
746 if (ref2->committer_time == 0) {
747 err = get_committer_time(ref2, repo);
752 if (ref1->committer_time < ref2->committer_time)
754 else if (ref2->committer_time < ref1->committer_time)
757 return got_ref_cmp_by_name(arg, cmp, ref1, ref2);
762 const struct got_error *
763 got_reflist_insert(struct got_reflist_entry **newp,
764 struct got_reflist_head *refs, struct got_reference *ref,
765 got_ref_cmp_cb cmp_cb, void *cmp_arg)
767 const struct got_error *err;
768 struct got_reflist_entry *new, *re;
773 if (cmp_cb != got_ref_cmp_by_name && (ref->flags & GOT_REF_IS_PACKED)) {
775 * If we are not sorting elements by name then we must still
776 * detect collisions between a packed ref and an on-disk ref
777 * using the same name. On-disk refs take precedence and are
778 * already present on the list before packed refs get added.
780 TAILQ_FOREACH(re, refs, entry) {
781 err = got_ref_cmp_by_name(NULL, &cmp, re->ref, ref);
789 new = malloc(sizeof(*new));
791 return got_error_from_errno("malloc");
796 * We must de-duplicate entries on insert because packed-refs may
797 * contain redundant entries. On-disk refs take precedence.
798 * This code assumes that on-disk revs are read before packed-refs.
799 * We're iterating the list anyway, so insert elements sorted by name.
801 * Many callers will provide paths in a somewhat sorted order.
802 * Iterating backwards from the tail of the list should be more
803 * efficient than traversing through the entire list each time
804 * an element is inserted.
806 re = TAILQ_LAST(refs, got_reflist_head);
808 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
816 } else if (cmp < 0) {
817 TAILQ_INSERT_AFTER(refs, re, new, entry);
820 re = TAILQ_PREV(re, got_reflist_head, entry);
823 TAILQ_INSERT_HEAD(refs, new, entry);
827 const struct got_error *
828 got_reflist_sort(struct got_reflist_head *refs,
829 got_ref_cmp_cb cmp_cb, void *cmp_arg)
831 const struct got_error *err = NULL;
832 struct got_reflist_entry *re, *tmp, *new;
833 struct got_reflist_head sorted;
837 TAILQ_FOREACH_SAFE(re, refs, entry, tmp) {
838 struct got_reference *ref = re->ref;
839 TAILQ_REMOVE(refs, re, entry);
841 err = got_reflist_insert(&new, &sorted, ref, cmp_cb, cmp_arg);
842 if (err || new == NULL /* duplicate */)
848 TAILQ_CONCAT(refs, &sorted, entry);
852 static const struct got_error *
853 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
854 const char *subdir, struct got_repository *repo,
855 got_ref_cmp_cb cmp_cb, void *cmp_arg)
857 const struct got_error *err = NULL;
861 while (subdir[0] == '/')
864 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
865 return got_error_from_errno("asprintf");
867 d = opendir(path_subdir);
873 struct got_reference *ref;
881 if (strcmp(dent->d_name, ".") == 0 ||
882 strcmp(dent->d_name, "..") == 0)
885 err = got_path_dirent_type(&type, path_subdir, dent);
891 err = open_ref(&ref, path_refs, subdir, dent->d_name,
892 0, got_repo_get_object_format(repo));
893 if (err && err->code == GOT_ERR_BAD_REF_NAME)
898 struct got_reflist_entry *new;
899 err = got_reflist_insert(&new, refs, ref,
901 if (err || new == NULL /* duplicate */)
908 if (asprintf(&child, "%s%s%s", subdir,
909 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
910 err = got_error_from_errno("asprintf");
913 err = gather_on_disk_refs(refs, path_refs, child, repo,
928 const struct got_error *
929 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
930 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
932 const struct got_error *err;
933 char *packed_refs_path = NULL, *path_refs = NULL;
934 char *abs_namespace = NULL, *buf = NULL;
935 const char *ondisk_ref_namespace = NULL;
938 struct got_reference *ref;
939 struct got_reflist_entry *new;
941 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
942 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
943 if (path_refs == NULL) {
944 err = got_error_from_errno("get_refs_dir_path");
947 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0,
948 got_repo_get_object_format(repo));
951 err = got_reflist_insert(&new, refs, ref, cmp_cb, cmp_arg);
952 if (err || new == NULL /* duplicate */)
954 if (err && err->code != GOT_ERR_NOT_REF)
957 /* Try listing a single reference. */
958 const char *refname = ref_namespace;
959 path_refs = get_refs_dir_path(repo, refname);
960 if (path_refs == NULL) {
961 err = got_error_from_errno("get_refs_dir_path");
964 err = open_ref(&ref, path_refs, "", refname, 0,
965 got_repo_get_object_format(repo));
967 if (err->code != GOT_ERR_NOT_REF)
969 /* Try to look up references in a given namespace. */
971 err = got_reflist_insert(&new, refs, ref,
973 if (err || new == NULL /* duplicate */)
981 /* Canonicalize the path to eliminate double-slashes if any. */
982 if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
983 err = got_error_from_errno("asprintf");
986 len = strlen(abs_namespace) + 1;
989 err = got_error_from_errno("malloc");
992 err = got_canonpath(abs_namespace, buf, len);
995 ondisk_ref_namespace = buf;
996 while (ondisk_ref_namespace[0] == '/')
997 ondisk_ref_namespace++;
998 if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
999 ondisk_ref_namespace += 5;
1000 else if (strcmp(ondisk_ref_namespace, "refs") == 0)
1001 ondisk_ref_namespace = "";
1004 /* Gather on-disk refs before parsing packed-refs. */
1006 path_refs = get_refs_dir_path(repo, "");
1007 if (path_refs == NULL) {
1008 err = got_error_from_errno("get_refs_dir_path");
1011 err = gather_on_disk_refs(refs, path_refs,
1012 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
1018 * The packed-refs file may contain redundant entries, in which
1019 * case on-disk refs take precedence.
1021 packed_refs_path = got_repo_get_path_packed_refs(repo);
1022 if (packed_refs_path == NULL) {
1023 err = got_error_from_errno("got_repo_get_path_packed_refs");
1027 f = fopen(packed_refs_path, "re");
1029 size_t linesize = 0;
1033 if (fstat(fileno(f), &sb) == -1) {
1034 err = got_error_from_errno2("fstat", packed_refs_path);
1038 linelen = getline(&line, &linesize, f);
1039 if (linelen == -1) {
1042 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1045 if (linelen > 0 && line[linelen - 1] == '\n')
1046 line[linelen - 1] = '\0';
1047 err = parse_packed_ref_line(&ref, NULL, line,
1048 sb.st_mtime, got_repo_get_object_format(repo));
1052 if (ref_namespace) {
1054 name = got_ref_get_name(ref);
1055 if (!got_path_is_child(name,
1057 strlen(ref_namespace))) {
1062 err = got_reflist_insert(&new, refs, ref,
1064 if (err || new == NULL /* duplicate */)
1072 free(packed_refs_path);
1073 free(abs_namespace);
1077 if (f && fclose(f) == EOF && err == NULL)
1078 err = got_error_from_errno("fclose");
1083 got_ref_list_free(struct got_reflist_head *refs)
1085 struct got_reflist_entry *re;
1087 while ((re = TAILQ_FIRST(refs))) {
1088 TAILQ_REMOVE(refs, re, entry);
1089 got_ref_close(re->ref);
1095 got_ref_is_symbolic(struct got_reference *ref)
1097 return (ref->flags & GOT_REF_IS_SYMBOLIC);
1100 const struct got_error *
1101 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1103 if (ref->flags & GOT_REF_IS_SYMBOLIC)
1104 return got_error(GOT_ERR_BAD_REF_TYPE);
1106 memcpy(&ref->ref.ref.id, id, sizeof(ref->ref.ref.id));
1110 const struct got_error *
1111 got_ref_change_symref(struct got_reference *ref, const char *refname)
1115 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1116 return got_error(GOT_ERR_BAD_REF_TYPE);
1118 new_name = strdup(refname);
1119 if (new_name == NULL)
1120 return got_error_from_errno("strdup");
1122 free(ref->ref.symref.ref);
1123 ref->ref.symref.ref = new_name;
1127 const struct got_error *
1128 got_ref_change_symref_to_ref(struct got_reference *symref,
1129 struct got_object_id *id)
1131 if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1132 return got_error(GOT_ERR_BAD_REF_TYPE);
1134 symref->ref.ref.name = symref->ref.symref.name;
1135 memcpy(&symref->ref.ref.id, id, sizeof(symref->ref.ref.id));
1136 symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1140 const struct got_error *
1141 got_ref_write(struct got_reference *ref, struct got_repository *repo)
1143 const struct got_error *err = NULL, *unlock_err = NULL;
1144 const char *name = got_ref_get_name(ref);
1145 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1146 struct got_lockfile *lf = NULL;
1151 path_refs = get_refs_dir_path(repo, name);
1152 if (path_refs == NULL) {
1153 err = got_error_from_errno2("get_refs_dir_path", name);
1157 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1158 err = got_error_from_errno("asprintf");
1162 err = got_opentemp_named(&tmppath, &f, path, "");
1165 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1167 err = got_path_dirname(&parent, path);
1170 err = got_path_mkdir(parent);
1174 err = got_opentemp_named(&tmppath, &f, path, "");
1179 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1180 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1181 if (n != strlen(ref->ref.symref.ref) + 6) {
1182 err = got_ferror(f, GOT_ERR_IO);
1189 err = got_object_id_str(&hex, &ref->ref.ref.id);
1193 n = fprintf(f, "%s\n", hex);
1196 err = got_ferror(f, GOT_ERR_IO);
1201 if (ref->lf == NULL) {
1202 err = got_lockfile_lock(&lf, path, -1);
1207 /* XXX: check if old content matches our expectations? */
1209 if (stat(path, &sb) != 0) {
1210 if (errno != ENOENT) {
1211 err = got_error_from_errno2("stat", path);
1214 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1217 if (fchmod(fileno(f), sb.st_mode) != 0) {
1218 err = got_error_from_errno2("fchmod", tmppath);
1222 if (rename(tmppath, path) != 0) {
1223 err = got_error_from_errno3("rename", tmppath, path);
1229 if (stat(path, &sb) == -1) {
1230 err = got_error_from_errno2("stat", path);
1233 ref->mtime = sb.st_mtime;
1235 if (ref->lf == NULL && lf)
1236 unlock_err = got_lockfile_unlock(lf, -1);
1238 if (fclose(f) == EOF && err == NULL)
1239 err = got_error_from_errno("fclose");
1244 if (unlink(tmppath) == -1 && err == NULL)
1245 err = got_error_from_errno2("unlink", tmppath);
1248 return err ? err : unlock_err;
1251 static const struct got_error *
1252 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1254 const struct got_error *err = NULL, *unlock_err = NULL;
1255 struct got_lockfile *lf = NULL;
1256 FILE *f = NULL, *tmpf = NULL;
1257 char *line = NULL, *packed_refs_path, *tmppath = NULL;
1258 size_t linesize = 0;
1259 struct got_reflist_head refs;
1260 int found_delref = 0;
1262 /* The packed-refs file does not cotain symbolic references. */
1263 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1264 return got_error(GOT_ERR_BAD_REF_DATA);
1268 packed_refs_path = got_repo_get_path_packed_refs(repo);
1269 if (packed_refs_path == NULL)
1270 return got_error_from_errno("got_repo_get_path_packed_refs");
1272 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path, "");
1276 if (delref->lf == NULL) {
1277 err = got_lockfile_lock(&lf, packed_refs_path, -1);
1282 f = fopen(packed_refs_path, "re");
1284 err = got_error_from_errno2("fopen", packed_refs_path);
1289 struct got_reference *ref;
1290 struct got_reflist_entry *new;
1292 linelen = getline(&line, &linesize, f);
1293 if (linelen == -1) {
1296 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1299 if (linelen > 0 && line[linelen - 1] == '\n')
1300 line[linelen - 1] = '\0';
1301 err = parse_packed_ref_line(&ref, NULL, line, 0,
1302 got_repo_get_object_format(repo));
1308 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1309 got_object_id_cmp(&ref->ref.ref.id,
1310 &delref->ref.ref.id) == 0) {
1316 err = got_reflist_insert(&new, &refs, ref,
1317 got_ref_cmp_by_name, NULL);
1318 if (err || new == NULL /* duplicate */)
1325 struct got_reflist_entry *re;
1329 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1330 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1331 err = got_ferror(f, GOT_ERR_IO);
1335 TAILQ_FOREACH(re, &refs, entry) {
1339 err = got_object_id_str(&hex, &re->ref->ref.ref.id);
1343 n = fprintf(tmpf, "%s ", hex);
1346 err = got_ferror(f, GOT_ERR_IO);
1350 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1351 if (n != strlen(re->ref->ref.ref.name) + 1) {
1352 err = got_ferror(f, GOT_ERR_IO);
1357 if (fflush(tmpf) != 0) {
1358 err = got_error_from_errno("fflush");
1362 if (fstat(fileno(f), &sb) != 0) {
1363 if (errno != ENOENT) {
1364 err = got_error_from_errno2("fstat",
1368 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1371 if (fchmod(fileno(tmpf), sb.st_mode) != 0) {
1372 err = got_error_from_errno2("fchmod", tmppath);
1376 if (rename(tmppath, packed_refs_path) != 0) {
1377 err = got_error_from_errno3("rename", tmppath,
1385 if (delref->lf == NULL && lf)
1386 unlock_err = got_lockfile_unlock(lf, -1);
1388 if (fclose(f) == EOF && err == NULL)
1389 err = got_error_from_errno("fclose");
1391 if (tmppath && unlink(tmppath) == -1 && err == NULL)
1392 err = got_error_from_errno2("unlink", tmppath);
1393 if (tmpf && fclose(tmpf) == EOF && err == NULL)
1394 err = got_error_from_errno("fclose");
1396 free(packed_refs_path);
1398 got_ref_list_free(&refs);
1399 return err ? err : unlock_err;
1402 static const struct got_error *
1403 delete_loose_ref(struct got_reference *ref, struct got_repository *repo)
1405 const struct got_error *err = NULL, *unlock_err = NULL;
1406 const char *name = got_ref_get_name(ref);
1407 char *path_refs = NULL, *path = NULL;
1408 struct got_lockfile *lf = NULL;
1410 path_refs = get_refs_dir_path(repo, name);
1411 if (path_refs == NULL) {
1412 err = got_error_from_errno2("get_refs_dir_path", name);
1416 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1417 err = got_error_from_errno("asprintf");
1421 if (ref->lf == NULL) {
1422 err = got_lockfile_lock(&lf, path, -1);
1427 /* XXX: check if old content matches our expectations? */
1429 if (unlink(path) == -1)
1430 err = got_error_from_errno2("unlink", path);
1432 if (ref->lf == NULL && lf)
1433 unlock_err = got_lockfile_unlock(lf, -1);
1437 return err ? err : unlock_err;
1440 const struct got_error *
1441 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1443 const struct got_error *err = NULL;
1444 struct got_reference *ref2;
1446 if (ref->flags & GOT_REF_IS_PACKED) {
1447 err = delete_packed_ref(ref, repo);
1451 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1453 if (err->code == GOT_ERR_NOT_REF)
1458 err = delete_loose_ref(ref2, repo);
1459 got_ref_close(ref2);
1462 err = delete_loose_ref(ref, repo);
1466 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1468 if (err->code == GOT_ERR_NOT_REF)
1473 err = delete_packed_ref(ref2, repo);
1474 got_ref_close(ref2);
1479 const struct got_error *
1480 got_ref_unlock(struct got_reference *ref)
1482 const struct got_error *err;
1483 err = got_lockfile_unlock(ref->lf, -1);
1488 struct got_reflist_object_id_map {
1489 struct got_object_idset *idset;
1492 struct got_reflist_object_id_map_entry {
1493 struct got_reflist_head refs;
1496 static const struct got_error *
1497 add_object_id_map_entry(struct got_object_idset *idset,
1498 struct got_object_id *id, struct got_reflist_entry *re)
1500 const struct got_error *err = NULL;
1501 struct got_reflist_object_id_map_entry *ent;
1502 struct got_reflist_entry *new;
1504 ent = got_object_idset_get(idset, id);
1506 ent = malloc(sizeof(*ent));
1508 return got_error_from_errno("malloc");
1510 TAILQ_INIT(&ent->refs);
1511 err = got_object_idset_add(idset, id, ent);
1518 err = got_reflist_entry_dup(&new, re);
1522 TAILQ_INSERT_TAIL(&ent->refs, new, entry);
1526 const struct got_error *
1527 got_reflist_object_id_map_create(struct got_reflist_object_id_map **map,
1528 struct got_reflist_head *refs, struct got_repository *repo)
1530 const struct got_error *err = NULL;
1531 struct got_object_idset *idset;
1532 struct got_object_id *id = NULL;
1533 struct got_reflist_entry *re;
1535 idset = got_object_idset_alloc();
1537 return got_error_from_errno("got_object_idset_alloc");
1539 *map = malloc(sizeof(**map));
1541 got_object_idset_free(idset);
1542 return got_error_from_errno("malloc");
1544 (*map)->idset = idset;
1546 TAILQ_FOREACH(re, refs, entry) {
1547 struct got_tag_object *tag = NULL;
1549 err = got_ref_resolve(&id, repo, re->ref);
1553 err = add_object_id_map_entry(idset, id, re);
1557 if (strstr(got_ref_get_name(re->ref), "/tags/") == NULL) {
1563 err = got_object_open_as_tag(&tag, repo, id);
1565 if (err->code != GOT_ERR_OBJ_TYPE)
1567 /* Ref points at something other than a tag. */
1575 err = add_object_id_map_entry(idset,
1576 got_object_tag_get_object_id(tag), re);
1577 got_object_tag_close(tag);
1587 got_reflist_object_id_map_free(*map);
1593 struct got_reflist_head *
1594 got_reflist_object_id_map_lookup(struct got_reflist_object_id_map *map,
1595 struct got_object_id *id)
1597 struct got_reflist_object_id_map_entry *ent;
1598 ent = got_object_idset_get(map->idset, id);
1604 static const struct got_error *
1605 free_id_map_entry(struct got_object_id *id, void *data, void *arg)
1607 struct got_reflist_object_id_map_entry *ent = data;
1609 got_ref_list_free(&ent->refs);
1615 got_reflist_object_id_map_free(struct got_reflist_object_id_map *map)
1617 got_object_idset_for_each(map->idset, free_id_map_entry, NULL);
1618 got_object_idset_free(map->idset);