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 (!is_valid_ref_name(name))
413 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
415 if (ref_is_absolute || ref_is_well_known) {
416 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
417 return got_error_from_errno("asprintf");
418 absname = (char *)name;
420 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
421 subdir[0] ? "/" : "", name) == -1)
422 return got_error_from_errno("asprintf");
424 if (asprintf(&absname, "refs/%s%s%s",
425 subdir, subdir[0] ? "/" : "", name) == -1) {
426 err = got_error_from_errno("asprintf");
431 err = parse_ref_file(ref, name, absname, path, lock);
433 if (!ref_is_absolute && !ref_is_well_known)
439 const struct got_error *
440 got_ref_open(struct got_reference **ref, struct got_repository *repo,
441 const char *refname, int lock)
443 const struct got_error *err = NULL;
444 char *path_refs = NULL;
445 const char *subdirs[] = {
446 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
449 int well_known = is_well_known_ref(refname);
450 struct got_lockfile *lf = NULL;
454 path_refs = get_refs_dir_path(repo, refname);
455 if (path_refs == NULL) {
456 err = got_error_from_errno2("get_refs_dir_path", refname);
461 err = open_ref(ref, path_refs, "", refname, lock);
463 char *packed_refs_path;
466 /* Search on-disk refs before packed refs! */
467 for (i = 0; i < nitems(subdirs); i++) {
468 err = open_ref(ref, path_refs, subdirs[i], refname,
470 if ((err && err->code != GOT_ERR_NOT_REF) || *ref)
474 packed_refs_path = got_repo_get_path_packed_refs(repo);
475 if (packed_refs_path == NULL) {
476 err = got_error_from_errno(
477 "got_repo_get_path_packed_refs");
482 err = got_lockfile_lock(&lf, packed_refs_path);
486 f = fopen(packed_refs_path, "rb");
487 free(packed_refs_path);
489 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
492 if (fclose(f) == EOF) {
493 err = got_error_from_errno("fclose");
502 if (!err && *ref == NULL)
503 err = got_error_not_ref(refname);
505 got_lockfile_unlock(lf);
511 got_ref_close(struct got_reference *ref)
513 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
514 free(ref->ref.symref.name);
515 free(ref->ref.symref.ref);
517 free(ref->ref.ref.name);
521 struct got_reference *
522 got_ref_dup(struct got_reference *ref)
524 struct got_reference *ret;
526 ret = calloc(1, sizeof(*ret));
530 ret->flags = ref->flags;
531 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
532 ret->ref.symref.name = strdup(ref->ref.symref.name);
533 if (ret->ref.symref.name == NULL) {
537 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
538 if (ret->ref.symref.ref == NULL) {
539 free(ret->ref.symref.name);
544 ret->ref.ref.name = strdup(ref->ref.ref.name);
545 if (ret->ref.ref.name == NULL) {
549 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
550 sizeof(ret->ref.ref.sha1));
556 const struct got_error *
557 got_reflist_entry_dup(struct got_reflist_entry **newp,
558 struct got_reflist_entry *re)
560 const struct got_error *err = NULL;
561 struct got_reflist_entry *new;
565 new = malloc(sizeof(*new));
567 return got_error_from_errno("malloc");
569 new->ref = got_ref_dup(re->ref);
570 if (new->ref == NULL) {
571 err = got_error_from_errno("got_ref_dup");
580 static const struct got_error *
581 resolve_symbolic_ref(struct got_reference **resolved,
582 struct got_repository *repo, struct got_reference *ref)
584 struct got_reference *nextref;
585 const struct got_error *err;
587 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
591 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
592 err = resolve_symbolic_ref(resolved, repo, nextref);
594 *resolved = got_ref_dup(nextref);
596 got_ref_close(nextref);
600 static const struct got_error *
601 ref_resolve(struct got_object_id **id, struct got_repository *repo,
602 struct got_reference *ref, int recursion)
604 const struct got_error *err;
607 return got_error_msg(GOT_ERR_RECURSION,
608 "reference recursion limit reached");
610 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
611 struct got_reference *resolved = NULL;
612 err = resolve_symbolic_ref(&resolved, repo, ref);
614 err = ref_resolve(id, repo, resolved, --recursion);
616 got_ref_close(resolved);
620 *id = calloc(1, sizeof(**id));
622 return got_error_from_errno("calloc");
623 memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
627 const struct got_error *
628 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
629 struct got_reference *ref)
631 return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
635 got_ref_to_str(struct got_reference *ref)
639 if (ref->flags & GOT_REF_IS_SYMBOLIC)
640 return strdup(ref->ref.symref.ref);
642 str = malloc(SHA1_DIGEST_STRING_LENGTH);
646 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
647 SHA1_DIGEST_STRING_LENGTH) == NULL) {
656 got_ref_get_name(struct got_reference *ref)
658 if (ref->flags & GOT_REF_IS_SYMBOLIC)
659 return ref->ref.symref.name;
661 return ref->ref.ref.name;
665 got_ref_get_symref_target(struct got_reference *ref)
667 if (ref->flags & GOT_REF_IS_SYMBOLIC)
668 return ref->ref.symref.ref;
673 const struct got_error *
674 got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
675 struct got_reference* re2)
677 const char *name1 = got_ref_get_name(re1);
678 const char *name2 = got_ref_get_name(re2);
680 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
684 const struct got_error *
685 got_ref_cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
686 struct got_reference *ref2)
688 const struct got_error *err = NULL;
689 struct got_repository *repo = arg;
690 struct got_object_id *id1, *id2 = NULL;
691 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
692 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
697 err = got_ref_resolve(&id1, repo, ref1);
700 err = got_object_open_as_tag(&tag1, repo, id1);
702 if (err->code != GOT_ERR_OBJ_TYPE)
704 /* "lightweight" tag */
705 err = got_object_open_as_commit(&commit1, repo, id1);
708 time1 = got_object_commit_get_committer_time(commit1);
710 time1 = got_object_tag_get_tagger_time(tag1);
712 err = got_ref_resolve(&id2, repo, ref2);
715 err = got_object_open_as_tag(&tag2, repo, id2);
717 if (err->code != GOT_ERR_OBJ_TYPE)
719 /* "lightweight" tag */
720 err = got_object_open_as_commit(&commit2, repo, id2);
723 time2 = got_object_commit_get_committer_time(commit2);
725 time2 = got_object_tag_get_tagger_time(tag2);
727 /* Put latest tags first. */
730 else if (time1 > time2)
733 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
738 got_object_tag_close(tag1);
740 got_object_tag_close(tag2);
742 got_object_commit_close(commit1);
744 got_object_commit_close(commit2);
748 const struct got_error *
749 got_ref_cmp_by_commit_timestamp_descending(void *arg, int *cmp,
750 struct got_reference *ref1, struct got_reference *ref2)
752 const struct got_error *err;
753 struct got_repository *repo = arg;
754 struct got_object_id *id1, *id2 = NULL;
755 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
760 err = got_ref_resolve(&id1, repo, ref1);
763 err = got_ref_resolve(&id2, repo, ref2);
767 err = got_object_open_as_commit(&commit1, repo, id1);
770 err = got_object_open_as_commit(&commit2, repo, id2);
774 time1 = got_object_commit_get_committer_time(commit1);
775 time2 = got_object_commit_get_committer_time(commit2);
778 else if (time2 < time1)
784 got_object_commit_close(commit1);
786 got_object_commit_close(commit2);
790 const struct got_error *
791 got_reflist_insert(struct got_reflist_entry **newp, struct got_reflist_head *refs,
792 struct got_reference *ref, struct got_repository *repo,
793 got_ref_cmp_cb cmp_cb, void *cmp_arg)
795 const struct got_error *err;
796 struct got_reflist_entry *new, *re;
801 new = malloc(sizeof(*new));
803 return got_error_from_errno("malloc");
808 * We must de-duplicate entries on insert because packed-refs may
809 * contain redundant entries. On-disk refs take precedence.
810 * This code assumes that on-disk revs are read before packed-refs.
811 * We're iterating the list anyway, so insert elements sorted by name.
813 * Many callers will provide paths in a somewhat sorted order.
814 * Iterating backwards from the tail of the list should be more
815 * efficient than traversing through the entire list each time
816 * an element is inserted.
818 re = TAILQ_LAST(refs, got_reflist_head);
820 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
828 } else if (cmp < 0) {
829 TAILQ_INSERT_AFTER(refs, re, new, entry);
832 re = TAILQ_PREV(re, got_reflist_head, entry);
835 TAILQ_INSERT_HEAD(refs, new, entry);
839 static const struct got_error *
840 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
841 const char *subdir, struct got_repository *repo,
842 got_ref_cmp_cb cmp_cb, void *cmp_arg)
844 const struct got_error *err = NULL;
848 while (subdir[0] == '/')
851 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
852 return got_error_from_errno("asprintf");
854 d = opendir(path_subdir);
860 struct got_reference *ref;
868 if (strcmp(dent->d_name, ".") == 0 ||
869 strcmp(dent->d_name, "..") == 0)
872 err = got_path_dirent_type(&type, path_subdir, dent);
878 err = open_ref(&ref, path_refs, subdir, dent->d_name,
883 struct got_reflist_entry *new;
884 err = got_reflist_insert(&new, refs, ref, repo,
886 if (err || new == NULL /* duplicate */)
893 if (asprintf(&child, "%s%s%s", subdir,
894 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
895 err = got_error_from_errno("asprintf");
898 err = gather_on_disk_refs(refs, path_refs, child, repo,
913 const struct got_error *
914 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
915 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
917 const struct got_error *err;
918 char *packed_refs_path, *path_refs = NULL;
919 char *abs_namespace = NULL;
920 char *buf = NULL, *ondisk_ref_namespace = NULL;
923 struct got_reference *ref;
924 struct got_reflist_entry *new;
926 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
927 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
928 if (path_refs == NULL) {
929 err = got_error_from_errno("get_refs_dir_path");
932 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
935 err = got_reflist_insert(&new, refs, ref, repo,
937 if (err || new == NULL /* duplicate */)
939 if (err && err->code != GOT_ERR_NOT_REF)
942 /* Try listing a single reference. */
943 const char *refname = ref_namespace;
944 path_refs = get_refs_dir_path(repo, refname);
945 if (path_refs == NULL) {
946 err = got_error_from_errno("get_refs_dir_path");
949 err = open_ref(&ref, path_refs, "", refname, 0);
951 if (err->code != GOT_ERR_NOT_REF)
953 /* Try to look up references in a given namespace. */
955 err = got_reflist_insert(&new, refs, ref, repo,
957 if (err || new == NULL /* duplicate */)
965 /* Canonicalize the path to eliminate double-slashes if any. */
966 if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
967 err = got_error_from_errno("asprintf");
970 len = strlen(abs_namespace) + 1;
973 err = got_error_from_errno("malloc");
976 err = got_canonpath(abs_namespace, buf, len);
979 ondisk_ref_namespace = buf;
980 while (ondisk_ref_namespace[0] == '/')
981 ondisk_ref_namespace++;
982 if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
983 ondisk_ref_namespace += 5;
984 else if (strcmp(ondisk_ref_namespace, "refs") == 0)
985 ondisk_ref_namespace = "";
988 /* Gather on-disk refs before parsing packed-refs. */
990 path_refs = get_refs_dir_path(repo, "");
991 if (path_refs == NULL) {
992 err = got_error_from_errno("get_refs_dir_path");
995 err = gather_on_disk_refs(refs, path_refs,
996 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
1002 * The packed-refs file may contain redundant entries, in which
1003 * case on-disk refs take precedence.
1005 packed_refs_path = got_repo_get_path_packed_refs(repo);
1006 if (packed_refs_path == NULL) {
1007 err = got_error_from_errno("got_repo_get_path_packed_refs");
1011 f = fopen(packed_refs_path, "r");
1012 free(packed_refs_path);
1014 size_t linesize = 0;
1017 linelen = getline(&line, &linesize, f);
1018 if (linelen == -1) {
1021 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1024 if (linelen > 0 && line[linelen - 1] == '\n')
1025 line[linelen - 1] = '\0';
1026 err = parse_packed_ref_line(&ref, NULL, line);
1030 if (ref_namespace) {
1032 name = got_ref_get_name(ref);
1033 if (!got_path_is_child(name,
1035 strlen(ref_namespace))) {
1040 err = got_reflist_insert(&new, refs, ref, repo,
1042 if (err || new == NULL /* duplicate */)
1050 free(abs_namespace);
1054 if (f && fclose(f) == EOF && err == NULL)
1055 err = got_error_from_errno("fclose");
1060 got_ref_list_free(struct got_reflist_head *refs)
1062 struct got_reflist_entry *re;
1064 while ((re = TAILQ_FIRST(refs))) {
1065 TAILQ_REMOVE(refs, re, entry);
1066 got_ref_close(re->ref);
1073 got_ref_is_symbolic(struct got_reference *ref)
1075 return (ref->flags & GOT_REF_IS_SYMBOLIC);
1078 const struct got_error *
1079 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1081 if (ref->flags & GOT_REF_IS_SYMBOLIC)
1082 return got_error(GOT_ERR_BAD_REF_TYPE);
1084 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
1088 const struct got_error *
1089 got_ref_change_symref(struct got_reference *ref, const char *refname)
1093 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1094 return got_error(GOT_ERR_BAD_REF_TYPE);
1096 new_name = strdup(refname);
1097 if (new_name == NULL)
1098 return got_error_from_errno("strdup");
1100 free(ref->ref.symref.ref);
1101 ref->ref.symref.ref = new_name;
1105 const struct got_error *
1106 got_ref_change_symref_to_ref(struct got_reference *symref,
1107 struct got_object_id *id)
1109 if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1110 return got_error(GOT_ERR_BAD_REF_TYPE);
1112 symref->ref.ref.name = symref->ref.symref.name;
1113 memcpy(symref->ref.ref.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1114 symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1118 const struct got_error *
1119 got_ref_write(struct got_reference *ref, struct got_repository *repo)
1121 const struct got_error *err = NULL, *unlock_err = NULL;
1122 const char *name = got_ref_get_name(ref);
1123 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1124 struct got_lockfile *lf = NULL;
1129 path_refs = get_refs_dir_path(repo, name);
1130 if (path_refs == NULL) {
1131 err = got_error_from_errno2("get_refs_dir_path", name);
1135 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1136 err = got_error_from_errno("asprintf");
1140 err = got_opentemp_named(&tmppath, &f, path);
1143 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1145 err = got_path_dirname(&parent, path);
1148 err = got_path_mkdir(parent);
1152 err = got_opentemp_named(&tmppath, &f, path);
1157 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1158 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1159 if (n != strlen(ref->ref.symref.ref) + 6) {
1160 err = got_ferror(f, GOT_ERR_IO);
1164 char hex[SHA1_DIGEST_STRING_LENGTH];
1165 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
1166 sizeof(hex)) == NULL) {
1167 err = got_error(GOT_ERR_BAD_REF_DATA);
1170 n = fprintf(f, "%s\n", hex);
1171 if (n != sizeof(hex)) {
1172 err = got_ferror(f, GOT_ERR_IO);
1177 if (ref->lf == NULL) {
1178 err = got_lockfile_lock(&lf, path);
1183 /* XXX: check if old content matches our expectations? */
1185 if (stat(path, &sb) != 0) {
1186 if (errno != ENOENT) {
1187 err = got_error_from_errno2("stat", path);
1190 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1193 if (fchmod(fileno(f), sb.st_mode) != 0) {
1194 err = got_error_from_errno2("fchmod", tmppath);
1198 if (rename(tmppath, path) != 0) {
1199 err = got_error_from_errno3("rename", tmppath, path);
1205 if (ref->lf == NULL && lf)
1206 unlock_err = got_lockfile_unlock(lf);
1208 if (fclose(f) == EOF && err == NULL)
1209 err = got_error_from_errno("fclose");
1214 if (unlink(tmppath) != 0 && err == NULL)
1215 err = got_error_from_errno2("unlink", tmppath);
1218 return err ? err : unlock_err;
1221 static const struct got_error *
1222 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1224 const struct got_error *err = NULL, *unlock_err = NULL;
1225 struct got_lockfile *lf = NULL;
1226 FILE *f = NULL, *tmpf = NULL;
1227 char *line = NULL, *packed_refs_path, *tmppath = NULL;
1228 size_t linesize = 0;
1229 struct got_reflist_head refs;
1230 int found_delref = 0;
1232 /* The packed-refs file does not cotain symbolic references. */
1233 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1234 return got_error(GOT_ERR_BAD_REF_DATA);
1238 packed_refs_path = got_repo_get_path_packed_refs(repo);
1239 if (packed_refs_path == NULL)
1240 return got_error_from_errno("got_repo_get_path_packed_refs");
1242 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
1246 if (delref->lf == NULL) {
1247 err = got_lockfile_lock(&lf, packed_refs_path);
1252 f = fopen(packed_refs_path, "r");
1254 err = got_error_from_errno2("fopen", packed_refs_path);
1259 struct got_reference *ref;
1260 struct got_reflist_entry *new;
1262 linelen = getline(&line, &linesize, f);
1263 if (linelen == -1) {
1266 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1269 if (linelen > 0 && line[linelen - 1] == '\n')
1270 line[linelen - 1] = '\0';
1271 err = parse_packed_ref_line(&ref, NULL, line);
1277 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1278 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1279 sizeof(delref->ref.ref.sha1)) == 0) {
1285 err = got_reflist_insert(&new, &refs, ref, repo,
1286 got_ref_cmp_by_name, NULL);
1287 if (err || new == NULL /* duplicate */)
1294 struct got_reflist_entry *re;
1298 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1299 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1300 err = got_ferror(f, GOT_ERR_IO);
1304 TAILQ_FOREACH(re, &refs, entry) {
1305 uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
1307 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1308 sizeof(hex)) == NULL) {
1309 err = got_error(GOT_ERR_BAD_REF_DATA);
1312 n = fprintf(tmpf, "%s ", hex);
1313 if (n != sizeof(hex)) {
1314 err = got_ferror(f, GOT_ERR_IO);
1317 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1318 if (n != strlen(re->ref->ref.ref.name) + 1) {
1319 err = got_ferror(f, GOT_ERR_IO);
1324 if (fflush(tmpf) != 0) {
1325 err = got_error_from_errno("fflush");
1329 if (fstat(fileno(f), &sb) != 0) {
1330 if (errno != ENOENT) {
1331 err = got_error_from_errno2("fstat",
1335 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1338 if (fchmod(fileno(tmpf), sb.st_mode) != 0) {
1339 err = got_error_from_errno2("fchmod", tmppath);
1343 if (rename(tmppath, packed_refs_path) != 0) {
1344 err = got_error_from_errno3("rename", tmppath,
1350 if (delref->lf == NULL && lf)
1351 unlock_err = got_lockfile_unlock(lf);
1353 if (fclose(f) == EOF && err == NULL)
1354 err = got_error_from_errno("fclose");
1358 if (fclose(tmpf) == EOF && err == NULL)
1359 err = got_error_from_errno("fclose");
1362 free(packed_refs_path);
1364 got_ref_list_free(&refs);
1365 return err ? err : unlock_err;
1368 static const struct got_error *
1369 delete_loose_ref(struct got_reference *ref, struct got_repository *repo)
1371 const struct got_error *err = NULL, *unlock_err = NULL;
1372 const char *name = got_ref_get_name(ref);
1373 char *path_refs = NULL, *path = NULL;
1374 struct got_lockfile *lf = NULL;
1376 path_refs = get_refs_dir_path(repo, name);
1377 if (path_refs == NULL) {
1378 err = got_error_from_errno2("get_refs_dir_path", name);
1382 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1383 err = got_error_from_errno("asprintf");
1387 if (ref->lf == NULL) {
1388 err = got_lockfile_lock(&lf, path);
1393 /* XXX: check if old content matches our expectations? */
1395 if (unlink(path) != 0)
1396 err = got_error_from_errno2("unlink", path);
1398 if (ref->lf == NULL && lf)
1399 unlock_err = got_lockfile_unlock(lf);
1403 return err ? err : unlock_err;
1406 const struct got_error *
1407 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1409 const struct got_error *err = NULL;
1410 struct got_reference *ref2;
1412 if (ref->flags & GOT_REF_IS_PACKED) {
1413 err = delete_packed_ref(ref, repo);
1417 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1419 if (err->code == GOT_ERR_NOT_REF)
1424 err = delete_loose_ref(ref2, repo);
1425 got_ref_close(ref2);
1428 err = delete_loose_ref(ref, repo);
1432 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1434 if (err->code == GOT_ERR_NOT_REF)
1439 err = delete_packed_ref(ref2, repo);
1440 got_ref_close(ref2);
1445 const struct got_error *
1446 got_ref_unlock(struct got_reference *ref)
1448 const struct got_error *err;
1449 err = got_lockfile_unlock(ref->lf);
1454 struct got_reflist_object_id_map {
1455 struct got_object_idset *idset;
1458 struct got_reflist_object_id_map_entry {
1459 struct got_reflist_head refs;
1462 static const struct got_error *
1463 add_object_id_map_entry(struct got_object_idset *idset,
1464 struct got_object_id *id, struct got_reflist_entry *re)
1466 const struct got_error *err = NULL;
1467 struct got_reflist_object_id_map_entry *ent;
1468 struct got_reflist_entry *new;
1470 ent = got_object_idset_get(idset, id);
1472 ent = malloc(sizeof(*ent));
1474 return got_error_from_errno("malloc");
1476 TAILQ_INIT(&ent->refs);
1477 err = got_object_idset_add(idset, id, ent);
1482 err = got_reflist_entry_dup(&new, re);
1486 TAILQ_INSERT_TAIL(&ent->refs, new, entry);
1490 const struct got_error *
1491 got_reflist_object_id_map_create(struct got_reflist_object_id_map **map,
1492 struct got_reflist_head *refs, struct got_repository *repo)
1494 const struct got_error *err = NULL;
1495 struct got_object_idset *idset;
1496 struct got_object_id *id = NULL;
1497 struct got_reflist_entry *re;
1499 idset = got_object_idset_alloc();
1501 return got_error_from_errno("got_object_idset_alloc");
1503 *map = malloc(sizeof(**map));
1505 got_object_idset_free(idset);
1506 return got_error_from_errno("malloc");
1508 (*map)->idset = idset;
1510 TAILQ_FOREACH(re, refs, entry) {
1511 struct got_tag_object *tag = NULL;
1513 err = got_ref_resolve(&id, repo, re->ref);
1517 err = add_object_id_map_entry(idset, id, re);
1521 if (strstr(got_ref_get_name(re->ref), "/tags/") == NULL) {
1527 err = got_object_open_as_tag(&tag, repo, id);
1529 if (err->code != GOT_ERR_OBJ_TYPE)
1531 /* Ref points at something other than a tag. */
1539 err = add_object_id_map_entry(idset,
1540 got_object_tag_get_object_id(tag), re);
1541 got_object_tag_close(tag);
1551 got_reflist_object_id_map_free(*map);
1557 struct got_reflist_head *
1558 got_reflist_object_id_map_lookup(struct got_reflist_object_id_map *map,
1559 struct got_object_id *id)
1561 struct got_reflist_object_id_map_entry *ent;
1562 ent = got_object_idset_get(map->idset, id);
1568 static const struct got_error *
1569 free_id_map_entry(struct got_object_id *id, void *data, void *arg)
1571 struct got_reflist_object_id_map_entry *ent = data;
1573 got_ref_list_free(&ent->refs);
1579 got_reflist_object_id_map_free(struct got_reflist_object_id_map *map)
1581 got_object_idset_for_each(map->idset, free_id_map_entry, NULL);
1582 got_object_idset_free(map->idset);