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>
33 #include "got_error.h"
34 #include "got_object.h"
35 #include "got_repository.h"
36 #include "got_reference.h"
37 #include "got_opentemp.h"
40 #include "got_lib_sha1.h"
41 #include "got_lib_delta.h"
42 #include "got_lib_inflate.h"
43 #include "got_lib_object.h"
44 #include "got_lib_lockfile.h"
47 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
50 #define GOT_REF_HEADS "heads"
51 #define GOT_REF_TAGS "tags"
52 #define GOT_REF_REMOTES "remotes"
55 * We do not resolve tags yet, and don't yet care about sorting refs either,
56 * so packed-refs files we write contain a minimal header which disables all
57 * packed-refs "traits" supported by Git.
59 #define GOT_PACKED_REFS_HEADER "# pack-refs with:"
61 /* A symbolic reference. */
67 /* A non-symbolic reference (there is no better designation). */
70 u_int8_t sha1[SHA1_DIGEST_LENGTH];
73 /* A reference which points to an arbitrary object. */
74 struct got_reference {
76 #define GOT_REF_IS_SYMBOLIC 0x01
77 #define GOT_REF_IS_PACKED 0x02
81 struct got_symref symref;
84 struct got_lockfile *lf;
87 static const struct got_error *
88 alloc_ref(struct got_reference **ref, const char *name,
89 struct got_object_id *id, int flags)
91 const struct got_error *err = NULL;
93 *ref = calloc(1, sizeof(**ref));
95 return got_error_from_errno("calloc");
97 memcpy((*ref)->ref.ref.sha1, id->sha1, sizeof((*ref)->ref.ref.sha1));
98 (*ref)->flags = flags;
99 (*ref)->ref.ref.name = strdup(name);
100 if ((*ref)->ref.ref.name == NULL) {
101 err = got_error_from_errno("strdup");
108 static const struct got_error *
109 alloc_symref(struct got_reference **ref, const char *name,
110 const char *target_ref, int flags)
112 const struct got_error *err = NULL;
114 *ref = calloc(1, sizeof(**ref));
116 return got_error_from_errno("calloc");
118 (*ref)->flags = GOT_REF_IS_SYMBOLIC | flags;
119 (*ref)->ref.symref.name = strdup(name);
120 if ((*ref)->ref.symref.name == NULL) {
121 err = got_error_from_errno("strdup");
125 (*ref)->ref.symref.ref = strdup(target_ref);
126 if ((*ref)->ref.symref.ref == NULL) {
127 err = got_error_from_errno("strdup");
134 static const struct got_error *
135 parse_symref(struct got_reference **ref, const char *name, const char *line)
138 return got_error(GOT_ERR_BAD_REF_DATA);
140 return alloc_symref(ref, name, line, 0);
143 static const struct got_error *
144 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
146 struct got_object_id id;
148 if (strncmp(line, "ref: ", 5) == 0) {
150 return parse_symref(ref, name, line);
153 if (!got_parse_sha1_digest(id.sha1, line))
154 return got_error(GOT_ERR_BAD_REF_DATA);
156 return alloc_ref(ref, name, &id, 0);
159 static const struct got_error *
160 parse_ref_file(struct got_reference **ref, const char *name,
161 const char *abspath, int lock)
163 const struct got_error *err = NULL;
167 const char delim[3] = {'\0', '\0', '\0'};
168 struct got_lockfile *lf = NULL;
171 err = got_lockfile_lock(&lf, abspath);
176 f = fopen(abspath, "rb");
179 got_lockfile_unlock(lf);
183 line = fparseln(f, &len, NULL, delim, 0);
185 err = got_error(GOT_ERR_BAD_REF_DATA);
187 got_lockfile_unlock(lf);
191 err = parse_ref_line(ref, name, line);
194 got_lockfile_unlock(lf);
199 got_lockfile_unlock(lf);
204 if (fclose(f) != 0 && err == NULL) {
205 err = got_error_from_errno("fclose");
208 got_ref_unlock(*ref);
217 is_well_known_ref(const char *refname)
219 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
220 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
221 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
222 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
226 get_refs_dir_path(struct got_repository *repo, const char *refname)
228 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
229 return strdup(got_repo_get_path_git_dir(repo));
231 return got_repo_get_path_refs(repo);
235 is_valid_ref_name(const char *name)
237 const char *s, *slash, *seg;
238 const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
239 const char *forbidden_seq[] = { "//", "..", "@{" };
240 const char *lfs = GOT_LOCKFILE_SUFFIX;
241 const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
244 if (name[0] == '@' && name[1] == '\0')
247 slash = strchr(name, '/');
253 if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
256 for (i = 0; i < nitems(forbidden); i++) {
257 if (*s == forbidden[i])
260 for (i = 0; i < nitems(forbidden_seq); i++) {
261 if (s[0] == forbidden_seq[i][0] &&
262 s[1] == forbidden_seq[i][1])
265 if (iscntrl((unsigned char)s[0]))
267 if (s[0] == '.' && s[1] == '\0')
270 const char *nextseg = s + 1;
271 if (nextseg[0] == '\0' || nextseg[0] == '.' ||
274 if (seg <= s - lfs_len &&
275 strncmp(s - lfs_len, lfs, lfs_len) == 0)
282 if (seg <= s - lfs_len &&
283 strncmp(s - lfs_len, lfs, lfs_len) == 0)
289 const struct got_error *
290 got_ref_alloc(struct got_reference **ref, const char *name,
291 struct got_object_id *id)
293 if (!is_valid_ref_name(name))
294 return got_error(GOT_ERR_BAD_REF_NAME);
296 return alloc_ref(ref, name, id, 0);
299 const struct got_error *
300 got_ref_alloc_symref(struct got_reference **ref, const char *name,
301 struct got_reference *target_ref)
303 if (!is_valid_ref_name(name))
304 return got_error(GOT_ERR_BAD_REF_NAME);
306 return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
309 static const struct got_error *
310 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
313 struct got_object_id id;
318 if (line[0] == '#' || line[0] == '^')
321 if (!got_parse_sha1_digest(id.sha1, line))
322 return got_error(GOT_ERR_BAD_REF_DATA);
325 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
329 name = line + SHA1_DIGEST_STRING_LENGTH;
331 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED);
334 static const struct got_error *
335 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
336 int nsubdirs, const char *refname)
338 const struct got_error *err = NULL;
342 const char delim[3] = {'\0', '\0', '\0'};
343 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
348 abs_refname = (char *)refname;
350 line = fparseln(f, &len, NULL, delim, 0);
354 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
357 for (i = 0; i < nsubdirs; i++) {
358 if (!ref_is_absolute &&
359 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
361 return got_error_from_errno("asprintf");
362 err = parse_packed_ref_line(ref, abs_refname, line);
363 if (!ref_is_absolute)
365 if (err || *ref != NULL)
371 } while (*ref == NULL);
376 static const struct got_error *
377 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
378 const char *name, int lock)
380 const struct got_error *err = NULL;
382 char *normpath = NULL;
383 char *absname = NULL;
384 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
385 int ref_is_well_known = is_well_known_ref(name);
389 if (ref_is_absolute || ref_is_well_known) {
390 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
391 return got_error_from_errno("asprintf");
392 absname = (char *)name;
394 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
395 subdir[0] ? "/" : "", name) == -1)
396 return got_error_from_errno("asprintf");
398 if (asprintf(&absname, "refs/%s%s%s",
399 subdir, subdir[0] ? "/" : "", name) == -1) {
400 err = got_error_from_errno("asprintf");
405 normpath = got_path_normalize(path);
406 if (normpath == NULL) {
410 err = got_error_from_errno2("got_path_normalize", path);
414 err = parse_ref_file(ref, absname, normpath, lock);
416 if (!ref_is_absolute && !ref_is_well_known)
423 const struct got_error *
424 got_ref_open(struct got_reference **ref, struct got_repository *repo,
425 const char *refname, int lock)
427 const struct got_error *err = NULL;
428 char *path_refs = NULL;
429 const char *subdirs[] = {
430 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
432 int i, well_known = is_well_known_ref(refname);
433 struct got_lockfile *lf = NULL;
437 path_refs = get_refs_dir_path(repo, refname);
438 if (path_refs == NULL) {
439 err = got_error_from_errno2("get_refs_dir_path", refname);
444 err = open_ref(ref, path_refs, "", refname, lock);
446 char *packed_refs_path;
449 /* Search on-disk refs before packed refs! */
450 for (i = 0; i < nitems(subdirs); i++) {
451 err = open_ref(ref, path_refs, subdirs[i], refname,
457 packed_refs_path = got_repo_get_path_packed_refs(repo);
458 if (packed_refs_path == NULL) {
459 err = got_error_from_errno(
460 "got_repo_get_path_packed_refs");
465 err = got_lockfile_lock(&lf, packed_refs_path);
469 f = fopen(packed_refs_path, "rb");
470 free(packed_refs_path);
472 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
475 if (fclose(f) != 0) {
476 err = got_error_from_errno("fclose");
485 if (!err && *ref == NULL)
486 err = got_error_not_ref(refname);
488 got_lockfile_unlock(lf);
494 got_ref_close(struct got_reference *ref)
496 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
497 free(ref->ref.symref.name);
498 free(ref->ref.symref.ref);
500 free(ref->ref.ref.name);
504 struct got_reference *
505 got_ref_dup(struct got_reference *ref)
507 struct got_reference *ret;
509 ret = calloc(1, sizeof(*ret));
513 ret->flags = ref->flags;
514 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
515 ret->ref.symref.name = strdup(ref->ref.symref.name);
516 if (ret->ref.symref.name == NULL) {
520 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
521 if (ret->ref.symref.ref == NULL) {
522 free(ret->ref.symref.name);
527 ref->ref.ref.name = strdup(ref->ref.ref.name);
528 if (ref->ref.ref.name == NULL) {
532 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
533 sizeof(ret->ref.ref.sha1));
539 static const struct got_error *
540 resolve_symbolic_ref(struct got_reference **resolved,
541 struct got_repository *repo, struct got_reference *ref)
543 struct got_reference *nextref;
544 const struct got_error *err;
546 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
550 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
551 err = resolve_symbolic_ref(resolved, repo, nextref);
553 *resolved = got_ref_dup(nextref);
555 got_ref_close(nextref);
559 const struct got_error *
560 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
561 struct got_reference *ref)
563 const struct got_error *err;
565 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
566 struct got_reference *resolved = NULL;
567 err = resolve_symbolic_ref(&resolved, repo, ref);
569 err = got_ref_resolve(id, repo, resolved);
571 got_ref_close(resolved);
575 *id = calloc(1, sizeof(**id));
577 return got_error_from_errno("calloc");
578 memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
583 got_ref_to_str(struct got_reference *ref)
587 if (ref->flags & GOT_REF_IS_SYMBOLIC)
588 return strdup(ref->ref.symref.ref);
590 str = malloc(SHA1_DIGEST_STRING_LENGTH);
594 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
595 SHA1_DIGEST_STRING_LENGTH) == NULL) {
604 got_ref_get_name(struct got_reference *ref)
606 if (ref->flags & GOT_REF_IS_SYMBOLIC)
607 return ref->ref.symref.name;
609 return ref->ref.ref.name;
613 got_ref_get_symref_target(struct got_reference *ref)
615 if (ref->flags & GOT_REF_IS_SYMBOLIC)
616 return ref->ref.symref.ref;
621 static const struct got_error *
622 insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
623 struct got_reference *ref, struct got_repository *repo)
625 const struct got_error *err;
626 struct got_object_id *id;
627 struct got_reflist_entry *new, *re, *prev = NULL;
632 err = got_ref_resolve(&id, repo, ref);
636 new = malloc(sizeof(*new));
639 return got_error_from_errno("malloc");
646 * We must de-duplicate entries on insert because packed-refs may
647 * contain redundant entries. On-disk refs take precedence.
648 * This code assumes that on-disk revs are read before packed-refs.
649 * We're iterating the list anyway, so insert elements sorted by name.
651 re = SIMPLEQ_FIRST(refs);
653 cmp = got_path_cmp(got_ref_get_name(re->ref),
654 got_ref_get_name(new->ref));
661 } else if (cmp > 0) {
663 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
665 SIMPLEQ_INSERT_HEAD(refs, new, entry);
669 re = SIMPLEQ_NEXT(re, entry);
673 SIMPLEQ_INSERT_TAIL(refs, new, entry);
677 static const struct got_error *
678 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
679 const char *subdir, struct got_repository *repo)
681 const struct got_error *err = NULL;
685 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
686 return got_error_from_errno("asprintf");
688 d = opendir(path_subdir);
694 struct got_reference *ref;
701 if (strcmp(dent->d_name, ".") == 0 ||
702 strcmp(dent->d_name, "..") == 0)
705 switch (dent->d_type) {
707 err = open_ref(&ref, path_refs, subdir, dent->d_name,
712 struct got_reflist_entry *new;
713 err = insert_ref(&new, refs, ref, repo);
714 if (err || new == NULL /* duplicate */)
721 if (asprintf(&child, "%s%s%s", subdir,
722 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
723 err = got_error_from_errno("asprintf");
726 err = gather_on_disk_refs(refs, path_refs, child, repo);
740 const struct got_error *
741 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo)
743 const struct got_error *err;
744 char *packed_refs_path, *path_refs = NULL;
746 struct got_reference *ref;
747 struct got_reflist_entry *new;
749 /* HEAD ref should always exist. */
750 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
751 if (path_refs == NULL) {
752 err = got_error_from_errno("get_refs_dir_path");
755 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
758 err = insert_ref(&new, refs, ref, repo);
759 if (err || new == NULL /* duplicate */)
764 /* Gather on-disk refs before parsing packed-refs. */
766 path_refs = get_refs_dir_path(repo, "");
767 if (path_refs == NULL) {
768 err = got_error_from_errno("get_refs_dir_path");
771 err = gather_on_disk_refs(refs, path_refs, "", repo);
776 * The packed-refs file may contain redundant entries, in which
777 * case on-disk refs take precedence.
779 packed_refs_path = got_repo_get_path_packed_refs(repo);
780 if (packed_refs_path == NULL) {
781 err = got_error_from_errno("got_repo_get_path_packed_refs");
785 f = fopen(packed_refs_path, "r");
786 free(packed_refs_path);
790 const char delim[3] = {'\0', '\0', '\0'};
792 line = fparseln(f, &len, NULL, delim, 0);
796 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
799 err = parse_packed_ref_line(&ref, NULL, line);
804 err = insert_ref(&new, refs, ref, repo);
805 if (err || new == NULL /* duplicate */)
814 if (f && fclose(f) != 0 && err == NULL)
815 err = got_error_from_errno("fclose");
820 got_ref_list_free(struct got_reflist_head *refs)
822 struct got_reflist_entry *re;
824 while (!SIMPLEQ_EMPTY(refs)) {
825 re = SIMPLEQ_FIRST(refs);
826 SIMPLEQ_REMOVE_HEAD(refs, entry);
827 got_ref_close(re->ref);
835 got_ref_is_symbolic(struct got_reference *ref)
837 return (ref->flags & GOT_REF_IS_SYMBOLIC);
840 const struct got_error *
841 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
843 if (ref->flags & GOT_REF_IS_SYMBOLIC)
844 return got_error(GOT_ERR_BAD_REF_TYPE);
846 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
850 const struct got_error *
851 got_ref_change_symref(struct got_reference *ref, char *refname)
855 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
856 return got_error(GOT_ERR_BAD_REF_TYPE);
858 new_name = strdup(refname);
859 if (new_name == NULL)
860 return got_error_from_errno("strdup");
862 free(ref->ref.symref.name);
863 ref->ref.symref.name = new_name;
867 const struct got_error *
868 got_ref_write(struct got_reference *ref, struct got_repository *repo)
870 const struct got_error *err = NULL, *unlock_err = NULL;
871 const char *name = got_ref_get_name(ref);
872 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
873 struct got_lockfile *lf = NULL;
878 path_refs = get_refs_dir_path(repo, name);
879 if (path_refs == NULL) {
880 err = got_error_from_errno2("get_refs_dir_path", name);
884 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
885 err = got_error_from_errno("asprintf");
889 err = got_opentemp_named(&tmppath, &f, path);
892 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
894 err = got_path_dirname(&parent, path);
897 err = got_path_mkdir(parent);
901 err = got_opentemp_named(&tmppath, &f, path);
906 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
907 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
908 if (n != strlen(ref->ref.symref.ref) + 6) {
909 err = got_ferror(f, GOT_ERR_IO);
913 char hex[SHA1_DIGEST_STRING_LENGTH];
914 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
915 sizeof(hex)) == NULL) {
916 err = got_error(GOT_ERR_BAD_REF_DATA);
919 n = fprintf(f, "%s\n", hex);
920 if (n != sizeof(hex)) {
921 err = got_ferror(f, GOT_ERR_IO);
926 if (ref->lf == NULL) {
927 err = got_lockfile_lock(&lf, path);
932 /* XXX: check if old content matches our expectations? */
934 if (stat(path, &sb) != 0) {
935 if (errno != ENOENT) {
936 err = got_error_from_errno2("stat", path);
939 sb.st_mode = GOT_DEFAULT_FILE_MODE;
942 if (rename(tmppath, path) != 0) {
943 err = got_error_from_errno3("rename", tmppath, path);
949 if (chmod(path, sb.st_mode) != 0) {
950 err = got_error_from_errno2("chmod", path);
954 if (ref->lf == NULL && lf)
955 unlock_err = got_lockfile_unlock(lf);
957 if (fclose(f) != 0 && err == NULL)
958 err = got_error_from_errno("fclose");
963 if (unlink(tmppath) != 0 && err == NULL)
964 err = got_error_from_errno2("unlink", tmppath);
967 return err ? err : unlock_err;
970 static const struct got_error *
971 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
973 const struct got_error *err = NULL, *unlock_err = NULL;
974 struct got_lockfile *lf = NULL;
975 FILE *f = NULL, *tmpf = NULL;
976 char *packed_refs_path, *tmppath = NULL;
977 struct got_reflist_head refs;
978 int found_delref = 0;
980 /* The packed-refs file does not cotain symbolic references. */
981 if (delref->flags & GOT_REF_IS_SYMBOLIC)
982 return got_error(GOT_ERR_BAD_REF_DATA);
986 packed_refs_path = got_repo_get_path_packed_refs(repo);
987 if (packed_refs_path == NULL)
988 return got_error_from_errno("got_repo_get_path_packed_refs");
990 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
994 if (delref->lf == NULL) {
995 err = got_lockfile_lock(&lf, packed_refs_path);
1000 f = fopen(packed_refs_path, "r");
1002 err = got_error_from_errno2("fopen", packed_refs_path);
1008 const char delim[3] = {'\0', '\0', '\0'};
1009 struct got_reference *ref;
1010 struct got_reflist_entry *new;
1012 line = fparseln(f, &len, NULL, delim, 0);
1016 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1019 err = parse_packed_ref_line(&ref, NULL, line);
1026 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1027 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1028 sizeof(delref->ref.ref.sha1)) == 0) {
1034 err = insert_ref(&new, &refs, ref, repo);
1035 if (err || new == NULL /* duplicate */)
1042 struct got_reflist_entry *re;
1046 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1047 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1048 err = got_ferror(f, GOT_ERR_IO);
1052 SIMPLEQ_FOREACH(re, &refs, entry) {
1053 uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
1055 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1056 sizeof(hex)) == NULL) {
1057 err = got_error(GOT_ERR_BAD_REF_DATA);
1060 n = fprintf(tmpf, "%s ", hex);
1061 if (n != sizeof(hex)) {
1062 err = got_ferror(f, GOT_ERR_IO);
1065 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1066 if (n != strlen(re->ref->ref.ref.name) + 1) {
1067 err = got_ferror(f, GOT_ERR_IO);
1072 if (fflush(tmpf) != 0) {
1073 err = got_error_from_errno("fflush");
1077 if (stat(packed_refs_path, &sb) != 0) {
1078 if (errno != ENOENT) {
1079 err = got_error_from_errno2("stat",
1083 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1086 if (rename(tmppath, packed_refs_path) != 0) {
1087 err = got_error_from_errno3("rename", tmppath,
1092 if (chmod(packed_refs_path, sb.st_mode) != 0) {
1093 err = got_error_from_errno2("chmod",
1099 if (delref->lf == NULL && lf)
1100 unlock_err = got_lockfile_unlock(lf);
1102 if (fclose(f) != 0 && err == NULL)
1103 err = got_error_from_errno("fclose");
1107 if (fclose(tmpf) != 0 && err == NULL)
1108 err = got_error_from_errno("fclose");
1111 free(packed_refs_path);
1112 got_ref_list_free(&refs);
1113 return err ? err : unlock_err;
1116 const struct got_error *
1117 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1119 const struct got_error *err = NULL, *unlock_err = NULL;
1120 const char *name = got_ref_get_name(ref);
1121 char *path_refs = NULL, *path = NULL;
1122 struct got_lockfile *lf = NULL;
1124 if (ref->flags & GOT_REF_IS_PACKED)
1125 return delete_packed_ref(ref, repo);
1127 path_refs = get_refs_dir_path(repo, name);
1128 if (path_refs == NULL) {
1129 err = got_error_from_errno2("get_refs_dir_path", name);
1133 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1134 err = got_error_from_errno("asprintf");
1138 if (ref->lf == NULL) {
1139 err = got_lockfile_lock(&lf, path);
1144 /* XXX: check if old content matches our expectations? */
1146 if (unlink(path) != 0)
1147 err = got_error_from_errno2("unlink", path);
1149 if (ref->lf == NULL && lf)
1150 unlock_err = got_lockfile_unlock(lf);
1154 return err ? err : unlock_err;
1157 const struct got_error *
1158 got_ref_unlock(struct got_reference *ref)
1160 const struct got_error *err;
1161 err = got_lockfile_unlock(ref->lf);