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) == EOF && 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 = NULL;
363 size_t linesize = 0;
364 ssize_t linelen;
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 linelen = getline(&line, &linesize, f);
373 if (linelen == -1) {
374 if (feof(f))
375 break;
376 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
377 break;
379 if (linelen > 0 && line[linelen - 1] == '\n')
380 line[linelen - 1] = '\0';
381 for (i = 0; i < nsubdirs; i++) {
382 if (!ref_is_absolute &&
383 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
384 refname) == -1)
385 return got_error_from_errno("asprintf");
386 err = parse_packed_ref_line(ref, abs_refname, line);
387 if (!ref_is_absolute)
388 free(abs_refname);
389 if (err || *ref != NULL)
390 break;
392 if (err)
393 break;
394 } while (*ref == NULL);
395 free(line);
397 return err;
400 static const struct got_error *
401 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
402 const char *name, int lock)
404 const struct got_error *err = NULL;
405 char *path = NULL;
406 char *absname = NULL;
407 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
408 int ref_is_well_known = (subdir[0] == '\0' && is_well_known_ref(name));
410 *ref = NULL;
412 if (ref_is_absolute || ref_is_well_known) {
413 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
414 return got_error_from_errno("asprintf");
415 absname = (char *)name;
416 } else {
417 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
418 subdir[0] ? "/" : "", name) == -1)
419 return got_error_from_errno("asprintf");
421 if (asprintf(&absname, "refs/%s%s%s",
422 subdir, subdir[0] ? "/" : "", name) == -1) {
423 err = got_error_from_errno("asprintf");
424 goto done;
428 err = parse_ref_file(ref, name, absname, path, lock);
429 done:
430 if (!ref_is_absolute && !ref_is_well_known)
431 free(absname);
432 free(path);
433 return err;
436 const struct got_error *
437 got_ref_open(struct got_reference **ref, struct got_repository *repo,
438 const char *refname, int lock)
440 const struct got_error *err = NULL;
441 char *path_refs = NULL;
442 const char *subdirs[] = {
443 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
444 };
445 size_t i;
446 int well_known = is_well_known_ref(refname);
447 struct got_lockfile *lf = NULL;
449 *ref = NULL;
451 path_refs = get_refs_dir_path(repo, refname);
452 if (path_refs == NULL) {
453 err = got_error_from_errno2("get_refs_dir_path", refname);
454 goto done;
457 if (well_known) {
458 err = open_ref(ref, path_refs, "", refname, lock);
459 } else {
460 char *packed_refs_path;
461 FILE *f;
463 /* Search on-disk refs before packed refs! */
464 for (i = 0; i < nitems(subdirs); i++) {
465 err = open_ref(ref, path_refs, subdirs[i], refname,
466 lock);
467 if ((err && err->code != GOT_ERR_NOT_REF) || *ref)
468 goto done;
471 packed_refs_path = got_repo_get_path_packed_refs(repo);
472 if (packed_refs_path == NULL) {
473 err = got_error_from_errno(
474 "got_repo_get_path_packed_refs");
475 goto done;
478 if (lock) {
479 err = got_lockfile_lock(&lf, packed_refs_path);
480 if (err)
481 goto done;
483 f = fopen(packed_refs_path, "rb");
484 free(packed_refs_path);
485 if (f != NULL) {
486 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
487 refname);
488 if (!err) {
489 if (fclose(f) == EOF) {
490 err = got_error_from_errno("fclose");
491 got_ref_close(*ref);
492 *ref = NULL;
493 } else if (*ref)
494 (*ref)->lf = lf;
498 done:
499 if (!err && *ref == NULL)
500 err = got_error_not_ref(refname);
501 if (err && lf)
502 got_lockfile_unlock(lf);
503 free(path_refs);
504 return err;
507 void
508 got_ref_close(struct got_reference *ref)
510 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
511 free(ref->ref.symref.name);
512 free(ref->ref.symref.ref);
513 } else
514 free(ref->ref.ref.name);
515 free(ref);
518 struct got_reference *
519 got_ref_dup(struct got_reference *ref)
521 struct got_reference *ret;
523 ret = calloc(1, sizeof(*ret));
524 if (ret == NULL)
525 return NULL;
527 ret->flags = ref->flags;
528 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
529 ret->ref.symref.name = strdup(ref->ref.symref.name);
530 if (ret->ref.symref.name == NULL) {
531 free(ret);
532 return NULL;
534 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
535 if (ret->ref.symref.ref == NULL) {
536 free(ret->ref.symref.name);
537 free(ret);
538 return NULL;
540 } else {
541 ret->ref.ref.name = strdup(ref->ref.ref.name);
542 if (ret->ref.ref.name == NULL) {
543 free(ret);
544 return NULL;
546 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
547 sizeof(ret->ref.ref.sha1));
550 return ret;
553 const struct got_error *
554 got_reflist_entry_dup(struct got_reflist_entry **newp,
555 struct got_reflist_entry *re)
557 const struct got_error *err = NULL;
558 struct got_reflist_entry *new;
560 *newp = NULL;
562 new = malloc(sizeof(*new));
563 if (new == NULL)
564 return got_error_from_errno("malloc");
566 new->ref = got_ref_dup(re->ref);
567 if (new->ref == NULL) {
568 err = got_error_from_errno("got_ref_dup");
569 free(new);
570 return err;
573 *newp = new;
574 return NULL;
577 static const struct got_error *
578 resolve_symbolic_ref(struct got_reference **resolved,
579 struct got_repository *repo, struct got_reference *ref)
581 struct got_reference *nextref;
582 const struct got_error *err;
584 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
585 if (err)
586 return err;
588 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
589 err = resolve_symbolic_ref(resolved, repo, nextref);
590 else
591 *resolved = got_ref_dup(nextref);
593 got_ref_close(nextref);
594 return err;
597 static const struct got_error *
598 ref_resolve(struct got_object_id **id, struct got_repository *repo,
599 struct got_reference *ref, int recursion)
601 const struct got_error *err;
603 if (recursion <= 0)
604 return got_error_msg(GOT_ERR_RECURSION,
605 "reference recursion limit reached");
607 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
608 struct got_reference *resolved = NULL;
609 err = resolve_symbolic_ref(&resolved, repo, ref);
610 if (err == NULL)
611 err = ref_resolve(id, repo, resolved, --recursion);
612 if (resolved)
613 got_ref_close(resolved);
614 return err;
617 *id = calloc(1, sizeof(**id));
618 if (*id == NULL)
619 return got_error_from_errno("calloc");
620 memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
621 return NULL;
624 const struct got_error *
625 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
626 struct got_reference *ref)
628 return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
631 char *
632 got_ref_to_str(struct got_reference *ref)
634 char *str;
636 if (ref->flags & GOT_REF_IS_SYMBOLIC)
637 return strdup(ref->ref.symref.ref);
639 str = malloc(SHA1_DIGEST_STRING_LENGTH);
640 if (str == NULL)
641 return NULL;
643 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
644 SHA1_DIGEST_STRING_LENGTH) == NULL) {
645 free(str);
646 return NULL;
649 return str;
652 const char *
653 got_ref_get_name(struct got_reference *ref)
655 if (ref->flags & GOT_REF_IS_SYMBOLIC)
656 return ref->ref.symref.name;
658 return ref->ref.ref.name;
661 const char *
662 got_ref_get_symref_target(struct got_reference *ref)
664 if (ref->flags & GOT_REF_IS_SYMBOLIC)
665 return ref->ref.symref.ref;
667 return NULL;
670 const struct got_error *
671 got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
672 struct got_reference* re2)
674 const char *name1 = got_ref_get_name(re1);
675 const char *name2 = got_ref_get_name(re2);
677 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
678 return NULL;
681 const struct got_error *
682 got_ref_cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
683 struct got_reference *ref2)
685 const struct got_error *err = NULL;
686 struct got_repository *repo = arg;
687 struct got_object_id *id1, *id2 = NULL;
688 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
689 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
690 time_t time1, time2;
692 *cmp = 0;
694 err = got_ref_resolve(&id1, repo, ref1);
695 if (err)
696 return err;
697 err = got_object_open_as_tag(&tag1, repo, id1);
698 if (err) {
699 if (err->code != GOT_ERR_OBJ_TYPE)
700 goto done;
701 /* "lightweight" tag */
702 err = got_object_open_as_commit(&commit1, repo, id1);
703 if (err)
704 goto done;
705 time1 = got_object_commit_get_committer_time(commit1);
706 } else
707 time1 = got_object_tag_get_tagger_time(tag1);
709 err = got_ref_resolve(&id2, repo, ref2);
710 if (err)
711 goto done;
712 err = got_object_open_as_tag(&tag2, repo, id2);
713 if (err) {
714 if (err->code != GOT_ERR_OBJ_TYPE)
715 goto done;
716 /* "lightweight" tag */
717 err = got_object_open_as_commit(&commit2, repo, id2);
718 if (err)
719 goto done;
720 time2 = got_object_commit_get_committer_time(commit2);
721 } else
722 time2 = got_object_tag_get_tagger_time(tag2);
724 /* Put latest tags first. */
725 if (time1 < time2)
726 *cmp = 1;
727 else if (time1 > time2)
728 *cmp = -1;
729 else
730 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
731 done:
732 free(id1);
733 free(id2);
734 if (tag1)
735 got_object_tag_close(tag1);
736 if (tag2)
737 got_object_tag_close(tag2);
738 if (commit1)
739 got_object_commit_close(commit1);
740 if (commit2)
741 got_object_commit_close(commit2);
742 return err;
745 static const struct got_error *
746 insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
747 struct got_reference *ref, struct got_repository *repo,
748 got_ref_cmp_cb cmp_cb, void *cmp_arg)
750 const struct got_error *err;
751 struct got_reflist_entry *new, *re;
752 int cmp;
754 *newp = NULL;
756 new = malloc(sizeof(*new));
757 if (new == NULL)
758 return got_error_from_errno("malloc");
759 new->ref = ref;
760 *newp = new;
762 /*
763 * We must de-duplicate entries on insert because packed-refs may
764 * contain redundant entries. On-disk refs take precedence.
765 * This code assumes that on-disk revs are read before packed-refs.
766 * We're iterating the list anyway, so insert elements sorted by name.
768 * Many callers will provide paths in a somewhat sorted order.
769 * Iterating backwards from the tail of the list should be more
770 * efficient than traversing through the entire list each time
771 * an element is inserted.
772 */
773 re = TAILQ_LAST(refs, got_reflist_head);
774 while (re) {
775 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
776 if (err)
777 return err;
778 if (cmp == 0) {
779 /* duplicate */
780 free(new);
781 *newp = NULL;
782 return NULL;
783 } else if (cmp < 0) {
784 TAILQ_INSERT_AFTER(refs, re, new, entry);
785 return NULL;
787 re = TAILQ_PREV(re, got_reflist_head, entry);
790 TAILQ_INSERT_HEAD(refs, new, entry);
791 return NULL;
794 static const struct got_error *
795 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
796 const char *subdir, struct got_repository *repo,
797 got_ref_cmp_cb cmp_cb, void *cmp_arg)
799 const struct got_error *err = NULL;
800 DIR *d = NULL;
801 char *path_subdir;
803 while (subdir[0] == '/')
804 subdir++;
806 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
807 return got_error_from_errno("asprintf");
809 d = opendir(path_subdir);
810 if (d == NULL)
811 goto done;
813 for (;;) {
814 struct dirent *dent;
815 struct got_reference *ref;
816 char *child;
817 int type;
819 dent = readdir(d);
820 if (dent == NULL)
821 break;
823 if (strcmp(dent->d_name, ".") == 0 ||
824 strcmp(dent->d_name, "..") == 0)
825 continue;
827 err = got_path_dirent_type(&type, path_subdir, dent);
828 if (err)
829 break;
831 switch (type) {
832 case DT_REG:
833 err = open_ref(&ref, path_refs, subdir, dent->d_name,
834 0);
835 if (err)
836 goto done;
837 if (ref) {
838 struct got_reflist_entry *new;
839 err = insert_ref(&new, refs, ref, repo,
840 cmp_cb, cmp_arg);
841 if (err || new == NULL /* duplicate */)
842 got_ref_close(ref);
843 if (err)
844 goto done;
846 break;
847 case DT_DIR:
848 if (asprintf(&child, "%s%s%s", subdir,
849 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
850 err = got_error_from_errno("asprintf");
851 break;
853 err = gather_on_disk_refs(refs, path_refs, child, repo,
854 cmp_cb, cmp_arg);
855 free(child);
856 break;
857 default:
858 break;
861 done:
862 if (d)
863 closedir(d);
864 free(path_subdir);
865 return err;
868 const struct got_error *
869 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
870 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
872 const struct got_error *err;
873 char *packed_refs_path, *path_refs = NULL;
874 char *abs_namespace = NULL;
875 char *buf = NULL, *ondisk_ref_namespace = NULL;
876 char *line = NULL;
877 FILE *f = NULL;
878 struct got_reference *ref;
879 struct got_reflist_entry *new;
881 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
882 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
883 if (path_refs == NULL) {
884 err = got_error_from_errno("get_refs_dir_path");
885 goto done;
887 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
888 if (err)
889 goto done;
890 err = insert_ref(&new, refs, ref, repo,
891 cmp_cb, cmp_arg);
892 if (err || new == NULL /* duplicate */)
893 got_ref_close(ref);
894 if (err && err->code != GOT_ERR_NOT_REF)
895 goto done;
896 } else {
897 /* Try listing a single reference. */
898 const char *refname = ref_namespace;
899 path_refs = get_refs_dir_path(repo, refname);
900 if (path_refs == NULL) {
901 err = got_error_from_errno("get_refs_dir_path");
902 goto done;
904 err = open_ref(&ref, path_refs, "", refname, 0);
905 if (err) {
906 if (err->code != GOT_ERR_NOT_REF)
907 goto done;
908 /* Try to look up references in a given namespace. */
909 } else {
910 err = insert_ref(&new, refs, ref, repo,
911 cmp_cb, cmp_arg);
912 if (err || new == NULL /* duplicate */)
913 got_ref_close(ref);
914 return err;
918 if (ref_namespace) {
919 size_t len;
920 /* Canonicalize the path to eliminate double-slashes if any. */
921 if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
922 err = got_error_from_errno("asprintf");
923 goto done;
925 len = strlen(abs_namespace) + 1;
926 buf = malloc(len);
927 if (buf == NULL) {
928 err = got_error_from_errno("malloc");
929 goto done;
931 err = got_canonpath(abs_namespace, buf, len);
932 if (err)
933 goto done;
934 ondisk_ref_namespace = buf;
935 while (ondisk_ref_namespace[0] == '/')
936 ondisk_ref_namespace++;
937 if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
938 ondisk_ref_namespace += 5;
939 else if (strcmp(ondisk_ref_namespace, "refs") == 0)
940 ondisk_ref_namespace = "";
943 /* Gather on-disk refs before parsing packed-refs. */
944 free(path_refs);
945 path_refs = get_refs_dir_path(repo, "");
946 if (path_refs == NULL) {
947 err = got_error_from_errno("get_refs_dir_path");
948 goto done;
950 err = gather_on_disk_refs(refs, path_refs,
951 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
952 cmp_cb, cmp_arg);
953 if (err)
954 goto done;
956 /*
957 * The packed-refs file may contain redundant entries, in which
958 * case on-disk refs take precedence.
959 */
960 packed_refs_path = got_repo_get_path_packed_refs(repo);
961 if (packed_refs_path == NULL) {
962 err = got_error_from_errno("got_repo_get_path_packed_refs");
963 goto done;
966 f = fopen(packed_refs_path, "r");
967 free(packed_refs_path);
968 if (f) {
969 size_t linesize = 0;
970 ssize_t linelen;
971 for (;;) {
972 linelen = getline(&line, &linesize, f);
973 if (linelen == -1) {
974 if (feof(f))
975 break;
976 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
977 goto done;
979 if (linelen > 0 && line[linelen - 1] == '\n')
980 line[linelen - 1] = '\0';
981 err = parse_packed_ref_line(&ref, NULL, line);
982 if (err)
983 goto done;
984 if (ref) {
985 if (ref_namespace) {
986 const char *name;
987 name = got_ref_get_name(ref);
988 if (!got_path_is_child(name,
989 ref_namespace,
990 strlen(ref_namespace))) {
991 got_ref_close(ref);
992 continue;
995 err = insert_ref(&new, refs, ref, repo,
996 cmp_cb, cmp_arg);
997 if (err || new == NULL /* duplicate */)
998 got_ref_close(ref);
999 if (err)
1000 goto done;
1004 done:
1005 free(abs_namespace);
1006 free(buf);
1007 free(line);
1008 free(path_refs);
1009 if (f && fclose(f) == EOF && err == NULL)
1010 err = got_error_from_errno("fclose");
1011 return err;
1014 void
1015 got_ref_list_free(struct got_reflist_head *refs)
1017 struct got_reflist_entry *re;
1019 while ((re = TAILQ_FIRST(refs))) {
1020 TAILQ_REMOVE(refs, re, entry);
1021 free(re);
1026 int
1027 got_ref_is_symbolic(struct got_reference *ref)
1029 return (ref->flags & GOT_REF_IS_SYMBOLIC);
1032 const struct got_error *
1033 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1035 if (ref->flags & GOT_REF_IS_SYMBOLIC)
1036 return got_error(GOT_ERR_BAD_REF_TYPE);
1038 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
1039 return NULL;
1042 const struct got_error *
1043 got_ref_change_symref(struct got_reference *ref, const char *refname)
1045 char *new_name;
1047 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1048 return got_error(GOT_ERR_BAD_REF_TYPE);
1050 new_name = strdup(refname);
1051 if (new_name == NULL)
1052 return got_error_from_errno("strdup");
1054 free(ref->ref.symref.ref);
1055 ref->ref.symref.ref = new_name;
1056 return NULL;
1059 const struct got_error *
1060 got_ref_change_symref_to_ref(struct got_reference *symref,
1061 struct got_object_id *id)
1063 if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1064 return got_error(GOT_ERR_BAD_REF_TYPE);
1066 symref->ref.ref.name = symref->ref.symref.name;
1067 memcpy(symref->ref.ref.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1068 symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1069 return NULL;
1072 const struct got_error *
1073 got_ref_write(struct got_reference *ref, struct got_repository *repo)
1075 const struct got_error *err = NULL, *unlock_err = NULL;
1076 const char *name = got_ref_get_name(ref);
1077 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1078 struct got_lockfile *lf = NULL;
1079 FILE *f = NULL;
1080 size_t n;
1081 struct stat sb;
1083 path_refs = get_refs_dir_path(repo, name);
1084 if (path_refs == NULL) {
1085 err = got_error_from_errno2("get_refs_dir_path", name);
1086 goto done;
1089 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1090 err = got_error_from_errno("asprintf");
1091 goto done;
1094 err = got_opentemp_named(&tmppath, &f, path);
1095 if (err) {
1096 char *parent;
1097 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1098 goto done;
1099 err = got_path_dirname(&parent, path);
1100 if (err)
1101 goto done;
1102 err = got_path_mkdir(parent);
1103 free(parent);
1104 if (err)
1105 goto done;
1106 err = got_opentemp_named(&tmppath, &f, path);
1107 if (err)
1108 goto done;
1111 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1112 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1113 if (n != strlen(ref->ref.symref.ref) + 6) {
1114 err = got_ferror(f, GOT_ERR_IO);
1115 goto done;
1117 } else {
1118 char hex[SHA1_DIGEST_STRING_LENGTH];
1119 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
1120 sizeof(hex)) == NULL) {
1121 err = got_error(GOT_ERR_BAD_REF_DATA);
1122 goto done;
1124 n = fprintf(f, "%s\n", hex);
1125 if (n != sizeof(hex)) {
1126 err = got_ferror(f, GOT_ERR_IO);
1127 goto done;
1131 if (ref->lf == NULL) {
1132 err = got_lockfile_lock(&lf, path);
1133 if (err)
1134 goto done;
1137 /* XXX: check if old content matches our expectations? */
1139 if (stat(path, &sb) != 0) {
1140 if (errno != ENOENT) {
1141 err = got_error_from_errno2("stat", path);
1142 goto done;
1144 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1147 if (fchmod(fileno(f), sb.st_mode) != 0) {
1148 err = got_error_from_errno2("fchmod", tmppath);
1149 goto done;
1152 if (rename(tmppath, path) != 0) {
1153 err = got_error_from_errno3("rename", tmppath, path);
1154 goto done;
1156 free(tmppath);
1157 tmppath = NULL;
1158 done:
1159 if (ref->lf == NULL && lf)
1160 unlock_err = got_lockfile_unlock(lf);
1161 if (f) {
1162 if (fclose(f) == EOF && err == NULL)
1163 err = got_error_from_errno("fclose");
1165 free(path_refs);
1166 free(path);
1167 if (tmppath) {
1168 if (unlink(tmppath) != 0 && err == NULL)
1169 err = got_error_from_errno2("unlink", tmppath);
1170 free(tmppath);
1172 return err ? err : unlock_err;
1175 static const struct got_error *
1176 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1178 const struct got_error *err = NULL, *unlock_err = NULL;
1179 struct got_lockfile *lf = NULL;
1180 FILE *f = NULL, *tmpf = NULL;
1181 char *line = NULL, *packed_refs_path, *tmppath = NULL;
1182 size_t linesize = 0;
1183 struct got_reflist_head refs;
1184 int found_delref = 0;
1186 /* The packed-refs file does not cotain symbolic references. */
1187 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1188 return got_error(GOT_ERR_BAD_REF_DATA);
1190 TAILQ_INIT(&refs);
1192 packed_refs_path = got_repo_get_path_packed_refs(repo);
1193 if (packed_refs_path == NULL)
1194 return got_error_from_errno("got_repo_get_path_packed_refs");
1196 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
1197 if (err)
1198 goto done;
1200 if (delref->lf == NULL) {
1201 err = got_lockfile_lock(&lf, packed_refs_path);
1202 if (err)
1203 goto done;
1206 f = fopen(packed_refs_path, "r");
1207 if (f == NULL) {
1208 err = got_error_from_errno2("fopen", packed_refs_path);
1209 goto done;
1211 for (;;) {
1212 ssize_t linelen;
1213 struct got_reference *ref;
1214 struct got_reflist_entry *new;
1216 linelen = getline(&line, &linesize, f);
1217 if (linelen == -1) {
1218 if (feof(f))
1219 break;
1220 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1221 goto done;
1223 if (linelen > 0 && line[linelen - 1] == '\n')
1224 line[linelen - 1] = '\0';
1225 err = parse_packed_ref_line(&ref, NULL, line);
1226 if (err)
1227 goto done;
1228 if (ref == NULL)
1229 continue;
1231 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1232 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1233 sizeof(delref->ref.ref.sha1)) == 0) {
1234 found_delref = 1;
1235 got_ref_close(ref);
1236 continue;
1239 err = insert_ref(&new, &refs, ref, repo,
1240 got_ref_cmp_by_name, NULL);
1241 if (err || new == NULL /* duplicate */)
1242 got_ref_close(ref);
1243 if (err)
1244 goto done;
1247 if (found_delref) {
1248 struct got_reflist_entry *re;
1249 size_t n;
1250 struct stat sb;
1252 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1253 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1254 err = got_ferror(f, GOT_ERR_IO);
1255 goto done;
1258 TAILQ_FOREACH(re, &refs, entry) {
1259 uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
1261 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1262 sizeof(hex)) == NULL) {
1263 err = got_error(GOT_ERR_BAD_REF_DATA);
1264 goto done;
1266 n = fprintf(tmpf, "%s ", hex);
1267 if (n != sizeof(hex)) {
1268 err = got_ferror(f, GOT_ERR_IO);
1269 goto done;
1271 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1272 if (n != strlen(re->ref->ref.ref.name) + 1) {
1273 err = got_ferror(f, GOT_ERR_IO);
1274 goto done;
1278 if (fflush(tmpf) != 0) {
1279 err = got_error_from_errno("fflush");
1280 goto done;
1283 if (fstat(fileno(f), &sb) != 0) {
1284 if (errno != ENOENT) {
1285 err = got_error_from_errno2("fstat",
1286 packed_refs_path);
1287 goto done;
1289 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1292 if (fchmod(fileno(tmpf), sb.st_mode) != 0) {
1293 err = got_error_from_errno2("fchmod", tmppath);
1294 goto done;
1297 if (rename(tmppath, packed_refs_path) != 0) {
1298 err = got_error_from_errno3("rename", tmppath,
1299 packed_refs_path);
1300 goto done;
1303 done:
1304 if (delref->lf == NULL && lf)
1305 unlock_err = got_lockfile_unlock(lf);
1306 if (f) {
1307 if (fclose(f) == EOF && err == NULL)
1308 err = got_error_from_errno("fclose");
1310 if (tmpf) {
1311 unlink(tmppath);
1312 if (fclose(tmpf) == EOF && err == NULL)
1313 err = got_error_from_errno("fclose");
1315 free(tmppath);
1316 free(packed_refs_path);
1317 free(line);
1318 got_ref_list_free(&refs);
1319 return err ? err : unlock_err;
1322 static const struct got_error *
1323 delete_loose_ref(struct got_reference *ref, struct got_repository *repo)
1325 const struct got_error *err = NULL, *unlock_err = NULL;
1326 const char *name = got_ref_get_name(ref);
1327 char *path_refs = NULL, *path = NULL;
1328 struct got_lockfile *lf = NULL;
1330 path_refs = get_refs_dir_path(repo, name);
1331 if (path_refs == NULL) {
1332 err = got_error_from_errno2("get_refs_dir_path", name);
1333 goto done;
1336 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1337 err = got_error_from_errno("asprintf");
1338 goto done;
1341 if (ref->lf == NULL) {
1342 err = got_lockfile_lock(&lf, path);
1343 if (err)
1344 goto done;
1347 /* XXX: check if old content matches our expectations? */
1349 if (unlink(path) != 0)
1350 err = got_error_from_errno2("unlink", path);
1351 done:
1352 if (ref->lf == NULL && lf)
1353 unlock_err = got_lockfile_unlock(lf);
1355 free(path_refs);
1356 free(path);
1357 return err ? err : unlock_err;
1360 const struct got_error *
1361 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1363 const struct got_error *err = NULL;
1364 struct got_reference *ref2;
1366 if (ref->flags & GOT_REF_IS_PACKED) {
1367 err = delete_packed_ref(ref, repo);
1368 if (err)
1369 return err;
1371 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1372 if (err) {
1373 if (err->code == GOT_ERR_NOT_REF)
1374 return NULL;
1375 return err;
1378 err = delete_loose_ref(ref2, repo);
1379 got_ref_close(ref2);
1380 return err;
1381 } else {
1382 err = delete_loose_ref(ref, repo);
1383 if (err)
1384 return err;
1386 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1387 if (err) {
1388 if (err->code == GOT_ERR_NOT_REF)
1389 return NULL;
1390 return err;
1393 err = delete_packed_ref(ref2, repo);
1394 got_ref_close(ref2);
1395 return err;
1399 const struct got_error *
1400 got_ref_unlock(struct got_reference *ref)
1402 const struct got_error *err;
1403 err = got_lockfile_unlock(ref->lf);
1404 ref->lf = NULL;
1405 return err;
1408 struct got_reflist_object_id_map {
1409 struct got_object_idset *idset;
1412 struct got_reflist_object_id_map_entry {
1413 struct got_reflist_head refs;
1416 static const struct got_error *
1417 add_object_id_map_entry(struct got_object_idset *idset,
1418 struct got_object_id *id, struct got_reflist_entry *re)
1420 const struct got_error *err = NULL;
1421 struct got_reflist_object_id_map_entry *ent;
1422 struct got_reflist_entry *new;
1424 ent = got_object_idset_get(idset, id);
1425 if (ent == NULL) {
1426 ent = malloc(sizeof(*ent));
1427 if (ent == NULL)
1428 return got_error_from_errno("malloc");
1430 TAILQ_INIT(&ent->refs);
1431 err = got_object_idset_add(idset, id, ent);
1432 if (err)
1433 return err;
1436 err = got_reflist_entry_dup(&new, re);
1437 if (err)
1438 return err;
1440 TAILQ_INSERT_TAIL(&ent->refs, new, entry);
1441 return NULL;
1444 const struct got_error *
1445 got_reflist_object_id_map_create(struct got_reflist_object_id_map **map,
1446 struct got_reflist_head *refs, struct got_repository *repo)
1448 const struct got_error *err = NULL;
1449 struct got_object_idset *idset;
1450 struct got_object_id *id = NULL;
1451 struct got_reflist_entry *re;
1453 idset = got_object_idset_alloc();
1454 if (idset == NULL)
1455 return got_error_from_errno("got_object_idset_alloc");
1457 *map = malloc(sizeof(**map));
1458 if (*map == NULL) {
1459 got_object_idset_free(idset);
1460 return got_error_from_errno("malloc");
1462 (*map)->idset = idset;
1464 TAILQ_FOREACH(re, refs, entry) {
1465 struct got_tag_object *tag = NULL;
1467 err = got_ref_resolve(&id, repo, re->ref);
1468 if (err)
1469 goto done;
1471 err = add_object_id_map_entry(idset, id, re);
1472 if (err)
1473 goto done;
1475 if (strstr(got_ref_get_name(re->ref), "/tags/") == NULL) {
1476 free(id);
1477 id = NULL;
1478 continue;
1481 err = got_object_open_as_tag(&tag, repo, id);
1482 if (err) {
1483 if (err->code != GOT_ERR_OBJ_TYPE)
1484 goto done;
1485 /* Ref points at something other than a tag. */
1486 err = NULL;
1487 tag = NULL;
1488 free(id);
1489 id = NULL;
1490 continue;
1493 err = add_object_id_map_entry(idset,
1494 got_object_tag_get_object_id(tag), re);
1495 got_object_tag_close(tag);
1496 if (err)
1497 goto done;
1499 free(id);
1500 id = NULL;
1502 done:
1503 free(id);
1504 if (err) {
1505 got_reflist_object_id_map_free(*map);
1506 *map = NULL;
1508 return err;
1511 struct got_reflist_head *
1512 got_reflist_object_id_map_lookup(struct got_reflist_object_id_map *map,
1513 struct got_object_id *id)
1515 struct got_reflist_object_id_map_entry *ent;
1516 ent = got_object_idset_get(map->idset, id);
1517 if (ent)
1518 return &ent->refs;
1519 return NULL;
1522 static const struct got_error *
1523 free_id_map_entry(struct got_object_id *id, void *data, void *arg)
1525 struct got_reflist_object_id_map_entry *ent = data;
1527 got_ref_list_free(&ent->refs);
1528 free(ent);
1529 return NULL;
1532 void
1533 got_reflist_object_id_map_free(struct got_reflist_object_id_map *map)
1535 got_object_idset_for_each(map->idset, free_id_map_entry, NULL);
1536 got_object_idset_free(map->idset);
1537 free(map);