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>
31 #include "got_error.h"
32 #include "got_object.h"
33 #include "got_repository.h"
34 #include "got_reference.h"
35 #include "got_opentemp.h"
37 #include "got_lib_sha1.h"
38 #include "got_lib_path.h"
39 #include "got_lib_delta.h"
40 #include "got_lib_inflate.h"
41 #include "got_lib_object.h"
42 #include "got_lib_lockfile.h"
45 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
48 #define GOT_REF_HEADS "heads"
49 #define GOT_REF_TAGS "tags"
50 #define GOT_REF_REMOTES "remotes"
52 /* A symbolic reference. */
58 /* A non-symbolic reference (there is no better designation). */
61 u_int8_t sha1[SHA1_DIGEST_LENGTH];
64 /* A reference which points to an arbitrary object. */
65 struct got_reference {
67 #define GOT_REF_IS_SYMBOLIC 0x01
71 struct got_symref symref;
75 static const struct got_error *
76 parse_symref(struct got_reference **ref, const char *name, const char *line)
78 struct got_symref *symref;
83 return got_error(GOT_ERR_BAD_REF_DATA);
85 symref_name = strdup(name);
86 if (symref_name == NULL)
87 return got_error_from_errno();
88 symref_ref = strdup(line);
89 if (symref_ref == NULL) {
90 const struct got_error *err = got_error_from_errno();
95 *ref = calloc(1, sizeof(**ref));
97 return got_error_from_errno();
98 (*ref)->flags |= GOT_REF_IS_SYMBOLIC;
99 symref = &((*ref)->ref.symref);
100 symref->name = symref_name;
101 symref->ref = symref_ref;
105 static const struct got_error *
106 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
108 struct got_object_id id;
110 if (strncmp(line, "ref: ", 5) == 0) {
112 return parse_symref(ref, name, line);
115 if (!got_parse_sha1_digest(id.sha1, line))
116 return got_error(GOT_ERR_BAD_REF_DATA);
118 return got_ref_alloc(ref, name, &id);
121 static const struct got_error *
122 parse_ref_file(struct got_reference **ref, const char *name,
125 const struct got_error *err = NULL;
126 FILE *f = fopen(abspath, "rb");
129 const char delim[3] = {'\0', '\0', '\0'};
134 line = fparseln(f, &len, NULL, delim, 0);
136 err = got_error(GOT_ERR_BAD_REF_DATA);
140 err = parse_ref_line(ref, name, line);
143 if (fclose(f) != 0 && err == NULL)
144 err = got_error_from_errno();
149 is_well_known_ref(const char *refname)
151 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
152 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
153 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
154 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
158 get_refs_dir_path(struct got_repository *repo, const char *refname)
160 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
161 return strdup(got_repo_get_path_git_dir(repo));
163 return got_repo_get_path_refs(repo);
167 is_valid_ref_name(const char *name)
169 const char *s, *slash, *seg;
170 const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
171 const char *forbidden_seq[] = { "//", "..", "@{" };
172 const char *lfs = GOT_LOCKFILE_SUFFIX;
173 const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
176 if (name[0] == '@' && name[1] == '\0')
179 slash = strchr(name, '/');
185 if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
188 for (i = 0; i < nitems(forbidden); i++) {
189 if (*s == forbidden[i])
192 for (i = 0; i < nitems(forbidden_seq); i++) {
193 if (s[0] == forbidden_seq[i][0] &&
194 s[1] == forbidden_seq[i][1])
197 if (iscntrl((unsigned char)s[0]))
199 if (s[0] == '.' && s[1] == '\0')
202 const char *nextseg = s + 1;
203 if (nextseg[0] == '\0' || nextseg[0] == '.' ||
206 if (seg <= s - lfs_len &&
207 strncmp(s - lfs_len, lfs, lfs_len) == 0)
214 if (seg <= s - lfs_len &&
215 strncmp(s - lfs_len, lfs, lfs_len) == 0)
221 const struct got_error *
222 got_ref_alloc(struct got_reference **ref, const char *name,
223 struct got_object_id *id)
225 const struct got_error *err = NULL;
227 if (!is_valid_ref_name(name))
228 return got_error(GOT_ERR_BAD_REF_NAME);
230 *ref = calloc(1, sizeof(**ref));
232 return got_error_from_errno();
234 memcpy(&(*ref)->ref.ref.sha1, id->sha1, SHA1_DIGEST_LENGTH);
235 (*ref)->ref.ref.name = strdup(name);
236 if ((*ref)->ref.ref.name == NULL) {
237 err = got_error_from_errno();
244 static const struct got_error *
245 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
248 struct got_object_id id;
253 if (line[0] == '#' || line[0] == '^')
256 if (!got_parse_sha1_digest(id.sha1, line))
257 return got_error(GOT_ERR_BAD_REF_DATA);
260 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
264 name = line + SHA1_DIGEST_STRING_LENGTH;
266 return got_ref_alloc(ref, name, &id);
269 static const struct got_error *
270 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
271 int nsubdirs, const char *refname)
273 const struct got_error *err = NULL;
277 const char delim[3] = {'\0', '\0', '\0'};
278 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
283 abs_refname = (char *)refname;
285 line = fparseln(f, &len, NULL, delim, 0);
288 for (i = 0; i < nsubdirs; i++) {
289 if (!ref_is_absolute &&
290 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
292 return got_error_from_errno();
293 err = parse_packed_ref_line(ref, abs_refname, line);
294 if (!ref_is_absolute)
296 if (err || *ref != NULL)
302 } while (*ref == NULL);
307 static const struct got_error *
308 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
311 const struct got_error *err = NULL;
313 char *normpath = NULL;
314 char *absname = NULL;
315 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
316 int ref_is_well_known = is_well_known_ref(name);
320 if (ref_is_absolute || ref_is_well_known) {
321 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
322 return got_error_from_errno();
323 absname = (char *)name;
325 if (asprintf(&path, "%s/%s/%s", path_refs, subdir, name) == -1)
326 return got_error_from_errno();
328 if (asprintf(&absname, "refs/%s/%s", subdir, name) == -1) {
329 err = got_error_from_errno();
334 normpath = got_path_normalize(path);
335 if (normpath == NULL) {
336 err = got_error_from_errno();
340 err = parse_ref_file(ref, absname, normpath);
342 if (!ref_is_absolute && !ref_is_well_known)
349 const struct got_error *
350 got_ref_open(struct got_reference **ref, struct got_repository *repo,
353 const struct got_error *err = NULL;
354 char *path_refs = NULL;
355 const char *subdirs[] = {
356 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
358 int i, well_known = is_well_known_ref(refname);
362 path_refs = get_refs_dir_path(repo, refname);
363 if (path_refs == NULL) {
364 err = got_error_from_errno();
369 char *packed_refs_path;
372 /* Search on-disk refs before packed refs! */
373 for (i = 0; i < nitems(subdirs); i++) {
374 err = open_ref(ref, path_refs, subdirs[i], refname);
379 packed_refs_path = got_repo_get_path_packed_refs(repo);
380 if (packed_refs_path == NULL) {
381 err = got_error_from_errno();
385 f = fopen(packed_refs_path, "rb");
386 free(packed_refs_path);
388 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
390 if (fclose(f) != 0 && err == NULL)
391 err = got_error_from_errno();
397 err = open_ref(ref, path_refs, "", refname);
402 err = got_error_not_ref(refname);
408 got_ref_close(struct got_reference *ref)
410 if (ref->flags & GOT_REF_IS_SYMBOLIC)
411 free(ref->ref.symref.name);
413 free(ref->ref.ref.name);
417 struct got_reference *
418 got_ref_dup(struct got_reference *ref)
420 struct got_reference *ret;
422 ret = calloc(1, sizeof(*ret));
426 ret->flags = ref->flags;
427 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
428 ret->ref.symref.name = strdup(ref->ref.symref.name);
429 if (ret->ref.symref.name == NULL) {
433 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
434 if (ret->ref.symref.ref == NULL) {
435 free(ret->ref.symref.name);
440 ref->ref.ref.name = strdup(ref->ref.ref.name);
441 if (ref->ref.ref.name == NULL) {
445 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
452 static const struct got_error *
453 resolve_symbolic_ref(struct got_reference **resolved,
454 struct got_repository *repo, struct got_reference *ref)
456 struct got_reference *nextref;
457 const struct got_error *err;
459 err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
463 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
464 err = resolve_symbolic_ref(resolved, repo, nextref);
466 *resolved = got_ref_dup(nextref);
468 got_ref_close(nextref);
472 const struct got_error *
473 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
474 struct got_reference *ref)
476 const struct got_error *err;
478 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
479 struct got_reference *resolved = NULL;
480 err = resolve_symbolic_ref(&resolved, repo, ref);
482 err = got_ref_resolve(id, repo, resolved);
487 *id = calloc(1, sizeof(**id));
489 return got_error_from_errno();
490 memcpy((*id)->sha1, ref->ref.ref.sha1, SHA1_DIGEST_LENGTH);
495 got_ref_to_str(struct got_reference *ref)
499 if (ref->flags & GOT_REF_IS_SYMBOLIC)
500 return strdup(ref->ref.symref.ref);
502 str = malloc(SHA1_DIGEST_STRING_LENGTH);
506 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
507 SHA1_DIGEST_STRING_LENGTH) == NULL) {
516 got_ref_get_name(struct got_reference *ref)
518 if (ref->flags & GOT_REF_IS_SYMBOLIC)
519 return ref->ref.symref.name;
521 return ref->ref.ref.name;
524 static const struct got_error *
525 insert_ref(struct got_reflist_head *refs, struct got_reference *ref,
526 struct got_repository *repo)
528 const struct got_error *err;
529 struct got_object_id *id;
530 struct got_reflist_entry *new, *re, *prev;
533 err = got_ref_resolve(&id, repo, ref);
537 new = malloc(sizeof(*re));
540 return got_error_from_errno();
546 * We must de-duplicate entries on insert because packed-refs may
547 * contain redundant entries. On-disk refs take precedence.
548 * This code assumes that on-disk revs are read before packed-refs.
549 * We're iterating the list anyway, so insert elements sorted by name.
551 re = SIMPLEQ_FIRST(refs);
553 cmp = got_path_cmp(got_ref_get_name(re->ref),
554 got_ref_get_name(ref));
556 free(ref); /* duplicate */
558 } else if (cmp > 0) {
560 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
562 SIMPLEQ_INSERT_HEAD(refs, new, entry);
566 re = SIMPLEQ_NEXT(re, entry);
570 SIMPLEQ_INSERT_TAIL(refs, new, entry);
574 static const struct got_error *
575 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
576 const char *subdir, struct got_repository *repo)
578 const struct got_error *err = NULL;
582 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
583 return got_error_from_errno();
585 d = opendir(path_subdir);
591 struct got_reference *ref;
598 if (strcmp(dent->d_name, ".") == 0 ||
599 strcmp(dent->d_name, "..") == 0)
602 switch (dent->d_type) {
604 err = open_ref(&ref, path_refs, subdir, dent->d_name);
608 err = insert_ref(refs, ref, repo);
614 if (asprintf(&child, "%s%s%s", subdir,
615 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
616 err = got_error_from_errno();
619 err = gather_on_disk_refs(refs, path_refs, child, repo);
633 const struct got_error *
634 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo)
636 const struct got_error *err;
637 char *packed_refs_path, *path_refs = NULL;
639 struct got_reference *ref;
641 /* HEAD ref should always exist. */
642 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
643 if (path_refs == NULL) {
644 err = got_error_from_errno();
647 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD);
650 err = insert_ref(refs, ref, repo);
654 /* Gather on-disk refs before parsing packed-refs. */
656 path_refs = get_refs_dir_path(repo, "");
657 if (path_refs == NULL) {
658 err = got_error_from_errno();
661 err = gather_on_disk_refs(refs, path_refs, "", repo);
666 * The packed-refs file may contain redundant entries, in which
667 * case on-disk refs take precedence.
669 packed_refs_path = got_repo_get_path_packed_refs(repo);
670 if (packed_refs_path == NULL) {
671 err = got_error_from_errno();
675 f = fopen(packed_refs_path, "r");
676 free(packed_refs_path);
680 const char delim[3] = {'\0', '\0', '\0'};
682 line = fparseln(f, &len, NULL, delim, 0);
685 err = parse_packed_ref_line(&ref, NULL, line);
689 err = insert_ref(refs, ref, repo);
697 if (f && fclose(f) != 0 && err == NULL)
698 err = got_error_from_errno();
702 const struct got_error *
703 got_ref_write(struct got_reference *ref, struct got_repository *repo)
705 const struct got_error *err = NULL, *unlock_err = NULL;
706 const char *name = got_ref_get_name(ref);
707 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
708 struct got_lockfile *lf = NULL;
713 path_refs = get_refs_dir_path(repo, name);
714 if (path_refs == NULL) {
715 err = got_error_from_errno();
719 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
720 err = got_error_from_errno();
724 err = got_opentemp_named(&tmppath, &f, path);
726 err = got_error_from_errno();
730 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
731 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
732 if (n != strlen(ref->ref.symref.ref) + 6) {
733 err = got_ferror(f, GOT_ERR_IO);
737 char hex[SHA1_DIGEST_STRING_LENGTH];
738 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
739 sizeof(hex)) == NULL) {
740 err = got_error(GOT_ERR_BAD_REF_DATA);
743 n = fprintf(f, "%s\n", hex);
744 if (n != sizeof(hex) + 1) {
745 err = got_ferror(f, GOT_ERR_IO);
750 err = got_lockfile_lock(&lf, path);
754 /* XXX: check if old content matches our expectations? */
756 if (stat(path, &sb) != 0) {
757 err = got_error_from_errno();
761 if (rename(tmppath, path) != 0) {
762 err = got_error_from_errno();
768 if (chmod(path, sb.st_mode) != 0) {
769 err = got_error_from_errno();
774 unlock_err = got_lockfile_unlock(lf);
776 if (fclose(f) != 0 && err == NULL)
777 err = got_error_from_errno();
782 if (unlink(tmppath) != 0 && err == NULL)
783 err = got_error_from_errno();
786 return err ? err : unlock_err;