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 #define GOT_REF_RECURSE_MAX 20
69 /* A non-symbolic reference (there is no better designation). */
72 u_int8_t sha1[SHA1_DIGEST_LENGTH];
75 /* A reference which points to an arbitrary object. */
76 struct got_reference {
78 #define GOT_REF_IS_SYMBOLIC 0x01
79 #define GOT_REF_IS_PACKED 0x02
83 struct got_symref symref;
86 struct got_lockfile *lf;
89 static const struct got_error *
90 alloc_ref(struct got_reference **ref, const char *name,
91 struct got_object_id *id, int flags)
93 const struct got_error *err = NULL;
95 *ref = calloc(1, sizeof(**ref));
97 return got_error_from_errno("calloc");
99 memcpy((*ref)->ref.ref.sha1, id->sha1, sizeof((*ref)->ref.ref.sha1));
100 (*ref)->flags = flags;
101 (*ref)->ref.ref.name = strdup(name);
102 if ((*ref)->ref.ref.name == NULL) {
103 err = got_error_from_errno("strdup");
110 static const struct got_error *
111 alloc_symref(struct got_reference **ref, const char *name,
112 const char *target_ref, int flags)
114 const struct got_error *err = NULL;
116 *ref = calloc(1, sizeof(**ref));
118 return got_error_from_errno("calloc");
120 (*ref)->flags = GOT_REF_IS_SYMBOLIC | flags;
121 (*ref)->ref.symref.name = strdup(name);
122 if ((*ref)->ref.symref.name == NULL) {
123 err = got_error_from_errno("strdup");
127 (*ref)->ref.symref.ref = strdup(target_ref);
128 if ((*ref)->ref.symref.ref == NULL) {
129 err = got_error_from_errno("strdup");
136 static const struct got_error *
137 parse_symref(struct got_reference **ref, const char *name, const char *line)
140 return got_error(GOT_ERR_BAD_REF_DATA);
142 return alloc_symref(ref, name, line, 0);
145 static const struct got_error *
146 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
148 struct got_object_id id;
150 if (strncmp(line, "ref: ", 5) == 0) {
152 return parse_symref(ref, name, line);
155 if (!got_parse_sha1_digest(id.sha1, line))
156 return got_error(GOT_ERR_BAD_REF_DATA);
158 return alloc_ref(ref, name, &id, 0);
161 static const struct got_error *
162 parse_ref_file(struct got_reference **ref, const char *name,
163 const char *abspath, int lock)
165 const struct got_error *err = NULL;
169 const char delim[3] = {'\0', '\0', '\0'};
170 struct got_lockfile *lf = NULL;
173 err = got_lockfile_lock(&lf, abspath);
178 f = fopen(abspath, "rb");
181 got_lockfile_unlock(lf);
185 line = fparseln(f, &len, NULL, delim, 0);
187 err = got_error(GOT_ERR_BAD_REF_DATA);
189 got_lockfile_unlock(lf);
193 err = parse_ref_line(ref, name, line);
196 got_lockfile_unlock(lf);
201 got_lockfile_unlock(lf);
206 if (fclose(f) != 0 && err == NULL) {
207 err = got_error_from_errno("fclose");
210 got_ref_unlock(*ref);
219 is_well_known_ref(const char *refname)
221 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
222 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
223 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
224 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
228 get_refs_dir_path(struct got_repository *repo, const char *refname)
230 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
231 return strdup(got_repo_get_path_git_dir(repo));
233 return got_repo_get_path_refs(repo);
237 is_valid_ref_name(const char *name)
239 const char *s, *slash, *seg;
240 const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
241 const char *forbidden_seq[] = { "//", "..", "@{" };
242 const char *lfs = GOT_LOCKFILE_SUFFIX;
243 const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
246 if (name[0] == '@' && name[1] == '\0')
249 slash = strchr(name, '/');
255 if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
258 for (i = 0; i < nitems(forbidden); i++) {
259 if (*s == forbidden[i])
262 for (i = 0; i < nitems(forbidden_seq); i++) {
263 if (s[0] == forbidden_seq[i][0] &&
264 s[1] == forbidden_seq[i][1])
267 if (iscntrl((unsigned char)s[0]))
269 if (s[0] == '.' && s[1] == '\0')
272 const char *nextseg = s + 1;
273 if (nextseg[0] == '\0' || nextseg[0] == '.' ||
276 if (seg <= s - lfs_len &&
277 strncmp(s - lfs_len, lfs, lfs_len) == 0)
284 if (seg <= s - lfs_len &&
285 strncmp(s - lfs_len, lfs, lfs_len) == 0)
291 const struct got_error *
292 got_ref_alloc(struct got_reference **ref, const char *name,
293 struct got_object_id *id)
295 if (!is_valid_ref_name(name))
296 return got_error(GOT_ERR_BAD_REF_NAME);
298 return alloc_ref(ref, name, id, 0);
301 const struct got_error *
302 got_ref_alloc_symref(struct got_reference **ref, const char *name,
303 struct got_reference *target_ref)
305 if (!is_valid_ref_name(name))
306 return got_error(GOT_ERR_BAD_REF_NAME);
308 return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
311 static const struct got_error *
312 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
315 struct got_object_id id;
320 if (line[0] == '#' || line[0] == '^')
323 if (!got_parse_sha1_digest(id.sha1, line))
324 return got_error(GOT_ERR_BAD_REF_DATA);
327 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
331 name = line + SHA1_DIGEST_STRING_LENGTH;
333 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED);
336 static const struct got_error *
337 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
338 int nsubdirs, const char *refname)
340 const struct got_error *err = NULL;
344 const char delim[3] = {'\0', '\0', '\0'};
345 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
350 abs_refname = (char *)refname;
352 line = fparseln(f, &len, NULL, delim, 0);
356 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
359 for (i = 0; i < nsubdirs; i++) {
360 if (!ref_is_absolute &&
361 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
363 return got_error_from_errno("asprintf");
364 err = parse_packed_ref_line(ref, abs_refname, line);
365 if (!ref_is_absolute)
367 if (err || *ref != NULL)
373 } while (*ref == NULL);
378 static const struct got_error *
379 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
380 const char *name, int lock)
382 const struct got_error *err = NULL;
384 char *absname = NULL;
385 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
386 int ref_is_well_known = is_well_known_ref(name);
390 if (ref_is_absolute || ref_is_well_known) {
391 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
392 return got_error_from_errno("asprintf");
393 absname = (char *)name;
395 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
396 subdir[0] ? "/" : "", name) == -1)
397 return got_error_from_errno("asprintf");
399 if (asprintf(&absname, "refs/%s%s%s",
400 subdir, subdir[0] ? "/" : "", name) == -1) {
401 err = got_error_from_errno("asprintf");
406 err = parse_ref_file(ref, absname, path, lock);
408 if (!ref_is_absolute && !ref_is_well_known)
414 const struct got_error *
415 got_ref_open(struct got_reference **ref, struct got_repository *repo,
416 const char *refname, int lock)
418 const struct got_error *err = NULL;
419 char *path_refs = NULL;
420 const char *subdirs[] = {
421 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
423 int i, well_known = is_well_known_ref(refname);
424 struct got_lockfile *lf = NULL;
428 path_refs = get_refs_dir_path(repo, refname);
429 if (path_refs == NULL) {
430 err = got_error_from_errno2("get_refs_dir_path", refname);
435 err = open_ref(ref, path_refs, "", refname, lock);
437 char *packed_refs_path;
440 /* Search on-disk refs before packed refs! */
441 for (i = 0; i < nitems(subdirs); i++) {
442 err = open_ref(ref, path_refs, subdirs[i], refname,
448 packed_refs_path = got_repo_get_path_packed_refs(repo);
449 if (packed_refs_path == NULL) {
450 err = got_error_from_errno(
451 "got_repo_get_path_packed_refs");
456 err = got_lockfile_lock(&lf, packed_refs_path);
460 f = fopen(packed_refs_path, "rb");
461 free(packed_refs_path);
463 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
466 if (fclose(f) != 0) {
467 err = got_error_from_errno("fclose");
476 if (!err && *ref == NULL)
477 err = got_error_not_ref(refname);
479 got_lockfile_unlock(lf);
485 got_ref_close(struct got_reference *ref)
487 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
488 free(ref->ref.symref.name);
489 free(ref->ref.symref.ref);
491 free(ref->ref.ref.name);
495 struct got_reference *
496 got_ref_dup(struct got_reference *ref)
498 struct got_reference *ret;
500 ret = calloc(1, sizeof(*ret));
504 ret->flags = ref->flags;
505 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
506 ret->ref.symref.name = strdup(ref->ref.symref.name);
507 if (ret->ref.symref.name == NULL) {
511 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
512 if (ret->ref.symref.ref == NULL) {
513 free(ret->ref.symref.name);
518 ref->ref.ref.name = strdup(ref->ref.ref.name);
519 if (ref->ref.ref.name == NULL) {
523 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
524 sizeof(ret->ref.ref.sha1));
530 static const struct got_error *
531 resolve_symbolic_ref(struct got_reference **resolved,
532 struct got_repository *repo, struct got_reference *ref)
534 struct got_reference *nextref;
535 const struct got_error *err;
537 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
541 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
542 err = resolve_symbolic_ref(resolved, repo, nextref);
544 *resolved = got_ref_dup(nextref);
546 got_ref_close(nextref);
550 static const struct got_error *
551 ref_resolve(struct got_object_id **id, struct got_repository *repo,
552 struct got_reference *ref, int recursion)
554 const struct got_error *err;
557 return got_error_msg(GOT_ERR_RECURSION,
558 "reference recursion limit reached");
560 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
561 struct got_reference *resolved = NULL;
562 err = resolve_symbolic_ref(&resolved, repo, ref);
564 err = ref_resolve(id, repo, resolved, --recursion);
566 got_ref_close(resolved);
570 *id = calloc(1, sizeof(**id));
572 return got_error_from_errno("calloc");
573 memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
577 const struct got_error *
578 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
579 struct got_reference *ref)
581 return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
585 got_ref_to_str(struct got_reference *ref)
589 if (ref->flags & GOT_REF_IS_SYMBOLIC)
590 return strdup(ref->ref.symref.ref);
592 str = malloc(SHA1_DIGEST_STRING_LENGTH);
596 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
597 SHA1_DIGEST_STRING_LENGTH) == NULL) {
606 got_ref_get_name(struct got_reference *ref)
608 if (ref->flags & GOT_REF_IS_SYMBOLIC)
609 return ref->ref.symref.name;
611 return ref->ref.ref.name;
615 got_ref_get_symref_target(struct got_reference *ref)
617 if (ref->flags & GOT_REF_IS_SYMBOLIC)
618 return ref->ref.symref.ref;
623 static const struct got_error *
624 insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
625 struct got_reference *ref, struct got_repository *repo)
627 const struct got_error *err;
628 struct got_object_id *id;
629 struct got_reflist_entry *new, *re, *prev = NULL;
634 err = got_ref_resolve(&id, repo, ref);
638 new = malloc(sizeof(*new));
641 return got_error_from_errno("malloc");
648 * We must de-duplicate entries on insert because packed-refs may
649 * contain redundant entries. On-disk refs take precedence.
650 * This code assumes that on-disk revs are read before packed-refs.
651 * We're iterating the list anyway, so insert elements sorted by name.
653 re = SIMPLEQ_FIRST(refs);
655 const char *name = got_ref_get_name(re->ref);
656 const char *new_name = got_ref_get_name(new->ref);
657 cmp = got_path_cmp(name, new_name, strlen(name),
665 } else if (cmp > 0) {
667 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
669 SIMPLEQ_INSERT_HEAD(refs, new, entry);
673 re = SIMPLEQ_NEXT(re, entry);
677 SIMPLEQ_INSERT_TAIL(refs, new, entry);
681 static const struct got_error *
682 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
683 const char *subdir, struct got_repository *repo)
685 const struct got_error *err = NULL;
689 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
690 return got_error_from_errno("asprintf");
692 d = opendir(path_subdir);
698 struct got_reference *ref;
705 if (strcmp(dent->d_name, ".") == 0 ||
706 strcmp(dent->d_name, "..") == 0)
709 switch (dent->d_type) {
711 err = open_ref(&ref, path_refs, subdir, dent->d_name,
716 struct got_reflist_entry *new;
717 err = insert_ref(&new, refs, ref, repo);
718 if (err || new == NULL /* duplicate */)
725 if (asprintf(&child, "%s%s%s", subdir,
726 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
727 err = got_error_from_errno("asprintf");
730 err = gather_on_disk_refs(refs, path_refs, child, repo);
744 const struct got_error *
745 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
746 const char *ref_namespace)
748 const struct got_error *err;
749 char *packed_refs_path, *path_refs = NULL;
751 struct got_reference *ref;
752 struct got_reflist_entry *new;
754 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
755 /* HEAD ref should always exist. */
756 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
757 if (path_refs == NULL) {
758 err = got_error_from_errno("get_refs_dir_path");
761 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
764 err = insert_ref(&new, refs, ref, repo);
765 if (err || new == NULL /* duplicate */)
771 if (ref_namespace && strncmp(ref_namespace, "refs/", 5) == 0)
774 /* Gather on-disk refs before parsing packed-refs. */
776 path_refs = get_refs_dir_path(repo, "");
777 if (path_refs == NULL) {
778 err = got_error_from_errno("get_refs_dir_path");
781 err = gather_on_disk_refs(refs, path_refs,
782 ref_namespace ? ref_namespace : "", repo);
787 * The packed-refs file may contain redundant entries, in which
788 * case on-disk refs take precedence.
790 packed_refs_path = got_repo_get_path_packed_refs(repo);
791 if (packed_refs_path == NULL) {
792 err = got_error_from_errno("got_repo_get_path_packed_refs");
796 f = fopen(packed_refs_path, "r");
797 free(packed_refs_path);
801 const char delim[3] = {'\0', '\0', '\0'};
803 line = fparseln(f, &len, NULL, delim, 0);
807 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
810 err = parse_packed_ref_line(&ref, NULL, line);
817 name = got_ref_get_name(ref);
818 if (strncmp(name, ref_namespace,
819 strlen(ref_namespace)) != 0) {
824 err = insert_ref(&new, refs, ref, repo);
825 if (err || new == NULL /* duplicate */)
834 if (f && fclose(f) != 0 && err == NULL)
835 err = got_error_from_errno("fclose");
840 got_ref_list_free(struct got_reflist_head *refs)
842 struct got_reflist_entry *re;
844 while (!SIMPLEQ_EMPTY(refs)) {
845 re = SIMPLEQ_FIRST(refs);
846 SIMPLEQ_REMOVE_HEAD(refs, entry);
847 got_ref_close(re->ref);
855 got_ref_is_symbolic(struct got_reference *ref)
857 return (ref->flags & GOT_REF_IS_SYMBOLIC);
860 const struct got_error *
861 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
863 if (ref->flags & GOT_REF_IS_SYMBOLIC)
864 return got_error(GOT_ERR_BAD_REF_TYPE);
866 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
870 const struct got_error *
871 got_ref_change_symref(struct got_reference *ref, char *refname)
875 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
876 return got_error(GOT_ERR_BAD_REF_TYPE);
878 new_name = strdup(refname);
879 if (new_name == NULL)
880 return got_error_from_errno("strdup");
882 free(ref->ref.symref.name);
883 ref->ref.symref.name = new_name;
887 const struct got_error *
888 got_ref_write(struct got_reference *ref, struct got_repository *repo)
890 const struct got_error *err = NULL, *unlock_err = NULL;
891 const char *name = got_ref_get_name(ref);
892 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
893 struct got_lockfile *lf = NULL;
898 path_refs = get_refs_dir_path(repo, name);
899 if (path_refs == NULL) {
900 err = got_error_from_errno2("get_refs_dir_path", name);
904 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
905 err = got_error_from_errno("asprintf");
909 err = got_opentemp_named(&tmppath, &f, path);
912 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
914 err = got_path_dirname(&parent, path);
917 err = got_path_mkdir(parent);
921 err = got_opentemp_named(&tmppath, &f, path);
926 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
927 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
928 if (n != strlen(ref->ref.symref.ref) + 6) {
929 err = got_ferror(f, GOT_ERR_IO);
933 char hex[SHA1_DIGEST_STRING_LENGTH];
934 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
935 sizeof(hex)) == NULL) {
936 err = got_error(GOT_ERR_BAD_REF_DATA);
939 n = fprintf(f, "%s\n", hex);
940 if (n != sizeof(hex)) {
941 err = got_ferror(f, GOT_ERR_IO);
946 if (ref->lf == NULL) {
947 err = got_lockfile_lock(&lf, path);
952 /* XXX: check if old content matches our expectations? */
954 if (stat(path, &sb) != 0) {
955 if (errno != ENOENT) {
956 err = got_error_from_errno2("stat", path);
959 sb.st_mode = GOT_DEFAULT_FILE_MODE;
962 if (rename(tmppath, path) != 0) {
963 err = got_error_from_errno3("rename", tmppath, path);
969 if (chmod(path, sb.st_mode) != 0) {
970 err = got_error_from_errno2("chmod", path);
974 if (ref->lf == NULL && lf)
975 unlock_err = got_lockfile_unlock(lf);
977 if (fclose(f) != 0 && err == NULL)
978 err = got_error_from_errno("fclose");
983 if (unlink(tmppath) != 0 && err == NULL)
984 err = got_error_from_errno2("unlink", tmppath);
987 return err ? err : unlock_err;
990 static const struct got_error *
991 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
993 const struct got_error *err = NULL, *unlock_err = NULL;
994 struct got_lockfile *lf = NULL;
995 FILE *f = NULL, *tmpf = NULL;
996 char *packed_refs_path, *tmppath = NULL;
997 struct got_reflist_head refs;
998 int found_delref = 0;
1000 /* The packed-refs file does not cotain symbolic references. */
1001 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1002 return got_error(GOT_ERR_BAD_REF_DATA);
1004 SIMPLEQ_INIT(&refs);
1006 packed_refs_path = got_repo_get_path_packed_refs(repo);
1007 if (packed_refs_path == NULL)
1008 return got_error_from_errno("got_repo_get_path_packed_refs");
1010 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
1014 if (delref->lf == NULL) {
1015 err = got_lockfile_lock(&lf, packed_refs_path);
1020 f = fopen(packed_refs_path, "r");
1022 err = got_error_from_errno2("fopen", packed_refs_path);
1028 const char delim[3] = {'\0', '\0', '\0'};
1029 struct got_reference *ref;
1030 struct got_reflist_entry *new;
1032 line = fparseln(f, &len, NULL, delim, 0);
1036 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1039 err = parse_packed_ref_line(&ref, NULL, line);
1046 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1047 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1048 sizeof(delref->ref.ref.sha1)) == 0) {
1054 err = insert_ref(&new, &refs, ref, repo);
1055 if (err || new == NULL /* duplicate */)
1062 struct got_reflist_entry *re;
1066 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1067 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1068 err = got_ferror(f, GOT_ERR_IO);
1072 SIMPLEQ_FOREACH(re, &refs, entry) {
1073 uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
1075 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1076 sizeof(hex)) == NULL) {
1077 err = got_error(GOT_ERR_BAD_REF_DATA);
1080 n = fprintf(tmpf, "%s ", hex);
1081 if (n != sizeof(hex)) {
1082 err = got_ferror(f, GOT_ERR_IO);
1085 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1086 if (n != strlen(re->ref->ref.ref.name) + 1) {
1087 err = got_ferror(f, GOT_ERR_IO);
1092 if (fflush(tmpf) != 0) {
1093 err = got_error_from_errno("fflush");
1097 if (stat(packed_refs_path, &sb) != 0) {
1098 if (errno != ENOENT) {
1099 err = got_error_from_errno2("stat",
1103 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1106 if (rename(tmppath, packed_refs_path) != 0) {
1107 err = got_error_from_errno3("rename", tmppath,
1112 if (chmod(packed_refs_path, sb.st_mode) != 0) {
1113 err = got_error_from_errno2("chmod",
1119 if (delref->lf == NULL && lf)
1120 unlock_err = got_lockfile_unlock(lf);
1122 if (fclose(f) != 0 && err == NULL)
1123 err = got_error_from_errno("fclose");
1127 if (fclose(tmpf) != 0 && err == NULL)
1128 err = got_error_from_errno("fclose");
1131 free(packed_refs_path);
1132 got_ref_list_free(&refs);
1133 return err ? err : unlock_err;
1136 const struct got_error *
1137 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1139 const struct got_error *err = NULL, *unlock_err = NULL;
1140 const char *name = got_ref_get_name(ref);
1141 char *path_refs = NULL, *path = NULL;
1142 struct got_lockfile *lf = NULL;
1144 if (ref->flags & GOT_REF_IS_PACKED)
1145 return delete_packed_ref(ref, repo);
1147 path_refs = get_refs_dir_path(repo, name);
1148 if (path_refs == NULL) {
1149 err = got_error_from_errno2("get_refs_dir_path", name);
1153 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1154 err = got_error_from_errno("asprintf");
1158 if (ref->lf == NULL) {
1159 err = got_lockfile_lock(&lf, path);
1164 /* XXX: check if old content matches our expectations? */
1166 if (unlink(path) != 0)
1167 err = got_error_from_errno2("unlink", path);
1169 if (ref->lf == NULL && lf)
1170 unlock_err = got_lockfile_unlock(lf);
1174 return err ? err : unlock_err;
1177 const struct got_error *
1178 got_ref_unlock(struct got_reference *ref)
1180 const struct got_error *err;
1181 err = got_lockfile_unlock(ref->lf);