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;
158 const char delim[3] = {'\0', '\0', '\0'};
160 f = fopen(abspath, "rb");
164 line = fparseln(f, &len, NULL, delim, 0);
166 err = got_error(GOT_ERR_BAD_REF_DATA);
170 err = parse_ref_line(ref, name, line);
173 if (fclose(f) != 0 && err == NULL)
174 err = got_error_from_errno();
179 is_well_known_ref(const char *refname)
181 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
182 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
183 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
184 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
188 get_refs_dir_path(struct got_repository *repo, const char *refname)
190 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
191 return strdup(got_repo_get_path_git_dir(repo));
193 return got_repo_get_path_refs(repo);
197 is_valid_ref_name(const char *name)
199 const char *s, *slash, *seg;
200 const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
201 const char *forbidden_seq[] = { "//", "..", "@{" };
202 const char *lfs = GOT_LOCKFILE_SUFFIX;
203 const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
206 if (name[0] == '@' && name[1] == '\0')
209 slash = strchr(name, '/');
215 if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
218 for (i = 0; i < nitems(forbidden); i++) {
219 if (*s == forbidden[i])
222 for (i = 0; i < nitems(forbidden_seq); i++) {
223 if (s[0] == forbidden_seq[i][0] &&
224 s[1] == forbidden_seq[i][1])
227 if (iscntrl((unsigned char)s[0]))
229 if (s[0] == '.' && s[1] == '\0')
232 const char *nextseg = s + 1;
233 if (nextseg[0] == '\0' || nextseg[0] == '.' ||
236 if (seg <= s - lfs_len &&
237 strncmp(s - lfs_len, lfs, lfs_len) == 0)
244 if (seg <= s - lfs_len &&
245 strncmp(s - lfs_len, lfs, lfs_len) == 0)
251 const struct got_error *
252 got_ref_alloc(struct got_reference **ref, const char *name,
253 struct got_object_id *id)
255 if (!is_valid_ref_name(name))
256 return got_error(GOT_ERR_BAD_REF_NAME);
258 return alloc_ref(ref, name, id, 0);
261 static const struct got_error *
262 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
265 struct got_object_id id;
270 if (line[0] == '#' || line[0] == '^')
273 if (!got_parse_sha1_digest(id.sha1, line))
274 return got_error(GOT_ERR_BAD_REF_DATA);
277 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
281 name = line + SHA1_DIGEST_STRING_LENGTH;
283 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED);
286 static const struct got_error *
287 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
288 int nsubdirs, const char *refname)
290 const struct got_error *err = NULL;
294 const char delim[3] = {'\0', '\0', '\0'};
295 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
300 abs_refname = (char *)refname;
302 line = fparseln(f, &len, NULL, delim, 0);
306 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
309 for (i = 0; i < nsubdirs; i++) {
310 if (!ref_is_absolute &&
311 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
313 return got_error_from_errno();
314 err = parse_packed_ref_line(ref, abs_refname, line);
315 if (!ref_is_absolute)
317 if (err || *ref != NULL)
323 } while (*ref == NULL);
328 static const struct got_error *
329 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
332 const struct got_error *err = NULL;
334 char *normpath = NULL;
335 char *absname = NULL;
336 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
337 int ref_is_well_known = is_well_known_ref(name);
341 if (ref_is_absolute || ref_is_well_known) {
342 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
343 return got_error_from_errno();
344 absname = (char *)name;
346 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
347 subdir[0] ? "/" : "", name) == -1)
348 return got_error_from_errno();
350 if (asprintf(&absname, "refs/%s%s%s",
351 subdir, subdir[0] ? "/" : "", name) == -1) {
352 err = got_error_from_errno();
357 normpath = got_path_normalize(path);
358 if (normpath == NULL) {
359 err = got_error_from_errno();
363 err = parse_ref_file(ref, absname, normpath);
365 if (!ref_is_absolute && !ref_is_well_known)
372 const struct got_error *
373 got_ref_open(struct got_reference **ref, struct got_repository *repo,
376 const struct got_error *err = NULL;
377 char *path_refs = NULL;
378 const char *subdirs[] = {
379 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
381 int i, well_known = is_well_known_ref(refname);
385 path_refs = get_refs_dir_path(repo, refname);
386 if (path_refs == NULL) {
387 err = got_error_from_errno();
392 char *packed_refs_path;
395 /* Search on-disk refs before packed refs! */
396 for (i = 0; i < nitems(subdirs); i++) {
397 err = open_ref(ref, path_refs, subdirs[i], refname);
402 packed_refs_path = got_repo_get_path_packed_refs(repo);
403 if (packed_refs_path == NULL) {
404 err = got_error_from_errno();
408 f = fopen(packed_refs_path, "rb");
409 free(packed_refs_path);
411 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
413 if (fclose(f) != 0 && err == NULL)
414 err = got_error_from_errno();
420 err = open_ref(ref, path_refs, "", refname);
425 err = got_error_not_ref(refname);
431 got_ref_close(struct got_reference *ref)
433 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
434 free(ref->ref.symref.name);
435 free(ref->ref.symref.ref);
437 free(ref->ref.ref.name);
441 struct got_reference *
442 got_ref_dup(struct got_reference *ref)
444 struct got_reference *ret;
446 ret = calloc(1, sizeof(*ret));
450 ret->flags = ref->flags;
451 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
452 ret->ref.symref.name = strdup(ref->ref.symref.name);
453 if (ret->ref.symref.name == NULL) {
457 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
458 if (ret->ref.symref.ref == NULL) {
459 free(ret->ref.symref.name);
464 ref->ref.ref.name = strdup(ref->ref.ref.name);
465 if (ref->ref.ref.name == NULL) {
469 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
476 static const struct got_error *
477 resolve_symbolic_ref(struct got_reference **resolved,
478 struct got_repository *repo, struct got_reference *ref)
480 struct got_reference *nextref;
481 const struct got_error *err;
483 err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
487 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
488 err = resolve_symbolic_ref(resolved, repo, nextref);
490 *resolved = got_ref_dup(nextref);
492 got_ref_close(nextref);
496 const struct got_error *
497 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
498 struct got_reference *ref)
500 const struct got_error *err;
502 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
503 struct got_reference *resolved = NULL;
504 err = resolve_symbolic_ref(&resolved, repo, ref);
506 err = got_ref_resolve(id, repo, resolved);
507 got_ref_close(resolved);
511 *id = calloc(1, sizeof(**id));
513 return got_error_from_errno();
514 memcpy((*id)->sha1, ref->ref.ref.sha1, SHA1_DIGEST_LENGTH);
519 got_ref_to_str(struct got_reference *ref)
523 if (ref->flags & GOT_REF_IS_SYMBOLIC)
524 return strdup(ref->ref.symref.ref);
526 str = malloc(SHA1_DIGEST_STRING_LENGTH);
530 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
531 SHA1_DIGEST_STRING_LENGTH) == NULL) {
540 got_ref_get_name(struct got_reference *ref)
542 if (ref->flags & GOT_REF_IS_SYMBOLIC)
543 return ref->ref.symref.name;
545 return ref->ref.ref.name;
548 static const struct got_error *
549 insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
550 struct got_reference *ref, struct got_repository *repo)
552 const struct got_error *err;
553 struct got_object_id *id;
554 struct got_reflist_entry *new, *re, *prev;
559 err = got_ref_resolve(&id, repo, ref);
563 new = malloc(sizeof(*new));
566 return got_error_from_errno();
573 * We must de-duplicate entries on insert because packed-refs may
574 * contain redundant entries. On-disk refs take precedence.
575 * This code assumes that on-disk revs are read before packed-refs.
576 * We're iterating the list anyway, so insert elements sorted by name.
578 re = SIMPLEQ_FIRST(refs);
580 cmp = got_path_cmp(got_ref_get_name(re->ref),
581 got_ref_get_name(new->ref));
588 } else if (cmp > 0) {
590 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
592 SIMPLEQ_INSERT_HEAD(refs, new, entry);
596 re = SIMPLEQ_NEXT(re, entry);
600 SIMPLEQ_INSERT_TAIL(refs, new, entry);
604 static const struct got_error *
605 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
606 const char *subdir, struct got_repository *repo)
608 const struct got_error *err = NULL;
612 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
613 return got_error_from_errno();
615 d = opendir(path_subdir);
621 struct got_reference *ref;
628 if (strcmp(dent->d_name, ".") == 0 ||
629 strcmp(dent->d_name, "..") == 0)
632 switch (dent->d_type) {
634 err = open_ref(&ref, path_refs, subdir, dent->d_name);
638 struct got_reflist_entry *new;
639 err = insert_ref(&new, refs, ref, repo);
640 if (err || new == NULL /* duplicate */)
647 if (asprintf(&child, "%s%s%s", subdir,
648 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
649 err = got_error_from_errno();
652 err = gather_on_disk_refs(refs, path_refs, child, repo);
666 const struct got_error *
667 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo)
669 const struct got_error *err;
670 char *packed_refs_path, *path_refs = NULL;
672 struct got_reference *ref;
673 struct got_reflist_entry *new;
675 /* HEAD ref should always exist. */
676 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
677 if (path_refs == NULL) {
678 err = got_error_from_errno();
681 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD);
684 err = insert_ref(&new, refs, ref, repo);
685 if (err || new == NULL /* duplicate */)
690 /* Gather on-disk refs before parsing packed-refs. */
692 path_refs = get_refs_dir_path(repo, "");
693 if (path_refs == NULL) {
694 err = got_error_from_errno();
697 err = gather_on_disk_refs(refs, path_refs, "", repo);
702 * The packed-refs file may contain redundant entries, in which
703 * case on-disk refs take precedence.
705 packed_refs_path = got_repo_get_path_packed_refs(repo);
706 if (packed_refs_path == NULL) {
707 err = got_error_from_errno();
711 f = fopen(packed_refs_path, "r");
712 free(packed_refs_path);
716 const char delim[3] = {'\0', '\0', '\0'};
718 line = fparseln(f, &len, NULL, delim, 0);
722 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
725 err = parse_packed_ref_line(&ref, NULL, line);
730 err = insert_ref(&new, refs, ref, repo);
731 if (err || new == NULL /* duplicate */)
740 if (f && fclose(f) != 0 && err == NULL)
741 err = got_error_from_errno();
746 got_ref_list_free(struct got_reflist_head *refs)
748 struct got_reflist_entry *re;
750 while (!SIMPLEQ_EMPTY(refs)) {
751 re = SIMPLEQ_FIRST(refs);
752 SIMPLEQ_REMOVE_HEAD(refs, entry);
753 got_ref_close(re->ref);
760 const struct got_error *
761 got_ref_write(struct got_reference *ref, struct got_repository *repo)
763 const struct got_error *err = NULL, *unlock_err = NULL;
764 const char *name = got_ref_get_name(ref);
765 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
766 struct got_lockfile *lf = NULL;
771 path_refs = get_refs_dir_path(repo, name);
772 if (path_refs == NULL) {
773 err = got_error_from_errno();
777 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
778 err = got_error_from_errno();
782 err = got_opentemp_named(&tmppath, &f, path);
785 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
787 err = got_path_dirname(&parent, path);
790 err = got_path_mkdir(parent);
794 err = got_opentemp_named(&tmppath, &f, path);
799 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
800 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
801 if (n != strlen(ref->ref.symref.ref) + 6) {
802 err = got_ferror(f, GOT_ERR_IO);
806 char hex[SHA1_DIGEST_STRING_LENGTH];
807 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
808 sizeof(hex)) == NULL) {
809 err = got_error(GOT_ERR_BAD_REF_DATA);
812 n = fprintf(f, "%s\n", hex);
813 if (n != sizeof(hex)) {
814 err = got_ferror(f, GOT_ERR_IO);
819 err = got_lockfile_lock(&lf, path);
823 /* XXX: check if old content matches our expectations? */
825 if (stat(path, &sb) != 0) {
826 if (errno != ENOENT) {
827 err = got_error_from_errno();
830 sb.st_mode = GOT_DEFAULT_FILE_MODE;
833 if (rename(tmppath, path) != 0) {
834 err = got_error_from_errno();
840 if (chmod(path, sb.st_mode) != 0) {
841 err = got_error_from_errno();
846 unlock_err = got_lockfile_unlock(lf);
848 if (fclose(f) != 0 && err == NULL)
849 err = got_error_from_errno();
854 if (unlink(tmppath) != 0 && err == NULL)
855 err = got_error_from_errno();
858 return err ? err : unlock_err;
861 const struct got_error *
862 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
864 const struct got_error *err = NULL, *unlock_err = NULL;
865 const char *name = got_ref_get_name(ref);
866 char *path_refs = NULL, *path = NULL;
867 struct got_lockfile *lf = NULL;
869 /* TODO: handle packed refs ! */
871 path_refs = get_refs_dir_path(repo, name);
872 if (path_refs == NULL) {
873 err = got_error_from_errno();
877 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
878 err = got_error_from_errno();
882 err = got_lockfile_lock(&lf, path);
886 /* XXX: check if old content matches our expectations? */
888 if (unlink(path) != 0)
889 err = got_error_from_errno();
892 unlock_err = got_lockfile_unlock(lf);
896 return err ? err : unlock_err;