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;
171 struct got_lockfile *lf = NULL;
174 err = got_lockfile_lock(&lf, abspath);
179 f = fopen(abspath, "rb");
182 got_lockfile_unlock(lf);
186 linelen = getline(&line, &linesize, f);
189 err = NULL; /* ignore empty files (could be locks) */
191 err = got_error_from_errno2("getline", abspath);
193 got_lockfile_unlock(lf);
196 while (linelen > 0 && line[linelen - 1] == '\n') {
197 line[linelen - 1] = '\0';
201 err = parse_ref_line(ref, name, line);
204 got_lockfile_unlock(lf);
209 got_lockfile_unlock(lf);
214 if (fclose(f) != 0 && err == NULL) {
215 err = got_error_from_errno("fclose");
218 got_ref_unlock(*ref);
227 is_well_known_ref(const char *refname)
229 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
230 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
231 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
232 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
236 get_refs_dir_path(struct got_repository *repo, const char *refname)
238 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
239 return strdup(got_repo_get_path_git_dir(repo));
241 return got_repo_get_path_refs(repo);
245 is_valid_ref_name(const char *name)
247 const char *s, *slash, *seg;
248 const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
249 const char *forbidden_seq[] = { "//", "..", "@{" };
250 const char *lfs = GOT_LOCKFILE_SUFFIX;
251 const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
254 if (name[0] == '@' && name[1] == '\0')
257 slash = strchr(name, '/');
263 if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
266 for (i = 0; i < nitems(forbidden); i++) {
267 if (*s == forbidden[i])
270 for (i = 0; i < nitems(forbidden_seq); i++) {
271 if (s[0] == forbidden_seq[i][0] &&
272 s[1] == forbidden_seq[i][1])
275 if (iscntrl((unsigned char)s[0]))
277 if (s[0] == '.' && s[1] == '\0')
280 const char *nextseg = s + 1;
281 if (nextseg[0] == '\0' || nextseg[0] == '.' ||
284 if (seg <= s - lfs_len &&
285 strncmp(s - lfs_len, lfs, lfs_len) == 0)
292 if (seg <= s - lfs_len &&
293 strncmp(s - lfs_len, lfs, lfs_len) == 0)
299 const struct got_error *
300 got_ref_alloc(struct got_reference **ref, const char *name,
301 struct got_object_id *id)
303 if (!is_valid_ref_name(name))
304 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
306 return alloc_ref(ref, name, id, 0);
309 const struct got_error *
310 got_ref_alloc_symref(struct got_reference **ref, const char *name,
311 struct got_reference *target_ref)
313 if (!is_valid_ref_name(name))
314 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
316 return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
319 static const struct got_error *
320 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
323 struct got_object_id id;
328 if (line[0] == '#' || line[0] == '^')
331 if (!got_parse_sha1_digest(id.sha1, line))
332 return got_error(GOT_ERR_BAD_REF_DATA);
335 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
339 name = line + SHA1_DIGEST_STRING_LENGTH;
341 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED);
344 static const struct got_error *
345 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
346 int nsubdirs, const char *refname)
348 const struct got_error *err = NULL;
352 const char delim[3] = {'\0', '\0', '\0'};
353 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
358 abs_refname = (char *)refname;
360 line = fparseln(f, &len, NULL, delim, 0);
364 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
367 for (i = 0; i < nsubdirs; i++) {
368 if (!ref_is_absolute &&
369 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
371 return got_error_from_errno("asprintf");
372 err = parse_packed_ref_line(ref, abs_refname, line);
373 if (!ref_is_absolute)
375 if (err || *ref != NULL)
381 } while (*ref == NULL);
386 static const struct got_error *
387 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
388 const char *name, int lock)
390 const struct got_error *err = NULL;
392 char *absname = NULL;
393 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
394 int ref_is_well_known = is_well_known_ref(name);
398 if (ref_is_absolute || ref_is_well_known) {
399 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
400 return got_error_from_errno("asprintf");
401 absname = (char *)name;
403 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
404 subdir[0] ? "/" : "", name) == -1)
405 return got_error_from_errno("asprintf");
407 if (asprintf(&absname, "refs/%s%s%s",
408 subdir, subdir[0] ? "/" : "", name) == -1) {
409 err = got_error_from_errno("asprintf");
414 err = parse_ref_file(ref, absname, path, lock);
416 if (!ref_is_absolute && !ref_is_well_known)
422 const struct got_error *
423 got_ref_open(struct got_reference **ref, struct got_repository *repo,
424 const char *refname, int lock)
426 const struct got_error *err = NULL;
427 char *path_refs = NULL;
428 const char *subdirs[] = {
429 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
431 int i, well_known = is_well_known_ref(refname);
432 struct got_lockfile *lf = NULL;
436 path_refs = get_refs_dir_path(repo, refname);
437 if (path_refs == NULL) {
438 err = got_error_from_errno2("get_refs_dir_path", refname);
443 err = open_ref(ref, path_refs, "", refname, lock);
445 char *packed_refs_path;
448 /* Search on-disk refs before packed refs! */
449 for (i = 0; i < nitems(subdirs); i++) {
450 err = open_ref(ref, path_refs, subdirs[i], refname,
456 packed_refs_path = got_repo_get_path_packed_refs(repo);
457 if (packed_refs_path == NULL) {
458 err = got_error_from_errno(
459 "got_repo_get_path_packed_refs");
464 err = got_lockfile_lock(&lf, packed_refs_path);
468 f = fopen(packed_refs_path, "rb");
469 free(packed_refs_path);
471 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
474 if (fclose(f) != 0) {
475 err = got_error_from_errno("fclose");
484 if (!err && *ref == NULL)
485 err = got_error_not_ref(refname);
487 got_lockfile_unlock(lf);
493 got_ref_close(struct got_reference *ref)
495 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
496 free(ref->ref.symref.name);
497 free(ref->ref.symref.ref);
499 free(ref->ref.ref.name);
503 struct got_reference *
504 got_ref_dup(struct got_reference *ref)
506 struct got_reference *ret;
508 ret = calloc(1, sizeof(*ret));
512 ret->flags = ref->flags;
513 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
514 ret->ref.symref.name = strdup(ref->ref.symref.name);
515 if (ret->ref.symref.name == NULL) {
519 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
520 if (ret->ref.symref.ref == NULL) {
521 free(ret->ref.symref.name);
526 ref->ref.ref.name = strdup(ref->ref.ref.name);
527 if (ref->ref.ref.name == NULL) {
531 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
532 sizeof(ret->ref.ref.sha1));
538 const struct got_error *
539 got_reflist_entry_dup(struct got_reflist_entry **newp,
540 struct got_reflist_entry *re)
542 const struct got_error *err = NULL;
543 struct got_reflist_entry *new;
547 new = malloc(sizeof(*new));
549 return got_error_from_errno("malloc");
551 new->ref = got_ref_dup(re->ref);
552 if (new->ref == NULL) {
553 err = got_error_from_errno("got_ref_dup");
558 new->id = got_object_id_dup(re->id);
559 if (new->id == NULL) {
560 err = got_error_from_errno("got_ref_dup");
570 static const struct got_error *
571 resolve_symbolic_ref(struct got_reference **resolved,
572 struct got_repository *repo, struct got_reference *ref)
574 struct got_reference *nextref;
575 const struct got_error *err;
577 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
581 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
582 err = resolve_symbolic_ref(resolved, repo, nextref);
584 *resolved = got_ref_dup(nextref);
586 got_ref_close(nextref);
590 static const struct got_error *
591 ref_resolve(struct got_object_id **id, struct got_repository *repo,
592 struct got_reference *ref, int recursion)
594 const struct got_error *err;
597 return got_error_msg(GOT_ERR_RECURSION,
598 "reference recursion limit reached");
600 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
601 struct got_reference *resolved = NULL;
602 err = resolve_symbolic_ref(&resolved, repo, ref);
604 err = ref_resolve(id, repo, resolved, --recursion);
606 got_ref_close(resolved);
610 *id = calloc(1, sizeof(**id));
612 return got_error_from_errno("calloc");
613 memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
617 const struct got_error *
618 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
619 struct got_reference *ref)
621 return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
625 got_ref_to_str(struct got_reference *ref)
629 if (ref->flags & GOT_REF_IS_SYMBOLIC)
630 return strdup(ref->ref.symref.ref);
632 str = malloc(SHA1_DIGEST_STRING_LENGTH);
636 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
637 SHA1_DIGEST_STRING_LENGTH) == NULL) {
646 got_ref_get_name(struct got_reference *ref)
648 if (ref->flags & GOT_REF_IS_SYMBOLIC)
649 return ref->ref.symref.name;
651 return ref->ref.ref.name;
655 got_ref_get_symref_target(struct got_reference *ref)
657 if (ref->flags & GOT_REF_IS_SYMBOLIC)
658 return ref->ref.symref.ref;
663 const struct got_error *
664 got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
665 struct got_reference* re2)
667 const char *name1 = got_ref_get_name(re1);
668 const char *name2 = got_ref_get_name(re2);
670 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
674 static const struct got_error *
675 insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
676 struct got_reference *ref, struct got_repository *repo,
677 got_ref_cmp_cb cmp_cb, void *cmp_arg)
679 const struct got_error *err;
680 struct got_object_id *id;
681 struct got_reflist_entry *new, *re, *prev = NULL;
686 err = got_ref_resolve(&id, repo, ref);
690 new = malloc(sizeof(*new));
693 return got_error_from_errno("malloc");
700 * We must de-duplicate entries on insert because packed-refs may
701 * contain redundant entries. On-disk refs take precedence.
702 * This code assumes that on-disk revs are read before packed-refs.
703 * We're iterating the list anyway, so insert elements sorted by name.
705 re = SIMPLEQ_FIRST(refs);
707 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
716 } else if (cmp > 0) {
718 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
720 SIMPLEQ_INSERT_HEAD(refs, new, entry);
724 re = SIMPLEQ_NEXT(re, entry);
728 SIMPLEQ_INSERT_TAIL(refs, new, entry);
732 static const struct got_error *
733 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
734 const char *subdir, struct got_repository *repo,
735 got_ref_cmp_cb cmp_cb, void *cmp_arg)
737 const struct got_error *err = NULL;
741 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
742 return got_error_from_errno("asprintf");
744 d = opendir(path_subdir);
750 struct got_reference *ref;
757 if (strcmp(dent->d_name, ".") == 0 ||
758 strcmp(dent->d_name, "..") == 0)
761 switch (dent->d_type) {
763 err = open_ref(&ref, path_refs, subdir, dent->d_name,
768 struct got_reflist_entry *new;
769 err = insert_ref(&new, refs, ref, repo,
771 if (err || new == NULL /* duplicate */)
778 if (asprintf(&child, "%s%s%s", subdir,
779 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
780 err = got_error_from_errno("asprintf");
783 err = gather_on_disk_refs(refs, path_refs, child, repo,
798 const struct got_error *
799 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
800 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
802 const struct got_error *err;
803 char *packed_refs_path, *path_refs = NULL;
804 const char *ondisk_ref_namespace = NULL;
806 struct got_reference *ref;
807 struct got_reflist_entry *new;
809 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
810 /* HEAD ref should always exist. */
811 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
812 if (path_refs == NULL) {
813 err = got_error_from_errno("get_refs_dir_path");
816 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
819 err = insert_ref(&new, refs, ref, repo, cmp_cb, cmp_arg);
820 if (err || new == NULL /* duplicate */)
826 ondisk_ref_namespace = ref_namespace;
827 if (ref_namespace && strncmp(ref_namespace, "refs/", 5) == 0)
828 ondisk_ref_namespace += 5;
830 /* Gather on-disk refs before parsing packed-refs. */
832 path_refs = get_refs_dir_path(repo, "");
833 if (path_refs == NULL) {
834 err = got_error_from_errno("get_refs_dir_path");
837 err = gather_on_disk_refs(refs, path_refs,
838 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
844 * The packed-refs file may contain redundant entries, in which
845 * case on-disk refs take precedence.
847 packed_refs_path = got_repo_get_path_packed_refs(repo);
848 if (packed_refs_path == NULL) {
849 err = got_error_from_errno("got_repo_get_path_packed_refs");
853 f = fopen(packed_refs_path, "r");
854 free(packed_refs_path);
858 const char delim[3] = {'\0', '\0', '\0'};
860 line = fparseln(f, &len, NULL, delim, 0);
864 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
867 err = parse_packed_ref_line(&ref, NULL, line);
874 name = got_ref_get_name(ref);
875 if (strncmp(name, ref_namespace,
876 strlen(ref_namespace)) != 0) {
881 err = insert_ref(&new, refs, ref, repo,
883 if (err || new == NULL /* duplicate */)
892 if (f && fclose(f) != 0 && err == NULL)
893 err = got_error_from_errno("fclose");
898 got_ref_list_free(struct got_reflist_head *refs)
900 struct got_reflist_entry *re;
902 while (!SIMPLEQ_EMPTY(refs)) {
903 re = SIMPLEQ_FIRST(refs);
904 SIMPLEQ_REMOVE_HEAD(refs, entry);
905 got_ref_close(re->ref);
913 got_ref_is_symbolic(struct got_reference *ref)
915 return (ref->flags & GOT_REF_IS_SYMBOLIC);
918 const struct got_error *
919 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
921 if (ref->flags & GOT_REF_IS_SYMBOLIC)
922 return got_error(GOT_ERR_BAD_REF_TYPE);
924 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
928 const struct got_error *
929 got_ref_change_symref(struct got_reference *ref, char *refname)
933 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
934 return got_error(GOT_ERR_BAD_REF_TYPE);
936 new_name = strdup(refname);
937 if (new_name == NULL)
938 return got_error_from_errno("strdup");
940 free(ref->ref.symref.name);
941 ref->ref.symref.name = new_name;
945 const struct got_error *
946 got_ref_write(struct got_reference *ref, struct got_repository *repo)
948 const struct got_error *err = NULL, *unlock_err = NULL;
949 const char *name = got_ref_get_name(ref);
950 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
951 struct got_lockfile *lf = NULL;
956 path_refs = get_refs_dir_path(repo, name);
957 if (path_refs == NULL) {
958 err = got_error_from_errno2("get_refs_dir_path", name);
962 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
963 err = got_error_from_errno("asprintf");
967 err = got_opentemp_named(&tmppath, &f, path);
970 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
972 err = got_path_dirname(&parent, path);
975 err = got_path_mkdir(parent);
979 err = got_opentemp_named(&tmppath, &f, path);
984 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
985 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
986 if (n != strlen(ref->ref.symref.ref) + 6) {
987 err = got_ferror(f, GOT_ERR_IO);
991 char hex[SHA1_DIGEST_STRING_LENGTH];
992 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
993 sizeof(hex)) == NULL) {
994 err = got_error(GOT_ERR_BAD_REF_DATA);
997 n = fprintf(f, "%s\n", hex);
998 if (n != sizeof(hex)) {
999 err = got_ferror(f, GOT_ERR_IO);
1004 if (ref->lf == NULL) {
1005 err = got_lockfile_lock(&lf, path);
1010 /* XXX: check if old content matches our expectations? */
1012 if (stat(path, &sb) != 0) {
1013 if (errno != ENOENT) {
1014 err = got_error_from_errno2("stat", path);
1017 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1020 if (rename(tmppath, path) != 0) {
1021 err = got_error_from_errno3("rename", tmppath, path);
1027 if (chmod(path, sb.st_mode) != 0) {
1028 err = got_error_from_errno2("chmod", path);
1032 if (ref->lf == NULL && lf)
1033 unlock_err = got_lockfile_unlock(lf);
1035 if (fclose(f) != 0 && err == NULL)
1036 err = got_error_from_errno("fclose");
1041 if (unlink(tmppath) != 0 && err == NULL)
1042 err = got_error_from_errno2("unlink", tmppath);
1045 return err ? err : unlock_err;
1048 static const struct got_error *
1049 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1051 const struct got_error *err = NULL, *unlock_err = NULL;
1052 struct got_lockfile *lf = NULL;
1053 FILE *f = NULL, *tmpf = NULL;
1054 char *packed_refs_path, *tmppath = NULL;
1055 struct got_reflist_head refs;
1056 int found_delref = 0;
1058 /* The packed-refs file does not cotain symbolic references. */
1059 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1060 return got_error(GOT_ERR_BAD_REF_DATA);
1062 SIMPLEQ_INIT(&refs);
1064 packed_refs_path = got_repo_get_path_packed_refs(repo);
1065 if (packed_refs_path == NULL)
1066 return got_error_from_errno("got_repo_get_path_packed_refs");
1068 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
1072 if (delref->lf == NULL) {
1073 err = got_lockfile_lock(&lf, packed_refs_path);
1078 f = fopen(packed_refs_path, "r");
1080 err = got_error_from_errno2("fopen", packed_refs_path);
1086 const char delim[3] = {'\0', '\0', '\0'};
1087 struct got_reference *ref;
1088 struct got_reflist_entry *new;
1090 line = fparseln(f, &len, NULL, delim, 0);
1094 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1097 err = parse_packed_ref_line(&ref, NULL, line);
1104 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1105 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1106 sizeof(delref->ref.ref.sha1)) == 0) {
1112 err = insert_ref(&new, &refs, ref, repo,
1113 got_ref_cmp_by_name, NULL);
1114 if (err || new == NULL /* duplicate */)
1121 struct got_reflist_entry *re;
1125 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1126 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1127 err = got_ferror(f, GOT_ERR_IO);
1131 SIMPLEQ_FOREACH(re, &refs, entry) {
1132 uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
1134 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1135 sizeof(hex)) == NULL) {
1136 err = got_error(GOT_ERR_BAD_REF_DATA);
1139 n = fprintf(tmpf, "%s ", hex);
1140 if (n != sizeof(hex)) {
1141 err = got_ferror(f, GOT_ERR_IO);
1144 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1145 if (n != strlen(re->ref->ref.ref.name) + 1) {
1146 err = got_ferror(f, GOT_ERR_IO);
1151 if (fflush(tmpf) != 0) {
1152 err = got_error_from_errno("fflush");
1156 if (stat(packed_refs_path, &sb) != 0) {
1157 if (errno != ENOENT) {
1158 err = got_error_from_errno2("stat",
1162 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1165 if (rename(tmppath, packed_refs_path) != 0) {
1166 err = got_error_from_errno3("rename", tmppath,
1171 if (chmod(packed_refs_path, sb.st_mode) != 0) {
1172 err = got_error_from_errno2("chmod",
1178 if (delref->lf == NULL && lf)
1179 unlock_err = got_lockfile_unlock(lf);
1181 if (fclose(f) != 0 && err == NULL)
1182 err = got_error_from_errno("fclose");
1186 if (fclose(tmpf) != 0 && err == NULL)
1187 err = got_error_from_errno("fclose");
1190 free(packed_refs_path);
1191 got_ref_list_free(&refs);
1192 return err ? err : unlock_err;
1195 const struct got_error *
1196 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1198 const struct got_error *err = NULL, *unlock_err = NULL;
1199 const char *name = got_ref_get_name(ref);
1200 char *path_refs = NULL, *path = NULL;
1201 struct got_lockfile *lf = NULL;
1203 if (ref->flags & GOT_REF_IS_PACKED)
1204 return delete_packed_ref(ref, repo);
1206 path_refs = get_refs_dir_path(repo, name);
1207 if (path_refs == NULL) {
1208 err = got_error_from_errno2("get_refs_dir_path", name);
1212 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1213 err = got_error_from_errno("asprintf");
1217 if (ref->lf == NULL) {
1218 err = got_lockfile_lock(&lf, path);
1223 /* XXX: check if old content matches our expectations? */
1225 if (unlink(path) != 0)
1226 err = got_error_from_errno2("unlink", path);
1228 if (ref->lf == NULL && lf)
1229 unlock_err = got_lockfile_unlock(lf);
1233 return err ? err : unlock_err;
1236 const struct got_error *
1237 got_ref_unlock(struct got_reference *ref)
1239 const struct got_error *err;
1240 err = got_lockfile_unlock(ref->lf);