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 <unistd.h>
30 #include <util.h>
31 #include <zlib.h>
32 #include <time.h>
33 #include <libgen.h>
35 #include "got_error.h"
36 #include "got_object.h"
37 #include "got_repository.h"
38 #include "got_reference.h"
39 #include "got_opentemp.h"
40 #include "got_path.h"
42 #include "got_lib_sha1.h"
43 #include "got_lib_delta.h"
44 #include "got_lib_inflate.h"
45 #include "got_lib_object.h"
46 #include "got_lib_lockfile.h"
48 #ifndef nitems
49 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
50 #endif
52 #define GOT_REF_HEADS "heads"
53 #define GOT_REF_TAGS "tags"
54 #define GOT_REF_REMOTES "remotes"
56 /*
57 * We do not resolve tags yet, and don't yet care about sorting refs either,
58 * so packed-refs files we write contain a minimal header which disables all
59 * packed-refs "traits" supported by Git.
60 */
61 #define GOT_PACKED_REFS_HEADER "# pack-refs with:"
63 /* A symbolic reference. */
64 struct got_symref {
65 char *name;
66 char *ref;
67 };
69 #define GOT_REF_RECURSE_MAX 20
71 /* A non-symbolic reference (there is no better designation). */
72 struct got_ref {
73 char *name;
74 u_int8_t sha1[SHA1_DIGEST_LENGTH];
75 };
77 /* A reference which points to an arbitrary object. */
78 struct got_reference {
79 unsigned int flags;
80 #define GOT_REF_IS_SYMBOLIC 0x01
81 #define GOT_REF_IS_PACKED 0x02
83 union {
84 struct got_ref ref;
85 struct got_symref symref;
86 } ref;
88 struct got_lockfile *lf;
89 };
91 static const struct got_error *
92 alloc_ref(struct got_reference **ref, const char *name,
93 struct got_object_id *id, int flags)
94 {
95 const struct got_error *err = NULL;
97 *ref = calloc(1, sizeof(**ref));
98 if (*ref == NULL)
99 return got_error_from_errno("calloc");
101 memcpy((*ref)->ref.ref.sha1, id->sha1, sizeof((*ref)->ref.ref.sha1));
102 (*ref)->flags = flags;
103 (*ref)->ref.ref.name = strdup(name);
104 if ((*ref)->ref.ref.name == NULL) {
105 err = got_error_from_errno("strdup");
106 got_ref_close(*ref);
107 *ref = NULL;
109 return err;
112 static const struct got_error *
113 alloc_symref(struct got_reference **ref, const char *name,
114 const char *target_ref, int flags)
116 const struct got_error *err = NULL;
118 *ref = calloc(1, sizeof(**ref));
119 if (*ref == NULL)
120 return got_error_from_errno("calloc");
122 (*ref)->flags = GOT_REF_IS_SYMBOLIC | flags;
123 (*ref)->ref.symref.name = strdup(name);
124 if ((*ref)->ref.symref.name == NULL) {
125 err = got_error_from_errno("strdup");
126 got_ref_close(*ref);
127 *ref = NULL;
128 return err;
130 (*ref)->ref.symref.ref = strdup(target_ref);
131 if ((*ref)->ref.symref.ref == NULL) {
132 err = got_error_from_errno("strdup");
133 got_ref_close(*ref);
134 *ref = NULL;
136 return err;
139 static const struct got_error *
140 parse_symref(struct got_reference **ref, const char *name, const char *line)
142 if (line[0] == '\0')
143 return got_error(GOT_ERR_BAD_REF_DATA);
145 return alloc_symref(ref, name, line, 0);
148 static const struct got_error *
149 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
151 struct got_object_id id;
153 if (strncmp(line, "ref: ", 5) == 0) {
154 line += 5;
155 return parse_symref(ref, name, line);
158 if (!got_parse_sha1_digest(id.sha1, line))
159 return got_error(GOT_ERR_BAD_REF_DATA);
161 return alloc_ref(ref, name, &id, 0);
164 static const struct got_error *
165 parse_ref_file(struct got_reference **ref, const char *name,
166 const char *absname, const char *abspath, int lock)
168 const struct got_error *err = NULL;
169 FILE *f;
170 char *line = NULL;
171 size_t linesize = 0;
172 ssize_t linelen;
173 struct got_lockfile *lf = NULL;
175 if (lock) {
176 err = got_lockfile_lock(&lf, abspath);
177 if (err) {
178 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
179 err = got_error_not_ref(name);
180 return err;
184 f = fopen(abspath, "rb");
185 if (f == NULL) {
186 if (errno != ENOTDIR && errno != ENOENT)
187 err = got_error_from_errno2("fopen", abspath);
188 else
189 err = got_error_not_ref(name);
190 if (lock)
191 got_lockfile_unlock(lf);
192 return err;
195 linelen = getline(&line, &linesize, f);
196 if (linelen == -1) {
197 if (feof(f))
198 err = NULL; /* ignore empty files (could be locks) */
199 else {
200 if (errno == EISDIR)
201 err = got_error(GOT_ERR_NOT_REF);
202 else if (ferror(f))
203 err = got_ferror(f, GOT_ERR_IO);
204 else
205 err = got_error_from_errno2("getline", abspath);
207 if (lock)
208 got_lockfile_unlock(lf);
209 goto done;
211 while (linelen > 0 && line[linelen - 1] == '\n') {
212 line[linelen - 1] = '\0';
213 linelen--;
216 err = parse_ref_line(ref, absname, line);
217 if (lock) {
218 if (err)
219 got_lockfile_unlock(lf);
220 else {
221 if (*ref)
222 (*ref)->lf = lf;
223 else
224 got_lockfile_unlock(lf);
227 done:
228 free(line);
229 if (fclose(f) != 0 && err == NULL) {
230 err = got_error_from_errno("fclose");
231 if (*ref) {
232 if (lock)
233 got_ref_unlock(*ref);
234 got_ref_close(*ref);
235 *ref = NULL;
238 return err;
241 static int
242 is_well_known_ref(const char *refname)
244 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
245 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
246 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
247 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
250 static char *
251 get_refs_dir_path(struct got_repository *repo, const char *refname)
253 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
254 return strdup(got_repo_get_path_git_dir(repo));
256 return got_repo_get_path_refs(repo);
259 static int
260 is_valid_ref_name(const char *name)
262 const char *s, *seg;
263 const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
264 const char *forbidden_seq[] = { "//", "..", "@{" };
265 const char *lfs = GOT_LOCKFILE_SUFFIX;
266 const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
267 size_t i;
269 if (name[0] == '@' && name[1] == '\0')
270 return 0;
272 s = name;
273 seg = s;
274 if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
275 return 0;
276 while (*s) {
277 for (i = 0; i < nitems(forbidden); i++) {
278 if (*s == forbidden[i])
279 return 0;
281 for (i = 0; i < nitems(forbidden_seq); i++) {
282 if (s[0] == forbidden_seq[i][0] &&
283 s[1] == forbidden_seq[i][1])
284 return 0;
286 if (iscntrl((unsigned char)s[0]))
287 return 0;
288 if (s[0] == '.' && s[1] == '\0')
289 return 0;
290 if (*s == '/') {
291 const char *nextseg = s + 1;
292 if (nextseg[0] == '\0' || nextseg[0] == '.' ||
293 nextseg[0] == '/')
294 return 0;
295 if (seg <= s - lfs_len &&
296 strncmp(s - lfs_len, lfs, lfs_len) == 0)
297 return 0;
298 seg = nextseg;
300 s++;
303 if (seg <= s - lfs_len &&
304 strncmp(s - lfs_len, lfs, lfs_len) == 0)
305 return 0;
307 return 1;
310 const struct got_error *
311 got_ref_alloc(struct got_reference **ref, const char *name,
312 struct got_object_id *id)
314 if (!is_valid_ref_name(name))
315 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
317 return alloc_ref(ref, name, id, 0);
320 const struct got_error *
321 got_ref_alloc_symref(struct got_reference **ref, const char *name,
322 struct got_reference *target_ref)
324 if (!is_valid_ref_name(name))
325 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
327 return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
330 static const struct got_error *
331 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
332 const char *line)
334 struct got_object_id id;
335 const char *name;
337 *ref = NULL;
339 if (line[0] == '#' || line[0] == '^')
340 return NULL;
342 if (!got_parse_sha1_digest(id.sha1, line))
343 return got_error(GOT_ERR_BAD_REF_DATA);
345 if (abs_refname) {
346 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
347 return NULL;
348 name = abs_refname;
349 } else
350 name = line + SHA1_DIGEST_STRING_LENGTH;
352 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED);
355 static const struct got_error *
356 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
357 int nsubdirs, const char *refname)
359 const struct got_error *err = NULL;
360 char *abs_refname;
361 char *line;
362 size_t len;
363 const char delim[3] = {'\0', '\0', '\0'};
364 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
366 *ref = NULL;
368 if (ref_is_absolute)
369 abs_refname = (char *)refname;
370 do {
371 line = fparseln(f, &len, NULL, delim, 0);
372 if (line == NULL) {
373 if (feof(f))
374 break;
375 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
376 break;
378 for (i = 0; i < nsubdirs; i++) {
379 if (!ref_is_absolute &&
380 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
381 refname) == -1)
382 return got_error_from_errno("asprintf");
383 err = parse_packed_ref_line(ref, abs_refname, line);
384 if (!ref_is_absolute)
385 free(abs_refname);
386 if (err || *ref != NULL)
387 break;
389 free(line);
390 if (err)
391 break;
392 } while (*ref == NULL);
394 return err;
397 static const struct got_error *
398 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
399 const char *name, int lock)
401 const struct got_error *err = NULL;
402 char *path = NULL;
403 char *absname = NULL;
404 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
405 int ref_is_well_known = (subdir[0] == '\0' && is_well_known_ref(name));
407 *ref = NULL;
409 if (ref_is_absolute || ref_is_well_known) {
410 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
411 return got_error_from_errno("asprintf");
412 absname = (char *)name;
413 } else {
414 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
415 subdir[0] ? "/" : "", name) == -1)
416 return got_error_from_errno("asprintf");
418 if (asprintf(&absname, "refs/%s%s%s",
419 subdir, subdir[0] ? "/" : "", name) == -1) {
420 err = got_error_from_errno("asprintf");
421 goto done;
425 err = parse_ref_file(ref, name, absname, path, lock);
426 done:
427 if (!ref_is_absolute && !ref_is_well_known)
428 free(absname);
429 free(path);
430 return err;
433 const struct got_error *
434 got_ref_open(struct got_reference **ref, struct got_repository *repo,
435 const char *refname, int lock)
437 const struct got_error *err = NULL;
438 char *path_refs = NULL;
439 const char *subdirs[] = {
440 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
441 };
442 size_t i;
443 int well_known = is_well_known_ref(refname);
444 struct got_lockfile *lf = NULL;
446 *ref = NULL;
448 path_refs = get_refs_dir_path(repo, refname);
449 if (path_refs == NULL) {
450 err = got_error_from_errno2("get_refs_dir_path", refname);
451 goto done;
454 if (well_known) {
455 err = open_ref(ref, path_refs, "", refname, lock);
456 } else {
457 char *packed_refs_path;
458 FILE *f;
460 /* Search on-disk refs before packed refs! */
461 for (i = 0; i < nitems(subdirs); i++) {
462 err = open_ref(ref, path_refs, subdirs[i], refname,
463 lock);
464 if ((err && err->code != GOT_ERR_NOT_REF) || *ref)
465 goto done;
468 packed_refs_path = got_repo_get_path_packed_refs(repo);
469 if (packed_refs_path == NULL) {
470 err = got_error_from_errno(
471 "got_repo_get_path_packed_refs");
472 goto done;
475 if (lock) {
476 err = got_lockfile_lock(&lf, packed_refs_path);
477 if (err)
478 goto done;
480 f = fopen(packed_refs_path, "rb");
481 free(packed_refs_path);
482 if (f != NULL) {
483 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
484 refname);
485 if (!err) {
486 if (fclose(f) != 0) {
487 err = got_error_from_errno("fclose");
488 got_ref_close(*ref);
489 *ref = NULL;
490 } else if (*ref)
491 (*ref)->lf = lf;
495 done:
496 if (!err && *ref == NULL)
497 err = got_error_not_ref(refname);
498 if (err && lf)
499 got_lockfile_unlock(lf);
500 free(path_refs);
501 return err;
504 void
505 got_ref_close(struct got_reference *ref)
507 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
508 free(ref->ref.symref.name);
509 free(ref->ref.symref.ref);
510 } else
511 free(ref->ref.ref.name);
512 free(ref);
515 struct got_reference *
516 got_ref_dup(struct got_reference *ref)
518 struct got_reference *ret;
520 ret = calloc(1, sizeof(*ret));
521 if (ret == NULL)
522 return NULL;
524 ret->flags = ref->flags;
525 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
526 ret->ref.symref.name = strdup(ref->ref.symref.name);
527 if (ret->ref.symref.name == NULL) {
528 free(ret);
529 return NULL;
531 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
532 if (ret->ref.symref.ref == NULL) {
533 free(ret->ref.symref.name);
534 free(ret);
535 return NULL;
537 } else {
538 ref->ref.ref.name = strdup(ref->ref.ref.name);
539 if (ref->ref.ref.name == NULL) {
540 free(ret);
541 return NULL;
543 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
544 sizeof(ret->ref.ref.sha1));
547 return ret;
550 const struct got_error *
551 got_reflist_entry_dup(struct got_reflist_entry **newp,
552 struct got_reflist_entry *re)
554 const struct got_error *err = NULL;
555 struct got_reflist_entry *new;
557 *newp = NULL;
559 new = malloc(sizeof(*new));
560 if (new == NULL)
561 return got_error_from_errno("malloc");
563 new->ref = got_ref_dup(re->ref);
564 if (new->ref == NULL) {
565 err = got_error_from_errno("got_ref_dup");
566 free(new);
567 return err;
570 *newp = new;
571 return NULL;
574 static const struct got_error *
575 resolve_symbolic_ref(struct got_reference **resolved,
576 struct got_repository *repo, struct got_reference *ref)
578 struct got_reference *nextref;
579 const struct got_error *err;
581 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
582 if (err)
583 return err;
585 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
586 err = resolve_symbolic_ref(resolved, repo, nextref);
587 else
588 *resolved = got_ref_dup(nextref);
590 got_ref_close(nextref);
591 return err;
594 static const struct got_error *
595 ref_resolve(struct got_object_id **id, struct got_repository *repo,
596 struct got_reference *ref, int recursion)
598 const struct got_error *err;
600 if (recursion <= 0)
601 return got_error_msg(GOT_ERR_RECURSION,
602 "reference recursion limit reached");
604 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
605 struct got_reference *resolved = NULL;
606 err = resolve_symbolic_ref(&resolved, repo, ref);
607 if (err == NULL)
608 err = ref_resolve(id, repo, resolved, --recursion);
609 if (resolved)
610 got_ref_close(resolved);
611 return err;
614 *id = calloc(1, sizeof(**id));
615 if (*id == NULL)
616 return got_error_from_errno("calloc");
617 memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
618 return NULL;
621 const struct got_error *
622 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
623 struct got_reference *ref)
625 return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
628 char *
629 got_ref_to_str(struct got_reference *ref)
631 char *str;
633 if (ref->flags & GOT_REF_IS_SYMBOLIC)
634 return strdup(ref->ref.symref.ref);
636 str = malloc(SHA1_DIGEST_STRING_LENGTH);
637 if (str == NULL)
638 return NULL;
640 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
641 SHA1_DIGEST_STRING_LENGTH) == NULL) {
642 free(str);
643 return NULL;
646 return str;
649 const char *
650 got_ref_get_name(struct got_reference *ref)
652 if (ref->flags & GOT_REF_IS_SYMBOLIC)
653 return ref->ref.symref.name;
655 return ref->ref.ref.name;
658 const char *
659 got_ref_get_symref_target(struct got_reference *ref)
661 if (ref->flags & GOT_REF_IS_SYMBOLIC)
662 return ref->ref.symref.ref;
664 return NULL;
667 const struct got_error *
668 got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
669 struct got_reference* re2)
671 const char *name1 = got_ref_get_name(re1);
672 const char *name2 = got_ref_get_name(re2);
674 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
675 return NULL;
678 const struct got_error *
679 got_ref_cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
680 struct got_reference *ref2)
682 const struct got_error *err = NULL;
683 struct got_repository *repo = arg;
684 struct got_object_id *id1, *id2 = NULL;
685 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
686 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
687 time_t time1, time2;
689 *cmp = 0;
691 err = got_ref_resolve(&id1, repo, ref1);
692 if (err)
693 return err;
694 err = got_object_open_as_tag(&tag1, repo, id1);
695 if (err) {
696 if (err->code != GOT_ERR_OBJ_TYPE)
697 goto done;
698 /* "lightweight" tag */
699 err = got_object_open_as_commit(&commit1, repo, id1);
700 if (err)
701 goto done;
702 time1 = got_object_commit_get_committer_time(commit1);
703 } else
704 time1 = got_object_tag_get_tagger_time(tag1);
706 err = got_ref_resolve(&id2, repo, ref2);
707 if (err)
708 goto done;
709 err = got_object_open_as_tag(&tag2, repo, id2);
710 if (err) {
711 if (err->code != GOT_ERR_OBJ_TYPE)
712 goto done;
713 /* "lightweight" tag */
714 err = got_object_open_as_commit(&commit2, repo, id2);
715 if (err)
716 goto done;
717 time2 = got_object_commit_get_committer_time(commit2);
718 } else
719 time2 = got_object_tag_get_tagger_time(tag2);
721 /* Put latest tags first. */
722 if (time1 < time2)
723 *cmp = 1;
724 else if (time1 > time2)
725 *cmp = -1;
726 else
727 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
728 done:
729 free(id1);
730 free(id2);
731 if (tag1)
732 got_object_tag_close(tag1);
733 if (tag2)
734 got_object_tag_close(tag2);
735 if (commit1)
736 got_object_commit_close(commit1);
737 if (commit2)
738 got_object_commit_close(commit2);
739 return err;
742 static const struct got_error *
743 insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
744 struct got_reference *ref, struct got_repository *repo,
745 got_ref_cmp_cb cmp_cb, void *cmp_arg)
747 const struct got_error *err;
748 struct got_reflist_entry *new, *re, *prev = NULL;
749 int cmp;
751 *newp = NULL;
753 new = malloc(sizeof(*new));
754 if (new == NULL)
755 return got_error_from_errno("malloc");
756 new->ref = ref;
757 *newp = new;
759 /*
760 * We must de-duplicate entries on insert because packed-refs may
761 * contain redundant entries. On-disk refs take precedence.
762 * This code assumes that on-disk revs are read before packed-refs.
763 * We're iterating the list anyway, so insert elements sorted by name.
764 */
765 re = SIMPLEQ_FIRST(refs);
766 while (re) {
767 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
768 if (err)
769 return err;
770 if (cmp == 0) {
771 /* duplicate */
772 free(new);
773 *newp = NULL;
774 return NULL;
775 } else if (cmp > 0) {
776 if (prev)
777 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
778 else
779 SIMPLEQ_INSERT_HEAD(refs, new, entry);
780 return NULL;
781 } else {
782 prev = re;
783 re = SIMPLEQ_NEXT(re, entry);
787 SIMPLEQ_INSERT_TAIL(refs, new, entry);
788 return NULL;
791 static const struct got_error *
792 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
793 const char *subdir, struct got_repository *repo,
794 got_ref_cmp_cb cmp_cb, void *cmp_arg)
796 const struct got_error *err = NULL;
797 DIR *d = NULL;
798 char *path_subdir;
800 while (subdir[0] == '/')
801 subdir++;
803 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
804 return got_error_from_errno("asprintf");
806 d = opendir(path_subdir);
807 if (d == NULL)
808 goto done;
810 for (;;) {
811 struct dirent *dent;
812 struct got_reference *ref;
813 char *child;
814 int type;
816 dent = readdir(d);
817 if (dent == NULL)
818 break;
820 if (strcmp(dent->d_name, ".") == 0 ||
821 strcmp(dent->d_name, "..") == 0)
822 continue;
824 err = got_path_dirent_type(&type, path_subdir, dent);
825 if (err)
826 break;
828 switch (type) {
829 case DT_REG:
830 err = open_ref(&ref, path_refs, subdir, dent->d_name,
831 0);
832 if (err)
833 goto done;
834 if (ref) {
835 struct got_reflist_entry *new;
836 err = insert_ref(&new, refs, ref, repo,
837 cmp_cb, cmp_arg);
838 if (err || new == NULL /* duplicate */)
839 got_ref_close(ref);
840 if (err)
841 goto done;
843 break;
844 case DT_DIR:
845 if (asprintf(&child, "%s%s%s", subdir,
846 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
847 err = got_error_from_errno("asprintf");
848 break;
850 err = gather_on_disk_refs(refs, path_refs, child, repo,
851 cmp_cb, cmp_arg);
852 free(child);
853 break;
854 default:
855 break;
858 done:
859 if (d)
860 closedir(d);
861 free(path_subdir);
862 return err;
865 const struct got_error *
866 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
867 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
869 const struct got_error *err;
870 char *packed_refs_path, *path_refs = NULL;
871 char *abs_namespace = NULL;
872 char *buf = NULL, *ondisk_ref_namespace = NULL;
873 FILE *f = NULL;
874 struct got_reference *ref;
875 struct got_reflist_entry *new;
877 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
878 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
879 if (path_refs == NULL) {
880 err = got_error_from_errno("get_refs_dir_path");
881 goto done;
883 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
884 if (err)
885 goto done;
886 err = insert_ref(&new, refs, ref, repo,
887 cmp_cb, cmp_arg);
888 if (err || new == NULL /* duplicate */)
889 got_ref_close(ref);
890 if (err && err->code != GOT_ERR_NOT_REF)
891 goto done;
892 } else {
893 /* Try listing a single reference. */
894 const char *refname = ref_namespace;
895 path_refs = get_refs_dir_path(repo, refname);
896 if (path_refs == NULL) {
897 err = got_error_from_errno("get_refs_dir_path");
898 goto done;
900 err = open_ref(&ref, path_refs, "", refname, 0);
901 if (err) {
902 if (err->code != GOT_ERR_NOT_REF)
903 goto done;
904 /* Try to look up references in a given namespace. */
905 } else {
906 err = insert_ref(&new, refs, ref, repo,
907 cmp_cb, cmp_arg);
908 if (err || new == NULL /* duplicate */)
909 got_ref_close(ref);
910 return err;
914 if (ref_namespace) {
915 size_t len;
916 /* Canonicalize the path to eliminate double-slashes if any. */
917 if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
918 err = got_error_from_errno("asprintf");
919 goto done;
921 len = strlen(abs_namespace) + 1;
922 buf = malloc(len);
923 if (buf == NULL) {
924 err = got_error_from_errno("malloc");
925 goto done;
927 err = got_canonpath(abs_namespace, buf, len);
928 if (err)
929 goto done;
930 ondisk_ref_namespace = buf;
931 while (ondisk_ref_namespace[0] == '/')
932 ondisk_ref_namespace++;
933 if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
934 ondisk_ref_namespace += 5;
935 else if (strcmp(ondisk_ref_namespace, "refs") == 0)
936 ondisk_ref_namespace = "";
939 /* Gather on-disk refs before parsing packed-refs. */
940 free(path_refs);
941 path_refs = get_refs_dir_path(repo, "");
942 if (path_refs == NULL) {
943 err = got_error_from_errno("get_refs_dir_path");
944 goto done;
946 err = gather_on_disk_refs(refs, path_refs,
947 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
948 cmp_cb, cmp_arg);
949 if (err)
950 goto done;
952 /*
953 * The packed-refs file may contain redundant entries, in which
954 * case on-disk refs take precedence.
955 */
956 packed_refs_path = got_repo_get_path_packed_refs(repo);
957 if (packed_refs_path == NULL) {
958 err = got_error_from_errno("got_repo_get_path_packed_refs");
959 goto done;
962 f = fopen(packed_refs_path, "r");
963 free(packed_refs_path);
964 if (f) {
965 char *line;
966 size_t len;
967 const char delim[3] = {'\0', '\0', '\0'};
968 for (;;) {
969 line = fparseln(f, &len, NULL, delim, 0);
970 if (line == NULL) {
971 if (feof(f))
972 break;
973 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
974 goto done;
976 err = parse_packed_ref_line(&ref, NULL, line);
977 free(line);
978 if (err)
979 goto done;
980 if (ref) {
981 if (ref_namespace) {
982 const char *name;
983 name = got_ref_get_name(ref);
984 if (!got_path_is_child(name,
985 ref_namespace,
986 strlen(ref_namespace))) {
987 got_ref_close(ref);
988 continue;
991 err = insert_ref(&new, refs, ref, repo,
992 cmp_cb, cmp_arg);
993 if (err || new == NULL /* duplicate */)
994 got_ref_close(ref);
995 if (err)
996 goto done;
1000 done:
1001 free(abs_namespace);
1002 free(buf);
1003 free(path_refs);
1004 if (f && fclose(f) != 0 && err == NULL)
1005 err = got_error_from_errno("fclose");
1006 return err;
1009 void
1010 got_ref_list_free(struct got_reflist_head *refs)
1012 struct got_reflist_entry *re;
1014 while (!SIMPLEQ_EMPTY(refs)) {
1015 re = SIMPLEQ_FIRST(refs);
1016 SIMPLEQ_REMOVE_HEAD(refs, entry);
1017 got_ref_close(re->ref);
1018 free(re);
1023 int
1024 got_ref_is_symbolic(struct got_reference *ref)
1026 return (ref->flags & GOT_REF_IS_SYMBOLIC);
1029 const struct got_error *
1030 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1032 if (ref->flags & GOT_REF_IS_SYMBOLIC)
1033 return got_error(GOT_ERR_BAD_REF_TYPE);
1035 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
1036 return NULL;
1039 const struct got_error *
1040 got_ref_change_symref(struct got_reference *ref, const char *refname)
1042 char *new_name;
1044 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1045 return got_error(GOT_ERR_BAD_REF_TYPE);
1047 new_name = strdup(refname);
1048 if (new_name == NULL)
1049 return got_error_from_errno("strdup");
1051 free(ref->ref.symref.ref);
1052 ref->ref.symref.ref = new_name;
1053 return NULL;
1056 const struct got_error *
1057 got_ref_change_symref_to_ref(struct got_reference *symref,
1058 struct got_object_id *id)
1060 if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1061 return got_error(GOT_ERR_BAD_REF_TYPE);
1063 symref->ref.ref.name = symref->ref.symref.name;
1064 memcpy(symref->ref.ref.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1065 symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1066 return NULL;
1069 const struct got_error *
1070 got_ref_write(struct got_reference *ref, struct got_repository *repo)
1072 const struct got_error *err = NULL, *unlock_err = NULL;
1073 const char *name = got_ref_get_name(ref);
1074 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1075 struct got_lockfile *lf = NULL;
1076 FILE *f = NULL;
1077 size_t n;
1078 struct stat sb;
1080 path_refs = get_refs_dir_path(repo, name);
1081 if (path_refs == NULL) {
1082 err = got_error_from_errno2("get_refs_dir_path", name);
1083 goto done;
1086 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1087 err = got_error_from_errno("asprintf");
1088 goto done;
1091 err = got_opentemp_named(&tmppath, &f, path);
1092 if (err) {
1093 char *parent;
1094 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1095 goto done;
1096 err = got_path_dirname(&parent, path);
1097 if (err)
1098 goto done;
1099 err = got_path_mkdir(parent);
1100 free(parent);
1101 if (err)
1102 goto done;
1103 err = got_opentemp_named(&tmppath, &f, path);
1104 if (err)
1105 goto done;
1108 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1109 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1110 if (n != strlen(ref->ref.symref.ref) + 6) {
1111 err = got_ferror(f, GOT_ERR_IO);
1112 goto done;
1114 } else {
1115 char hex[SHA1_DIGEST_STRING_LENGTH];
1116 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
1117 sizeof(hex)) == NULL) {
1118 err = got_error(GOT_ERR_BAD_REF_DATA);
1119 goto done;
1121 n = fprintf(f, "%s\n", hex);
1122 if (n != sizeof(hex)) {
1123 err = got_ferror(f, GOT_ERR_IO);
1124 goto done;
1128 if (ref->lf == NULL) {
1129 err = got_lockfile_lock(&lf, path);
1130 if (err)
1131 goto done;
1134 /* XXX: check if old content matches our expectations? */
1136 if (stat(path, &sb) != 0) {
1137 if (errno != ENOENT) {
1138 err = got_error_from_errno2("stat", path);
1139 goto done;
1141 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1144 if (fchmod(fileno(f), sb.st_mode) != 0) {
1145 err = got_error_from_errno2("fchmod", tmppath);
1146 goto done;
1149 if (rename(tmppath, path) != 0) {
1150 err = got_error_from_errno3("rename", tmppath, path);
1151 goto done;
1153 free(tmppath);
1154 tmppath = NULL;
1155 done:
1156 if (ref->lf == NULL && lf)
1157 unlock_err = got_lockfile_unlock(lf);
1158 if (f) {
1159 if (fclose(f) != 0 && err == NULL)
1160 err = got_error_from_errno("fclose");
1162 free(path_refs);
1163 free(path);
1164 if (tmppath) {
1165 if (unlink(tmppath) != 0 && err == NULL)
1166 err = got_error_from_errno2("unlink", tmppath);
1167 free(tmppath);
1169 return err ? err : unlock_err;
1172 static const struct got_error *
1173 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1175 const struct got_error *err = NULL, *unlock_err = NULL;
1176 struct got_lockfile *lf = NULL;
1177 FILE *f = NULL, *tmpf = NULL;
1178 char *packed_refs_path, *tmppath = NULL;
1179 struct got_reflist_head refs;
1180 int found_delref = 0;
1182 /* The packed-refs file does not cotain symbolic references. */
1183 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1184 return got_error(GOT_ERR_BAD_REF_DATA);
1186 SIMPLEQ_INIT(&refs);
1188 packed_refs_path = got_repo_get_path_packed_refs(repo);
1189 if (packed_refs_path == NULL)
1190 return got_error_from_errno("got_repo_get_path_packed_refs");
1192 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
1193 if (err)
1194 goto done;
1196 if (delref->lf == NULL) {
1197 err = got_lockfile_lock(&lf, packed_refs_path);
1198 if (err)
1199 goto done;
1202 f = fopen(packed_refs_path, "r");
1203 if (f == NULL) {
1204 err = got_error_from_errno2("fopen", packed_refs_path);
1205 goto done;
1207 for (;;) {
1208 char *line;
1209 size_t len;
1210 const char delim[3] = {'\0', '\0', '\0'};
1211 struct got_reference *ref;
1212 struct got_reflist_entry *new;
1214 line = fparseln(f, &len, NULL, delim, 0);
1215 if (line == NULL) {
1216 if (feof(f))
1217 break;
1218 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1219 goto done;
1221 err = parse_packed_ref_line(&ref, NULL, line);
1222 free(line);
1223 if (err)
1224 goto done;
1225 if (ref == NULL)
1226 continue;
1228 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1229 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1230 sizeof(delref->ref.ref.sha1)) == 0) {
1231 found_delref = 1;
1232 got_ref_close(ref);
1233 continue;
1236 err = insert_ref(&new, &refs, ref, repo,
1237 got_ref_cmp_by_name, NULL);
1238 if (err || new == NULL /* duplicate */)
1239 got_ref_close(ref);
1240 if (err)
1241 goto done;
1244 if (found_delref) {
1245 struct got_reflist_entry *re;
1246 size_t n;
1247 struct stat sb;
1249 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1250 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1251 err = got_ferror(f, GOT_ERR_IO);
1252 goto done;
1255 SIMPLEQ_FOREACH(re, &refs, entry) {
1256 uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
1258 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1259 sizeof(hex)) == NULL) {
1260 err = got_error(GOT_ERR_BAD_REF_DATA);
1261 goto done;
1263 n = fprintf(tmpf, "%s ", hex);
1264 if (n != sizeof(hex)) {
1265 err = got_ferror(f, GOT_ERR_IO);
1266 goto done;
1268 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1269 if (n != strlen(re->ref->ref.ref.name) + 1) {
1270 err = got_ferror(f, GOT_ERR_IO);
1271 goto done;
1275 if (fflush(tmpf) != 0) {
1276 err = got_error_from_errno("fflush");
1277 goto done;
1280 if (fstat(fileno(f), &sb) != 0) {
1281 if (errno != ENOENT) {
1282 err = got_error_from_errno2("fstat",
1283 packed_refs_path);
1284 goto done;
1286 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1289 if (fchmod(fileno(tmpf), sb.st_mode) != 0) {
1290 err = got_error_from_errno2("fchmod", tmppath);
1291 goto done;
1294 if (rename(tmppath, packed_refs_path) != 0) {
1295 err = got_error_from_errno3("rename", tmppath,
1296 packed_refs_path);
1297 goto done;
1300 done:
1301 if (delref->lf == NULL && lf)
1302 unlock_err = got_lockfile_unlock(lf);
1303 if (f) {
1304 if (fclose(f) != 0 && err == NULL)
1305 err = got_error_from_errno("fclose");
1307 if (tmpf) {
1308 unlink(tmppath);
1309 if (fclose(tmpf) != 0 && err == NULL)
1310 err = got_error_from_errno("fclose");
1312 free(tmppath);
1313 free(packed_refs_path);
1314 got_ref_list_free(&refs);
1315 return err ? err : unlock_err;
1318 static const struct got_error *
1319 delete_loose_ref(struct got_reference *ref, struct got_repository *repo)
1321 const struct got_error *err = NULL, *unlock_err = NULL;
1322 const char *name = got_ref_get_name(ref);
1323 char *path_refs = NULL, *path = NULL;
1324 struct got_lockfile *lf = NULL;
1326 path_refs = get_refs_dir_path(repo, name);
1327 if (path_refs == NULL) {
1328 err = got_error_from_errno2("get_refs_dir_path", name);
1329 goto done;
1332 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1333 err = got_error_from_errno("asprintf");
1334 goto done;
1337 if (ref->lf == NULL) {
1338 err = got_lockfile_lock(&lf, path);
1339 if (err)
1340 goto done;
1343 /* XXX: check if old content matches our expectations? */
1345 if (unlink(path) != 0)
1346 err = got_error_from_errno2("unlink", path);
1347 done:
1348 if (ref->lf == NULL && lf)
1349 unlock_err = got_lockfile_unlock(lf);
1351 free(path_refs);
1352 free(path);
1353 return err ? err : unlock_err;
1356 const struct got_error *
1357 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1359 const struct got_error *err = NULL;
1360 struct got_reference *ref2;
1362 if (ref->flags & GOT_REF_IS_PACKED) {
1363 err = delete_packed_ref(ref, repo);
1364 if (err)
1365 return err;
1367 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1368 if (err) {
1369 if (err->code == GOT_ERR_NOT_REF)
1370 return NULL;
1371 return err;
1374 err = delete_loose_ref(ref2, repo);
1375 got_ref_close(ref2);
1376 return err;
1377 } else {
1378 err = delete_loose_ref(ref, repo);
1379 if (err)
1380 return err;
1382 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1383 if (err) {
1384 if (err->code == GOT_ERR_NOT_REF)
1385 return NULL;
1386 return err;
1389 err = delete_packed_ref(ref2, repo);
1390 got_ref_close(ref2);
1391 return err;
1395 const struct got_error *
1396 got_ref_unlock(struct got_reference *ref)
1398 const struct got_error *err;
1399 err = got_lockfile_unlock(ref->lf);
1400 ref->lf = NULL;
1401 return err;