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 *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 return (err);
180 f = fopen(abspath, "rb");
181 if (f == NULL) {
182 if (lock)
183 got_lockfile_unlock(lf);
184 return NULL;
187 linelen = getline(&line, &linesize, f);
188 if (linelen == -1) {
189 if (feof(f))
190 err = NULL; /* ignore empty files (could be locks) */
191 else
192 err = got_error_from_errno2("getline", abspath);
193 if (lock)
194 got_lockfile_unlock(lf);
195 goto done;
197 while (linelen > 0 && line[linelen - 1] == '\n') {
198 line[linelen - 1] = '\0';
199 linelen--;
202 err = parse_ref_line(ref, name, line);
203 if (lock) {
204 if (err)
205 got_lockfile_unlock(lf);
206 else {
207 if (*ref)
208 (*ref)->lf = lf;
209 else
210 got_lockfile_unlock(lf);
213 done:
214 free(line);
215 if (fclose(f) != 0 && err == NULL) {
216 err = got_error_from_errno("fclose");
217 if (*ref) {
218 if (lock)
219 got_ref_unlock(*ref);
220 got_ref_close(*ref);
221 *ref = NULL;
224 return err;
227 static int
228 is_well_known_ref(const char *refname)
230 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
231 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
232 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
233 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
236 static char *
237 get_refs_dir_path(struct got_repository *repo, const char *refname)
239 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
240 return strdup(got_repo_get_path_git_dir(repo));
242 return got_repo_get_path_refs(repo);
245 static int
246 is_valid_ref_name(const char *name)
248 const char *s, *slash, *seg;
249 const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
250 const char *forbidden_seq[] = { "//", "..", "@{" };
251 const char *lfs = GOT_LOCKFILE_SUFFIX;
252 const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
253 int i;
255 if (name[0] == '@' && name[1] == '\0')
256 return 0;
258 slash = strchr(name, '/');
259 if (slash == NULL)
260 return 0;
262 s = name;
263 seg = s;
264 if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
265 return 0;
266 while (*s) {
267 for (i = 0; i < nitems(forbidden); i++) {
268 if (*s == forbidden[i])
269 return 0;
271 for (i = 0; i < nitems(forbidden_seq); i++) {
272 if (s[0] == forbidden_seq[i][0] &&
273 s[1] == forbidden_seq[i][1])
274 return 0;
276 if (iscntrl((unsigned char)s[0]))
277 return 0;
278 if (s[0] == '.' && s[1] == '\0')
279 return 0;
280 if (*s == '/') {
281 const char *nextseg = s + 1;
282 if (nextseg[0] == '\0' || nextseg[0] == '.' ||
283 nextseg[0] == '/')
284 return 0;
285 if (seg <= s - lfs_len &&
286 strncmp(s - lfs_len, lfs, lfs_len) == 0)
287 return 0;
288 seg = nextseg;
290 s++;
293 if (seg <= s - lfs_len &&
294 strncmp(s - lfs_len, lfs, lfs_len) == 0)
295 return 0;
297 return 1;
300 const struct got_error *
301 got_ref_alloc(struct got_reference **ref, const char *name,
302 struct got_object_id *id)
304 if (!is_valid_ref_name(name))
305 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
307 return alloc_ref(ref, name, id, 0);
310 const struct got_error *
311 got_ref_alloc_symref(struct got_reference **ref, const char *name,
312 struct got_reference *target_ref)
314 if (!is_valid_ref_name(name))
315 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
317 return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
320 static const struct got_error *
321 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
322 const char *line)
324 struct got_object_id id;
325 const char *name;
327 *ref = NULL;
329 if (line[0] == '#' || line[0] == '^')
330 return NULL;
332 if (!got_parse_sha1_digest(id.sha1, line))
333 return got_error(GOT_ERR_BAD_REF_DATA);
335 if (abs_refname) {
336 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
337 return NULL;
338 name = abs_refname;
339 } else
340 name = line + SHA1_DIGEST_STRING_LENGTH;
342 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED);
345 static const struct got_error *
346 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
347 int nsubdirs, const char *refname)
349 const struct got_error *err = NULL;
350 char *abs_refname;
351 char *line;
352 size_t len;
353 const char delim[3] = {'\0', '\0', '\0'};
354 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
356 *ref = NULL;
358 if (ref_is_absolute)
359 abs_refname = (char *)refname;
360 do {
361 line = fparseln(f, &len, NULL, delim, 0);
362 if (line == NULL) {
363 if (feof(f))
364 break;
365 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
366 break;
368 for (i = 0; i < nsubdirs; i++) {
369 if (!ref_is_absolute &&
370 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
371 refname) == -1)
372 return got_error_from_errno("asprintf");
373 err = parse_packed_ref_line(ref, abs_refname, line);
374 if (!ref_is_absolute)
375 free(abs_refname);
376 if (err || *ref != NULL)
377 break;
379 free(line);
380 if (err)
381 break;
382 } while (*ref == NULL);
384 return err;
387 static const struct got_error *
388 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
389 const char *name, int lock)
391 const struct got_error *err = NULL;
392 char *path = NULL;
393 char *absname = NULL;
394 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
395 int ref_is_well_known = is_well_known_ref(name);
397 *ref = NULL;
399 if (ref_is_absolute || ref_is_well_known) {
400 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
401 return got_error_from_errno("asprintf");
402 absname = (char *)name;
403 } else {
404 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
405 subdir[0] ? "/" : "", name) == -1)
406 return got_error_from_errno("asprintf");
408 if (asprintf(&absname, "refs/%s%s%s",
409 subdir, subdir[0] ? "/" : "", name) == -1) {
410 err = got_error_from_errno("asprintf");
411 goto done;
415 err = parse_ref_file(ref, absname, path, lock);
416 done:
417 if (!ref_is_absolute && !ref_is_well_known)
418 free(absname);
419 free(path);
420 return err;
423 const struct got_error *
424 got_ref_open(struct got_reference **ref, struct got_repository *repo,
425 const char *refname, int lock)
427 const struct got_error *err = NULL;
428 char *path_refs = NULL;
429 const char *subdirs[] = {
430 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
431 };
432 int i, well_known = is_well_known_ref(refname);
433 struct got_lockfile *lf = NULL;
435 *ref = NULL;
437 path_refs = get_refs_dir_path(repo, refname);
438 if (path_refs == NULL) {
439 err = got_error_from_errno2("get_refs_dir_path", refname);
440 goto done;
443 if (well_known) {
444 err = open_ref(ref, path_refs, "", refname, lock);
445 } else {
446 char *packed_refs_path;
447 FILE *f;
449 /* Search on-disk refs before packed refs! */
450 for (i = 0; i < nitems(subdirs); i++) {
451 err = open_ref(ref, path_refs, subdirs[i], refname,
452 lock);
453 if (err || *ref)
454 goto done;
457 packed_refs_path = got_repo_get_path_packed_refs(repo);
458 if (packed_refs_path == NULL) {
459 err = got_error_from_errno(
460 "got_repo_get_path_packed_refs");
461 goto done;
464 if (lock) {
465 err = got_lockfile_lock(&lf, packed_refs_path);
466 if (err)
467 goto done;
469 f = fopen(packed_refs_path, "rb");
470 free(packed_refs_path);
471 if (f != NULL) {
472 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
473 refname);
474 if (!err) {
475 if (fclose(f) != 0) {
476 err = got_error_from_errno("fclose");
477 got_ref_close(*ref);
478 *ref = NULL;
479 } else if (*ref)
480 (*ref)->lf = lf;
484 done:
485 if (!err && *ref == NULL)
486 err = got_error_not_ref(refname);
487 if (err && lf)
488 got_lockfile_unlock(lf);
489 free(path_refs);
490 return err;
493 void
494 got_ref_close(struct got_reference *ref)
496 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
497 free(ref->ref.symref.name);
498 free(ref->ref.symref.ref);
499 } else
500 free(ref->ref.ref.name);
501 free(ref);
504 struct got_reference *
505 got_ref_dup(struct got_reference *ref)
507 struct got_reference *ret;
509 ret = calloc(1, sizeof(*ret));
510 if (ret == NULL)
511 return NULL;
513 ret->flags = ref->flags;
514 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
515 ret->ref.symref.name = strdup(ref->ref.symref.name);
516 if (ret->ref.symref.name == NULL) {
517 free(ret);
518 return NULL;
520 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
521 if (ret->ref.symref.ref == NULL) {
522 free(ret->ref.symref.name);
523 free(ret);
524 return NULL;
526 } else {
527 ref->ref.ref.name = strdup(ref->ref.ref.name);
528 if (ref->ref.ref.name == NULL) {
529 free(ret);
530 return NULL;
532 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
533 sizeof(ret->ref.ref.sha1));
536 return ret;
539 const struct got_error *
540 got_reflist_entry_dup(struct got_reflist_entry **newp,
541 struct got_reflist_entry *re)
543 const struct got_error *err = NULL;
544 struct got_reflist_entry *new;
546 *newp = NULL;
548 new = malloc(sizeof(*new));
549 if (new == NULL)
550 return got_error_from_errno("malloc");
552 new->ref = got_ref_dup(re->ref);
553 if (new->ref == NULL) {
554 err = got_error_from_errno("got_ref_dup");
555 free(new);
556 return err;
559 new->id = got_object_id_dup(re->id);
560 if (new->id == NULL) {
561 err = got_error_from_errno("got_ref_dup");
562 free(new->id);
563 free(new);
564 return err;
567 *newp = new;
568 return NULL;
571 static const struct got_error *
572 resolve_symbolic_ref(struct got_reference **resolved,
573 struct got_repository *repo, struct got_reference *ref)
575 struct got_reference *nextref;
576 const struct got_error *err;
578 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
579 if (err)
580 return err;
582 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
583 err = resolve_symbolic_ref(resolved, repo, nextref);
584 else
585 *resolved = got_ref_dup(nextref);
587 got_ref_close(nextref);
588 return err;
591 static const struct got_error *
592 ref_resolve(struct got_object_id **id, struct got_repository *repo,
593 struct got_reference *ref, int recursion)
595 const struct got_error *err;
597 if (recursion <= 0)
598 return got_error_msg(GOT_ERR_RECURSION,
599 "reference recursion limit reached");
601 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
602 struct got_reference *resolved = NULL;
603 err = resolve_symbolic_ref(&resolved, repo, ref);
604 if (err == NULL)
605 err = ref_resolve(id, repo, resolved, --recursion);
606 if (resolved)
607 got_ref_close(resolved);
608 return err;
611 *id = calloc(1, sizeof(**id));
612 if (*id == NULL)
613 return got_error_from_errno("calloc");
614 memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
615 return NULL;
618 const struct got_error *
619 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
620 struct got_reference *ref)
622 return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
625 char *
626 got_ref_to_str(struct got_reference *ref)
628 char *str;
630 if (ref->flags & GOT_REF_IS_SYMBOLIC)
631 return strdup(ref->ref.symref.ref);
633 str = malloc(SHA1_DIGEST_STRING_LENGTH);
634 if (str == NULL)
635 return NULL;
637 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
638 SHA1_DIGEST_STRING_LENGTH) == NULL) {
639 free(str);
640 return NULL;
643 return str;
646 const char *
647 got_ref_get_name(struct got_reference *ref)
649 if (ref->flags & GOT_REF_IS_SYMBOLIC)
650 return ref->ref.symref.name;
652 return ref->ref.ref.name;
655 const char *
656 got_ref_get_symref_target(struct got_reference *ref)
658 if (ref->flags & GOT_REF_IS_SYMBOLIC)
659 return ref->ref.symref.ref;
661 return NULL;
664 const struct got_error *
665 got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
666 struct got_reference* re2)
668 const char *name1 = got_ref_get_name(re1);
669 const char *name2 = got_ref_get_name(re2);
671 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
672 return NULL;
675 const struct got_error *
676 got_ref_cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
677 struct got_reference *ref2)
679 const struct got_error *err = NULL;
680 struct got_repository *repo = arg;
681 struct got_object_id *id1, *id2 = NULL;
682 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
683 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
684 time_t time1, time2;
686 *cmp = 0;
688 err = got_ref_resolve(&id1, repo, ref1);
689 if (err)
690 return err;
691 err = got_object_open_as_tag(&tag1, repo, id1);
692 if (err) {
693 if (err->code != GOT_ERR_OBJ_TYPE)
694 goto done;
695 /* "lightweight" tag */
696 err = got_object_open_as_commit(&commit1, repo, id1);
697 if (err)
698 goto done;
699 time1 = got_object_commit_get_committer_time(commit1);
700 } else
701 time1 = got_object_tag_get_tagger_time(tag1);
703 err = got_ref_resolve(&id2, repo, ref2);
704 if (err)
705 goto done;
706 err = got_object_open_as_tag(&tag2, repo, id2);
707 if (err) {
708 if (err->code != GOT_ERR_OBJ_TYPE)
709 goto done;
710 /* "lightweight" tag */
711 err = got_object_open_as_commit(&commit2, repo, id2);
712 if (err)
713 goto done;
714 time2 = got_object_commit_get_committer_time(commit2);
715 } else
716 time2 = got_object_tag_get_tagger_time(tag2);
718 /* Put latest tags first. */
719 if (time1 < time2)
720 *cmp = 1;
721 else if (time1 > time2)
722 *cmp = -1;
723 else
724 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
725 done:
726 free(id1);
727 free(id2);
728 if (tag1)
729 got_object_tag_close(tag1);
730 if (tag2)
731 got_object_tag_close(tag2);
732 if (commit1)
733 got_object_commit_close(commit1);
734 if (commit2)
735 got_object_commit_close(commit2);
736 return err;
739 static const struct got_error *
740 insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
741 struct got_reference *ref, struct got_repository *repo,
742 got_ref_cmp_cb cmp_cb, void *cmp_arg)
744 const struct got_error *err;
745 struct got_object_id *id;
746 struct got_reflist_entry *new, *re, *prev = NULL;
747 int cmp;
749 *newp = NULL;
751 err = got_ref_resolve(&id, repo, ref);
752 if (err)
753 return err;
755 new = malloc(sizeof(*new));
756 if (new == NULL) {
757 free(id);
758 return got_error_from_errno("malloc");
760 new->ref = ref;
761 new->id = id;
762 *newp = new;
764 /*
765 * We must de-duplicate entries on insert because packed-refs may
766 * contain redundant entries. On-disk refs take precedence.
767 * This code assumes that on-disk revs are read before packed-refs.
768 * We're iterating the list anyway, so insert elements sorted by name.
769 */
770 re = SIMPLEQ_FIRST(refs);
771 while (re) {
772 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
773 if (err)
774 return err;
775 if (cmp == 0) {
776 /* duplicate */
777 free(new->id);
778 free(new);
779 *newp = NULL;
780 return NULL;
781 } else if (cmp > 0) {
782 if (prev)
783 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
784 else
785 SIMPLEQ_INSERT_HEAD(refs, new, entry);
786 return NULL;
787 } else {
788 prev = re;
789 re = SIMPLEQ_NEXT(re, entry);
793 SIMPLEQ_INSERT_TAIL(refs, new, entry);
794 return NULL;
797 static const struct got_error *
798 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
799 const char *subdir, struct got_repository *repo,
800 got_ref_cmp_cb cmp_cb, void *cmp_arg)
802 const struct got_error *err = NULL;
803 DIR *d = NULL;
804 char *path_subdir;
806 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
807 return got_error_from_errno("asprintf");
809 d = opendir(path_subdir);
810 if (d == NULL)
811 goto done;
813 for (;;) {
814 struct dirent *dent;
815 struct got_reference *ref;
816 char *child;
818 dent = readdir(d);
819 if (dent == NULL)
820 break;
822 if (strcmp(dent->d_name, ".") == 0 ||
823 strcmp(dent->d_name, "..") == 0)
824 continue;
826 switch (dent->d_type) {
827 case DT_REG:
828 err = open_ref(&ref, path_refs, subdir, dent->d_name,
829 0);
830 if (err)
831 goto done;
832 if (ref) {
833 struct got_reflist_entry *new;
834 err = insert_ref(&new, refs, ref, repo,
835 cmp_cb, cmp_arg);
836 if (err || new == NULL /* duplicate */)
837 got_ref_close(ref);
838 if (err)
839 goto done;
841 break;
842 case DT_DIR:
843 if (asprintf(&child, "%s%s%s", subdir,
844 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
845 err = got_error_from_errno("asprintf");
846 break;
848 err = gather_on_disk_refs(refs, path_refs, child, repo,
849 cmp_cb, cmp_arg);
850 free(child);
851 break;
852 default:
853 break;
856 done:
857 if (d)
858 closedir(d);
859 free(path_subdir);
860 return err;
863 const struct got_error *
864 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
865 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
867 const struct got_error *err;
868 char *packed_refs_path, *path_refs = NULL;
869 const char *ondisk_ref_namespace = NULL;
870 FILE *f = NULL;
871 struct got_reference *ref;
872 struct got_reflist_entry *new;
874 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
875 /* HEAD ref should always exist. */
876 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
877 if (path_refs == NULL) {
878 err = got_error_from_errno("get_refs_dir_path");
879 goto done;
881 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
882 if (err)
883 goto done;
884 err = insert_ref(&new, refs, ref, repo, cmp_cb, cmp_arg);
885 if (err || new == NULL /* duplicate */)
886 got_ref_close(ref);
887 if (err)
888 goto done;
891 ondisk_ref_namespace = ref_namespace;
892 if (ref_namespace && strncmp(ref_namespace, "refs/", 5) == 0)
893 ondisk_ref_namespace += 5;
895 /* Gather on-disk refs before parsing packed-refs. */
896 free(path_refs);
897 path_refs = get_refs_dir_path(repo, "");
898 if (path_refs == NULL) {
899 err = got_error_from_errno("get_refs_dir_path");
900 goto done;
902 err = gather_on_disk_refs(refs, path_refs,
903 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
904 cmp_cb, cmp_arg);
905 if (err)
906 goto done;
908 /*
909 * The packed-refs file may contain redundant entries, in which
910 * case on-disk refs take precedence.
911 */
912 packed_refs_path = got_repo_get_path_packed_refs(repo);
913 if (packed_refs_path == NULL) {
914 err = got_error_from_errno("got_repo_get_path_packed_refs");
915 goto done;
918 f = fopen(packed_refs_path, "r");
919 free(packed_refs_path);
920 if (f) {
921 char *line;
922 size_t len;
923 const char delim[3] = {'\0', '\0', '\0'};
924 for (;;) {
925 line = fparseln(f, &len, NULL, delim, 0);
926 if (line == NULL) {
927 if (feof(f))
928 break;
929 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
930 goto done;
932 err = parse_packed_ref_line(&ref, NULL, line);
933 free(line);
934 if (err)
935 goto done;
936 if (ref) {
937 if (ref_namespace) {
938 const char *name;
939 name = got_ref_get_name(ref);
940 if (strncmp(name, ref_namespace,
941 strlen(ref_namespace)) != 0) {
942 got_ref_close(ref);
943 continue;
946 err = insert_ref(&new, refs, ref, repo,
947 cmp_cb, cmp_arg);
948 if (err || new == NULL /* duplicate */)
949 got_ref_close(ref);
950 if (err)
951 goto done;
955 done:
956 free(path_refs);
957 if (f && fclose(f) != 0 && err == NULL)
958 err = got_error_from_errno("fclose");
959 return err;
962 void
963 got_ref_list_free(struct got_reflist_head *refs)
965 struct got_reflist_entry *re;
967 while (!SIMPLEQ_EMPTY(refs)) {
968 re = SIMPLEQ_FIRST(refs);
969 SIMPLEQ_REMOVE_HEAD(refs, entry);
970 got_ref_close(re->ref);
971 free(re->id);
972 free(re);
977 int
978 got_ref_is_symbolic(struct got_reference *ref)
980 return (ref->flags & GOT_REF_IS_SYMBOLIC);
983 const struct got_error *
984 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
986 if (ref->flags & GOT_REF_IS_SYMBOLIC)
987 return got_error(GOT_ERR_BAD_REF_TYPE);
989 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
990 return NULL;
993 const struct got_error *
994 got_ref_change_symref(struct got_reference *ref, char *refname)
996 char *new_name;
998 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
999 return got_error(GOT_ERR_BAD_REF_TYPE);
1001 new_name = strdup(refname);
1002 if (new_name == NULL)
1003 return got_error_from_errno("strdup");
1005 free(ref->ref.symref.name);
1006 ref->ref.symref.name = new_name;
1007 return NULL;
1010 const struct got_error *
1011 got_ref_write(struct got_reference *ref, struct got_repository *repo)
1013 const struct got_error *err = NULL, *unlock_err = NULL;
1014 const char *name = got_ref_get_name(ref);
1015 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1016 struct got_lockfile *lf = NULL;
1017 FILE *f = NULL;
1018 size_t n;
1019 struct stat sb;
1021 path_refs = get_refs_dir_path(repo, name);
1022 if (path_refs == NULL) {
1023 err = got_error_from_errno2("get_refs_dir_path", name);
1024 goto done;
1027 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1028 err = got_error_from_errno("asprintf");
1029 goto done;
1032 err = got_opentemp_named(&tmppath, &f, path);
1033 if (err) {
1034 char *parent;
1035 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1036 goto done;
1037 err = got_path_dirname(&parent, path);
1038 if (err)
1039 goto done;
1040 err = got_path_mkdir(parent);
1041 free(parent);
1042 if (err)
1043 goto done;
1044 err = got_opentemp_named(&tmppath, &f, path);
1045 if (err)
1046 goto done;
1049 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1050 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1051 if (n != strlen(ref->ref.symref.ref) + 6) {
1052 err = got_ferror(f, GOT_ERR_IO);
1053 goto done;
1055 } else {
1056 char hex[SHA1_DIGEST_STRING_LENGTH];
1057 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
1058 sizeof(hex)) == NULL) {
1059 err = got_error(GOT_ERR_BAD_REF_DATA);
1060 goto done;
1062 n = fprintf(f, "%s\n", hex);
1063 if (n != sizeof(hex)) {
1064 err = got_ferror(f, GOT_ERR_IO);
1065 goto done;
1069 if (ref->lf == NULL) {
1070 err = got_lockfile_lock(&lf, path);
1071 if (err)
1072 goto done;
1075 /* XXX: check if old content matches our expectations? */
1077 if (stat(path, &sb) != 0) {
1078 if (errno != ENOENT) {
1079 err = got_error_from_errno2("stat", path);
1080 goto done;
1082 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1085 if (rename(tmppath, path) != 0) {
1086 err = got_error_from_errno3("rename", tmppath, path);
1087 goto done;
1089 free(tmppath);
1090 tmppath = NULL;
1092 if (chmod(path, sb.st_mode) != 0) {
1093 err = got_error_from_errno2("chmod", path);
1094 goto done;
1096 done:
1097 if (ref->lf == NULL && lf)
1098 unlock_err = got_lockfile_unlock(lf);
1099 if (f) {
1100 if (fclose(f) != 0 && err == NULL)
1101 err = got_error_from_errno("fclose");
1103 free(path_refs);
1104 free(path);
1105 if (tmppath) {
1106 if (unlink(tmppath) != 0 && err == NULL)
1107 err = got_error_from_errno2("unlink", tmppath);
1108 free(tmppath);
1110 return err ? err : unlock_err;
1113 static const struct got_error *
1114 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1116 const struct got_error *err = NULL, *unlock_err = NULL;
1117 struct got_lockfile *lf = NULL;
1118 FILE *f = NULL, *tmpf = NULL;
1119 char *packed_refs_path, *tmppath = NULL;
1120 struct got_reflist_head refs;
1121 int found_delref = 0;
1123 /* The packed-refs file does not cotain symbolic references. */
1124 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1125 return got_error(GOT_ERR_BAD_REF_DATA);
1127 SIMPLEQ_INIT(&refs);
1129 packed_refs_path = got_repo_get_path_packed_refs(repo);
1130 if (packed_refs_path == NULL)
1131 return got_error_from_errno("got_repo_get_path_packed_refs");
1133 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
1134 if (err)
1135 goto done;
1137 if (delref->lf == NULL) {
1138 err = got_lockfile_lock(&lf, packed_refs_path);
1139 if (err)
1140 goto done;
1143 f = fopen(packed_refs_path, "r");
1144 if (f == NULL) {
1145 err = got_error_from_errno2("fopen", packed_refs_path);
1146 goto done;
1148 for (;;) {
1149 char *line;
1150 size_t len;
1151 const char delim[3] = {'\0', '\0', '\0'};
1152 struct got_reference *ref;
1153 struct got_reflist_entry *new;
1155 line = fparseln(f, &len, NULL, delim, 0);
1156 if (line == NULL) {
1157 if (feof(f))
1158 break;
1159 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1160 goto done;
1162 err = parse_packed_ref_line(&ref, NULL, line);
1163 free(line);
1164 if (err)
1165 goto done;
1166 if (ref == NULL)
1167 continue;
1169 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1170 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1171 sizeof(delref->ref.ref.sha1)) == 0) {
1172 found_delref = 1;
1173 got_ref_close(ref);
1174 continue;
1177 err = insert_ref(&new, &refs, ref, repo,
1178 got_ref_cmp_by_name, NULL);
1179 if (err || new == NULL /* duplicate */)
1180 got_ref_close(ref);
1181 if (err)
1182 goto done;
1185 if (found_delref) {
1186 struct got_reflist_entry *re;
1187 size_t n;
1188 struct stat sb;
1190 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1191 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1192 err = got_ferror(f, GOT_ERR_IO);
1193 goto done;
1196 SIMPLEQ_FOREACH(re, &refs, entry) {
1197 uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
1199 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1200 sizeof(hex)) == NULL) {
1201 err = got_error(GOT_ERR_BAD_REF_DATA);
1202 goto done;
1204 n = fprintf(tmpf, "%s ", hex);
1205 if (n != sizeof(hex)) {
1206 err = got_ferror(f, GOT_ERR_IO);
1207 goto done;
1209 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1210 if (n != strlen(re->ref->ref.ref.name) + 1) {
1211 err = got_ferror(f, GOT_ERR_IO);
1212 goto done;
1216 if (fflush(tmpf) != 0) {
1217 err = got_error_from_errno("fflush");
1218 goto done;
1221 if (stat(packed_refs_path, &sb) != 0) {
1222 if (errno != ENOENT) {
1223 err = got_error_from_errno2("stat",
1224 packed_refs_path);
1225 goto done;
1227 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1230 if (rename(tmppath, packed_refs_path) != 0) {
1231 err = got_error_from_errno3("rename", tmppath,
1232 packed_refs_path);
1233 goto done;
1236 if (chmod(packed_refs_path, sb.st_mode) != 0) {
1237 err = got_error_from_errno2("chmod",
1238 packed_refs_path);
1239 goto done;
1242 done:
1243 if (delref->lf == NULL && lf)
1244 unlock_err = got_lockfile_unlock(lf);
1245 if (f) {
1246 if (fclose(f) != 0 && err == NULL)
1247 err = got_error_from_errno("fclose");
1249 if (tmpf) {
1250 unlink(tmppath);
1251 if (fclose(tmpf) != 0 && err == NULL)
1252 err = got_error_from_errno("fclose");
1254 free(tmppath);
1255 free(packed_refs_path);
1256 got_ref_list_free(&refs);
1257 return err ? err : unlock_err;
1260 const struct got_error *
1261 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1263 const struct got_error *err = NULL, *unlock_err = NULL;
1264 const char *name = got_ref_get_name(ref);
1265 char *path_refs = NULL, *path = NULL;
1266 struct got_lockfile *lf = NULL;
1268 if (ref->flags & GOT_REF_IS_PACKED)
1269 return delete_packed_ref(ref, repo);
1271 path_refs = get_refs_dir_path(repo, name);
1272 if (path_refs == NULL) {
1273 err = got_error_from_errno2("get_refs_dir_path", name);
1274 goto done;
1277 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1278 err = got_error_from_errno("asprintf");
1279 goto done;
1282 if (ref->lf == NULL) {
1283 err = got_lockfile_lock(&lf, path);
1284 if (err)
1285 goto done;
1288 /* XXX: check if old content matches our expectations? */
1290 if (unlink(path) != 0)
1291 err = got_error_from_errno2("unlink", path);
1292 done:
1293 if (ref->lf == NULL && lf)
1294 unlock_err = got_lockfile_unlock(lf);
1296 free(path_refs);
1297 free(path);
1298 return err ? err : unlock_err;
1301 const struct got_error *
1302 got_ref_unlock(struct got_reference *ref)
1304 const struct got_error *err;
1305 err = got_lockfile_unlock(ref->lf);
1306 ref->lf = NULL;
1307 return err;