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>
32 #include "got_error.h"
33 #include "got_object.h"
34 #include "got_repository.h"
35 #include "got_reference.h"
36 #include "got_opentemp.h"
38 #include "got_lib_sha1.h"
39 #include "got_lib_path.h"
40 #include "got_lib_delta.h"
41 #include "got_lib_inflate.h"
42 #include "got_lib_object.h"
43 #include "got_lib_lockfile.h"
46 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
49 #define GOT_REF_HEADS "heads"
50 #define GOT_REF_TAGS "tags"
51 #define GOT_REF_REMOTES "remotes"
53 /* A symbolic reference. */
59 /* A non-symbolic reference (there is no better designation). */
62 u_int8_t sha1[SHA1_DIGEST_LENGTH];
65 /* A reference which points to an arbitrary object. */
66 struct got_reference {
68 #define GOT_REF_IS_SYMBOLIC 0x01
72 struct got_symref symref;
76 static const struct got_error *
77 parse_symref(struct got_reference **ref, const char *name, const char *line)
79 struct got_symref *symref;
84 return got_error(GOT_ERR_BAD_REF_DATA);
86 symref_name = strdup(name);
87 if (symref_name == NULL)
88 return got_error_from_errno();
89 symref_ref = strdup(line);
90 if (symref_ref == NULL) {
91 const struct got_error *err = got_error_from_errno();
96 *ref = calloc(1, sizeof(**ref));
98 return got_error_from_errno();
99 (*ref)->flags |= GOT_REF_IS_SYMBOLIC;
100 symref = &((*ref)->ref.symref);
101 symref->name = symref_name;
102 symref->ref = symref_ref;
106 static const struct got_error *
107 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
109 struct got_object_id id;
111 if (strncmp(line, "ref: ", 5) == 0) {
113 return parse_symref(ref, name, line);
116 if (!got_parse_sha1_digest(id.sha1, line))
117 return got_error(GOT_ERR_BAD_REF_DATA);
119 return got_ref_alloc(ref, name, &id);
122 static const struct got_error *
123 parse_ref_file(struct got_reference **ref, const char *name,
126 const struct got_error *err = NULL;
127 FILE *f = fopen(abspath, "rb");
130 const char delim[3] = {'\0', '\0', '\0'};
135 line = fparseln(f, &len, NULL, delim, 0);
137 err = got_error(GOT_ERR_BAD_REF_DATA);
141 err = parse_ref_line(ref, name, line);
144 if (fclose(f) != 0 && err == NULL)
145 err = got_error_from_errno();
150 is_well_known_ref(const char *refname)
152 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
153 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
154 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
155 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
159 get_refs_dir_path(struct got_repository *repo, const char *refname)
161 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
162 return strdup(got_repo_get_path_git_dir(repo));
164 return got_repo_get_path_refs(repo);
168 is_valid_ref_name(const char *name)
170 const char *s, *slash, *seg;
171 const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
172 const char *forbidden_seq[] = { "//", "..", "@{" };
173 const char *lfs = GOT_LOCKFILE_SUFFIX;
174 const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
177 if (name[0] == '@' && name[1] == '\0')
180 slash = strchr(name, '/');
186 if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
189 for (i = 0; i < nitems(forbidden); i++) {
190 if (*s == forbidden[i])
193 for (i = 0; i < nitems(forbidden_seq); i++) {
194 if (s[0] == forbidden_seq[i][0] &&
195 s[1] == forbidden_seq[i][1])
198 if (iscntrl((unsigned char)s[0]))
200 if (s[0] == '.' && s[1] == '\0')
203 const char *nextseg = s + 1;
204 if (nextseg[0] == '\0' || nextseg[0] == '.' ||
207 if (seg <= s - lfs_len &&
208 strncmp(s - lfs_len, lfs, lfs_len) == 0)
215 if (seg <= s - lfs_len &&
216 strncmp(s - lfs_len, lfs, lfs_len) == 0)
222 const struct got_error *
223 got_ref_alloc(struct got_reference **ref, const char *name,
224 struct got_object_id *id)
226 const struct got_error *err = NULL;
228 if (!is_valid_ref_name(name))
229 return got_error(GOT_ERR_BAD_REF_NAME);
231 *ref = calloc(1, sizeof(**ref));
233 return got_error_from_errno();
235 memcpy(&(*ref)->ref.ref.sha1, id->sha1, SHA1_DIGEST_LENGTH);
236 (*ref)->ref.ref.name = strdup(name);
237 if ((*ref)->ref.ref.name == NULL) {
238 err = got_error_from_errno();
245 static const struct got_error *
246 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
249 struct got_object_id id;
254 if (line[0] == '#' || line[0] == '^')
257 if (!got_parse_sha1_digest(id.sha1, line))
258 return got_error(GOT_ERR_BAD_REF_DATA);
261 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
265 name = line + SHA1_DIGEST_STRING_LENGTH;
267 return got_ref_alloc(ref, name, &id);
270 static const struct got_error *
271 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
272 int nsubdirs, const char *refname)
274 const struct got_error *err = NULL;
278 const char delim[3] = {'\0', '\0', '\0'};
279 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
284 abs_refname = (char *)refname;
286 line = fparseln(f, &len, NULL, delim, 0);
289 for (i = 0; i < nsubdirs; i++) {
290 if (!ref_is_absolute &&
291 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
293 return got_error_from_errno();
294 err = parse_packed_ref_line(ref, abs_refname, line);
295 if (!ref_is_absolute)
297 if (err || *ref != NULL)
303 } while (*ref == NULL);
308 static const struct got_error *
309 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
312 const struct got_error *err = NULL;
314 char *normpath = NULL;
315 char *absname = NULL;
316 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
317 int ref_is_well_known = is_well_known_ref(name);
321 if (ref_is_absolute || ref_is_well_known) {
322 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
323 return got_error_from_errno();
324 absname = (char *)name;
326 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
327 subdir[0] ? "/" : "", name) == -1)
328 return got_error_from_errno();
330 if (asprintf(&absname, "refs/%s%s%s",
331 subdir, subdir[0] ? "/" : "", name) == -1) {
332 err = got_error_from_errno();
337 normpath = got_path_normalize(path);
338 if (normpath == NULL) {
339 err = got_error_from_errno();
343 err = parse_ref_file(ref, absname, normpath);
345 if (!ref_is_absolute && !ref_is_well_known)
352 const struct got_error *
353 got_ref_open(struct got_reference **ref, struct got_repository *repo,
356 const struct got_error *err = NULL;
357 char *path_refs = NULL;
358 const char *subdirs[] = {
359 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
361 int i, well_known = is_well_known_ref(refname);
365 path_refs = get_refs_dir_path(repo, refname);
366 if (path_refs == NULL) {
367 err = got_error_from_errno();
372 char *packed_refs_path;
375 /* Search on-disk refs before packed refs! */
376 for (i = 0; i < nitems(subdirs); i++) {
377 err = open_ref(ref, path_refs, subdirs[i], refname);
382 packed_refs_path = got_repo_get_path_packed_refs(repo);
383 if (packed_refs_path == NULL) {
384 err = got_error_from_errno();
388 f = fopen(packed_refs_path, "rb");
389 free(packed_refs_path);
391 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
393 if (fclose(f) != 0 && err == NULL)
394 err = got_error_from_errno();
400 err = open_ref(ref, path_refs, "", refname);
405 err = got_error_not_ref(refname);
411 got_ref_close(struct got_reference *ref)
413 if (ref->flags & GOT_REF_IS_SYMBOLIC)
414 free(ref->ref.symref.name);
416 free(ref->ref.ref.name);
420 struct got_reference *
421 got_ref_dup(struct got_reference *ref)
423 struct got_reference *ret;
425 ret = calloc(1, sizeof(*ret));
429 ret->flags = ref->flags;
430 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
431 ret->ref.symref.name = strdup(ref->ref.symref.name);
432 if (ret->ref.symref.name == NULL) {
436 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
437 if (ret->ref.symref.ref == NULL) {
438 free(ret->ref.symref.name);
443 ref->ref.ref.name = strdup(ref->ref.ref.name);
444 if (ref->ref.ref.name == NULL) {
448 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
455 static const struct got_error *
456 resolve_symbolic_ref(struct got_reference **resolved,
457 struct got_repository *repo, struct got_reference *ref)
459 struct got_reference *nextref;
460 const struct got_error *err;
462 err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
466 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
467 err = resolve_symbolic_ref(resolved, repo, nextref);
469 *resolved = got_ref_dup(nextref);
471 got_ref_close(nextref);
475 const struct got_error *
476 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
477 struct got_reference *ref)
479 const struct got_error *err;
481 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
482 struct got_reference *resolved = NULL;
483 err = resolve_symbolic_ref(&resolved, repo, ref);
485 err = got_ref_resolve(id, repo, resolved);
490 *id = calloc(1, sizeof(**id));
492 return got_error_from_errno();
493 memcpy((*id)->sha1, ref->ref.ref.sha1, SHA1_DIGEST_LENGTH);
498 got_ref_to_str(struct got_reference *ref)
502 if (ref->flags & GOT_REF_IS_SYMBOLIC)
503 return strdup(ref->ref.symref.ref);
505 str = malloc(SHA1_DIGEST_STRING_LENGTH);
509 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
510 SHA1_DIGEST_STRING_LENGTH) == NULL) {
519 got_ref_get_name(struct got_reference *ref)
521 if (ref->flags & GOT_REF_IS_SYMBOLIC)
522 return ref->ref.symref.name;
524 return ref->ref.ref.name;
527 static const struct got_error *
528 insert_ref(struct got_reflist_head *refs, struct got_reference *ref,
529 struct got_repository *repo)
531 const struct got_error *err;
532 struct got_object_id *id;
533 struct got_reflist_entry *new, *re, *prev;
536 err = got_ref_resolve(&id, repo, ref);
540 new = malloc(sizeof(*re));
543 return got_error_from_errno();
549 * We must de-duplicate entries on insert because packed-refs may
550 * contain redundant entries. On-disk refs take precedence.
551 * This code assumes that on-disk revs are read before packed-refs.
552 * We're iterating the list anyway, so insert elements sorted by name.
554 re = SIMPLEQ_FIRST(refs);
556 cmp = got_path_cmp(got_ref_get_name(re->ref),
557 got_ref_get_name(ref));
559 free(ref); /* duplicate */
561 } else if (cmp > 0) {
563 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
565 SIMPLEQ_INSERT_HEAD(refs, new, entry);
569 re = SIMPLEQ_NEXT(re, entry);
573 SIMPLEQ_INSERT_TAIL(refs, new, entry);
577 static const struct got_error *
578 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
579 const char *subdir, struct got_repository *repo)
581 const struct got_error *err = NULL;
585 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
586 return got_error_from_errno();
588 d = opendir(path_subdir);
594 struct got_reference *ref;
601 if (strcmp(dent->d_name, ".") == 0 ||
602 strcmp(dent->d_name, "..") == 0)
605 switch (dent->d_type) {
607 err = open_ref(&ref, path_refs, subdir, dent->d_name);
611 err = insert_ref(refs, ref, repo);
617 if (asprintf(&child, "%s%s%s", subdir,
618 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
619 err = got_error_from_errno();
622 err = gather_on_disk_refs(refs, path_refs, child, repo);
636 const struct got_error *
637 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo)
639 const struct got_error *err;
640 char *packed_refs_path, *path_refs = NULL;
642 struct got_reference *ref;
644 /* HEAD ref should always exist. */
645 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
646 if (path_refs == NULL) {
647 err = got_error_from_errno();
650 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD);
653 err = insert_ref(refs, ref, repo);
657 /* Gather on-disk refs before parsing packed-refs. */
659 path_refs = get_refs_dir_path(repo, "");
660 if (path_refs == NULL) {
661 err = got_error_from_errno();
664 err = gather_on_disk_refs(refs, path_refs, "", repo);
669 * The packed-refs file may contain redundant entries, in which
670 * case on-disk refs take precedence.
672 packed_refs_path = got_repo_get_path_packed_refs(repo);
673 if (packed_refs_path == NULL) {
674 err = got_error_from_errno();
678 f = fopen(packed_refs_path, "r");
679 free(packed_refs_path);
683 const char delim[3] = {'\0', '\0', '\0'};
685 line = fparseln(f, &len, NULL, delim, 0);
688 err = parse_packed_ref_line(&ref, NULL, line);
692 err = insert_ref(refs, ref, repo);
700 if (f && fclose(f) != 0 && err == NULL)
701 err = got_error_from_errno();
705 const struct got_error *
706 got_ref_write(struct got_reference *ref, struct got_repository *repo)
708 const struct got_error *err = NULL, *unlock_err = NULL;
709 const char *name = got_ref_get_name(ref);
710 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
711 struct got_lockfile *lf = NULL;
716 path_refs = get_refs_dir_path(repo, name);
717 if (path_refs == NULL) {
718 err = got_error_from_errno();
722 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
723 err = got_error_from_errno();
727 err = got_opentemp_named(&tmppath, &f, path);
729 err = got_error_from_errno();
733 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
734 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
735 if (n != strlen(ref->ref.symref.ref) + 6) {
736 err = got_ferror(f, GOT_ERR_IO);
740 char hex[SHA1_DIGEST_STRING_LENGTH];
741 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
742 sizeof(hex)) == NULL) {
743 err = got_error(GOT_ERR_BAD_REF_DATA);
746 n = fprintf(f, "%s\n", hex);
747 if (n != sizeof(hex)) {
748 err = got_ferror(f, GOT_ERR_IO);
753 err = got_lockfile_lock(&lf, path);
757 /* XXX: check if old content matches our expectations? */
759 if (stat(path, &sb) != 0 && errno != ENOENT) {
760 err = got_error_from_errno();
764 if (rename(tmppath, path) != 0) {
765 err = got_error_from_errno();
771 if (chmod(path, sb.st_mode) != 0) {
772 err = got_error_from_errno();
777 unlock_err = got_lockfile_unlock(lf);
779 if (fclose(f) != 0 && err == NULL)
780 err = got_error_from_errno();
785 if (unlink(tmppath) != 0 && err == NULL)
786 err = got_error_from_errno();
789 return err ? err : unlock_err;
792 const struct got_error *
793 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
795 const struct got_error *err = NULL, *unlock_err = NULL;
796 const char *name = got_ref_get_name(ref);
797 char *path_refs = NULL, *path = NULL;
798 struct got_lockfile *lf = NULL;
800 /* TODO: handle packed refs ! */
802 path_refs = get_refs_dir_path(repo, name);
803 if (path_refs == NULL) {
804 err = got_error_from_errno();
808 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
809 err = got_error_from_errno();
813 err = got_lockfile_lock(&lf, path);
817 /* XXX: check if old content matches our expectations? */
819 if (unlink(path) != 0)
820 err = got_error_from_errno();
823 unlock_err = got_lockfile_unlock(lf);
827 return err ? err : unlock_err;