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) != 0 && 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;
364 const char delim[3] = {'\0', '\0', '\0'};
365 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
370 abs_refname = (char *)refname;
372 line = fparseln(f, &len, NULL, delim, 0);
376 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
379 for (i = 0; i < nsubdirs; i++) {
380 if (!ref_is_absolute &&
381 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
383 return got_error_from_errno("asprintf");
384 err = parse_packed_ref_line(ref, abs_refname, line);
385 if (!ref_is_absolute)
387 if (err || *ref != NULL)
393 } while (*ref == NULL);
398 static const struct got_error *
399 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
400 const char *name, int lock)
402 const struct got_error *err = NULL;
404 char *absname = NULL;
405 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
406 int ref_is_well_known = (subdir[0] == '\0' && is_well_known_ref(name));
410 if (ref_is_absolute || ref_is_well_known) {
411 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
412 return got_error_from_errno("asprintf");
413 absname = (char *)name;
415 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
416 subdir[0] ? "/" : "", name) == -1)
417 return got_error_from_errno("asprintf");
419 if (asprintf(&absname, "refs/%s%s%s",
420 subdir, subdir[0] ? "/" : "", name) == -1) {
421 err = got_error_from_errno("asprintf");
426 err = parse_ref_file(ref, name, absname, path, lock);
428 if (!ref_is_absolute && !ref_is_well_known)
434 const struct got_error *
435 got_ref_open(struct got_reference **ref, struct got_repository *repo,
436 const char *refname, int lock)
438 const struct got_error *err = NULL;
439 char *path_refs = NULL;
440 const char *subdirs[] = {
441 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
444 int well_known = is_well_known_ref(refname);
445 struct got_lockfile *lf = NULL;
449 path_refs = get_refs_dir_path(repo, refname);
450 if (path_refs == NULL) {
451 err = got_error_from_errno2("get_refs_dir_path", refname);
456 err = open_ref(ref, path_refs, "", refname, lock);
458 char *packed_refs_path;
461 /* Search on-disk refs before packed refs! */
462 for (i = 0; i < nitems(subdirs); i++) {
463 err = open_ref(ref, path_refs, subdirs[i], refname,
465 if ((err && err->code != GOT_ERR_NOT_REF) || *ref)
469 packed_refs_path = got_repo_get_path_packed_refs(repo);
470 if (packed_refs_path == NULL) {
471 err = got_error_from_errno(
472 "got_repo_get_path_packed_refs");
477 err = got_lockfile_lock(&lf, packed_refs_path);
481 f = fopen(packed_refs_path, "rb");
482 free(packed_refs_path);
484 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
487 if (fclose(f) != 0) {
488 err = got_error_from_errno("fclose");
497 if (!err && *ref == NULL)
498 err = got_error_not_ref(refname);
500 got_lockfile_unlock(lf);
506 got_ref_close(struct got_reference *ref)
508 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
509 free(ref->ref.symref.name);
510 free(ref->ref.symref.ref);
512 free(ref->ref.ref.name);
516 struct got_reference *
517 got_ref_dup(struct got_reference *ref)
519 struct got_reference *ret;
521 ret = calloc(1, sizeof(*ret));
525 ret->flags = ref->flags;
526 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
527 ret->ref.symref.name = strdup(ref->ref.symref.name);
528 if (ret->ref.symref.name == NULL) {
532 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
533 if (ret->ref.symref.ref == NULL) {
534 free(ret->ref.symref.name);
539 ret->ref.ref.name = strdup(ref->ref.ref.name);
540 if (ret->ref.ref.name == NULL) {
544 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
545 sizeof(ret->ref.ref.sha1));
551 const struct got_error *
552 got_reflist_entry_dup(struct got_reflist_entry **newp,
553 struct got_reflist_entry *re)
555 const struct got_error *err = NULL;
556 struct got_reflist_entry *new;
560 new = malloc(sizeof(*new));
562 return got_error_from_errno("malloc");
564 new->ref = got_ref_dup(re->ref);
565 if (new->ref == NULL) {
566 err = got_error_from_errno("got_ref_dup");
575 static const struct got_error *
576 resolve_symbolic_ref(struct got_reference **resolved,
577 struct got_repository *repo, struct got_reference *ref)
579 struct got_reference *nextref;
580 const struct got_error *err;
582 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
586 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
587 err = resolve_symbolic_ref(resolved, repo, nextref);
589 *resolved = got_ref_dup(nextref);
591 got_ref_close(nextref);
595 static const struct got_error *
596 ref_resolve(struct got_object_id **id, struct got_repository *repo,
597 struct got_reference *ref, int recursion)
599 const struct got_error *err;
602 return got_error_msg(GOT_ERR_RECURSION,
603 "reference recursion limit reached");
605 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
606 struct got_reference *resolved = NULL;
607 err = resolve_symbolic_ref(&resolved, repo, ref);
609 err = ref_resolve(id, repo, resolved, --recursion);
611 got_ref_close(resolved);
615 *id = calloc(1, sizeof(**id));
617 return got_error_from_errno("calloc");
618 memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
622 const struct got_error *
623 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
624 struct got_reference *ref)
626 return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
630 got_ref_to_str(struct got_reference *ref)
634 if (ref->flags & GOT_REF_IS_SYMBOLIC)
635 return strdup(ref->ref.symref.ref);
637 str = malloc(SHA1_DIGEST_STRING_LENGTH);
641 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
642 SHA1_DIGEST_STRING_LENGTH) == NULL) {
651 got_ref_get_name(struct got_reference *ref)
653 if (ref->flags & GOT_REF_IS_SYMBOLIC)
654 return ref->ref.symref.name;
656 return ref->ref.ref.name;
660 got_ref_get_symref_target(struct got_reference *ref)
662 if (ref->flags & GOT_REF_IS_SYMBOLIC)
663 return ref->ref.symref.ref;
668 const struct got_error *
669 got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
670 struct got_reference* re2)
672 const char *name1 = got_ref_get_name(re1);
673 const char *name2 = got_ref_get_name(re2);
675 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
679 const struct got_error *
680 got_ref_cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
681 struct got_reference *ref2)
683 const struct got_error *err = NULL;
684 struct got_repository *repo = arg;
685 struct got_object_id *id1, *id2 = NULL;
686 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
687 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
692 err = got_ref_resolve(&id1, repo, ref1);
695 err = got_object_open_as_tag(&tag1, repo, id1);
697 if (err->code != GOT_ERR_OBJ_TYPE)
699 /* "lightweight" tag */
700 err = got_object_open_as_commit(&commit1, repo, id1);
703 time1 = got_object_commit_get_committer_time(commit1);
705 time1 = got_object_tag_get_tagger_time(tag1);
707 err = got_ref_resolve(&id2, repo, ref2);
710 err = got_object_open_as_tag(&tag2, repo, id2);
712 if (err->code != GOT_ERR_OBJ_TYPE)
714 /* "lightweight" tag */
715 err = got_object_open_as_commit(&commit2, repo, id2);
718 time2 = got_object_commit_get_committer_time(commit2);
720 time2 = got_object_tag_get_tagger_time(tag2);
722 /* Put latest tags first. */
725 else if (time1 > time2)
728 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
733 got_object_tag_close(tag1);
735 got_object_tag_close(tag2);
737 got_object_commit_close(commit1);
739 got_object_commit_close(commit2);
743 static const struct got_error *
744 insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
745 struct got_reference *ref, struct got_repository *repo,
746 got_ref_cmp_cb cmp_cb, void *cmp_arg)
748 const struct got_error *err;
749 struct got_reflist_entry *new, *re, *prev = NULL;
754 new = malloc(sizeof(*new));
756 return got_error_from_errno("malloc");
761 * We must de-duplicate entries on insert because packed-refs may
762 * contain redundant entries. On-disk refs take precedence.
763 * This code assumes that on-disk revs are read before packed-refs.
764 * We're iterating the list anyway, so insert elements sorted by name.
766 re = SIMPLEQ_FIRST(refs);
768 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
776 } else if (cmp > 0) {
778 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
780 SIMPLEQ_INSERT_HEAD(refs, new, entry);
784 re = SIMPLEQ_NEXT(re, entry);
788 SIMPLEQ_INSERT_TAIL(refs, new, entry);
792 static const struct got_error *
793 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
794 const char *subdir, struct got_repository *repo,
795 got_ref_cmp_cb cmp_cb, void *cmp_arg)
797 const struct got_error *err = NULL;
801 while (subdir[0] == '/')
804 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
805 return got_error_from_errno("asprintf");
807 d = opendir(path_subdir);
813 struct got_reference *ref;
821 if (strcmp(dent->d_name, ".") == 0 ||
822 strcmp(dent->d_name, "..") == 0)
825 err = got_path_dirent_type(&type, path_subdir, dent);
831 err = open_ref(&ref, path_refs, subdir, dent->d_name,
836 struct got_reflist_entry *new;
837 err = insert_ref(&new, refs, ref, repo,
839 if (err || new == NULL /* duplicate */)
846 if (asprintf(&child, "%s%s%s", subdir,
847 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
848 err = got_error_from_errno("asprintf");
851 err = gather_on_disk_refs(refs, path_refs, child, repo,
866 const struct got_error *
867 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
868 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
870 const struct got_error *err;
871 char *packed_refs_path, *path_refs = NULL;
872 char *abs_namespace = NULL;
873 char *buf = NULL, *ondisk_ref_namespace = NULL;
875 struct got_reference *ref;
876 struct got_reflist_entry *new;
878 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
879 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
880 if (path_refs == NULL) {
881 err = got_error_from_errno("get_refs_dir_path");
884 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
887 err = insert_ref(&new, refs, ref, repo,
889 if (err || new == NULL /* duplicate */)
891 if (err && err->code != GOT_ERR_NOT_REF)
894 /* Try listing a single reference. */
895 const char *refname = ref_namespace;
896 path_refs = get_refs_dir_path(repo, refname);
897 if (path_refs == NULL) {
898 err = got_error_from_errno("get_refs_dir_path");
901 err = open_ref(&ref, path_refs, "", refname, 0);
903 if (err->code != GOT_ERR_NOT_REF)
905 /* Try to look up references in a given namespace. */
907 err = insert_ref(&new, refs, ref, repo,
909 if (err || new == NULL /* duplicate */)
917 /* Canonicalize the path to eliminate double-slashes if any. */
918 if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
919 err = got_error_from_errno("asprintf");
922 len = strlen(abs_namespace) + 1;
925 err = got_error_from_errno("malloc");
928 err = got_canonpath(abs_namespace, buf, len);
931 ondisk_ref_namespace = buf;
932 while (ondisk_ref_namespace[0] == '/')
933 ondisk_ref_namespace++;
934 if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
935 ondisk_ref_namespace += 5;
936 else if (strcmp(ondisk_ref_namespace, "refs") == 0)
937 ondisk_ref_namespace = "";
940 /* Gather on-disk refs before parsing packed-refs. */
942 path_refs = get_refs_dir_path(repo, "");
943 if (path_refs == NULL) {
944 err = got_error_from_errno("get_refs_dir_path");
947 err = gather_on_disk_refs(refs, path_refs,
948 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
954 * The packed-refs file may contain redundant entries, in which
955 * case on-disk refs take precedence.
957 packed_refs_path = got_repo_get_path_packed_refs(repo);
958 if (packed_refs_path == NULL) {
959 err = got_error_from_errno("got_repo_get_path_packed_refs");
963 f = fopen(packed_refs_path, "r");
964 free(packed_refs_path);
968 const char delim[3] = {'\0', '\0', '\0'};
970 line = fparseln(f, &len, NULL, delim, 0);
974 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
977 err = parse_packed_ref_line(&ref, NULL, line);
984 name = got_ref_get_name(ref);
985 if (!got_path_is_child(name,
987 strlen(ref_namespace))) {
992 err = insert_ref(&new, refs, ref, repo,
994 if (err || new == NULL /* duplicate */)
1002 free(abs_namespace);
1005 if (f && fclose(f) != 0 && err == NULL)
1006 err = got_error_from_errno("fclose");
1011 got_ref_list_free(struct got_reflist_head *refs)
1013 struct got_reflist_entry *re;
1015 while (!SIMPLEQ_EMPTY(refs)) {
1016 re = SIMPLEQ_FIRST(refs);
1017 SIMPLEQ_REMOVE_HEAD(refs, entry);
1018 got_ref_close(re->ref);
1025 got_ref_is_symbolic(struct got_reference *ref)
1027 return (ref->flags & GOT_REF_IS_SYMBOLIC);
1030 const struct got_error *
1031 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1033 if (ref->flags & GOT_REF_IS_SYMBOLIC)
1034 return got_error(GOT_ERR_BAD_REF_TYPE);
1036 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
1040 const struct got_error *
1041 got_ref_change_symref(struct got_reference *ref, const char *refname)
1045 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1046 return got_error(GOT_ERR_BAD_REF_TYPE);
1048 new_name = strdup(refname);
1049 if (new_name == NULL)
1050 return got_error_from_errno("strdup");
1052 free(ref->ref.symref.ref);
1053 ref->ref.symref.ref = new_name;
1057 const struct got_error *
1058 got_ref_change_symref_to_ref(struct got_reference *symref,
1059 struct got_object_id *id)
1061 if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1062 return got_error(GOT_ERR_BAD_REF_TYPE);
1064 symref->ref.ref.name = symref->ref.symref.name;
1065 memcpy(symref->ref.ref.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1066 symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1070 const struct got_error *
1071 got_ref_write(struct got_reference *ref, struct got_repository *repo)
1073 const struct got_error *err = NULL, *unlock_err = NULL;
1074 const char *name = got_ref_get_name(ref);
1075 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1076 struct got_lockfile *lf = NULL;
1081 path_refs = get_refs_dir_path(repo, name);
1082 if (path_refs == NULL) {
1083 err = got_error_from_errno2("get_refs_dir_path", name);
1087 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1088 err = got_error_from_errno("asprintf");
1092 err = got_opentemp_named(&tmppath, &f, path);
1095 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1097 err = got_path_dirname(&parent, path);
1100 err = got_path_mkdir(parent);
1104 err = got_opentemp_named(&tmppath, &f, path);
1109 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1110 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1111 if (n != strlen(ref->ref.symref.ref) + 6) {
1112 err = got_ferror(f, GOT_ERR_IO);
1116 char hex[SHA1_DIGEST_STRING_LENGTH];
1117 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
1118 sizeof(hex)) == NULL) {
1119 err = got_error(GOT_ERR_BAD_REF_DATA);
1122 n = fprintf(f, "%s\n", hex);
1123 if (n != sizeof(hex)) {
1124 err = got_ferror(f, GOT_ERR_IO);
1129 if (ref->lf == NULL) {
1130 err = got_lockfile_lock(&lf, path);
1135 /* XXX: check if old content matches our expectations? */
1137 if (stat(path, &sb) != 0) {
1138 if (errno != ENOENT) {
1139 err = got_error_from_errno2("stat", path);
1142 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1145 if (fchmod(fileno(f), sb.st_mode) != 0) {
1146 err = got_error_from_errno2("fchmod", tmppath);
1150 if (rename(tmppath, path) != 0) {
1151 err = got_error_from_errno3("rename", tmppath, path);
1157 if (ref->lf == NULL && lf)
1158 unlock_err = got_lockfile_unlock(lf);
1160 if (fclose(f) != 0 && err == NULL)
1161 err = got_error_from_errno("fclose");
1166 if (unlink(tmppath) != 0 && err == NULL)
1167 err = got_error_from_errno2("unlink", tmppath);
1170 return err ? err : unlock_err;
1173 static const struct got_error *
1174 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1176 const struct got_error *err = NULL, *unlock_err = NULL;
1177 struct got_lockfile *lf = NULL;
1178 FILE *f = NULL, *tmpf = NULL;
1179 char *packed_refs_path, *tmppath = NULL;
1180 struct got_reflist_head refs;
1181 int found_delref = 0;
1183 /* The packed-refs file does not cotain symbolic references. */
1184 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1185 return got_error(GOT_ERR_BAD_REF_DATA);
1187 SIMPLEQ_INIT(&refs);
1189 packed_refs_path = got_repo_get_path_packed_refs(repo);
1190 if (packed_refs_path == NULL)
1191 return got_error_from_errno("got_repo_get_path_packed_refs");
1193 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
1197 if (delref->lf == NULL) {
1198 err = got_lockfile_lock(&lf, packed_refs_path);
1203 f = fopen(packed_refs_path, "r");
1205 err = got_error_from_errno2("fopen", packed_refs_path);
1211 const char delim[3] = {'\0', '\0', '\0'};
1212 struct got_reference *ref;
1213 struct got_reflist_entry *new;
1215 line = fparseln(f, &len, NULL, delim, 0);
1219 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1222 err = parse_packed_ref_line(&ref, NULL, line);
1229 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1230 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1231 sizeof(delref->ref.ref.sha1)) == 0) {
1237 err = insert_ref(&new, &refs, ref, repo,
1238 got_ref_cmp_by_name, NULL);
1239 if (err || new == NULL /* duplicate */)
1246 struct got_reflist_entry *re;
1250 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1251 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1252 err = got_ferror(f, GOT_ERR_IO);
1256 SIMPLEQ_FOREACH(re, &refs, entry) {
1257 uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
1259 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1260 sizeof(hex)) == NULL) {
1261 err = got_error(GOT_ERR_BAD_REF_DATA);
1264 n = fprintf(tmpf, "%s ", hex);
1265 if (n != sizeof(hex)) {
1266 err = got_ferror(f, GOT_ERR_IO);
1269 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1270 if (n != strlen(re->ref->ref.ref.name) + 1) {
1271 err = got_ferror(f, GOT_ERR_IO);
1276 if (fflush(tmpf) != 0) {
1277 err = got_error_from_errno("fflush");
1281 if (fstat(fileno(f), &sb) != 0) {
1282 if (errno != ENOENT) {
1283 err = got_error_from_errno2("fstat",
1287 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1290 if (fchmod(fileno(tmpf), sb.st_mode) != 0) {
1291 err = got_error_from_errno2("fchmod", tmppath);
1295 if (rename(tmppath, packed_refs_path) != 0) {
1296 err = got_error_from_errno3("rename", tmppath,
1302 if (delref->lf == NULL && lf)
1303 unlock_err = got_lockfile_unlock(lf);
1305 if (fclose(f) != 0 && err == NULL)
1306 err = got_error_from_errno("fclose");
1310 if (fclose(tmpf) != 0 && err == NULL)
1311 err = got_error_from_errno("fclose");
1314 free(packed_refs_path);
1315 got_ref_list_free(&refs);
1316 return err ? err : unlock_err;
1319 static const struct got_error *
1320 delete_loose_ref(struct got_reference *ref, struct got_repository *repo)
1322 const struct got_error *err = NULL, *unlock_err = NULL;
1323 const char *name = got_ref_get_name(ref);
1324 char *path_refs = NULL, *path = NULL;
1325 struct got_lockfile *lf = NULL;
1327 path_refs = get_refs_dir_path(repo, name);
1328 if (path_refs == NULL) {
1329 err = got_error_from_errno2("get_refs_dir_path", name);
1333 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1334 err = got_error_from_errno("asprintf");
1338 if (ref->lf == NULL) {
1339 err = got_lockfile_lock(&lf, path);
1344 /* XXX: check if old content matches our expectations? */
1346 if (unlink(path) != 0)
1347 err = got_error_from_errno2("unlink", path);
1349 if (ref->lf == NULL && lf)
1350 unlock_err = got_lockfile_unlock(lf);
1354 return err ? err : unlock_err;
1357 const struct got_error *
1358 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1360 const struct got_error *err = NULL;
1361 struct got_reference *ref2;
1363 if (ref->flags & GOT_REF_IS_PACKED) {
1364 err = delete_packed_ref(ref, repo);
1368 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1370 if (err->code == GOT_ERR_NOT_REF)
1375 err = delete_loose_ref(ref2, repo);
1376 got_ref_close(ref2);
1379 err = delete_loose_ref(ref, repo);
1383 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1385 if (err->code == GOT_ERR_NOT_REF)
1390 err = delete_packed_ref(ref2, repo);
1391 got_ref_close(ref2);
1396 const struct got_error *
1397 got_ref_unlock(struct got_reference *ref)
1399 const struct got_error *err;
1400 err = got_lockfile_unlock(ref->lf);
1405 struct got_reflist_object_id_map {
1406 struct got_object_idset *idset;
1409 struct got_reflist_object_id_map_entry {
1410 struct got_reflist_head refs;
1413 const struct got_error *
1414 got_reflist_object_id_map_create(struct got_reflist_object_id_map **map,
1415 struct got_reflist_head *refs, struct got_repository *repo)
1417 const struct got_error *err = NULL;
1418 struct got_object_idset *idset;
1419 struct got_object_id *id = NULL;
1420 struct got_reflist_entry *re;
1422 idset = got_object_idset_alloc();
1424 return got_error_from_errno("got_object_idset_alloc");
1426 *map = malloc(sizeof(**map));
1428 got_object_idset_free(idset);
1429 return got_error_from_errno("malloc");
1431 (*map)->idset = idset;
1433 SIMPLEQ_FOREACH(re, refs, entry) {
1434 struct got_reflist_entry *new;
1435 struct got_reflist_object_id_map_entry *ent;
1437 err = got_ref_resolve(&id, repo, re->ref);
1441 ent = got_object_idset_get(idset, id);
1443 ent = malloc(sizeof(*ent));
1445 err = got_error_from_errno("malloc");
1448 SIMPLEQ_INIT(&ent->refs);
1449 err = got_object_idset_add(idset, id, ent);
1454 err = got_reflist_entry_dup(&new, re);
1457 SIMPLEQ_INSERT_TAIL(&ent->refs, new, entry);
1464 got_reflist_object_map_free(*map);
1470 struct got_reflist_head *
1471 got_reflist_object_id_map_lookup(struct got_reflist_object_id_map *map,
1472 struct got_object_id *id)
1474 struct got_reflist_object_id_map_entry *ent;
1475 ent = got_object_idset_get(map->idset, id);
1481 static const struct got_error *
1482 free_id_map_entry(struct got_object_id *id, void *data, void *arg)
1484 struct got_reflist_object_id_map_entry *ent = data;
1486 got_ref_list_free(&ent->refs);
1492 got_reflist_object_map_free(struct got_reflist_object_id_map *map)
1494 got_object_idset_for_each(map->idset, free_id_map_entry, NULL);
1495 got_object_idset_free(map->idset);