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>
33 #include "got_error.h"
34 #include "got_object.h"
35 #include "got_repository.h"
36 #include "got_reference.h"
37 #include "got_opentemp.h"
39 #include "got_lib_sha1.h"
40 #include "got_lib_path.h"
41 #include "got_lib_delta.h"
42 #include "got_lib_inflate.h"
43 #include "got_lib_object.h"
44 #include "got_lib_lockfile.h"
47 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
50 #define GOT_REF_HEADS "heads"
51 #define GOT_REF_TAGS "tags"
52 #define GOT_REF_REMOTES "remotes"
54 /* A symbolic reference. */
60 /* A non-symbolic reference (there is no better designation). */
63 u_int8_t sha1[SHA1_DIGEST_LENGTH];
66 /* A reference which points to an arbitrary object. */
67 struct got_reference {
69 #define GOT_REF_IS_SYMBOLIC 0x01
70 #define GOT_REF_IS_PACKED 0x02
74 struct got_symref symref;
78 static const struct got_error *
79 alloc_ref(struct got_reference **ref, const char *name,
80 struct got_object_id *id, int flags)
82 const struct got_error *err = NULL;
84 *ref = calloc(1, sizeof(**ref));
86 return got_error_from_errno();
88 memcpy(&(*ref)->ref.ref.sha1, id->sha1, SHA1_DIGEST_LENGTH);
89 (*ref)->flags = flags;
90 (*ref)->ref.ref.name = strdup(name);
91 if ((*ref)->ref.ref.name == NULL) {
92 err = got_error_from_errno();
99 static const struct got_error *
100 alloc_symref(struct got_reference **ref, const char *name,
101 const char *target_ref, int flags)
103 const struct got_error *err = NULL;
105 *ref = calloc(1, sizeof(**ref));
107 return got_error_from_errno();
109 (*ref)->flags = GOT_REF_IS_SYMBOLIC | flags;
110 (*ref)->ref.symref.name = strdup(name);
111 if ((*ref)->ref.symref.name == NULL) {
112 err = got_error_from_errno();
116 (*ref)->ref.symref.ref = strdup(target_ref);
117 if ((*ref)->ref.symref.ref == NULL) {
118 err = got_error_from_errno();
125 static const struct got_error *
126 parse_symref(struct got_reference **ref, const char *name, const char *line)
129 return got_error(GOT_ERR_BAD_REF_DATA);
131 return alloc_symref(ref, name, line, 0);
134 static const struct got_error *
135 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
137 struct got_object_id id;
139 if (strncmp(line, "ref: ", 5) == 0) {
141 return parse_symref(ref, name, line);
144 if (!got_parse_sha1_digest(id.sha1, line))
145 return got_error(GOT_ERR_BAD_REF_DATA);
147 return alloc_ref(ref, name, &id, 0);
150 static const struct got_error *
151 parse_ref_file(struct got_reference **ref, const char *name,
154 const struct got_error *err = NULL;
155 FILE *f = fopen(abspath, "rb");
158 const char delim[3] = {'\0', '\0', '\0'};
163 line = fparseln(f, &len, NULL, delim, 0);
165 err = got_error(GOT_ERR_BAD_REF_DATA);
169 err = parse_ref_line(ref, name, line);
172 if (fclose(f) != 0 && err == NULL)
173 err = got_error_from_errno();
178 is_well_known_ref(const char *refname)
180 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
181 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
182 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
183 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
187 get_refs_dir_path(struct got_repository *repo, const char *refname)
189 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
190 return strdup(got_repo_get_path_git_dir(repo));
192 return got_repo_get_path_refs(repo);
196 is_valid_ref_name(const char *name)
198 const char *s, *slash, *seg;
199 const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
200 const char *forbidden_seq[] = { "//", "..", "@{" };
201 const char *lfs = GOT_LOCKFILE_SUFFIX;
202 const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
205 if (name[0] == '@' && name[1] == '\0')
208 slash = strchr(name, '/');
214 if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
217 for (i = 0; i < nitems(forbidden); i++) {
218 if (*s == forbidden[i])
221 for (i = 0; i < nitems(forbidden_seq); i++) {
222 if (s[0] == forbidden_seq[i][0] &&
223 s[1] == forbidden_seq[i][1])
226 if (iscntrl((unsigned char)s[0]))
228 if (s[0] == '.' && s[1] == '\0')
231 const char *nextseg = s + 1;
232 if (nextseg[0] == '\0' || nextseg[0] == '.' ||
235 if (seg <= s - lfs_len &&
236 strncmp(s - lfs_len, lfs, lfs_len) == 0)
243 if (seg <= s - lfs_len &&
244 strncmp(s - lfs_len, lfs, lfs_len) == 0)
250 const struct got_error *
251 got_ref_alloc(struct got_reference **ref, const char *name,
252 struct got_object_id *id)
254 if (!is_valid_ref_name(name))
255 return got_error(GOT_ERR_BAD_REF_NAME);
257 return alloc_ref(ref, name, id, 0);
260 static const struct got_error *
261 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
264 struct got_object_id id;
269 if (line[0] == '#' || line[0] == '^')
272 if (!got_parse_sha1_digest(id.sha1, line))
273 return got_error(GOT_ERR_BAD_REF_DATA);
276 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
280 name = line + SHA1_DIGEST_STRING_LENGTH;
282 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED);
285 static const struct got_error *
286 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
287 int nsubdirs, const char *refname)
289 const struct got_error *err = NULL;
293 const char delim[3] = {'\0', '\0', '\0'};
294 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
299 abs_refname = (char *)refname;
301 line = fparseln(f, &len, NULL, delim, 0);
304 for (i = 0; i < nsubdirs; i++) {
305 if (!ref_is_absolute &&
306 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
308 return got_error_from_errno();
309 err = parse_packed_ref_line(ref, abs_refname, line);
310 if (!ref_is_absolute)
312 if (err || *ref != NULL)
318 } while (*ref == NULL);
323 static const struct got_error *
324 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
327 const struct got_error *err = NULL;
329 char *normpath = NULL;
330 char *absname = NULL;
331 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
332 int ref_is_well_known = is_well_known_ref(name);
336 if (ref_is_absolute || ref_is_well_known) {
337 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
338 return got_error_from_errno();
339 absname = (char *)name;
341 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
342 subdir[0] ? "/" : "", name) == -1)
343 return got_error_from_errno();
345 if (asprintf(&absname, "refs/%s%s%s",
346 subdir, subdir[0] ? "/" : "", name) == -1) {
347 err = got_error_from_errno();
352 normpath = got_path_normalize(path);
353 if (normpath == NULL) {
354 err = got_error_from_errno();
358 err = parse_ref_file(ref, absname, normpath);
360 if (!ref_is_absolute && !ref_is_well_known)
367 const struct got_error *
368 got_ref_open(struct got_reference **ref, struct got_repository *repo,
371 const struct got_error *err = NULL;
372 char *path_refs = NULL;
373 const char *subdirs[] = {
374 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
376 int i, well_known = is_well_known_ref(refname);
380 path_refs = get_refs_dir_path(repo, refname);
381 if (path_refs == NULL) {
382 err = got_error_from_errno();
387 char *packed_refs_path;
390 /* Search on-disk refs before packed refs! */
391 for (i = 0; i < nitems(subdirs); i++) {
392 err = open_ref(ref, path_refs, subdirs[i], refname);
397 packed_refs_path = got_repo_get_path_packed_refs(repo);
398 if (packed_refs_path == NULL) {
399 err = got_error_from_errno();
403 f = fopen(packed_refs_path, "rb");
404 free(packed_refs_path);
406 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
408 if (fclose(f) != 0 && err == NULL)
409 err = got_error_from_errno();
415 err = open_ref(ref, path_refs, "", refname);
420 err = got_error_not_ref(refname);
426 got_ref_close(struct got_reference *ref)
428 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
429 free(ref->ref.symref.name);
430 free(ref->ref.symref.ref);
432 free(ref->ref.ref.name);
436 struct got_reference *
437 got_ref_dup(struct got_reference *ref)
439 struct got_reference *ret;
441 ret = calloc(1, sizeof(*ret));
445 ret->flags = ref->flags;
446 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
447 ret->ref.symref.name = strdup(ref->ref.symref.name);
448 if (ret->ref.symref.name == NULL) {
452 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
453 if (ret->ref.symref.ref == NULL) {
454 free(ret->ref.symref.name);
459 ref->ref.ref.name = strdup(ref->ref.ref.name);
460 if (ref->ref.ref.name == NULL) {
464 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
471 static const struct got_error *
472 resolve_symbolic_ref(struct got_reference **resolved,
473 struct got_repository *repo, struct got_reference *ref)
475 struct got_reference *nextref;
476 const struct got_error *err;
478 err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
482 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
483 err = resolve_symbolic_ref(resolved, repo, nextref);
485 *resolved = got_ref_dup(nextref);
487 got_ref_close(nextref);
491 const struct got_error *
492 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
493 struct got_reference *ref)
495 const struct got_error *err;
497 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
498 struct got_reference *resolved = NULL;
499 err = resolve_symbolic_ref(&resolved, repo, ref);
501 err = got_ref_resolve(id, repo, resolved);
502 got_ref_close(resolved);
506 *id = calloc(1, sizeof(**id));
508 return got_error_from_errno();
509 memcpy((*id)->sha1, ref->ref.ref.sha1, SHA1_DIGEST_LENGTH);
514 got_ref_to_str(struct got_reference *ref)
518 if (ref->flags & GOT_REF_IS_SYMBOLIC)
519 return strdup(ref->ref.symref.ref);
521 str = malloc(SHA1_DIGEST_STRING_LENGTH);
525 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
526 SHA1_DIGEST_STRING_LENGTH) == NULL) {
535 got_ref_get_name(struct got_reference *ref)
537 if (ref->flags & GOT_REF_IS_SYMBOLIC)
538 return ref->ref.symref.name;
540 return ref->ref.ref.name;
543 static const struct got_error *
544 insert_ref(struct got_reflist_head *refs, struct got_reference *ref,
545 struct got_repository *repo)
547 const struct got_error *err;
548 struct got_object_id *id;
549 struct got_reflist_entry *new, *re, *prev;
552 err = got_ref_resolve(&id, repo, ref);
556 new = malloc(sizeof(*re));
559 return got_error_from_errno();
565 * We must de-duplicate entries on insert because packed-refs may
566 * contain redundant entries. On-disk refs take precedence.
567 * This code assumes that on-disk revs are read before packed-refs.
568 * We're iterating the list anyway, so insert elements sorted by name.
570 re = SIMPLEQ_FIRST(refs);
572 cmp = got_path_cmp(got_ref_get_name(re->ref),
573 got_ref_get_name(ref));
575 got_ref_close(ref); /* duplicate */
577 } else if (cmp > 0) {
579 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
581 SIMPLEQ_INSERT_HEAD(refs, new, entry);
585 re = SIMPLEQ_NEXT(re, entry);
589 SIMPLEQ_INSERT_TAIL(refs, new, entry);
593 static const struct got_error *
594 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
595 const char *subdir, struct got_repository *repo)
597 const struct got_error *err = NULL;
601 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
602 return got_error_from_errno();
604 d = opendir(path_subdir);
610 struct got_reference *ref;
617 if (strcmp(dent->d_name, ".") == 0 ||
618 strcmp(dent->d_name, "..") == 0)
621 switch (dent->d_type) {
623 err = open_ref(&ref, path_refs, subdir, dent->d_name);
627 err = insert_ref(refs, ref, repo);
633 if (asprintf(&child, "%s%s%s", subdir,
634 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
635 err = got_error_from_errno();
638 err = gather_on_disk_refs(refs, path_refs, child, repo);
652 const struct got_error *
653 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo)
655 const struct got_error *err;
656 char *packed_refs_path, *path_refs = NULL;
658 struct got_reference *ref;
660 /* HEAD ref should always exist. */
661 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
662 if (path_refs == NULL) {
663 err = got_error_from_errno();
666 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD);
669 err = insert_ref(refs, ref, repo);
673 /* Gather on-disk refs before parsing packed-refs. */
675 path_refs = get_refs_dir_path(repo, "");
676 if (path_refs == NULL) {
677 err = got_error_from_errno();
680 err = gather_on_disk_refs(refs, path_refs, "", repo);
685 * The packed-refs file may contain redundant entries, in which
686 * case on-disk refs take precedence.
688 packed_refs_path = got_repo_get_path_packed_refs(repo);
689 if (packed_refs_path == NULL) {
690 err = got_error_from_errno();
694 f = fopen(packed_refs_path, "r");
695 free(packed_refs_path);
699 const char delim[3] = {'\0', '\0', '\0'};
701 line = fparseln(f, &len, NULL, delim, 0);
704 err = parse_packed_ref_line(&ref, NULL, line);
708 err = insert_ref(refs, ref, repo);
716 if (f && fclose(f) != 0 && err == NULL)
717 err = got_error_from_errno();
722 got_ref_list_free(struct got_reflist_head *refs)
724 struct got_reflist_entry *re;
726 while (!SIMPLEQ_EMPTY(refs)) {
727 re = SIMPLEQ_FIRST(refs);
728 SIMPLEQ_REMOVE_HEAD(refs, entry);
729 got_ref_close(re->ref);
736 const struct got_error *
737 got_ref_write(struct got_reference *ref, struct got_repository *repo)
739 const struct got_error *err = NULL, *unlock_err = NULL;
740 const char *name = got_ref_get_name(ref);
741 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
742 struct got_lockfile *lf = NULL;
747 path_refs = get_refs_dir_path(repo, name);
748 if (path_refs == NULL) {
749 err = got_error_from_errno();
753 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
754 err = got_error_from_errno();
758 err = got_opentemp_named(&tmppath, &f, path);
761 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
763 err = got_path_dirname(&parent, path);
766 err = got_path_mkdir(parent);
770 err = got_opentemp_named(&tmppath, &f, path);
775 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
776 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
777 if (n != strlen(ref->ref.symref.ref) + 6) {
778 err = got_ferror(f, GOT_ERR_IO);
782 char hex[SHA1_DIGEST_STRING_LENGTH];
783 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
784 sizeof(hex)) == NULL) {
785 err = got_error(GOT_ERR_BAD_REF_DATA);
788 n = fprintf(f, "%s\n", hex);
789 if (n != sizeof(hex)) {
790 err = got_ferror(f, GOT_ERR_IO);
795 err = got_lockfile_lock(&lf, path);
799 /* XXX: check if old content matches our expectations? */
801 if (stat(path, &sb) != 0) {
802 if (errno != ENOENT) {
803 err = got_error_from_errno();
806 sb.st_mode = GOT_DEFAULT_FILE_MODE;
809 if (rename(tmppath, path) != 0) {
810 err = got_error_from_errno();
816 if (chmod(path, sb.st_mode) != 0) {
817 err = got_error_from_errno();
822 unlock_err = got_lockfile_unlock(lf);
824 if (fclose(f) != 0 && err == NULL)
825 err = got_error_from_errno();
830 if (unlink(tmppath) != 0 && err == NULL)
831 err = got_error_from_errno();
834 return err ? err : unlock_err;
837 const struct got_error *
838 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
840 const struct got_error *err = NULL, *unlock_err = NULL;
841 const char *name = got_ref_get_name(ref);
842 char *path_refs = NULL, *path = NULL;
843 struct got_lockfile *lf = NULL;
845 /* TODO: handle packed refs ! */
847 path_refs = get_refs_dir_path(repo, name);
848 if (path_refs == NULL) {
849 err = got_error_from_errno();
853 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
854 err = got_error_from_errno();
858 err = got_lockfile_lock(&lf, path);
862 /* XXX: check if old content matches our expectations? */
864 if (unlink(path) != 0)
865 err = got_error_from_errno();
868 unlock_err = got_lockfile_unlock(lf);
872 return err ? err : unlock_err;