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 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
178 err = got_error(GOT_ERR_NOT_REF);
179 return err;
183 f = fopen(abspath, "rb");
184 if (f == NULL) {
185 if (lock)
186 got_lockfile_unlock(lf);
187 return NULL;
190 linelen = getline(&line, &linesize, f);
191 if (linelen == -1) {
192 if (feof(f))
193 err = NULL; /* ignore empty files (could be locks) */
194 else
195 err = got_error_from_errno2("getline", abspath);
196 if (lock)
197 got_lockfile_unlock(lf);
198 goto done;
200 while (linelen > 0 && line[linelen - 1] == '\n') {
201 line[linelen - 1] = '\0';
202 linelen--;
205 err = parse_ref_line(ref, name, line);
206 if (lock) {
207 if (err)
208 got_lockfile_unlock(lf);
209 else {
210 if (*ref)
211 (*ref)->lf = lf;
212 else
213 got_lockfile_unlock(lf);
216 done:
217 free(line);
218 if (fclose(f) != 0 && err == NULL) {
219 err = got_error_from_errno("fclose");
220 if (*ref) {
221 if (lock)
222 got_ref_unlock(*ref);
223 got_ref_close(*ref);
224 *ref = NULL;
227 return err;
230 static int
231 is_well_known_ref(const char *refname)
233 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
234 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
235 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
236 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
239 static char *
240 get_refs_dir_path(struct got_repository *repo, const char *refname)
242 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
243 return strdup(got_repo_get_path_git_dir(repo));
245 return got_repo_get_path_refs(repo);
248 static int
249 is_valid_ref_name(const char *name)
251 const char *s, *seg;
252 const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
253 const char *forbidden_seq[] = { "//", "..", "@{" };
254 const char *lfs = GOT_LOCKFILE_SUFFIX;
255 const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
256 int i;
258 if (name[0] == '@' && name[1] == '\0')
259 return 0;
261 s = name;
262 seg = s;
263 if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
264 return 0;
265 while (*s) {
266 for (i = 0; i < nitems(forbidden); i++) {
267 if (*s == forbidden[i])
268 return 0;
270 for (i = 0; i < nitems(forbidden_seq); i++) {
271 if (s[0] == forbidden_seq[i][0] &&
272 s[1] == forbidden_seq[i][1])
273 return 0;
275 if (iscntrl((unsigned char)s[0]))
276 return 0;
277 if (s[0] == '.' && s[1] == '\0')
278 return 0;
279 if (*s == '/') {
280 const char *nextseg = s + 1;
281 if (nextseg[0] == '\0' || nextseg[0] == '.' ||
282 nextseg[0] == '/')
283 return 0;
284 if (seg <= s - lfs_len &&
285 strncmp(s - lfs_len, lfs, lfs_len) == 0)
286 return 0;
287 seg = nextseg;
289 s++;
292 if (seg <= s - lfs_len &&
293 strncmp(s - lfs_len, lfs, lfs_len) == 0)
294 return 0;
296 return 1;
299 const struct got_error *
300 got_ref_alloc(struct got_reference **ref, const char *name,
301 struct got_object_id *id)
303 if (!is_valid_ref_name(name))
304 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
306 return alloc_ref(ref, name, id, 0);
309 const struct got_error *
310 got_ref_alloc_symref(struct got_reference **ref, const char *name,
311 struct got_reference *target_ref)
313 if (!is_valid_ref_name(name))
314 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
316 return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
319 static const struct got_error *
320 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
321 const char *line)
323 struct got_object_id id;
324 const char *name;
326 *ref = NULL;
328 if (line[0] == '#' || line[0] == '^')
329 return NULL;
331 if (!got_parse_sha1_digest(id.sha1, line))
332 return got_error(GOT_ERR_BAD_REF_DATA);
334 if (abs_refname) {
335 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
336 return NULL;
337 name = abs_refname;
338 } else
339 name = line + SHA1_DIGEST_STRING_LENGTH;
341 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED);
344 static const struct got_error *
345 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
346 int nsubdirs, const char *refname)
348 const struct got_error *err = NULL;
349 char *abs_refname;
350 char *line;
351 size_t len;
352 const char delim[3] = {'\0', '\0', '\0'};
353 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
355 *ref = NULL;
357 if (ref_is_absolute)
358 abs_refname = (char *)refname;
359 do {
360 line = fparseln(f, &len, NULL, delim, 0);
361 if (line == NULL) {
362 if (feof(f))
363 break;
364 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
365 break;
367 for (i = 0; i < nsubdirs; i++) {
368 if (!ref_is_absolute &&
369 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
370 refname) == -1)
371 return got_error_from_errno("asprintf");
372 err = parse_packed_ref_line(ref, abs_refname, line);
373 if (!ref_is_absolute)
374 free(abs_refname);
375 if (err || *ref != NULL)
376 break;
378 free(line);
379 if (err)
380 break;
381 } while (*ref == NULL);
383 return err;
386 static const struct got_error *
387 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
388 const char *name, int lock)
390 const struct got_error *err = NULL;
391 char *path = NULL;
392 char *absname = NULL;
393 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
394 int ref_is_well_known = is_well_known_ref(name);
396 *ref = NULL;
398 if (ref_is_absolute || ref_is_well_known) {
399 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
400 return got_error_from_errno("asprintf");
401 absname = (char *)name;
402 } else {
403 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
404 subdir[0] ? "/" : "", name) == -1)
405 return got_error_from_errno("asprintf");
407 if (asprintf(&absname, "refs/%s%s%s",
408 subdir, subdir[0] ? "/" : "", name) == -1) {
409 err = got_error_from_errno("asprintf");
410 goto done;
414 err = parse_ref_file(ref, absname, path, lock);
415 done:
416 if (!ref_is_absolute && !ref_is_well_known)
417 free(absname);
418 free(path);
419 return err;
422 const struct got_error *
423 got_ref_open(struct got_reference **ref, struct got_repository *repo,
424 const char *refname, int lock)
426 const struct got_error *err = NULL;
427 char *path_refs = NULL;
428 const char *subdirs[] = {
429 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
430 };
431 int i, well_known = is_well_known_ref(refname);
432 struct got_lockfile *lf = NULL;
434 *ref = NULL;
436 path_refs = get_refs_dir_path(repo, refname);
437 if (path_refs == NULL) {
438 err = got_error_from_errno2("get_refs_dir_path", refname);
439 goto done;
442 if (well_known) {
443 err = open_ref(ref, path_refs, "", refname, lock);
444 } else {
445 char *packed_refs_path;
446 FILE *f;
448 /* Search on-disk refs before packed refs! */
449 for (i = 0; i < nitems(subdirs); i++) {
450 err = open_ref(ref, path_refs, subdirs[i], refname,
451 lock);
452 if (err || *ref)
453 goto done;
456 packed_refs_path = got_repo_get_path_packed_refs(repo);
457 if (packed_refs_path == NULL) {
458 err = got_error_from_errno(
459 "got_repo_get_path_packed_refs");
460 goto done;
463 if (lock) {
464 err = got_lockfile_lock(&lf, packed_refs_path);
465 if (err)
466 goto done;
468 f = fopen(packed_refs_path, "rb");
469 free(packed_refs_path);
470 if (f != NULL) {
471 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
472 refname);
473 if (!err) {
474 if (fclose(f) != 0) {
475 err = got_error_from_errno("fclose");
476 got_ref_close(*ref);
477 *ref = NULL;
478 } else if (*ref)
479 (*ref)->lf = lf;
483 done:
484 if (!err && *ref == NULL)
485 err = got_error_not_ref(refname);
486 if (err && lf)
487 got_lockfile_unlock(lf);
488 free(path_refs);
489 return err;
492 void
493 got_ref_close(struct got_reference *ref)
495 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
496 free(ref->ref.symref.name);
497 free(ref->ref.symref.ref);
498 } else
499 free(ref->ref.ref.name);
500 free(ref);
503 struct got_reference *
504 got_ref_dup(struct got_reference *ref)
506 struct got_reference *ret;
508 ret = calloc(1, sizeof(*ret));
509 if (ret == NULL)
510 return NULL;
512 ret->flags = ref->flags;
513 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
514 ret->ref.symref.name = strdup(ref->ref.symref.name);
515 if (ret->ref.symref.name == NULL) {
516 free(ret);
517 return NULL;
519 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
520 if (ret->ref.symref.ref == NULL) {
521 free(ret->ref.symref.name);
522 free(ret);
523 return NULL;
525 } else {
526 ref->ref.ref.name = strdup(ref->ref.ref.name);
527 if (ref->ref.ref.name == NULL) {
528 free(ret);
529 return NULL;
531 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
532 sizeof(ret->ref.ref.sha1));
535 return ret;
538 const struct got_error *
539 got_reflist_entry_dup(struct got_reflist_entry **newp,
540 struct got_reflist_entry *re)
542 const struct got_error *err = NULL;
543 struct got_reflist_entry *new;
545 *newp = NULL;
547 new = malloc(sizeof(*new));
548 if (new == NULL)
549 return got_error_from_errno("malloc");
551 new->ref = got_ref_dup(re->ref);
552 if (new->ref == NULL) {
553 err = got_error_from_errno("got_ref_dup");
554 free(new);
555 return err;
558 new->id = got_object_id_dup(re->id);
559 if (new->id == NULL) {
560 err = got_error_from_errno("got_ref_dup");
561 free(new->id);
562 free(new);
563 return err;
566 *newp = new;
567 return NULL;
570 static const struct got_error *
571 resolve_symbolic_ref(struct got_reference **resolved,
572 struct got_repository *repo, struct got_reference *ref)
574 struct got_reference *nextref;
575 const struct got_error *err;
577 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
578 if (err)
579 return err;
581 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
582 err = resolve_symbolic_ref(resolved, repo, nextref);
583 else
584 *resolved = got_ref_dup(nextref);
586 got_ref_close(nextref);
587 return err;
590 static const struct got_error *
591 ref_resolve(struct got_object_id **id, struct got_repository *repo,
592 struct got_reference *ref, int recursion)
594 const struct got_error *err;
596 if (recursion <= 0)
597 return got_error_msg(GOT_ERR_RECURSION,
598 "reference recursion limit reached");
600 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
601 struct got_reference *resolved = NULL;
602 err = resolve_symbolic_ref(&resolved, repo, ref);
603 if (err == NULL)
604 err = ref_resolve(id, repo, resolved, --recursion);
605 if (resolved)
606 got_ref_close(resolved);
607 return err;
610 *id = calloc(1, sizeof(**id));
611 if (*id == NULL)
612 return got_error_from_errno("calloc");
613 memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
614 return NULL;
617 const struct got_error *
618 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
619 struct got_reference *ref)
621 return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
624 char *
625 got_ref_to_str(struct got_reference *ref)
627 char *str;
629 if (ref->flags & GOT_REF_IS_SYMBOLIC)
630 return strdup(ref->ref.symref.ref);
632 str = malloc(SHA1_DIGEST_STRING_LENGTH);
633 if (str == NULL)
634 return NULL;
636 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
637 SHA1_DIGEST_STRING_LENGTH) == NULL) {
638 free(str);
639 return NULL;
642 return str;
645 const char *
646 got_ref_get_name(struct got_reference *ref)
648 if (ref->flags & GOT_REF_IS_SYMBOLIC)
649 return ref->ref.symref.name;
651 return ref->ref.ref.name;
654 const char *
655 got_ref_get_symref_target(struct got_reference *ref)
657 if (ref->flags & GOT_REF_IS_SYMBOLIC)
658 return ref->ref.symref.ref;
660 return NULL;
663 const struct got_error *
664 got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
665 struct got_reference* re2)
667 const char *name1 = got_ref_get_name(re1);
668 const char *name2 = got_ref_get_name(re2);
670 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
671 return NULL;
674 const struct got_error *
675 got_ref_cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
676 struct got_reference *ref2)
678 const struct got_error *err = NULL;
679 struct got_repository *repo = arg;
680 struct got_object_id *id1, *id2 = NULL;
681 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
682 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
683 time_t time1, time2;
685 *cmp = 0;
687 err = got_ref_resolve(&id1, repo, ref1);
688 if (err)
689 return err;
690 err = got_object_open_as_tag(&tag1, repo, id1);
691 if (err) {
692 if (err->code != GOT_ERR_OBJ_TYPE)
693 goto done;
694 /* "lightweight" tag */
695 err = got_object_open_as_commit(&commit1, repo, id1);
696 if (err)
697 goto done;
698 time1 = got_object_commit_get_committer_time(commit1);
699 } else
700 time1 = got_object_tag_get_tagger_time(tag1);
702 err = got_ref_resolve(&id2, repo, ref2);
703 if (err)
704 goto done;
705 err = got_object_open_as_tag(&tag2, repo, id2);
706 if (err) {
707 if (err->code != GOT_ERR_OBJ_TYPE)
708 goto done;
709 /* "lightweight" tag */
710 err = got_object_open_as_commit(&commit2, repo, id2);
711 if (err)
712 goto done;
713 time2 = got_object_commit_get_committer_time(commit2);
714 } else
715 time2 = got_object_tag_get_tagger_time(tag2);
717 /* Put latest tags first. */
718 if (time1 < time2)
719 *cmp = 1;
720 else if (time1 > time2)
721 *cmp = -1;
722 else
723 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
724 done:
725 free(id1);
726 free(id2);
727 if (tag1)
728 got_object_tag_close(tag1);
729 if (tag2)
730 got_object_tag_close(tag2);
731 if (commit1)
732 got_object_commit_close(commit1);
733 if (commit2)
734 got_object_commit_close(commit2);
735 return err;
738 static const struct got_error *
739 insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
740 struct got_reference *ref, struct got_repository *repo,
741 got_ref_cmp_cb cmp_cb, void *cmp_arg)
743 const struct got_error *err;
744 struct got_object_id *id;
745 struct got_reflist_entry *new, *re, *prev = NULL;
746 int cmp;
748 *newp = NULL;
750 err = got_ref_resolve(&id, repo, ref);
751 if (err)
752 return err;
754 new = malloc(sizeof(*new));
755 if (new == NULL) {
756 free(id);
757 return got_error_from_errno("malloc");
759 new->ref = ref;
760 new->id = id;
761 *newp = new;
763 /*
764 * We must de-duplicate entries on insert because packed-refs may
765 * contain redundant entries. On-disk refs take precedence.
766 * This code assumes that on-disk revs are read before packed-refs.
767 * We're iterating the list anyway, so insert elements sorted by name.
768 */
769 re = SIMPLEQ_FIRST(refs);
770 while (re) {
771 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
772 if (err)
773 return err;
774 if (cmp == 0) {
775 /* duplicate */
776 free(new->id);
777 free(new);
778 *newp = NULL;
779 return NULL;
780 } else if (cmp > 0) {
781 if (prev)
782 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
783 else
784 SIMPLEQ_INSERT_HEAD(refs, new, entry);
785 return NULL;
786 } else {
787 prev = re;
788 re = SIMPLEQ_NEXT(re, entry);
792 SIMPLEQ_INSERT_TAIL(refs, new, entry);
793 return NULL;
796 static const struct got_error *
797 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
798 const char *subdir, struct got_repository *repo,
799 got_ref_cmp_cb cmp_cb, void *cmp_arg)
801 const struct got_error *err = NULL;
802 DIR *d = NULL;
803 char *path_subdir;
805 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
806 return got_error_from_errno("asprintf");
808 d = opendir(path_subdir);
809 if (d == NULL)
810 goto done;
812 for (;;) {
813 struct dirent *dent;
814 struct got_reference *ref;
815 char *child;
817 dent = readdir(d);
818 if (dent == NULL)
819 break;
821 if (strcmp(dent->d_name, ".") == 0 ||
822 strcmp(dent->d_name, "..") == 0)
823 continue;
825 switch (dent->d_type) {
826 case DT_REG:
827 err = open_ref(&ref, path_refs, subdir, dent->d_name,
828 0);
829 if (err)
830 goto done;
831 if (ref) {
832 struct got_reflist_entry *new;
833 err = insert_ref(&new, refs, ref, repo,
834 cmp_cb, cmp_arg);
835 if (err || new == NULL /* duplicate */)
836 got_ref_close(ref);
837 if (err)
838 goto done;
840 break;
841 case DT_DIR:
842 if (asprintf(&child, "%s%s%s", subdir,
843 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
844 err = got_error_from_errno("asprintf");
845 break;
847 err = gather_on_disk_refs(refs, path_refs, child, repo,
848 cmp_cb, cmp_arg);
849 free(child);
850 break;
851 default:
852 break;
855 done:
856 if (d)
857 closedir(d);
858 free(path_subdir);
859 return err;
862 const struct got_error *
863 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
864 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
866 const struct got_error *err;
867 char *packed_refs_path, *path_refs = NULL;
868 const char *ondisk_ref_namespace = NULL;
869 FILE *f = NULL;
870 struct got_reference *ref;
871 struct got_reflist_entry *new;
873 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
874 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
875 if (path_refs == NULL) {
876 err = got_error_from_errno("get_refs_dir_path");
877 goto done;
879 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
880 if (err)
881 goto done;
882 err = insert_ref(&new, refs, ref, repo,
883 cmp_cb, cmp_arg);
884 if (err || new == NULL /* duplicate */)
885 got_ref_close(ref);
886 if (err && err->code != GOT_ERR_NOT_REF)
887 goto done;
890 ondisk_ref_namespace = ref_namespace;
891 if (ref_namespace && strncmp(ref_namespace, "refs/", 5) == 0)
892 ondisk_ref_namespace += 5;
894 /* Gather on-disk refs before parsing packed-refs. */
895 free(path_refs);
896 path_refs = get_refs_dir_path(repo, "");
897 if (path_refs == NULL) {
898 err = got_error_from_errno("get_refs_dir_path");
899 goto done;
901 err = gather_on_disk_refs(refs, path_refs,
902 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
903 cmp_cb, cmp_arg);
904 if (err)
905 goto done;
907 /*
908 * The packed-refs file may contain redundant entries, in which
909 * case on-disk refs take precedence.
910 */
911 packed_refs_path = got_repo_get_path_packed_refs(repo);
912 if (packed_refs_path == NULL) {
913 err = got_error_from_errno("got_repo_get_path_packed_refs");
914 goto done;
917 f = fopen(packed_refs_path, "r");
918 free(packed_refs_path);
919 if (f) {
920 char *line;
921 size_t len;
922 const char delim[3] = {'\0', '\0', '\0'};
923 for (;;) {
924 line = fparseln(f, &len, NULL, delim, 0);
925 if (line == NULL) {
926 if (feof(f))
927 break;
928 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
929 goto done;
931 err = parse_packed_ref_line(&ref, NULL, line);
932 free(line);
933 if (err)
934 goto done;
935 if (ref) {
936 if (ref_namespace) {
937 const char *name;
938 name = got_ref_get_name(ref);
939 if (strncmp(name, ref_namespace,
940 strlen(ref_namespace)) != 0) {
941 got_ref_close(ref);
942 continue;
945 err = insert_ref(&new, refs, ref, repo,
946 cmp_cb, cmp_arg);
947 if (err || new == NULL /* duplicate */)
948 got_ref_close(ref);
949 if (err)
950 goto done;
954 done:
955 free(path_refs);
956 if (f && fclose(f) != 0 && err == NULL)
957 err = got_error_from_errno("fclose");
958 return err;
961 void
962 got_ref_list_free(struct got_reflist_head *refs)
964 struct got_reflist_entry *re;
966 while (!SIMPLEQ_EMPTY(refs)) {
967 re = SIMPLEQ_FIRST(refs);
968 SIMPLEQ_REMOVE_HEAD(refs, entry);
969 got_ref_close(re->ref);
970 free(re->id);
971 free(re);
976 int
977 got_ref_is_symbolic(struct got_reference *ref)
979 return (ref->flags & GOT_REF_IS_SYMBOLIC);
982 const struct got_error *
983 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
985 if (ref->flags & GOT_REF_IS_SYMBOLIC)
986 return got_error(GOT_ERR_BAD_REF_TYPE);
988 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
989 return NULL;
992 const struct got_error *
993 got_ref_change_symref(struct got_reference *ref, char *refname)
995 char *new_name;
997 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
998 return got_error(GOT_ERR_BAD_REF_TYPE);
1000 new_name = strdup(refname);
1001 if (new_name == NULL)
1002 return got_error_from_errno("strdup");
1004 free(ref->ref.symref.name);
1005 ref->ref.symref.name = new_name;
1006 return NULL;
1009 const struct got_error *
1010 got_ref_change_symref_to_ref(struct got_reference *symref,
1011 struct got_object_id *id)
1013 if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1014 return got_error(GOT_ERR_BAD_REF_TYPE);
1016 symref->ref.ref.name = symref->ref.symref.name;
1017 memcpy(symref->ref.ref.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1018 symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1019 return NULL;
1022 const struct got_error *
1023 got_ref_write(struct got_reference *ref, struct got_repository *repo)
1025 const struct got_error *err = NULL, *unlock_err = NULL;
1026 const char *name = got_ref_get_name(ref);
1027 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1028 struct got_lockfile *lf = NULL;
1029 FILE *f = NULL;
1030 size_t n;
1031 struct stat sb;
1033 path_refs = get_refs_dir_path(repo, name);
1034 if (path_refs == NULL) {
1035 err = got_error_from_errno2("get_refs_dir_path", name);
1036 goto done;
1039 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1040 err = got_error_from_errno("asprintf");
1041 goto done;
1044 err = got_opentemp_named(&tmppath, &f, path);
1045 if (err) {
1046 char *parent;
1047 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1048 goto done;
1049 err = got_path_dirname(&parent, path);
1050 if (err)
1051 goto done;
1052 err = got_path_mkdir(parent);
1053 free(parent);
1054 if (err)
1055 goto done;
1056 err = got_opentemp_named(&tmppath, &f, path);
1057 if (err)
1058 goto done;
1061 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1062 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1063 if (n != strlen(ref->ref.symref.ref) + 6) {
1064 err = got_ferror(f, GOT_ERR_IO);
1065 goto done;
1067 } else {
1068 char hex[SHA1_DIGEST_STRING_LENGTH];
1069 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
1070 sizeof(hex)) == NULL) {
1071 err = got_error(GOT_ERR_BAD_REF_DATA);
1072 goto done;
1074 n = fprintf(f, "%s\n", hex);
1075 if (n != sizeof(hex)) {
1076 err = got_ferror(f, GOT_ERR_IO);
1077 goto done;
1081 if (ref->lf == NULL) {
1082 err = got_lockfile_lock(&lf, path);
1083 if (err)
1084 goto done;
1087 /* XXX: check if old content matches our expectations? */
1089 if (stat(path, &sb) != 0) {
1090 if (errno != ENOENT) {
1091 err = got_error_from_errno2("stat", path);
1092 goto done;
1094 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1097 if (rename(tmppath, path) != 0) {
1098 err = got_error_from_errno3("rename", tmppath, path);
1099 goto done;
1101 free(tmppath);
1102 tmppath = NULL;
1104 if (chmod(path, sb.st_mode) != 0) {
1105 err = got_error_from_errno2("chmod", path);
1106 goto done;
1108 done:
1109 if (ref->lf == NULL && lf)
1110 unlock_err = got_lockfile_unlock(lf);
1111 if (f) {
1112 if (fclose(f) != 0 && err == NULL)
1113 err = got_error_from_errno("fclose");
1115 free(path_refs);
1116 free(path);
1117 if (tmppath) {
1118 if (unlink(tmppath) != 0 && err == NULL)
1119 err = got_error_from_errno2("unlink", tmppath);
1120 free(tmppath);
1122 return err ? err : unlock_err;
1125 static const struct got_error *
1126 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1128 const struct got_error *err = NULL, *unlock_err = NULL;
1129 struct got_lockfile *lf = NULL;
1130 FILE *f = NULL, *tmpf = NULL;
1131 char *packed_refs_path, *tmppath = NULL;
1132 struct got_reflist_head refs;
1133 int found_delref = 0;
1135 /* The packed-refs file does not cotain symbolic references. */
1136 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1137 return got_error(GOT_ERR_BAD_REF_DATA);
1139 SIMPLEQ_INIT(&refs);
1141 packed_refs_path = got_repo_get_path_packed_refs(repo);
1142 if (packed_refs_path == NULL)
1143 return got_error_from_errno("got_repo_get_path_packed_refs");
1145 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
1146 if (err)
1147 goto done;
1149 if (delref->lf == NULL) {
1150 err = got_lockfile_lock(&lf, packed_refs_path);
1151 if (err)
1152 goto done;
1155 f = fopen(packed_refs_path, "r");
1156 if (f == NULL) {
1157 err = got_error_from_errno2("fopen", packed_refs_path);
1158 goto done;
1160 for (;;) {
1161 char *line;
1162 size_t len;
1163 const char delim[3] = {'\0', '\0', '\0'};
1164 struct got_reference *ref;
1165 struct got_reflist_entry *new;
1167 line = fparseln(f, &len, NULL, delim, 0);
1168 if (line == NULL) {
1169 if (feof(f))
1170 break;
1171 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1172 goto done;
1174 err = parse_packed_ref_line(&ref, NULL, line);
1175 free(line);
1176 if (err)
1177 goto done;
1178 if (ref == NULL)
1179 continue;
1181 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1182 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1183 sizeof(delref->ref.ref.sha1)) == 0) {
1184 found_delref = 1;
1185 got_ref_close(ref);
1186 continue;
1189 err = insert_ref(&new, &refs, ref, repo,
1190 got_ref_cmp_by_name, NULL);
1191 if (err || new == NULL /* duplicate */)
1192 got_ref_close(ref);
1193 if (err)
1194 goto done;
1197 if (found_delref) {
1198 struct got_reflist_entry *re;
1199 size_t n;
1200 struct stat sb;
1202 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1203 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1204 err = got_ferror(f, GOT_ERR_IO);
1205 goto done;
1208 SIMPLEQ_FOREACH(re, &refs, entry) {
1209 uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
1211 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1212 sizeof(hex)) == NULL) {
1213 err = got_error(GOT_ERR_BAD_REF_DATA);
1214 goto done;
1216 n = fprintf(tmpf, "%s ", hex);
1217 if (n != sizeof(hex)) {
1218 err = got_ferror(f, GOT_ERR_IO);
1219 goto done;
1221 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1222 if (n != strlen(re->ref->ref.ref.name) + 1) {
1223 err = got_ferror(f, GOT_ERR_IO);
1224 goto done;
1228 if (fflush(tmpf) != 0) {
1229 err = got_error_from_errno("fflush");
1230 goto done;
1233 if (stat(packed_refs_path, &sb) != 0) {
1234 if (errno != ENOENT) {
1235 err = got_error_from_errno2("stat",
1236 packed_refs_path);
1237 goto done;
1239 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1242 if (rename(tmppath, packed_refs_path) != 0) {
1243 err = got_error_from_errno3("rename", tmppath,
1244 packed_refs_path);
1245 goto done;
1248 if (chmod(packed_refs_path, sb.st_mode) != 0) {
1249 err = got_error_from_errno2("chmod",
1250 packed_refs_path);
1251 goto done;
1254 done:
1255 if (delref->lf == NULL && lf)
1256 unlock_err = got_lockfile_unlock(lf);
1257 if (f) {
1258 if (fclose(f) != 0 && err == NULL)
1259 err = got_error_from_errno("fclose");
1261 if (tmpf) {
1262 unlink(tmppath);
1263 if (fclose(tmpf) != 0 && err == NULL)
1264 err = got_error_from_errno("fclose");
1266 free(tmppath);
1267 free(packed_refs_path);
1268 got_ref_list_free(&refs);
1269 return err ? err : unlock_err;
1272 const struct got_error *
1273 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1275 const struct got_error *err = NULL, *unlock_err = NULL;
1276 const char *name = got_ref_get_name(ref);
1277 char *path_refs = NULL, *path = NULL;
1278 struct got_lockfile *lf = NULL;
1280 if (ref->flags & GOT_REF_IS_PACKED)
1281 return delete_packed_ref(ref, repo);
1283 path_refs = get_refs_dir_path(repo, name);
1284 if (path_refs == NULL) {
1285 err = got_error_from_errno2("get_refs_dir_path", name);
1286 goto done;
1289 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1290 err = got_error_from_errno("asprintf");
1291 goto done;
1294 if (ref->lf == NULL) {
1295 err = got_lockfile_lock(&lf, path);
1296 if (err)
1297 goto done;
1300 /* XXX: check if old content matches our expectations? */
1302 if (unlink(path) != 0)
1303 err = got_error_from_errno2("unlink", path);
1304 done:
1305 if (ref->lf == NULL && lf)
1306 unlock_err = got_lockfile_unlock(lf);
1308 free(path_refs);
1309 free(path);
1310 return err ? err : unlock_err;
1313 const struct got_error *
1314 got_ref_unlock(struct got_reference *ref)
1316 const struct got_error *err;
1317 err = got_lockfile_unlock(ref->lf);
1318 ref->lf = NULL;
1319 return err;