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
73 struct got_symref symref;
77 static const struct got_error *
78 parse_symref(struct got_reference **ref, const char *name, const char *line)
80 struct got_symref *symref;
85 return got_error(GOT_ERR_BAD_REF_DATA);
87 symref_name = strdup(name);
88 if (symref_name == NULL)
89 return got_error_from_errno();
90 symref_ref = strdup(line);
91 if (symref_ref == NULL) {
92 const struct got_error *err = got_error_from_errno();
97 *ref = calloc(1, sizeof(**ref));
99 return got_error_from_errno();
100 (*ref)->flags |= GOT_REF_IS_SYMBOLIC;
101 symref = &((*ref)->ref.symref);
102 symref->name = symref_name;
103 symref->ref = symref_ref;
107 static const struct got_error *
108 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
110 struct got_object_id id;
112 if (strncmp(line, "ref: ", 5) == 0) {
114 return parse_symref(ref, name, line);
117 if (!got_parse_sha1_digest(id.sha1, line))
118 return got_error(GOT_ERR_BAD_REF_DATA);
120 return got_ref_alloc(ref, name, &id);
123 static const struct got_error *
124 parse_ref_file(struct got_reference **ref, const char *name,
127 const struct got_error *err = NULL;
128 FILE *f = fopen(abspath, "rb");
131 const char delim[3] = {'\0', '\0', '\0'};
136 line = fparseln(f, &len, NULL, delim, 0);
138 err = got_error(GOT_ERR_BAD_REF_DATA);
142 err = parse_ref_line(ref, name, line);
145 if (fclose(f) != 0 && err == NULL)
146 err = got_error_from_errno();
151 is_well_known_ref(const char *refname)
153 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
154 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
155 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
156 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
160 get_refs_dir_path(struct got_repository *repo, const char *refname)
162 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
163 return strdup(got_repo_get_path_git_dir(repo));
165 return got_repo_get_path_refs(repo);
169 is_valid_ref_name(const char *name)
171 const char *s, *slash, *seg;
172 const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
173 const char *forbidden_seq[] = { "//", "..", "@{" };
174 const char *lfs = GOT_LOCKFILE_SUFFIX;
175 const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
178 if (name[0] == '@' && name[1] == '\0')
181 slash = strchr(name, '/');
187 if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
190 for (i = 0; i < nitems(forbidden); i++) {
191 if (*s == forbidden[i])
194 for (i = 0; i < nitems(forbidden_seq); i++) {
195 if (s[0] == forbidden_seq[i][0] &&
196 s[1] == forbidden_seq[i][1])
199 if (iscntrl((unsigned char)s[0]))
201 if (s[0] == '.' && s[1] == '\0')
204 const char *nextseg = s + 1;
205 if (nextseg[0] == '\0' || nextseg[0] == '.' ||
208 if (seg <= s - lfs_len &&
209 strncmp(s - lfs_len, lfs, lfs_len) == 0)
216 if (seg <= s - lfs_len &&
217 strncmp(s - lfs_len, lfs, lfs_len) == 0)
223 const struct got_error *
224 got_ref_alloc(struct got_reference **ref, const char *name,
225 struct got_object_id *id)
227 const struct got_error *err = NULL;
229 if (!is_valid_ref_name(name))
230 return got_error(GOT_ERR_BAD_REF_NAME);
232 *ref = calloc(1, sizeof(**ref));
234 return got_error_from_errno();
236 memcpy(&(*ref)->ref.ref.sha1, id->sha1, SHA1_DIGEST_LENGTH);
237 (*ref)->ref.ref.name = strdup(name);
238 if ((*ref)->ref.ref.name == NULL) {
239 err = got_error_from_errno();
246 static const struct got_error *
247 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
250 struct got_object_id id;
255 if (line[0] == '#' || line[0] == '^')
258 if (!got_parse_sha1_digest(id.sha1, line))
259 return got_error(GOT_ERR_BAD_REF_DATA);
262 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
266 name = line + SHA1_DIGEST_STRING_LENGTH;
268 return got_ref_alloc(ref, name, &id);
271 static const struct got_error *
272 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
273 int nsubdirs, const char *refname)
275 const struct got_error *err = NULL;
279 const char delim[3] = {'\0', '\0', '\0'};
280 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
285 abs_refname = (char *)refname;
287 line = fparseln(f, &len, NULL, delim, 0);
290 for (i = 0; i < nsubdirs; i++) {
291 if (!ref_is_absolute &&
292 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
294 return got_error_from_errno();
295 err = parse_packed_ref_line(ref, abs_refname, line);
296 if (!ref_is_absolute)
298 if (err || *ref != NULL)
304 } while (*ref == NULL);
309 static const struct got_error *
310 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
313 const struct got_error *err = NULL;
315 char *normpath = NULL;
316 char *absname = NULL;
317 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
318 int ref_is_well_known = is_well_known_ref(name);
322 if (ref_is_absolute || ref_is_well_known) {
323 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
324 return got_error_from_errno();
325 absname = (char *)name;
327 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
328 subdir[0] ? "/" : "", name) == -1)
329 return got_error_from_errno();
331 if (asprintf(&absname, "refs/%s%s%s",
332 subdir, subdir[0] ? "/" : "", name) == -1) {
333 err = got_error_from_errno();
338 normpath = got_path_normalize(path);
339 if (normpath == NULL) {
340 err = got_error_from_errno();
344 err = parse_ref_file(ref, absname, normpath);
346 if (!ref_is_absolute && !ref_is_well_known)
353 const struct got_error *
354 got_ref_open(struct got_reference **ref, struct got_repository *repo,
357 const struct got_error *err = NULL;
358 char *path_refs = NULL;
359 const char *subdirs[] = {
360 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
362 int i, well_known = is_well_known_ref(refname);
366 path_refs = get_refs_dir_path(repo, refname);
367 if (path_refs == NULL) {
368 err = got_error_from_errno();
373 char *packed_refs_path;
376 /* Search on-disk refs before packed refs! */
377 for (i = 0; i < nitems(subdirs); i++) {
378 err = open_ref(ref, path_refs, subdirs[i], refname);
383 packed_refs_path = got_repo_get_path_packed_refs(repo);
384 if (packed_refs_path == NULL) {
385 err = got_error_from_errno();
389 f = fopen(packed_refs_path, "rb");
390 free(packed_refs_path);
392 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
394 if (fclose(f) != 0 && err == NULL)
395 err = got_error_from_errno();
401 err = open_ref(ref, path_refs, "", refname);
406 err = got_error_not_ref(refname);
412 got_ref_close(struct got_reference *ref)
414 if (ref->flags & GOT_REF_IS_SYMBOLIC)
415 free(ref->ref.symref.name);
417 free(ref->ref.ref.name);
421 struct got_reference *
422 got_ref_dup(struct got_reference *ref)
424 struct got_reference *ret;
426 ret = calloc(1, sizeof(*ret));
430 ret->flags = ref->flags;
431 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
432 ret->ref.symref.name = strdup(ref->ref.symref.name);
433 if (ret->ref.symref.name == NULL) {
437 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
438 if (ret->ref.symref.ref == NULL) {
439 free(ret->ref.symref.name);
444 ref->ref.ref.name = strdup(ref->ref.ref.name);
445 if (ref->ref.ref.name == NULL) {
449 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
456 static const struct got_error *
457 resolve_symbolic_ref(struct got_reference **resolved,
458 struct got_repository *repo, struct got_reference *ref)
460 struct got_reference *nextref;
461 const struct got_error *err;
463 err = got_ref_open(&nextref, repo, ref->ref.symref.ref);
467 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
468 err = resolve_symbolic_ref(resolved, repo, nextref);
470 *resolved = got_ref_dup(nextref);
472 got_ref_close(nextref);
476 const struct got_error *
477 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
478 struct got_reference *ref)
480 const struct got_error *err;
482 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
483 struct got_reference *resolved = NULL;
484 err = resolve_symbolic_ref(&resolved, repo, ref);
486 err = got_ref_resolve(id, repo, resolved);
491 *id = calloc(1, sizeof(**id));
493 return got_error_from_errno();
494 memcpy((*id)->sha1, ref->ref.ref.sha1, SHA1_DIGEST_LENGTH);
499 got_ref_to_str(struct got_reference *ref)
503 if (ref->flags & GOT_REF_IS_SYMBOLIC)
504 return strdup(ref->ref.symref.ref);
506 str = malloc(SHA1_DIGEST_STRING_LENGTH);
510 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
511 SHA1_DIGEST_STRING_LENGTH) == NULL) {
520 got_ref_get_name(struct got_reference *ref)
522 if (ref->flags & GOT_REF_IS_SYMBOLIC)
523 return ref->ref.symref.name;
525 return ref->ref.ref.name;
528 static const struct got_error *
529 insert_ref(struct got_reflist_head *refs, struct got_reference *ref,
530 struct got_repository *repo)
532 const struct got_error *err;
533 struct got_object_id *id;
534 struct got_reflist_entry *new, *re, *prev;
537 err = got_ref_resolve(&id, repo, ref);
541 new = malloc(sizeof(*re));
544 return got_error_from_errno();
550 * We must de-duplicate entries on insert because packed-refs may
551 * contain redundant entries. On-disk refs take precedence.
552 * This code assumes that on-disk revs are read before packed-refs.
553 * We're iterating the list anyway, so insert elements sorted by name.
555 re = SIMPLEQ_FIRST(refs);
557 cmp = got_path_cmp(got_ref_get_name(re->ref),
558 got_ref_get_name(ref));
560 free(ref); /* duplicate */
562 } else if (cmp > 0) {
564 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
566 SIMPLEQ_INSERT_HEAD(refs, new, entry);
570 re = SIMPLEQ_NEXT(re, entry);
574 SIMPLEQ_INSERT_TAIL(refs, new, entry);
578 static const struct got_error *
579 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
580 const char *subdir, struct got_repository *repo)
582 const struct got_error *err = NULL;
586 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
587 return got_error_from_errno();
589 d = opendir(path_subdir);
595 struct got_reference *ref;
602 if (strcmp(dent->d_name, ".") == 0 ||
603 strcmp(dent->d_name, "..") == 0)
606 switch (dent->d_type) {
608 err = open_ref(&ref, path_refs, subdir, dent->d_name);
612 err = insert_ref(refs, ref, repo);
618 if (asprintf(&child, "%s%s%s", subdir,
619 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
620 err = got_error_from_errno();
623 err = gather_on_disk_refs(refs, path_refs, child, repo);
637 const struct got_error *
638 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo)
640 const struct got_error *err;
641 char *packed_refs_path, *path_refs = NULL;
643 struct got_reference *ref;
645 /* HEAD ref should always exist. */
646 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
647 if (path_refs == NULL) {
648 err = got_error_from_errno();
651 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD);
654 err = insert_ref(refs, ref, repo);
658 /* Gather on-disk refs before parsing packed-refs. */
660 path_refs = get_refs_dir_path(repo, "");
661 if (path_refs == NULL) {
662 err = got_error_from_errno();
665 err = gather_on_disk_refs(refs, path_refs, "", repo);
670 * The packed-refs file may contain redundant entries, in which
671 * case on-disk refs take precedence.
673 packed_refs_path = got_repo_get_path_packed_refs(repo);
674 if (packed_refs_path == NULL) {
675 err = got_error_from_errno();
679 f = fopen(packed_refs_path, "r");
680 free(packed_refs_path);
684 const char delim[3] = {'\0', '\0', '\0'};
686 line = fparseln(f, &len, NULL, delim, 0);
689 err = parse_packed_ref_line(&ref, NULL, line);
693 err = insert_ref(refs, ref, repo);
701 if (f && fclose(f) != 0 && err == NULL)
702 err = got_error_from_errno();
707 got_ref_list_free(struct got_reflist_head *refs)
709 struct got_reflist_entry *re;
711 while (!SIMPLEQ_EMPTY(refs)) {
712 re = SIMPLEQ_FIRST(refs);
713 SIMPLEQ_REMOVE_HEAD(refs, entry);
714 got_ref_close(re->ref);
721 const struct got_error *
722 got_ref_write(struct got_reference *ref, struct got_repository *repo)
724 const struct got_error *err = NULL, *unlock_err = NULL;
725 const char *name = got_ref_get_name(ref);
726 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
727 struct got_lockfile *lf = NULL;
732 path_refs = get_refs_dir_path(repo, name);
733 if (path_refs == NULL) {
734 err = got_error_from_errno();
738 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
739 err = got_error_from_errno();
743 err = got_opentemp_named(&tmppath, &f, path);
746 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
748 parent = dirname(path);
749 if (parent == NULL) {
750 err = got_error_from_errno();
753 err = got_path_mkdir(parent);
756 err = got_opentemp_named(&tmppath, &f, path);
761 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
762 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
763 if (n != strlen(ref->ref.symref.ref) + 6) {
764 err = got_ferror(f, GOT_ERR_IO);
768 char hex[SHA1_DIGEST_STRING_LENGTH];
769 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
770 sizeof(hex)) == NULL) {
771 err = got_error(GOT_ERR_BAD_REF_DATA);
774 n = fprintf(f, "%s\n", hex);
775 if (n != sizeof(hex)) {
776 err = got_ferror(f, GOT_ERR_IO);
781 err = got_lockfile_lock(&lf, path);
785 /* XXX: check if old content matches our expectations? */
787 if (stat(path, &sb) != 0) {
788 if (errno != ENOENT) {
789 err = got_error_from_errno();
792 sb.st_mode = GOT_DEFAULT_FILE_MODE;
795 if (rename(tmppath, path) != 0) {
796 err = got_error_from_errno();
802 if (chmod(path, sb.st_mode) != 0) {
803 err = got_error_from_errno();
808 unlock_err = got_lockfile_unlock(lf);
810 if (fclose(f) != 0 && err == NULL)
811 err = got_error_from_errno();
816 if (unlink(tmppath) != 0 && err == NULL)
817 err = got_error_from_errno();
820 return err ? err : unlock_err;
823 const struct got_error *
824 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
826 const struct got_error *err = NULL, *unlock_err = NULL;
827 const char *name = got_ref_get_name(ref);
828 char *path_refs = NULL, *path = NULL;
829 struct got_lockfile *lf = NULL;
831 /* TODO: handle packed refs ! */
833 path_refs = get_refs_dir_path(repo, name);
834 if (path_refs == NULL) {
835 err = got_error_from_errno();
839 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
840 err = got_error_from_errno();
844 err = got_lockfile_lock(&lf, path);
848 /* XXX: check if old content matches our expectations? */
850 if (unlink(path) != 0)
851 err = got_error_from_errno();
854 unlock_err = got_lockfile_unlock(lf);
858 return err ? err : unlock_err;