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>
32 #include "got_compat.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_object_idset.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;
92 static const struct got_error *
93 alloc_ref(struct got_reference **ref, const char *name,
94 struct got_object_id *id, int flags, time_t mtime)
96 const struct got_error *err = NULL;
98 *ref = calloc(1, sizeof(**ref));
100 return got_error_from_errno("calloc");
102 memcpy((*ref)->ref.ref.sha1, id->sha1, sizeof((*ref)->ref.ref.sha1));
103 (*ref)->flags = flags;
104 (*ref)->ref.ref.name = strdup(name);
105 (*ref)->mtime = mtime;
106 if ((*ref)->ref.ref.name == NULL) {
107 err = got_error_from_errno("strdup");
114 static const struct got_error *
115 alloc_symref(struct got_reference **ref, const char *name,
116 const char *target_ref, int flags)
118 const struct got_error *err = NULL;
120 *ref = calloc(1, sizeof(**ref));
122 return got_error_from_errno("calloc");
124 (*ref)->flags = GOT_REF_IS_SYMBOLIC | flags;
125 (*ref)->ref.symref.name = strdup(name);
126 if ((*ref)->ref.symref.name == NULL) {
127 err = got_error_from_errno("strdup");
132 (*ref)->ref.symref.ref = strdup(target_ref);
133 if ((*ref)->ref.symref.ref == NULL) {
134 err = got_error_from_errno("strdup");
141 static const struct got_error *
142 parse_symref(struct got_reference **ref, const char *name, const char *line)
145 return got_error(GOT_ERR_BAD_REF_DATA);
147 return alloc_symref(ref, name, line, 0);
150 static const struct got_error *
151 parse_ref_line(struct got_reference **ref, const char *name, const char *line,
154 struct got_object_id id;
156 if (strncmp(line, "ref: ", 5) == 0) {
158 return parse_symref(ref, name, line);
161 if (!got_parse_sha1_digest(id.sha1, line))
162 return got_error(GOT_ERR_BAD_REF_DATA);
164 return alloc_ref(ref, name, &id, 0, mtime);
167 static const struct got_error *
168 parse_ref_file(struct got_reference **ref, const char *name,
169 const char *absname, const char *abspath, int lock)
171 const struct got_error *err = NULL;
176 struct got_lockfile *lf = NULL;
180 err = got_lockfile_lock(&lf, abspath, -1);
182 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
183 err = got_error_not_ref(name);
188 f = fopen(abspath, "rb");
190 if (errno != ENOTDIR && errno != ENOENT)
191 err = got_error_from_errno2("fopen", abspath);
193 err = got_error_not_ref(name);
195 got_lockfile_unlock(lf, -1);
198 if (fstat(fileno(f), &sb) == -1) {
199 err = got_error_from_errno2("fstat", abspath);
203 linelen = getline(&line, &linesize, f);
206 err = NULL; /* ignore empty files (could be locks) */
209 err = got_error(GOT_ERR_NOT_REF);
211 err = got_ferror(f, GOT_ERR_IO);
213 err = got_error_from_errno2("getline", abspath);
216 got_lockfile_unlock(lf, -1);
219 while (linelen > 0 && line[linelen - 1] == '\n') {
220 line[linelen - 1] = '\0';
224 err = parse_ref_line(ref, absname, line, sb.st_mtime);
227 got_lockfile_unlock(lf, -1);
232 got_lockfile_unlock(lf, -1);
237 if (fclose(f) == EOF && err == NULL) {
238 err = got_error_from_errno("fclose");
241 got_ref_unlock(*ref);
250 is_well_known_ref(const char *refname)
252 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
253 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
254 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
255 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
259 get_refs_dir_path(struct got_repository *repo, const char *refname)
261 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
262 return strdup(got_repo_get_path_git_dir(repo));
264 return got_repo_get_path_refs(repo);
268 got_ref_name_is_valid(const char *name)
271 const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
272 const char *forbidden_seq[] = { "//", "..", "@{" };
273 const char *lfs = GOT_LOCKFILE_SUFFIX;
274 const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
277 if (name[0] == '@' && name[1] == '\0')
282 if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
285 for (i = 0; i < nitems(forbidden); i++) {
286 if (*s == forbidden[i])
289 for (i = 0; i < nitems(forbidden_seq); i++) {
290 if (s[0] == forbidden_seq[i][0] &&
291 s[1] == forbidden_seq[i][1])
294 if (iscntrl((unsigned char)s[0]))
296 if (s[0] == '.' && s[1] == '\0')
299 const char *nextseg = s + 1;
300 if (nextseg[0] == '\0' || nextseg[0] == '.' ||
303 if (seg <= s - lfs_len &&
304 strncmp(s - lfs_len, lfs, lfs_len) == 0)
311 if (seg <= s - lfs_len &&
312 strncmp(s - lfs_len, lfs, lfs_len) == 0)
318 const struct got_error *
319 got_ref_alloc(struct got_reference **ref, const char *name,
320 struct got_object_id *id)
322 if (!got_ref_name_is_valid(name))
323 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
325 return alloc_ref(ref, name, id, 0, 0);
328 const struct got_error *
329 got_ref_alloc_symref(struct got_reference **ref, const char *name,
330 struct got_reference *target_ref)
332 if (!got_ref_name_is_valid(name))
333 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
335 return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
338 static const struct got_error *
339 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
340 const char *line, time_t mtime)
342 struct got_object_id id;
347 if (line[0] == '#' || line[0] == '^')
350 if (!got_parse_sha1_digest(id.sha1, line))
351 return got_error(GOT_ERR_BAD_REF_DATA);
354 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
358 name = line + SHA1_DIGEST_STRING_LENGTH;
360 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED, mtime);
363 static const struct got_error *
364 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
365 int nsubdirs, const char *refname, time_t mtime)
367 const struct got_error *err = NULL;
372 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
377 abs_refname = (char *)refname;
379 linelen = getline(&line, &linesize, f);
383 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
386 if (linelen > 0 && line[linelen - 1] == '\n')
387 line[linelen - 1] = '\0';
388 for (i = 0; i < nsubdirs; i++) {
389 if (!ref_is_absolute &&
390 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
392 return got_error_from_errno("asprintf");
393 err = parse_packed_ref_line(ref, abs_refname, line,
395 if (!ref_is_absolute)
397 if (err || *ref != NULL)
402 } while (*ref == NULL);
408 static const struct got_error *
409 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
410 const char *name, int lock)
412 const struct got_error *err = NULL;
414 char *absname = NULL;
415 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
416 int ref_is_well_known = (subdir[0] == '\0' && is_well_known_ref(name));
420 if (!got_ref_name_is_valid(name))
421 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
423 if (ref_is_absolute || ref_is_well_known) {
424 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
425 return got_error_from_errno("asprintf");
426 absname = (char *)name;
428 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
429 subdir[0] ? "/" : "", name) == -1)
430 return got_error_from_errno("asprintf");
432 if (asprintf(&absname, "refs/%s%s%s",
433 subdir, subdir[0] ? "/" : "", name) == -1) {
434 err = got_error_from_errno("asprintf");
439 err = parse_ref_file(ref, name, absname, path, lock);
441 if (!ref_is_absolute && !ref_is_well_known)
447 const struct got_error *
448 got_ref_open(struct got_reference **ref, struct got_repository *repo,
449 const char *refname, int lock)
451 const struct got_error *err = NULL;
452 char *path_refs = NULL;
453 const char *subdirs[] = {
454 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
457 int well_known = is_well_known_ref(refname);
458 struct got_lockfile *lf = NULL;
462 path_refs = get_refs_dir_path(repo, refname);
463 if (path_refs == NULL) {
464 err = got_error_from_errno2("get_refs_dir_path", refname);
469 err = open_ref(ref, path_refs, "", refname, lock);
471 char *packed_refs_path;
474 /* Search on-disk refs before packed refs! */
475 for (i = 0; i < nitems(subdirs); i++) {
476 err = open_ref(ref, path_refs, subdirs[i], refname,
478 if ((err && err->code != GOT_ERR_NOT_REF) || *ref)
482 packed_refs_path = got_repo_get_path_packed_refs(repo);
483 if (packed_refs_path == NULL) {
484 err = got_error_from_errno(
485 "got_repo_get_path_packed_refs");
490 err = got_lockfile_lock(&lf, packed_refs_path, -1);
494 f = fopen(packed_refs_path, "rb");
495 free(packed_refs_path);
498 if (fstat(fileno(f), &sb) == -1) {
499 err = got_error_from_errno2("fstat",
503 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
504 refname, sb.st_mtime);
506 if (fclose(f) == EOF) {
507 err = got_error_from_errno("fclose");
516 if (!err && *ref == NULL)
517 err = got_error_not_ref(refname);
519 got_lockfile_unlock(lf, -1);
525 got_ref_close(struct got_reference *ref)
527 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
528 free(ref->ref.symref.name);
529 free(ref->ref.symref.ref);
531 free(ref->ref.ref.name);
535 struct got_reference *
536 got_ref_dup(struct got_reference *ref)
538 struct got_reference *ret;
540 ret = calloc(1, sizeof(*ret));
544 ret->flags = ref->flags;
545 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
546 ret->ref.symref.name = strdup(ref->ref.symref.name);
547 if (ret->ref.symref.name == NULL) {
551 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
552 if (ret->ref.symref.ref == NULL) {
553 free(ret->ref.symref.name);
558 ret->ref.ref.name = strdup(ref->ref.ref.name);
559 if (ret->ref.ref.name == NULL) {
563 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
564 sizeof(ret->ref.ref.sha1));
570 const struct got_error *
571 got_reflist_entry_dup(struct got_reflist_entry **newp,
572 struct got_reflist_entry *re)
574 const struct got_error *err = NULL;
575 struct got_reflist_entry *new;
579 new = malloc(sizeof(*new));
581 return got_error_from_errno("malloc");
583 new->ref = got_ref_dup(re->ref);
584 if (new->ref == NULL) {
585 err = got_error_from_errno("got_ref_dup");
594 const struct got_error *
595 got_ref_resolve_symbolic(struct got_reference **resolved,
596 struct got_repository *repo, struct got_reference *ref)
598 struct got_reference *nextref;
599 const struct got_error *err;
601 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
605 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
606 err = got_ref_resolve_symbolic(resolved, repo, nextref);
608 *resolved = got_ref_dup(nextref);
610 got_ref_close(nextref);
614 static const struct got_error *
615 ref_resolve(struct got_object_id **id, struct got_repository *repo,
616 struct got_reference *ref, int recursion)
618 const struct got_error *err;
621 return got_error_msg(GOT_ERR_RECURSION,
622 "reference recursion limit reached");
624 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
625 struct got_reference *resolved = NULL;
626 err = got_ref_resolve_symbolic(&resolved, repo, ref);
628 err = ref_resolve(id, repo, resolved, --recursion);
630 got_ref_close(resolved);
634 *id = calloc(1, sizeof(**id));
636 return got_error_from_errno("calloc");
637 memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
641 const struct got_error *
642 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
643 struct got_reference *ref)
645 return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
649 got_ref_to_str(struct got_reference *ref)
653 if (ref->flags & GOT_REF_IS_SYMBOLIC)
654 return strdup(ref->ref.symref.ref);
656 str = malloc(SHA1_DIGEST_STRING_LENGTH);
660 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
661 SHA1_DIGEST_STRING_LENGTH) == NULL) {
670 got_ref_get_name(struct got_reference *ref)
672 if (ref->flags & GOT_REF_IS_SYMBOLIC)
673 return ref->ref.symref.name;
675 return ref->ref.ref.name;
679 got_ref_get_symref_target(struct got_reference *ref)
681 if (ref->flags & GOT_REF_IS_SYMBOLIC)
682 return ref->ref.symref.ref;
688 got_ref_get_mtime(struct got_reference *ref)
693 const struct got_error *
694 got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
695 struct got_reference* re2)
697 const char *name1 = got_ref_get_name(re1);
698 const char *name2 = got_ref_get_name(re2);
700 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
704 const struct got_error *
705 got_ref_cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
706 struct got_reference *ref2)
708 const struct got_error *err = NULL;
709 struct got_repository *repo = arg;
710 struct got_object_id *id1, *id2 = NULL;
711 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
712 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
717 err = got_ref_resolve(&id1, repo, ref1);
720 err = got_object_open_as_tag(&tag1, repo, id1);
722 if (err->code != GOT_ERR_OBJ_TYPE)
724 /* "lightweight" tag */
725 err = got_object_open_as_commit(&commit1, repo, id1);
728 time1 = got_object_commit_get_committer_time(commit1);
730 time1 = got_object_tag_get_tagger_time(tag1);
732 err = got_ref_resolve(&id2, repo, ref2);
735 err = got_object_open_as_tag(&tag2, repo, id2);
737 if (err->code != GOT_ERR_OBJ_TYPE)
739 /* "lightweight" tag */
740 err = got_object_open_as_commit(&commit2, repo, id2);
743 time2 = got_object_commit_get_committer_time(commit2);
745 time2 = got_object_tag_get_tagger_time(tag2);
747 /* Put latest tags first. */
750 else if (time1 > time2)
753 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
758 got_object_tag_close(tag1);
760 got_object_tag_close(tag2);
762 got_object_commit_close(commit1);
764 got_object_commit_close(commit2);
768 const struct got_error *
769 got_ref_cmp_by_commit_timestamp_descending(void *arg, int *cmp,
770 struct got_reference *ref1, struct got_reference *ref2)
772 const struct got_error *err;
773 struct got_repository *repo = arg;
774 struct got_object_id *id1, *id2 = NULL;
775 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
780 err = got_ref_resolve(&id1, repo, ref1);
783 err = got_ref_resolve(&id2, repo, ref2);
787 err = got_object_open_as_commit(&commit1, repo, id1);
790 err = got_object_open_as_commit(&commit2, repo, id2);
794 time1 = got_object_commit_get_committer_time(commit1);
795 time2 = got_object_commit_get_committer_time(commit2);
798 else if (time2 < time1)
804 got_object_commit_close(commit1);
806 got_object_commit_close(commit2);
810 const struct got_error *
811 got_reflist_insert(struct got_reflist_entry **newp, struct got_reflist_head *refs,
812 struct got_reference *ref, got_ref_cmp_cb cmp_cb, void *cmp_arg)
814 const struct got_error *err;
815 struct got_reflist_entry *new, *re;
820 new = malloc(sizeof(*new));
822 return got_error_from_errno("malloc");
827 * We must de-duplicate entries on insert because packed-refs may
828 * contain redundant entries. On-disk refs take precedence.
829 * This code assumes that on-disk revs are read before packed-refs.
830 * We're iterating the list anyway, so insert elements sorted by name.
832 * Many callers will provide paths in a somewhat sorted order.
833 * Iterating backwards from the tail of the list should be more
834 * efficient than traversing through the entire list each time
835 * an element is inserted.
837 re = TAILQ_LAST(refs, got_reflist_head);
839 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
847 } else if (cmp < 0) {
848 TAILQ_INSERT_AFTER(refs, re, new, entry);
851 re = TAILQ_PREV(re, got_reflist_head, entry);
854 TAILQ_INSERT_HEAD(refs, new, entry);
858 static const struct got_error *
859 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
860 const char *subdir, struct got_repository *repo,
861 got_ref_cmp_cb cmp_cb, void *cmp_arg)
863 const struct got_error *err = NULL;
867 while (subdir[0] == '/')
870 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
871 return got_error_from_errno("asprintf");
873 d = opendir(path_subdir);
879 struct got_reference *ref;
887 if (strcmp(dent->d_name, ".") == 0 ||
888 strcmp(dent->d_name, "..") == 0)
891 err = got_path_dirent_type(&type, path_subdir, dent);
897 err = open_ref(&ref, path_refs, subdir, dent->d_name,
902 struct got_reflist_entry *new;
903 err = got_reflist_insert(&new, refs, ref,
905 if (err || new == NULL /* duplicate */)
912 if (asprintf(&child, "%s%s%s", subdir,
913 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
914 err = got_error_from_errno("asprintf");
917 err = gather_on_disk_refs(refs, path_refs, child, repo,
932 const struct got_error *
933 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
934 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
936 const struct got_error *err;
937 char *packed_refs_path, *path_refs = NULL;
938 char *abs_namespace = NULL;
939 char *buf = NULL, *ondisk_ref_namespace = NULL;
942 struct got_reference *ref;
943 struct got_reflist_entry *new;
945 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
946 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
947 if (path_refs == NULL) {
948 err = got_error_from_errno("get_refs_dir_path");
951 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
954 err = got_reflist_insert(&new, refs, ref, cmp_cb, cmp_arg);
955 if (err || new == NULL /* duplicate */)
957 if (err && err->code != GOT_ERR_NOT_REF)
960 /* Try listing a single reference. */
961 const char *refname = ref_namespace;
962 path_refs = get_refs_dir_path(repo, refname);
963 if (path_refs == NULL) {
964 err = got_error_from_errno("get_refs_dir_path");
967 err = open_ref(&ref, path_refs, "", refname, 0);
969 if (err->code != GOT_ERR_NOT_REF)
971 /* Try to look up references in a given namespace. */
973 err = got_reflist_insert(&new, refs, ref,
975 if (err || new == NULL /* duplicate */)
983 /* Canonicalize the path to eliminate double-slashes if any. */
984 if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
985 err = got_error_from_errno("asprintf");
988 len = strlen(abs_namespace) + 1;
991 err = got_error_from_errno("malloc");
994 err = got_canonpath(abs_namespace, buf, len);
997 ondisk_ref_namespace = buf;
998 while (ondisk_ref_namespace[0] == '/')
999 ondisk_ref_namespace++;
1000 if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
1001 ondisk_ref_namespace += 5;
1002 else if (strcmp(ondisk_ref_namespace, "refs") == 0)
1003 ondisk_ref_namespace = "";
1006 /* Gather on-disk refs before parsing packed-refs. */
1008 path_refs = get_refs_dir_path(repo, "");
1009 if (path_refs == NULL) {
1010 err = got_error_from_errno("get_refs_dir_path");
1013 err = gather_on_disk_refs(refs, path_refs,
1014 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
1020 * The packed-refs file may contain redundant entries, in which
1021 * case on-disk refs take precedence.
1023 packed_refs_path = got_repo_get_path_packed_refs(repo);
1024 if (packed_refs_path == NULL) {
1025 err = got_error_from_errno("got_repo_get_path_packed_refs");
1029 f = fopen(packed_refs_path, "r");
1030 free(packed_refs_path);
1032 size_t linesize = 0;
1036 if (fstat(fileno(f), &sb) == -1) {
1037 err = got_error_from_errno2("fstat", packed_refs_path);
1041 linelen = getline(&line, &linesize, f);
1042 if (linelen == -1) {
1045 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1048 if (linelen > 0 && line[linelen - 1] == '\n')
1049 line[linelen - 1] = '\0';
1050 err = parse_packed_ref_line(&ref, NULL, line,
1055 if (ref_namespace) {
1057 name = got_ref_get_name(ref);
1058 if (!got_path_is_child(name,
1060 strlen(ref_namespace))) {
1065 err = got_reflist_insert(&new, refs, ref,
1067 if (err || new == NULL /* duplicate */)
1075 free(abs_namespace);
1079 if (f && fclose(f) == EOF && err == NULL)
1080 err = got_error_from_errno("fclose");
1085 got_ref_list_free(struct got_reflist_head *refs)
1087 struct got_reflist_entry *re;
1089 while ((re = TAILQ_FIRST(refs))) {
1090 TAILQ_REMOVE(refs, re, entry);
1091 got_ref_close(re->ref);
1098 got_ref_is_symbolic(struct got_reference *ref)
1100 return (ref->flags & GOT_REF_IS_SYMBOLIC);
1103 const struct got_error *
1104 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1106 if (ref->flags & GOT_REF_IS_SYMBOLIC)
1107 return got_error(GOT_ERR_BAD_REF_TYPE);
1109 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
1113 const struct got_error *
1114 got_ref_change_symref(struct got_reference *ref, const char *refname)
1118 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1119 return got_error(GOT_ERR_BAD_REF_TYPE);
1121 new_name = strdup(refname);
1122 if (new_name == NULL)
1123 return got_error_from_errno("strdup");
1125 free(ref->ref.symref.ref);
1126 ref->ref.symref.ref = new_name;
1130 const struct got_error *
1131 got_ref_change_symref_to_ref(struct got_reference *symref,
1132 struct got_object_id *id)
1134 if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1135 return got_error(GOT_ERR_BAD_REF_TYPE);
1137 symref->ref.ref.name = symref->ref.symref.name;
1138 memcpy(symref->ref.ref.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1139 symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1143 const struct got_error *
1144 got_ref_write(struct got_reference *ref, struct got_repository *repo)
1146 const struct got_error *err = NULL, *unlock_err = NULL;
1147 const char *name = got_ref_get_name(ref);
1148 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1149 struct got_lockfile *lf = NULL;
1154 path_refs = get_refs_dir_path(repo, name);
1155 if (path_refs == NULL) {
1156 err = got_error_from_errno2("get_refs_dir_path", name);
1160 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1161 err = got_error_from_errno("asprintf");
1165 err = got_opentemp_named(&tmppath, &f, path);
1168 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1170 err = got_path_dirname(&parent, path);
1173 err = got_path_mkdir(parent);
1177 err = got_opentemp_named(&tmppath, &f, path);
1182 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1183 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1184 if (n != strlen(ref->ref.symref.ref) + 6) {
1185 err = got_ferror(f, GOT_ERR_IO);
1189 char hex[SHA1_DIGEST_STRING_LENGTH];
1190 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
1191 sizeof(hex)) == NULL) {
1192 err = got_error(GOT_ERR_BAD_REF_DATA);
1195 n = fprintf(f, "%s\n", hex);
1196 if (n != sizeof(hex)) {
1197 err = got_ferror(f, GOT_ERR_IO);
1202 if (ref->lf == NULL) {
1203 err = got_lockfile_lock(&lf, path, -1);
1208 /* XXX: check if old content matches our expectations? */
1210 if (stat(path, &sb) != 0) {
1211 if (errno != ENOENT) {
1212 err = got_error_from_errno2("stat", path);
1215 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1218 if (fchmod(fileno(f), sb.st_mode) != 0) {
1219 err = got_error_from_errno2("fchmod", tmppath);
1223 if (rename(tmppath, path) != 0) {
1224 err = got_error_from_errno3("rename", tmppath, path);
1230 if (stat(path, &sb) == -1) {
1231 err = got_error_from_errno2("stat", path);
1234 ref->mtime = sb.st_mtime;
1236 if (ref->lf == NULL && lf)
1237 unlock_err = got_lockfile_unlock(lf, -1);
1239 if (fclose(f) == EOF && err == NULL)
1240 err = got_error_from_errno("fclose");
1245 if (unlink(tmppath) != 0 && err == NULL)
1246 err = got_error_from_errno2("unlink", tmppath);
1249 return err ? err : unlock_err;
1252 static const struct got_error *
1253 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1255 const struct got_error *err = NULL, *unlock_err = NULL;
1256 struct got_lockfile *lf = NULL;
1257 FILE *f = NULL, *tmpf = NULL;
1258 char *line = NULL, *packed_refs_path, *tmppath = NULL;
1259 size_t linesize = 0;
1260 struct got_reflist_head refs;
1261 int found_delref = 0;
1263 /* The packed-refs file does not cotain symbolic references. */
1264 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1265 return got_error(GOT_ERR_BAD_REF_DATA);
1269 packed_refs_path = got_repo_get_path_packed_refs(repo);
1270 if (packed_refs_path == NULL)
1271 return got_error_from_errno("got_repo_get_path_packed_refs");
1273 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
1277 if (delref->lf == NULL) {
1278 err = got_lockfile_lock(&lf, packed_refs_path, -1);
1283 f = fopen(packed_refs_path, "r");
1285 err = got_error_from_errno2("fopen", packed_refs_path);
1290 struct got_reference *ref;
1291 struct got_reflist_entry *new;
1293 linelen = getline(&line, &linesize, f);
1294 if (linelen == -1) {
1297 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1300 if (linelen > 0 && line[linelen - 1] == '\n')
1301 line[linelen - 1] = '\0';
1302 err = parse_packed_ref_line(&ref, NULL, line, 0);
1308 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1309 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1310 sizeof(delref->ref.ref.sha1)) == 0) {
1316 err = got_reflist_insert(&new, &refs, ref,
1317 got_ref_cmp_by_name, NULL);
1318 if (err || new == NULL /* duplicate */)
1325 struct got_reflist_entry *re;
1329 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1330 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1331 err = got_ferror(f, GOT_ERR_IO);
1335 TAILQ_FOREACH(re, &refs, entry) {
1336 char hex[SHA1_DIGEST_STRING_LENGTH];
1338 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1339 sizeof(hex)) == NULL) {
1340 err = got_error(GOT_ERR_BAD_REF_DATA);
1343 n = fprintf(tmpf, "%s ", hex);
1344 if (n != sizeof(hex)) {
1345 err = got_ferror(f, GOT_ERR_IO);
1348 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1349 if (n != strlen(re->ref->ref.ref.name) + 1) {
1350 err = got_ferror(f, GOT_ERR_IO);
1355 if (fflush(tmpf) != 0) {
1356 err = got_error_from_errno("fflush");
1360 if (fstat(fileno(f), &sb) != 0) {
1361 if (errno != ENOENT) {
1362 err = got_error_from_errno2("fstat",
1366 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1369 if (fchmod(fileno(tmpf), sb.st_mode) != 0) {
1370 err = got_error_from_errno2("fchmod", tmppath);
1374 if (rename(tmppath, packed_refs_path) != 0) {
1375 err = got_error_from_errno3("rename", tmppath,
1381 if (delref->lf == NULL && lf)
1382 unlock_err = got_lockfile_unlock(lf, -1);
1384 if (fclose(f) == EOF && err == NULL)
1385 err = got_error_from_errno("fclose");
1389 if (fclose(tmpf) == EOF && err == NULL)
1390 err = got_error_from_errno("fclose");
1393 free(packed_refs_path);
1395 got_ref_list_free(&refs);
1396 return err ? err : unlock_err;
1399 static const struct got_error *
1400 delete_loose_ref(struct got_reference *ref, struct got_repository *repo)
1402 const struct got_error *err = NULL, *unlock_err = NULL;
1403 const char *name = got_ref_get_name(ref);
1404 char *path_refs = NULL, *path = NULL;
1405 struct got_lockfile *lf = NULL;
1407 path_refs = get_refs_dir_path(repo, name);
1408 if (path_refs == NULL) {
1409 err = got_error_from_errno2("get_refs_dir_path", name);
1413 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1414 err = got_error_from_errno("asprintf");
1418 if (ref->lf == NULL) {
1419 err = got_lockfile_lock(&lf, path, -1);
1424 /* XXX: check if old content matches our expectations? */
1426 if (unlink(path) != 0)
1427 err = got_error_from_errno2("unlink", path);
1429 if (ref->lf == NULL && lf)
1430 unlock_err = got_lockfile_unlock(lf, -1);
1434 return err ? err : unlock_err;
1437 const struct got_error *
1438 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1440 const struct got_error *err = NULL;
1441 struct got_reference *ref2;
1443 if (ref->flags & GOT_REF_IS_PACKED) {
1444 err = delete_packed_ref(ref, repo);
1448 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1450 if (err->code == GOT_ERR_NOT_REF)
1455 err = delete_loose_ref(ref2, repo);
1456 got_ref_close(ref2);
1459 err = delete_loose_ref(ref, repo);
1463 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1465 if (err->code == GOT_ERR_NOT_REF)
1470 err = delete_packed_ref(ref2, repo);
1471 got_ref_close(ref2);
1476 const struct got_error *
1477 got_ref_unlock(struct got_reference *ref)
1479 const struct got_error *err;
1480 err = got_lockfile_unlock(ref->lf, -1);
1485 struct got_reflist_object_id_map {
1486 struct got_object_idset *idset;
1489 struct got_reflist_object_id_map_entry {
1490 struct got_reflist_head refs;
1493 static const struct got_error *
1494 add_object_id_map_entry(struct got_object_idset *idset,
1495 struct got_object_id *id, struct got_reflist_entry *re)
1497 const struct got_error *err = NULL;
1498 struct got_reflist_object_id_map_entry *ent;
1499 struct got_reflist_entry *new;
1501 ent = got_object_idset_get(idset, id);
1503 ent = malloc(sizeof(*ent));
1505 return got_error_from_errno("malloc");
1507 TAILQ_INIT(&ent->refs);
1508 err = got_object_idset_add(idset, id, ent);
1513 err = got_reflist_entry_dup(&new, re);
1517 TAILQ_INSERT_TAIL(&ent->refs, new, entry);
1521 const struct got_error *
1522 got_reflist_object_id_map_create(struct got_reflist_object_id_map **map,
1523 struct got_reflist_head *refs, struct got_repository *repo)
1525 const struct got_error *err = NULL;
1526 struct got_object_idset *idset;
1527 struct got_object_id *id = NULL;
1528 struct got_reflist_entry *re;
1530 idset = got_object_idset_alloc();
1532 return got_error_from_errno("got_object_idset_alloc");
1534 *map = malloc(sizeof(**map));
1536 got_object_idset_free(idset);
1537 return got_error_from_errno("malloc");
1539 (*map)->idset = idset;
1541 TAILQ_FOREACH(re, refs, entry) {
1542 struct got_tag_object *tag = NULL;
1544 err = got_ref_resolve(&id, repo, re->ref);
1548 err = add_object_id_map_entry(idset, id, re);
1552 if (strstr(got_ref_get_name(re->ref), "/tags/") == NULL) {
1558 err = got_object_open_as_tag(&tag, repo, id);
1560 if (err->code != GOT_ERR_OBJ_TYPE)
1562 /* Ref points at something other than a tag. */
1570 err = add_object_id_map_entry(idset,
1571 got_object_tag_get_object_id(tag), re);
1572 got_object_tag_close(tag);
1582 got_reflist_object_id_map_free(*map);
1588 struct got_reflist_head *
1589 got_reflist_object_id_map_lookup(struct got_reflist_object_id_map *map,
1590 struct got_object_id *id)
1592 struct got_reflist_object_id_map_entry *ent;
1593 ent = got_object_idset_get(map->idset, id);
1599 static const struct got_error *
1600 free_id_map_entry(struct got_object_id *id, void *data, void *arg)
1602 struct got_reflist_object_id_map_entry *ent = data;
1604 got_ref_list_free(&ent->refs);
1610 got_reflist_object_id_map_free(struct got_reflist_object_id_map *map)
1612 got_object_idset_for_each(map->idset, free_id_map_entry, NULL);
1613 got_object_idset_free(map->idset);