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;
860 struct got_reference *ref;
861 struct got_reflist_entry *new;
863 while (subdir[0] == '/')
866 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
867 return got_error_from_errno("asprintf");
869 d = opendir(path_subdir);
873 if (errno != ENOTDIR)
876 /* This could be a regular on-disk reference file. */
878 err = got_path_dirname(&path_subdir, subdir);
881 err = got_path_basename(&refname, subdir);
886 err = open_ref(&ref, path_refs, path_subdir, refname,
887 0, got_repo_get_object_format(repo));
891 if (err->code == GOT_ERR_NOT_REF)
895 err = got_reflist_insert(&new, refs, ref,
897 if (err || new == NULL /* duplicate */)
911 if (strcmp(dent->d_name, ".") == 0 ||
912 strcmp(dent->d_name, "..") == 0)
915 err = got_path_dirent_type(&type, path_subdir, dent);
921 err = open_ref(&ref, path_refs, subdir, dent->d_name,
922 0, got_repo_get_object_format(repo));
923 if (err && err->code == GOT_ERR_BAD_REF_NAME)
928 err = got_reflist_insert(&new, refs, ref,
930 if (err || new == NULL /* duplicate */)
937 if (asprintf(&child, "%s%s%s", subdir,
938 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
939 err = got_error_from_errno("asprintf");
942 err = gather_on_disk_refs(refs, path_refs, child, repo,
958 match_packed_ref(struct got_reference *ref, const char *ref_namespace)
960 const char *name = got_ref_get_name(ref);
961 int namespace_is_absolute = (strncmp(ref_namespace, "refs/", 5) == 0);
963 if (namespace_is_absolute) {
964 return (strcmp(name, ref_namespace) == 0 ||
965 got_path_is_child(name, ref_namespace,
966 strlen(ref_namespace)));
969 /* Match all "subdirectories" as we do with on-disk refs. */
970 while (*name != '\0') {
973 if (strcmp(name, ref_namespace) == 0 ||
974 got_path_is_child(name, ref_namespace,
975 strlen(ref_namespace)))
977 while (*name != '\0' && *name != '/')
984 const struct got_error *
985 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
986 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
988 const struct got_error *err;
989 char *packed_refs_path = NULL, *path_refs = NULL;
990 char *abs_namespace = NULL, *buf = NULL;
991 const char *ondisk_ref_namespace = NULL;
994 struct got_reference *ref;
995 struct got_reflist_entry *new;
997 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
998 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
999 if (path_refs == NULL) {
1000 err = got_error_from_errno("get_refs_dir_path");
1003 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0,
1004 got_repo_get_object_format(repo));
1007 err = got_reflist_insert(&new, refs, ref, cmp_cb, cmp_arg);
1008 if (err || new == NULL /* duplicate */)
1010 if (err && err->code != GOT_ERR_NOT_REF)
1013 /* Try listing a single reference. */
1014 err = got_ref_open(&ref, repo, ref_namespace, 0);
1016 if (err->code != GOT_ERR_NOT_REF)
1018 /* Try to look up references in a given namespace. */
1020 err = got_reflist_insert(&new, refs, ref,
1022 if (err || new == NULL /* duplicate */)
1028 if (ref_namespace) {
1030 /* Canonicalize the path to eliminate double-slashes if any. */
1031 if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
1032 err = got_error_from_errno("asprintf");
1035 len = strlen(abs_namespace) + 1;
1038 err = got_error_from_errno("malloc");
1041 err = got_canonpath(abs_namespace, buf, len);
1044 ondisk_ref_namespace = buf;
1045 while (ondisk_ref_namespace[0] == '/')
1046 ondisk_ref_namespace++;
1047 if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
1048 ondisk_ref_namespace += 5;
1049 else if (strcmp(ondisk_ref_namespace, "refs") == 0)
1050 ondisk_ref_namespace = "";
1053 /* Gather on-disk refs before parsing packed-refs. */
1055 path_refs = get_refs_dir_path(repo, "");
1056 if (path_refs == NULL) {
1057 err = got_error_from_errno("get_refs_dir_path");
1060 err = gather_on_disk_refs(refs, path_refs,
1061 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
1067 * The packed-refs file may contain redundant entries, in which
1068 * case on-disk refs take precedence.
1070 packed_refs_path = got_repo_get_path_packed_refs(repo);
1071 if (packed_refs_path == NULL) {
1072 err = got_error_from_errno("got_repo_get_path_packed_refs");
1076 f = fopen(packed_refs_path, "re");
1078 size_t linesize = 0;
1082 if (fstat(fileno(f), &sb) == -1) {
1083 err = got_error_from_errno2("fstat", packed_refs_path);
1087 linelen = getline(&line, &linesize, f);
1088 if (linelen == -1) {
1091 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1094 if (linelen > 0 && line[linelen - 1] == '\n')
1095 line[linelen - 1] = '\0';
1096 err = parse_packed_ref_line(&ref, NULL, line,
1097 sb.st_mtime, got_repo_get_object_format(repo));
1101 if (ref_namespace &&
1102 !match_packed_ref(ref, ref_namespace)) {
1106 err = got_reflist_insert(&new, refs, ref,
1108 if (err || new == NULL /* duplicate */)
1116 free(packed_refs_path);
1117 free(abs_namespace);
1121 if (f && fclose(f) == EOF && err == NULL)
1122 err = got_error_from_errno("fclose");
1127 got_ref_list_free(struct got_reflist_head *refs)
1129 struct got_reflist_entry *re;
1131 while ((re = TAILQ_FIRST(refs))) {
1132 TAILQ_REMOVE(refs, re, entry);
1133 got_ref_close(re->ref);
1139 got_ref_is_symbolic(struct got_reference *ref)
1141 return (ref->flags & GOT_REF_IS_SYMBOLIC);
1144 const struct got_error *
1145 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1147 if (ref->flags & GOT_REF_IS_SYMBOLIC)
1148 return got_error(GOT_ERR_BAD_REF_TYPE);
1150 memcpy(&ref->ref.ref.id, id, sizeof(ref->ref.ref.id));
1154 const struct got_error *
1155 got_ref_change_symref(struct got_reference *ref, const char *refname)
1159 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1160 return got_error(GOT_ERR_BAD_REF_TYPE);
1162 new_name = strdup(refname);
1163 if (new_name == NULL)
1164 return got_error_from_errno("strdup");
1166 free(ref->ref.symref.ref);
1167 ref->ref.symref.ref = new_name;
1171 const struct got_error *
1172 got_ref_change_symref_to_ref(struct got_reference *symref,
1173 struct got_object_id *id)
1175 if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1176 return got_error(GOT_ERR_BAD_REF_TYPE);
1178 symref->ref.ref.name = symref->ref.symref.name;
1179 memcpy(&symref->ref.ref.id, id, sizeof(symref->ref.ref.id));
1180 symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1184 const struct got_error *
1185 got_ref_write(struct got_reference *ref, struct got_repository *repo)
1187 const struct got_error *err = NULL, *unlock_err = NULL;
1188 const char *name = got_ref_get_name(ref);
1189 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1190 struct got_lockfile *lf = NULL;
1195 path_refs = get_refs_dir_path(repo, name);
1196 if (path_refs == NULL) {
1197 err = got_error_from_errno2("get_refs_dir_path", name);
1201 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1202 err = got_error_from_errno("asprintf");
1206 err = got_opentemp_named(&tmppath, &f, path, "");
1209 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1211 err = got_path_dirname(&parent, path);
1214 err = got_path_mkdir(parent);
1218 err = got_opentemp_named(&tmppath, &f, path, "");
1223 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1224 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1225 if (n != strlen(ref->ref.symref.ref) + 6) {
1226 err = got_ferror(f, GOT_ERR_IO);
1233 err = got_object_id_str(&hex, &ref->ref.ref.id);
1237 n = fprintf(f, "%s\n", hex);
1240 err = got_ferror(f, GOT_ERR_IO);
1245 if (ref->lf == NULL) {
1246 err = got_lockfile_lock(&lf, path, -1);
1251 /* XXX: check if old content matches our expectations? */
1253 if (stat(path, &sb) != 0) {
1254 if (errno != ENOENT) {
1255 err = got_error_from_errno2("stat", path);
1258 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1261 if (fchmod(fileno(f), sb.st_mode) != 0) {
1262 err = got_error_from_errno2("fchmod", tmppath);
1266 if (rename(tmppath, path) != 0) {
1267 err = got_error_from_errno3("rename", tmppath, path);
1273 if (stat(path, &sb) == -1) {
1274 err = got_error_from_errno2("stat", path);
1277 ref->mtime = sb.st_mtime;
1279 if (ref->lf == NULL && lf)
1280 unlock_err = got_lockfile_unlock(lf, -1);
1282 if (fclose(f) == EOF && err == NULL)
1283 err = got_error_from_errno("fclose");
1288 if (unlink(tmppath) == -1 && err == NULL)
1289 err = got_error_from_errno2("unlink", tmppath);
1292 return err ? err : unlock_err;
1295 static const struct got_error *
1296 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1298 const struct got_error *err = NULL, *unlock_err = NULL;
1299 struct got_lockfile *lf = NULL;
1300 FILE *f = NULL, *tmpf = NULL;
1301 char *line = NULL, *packed_refs_path, *tmppath = NULL;
1302 size_t linesize = 0;
1303 struct got_reflist_head refs;
1304 int found_delref = 0;
1306 /* The packed-refs file does not cotain symbolic references. */
1307 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1308 return got_error(GOT_ERR_BAD_REF_DATA);
1312 packed_refs_path = got_repo_get_path_packed_refs(repo);
1313 if (packed_refs_path == NULL)
1314 return got_error_from_errno("got_repo_get_path_packed_refs");
1316 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path, "");
1320 if (delref->lf == NULL) {
1321 err = got_lockfile_lock(&lf, packed_refs_path, -1);
1326 f = fopen(packed_refs_path, "re");
1328 err = got_error_from_errno2("fopen", packed_refs_path);
1333 struct got_reference *ref;
1334 struct got_reflist_entry *new;
1336 linelen = getline(&line, &linesize, f);
1337 if (linelen == -1) {
1340 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1343 if (linelen > 0 && line[linelen - 1] == '\n')
1344 line[linelen - 1] = '\0';
1345 err = parse_packed_ref_line(&ref, NULL, line, 0,
1346 got_repo_get_object_format(repo));
1352 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1353 got_object_id_cmp(&ref->ref.ref.id,
1354 &delref->ref.ref.id) == 0) {
1360 err = got_reflist_insert(&new, &refs, ref,
1361 got_ref_cmp_by_name, NULL);
1362 if (err || new == NULL /* duplicate */)
1369 struct got_reflist_entry *re;
1373 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1374 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1375 err = got_ferror(f, GOT_ERR_IO);
1379 TAILQ_FOREACH(re, &refs, entry) {
1383 err = got_object_id_str(&hex, &re->ref->ref.ref.id);
1387 n = fprintf(tmpf, "%s ", hex);
1390 err = got_ferror(f, GOT_ERR_IO);
1394 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1395 if (n != strlen(re->ref->ref.ref.name) + 1) {
1396 err = got_ferror(f, GOT_ERR_IO);
1401 if (fflush(tmpf) != 0) {
1402 err = got_error_from_errno("fflush");
1406 if (fstat(fileno(f), &sb) != 0) {
1407 if (errno != ENOENT) {
1408 err = got_error_from_errno2("fstat",
1412 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1415 if (fchmod(fileno(tmpf), sb.st_mode) != 0) {
1416 err = got_error_from_errno2("fchmod", tmppath);
1420 if (rename(tmppath, packed_refs_path) != 0) {
1421 err = got_error_from_errno3("rename", tmppath,
1429 if (delref->lf == NULL && lf)
1430 unlock_err = got_lockfile_unlock(lf, -1);
1432 if (fclose(f) == EOF && err == NULL)
1433 err = got_error_from_errno("fclose");
1435 if (tmppath && unlink(tmppath) == -1 && err == NULL)
1436 err = got_error_from_errno2("unlink", tmppath);
1437 if (tmpf && fclose(tmpf) == EOF && err == NULL)
1438 err = got_error_from_errno("fclose");
1440 free(packed_refs_path);
1442 got_ref_list_free(&refs);
1443 return err ? err : unlock_err;
1446 static const struct got_error *
1447 delete_loose_ref(struct got_reference *ref, struct got_repository *repo)
1449 const struct got_error *err = NULL, *unlock_err = NULL;
1450 const char *name = got_ref_get_name(ref);
1451 char *path_refs = NULL, *path = NULL;
1452 struct got_lockfile *lf = NULL;
1454 path_refs = get_refs_dir_path(repo, name);
1455 if (path_refs == NULL) {
1456 err = got_error_from_errno2("get_refs_dir_path", name);
1460 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1461 err = got_error_from_errno("asprintf");
1465 if (ref->lf == NULL) {
1466 err = got_lockfile_lock(&lf, path, -1);
1471 /* XXX: check if old content matches our expectations? */
1473 if (unlink(path) == -1)
1474 err = got_error_from_errno2("unlink", path);
1476 if (ref->lf == NULL && lf)
1477 unlock_err = got_lockfile_unlock(lf, -1);
1481 return err ? err : unlock_err;
1484 const struct got_error *
1485 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1487 const struct got_error *err = NULL;
1488 struct got_reference *ref2;
1490 if (ref->flags & GOT_REF_IS_PACKED) {
1491 err = delete_packed_ref(ref, repo);
1495 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1497 if (err->code == GOT_ERR_NOT_REF)
1502 err = delete_loose_ref(ref2, repo);
1503 got_ref_close(ref2);
1506 err = delete_loose_ref(ref, repo);
1510 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1512 if (err->code == GOT_ERR_NOT_REF)
1517 err = delete_packed_ref(ref2, repo);
1518 got_ref_close(ref2);
1523 const struct got_error *
1524 got_ref_unlock(struct got_reference *ref)
1526 const struct got_error *err;
1527 err = got_lockfile_unlock(ref->lf, -1);
1532 struct got_reflist_object_id_map {
1533 struct got_object_idset *idset;
1536 struct got_reflist_object_id_map_entry {
1537 struct got_reflist_head refs;
1540 static const struct got_error *
1541 add_object_id_map_entry(struct got_object_idset *idset,
1542 struct got_object_id *id, struct got_reflist_entry *re)
1544 const struct got_error *err = NULL;
1545 struct got_reflist_object_id_map_entry *ent;
1546 struct got_reflist_entry *new;
1548 ent = got_object_idset_get(idset, id);
1550 ent = malloc(sizeof(*ent));
1552 return got_error_from_errno("malloc");
1554 TAILQ_INIT(&ent->refs);
1555 err = got_object_idset_add(idset, id, ent);
1562 err = got_reflist_entry_dup(&new, re);
1566 TAILQ_INSERT_TAIL(&ent->refs, new, entry);
1570 const struct got_error *
1571 got_reflist_object_id_map_create(struct got_reflist_object_id_map **map,
1572 struct got_reflist_head *refs, struct got_repository *repo)
1574 const struct got_error *err = NULL;
1575 struct got_object_idset *idset;
1576 struct got_object_id *id = NULL;
1577 struct got_reflist_entry *re;
1579 idset = got_object_idset_alloc();
1581 return got_error_from_errno("got_object_idset_alloc");
1583 *map = malloc(sizeof(**map));
1585 got_object_idset_free(idset);
1586 return got_error_from_errno("malloc");
1588 (*map)->idset = idset;
1590 TAILQ_FOREACH(re, refs, entry) {
1591 struct got_tag_object *tag = NULL;
1593 err = got_ref_resolve(&id, repo, re->ref);
1597 err = add_object_id_map_entry(idset, id, re);
1601 if (strstr(got_ref_get_name(re->ref), "/tags/") == NULL) {
1607 err = got_object_open_as_tag(&tag, repo, id);
1609 if (err->code != GOT_ERR_OBJ_TYPE)
1611 /* Ref points at something other than a tag. */
1619 err = add_object_id_map_entry(idset,
1620 got_object_tag_get_object_id(tag), re);
1621 got_object_tag_close(tag);
1631 got_reflist_object_id_map_free(*map);
1637 struct got_reflist_head *
1638 got_reflist_object_id_map_lookup(struct got_reflist_object_id_map *map,
1639 struct got_object_id *id)
1641 struct got_reflist_object_id_map_entry *ent;
1642 ent = got_object_idset_get(map->idset, id);
1648 static const struct got_error *
1649 free_id_map_entry(struct got_object_id *id, void *data, void *arg)
1651 struct got_reflist_object_id_map_entry *ent = data;
1653 got_ref_list_free(&ent->refs);
1659 got_reflist_object_id_map_free(struct got_reflist_object_id_map *map)
1661 got_object_idset_for_each(map->idset, free_id_map_entry, NULL);
1662 got_object_idset_free(map->idset);