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);
305 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
308 for (i = 0; i < nsubdirs; i++) {
309 if (!ref_is_absolute &&
310 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
312 return got_error_from_errno();
313 err = parse_packed_ref_line(ref, abs_refname, line);
314 if (!ref_is_absolute)
316 if (err || *ref != NULL)
322 } while (*ref == NULL);
327 static const struct got_error *
328 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
331 const struct got_error *err = NULL;
333 char *normpath = NULL;
334 char *absname = NULL;
335 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
336 int ref_is_well_known = is_well_known_ref(name);
340 if (ref_is_absolute || ref_is_well_known) {
341 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
342 return got_error_from_errno();
343 absname = (char *)name;
345 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
346 subdir[0] ? "/" : "", name) == -1)
347 return got_error_from_errno();
349 if (asprintf(&absname, "refs/%s%s%s",
350 subdir, subdir[0] ? "/" : "", name) == -1) {
351 err = got_error_from_errno();
356 normpath = got_path_normalize(path);
357 if (normpath == NULL) {
358 err = got_error_from_errno();
362 err = parse_ref_file(ref, absname, normpath);
364 if (!ref_is_absolute && !ref_is_well_known)
371 const struct got_error *
372 got_ref_open(struct got_reference **ref, struct got_repository *repo,
375 const struct got_error *err = NULL;
376 char *path_refs = NULL;
377 const char *subdirs[] = {
378 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
380 int i, well_known = is_well_known_ref(refname);
384 path_refs = get_refs_dir_path(repo, refname);
385 if (path_refs == NULL) {
386 err = got_error_from_errno();
391 char *packed_refs_path;
394 /* Search on-disk refs before packed refs! */
395 for (i = 0; i < nitems(subdirs); i++) {
396 err = open_ref(ref, path_refs, subdirs[i], refname);
401 packed_refs_path = got_repo_get_path_packed_refs(repo);
402 if (packed_refs_path == NULL) {
403 err = got_error_from_errno();
407 f = fopen(packed_refs_path, "rb");
408 free(packed_refs_path);
410 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
412 if (fclose(f) != 0 && err == NULL)
413 err = got_error_from_errno();
419 err = open_ref(ref, path_refs, "", refname);
424 err = got_error_not_ref(refname);
430 got_ref_close(struct got_reference *ref)
432 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
433 free(ref->ref.symref.name);
434 free(ref->ref.symref.ref);
436 free(ref->ref.ref.name);
440 struct got_reference *
441 got_ref_dup(struct got_reference *ref)
443 struct got_reference *ret;
445 ret = calloc(1, sizeof(*ret));
449 ret->flags = ref->flags;
450 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
451 ret->ref.symref.name = strdup(ref->ref.symref.name);
452 if (ret->ref.symref.name == NULL) {
456 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
457 if (ret->ref.symref.ref == NULL) {
458 free(ret->ref.symref.name);
463 ref->ref.ref.name = strdup(ref->ref.ref.name);
464 if (ref->ref.ref.name == NULL) {
468 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
475 static const struct got_error *
476 resolve_symbolic_ref(struct got_reference **resolved,
477 struct got_repository *repo, struct got_reference *ref)
479 struct got_reference *nextref;
480 const struct got_error *err;
482 err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
486 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
487 err = resolve_symbolic_ref(resolved, repo, nextref);
489 *resolved = got_ref_dup(nextref);
491 got_ref_close(nextref);
495 const struct got_error *
496 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
497 struct got_reference *ref)
499 const struct got_error *err;
501 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
502 struct got_reference *resolved = NULL;
503 err = resolve_symbolic_ref(&resolved, repo, ref);
505 err = got_ref_resolve(id, repo, resolved);
506 got_ref_close(resolved);
510 *id = calloc(1, sizeof(**id));
512 return got_error_from_errno();
513 memcpy((*id)->sha1, ref->ref.ref.sha1, SHA1_DIGEST_LENGTH);
518 got_ref_to_str(struct got_reference *ref)
522 if (ref->flags & GOT_REF_IS_SYMBOLIC)
523 return strdup(ref->ref.symref.ref);
525 str = malloc(SHA1_DIGEST_STRING_LENGTH);
529 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
530 SHA1_DIGEST_STRING_LENGTH) == NULL) {
539 got_ref_get_name(struct got_reference *ref)
541 if (ref->flags & GOT_REF_IS_SYMBOLIC)
542 return ref->ref.symref.name;
544 return ref->ref.ref.name;
547 static const struct got_error *
548 insert_ref(struct got_reflist_head *refs, struct got_reference *ref,
549 struct got_repository *repo)
551 const struct got_error *err;
552 struct got_object_id *id;
553 struct got_reflist_entry *new, *re, *prev;
556 err = got_ref_resolve(&id, repo, ref);
560 new = malloc(sizeof(*re));
563 return got_error_from_errno();
569 * We must de-duplicate entries on insert because packed-refs may
570 * contain redundant entries. On-disk refs take precedence.
571 * This code assumes that on-disk revs are read before packed-refs.
572 * We're iterating the list anyway, so insert elements sorted by name.
574 re = SIMPLEQ_FIRST(refs);
576 cmp = got_path_cmp(got_ref_get_name(re->ref),
577 got_ref_get_name(new->ref));
583 } else if (cmp > 0) {
585 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
587 SIMPLEQ_INSERT_HEAD(refs, new, entry);
591 re = SIMPLEQ_NEXT(re, entry);
595 SIMPLEQ_INSERT_TAIL(refs, new, entry);
599 static const struct got_error *
600 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
601 const char *subdir, struct got_repository *repo)
603 const struct got_error *err = NULL;
607 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
608 return got_error_from_errno();
610 d = opendir(path_subdir);
616 struct got_reference *ref;
623 if (strcmp(dent->d_name, ".") == 0 ||
624 strcmp(dent->d_name, "..") == 0)
627 switch (dent->d_type) {
629 err = open_ref(&ref, path_refs, subdir, dent->d_name);
633 err = insert_ref(refs, ref, repo);
639 if (asprintf(&child, "%s%s%s", subdir,
640 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
641 err = got_error_from_errno();
644 err = gather_on_disk_refs(refs, path_refs, child, repo);
658 const struct got_error *
659 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo)
661 const struct got_error *err;
662 char *packed_refs_path, *path_refs = NULL;
664 struct got_reference *ref;
666 /* HEAD ref should always exist. */
667 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
668 if (path_refs == NULL) {
669 err = got_error_from_errno();
672 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD);
675 err = insert_ref(refs, ref, repo);
679 /* Gather on-disk refs before parsing packed-refs. */
681 path_refs = get_refs_dir_path(repo, "");
682 if (path_refs == NULL) {
683 err = got_error_from_errno();
686 err = gather_on_disk_refs(refs, path_refs, "", repo);
691 * The packed-refs file may contain redundant entries, in which
692 * case on-disk refs take precedence.
694 packed_refs_path = got_repo_get_path_packed_refs(repo);
695 if (packed_refs_path == NULL) {
696 err = got_error_from_errno();
700 f = fopen(packed_refs_path, "r");
701 free(packed_refs_path);
705 const char delim[3] = {'\0', '\0', '\0'};
707 line = fparseln(f, &len, NULL, delim, 0);
711 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
714 err = parse_packed_ref_line(&ref, NULL, line);
719 err = insert_ref(refs, ref, repo);
727 if (f && fclose(f) != 0 && err == NULL)
728 err = got_error_from_errno();
733 got_ref_list_free(struct got_reflist_head *refs)
735 struct got_reflist_entry *re;
737 while (!SIMPLEQ_EMPTY(refs)) {
738 re = SIMPLEQ_FIRST(refs);
739 SIMPLEQ_REMOVE_HEAD(refs, entry);
740 got_ref_close(re->ref);
747 const struct got_error *
748 got_ref_write(struct got_reference *ref, struct got_repository *repo)
750 const struct got_error *err = NULL, *unlock_err = NULL;
751 const char *name = got_ref_get_name(ref);
752 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
753 struct got_lockfile *lf = NULL;
758 path_refs = get_refs_dir_path(repo, name);
759 if (path_refs == NULL) {
760 err = got_error_from_errno();
764 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
765 err = got_error_from_errno();
769 err = got_opentemp_named(&tmppath, &f, path);
772 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
774 err = got_path_dirname(&parent, path);
777 err = got_path_mkdir(parent);
781 err = got_opentemp_named(&tmppath, &f, path);
786 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
787 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
788 if (n != strlen(ref->ref.symref.ref) + 6) {
789 err = got_ferror(f, GOT_ERR_IO);
793 char hex[SHA1_DIGEST_STRING_LENGTH];
794 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
795 sizeof(hex)) == NULL) {
796 err = got_error(GOT_ERR_BAD_REF_DATA);
799 n = fprintf(f, "%s\n", hex);
800 if (n != sizeof(hex)) {
801 err = got_ferror(f, GOT_ERR_IO);
806 err = got_lockfile_lock(&lf, path);
810 /* XXX: check if old content matches our expectations? */
812 if (stat(path, &sb) != 0) {
813 if (errno != ENOENT) {
814 err = got_error_from_errno();
817 sb.st_mode = GOT_DEFAULT_FILE_MODE;
820 if (rename(tmppath, path) != 0) {
821 err = got_error_from_errno();
827 if (chmod(path, sb.st_mode) != 0) {
828 err = got_error_from_errno();
833 unlock_err = got_lockfile_unlock(lf);
835 if (fclose(f) != 0 && err == NULL)
836 err = got_error_from_errno();
841 if (unlink(tmppath) != 0 && err == NULL)
842 err = got_error_from_errno();
845 return err ? err : unlock_err;
848 const struct got_error *
849 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
851 const struct got_error *err = NULL, *unlock_err = NULL;
852 const char *name = got_ref_get_name(ref);
853 char *path_refs = NULL, *path = NULL;
854 struct got_lockfile *lf = NULL;
856 /* TODO: handle packed refs ! */
858 path_refs = get_refs_dir_path(repo, name);
859 if (path_refs == NULL) {
860 err = got_error_from_errno();
864 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
865 err = got_error_from_errno();
869 err = got_lockfile_lock(&lf, path);
873 /* XXX: check if old content matches our expectations? */
875 if (unlink(path) != 0)
876 err = got_error_from_errno();
879 unlock_err = got_lockfile_unlock(lf);
883 return err ? err : unlock_err;