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>
35 #include "got_error.h"
36 #include "got_object.h"
37 #include "got_repository.h"
38 #include "got_reference.h"
39 #include "got_opentemp.h"
42 #include "got_lib_sha1.h"
43 #include "got_lib_delta.h"
44 #include "got_lib_inflate.h"
45 #include "got_lib_object.h"
46 #include "got_lib_lockfile.h"
49 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
52 #define GOT_REF_HEADS "heads"
53 #define GOT_REF_TAGS "tags"
54 #define GOT_REF_REMOTES "remotes"
57 * We do not resolve tags yet, and don't yet care about sorting refs either,
58 * so packed-refs files we write contain a minimal header which disables all
59 * packed-refs "traits" supported by Git.
61 #define GOT_PACKED_REFS_HEADER "# pack-refs with:"
63 /* A symbolic reference. */
69 #define GOT_REF_RECURSE_MAX 20
71 /* A non-symbolic reference (there is no better designation). */
74 u_int8_t sha1[SHA1_DIGEST_LENGTH];
77 /* A reference which points to an arbitrary object. */
78 struct got_reference {
80 #define GOT_REF_IS_SYMBOLIC 0x01
81 #define GOT_REF_IS_PACKED 0x02
85 struct got_symref symref;
88 struct got_lockfile *lf;
91 static const struct got_error *
92 alloc_ref(struct got_reference **ref, const char *name,
93 struct got_object_id *id, int flags)
95 const struct got_error *err = NULL;
97 *ref = calloc(1, sizeof(**ref));
99 return got_error_from_errno("calloc");
101 memcpy((*ref)->ref.ref.sha1, id->sha1, sizeof((*ref)->ref.ref.sha1));
102 (*ref)->flags = flags;
103 (*ref)->ref.ref.name = strdup(name);
104 if ((*ref)->ref.ref.name == NULL) {
105 err = got_error_from_errno("strdup");
112 static const struct got_error *
113 alloc_symref(struct got_reference **ref, const char *name,
114 const char *target_ref, int flags)
116 const struct got_error *err = NULL;
118 *ref = calloc(1, sizeof(**ref));
120 return got_error_from_errno("calloc");
122 (*ref)->flags = GOT_REF_IS_SYMBOLIC | flags;
123 (*ref)->ref.symref.name = strdup(name);
124 if ((*ref)->ref.symref.name == NULL) {
125 err = got_error_from_errno("strdup");
130 (*ref)->ref.symref.ref = strdup(target_ref);
131 if ((*ref)->ref.symref.ref == NULL) {
132 err = got_error_from_errno("strdup");
139 static const struct got_error *
140 parse_symref(struct got_reference **ref, const char *name, const char *line)
143 return got_error(GOT_ERR_BAD_REF_DATA);
145 return alloc_symref(ref, name, line, 0);
148 static const struct got_error *
149 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
151 struct got_object_id id;
153 if (strncmp(line, "ref: ", 5) == 0) {
155 return parse_symref(ref, name, line);
158 if (!got_parse_sha1_digest(id.sha1, line))
159 return got_error(GOT_ERR_BAD_REF_DATA);
161 return alloc_ref(ref, name, &id, 0);
164 static const struct got_error *
165 parse_ref_file(struct got_reference **ref, const char *name,
166 const char *absname, const char *abspath, int lock)
168 const struct got_error *err = NULL;
173 struct got_lockfile *lf = NULL;
176 err = got_lockfile_lock(&lf, abspath);
178 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
179 err = got_error_not_ref(name);
184 f = fopen(abspath, "rb");
186 if (errno != ENOTDIR && errno != ENOENT)
187 err = got_error_from_errno2("fopen", abspath);
189 err = got_error_not_ref(name);
191 got_lockfile_unlock(lf);
195 linelen = getline(&line, &linesize, f);
198 err = NULL; /* ignore empty files (could be locks) */
201 err = got_error(GOT_ERR_NOT_REF);
203 err = got_ferror(f, GOT_ERR_IO);
205 err = got_error_from_errno2("getline", abspath);
208 got_lockfile_unlock(lf);
211 while (linelen > 0 && line[linelen - 1] == '\n') {
212 line[linelen - 1] = '\0';
216 err = parse_ref_line(ref, absname, line);
219 got_lockfile_unlock(lf);
224 got_lockfile_unlock(lf);
229 if (fclose(f) != 0 && err == NULL) {
230 err = got_error_from_errno("fclose");
233 got_ref_unlock(*ref);
242 is_well_known_ref(const char *refname)
244 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
245 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
246 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
247 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
251 get_refs_dir_path(struct got_repository *repo, const char *refname)
253 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
254 return strdup(got_repo_get_path_git_dir(repo));
256 return got_repo_get_path_refs(repo);
260 is_valid_ref_name(const char *name)
263 const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
264 const char *forbidden_seq[] = { "//", "..", "@{" };
265 const char *lfs = GOT_LOCKFILE_SUFFIX;
266 const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
269 if (name[0] == '@' && name[1] == '\0')
274 if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
277 for (i = 0; i < nitems(forbidden); i++) {
278 if (*s == forbidden[i])
281 for (i = 0; i < nitems(forbidden_seq); i++) {
282 if (s[0] == forbidden_seq[i][0] &&
283 s[1] == forbidden_seq[i][1])
286 if (iscntrl((unsigned char)s[0]))
288 if (s[0] == '.' && s[1] == '\0')
291 const char *nextseg = s + 1;
292 if (nextseg[0] == '\0' || nextseg[0] == '.' ||
295 if (seg <= s - lfs_len &&
296 strncmp(s - lfs_len, lfs, lfs_len) == 0)
303 if (seg <= s - lfs_len &&
304 strncmp(s - lfs_len, lfs, lfs_len) == 0)
310 const struct got_error *
311 got_ref_alloc(struct got_reference **ref, const char *name,
312 struct got_object_id *id)
314 if (!is_valid_ref_name(name))
315 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
317 return alloc_ref(ref, name, id, 0);
320 const struct got_error *
321 got_ref_alloc_symref(struct got_reference **ref, const char *name,
322 struct got_reference *target_ref)
324 if (!is_valid_ref_name(name))
325 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
327 return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
330 static const struct got_error *
331 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
334 struct got_object_id id;
339 if (line[0] == '#' || line[0] == '^')
342 if (!got_parse_sha1_digest(id.sha1, line))
343 return got_error(GOT_ERR_BAD_REF_DATA);
346 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
350 name = line + SHA1_DIGEST_STRING_LENGTH;
352 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED);
355 static const struct got_error *
356 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
357 int nsubdirs, const char *refname)
359 const struct got_error *err = NULL;
363 const char delim[3] = {'\0', '\0', '\0'};
364 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
369 abs_refname = (char *)refname;
371 line = fparseln(f, &len, NULL, delim, 0);
375 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
378 for (i = 0; i < nsubdirs; i++) {
379 if (!ref_is_absolute &&
380 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
382 return got_error_from_errno("asprintf");
383 err = parse_packed_ref_line(ref, abs_refname, line);
384 if (!ref_is_absolute)
386 if (err || *ref != NULL)
392 } while (*ref == NULL);
397 static const struct got_error *
398 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
399 const char *name, int lock)
401 const struct got_error *err = NULL;
403 char *absname = NULL;
404 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
405 int ref_is_well_known = (subdir[0] == '\0' && is_well_known_ref(name));
409 if (ref_is_absolute || ref_is_well_known) {
410 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
411 return got_error_from_errno("asprintf");
412 absname = (char *)name;
414 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
415 subdir[0] ? "/" : "", name) == -1)
416 return got_error_from_errno("asprintf");
418 if (asprintf(&absname, "refs/%s%s%s",
419 subdir, subdir[0] ? "/" : "", name) == -1) {
420 err = got_error_from_errno("asprintf");
425 err = parse_ref_file(ref, name, absname, path, lock);
427 if (!ref_is_absolute && !ref_is_well_known)
433 const struct got_error *
434 got_ref_open(struct got_reference **ref, struct got_repository *repo,
435 const char *refname, int lock)
437 const struct got_error *err = NULL;
438 char *path_refs = NULL;
439 const char *subdirs[] = {
440 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
442 int i, well_known = is_well_known_ref(refname);
443 struct got_lockfile *lf = NULL;
447 path_refs = get_refs_dir_path(repo, refname);
448 if (path_refs == NULL) {
449 err = got_error_from_errno2("get_refs_dir_path", refname);
454 err = open_ref(ref, path_refs, "", refname, lock);
456 char *packed_refs_path;
459 /* Search on-disk refs before packed refs! */
460 for (i = 0; i < nitems(subdirs); i++) {
461 err = open_ref(ref, path_refs, subdirs[i], refname,
463 if ((err && err->code != GOT_ERR_NOT_REF) || *ref)
467 packed_refs_path = got_repo_get_path_packed_refs(repo);
468 if (packed_refs_path == NULL) {
469 err = got_error_from_errno(
470 "got_repo_get_path_packed_refs");
475 err = got_lockfile_lock(&lf, packed_refs_path);
479 f = fopen(packed_refs_path, "rb");
480 free(packed_refs_path);
482 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
485 if (fclose(f) != 0) {
486 err = got_error_from_errno("fclose");
495 if (!err && *ref == NULL)
496 err = got_error_not_ref(refname);
498 got_lockfile_unlock(lf);
504 got_ref_close(struct got_reference *ref)
506 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
507 free(ref->ref.symref.name);
508 free(ref->ref.symref.ref);
510 free(ref->ref.ref.name);
514 struct got_reference *
515 got_ref_dup(struct got_reference *ref)
517 struct got_reference *ret;
519 ret = calloc(1, sizeof(*ret));
523 ret->flags = ref->flags;
524 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
525 ret->ref.symref.name = strdup(ref->ref.symref.name);
526 if (ret->ref.symref.name == NULL) {
530 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
531 if (ret->ref.symref.ref == NULL) {
532 free(ret->ref.symref.name);
537 ref->ref.ref.name = strdup(ref->ref.ref.name);
538 if (ref->ref.ref.name == NULL) {
542 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
543 sizeof(ret->ref.ref.sha1));
549 const struct got_error *
550 got_reflist_entry_dup(struct got_reflist_entry **newp,
551 struct got_reflist_entry *re)
553 const struct got_error *err = NULL;
554 struct got_reflist_entry *new;
558 new = malloc(sizeof(*new));
560 return got_error_from_errno("malloc");
562 new->ref = got_ref_dup(re->ref);
563 if (new->ref == NULL) {
564 err = got_error_from_errno("got_ref_dup");
573 static const struct got_error *
574 resolve_symbolic_ref(struct got_reference **resolved,
575 struct got_repository *repo, struct got_reference *ref)
577 struct got_reference *nextref;
578 const struct got_error *err;
580 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
584 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
585 err = resolve_symbolic_ref(resolved, repo, nextref);
587 *resolved = got_ref_dup(nextref);
589 got_ref_close(nextref);
593 static const struct got_error *
594 ref_resolve(struct got_object_id **id, struct got_repository *repo,
595 struct got_reference *ref, int recursion)
597 const struct got_error *err;
600 return got_error_msg(GOT_ERR_RECURSION,
601 "reference recursion limit reached");
603 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
604 struct got_reference *resolved = NULL;
605 err = resolve_symbolic_ref(&resolved, repo, ref);
607 err = ref_resolve(id, repo, resolved, --recursion);
609 got_ref_close(resolved);
613 *id = calloc(1, sizeof(**id));
615 return got_error_from_errno("calloc");
616 memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
620 const struct got_error *
621 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
622 struct got_reference *ref)
624 return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
628 got_ref_to_str(struct got_reference *ref)
632 if (ref->flags & GOT_REF_IS_SYMBOLIC)
633 return strdup(ref->ref.symref.ref);
635 str = malloc(SHA1_DIGEST_STRING_LENGTH);
639 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
640 SHA1_DIGEST_STRING_LENGTH) == NULL) {
649 got_ref_get_name(struct got_reference *ref)
651 if (ref->flags & GOT_REF_IS_SYMBOLIC)
652 return ref->ref.symref.name;
654 return ref->ref.ref.name;
658 got_ref_get_symref_target(struct got_reference *ref)
660 if (ref->flags & GOT_REF_IS_SYMBOLIC)
661 return ref->ref.symref.ref;
666 const struct got_error *
667 got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
668 struct got_reference* re2)
670 const char *name1 = got_ref_get_name(re1);
671 const char *name2 = got_ref_get_name(re2);
673 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
677 const struct got_error *
678 got_ref_cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
679 struct got_reference *ref2)
681 const struct got_error *err = NULL;
682 struct got_repository *repo = arg;
683 struct got_object_id *id1, *id2 = NULL;
684 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
685 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
690 err = got_ref_resolve(&id1, repo, ref1);
693 err = got_object_open_as_tag(&tag1, repo, id1);
695 if (err->code != GOT_ERR_OBJ_TYPE)
697 /* "lightweight" tag */
698 err = got_object_open_as_commit(&commit1, repo, id1);
701 time1 = got_object_commit_get_committer_time(commit1);
703 time1 = got_object_tag_get_tagger_time(tag1);
705 err = got_ref_resolve(&id2, repo, ref2);
708 err = got_object_open_as_tag(&tag2, repo, id2);
710 if (err->code != GOT_ERR_OBJ_TYPE)
712 /* "lightweight" tag */
713 err = got_object_open_as_commit(&commit2, repo, id2);
716 time2 = got_object_commit_get_committer_time(commit2);
718 time2 = got_object_tag_get_tagger_time(tag2);
720 /* Put latest tags first. */
723 else if (time1 > time2)
726 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
731 got_object_tag_close(tag1);
733 got_object_tag_close(tag2);
735 got_object_commit_close(commit1);
737 got_object_commit_close(commit2);
741 static const struct got_error *
742 insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
743 struct got_reference *ref, struct got_repository *repo,
744 got_ref_cmp_cb cmp_cb, void *cmp_arg)
746 const struct got_error *err;
747 struct got_reflist_entry *new, *re, *prev = NULL;
752 new = malloc(sizeof(*new));
754 return got_error_from_errno("malloc");
759 * We must de-duplicate entries on insert because packed-refs may
760 * contain redundant entries. On-disk refs take precedence.
761 * This code assumes that on-disk revs are read before packed-refs.
762 * We're iterating the list anyway, so insert elements sorted by name.
764 re = SIMPLEQ_FIRST(refs);
766 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
774 } else if (cmp > 0) {
776 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
778 SIMPLEQ_INSERT_HEAD(refs, new, entry);
782 re = SIMPLEQ_NEXT(re, entry);
786 SIMPLEQ_INSERT_TAIL(refs, new, entry);
790 static const struct got_error *
791 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
792 const char *subdir, struct got_repository *repo,
793 got_ref_cmp_cb cmp_cb, void *cmp_arg)
795 const struct got_error *err = NULL;
799 while (subdir[0] == '/')
802 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
803 return got_error_from_errno("asprintf");
805 d = opendir(path_subdir);
811 struct got_reference *ref;
819 if (strcmp(dent->d_name, ".") == 0 ||
820 strcmp(dent->d_name, "..") == 0)
823 err = got_path_dirent_type(&type, path_subdir, dent);
829 err = open_ref(&ref, path_refs, subdir, dent->d_name,
834 struct got_reflist_entry *new;
835 err = insert_ref(&new, refs, ref, repo,
837 if (err || new == NULL /* duplicate */)
844 if (asprintf(&child, "%s%s%s", subdir,
845 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
846 err = got_error_from_errno("asprintf");
849 err = gather_on_disk_refs(refs, path_refs, child, repo,
864 const struct got_error *
865 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
866 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
868 const struct got_error *err;
869 char *packed_refs_path, *path_refs = NULL;
870 char *abs_namespace = NULL;
871 char *buf = NULL, *ondisk_ref_namespace = NULL;
873 struct got_reference *ref;
874 struct got_reflist_entry *new;
876 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
877 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
878 if (path_refs == NULL) {
879 err = got_error_from_errno("get_refs_dir_path");
882 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
885 err = insert_ref(&new, refs, ref, repo,
887 if (err || new == NULL /* duplicate */)
889 if (err && err->code != GOT_ERR_NOT_REF)
892 /* Try listing a single reference. */
893 const char *refname = ref_namespace;
894 path_refs = get_refs_dir_path(repo, refname);
895 if (path_refs == NULL) {
896 err = got_error_from_errno("get_refs_dir_path");
899 err = open_ref(&ref, path_refs, "", refname, 0);
901 if (err->code != GOT_ERR_NOT_REF)
903 /* Try to look up references in a given namespace. */
905 err = insert_ref(&new, refs, ref, repo,
907 if (err || new == NULL /* duplicate */)
915 /* Canonicalize the path to eliminate double-slashes if any. */
916 if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
917 err = got_error_from_errno("asprintf");
920 len = strlen(abs_namespace) + 1;
923 err = got_error_from_errno("malloc");
926 err = got_canonpath(abs_namespace, buf, len);
929 ondisk_ref_namespace = buf;
930 while (ondisk_ref_namespace[0] == '/')
931 ondisk_ref_namespace++;
932 if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
933 ondisk_ref_namespace += 5;
934 else if (strcmp(ondisk_ref_namespace, "refs") == 0)
935 ondisk_ref_namespace = "";
938 /* Gather on-disk refs before parsing packed-refs. */
940 path_refs = get_refs_dir_path(repo, "");
941 if (path_refs == NULL) {
942 err = got_error_from_errno("get_refs_dir_path");
945 err = gather_on_disk_refs(refs, path_refs,
946 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
952 * The packed-refs file may contain redundant entries, in which
953 * case on-disk refs take precedence.
955 packed_refs_path = got_repo_get_path_packed_refs(repo);
956 if (packed_refs_path == NULL) {
957 err = got_error_from_errno("got_repo_get_path_packed_refs");
961 f = fopen(packed_refs_path, "r");
962 free(packed_refs_path);
966 const char delim[3] = {'\0', '\0', '\0'};
968 line = fparseln(f, &len, NULL, delim, 0);
972 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
975 err = parse_packed_ref_line(&ref, NULL, line);
982 name = got_ref_get_name(ref);
983 if (!got_path_is_child(name,
985 strlen(ref_namespace))) {
990 err = insert_ref(&new, refs, ref, repo,
992 if (err || new == NULL /* duplicate */)
1000 free(abs_namespace);
1003 if (f && fclose(f) != 0 && err == NULL)
1004 err = got_error_from_errno("fclose");
1009 got_ref_list_free(struct got_reflist_head *refs)
1011 struct got_reflist_entry *re;
1013 while (!SIMPLEQ_EMPTY(refs)) {
1014 re = SIMPLEQ_FIRST(refs);
1015 SIMPLEQ_REMOVE_HEAD(refs, entry);
1016 got_ref_close(re->ref);
1023 got_ref_is_symbolic(struct got_reference *ref)
1025 return (ref->flags & GOT_REF_IS_SYMBOLIC);
1028 const struct got_error *
1029 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1031 if (ref->flags & GOT_REF_IS_SYMBOLIC)
1032 return got_error(GOT_ERR_BAD_REF_TYPE);
1034 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
1038 const struct got_error *
1039 got_ref_change_symref(struct got_reference *ref, const char *refname)
1043 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1044 return got_error(GOT_ERR_BAD_REF_TYPE);
1046 new_name = strdup(refname);
1047 if (new_name == NULL)
1048 return got_error_from_errno("strdup");
1050 free(ref->ref.symref.ref);
1051 ref->ref.symref.ref = new_name;
1055 const struct got_error *
1056 got_ref_change_symref_to_ref(struct got_reference *symref,
1057 struct got_object_id *id)
1059 if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1060 return got_error(GOT_ERR_BAD_REF_TYPE);
1062 symref->ref.ref.name = symref->ref.symref.name;
1063 memcpy(symref->ref.ref.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1064 symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1068 const struct got_error *
1069 got_ref_write(struct got_reference *ref, struct got_repository *repo)
1071 const struct got_error *err = NULL, *unlock_err = NULL;
1072 const char *name = got_ref_get_name(ref);
1073 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1074 struct got_lockfile *lf = NULL;
1079 path_refs = get_refs_dir_path(repo, name);
1080 if (path_refs == NULL) {
1081 err = got_error_from_errno2("get_refs_dir_path", name);
1085 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1086 err = got_error_from_errno("asprintf");
1090 err = got_opentemp_named(&tmppath, &f, path);
1093 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1095 err = got_path_dirname(&parent, path);
1098 err = got_path_mkdir(parent);
1102 err = got_opentemp_named(&tmppath, &f, path);
1107 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1108 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1109 if (n != strlen(ref->ref.symref.ref) + 6) {
1110 err = got_ferror(f, GOT_ERR_IO);
1114 char hex[SHA1_DIGEST_STRING_LENGTH];
1115 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
1116 sizeof(hex)) == NULL) {
1117 err = got_error(GOT_ERR_BAD_REF_DATA);
1120 n = fprintf(f, "%s\n", hex);
1121 if (n != sizeof(hex)) {
1122 err = got_ferror(f, GOT_ERR_IO);
1127 if (ref->lf == NULL) {
1128 err = got_lockfile_lock(&lf, path);
1133 /* XXX: check if old content matches our expectations? */
1135 if (stat(path, &sb) != 0) {
1136 if (errno != ENOENT) {
1137 err = got_error_from_errno2("stat", path);
1140 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1143 if (rename(tmppath, path) != 0) {
1144 err = got_error_from_errno3("rename", tmppath, path);
1150 if (chmod(path, sb.st_mode) != 0) {
1151 err = got_error_from_errno2("chmod", path);
1155 if (ref->lf == NULL && lf)
1156 unlock_err = got_lockfile_unlock(lf);
1158 if (fclose(f) != 0 && err == NULL)
1159 err = got_error_from_errno("fclose");
1164 if (unlink(tmppath) != 0 && err == NULL)
1165 err = got_error_from_errno2("unlink", tmppath);
1168 return err ? err : unlock_err;
1171 static const struct got_error *
1172 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1174 const struct got_error *err = NULL, *unlock_err = NULL;
1175 struct got_lockfile *lf = NULL;
1176 FILE *f = NULL, *tmpf = NULL;
1177 char *packed_refs_path, *tmppath = NULL;
1178 struct got_reflist_head refs;
1179 int found_delref = 0;
1181 /* The packed-refs file does not cotain symbolic references. */
1182 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1183 return got_error(GOT_ERR_BAD_REF_DATA);
1185 SIMPLEQ_INIT(&refs);
1187 packed_refs_path = got_repo_get_path_packed_refs(repo);
1188 if (packed_refs_path == NULL)
1189 return got_error_from_errno("got_repo_get_path_packed_refs");
1191 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
1195 if (delref->lf == NULL) {
1196 err = got_lockfile_lock(&lf, packed_refs_path);
1201 f = fopen(packed_refs_path, "r");
1203 err = got_error_from_errno2("fopen", packed_refs_path);
1209 const char delim[3] = {'\0', '\0', '\0'};
1210 struct got_reference *ref;
1211 struct got_reflist_entry *new;
1213 line = fparseln(f, &len, NULL, delim, 0);
1217 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1220 err = parse_packed_ref_line(&ref, NULL, line);
1227 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1228 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1229 sizeof(delref->ref.ref.sha1)) == 0) {
1235 err = insert_ref(&new, &refs, ref, repo,
1236 got_ref_cmp_by_name, NULL);
1237 if (err || new == NULL /* duplicate */)
1244 struct got_reflist_entry *re;
1248 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1249 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1250 err = got_ferror(f, GOT_ERR_IO);
1254 SIMPLEQ_FOREACH(re, &refs, entry) {
1255 uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
1257 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1258 sizeof(hex)) == NULL) {
1259 err = got_error(GOT_ERR_BAD_REF_DATA);
1262 n = fprintf(tmpf, "%s ", hex);
1263 if (n != sizeof(hex)) {
1264 err = got_ferror(f, GOT_ERR_IO);
1267 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1268 if (n != strlen(re->ref->ref.ref.name) + 1) {
1269 err = got_ferror(f, GOT_ERR_IO);
1274 if (fflush(tmpf) != 0) {
1275 err = got_error_from_errno("fflush");
1279 if (stat(packed_refs_path, &sb) != 0) {
1280 if (errno != ENOENT) {
1281 err = got_error_from_errno2("stat",
1285 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1288 if (rename(tmppath, packed_refs_path) != 0) {
1289 err = got_error_from_errno3("rename", tmppath,
1294 if (chmod(packed_refs_path, sb.st_mode) != 0) {
1295 err = got_error_from_errno2("chmod",
1301 if (delref->lf == NULL && lf)
1302 unlock_err = got_lockfile_unlock(lf);
1304 if (fclose(f) != 0 && err == NULL)
1305 err = got_error_from_errno("fclose");
1309 if (fclose(tmpf) != 0 && err == NULL)
1310 err = got_error_from_errno("fclose");
1313 free(packed_refs_path);
1314 got_ref_list_free(&refs);
1315 return err ? err : unlock_err;
1318 static const struct got_error *
1319 delete_loose_ref(struct got_reference *ref, struct got_repository *repo)
1321 const struct got_error *err = NULL, *unlock_err = NULL;
1322 const char *name = got_ref_get_name(ref);
1323 char *path_refs = NULL, *path = NULL;
1324 struct got_lockfile *lf = NULL;
1326 path_refs = get_refs_dir_path(repo, name);
1327 if (path_refs == NULL) {
1328 err = got_error_from_errno2("get_refs_dir_path", name);
1332 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1333 err = got_error_from_errno("asprintf");
1337 if (ref->lf == NULL) {
1338 err = got_lockfile_lock(&lf, path);
1343 /* XXX: check if old content matches our expectations? */
1345 if (unlink(path) != 0)
1346 err = got_error_from_errno2("unlink", path);
1348 if (ref->lf == NULL && lf)
1349 unlock_err = got_lockfile_unlock(lf);
1353 return err ? err : unlock_err;
1356 const struct got_error *
1357 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1359 const struct got_error *err = NULL;
1360 struct got_reference *ref2;
1362 if (ref->flags & GOT_REF_IS_PACKED) {
1363 err = delete_packed_ref(ref, repo);
1367 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1369 if (err->code == GOT_ERR_NOT_REF)
1374 err = delete_loose_ref(ref2, repo);
1375 got_ref_close(ref2);
1378 err = delete_loose_ref(ref, repo);
1382 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1384 if (err->code == GOT_ERR_NOT_REF)
1389 err = delete_packed_ref(ref2, repo);
1390 got_ref_close(ref2);
1395 const struct got_error *
1396 got_ref_unlock(struct got_reference *ref)
1398 const struct got_error *err;
1399 err = got_lockfile_unlock(ref->lf);