2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/types.h>
18 #include <sys/queue.h>
35 #include "got_error.h"
36 #include "got_object.h"
37 #include "got_repository.h"
38 #include "got_reference.h"
39 #include "got_opentemp.h"
42 #include "got_lib_sha1.h"
43 #include "got_lib_delta.h"
44 #include "got_lib_inflate.h"
45 #include "got_lib_object.h"
46 #include "got_lib_object_idset.h"
47 #include "got_lib_lockfile.h"
50 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
53 #define GOT_REF_HEADS "heads"
54 #define GOT_REF_TAGS "tags"
55 #define GOT_REF_REMOTES "remotes"
58 * We do not resolve tags yet, and don't yet care about sorting refs either,
59 * so packed-refs files we write contain a minimal header which disables all
60 * packed-refs "traits" supported by Git.
62 #define GOT_PACKED_REFS_HEADER "# pack-refs with:"
64 /* A symbolic reference. */
70 #define GOT_REF_RECURSE_MAX 20
72 /* A non-symbolic reference (there is no better designation). */
75 u_int8_t sha1[SHA1_DIGEST_LENGTH];
78 /* A reference which points to an arbitrary object. */
79 struct got_reference {
81 #define GOT_REF_IS_SYMBOLIC 0x01
82 #define GOT_REF_IS_PACKED 0x02
86 struct got_symref symref;
89 struct got_lockfile *lf;
92 static const struct got_error *
93 alloc_ref(struct got_reference **ref, const char *name,
94 struct got_object_id *id, int flags)
96 const struct got_error *err = NULL;
98 *ref = calloc(1, sizeof(**ref));
100 return got_error_from_errno("calloc");
102 memcpy((*ref)->ref.ref.sha1, id->sha1, sizeof((*ref)->ref.ref.sha1));
103 (*ref)->flags = flags;
104 (*ref)->ref.ref.name = strdup(name);
105 if ((*ref)->ref.ref.name == NULL) {
106 err = got_error_from_errno("strdup");
113 static const struct got_error *
114 alloc_symref(struct got_reference **ref, const char *name,
115 const char *target_ref, int flags)
117 const struct got_error *err = NULL;
119 *ref = calloc(1, sizeof(**ref));
121 return got_error_from_errno("calloc");
123 (*ref)->flags = GOT_REF_IS_SYMBOLIC | flags;
124 (*ref)->ref.symref.name = strdup(name);
125 if ((*ref)->ref.symref.name == NULL) {
126 err = got_error_from_errno("strdup");
131 (*ref)->ref.symref.ref = strdup(target_ref);
132 if ((*ref)->ref.symref.ref == NULL) {
133 err = got_error_from_errno("strdup");
140 static const struct got_error *
141 parse_symref(struct got_reference **ref, const char *name, const char *line)
144 return got_error(GOT_ERR_BAD_REF_DATA);
146 return alloc_symref(ref, name, line, 0);
149 static const struct got_error *
150 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
152 struct got_object_id id;
154 if (strncmp(line, "ref: ", 5) == 0) {
156 return parse_symref(ref, name, line);
159 if (!got_parse_sha1_digest(id.sha1, line))
160 return got_error(GOT_ERR_BAD_REF_DATA);
162 return alloc_ref(ref, name, &id, 0);
165 static const struct got_error *
166 parse_ref_file(struct got_reference **ref, const char *name,
167 const char *absname, const char *abspath, int lock)
169 const struct got_error *err = NULL;
174 struct got_lockfile *lf = NULL;
177 err = got_lockfile_lock(&lf, abspath);
179 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
180 err = got_error_not_ref(name);
185 f = fopen(abspath, "rb");
187 if (errno != ENOTDIR && errno != ENOENT)
188 err = got_error_from_errno2("fopen", abspath);
190 err = got_error_not_ref(name);
192 got_lockfile_unlock(lf);
196 linelen = getline(&line, &linesize, f);
199 err = NULL; /* ignore empty files (could be locks) */
202 err = got_error(GOT_ERR_NOT_REF);
204 err = got_ferror(f, GOT_ERR_IO);
206 err = got_error_from_errno2("getline", abspath);
209 got_lockfile_unlock(lf);
212 while (linelen > 0 && line[linelen - 1] == '\n') {
213 line[linelen - 1] = '\0';
217 err = parse_ref_line(ref, absname, line);
220 got_lockfile_unlock(lf);
225 got_lockfile_unlock(lf);
230 if (fclose(f) == EOF && err == NULL) {
231 err = got_error_from_errno("fclose");
234 got_ref_unlock(*ref);
243 is_well_known_ref(const char *refname)
245 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
246 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
247 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
248 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
252 get_refs_dir_path(struct got_repository *repo, const char *refname)
254 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
255 return strdup(got_repo_get_path_git_dir(repo));
257 return got_repo_get_path_refs(repo);
261 is_valid_ref_name(const char *name)
264 const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
265 const char *forbidden_seq[] = { "//", "..", "@{" };
266 const char *lfs = GOT_LOCKFILE_SUFFIX;
267 const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
270 if (name[0] == '@' && name[1] == '\0')
275 if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
278 for (i = 0; i < nitems(forbidden); i++) {
279 if (*s == forbidden[i])
282 for (i = 0; i < nitems(forbidden_seq); i++) {
283 if (s[0] == forbidden_seq[i][0] &&
284 s[1] == forbidden_seq[i][1])
287 if (iscntrl((unsigned char)s[0]))
289 if (s[0] == '.' && s[1] == '\0')
292 const char *nextseg = s + 1;
293 if (nextseg[0] == '\0' || nextseg[0] == '.' ||
296 if (seg <= s - lfs_len &&
297 strncmp(s - lfs_len, lfs, lfs_len) == 0)
304 if (seg <= s - lfs_len &&
305 strncmp(s - lfs_len, lfs, lfs_len) == 0)
311 const struct got_error *
312 got_ref_alloc(struct got_reference **ref, const char *name,
313 struct got_object_id *id)
315 if (!is_valid_ref_name(name))
316 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
318 return alloc_ref(ref, name, id, 0);
321 const struct got_error *
322 got_ref_alloc_symref(struct got_reference **ref, const char *name,
323 struct got_reference *target_ref)
325 if (!is_valid_ref_name(name))
326 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
328 return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
331 static const struct got_error *
332 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
335 struct got_object_id id;
340 if (line[0] == '#' || line[0] == '^')
343 if (!got_parse_sha1_digest(id.sha1, line))
344 return got_error(GOT_ERR_BAD_REF_DATA);
347 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
351 name = line + SHA1_DIGEST_STRING_LENGTH;
353 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED);
356 static const struct got_error *
357 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
358 int nsubdirs, const char *refname)
360 const struct got_error *err = NULL;
365 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
370 abs_refname = (char *)refname;
372 linelen = getline(&line, &linesize, f);
376 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
379 if (linelen > 0 && line[linelen - 1] == '\n')
380 line[linelen - 1] = '\0';
381 for (i = 0; i < nsubdirs; i++) {
382 if (!ref_is_absolute &&
383 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
385 return got_error_from_errno("asprintf");
386 err = parse_packed_ref_line(ref, abs_refname, line);
387 if (!ref_is_absolute)
389 if (err || *ref != NULL)
394 } while (*ref == NULL);
400 static const struct got_error *
401 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
402 const char *name, int lock)
404 const struct got_error *err = NULL;
406 char *absname = NULL;
407 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
408 int ref_is_well_known = (subdir[0] == '\0' && is_well_known_ref(name));
412 if (ref_is_absolute || ref_is_well_known) {
413 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
414 return got_error_from_errno("asprintf");
415 absname = (char *)name;
417 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
418 subdir[0] ? "/" : "", name) == -1)
419 return got_error_from_errno("asprintf");
421 if (asprintf(&absname, "refs/%s%s%s",
422 subdir, subdir[0] ? "/" : "", name) == -1) {
423 err = got_error_from_errno("asprintf");
428 err = parse_ref_file(ref, name, absname, path, lock);
430 if (!ref_is_absolute && !ref_is_well_known)
436 const struct got_error *
437 got_ref_open(struct got_reference **ref, struct got_repository *repo,
438 const char *refname, int lock)
440 const struct got_error *err = NULL;
441 char *path_refs = NULL;
442 const char *subdirs[] = {
443 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
446 int well_known = is_well_known_ref(refname);
447 struct got_lockfile *lf = NULL;
451 path_refs = get_refs_dir_path(repo, refname);
452 if (path_refs == NULL) {
453 err = got_error_from_errno2("get_refs_dir_path", refname);
458 err = open_ref(ref, path_refs, "", refname, lock);
460 char *packed_refs_path;
463 /* Search on-disk refs before packed refs! */
464 for (i = 0; i < nitems(subdirs); i++) {
465 err = open_ref(ref, path_refs, subdirs[i], refname,
467 if ((err && err->code != GOT_ERR_NOT_REF) || *ref)
471 packed_refs_path = got_repo_get_path_packed_refs(repo);
472 if (packed_refs_path == NULL) {
473 err = got_error_from_errno(
474 "got_repo_get_path_packed_refs");
479 err = got_lockfile_lock(&lf, packed_refs_path);
483 f = fopen(packed_refs_path, "rb");
484 free(packed_refs_path);
486 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
489 if (fclose(f) == EOF) {
490 err = got_error_from_errno("fclose");
499 if (!err && *ref == NULL)
500 err = got_error_not_ref(refname);
502 got_lockfile_unlock(lf);
508 got_ref_close(struct got_reference *ref)
510 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
511 free(ref->ref.symref.name);
512 free(ref->ref.symref.ref);
514 free(ref->ref.ref.name);
518 struct got_reference *
519 got_ref_dup(struct got_reference *ref)
521 struct got_reference *ret;
523 ret = calloc(1, sizeof(*ret));
527 ret->flags = ref->flags;
528 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
529 ret->ref.symref.name = strdup(ref->ref.symref.name);
530 if (ret->ref.symref.name == NULL) {
534 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
535 if (ret->ref.symref.ref == NULL) {
536 free(ret->ref.symref.name);
541 ret->ref.ref.name = strdup(ref->ref.ref.name);
542 if (ret->ref.ref.name == NULL) {
546 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
547 sizeof(ret->ref.ref.sha1));
553 const struct got_error *
554 got_reflist_entry_dup(struct got_reflist_entry **newp,
555 struct got_reflist_entry *re)
557 const struct got_error *err = NULL;
558 struct got_reflist_entry *new;
562 new = malloc(sizeof(*new));
564 return got_error_from_errno("malloc");
566 new->ref = got_ref_dup(re->ref);
567 if (new->ref == NULL) {
568 err = got_error_from_errno("got_ref_dup");
577 static const struct got_error *
578 resolve_symbolic_ref(struct got_reference **resolved,
579 struct got_repository *repo, struct got_reference *ref)
581 struct got_reference *nextref;
582 const struct got_error *err;
584 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
588 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
589 err = resolve_symbolic_ref(resolved, repo, nextref);
591 *resolved = got_ref_dup(nextref);
593 got_ref_close(nextref);
597 static const struct got_error *
598 ref_resolve(struct got_object_id **id, struct got_repository *repo,
599 struct got_reference *ref, int recursion)
601 const struct got_error *err;
604 return got_error_msg(GOT_ERR_RECURSION,
605 "reference recursion limit reached");
607 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
608 struct got_reference *resolved = NULL;
609 err = resolve_symbolic_ref(&resolved, repo, ref);
611 err = ref_resolve(id, repo, resolved, --recursion);
613 got_ref_close(resolved);
617 *id = calloc(1, sizeof(**id));
619 return got_error_from_errno("calloc");
620 memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
624 const struct got_error *
625 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
626 struct got_reference *ref)
628 return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
632 got_ref_to_str(struct got_reference *ref)
636 if (ref->flags & GOT_REF_IS_SYMBOLIC)
637 return strdup(ref->ref.symref.ref);
639 str = malloc(SHA1_DIGEST_STRING_LENGTH);
643 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
644 SHA1_DIGEST_STRING_LENGTH) == NULL) {
653 got_ref_get_name(struct got_reference *ref)
655 if (ref->flags & GOT_REF_IS_SYMBOLIC)
656 return ref->ref.symref.name;
658 return ref->ref.ref.name;
662 got_ref_get_symref_target(struct got_reference *ref)
664 if (ref->flags & GOT_REF_IS_SYMBOLIC)
665 return ref->ref.symref.ref;
670 const struct got_error *
671 got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
672 struct got_reference* re2)
674 const char *name1 = got_ref_get_name(re1);
675 const char *name2 = got_ref_get_name(re2);
677 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
681 const struct got_error *
682 got_ref_cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
683 struct got_reference *ref2)
685 const struct got_error *err = NULL;
686 struct got_repository *repo = arg;
687 struct got_object_id *id1, *id2 = NULL;
688 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
689 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
694 err = got_ref_resolve(&id1, repo, ref1);
697 err = got_object_open_as_tag(&tag1, repo, id1);
699 if (err->code != GOT_ERR_OBJ_TYPE)
701 /* "lightweight" tag */
702 err = got_object_open_as_commit(&commit1, repo, id1);
705 time1 = got_object_commit_get_committer_time(commit1);
707 time1 = got_object_tag_get_tagger_time(tag1);
709 err = got_ref_resolve(&id2, repo, ref2);
712 err = got_object_open_as_tag(&tag2, repo, id2);
714 if (err->code != GOT_ERR_OBJ_TYPE)
716 /* "lightweight" tag */
717 err = got_object_open_as_commit(&commit2, repo, id2);
720 time2 = got_object_commit_get_committer_time(commit2);
722 time2 = got_object_tag_get_tagger_time(tag2);
724 /* Put latest tags first. */
727 else if (time1 > time2)
730 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
735 got_object_tag_close(tag1);
737 got_object_tag_close(tag2);
739 got_object_commit_close(commit1);
741 got_object_commit_close(commit2);
745 const struct got_error *
746 got_ref_cmp_by_commit_timestamp_descending(void *arg, int *cmp,
747 struct got_reference *ref1, struct got_reference *ref2)
749 const struct got_error *err;
750 struct got_repository *repo = arg;
751 struct got_object_id *id1, *id2 = NULL;
752 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
757 err = got_ref_resolve(&id1, repo, ref1);
760 err = got_ref_resolve(&id2, repo, ref2);
764 err = got_object_open_as_commit(&commit1, repo, id1);
767 err = got_object_open_as_commit(&commit2, repo, id2);
771 time1 = got_object_commit_get_committer_time(commit1);
772 time2 = got_object_commit_get_committer_time(commit2);
775 else if (time2 < time1)
781 got_object_commit_close(commit1);
783 got_object_commit_close(commit2);
787 static const struct got_error *
788 insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
789 struct got_reference *ref, struct got_repository *repo,
790 got_ref_cmp_cb cmp_cb, void *cmp_arg)
792 const struct got_error *err;
793 struct got_reflist_entry *new, *re;
798 new = malloc(sizeof(*new));
800 return got_error_from_errno("malloc");
805 * We must de-duplicate entries on insert because packed-refs may
806 * contain redundant entries. On-disk refs take precedence.
807 * This code assumes that on-disk revs are read before packed-refs.
808 * We're iterating the list anyway, so insert elements sorted by name.
810 * Many callers will provide paths in a somewhat sorted order.
811 * Iterating backwards from the tail of the list should be more
812 * efficient than traversing through the entire list each time
813 * an element is inserted.
815 re = TAILQ_LAST(refs, got_reflist_head);
817 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
825 } else if (cmp < 0) {
826 TAILQ_INSERT_AFTER(refs, re, new, entry);
829 re = TAILQ_PREV(re, got_reflist_head, entry);
832 TAILQ_INSERT_HEAD(refs, new, entry);
836 static const struct got_error *
837 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
838 const char *subdir, struct got_repository *repo,
839 got_ref_cmp_cb cmp_cb, void *cmp_arg)
841 const struct got_error *err = NULL;
845 while (subdir[0] == '/')
848 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
849 return got_error_from_errno("asprintf");
851 d = opendir(path_subdir);
857 struct got_reference *ref;
865 if (strcmp(dent->d_name, ".") == 0 ||
866 strcmp(dent->d_name, "..") == 0)
869 err = got_path_dirent_type(&type, path_subdir, dent);
875 err = open_ref(&ref, path_refs, subdir, dent->d_name,
880 struct got_reflist_entry *new;
881 err = insert_ref(&new, refs, ref, repo,
883 if (err || new == NULL /* duplicate */)
890 if (asprintf(&child, "%s%s%s", subdir,
891 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
892 err = got_error_from_errno("asprintf");
895 err = gather_on_disk_refs(refs, path_refs, child, repo,
910 const struct got_error *
911 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
912 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
914 const struct got_error *err;
915 char *packed_refs_path, *path_refs = NULL;
916 char *abs_namespace = NULL;
917 char *buf = NULL, *ondisk_ref_namespace = NULL;
920 struct got_reference *ref;
921 struct got_reflist_entry *new;
923 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
924 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
925 if (path_refs == NULL) {
926 err = got_error_from_errno("get_refs_dir_path");
929 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
932 err = insert_ref(&new, refs, ref, repo,
934 if (err || new == NULL /* duplicate */)
936 if (err && err->code != GOT_ERR_NOT_REF)
939 /* Try listing a single reference. */
940 const char *refname = ref_namespace;
941 path_refs = get_refs_dir_path(repo, refname);
942 if (path_refs == NULL) {
943 err = got_error_from_errno("get_refs_dir_path");
946 err = open_ref(&ref, path_refs, "", refname, 0);
948 if (err->code != GOT_ERR_NOT_REF)
950 /* Try to look up references in a given namespace. */
952 err = insert_ref(&new, refs, ref, repo,
954 if (err || new == NULL /* duplicate */)
962 /* Canonicalize the path to eliminate double-slashes if any. */
963 if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
964 err = got_error_from_errno("asprintf");
967 len = strlen(abs_namespace) + 1;
970 err = got_error_from_errno("malloc");
973 err = got_canonpath(abs_namespace, buf, len);
976 ondisk_ref_namespace = buf;
977 while (ondisk_ref_namespace[0] == '/')
978 ondisk_ref_namespace++;
979 if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
980 ondisk_ref_namespace += 5;
981 else if (strcmp(ondisk_ref_namespace, "refs") == 0)
982 ondisk_ref_namespace = "";
985 /* Gather on-disk refs before parsing packed-refs. */
987 path_refs = get_refs_dir_path(repo, "");
988 if (path_refs == NULL) {
989 err = got_error_from_errno("get_refs_dir_path");
992 err = gather_on_disk_refs(refs, path_refs,
993 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
999 * The packed-refs file may contain redundant entries, in which
1000 * case on-disk refs take precedence.
1002 packed_refs_path = got_repo_get_path_packed_refs(repo);
1003 if (packed_refs_path == NULL) {
1004 err = got_error_from_errno("got_repo_get_path_packed_refs");
1008 f = fopen(packed_refs_path, "r");
1009 free(packed_refs_path);
1011 size_t linesize = 0;
1014 linelen = getline(&line, &linesize, f);
1015 if (linelen == -1) {
1018 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1021 if (linelen > 0 && line[linelen - 1] == '\n')
1022 line[linelen - 1] = '\0';
1023 err = parse_packed_ref_line(&ref, NULL, line);
1027 if (ref_namespace) {
1029 name = got_ref_get_name(ref);
1030 if (!got_path_is_child(name,
1032 strlen(ref_namespace))) {
1037 err = insert_ref(&new, refs, ref, repo,
1039 if (err || new == NULL /* duplicate */)
1047 free(abs_namespace);
1051 if (f && fclose(f) == EOF && err == NULL)
1052 err = got_error_from_errno("fclose");
1057 got_ref_list_free(struct got_reflist_head *refs)
1059 struct got_reflist_entry *re;
1061 while ((re = TAILQ_FIRST(refs))) {
1062 TAILQ_REMOVE(refs, re, entry);
1069 got_ref_is_symbolic(struct got_reference *ref)
1071 return (ref->flags & GOT_REF_IS_SYMBOLIC);
1074 const struct got_error *
1075 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1077 if (ref->flags & GOT_REF_IS_SYMBOLIC)
1078 return got_error(GOT_ERR_BAD_REF_TYPE);
1080 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
1084 const struct got_error *
1085 got_ref_change_symref(struct got_reference *ref, const char *refname)
1089 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1090 return got_error(GOT_ERR_BAD_REF_TYPE);
1092 new_name = strdup(refname);
1093 if (new_name == NULL)
1094 return got_error_from_errno("strdup");
1096 free(ref->ref.symref.ref);
1097 ref->ref.symref.ref = new_name;
1101 const struct got_error *
1102 got_ref_change_symref_to_ref(struct got_reference *symref,
1103 struct got_object_id *id)
1105 if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1106 return got_error(GOT_ERR_BAD_REF_TYPE);
1108 symref->ref.ref.name = symref->ref.symref.name;
1109 memcpy(symref->ref.ref.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1110 symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1114 const struct got_error *
1115 got_ref_write(struct got_reference *ref, struct got_repository *repo)
1117 const struct got_error *err = NULL, *unlock_err = NULL;
1118 const char *name = got_ref_get_name(ref);
1119 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1120 struct got_lockfile *lf = NULL;
1125 path_refs = get_refs_dir_path(repo, name);
1126 if (path_refs == NULL) {
1127 err = got_error_from_errno2("get_refs_dir_path", name);
1131 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1132 err = got_error_from_errno("asprintf");
1136 err = got_opentemp_named(&tmppath, &f, path);
1139 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1141 err = got_path_dirname(&parent, path);
1144 err = got_path_mkdir(parent);
1148 err = got_opentemp_named(&tmppath, &f, path);
1153 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1154 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1155 if (n != strlen(ref->ref.symref.ref) + 6) {
1156 err = got_ferror(f, GOT_ERR_IO);
1160 char hex[SHA1_DIGEST_STRING_LENGTH];
1161 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
1162 sizeof(hex)) == NULL) {
1163 err = got_error(GOT_ERR_BAD_REF_DATA);
1166 n = fprintf(f, "%s\n", hex);
1167 if (n != sizeof(hex)) {
1168 err = got_ferror(f, GOT_ERR_IO);
1173 if (ref->lf == NULL) {
1174 err = got_lockfile_lock(&lf, path);
1179 /* XXX: check if old content matches our expectations? */
1181 if (stat(path, &sb) != 0) {
1182 if (errno != ENOENT) {
1183 err = got_error_from_errno2("stat", path);
1186 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1189 if (fchmod(fileno(f), sb.st_mode) != 0) {
1190 err = got_error_from_errno2("fchmod", tmppath);
1194 if (rename(tmppath, path) != 0) {
1195 err = got_error_from_errno3("rename", tmppath, path);
1201 if (ref->lf == NULL && lf)
1202 unlock_err = got_lockfile_unlock(lf);
1204 if (fclose(f) == EOF && err == NULL)
1205 err = got_error_from_errno("fclose");
1210 if (unlink(tmppath) != 0 && err == NULL)
1211 err = got_error_from_errno2("unlink", tmppath);
1214 return err ? err : unlock_err;
1217 static const struct got_error *
1218 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1220 const struct got_error *err = NULL, *unlock_err = NULL;
1221 struct got_lockfile *lf = NULL;
1222 FILE *f = NULL, *tmpf = NULL;
1223 char *line = NULL, *packed_refs_path, *tmppath = NULL;
1224 size_t linesize = 0;
1225 struct got_reflist_head refs;
1226 int found_delref = 0;
1228 /* The packed-refs file does not cotain symbolic references. */
1229 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1230 return got_error(GOT_ERR_BAD_REF_DATA);
1234 packed_refs_path = got_repo_get_path_packed_refs(repo);
1235 if (packed_refs_path == NULL)
1236 return got_error_from_errno("got_repo_get_path_packed_refs");
1238 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
1242 if (delref->lf == NULL) {
1243 err = got_lockfile_lock(&lf, packed_refs_path);
1248 f = fopen(packed_refs_path, "r");
1250 err = got_error_from_errno2("fopen", packed_refs_path);
1255 struct got_reference *ref;
1256 struct got_reflist_entry *new;
1258 linelen = getline(&line, &linesize, f);
1259 if (linelen == -1) {
1262 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1265 if (linelen > 0 && line[linelen - 1] == '\n')
1266 line[linelen - 1] = '\0';
1267 err = parse_packed_ref_line(&ref, NULL, line);
1273 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1274 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1275 sizeof(delref->ref.ref.sha1)) == 0) {
1281 err = insert_ref(&new, &refs, ref, repo,
1282 got_ref_cmp_by_name, NULL);
1283 if (err || new == NULL /* duplicate */)
1290 struct got_reflist_entry *re;
1294 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1295 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1296 err = got_ferror(f, GOT_ERR_IO);
1300 TAILQ_FOREACH(re, &refs, entry) {
1301 uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
1303 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1304 sizeof(hex)) == NULL) {
1305 err = got_error(GOT_ERR_BAD_REF_DATA);
1308 n = fprintf(tmpf, "%s ", hex);
1309 if (n != sizeof(hex)) {
1310 err = got_ferror(f, GOT_ERR_IO);
1313 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1314 if (n != strlen(re->ref->ref.ref.name) + 1) {
1315 err = got_ferror(f, GOT_ERR_IO);
1320 if (fflush(tmpf) != 0) {
1321 err = got_error_from_errno("fflush");
1325 if (fstat(fileno(f), &sb) != 0) {
1326 if (errno != ENOENT) {
1327 err = got_error_from_errno2("fstat",
1331 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1334 if (fchmod(fileno(tmpf), sb.st_mode) != 0) {
1335 err = got_error_from_errno2("fchmod", tmppath);
1339 if (rename(tmppath, packed_refs_path) != 0) {
1340 err = got_error_from_errno3("rename", tmppath,
1346 if (delref->lf == NULL && lf)
1347 unlock_err = got_lockfile_unlock(lf);
1349 if (fclose(f) == EOF && err == NULL)
1350 err = got_error_from_errno("fclose");
1354 if (fclose(tmpf) == EOF && err == NULL)
1355 err = got_error_from_errno("fclose");
1358 free(packed_refs_path);
1360 got_ref_list_free(&refs);
1361 return err ? err : unlock_err;
1364 static const struct got_error *
1365 delete_loose_ref(struct got_reference *ref, struct got_repository *repo)
1367 const struct got_error *err = NULL, *unlock_err = NULL;
1368 const char *name = got_ref_get_name(ref);
1369 char *path_refs = NULL, *path = NULL;
1370 struct got_lockfile *lf = NULL;
1372 path_refs = get_refs_dir_path(repo, name);
1373 if (path_refs == NULL) {
1374 err = got_error_from_errno2("get_refs_dir_path", name);
1378 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1379 err = got_error_from_errno("asprintf");
1383 if (ref->lf == NULL) {
1384 err = got_lockfile_lock(&lf, path);
1389 /* XXX: check if old content matches our expectations? */
1391 if (unlink(path) != 0)
1392 err = got_error_from_errno2("unlink", path);
1394 if (ref->lf == NULL && lf)
1395 unlock_err = got_lockfile_unlock(lf);
1399 return err ? err : unlock_err;
1402 const struct got_error *
1403 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1405 const struct got_error *err = NULL;
1406 struct got_reference *ref2;
1408 if (ref->flags & GOT_REF_IS_PACKED) {
1409 err = delete_packed_ref(ref, repo);
1413 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1415 if (err->code == GOT_ERR_NOT_REF)
1420 err = delete_loose_ref(ref2, repo);
1421 got_ref_close(ref2);
1424 err = delete_loose_ref(ref, repo);
1428 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1430 if (err->code == GOT_ERR_NOT_REF)
1435 err = delete_packed_ref(ref2, repo);
1436 got_ref_close(ref2);
1441 const struct got_error *
1442 got_ref_unlock(struct got_reference *ref)
1444 const struct got_error *err;
1445 err = got_lockfile_unlock(ref->lf);
1450 struct got_reflist_object_id_map {
1451 struct got_object_idset *idset;
1454 struct got_reflist_object_id_map_entry {
1455 struct got_reflist_head refs;
1458 static const struct got_error *
1459 add_object_id_map_entry(struct got_object_idset *idset,
1460 struct got_object_id *id, struct got_reflist_entry *re)
1462 const struct got_error *err = NULL;
1463 struct got_reflist_object_id_map_entry *ent;
1464 struct got_reflist_entry *new;
1466 ent = got_object_idset_get(idset, id);
1468 ent = malloc(sizeof(*ent));
1470 return got_error_from_errno("malloc");
1472 TAILQ_INIT(&ent->refs);
1473 err = got_object_idset_add(idset, id, ent);
1478 err = got_reflist_entry_dup(&new, re);
1482 TAILQ_INSERT_TAIL(&ent->refs, new, entry);
1486 const struct got_error *
1487 got_reflist_object_id_map_create(struct got_reflist_object_id_map **map,
1488 struct got_reflist_head *refs, struct got_repository *repo)
1490 const struct got_error *err = NULL;
1491 struct got_object_idset *idset;
1492 struct got_object_id *id = NULL;
1493 struct got_reflist_entry *re;
1495 idset = got_object_idset_alloc();
1497 return got_error_from_errno("got_object_idset_alloc");
1499 *map = malloc(sizeof(**map));
1501 got_object_idset_free(idset);
1502 return got_error_from_errno("malloc");
1504 (*map)->idset = idset;
1506 TAILQ_FOREACH(re, refs, entry) {
1507 struct got_tag_object *tag = NULL;
1509 err = got_ref_resolve(&id, repo, re->ref);
1513 err = add_object_id_map_entry(idset, id, re);
1517 if (strstr(got_ref_get_name(re->ref), "/tags/") == NULL) {
1523 err = got_object_open_as_tag(&tag, repo, id);
1525 if (err->code != GOT_ERR_OBJ_TYPE)
1527 /* Ref points at something other than a tag. */
1535 err = add_object_id_map_entry(idset,
1536 got_object_tag_get_object_id(tag), re);
1537 got_object_tag_close(tag);
1547 got_reflist_object_id_map_free(*map);
1553 struct got_reflist_head *
1554 got_reflist_object_id_map_lookup(struct got_reflist_object_id_map *map,
1555 struct got_object_id *id)
1557 struct got_reflist_object_id_map_entry *ent;
1558 ent = got_object_idset_get(map->idset, id);
1564 static const struct got_error *
1565 free_id_map_entry(struct got_object_id *id, void *data, void *arg)
1567 struct got_reflist_object_id_map_entry *ent = data;
1569 got_ref_list_free(&ent->refs);
1575 got_reflist_object_id_map_free(struct got_reflist_object_id_map *map)
1577 got_object_idset_for_each(map->idset, free_id_map_entry, NULL);
1578 got_object_idset_free(map->idset);