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_object_idset.h"
47 #include "got_lib_lockfile.h"
49 #ifndef nitems
50 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
51 #endif
53 #define GOT_REF_HEADS "heads"
54 #define GOT_REF_TAGS "tags"
55 #define GOT_REF_REMOTES "remotes"
57 /*
58 * We do not resolve tags yet, and don't yet care about sorting refs either,
59 * so packed-refs files we write contain a minimal header which disables all
60 * packed-refs "traits" supported by Git.
61 */
62 #define GOT_PACKED_REFS_HEADER "# pack-refs with:"
64 /* A symbolic reference. */
65 struct got_symref {
66 char *name;
67 char *ref;
68 };
70 #define GOT_REF_RECURSE_MAX 20
72 /* A non-symbolic reference (there is no better designation). */
73 struct got_ref {
74 char *name;
75 u_int8_t sha1[SHA1_DIGEST_LENGTH];
76 };
78 /* A reference which points to an arbitrary object. */
79 struct got_reference {
80 unsigned int flags;
81 #define GOT_REF_IS_SYMBOLIC 0x01
82 #define GOT_REF_IS_PACKED 0x02
84 union {
85 struct got_ref ref;
86 struct got_symref symref;
87 } ref;
89 struct got_lockfile *lf;
90 };
92 static const struct got_error *
93 alloc_ref(struct got_reference **ref, const char *name,
94 struct got_object_id *id, int flags)
95 {
96 const struct got_error *err = NULL;
98 *ref = calloc(1, sizeof(**ref));
99 if (*ref == NULL)
100 return got_error_from_errno("calloc");
102 memcpy((*ref)->ref.ref.sha1, id->sha1, sizeof((*ref)->ref.ref.sha1));
103 (*ref)->flags = flags;
104 (*ref)->ref.ref.name = strdup(name);
105 if ((*ref)->ref.ref.name == NULL) {
106 err = got_error_from_errno("strdup");
107 got_ref_close(*ref);
108 *ref = NULL;
110 return err;
113 static const struct got_error *
114 alloc_symref(struct got_reference **ref, const char *name,
115 const char *target_ref, int flags)
117 const struct got_error *err = NULL;
119 *ref = calloc(1, sizeof(**ref));
120 if (*ref == NULL)
121 return got_error_from_errno("calloc");
123 (*ref)->flags = GOT_REF_IS_SYMBOLIC | flags;
124 (*ref)->ref.symref.name = strdup(name);
125 if ((*ref)->ref.symref.name == NULL) {
126 err = got_error_from_errno("strdup");
127 got_ref_close(*ref);
128 *ref = NULL;
129 return err;
131 (*ref)->ref.symref.ref = strdup(target_ref);
132 if ((*ref)->ref.symref.ref == NULL) {
133 err = got_error_from_errno("strdup");
134 got_ref_close(*ref);
135 *ref = NULL;
137 return err;
140 static const struct got_error *
141 parse_symref(struct got_reference **ref, const char *name, const char *line)
143 if (line[0] == '\0')
144 return got_error(GOT_ERR_BAD_REF_DATA);
146 return alloc_symref(ref, name, line, 0);
149 static const struct got_error *
150 parse_ref_line(struct got_reference **ref, const char *name, const char *line)
152 struct got_object_id id;
154 if (strncmp(line, "ref: ", 5) == 0) {
155 line += 5;
156 return parse_symref(ref, name, line);
159 if (!got_parse_sha1_digest(id.sha1, line))
160 return got_error(GOT_ERR_BAD_REF_DATA);
162 return alloc_ref(ref, name, &id, 0);
165 static const struct got_error *
166 parse_ref_file(struct got_reference **ref, const char *name,
167 const char *absname, const char *abspath, int lock)
169 const struct got_error *err = NULL;
170 FILE *f;
171 char *line = NULL;
172 size_t linesize = 0;
173 ssize_t linelen;
174 struct got_lockfile *lf = NULL;
176 if (lock) {
177 err = got_lockfile_lock(&lf, abspath);
178 if (err) {
179 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
180 err = got_error_not_ref(name);
181 return err;
185 f = fopen(abspath, "rb");
186 if (f == NULL) {
187 if (errno != ENOTDIR && errno != ENOENT)
188 err = got_error_from_errno2("fopen", abspath);
189 else
190 err = got_error_not_ref(name);
191 if (lock)
192 got_lockfile_unlock(lf);
193 return err;
196 linelen = getline(&line, &linesize, f);
197 if (linelen == -1) {
198 if (feof(f))
199 err = NULL; /* ignore empty files (could be locks) */
200 else {
201 if (errno == EISDIR)
202 err = got_error(GOT_ERR_NOT_REF);
203 else if (ferror(f))
204 err = got_ferror(f, GOT_ERR_IO);
205 else
206 err = got_error_from_errno2("getline", abspath);
208 if (lock)
209 got_lockfile_unlock(lf);
210 goto done;
212 while (linelen > 0 && line[linelen - 1] == '\n') {
213 line[linelen - 1] = '\0';
214 linelen--;
217 err = parse_ref_line(ref, absname, line);
218 if (lock) {
219 if (err)
220 got_lockfile_unlock(lf);
221 else {
222 if (*ref)
223 (*ref)->lf = lf;
224 else
225 got_lockfile_unlock(lf);
228 done:
229 free(line);
230 if (fclose(f) != 0 && err == NULL) {
231 err = got_error_from_errno("fclose");
232 if (*ref) {
233 if (lock)
234 got_ref_unlock(*ref);
235 got_ref_close(*ref);
236 *ref = NULL;
239 return err;
242 static int
243 is_well_known_ref(const char *refname)
245 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
246 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
247 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
248 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
251 static char *
252 get_refs_dir_path(struct got_repository *repo, const char *refname)
254 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
255 return strdup(got_repo_get_path_git_dir(repo));
257 return got_repo_get_path_refs(repo);
260 static int
261 is_valid_ref_name(const char *name)
263 const char *s, *seg;
264 const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
265 const char *forbidden_seq[] = { "//", "..", "@{" };
266 const char *lfs = GOT_LOCKFILE_SUFFIX;
267 const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
268 size_t i;
270 if (name[0] == '@' && name[1] == '\0')
271 return 0;
273 s = name;
274 seg = s;
275 if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
276 return 0;
277 while (*s) {
278 for (i = 0; i < nitems(forbidden); i++) {
279 if (*s == forbidden[i])
280 return 0;
282 for (i = 0; i < nitems(forbidden_seq); i++) {
283 if (s[0] == forbidden_seq[i][0] &&
284 s[1] == forbidden_seq[i][1])
285 return 0;
287 if (iscntrl((unsigned char)s[0]))
288 return 0;
289 if (s[0] == '.' && s[1] == '\0')
290 return 0;
291 if (*s == '/') {
292 const char *nextseg = s + 1;
293 if (nextseg[0] == '\0' || nextseg[0] == '.' ||
294 nextseg[0] == '/')
295 return 0;
296 if (seg <= s - lfs_len &&
297 strncmp(s - lfs_len, lfs, lfs_len) == 0)
298 return 0;
299 seg = nextseg;
301 s++;
304 if (seg <= s - lfs_len &&
305 strncmp(s - lfs_len, lfs, lfs_len) == 0)
306 return 0;
308 return 1;
311 const struct got_error *
312 got_ref_alloc(struct got_reference **ref, const char *name,
313 struct got_object_id *id)
315 if (!is_valid_ref_name(name))
316 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
318 return alloc_ref(ref, name, id, 0);
321 const struct got_error *
322 got_ref_alloc_symref(struct got_reference **ref, const char *name,
323 struct got_reference *target_ref)
325 if (!is_valid_ref_name(name))
326 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
328 return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
331 static const struct got_error *
332 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
333 const char *line)
335 struct got_object_id id;
336 const char *name;
338 *ref = NULL;
340 if (line[0] == '#' || line[0] == '^')
341 return NULL;
343 if (!got_parse_sha1_digest(id.sha1, line))
344 return got_error(GOT_ERR_BAD_REF_DATA);
346 if (abs_refname) {
347 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
348 return NULL;
349 name = abs_refname;
350 } else
351 name = line + SHA1_DIGEST_STRING_LENGTH;
353 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED);
356 static const struct got_error *
357 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
358 int nsubdirs, const char *refname)
360 const struct got_error *err = NULL;
361 char *abs_refname;
362 char *line;
363 size_t len;
364 const char delim[3] = {'\0', '\0', '\0'};
365 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
367 *ref = NULL;
369 if (ref_is_absolute)
370 abs_refname = (char *)refname;
371 do {
372 line = fparseln(f, &len, NULL, delim, 0);
373 if (line == NULL) {
374 if (feof(f))
375 break;
376 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
377 break;
379 for (i = 0; i < nsubdirs; i++) {
380 if (!ref_is_absolute &&
381 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
382 refname) == -1)
383 return got_error_from_errno("asprintf");
384 err = parse_packed_ref_line(ref, abs_refname, line);
385 if (!ref_is_absolute)
386 free(abs_refname);
387 if (err || *ref != NULL)
388 break;
390 free(line);
391 if (err)
392 break;
393 } while (*ref == NULL);
395 return err;
398 static const struct got_error *
399 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
400 const char *name, int lock)
402 const struct got_error *err = NULL;
403 char *path = NULL;
404 char *absname = NULL;
405 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
406 int ref_is_well_known = (subdir[0] == '\0' && is_well_known_ref(name));
408 *ref = NULL;
410 if (ref_is_absolute || ref_is_well_known) {
411 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
412 return got_error_from_errno("asprintf");
413 absname = (char *)name;
414 } else {
415 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
416 subdir[0] ? "/" : "", name) == -1)
417 return got_error_from_errno("asprintf");
419 if (asprintf(&absname, "refs/%s%s%s",
420 subdir, subdir[0] ? "/" : "", name) == -1) {
421 err = got_error_from_errno("asprintf");
422 goto done;
426 err = parse_ref_file(ref, name, absname, path, lock);
427 done:
428 if (!ref_is_absolute && !ref_is_well_known)
429 free(absname);
430 free(path);
431 return err;
434 const struct got_error *
435 got_ref_open(struct got_reference **ref, struct got_repository *repo,
436 const char *refname, int lock)
438 const struct got_error *err = NULL;
439 char *path_refs = NULL;
440 const char *subdirs[] = {
441 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
442 };
443 size_t i;
444 int well_known = is_well_known_ref(refname);
445 struct got_lockfile *lf = NULL;
447 *ref = NULL;
449 path_refs = get_refs_dir_path(repo, refname);
450 if (path_refs == NULL) {
451 err = got_error_from_errno2("get_refs_dir_path", refname);
452 goto done;
455 if (well_known) {
456 err = open_ref(ref, path_refs, "", refname, lock);
457 } else {
458 char *packed_refs_path;
459 FILE *f;
461 /* Search on-disk refs before packed refs! */
462 for (i = 0; i < nitems(subdirs); i++) {
463 err = open_ref(ref, path_refs, subdirs[i], refname,
464 lock);
465 if ((err && err->code != GOT_ERR_NOT_REF) || *ref)
466 goto done;
469 packed_refs_path = got_repo_get_path_packed_refs(repo);
470 if (packed_refs_path == NULL) {
471 err = got_error_from_errno(
472 "got_repo_get_path_packed_refs");
473 goto done;
476 if (lock) {
477 err = got_lockfile_lock(&lf, packed_refs_path);
478 if (err)
479 goto done;
481 f = fopen(packed_refs_path, "rb");
482 free(packed_refs_path);
483 if (f != NULL) {
484 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
485 refname);
486 if (!err) {
487 if (fclose(f) != 0) {
488 err = got_error_from_errno("fclose");
489 got_ref_close(*ref);
490 *ref = NULL;
491 } else if (*ref)
492 (*ref)->lf = lf;
496 done:
497 if (!err && *ref == NULL)
498 err = got_error_not_ref(refname);
499 if (err && lf)
500 got_lockfile_unlock(lf);
501 free(path_refs);
502 return err;
505 void
506 got_ref_close(struct got_reference *ref)
508 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
509 free(ref->ref.symref.name);
510 free(ref->ref.symref.ref);
511 } else
512 free(ref->ref.ref.name);
513 free(ref);
516 struct got_reference *
517 got_ref_dup(struct got_reference *ref)
519 struct got_reference *ret;
521 ret = calloc(1, sizeof(*ret));
522 if (ret == NULL)
523 return NULL;
525 ret->flags = ref->flags;
526 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
527 ret->ref.symref.name = strdup(ref->ref.symref.name);
528 if (ret->ref.symref.name == NULL) {
529 free(ret);
530 return NULL;
532 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
533 if (ret->ref.symref.ref == NULL) {
534 free(ret->ref.symref.name);
535 free(ret);
536 return NULL;
538 } else {
539 ret->ref.ref.name = strdup(ref->ref.ref.name);
540 if (ret->ref.ref.name == NULL) {
541 free(ret);
542 return NULL;
544 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
545 sizeof(ret->ref.ref.sha1));
548 return ret;
551 const struct got_error *
552 got_reflist_entry_dup(struct got_reflist_entry **newp,
553 struct got_reflist_entry *re)
555 const struct got_error *err = NULL;
556 struct got_reflist_entry *new;
558 *newp = NULL;
560 new = malloc(sizeof(*new));
561 if (new == NULL)
562 return got_error_from_errno("malloc");
564 new->ref = got_ref_dup(re->ref);
565 if (new->ref == NULL) {
566 err = got_error_from_errno("got_ref_dup");
567 free(new);
568 return err;
571 *newp = new;
572 return NULL;
575 static const struct got_error *
576 resolve_symbolic_ref(struct got_reference **resolved,
577 struct got_repository *repo, struct got_reference *ref)
579 struct got_reference *nextref;
580 const struct got_error *err;
582 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
583 if (err)
584 return err;
586 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
587 err = resolve_symbolic_ref(resolved, repo, nextref);
588 else
589 *resolved = got_ref_dup(nextref);
591 got_ref_close(nextref);
592 return err;
595 static const struct got_error *
596 ref_resolve(struct got_object_id **id, struct got_repository *repo,
597 struct got_reference *ref, int recursion)
599 const struct got_error *err;
601 if (recursion <= 0)
602 return got_error_msg(GOT_ERR_RECURSION,
603 "reference recursion limit reached");
605 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
606 struct got_reference *resolved = NULL;
607 err = resolve_symbolic_ref(&resolved, repo, ref);
608 if (err == NULL)
609 err = ref_resolve(id, repo, resolved, --recursion);
610 if (resolved)
611 got_ref_close(resolved);
612 return err;
615 *id = calloc(1, sizeof(**id));
616 if (*id == NULL)
617 return got_error_from_errno("calloc");
618 memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
619 return NULL;
622 const struct got_error *
623 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
624 struct got_reference *ref)
626 return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
629 char *
630 got_ref_to_str(struct got_reference *ref)
632 char *str;
634 if (ref->flags & GOT_REF_IS_SYMBOLIC)
635 return strdup(ref->ref.symref.ref);
637 str = malloc(SHA1_DIGEST_STRING_LENGTH);
638 if (str == NULL)
639 return NULL;
641 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
642 SHA1_DIGEST_STRING_LENGTH) == NULL) {
643 free(str);
644 return NULL;
647 return str;
650 const char *
651 got_ref_get_name(struct got_reference *ref)
653 if (ref->flags & GOT_REF_IS_SYMBOLIC)
654 return ref->ref.symref.name;
656 return ref->ref.ref.name;
659 const char *
660 got_ref_get_symref_target(struct got_reference *ref)
662 if (ref->flags & GOT_REF_IS_SYMBOLIC)
663 return ref->ref.symref.ref;
665 return NULL;
668 const struct got_error *
669 got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
670 struct got_reference* re2)
672 const char *name1 = got_ref_get_name(re1);
673 const char *name2 = got_ref_get_name(re2);
675 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
676 return NULL;
679 const struct got_error *
680 got_ref_cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
681 struct got_reference *ref2)
683 const struct got_error *err = NULL;
684 struct got_repository *repo = arg;
685 struct got_object_id *id1, *id2 = NULL;
686 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
687 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
688 time_t time1, time2;
690 *cmp = 0;
692 err = got_ref_resolve(&id1, repo, ref1);
693 if (err)
694 return err;
695 err = got_object_open_as_tag(&tag1, repo, id1);
696 if (err) {
697 if (err->code != GOT_ERR_OBJ_TYPE)
698 goto done;
699 /* "lightweight" tag */
700 err = got_object_open_as_commit(&commit1, repo, id1);
701 if (err)
702 goto done;
703 time1 = got_object_commit_get_committer_time(commit1);
704 } else
705 time1 = got_object_tag_get_tagger_time(tag1);
707 err = got_ref_resolve(&id2, repo, ref2);
708 if (err)
709 goto done;
710 err = got_object_open_as_tag(&tag2, repo, id2);
711 if (err) {
712 if (err->code != GOT_ERR_OBJ_TYPE)
713 goto done;
714 /* "lightweight" tag */
715 err = got_object_open_as_commit(&commit2, repo, id2);
716 if (err)
717 goto done;
718 time2 = got_object_commit_get_committer_time(commit2);
719 } else
720 time2 = got_object_tag_get_tagger_time(tag2);
722 /* Put latest tags first. */
723 if (time1 < time2)
724 *cmp = 1;
725 else if (time1 > time2)
726 *cmp = -1;
727 else
728 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
729 done:
730 free(id1);
731 free(id2);
732 if (tag1)
733 got_object_tag_close(tag1);
734 if (tag2)
735 got_object_tag_close(tag2);
736 if (commit1)
737 got_object_commit_close(commit1);
738 if (commit2)
739 got_object_commit_close(commit2);
740 return err;
743 static const struct got_error *
744 insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
745 struct got_reference *ref, struct got_repository *repo,
746 got_ref_cmp_cb cmp_cb, void *cmp_arg)
748 const struct got_error *err;
749 struct got_reflist_entry *new, *re, *prev = NULL;
750 int cmp;
752 *newp = NULL;
754 new = malloc(sizeof(*new));
755 if (new == NULL)
756 return got_error_from_errno("malloc");
757 new->ref = ref;
758 *newp = new;
760 /*
761 * We must de-duplicate entries on insert because packed-refs may
762 * contain redundant entries. On-disk refs take precedence.
763 * This code assumes that on-disk revs are read before packed-refs.
764 * We're iterating the list anyway, so insert elements sorted by name.
765 */
766 re = SIMPLEQ_FIRST(refs);
767 while (re) {
768 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
769 if (err)
770 return err;
771 if (cmp == 0) {
772 /* duplicate */
773 free(new);
774 *newp = NULL;
775 return NULL;
776 } else if (cmp > 0) {
777 if (prev)
778 SIMPLEQ_INSERT_AFTER(refs, prev, new, entry);
779 else
780 SIMPLEQ_INSERT_HEAD(refs, new, entry);
781 return NULL;
782 } else {
783 prev = re;
784 re = SIMPLEQ_NEXT(re, entry);
788 SIMPLEQ_INSERT_TAIL(refs, new, entry);
789 return NULL;
792 static const struct got_error *
793 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
794 const char *subdir, struct got_repository *repo,
795 got_ref_cmp_cb cmp_cb, void *cmp_arg)
797 const struct got_error *err = NULL;
798 DIR *d = NULL;
799 char *path_subdir;
801 while (subdir[0] == '/')
802 subdir++;
804 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
805 return got_error_from_errno("asprintf");
807 d = opendir(path_subdir);
808 if (d == NULL)
809 goto done;
811 for (;;) {
812 struct dirent *dent;
813 struct got_reference *ref;
814 char *child;
815 int type;
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 err = got_path_dirent_type(&type, path_subdir, dent);
826 if (err)
827 break;
829 switch (type) {
830 case DT_REG:
831 err = open_ref(&ref, path_refs, subdir, dent->d_name,
832 0);
833 if (err)
834 goto done;
835 if (ref) {
836 struct got_reflist_entry *new;
837 err = insert_ref(&new, refs, ref, repo,
838 cmp_cb, cmp_arg);
839 if (err || new == NULL /* duplicate */)
840 got_ref_close(ref);
841 if (err)
842 goto done;
844 break;
845 case DT_DIR:
846 if (asprintf(&child, "%s%s%s", subdir,
847 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
848 err = got_error_from_errno("asprintf");
849 break;
851 err = gather_on_disk_refs(refs, path_refs, child, repo,
852 cmp_cb, cmp_arg);
853 free(child);
854 break;
855 default:
856 break;
859 done:
860 if (d)
861 closedir(d);
862 free(path_subdir);
863 return err;
866 const struct got_error *
867 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
868 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
870 const struct got_error *err;
871 char *packed_refs_path, *path_refs = NULL;
872 char *abs_namespace = NULL;
873 char *buf = NULL, *ondisk_ref_namespace = NULL;
874 FILE *f = NULL;
875 struct got_reference *ref;
876 struct got_reflist_entry *new;
878 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
879 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
880 if (path_refs == NULL) {
881 err = got_error_from_errno("get_refs_dir_path");
882 goto done;
884 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
885 if (err)
886 goto done;
887 err = insert_ref(&new, refs, ref, repo,
888 cmp_cb, cmp_arg);
889 if (err || new == NULL /* duplicate */)
890 got_ref_close(ref);
891 if (err && err->code != GOT_ERR_NOT_REF)
892 goto done;
893 } else {
894 /* Try listing a single reference. */
895 const char *refname = ref_namespace;
896 path_refs = get_refs_dir_path(repo, refname);
897 if (path_refs == NULL) {
898 err = got_error_from_errno("get_refs_dir_path");
899 goto done;
901 err = open_ref(&ref, path_refs, "", refname, 0);
902 if (err) {
903 if (err->code != GOT_ERR_NOT_REF)
904 goto done;
905 /* Try to look up references in a given namespace. */
906 } else {
907 err = insert_ref(&new, refs, ref, repo,
908 cmp_cb, cmp_arg);
909 if (err || new == NULL /* duplicate */)
910 got_ref_close(ref);
911 return err;
915 if (ref_namespace) {
916 size_t len;
917 /* Canonicalize the path to eliminate double-slashes if any. */
918 if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
919 err = got_error_from_errno("asprintf");
920 goto done;
922 len = strlen(abs_namespace) + 1;
923 buf = malloc(len);
924 if (buf == NULL) {
925 err = got_error_from_errno("malloc");
926 goto done;
928 err = got_canonpath(abs_namespace, buf, len);
929 if (err)
930 goto done;
931 ondisk_ref_namespace = buf;
932 while (ondisk_ref_namespace[0] == '/')
933 ondisk_ref_namespace++;
934 if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
935 ondisk_ref_namespace += 5;
936 else if (strcmp(ondisk_ref_namespace, "refs") == 0)
937 ondisk_ref_namespace = "";
940 /* Gather on-disk refs before parsing packed-refs. */
941 free(path_refs);
942 path_refs = get_refs_dir_path(repo, "");
943 if (path_refs == NULL) {
944 err = got_error_from_errno("get_refs_dir_path");
945 goto done;
947 err = gather_on_disk_refs(refs, path_refs,
948 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
949 cmp_cb, cmp_arg);
950 if (err)
951 goto done;
953 /*
954 * The packed-refs file may contain redundant entries, in which
955 * case on-disk refs take precedence.
956 */
957 packed_refs_path = got_repo_get_path_packed_refs(repo);
958 if (packed_refs_path == NULL) {
959 err = got_error_from_errno("got_repo_get_path_packed_refs");
960 goto done;
963 f = fopen(packed_refs_path, "r");
964 free(packed_refs_path);
965 if (f) {
966 char *line;
967 size_t len;
968 const char delim[3] = {'\0', '\0', '\0'};
969 for (;;) {
970 line = fparseln(f, &len, NULL, delim, 0);
971 if (line == NULL) {
972 if (feof(f))
973 break;
974 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
975 goto done;
977 err = parse_packed_ref_line(&ref, NULL, line);
978 free(line);
979 if (err)
980 goto done;
981 if (ref) {
982 if (ref_namespace) {
983 const char *name;
984 name = got_ref_get_name(ref);
985 if (!got_path_is_child(name,
986 ref_namespace,
987 strlen(ref_namespace))) {
988 got_ref_close(ref);
989 continue;
992 err = insert_ref(&new, refs, ref, repo,
993 cmp_cb, cmp_arg);
994 if (err || new == NULL /* duplicate */)
995 got_ref_close(ref);
996 if (err)
997 goto done;
1001 done:
1002 free(abs_namespace);
1003 free(buf);
1004 free(path_refs);
1005 if (f && fclose(f) != 0 && err == NULL)
1006 err = got_error_from_errno("fclose");
1007 return err;
1010 void
1011 got_ref_list_free(struct got_reflist_head *refs)
1013 struct got_reflist_entry *re;
1015 while (!SIMPLEQ_EMPTY(refs)) {
1016 re = SIMPLEQ_FIRST(refs);
1017 SIMPLEQ_REMOVE_HEAD(refs, entry);
1018 got_ref_close(re->ref);
1019 free(re);
1024 int
1025 got_ref_is_symbolic(struct got_reference *ref)
1027 return (ref->flags & GOT_REF_IS_SYMBOLIC);
1030 const struct got_error *
1031 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1033 if (ref->flags & GOT_REF_IS_SYMBOLIC)
1034 return got_error(GOT_ERR_BAD_REF_TYPE);
1036 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
1037 return NULL;
1040 const struct got_error *
1041 got_ref_change_symref(struct got_reference *ref, const char *refname)
1043 char *new_name;
1045 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1046 return got_error(GOT_ERR_BAD_REF_TYPE);
1048 new_name = strdup(refname);
1049 if (new_name == NULL)
1050 return got_error_from_errno("strdup");
1052 free(ref->ref.symref.ref);
1053 ref->ref.symref.ref = new_name;
1054 return NULL;
1057 const struct got_error *
1058 got_ref_change_symref_to_ref(struct got_reference *symref,
1059 struct got_object_id *id)
1061 if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1062 return got_error(GOT_ERR_BAD_REF_TYPE);
1064 symref->ref.ref.name = symref->ref.symref.name;
1065 memcpy(symref->ref.ref.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1066 symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1067 return NULL;
1070 const struct got_error *
1071 got_ref_write(struct got_reference *ref, struct got_repository *repo)
1073 const struct got_error *err = NULL, *unlock_err = NULL;
1074 const char *name = got_ref_get_name(ref);
1075 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1076 struct got_lockfile *lf = NULL;
1077 FILE *f = NULL;
1078 size_t n;
1079 struct stat sb;
1081 path_refs = get_refs_dir_path(repo, name);
1082 if (path_refs == NULL) {
1083 err = got_error_from_errno2("get_refs_dir_path", name);
1084 goto done;
1087 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1088 err = got_error_from_errno("asprintf");
1089 goto done;
1092 err = got_opentemp_named(&tmppath, &f, path);
1093 if (err) {
1094 char *parent;
1095 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1096 goto done;
1097 err = got_path_dirname(&parent, path);
1098 if (err)
1099 goto done;
1100 err = got_path_mkdir(parent);
1101 free(parent);
1102 if (err)
1103 goto done;
1104 err = got_opentemp_named(&tmppath, &f, path);
1105 if (err)
1106 goto done;
1109 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1110 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1111 if (n != strlen(ref->ref.symref.ref) + 6) {
1112 err = got_ferror(f, GOT_ERR_IO);
1113 goto done;
1115 } else {
1116 char hex[SHA1_DIGEST_STRING_LENGTH];
1117 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
1118 sizeof(hex)) == NULL) {
1119 err = got_error(GOT_ERR_BAD_REF_DATA);
1120 goto done;
1122 n = fprintf(f, "%s\n", hex);
1123 if (n != sizeof(hex)) {
1124 err = got_ferror(f, GOT_ERR_IO);
1125 goto done;
1129 if (ref->lf == NULL) {
1130 err = got_lockfile_lock(&lf, path);
1131 if (err)
1132 goto done;
1135 /* XXX: check if old content matches our expectations? */
1137 if (stat(path, &sb) != 0) {
1138 if (errno != ENOENT) {
1139 err = got_error_from_errno2("stat", path);
1140 goto done;
1142 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1145 if (fchmod(fileno(f), sb.st_mode) != 0) {
1146 err = got_error_from_errno2("fchmod", tmppath);
1147 goto done;
1150 if (rename(tmppath, path) != 0) {
1151 err = got_error_from_errno3("rename", tmppath, path);
1152 goto done;
1154 free(tmppath);
1155 tmppath = NULL;
1156 done:
1157 if (ref->lf == NULL && lf)
1158 unlock_err = got_lockfile_unlock(lf);
1159 if (f) {
1160 if (fclose(f) != 0 && err == NULL)
1161 err = got_error_from_errno("fclose");
1163 free(path_refs);
1164 free(path);
1165 if (tmppath) {
1166 if (unlink(tmppath) != 0 && err == NULL)
1167 err = got_error_from_errno2("unlink", tmppath);
1168 free(tmppath);
1170 return err ? err : unlock_err;
1173 static const struct got_error *
1174 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1176 const struct got_error *err = NULL, *unlock_err = NULL;
1177 struct got_lockfile *lf = NULL;
1178 FILE *f = NULL, *tmpf = NULL;
1179 char *packed_refs_path, *tmppath = NULL;
1180 struct got_reflist_head refs;
1181 int found_delref = 0;
1183 /* The packed-refs file does not cotain symbolic references. */
1184 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1185 return got_error(GOT_ERR_BAD_REF_DATA);
1187 SIMPLEQ_INIT(&refs);
1189 packed_refs_path = got_repo_get_path_packed_refs(repo);
1190 if (packed_refs_path == NULL)
1191 return got_error_from_errno("got_repo_get_path_packed_refs");
1193 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
1194 if (err)
1195 goto done;
1197 if (delref->lf == NULL) {
1198 err = got_lockfile_lock(&lf, packed_refs_path);
1199 if (err)
1200 goto done;
1203 f = fopen(packed_refs_path, "r");
1204 if (f == NULL) {
1205 err = got_error_from_errno2("fopen", packed_refs_path);
1206 goto done;
1208 for (;;) {
1209 char *line;
1210 size_t len;
1211 const char delim[3] = {'\0', '\0', '\0'};
1212 struct got_reference *ref;
1213 struct got_reflist_entry *new;
1215 line = fparseln(f, &len, NULL, delim, 0);
1216 if (line == NULL) {
1217 if (feof(f))
1218 break;
1219 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1220 goto done;
1222 err = parse_packed_ref_line(&ref, NULL, line);
1223 free(line);
1224 if (err)
1225 goto done;
1226 if (ref == NULL)
1227 continue;
1229 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1230 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1231 sizeof(delref->ref.ref.sha1)) == 0) {
1232 found_delref = 1;
1233 got_ref_close(ref);
1234 continue;
1237 err = insert_ref(&new, &refs, ref, repo,
1238 got_ref_cmp_by_name, NULL);
1239 if (err || new == NULL /* duplicate */)
1240 got_ref_close(ref);
1241 if (err)
1242 goto done;
1245 if (found_delref) {
1246 struct got_reflist_entry *re;
1247 size_t n;
1248 struct stat sb;
1250 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1251 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1252 err = got_ferror(f, GOT_ERR_IO);
1253 goto done;
1256 SIMPLEQ_FOREACH(re, &refs, entry) {
1257 uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
1259 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1260 sizeof(hex)) == NULL) {
1261 err = got_error(GOT_ERR_BAD_REF_DATA);
1262 goto done;
1264 n = fprintf(tmpf, "%s ", hex);
1265 if (n != sizeof(hex)) {
1266 err = got_ferror(f, GOT_ERR_IO);
1267 goto done;
1269 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1270 if (n != strlen(re->ref->ref.ref.name) + 1) {
1271 err = got_ferror(f, GOT_ERR_IO);
1272 goto done;
1276 if (fflush(tmpf) != 0) {
1277 err = got_error_from_errno("fflush");
1278 goto done;
1281 if (fstat(fileno(f), &sb) != 0) {
1282 if (errno != ENOENT) {
1283 err = got_error_from_errno2("fstat",
1284 packed_refs_path);
1285 goto done;
1287 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1290 if (fchmod(fileno(tmpf), sb.st_mode) != 0) {
1291 err = got_error_from_errno2("fchmod", tmppath);
1292 goto done;
1295 if (rename(tmppath, packed_refs_path) != 0) {
1296 err = got_error_from_errno3("rename", tmppath,
1297 packed_refs_path);
1298 goto done;
1301 done:
1302 if (delref->lf == NULL && lf)
1303 unlock_err = got_lockfile_unlock(lf);
1304 if (f) {
1305 if (fclose(f) != 0 && err == NULL)
1306 err = got_error_from_errno("fclose");
1308 if (tmpf) {
1309 unlink(tmppath);
1310 if (fclose(tmpf) != 0 && err == NULL)
1311 err = got_error_from_errno("fclose");
1313 free(tmppath);
1314 free(packed_refs_path);
1315 got_ref_list_free(&refs);
1316 return err ? err : unlock_err;
1319 static const struct got_error *
1320 delete_loose_ref(struct got_reference *ref, struct got_repository *repo)
1322 const struct got_error *err = NULL, *unlock_err = NULL;
1323 const char *name = got_ref_get_name(ref);
1324 char *path_refs = NULL, *path = NULL;
1325 struct got_lockfile *lf = NULL;
1327 path_refs = get_refs_dir_path(repo, name);
1328 if (path_refs == NULL) {
1329 err = got_error_from_errno2("get_refs_dir_path", name);
1330 goto done;
1333 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1334 err = got_error_from_errno("asprintf");
1335 goto done;
1338 if (ref->lf == NULL) {
1339 err = got_lockfile_lock(&lf, path);
1340 if (err)
1341 goto done;
1344 /* XXX: check if old content matches our expectations? */
1346 if (unlink(path) != 0)
1347 err = got_error_from_errno2("unlink", path);
1348 done:
1349 if (ref->lf == NULL && lf)
1350 unlock_err = got_lockfile_unlock(lf);
1352 free(path_refs);
1353 free(path);
1354 return err ? err : unlock_err;
1357 const struct got_error *
1358 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1360 const struct got_error *err = NULL;
1361 struct got_reference *ref2;
1363 if (ref->flags & GOT_REF_IS_PACKED) {
1364 err = delete_packed_ref(ref, repo);
1365 if (err)
1366 return err;
1368 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1369 if (err) {
1370 if (err->code == GOT_ERR_NOT_REF)
1371 return NULL;
1372 return err;
1375 err = delete_loose_ref(ref2, repo);
1376 got_ref_close(ref2);
1377 return err;
1378 } else {
1379 err = delete_loose_ref(ref, repo);
1380 if (err)
1381 return err;
1383 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1384 if (err) {
1385 if (err->code == GOT_ERR_NOT_REF)
1386 return NULL;
1387 return err;
1390 err = delete_packed_ref(ref2, repo);
1391 got_ref_close(ref2);
1392 return err;
1396 const struct got_error *
1397 got_ref_unlock(struct got_reference *ref)
1399 const struct got_error *err;
1400 err = got_lockfile_unlock(ref->lf);
1401 ref->lf = NULL;
1402 return err;
1405 struct got_reflist_object_id_map {
1406 struct got_object_idset *idset;
1409 struct got_reflist_object_id_map_entry {
1410 struct got_reflist_head refs;
1413 const struct got_error *
1414 got_reflist_object_id_map_create(struct got_reflist_object_id_map **map,
1415 struct got_reflist_head *refs, struct got_repository *repo)
1417 const struct got_error *err = NULL;
1418 struct got_object_idset *idset;
1419 struct got_object_id *id = NULL;
1420 struct got_reflist_entry *re;
1422 idset = got_object_idset_alloc();
1423 if (idset == NULL)
1424 return got_error_from_errno("got_object_idset_alloc");
1426 *map = malloc(sizeof(**map));
1427 if (*map == NULL) {
1428 got_object_idset_free(idset);
1429 return got_error_from_errno("malloc");
1431 (*map)->idset = idset;
1433 SIMPLEQ_FOREACH(re, refs, entry) {
1434 struct got_reflist_entry *new;
1435 struct got_reflist_object_id_map_entry *ent;
1437 err = got_ref_resolve(&id, repo, re->ref);
1438 if (err)
1439 goto done;
1441 ent = got_object_idset_get(idset, id);
1442 if (ent == NULL) {
1443 ent = malloc(sizeof(*ent));
1444 if (ent == NULL) {
1445 err = got_error_from_errno("malloc");
1446 goto done;
1448 SIMPLEQ_INIT(&ent->refs);
1449 err = got_object_idset_add(idset, id, ent);
1450 if (err)
1451 goto done;
1454 err = got_reflist_entry_dup(&new, re);
1455 if (err)
1456 goto done;
1457 SIMPLEQ_INSERT_TAIL(&ent->refs, new, entry);
1458 free(id);
1459 id = NULL;
1461 done:
1462 free(id);
1463 if (err) {
1464 got_reflist_object_map_free(*map);
1465 *map = NULL;
1467 return NULL;
1470 struct got_reflist_head *
1471 got_reflist_object_id_map_lookup(struct got_reflist_object_id_map *map,
1472 struct got_object_id *id)
1474 struct got_reflist_object_id_map_entry *ent;
1475 ent = got_object_idset_get(map->idset, id);
1476 if (ent)
1477 return &ent->refs;
1478 return NULL;
1481 static const struct got_error *
1482 free_id_map_entry(struct got_object_id *id, void *data, void *arg)
1484 struct got_reflist_object_id_map_entry *ent = data;
1486 got_ref_list_free(&ent->refs);
1487 free(ent);
1488 return NULL;
1491 void
1492 got_reflist_object_map_free(struct got_reflist_object_id_map *map)
1494 got_object_idset_for_each(map->idset, free_id_map_entry, NULL);
1495 got_object_idset_free(map->idset);
1496 free(map);