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 <dirent.h>
23 #include <limits.h>
24 #include <sha1.h>
25 #include <sha2.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_hash.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 struct got_object_id id;
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 time_t mtime;
92 /*
93 * Cached timestamp for got_ref_cmp_by_commit_timestamp_descending()
94 * and got_ref_cmp_tags().
95 */
96 time_t committer_time;
97 };
99 static const struct got_error *
100 alloc_ref(struct got_reference **ref, const char *name,
101 struct got_object_id *id, int flags, time_t mtime)
103 const struct got_error *err = NULL;
105 *ref = calloc(1, sizeof(**ref));
106 if (*ref == NULL)
107 return got_error_from_errno("calloc");
109 memcpy(&(*ref)->ref.ref.id, id, sizeof((*ref)->ref.ref.id));
110 (*ref)->flags = flags;
111 (*ref)->ref.ref.name = strdup(name);
112 (*ref)->mtime = mtime;
113 if ((*ref)->ref.ref.name == NULL) {
114 err = got_error_from_errno("strdup");
115 got_ref_close(*ref);
116 *ref = NULL;
118 return err;
121 static const struct got_error *
122 alloc_symref(struct got_reference **ref, const char *name,
123 const char *target_ref, int flags)
125 const struct got_error *err = NULL;
127 *ref = calloc(1, sizeof(**ref));
128 if (*ref == NULL)
129 return got_error_from_errno("calloc");
131 (*ref)->flags = GOT_REF_IS_SYMBOLIC | flags;
132 (*ref)->ref.symref.name = strdup(name);
133 if ((*ref)->ref.symref.name == NULL) {
134 err = got_error_from_errno("strdup");
135 got_ref_close(*ref);
136 *ref = NULL;
137 return err;
139 (*ref)->ref.symref.ref = strdup(target_ref);
140 if ((*ref)->ref.symref.ref == NULL) {
141 err = got_error_from_errno("strdup");
142 got_ref_close(*ref);
143 *ref = NULL;
145 return err;
148 static const struct got_error *
149 parse_symref(struct got_reference **ref, const char *name, const char *line)
151 if (line[0] == '\0')
152 return got_error(GOT_ERR_BAD_REF_DATA);
154 return alloc_symref(ref, name, line, 0);
157 static const struct got_error *
158 parse_ref_line(struct got_reference **ref, const char *name, const char *line,
159 time_t mtime, enum got_hash_algorithm algo)
161 struct got_object_id id;
163 if (strncmp(line, "ref: ", 5) == 0) {
164 line += 5;
165 return parse_symref(ref, name, line);
168 if (!got_parse_object_id(&id, line, algo))
169 return got_error(GOT_ERR_BAD_REF_DATA);
171 return alloc_ref(ref, name, &id, 0, mtime);
174 static const struct got_error *
175 parse_ref_file(struct got_reference **ref, const char *name,
176 const char *absname, const char *abspath, int lock,
177 enum got_hash_algorithm algo)
179 const struct got_error *err = NULL;
180 FILE *f;
181 char *line = NULL;
182 size_t linesize = 0;
183 ssize_t linelen;
184 struct got_lockfile *lf = NULL;
185 struct stat sb;
187 if (lock) {
188 err = got_lockfile_lock(&lf, abspath, -1);
189 if (err) {
190 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
191 err = got_error_not_ref(name);
192 return err;
196 f = fopen(abspath, "rbe");
197 if (f == NULL) {
198 if (errno != ENOTDIR && errno != ENOENT)
199 err = got_error_from_errno2("fopen", abspath);
200 else
201 err = got_error_not_ref(name);
202 if (lock)
203 got_lockfile_unlock(lf, -1);
204 return err;
206 if (fstat(fileno(f), &sb) == -1) {
207 err = got_error_from_errno2("fstat", abspath);
208 goto done;
211 linelen = getline(&line, &linesize, f);
212 if (linelen == -1) {
213 if (feof(f))
214 err = NULL; /* ignore empty files (could be locks) */
215 else {
216 if (errno == EISDIR)
217 err = got_error(GOT_ERR_NOT_REF);
218 else if (ferror(f))
219 err = got_ferror(f, GOT_ERR_IO);
220 else
221 err = got_error_from_errno2("getline", abspath);
223 if (lock)
224 got_lockfile_unlock(lf, -1);
225 goto done;
227 while (linelen > 0 && line[linelen - 1] == '\n') {
228 line[linelen - 1] = '\0';
229 linelen--;
232 err = parse_ref_line(ref, absname, line, sb.st_mtime, algo);
233 if (lock) {
234 if (err)
235 got_lockfile_unlock(lf, -1);
236 else {
237 if (*ref)
238 (*ref)->lf = lf;
239 else
240 got_lockfile_unlock(lf, -1);
243 done:
244 free(line);
245 if (fclose(f) == EOF && err == NULL) {
246 err = got_error_from_errno("fclose");
247 if (*ref) {
248 if (lock)
249 got_ref_unlock(*ref);
250 got_ref_close(*ref);
251 *ref = NULL;
254 return err;
257 static int
258 is_well_known_ref(const char *refname)
260 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
261 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
262 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
263 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
266 static char *
267 get_refs_dir_path(struct got_repository *repo, const char *refname)
269 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
270 return strdup(got_repo_get_path_git_dir(repo));
272 return got_repo_get_path_refs(repo);
275 const struct got_error *
276 got_ref_alloc(struct got_reference **ref, const char *name,
277 struct got_object_id *id)
279 if (!got_ref_name_is_valid(name))
280 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
282 return alloc_ref(ref, name, id, 0, 0);
285 const struct got_error *
286 got_ref_alloc_symref(struct got_reference **ref, const char *name,
287 struct got_reference *target_ref)
289 if (!got_ref_name_is_valid(name))
290 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
292 return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
295 static const struct got_error *
296 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
297 const char *line, time_t mtime, enum got_hash_algorithm algo)
299 struct got_object_id id;
300 const char *name;
302 *ref = NULL;
304 if (line[0] == '#' || line[0] == '^')
305 return NULL;
307 if (!got_parse_object_id(&id, line, algo))
308 return got_error(GOT_ERR_BAD_REF_DATA);
310 if (abs_refname) {
311 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
312 return NULL;
313 name = abs_refname;
314 } else
315 name = line + SHA1_DIGEST_STRING_LENGTH;
317 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED, mtime);
320 static const struct got_error *
321 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
322 int nsubdirs, const char *refname, time_t mtime,
323 enum got_hash_algorithm algo)
325 const struct got_error *err = NULL;
326 char *abs_refname;
327 char *line = NULL;
328 size_t linesize = 0;
329 ssize_t linelen;
330 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
332 *ref = NULL;
334 if (ref_is_absolute)
335 abs_refname = (char *)refname;
336 do {
337 linelen = getline(&line, &linesize, f);
338 if (linelen == -1) {
339 if (feof(f))
340 break;
341 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
342 break;
344 if (linelen > 0 && line[linelen - 1] == '\n')
345 line[linelen - 1] = '\0';
346 for (i = 0; i < nsubdirs; i++) {
347 if (!ref_is_absolute &&
348 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
349 refname) == -1)
350 return got_error_from_errno("asprintf");
351 err = parse_packed_ref_line(ref, abs_refname, line,
352 mtime, algo);
353 if (!ref_is_absolute)
354 free(abs_refname);
355 if (err || *ref != NULL)
356 break;
358 if (err)
359 break;
360 } while (*ref == NULL);
361 free(line);
363 return err;
366 static const struct got_error *
367 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
368 const char *name, int lock, enum got_hash_algorithm algo)
370 const struct got_error *err = NULL;
371 char *path = NULL;
372 char *absname = NULL;
373 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
374 int ref_is_well_known = (subdir[0] == '\0' && is_well_known_ref(name));
376 *ref = NULL;
378 if (!got_ref_name_is_valid(name))
379 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
381 if (ref_is_absolute || ref_is_well_known) {
382 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
383 return got_error_from_errno("asprintf");
384 absname = (char *)name;
385 } else {
386 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
387 subdir[0] ? "/" : "", name) == -1)
388 return got_error_from_errno("asprintf");
390 if (asprintf(&absname, "refs/%s%s%s",
391 subdir, subdir[0] ? "/" : "", name) == -1) {
392 err = got_error_from_errno("asprintf");
393 goto done;
397 err = parse_ref_file(ref, name, absname, path, lock, algo);
398 done:
399 if (!ref_is_absolute && !ref_is_well_known)
400 free(absname);
401 free(path);
402 return err;
405 const struct got_error *
406 got_ref_open(struct got_reference **ref, struct got_repository *repo,
407 const char *refname, int lock)
409 const struct got_error *err = NULL;
410 char *packed_refs_path = NULL, *path_refs = NULL;
411 const char *subdirs[] = {
412 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
413 };
414 size_t i;
415 int well_known = is_well_known_ref(refname);
416 struct got_lockfile *lf = NULL;
418 *ref = NULL;
420 path_refs = get_refs_dir_path(repo, refname);
421 if (path_refs == NULL) {
422 err = got_error_from_errno2("get_refs_dir_path", refname);
423 goto done;
426 if (well_known) {
427 err = open_ref(ref, path_refs, "", refname, lock,
428 got_repo_get_object_format(repo));
429 } else {
430 FILE *f;
432 /* Search on-disk refs before packed refs! */
433 for (i = 0; i < nitems(subdirs); i++) {
434 err = open_ref(ref, path_refs, subdirs[i], refname,
435 lock, got_repo_get_object_format(repo));
436 if ((err && err->code != GOT_ERR_NOT_REF) || *ref)
437 goto done;
440 packed_refs_path = got_repo_get_path_packed_refs(repo);
441 if (packed_refs_path == NULL) {
442 err = got_error_from_errno(
443 "got_repo_get_path_packed_refs");
444 goto done;
447 if (lock) {
448 err = got_lockfile_lock(&lf, packed_refs_path, -1);
449 if (err)
450 goto done;
452 f = fopen(packed_refs_path, "rbe");
453 if (f != NULL) {
454 struct stat sb;
455 if (fstat(fileno(f), &sb) == -1) {
456 err = got_error_from_errno2("fstat",
457 packed_refs_path);
458 goto done;
460 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
461 refname, sb.st_mtime,
462 got_repo_get_object_format(repo));
463 if (!err) {
464 if (fclose(f) == EOF) {
465 err = got_error_from_errno("fclose");
466 got_ref_close(*ref);
467 *ref = NULL;
468 } else if (*ref)
469 (*ref)->lf = lf;
473 done:
474 if (!err && *ref == NULL)
475 err = got_error_not_ref(refname);
476 if (err && lf)
477 got_lockfile_unlock(lf, -1);
478 free(packed_refs_path);
479 free(path_refs);
480 return err;
483 void
484 got_ref_close(struct got_reference *ref)
486 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
487 free(ref->ref.symref.name);
488 free(ref->ref.symref.ref);
489 } else
490 free(ref->ref.ref.name);
491 free(ref);
494 struct got_reference *
495 got_ref_dup(struct got_reference *ref)
497 struct got_reference *ret;
499 ret = calloc(1, sizeof(*ret));
500 if (ret == NULL)
501 return NULL;
503 ret->flags = ref->flags;
504 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
505 ret->ref.symref.name = strdup(ref->ref.symref.name);
506 if (ret->ref.symref.name == NULL) {
507 free(ret);
508 return NULL;
510 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
511 if (ret->ref.symref.ref == NULL) {
512 free(ret->ref.symref.name);
513 free(ret);
514 return NULL;
516 } else {
517 ret->ref.ref.name = strdup(ref->ref.ref.name);
518 if (ret->ref.ref.name == NULL) {
519 free(ret);
520 return NULL;
522 memcpy(&ret->ref.ref.id, &ref->ref.ref.id,
523 sizeof(ret->ref.ref.id));
526 return ret;
529 const struct got_error *
530 got_reflist_entry_dup(struct got_reflist_entry **newp,
531 struct got_reflist_entry *re)
533 const struct got_error *err = NULL;
534 struct got_reflist_entry *new;
536 *newp = NULL;
538 new = malloc(sizeof(*new));
539 if (new == NULL)
540 return got_error_from_errno("malloc");
542 new->ref = got_ref_dup(re->ref);
543 if (new->ref == NULL) {
544 err = got_error_from_errno("got_ref_dup");
545 free(new);
546 return err;
549 *newp = new;
550 return NULL;
553 const struct got_error *
554 got_ref_resolve_symbolic(struct got_reference **resolved,
555 struct got_repository *repo, struct got_reference *ref)
557 struct got_reference *nextref;
558 const struct got_error *err;
560 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
561 if (err)
562 return err;
564 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
565 err = got_ref_resolve_symbolic(resolved, repo, nextref);
566 else
567 *resolved = got_ref_dup(nextref);
569 got_ref_close(nextref);
570 return err;
573 static const struct got_error *
574 ref_resolve(struct got_object_id **id, struct got_repository *repo,
575 struct got_reference *ref, int recursion)
577 const struct got_error *err;
579 if (recursion <= 0)
580 return got_error_msg(GOT_ERR_RECURSION,
581 "reference recursion limit reached");
583 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
584 struct got_reference *resolved = NULL;
585 err = got_ref_resolve_symbolic(&resolved, repo, ref);
586 if (err == NULL)
587 err = ref_resolve(id, repo, resolved, --recursion);
588 if (resolved)
589 got_ref_close(resolved);
590 return err;
593 *id = calloc(1, sizeof(**id));
594 if (*id == NULL)
595 return got_error_from_errno("calloc");
596 memcpy(*id, &ref->ref.ref.id, sizeof(**id));
597 return NULL;
600 const struct got_error *
601 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
602 struct got_reference *ref)
604 return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
607 char *
608 got_ref_to_str(struct got_reference *ref)
610 char *str;
612 if (ref->flags & GOT_REF_IS_SYMBOLIC)
613 return strdup(ref->ref.symref.ref);
615 if (got_object_id_str(&str, &ref->ref.ref.id) != NULL)
616 return NULL;
618 return str;
621 const char *
622 got_ref_get_name(struct got_reference *ref)
624 if (ref->flags & GOT_REF_IS_SYMBOLIC)
625 return ref->ref.symref.name;
627 return ref->ref.ref.name;
630 const char *
631 got_ref_get_symref_target(struct got_reference *ref)
633 if (ref->flags & GOT_REF_IS_SYMBOLIC)
634 return ref->ref.symref.ref;
636 return NULL;
639 time_t
640 got_ref_get_mtime(struct got_reference *ref)
642 return ref->mtime;
645 const struct got_error *
646 got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
647 struct got_reference* re2)
649 const char *name1 = got_ref_get_name(re1);
650 const char *name2 = got_ref_get_name(re2);
652 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
653 return NULL;
656 static const struct got_error *
657 get_committer_time(struct got_reference *ref, struct got_repository *repo)
659 const struct got_error *err = NULL;
660 int obj_type;
661 struct got_commit_object *commit = NULL;
662 struct got_tag_object *tag = NULL;
663 struct got_object_id *id = NULL;
665 err = got_ref_resolve(&id, repo, ref);
666 if (err)
667 return err;
669 err = got_object_get_type(&obj_type, repo, id);
670 if (err)
671 goto done;
673 switch (obj_type) {
674 case GOT_OBJ_TYPE_COMMIT:
675 err = got_object_open_as_commit(&commit, repo, id);
676 if (err)
677 goto done;
678 ref->committer_time =
679 got_object_commit_get_committer_time(commit);
680 break;
681 case GOT_OBJ_TYPE_TAG:
682 err = got_object_open_as_tag(&tag, repo, id);
683 if (err)
684 goto done;
685 ref->committer_time = got_object_tag_get_tagger_time(tag);
686 break;
687 default:
688 /* best effort for other object types */
689 ref->committer_time = got_ref_get_mtime(ref);
690 break;
692 done:
693 free(id);
694 if (commit)
695 got_object_commit_close(commit);
696 if (tag)
697 got_object_tag_close(tag);
698 return err;
701 const struct got_error *
702 got_ref_cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
703 struct got_reference *ref2)
705 const struct got_error *err = NULL;
706 struct got_repository *repo = arg;
708 *cmp = 0;
710 if (ref1->committer_time == 0) {
711 err = get_committer_time(ref1, repo);
712 if (err)
713 return err;
715 if (ref2->committer_time == 0) {
716 err = get_committer_time(ref2, repo);
717 if (err)
718 return err;
721 /* Put latest tags first. */
722 if (ref1->committer_time < ref2->committer_time)
723 *cmp = 1;
724 else if (ref1->committer_time > ref2->committer_time)
725 *cmp = -1;
726 else
727 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
729 return err;
732 const struct got_error *
733 got_ref_cmp_by_commit_timestamp_descending(void *arg, int *cmp,
734 struct got_reference *ref1, struct got_reference *ref2)
736 const struct got_error *err = NULL;
737 struct got_repository *repo = arg;
739 *cmp = 0;
741 if (ref1->committer_time == 0) {
742 err = get_committer_time(ref1, repo);
743 if (err)
744 return err;
746 if (ref2->committer_time == 0) {
747 err = get_committer_time(ref2, repo);
748 if (err)
749 return err;
752 if (ref1->committer_time < ref2->committer_time)
753 *cmp = 1;
754 else if (ref2->committer_time < ref1->committer_time)
755 *cmp = -1;
756 else
757 return got_ref_cmp_by_name(arg, cmp, ref1, ref2);
759 return err;
762 const struct got_error *
763 got_reflist_insert(struct got_reflist_entry **newp,
764 struct got_reflist_head *refs, struct got_reference *ref,
765 got_ref_cmp_cb cmp_cb, void *cmp_arg)
767 const struct got_error *err;
768 struct got_reflist_entry *new, *re;
769 int cmp;
771 *newp = NULL;
773 if (cmp_cb != got_ref_cmp_by_name && (ref->flags & GOT_REF_IS_PACKED)) {
774 /*
775 * If we are not sorting elements by name then we must still
776 * detect collisions between a packed ref and an on-disk ref
777 * using the same name. On-disk refs take precedence and are
778 * already present on the list before packed refs get added.
779 */
780 TAILQ_FOREACH(re, refs, entry) {
781 err = got_ref_cmp_by_name(NULL, &cmp, re->ref, ref);
782 if (err)
783 return err;
784 if (cmp == 0)
785 return NULL;
789 new = malloc(sizeof(*new));
790 if (new == NULL)
791 return got_error_from_errno("malloc");
792 new->ref = ref;
793 *newp = new;
795 /*
796 * We must de-duplicate entries on insert because packed-refs may
797 * contain redundant entries. On-disk refs take precedence.
798 * This code assumes that on-disk revs are read before packed-refs.
799 * We're iterating the list anyway, so insert elements sorted by name.
801 * Many callers will provide paths in a somewhat sorted order.
802 * Iterating backwards from the tail of the list should be more
803 * efficient than traversing through the entire list each time
804 * an element is inserted.
805 */
806 re = TAILQ_LAST(refs, got_reflist_head);
807 while (re) {
808 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
809 if (err)
810 return err;
811 if (cmp == 0) {
812 /* duplicate */
813 free(new);
814 *newp = NULL;
815 return NULL;
816 } else if (cmp < 0) {
817 TAILQ_INSERT_AFTER(refs, re, new, entry);
818 return NULL;
820 re = TAILQ_PREV(re, got_reflist_head, entry);
823 TAILQ_INSERT_HEAD(refs, new, entry);
824 return NULL;
827 const struct got_error *
828 got_reflist_sort(struct got_reflist_head *refs,
829 got_ref_cmp_cb cmp_cb, void *cmp_arg)
831 const struct got_error *err = NULL;
832 struct got_reflist_entry *re, *tmp, *new;
833 struct got_reflist_head sorted;
835 TAILQ_INIT(&sorted);
837 TAILQ_FOREACH_SAFE(re, refs, entry, tmp) {
838 struct got_reference *ref = re->ref;
839 TAILQ_REMOVE(refs, re, entry);
840 free(re);
841 err = got_reflist_insert(&new, &sorted, ref, cmp_cb, cmp_arg);
842 if (err || new == NULL /* duplicate */)
843 got_ref_close(ref);
844 if (err)
845 return err;
848 TAILQ_CONCAT(refs, &sorted, entry);
849 return NULL;
852 static const struct got_error *
853 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
854 const char *subdir, struct got_repository *repo,
855 got_ref_cmp_cb cmp_cb, void *cmp_arg)
857 const struct got_error *err = NULL;
858 DIR *d = NULL;
859 char *path_subdir;
861 while (subdir[0] == '/')
862 subdir++;
864 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
865 return got_error_from_errno("asprintf");
867 d = opendir(path_subdir);
868 if (d == NULL)
869 goto done;
871 for (;;) {
872 struct dirent *dent;
873 struct got_reference *ref;
874 char *child;
875 int type;
877 dent = readdir(d);
878 if (dent == NULL)
879 break;
881 if (strcmp(dent->d_name, ".") == 0 ||
882 strcmp(dent->d_name, "..") == 0)
883 continue;
885 err = got_path_dirent_type(&type, path_subdir, dent);
886 if (err)
887 break;
889 switch (type) {
890 case DT_REG:
891 err = open_ref(&ref, path_refs, subdir, dent->d_name,
892 0, got_repo_get_object_format(repo));
893 if (err && err->code == GOT_ERR_BAD_REF_NAME)
894 break;
895 if (err)
896 goto done;
897 if (ref) {
898 struct got_reflist_entry *new;
899 err = got_reflist_insert(&new, refs, ref,
900 cmp_cb, cmp_arg);
901 if (err || new == NULL /* duplicate */)
902 got_ref_close(ref);
903 if (err)
904 goto done;
906 break;
907 case DT_DIR:
908 if (asprintf(&child, "%s%s%s", subdir,
909 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
910 err = got_error_from_errno("asprintf");
911 break;
913 err = gather_on_disk_refs(refs, path_refs, child, repo,
914 cmp_cb, cmp_arg);
915 free(child);
916 break;
917 default:
918 break;
921 done:
922 if (d)
923 closedir(d);
924 free(path_subdir);
925 return err;
928 const struct got_error *
929 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
930 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
932 const struct got_error *err;
933 char *packed_refs_path = NULL, *path_refs = NULL;
934 char *abs_namespace = NULL, *buf = NULL;
935 const char *ondisk_ref_namespace = NULL;
936 char *line = NULL;
937 FILE *f = NULL;
938 struct got_reference *ref;
939 struct got_reflist_entry *new;
941 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
942 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
943 if (path_refs == NULL) {
944 err = got_error_from_errno("get_refs_dir_path");
945 goto done;
947 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0,
948 got_repo_get_object_format(repo));
949 if (err)
950 goto done;
951 err = got_reflist_insert(&new, refs, ref, cmp_cb, cmp_arg);
952 if (err || new == NULL /* duplicate */)
953 got_ref_close(ref);
954 if (err && err->code != GOT_ERR_NOT_REF)
955 goto done;
956 } else {
957 /* Try listing a single reference. */
958 const char *refname = ref_namespace;
959 path_refs = get_refs_dir_path(repo, refname);
960 if (path_refs == NULL) {
961 err = got_error_from_errno("get_refs_dir_path");
962 goto done;
964 err = open_ref(&ref, path_refs, "", refname, 0,
965 got_repo_get_object_format(repo));
966 if (err) {
967 if (err->code != GOT_ERR_NOT_REF)
968 goto done;
969 /* Try to look up references in a given namespace. */
970 } else {
971 err = got_reflist_insert(&new, refs, ref,
972 cmp_cb, cmp_arg);
973 if (err || new == NULL /* duplicate */)
974 got_ref_close(ref);
975 return err;
979 if (ref_namespace) {
980 size_t len;
981 /* Canonicalize the path to eliminate double-slashes if any. */
982 if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
983 err = got_error_from_errno("asprintf");
984 goto done;
986 len = strlen(abs_namespace) + 1;
987 buf = malloc(len);
988 if (buf == NULL) {
989 err = got_error_from_errno("malloc");
990 goto done;
992 err = got_canonpath(abs_namespace, buf, len);
993 if (err)
994 goto done;
995 ondisk_ref_namespace = buf;
996 while (ondisk_ref_namespace[0] == '/')
997 ondisk_ref_namespace++;
998 if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
999 ondisk_ref_namespace += 5;
1000 else if (strcmp(ondisk_ref_namespace, "refs") == 0)
1001 ondisk_ref_namespace = "";
1004 /* Gather on-disk refs before parsing packed-refs. */
1005 free(path_refs);
1006 path_refs = get_refs_dir_path(repo, "");
1007 if (path_refs == NULL) {
1008 err = got_error_from_errno("get_refs_dir_path");
1009 goto done;
1011 err = gather_on_disk_refs(refs, path_refs,
1012 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
1013 cmp_cb, cmp_arg);
1014 if (err)
1015 goto done;
1018 * The packed-refs file may contain redundant entries, in which
1019 * case on-disk refs take precedence.
1021 packed_refs_path = got_repo_get_path_packed_refs(repo);
1022 if (packed_refs_path == NULL) {
1023 err = got_error_from_errno("got_repo_get_path_packed_refs");
1024 goto done;
1027 f = fopen(packed_refs_path, "re");
1028 if (f) {
1029 size_t linesize = 0;
1030 ssize_t linelen;
1031 struct stat sb;
1033 if (fstat(fileno(f), &sb) == -1) {
1034 err = got_error_from_errno2("fstat", packed_refs_path);
1035 goto done;
1037 for (;;) {
1038 linelen = getline(&line, &linesize, f);
1039 if (linelen == -1) {
1040 if (feof(f))
1041 break;
1042 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1043 goto done;
1045 if (linelen > 0 && line[linelen - 1] == '\n')
1046 line[linelen - 1] = '\0';
1047 err = parse_packed_ref_line(&ref, NULL, line,
1048 sb.st_mtime, got_repo_get_object_format(repo));
1049 if (err)
1050 goto done;
1051 if (ref) {
1052 if (ref_namespace) {
1053 const char *name;
1054 name = got_ref_get_name(ref);
1055 if (!got_path_is_child(name,
1056 ref_namespace,
1057 strlen(ref_namespace))) {
1058 got_ref_close(ref);
1059 continue;
1062 err = got_reflist_insert(&new, refs, ref,
1063 cmp_cb, cmp_arg);
1064 if (err || new == NULL /* duplicate */)
1065 got_ref_close(ref);
1066 if (err)
1067 goto done;
1071 done:
1072 free(packed_refs_path);
1073 free(abs_namespace);
1074 free(buf);
1075 free(line);
1076 free(path_refs);
1077 if (f && fclose(f) == EOF && err == NULL)
1078 err = got_error_from_errno("fclose");
1079 return err;
1082 void
1083 got_ref_list_free(struct got_reflist_head *refs)
1085 struct got_reflist_entry *re;
1087 while ((re = TAILQ_FIRST(refs))) {
1088 TAILQ_REMOVE(refs, re, entry);
1089 got_ref_close(re->ref);
1090 free(re);
1094 int
1095 got_ref_is_symbolic(struct got_reference *ref)
1097 return (ref->flags & GOT_REF_IS_SYMBOLIC);
1100 const struct got_error *
1101 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1103 if (ref->flags & GOT_REF_IS_SYMBOLIC)
1104 return got_error(GOT_ERR_BAD_REF_TYPE);
1106 memcpy(&ref->ref.ref.id, id, sizeof(ref->ref.ref.id));
1107 return NULL;
1110 const struct got_error *
1111 got_ref_change_symref(struct got_reference *ref, const char *refname)
1113 char *new_name;
1115 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1116 return got_error(GOT_ERR_BAD_REF_TYPE);
1118 new_name = strdup(refname);
1119 if (new_name == NULL)
1120 return got_error_from_errno("strdup");
1122 free(ref->ref.symref.ref);
1123 ref->ref.symref.ref = new_name;
1124 return NULL;
1127 const struct got_error *
1128 got_ref_change_symref_to_ref(struct got_reference *symref,
1129 struct got_object_id *id)
1131 if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1132 return got_error(GOT_ERR_BAD_REF_TYPE);
1134 symref->ref.ref.name = symref->ref.symref.name;
1135 memcpy(&symref->ref.ref.id, id, sizeof(symref->ref.ref.id));
1136 symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1137 return NULL;
1140 const struct got_error *
1141 got_ref_write(struct got_reference *ref, struct got_repository *repo)
1143 const struct got_error *err = NULL, *unlock_err = NULL;
1144 const char *name = got_ref_get_name(ref);
1145 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1146 struct got_lockfile *lf = NULL;
1147 FILE *f = NULL;
1148 size_t n;
1149 struct stat sb;
1151 path_refs = get_refs_dir_path(repo, name);
1152 if (path_refs == NULL) {
1153 err = got_error_from_errno2("get_refs_dir_path", name);
1154 goto done;
1157 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1158 err = got_error_from_errno("asprintf");
1159 goto done;
1162 err = got_opentemp_named(&tmppath, &f, path, "");
1163 if (err) {
1164 char *parent;
1165 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1166 goto done;
1167 err = got_path_dirname(&parent, path);
1168 if (err)
1169 goto done;
1170 err = got_path_mkdir(parent);
1171 free(parent);
1172 if (err)
1173 goto done;
1174 err = got_opentemp_named(&tmppath, &f, path, "");
1175 if (err)
1176 goto done;
1179 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1180 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1181 if (n != strlen(ref->ref.symref.ref) + 6) {
1182 err = got_ferror(f, GOT_ERR_IO);
1183 goto done;
1185 } else {
1186 char *hex;
1187 size_t len;
1189 err = got_object_id_str(&hex, &ref->ref.ref.id);
1190 if (err)
1191 goto done;
1192 len = strlen(hex);
1193 n = fprintf(f, "%s\n", hex);
1194 free(hex);
1195 if (n != len + 1) {
1196 err = got_ferror(f, GOT_ERR_IO);
1197 goto done;
1201 if (ref->lf == NULL) {
1202 err = got_lockfile_lock(&lf, path, -1);
1203 if (err)
1204 goto done;
1207 /* XXX: check if old content matches our expectations? */
1209 if (stat(path, &sb) != 0) {
1210 if (errno != ENOENT) {
1211 err = got_error_from_errno2("stat", path);
1212 goto done;
1214 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1217 if (fchmod(fileno(f), sb.st_mode) != 0) {
1218 err = got_error_from_errno2("fchmod", tmppath);
1219 goto done;
1222 if (rename(tmppath, path) != 0) {
1223 err = got_error_from_errno3("rename", tmppath, path);
1224 goto done;
1226 free(tmppath);
1227 tmppath = NULL;
1229 if (stat(path, &sb) == -1) {
1230 err = got_error_from_errno2("stat", path);
1231 goto done;
1233 ref->mtime = sb.st_mtime;
1234 done:
1235 if (ref->lf == NULL && lf)
1236 unlock_err = got_lockfile_unlock(lf, -1);
1237 if (f) {
1238 if (fclose(f) == EOF && err == NULL)
1239 err = got_error_from_errno("fclose");
1241 free(path_refs);
1242 free(path);
1243 if (tmppath) {
1244 if (unlink(tmppath) == -1 && err == NULL)
1245 err = got_error_from_errno2("unlink", tmppath);
1246 free(tmppath);
1248 return err ? err : unlock_err;
1251 static const struct got_error *
1252 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1254 const struct got_error *err = NULL, *unlock_err = NULL;
1255 struct got_lockfile *lf = NULL;
1256 FILE *f = NULL, *tmpf = NULL;
1257 char *line = NULL, *packed_refs_path, *tmppath = NULL;
1258 size_t linesize = 0;
1259 struct got_reflist_head refs;
1260 int found_delref = 0;
1262 /* The packed-refs file does not cotain symbolic references. */
1263 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1264 return got_error(GOT_ERR_BAD_REF_DATA);
1266 TAILQ_INIT(&refs);
1268 packed_refs_path = got_repo_get_path_packed_refs(repo);
1269 if (packed_refs_path == NULL)
1270 return got_error_from_errno("got_repo_get_path_packed_refs");
1272 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path, "");
1273 if (err)
1274 goto done;
1276 if (delref->lf == NULL) {
1277 err = got_lockfile_lock(&lf, packed_refs_path, -1);
1278 if (err)
1279 goto done;
1282 f = fopen(packed_refs_path, "re");
1283 if (f == NULL) {
1284 err = got_error_from_errno2("fopen", packed_refs_path);
1285 goto done;
1287 for (;;) {
1288 ssize_t linelen;
1289 struct got_reference *ref;
1290 struct got_reflist_entry *new;
1292 linelen = getline(&line, &linesize, f);
1293 if (linelen == -1) {
1294 if (feof(f))
1295 break;
1296 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1297 goto done;
1299 if (linelen > 0 && line[linelen - 1] == '\n')
1300 line[linelen - 1] = '\0';
1301 err = parse_packed_ref_line(&ref, NULL, line, 0,
1302 got_repo_get_object_format(repo));
1303 if (err)
1304 goto done;
1305 if (ref == NULL)
1306 continue;
1308 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1309 got_object_id_cmp(&ref->ref.ref.id,
1310 &delref->ref.ref.id) == 0) {
1311 found_delref = 1;
1312 got_ref_close(ref);
1313 continue;
1316 err = got_reflist_insert(&new, &refs, ref,
1317 got_ref_cmp_by_name, NULL);
1318 if (err || new == NULL /* duplicate */)
1319 got_ref_close(ref);
1320 if (err)
1321 goto done;
1324 if (found_delref) {
1325 struct got_reflist_entry *re;
1326 size_t n;
1327 struct stat sb;
1329 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1330 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1331 err = got_ferror(f, GOT_ERR_IO);
1332 goto done;
1335 TAILQ_FOREACH(re, &refs, entry) {
1336 char *hex;
1337 size_t len;
1339 err = got_object_id_str(&hex, &re->ref->ref.ref.id);
1340 if (err)
1341 goto done;
1342 len = strlen(hex);
1343 n = fprintf(tmpf, "%s ", hex);
1344 free(hex);
1345 if (n != len + 1) {
1346 err = got_ferror(f, GOT_ERR_IO);
1347 goto done;
1350 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1351 if (n != strlen(re->ref->ref.ref.name) + 1) {
1352 err = got_ferror(f, GOT_ERR_IO);
1353 goto done;
1357 if (fflush(tmpf) != 0) {
1358 err = got_error_from_errno("fflush");
1359 goto done;
1362 if (fstat(fileno(f), &sb) != 0) {
1363 if (errno != ENOENT) {
1364 err = got_error_from_errno2("fstat",
1365 packed_refs_path);
1366 goto done;
1368 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1371 if (fchmod(fileno(tmpf), sb.st_mode) != 0) {
1372 err = got_error_from_errno2("fchmod", tmppath);
1373 goto done;
1376 if (rename(tmppath, packed_refs_path) != 0) {
1377 err = got_error_from_errno3("rename", tmppath,
1378 packed_refs_path);
1379 goto done;
1381 free(tmppath);
1382 tmppath = NULL;
1384 done:
1385 if (delref->lf == NULL && lf)
1386 unlock_err = got_lockfile_unlock(lf, -1);
1387 if (f) {
1388 if (fclose(f) == EOF && err == NULL)
1389 err = got_error_from_errno("fclose");
1391 if (tmppath && unlink(tmppath) == -1 && err == NULL)
1392 err = got_error_from_errno2("unlink", tmppath);
1393 if (tmpf && fclose(tmpf) == EOF && err == NULL)
1394 err = got_error_from_errno("fclose");
1395 free(tmppath);
1396 free(packed_refs_path);
1397 free(line);
1398 got_ref_list_free(&refs);
1399 return err ? err : unlock_err;
1402 static const struct got_error *
1403 delete_loose_ref(struct got_reference *ref, struct got_repository *repo)
1405 const struct got_error *err = NULL, *unlock_err = NULL;
1406 const char *name = got_ref_get_name(ref);
1407 char *path_refs = NULL, *path = NULL;
1408 struct got_lockfile *lf = NULL;
1410 path_refs = get_refs_dir_path(repo, name);
1411 if (path_refs == NULL) {
1412 err = got_error_from_errno2("get_refs_dir_path", name);
1413 goto done;
1416 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1417 err = got_error_from_errno("asprintf");
1418 goto done;
1421 if (ref->lf == NULL) {
1422 err = got_lockfile_lock(&lf, path, -1);
1423 if (err)
1424 goto done;
1427 /* XXX: check if old content matches our expectations? */
1429 if (unlink(path) == -1)
1430 err = got_error_from_errno2("unlink", path);
1431 done:
1432 if (ref->lf == NULL && lf)
1433 unlock_err = got_lockfile_unlock(lf, -1);
1435 free(path_refs);
1436 free(path);
1437 return err ? err : unlock_err;
1440 const struct got_error *
1441 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1443 const struct got_error *err = NULL;
1444 struct got_reference *ref2;
1446 if (ref->flags & GOT_REF_IS_PACKED) {
1447 err = delete_packed_ref(ref, repo);
1448 if (err)
1449 return err;
1451 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1452 if (err) {
1453 if (err->code == GOT_ERR_NOT_REF)
1454 return NULL;
1455 return err;
1458 err = delete_loose_ref(ref2, repo);
1459 got_ref_close(ref2);
1460 return err;
1461 } else {
1462 err = delete_loose_ref(ref, repo);
1463 if (err)
1464 return err;
1466 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1467 if (err) {
1468 if (err->code == GOT_ERR_NOT_REF)
1469 return NULL;
1470 return err;
1473 err = delete_packed_ref(ref2, repo);
1474 got_ref_close(ref2);
1475 return err;
1479 const struct got_error *
1480 got_ref_unlock(struct got_reference *ref)
1482 const struct got_error *err;
1483 err = got_lockfile_unlock(ref->lf, -1);
1484 ref->lf = NULL;
1485 return err;
1488 struct got_reflist_object_id_map {
1489 struct got_object_idset *idset;
1492 struct got_reflist_object_id_map_entry {
1493 struct got_reflist_head refs;
1496 static const struct got_error *
1497 add_object_id_map_entry(struct got_object_idset *idset,
1498 struct got_object_id *id, struct got_reflist_entry *re)
1500 const struct got_error *err = NULL;
1501 struct got_reflist_object_id_map_entry *ent;
1502 struct got_reflist_entry *new;
1504 ent = got_object_idset_get(idset, id);
1505 if (ent == NULL) {
1506 ent = malloc(sizeof(*ent));
1507 if (ent == NULL)
1508 return got_error_from_errno("malloc");
1510 TAILQ_INIT(&ent->refs);
1511 err = got_object_idset_add(idset, id, ent);
1512 if (err) {
1513 free(ent);
1514 return err;
1518 err = got_reflist_entry_dup(&new, re);
1519 if (err)
1520 return err;
1522 TAILQ_INSERT_TAIL(&ent->refs, new, entry);
1523 return NULL;
1526 const struct got_error *
1527 got_reflist_object_id_map_create(struct got_reflist_object_id_map **map,
1528 struct got_reflist_head *refs, struct got_repository *repo)
1530 const struct got_error *err = NULL;
1531 struct got_object_idset *idset;
1532 struct got_object_id *id = NULL;
1533 struct got_reflist_entry *re;
1535 idset = got_object_idset_alloc();
1536 if (idset == NULL)
1537 return got_error_from_errno("got_object_idset_alloc");
1539 *map = malloc(sizeof(**map));
1540 if (*map == NULL) {
1541 got_object_idset_free(idset);
1542 return got_error_from_errno("malloc");
1544 (*map)->idset = idset;
1546 TAILQ_FOREACH(re, refs, entry) {
1547 struct got_tag_object *tag = NULL;
1549 err = got_ref_resolve(&id, repo, re->ref);
1550 if (err)
1551 goto done;
1553 err = add_object_id_map_entry(idset, id, re);
1554 if (err)
1555 goto done;
1557 if (strstr(got_ref_get_name(re->ref), "/tags/") == NULL) {
1558 free(id);
1559 id = NULL;
1560 continue;
1563 err = got_object_open_as_tag(&tag, repo, id);
1564 if (err) {
1565 if (err->code != GOT_ERR_OBJ_TYPE)
1566 goto done;
1567 /* Ref points at something other than a tag. */
1568 err = NULL;
1569 tag = NULL;
1570 free(id);
1571 id = NULL;
1572 continue;
1575 err = add_object_id_map_entry(idset,
1576 got_object_tag_get_object_id(tag), re);
1577 got_object_tag_close(tag);
1578 if (err)
1579 goto done;
1581 free(id);
1582 id = NULL;
1584 done:
1585 free(id);
1586 if (err) {
1587 got_reflist_object_id_map_free(*map);
1588 *map = NULL;
1590 return err;
1593 struct got_reflist_head *
1594 got_reflist_object_id_map_lookup(struct got_reflist_object_id_map *map,
1595 struct got_object_id *id)
1597 struct got_reflist_object_id_map_entry *ent;
1598 ent = got_object_idset_get(map->idset, id);
1599 if (ent)
1600 return &ent->refs;
1601 return NULL;
1604 static const struct got_error *
1605 free_id_map_entry(struct got_object_id *id, void *data, void *arg)
1607 struct got_reflist_object_id_map_entry *ent = data;
1609 got_ref_list_free(&ent->refs);
1610 free(ent);
1611 return NULL;
1614 void
1615 got_reflist_object_id_map_free(struct got_reflist_object_id_map *map)
1617 got_object_idset_for_each(map->idset, free_id_map_entry, NULL);
1618 got_object_idset_free(map->idset);
1619 free(map);