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);
177 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
178 err = got_error(GOT_ERR_NOT_REF);
183 f = fopen(abspath, "rb");
186 got_lockfile_unlock(lf);
190 linelen = getline(&line, &linesize, f);
193 err = NULL; /* ignore empty files (could be locks) */
195 err = got_error_from_errno2("getline", abspath);
197 got_lockfile_unlock(lf);
200 while (linelen > 0 && line[linelen - 1] == '\n') {
201 line[linelen - 1] = '\0';
205 err = parse_ref_line(ref, name, line);
208 got_lockfile_unlock(lf);
213 got_lockfile_unlock(lf);
218 if (fclose(f) != 0 && err == NULL) {
219 err = got_error_from_errno("fclose");
222 got_ref_unlock(*ref);
231 is_well_known_ref(const char *refname)
233 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
234 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
235 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
236 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
240 get_refs_dir_path(struct got_repository *repo, const char *refname)
242 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
243 return strdup(got_repo_get_path_git_dir(repo));
245 return got_repo_get_path_refs(repo);
249 is_valid_ref_name(const char *name)
252 const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
253 const char *forbidden_seq[] = { "//", "..", "@{" };
254 const char *lfs = GOT_LOCKFILE_SUFFIX;
255 const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
258 if (name[0] == '@' && name[1] == '\0')
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 const struct got_error *
675 got_ref_cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
676 struct got_reference *ref2)
678 const struct got_error *err = NULL;
679 struct got_repository *repo = arg;
680 struct got_object_id *id1, *id2 = NULL;
681 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
682 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
687 err = got_ref_resolve(&id1, repo, ref1);
690 err = got_object_open_as_tag(&tag1, repo, id1);
692 if (err->code != GOT_ERR_OBJ_TYPE)
694 /* "lightweight" tag */
695 err = got_object_open_as_commit(&commit1, repo, id1);
698 time1 = got_object_commit_get_committer_time(commit1);
700 time1 = got_object_tag_get_tagger_time(tag1);
702 err = got_ref_resolve(&id2, repo, ref2);
705 err = got_object_open_as_tag(&tag2, repo, id2);
707 if (err->code != GOT_ERR_OBJ_TYPE)
709 /* "lightweight" tag */
710 err = got_object_open_as_commit(&commit2, repo, id2);
713 time2 = got_object_commit_get_committer_time(commit2);
715 time2 = got_object_tag_get_tagger_time(tag2);
717 /* Put latest tags first. */
720 else if (time1 > time2)
723 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
728 got_object_tag_close(tag1);
730 got_object_tag_close(tag2);
732 got_object_commit_close(commit1);
734 got_object_commit_close(commit2);
738 static const struct got_error *
739 insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
740 struct got_reference *ref, struct got_repository *repo,
741 got_ref_cmp_cb cmp_cb, void *cmp_arg)
743 const struct got_error *err;
744 struct got_object_id *id;
745 struct got_reflist_entry *new, *re, *prev = NULL;
750 err = got_ref_resolve(&id, repo, ref);
754 new = malloc(sizeof(*new));
757 return got_error_from_errno("malloc");
764 * We must de-duplicate entries on insert because packed-refs may
765 * contain redundant entries. On-disk refs take precedence.
766 * This code assumes that on-disk revs are read before packed-refs.
767 * We're iterating the list anyway, so insert elements sorted by name.
769 re = SIMPLEQ_FIRST(refs);
771 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
780 } else if (cmp > 0) {
782 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
784 SIMPLEQ_INSERT_HEAD(refs, new, entry);
788 re = SIMPLEQ_NEXT(re, entry);
792 SIMPLEQ_INSERT_TAIL(refs, new, entry);
796 static const struct got_error *
797 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
798 const char *subdir, struct got_repository *repo,
799 got_ref_cmp_cb cmp_cb, void *cmp_arg)
801 const struct got_error *err = NULL;
805 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
806 return got_error_from_errno("asprintf");
808 d = opendir(path_subdir);
814 struct got_reference *ref;
821 if (strcmp(dent->d_name, ".") == 0 ||
822 strcmp(dent->d_name, "..") == 0)
825 switch (dent->d_type) {
827 err = open_ref(&ref, path_refs, subdir, dent->d_name,
832 struct got_reflist_entry *new;
833 err = insert_ref(&new, refs, ref, repo,
835 if (err || new == NULL /* duplicate */)
842 if (asprintf(&child, "%s%s%s", subdir,
843 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
844 err = got_error_from_errno("asprintf");
847 err = gather_on_disk_refs(refs, path_refs, child, repo,
862 const struct got_error *
863 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
864 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
866 const struct got_error *err;
867 char *packed_refs_path, *path_refs = NULL;
868 const char *ondisk_ref_namespace = NULL;
870 struct got_reference *ref;
871 struct got_reflist_entry *new;
873 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
874 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
875 if (path_refs == NULL) {
876 err = got_error_from_errno("get_refs_dir_path");
879 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
882 err = insert_ref(&new, refs, ref, repo,
884 if (err || new == NULL /* duplicate */)
886 if (err && err->code != GOT_ERR_NOT_REF)
890 ondisk_ref_namespace = ref_namespace;
891 if (ref_namespace && strncmp(ref_namespace, "refs/", 5) == 0)
892 ondisk_ref_namespace += 5;
894 /* Gather on-disk refs before parsing packed-refs. */
896 path_refs = get_refs_dir_path(repo, "");
897 if (path_refs == NULL) {
898 err = got_error_from_errno("get_refs_dir_path");
901 err = gather_on_disk_refs(refs, path_refs,
902 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
908 * The packed-refs file may contain redundant entries, in which
909 * case on-disk refs take precedence.
911 packed_refs_path = got_repo_get_path_packed_refs(repo);
912 if (packed_refs_path == NULL) {
913 err = got_error_from_errno("got_repo_get_path_packed_refs");
917 f = fopen(packed_refs_path, "r");
918 free(packed_refs_path);
922 const char delim[3] = {'\0', '\0', '\0'};
924 line = fparseln(f, &len, NULL, delim, 0);
928 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
931 err = parse_packed_ref_line(&ref, NULL, line);
938 name = got_ref_get_name(ref);
939 if (strncmp(name, ref_namespace,
940 strlen(ref_namespace)) != 0) {
945 err = insert_ref(&new, refs, ref, repo,
947 if (err || new == NULL /* duplicate */)
956 if (f && fclose(f) != 0 && err == NULL)
957 err = got_error_from_errno("fclose");
962 got_ref_list_free(struct got_reflist_head *refs)
964 struct got_reflist_entry *re;
966 while (!SIMPLEQ_EMPTY(refs)) {
967 re = SIMPLEQ_FIRST(refs);
968 SIMPLEQ_REMOVE_HEAD(refs, entry);
969 got_ref_close(re->ref);
977 got_ref_is_symbolic(struct got_reference *ref)
979 return (ref->flags & GOT_REF_IS_SYMBOLIC);
982 const struct got_error *
983 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
985 if (ref->flags & GOT_REF_IS_SYMBOLIC)
986 return got_error(GOT_ERR_BAD_REF_TYPE);
988 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
992 const struct got_error *
993 got_ref_change_symref(struct got_reference *ref, char *refname)
997 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
998 return got_error(GOT_ERR_BAD_REF_TYPE);
1000 new_name = strdup(refname);
1001 if (new_name == NULL)
1002 return got_error_from_errno("strdup");
1004 free(ref->ref.symref.name);
1005 ref->ref.symref.name = new_name;
1009 const struct got_error *
1010 got_ref_change_symref_to_ref(struct got_reference *symref,
1011 struct got_object_id *id)
1013 if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1014 return got_error(GOT_ERR_BAD_REF_TYPE);
1016 symref->ref.ref.name = symref->ref.symref.name;
1017 memcpy(symref->ref.ref.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1018 symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1022 const struct got_error *
1023 got_ref_write(struct got_reference *ref, struct got_repository *repo)
1025 const struct got_error *err = NULL, *unlock_err = NULL;
1026 const char *name = got_ref_get_name(ref);
1027 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1028 struct got_lockfile *lf = NULL;
1033 path_refs = get_refs_dir_path(repo, name);
1034 if (path_refs == NULL) {
1035 err = got_error_from_errno2("get_refs_dir_path", name);
1039 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1040 err = got_error_from_errno("asprintf");
1044 err = got_opentemp_named(&tmppath, &f, path);
1047 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1049 err = got_path_dirname(&parent, path);
1052 err = got_path_mkdir(parent);
1056 err = got_opentemp_named(&tmppath, &f, path);
1061 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1062 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1063 if (n != strlen(ref->ref.symref.ref) + 6) {
1064 err = got_ferror(f, GOT_ERR_IO);
1068 char hex[SHA1_DIGEST_STRING_LENGTH];
1069 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
1070 sizeof(hex)) == NULL) {
1071 err = got_error(GOT_ERR_BAD_REF_DATA);
1074 n = fprintf(f, "%s\n", hex);
1075 if (n != sizeof(hex)) {
1076 err = got_ferror(f, GOT_ERR_IO);
1081 if (ref->lf == NULL) {
1082 err = got_lockfile_lock(&lf, path);
1087 /* XXX: check if old content matches our expectations? */
1089 if (stat(path, &sb) != 0) {
1090 if (errno != ENOENT) {
1091 err = got_error_from_errno2("stat", path);
1094 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1097 if (rename(tmppath, path) != 0) {
1098 err = got_error_from_errno3("rename", tmppath, path);
1104 if (chmod(path, sb.st_mode) != 0) {
1105 err = got_error_from_errno2("chmod", path);
1109 if (ref->lf == NULL && lf)
1110 unlock_err = got_lockfile_unlock(lf);
1112 if (fclose(f) != 0 && err == NULL)
1113 err = got_error_from_errno("fclose");
1118 if (unlink(tmppath) != 0 && err == NULL)
1119 err = got_error_from_errno2("unlink", tmppath);
1122 return err ? err : unlock_err;
1125 static const struct got_error *
1126 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1128 const struct got_error *err = NULL, *unlock_err = NULL;
1129 struct got_lockfile *lf = NULL;
1130 FILE *f = NULL, *tmpf = NULL;
1131 char *packed_refs_path, *tmppath = NULL;
1132 struct got_reflist_head refs;
1133 int found_delref = 0;
1135 /* The packed-refs file does not cotain symbolic references. */
1136 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1137 return got_error(GOT_ERR_BAD_REF_DATA);
1139 SIMPLEQ_INIT(&refs);
1141 packed_refs_path = got_repo_get_path_packed_refs(repo);
1142 if (packed_refs_path == NULL)
1143 return got_error_from_errno("got_repo_get_path_packed_refs");
1145 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
1149 if (delref->lf == NULL) {
1150 err = got_lockfile_lock(&lf, packed_refs_path);
1155 f = fopen(packed_refs_path, "r");
1157 err = got_error_from_errno2("fopen", packed_refs_path);
1163 const char delim[3] = {'\0', '\0', '\0'};
1164 struct got_reference *ref;
1165 struct got_reflist_entry *new;
1167 line = fparseln(f, &len, NULL, delim, 0);
1171 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1174 err = parse_packed_ref_line(&ref, NULL, line);
1181 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1182 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1183 sizeof(delref->ref.ref.sha1)) == 0) {
1189 err = insert_ref(&new, &refs, ref, repo,
1190 got_ref_cmp_by_name, NULL);
1191 if (err || new == NULL /* duplicate */)
1198 struct got_reflist_entry *re;
1202 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1203 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1204 err = got_ferror(f, GOT_ERR_IO);
1208 SIMPLEQ_FOREACH(re, &refs, entry) {
1209 uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
1211 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1212 sizeof(hex)) == NULL) {
1213 err = got_error(GOT_ERR_BAD_REF_DATA);
1216 n = fprintf(tmpf, "%s ", hex);
1217 if (n != sizeof(hex)) {
1218 err = got_ferror(f, GOT_ERR_IO);
1221 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1222 if (n != strlen(re->ref->ref.ref.name) + 1) {
1223 err = got_ferror(f, GOT_ERR_IO);
1228 if (fflush(tmpf) != 0) {
1229 err = got_error_from_errno("fflush");
1233 if (stat(packed_refs_path, &sb) != 0) {
1234 if (errno != ENOENT) {
1235 err = got_error_from_errno2("stat",
1239 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1242 if (rename(tmppath, packed_refs_path) != 0) {
1243 err = got_error_from_errno3("rename", tmppath,
1248 if (chmod(packed_refs_path, sb.st_mode) != 0) {
1249 err = got_error_from_errno2("chmod",
1255 if (delref->lf == NULL && lf)
1256 unlock_err = got_lockfile_unlock(lf);
1258 if (fclose(f) != 0 && err == NULL)
1259 err = got_error_from_errno("fclose");
1263 if (fclose(tmpf) != 0 && err == NULL)
1264 err = got_error_from_errno("fclose");
1267 free(packed_refs_path);
1268 got_ref_list_free(&refs);
1269 return err ? err : unlock_err;
1272 const struct got_error *
1273 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1275 const struct got_error *err = NULL, *unlock_err = NULL;
1276 const char *name = got_ref_get_name(ref);
1277 char *path_refs = NULL, *path = NULL;
1278 struct got_lockfile *lf = NULL;
1280 if (ref->flags & GOT_REF_IS_PACKED)
1281 return delete_packed_ref(ref, repo);
1283 path_refs = get_refs_dir_path(repo, name);
1284 if (path_refs == NULL) {
1285 err = got_error_from_errno2("get_refs_dir_path", name);
1289 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1290 err = got_error_from_errno("asprintf");
1294 if (ref->lf == NULL) {
1295 err = got_lockfile_lock(&lf, path);
1300 /* XXX: check if old content matches our expectations? */
1302 if (unlink(path) != 0)
1303 err = got_error_from_errno2("unlink", path);
1305 if (ref->lf == NULL && lf)
1306 unlock_err = got_lockfile_unlock(lf);
1310 return err ? err : unlock_err;
1313 const struct got_error *
1314 got_ref_unlock(struct got_reference *ref)
1316 const struct got_error *err;
1317 err = got_lockfile_unlock(ref->lf);