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");
569 new->id = got_object_id_dup(re->id);
570 if (new->id == NULL) {
571 err = got_error_from_errno("got_ref_dup");
581 static const struct got_error *
582 resolve_symbolic_ref(struct got_reference **resolved,
583 struct got_repository *repo, struct got_reference *ref)
585 struct got_reference *nextref;
586 const struct got_error *err;
588 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
592 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
593 err = resolve_symbolic_ref(resolved, repo, nextref);
595 *resolved = got_ref_dup(nextref);
597 got_ref_close(nextref);
601 static const struct got_error *
602 ref_resolve(struct got_object_id **id, struct got_repository *repo,
603 struct got_reference *ref, int recursion)
605 const struct got_error *err;
608 return got_error_msg(GOT_ERR_RECURSION,
609 "reference recursion limit reached");
611 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
612 struct got_reference *resolved = NULL;
613 err = resolve_symbolic_ref(&resolved, repo, ref);
615 err = ref_resolve(id, repo, resolved, --recursion);
617 got_ref_close(resolved);
621 *id = calloc(1, sizeof(**id));
623 return got_error_from_errno("calloc");
624 memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
628 const struct got_error *
629 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
630 struct got_reference *ref)
632 return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
636 got_ref_to_str(struct got_reference *ref)
640 if (ref->flags & GOT_REF_IS_SYMBOLIC)
641 return strdup(ref->ref.symref.ref);
643 str = malloc(SHA1_DIGEST_STRING_LENGTH);
647 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
648 SHA1_DIGEST_STRING_LENGTH) == NULL) {
657 got_ref_get_name(struct got_reference *ref)
659 if (ref->flags & GOT_REF_IS_SYMBOLIC)
660 return ref->ref.symref.name;
662 return ref->ref.ref.name;
666 got_ref_get_symref_target(struct got_reference *ref)
668 if (ref->flags & GOT_REF_IS_SYMBOLIC)
669 return ref->ref.symref.ref;
674 const struct got_error *
675 got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
676 struct got_reference* re2)
678 const char *name1 = got_ref_get_name(re1);
679 const char *name2 = got_ref_get_name(re2);
681 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
685 const struct got_error *
686 got_ref_cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
687 struct got_reference *ref2)
689 const struct got_error *err = NULL;
690 struct got_repository *repo = arg;
691 struct got_object_id *id1, *id2 = NULL;
692 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
693 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
698 err = got_ref_resolve(&id1, repo, ref1);
701 err = got_object_open_as_tag(&tag1, repo, id1);
703 if (err->code != GOT_ERR_OBJ_TYPE)
705 /* "lightweight" tag */
706 err = got_object_open_as_commit(&commit1, repo, id1);
709 time1 = got_object_commit_get_committer_time(commit1);
711 time1 = got_object_tag_get_tagger_time(tag1);
713 err = got_ref_resolve(&id2, repo, ref2);
716 err = got_object_open_as_tag(&tag2, repo, id2);
718 if (err->code != GOT_ERR_OBJ_TYPE)
720 /* "lightweight" tag */
721 err = got_object_open_as_commit(&commit2, repo, id2);
724 time2 = got_object_commit_get_committer_time(commit2);
726 time2 = got_object_tag_get_tagger_time(tag2);
728 /* Put latest tags first. */
731 else if (time1 > time2)
734 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
739 got_object_tag_close(tag1);
741 got_object_tag_close(tag2);
743 got_object_commit_close(commit1);
745 got_object_commit_close(commit2);
749 static const struct got_error *
750 insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
751 struct got_reference *ref, struct got_repository *repo,
752 got_ref_cmp_cb cmp_cb, void *cmp_arg)
754 const struct got_error *err;
755 struct got_object_id *id;
756 struct got_reflist_entry *new, *re, *prev = NULL;
761 err = got_ref_resolve(&id, repo, ref);
765 new = malloc(sizeof(*new));
768 return got_error_from_errno("malloc");
775 * We must de-duplicate entries on insert because packed-refs may
776 * contain redundant entries. On-disk refs take precedence.
777 * This code assumes that on-disk revs are read before packed-refs.
778 * We're iterating the list anyway, so insert elements sorted by name.
780 re = SIMPLEQ_FIRST(refs);
782 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
791 } else if (cmp > 0) {
793 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
795 SIMPLEQ_INSERT_HEAD(refs, new, entry);
799 re = SIMPLEQ_NEXT(re, entry);
803 SIMPLEQ_INSERT_TAIL(refs, new, entry);
807 static const struct got_error *
808 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
809 const char *subdir, struct got_repository *repo,
810 got_ref_cmp_cb cmp_cb, void *cmp_arg)
812 const struct got_error *err = NULL;
816 while (subdir[0] == '/')
819 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
820 return got_error_from_errno("asprintf");
822 d = opendir(path_subdir);
828 struct got_reference *ref;
836 if (strcmp(dent->d_name, ".") == 0 ||
837 strcmp(dent->d_name, "..") == 0)
840 err = got_path_dirent_type(&type, path_subdir, dent);
846 err = open_ref(&ref, path_refs, subdir, dent->d_name,
851 struct got_reflist_entry *new;
852 err = insert_ref(&new, refs, ref, repo,
854 if (err || new == NULL /* duplicate */)
861 if (asprintf(&child, "%s%s%s", subdir,
862 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
863 err = got_error_from_errno("asprintf");
866 err = gather_on_disk_refs(refs, path_refs, child, repo,
881 const struct got_error *
882 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
883 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
885 const struct got_error *err;
886 char *packed_refs_path, *path_refs = NULL;
887 char *abs_namespace = NULL;
888 char *buf = NULL, *ondisk_ref_namespace = NULL;
890 struct got_reference *ref;
891 struct got_reflist_entry *new;
893 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
894 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
895 if (path_refs == NULL) {
896 err = got_error_from_errno("get_refs_dir_path");
899 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
902 err = insert_ref(&new, refs, ref, repo,
904 if (err || new == NULL /* duplicate */)
906 if (err && err->code != GOT_ERR_NOT_REF)
909 /* Try listing a single reference. */
910 const char *refname = ref_namespace;
911 path_refs = get_refs_dir_path(repo, refname);
912 if (path_refs == NULL) {
913 err = got_error_from_errno("get_refs_dir_path");
916 err = open_ref(&ref, path_refs, "", refname, 0);
918 if (err->code != GOT_ERR_NOT_REF)
920 /* Try to look up references in a given namespace. */
922 err = insert_ref(&new, refs, ref, repo,
924 if (err || new == NULL /* duplicate */)
932 /* Canonicalize the path to eliminate double-slashes if any. */
933 if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
934 err = got_error_from_errno("asprintf");
937 len = strlen(abs_namespace) + 1;
940 err = got_error_from_errno("malloc");
943 err = got_canonpath(abs_namespace, buf, len);
946 ondisk_ref_namespace = buf;
947 while (ondisk_ref_namespace[0] == '/')
948 ondisk_ref_namespace++;
949 if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
950 ondisk_ref_namespace += 5;
951 else if (strcmp(ondisk_ref_namespace, "refs") == 0)
952 ondisk_ref_namespace = "";
955 /* Gather on-disk refs before parsing packed-refs. */
957 path_refs = get_refs_dir_path(repo, "");
958 if (path_refs == NULL) {
959 err = got_error_from_errno("get_refs_dir_path");
962 err = gather_on_disk_refs(refs, path_refs,
963 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
969 * The packed-refs file may contain redundant entries, in which
970 * case on-disk refs take precedence.
972 packed_refs_path = got_repo_get_path_packed_refs(repo);
973 if (packed_refs_path == NULL) {
974 err = got_error_from_errno("got_repo_get_path_packed_refs");
978 f = fopen(packed_refs_path, "r");
979 free(packed_refs_path);
983 const char delim[3] = {'\0', '\0', '\0'};
985 line = fparseln(f, &len, NULL, delim, 0);
989 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
992 err = parse_packed_ref_line(&ref, NULL, line);
999 name = got_ref_get_name(ref);
1000 if (!got_path_is_child(name,
1002 strlen(ref_namespace))) {
1007 err = insert_ref(&new, refs, ref, repo,
1009 if (err || new == NULL /* duplicate */)
1017 free(abs_namespace);
1020 if (f && fclose(f) != 0 && err == NULL)
1021 err = got_error_from_errno("fclose");
1026 got_ref_list_free(struct got_reflist_head *refs)
1028 struct got_reflist_entry *re;
1030 while (!SIMPLEQ_EMPTY(refs)) {
1031 re = SIMPLEQ_FIRST(refs);
1032 SIMPLEQ_REMOVE_HEAD(refs, entry);
1033 got_ref_close(re->ref);
1041 got_ref_is_symbolic(struct got_reference *ref)
1043 return (ref->flags & GOT_REF_IS_SYMBOLIC);
1046 const struct got_error *
1047 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1049 if (ref->flags & GOT_REF_IS_SYMBOLIC)
1050 return got_error(GOT_ERR_BAD_REF_TYPE);
1052 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
1056 const struct got_error *
1057 got_ref_change_symref(struct got_reference *ref, const char *refname)
1061 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1062 return got_error(GOT_ERR_BAD_REF_TYPE);
1064 new_name = strdup(refname);
1065 if (new_name == NULL)
1066 return got_error_from_errno("strdup");
1068 free(ref->ref.symref.ref);
1069 ref->ref.symref.ref = new_name;
1073 const struct got_error *
1074 got_ref_change_symref_to_ref(struct got_reference *symref,
1075 struct got_object_id *id)
1077 if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1078 return got_error(GOT_ERR_BAD_REF_TYPE);
1080 symref->ref.ref.name = symref->ref.symref.name;
1081 memcpy(symref->ref.ref.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1082 symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1086 const struct got_error *
1087 got_ref_write(struct got_reference *ref, struct got_repository *repo)
1089 const struct got_error *err = NULL, *unlock_err = NULL;
1090 const char *name = got_ref_get_name(ref);
1091 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1092 struct got_lockfile *lf = NULL;
1097 path_refs = get_refs_dir_path(repo, name);
1098 if (path_refs == NULL) {
1099 err = got_error_from_errno2("get_refs_dir_path", name);
1103 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1104 err = got_error_from_errno("asprintf");
1108 err = got_opentemp_named(&tmppath, &f, path);
1111 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1113 err = got_path_dirname(&parent, path);
1116 err = got_path_mkdir(parent);
1120 err = got_opentemp_named(&tmppath, &f, path);
1125 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1126 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1127 if (n != strlen(ref->ref.symref.ref) + 6) {
1128 err = got_ferror(f, GOT_ERR_IO);
1132 char hex[SHA1_DIGEST_STRING_LENGTH];
1133 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
1134 sizeof(hex)) == NULL) {
1135 err = got_error(GOT_ERR_BAD_REF_DATA);
1138 n = fprintf(f, "%s\n", hex);
1139 if (n != sizeof(hex)) {
1140 err = got_ferror(f, GOT_ERR_IO);
1145 if (ref->lf == NULL) {
1146 err = got_lockfile_lock(&lf, path);
1151 /* XXX: check if old content matches our expectations? */
1153 if (stat(path, &sb) != 0) {
1154 if (errno != ENOENT) {
1155 err = got_error_from_errno2("stat", path);
1158 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1161 if (rename(tmppath, path) != 0) {
1162 err = got_error_from_errno3("rename", tmppath, path);
1168 if (chmod(path, sb.st_mode) != 0) {
1169 err = got_error_from_errno2("chmod", path);
1173 if (ref->lf == NULL && lf)
1174 unlock_err = got_lockfile_unlock(lf);
1176 if (fclose(f) != 0 && err == NULL)
1177 err = got_error_from_errno("fclose");
1182 if (unlink(tmppath) != 0 && err == NULL)
1183 err = got_error_from_errno2("unlink", tmppath);
1186 return err ? err : unlock_err;
1189 static const struct got_error *
1190 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1192 const struct got_error *err = NULL, *unlock_err = NULL;
1193 struct got_lockfile *lf = NULL;
1194 FILE *f = NULL, *tmpf = NULL;
1195 char *packed_refs_path, *tmppath = NULL;
1196 struct got_reflist_head refs;
1197 int found_delref = 0;
1199 /* The packed-refs file does not cotain symbolic references. */
1200 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1201 return got_error(GOT_ERR_BAD_REF_DATA);
1203 SIMPLEQ_INIT(&refs);
1205 packed_refs_path = got_repo_get_path_packed_refs(repo);
1206 if (packed_refs_path == NULL)
1207 return got_error_from_errno("got_repo_get_path_packed_refs");
1209 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
1213 if (delref->lf == NULL) {
1214 err = got_lockfile_lock(&lf, packed_refs_path);
1219 f = fopen(packed_refs_path, "r");
1221 err = got_error_from_errno2("fopen", packed_refs_path);
1227 const char delim[3] = {'\0', '\0', '\0'};
1228 struct got_reference *ref;
1229 struct got_reflist_entry *new;
1231 line = fparseln(f, &len, NULL, delim, 0);
1235 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1238 err = parse_packed_ref_line(&ref, NULL, line);
1245 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1246 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1247 sizeof(delref->ref.ref.sha1)) == 0) {
1253 err = insert_ref(&new, &refs, ref, repo,
1254 got_ref_cmp_by_name, NULL);
1255 if (err || new == NULL /* duplicate */)
1262 struct got_reflist_entry *re;
1266 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1267 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1268 err = got_ferror(f, GOT_ERR_IO);
1272 SIMPLEQ_FOREACH(re, &refs, entry) {
1273 uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
1275 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1276 sizeof(hex)) == NULL) {
1277 err = got_error(GOT_ERR_BAD_REF_DATA);
1280 n = fprintf(tmpf, "%s ", hex);
1281 if (n != sizeof(hex)) {
1282 err = got_ferror(f, GOT_ERR_IO);
1285 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1286 if (n != strlen(re->ref->ref.ref.name) + 1) {
1287 err = got_ferror(f, GOT_ERR_IO);
1292 if (fflush(tmpf) != 0) {
1293 err = got_error_from_errno("fflush");
1297 if (stat(packed_refs_path, &sb) != 0) {
1298 if (errno != ENOENT) {
1299 err = got_error_from_errno2("stat",
1303 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1306 if (rename(tmppath, packed_refs_path) != 0) {
1307 err = got_error_from_errno3("rename", tmppath,
1312 if (chmod(packed_refs_path, sb.st_mode) != 0) {
1313 err = got_error_from_errno2("chmod",
1319 if (delref->lf == NULL && lf)
1320 unlock_err = got_lockfile_unlock(lf);
1322 if (fclose(f) != 0 && err == NULL)
1323 err = got_error_from_errno("fclose");
1327 if (fclose(tmpf) != 0 && err == NULL)
1328 err = got_error_from_errno("fclose");
1331 free(packed_refs_path);
1332 got_ref_list_free(&refs);
1333 return err ? err : unlock_err;
1336 const struct got_error *
1337 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1339 const struct got_error *err = NULL, *unlock_err = NULL;
1340 const char *name = got_ref_get_name(ref);
1341 char *path_refs = NULL, *path = NULL;
1342 struct got_lockfile *lf = NULL;
1344 if (ref->flags & GOT_REF_IS_PACKED)
1345 return delete_packed_ref(ref, repo);
1347 path_refs = get_refs_dir_path(repo, name);
1348 if (path_refs == NULL) {
1349 err = got_error_from_errno2("get_refs_dir_path", name);
1353 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1354 err = got_error_from_errno("asprintf");
1358 if (ref->lf == NULL) {
1359 err = got_lockfile_lock(&lf, path);
1364 /* XXX: check if old content matches our expectations? */
1366 if (unlink(path) != 0)
1367 err = got_error_from_errno2("unlink", path);
1369 if (ref->lf == NULL && lf)
1370 unlock_err = got_lockfile_unlock(lf);
1374 return err ? err : unlock_err;
1377 const struct got_error *
1378 got_ref_unlock(struct got_reference *ref)
1380 const struct got_error *err;
1381 err = got_lockfile_unlock(ref->lf);