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");
128 (*ref)->ref.symref.ref = strdup(target_ref);
129 if ((*ref)->ref.symref.ref == NULL) {
130 err = got_error_from_errno("strdup");
137 static const struct got_error *
138 parse_symref(struct got_reference **ref, const char *name, const char *line)
141 return got_error(GOT_ERR_BAD_REF_DATA);
143 return alloc_symref(ref, name, line, 0);
146 static const struct got_error *
147 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
149 struct got_object_id id;
151 if (strncmp(line, "ref: ", 5) == 0) {
153 return parse_symref(ref, name, line);
156 if (!got_parse_sha1_digest(id.sha1, line))
157 return got_error(GOT_ERR_BAD_REF_DATA);
159 return alloc_ref(ref, name, &id, 0);
162 static const struct got_error *
163 parse_ref_file(struct got_reference **ref, const char *name,
164 const char *abspath, int lock)
166 const struct got_error *err = NULL;
170 const char delim[3] = {'\0', '\0', '\0'};
171 struct got_lockfile *lf = NULL;
174 err = got_lockfile_lock(&lf, abspath);
179 f = fopen(abspath, "rb");
182 got_lockfile_unlock(lf);
186 line = fparseln(f, &len, NULL, delim, 0);
188 err = got_error(GOT_ERR_BAD_REF_DATA);
190 got_lockfile_unlock(lf);
194 err = parse_ref_line(ref, name, line);
197 got_lockfile_unlock(lf);
202 got_lockfile_unlock(lf);
207 if (fclose(f) != 0 && err == NULL) {
208 err = got_error_from_errno("fclose");
211 got_ref_unlock(*ref);
220 is_well_known_ref(const char *refname)
222 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
223 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
224 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
225 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
229 get_refs_dir_path(struct got_repository *repo, const char *refname)
231 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
232 return strdup(got_repo_get_path_git_dir(repo));
234 return got_repo_get_path_refs(repo);
238 is_valid_ref_name(const char *name)
240 const char *s, *slash, *seg;
241 const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
242 const char *forbidden_seq[] = { "//", "..", "@{" };
243 const char *lfs = GOT_LOCKFILE_SUFFIX;
244 const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
247 if (name[0] == '@' && name[1] == '\0')
250 slash = strchr(name, '/');
256 if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
259 for (i = 0; i < nitems(forbidden); i++) {
260 if (*s == forbidden[i])
263 for (i = 0; i < nitems(forbidden_seq); i++) {
264 if (s[0] == forbidden_seq[i][0] &&
265 s[1] == forbidden_seq[i][1])
268 if (iscntrl((unsigned char)s[0]))
270 if (s[0] == '.' && s[1] == '\0')
273 const char *nextseg = s + 1;
274 if (nextseg[0] == '\0' || nextseg[0] == '.' ||
277 if (seg <= s - lfs_len &&
278 strncmp(s - lfs_len, lfs, lfs_len) == 0)
285 if (seg <= s - lfs_len &&
286 strncmp(s - lfs_len, lfs, lfs_len) == 0)
292 const struct got_error *
293 got_ref_alloc(struct got_reference **ref, const char *name,
294 struct got_object_id *id)
296 if (!is_valid_ref_name(name))
297 return got_error(GOT_ERR_BAD_REF_NAME);
299 return alloc_ref(ref, name, id, 0);
302 const struct got_error *
303 got_ref_alloc_symref(struct got_reference **ref, const char *name,
304 struct got_reference *target_ref)
306 if (!is_valid_ref_name(name))
307 return got_error(GOT_ERR_BAD_REF_NAME);
309 return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
312 static const struct got_error *
313 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
316 struct got_object_id id;
321 if (line[0] == '#' || line[0] == '^')
324 if (!got_parse_sha1_digest(id.sha1, line))
325 return got_error(GOT_ERR_BAD_REF_DATA);
328 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
332 name = line + SHA1_DIGEST_STRING_LENGTH;
334 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED);
337 static const struct got_error *
338 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
339 int nsubdirs, const char *refname)
341 const struct got_error *err = NULL;
345 const char delim[3] = {'\0', '\0', '\0'};
346 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
351 abs_refname = (char *)refname;
353 line = fparseln(f, &len, NULL, delim, 0);
357 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
360 for (i = 0; i < nsubdirs; i++) {
361 if (!ref_is_absolute &&
362 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
364 return got_error_from_errno("asprintf");
365 err = parse_packed_ref_line(ref, abs_refname, line);
366 if (!ref_is_absolute)
368 if (err || *ref != NULL)
374 } while (*ref == NULL);
379 static const struct got_error *
380 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
381 const char *name, int lock)
383 const struct got_error *err = NULL;
385 char *absname = NULL;
386 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
387 int ref_is_well_known = is_well_known_ref(name);
391 if (ref_is_absolute || ref_is_well_known) {
392 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
393 return got_error_from_errno("asprintf");
394 absname = (char *)name;
396 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
397 subdir[0] ? "/" : "", name) == -1)
398 return got_error_from_errno("asprintf");
400 if (asprintf(&absname, "refs/%s%s%s",
401 subdir, subdir[0] ? "/" : "", name) == -1) {
402 err = got_error_from_errno("asprintf");
407 err = parse_ref_file(ref, absname, path, lock);
409 if (!ref_is_absolute && !ref_is_well_known)
415 const struct got_error *
416 got_ref_open(struct got_reference **ref, struct got_repository *repo,
417 const char *refname, int lock)
419 const struct got_error *err = NULL;
420 char *path_refs = NULL;
421 const char *subdirs[] = {
422 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
424 int i, well_known = is_well_known_ref(refname);
425 struct got_lockfile *lf = NULL;
429 path_refs = get_refs_dir_path(repo, refname);
430 if (path_refs == NULL) {
431 err = got_error_from_errno2("get_refs_dir_path", refname);
436 err = open_ref(ref, path_refs, "", refname, lock);
438 char *packed_refs_path;
441 /* Search on-disk refs before packed refs! */
442 for (i = 0; i < nitems(subdirs); i++) {
443 err = open_ref(ref, path_refs, subdirs[i], refname,
449 packed_refs_path = got_repo_get_path_packed_refs(repo);
450 if (packed_refs_path == NULL) {
451 err = got_error_from_errno(
452 "got_repo_get_path_packed_refs");
457 err = got_lockfile_lock(&lf, packed_refs_path);
461 f = fopen(packed_refs_path, "rb");
462 free(packed_refs_path);
464 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
467 if (fclose(f) != 0) {
468 err = got_error_from_errno("fclose");
477 if (!err && *ref == NULL)
478 err = got_error_not_ref(refname);
480 got_lockfile_unlock(lf);
486 got_ref_close(struct got_reference *ref)
488 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
489 free(ref->ref.symref.name);
490 free(ref->ref.symref.ref);
492 free(ref->ref.ref.name);
496 struct got_reference *
497 got_ref_dup(struct got_reference *ref)
499 struct got_reference *ret;
501 ret = calloc(1, sizeof(*ret));
505 ret->flags = ref->flags;
506 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
507 ret->ref.symref.name = strdup(ref->ref.symref.name);
508 if (ret->ref.symref.name == NULL) {
512 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
513 if (ret->ref.symref.ref == NULL) {
514 free(ret->ref.symref.name);
519 ref->ref.ref.name = strdup(ref->ref.ref.name);
520 if (ref->ref.ref.name == NULL) {
524 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
525 sizeof(ret->ref.ref.sha1));
531 const struct got_error *
532 got_reflist_entry_dup(struct got_reflist_entry **newp,
533 struct got_reflist_entry *re)
535 const struct got_error *err = NULL;
536 struct got_reflist_entry *new;
540 new = malloc(sizeof(*new));
542 return got_error_from_errno("malloc");
544 new->ref = got_ref_dup(re->ref);
545 if (new->ref == NULL) {
546 err = got_error_from_errno("got_ref_dup");
551 new->id = got_object_id_dup(re->id);
552 if (new->id == NULL) {
553 err = got_error_from_errno("got_ref_dup");
563 static const struct got_error *
564 resolve_symbolic_ref(struct got_reference **resolved,
565 struct got_repository *repo, struct got_reference *ref)
567 struct got_reference *nextref;
568 const struct got_error *err;
570 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
574 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
575 err = resolve_symbolic_ref(resolved, repo, nextref);
577 *resolved = got_ref_dup(nextref);
579 got_ref_close(nextref);
583 static const struct got_error *
584 ref_resolve(struct got_object_id **id, struct got_repository *repo,
585 struct got_reference *ref, int recursion)
587 const struct got_error *err;
590 return got_error_msg(GOT_ERR_RECURSION,
591 "reference recursion limit reached");
593 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
594 struct got_reference *resolved = NULL;
595 err = resolve_symbolic_ref(&resolved, repo, ref);
597 err = ref_resolve(id, repo, resolved, --recursion);
599 got_ref_close(resolved);
603 *id = calloc(1, sizeof(**id));
605 return got_error_from_errno("calloc");
606 memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
610 const struct got_error *
611 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
612 struct got_reference *ref)
614 return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
618 got_ref_to_str(struct got_reference *ref)
622 if (ref->flags & GOT_REF_IS_SYMBOLIC)
623 return strdup(ref->ref.symref.ref);
625 str = malloc(SHA1_DIGEST_STRING_LENGTH);
629 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
630 SHA1_DIGEST_STRING_LENGTH) == NULL) {
639 got_ref_get_name(struct got_reference *ref)
641 if (ref->flags & GOT_REF_IS_SYMBOLIC)
642 return ref->ref.symref.name;
644 return ref->ref.ref.name;
648 got_ref_get_symref_target(struct got_reference *ref)
650 if (ref->flags & GOT_REF_IS_SYMBOLIC)
651 return ref->ref.symref.ref;
656 const struct got_error *
657 got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
658 struct got_reference* re2)
660 const char *name1 = got_ref_get_name(re1);
661 const char *name2 = got_ref_get_name(re2);
663 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
667 static const struct got_error *
668 insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
669 struct got_reference *ref, struct got_repository *repo,
670 got_ref_cmp_cb cmp_cb, void *cmp_arg)
672 const struct got_error *err;
673 struct got_object_id *id;
674 struct got_reflist_entry *new, *re, *prev = NULL;
679 err = got_ref_resolve(&id, repo, ref);
683 new = malloc(sizeof(*new));
686 return got_error_from_errno("malloc");
693 * We must de-duplicate entries on insert because packed-refs may
694 * contain redundant entries. On-disk refs take precedence.
695 * This code assumes that on-disk revs are read before packed-refs.
696 * We're iterating the list anyway, so insert elements sorted by name.
698 re = SIMPLEQ_FIRST(refs);
700 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
709 } else if (cmp > 0) {
711 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
713 SIMPLEQ_INSERT_HEAD(refs, new, entry);
717 re = SIMPLEQ_NEXT(re, entry);
721 SIMPLEQ_INSERT_TAIL(refs, new, entry);
725 static const struct got_error *
726 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
727 const char *subdir, struct got_repository *repo,
728 got_ref_cmp_cb cmp_cb, void *cmp_arg)
730 const struct got_error *err = NULL;
734 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
735 return got_error_from_errno("asprintf");
737 d = opendir(path_subdir);
743 struct got_reference *ref;
750 if (strcmp(dent->d_name, ".") == 0 ||
751 strcmp(dent->d_name, "..") == 0)
754 switch (dent->d_type) {
756 err = open_ref(&ref, path_refs, subdir, dent->d_name,
761 struct got_reflist_entry *new;
762 err = insert_ref(&new, refs, ref, repo,
764 if (err || new == NULL /* duplicate */)
771 if (asprintf(&child, "%s%s%s", subdir,
772 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
773 err = got_error_from_errno("asprintf");
776 err = gather_on_disk_refs(refs, path_refs, child, repo,
791 const struct got_error *
792 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
793 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
795 const struct got_error *err;
796 char *packed_refs_path, *path_refs = NULL;
797 const char *ondisk_ref_namespace = NULL;
799 struct got_reference *ref;
800 struct got_reflist_entry *new;
802 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
803 /* HEAD ref should always exist. */
804 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
805 if (path_refs == NULL) {
806 err = got_error_from_errno("get_refs_dir_path");
809 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
812 err = insert_ref(&new, refs, ref, repo, cmp_cb, cmp_arg);
813 if (err || new == NULL /* duplicate */)
819 ondisk_ref_namespace = ref_namespace;
820 if (ref_namespace && strncmp(ref_namespace, "refs/", 5) == 0)
821 ondisk_ref_namespace += 5;
823 /* Gather on-disk refs before parsing packed-refs. */
825 path_refs = get_refs_dir_path(repo, "");
826 if (path_refs == NULL) {
827 err = got_error_from_errno("get_refs_dir_path");
830 err = gather_on_disk_refs(refs, path_refs,
831 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
837 * The packed-refs file may contain redundant entries, in which
838 * case on-disk refs take precedence.
840 packed_refs_path = got_repo_get_path_packed_refs(repo);
841 if (packed_refs_path == NULL) {
842 err = got_error_from_errno("got_repo_get_path_packed_refs");
846 f = fopen(packed_refs_path, "r");
847 free(packed_refs_path);
851 const char delim[3] = {'\0', '\0', '\0'};
853 line = fparseln(f, &len, NULL, delim, 0);
857 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
860 err = parse_packed_ref_line(&ref, NULL, line);
867 name = got_ref_get_name(ref);
868 if (strncmp(name, ref_namespace,
869 strlen(ref_namespace)) != 0) {
874 err = insert_ref(&new, refs, ref, repo,
876 if (err || new == NULL /* duplicate */)
885 if (f && fclose(f) != 0 && err == NULL)
886 err = got_error_from_errno("fclose");
891 got_ref_list_free(struct got_reflist_head *refs)
893 struct got_reflist_entry *re;
895 while (!SIMPLEQ_EMPTY(refs)) {
896 re = SIMPLEQ_FIRST(refs);
897 SIMPLEQ_REMOVE_HEAD(refs, entry);
898 got_ref_close(re->ref);
906 got_ref_is_symbolic(struct got_reference *ref)
908 return (ref->flags & GOT_REF_IS_SYMBOLIC);
911 const struct got_error *
912 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
914 if (ref->flags & GOT_REF_IS_SYMBOLIC)
915 return got_error(GOT_ERR_BAD_REF_TYPE);
917 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
921 const struct got_error *
922 got_ref_change_symref(struct got_reference *ref, char *refname)
926 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
927 return got_error(GOT_ERR_BAD_REF_TYPE);
929 new_name = strdup(refname);
930 if (new_name == NULL)
931 return got_error_from_errno("strdup");
933 free(ref->ref.symref.name);
934 ref->ref.symref.name = new_name;
938 const struct got_error *
939 got_ref_write(struct got_reference *ref, struct got_repository *repo)
941 const struct got_error *err = NULL, *unlock_err = NULL;
942 const char *name = got_ref_get_name(ref);
943 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
944 struct got_lockfile *lf = NULL;
949 path_refs = get_refs_dir_path(repo, name);
950 if (path_refs == NULL) {
951 err = got_error_from_errno2("get_refs_dir_path", name);
955 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
956 err = got_error_from_errno("asprintf");
960 err = got_opentemp_named(&tmppath, &f, path);
963 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
965 err = got_path_dirname(&parent, path);
968 err = got_path_mkdir(parent);
972 err = got_opentemp_named(&tmppath, &f, path);
977 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
978 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
979 if (n != strlen(ref->ref.symref.ref) + 6) {
980 err = got_ferror(f, GOT_ERR_IO);
984 char hex[SHA1_DIGEST_STRING_LENGTH];
985 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
986 sizeof(hex)) == NULL) {
987 err = got_error(GOT_ERR_BAD_REF_DATA);
990 n = fprintf(f, "%s\n", hex);
991 if (n != sizeof(hex)) {
992 err = got_ferror(f, GOT_ERR_IO);
997 if (ref->lf == NULL) {
998 err = got_lockfile_lock(&lf, path);
1003 /* XXX: check if old content matches our expectations? */
1005 if (stat(path, &sb) != 0) {
1006 if (errno != ENOENT) {
1007 err = got_error_from_errno2("stat", path);
1010 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1013 if (rename(tmppath, path) != 0) {
1014 err = got_error_from_errno3("rename", tmppath, path);
1020 if (chmod(path, sb.st_mode) != 0) {
1021 err = got_error_from_errno2("chmod", path);
1025 if (ref->lf == NULL && lf)
1026 unlock_err = got_lockfile_unlock(lf);
1028 if (fclose(f) != 0 && err == NULL)
1029 err = got_error_from_errno("fclose");
1034 if (unlink(tmppath) != 0 && err == NULL)
1035 err = got_error_from_errno2("unlink", tmppath);
1038 return err ? err : unlock_err;
1041 static const struct got_error *
1042 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1044 const struct got_error *err = NULL, *unlock_err = NULL;
1045 struct got_lockfile *lf = NULL;
1046 FILE *f = NULL, *tmpf = NULL;
1047 char *packed_refs_path, *tmppath = NULL;
1048 struct got_reflist_head refs;
1049 int found_delref = 0;
1051 /* The packed-refs file does not cotain symbolic references. */
1052 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1053 return got_error(GOT_ERR_BAD_REF_DATA);
1055 SIMPLEQ_INIT(&refs);
1057 packed_refs_path = got_repo_get_path_packed_refs(repo);
1058 if (packed_refs_path == NULL)
1059 return got_error_from_errno("got_repo_get_path_packed_refs");
1061 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
1065 if (delref->lf == NULL) {
1066 err = got_lockfile_lock(&lf, packed_refs_path);
1071 f = fopen(packed_refs_path, "r");
1073 err = got_error_from_errno2("fopen", packed_refs_path);
1079 const char delim[3] = {'\0', '\0', '\0'};
1080 struct got_reference *ref;
1081 struct got_reflist_entry *new;
1083 line = fparseln(f, &len, NULL, delim, 0);
1087 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1090 err = parse_packed_ref_line(&ref, NULL, line);
1097 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1098 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1099 sizeof(delref->ref.ref.sha1)) == 0) {
1105 err = insert_ref(&new, &refs, ref, repo,
1106 got_ref_cmp_by_name, NULL);
1107 if (err || new == NULL /* duplicate */)
1114 struct got_reflist_entry *re;
1118 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1119 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1120 err = got_ferror(f, GOT_ERR_IO);
1124 SIMPLEQ_FOREACH(re, &refs, entry) {
1125 uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
1127 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1128 sizeof(hex)) == NULL) {
1129 err = got_error(GOT_ERR_BAD_REF_DATA);
1132 n = fprintf(tmpf, "%s ", hex);
1133 if (n != sizeof(hex)) {
1134 err = got_ferror(f, GOT_ERR_IO);
1137 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1138 if (n != strlen(re->ref->ref.ref.name) + 1) {
1139 err = got_ferror(f, GOT_ERR_IO);
1144 if (fflush(tmpf) != 0) {
1145 err = got_error_from_errno("fflush");
1149 if (stat(packed_refs_path, &sb) != 0) {
1150 if (errno != ENOENT) {
1151 err = got_error_from_errno2("stat",
1155 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1158 if (rename(tmppath, packed_refs_path) != 0) {
1159 err = got_error_from_errno3("rename", tmppath,
1164 if (chmod(packed_refs_path, sb.st_mode) != 0) {
1165 err = got_error_from_errno2("chmod",
1171 if (delref->lf == NULL && lf)
1172 unlock_err = got_lockfile_unlock(lf);
1174 if (fclose(f) != 0 && err == NULL)
1175 err = got_error_from_errno("fclose");
1179 if (fclose(tmpf) != 0 && err == NULL)
1180 err = got_error_from_errno("fclose");
1183 free(packed_refs_path);
1184 got_ref_list_free(&refs);
1185 return err ? err : unlock_err;
1188 const struct got_error *
1189 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1191 const struct got_error *err = NULL, *unlock_err = NULL;
1192 const char *name = got_ref_get_name(ref);
1193 char *path_refs = NULL, *path = NULL;
1194 struct got_lockfile *lf = NULL;
1196 if (ref->flags & GOT_REF_IS_PACKED)
1197 return delete_packed_ref(ref, repo);
1199 path_refs = get_refs_dir_path(repo, name);
1200 if (path_refs == NULL) {
1201 err = got_error_from_errno2("get_refs_dir_path", name);
1205 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1206 err = got_error_from_errno("asprintf");
1210 if (ref->lf == NULL) {
1211 err = got_lockfile_lock(&lf, path);
1216 /* XXX: check if old content matches our expectations? */
1218 if (unlink(path) != 0)
1219 err = got_error_from_errno2("unlink", path);
1221 if (ref->lf == NULL && lf)
1222 unlock_err = got_lockfile_unlock(lf);
1226 return err ? err : unlock_err;
1229 const struct got_error *
1230 got_ref_unlock(struct got_reference *ref)
1232 const struct got_error *err;
1233 err = got_lockfile_unlock(ref->lf);