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"
39 #include "got_lib_sha1.h"
40 #include "got_lib_path.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;
85 static const struct got_error *
86 alloc_ref(struct got_reference **ref, const char *name,
87 struct got_object_id *id, int flags)
89 const struct got_error *err = NULL;
91 *ref = calloc(1, sizeof(**ref));
93 return got_error_from_errno();
95 memcpy((*ref)->ref.ref.sha1, id->sha1, sizeof((*ref)->ref.ref.sha1));
96 (*ref)->flags = flags;
97 (*ref)->ref.ref.name = strdup(name);
98 if ((*ref)->ref.ref.name == NULL) {
99 err = got_error_from_errno();
106 static const struct got_error *
107 alloc_symref(struct got_reference **ref, const char *name,
108 const char *target_ref, int flags)
110 const struct got_error *err = NULL;
112 *ref = calloc(1, sizeof(**ref));
114 return got_error_from_errno();
116 (*ref)->flags = GOT_REF_IS_SYMBOLIC | flags;
117 (*ref)->ref.symref.name = strdup(name);
118 if ((*ref)->ref.symref.name == NULL) {
119 err = got_error_from_errno();
123 (*ref)->ref.symref.ref = strdup(target_ref);
124 if ((*ref)->ref.symref.ref == NULL) {
125 err = got_error_from_errno();
132 static const struct got_error *
133 parse_symref(struct got_reference **ref, const char *name, const char *line)
136 return got_error(GOT_ERR_BAD_REF_DATA);
138 return alloc_symref(ref, name, line, 0);
141 static const struct got_error *
142 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
144 struct got_object_id id;
146 if (strncmp(line, "ref: ", 5) == 0) {
148 return parse_symref(ref, name, line);
151 if (!got_parse_sha1_digest(id.sha1, line))
152 return got_error(GOT_ERR_BAD_REF_DATA);
154 return alloc_ref(ref, name, &id, 0);
157 static const struct got_error *
158 parse_ref_file(struct got_reference **ref, const char *name,
161 const struct got_error *err = NULL;
165 const char delim[3] = {'\0', '\0', '\0'};
167 f = fopen(abspath, "rb");
171 line = fparseln(f, &len, NULL, delim, 0);
173 err = got_error(GOT_ERR_BAD_REF_DATA);
177 err = parse_ref_line(ref, name, line);
180 if (fclose(f) != 0 && err == NULL)
181 err = got_error_from_errno();
186 is_well_known_ref(const char *refname)
188 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
189 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
190 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
191 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
195 get_refs_dir_path(struct got_repository *repo, const char *refname)
197 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
198 return strdup(got_repo_get_path_git_dir(repo));
200 return got_repo_get_path_refs(repo);
204 is_valid_ref_name(const char *name)
206 const char *s, *slash, *seg;
207 const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
208 const char *forbidden_seq[] = { "//", "..", "@{" };
209 const char *lfs = GOT_LOCKFILE_SUFFIX;
210 const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
213 if (name[0] == '@' && name[1] == '\0')
216 slash = strchr(name, '/');
222 if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
225 for (i = 0; i < nitems(forbidden); i++) {
226 if (*s == forbidden[i])
229 for (i = 0; i < nitems(forbidden_seq); i++) {
230 if (s[0] == forbidden_seq[i][0] &&
231 s[1] == forbidden_seq[i][1])
234 if (iscntrl((unsigned char)s[0]))
236 if (s[0] == '.' && s[1] == '\0')
239 const char *nextseg = s + 1;
240 if (nextseg[0] == '\0' || nextseg[0] == '.' ||
243 if (seg <= s - lfs_len &&
244 strncmp(s - lfs_len, lfs, lfs_len) == 0)
251 if (seg <= s - lfs_len &&
252 strncmp(s - lfs_len, lfs, lfs_len) == 0)
258 const struct got_error *
259 got_ref_alloc(struct got_reference **ref, const char *name,
260 struct got_object_id *id)
262 if (!is_valid_ref_name(name))
263 return got_error(GOT_ERR_BAD_REF_NAME);
265 return alloc_ref(ref, name, id, 0);
268 static const struct got_error *
269 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
272 struct got_object_id id;
277 if (line[0] == '#' || line[0] == '^')
280 if (!got_parse_sha1_digest(id.sha1, line))
281 return got_error(GOT_ERR_BAD_REF_DATA);
284 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
288 name = line + SHA1_DIGEST_STRING_LENGTH;
290 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED);
293 static const struct got_error *
294 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
295 int nsubdirs, const char *refname)
297 const struct got_error *err = NULL;
301 const char delim[3] = {'\0', '\0', '\0'};
302 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
307 abs_refname = (char *)refname;
309 line = fparseln(f, &len, NULL, delim, 0);
313 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
316 for (i = 0; i < nsubdirs; i++) {
317 if (!ref_is_absolute &&
318 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
320 return got_error_from_errno();
321 err = parse_packed_ref_line(ref, abs_refname, line);
322 if (!ref_is_absolute)
324 if (err || *ref != NULL)
330 } while (*ref == NULL);
335 static const struct got_error *
336 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
339 const struct got_error *err = NULL;
341 char *normpath = NULL;
342 char *absname = NULL;
343 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
344 int ref_is_well_known = is_well_known_ref(name);
348 if (ref_is_absolute || ref_is_well_known) {
349 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
350 return got_error_from_errno();
351 absname = (char *)name;
353 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
354 subdir[0] ? "/" : "", name) == -1)
355 return got_error_from_errno();
357 if (asprintf(&absname, "refs/%s%s%s",
358 subdir, subdir[0] ? "/" : "", name) == -1) {
359 err = got_error_from_errno();
364 normpath = got_path_normalize(path);
365 if (normpath == NULL) {
366 err = got_error_from_errno();
370 err = parse_ref_file(ref, absname, normpath);
372 if (!ref_is_absolute && !ref_is_well_known)
379 const struct got_error *
380 got_ref_open(struct got_reference **ref, struct got_repository *repo,
383 const struct got_error *err = NULL;
384 char *path_refs = NULL;
385 const char *subdirs[] = {
386 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
388 int i, well_known = is_well_known_ref(refname);
392 path_refs = get_refs_dir_path(repo, refname);
393 if (path_refs == NULL) {
394 err = got_error_from_errno();
399 char *packed_refs_path;
402 /* Search on-disk refs before packed refs! */
403 for (i = 0; i < nitems(subdirs); i++) {
404 err = open_ref(ref, path_refs, subdirs[i], refname);
409 packed_refs_path = got_repo_get_path_packed_refs(repo);
410 if (packed_refs_path == NULL) {
411 err = got_error_from_errno();
415 f = fopen(packed_refs_path, "rb");
416 free(packed_refs_path);
418 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
420 if (fclose(f) != 0 && err == NULL)
421 err = got_error_from_errno();
427 err = open_ref(ref, path_refs, "", refname);
432 err = got_error_not_ref(refname);
438 got_ref_close(struct got_reference *ref)
440 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
441 free(ref->ref.symref.name);
442 free(ref->ref.symref.ref);
444 free(ref->ref.ref.name);
448 struct got_reference *
449 got_ref_dup(struct got_reference *ref)
451 struct got_reference *ret;
453 ret = calloc(1, sizeof(*ret));
457 ret->flags = ref->flags;
458 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
459 ret->ref.symref.name = strdup(ref->ref.symref.name);
460 if (ret->ref.symref.name == NULL) {
464 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
465 if (ret->ref.symref.ref == NULL) {
466 free(ret->ref.symref.name);
471 ref->ref.ref.name = strdup(ref->ref.ref.name);
472 if (ref->ref.ref.name == NULL) {
476 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
477 sizeof(ret->ref.ref.sha1));
483 static const struct got_error *
484 resolve_symbolic_ref(struct got_reference **resolved,
485 struct got_repository *repo, struct got_reference *ref)
487 struct got_reference *nextref;
488 const struct got_error *err;
490 err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
494 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
495 err = resolve_symbolic_ref(resolved, repo, nextref);
497 *resolved = got_ref_dup(nextref);
499 got_ref_close(nextref);
503 const struct got_error *
504 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
505 struct got_reference *ref)
507 const struct got_error *err;
509 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
510 struct got_reference *resolved = NULL;
511 err = resolve_symbolic_ref(&resolved, repo, ref);
513 err = got_ref_resolve(id, repo, resolved);
515 got_ref_close(resolved);
519 *id = calloc(1, sizeof(**id));
521 return got_error_from_errno();
522 memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
527 got_ref_to_str(struct got_reference *ref)
531 if (ref->flags & GOT_REF_IS_SYMBOLIC)
532 return strdup(ref->ref.symref.ref);
534 str = malloc(SHA1_DIGEST_STRING_LENGTH);
538 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
539 SHA1_DIGEST_STRING_LENGTH) == NULL) {
548 got_ref_get_name(struct got_reference *ref)
550 if (ref->flags & GOT_REF_IS_SYMBOLIC)
551 return ref->ref.symref.name;
553 return ref->ref.ref.name;
556 static const struct got_error *
557 insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
558 struct got_reference *ref, struct got_repository *repo)
560 const struct got_error *err;
561 struct got_object_id *id;
562 struct got_reflist_entry *new, *re, *prev = NULL;
567 err = got_ref_resolve(&id, repo, ref);
571 new = malloc(sizeof(*new));
574 return got_error_from_errno();
581 * We must de-duplicate entries on insert because packed-refs may
582 * contain redundant entries. On-disk refs take precedence.
583 * This code assumes that on-disk revs are read before packed-refs.
584 * We're iterating the list anyway, so insert elements sorted by name.
586 re = SIMPLEQ_FIRST(refs);
588 cmp = got_path_cmp(got_ref_get_name(re->ref),
589 got_ref_get_name(new->ref));
596 } else if (cmp > 0) {
598 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
600 SIMPLEQ_INSERT_HEAD(refs, new, entry);
604 re = SIMPLEQ_NEXT(re, entry);
608 SIMPLEQ_INSERT_TAIL(refs, new, entry);
612 static const struct got_error *
613 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
614 const char *subdir, struct got_repository *repo)
616 const struct got_error *err = NULL;
620 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
621 return got_error_from_errno();
623 d = opendir(path_subdir);
629 struct got_reference *ref;
636 if (strcmp(dent->d_name, ".") == 0 ||
637 strcmp(dent->d_name, "..") == 0)
640 switch (dent->d_type) {
642 err = open_ref(&ref, path_refs, subdir, dent->d_name);
646 struct got_reflist_entry *new;
647 err = insert_ref(&new, refs, ref, repo);
648 if (err || new == NULL /* duplicate */)
655 if (asprintf(&child, "%s%s%s", subdir,
656 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
657 err = got_error_from_errno();
660 err = gather_on_disk_refs(refs, path_refs, child, repo);
674 const struct got_error *
675 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo)
677 const struct got_error *err;
678 char *packed_refs_path, *path_refs = NULL;
680 struct got_reference *ref;
681 struct got_reflist_entry *new;
683 /* HEAD ref should always exist. */
684 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
685 if (path_refs == NULL) {
686 err = got_error_from_errno();
689 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD);
692 err = insert_ref(&new, refs, ref, repo);
693 if (err || new == NULL /* duplicate */)
698 /* Gather on-disk refs before parsing packed-refs. */
700 path_refs = get_refs_dir_path(repo, "");
701 if (path_refs == NULL) {
702 err = got_error_from_errno();
705 err = gather_on_disk_refs(refs, path_refs, "", repo);
710 * The packed-refs file may contain redundant entries, in which
711 * case on-disk refs take precedence.
713 packed_refs_path = got_repo_get_path_packed_refs(repo);
714 if (packed_refs_path == NULL) {
715 err = got_error_from_errno();
719 f = fopen(packed_refs_path, "r");
720 free(packed_refs_path);
724 const char delim[3] = {'\0', '\0', '\0'};
726 line = fparseln(f, &len, NULL, delim, 0);
730 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
733 err = parse_packed_ref_line(&ref, NULL, line);
738 err = insert_ref(&new, refs, ref, repo);
739 if (err || new == NULL /* duplicate */)
748 if (f && fclose(f) != 0 && err == NULL)
749 err = got_error_from_errno();
754 got_ref_list_free(struct got_reflist_head *refs)
756 struct got_reflist_entry *re;
758 while (!SIMPLEQ_EMPTY(refs)) {
759 re = SIMPLEQ_FIRST(refs);
760 SIMPLEQ_REMOVE_HEAD(refs, entry);
761 got_ref_close(re->ref);
769 got_ref_is_symbolic(struct got_reference *ref)
771 return (ref->flags & GOT_REF_IS_SYMBOLIC);
774 const struct got_error *
775 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
777 if (ref->flags & GOT_REF_IS_SYMBOLIC)
778 return got_error(GOT_ERR_BAD_REF_TYPE);
780 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
784 const struct got_error *
785 got_ref_change_symref(struct got_reference *ref, char *refname)
789 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
790 return got_error(GOT_ERR_BAD_REF_TYPE);
792 new_name = strdup(refname);
793 if (new_name == NULL)
794 return got_error_from_errno();
796 free(ref->ref.symref.name);
797 ref->ref.symref.name = new_name;
801 const struct got_error *
802 got_ref_write(struct got_reference *ref, struct got_repository *repo)
804 const struct got_error *err = NULL, *unlock_err = NULL;
805 const char *name = got_ref_get_name(ref);
806 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
807 struct got_lockfile *lf = NULL;
812 path_refs = get_refs_dir_path(repo, name);
813 if (path_refs == NULL) {
814 err = got_error_from_errno();
818 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
819 err = got_error_from_errno();
823 err = got_opentemp_named(&tmppath, &f, path);
826 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
828 err = got_path_dirname(&parent, path);
831 err = got_path_mkdir(parent);
835 err = got_opentemp_named(&tmppath, &f, path);
840 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
841 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
842 if (n != strlen(ref->ref.symref.ref) + 6) {
843 err = got_ferror(f, GOT_ERR_IO);
847 char hex[SHA1_DIGEST_STRING_LENGTH];
848 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
849 sizeof(hex)) == NULL) {
850 err = got_error(GOT_ERR_BAD_REF_DATA);
853 n = fprintf(f, "%s\n", hex);
854 if (n != sizeof(hex)) {
855 err = got_ferror(f, GOT_ERR_IO);
860 err = got_lockfile_lock(&lf, path);
864 /* XXX: check if old content matches our expectations? */
866 if (stat(path, &sb) != 0) {
867 if (errno != ENOENT) {
868 err = got_error_from_errno();
871 sb.st_mode = GOT_DEFAULT_FILE_MODE;
874 if (rename(tmppath, path) != 0) {
875 err = got_error_from_errno();
881 if (chmod(path, sb.st_mode) != 0) {
882 err = got_error_from_errno();
887 unlock_err = got_lockfile_unlock(lf);
889 if (fclose(f) != 0 && err == NULL)
890 err = got_error_from_errno();
895 if (unlink(tmppath) != 0 && err == NULL)
896 err = got_error_from_errno();
899 return err ? err : unlock_err;
902 static const struct got_error *
903 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
905 const struct got_error *err = NULL, *unlock_err = NULL;
906 struct got_lockfile *lf = NULL;
907 FILE *f = NULL, *tmpf = NULL;
908 char *packed_refs_path, *tmppath = NULL;
909 struct got_reflist_head refs;
910 int found_delref = 0;
912 /* The packed-refs file does not cotain symbolic references. */
913 if (delref->flags & GOT_REF_IS_SYMBOLIC)
914 return got_error(GOT_ERR_BAD_REF_DATA);
918 packed_refs_path = got_repo_get_path_packed_refs(repo);
919 if (packed_refs_path == NULL)
920 return got_error_from_errno();
922 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
926 err = got_lockfile_lock(&lf, packed_refs_path);
930 f = fopen(packed_refs_path, "r");
932 err = got_error_from_errno();
938 const char delim[3] = {'\0', '\0', '\0'};
939 struct got_reference *ref;
940 struct got_reflist_entry *new;
942 line = fparseln(f, &len, NULL, delim, 0);
946 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
949 err = parse_packed_ref_line(&ref, NULL, line);
956 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
957 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
958 sizeof(delref->ref.ref.sha1)) == 0) {
964 err = insert_ref(&new, &refs, ref, repo);
965 if (err || new == NULL /* duplicate */)
972 struct got_reflist_entry *re;
976 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
977 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
978 err = got_ferror(f, GOT_ERR_IO);
982 SIMPLEQ_FOREACH(re, &refs, entry) {
983 uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
985 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
986 sizeof(hex)) == NULL) {
987 err = got_error(GOT_ERR_BAD_REF_DATA);
990 n = fprintf(tmpf, "%s ", hex);
991 if (n != sizeof(hex)) {
992 err = got_ferror(f, GOT_ERR_IO);
995 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
996 if (n != strlen(re->ref->ref.ref.name) + 1) {
997 err = got_ferror(f, GOT_ERR_IO);
1002 if (fflush(tmpf) != 0) {
1003 err = got_error_from_errno();
1007 if (stat(packed_refs_path, &sb) != 0) {
1008 if (errno != ENOENT) {
1009 err = got_error_from_errno();
1012 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1015 if (rename(tmppath, packed_refs_path) != 0) {
1016 err = got_error_from_errno();
1020 if (chmod(packed_refs_path, sb.st_mode) != 0) {
1021 err = got_error_from_errno();
1027 unlock_err = got_lockfile_unlock(lf);
1029 if (fclose(f) != 0 && err == NULL)
1030 err = got_error_from_errno();
1034 if (fclose(tmpf) != 0 && err == NULL)
1035 err = got_error_from_errno();
1038 free(packed_refs_path);
1039 got_ref_list_free(&refs);
1040 return err ? err : unlock_err;
1043 const struct got_error *
1044 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1046 const struct got_error *err = NULL, *unlock_err = NULL;
1047 const char *name = got_ref_get_name(ref);
1048 char *path_refs = NULL, *path = NULL;
1049 struct got_lockfile *lf = NULL;
1051 if (ref->flags & GOT_REF_IS_PACKED)
1052 return delete_packed_ref(ref, repo);
1054 path_refs = get_refs_dir_path(repo, name);
1055 if (path_refs == NULL) {
1056 err = got_error_from_errno();
1060 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1061 err = got_error_from_errno();
1065 err = got_lockfile_lock(&lf, path);
1069 /* XXX: check if old content matches our expectations? */
1071 if (unlink(path) != 0)
1072 err = got_error_from_errno();
1075 unlock_err = got_lockfile_unlock(lf);
1079 return err ? err : unlock_err;