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 *abspath, int lock)
167 const struct got_error *err = NULL;
172 struct got_lockfile *lf = NULL;
175 err = got_lockfile_lock(&lf, abspath);
180 f = fopen(abspath, "rb");
183 got_lockfile_unlock(lf);
187 linelen = getline(&line, &linesize, f);
190 err = NULL; /* ignore empty files (could be locks) */
192 err = got_error_from_errno2("getline", abspath);
194 got_lockfile_unlock(lf);
197 while (linelen > 0 && line[linelen - 1] == '\n') {
198 line[linelen - 1] = '\0';
202 err = parse_ref_line(ref, name, line);
205 got_lockfile_unlock(lf);
210 got_lockfile_unlock(lf);
215 if (fclose(f) != 0 && err == NULL) {
216 err = got_error_from_errno("fclose");
219 got_ref_unlock(*ref);
228 is_well_known_ref(const char *refname)
230 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
231 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
232 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
233 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
237 get_refs_dir_path(struct got_repository *repo, const char *refname)
239 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
240 return strdup(got_repo_get_path_git_dir(repo));
242 return got_repo_get_path_refs(repo);
246 is_valid_ref_name(const char *name)
248 const char *s, *slash, *seg;
249 const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
250 const char *forbidden_seq[] = { "//", "..", "@{" };
251 const char *lfs = GOT_LOCKFILE_SUFFIX;
252 const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
255 if (name[0] == '@' && name[1] == '\0')
258 slash = strchr(name, '/');
264 if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
267 for (i = 0; i < nitems(forbidden); i++) {
268 if (*s == forbidden[i])
271 for (i = 0; i < nitems(forbidden_seq); i++) {
272 if (s[0] == forbidden_seq[i][0] &&
273 s[1] == forbidden_seq[i][1])
276 if (iscntrl((unsigned char)s[0]))
278 if (s[0] == '.' && s[1] == '\0')
281 const char *nextseg = s + 1;
282 if (nextseg[0] == '\0' || nextseg[0] == '.' ||
285 if (seg <= s - lfs_len &&
286 strncmp(s - lfs_len, lfs, lfs_len) == 0)
293 if (seg <= s - lfs_len &&
294 strncmp(s - lfs_len, lfs, lfs_len) == 0)
300 const struct got_error *
301 got_ref_alloc(struct got_reference **ref, const char *name,
302 struct got_object_id *id)
304 if (!is_valid_ref_name(name))
305 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
307 return alloc_ref(ref, name, id, 0);
310 const struct got_error *
311 got_ref_alloc_symref(struct got_reference **ref, const char *name,
312 struct got_reference *target_ref)
314 if (!is_valid_ref_name(name))
315 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
317 return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
320 static const struct got_error *
321 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
324 struct got_object_id id;
329 if (line[0] == '#' || line[0] == '^')
332 if (!got_parse_sha1_digest(id.sha1, line))
333 return got_error(GOT_ERR_BAD_REF_DATA);
336 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
340 name = line + SHA1_DIGEST_STRING_LENGTH;
342 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED);
345 static const struct got_error *
346 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
347 int nsubdirs, const char *refname)
349 const struct got_error *err = NULL;
353 const char delim[3] = {'\0', '\0', '\0'};
354 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
359 abs_refname = (char *)refname;
361 line = fparseln(f, &len, NULL, delim, 0);
365 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
368 for (i = 0; i < nsubdirs; i++) {
369 if (!ref_is_absolute &&
370 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
372 return got_error_from_errno("asprintf");
373 err = parse_packed_ref_line(ref, abs_refname, line);
374 if (!ref_is_absolute)
376 if (err || *ref != NULL)
382 } while (*ref == NULL);
387 static const struct got_error *
388 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
389 const char *name, int lock)
391 const struct got_error *err = NULL;
393 char *absname = NULL;
394 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
395 int ref_is_well_known = is_well_known_ref(name);
399 if (ref_is_absolute || ref_is_well_known) {
400 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
401 return got_error_from_errno("asprintf");
402 absname = (char *)name;
404 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
405 subdir[0] ? "/" : "", name) == -1)
406 return got_error_from_errno("asprintf");
408 if (asprintf(&absname, "refs/%s%s%s",
409 subdir, subdir[0] ? "/" : "", name) == -1) {
410 err = got_error_from_errno("asprintf");
415 err = parse_ref_file(ref, absname, path, lock);
417 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 const struct got_error *
540 got_reflist_entry_dup(struct got_reflist_entry **newp,
541 struct got_reflist_entry *re)
543 const struct got_error *err = NULL;
544 struct got_reflist_entry *new;
548 new = malloc(sizeof(*new));
550 return got_error_from_errno("malloc");
552 new->ref = got_ref_dup(re->ref);
553 if (new->ref == NULL) {
554 err = got_error_from_errno("got_ref_dup");
559 new->id = got_object_id_dup(re->id);
560 if (new->id == NULL) {
561 err = got_error_from_errno("got_ref_dup");
571 static const struct got_error *
572 resolve_symbolic_ref(struct got_reference **resolved,
573 struct got_repository *repo, struct got_reference *ref)
575 struct got_reference *nextref;
576 const struct got_error *err;
578 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
582 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
583 err = resolve_symbolic_ref(resolved, repo, nextref);
585 *resolved = got_ref_dup(nextref);
587 got_ref_close(nextref);
591 static const struct got_error *
592 ref_resolve(struct got_object_id **id, struct got_repository *repo,
593 struct got_reference *ref, int recursion)
595 const struct got_error *err;
598 return got_error_msg(GOT_ERR_RECURSION,
599 "reference recursion limit reached");
601 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
602 struct got_reference *resolved = NULL;
603 err = resolve_symbolic_ref(&resolved, repo, ref);
605 err = ref_resolve(id, repo, resolved, --recursion);
607 got_ref_close(resolved);
611 *id = calloc(1, sizeof(**id));
613 return got_error_from_errno("calloc");
614 memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
618 const struct got_error *
619 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
620 struct got_reference *ref)
622 return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
626 got_ref_to_str(struct got_reference *ref)
630 if (ref->flags & GOT_REF_IS_SYMBOLIC)
631 return strdup(ref->ref.symref.ref);
633 str = malloc(SHA1_DIGEST_STRING_LENGTH);
637 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
638 SHA1_DIGEST_STRING_LENGTH) == NULL) {
647 got_ref_get_name(struct got_reference *ref)
649 if (ref->flags & GOT_REF_IS_SYMBOLIC)
650 return ref->ref.symref.name;
652 return ref->ref.ref.name;
656 got_ref_get_symref_target(struct got_reference *ref)
658 if (ref->flags & GOT_REF_IS_SYMBOLIC)
659 return ref->ref.symref.ref;
664 const struct got_error *
665 got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
666 struct got_reference* re2)
668 const char *name1 = got_ref_get_name(re1);
669 const char *name2 = got_ref_get_name(re2);
671 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
675 static const struct got_error *
676 insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
677 struct got_reference *ref, struct got_repository *repo,
678 got_ref_cmp_cb cmp_cb, void *cmp_arg)
680 const struct got_error *err;
681 struct got_object_id *id;
682 struct got_reflist_entry *new, *re, *prev = NULL;
687 err = got_ref_resolve(&id, repo, ref);
691 new = malloc(sizeof(*new));
694 return got_error_from_errno("malloc");
701 * We must de-duplicate entries on insert because packed-refs may
702 * contain redundant entries. On-disk refs take precedence.
703 * This code assumes that on-disk revs are read before packed-refs.
704 * We're iterating the list anyway, so insert elements sorted by name.
706 re = SIMPLEQ_FIRST(refs);
708 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
717 } else if (cmp > 0) {
719 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
721 SIMPLEQ_INSERT_HEAD(refs, new, entry);
725 re = SIMPLEQ_NEXT(re, entry);
729 SIMPLEQ_INSERT_TAIL(refs, new, entry);
733 static const struct got_error *
734 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
735 const char *subdir, struct got_repository *repo,
736 got_ref_cmp_cb cmp_cb, void *cmp_arg)
738 const struct got_error *err = NULL;
742 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
743 return got_error_from_errno("asprintf");
745 d = opendir(path_subdir);
751 struct got_reference *ref;
758 if (strcmp(dent->d_name, ".") == 0 ||
759 strcmp(dent->d_name, "..") == 0)
762 switch (dent->d_type) {
764 err = open_ref(&ref, path_refs, subdir, dent->d_name,
769 struct got_reflist_entry *new;
770 err = insert_ref(&new, refs, ref, repo,
772 if (err || new == NULL /* duplicate */)
779 if (asprintf(&child, "%s%s%s", subdir,
780 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
781 err = got_error_from_errno("asprintf");
784 err = gather_on_disk_refs(refs, path_refs, child, repo,
799 const struct got_error *
800 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
801 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
803 const struct got_error *err;
804 char *packed_refs_path, *path_refs = NULL;
805 const char *ondisk_ref_namespace = NULL;
807 struct got_reference *ref;
808 struct got_reflist_entry *new;
810 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
811 /* HEAD ref should always exist. */
812 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
813 if (path_refs == NULL) {
814 err = got_error_from_errno("get_refs_dir_path");
817 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
820 err = insert_ref(&new, refs, ref, repo, cmp_cb, cmp_arg);
821 if (err || new == NULL /* duplicate */)
827 ondisk_ref_namespace = ref_namespace;
828 if (ref_namespace && strncmp(ref_namespace, "refs/", 5) == 0)
829 ondisk_ref_namespace += 5;
831 /* Gather on-disk refs before parsing packed-refs. */
833 path_refs = get_refs_dir_path(repo, "");
834 if (path_refs == NULL) {
835 err = got_error_from_errno("get_refs_dir_path");
838 err = gather_on_disk_refs(refs, path_refs,
839 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
845 * The packed-refs file may contain redundant entries, in which
846 * case on-disk refs take precedence.
848 packed_refs_path = got_repo_get_path_packed_refs(repo);
849 if (packed_refs_path == NULL) {
850 err = got_error_from_errno("got_repo_get_path_packed_refs");
854 f = fopen(packed_refs_path, "r");
855 free(packed_refs_path);
859 const char delim[3] = {'\0', '\0', '\0'};
861 line = fparseln(f, &len, NULL, delim, 0);
865 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
868 err = parse_packed_ref_line(&ref, NULL, line);
875 name = got_ref_get_name(ref);
876 if (strncmp(name, ref_namespace,
877 strlen(ref_namespace)) != 0) {
882 err = insert_ref(&new, refs, ref, repo,
884 if (err || new == NULL /* duplicate */)
893 if (f && fclose(f) != 0 && err == NULL)
894 err = got_error_from_errno("fclose");
899 got_ref_list_free(struct got_reflist_head *refs)
901 struct got_reflist_entry *re;
903 while (!SIMPLEQ_EMPTY(refs)) {
904 re = SIMPLEQ_FIRST(refs);
905 SIMPLEQ_REMOVE_HEAD(refs, entry);
906 got_ref_close(re->ref);
914 got_ref_is_symbolic(struct got_reference *ref)
916 return (ref->flags & GOT_REF_IS_SYMBOLIC);
919 const struct got_error *
920 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
922 if (ref->flags & GOT_REF_IS_SYMBOLIC)
923 return got_error(GOT_ERR_BAD_REF_TYPE);
925 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
929 const struct got_error *
930 got_ref_change_symref(struct got_reference *ref, char *refname)
934 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
935 return got_error(GOT_ERR_BAD_REF_TYPE);
937 new_name = strdup(refname);
938 if (new_name == NULL)
939 return got_error_from_errno("strdup");
941 free(ref->ref.symref.name);
942 ref->ref.symref.name = new_name;
946 const struct got_error *
947 got_ref_write(struct got_reference *ref, struct got_repository *repo)
949 const struct got_error *err = NULL, *unlock_err = NULL;
950 const char *name = got_ref_get_name(ref);
951 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
952 struct got_lockfile *lf = NULL;
957 path_refs = get_refs_dir_path(repo, name);
958 if (path_refs == NULL) {
959 err = got_error_from_errno2("get_refs_dir_path", name);
963 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
964 err = got_error_from_errno("asprintf");
968 err = got_opentemp_named(&tmppath, &f, path);
971 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
973 err = got_path_dirname(&parent, path);
976 err = got_path_mkdir(parent);
980 err = got_opentemp_named(&tmppath, &f, path);
985 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
986 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
987 if (n != strlen(ref->ref.symref.ref) + 6) {
988 err = got_ferror(f, GOT_ERR_IO);
992 char hex[SHA1_DIGEST_STRING_LENGTH];
993 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
994 sizeof(hex)) == NULL) {
995 err = got_error(GOT_ERR_BAD_REF_DATA);
998 n = fprintf(f, "%s\n", hex);
999 if (n != sizeof(hex)) {
1000 err = got_ferror(f, GOT_ERR_IO);
1005 if (ref->lf == NULL) {
1006 err = got_lockfile_lock(&lf, path);
1011 /* XXX: check if old content matches our expectations? */
1013 if (stat(path, &sb) != 0) {
1014 if (errno != ENOENT) {
1015 err = got_error_from_errno2("stat", path);
1018 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1021 if (rename(tmppath, path) != 0) {
1022 err = got_error_from_errno3("rename", tmppath, path);
1028 if (chmod(path, sb.st_mode) != 0) {
1029 err = got_error_from_errno2("chmod", path);
1033 if (ref->lf == NULL && lf)
1034 unlock_err = got_lockfile_unlock(lf);
1036 if (fclose(f) != 0 && err == NULL)
1037 err = got_error_from_errno("fclose");
1042 if (unlink(tmppath) != 0 && err == NULL)
1043 err = got_error_from_errno2("unlink", tmppath);
1046 return err ? err : unlock_err;
1049 static const struct got_error *
1050 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1052 const struct got_error *err = NULL, *unlock_err = NULL;
1053 struct got_lockfile *lf = NULL;
1054 FILE *f = NULL, *tmpf = NULL;
1055 char *packed_refs_path, *tmppath = NULL;
1056 struct got_reflist_head refs;
1057 int found_delref = 0;
1059 /* The packed-refs file does not cotain symbolic references. */
1060 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1061 return got_error(GOT_ERR_BAD_REF_DATA);
1063 SIMPLEQ_INIT(&refs);
1065 packed_refs_path = got_repo_get_path_packed_refs(repo);
1066 if (packed_refs_path == NULL)
1067 return got_error_from_errno("got_repo_get_path_packed_refs");
1069 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
1073 if (delref->lf == NULL) {
1074 err = got_lockfile_lock(&lf, packed_refs_path);
1079 f = fopen(packed_refs_path, "r");
1081 err = got_error_from_errno2("fopen", packed_refs_path);
1087 const char delim[3] = {'\0', '\0', '\0'};
1088 struct got_reference *ref;
1089 struct got_reflist_entry *new;
1091 line = fparseln(f, &len, NULL, delim, 0);
1095 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1098 err = parse_packed_ref_line(&ref, NULL, line);
1105 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1106 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1107 sizeof(delref->ref.ref.sha1)) == 0) {
1113 err = insert_ref(&new, &refs, ref, repo,
1114 got_ref_cmp_by_name, NULL);
1115 if (err || new == NULL /* duplicate */)
1122 struct got_reflist_entry *re;
1126 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1127 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1128 err = got_ferror(f, GOT_ERR_IO);
1132 SIMPLEQ_FOREACH(re, &refs, entry) {
1133 uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
1135 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1136 sizeof(hex)) == NULL) {
1137 err = got_error(GOT_ERR_BAD_REF_DATA);
1140 n = fprintf(tmpf, "%s ", hex);
1141 if (n != sizeof(hex)) {
1142 err = got_ferror(f, GOT_ERR_IO);
1145 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1146 if (n != strlen(re->ref->ref.ref.name) + 1) {
1147 err = got_ferror(f, GOT_ERR_IO);
1152 if (fflush(tmpf) != 0) {
1153 err = got_error_from_errno("fflush");
1157 if (stat(packed_refs_path, &sb) != 0) {
1158 if (errno != ENOENT) {
1159 err = got_error_from_errno2("stat",
1163 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1166 if (rename(tmppath, packed_refs_path) != 0) {
1167 err = got_error_from_errno3("rename", tmppath,
1172 if (chmod(packed_refs_path, sb.st_mode) != 0) {
1173 err = got_error_from_errno2("chmod",
1179 if (delref->lf == NULL && lf)
1180 unlock_err = got_lockfile_unlock(lf);
1182 if (fclose(f) != 0 && err == NULL)
1183 err = got_error_from_errno("fclose");
1187 if (fclose(tmpf) != 0 && err == NULL)
1188 err = got_error_from_errno("fclose");
1191 free(packed_refs_path);
1192 got_ref_list_free(&refs);
1193 return err ? err : unlock_err;
1196 const struct got_error *
1197 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1199 const struct got_error *err = NULL, *unlock_err = NULL;
1200 const char *name = got_ref_get_name(ref);
1201 char *path_refs = NULL, *path = NULL;
1202 struct got_lockfile *lf = NULL;
1204 if (ref->flags & GOT_REF_IS_PACKED)
1205 return delete_packed_ref(ref, repo);
1207 path_refs = get_refs_dir_path(repo, name);
1208 if (path_refs == NULL) {
1209 err = got_error_from_errno2("get_refs_dir_path", name);
1213 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1214 err = got_error_from_errno("asprintf");
1218 if (ref->lf == NULL) {
1219 err = got_lockfile_lock(&lf, path);
1224 /* XXX: check if old content matches our expectations? */
1226 if (unlink(path) != 0)
1227 err = got_error_from_errno2("unlink", path);
1229 if (ref->lf == NULL && lf)
1230 unlock_err = got_lockfile_unlock(lf);
1234 return err ? err : unlock_err;
1237 const struct got_error *
1238 got_ref_unlock(struct got_reference *ref)
1240 const struct got_error *err;
1241 err = got_lockfile_unlock(ref->lf);