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 const struct got_error *
746 got_ref_cmp_by_commit_timestamp_descending(void *arg, int *cmp,
747 struct got_reference *ref1, struct got_reference *ref2)
749 const struct got_error *err;
750 struct got_repository *repo = arg;
751 struct got_object_id *id1, *id2 = NULL;
752 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
753 time_t time1, time2;
755 *cmp = 0;
757 err = got_ref_resolve(&id1, repo, ref1);
758 if (err)
759 return err;
760 err = got_ref_resolve(&id2, repo, ref2);
761 if (err)
762 goto done;
764 err = got_object_open_as_commit(&commit1, repo, id1);
765 if (err)
766 goto done;
767 err = got_object_open_as_commit(&commit2, repo, id2);
768 if (err)
769 goto done;
771 time1 = got_object_commit_get_committer_time(commit1);
772 time2 = got_object_commit_get_committer_time(commit2);
773 if (time1 < time2)
774 *cmp = 1;
775 else if (time2 < time1)
776 *cmp = -1;
777 done:
778 free(id1);
779 free(id2);
780 if (commit1)
781 got_object_commit_close(commit1);
782 if (commit2)
783 got_object_commit_close(commit2);
784 return err;
787 static const struct got_error *
788 insert_ref(struct got_reflist_entry **newp, struct got_reflist_head *refs,
789 struct got_reference *ref, struct got_repository *repo,
790 got_ref_cmp_cb cmp_cb, void *cmp_arg)
792 const struct got_error *err;
793 struct got_reflist_entry *new, *re;
794 int cmp;
796 *newp = NULL;
798 new = malloc(sizeof(*new));
799 if (new == NULL)
800 return got_error_from_errno("malloc");
801 new->ref = ref;
802 *newp = new;
804 /*
805 * We must de-duplicate entries on insert because packed-refs may
806 * contain redundant entries. On-disk refs take precedence.
807 * This code assumes that on-disk revs are read before packed-refs.
808 * We're iterating the list anyway, so insert elements sorted by name.
810 * Many callers will provide paths in a somewhat sorted order.
811 * Iterating backwards from the tail of the list should be more
812 * efficient than traversing through the entire list each time
813 * an element is inserted.
814 */
815 re = TAILQ_LAST(refs, got_reflist_head);
816 while (re) {
817 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
818 if (err)
819 return err;
820 if (cmp == 0) {
821 /* duplicate */
822 free(new);
823 *newp = NULL;
824 return NULL;
825 } else if (cmp < 0) {
826 TAILQ_INSERT_AFTER(refs, re, new, entry);
827 return NULL;
829 re = TAILQ_PREV(re, got_reflist_head, entry);
832 TAILQ_INSERT_HEAD(refs, new, entry);
833 return NULL;
836 static const struct got_error *
837 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
838 const char *subdir, struct got_repository *repo,
839 got_ref_cmp_cb cmp_cb, void *cmp_arg)
841 const struct got_error *err = NULL;
842 DIR *d = NULL;
843 char *path_subdir;
845 while (subdir[0] == '/')
846 subdir++;
848 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
849 return got_error_from_errno("asprintf");
851 d = opendir(path_subdir);
852 if (d == NULL)
853 goto done;
855 for (;;) {
856 struct dirent *dent;
857 struct got_reference *ref;
858 char *child;
859 int type;
861 dent = readdir(d);
862 if (dent == NULL)
863 break;
865 if (strcmp(dent->d_name, ".") == 0 ||
866 strcmp(dent->d_name, "..") == 0)
867 continue;
869 err = got_path_dirent_type(&type, path_subdir, dent);
870 if (err)
871 break;
873 switch (type) {
874 case DT_REG:
875 err = open_ref(&ref, path_refs, subdir, dent->d_name,
876 0);
877 if (err)
878 goto done;
879 if (ref) {
880 struct got_reflist_entry *new;
881 err = insert_ref(&new, refs, ref, repo,
882 cmp_cb, cmp_arg);
883 if (err || new == NULL /* duplicate */)
884 got_ref_close(ref);
885 if (err)
886 goto done;
888 break;
889 case DT_DIR:
890 if (asprintf(&child, "%s%s%s", subdir,
891 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
892 err = got_error_from_errno("asprintf");
893 break;
895 err = gather_on_disk_refs(refs, path_refs, child, repo,
896 cmp_cb, cmp_arg);
897 free(child);
898 break;
899 default:
900 break;
903 done:
904 if (d)
905 closedir(d);
906 free(path_subdir);
907 return err;
910 const struct got_error *
911 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
912 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
914 const struct got_error *err;
915 char *packed_refs_path, *path_refs = NULL;
916 char *abs_namespace = NULL;
917 char *buf = NULL, *ondisk_ref_namespace = NULL;
918 char *line = NULL;
919 FILE *f = NULL;
920 struct got_reference *ref;
921 struct got_reflist_entry *new;
923 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
924 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
925 if (path_refs == NULL) {
926 err = got_error_from_errno("get_refs_dir_path");
927 goto done;
929 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
930 if (err)
931 goto done;
932 err = insert_ref(&new, refs, ref, repo,
933 cmp_cb, cmp_arg);
934 if (err || new == NULL /* duplicate */)
935 got_ref_close(ref);
936 if (err && err->code != GOT_ERR_NOT_REF)
937 goto done;
938 } else {
939 /* Try listing a single reference. */
940 const char *refname = ref_namespace;
941 path_refs = get_refs_dir_path(repo, refname);
942 if (path_refs == NULL) {
943 err = got_error_from_errno("get_refs_dir_path");
944 goto done;
946 err = open_ref(&ref, path_refs, "", refname, 0);
947 if (err) {
948 if (err->code != GOT_ERR_NOT_REF)
949 goto done;
950 /* Try to look up references in a given namespace. */
951 } else {
952 err = insert_ref(&new, refs, ref, repo,
953 cmp_cb, cmp_arg);
954 if (err || new == NULL /* duplicate */)
955 got_ref_close(ref);
956 return err;
960 if (ref_namespace) {
961 size_t len;
962 /* Canonicalize the path to eliminate double-slashes if any. */
963 if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
964 err = got_error_from_errno("asprintf");
965 goto done;
967 len = strlen(abs_namespace) + 1;
968 buf = malloc(len);
969 if (buf == NULL) {
970 err = got_error_from_errno("malloc");
971 goto done;
973 err = got_canonpath(abs_namespace, buf, len);
974 if (err)
975 goto done;
976 ondisk_ref_namespace = buf;
977 while (ondisk_ref_namespace[0] == '/')
978 ondisk_ref_namespace++;
979 if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
980 ondisk_ref_namespace += 5;
981 else if (strcmp(ondisk_ref_namespace, "refs") == 0)
982 ondisk_ref_namespace = "";
985 /* Gather on-disk refs before parsing packed-refs. */
986 free(path_refs);
987 path_refs = get_refs_dir_path(repo, "");
988 if (path_refs == NULL) {
989 err = got_error_from_errno("get_refs_dir_path");
990 goto done;
992 err = gather_on_disk_refs(refs, path_refs,
993 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
994 cmp_cb, cmp_arg);
995 if (err)
996 goto done;
998 /*
999 * The packed-refs file may contain redundant entries, in which
1000 * case on-disk refs take precedence.
1002 packed_refs_path = got_repo_get_path_packed_refs(repo);
1003 if (packed_refs_path == NULL) {
1004 err = got_error_from_errno("got_repo_get_path_packed_refs");
1005 goto done;
1008 f = fopen(packed_refs_path, "r");
1009 free(packed_refs_path);
1010 if (f) {
1011 size_t linesize = 0;
1012 ssize_t linelen;
1013 for (;;) {
1014 linelen = getline(&line, &linesize, f);
1015 if (linelen == -1) {
1016 if (feof(f))
1017 break;
1018 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1019 goto done;
1021 if (linelen > 0 && line[linelen - 1] == '\n')
1022 line[linelen - 1] = '\0';
1023 err = parse_packed_ref_line(&ref, NULL, line);
1024 if (err)
1025 goto done;
1026 if (ref) {
1027 if (ref_namespace) {
1028 const char *name;
1029 name = got_ref_get_name(ref);
1030 if (!got_path_is_child(name,
1031 ref_namespace,
1032 strlen(ref_namespace))) {
1033 got_ref_close(ref);
1034 continue;
1037 err = insert_ref(&new, refs, ref, repo,
1038 cmp_cb, cmp_arg);
1039 if (err || new == NULL /* duplicate */)
1040 got_ref_close(ref);
1041 if (err)
1042 goto done;
1046 done:
1047 free(abs_namespace);
1048 free(buf);
1049 free(line);
1050 free(path_refs);
1051 if (f && fclose(f) == EOF && err == NULL)
1052 err = got_error_from_errno("fclose");
1053 return err;
1056 void
1057 got_ref_list_free(struct got_reflist_head *refs)
1059 struct got_reflist_entry *re;
1061 while ((re = TAILQ_FIRST(refs))) {
1062 TAILQ_REMOVE(refs, re, entry);
1063 free(re);
1068 int
1069 got_ref_is_symbolic(struct got_reference *ref)
1071 return (ref->flags & GOT_REF_IS_SYMBOLIC);
1074 const struct got_error *
1075 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1077 if (ref->flags & GOT_REF_IS_SYMBOLIC)
1078 return got_error(GOT_ERR_BAD_REF_TYPE);
1080 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
1081 return NULL;
1084 const struct got_error *
1085 got_ref_change_symref(struct got_reference *ref, const char *refname)
1087 char *new_name;
1089 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1090 return got_error(GOT_ERR_BAD_REF_TYPE);
1092 new_name = strdup(refname);
1093 if (new_name == NULL)
1094 return got_error_from_errno("strdup");
1096 free(ref->ref.symref.ref);
1097 ref->ref.symref.ref = new_name;
1098 return NULL;
1101 const struct got_error *
1102 got_ref_change_symref_to_ref(struct got_reference *symref,
1103 struct got_object_id *id)
1105 if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1106 return got_error(GOT_ERR_BAD_REF_TYPE);
1108 symref->ref.ref.name = symref->ref.symref.name;
1109 memcpy(symref->ref.ref.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1110 symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1111 return NULL;
1114 const struct got_error *
1115 got_ref_write(struct got_reference *ref, struct got_repository *repo)
1117 const struct got_error *err = NULL, *unlock_err = NULL;
1118 const char *name = got_ref_get_name(ref);
1119 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1120 struct got_lockfile *lf = NULL;
1121 FILE *f = NULL;
1122 size_t n;
1123 struct stat sb;
1125 path_refs = get_refs_dir_path(repo, name);
1126 if (path_refs == NULL) {
1127 err = got_error_from_errno2("get_refs_dir_path", name);
1128 goto done;
1131 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1132 err = got_error_from_errno("asprintf");
1133 goto done;
1136 err = got_opentemp_named(&tmppath, &f, path);
1137 if (err) {
1138 char *parent;
1139 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1140 goto done;
1141 err = got_path_dirname(&parent, path);
1142 if (err)
1143 goto done;
1144 err = got_path_mkdir(parent);
1145 free(parent);
1146 if (err)
1147 goto done;
1148 err = got_opentemp_named(&tmppath, &f, path);
1149 if (err)
1150 goto done;
1153 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1154 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1155 if (n != strlen(ref->ref.symref.ref) + 6) {
1156 err = got_ferror(f, GOT_ERR_IO);
1157 goto done;
1159 } else {
1160 char hex[SHA1_DIGEST_STRING_LENGTH];
1161 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
1162 sizeof(hex)) == NULL) {
1163 err = got_error(GOT_ERR_BAD_REF_DATA);
1164 goto done;
1166 n = fprintf(f, "%s\n", hex);
1167 if (n != sizeof(hex)) {
1168 err = got_ferror(f, GOT_ERR_IO);
1169 goto done;
1173 if (ref->lf == NULL) {
1174 err = got_lockfile_lock(&lf, path);
1175 if (err)
1176 goto done;
1179 /* XXX: check if old content matches our expectations? */
1181 if (stat(path, &sb) != 0) {
1182 if (errno != ENOENT) {
1183 err = got_error_from_errno2("stat", path);
1184 goto done;
1186 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1189 if (fchmod(fileno(f), sb.st_mode) != 0) {
1190 err = got_error_from_errno2("fchmod", tmppath);
1191 goto done;
1194 if (rename(tmppath, path) != 0) {
1195 err = got_error_from_errno3("rename", tmppath, path);
1196 goto done;
1198 free(tmppath);
1199 tmppath = NULL;
1200 done:
1201 if (ref->lf == NULL && lf)
1202 unlock_err = got_lockfile_unlock(lf);
1203 if (f) {
1204 if (fclose(f) == EOF && err == NULL)
1205 err = got_error_from_errno("fclose");
1207 free(path_refs);
1208 free(path);
1209 if (tmppath) {
1210 if (unlink(tmppath) != 0 && err == NULL)
1211 err = got_error_from_errno2("unlink", tmppath);
1212 free(tmppath);
1214 return err ? err : unlock_err;
1217 static const struct got_error *
1218 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1220 const struct got_error *err = NULL, *unlock_err = NULL;
1221 struct got_lockfile *lf = NULL;
1222 FILE *f = NULL, *tmpf = NULL;
1223 char *line = NULL, *packed_refs_path, *tmppath = NULL;
1224 size_t linesize = 0;
1225 struct got_reflist_head refs;
1226 int found_delref = 0;
1228 /* The packed-refs file does not cotain symbolic references. */
1229 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1230 return got_error(GOT_ERR_BAD_REF_DATA);
1232 TAILQ_INIT(&refs);
1234 packed_refs_path = got_repo_get_path_packed_refs(repo);
1235 if (packed_refs_path == NULL)
1236 return got_error_from_errno("got_repo_get_path_packed_refs");
1238 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
1239 if (err)
1240 goto done;
1242 if (delref->lf == NULL) {
1243 err = got_lockfile_lock(&lf, packed_refs_path);
1244 if (err)
1245 goto done;
1248 f = fopen(packed_refs_path, "r");
1249 if (f == NULL) {
1250 err = got_error_from_errno2("fopen", packed_refs_path);
1251 goto done;
1253 for (;;) {
1254 ssize_t linelen;
1255 struct got_reference *ref;
1256 struct got_reflist_entry *new;
1258 linelen = getline(&line, &linesize, f);
1259 if (linelen == -1) {
1260 if (feof(f))
1261 break;
1262 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1263 goto done;
1265 if (linelen > 0 && line[linelen - 1] == '\n')
1266 line[linelen - 1] = '\0';
1267 err = parse_packed_ref_line(&ref, NULL, line);
1268 if (err)
1269 goto done;
1270 if (ref == NULL)
1271 continue;
1273 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1274 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1275 sizeof(delref->ref.ref.sha1)) == 0) {
1276 found_delref = 1;
1277 got_ref_close(ref);
1278 continue;
1281 err = insert_ref(&new, &refs, ref, repo,
1282 got_ref_cmp_by_name, NULL);
1283 if (err || new == NULL /* duplicate */)
1284 got_ref_close(ref);
1285 if (err)
1286 goto done;
1289 if (found_delref) {
1290 struct got_reflist_entry *re;
1291 size_t n;
1292 struct stat sb;
1294 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1295 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1296 err = got_ferror(f, GOT_ERR_IO);
1297 goto done;
1300 TAILQ_FOREACH(re, &refs, entry) {
1301 uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
1303 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1304 sizeof(hex)) == NULL) {
1305 err = got_error(GOT_ERR_BAD_REF_DATA);
1306 goto done;
1308 n = fprintf(tmpf, "%s ", hex);
1309 if (n != sizeof(hex)) {
1310 err = got_ferror(f, GOT_ERR_IO);
1311 goto done;
1313 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1314 if (n != strlen(re->ref->ref.ref.name) + 1) {
1315 err = got_ferror(f, GOT_ERR_IO);
1316 goto done;
1320 if (fflush(tmpf) != 0) {
1321 err = got_error_from_errno("fflush");
1322 goto done;
1325 if (fstat(fileno(f), &sb) != 0) {
1326 if (errno != ENOENT) {
1327 err = got_error_from_errno2("fstat",
1328 packed_refs_path);
1329 goto done;
1331 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1334 if (fchmod(fileno(tmpf), sb.st_mode) != 0) {
1335 err = got_error_from_errno2("fchmod", tmppath);
1336 goto done;
1339 if (rename(tmppath, packed_refs_path) != 0) {
1340 err = got_error_from_errno3("rename", tmppath,
1341 packed_refs_path);
1342 goto done;
1345 done:
1346 if (delref->lf == NULL && lf)
1347 unlock_err = got_lockfile_unlock(lf);
1348 if (f) {
1349 if (fclose(f) == EOF && err == NULL)
1350 err = got_error_from_errno("fclose");
1352 if (tmpf) {
1353 unlink(tmppath);
1354 if (fclose(tmpf) == EOF && err == NULL)
1355 err = got_error_from_errno("fclose");
1357 free(tmppath);
1358 free(packed_refs_path);
1359 free(line);
1360 got_ref_list_free(&refs);
1361 return err ? err : unlock_err;
1364 static const struct got_error *
1365 delete_loose_ref(struct got_reference *ref, struct got_repository *repo)
1367 const struct got_error *err = NULL, *unlock_err = NULL;
1368 const char *name = got_ref_get_name(ref);
1369 char *path_refs = NULL, *path = NULL;
1370 struct got_lockfile *lf = NULL;
1372 path_refs = get_refs_dir_path(repo, name);
1373 if (path_refs == NULL) {
1374 err = got_error_from_errno2("get_refs_dir_path", name);
1375 goto done;
1378 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1379 err = got_error_from_errno("asprintf");
1380 goto done;
1383 if (ref->lf == NULL) {
1384 err = got_lockfile_lock(&lf, path);
1385 if (err)
1386 goto done;
1389 /* XXX: check if old content matches our expectations? */
1391 if (unlink(path) != 0)
1392 err = got_error_from_errno2("unlink", path);
1393 done:
1394 if (ref->lf == NULL && lf)
1395 unlock_err = got_lockfile_unlock(lf);
1397 free(path_refs);
1398 free(path);
1399 return err ? err : unlock_err;
1402 const struct got_error *
1403 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1405 const struct got_error *err = NULL;
1406 struct got_reference *ref2;
1408 if (ref->flags & GOT_REF_IS_PACKED) {
1409 err = delete_packed_ref(ref, repo);
1410 if (err)
1411 return err;
1413 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1414 if (err) {
1415 if (err->code == GOT_ERR_NOT_REF)
1416 return NULL;
1417 return err;
1420 err = delete_loose_ref(ref2, repo);
1421 got_ref_close(ref2);
1422 return err;
1423 } else {
1424 err = delete_loose_ref(ref, repo);
1425 if (err)
1426 return err;
1428 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1429 if (err) {
1430 if (err->code == GOT_ERR_NOT_REF)
1431 return NULL;
1432 return err;
1435 err = delete_packed_ref(ref2, repo);
1436 got_ref_close(ref2);
1437 return err;
1441 const struct got_error *
1442 got_ref_unlock(struct got_reference *ref)
1444 const struct got_error *err;
1445 err = got_lockfile_unlock(ref->lf);
1446 ref->lf = NULL;
1447 return err;
1450 struct got_reflist_object_id_map {
1451 struct got_object_idset *idset;
1454 struct got_reflist_object_id_map_entry {
1455 struct got_reflist_head refs;
1458 static const struct got_error *
1459 add_object_id_map_entry(struct got_object_idset *idset,
1460 struct got_object_id *id, struct got_reflist_entry *re)
1462 const struct got_error *err = NULL;
1463 struct got_reflist_object_id_map_entry *ent;
1464 struct got_reflist_entry *new;
1466 ent = got_object_idset_get(idset, id);
1467 if (ent == NULL) {
1468 ent = malloc(sizeof(*ent));
1469 if (ent == NULL)
1470 return got_error_from_errno("malloc");
1472 TAILQ_INIT(&ent->refs);
1473 err = got_object_idset_add(idset, id, ent);
1474 if (err)
1475 return err;
1478 err = got_reflist_entry_dup(&new, re);
1479 if (err)
1480 return err;
1482 TAILQ_INSERT_TAIL(&ent->refs, new, entry);
1483 return NULL;
1486 const struct got_error *
1487 got_reflist_object_id_map_create(struct got_reflist_object_id_map **map,
1488 struct got_reflist_head *refs, struct got_repository *repo)
1490 const struct got_error *err = NULL;
1491 struct got_object_idset *idset;
1492 struct got_object_id *id = NULL;
1493 struct got_reflist_entry *re;
1495 idset = got_object_idset_alloc();
1496 if (idset == NULL)
1497 return got_error_from_errno("got_object_idset_alloc");
1499 *map = malloc(sizeof(**map));
1500 if (*map == NULL) {
1501 got_object_idset_free(idset);
1502 return got_error_from_errno("malloc");
1504 (*map)->idset = idset;
1506 TAILQ_FOREACH(re, refs, entry) {
1507 struct got_tag_object *tag = NULL;
1509 err = got_ref_resolve(&id, repo, re->ref);
1510 if (err)
1511 goto done;
1513 err = add_object_id_map_entry(idset, id, re);
1514 if (err)
1515 goto done;
1517 if (strstr(got_ref_get_name(re->ref), "/tags/") == NULL) {
1518 free(id);
1519 id = NULL;
1520 continue;
1523 err = got_object_open_as_tag(&tag, repo, id);
1524 if (err) {
1525 if (err->code != GOT_ERR_OBJ_TYPE)
1526 goto done;
1527 /* Ref points at something other than a tag. */
1528 err = NULL;
1529 tag = NULL;
1530 free(id);
1531 id = NULL;
1532 continue;
1535 err = add_object_id_map_entry(idset,
1536 got_object_tag_get_object_id(tag), re);
1537 got_object_tag_close(tag);
1538 if (err)
1539 goto done;
1541 free(id);
1542 id = NULL;
1544 done:
1545 free(id);
1546 if (err) {
1547 got_reflist_object_id_map_free(*map);
1548 *map = NULL;
1550 return err;
1553 struct got_reflist_head *
1554 got_reflist_object_id_map_lookup(struct got_reflist_object_id_map *map,
1555 struct got_object_id *id)
1557 struct got_reflist_object_id_map_entry *ent;
1558 ent = got_object_idset_get(map->idset, id);
1559 if (ent)
1560 return &ent->refs;
1561 return NULL;
1564 static const struct got_error *
1565 free_id_map_entry(struct got_object_id *id, void *data, void *arg)
1567 struct got_reflist_object_id_map_entry *ent = data;
1569 got_ref_list_free(&ent->refs);
1570 free(ent);
1571 return NULL;
1574 void
1575 got_reflist_object_id_map_free(struct got_reflist_object_id_map *map)
1577 got_object_idset_for_each(map->idset, free_id_map_entry, NULL);
1578 got_object_idset_free(map->idset);
1579 free(map);