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 *absname, 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_not_ref(name);
183 f = fopen(abspath, "rb");
185 if (errno != ENOTDIR && errno != ENOENT)
186 err = got_error_from_errno2("fopen", abspath);
188 err = got_error_not_ref(name);
190 got_lockfile_unlock(lf);
194 linelen = getline(&line, &linesize, f);
197 err = NULL; /* ignore empty files (could be locks) */
200 err = got_error(GOT_ERR_NOT_REF);
202 err = got_ferror(f, GOT_ERR_IO);
204 err = got_error_from_errno2("getline", abspath);
207 got_lockfile_unlock(lf);
210 while (linelen > 0 && line[linelen - 1] == '\n') {
211 line[linelen - 1] = '\0';
215 err = parse_ref_line(ref, absname, line);
218 got_lockfile_unlock(lf);
223 got_lockfile_unlock(lf);
228 if (fclose(f) != 0 && err == NULL) {
229 err = got_error_from_errno("fclose");
232 got_ref_unlock(*ref);
241 is_well_known_ref(const char *refname)
243 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
244 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
245 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
246 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
250 get_refs_dir_path(struct got_repository *repo, const char *refname)
252 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
253 return strdup(got_repo_get_path_git_dir(repo));
255 return got_repo_get_path_refs(repo);
259 is_valid_ref_name(const char *name)
262 const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
263 const char *forbidden_seq[] = { "//", "..", "@{" };
264 const char *lfs = GOT_LOCKFILE_SUFFIX;
265 const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
268 if (name[0] == '@' && name[1] == '\0')
273 if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
276 for (i = 0; i < nitems(forbidden); i++) {
277 if (*s == forbidden[i])
280 for (i = 0; i < nitems(forbidden_seq); i++) {
281 if (s[0] == forbidden_seq[i][0] &&
282 s[1] == forbidden_seq[i][1])
285 if (iscntrl((unsigned char)s[0]))
287 if (s[0] == '.' && s[1] == '\0')
290 const char *nextseg = s + 1;
291 if (nextseg[0] == '\0' || nextseg[0] == '.' ||
294 if (seg <= s - lfs_len &&
295 strncmp(s - lfs_len, lfs, lfs_len) == 0)
302 if (seg <= s - lfs_len &&
303 strncmp(s - lfs_len, lfs, lfs_len) == 0)
309 const struct got_error *
310 got_ref_alloc(struct got_reference **ref, const char *name,
311 struct got_object_id *id)
313 if (!is_valid_ref_name(name))
314 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
316 return alloc_ref(ref, name, id, 0);
319 const struct got_error *
320 got_ref_alloc_symref(struct got_reference **ref, const char *name,
321 struct got_reference *target_ref)
323 if (!is_valid_ref_name(name))
324 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
326 return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
329 static const struct got_error *
330 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
333 struct got_object_id id;
338 if (line[0] == '#' || line[0] == '^')
341 if (!got_parse_sha1_digest(id.sha1, line))
342 return got_error(GOT_ERR_BAD_REF_DATA);
345 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
349 name = line + SHA1_DIGEST_STRING_LENGTH;
351 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED);
354 static const struct got_error *
355 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
356 int nsubdirs, const char *refname)
358 const struct got_error *err = NULL;
362 const char delim[3] = {'\0', '\0', '\0'};
363 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
368 abs_refname = (char *)refname;
370 line = fparseln(f, &len, NULL, delim, 0);
374 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
377 for (i = 0; i < nsubdirs; i++) {
378 if (!ref_is_absolute &&
379 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
381 return got_error_from_errno("asprintf");
382 err = parse_packed_ref_line(ref, abs_refname, line);
383 if (!ref_is_absolute)
385 if (err || *ref != NULL)
391 } while (*ref == NULL);
396 static const struct got_error *
397 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
398 const char *name, int lock)
400 const struct got_error *err = NULL;
402 char *absname = NULL;
403 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
404 int ref_is_well_known = is_well_known_ref(name);
408 if (ref_is_absolute || ref_is_well_known) {
409 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
410 return got_error_from_errno("asprintf");
411 absname = (char *)name;
413 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
414 subdir[0] ? "/" : "", name) == -1)
415 return got_error_from_errno("asprintf");
417 if (asprintf(&absname, "refs/%s%s%s",
418 subdir, subdir[0] ? "/" : "", name) == -1) {
419 err = got_error_from_errno("asprintf");
424 err = parse_ref_file(ref, name, absname, path, lock);
426 if (!ref_is_absolute && !ref_is_well_known)
432 const struct got_error *
433 got_ref_open(struct got_reference **ref, struct got_repository *repo,
434 const char *refname, int lock)
436 const struct got_error *err = NULL;
437 char *path_refs = NULL;
438 const char *subdirs[] = {
439 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
441 int i, well_known = is_well_known_ref(refname);
442 struct got_lockfile *lf = NULL;
446 path_refs = get_refs_dir_path(repo, refname);
447 if (path_refs == NULL) {
448 err = got_error_from_errno2("get_refs_dir_path", refname);
453 err = open_ref(ref, path_refs, "", refname, lock);
455 char *packed_refs_path;
458 /* Search on-disk refs before packed refs! */
459 for (i = 0; i < nitems(subdirs); i++) {
460 err = open_ref(ref, path_refs, subdirs[i], refname,
462 if ((err && err->code != GOT_ERR_NOT_REF) || *ref)
466 packed_refs_path = got_repo_get_path_packed_refs(repo);
467 if (packed_refs_path == NULL) {
468 err = got_error_from_errno(
469 "got_repo_get_path_packed_refs");
474 err = got_lockfile_lock(&lf, packed_refs_path);
478 f = fopen(packed_refs_path, "rb");
479 free(packed_refs_path);
481 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
484 if (fclose(f) != 0) {
485 err = got_error_from_errno("fclose");
494 if (!err && *ref == NULL)
495 err = got_error_not_ref(refname);
497 got_lockfile_unlock(lf);
503 got_ref_close(struct got_reference *ref)
505 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
506 free(ref->ref.symref.name);
507 free(ref->ref.symref.ref);
509 free(ref->ref.ref.name);
513 struct got_reference *
514 got_ref_dup(struct got_reference *ref)
516 struct got_reference *ret;
518 ret = calloc(1, sizeof(*ret));
522 ret->flags = ref->flags;
523 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
524 ret->ref.symref.name = strdup(ref->ref.symref.name);
525 if (ret->ref.symref.name == NULL) {
529 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
530 if (ret->ref.symref.ref == NULL) {
531 free(ret->ref.symref.name);
536 ref->ref.ref.name = strdup(ref->ref.ref.name);
537 if (ref->ref.ref.name == NULL) {
541 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
542 sizeof(ret->ref.ref.sha1));
548 const struct got_error *
549 got_reflist_entry_dup(struct got_reflist_entry **newp,
550 struct got_reflist_entry *re)
552 const struct got_error *err = NULL;
553 struct got_reflist_entry *new;
557 new = malloc(sizeof(*new));
559 return got_error_from_errno("malloc");
561 new->ref = got_ref_dup(re->ref);
562 if (new->ref == NULL) {
563 err = got_error_from_errno("got_ref_dup");
568 new->id = got_object_id_dup(re->id);
569 if (new->id == NULL) {
570 err = got_error_from_errno("got_ref_dup");
580 static const struct got_error *
581 resolve_symbolic_ref(struct got_reference **resolved,
582 struct got_repository *repo, struct got_reference *ref)
584 struct got_reference *nextref;
585 const struct got_error *err;
587 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
591 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
592 err = resolve_symbolic_ref(resolved, repo, nextref);
594 *resolved = got_ref_dup(nextref);
596 got_ref_close(nextref);
600 static const struct got_error *
601 ref_resolve(struct got_object_id **id, struct got_repository *repo,
602 struct got_reference *ref, int recursion)
604 const struct got_error *err;
607 return got_error_msg(GOT_ERR_RECURSION,
608 "reference recursion limit reached");
610 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
611 struct got_reference *resolved = NULL;
612 err = resolve_symbolic_ref(&resolved, repo, ref);
614 err = ref_resolve(id, repo, resolved, --recursion);
616 got_ref_close(resolved);
620 *id = calloc(1, sizeof(**id));
622 return got_error_from_errno("calloc");
623 memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
627 const struct got_error *
628 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
629 struct got_reference *ref)
631 return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
635 got_ref_to_str(struct got_reference *ref)
639 if (ref->flags & GOT_REF_IS_SYMBOLIC)
640 return strdup(ref->ref.symref.ref);
642 str = malloc(SHA1_DIGEST_STRING_LENGTH);
646 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
647 SHA1_DIGEST_STRING_LENGTH) == NULL) {
656 got_ref_get_name(struct got_reference *ref)
658 if (ref->flags & GOT_REF_IS_SYMBOLIC)
659 return ref->ref.symref.name;
661 return ref->ref.ref.name;
665 got_ref_get_symref_target(struct got_reference *ref)
667 if (ref->flags & GOT_REF_IS_SYMBOLIC)
668 return ref->ref.symref.ref;
673 const struct got_error *
674 got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
675 struct got_reference* re2)
677 const char *name1 = got_ref_get_name(re1);
678 const char *name2 = got_ref_get_name(re2);
680 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
684 const struct got_error *
685 got_ref_cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
686 struct got_reference *ref2)
688 const struct got_error *err = NULL;
689 struct got_repository *repo = arg;
690 struct got_object_id *id1, *id2 = NULL;
691 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
692 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
697 err = got_ref_resolve(&id1, repo, ref1);
700 err = got_object_open_as_tag(&tag1, repo, id1);
702 if (err->code != GOT_ERR_OBJ_TYPE)
704 /* "lightweight" tag */
705 err = got_object_open_as_commit(&commit1, repo, id1);
708 time1 = got_object_commit_get_committer_time(commit1);
710 time1 = got_object_tag_get_tagger_time(tag1);
712 err = got_ref_resolve(&id2, repo, ref2);
715 err = got_object_open_as_tag(&tag2, repo, id2);
717 if (err->code != GOT_ERR_OBJ_TYPE)
719 /* "lightweight" tag */
720 err = got_object_open_as_commit(&commit2, repo, id2);
723 time2 = got_object_commit_get_committer_time(commit2);
725 time2 = got_object_tag_get_tagger_time(tag2);
727 /* Put latest tags first. */
730 else if (time1 > time2)
733 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
738 got_object_tag_close(tag1);
740 got_object_tag_close(tag2);
742 got_object_commit_close(commit1);
744 got_object_commit_close(commit2);
748 static const struct got_error *
749 insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
750 struct got_reference *ref, struct got_repository *repo,
751 got_ref_cmp_cb cmp_cb, void *cmp_arg)
753 const struct got_error *err;
754 struct got_object_id *id;
755 struct got_reflist_entry *new, *re, *prev = NULL;
760 err = got_ref_resolve(&id, repo, ref);
764 new = malloc(sizeof(*new));
767 return got_error_from_errno("malloc");
774 * We must de-duplicate entries on insert because packed-refs may
775 * contain redundant entries. On-disk refs take precedence.
776 * This code assumes that on-disk revs are read before packed-refs.
777 * We're iterating the list anyway, so insert elements sorted by name.
779 re = SIMPLEQ_FIRST(refs);
781 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
790 } else if (cmp > 0) {
792 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
794 SIMPLEQ_INSERT_HEAD(refs, new, entry);
798 re = SIMPLEQ_NEXT(re, entry);
802 SIMPLEQ_INSERT_TAIL(refs, new, entry);
806 static const struct got_error *
807 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
808 const char *subdir, struct got_repository *repo,
809 got_ref_cmp_cb cmp_cb, void *cmp_arg)
811 const struct got_error *err = NULL;
815 while (subdir[0] == '/')
818 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
819 return got_error_from_errno("asprintf");
821 d = opendir(path_subdir);
827 struct got_reference *ref;
834 if (strcmp(dent->d_name, ".") == 0 ||
835 strcmp(dent->d_name, "..") == 0)
838 switch (dent->d_type) {
840 err = open_ref(&ref, path_refs, subdir, dent->d_name,
845 struct got_reflist_entry *new;
846 err = insert_ref(&new, refs, ref, repo,
848 if (err || new == NULL /* duplicate */)
855 if (asprintf(&child, "%s%s%s", subdir,
856 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
857 err = got_error_from_errno("asprintf");
860 err = gather_on_disk_refs(refs, path_refs, child, repo,
875 const struct got_error *
876 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
877 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
879 const struct got_error *err;
880 char *packed_refs_path, *path_refs = NULL;
881 char *abs_namespace = NULL;
882 char *buf = NULL, *ondisk_ref_namespace = NULL;
884 struct got_reference *ref;
885 struct got_reflist_entry *new;
887 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
888 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
889 if (path_refs == NULL) {
890 err = got_error_from_errno("get_refs_dir_path");
893 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
896 err = insert_ref(&new, refs, ref, repo,
898 if (err || new == NULL /* duplicate */)
900 if (err && err->code != GOT_ERR_NOT_REF)
903 /* Try listing a single reference. */
904 const char *refname = ref_namespace;
905 path_refs = get_refs_dir_path(repo, refname);
906 if (path_refs == NULL) {
907 err = got_error_from_errno("get_refs_dir_path");
910 err = open_ref(&ref, path_refs, "", refname, 0);
912 if (err->code != GOT_ERR_NOT_REF)
914 /* Try to look up references in a given namespace. */
916 err = insert_ref(&new, refs, ref, repo,
918 if (err || new == NULL /* duplicate */)
926 /* Canonicalize the path to eliminate double-slashes if any. */
927 if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
928 err = got_error_from_errno("asprintf");
931 len = strlen(abs_namespace) + 1;
934 err = got_error_from_errno("malloc");
937 err = got_canonpath(abs_namespace, buf, len);
940 ondisk_ref_namespace = buf;
941 while (ondisk_ref_namespace[0] == '/')
942 ondisk_ref_namespace++;
943 if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
944 ondisk_ref_namespace += 5;
945 else if (strcmp(ondisk_ref_namespace, "refs") == 0)
946 ondisk_ref_namespace = "";
949 /* Gather on-disk refs before parsing packed-refs. */
951 path_refs = get_refs_dir_path(repo, "");
952 if (path_refs == NULL) {
953 err = got_error_from_errno("get_refs_dir_path");
956 err = gather_on_disk_refs(refs, path_refs,
957 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
963 * The packed-refs file may contain redundant entries, in which
964 * case on-disk refs take precedence.
966 packed_refs_path = got_repo_get_path_packed_refs(repo);
967 if (packed_refs_path == NULL) {
968 err = got_error_from_errno("got_repo_get_path_packed_refs");
972 f = fopen(packed_refs_path, "r");
973 free(packed_refs_path);
977 const char delim[3] = {'\0', '\0', '\0'};
979 line = fparseln(f, &len, NULL, delim, 0);
983 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
986 err = parse_packed_ref_line(&ref, NULL, line);
993 name = got_ref_get_name(ref);
994 if (!got_path_is_child(name,
996 strlen(ref_namespace))) {
1001 err = insert_ref(&new, refs, ref, repo,
1003 if (err || new == NULL /* duplicate */)
1011 free(abs_namespace);
1014 if (f && fclose(f) != 0 && err == NULL)
1015 err = got_error_from_errno("fclose");
1020 got_ref_list_free(struct got_reflist_head *refs)
1022 struct got_reflist_entry *re;
1024 while (!SIMPLEQ_EMPTY(refs)) {
1025 re = SIMPLEQ_FIRST(refs);
1026 SIMPLEQ_REMOVE_HEAD(refs, entry);
1027 got_ref_close(re->ref);
1035 got_ref_is_symbolic(struct got_reference *ref)
1037 return (ref->flags & GOT_REF_IS_SYMBOLIC);
1040 const struct got_error *
1041 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1043 if (ref->flags & GOT_REF_IS_SYMBOLIC)
1044 return got_error(GOT_ERR_BAD_REF_TYPE);
1046 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
1050 const struct got_error *
1051 got_ref_change_symref(struct got_reference *ref, char *refname)
1055 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1056 return got_error(GOT_ERR_BAD_REF_TYPE);
1058 new_name = strdup(refname);
1059 if (new_name == NULL)
1060 return got_error_from_errno("strdup");
1062 free(ref->ref.symref.name);
1063 ref->ref.symref.name = new_name;
1067 const struct got_error *
1068 got_ref_change_symref_to_ref(struct got_reference *symref,
1069 struct got_object_id *id)
1071 if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1072 return got_error(GOT_ERR_BAD_REF_TYPE);
1074 symref->ref.ref.name = symref->ref.symref.name;
1075 memcpy(symref->ref.ref.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1076 symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1080 const struct got_error *
1081 got_ref_write(struct got_reference *ref, struct got_repository *repo)
1083 const struct got_error *err = NULL, *unlock_err = NULL;
1084 const char *name = got_ref_get_name(ref);
1085 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1086 struct got_lockfile *lf = NULL;
1091 path_refs = get_refs_dir_path(repo, name);
1092 if (path_refs == NULL) {
1093 err = got_error_from_errno2("get_refs_dir_path", name);
1097 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1098 err = got_error_from_errno("asprintf");
1102 err = got_opentemp_named(&tmppath, &f, path);
1105 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1107 err = got_path_dirname(&parent, path);
1110 err = got_path_mkdir(parent);
1114 err = got_opentemp_named(&tmppath, &f, path);
1119 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1120 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1121 if (n != strlen(ref->ref.symref.ref) + 6) {
1122 err = got_ferror(f, GOT_ERR_IO);
1126 char hex[SHA1_DIGEST_STRING_LENGTH];
1127 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
1128 sizeof(hex)) == NULL) {
1129 err = got_error(GOT_ERR_BAD_REF_DATA);
1132 n = fprintf(f, "%s\n", hex);
1133 if (n != sizeof(hex)) {
1134 err = got_ferror(f, GOT_ERR_IO);
1139 if (ref->lf == NULL) {
1140 err = got_lockfile_lock(&lf, path);
1145 /* XXX: check if old content matches our expectations? */
1147 if (stat(path, &sb) != 0) {
1148 if (errno != ENOENT) {
1149 err = got_error_from_errno2("stat", path);
1152 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1155 if (rename(tmppath, path) != 0) {
1156 err = got_error_from_errno3("rename", tmppath, path);
1162 if (chmod(path, sb.st_mode) != 0) {
1163 err = got_error_from_errno2("chmod", path);
1167 if (ref->lf == NULL && lf)
1168 unlock_err = got_lockfile_unlock(lf);
1170 if (fclose(f) != 0 && err == NULL)
1171 err = got_error_from_errno("fclose");
1176 if (unlink(tmppath) != 0 && err == NULL)
1177 err = got_error_from_errno2("unlink", tmppath);
1180 return err ? err : unlock_err;
1183 static const struct got_error *
1184 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1186 const struct got_error *err = NULL, *unlock_err = NULL;
1187 struct got_lockfile *lf = NULL;
1188 FILE *f = NULL, *tmpf = NULL;
1189 char *packed_refs_path, *tmppath = NULL;
1190 struct got_reflist_head refs;
1191 int found_delref = 0;
1193 /* The packed-refs file does not cotain symbolic references. */
1194 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1195 return got_error(GOT_ERR_BAD_REF_DATA);
1197 SIMPLEQ_INIT(&refs);
1199 packed_refs_path = got_repo_get_path_packed_refs(repo);
1200 if (packed_refs_path == NULL)
1201 return got_error_from_errno("got_repo_get_path_packed_refs");
1203 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
1207 if (delref->lf == NULL) {
1208 err = got_lockfile_lock(&lf, packed_refs_path);
1213 f = fopen(packed_refs_path, "r");
1215 err = got_error_from_errno2("fopen", packed_refs_path);
1221 const char delim[3] = {'\0', '\0', '\0'};
1222 struct got_reference *ref;
1223 struct got_reflist_entry *new;
1225 line = fparseln(f, &len, NULL, delim, 0);
1229 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1232 err = parse_packed_ref_line(&ref, NULL, line);
1239 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1240 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1241 sizeof(delref->ref.ref.sha1)) == 0) {
1247 err = insert_ref(&new, &refs, ref, repo,
1248 got_ref_cmp_by_name, NULL);
1249 if (err || new == NULL /* duplicate */)
1256 struct got_reflist_entry *re;
1260 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1261 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1262 err = got_ferror(f, GOT_ERR_IO);
1266 SIMPLEQ_FOREACH(re, &refs, entry) {
1267 uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
1269 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1270 sizeof(hex)) == NULL) {
1271 err = got_error(GOT_ERR_BAD_REF_DATA);
1274 n = fprintf(tmpf, "%s ", hex);
1275 if (n != sizeof(hex)) {
1276 err = got_ferror(f, GOT_ERR_IO);
1279 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1280 if (n != strlen(re->ref->ref.ref.name) + 1) {
1281 err = got_ferror(f, GOT_ERR_IO);
1286 if (fflush(tmpf) != 0) {
1287 err = got_error_from_errno("fflush");
1291 if (stat(packed_refs_path, &sb) != 0) {
1292 if (errno != ENOENT) {
1293 err = got_error_from_errno2("stat",
1297 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1300 if (rename(tmppath, packed_refs_path) != 0) {
1301 err = got_error_from_errno3("rename", tmppath,
1306 if (chmod(packed_refs_path, sb.st_mode) != 0) {
1307 err = got_error_from_errno2("chmod",
1313 if (delref->lf == NULL && lf)
1314 unlock_err = got_lockfile_unlock(lf);
1316 if (fclose(f) != 0 && err == NULL)
1317 err = got_error_from_errno("fclose");
1321 if (fclose(tmpf) != 0 && err == NULL)
1322 err = got_error_from_errno("fclose");
1325 free(packed_refs_path);
1326 got_ref_list_free(&refs);
1327 return err ? err : unlock_err;
1330 const struct got_error *
1331 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1333 const struct got_error *err = NULL, *unlock_err = NULL;
1334 const char *name = got_ref_get_name(ref);
1335 char *path_refs = NULL, *path = NULL;
1336 struct got_lockfile *lf = NULL;
1338 if (ref->flags & GOT_REF_IS_PACKED)
1339 return delete_packed_ref(ref, repo);
1341 path_refs = get_refs_dir_path(repo, name);
1342 if (path_refs == NULL) {
1343 err = got_error_from_errno2("get_refs_dir_path", name);
1347 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1348 err = got_error_from_errno("asprintf");
1352 if (ref->lf == NULL) {
1353 err = got_lockfile_lock(&lf, path);
1358 /* XXX: check if old content matches our expectations? */
1360 if (unlink(path) != 0)
1361 err = got_error_from_errno2("unlink", path);
1363 if (ref->lf == NULL && lf)
1364 unlock_err = got_lockfile_unlock(lf);
1368 return err ? err : unlock_err;
1371 const struct got_error *
1372 got_ref_unlock(struct got_reference *ref)
1374 const struct got_error *err;
1375 err = got_lockfile_unlock(ref->lf);