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_object_idset.h"
47 #include "got_lib_lockfile.h"
50 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
53 #define GOT_REF_HEADS "heads"
54 #define GOT_REF_TAGS "tags"
55 #define GOT_REF_REMOTES "remotes"
58 * We do not resolve tags yet, and don't yet care about sorting refs either,
59 * so packed-refs files we write contain a minimal header which disables all
60 * packed-refs "traits" supported by Git.
62 #define GOT_PACKED_REFS_HEADER "# pack-refs with:"
64 /* A symbolic reference. */
70 #define GOT_REF_RECURSE_MAX 20
72 /* A non-symbolic reference (there is no better designation). */
75 u_int8_t sha1[SHA1_DIGEST_LENGTH];
78 /* A reference which points to an arbitrary object. */
79 struct got_reference {
81 #define GOT_REF_IS_SYMBOLIC 0x01
82 #define GOT_REF_IS_PACKED 0x02
86 struct got_symref symref;
89 struct got_lockfile *lf;
93 static const struct got_error *
94 alloc_ref(struct got_reference **ref, const char *name,
95 struct got_object_id *id, int flags, time_t mtime)
97 const struct got_error *err = NULL;
99 *ref = calloc(1, sizeof(**ref));
101 return got_error_from_errno("calloc");
103 memcpy((*ref)->ref.ref.sha1, id->sha1, sizeof((*ref)->ref.ref.sha1));
104 (*ref)->flags = flags;
105 (*ref)->ref.ref.name = strdup(name);
106 (*ref)->mtime = mtime;
107 if ((*ref)->ref.ref.name == NULL) {
108 err = got_error_from_errno("strdup");
115 static const struct got_error *
116 alloc_symref(struct got_reference **ref, const char *name,
117 const char *target_ref, int flags)
119 const struct got_error *err = NULL;
121 *ref = calloc(1, sizeof(**ref));
123 return got_error_from_errno("calloc");
125 (*ref)->flags = GOT_REF_IS_SYMBOLIC | flags;
126 (*ref)->ref.symref.name = strdup(name);
127 if ((*ref)->ref.symref.name == NULL) {
128 err = got_error_from_errno("strdup");
133 (*ref)->ref.symref.ref = strdup(target_ref);
134 if ((*ref)->ref.symref.ref == NULL) {
135 err = got_error_from_errno("strdup");
142 static const struct got_error *
143 parse_symref(struct got_reference **ref, const char *name, const char *line)
146 return got_error(GOT_ERR_BAD_REF_DATA);
148 return alloc_symref(ref, name, line, 0);
151 static const struct got_error *
152 parse_ref_line(struct got_reference **ref, const char *name, const char *line,
155 struct got_object_id id;
157 if (strncmp(line, "ref: ", 5) == 0) {
159 return parse_symref(ref, name, line);
162 if (!got_parse_sha1_digest(id.sha1, line))
163 return got_error(GOT_ERR_BAD_REF_DATA);
165 return alloc_ref(ref, name, &id, 0, mtime);
168 static const struct got_error *
169 parse_ref_file(struct got_reference **ref, const char *name,
170 const char *absname, const char *abspath, int lock)
172 const struct got_error *err = NULL;
177 struct got_lockfile *lf = NULL;
181 err = got_lockfile_lock(&lf, abspath, -1);
183 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
184 err = got_error_not_ref(name);
189 f = fopen(abspath, "rb");
191 if (errno != ENOTDIR && errno != ENOENT)
192 err = got_error_from_errno2("fopen", abspath);
194 err = got_error_not_ref(name);
196 got_lockfile_unlock(lf, -1);
199 if (fstat(fileno(f), &sb) == -1) {
200 err = got_error_from_errno2("fstat", abspath);
204 linelen = getline(&line, &linesize, f);
207 err = NULL; /* ignore empty files (could be locks) */
210 err = got_error(GOT_ERR_NOT_REF);
212 err = got_ferror(f, GOT_ERR_IO);
214 err = got_error_from_errno2("getline", abspath);
217 got_lockfile_unlock(lf, -1);
220 while (linelen > 0 && line[linelen - 1] == '\n') {
221 line[linelen - 1] = '\0';
225 err = parse_ref_line(ref, absname, line, sb.st_mtime);
228 got_lockfile_unlock(lf, -1);
233 got_lockfile_unlock(lf, -1);
238 if (fclose(f) == EOF && err == NULL) {
239 err = got_error_from_errno("fclose");
242 got_ref_unlock(*ref);
251 is_well_known_ref(const char *refname)
253 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
254 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
255 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
256 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
260 get_refs_dir_path(struct got_repository *repo, const char *refname)
262 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
263 return strdup(got_repo_get_path_git_dir(repo));
265 return got_repo_get_path_refs(repo);
269 got_ref_name_is_valid(const char *name)
272 const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
273 const char *forbidden_seq[] = { "//", "..", "@{" };
274 const char *lfs = GOT_LOCKFILE_SUFFIX;
275 const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
278 if (name[0] == '@' && name[1] == '\0')
283 if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
286 for (i = 0; i < nitems(forbidden); i++) {
287 if (*s == forbidden[i])
290 for (i = 0; i < nitems(forbidden_seq); i++) {
291 if (s[0] == forbidden_seq[i][0] &&
292 s[1] == forbidden_seq[i][1])
295 if (iscntrl((unsigned char)s[0]))
297 if (s[0] == '.' && s[1] == '\0')
300 const char *nextseg = s + 1;
301 if (nextseg[0] == '\0' || nextseg[0] == '.' ||
304 if (seg <= s - lfs_len &&
305 strncmp(s - lfs_len, lfs, lfs_len) == 0)
312 if (seg <= s - lfs_len &&
313 strncmp(s - lfs_len, lfs, lfs_len) == 0)
319 const struct got_error *
320 got_ref_alloc(struct got_reference **ref, const char *name,
321 struct got_object_id *id)
323 if (!got_ref_name_is_valid(name))
324 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
326 return alloc_ref(ref, name, id, 0, 0);
329 const struct got_error *
330 got_ref_alloc_symref(struct got_reference **ref, const char *name,
331 struct got_reference *target_ref)
333 if (!got_ref_name_is_valid(name))
334 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
336 return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
339 static const struct got_error *
340 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
341 const char *line, time_t mtime)
343 struct got_object_id id;
348 if (line[0] == '#' || line[0] == '^')
351 if (!got_parse_sha1_digest(id.sha1, line))
352 return got_error(GOT_ERR_BAD_REF_DATA);
355 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
359 name = line + SHA1_DIGEST_STRING_LENGTH;
361 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED, mtime);
364 static const struct got_error *
365 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
366 int nsubdirs, const char *refname, time_t mtime)
368 const struct got_error *err = NULL;
373 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
378 abs_refname = (char *)refname;
380 linelen = getline(&line, &linesize, f);
384 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
387 if (linelen > 0 && line[linelen - 1] == '\n')
388 line[linelen - 1] = '\0';
389 for (i = 0; i < nsubdirs; i++) {
390 if (!ref_is_absolute &&
391 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
393 return got_error_from_errno("asprintf");
394 err = parse_packed_ref_line(ref, abs_refname, line,
396 if (!ref_is_absolute)
398 if (err || *ref != NULL)
403 } while (*ref == NULL);
409 static const struct got_error *
410 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
411 const char *name, int lock)
413 const struct got_error *err = NULL;
415 char *absname = NULL;
416 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
417 int ref_is_well_known = (subdir[0] == '\0' && is_well_known_ref(name));
421 if (!got_ref_name_is_valid(name))
422 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
424 if (ref_is_absolute || ref_is_well_known) {
425 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
426 return got_error_from_errno("asprintf");
427 absname = (char *)name;
429 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
430 subdir[0] ? "/" : "", name) == -1)
431 return got_error_from_errno("asprintf");
433 if (asprintf(&absname, "refs/%s%s%s",
434 subdir, subdir[0] ? "/" : "", name) == -1) {
435 err = got_error_from_errno("asprintf");
440 err = parse_ref_file(ref, name, absname, path, lock);
442 if (!ref_is_absolute && !ref_is_well_known)
448 const struct got_error *
449 got_ref_open(struct got_reference **ref, struct got_repository *repo,
450 const char *refname, int lock)
452 const struct got_error *err = NULL;
453 char *path_refs = NULL;
454 const char *subdirs[] = {
455 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
458 int well_known = is_well_known_ref(refname);
459 struct got_lockfile *lf = NULL;
463 path_refs = get_refs_dir_path(repo, refname);
464 if (path_refs == NULL) {
465 err = got_error_from_errno2("get_refs_dir_path", refname);
470 err = open_ref(ref, path_refs, "", refname, lock);
472 char *packed_refs_path;
475 /* Search on-disk refs before packed refs! */
476 for (i = 0; i < nitems(subdirs); i++) {
477 err = open_ref(ref, path_refs, subdirs[i], refname,
479 if ((err && err->code != GOT_ERR_NOT_REF) || *ref)
483 packed_refs_path = got_repo_get_path_packed_refs(repo);
484 if (packed_refs_path == NULL) {
485 err = got_error_from_errno(
486 "got_repo_get_path_packed_refs");
491 err = got_lockfile_lock(&lf, packed_refs_path, -1);
495 f = fopen(packed_refs_path, "rb");
496 free(packed_refs_path);
499 if (fstat(fileno(f), &sb) == -1) {
500 err = got_error_from_errno2("fstat",
504 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
505 refname, sb.st_mtime);
507 if (fclose(f) == EOF) {
508 err = got_error_from_errno("fclose");
517 if (!err && *ref == NULL)
518 err = got_error_not_ref(refname);
520 got_lockfile_unlock(lf, -1);
526 got_ref_close(struct got_reference *ref)
528 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
529 free(ref->ref.symref.name);
530 free(ref->ref.symref.ref);
532 free(ref->ref.ref.name);
536 struct got_reference *
537 got_ref_dup(struct got_reference *ref)
539 struct got_reference *ret;
541 ret = calloc(1, sizeof(*ret));
545 ret->flags = ref->flags;
546 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
547 ret->ref.symref.name = strdup(ref->ref.symref.name);
548 if (ret->ref.symref.name == NULL) {
552 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
553 if (ret->ref.symref.ref == NULL) {
554 free(ret->ref.symref.name);
559 ret->ref.ref.name = strdup(ref->ref.ref.name);
560 if (ret->ref.ref.name == NULL) {
564 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
565 sizeof(ret->ref.ref.sha1));
571 const struct got_error *
572 got_reflist_entry_dup(struct got_reflist_entry **newp,
573 struct got_reflist_entry *re)
575 const struct got_error *err = NULL;
576 struct got_reflist_entry *new;
580 new = malloc(sizeof(*new));
582 return got_error_from_errno("malloc");
584 new->ref = got_ref_dup(re->ref);
585 if (new->ref == NULL) {
586 err = got_error_from_errno("got_ref_dup");
595 const struct got_error *
596 got_ref_resolve_symbolic(struct got_reference **resolved,
597 struct got_repository *repo, struct got_reference *ref)
599 struct got_reference *nextref;
600 const struct got_error *err;
602 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
606 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
607 err = got_ref_resolve_symbolic(resolved, repo, nextref);
609 *resolved = got_ref_dup(nextref);
611 got_ref_close(nextref);
615 static const struct got_error *
616 ref_resolve(struct got_object_id **id, struct got_repository *repo,
617 struct got_reference *ref, int recursion)
619 const struct got_error *err;
622 return got_error_msg(GOT_ERR_RECURSION,
623 "reference recursion limit reached");
625 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
626 struct got_reference *resolved = NULL;
627 err = got_ref_resolve_symbolic(&resolved, repo, ref);
629 err = ref_resolve(id, repo, resolved, --recursion);
631 got_ref_close(resolved);
635 *id = calloc(1, sizeof(**id));
637 return got_error_from_errno("calloc");
638 memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
642 const struct got_error *
643 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
644 struct got_reference *ref)
646 return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
650 got_ref_to_str(struct got_reference *ref)
654 if (ref->flags & GOT_REF_IS_SYMBOLIC)
655 return strdup(ref->ref.symref.ref);
657 str = malloc(SHA1_DIGEST_STRING_LENGTH);
661 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
662 SHA1_DIGEST_STRING_LENGTH) == NULL) {
671 got_ref_get_name(struct got_reference *ref)
673 if (ref->flags & GOT_REF_IS_SYMBOLIC)
674 return ref->ref.symref.name;
676 return ref->ref.ref.name;
680 got_ref_get_symref_target(struct got_reference *ref)
682 if (ref->flags & GOT_REF_IS_SYMBOLIC)
683 return ref->ref.symref.ref;
689 got_ref_get_mtime(struct got_reference *ref)
694 const struct got_error *
695 got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
696 struct got_reference* re2)
698 const char *name1 = got_ref_get_name(re1);
699 const char *name2 = got_ref_get_name(re2);
701 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
705 const struct got_error *
706 got_ref_cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
707 struct got_reference *ref2)
709 const struct got_error *err = NULL;
710 struct got_repository *repo = arg;
711 struct got_object_id *id1, *id2 = NULL;
712 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
713 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
718 err = got_ref_resolve(&id1, repo, ref1);
721 err = got_object_open_as_tag(&tag1, repo, id1);
723 if (err->code != GOT_ERR_OBJ_TYPE)
725 /* "lightweight" tag */
726 err = got_object_open_as_commit(&commit1, repo, id1);
729 time1 = got_object_commit_get_committer_time(commit1);
731 time1 = got_object_tag_get_tagger_time(tag1);
733 err = got_ref_resolve(&id2, repo, ref2);
736 err = got_object_open_as_tag(&tag2, repo, id2);
738 if (err->code != GOT_ERR_OBJ_TYPE)
740 /* "lightweight" tag */
741 err = got_object_open_as_commit(&commit2, repo, id2);
744 time2 = got_object_commit_get_committer_time(commit2);
746 time2 = got_object_tag_get_tagger_time(tag2);
748 /* Put latest tags first. */
751 else if (time1 > time2)
754 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
759 got_object_tag_close(tag1);
761 got_object_tag_close(tag2);
763 got_object_commit_close(commit1);
765 got_object_commit_close(commit2);
769 const struct got_error *
770 got_ref_cmp_by_commit_timestamp_descending(void *arg, int *cmp,
771 struct got_reference *ref1, struct got_reference *ref2)
773 const struct got_error *err;
774 struct got_repository *repo = arg;
775 struct got_object_id *id1, *id2 = NULL;
776 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
781 err = got_ref_resolve(&id1, repo, ref1);
784 err = got_ref_resolve(&id2, repo, ref2);
788 err = got_object_open_as_commit(&commit1, repo, id1);
791 err = got_object_open_as_commit(&commit2, repo, id2);
795 time1 = got_object_commit_get_committer_time(commit1);
796 time2 = got_object_commit_get_committer_time(commit2);
799 else if (time2 < time1)
805 got_object_commit_close(commit1);
807 got_object_commit_close(commit2);
811 const struct got_error *
812 got_reflist_insert(struct got_reflist_entry **newp, struct got_reflist_head *refs,
813 struct got_reference *ref, got_ref_cmp_cb cmp_cb, void *cmp_arg)
815 const struct got_error *err;
816 struct got_reflist_entry *new, *re;
821 new = malloc(sizeof(*new));
823 return got_error_from_errno("malloc");
828 * We must de-duplicate entries on insert because packed-refs may
829 * contain redundant entries. On-disk refs take precedence.
830 * This code assumes that on-disk revs are read before packed-refs.
831 * We're iterating the list anyway, so insert elements sorted by name.
833 * Many callers will provide paths in a somewhat sorted order.
834 * Iterating backwards from the tail of the list should be more
835 * efficient than traversing through the entire list each time
836 * an element is inserted.
838 re = TAILQ_LAST(refs, got_reflist_head);
840 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
848 } else if (cmp < 0) {
849 TAILQ_INSERT_AFTER(refs, re, new, entry);
852 re = TAILQ_PREV(re, got_reflist_head, entry);
855 TAILQ_INSERT_HEAD(refs, new, entry);
859 static const struct got_error *
860 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
861 const char *subdir, struct got_repository *repo,
862 got_ref_cmp_cb cmp_cb, void *cmp_arg)
864 const struct got_error *err = NULL;
868 while (subdir[0] == '/')
871 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
872 return got_error_from_errno("asprintf");
874 d = opendir(path_subdir);
880 struct got_reference *ref;
888 if (strcmp(dent->d_name, ".") == 0 ||
889 strcmp(dent->d_name, "..") == 0)
892 err = got_path_dirent_type(&type, path_subdir, dent);
898 err = open_ref(&ref, path_refs, subdir, dent->d_name,
903 struct got_reflist_entry *new;
904 err = got_reflist_insert(&new, refs, ref,
906 if (err || new == NULL /* duplicate */)
913 if (asprintf(&child, "%s%s%s", subdir,
914 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
915 err = got_error_from_errno("asprintf");
918 err = gather_on_disk_refs(refs, path_refs, child, repo,
933 const struct got_error *
934 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
935 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
937 const struct got_error *err;
938 char *packed_refs_path, *path_refs = NULL;
939 char *abs_namespace = NULL;
940 char *buf = NULL, *ondisk_ref_namespace = NULL;
943 struct got_reference *ref;
944 struct got_reflist_entry *new;
946 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
947 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
948 if (path_refs == NULL) {
949 err = got_error_from_errno("get_refs_dir_path");
952 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
955 err = got_reflist_insert(&new, refs, ref, cmp_cb, cmp_arg);
956 if (err || new == NULL /* duplicate */)
958 if (err && err->code != GOT_ERR_NOT_REF)
961 /* Try listing a single reference. */
962 const char *refname = ref_namespace;
963 path_refs = get_refs_dir_path(repo, refname);
964 if (path_refs == NULL) {
965 err = got_error_from_errno("get_refs_dir_path");
968 err = open_ref(&ref, path_refs, "", refname, 0);
970 if (err->code != GOT_ERR_NOT_REF)
972 /* Try to look up references in a given namespace. */
974 err = got_reflist_insert(&new, refs, ref,
976 if (err || new == NULL /* duplicate */)
984 /* Canonicalize the path to eliminate double-slashes if any. */
985 if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
986 err = got_error_from_errno("asprintf");
989 len = strlen(abs_namespace) + 1;
992 err = got_error_from_errno("malloc");
995 err = got_canonpath(abs_namespace, buf, len);
998 ondisk_ref_namespace = buf;
999 while (ondisk_ref_namespace[0] == '/')
1000 ondisk_ref_namespace++;
1001 if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
1002 ondisk_ref_namespace += 5;
1003 else if (strcmp(ondisk_ref_namespace, "refs") == 0)
1004 ondisk_ref_namespace = "";
1007 /* Gather on-disk refs before parsing packed-refs. */
1009 path_refs = get_refs_dir_path(repo, "");
1010 if (path_refs == NULL) {
1011 err = got_error_from_errno("get_refs_dir_path");
1014 err = gather_on_disk_refs(refs, path_refs,
1015 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
1021 * The packed-refs file may contain redundant entries, in which
1022 * case on-disk refs take precedence.
1024 packed_refs_path = got_repo_get_path_packed_refs(repo);
1025 if (packed_refs_path == NULL) {
1026 err = got_error_from_errno("got_repo_get_path_packed_refs");
1030 f = fopen(packed_refs_path, "r");
1031 free(packed_refs_path);
1033 size_t linesize = 0;
1037 if (fstat(fileno(f), &sb) == -1) {
1038 err = got_error_from_errno2("fstat", packed_refs_path);
1042 linelen = getline(&line, &linesize, f);
1043 if (linelen == -1) {
1046 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1049 if (linelen > 0 && line[linelen - 1] == '\n')
1050 line[linelen - 1] = '\0';
1051 err = parse_packed_ref_line(&ref, NULL, line,
1056 if (ref_namespace) {
1058 name = got_ref_get_name(ref);
1059 if (!got_path_is_child(name,
1061 strlen(ref_namespace))) {
1066 err = got_reflist_insert(&new, refs, ref,
1068 if (err || new == NULL /* duplicate */)
1076 free(abs_namespace);
1080 if (f && fclose(f) == EOF && err == NULL)
1081 err = got_error_from_errno("fclose");
1086 got_ref_list_free(struct got_reflist_head *refs)
1088 struct got_reflist_entry *re;
1090 while ((re = TAILQ_FIRST(refs))) {
1091 TAILQ_REMOVE(refs, re, entry);
1092 got_ref_close(re->ref);
1099 got_ref_is_symbolic(struct got_reference *ref)
1101 return (ref->flags & GOT_REF_IS_SYMBOLIC);
1104 const struct got_error *
1105 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1107 if (ref->flags & GOT_REF_IS_SYMBOLIC)
1108 return got_error(GOT_ERR_BAD_REF_TYPE);
1110 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
1114 const struct got_error *
1115 got_ref_change_symref(struct got_reference *ref, const char *refname)
1119 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1120 return got_error(GOT_ERR_BAD_REF_TYPE);
1122 new_name = strdup(refname);
1123 if (new_name == NULL)
1124 return got_error_from_errno("strdup");
1126 free(ref->ref.symref.ref);
1127 ref->ref.symref.ref = new_name;
1131 const struct got_error *
1132 got_ref_change_symref_to_ref(struct got_reference *symref,
1133 struct got_object_id *id)
1135 if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1136 return got_error(GOT_ERR_BAD_REF_TYPE);
1138 symref->ref.ref.name = symref->ref.symref.name;
1139 memcpy(symref->ref.ref.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1140 symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1144 const struct got_error *
1145 got_ref_write(struct got_reference *ref, struct got_repository *repo)
1147 const struct got_error *err = NULL, *unlock_err = NULL;
1148 const char *name = got_ref_get_name(ref);
1149 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1150 struct got_lockfile *lf = NULL;
1155 path_refs = get_refs_dir_path(repo, name);
1156 if (path_refs == NULL) {
1157 err = got_error_from_errno2("get_refs_dir_path", name);
1161 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1162 err = got_error_from_errno("asprintf");
1166 err = got_opentemp_named(&tmppath, &f, path);
1169 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1171 err = got_path_dirname(&parent, path);
1174 err = got_path_mkdir(parent);
1178 err = got_opentemp_named(&tmppath, &f, path);
1183 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1184 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1185 if (n != strlen(ref->ref.symref.ref) + 6) {
1186 err = got_ferror(f, GOT_ERR_IO);
1190 char hex[SHA1_DIGEST_STRING_LENGTH];
1191 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
1192 sizeof(hex)) == NULL) {
1193 err = got_error(GOT_ERR_BAD_REF_DATA);
1196 n = fprintf(f, "%s\n", hex);
1197 if (n != sizeof(hex)) {
1198 err = got_ferror(f, GOT_ERR_IO);
1203 if (ref->lf == NULL) {
1204 err = got_lockfile_lock(&lf, path, -1);
1209 /* XXX: check if old content matches our expectations? */
1211 if (stat(path, &sb) != 0) {
1212 if (errno != ENOENT) {
1213 err = got_error_from_errno2("stat", path);
1216 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1219 if (fchmod(fileno(f), sb.st_mode) != 0) {
1220 err = got_error_from_errno2("fchmod", tmppath);
1224 if (rename(tmppath, path) != 0) {
1225 err = got_error_from_errno3("rename", tmppath, path);
1231 if (stat(path, &sb) == -1) {
1232 err = got_error_from_errno2("stat", path);
1235 ref->mtime = sb.st_mtime;
1237 if (ref->lf == NULL && lf)
1238 unlock_err = got_lockfile_unlock(lf, -1);
1240 if (fclose(f) == EOF && err == NULL)
1241 err = got_error_from_errno("fclose");
1246 if (unlink(tmppath) != 0 && err == NULL)
1247 err = got_error_from_errno2("unlink", tmppath);
1250 return err ? err : unlock_err;
1253 static const struct got_error *
1254 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1256 const struct got_error *err = NULL, *unlock_err = NULL;
1257 struct got_lockfile *lf = NULL;
1258 FILE *f = NULL, *tmpf = NULL;
1259 char *line = NULL, *packed_refs_path, *tmppath = NULL;
1260 size_t linesize = 0;
1261 struct got_reflist_head refs;
1262 int found_delref = 0;
1264 /* The packed-refs file does not cotain symbolic references. */
1265 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1266 return got_error(GOT_ERR_BAD_REF_DATA);
1270 packed_refs_path = got_repo_get_path_packed_refs(repo);
1271 if (packed_refs_path == NULL)
1272 return got_error_from_errno("got_repo_get_path_packed_refs");
1274 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
1278 if (delref->lf == NULL) {
1279 err = got_lockfile_lock(&lf, packed_refs_path, -1);
1284 f = fopen(packed_refs_path, "r");
1286 err = got_error_from_errno2("fopen", packed_refs_path);
1291 struct got_reference *ref;
1292 struct got_reflist_entry *new;
1294 linelen = getline(&line, &linesize, f);
1295 if (linelen == -1) {
1298 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1301 if (linelen > 0 && line[linelen - 1] == '\n')
1302 line[linelen - 1] = '\0';
1303 err = parse_packed_ref_line(&ref, NULL, line, 0);
1309 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1310 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1311 sizeof(delref->ref.ref.sha1)) == 0) {
1317 err = got_reflist_insert(&new, &refs, ref,
1318 got_ref_cmp_by_name, NULL);
1319 if (err || new == NULL /* duplicate */)
1326 struct got_reflist_entry *re;
1330 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1331 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1332 err = got_ferror(f, GOT_ERR_IO);
1336 TAILQ_FOREACH(re, &refs, entry) {
1337 uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
1339 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1340 sizeof(hex)) == NULL) {
1341 err = got_error(GOT_ERR_BAD_REF_DATA);
1344 n = fprintf(tmpf, "%s ", hex);
1345 if (n != sizeof(hex)) {
1346 err = got_ferror(f, GOT_ERR_IO);
1349 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1350 if (n != strlen(re->ref->ref.ref.name) + 1) {
1351 err = got_ferror(f, GOT_ERR_IO);
1356 if (fflush(tmpf) != 0) {
1357 err = got_error_from_errno("fflush");
1361 if (fstat(fileno(f), &sb) != 0) {
1362 if (errno != ENOENT) {
1363 err = got_error_from_errno2("fstat",
1367 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1370 if (fchmod(fileno(tmpf), sb.st_mode) != 0) {
1371 err = got_error_from_errno2("fchmod", tmppath);
1375 if (rename(tmppath, packed_refs_path) != 0) {
1376 err = got_error_from_errno3("rename", tmppath,
1382 if (delref->lf == NULL && lf)
1383 unlock_err = got_lockfile_unlock(lf, -1);
1385 if (fclose(f) == EOF && err == NULL)
1386 err = got_error_from_errno("fclose");
1390 if (fclose(tmpf) == EOF && err == NULL)
1391 err = got_error_from_errno("fclose");
1394 free(packed_refs_path);
1396 got_ref_list_free(&refs);
1397 return err ? err : unlock_err;
1400 static const struct got_error *
1401 delete_loose_ref(struct got_reference *ref, struct got_repository *repo)
1403 const struct got_error *err = NULL, *unlock_err = NULL;
1404 const char *name = got_ref_get_name(ref);
1405 char *path_refs = NULL, *path = NULL;
1406 struct got_lockfile *lf = NULL;
1408 path_refs = get_refs_dir_path(repo, name);
1409 if (path_refs == NULL) {
1410 err = got_error_from_errno2("get_refs_dir_path", name);
1414 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1415 err = got_error_from_errno("asprintf");
1419 if (ref->lf == NULL) {
1420 err = got_lockfile_lock(&lf, path, -1);
1425 /* XXX: check if old content matches our expectations? */
1427 if (unlink(path) != 0)
1428 err = got_error_from_errno2("unlink", path);
1430 if (ref->lf == NULL && lf)
1431 unlock_err = got_lockfile_unlock(lf, -1);
1435 return err ? err : unlock_err;
1438 const struct got_error *
1439 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1441 const struct got_error *err = NULL;
1442 struct got_reference *ref2;
1444 if (ref->flags & GOT_REF_IS_PACKED) {
1445 err = delete_packed_ref(ref, repo);
1449 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1451 if (err->code == GOT_ERR_NOT_REF)
1456 err = delete_loose_ref(ref2, repo);
1457 got_ref_close(ref2);
1460 err = delete_loose_ref(ref, repo);
1464 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1466 if (err->code == GOT_ERR_NOT_REF)
1471 err = delete_packed_ref(ref2, repo);
1472 got_ref_close(ref2);
1477 const struct got_error *
1478 got_ref_unlock(struct got_reference *ref)
1480 const struct got_error *err;
1481 err = got_lockfile_unlock(ref->lf, -1);
1486 struct got_reflist_object_id_map {
1487 struct got_object_idset *idset;
1490 struct got_reflist_object_id_map_entry {
1491 struct got_reflist_head refs;
1494 static const struct got_error *
1495 add_object_id_map_entry(struct got_object_idset *idset,
1496 struct got_object_id *id, struct got_reflist_entry *re)
1498 const struct got_error *err = NULL;
1499 struct got_reflist_object_id_map_entry *ent;
1500 struct got_reflist_entry *new;
1502 ent = got_object_idset_get(idset, id);
1504 ent = malloc(sizeof(*ent));
1506 return got_error_from_errno("malloc");
1508 TAILQ_INIT(&ent->refs);
1509 err = got_object_idset_add(idset, id, ent);
1514 err = got_reflist_entry_dup(&new, re);
1518 TAILQ_INSERT_TAIL(&ent->refs, new, entry);
1522 const struct got_error *
1523 got_reflist_object_id_map_create(struct got_reflist_object_id_map **map,
1524 struct got_reflist_head *refs, struct got_repository *repo)
1526 const struct got_error *err = NULL;
1527 struct got_object_idset *idset;
1528 struct got_object_id *id = NULL;
1529 struct got_reflist_entry *re;
1531 idset = got_object_idset_alloc();
1533 return got_error_from_errno("got_object_idset_alloc");
1535 *map = malloc(sizeof(**map));
1537 got_object_idset_free(idset);
1538 return got_error_from_errno("malloc");
1540 (*map)->idset = idset;
1542 TAILQ_FOREACH(re, refs, entry) {
1543 struct got_tag_object *tag = NULL;
1545 err = got_ref_resolve(&id, repo, re->ref);
1549 err = add_object_id_map_entry(idset, id, re);
1553 if (strstr(got_ref_get_name(re->ref), "/tags/") == NULL) {
1559 err = got_object_open_as_tag(&tag, repo, id);
1561 if (err->code != GOT_ERR_OBJ_TYPE)
1563 /* Ref points at something other than a tag. */
1571 err = add_object_id_map_entry(idset,
1572 got_object_tag_get_object_id(tag), re);
1573 got_object_tag_close(tag);
1583 got_reflist_object_id_map_free(*map);
1589 struct got_reflist_head *
1590 got_reflist_object_id_map_lookup(struct got_reflist_object_id_map *map,
1591 struct got_object_id *id)
1593 struct got_reflist_object_id_map_entry *ent;
1594 ent = got_object_idset_get(map->idset, id);
1600 static const struct got_error *
1601 free_id_map_entry(struct got_object_id *id, void *data, void *arg)
1603 struct got_reflist_object_id_map_entry *ent = data;
1605 got_ref_list_free(&ent->refs);
1611 got_reflist_object_id_map_free(struct got_reflist_object_id_map *map)
1613 got_object_idset_for_each(map->idset, free_id_map_entry, NULL);
1614 got_object_idset_free(map->idset);