Blob


1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/stat.h>
21 #include <errno.h>
22 #include <ctype.h>
23 #include <dirent.h>
24 #include <limits.h>
25 #include <sha1.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <util.h>
30 #include <zlib.h>
31 #include <time.h>
32 #include <libgen.h>
34 #include "got_error.h"
35 #include "got_object.h"
36 #include "got_repository.h"
37 #include "got_reference.h"
38 #include "got_opentemp.h"
39 #include "got_path.h"
41 #include "got_lib_sha1.h"
42 #include "got_lib_delta.h"
43 #include "got_lib_inflate.h"
44 #include "got_lib_object.h"
45 #include "got_lib_lockfile.h"
47 #ifndef nitems
48 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
49 #endif
51 #define GOT_REF_HEADS "heads"
52 #define GOT_REF_TAGS "tags"
53 #define GOT_REF_REMOTES "remotes"
55 /*
56 * We do not resolve tags yet, and don't yet care about sorting refs either,
57 * so packed-refs files we write contain a minimal header which disables all
58 * packed-refs "traits" supported by Git.
59 */
60 #define GOT_PACKED_REFS_HEADER "# pack-refs with:"
62 /* A symbolic reference. */
63 struct got_symref {
64 char *name;
65 char *ref;
66 };
68 #define GOT_REF_RECURSE_MAX 20
70 /* A non-symbolic reference (there is no better designation). */
71 struct got_ref {
72 char *name;
73 u_int8_t sha1[SHA1_DIGEST_LENGTH];
74 };
76 /* A reference which points to an arbitrary object. */
77 struct got_reference {
78 unsigned int flags;
79 #define GOT_REF_IS_SYMBOLIC 0x01
80 #define GOT_REF_IS_PACKED 0x02
82 union {
83 struct got_ref ref;
84 struct got_symref symref;
85 } ref;
87 struct got_lockfile *lf;
88 };
90 static const struct got_error *
91 alloc_ref(struct got_reference **ref, const char *name,
92 struct got_object_id *id, int flags)
93 {
94 const struct got_error *err = NULL;
96 *ref = calloc(1, sizeof(**ref));
97 if (*ref == NULL)
98 return got_error_from_errno("calloc");
100 memcpy((*ref)->ref.ref.sha1, id->sha1, sizeof((*ref)->ref.ref.sha1));
101 (*ref)->flags = flags;
102 (*ref)->ref.ref.name = strdup(name);
103 if ((*ref)->ref.ref.name == NULL) {
104 err = got_error_from_errno("strdup");
105 got_ref_close(*ref);
106 *ref = NULL;
108 return err;
111 static const struct got_error *
112 alloc_symref(struct got_reference **ref, const char *name,
113 const char *target_ref, int flags)
115 const struct got_error *err = NULL;
117 *ref = calloc(1, sizeof(**ref));
118 if (*ref == NULL)
119 return got_error_from_errno("calloc");
121 (*ref)->flags = GOT_REF_IS_SYMBOLIC | flags;
122 (*ref)->ref.symref.name = strdup(name);
123 if ((*ref)->ref.symref.name == NULL) {
124 err = got_error_from_errno("strdup");
125 got_ref_close(*ref);
126 *ref = NULL;
127 return err;
129 (*ref)->ref.symref.ref = strdup(target_ref);
130 if ((*ref)->ref.symref.ref == NULL) {
131 err = got_error_from_errno("strdup");
132 got_ref_close(*ref);
133 *ref = NULL;
135 return err;
138 static const struct got_error *
139 parse_symref(struct got_reference **ref, const char *name, const char *line)
141 if (line[0] == '\0')
142 return got_error(GOT_ERR_BAD_REF_DATA);
144 return alloc_symref(ref, name, line, 0);
147 static const struct got_error *
148 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
150 struct got_object_id id;
152 if (strncmp(line, "ref: ", 5) == 0) {
153 line += 5;
154 return parse_symref(ref, name, line);
157 if (!got_parse_sha1_digest(id.sha1, line))
158 return got_error(GOT_ERR_BAD_REF_DATA);
160 return alloc_ref(ref, name, &id, 0);
163 static const struct got_error *
164 parse_ref_file(struct got_reference **ref, const char *name,
165 const char *absname, const char *abspath, int lock)
167 const struct got_error *err = NULL;
168 FILE *f;
169 char *line = NULL;
170 size_t linesize = 0;
171 ssize_t linelen;
172 struct got_lockfile *lf = NULL;
174 if (lock) {
175 err = got_lockfile_lock(&lf, abspath);
176 if (err) {
177 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
178 err = got_error_not_ref(name);
179 return err;
183 f = fopen(abspath, "rb");
184 if (f == NULL) {
185 if (errno != ENOTDIR && errno != ENOENT)
186 err = got_error_from_errno2("fopen", abspath);
187 else
188 err = got_error_not_ref(name);
189 if (lock)
190 got_lockfile_unlock(lf);
191 return err;
194 linelen = getline(&line, &linesize, f);
195 if (linelen == -1) {
196 if (feof(f))
197 err = NULL; /* ignore empty files (could be locks) */
198 else {
199 if (errno == EISDIR)
200 err = got_error(GOT_ERR_NOT_REF);
201 else if (ferror(f))
202 err = got_ferror(f, GOT_ERR_IO);
203 else
204 err = got_error_from_errno2("getline", abspath);
206 if (lock)
207 got_lockfile_unlock(lf);
208 goto done;
210 while (linelen > 0 && line[linelen - 1] == '\n') {
211 line[linelen - 1] = '\0';
212 linelen--;
215 err = parse_ref_line(ref, absname, line);
216 if (lock) {
217 if (err)
218 got_lockfile_unlock(lf);
219 else {
220 if (*ref)
221 (*ref)->lf = lf;
222 else
223 got_lockfile_unlock(lf);
226 done:
227 free(line);
228 if (fclose(f) != 0 && err == NULL) {
229 err = got_error_from_errno("fclose");
230 if (*ref) {
231 if (lock)
232 got_ref_unlock(*ref);
233 got_ref_close(*ref);
234 *ref = NULL;
237 return err;
240 static int
241 is_well_known_ref(const char *refname)
243 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
244 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
245 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
246 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
249 static char *
250 get_refs_dir_path(struct got_repository *repo, const char *refname)
252 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
253 return strdup(got_repo_get_path_git_dir(repo));
255 return got_repo_get_path_refs(repo);
258 static int
259 is_valid_ref_name(const char *name)
261 const char *s, *seg;
262 const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
263 const char *forbidden_seq[] = { "//", "..", "@{" };
264 const char *lfs = GOT_LOCKFILE_SUFFIX;
265 const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
266 int i;
268 if (name[0] == '@' && name[1] == '\0')
269 return 0;
271 s = name;
272 seg = s;
273 if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
274 return 0;
275 while (*s) {
276 for (i = 0; i < nitems(forbidden); i++) {
277 if (*s == forbidden[i])
278 return 0;
280 for (i = 0; i < nitems(forbidden_seq); i++) {
281 if (s[0] == forbidden_seq[i][0] &&
282 s[1] == forbidden_seq[i][1])
283 return 0;
285 if (iscntrl((unsigned char)s[0]))
286 return 0;
287 if (s[0] == '.' && s[1] == '\0')
288 return 0;
289 if (*s == '/') {
290 const char *nextseg = s + 1;
291 if (nextseg[0] == '\0' || nextseg[0] == '.' ||
292 nextseg[0] == '/')
293 return 0;
294 if (seg <= s - lfs_len &&
295 strncmp(s - lfs_len, lfs, lfs_len) == 0)
296 return 0;
297 seg = nextseg;
299 s++;
302 if (seg <= s - lfs_len &&
303 strncmp(s - lfs_len, lfs, lfs_len) == 0)
304 return 0;
306 return 1;
309 const struct got_error *
310 got_ref_alloc(struct got_reference **ref, const char *name,
311 struct got_object_id *id)
313 if (!is_valid_ref_name(name))
314 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
316 return alloc_ref(ref, name, id, 0);
319 const struct got_error *
320 got_ref_alloc_symref(struct got_reference **ref, const char *name,
321 struct got_reference *target_ref)
323 if (!is_valid_ref_name(name))
324 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
326 return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
329 static const struct got_error *
330 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
331 const char *line)
333 struct got_object_id id;
334 const char *name;
336 *ref = NULL;
338 if (line[0] == '#' || line[0] == '^')
339 return NULL;
341 if (!got_parse_sha1_digest(id.sha1, line))
342 return got_error(GOT_ERR_BAD_REF_DATA);
344 if (abs_refname) {
345 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
346 return NULL;
347 name = abs_refname;
348 } else
349 name = line + SHA1_DIGEST_STRING_LENGTH;
351 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED);
354 static const struct got_error *
355 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
356 int nsubdirs, const char *refname)
358 const struct got_error *err = NULL;
359 char *abs_refname;
360 char *line;
361 size_t len;
362 const char delim[3] = {'\0', '\0', '\0'};
363 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
365 *ref = NULL;
367 if (ref_is_absolute)
368 abs_refname = (char *)refname;
369 do {
370 line = fparseln(f, &len, NULL, delim, 0);
371 if (line == NULL) {
372 if (feof(f))
373 break;
374 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
375 break;
377 for (i = 0; i < nsubdirs; i++) {
378 if (!ref_is_absolute &&
379 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
380 refname) == -1)
381 return got_error_from_errno("asprintf");
382 err = parse_packed_ref_line(ref, abs_refname, line);
383 if (!ref_is_absolute)
384 free(abs_refname);
385 if (err || *ref != NULL)
386 break;
388 free(line);
389 if (err)
390 break;
391 } while (*ref == NULL);
393 return err;
396 static const struct got_error *
397 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
398 const char *name, int lock)
400 const struct got_error *err = NULL;
401 char *path = NULL;
402 char *absname = NULL;
403 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
404 int ref_is_well_known = is_well_known_ref(name);
406 *ref = NULL;
408 if (ref_is_absolute || ref_is_well_known) {
409 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
410 return got_error_from_errno("asprintf");
411 absname = (char *)name;
412 } else {
413 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
414 subdir[0] ? "/" : "", name) == -1)
415 return got_error_from_errno("asprintf");
417 if (asprintf(&absname, "refs/%s%s%s",
418 subdir, subdir[0] ? "/" : "", name) == -1) {
419 err = got_error_from_errno("asprintf");
420 goto done;
424 err = parse_ref_file(ref, name, absname, path, lock);
425 done:
426 if (!ref_is_absolute && !ref_is_well_known)
427 free(absname);
428 free(path);
429 return err;
432 const struct got_error *
433 got_ref_open(struct got_reference **ref, struct got_repository *repo,
434 const char *refname, int lock)
436 const struct got_error *err = NULL;
437 char *path_refs = NULL;
438 const char *subdirs[] = {
439 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
440 };
441 int i, well_known = is_well_known_ref(refname);
442 struct got_lockfile *lf = NULL;
444 *ref = NULL;
446 path_refs = get_refs_dir_path(repo, refname);
447 if (path_refs == NULL) {
448 err = got_error_from_errno2("get_refs_dir_path", refname);
449 goto done;
452 if (well_known) {
453 err = open_ref(ref, path_refs, "", refname, lock);
454 } else {
455 char *packed_refs_path;
456 FILE *f;
458 /* Search on-disk refs before packed refs! */
459 for (i = 0; i < nitems(subdirs); i++) {
460 err = open_ref(ref, path_refs, subdirs[i], refname,
461 lock);
462 if ((err && err->code != GOT_ERR_NOT_REF) || *ref)
463 goto done;
466 packed_refs_path = got_repo_get_path_packed_refs(repo);
467 if (packed_refs_path == NULL) {
468 err = got_error_from_errno(
469 "got_repo_get_path_packed_refs");
470 goto done;
473 if (lock) {
474 err = got_lockfile_lock(&lf, packed_refs_path);
475 if (err)
476 goto done;
478 f = fopen(packed_refs_path, "rb");
479 free(packed_refs_path);
480 if (f != NULL) {
481 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
482 refname);
483 if (!err) {
484 if (fclose(f) != 0) {
485 err = got_error_from_errno("fclose");
486 got_ref_close(*ref);
487 *ref = NULL;
488 } else if (*ref)
489 (*ref)->lf = lf;
493 done:
494 if (!err && *ref == NULL)
495 err = got_error_not_ref(refname);
496 if (err && lf)
497 got_lockfile_unlock(lf);
498 free(path_refs);
499 return err;
502 void
503 got_ref_close(struct got_reference *ref)
505 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
506 free(ref->ref.symref.name);
507 free(ref->ref.symref.ref);
508 } else
509 free(ref->ref.ref.name);
510 free(ref);
513 struct got_reference *
514 got_ref_dup(struct got_reference *ref)
516 struct got_reference *ret;
518 ret = calloc(1, sizeof(*ret));
519 if (ret == NULL)
520 return NULL;
522 ret->flags = ref->flags;
523 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
524 ret->ref.symref.name = strdup(ref->ref.symref.name);
525 if (ret->ref.symref.name == NULL) {
526 free(ret);
527 return NULL;
529 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
530 if (ret->ref.symref.ref == NULL) {
531 free(ret->ref.symref.name);
532 free(ret);
533 return NULL;
535 } else {
536 ref->ref.ref.name = strdup(ref->ref.ref.name);
537 if (ref->ref.ref.name == NULL) {
538 free(ret);
539 return NULL;
541 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
542 sizeof(ret->ref.ref.sha1));
545 return ret;
548 const struct got_error *
549 got_reflist_entry_dup(struct got_reflist_entry **newp,
550 struct got_reflist_entry *re)
552 const struct got_error *err = NULL;
553 struct got_reflist_entry *new;
555 *newp = NULL;
557 new = malloc(sizeof(*new));
558 if (new == NULL)
559 return got_error_from_errno("malloc");
561 new->ref = got_ref_dup(re->ref);
562 if (new->ref == NULL) {
563 err = got_error_from_errno("got_ref_dup");
564 free(new);
565 return err;
568 new->id = got_object_id_dup(re->id);
569 if (new->id == NULL) {
570 err = got_error_from_errno("got_ref_dup");
571 free(new->id);
572 free(new);
573 return err;
576 *newp = new;
577 return NULL;
580 static const struct got_error *
581 resolve_symbolic_ref(struct got_reference **resolved,
582 struct got_repository *repo, struct got_reference *ref)
584 struct got_reference *nextref;
585 const struct got_error *err;
587 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
588 if (err)
589 return err;
591 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
592 err = resolve_symbolic_ref(resolved, repo, nextref);
593 else
594 *resolved = got_ref_dup(nextref);
596 got_ref_close(nextref);
597 return err;
600 static const struct got_error *
601 ref_resolve(struct got_object_id **id, struct got_repository *repo,
602 struct got_reference *ref, int recursion)
604 const struct got_error *err;
606 if (recursion <= 0)
607 return got_error_msg(GOT_ERR_RECURSION,
608 "reference recursion limit reached");
610 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
611 struct got_reference *resolved = NULL;
612 err = resolve_symbolic_ref(&resolved, repo, ref);
613 if (err == NULL)
614 err = ref_resolve(id, repo, resolved, --recursion);
615 if (resolved)
616 got_ref_close(resolved);
617 return err;
620 *id = calloc(1, sizeof(**id));
621 if (*id == NULL)
622 return got_error_from_errno("calloc");
623 memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
624 return NULL;
627 const struct got_error *
628 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
629 struct got_reference *ref)
631 return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
634 char *
635 got_ref_to_str(struct got_reference *ref)
637 char *str;
639 if (ref->flags & GOT_REF_IS_SYMBOLIC)
640 return strdup(ref->ref.symref.ref);
642 str = malloc(SHA1_DIGEST_STRING_LENGTH);
643 if (str == NULL)
644 return NULL;
646 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
647 SHA1_DIGEST_STRING_LENGTH) == NULL) {
648 free(str);
649 return NULL;
652 return str;
655 const char *
656 got_ref_get_name(struct got_reference *ref)
658 if (ref->flags & GOT_REF_IS_SYMBOLIC)
659 return ref->ref.symref.name;
661 return ref->ref.ref.name;
664 const char *
665 got_ref_get_symref_target(struct got_reference *ref)
667 if (ref->flags & GOT_REF_IS_SYMBOLIC)
668 return ref->ref.symref.ref;
670 return NULL;
673 const struct got_error *
674 got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
675 struct got_reference* re2)
677 const char *name1 = got_ref_get_name(re1);
678 const char *name2 = got_ref_get_name(re2);
680 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
681 return NULL;
684 const struct got_error *
685 got_ref_cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
686 struct got_reference *ref2)
688 const struct got_error *err = NULL;
689 struct got_repository *repo = arg;
690 struct got_object_id *id1, *id2 = NULL;
691 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
692 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
693 time_t time1, time2;
695 *cmp = 0;
697 err = got_ref_resolve(&id1, repo, ref1);
698 if (err)
699 return err;
700 err = got_object_open_as_tag(&tag1, repo, id1);
701 if (err) {
702 if (err->code != GOT_ERR_OBJ_TYPE)
703 goto done;
704 /* "lightweight" tag */
705 err = got_object_open_as_commit(&commit1, repo, id1);
706 if (err)
707 goto done;
708 time1 = got_object_commit_get_committer_time(commit1);
709 } else
710 time1 = got_object_tag_get_tagger_time(tag1);
712 err = got_ref_resolve(&id2, repo, ref2);
713 if (err)
714 goto done;
715 err = got_object_open_as_tag(&tag2, repo, id2);
716 if (err) {
717 if (err->code != GOT_ERR_OBJ_TYPE)
718 goto done;
719 /* "lightweight" tag */
720 err = got_object_open_as_commit(&commit2, repo, id2);
721 if (err)
722 goto done;
723 time2 = got_object_commit_get_committer_time(commit2);
724 } else
725 time2 = got_object_tag_get_tagger_time(tag2);
727 /* Put latest tags first. */
728 if (time1 < time2)
729 *cmp = 1;
730 else if (time1 > time2)
731 *cmp = -1;
732 else
733 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
734 done:
735 free(id1);
736 free(id2);
737 if (tag1)
738 got_object_tag_close(tag1);
739 if (tag2)
740 got_object_tag_close(tag2);
741 if (commit1)
742 got_object_commit_close(commit1);
743 if (commit2)
744 got_object_commit_close(commit2);
745 return err;
748 static const struct got_error *
749 insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
750 struct got_reference *ref, struct got_repository *repo,
751 got_ref_cmp_cb cmp_cb, void *cmp_arg)
753 const struct got_error *err;
754 struct got_object_id *id;
755 struct got_reflist_entry *new, *re, *prev = NULL;
756 int cmp;
758 *newp = NULL;
760 err = got_ref_resolve(&id, repo, ref);
761 if (err)
762 return err;
764 new = malloc(sizeof(*new));
765 if (new == NULL) {
766 free(id);
767 return got_error_from_errno("malloc");
769 new->ref = ref;
770 new->id = id;
771 *newp = new;
773 /*
774 * We must de-duplicate entries on insert because packed-refs may
775 * contain redundant entries. On-disk refs take precedence.
776 * This code assumes that on-disk revs are read before packed-refs.
777 * We're iterating the list anyway, so insert elements sorted by name.
778 */
779 re = SIMPLEQ_FIRST(refs);
780 while (re) {
781 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
782 if (err)
783 return err;
784 if (cmp == 0) {
785 /* duplicate */
786 free(new->id);
787 free(new);
788 *newp = NULL;
789 return NULL;
790 } else if (cmp > 0) {
791 if (prev)
792 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
793 else
794 SIMPLEQ_INSERT_HEAD(refs, new, entry);
795 return NULL;
796 } else {
797 prev = re;
798 re = SIMPLEQ_NEXT(re, entry);
802 SIMPLEQ_INSERT_TAIL(refs, new, entry);
803 return NULL;
806 static const struct got_error *
807 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
808 const char *subdir, struct got_repository *repo,
809 got_ref_cmp_cb cmp_cb, void *cmp_arg)
811 const struct got_error *err = NULL;
812 DIR *d = NULL;
813 char *path_subdir;
815 while (subdir[0] == '/')
816 subdir++;
818 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
819 return got_error_from_errno("asprintf");
821 d = opendir(path_subdir);
822 if (d == NULL)
823 goto done;
825 for (;;) {
826 struct dirent *dent;
827 struct got_reference *ref;
828 char *child;
830 dent = readdir(d);
831 if (dent == NULL)
832 break;
834 if (strcmp(dent->d_name, ".") == 0 ||
835 strcmp(dent->d_name, "..") == 0)
836 continue;
838 switch (dent->d_type) {
839 case DT_REG:
840 err = open_ref(&ref, path_refs, subdir, dent->d_name,
841 0);
842 if (err)
843 goto done;
844 if (ref) {
845 struct got_reflist_entry *new;
846 err = insert_ref(&new, refs, ref, repo,
847 cmp_cb, cmp_arg);
848 if (err || new == NULL /* duplicate */)
849 got_ref_close(ref);
850 if (err)
851 goto done;
853 break;
854 case DT_DIR:
855 if (asprintf(&child, "%s%s%s", subdir,
856 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
857 err = got_error_from_errno("asprintf");
858 break;
860 err = gather_on_disk_refs(refs, path_refs, child, repo,
861 cmp_cb, cmp_arg);
862 free(child);
863 break;
864 default:
865 break;
868 done:
869 if (d)
870 closedir(d);
871 free(path_subdir);
872 return err;
875 const struct got_error *
876 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
877 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
879 const struct got_error *err;
880 char *packed_refs_path, *path_refs = NULL;
881 char *abs_namespace = NULL;
882 char *buf = NULL, *ondisk_ref_namespace = NULL;
883 FILE *f = NULL;
884 struct got_reference *ref;
885 struct got_reflist_entry *new;
887 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
888 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
889 if (path_refs == NULL) {
890 err = got_error_from_errno("get_refs_dir_path");
891 goto done;
893 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
894 if (err)
895 goto done;
896 err = insert_ref(&new, refs, ref, repo,
897 cmp_cb, cmp_arg);
898 if (err || new == NULL /* duplicate */)
899 got_ref_close(ref);
900 if (err && err->code != GOT_ERR_NOT_REF)
901 goto done;
902 } else {
903 /* Try listing a single reference. */
904 const char *refname = ref_namespace;
905 path_refs = get_refs_dir_path(repo, refname);
906 if (path_refs == NULL) {
907 err = got_error_from_errno("get_refs_dir_path");
908 goto done;
910 err = open_ref(&ref, path_refs, "", refname, 0);
911 if (err) {
912 if (err->code != GOT_ERR_NOT_REF)
913 goto done;
914 /* Try to look up references in a given namespace. */
915 } else {
916 err = insert_ref(&new, refs, ref, repo,
917 cmp_cb, cmp_arg);
918 if (err || new == NULL /* duplicate */)
919 got_ref_close(ref);
920 return err;
924 if (ref_namespace) {
925 size_t len;
926 /* Canonicalize the path to eliminate double-slashes if any. */
927 if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
928 err = got_error_from_errno("asprintf");
929 goto done;
931 len = strlen(abs_namespace) + 1;
932 buf = malloc(len);
933 if (buf == NULL) {
934 err = got_error_from_errno("malloc");
935 goto done;
937 err = got_canonpath(abs_namespace, buf, len);
938 if (err)
939 goto done;
940 ondisk_ref_namespace = buf;
941 while (ondisk_ref_namespace[0] == '/')
942 ondisk_ref_namespace++;
943 if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
944 ondisk_ref_namespace += 5;
945 else if (strcmp(ondisk_ref_namespace, "refs") == 0)
946 ondisk_ref_namespace = "";
949 /* Gather on-disk refs before parsing packed-refs. */
950 free(path_refs);
951 path_refs = get_refs_dir_path(repo, "");
952 if (path_refs == NULL) {
953 err = got_error_from_errno("get_refs_dir_path");
954 goto done;
956 err = gather_on_disk_refs(refs, path_refs,
957 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
958 cmp_cb, cmp_arg);
959 if (err)
960 goto done;
962 /*
963 * The packed-refs file may contain redundant entries, in which
964 * case on-disk refs take precedence.
965 */
966 packed_refs_path = got_repo_get_path_packed_refs(repo);
967 if (packed_refs_path == NULL) {
968 err = got_error_from_errno("got_repo_get_path_packed_refs");
969 goto done;
972 f = fopen(packed_refs_path, "r");
973 free(packed_refs_path);
974 if (f) {
975 char *line;
976 size_t len;
977 const char delim[3] = {'\0', '\0', '\0'};
978 for (;;) {
979 line = fparseln(f, &len, NULL, delim, 0);
980 if (line == NULL) {
981 if (feof(f))
982 break;
983 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
984 goto done;
986 err = parse_packed_ref_line(&ref, NULL, line);
987 free(line);
988 if (err)
989 goto done;
990 if (ref) {
991 if (ref_namespace) {
992 const char *name;
993 name = got_ref_get_name(ref);
994 if (!got_path_is_child(name,
995 ref_namespace,
996 strlen(ref_namespace))) {
997 got_ref_close(ref);
998 continue;
1001 err = insert_ref(&new, refs, ref, repo,
1002 cmp_cb, cmp_arg);
1003 if (err || new == NULL /* duplicate */)
1004 got_ref_close(ref);
1005 if (err)
1006 goto done;
1010 done:
1011 free(abs_namespace);
1012 free(buf);
1013 free(path_refs);
1014 if (f && fclose(f) != 0 && err == NULL)
1015 err = got_error_from_errno("fclose");
1016 return err;
1019 void
1020 got_ref_list_free(struct got_reflist_head *refs)
1022 struct got_reflist_entry *re;
1024 while (!SIMPLEQ_EMPTY(refs)) {
1025 re = SIMPLEQ_FIRST(refs);
1026 SIMPLEQ_REMOVE_HEAD(refs, entry);
1027 got_ref_close(re->ref);
1028 free(re->id);
1029 free(re);
1034 int
1035 got_ref_is_symbolic(struct got_reference *ref)
1037 return (ref->flags & GOT_REF_IS_SYMBOLIC);
1040 const struct got_error *
1041 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1043 if (ref->flags & GOT_REF_IS_SYMBOLIC)
1044 return got_error(GOT_ERR_BAD_REF_TYPE);
1046 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
1047 return NULL;
1050 const struct got_error *
1051 got_ref_change_symref(struct got_reference *ref, char *refname)
1053 char *new_name;
1055 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1056 return got_error(GOT_ERR_BAD_REF_TYPE);
1058 new_name = strdup(refname);
1059 if (new_name == NULL)
1060 return got_error_from_errno("strdup");
1062 free(ref->ref.symref.name);
1063 ref->ref.symref.name = new_name;
1064 return NULL;
1067 const struct got_error *
1068 got_ref_change_symref_to_ref(struct got_reference *symref,
1069 struct got_object_id *id)
1071 if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1072 return got_error(GOT_ERR_BAD_REF_TYPE);
1074 symref->ref.ref.name = symref->ref.symref.name;
1075 memcpy(symref->ref.ref.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1076 symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1077 return NULL;
1080 const struct got_error *
1081 got_ref_write(struct got_reference *ref, struct got_repository *repo)
1083 const struct got_error *err = NULL, *unlock_err = NULL;
1084 const char *name = got_ref_get_name(ref);
1085 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1086 struct got_lockfile *lf = NULL;
1087 FILE *f = NULL;
1088 size_t n;
1089 struct stat sb;
1091 path_refs = get_refs_dir_path(repo, name);
1092 if (path_refs == NULL) {
1093 err = got_error_from_errno2("get_refs_dir_path", name);
1094 goto done;
1097 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1098 err = got_error_from_errno("asprintf");
1099 goto done;
1102 err = got_opentemp_named(&tmppath, &f, path);
1103 if (err) {
1104 char *parent;
1105 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1106 goto done;
1107 err = got_path_dirname(&parent, path);
1108 if (err)
1109 goto done;
1110 err = got_path_mkdir(parent);
1111 free(parent);
1112 if (err)
1113 goto done;
1114 err = got_opentemp_named(&tmppath, &f, path);
1115 if (err)
1116 goto done;
1119 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1120 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1121 if (n != strlen(ref->ref.symref.ref) + 6) {
1122 err = got_ferror(f, GOT_ERR_IO);
1123 goto done;
1125 } else {
1126 char hex[SHA1_DIGEST_STRING_LENGTH];
1127 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
1128 sizeof(hex)) == NULL) {
1129 err = got_error(GOT_ERR_BAD_REF_DATA);
1130 goto done;
1132 n = fprintf(f, "%s\n", hex);
1133 if (n != sizeof(hex)) {
1134 err = got_ferror(f, GOT_ERR_IO);
1135 goto done;
1139 if (ref->lf == NULL) {
1140 err = got_lockfile_lock(&lf, path);
1141 if (err)
1142 goto done;
1145 /* XXX: check if old content matches our expectations? */
1147 if (stat(path, &sb) != 0) {
1148 if (errno != ENOENT) {
1149 err = got_error_from_errno2("stat", path);
1150 goto done;
1152 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1155 if (rename(tmppath, path) != 0) {
1156 err = got_error_from_errno3("rename", tmppath, path);
1157 goto done;
1159 free(tmppath);
1160 tmppath = NULL;
1162 if (chmod(path, sb.st_mode) != 0) {
1163 err = got_error_from_errno2("chmod", path);
1164 goto done;
1166 done:
1167 if (ref->lf == NULL && lf)
1168 unlock_err = got_lockfile_unlock(lf);
1169 if (f) {
1170 if (fclose(f) != 0 && err == NULL)
1171 err = got_error_from_errno("fclose");
1173 free(path_refs);
1174 free(path);
1175 if (tmppath) {
1176 if (unlink(tmppath) != 0 && err == NULL)
1177 err = got_error_from_errno2("unlink", tmppath);
1178 free(tmppath);
1180 return err ? err : unlock_err;
1183 static const struct got_error *
1184 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1186 const struct got_error *err = NULL, *unlock_err = NULL;
1187 struct got_lockfile *lf = NULL;
1188 FILE *f = NULL, *tmpf = NULL;
1189 char *packed_refs_path, *tmppath = NULL;
1190 struct got_reflist_head refs;
1191 int found_delref = 0;
1193 /* The packed-refs file does not cotain symbolic references. */
1194 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1195 return got_error(GOT_ERR_BAD_REF_DATA);
1197 SIMPLEQ_INIT(&refs);
1199 packed_refs_path = got_repo_get_path_packed_refs(repo);
1200 if (packed_refs_path == NULL)
1201 return got_error_from_errno("got_repo_get_path_packed_refs");
1203 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
1204 if (err)
1205 goto done;
1207 if (delref->lf == NULL) {
1208 err = got_lockfile_lock(&lf, packed_refs_path);
1209 if (err)
1210 goto done;
1213 f = fopen(packed_refs_path, "r");
1214 if (f == NULL) {
1215 err = got_error_from_errno2("fopen", packed_refs_path);
1216 goto done;
1218 for (;;) {
1219 char *line;
1220 size_t len;
1221 const char delim[3] = {'\0', '\0', '\0'};
1222 struct got_reference *ref;
1223 struct got_reflist_entry *new;
1225 line = fparseln(f, &len, NULL, delim, 0);
1226 if (line == NULL) {
1227 if (feof(f))
1228 break;
1229 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1230 goto done;
1232 err = parse_packed_ref_line(&ref, NULL, line);
1233 free(line);
1234 if (err)
1235 goto done;
1236 if (ref == NULL)
1237 continue;
1239 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1240 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1241 sizeof(delref->ref.ref.sha1)) == 0) {
1242 found_delref = 1;
1243 got_ref_close(ref);
1244 continue;
1247 err = insert_ref(&new, &refs, ref, repo,
1248 got_ref_cmp_by_name, NULL);
1249 if (err || new == NULL /* duplicate */)
1250 got_ref_close(ref);
1251 if (err)
1252 goto done;
1255 if (found_delref) {
1256 struct got_reflist_entry *re;
1257 size_t n;
1258 struct stat sb;
1260 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1261 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1262 err = got_ferror(f, GOT_ERR_IO);
1263 goto done;
1266 SIMPLEQ_FOREACH(re, &refs, entry) {
1267 uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
1269 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1270 sizeof(hex)) == NULL) {
1271 err = got_error(GOT_ERR_BAD_REF_DATA);
1272 goto done;
1274 n = fprintf(tmpf, "%s ", hex);
1275 if (n != sizeof(hex)) {
1276 err = got_ferror(f, GOT_ERR_IO);
1277 goto done;
1279 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1280 if (n != strlen(re->ref->ref.ref.name) + 1) {
1281 err = got_ferror(f, GOT_ERR_IO);
1282 goto done;
1286 if (fflush(tmpf) != 0) {
1287 err = got_error_from_errno("fflush");
1288 goto done;
1291 if (stat(packed_refs_path, &sb) != 0) {
1292 if (errno != ENOENT) {
1293 err = got_error_from_errno2("stat",
1294 packed_refs_path);
1295 goto done;
1297 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1300 if (rename(tmppath, packed_refs_path) != 0) {
1301 err = got_error_from_errno3("rename", tmppath,
1302 packed_refs_path);
1303 goto done;
1306 if (chmod(packed_refs_path, sb.st_mode) != 0) {
1307 err = got_error_from_errno2("chmod",
1308 packed_refs_path);
1309 goto done;
1312 done:
1313 if (delref->lf == NULL && lf)
1314 unlock_err = got_lockfile_unlock(lf);
1315 if (f) {
1316 if (fclose(f) != 0 && err == NULL)
1317 err = got_error_from_errno("fclose");
1319 if (tmpf) {
1320 unlink(tmppath);
1321 if (fclose(tmpf) != 0 && err == NULL)
1322 err = got_error_from_errno("fclose");
1324 free(tmppath);
1325 free(packed_refs_path);
1326 got_ref_list_free(&refs);
1327 return err ? err : unlock_err;
1330 const struct got_error *
1331 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1333 const struct got_error *err = NULL, *unlock_err = NULL;
1334 const char *name = got_ref_get_name(ref);
1335 char *path_refs = NULL, *path = NULL;
1336 struct got_lockfile *lf = NULL;
1338 if (ref->flags & GOT_REF_IS_PACKED)
1339 return delete_packed_ref(ref, repo);
1341 path_refs = get_refs_dir_path(repo, name);
1342 if (path_refs == NULL) {
1343 err = got_error_from_errno2("get_refs_dir_path", name);
1344 goto done;
1347 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1348 err = got_error_from_errno("asprintf");
1349 goto done;
1352 if (ref->lf == NULL) {
1353 err = got_lockfile_lock(&lf, path);
1354 if (err)
1355 goto done;
1358 /* XXX: check if old content matches our expectations? */
1360 if (unlink(path) != 0)
1361 err = got_error_from_errno2("unlink", path);
1362 done:
1363 if (ref->lf == NULL && lf)
1364 unlock_err = got_lockfile_unlock(lf);
1366 free(path_refs);
1367 free(path);
1368 return err ? err : unlock_err;
1371 const struct got_error *
1372 got_ref_unlock(struct got_reference *ref)
1374 const struct got_error *err;
1375 err = got_lockfile_unlock(ref->lf);
1376 ref->lf = NULL;
1377 return err;