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>
34 #include "got_error.h"
35 #include "got_object.h"
36 #include "got_repository.h"
37 #include "got_reference.h"
38 #include "got_opentemp.h"
41 #include "got_lib_sha1.h"
42 #include "got_lib_delta.h"
43 #include "got_lib_inflate.h"
44 #include "got_lib_object.h"
45 #include "got_lib_lockfile.h"
48 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
51 #define GOT_REF_HEADS "heads"
52 #define GOT_REF_TAGS "tags"
53 #define GOT_REF_REMOTES "remotes"
56 * We do not resolve tags yet, and don't yet care about sorting refs either,
57 * so packed-refs files we write contain a minimal header which disables all
58 * packed-refs "traits" supported by Git.
60 #define GOT_PACKED_REFS_HEADER "# pack-refs with:"
62 /* A symbolic reference. */
68 #define GOT_REF_RECURSE_MAX 20
70 /* A non-symbolic reference (there is no better designation). */
73 u_int8_t sha1[SHA1_DIGEST_LENGTH];
76 /* A reference which points to an arbitrary object. */
77 struct got_reference {
79 #define GOT_REF_IS_SYMBOLIC 0x01
80 #define GOT_REF_IS_PACKED 0x02
84 struct got_symref symref;
87 struct got_lockfile *lf;
90 static const struct got_error *
91 alloc_ref(struct got_reference **ref, const char *name,
92 struct got_object_id *id, int flags)
94 const struct got_error *err = NULL;
96 *ref = calloc(1, sizeof(**ref));
98 return got_error_from_errno("calloc");
100 memcpy((*ref)->ref.ref.sha1, id->sha1, sizeof((*ref)->ref.ref.sha1));
101 (*ref)->flags = flags;
102 (*ref)->ref.ref.name = strdup(name);
103 if ((*ref)->ref.ref.name == NULL) {
104 err = got_error_from_errno("strdup");
111 static const struct got_error *
112 alloc_symref(struct got_reference **ref, const char *name,
113 const char *target_ref, int flags)
115 const struct got_error *err = NULL;
117 *ref = calloc(1, sizeof(**ref));
119 return got_error_from_errno("calloc");
121 (*ref)->flags = GOT_REF_IS_SYMBOLIC | flags;
122 (*ref)->ref.symref.name = strdup(name);
123 if ((*ref)->ref.symref.name == NULL) {
124 err = got_error_from_errno("strdup");
129 (*ref)->ref.symref.ref = strdup(target_ref);
130 if ((*ref)->ref.symref.ref == NULL) {
131 err = got_error_from_errno("strdup");
138 static const struct got_error *
139 parse_symref(struct got_reference **ref, const char *name, const char *line)
142 return got_error(GOT_ERR_BAD_REF_DATA);
144 return alloc_symref(ref, name, line, 0);
147 static const struct got_error *
148 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
150 struct got_object_id id;
152 if (strncmp(line, "ref: ", 5) == 0) {
154 return parse_symref(ref, name, line);
157 if (!got_parse_sha1_digest(id.sha1, line))
158 return got_error(GOT_ERR_BAD_REF_DATA);
160 return alloc_ref(ref, name, &id, 0);
163 static const struct got_error *
164 parse_ref_file(struct got_reference **ref, const char *name,
165 const char *absname, const char *abspath, int lock)
167 const struct got_error *err = NULL;
172 struct got_lockfile *lf = NULL;
175 err = got_lockfile_lock(&lf, abspath);
177 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
178 err = got_error_not_ref(name);
183 f = fopen(abspath, "rb");
185 if (errno != ENOTDIR && errno != ENOENT)
186 err = got_error_from_errno2("fopen", abspath);
188 err = got_error_not_ref(name);
190 got_lockfile_unlock(lf);
194 linelen = getline(&line, &linesize, f);
197 err = NULL; /* ignore empty files (could be locks) */
200 err = got_error(GOT_ERR_NOT_REF);
202 err = got_ferror(f, GOT_ERR_IO);
204 err = got_error_from_errno2("getline", abspath);
207 got_lockfile_unlock(lf);
210 while (linelen > 0 && line[linelen - 1] == '\n') {
211 line[linelen - 1] = '\0';
215 err = parse_ref_line(ref, absname, line);
218 got_lockfile_unlock(lf);
223 got_lockfile_unlock(lf);
228 if (fclose(f) != 0 && err == NULL) {
229 err = got_error_from_errno("fclose");
232 got_ref_unlock(*ref);
241 is_well_known_ref(const char *refname)
243 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
244 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
245 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
246 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
250 get_refs_dir_path(struct got_repository *repo, const char *refname)
252 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
253 return strdup(got_repo_get_path_git_dir(repo));
255 return got_repo_get_path_refs(repo);
259 is_valid_ref_name(const char *name)
262 const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
263 const char *forbidden_seq[] = { "//", "..", "@{" };
264 const char *lfs = GOT_LOCKFILE_SUFFIX;
265 const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
268 if (name[0] == '@' && name[1] == '\0')
273 if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
276 for (i = 0; i < nitems(forbidden); i++) {
277 if (*s == forbidden[i])
280 for (i = 0; i < nitems(forbidden_seq); i++) {
281 if (s[0] == forbidden_seq[i][0] &&
282 s[1] == forbidden_seq[i][1])
285 if (iscntrl((unsigned char)s[0]))
287 if (s[0] == '.' && s[1] == '\0')
290 const char *nextseg = s + 1;
291 if (nextseg[0] == '\0' || nextseg[0] == '.' ||
294 if (seg <= s - lfs_len &&
295 strncmp(s - lfs_len, lfs, lfs_len) == 0)
302 if (seg <= s - lfs_len &&
303 strncmp(s - lfs_len, lfs, lfs_len) == 0)
309 const struct got_error *
310 got_ref_alloc(struct got_reference **ref, const char *name,
311 struct got_object_id *id)
313 if (!is_valid_ref_name(name))
314 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
316 return alloc_ref(ref, name, id, 0);
319 const struct got_error *
320 got_ref_alloc_symref(struct got_reference **ref, const char *name,
321 struct got_reference *target_ref)
323 if (!is_valid_ref_name(name))
324 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
326 return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
329 static const struct got_error *
330 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
333 struct got_object_id id;
338 if (line[0] == '#' || line[0] == '^')
341 if (!got_parse_sha1_digest(id.sha1, line))
342 return got_error(GOT_ERR_BAD_REF_DATA);
345 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
349 name = line + SHA1_DIGEST_STRING_LENGTH;
351 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED);
354 static const struct got_error *
355 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
356 int nsubdirs, const char *refname)
358 const struct got_error *err = NULL;
362 const char delim[3] = {'\0', '\0', '\0'};
363 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
368 abs_refname = (char *)refname;
370 line = fparseln(f, &len, NULL, delim, 0);
374 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
377 for (i = 0; i < nsubdirs; i++) {
378 if (!ref_is_absolute &&
379 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
381 return got_error_from_errno("asprintf");
382 err = parse_packed_ref_line(ref, abs_refname, line);
383 if (!ref_is_absolute)
385 if (err || *ref != NULL)
391 } while (*ref == NULL);
396 static const struct got_error *
397 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
398 const char *name, int lock)
400 const struct got_error *err = NULL;
402 char *absname = NULL;
403 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
404 int ref_is_well_known = (subdir[0] == '\0' && is_well_known_ref(name));
408 if (ref_is_absolute || ref_is_well_known) {
409 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
410 return got_error_from_errno("asprintf");
411 absname = (char *)name;
413 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
414 subdir[0] ? "/" : "", name) == -1)
415 return got_error_from_errno("asprintf");
417 if (asprintf(&absname, "refs/%s%s%s",
418 subdir, subdir[0] ? "/" : "", name) == -1) {
419 err = got_error_from_errno("asprintf");
424 err = parse_ref_file(ref, name, absname, path, lock);
426 if (!ref_is_absolute && !ref_is_well_known)
432 const struct got_error *
433 got_ref_open(struct got_reference **ref, struct got_repository *repo,
434 const char *refname, int lock)
436 const struct got_error *err = NULL;
437 char *path_refs = NULL;
438 const char *subdirs[] = {
439 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
441 int i, well_known = is_well_known_ref(refname);
442 struct got_lockfile *lf = NULL;
446 path_refs = get_refs_dir_path(repo, refname);
447 if (path_refs == NULL) {
448 err = got_error_from_errno2("get_refs_dir_path", refname);
453 err = open_ref(ref, path_refs, "", refname, lock);
455 char *packed_refs_path;
458 /* Search on-disk refs before packed refs! */
459 for (i = 0; i < nitems(subdirs); i++) {
460 err = open_ref(ref, path_refs, subdirs[i], refname,
462 if ((err && err->code != GOT_ERR_NOT_REF) || *ref)
466 packed_refs_path = got_repo_get_path_packed_refs(repo);
467 if (packed_refs_path == NULL) {
468 err = got_error_from_errno(
469 "got_repo_get_path_packed_refs");
474 err = got_lockfile_lock(&lf, packed_refs_path);
478 f = fopen(packed_refs_path, "rb");
479 free(packed_refs_path);
481 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
484 if (fclose(f) != 0) {
485 err = got_error_from_errno("fclose");
494 if (!err && *ref == NULL)
495 err = got_error_not_ref(refname);
497 got_lockfile_unlock(lf);
503 got_ref_close(struct got_reference *ref)
505 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
506 free(ref->ref.symref.name);
507 free(ref->ref.symref.ref);
509 free(ref->ref.ref.name);
513 struct got_reference *
514 got_ref_dup(struct got_reference *ref)
516 struct got_reference *ret;
518 ret = calloc(1, sizeof(*ret));
522 ret->flags = ref->flags;
523 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
524 ret->ref.symref.name = strdup(ref->ref.symref.name);
525 if (ret->ref.symref.name == NULL) {
529 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
530 if (ret->ref.symref.ref == NULL) {
531 free(ret->ref.symref.name);
536 ref->ref.ref.name = strdup(ref->ref.ref.name);
537 if (ref->ref.ref.name == NULL) {
541 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
542 sizeof(ret->ref.ref.sha1));
548 const struct got_error *
549 got_reflist_entry_dup(struct got_reflist_entry **newp,
550 struct got_reflist_entry *re)
552 const struct got_error *err = NULL;
553 struct got_reflist_entry *new;
557 new = malloc(sizeof(*new));
559 return got_error_from_errno("malloc");
561 new->ref = got_ref_dup(re->ref);
562 if (new->ref == NULL) {
563 err = got_error_from_errno("got_ref_dup");
568 new->id = got_object_id_dup(re->id);
569 if (new->id == NULL) {
570 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 static const struct got_error *
749 insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
750 struct got_reference *ref, struct got_repository *repo,
751 got_ref_cmp_cb cmp_cb, void *cmp_arg)
753 const struct got_error *err;
754 struct got_object_id *id;
755 struct got_reflist_entry *new, *re, *prev = NULL;
760 err = got_ref_resolve(&id, repo, ref);
764 new = malloc(sizeof(*new));
767 return got_error_from_errno("malloc");
774 * We must de-duplicate entries on insert because packed-refs may
775 * contain redundant entries. On-disk refs take precedence.
776 * This code assumes that on-disk revs are read before packed-refs.
777 * We're iterating the list anyway, so insert elements sorted by name.
779 re = SIMPLEQ_FIRST(refs);
781 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
790 } else if (cmp > 0) {
792 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
794 SIMPLEQ_INSERT_HEAD(refs, new, entry);
798 re = SIMPLEQ_NEXT(re, entry);
802 SIMPLEQ_INSERT_TAIL(refs, new, entry);
806 static const struct got_error *
807 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
808 const char *subdir, struct got_repository *repo,
809 got_ref_cmp_cb cmp_cb, void *cmp_arg)
811 const struct got_error *err = NULL;
815 while (subdir[0] == '/')
818 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
819 return got_error_from_errno("asprintf");
821 d = opendir(path_subdir);
827 struct got_reference *ref;
835 if (strcmp(dent->d_name, ".") == 0 ||
836 strcmp(dent->d_name, "..") == 0)
839 err = got_path_dirent_type(&type, path_subdir, dent);
845 err = open_ref(&ref, path_refs, subdir, dent->d_name,
850 struct got_reflist_entry *new;
851 err = insert_ref(&new, refs, ref, repo,
853 if (err || new == NULL /* duplicate */)
860 if (asprintf(&child, "%s%s%s", subdir,
861 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
862 err = got_error_from_errno("asprintf");
865 err = gather_on_disk_refs(refs, path_refs, child, repo,
880 const struct got_error *
881 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
882 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
884 const struct got_error *err;
885 char *packed_refs_path, *path_refs = NULL;
886 char *abs_namespace = NULL;
887 char *buf = NULL, *ondisk_ref_namespace = NULL;
889 struct got_reference *ref;
890 struct got_reflist_entry *new;
892 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
893 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
894 if (path_refs == NULL) {
895 err = got_error_from_errno("get_refs_dir_path");
898 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
901 err = insert_ref(&new, refs, ref, repo,
903 if (err || new == NULL /* duplicate */)
905 if (err && err->code != GOT_ERR_NOT_REF)
908 /* Try listing a single reference. */
909 const char *refname = ref_namespace;
910 path_refs = get_refs_dir_path(repo, refname);
911 if (path_refs == NULL) {
912 err = got_error_from_errno("get_refs_dir_path");
915 err = open_ref(&ref, path_refs, "", refname, 0);
917 if (err->code != GOT_ERR_NOT_REF)
919 /* Try to look up references in a given namespace. */
921 err = insert_ref(&new, refs, ref, repo,
923 if (err || new == NULL /* duplicate */)
931 /* Canonicalize the path to eliminate double-slashes if any. */
932 if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
933 err = got_error_from_errno("asprintf");
936 len = strlen(abs_namespace) + 1;
939 err = got_error_from_errno("malloc");
942 err = got_canonpath(abs_namespace, buf, len);
945 ondisk_ref_namespace = buf;
946 while (ondisk_ref_namespace[0] == '/')
947 ondisk_ref_namespace++;
948 if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
949 ondisk_ref_namespace += 5;
950 else if (strcmp(ondisk_ref_namespace, "refs") == 0)
951 ondisk_ref_namespace = "";
954 /* Gather on-disk refs before parsing packed-refs. */
956 path_refs = get_refs_dir_path(repo, "");
957 if (path_refs == NULL) {
958 err = got_error_from_errno("get_refs_dir_path");
961 err = gather_on_disk_refs(refs, path_refs,
962 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
968 * The packed-refs file may contain redundant entries, in which
969 * case on-disk refs take precedence.
971 packed_refs_path = got_repo_get_path_packed_refs(repo);
972 if (packed_refs_path == NULL) {
973 err = got_error_from_errno("got_repo_get_path_packed_refs");
977 f = fopen(packed_refs_path, "r");
978 free(packed_refs_path);
982 const char delim[3] = {'\0', '\0', '\0'};
984 line = fparseln(f, &len, NULL, delim, 0);
988 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
991 err = parse_packed_ref_line(&ref, NULL, line);
998 name = got_ref_get_name(ref);
999 if (!got_path_is_child(name,
1001 strlen(ref_namespace))) {
1006 err = insert_ref(&new, refs, ref, repo,
1008 if (err || new == NULL /* duplicate */)
1016 free(abs_namespace);
1019 if (f && fclose(f) != 0 && err == NULL)
1020 err = got_error_from_errno("fclose");
1025 got_ref_list_free(struct got_reflist_head *refs)
1027 struct got_reflist_entry *re;
1029 while (!SIMPLEQ_EMPTY(refs)) {
1030 re = SIMPLEQ_FIRST(refs);
1031 SIMPLEQ_REMOVE_HEAD(refs, entry);
1032 got_ref_close(re->ref);
1040 got_ref_is_symbolic(struct got_reference *ref)
1042 return (ref->flags & GOT_REF_IS_SYMBOLIC);
1045 const struct got_error *
1046 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1048 if (ref->flags & GOT_REF_IS_SYMBOLIC)
1049 return got_error(GOT_ERR_BAD_REF_TYPE);
1051 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
1055 const struct got_error *
1056 got_ref_change_symref(struct got_reference *ref, const char *refname)
1060 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1061 return got_error(GOT_ERR_BAD_REF_TYPE);
1063 new_name = strdup(refname);
1064 if (new_name == NULL)
1065 return got_error_from_errno("strdup");
1067 free(ref->ref.symref.ref);
1068 ref->ref.symref.ref = new_name;
1072 const struct got_error *
1073 got_ref_change_symref_to_ref(struct got_reference *symref,
1074 struct got_object_id *id)
1076 if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1077 return got_error(GOT_ERR_BAD_REF_TYPE);
1079 symref->ref.ref.name = symref->ref.symref.name;
1080 memcpy(symref->ref.ref.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1081 symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1085 const struct got_error *
1086 got_ref_write(struct got_reference *ref, struct got_repository *repo)
1088 const struct got_error *err = NULL, *unlock_err = NULL;
1089 const char *name = got_ref_get_name(ref);
1090 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1091 struct got_lockfile *lf = NULL;
1096 path_refs = get_refs_dir_path(repo, name);
1097 if (path_refs == NULL) {
1098 err = got_error_from_errno2("get_refs_dir_path", name);
1102 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1103 err = got_error_from_errno("asprintf");
1107 err = got_opentemp_named(&tmppath, &f, path);
1110 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1112 err = got_path_dirname(&parent, path);
1115 err = got_path_mkdir(parent);
1119 err = got_opentemp_named(&tmppath, &f, path);
1124 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1125 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1126 if (n != strlen(ref->ref.symref.ref) + 6) {
1127 err = got_ferror(f, GOT_ERR_IO);
1131 char hex[SHA1_DIGEST_STRING_LENGTH];
1132 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
1133 sizeof(hex)) == NULL) {
1134 err = got_error(GOT_ERR_BAD_REF_DATA);
1137 n = fprintf(f, "%s\n", hex);
1138 if (n != sizeof(hex)) {
1139 err = got_ferror(f, GOT_ERR_IO);
1144 if (ref->lf == NULL) {
1145 err = got_lockfile_lock(&lf, path);
1150 /* XXX: check if old content matches our expectations? */
1152 if (stat(path, &sb) != 0) {
1153 if (errno != ENOENT) {
1154 err = got_error_from_errno2("stat", path);
1157 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1160 if (rename(tmppath, path) != 0) {
1161 err = got_error_from_errno3("rename", tmppath, path);
1167 if (chmod(path, sb.st_mode) != 0) {
1168 err = got_error_from_errno2("chmod", path);
1172 if (ref->lf == NULL && lf)
1173 unlock_err = got_lockfile_unlock(lf);
1175 if (fclose(f) != 0 && err == NULL)
1176 err = got_error_from_errno("fclose");
1181 if (unlink(tmppath) != 0 && err == NULL)
1182 err = got_error_from_errno2("unlink", tmppath);
1185 return err ? err : unlock_err;
1188 static const struct got_error *
1189 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1191 const struct got_error *err = NULL, *unlock_err = NULL;
1192 struct got_lockfile *lf = NULL;
1193 FILE *f = NULL, *tmpf = NULL;
1194 char *packed_refs_path, *tmppath = NULL;
1195 struct got_reflist_head refs;
1196 int found_delref = 0;
1198 /* The packed-refs file does not cotain symbolic references. */
1199 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1200 return got_error(GOT_ERR_BAD_REF_DATA);
1202 SIMPLEQ_INIT(&refs);
1204 packed_refs_path = got_repo_get_path_packed_refs(repo);
1205 if (packed_refs_path == NULL)
1206 return got_error_from_errno("got_repo_get_path_packed_refs");
1208 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
1212 if (delref->lf == NULL) {
1213 err = got_lockfile_lock(&lf, packed_refs_path);
1218 f = fopen(packed_refs_path, "r");
1220 err = got_error_from_errno2("fopen", packed_refs_path);
1226 const char delim[3] = {'\0', '\0', '\0'};
1227 struct got_reference *ref;
1228 struct got_reflist_entry *new;
1230 line = fparseln(f, &len, NULL, delim, 0);
1234 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1237 err = parse_packed_ref_line(&ref, NULL, line);
1244 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1245 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1246 sizeof(delref->ref.ref.sha1)) == 0) {
1252 err = insert_ref(&new, &refs, ref, repo,
1253 got_ref_cmp_by_name, NULL);
1254 if (err || new == NULL /* duplicate */)
1261 struct got_reflist_entry *re;
1265 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1266 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1267 err = got_ferror(f, GOT_ERR_IO);
1271 SIMPLEQ_FOREACH(re, &refs, entry) {
1272 uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
1274 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1275 sizeof(hex)) == NULL) {
1276 err = got_error(GOT_ERR_BAD_REF_DATA);
1279 n = fprintf(tmpf, "%s ", hex);
1280 if (n != sizeof(hex)) {
1281 err = got_ferror(f, GOT_ERR_IO);
1284 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1285 if (n != strlen(re->ref->ref.ref.name) + 1) {
1286 err = got_ferror(f, GOT_ERR_IO);
1291 if (fflush(tmpf) != 0) {
1292 err = got_error_from_errno("fflush");
1296 if (stat(packed_refs_path, &sb) != 0) {
1297 if (errno != ENOENT) {
1298 err = got_error_from_errno2("stat",
1302 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1305 if (rename(tmppath, packed_refs_path) != 0) {
1306 err = got_error_from_errno3("rename", tmppath,
1311 if (chmod(packed_refs_path, sb.st_mode) != 0) {
1312 err = got_error_from_errno2("chmod",
1318 if (delref->lf == NULL && lf)
1319 unlock_err = got_lockfile_unlock(lf);
1321 if (fclose(f) != 0 && err == NULL)
1322 err = got_error_from_errno("fclose");
1326 if (fclose(tmpf) != 0 && err == NULL)
1327 err = got_error_from_errno("fclose");
1330 free(packed_refs_path);
1331 got_ref_list_free(&refs);
1332 return err ? err : unlock_err;
1335 const struct got_error *
1336 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1338 const struct got_error *err = NULL, *unlock_err = NULL;
1339 const char *name = got_ref_get_name(ref);
1340 char *path_refs = NULL, *path = NULL;
1341 struct got_lockfile *lf = NULL;
1343 if (ref->flags & GOT_REF_IS_PACKED)
1344 return delete_packed_ref(ref, repo);
1346 path_refs = get_refs_dir_path(repo, name);
1347 if (path_refs == NULL) {
1348 err = got_error_from_errno2("get_refs_dir_path", name);
1352 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1353 err = got_error_from_errno("asprintf");
1357 if (ref->lf == NULL) {
1358 err = got_lockfile_lock(&lf, path);
1363 /* XXX: check if old content matches our expectations? */
1365 if (unlink(path) != 0)
1366 err = got_error_from_errno2("unlink", path);
1368 if (ref->lf == NULL && lf)
1369 unlock_err = got_lockfile_unlock(lf);
1373 return err ? err : unlock_err;
1376 const struct got_error *
1377 got_ref_unlock(struct got_reference *ref)
1379 const struct got_error *err;
1380 err = got_lockfile_unlock(ref->lf);