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 /* Cached timestamp for got_ref_cmp_by_commit_timestamp_descending() */
93 time_t committer_time;
94 };
96 static const struct got_error *
97 alloc_ref(struct got_reference **ref, const char *name,
98 struct got_object_id *id, int flags, time_t mtime)
99 {
100 const struct got_error *err = NULL;
102 *ref = calloc(1, sizeof(**ref));
103 if (*ref == NULL)
104 return got_error_from_errno("calloc");
106 memcpy(&(*ref)->ref.ref.id, id, sizeof((*ref)->ref.ref.id));
107 (*ref)->flags = flags;
108 (*ref)->ref.ref.name = strdup(name);
109 (*ref)->mtime = mtime;
110 if ((*ref)->ref.ref.name == NULL) {
111 err = got_error_from_errno("strdup");
112 got_ref_close(*ref);
113 *ref = NULL;
115 return err;
118 static const struct got_error *
119 alloc_symref(struct got_reference **ref, const char *name,
120 const char *target_ref, int flags)
122 const struct got_error *err = NULL;
124 *ref = calloc(1, sizeof(**ref));
125 if (*ref == NULL)
126 return got_error_from_errno("calloc");
128 (*ref)->flags = GOT_REF_IS_SYMBOLIC | flags;
129 (*ref)->ref.symref.name = strdup(name);
130 if ((*ref)->ref.symref.name == NULL) {
131 err = got_error_from_errno("strdup");
132 got_ref_close(*ref);
133 *ref = NULL;
134 return err;
136 (*ref)->ref.symref.ref = strdup(target_ref);
137 if ((*ref)->ref.symref.ref == NULL) {
138 err = got_error_from_errno("strdup");
139 got_ref_close(*ref);
140 *ref = NULL;
142 return err;
145 static const struct got_error *
146 parse_symref(struct got_reference **ref, const char *name, const char *line)
148 if (line[0] == '\0')
149 return got_error(GOT_ERR_BAD_REF_DATA);
151 return alloc_symref(ref, name, line, 0);
154 static const struct got_error *
155 parse_ref_line(struct got_reference **ref, const char *name, const char *line,
156 time_t mtime)
158 enum got_hash_algorithm algo = GOT_HASH_SHA1;
159 struct got_object_id id;
161 if (strncmp(line, "ref: ", 5) == 0) {
162 line += 5;
163 return parse_symref(ref, name, line);
166 if (!got_parse_object_id(&id, line, algo))
167 return got_error(GOT_ERR_BAD_REF_DATA);
169 return alloc_ref(ref, name, &id, 0, mtime);
172 static const struct got_error *
173 parse_ref_file(struct got_reference **ref, const char *name,
174 const char *absname, const char *abspath, int lock)
176 const struct got_error *err = NULL;
177 FILE *f;
178 char *line = NULL;
179 size_t linesize = 0;
180 ssize_t linelen;
181 struct got_lockfile *lf = NULL;
182 struct stat sb;
184 if (lock) {
185 err = got_lockfile_lock(&lf, abspath, -1);
186 if (err) {
187 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
188 err = got_error_not_ref(name);
189 return err;
193 f = fopen(abspath, "rbe");
194 if (f == NULL) {
195 if (errno != ENOTDIR && errno != ENOENT)
196 err = got_error_from_errno2("fopen", abspath);
197 else
198 err = got_error_not_ref(name);
199 if (lock)
200 got_lockfile_unlock(lf, -1);
201 return err;
203 if (fstat(fileno(f), &sb) == -1) {
204 err = got_error_from_errno2("fstat", abspath);
205 goto done;
208 linelen = getline(&line, &linesize, f);
209 if (linelen == -1) {
210 if (feof(f))
211 err = NULL; /* ignore empty files (could be locks) */
212 else {
213 if (errno == EISDIR)
214 err = got_error(GOT_ERR_NOT_REF);
215 else if (ferror(f))
216 err = got_ferror(f, GOT_ERR_IO);
217 else
218 err = got_error_from_errno2("getline", abspath);
220 if (lock)
221 got_lockfile_unlock(lf, -1);
222 goto done;
224 while (linelen > 0 && line[linelen - 1] == '\n') {
225 line[linelen - 1] = '\0';
226 linelen--;
229 err = parse_ref_line(ref, absname, line, sb.st_mtime);
230 if (lock) {
231 if (err)
232 got_lockfile_unlock(lf, -1);
233 else {
234 if (*ref)
235 (*ref)->lf = lf;
236 else
237 got_lockfile_unlock(lf, -1);
240 done:
241 free(line);
242 if (fclose(f) == EOF && err == NULL) {
243 err = got_error_from_errno("fclose");
244 if (*ref) {
245 if (lock)
246 got_ref_unlock(*ref);
247 got_ref_close(*ref);
248 *ref = NULL;
251 return err;
254 static int
255 is_well_known_ref(const char *refname)
257 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
258 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
259 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
260 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
263 static char *
264 get_refs_dir_path(struct got_repository *repo, const char *refname)
266 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
267 return strdup(got_repo_get_path_git_dir(repo));
269 return got_repo_get_path_refs(repo);
272 const struct got_error *
273 got_ref_alloc(struct got_reference **ref, const char *name,
274 struct got_object_id *id)
276 if (!got_ref_name_is_valid(name))
277 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
279 return alloc_ref(ref, name, id, 0, 0);
282 const struct got_error *
283 got_ref_alloc_symref(struct got_reference **ref, const char *name,
284 struct got_reference *target_ref)
286 if (!got_ref_name_is_valid(name))
287 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
289 return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
292 static const struct got_error *
293 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
294 const char *line, time_t mtime)
296 enum got_hash_algorithm algo = GOT_HASH_SHA1;
297 struct got_object_id id;
298 const char *name;
300 *ref = NULL;
302 if (line[0] == '#' || line[0] == '^')
303 return NULL;
305 if (!got_parse_object_id(&id, line, algo))
306 return got_error(GOT_ERR_BAD_REF_DATA);
308 if (abs_refname) {
309 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
310 return NULL;
311 name = abs_refname;
312 } else
313 name = line + SHA1_DIGEST_STRING_LENGTH;
315 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED, mtime);
318 static const struct got_error *
319 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
320 int nsubdirs, const char *refname, time_t mtime)
322 const struct got_error *err = NULL;
323 char *abs_refname;
324 char *line = NULL;
325 size_t linesize = 0;
326 ssize_t linelen;
327 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
329 *ref = NULL;
331 if (ref_is_absolute)
332 abs_refname = (char *)refname;
333 do {
334 linelen = getline(&line, &linesize, f);
335 if (linelen == -1) {
336 if (feof(f))
337 break;
338 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
339 break;
341 if (linelen > 0 && line[linelen - 1] == '\n')
342 line[linelen - 1] = '\0';
343 for (i = 0; i < nsubdirs; i++) {
344 if (!ref_is_absolute &&
345 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
346 refname) == -1)
347 return got_error_from_errno("asprintf");
348 err = parse_packed_ref_line(ref, abs_refname, line,
349 mtime);
350 if (!ref_is_absolute)
351 free(abs_refname);
352 if (err || *ref != NULL)
353 break;
355 if (err)
356 break;
357 } while (*ref == NULL);
358 free(line);
360 return err;
363 static const struct got_error *
364 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
365 const char *name, int lock)
367 const struct got_error *err = NULL;
368 char *path = NULL;
369 char *absname = NULL;
370 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
371 int ref_is_well_known = (subdir[0] == '\0' && is_well_known_ref(name));
373 *ref = NULL;
375 if (!got_ref_name_is_valid(name))
376 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
378 if (ref_is_absolute || ref_is_well_known) {
379 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
380 return got_error_from_errno("asprintf");
381 absname = (char *)name;
382 } else {
383 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
384 subdir[0] ? "/" : "", name) == -1)
385 return got_error_from_errno("asprintf");
387 if (asprintf(&absname, "refs/%s%s%s",
388 subdir, subdir[0] ? "/" : "", name) == -1) {
389 err = got_error_from_errno("asprintf");
390 goto done;
394 err = parse_ref_file(ref, name, absname, path, lock);
395 done:
396 if (!ref_is_absolute && !ref_is_well_known)
397 free(absname);
398 free(path);
399 return err;
402 const struct got_error *
403 got_ref_open(struct got_reference **ref, struct got_repository *repo,
404 const char *refname, int lock)
406 const struct got_error *err = NULL;
407 char *packed_refs_path = NULL, *path_refs = NULL;
408 const char *subdirs[] = {
409 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
410 };
411 size_t i;
412 int well_known = is_well_known_ref(refname);
413 struct got_lockfile *lf = NULL;
415 *ref = NULL;
417 path_refs = get_refs_dir_path(repo, refname);
418 if (path_refs == NULL) {
419 err = got_error_from_errno2("get_refs_dir_path", refname);
420 goto done;
423 if (well_known) {
424 err = open_ref(ref, path_refs, "", refname, lock);
425 } else {
426 FILE *f;
428 /* Search on-disk refs before packed refs! */
429 for (i = 0; i < nitems(subdirs); i++) {
430 err = open_ref(ref, path_refs, subdirs[i], refname,
431 lock);
432 if ((err && err->code != GOT_ERR_NOT_REF) || *ref)
433 goto done;
436 packed_refs_path = got_repo_get_path_packed_refs(repo);
437 if (packed_refs_path == NULL) {
438 err = got_error_from_errno(
439 "got_repo_get_path_packed_refs");
440 goto done;
443 if (lock) {
444 err = got_lockfile_lock(&lf, packed_refs_path, -1);
445 if (err)
446 goto done;
448 f = fopen(packed_refs_path, "rbe");
449 if (f != NULL) {
450 struct stat sb;
451 if (fstat(fileno(f), &sb) == -1) {
452 err = got_error_from_errno2("fstat",
453 packed_refs_path);
454 goto done;
456 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
457 refname, sb.st_mtime);
458 if (!err) {
459 if (fclose(f) == EOF) {
460 err = got_error_from_errno("fclose");
461 got_ref_close(*ref);
462 *ref = NULL;
463 } else if (*ref)
464 (*ref)->lf = lf;
468 done:
469 if (!err && *ref == NULL)
470 err = got_error_not_ref(refname);
471 if (err && lf)
472 got_lockfile_unlock(lf, -1);
473 free(packed_refs_path);
474 free(path_refs);
475 return err;
478 void
479 got_ref_close(struct got_reference *ref)
481 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
482 free(ref->ref.symref.name);
483 free(ref->ref.symref.ref);
484 } else
485 free(ref->ref.ref.name);
486 free(ref);
489 struct got_reference *
490 got_ref_dup(struct got_reference *ref)
492 struct got_reference *ret;
494 ret = calloc(1, sizeof(*ret));
495 if (ret == NULL)
496 return NULL;
498 ret->flags = ref->flags;
499 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
500 ret->ref.symref.name = strdup(ref->ref.symref.name);
501 if (ret->ref.symref.name == NULL) {
502 free(ret);
503 return NULL;
505 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
506 if (ret->ref.symref.ref == NULL) {
507 free(ret->ref.symref.name);
508 free(ret);
509 return NULL;
511 } else {
512 ret->ref.ref.name = strdup(ref->ref.ref.name);
513 if (ret->ref.ref.name == NULL) {
514 free(ret);
515 return NULL;
517 memcpy(&ret->ref.ref.id, &ref->ref.ref.id,
518 sizeof(ret->ref.ref.id));
521 return ret;
524 const struct got_error *
525 got_reflist_entry_dup(struct got_reflist_entry **newp,
526 struct got_reflist_entry *re)
528 const struct got_error *err = NULL;
529 struct got_reflist_entry *new;
531 *newp = NULL;
533 new = malloc(sizeof(*new));
534 if (new == NULL)
535 return got_error_from_errno("malloc");
537 new->ref = got_ref_dup(re->ref);
538 if (new->ref == NULL) {
539 err = got_error_from_errno("got_ref_dup");
540 free(new);
541 return err;
544 *newp = new;
545 return NULL;
548 const struct got_error *
549 got_ref_resolve_symbolic(struct got_reference **resolved,
550 struct got_repository *repo, struct got_reference *ref)
552 struct got_reference *nextref;
553 const struct got_error *err;
555 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
556 if (err)
557 return err;
559 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
560 err = got_ref_resolve_symbolic(resolved, repo, nextref);
561 else
562 *resolved = got_ref_dup(nextref);
564 got_ref_close(nextref);
565 return err;
568 static const struct got_error *
569 ref_resolve(struct got_object_id **id, struct got_repository *repo,
570 struct got_reference *ref, int recursion)
572 const struct got_error *err;
574 if (recursion <= 0)
575 return got_error_msg(GOT_ERR_RECURSION,
576 "reference recursion limit reached");
578 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
579 struct got_reference *resolved = NULL;
580 err = got_ref_resolve_symbolic(&resolved, repo, ref);
581 if (err == NULL)
582 err = ref_resolve(id, repo, resolved, --recursion);
583 if (resolved)
584 got_ref_close(resolved);
585 return err;
588 *id = calloc(1, sizeof(**id));
589 if (*id == NULL)
590 return got_error_from_errno("calloc");
591 memcpy(*id, &ref->ref.ref.id, sizeof(**id));
592 return NULL;
595 const struct got_error *
596 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
597 struct got_reference *ref)
599 return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
602 char *
603 got_ref_to_str(struct got_reference *ref)
605 char *str;
607 if (ref->flags & GOT_REF_IS_SYMBOLIC)
608 return strdup(ref->ref.symref.ref);
610 if (got_object_id_str(&str, &ref->ref.ref.id) != NULL)
611 return NULL;
613 return str;
616 const char *
617 got_ref_get_name(struct got_reference *ref)
619 if (ref->flags & GOT_REF_IS_SYMBOLIC)
620 return ref->ref.symref.name;
622 return ref->ref.ref.name;
625 const char *
626 got_ref_get_symref_target(struct got_reference *ref)
628 if (ref->flags & GOT_REF_IS_SYMBOLIC)
629 return ref->ref.symref.ref;
631 return NULL;
634 time_t
635 got_ref_get_mtime(struct got_reference *ref)
637 return ref->mtime;
640 const struct got_error *
641 got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
642 struct got_reference* re2)
644 const char *name1 = got_ref_get_name(re1);
645 const char *name2 = got_ref_get_name(re2);
647 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
648 return NULL;
651 const struct got_error *
652 got_ref_cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
653 struct got_reference *ref2)
655 const struct got_error *err = NULL;
656 struct got_repository *repo = arg;
657 struct got_object_id *id1, *id2 = NULL;
658 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
659 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
660 time_t time1, time2;
662 *cmp = 0;
664 err = got_ref_resolve(&id1, repo, ref1);
665 if (err)
666 return err;
667 err = got_object_open_as_tag(&tag1, repo, id1);
668 if (err) {
669 if (err->code != GOT_ERR_OBJ_TYPE)
670 goto done;
671 /* "lightweight" tag */
672 err = got_object_open_as_commit(&commit1, repo, id1);
673 if (err)
674 goto done;
675 time1 = got_object_commit_get_committer_time(commit1);
676 } else
677 time1 = got_object_tag_get_tagger_time(tag1);
679 err = got_ref_resolve(&id2, repo, ref2);
680 if (err)
681 goto done;
682 err = got_object_open_as_tag(&tag2, repo, id2);
683 if (err) {
684 if (err->code != GOT_ERR_OBJ_TYPE)
685 goto done;
686 /* "lightweight" tag */
687 err = got_object_open_as_commit(&commit2, repo, id2);
688 if (err)
689 goto done;
690 time2 = got_object_commit_get_committer_time(commit2);
691 } else
692 time2 = got_object_tag_get_tagger_time(tag2);
694 /* Put latest tags first. */
695 if (time1 < time2)
696 *cmp = 1;
697 else if (time1 > time2)
698 *cmp = -1;
699 else
700 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
701 done:
702 free(id1);
703 free(id2);
704 if (tag1)
705 got_object_tag_close(tag1);
706 if (tag2)
707 got_object_tag_close(tag2);
708 if (commit1)
709 got_object_commit_close(commit1);
710 if (commit2)
711 got_object_commit_close(commit2);
712 return err;
715 static const struct got_error *
716 get_committer_time(struct got_reference *ref, struct got_repository *repo)
718 const struct got_error *err = NULL;
719 int obj_type;
720 struct got_commit_object *commit = NULL;
721 struct got_tag_object *tag = NULL;
722 struct got_object_id *id = NULL;
724 err = got_ref_resolve(&id, repo, ref);
725 if (err)
726 return err;
728 err = got_object_get_type(&obj_type, repo, id);
729 if (err)
730 goto done;
732 switch (obj_type) {
733 case GOT_OBJ_TYPE_COMMIT:
734 err = got_object_open_as_commit(&commit, repo, id);
735 if (err)
736 goto done;
737 ref->committer_time =
738 got_object_commit_get_committer_time(commit);
739 break;
740 case GOT_OBJ_TYPE_TAG:
741 err = got_object_open_as_tag(&tag, repo, id);
742 if (err)
743 goto done;
744 ref->committer_time = got_object_tag_get_tagger_time(tag);
745 break;
746 default:
747 /* best effort for other object types */
748 ref->committer_time = got_ref_get_mtime(ref);
749 break;
751 done:
752 free(id);
753 if (commit)
754 got_object_commit_close(commit);
755 if (tag)
756 got_object_tag_close(tag);
757 return err;
760 const struct got_error *
761 got_ref_cmp_by_commit_timestamp_descending(void *arg, int *cmp,
762 struct got_reference *ref1, struct got_reference *ref2)
764 const struct got_error *err = NULL;
765 struct got_repository *repo = arg;
767 *cmp = 0;
769 if (ref1->committer_time == 0) {
770 err = get_committer_time(ref1, repo);
771 if (err)
772 return err;
774 if (ref2->committer_time == 0) {
775 err = get_committer_time(ref2, repo);
776 if (err)
777 return err;
780 if (ref1->committer_time < ref2->committer_time)
781 *cmp = 1;
782 else if (ref2->committer_time < ref1->committer_time)
783 *cmp = -1;
784 else
785 return got_ref_cmp_by_name(arg, cmp, ref1, ref2);
787 return err;
790 const struct got_error *
791 got_reflist_insert(struct got_reflist_entry **newp,
792 struct got_reflist_head *refs, struct got_reference *ref,
793 got_ref_cmp_cb cmp_cb, void *cmp_arg)
795 const struct got_error *err;
796 struct got_reflist_entry *new, *re;
797 int cmp;
799 *newp = NULL;
801 if (cmp_cb != got_ref_cmp_by_name && (ref->flags & GOT_REF_IS_PACKED)) {
802 /*
803 * If we are not sorting elements by name then we must still
804 * detect collisions between a packed ref and an on-disk ref
805 * using the same name. On-disk refs take precedence and are
806 * already present on the list before packed refs get added.
807 */
808 TAILQ_FOREACH(re, refs, entry) {
809 err = got_ref_cmp_by_name(NULL, &cmp, re->ref, ref);
810 if (err)
811 return err;
812 if (cmp == 0)
813 return NULL;
817 new = malloc(sizeof(*new));
818 if (new == NULL)
819 return got_error_from_errno("malloc");
820 new->ref = ref;
821 *newp = new;
823 /*
824 * We must de-duplicate entries on insert because packed-refs may
825 * contain redundant entries. On-disk refs take precedence.
826 * This code assumes that on-disk revs are read before packed-refs.
827 * We're iterating the list anyway, so insert elements sorted by name.
829 * Many callers will provide paths in a somewhat sorted order.
830 * Iterating backwards from the tail of the list should be more
831 * efficient than traversing through the entire list each time
832 * an element is inserted.
833 */
834 re = TAILQ_LAST(refs, got_reflist_head);
835 while (re) {
836 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
837 if (err)
838 return err;
839 if (cmp == 0) {
840 /* duplicate */
841 free(new);
842 *newp = NULL;
843 return NULL;
844 } else if (cmp < 0) {
845 TAILQ_INSERT_AFTER(refs, re, new, entry);
846 return NULL;
848 re = TAILQ_PREV(re, got_reflist_head, entry);
851 TAILQ_INSERT_HEAD(refs, new, entry);
852 return NULL;
855 const struct got_error *
856 got_reflist_sort(struct got_reflist_head *refs,
857 got_ref_cmp_cb cmp_cb, void *cmp_arg)
859 const struct got_error *err = NULL;
860 struct got_reflist_entry *re, *tmp, *new;
861 struct got_reflist_head sorted;
863 TAILQ_INIT(&sorted);
865 TAILQ_FOREACH_SAFE(re, refs, entry, tmp) {
866 struct got_reference *ref = re->ref;
867 TAILQ_REMOVE(refs, re, entry);
868 free(re);
869 err = got_reflist_insert(&new, &sorted, ref, cmp_cb, cmp_arg);
870 if (err || new == NULL /* duplicate */)
871 got_ref_close(ref);
872 if (err)
873 return err;
876 TAILQ_CONCAT(refs, &sorted, entry);
877 return NULL;
880 static const struct got_error *
881 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
882 const char *subdir, struct got_repository *repo,
883 got_ref_cmp_cb cmp_cb, void *cmp_arg)
885 const struct got_error *err = NULL;
886 DIR *d = NULL;
887 char *path_subdir;
889 while (subdir[0] == '/')
890 subdir++;
892 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
893 return got_error_from_errno("asprintf");
895 d = opendir(path_subdir);
896 if (d == NULL)
897 goto done;
899 for (;;) {
900 struct dirent *dent;
901 struct got_reference *ref;
902 char *child;
903 int type;
905 dent = readdir(d);
906 if (dent == NULL)
907 break;
909 if (strcmp(dent->d_name, ".") == 0 ||
910 strcmp(dent->d_name, "..") == 0)
911 continue;
913 err = got_path_dirent_type(&type, path_subdir, dent);
914 if (err)
915 break;
917 switch (type) {
918 case DT_REG:
919 err = open_ref(&ref, path_refs, subdir, dent->d_name,
920 0);
921 if (err)
922 goto done;
923 if (ref) {
924 struct got_reflist_entry *new;
925 err = got_reflist_insert(&new, refs, ref,
926 cmp_cb, cmp_arg);
927 if (err || new == NULL /* duplicate */)
928 got_ref_close(ref);
929 if (err)
930 goto done;
932 break;
933 case DT_DIR:
934 if (asprintf(&child, "%s%s%s", subdir,
935 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
936 err = got_error_from_errno("asprintf");
937 break;
939 err = gather_on_disk_refs(refs, path_refs, child, repo,
940 cmp_cb, cmp_arg);
941 free(child);
942 break;
943 default:
944 break;
947 done:
948 if (d)
949 closedir(d);
950 free(path_subdir);
951 return err;
954 const struct got_error *
955 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
956 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
958 const struct got_error *err;
959 char *packed_refs_path = NULL, *path_refs = NULL;
960 char *abs_namespace = NULL, *buf = NULL;
961 const char *ondisk_ref_namespace = NULL;
962 char *line = NULL;
963 FILE *f = NULL;
964 struct got_reference *ref;
965 struct got_reflist_entry *new;
967 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
968 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
969 if (path_refs == NULL) {
970 err = got_error_from_errno("get_refs_dir_path");
971 goto done;
973 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
974 if (err)
975 goto done;
976 err = got_reflist_insert(&new, refs, ref, cmp_cb, cmp_arg);
977 if (err || new == NULL /* duplicate */)
978 got_ref_close(ref);
979 if (err && err->code != GOT_ERR_NOT_REF)
980 goto done;
981 } else {
982 /* Try listing a single reference. */
983 const char *refname = ref_namespace;
984 path_refs = get_refs_dir_path(repo, refname);
985 if (path_refs == NULL) {
986 err = got_error_from_errno("get_refs_dir_path");
987 goto done;
989 err = open_ref(&ref, path_refs, "", refname, 0);
990 if (err) {
991 if (err->code != GOT_ERR_NOT_REF)
992 goto done;
993 /* Try to look up references in a given namespace. */
994 } else {
995 err = got_reflist_insert(&new, refs, ref,
996 cmp_cb, cmp_arg);
997 if (err || new == NULL /* duplicate */)
998 got_ref_close(ref);
999 return err;
1003 if (ref_namespace) {
1004 size_t len;
1005 /* Canonicalize the path to eliminate double-slashes if any. */
1006 if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
1007 err = got_error_from_errno("asprintf");
1008 goto done;
1010 len = strlen(abs_namespace) + 1;
1011 buf = malloc(len);
1012 if (buf == NULL) {
1013 err = got_error_from_errno("malloc");
1014 goto done;
1016 err = got_canonpath(abs_namespace, buf, len);
1017 if (err)
1018 goto done;
1019 ondisk_ref_namespace = buf;
1020 while (ondisk_ref_namespace[0] == '/')
1021 ondisk_ref_namespace++;
1022 if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
1023 ondisk_ref_namespace += 5;
1024 else if (strcmp(ondisk_ref_namespace, "refs") == 0)
1025 ondisk_ref_namespace = "";
1028 /* Gather on-disk refs before parsing packed-refs. */
1029 free(path_refs);
1030 path_refs = get_refs_dir_path(repo, "");
1031 if (path_refs == NULL) {
1032 err = got_error_from_errno("get_refs_dir_path");
1033 goto done;
1035 err = gather_on_disk_refs(refs, path_refs,
1036 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
1037 cmp_cb, cmp_arg);
1038 if (err)
1039 goto done;
1042 * The packed-refs file may contain redundant entries, in which
1043 * case on-disk refs take precedence.
1045 packed_refs_path = got_repo_get_path_packed_refs(repo);
1046 if (packed_refs_path == NULL) {
1047 err = got_error_from_errno("got_repo_get_path_packed_refs");
1048 goto done;
1051 f = fopen(packed_refs_path, "re");
1052 if (f) {
1053 size_t linesize = 0;
1054 ssize_t linelen;
1055 struct stat sb;
1057 if (fstat(fileno(f), &sb) == -1) {
1058 err = got_error_from_errno2("fstat", packed_refs_path);
1059 goto done;
1061 for (;;) {
1062 linelen = getline(&line, &linesize, f);
1063 if (linelen == -1) {
1064 if (feof(f))
1065 break;
1066 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1067 goto done;
1069 if (linelen > 0 && line[linelen - 1] == '\n')
1070 line[linelen - 1] = '\0';
1071 err = parse_packed_ref_line(&ref, NULL, line,
1072 sb.st_mtime);
1073 if (err)
1074 goto done;
1075 if (ref) {
1076 if (ref_namespace) {
1077 const char *name;
1078 name = got_ref_get_name(ref);
1079 if (!got_path_is_child(name,
1080 ref_namespace,
1081 strlen(ref_namespace))) {
1082 got_ref_close(ref);
1083 continue;
1086 err = got_reflist_insert(&new, refs, ref,
1087 cmp_cb, cmp_arg);
1088 if (err || new == NULL /* duplicate */)
1089 got_ref_close(ref);
1090 if (err)
1091 goto done;
1095 done:
1096 free(packed_refs_path);
1097 free(abs_namespace);
1098 free(buf);
1099 free(line);
1100 free(path_refs);
1101 if (f && fclose(f) == EOF && err == NULL)
1102 err = got_error_from_errno("fclose");
1103 return err;
1106 void
1107 got_ref_list_free(struct got_reflist_head *refs)
1109 struct got_reflist_entry *re;
1111 while ((re = TAILQ_FIRST(refs))) {
1112 TAILQ_REMOVE(refs, re, entry);
1113 got_ref_close(re->ref);
1114 free(re);
1118 int
1119 got_ref_is_symbolic(struct got_reference *ref)
1121 return (ref->flags & GOT_REF_IS_SYMBOLIC);
1124 const struct got_error *
1125 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1127 if (ref->flags & GOT_REF_IS_SYMBOLIC)
1128 return got_error(GOT_ERR_BAD_REF_TYPE);
1130 memcpy(&ref->ref.ref.id, id, sizeof(ref->ref.ref.id));
1131 return NULL;
1134 const struct got_error *
1135 got_ref_change_symref(struct got_reference *ref, const char *refname)
1137 char *new_name;
1139 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1140 return got_error(GOT_ERR_BAD_REF_TYPE);
1142 new_name = strdup(refname);
1143 if (new_name == NULL)
1144 return got_error_from_errno("strdup");
1146 free(ref->ref.symref.ref);
1147 ref->ref.symref.ref = new_name;
1148 return NULL;
1151 const struct got_error *
1152 got_ref_change_symref_to_ref(struct got_reference *symref,
1153 struct got_object_id *id)
1155 if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1156 return got_error(GOT_ERR_BAD_REF_TYPE);
1158 symref->ref.ref.name = symref->ref.symref.name;
1159 memcpy(&symref->ref.ref.id, id, sizeof(symref->ref.ref.id));
1160 symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1161 return NULL;
1164 const struct got_error *
1165 got_ref_write(struct got_reference *ref, struct got_repository *repo)
1167 const struct got_error *err = NULL, *unlock_err = NULL;
1168 const char *name = got_ref_get_name(ref);
1169 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1170 struct got_lockfile *lf = NULL;
1171 FILE *f = NULL;
1172 size_t n;
1173 struct stat sb;
1175 path_refs = get_refs_dir_path(repo, name);
1176 if (path_refs == NULL) {
1177 err = got_error_from_errno2("get_refs_dir_path", name);
1178 goto done;
1181 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1182 err = got_error_from_errno("asprintf");
1183 goto done;
1186 err = got_opentemp_named(&tmppath, &f, path, "");
1187 if (err) {
1188 char *parent;
1189 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1190 goto done;
1191 err = got_path_dirname(&parent, path);
1192 if (err)
1193 goto done;
1194 err = got_path_mkdir(parent);
1195 free(parent);
1196 if (err)
1197 goto done;
1198 err = got_opentemp_named(&tmppath, &f, path, "");
1199 if (err)
1200 goto done;
1203 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1204 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1205 if (n != strlen(ref->ref.symref.ref) + 6) {
1206 err = got_ferror(f, GOT_ERR_IO);
1207 goto done;
1209 } else {
1210 char *hex;
1211 size_t len;
1213 err = got_object_id_str(&hex, &ref->ref.ref.id);
1214 if (err)
1215 goto done;
1216 len = strlen(hex);
1217 n = fprintf(f, "%s\n", hex);
1218 free(hex);
1219 if (n != len + 1) {
1220 err = got_ferror(f, GOT_ERR_IO);
1221 goto done;
1225 if (ref->lf == NULL) {
1226 err = got_lockfile_lock(&lf, path, -1);
1227 if (err)
1228 goto done;
1231 /* XXX: check if old content matches our expectations? */
1233 if (stat(path, &sb) != 0) {
1234 if (errno != ENOENT) {
1235 err = got_error_from_errno2("stat", path);
1236 goto done;
1238 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1241 if (fchmod(fileno(f), sb.st_mode) != 0) {
1242 err = got_error_from_errno2("fchmod", tmppath);
1243 goto done;
1246 if (rename(tmppath, path) != 0) {
1247 err = got_error_from_errno3("rename", tmppath, path);
1248 goto done;
1250 free(tmppath);
1251 tmppath = NULL;
1253 if (stat(path, &sb) == -1) {
1254 err = got_error_from_errno2("stat", path);
1255 goto done;
1257 ref->mtime = sb.st_mtime;
1258 done:
1259 if (ref->lf == NULL && lf)
1260 unlock_err = got_lockfile_unlock(lf, -1);
1261 if (f) {
1262 if (fclose(f) == EOF && err == NULL)
1263 err = got_error_from_errno("fclose");
1265 free(path_refs);
1266 free(path);
1267 if (tmppath) {
1268 if (unlink(tmppath) == -1 && err == NULL)
1269 err = got_error_from_errno2("unlink", tmppath);
1270 free(tmppath);
1272 return err ? err : unlock_err;
1275 static const struct got_error *
1276 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1278 const struct got_error *err = NULL, *unlock_err = NULL;
1279 struct got_lockfile *lf = NULL;
1280 FILE *f = NULL, *tmpf = NULL;
1281 char *line = NULL, *packed_refs_path, *tmppath = NULL;
1282 size_t linesize = 0;
1283 struct got_reflist_head refs;
1284 int found_delref = 0;
1286 /* The packed-refs file does not cotain symbolic references. */
1287 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1288 return got_error(GOT_ERR_BAD_REF_DATA);
1290 TAILQ_INIT(&refs);
1292 packed_refs_path = got_repo_get_path_packed_refs(repo);
1293 if (packed_refs_path == NULL)
1294 return got_error_from_errno("got_repo_get_path_packed_refs");
1296 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path, "");
1297 if (err)
1298 goto done;
1300 if (delref->lf == NULL) {
1301 err = got_lockfile_lock(&lf, packed_refs_path, -1);
1302 if (err)
1303 goto done;
1306 f = fopen(packed_refs_path, "re");
1307 if (f == NULL) {
1308 err = got_error_from_errno2("fopen", packed_refs_path);
1309 goto done;
1311 for (;;) {
1312 ssize_t linelen;
1313 struct got_reference *ref;
1314 struct got_reflist_entry *new;
1316 linelen = getline(&line, &linesize, f);
1317 if (linelen == -1) {
1318 if (feof(f))
1319 break;
1320 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1321 goto done;
1323 if (linelen > 0 && line[linelen - 1] == '\n')
1324 line[linelen - 1] = '\0';
1325 err = parse_packed_ref_line(&ref, NULL, line, 0);
1326 if (err)
1327 goto done;
1328 if (ref == NULL)
1329 continue;
1331 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1332 got_object_id_cmp(&ref->ref.ref.id,
1333 &delref->ref.ref.id) == 0) {
1334 found_delref = 1;
1335 got_ref_close(ref);
1336 continue;
1339 err = got_reflist_insert(&new, &refs, ref,
1340 got_ref_cmp_by_name, NULL);
1341 if (err || new == NULL /* duplicate */)
1342 got_ref_close(ref);
1343 if (err)
1344 goto done;
1347 if (found_delref) {
1348 struct got_reflist_entry *re;
1349 size_t n;
1350 struct stat sb;
1352 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1353 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1354 err = got_ferror(f, GOT_ERR_IO);
1355 goto done;
1358 TAILQ_FOREACH(re, &refs, entry) {
1359 char *hex;
1360 size_t len;
1362 err = got_object_id_str(&hex, &re->ref->ref.ref.id);
1363 if (err)
1364 goto done;
1365 len = strlen(hex);
1366 n = fprintf(tmpf, "%s ", hex);
1367 free(hex);
1368 if (n != len + 1) {
1369 err = got_ferror(f, GOT_ERR_IO);
1370 goto done;
1373 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1374 if (n != strlen(re->ref->ref.ref.name) + 1) {
1375 err = got_ferror(f, GOT_ERR_IO);
1376 goto done;
1380 if (fflush(tmpf) != 0) {
1381 err = got_error_from_errno("fflush");
1382 goto done;
1385 if (fstat(fileno(f), &sb) != 0) {
1386 if (errno != ENOENT) {
1387 err = got_error_from_errno2("fstat",
1388 packed_refs_path);
1389 goto done;
1391 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1394 if (fchmod(fileno(tmpf), sb.st_mode) != 0) {
1395 err = got_error_from_errno2("fchmod", tmppath);
1396 goto done;
1399 if (rename(tmppath, packed_refs_path) != 0) {
1400 err = got_error_from_errno3("rename", tmppath,
1401 packed_refs_path);
1402 goto done;
1404 free(tmppath);
1405 tmppath = NULL;
1407 done:
1408 if (delref->lf == NULL && lf)
1409 unlock_err = got_lockfile_unlock(lf, -1);
1410 if (f) {
1411 if (fclose(f) == EOF && err == NULL)
1412 err = got_error_from_errno("fclose");
1414 if (tmppath && unlink(tmppath) == -1 && err == NULL)
1415 err = got_error_from_errno2("unlink", tmppath);
1416 if (tmpf && fclose(tmpf) == EOF && err == NULL)
1417 err = got_error_from_errno("fclose");
1418 free(tmppath);
1419 free(packed_refs_path);
1420 free(line);
1421 got_ref_list_free(&refs);
1422 return err ? err : unlock_err;
1425 static const struct got_error *
1426 delete_loose_ref(struct got_reference *ref, struct got_repository *repo)
1428 const struct got_error *err = NULL, *unlock_err = NULL;
1429 const char *name = got_ref_get_name(ref);
1430 char *path_refs = NULL, *path = NULL;
1431 struct got_lockfile *lf = NULL;
1433 path_refs = get_refs_dir_path(repo, name);
1434 if (path_refs == NULL) {
1435 err = got_error_from_errno2("get_refs_dir_path", name);
1436 goto done;
1439 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1440 err = got_error_from_errno("asprintf");
1441 goto done;
1444 if (ref->lf == NULL) {
1445 err = got_lockfile_lock(&lf, path, -1);
1446 if (err)
1447 goto done;
1450 /* XXX: check if old content matches our expectations? */
1452 if (unlink(path) == -1)
1453 err = got_error_from_errno2("unlink", path);
1454 done:
1455 if (ref->lf == NULL && lf)
1456 unlock_err = got_lockfile_unlock(lf, -1);
1458 free(path_refs);
1459 free(path);
1460 return err ? err : unlock_err;
1463 const struct got_error *
1464 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1466 const struct got_error *err = NULL;
1467 struct got_reference *ref2;
1469 if (ref->flags & GOT_REF_IS_PACKED) {
1470 err = delete_packed_ref(ref, repo);
1471 if (err)
1472 return err;
1474 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1475 if (err) {
1476 if (err->code == GOT_ERR_NOT_REF)
1477 return NULL;
1478 return err;
1481 err = delete_loose_ref(ref2, repo);
1482 got_ref_close(ref2);
1483 return err;
1484 } else {
1485 err = delete_loose_ref(ref, repo);
1486 if (err)
1487 return err;
1489 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1490 if (err) {
1491 if (err->code == GOT_ERR_NOT_REF)
1492 return NULL;
1493 return err;
1496 err = delete_packed_ref(ref2, repo);
1497 got_ref_close(ref2);
1498 return err;
1502 const struct got_error *
1503 got_ref_unlock(struct got_reference *ref)
1505 const struct got_error *err;
1506 err = got_lockfile_unlock(ref->lf, -1);
1507 ref->lf = NULL;
1508 return err;
1511 struct got_reflist_object_id_map {
1512 struct got_object_idset *idset;
1515 struct got_reflist_object_id_map_entry {
1516 struct got_reflist_head refs;
1519 static const struct got_error *
1520 add_object_id_map_entry(struct got_object_idset *idset,
1521 struct got_object_id *id, struct got_reflist_entry *re)
1523 const struct got_error *err = NULL;
1524 struct got_reflist_object_id_map_entry *ent;
1525 struct got_reflist_entry *new;
1527 ent = got_object_idset_get(idset, id);
1528 if (ent == NULL) {
1529 ent = malloc(sizeof(*ent));
1530 if (ent == NULL)
1531 return got_error_from_errno("malloc");
1533 TAILQ_INIT(&ent->refs);
1534 err = got_object_idset_add(idset, id, ent);
1535 if (err) {
1536 free(ent);
1537 return err;
1541 err = got_reflist_entry_dup(&new, re);
1542 if (err)
1543 return err;
1545 TAILQ_INSERT_TAIL(&ent->refs, new, entry);
1546 return NULL;
1549 const struct got_error *
1550 got_reflist_object_id_map_create(struct got_reflist_object_id_map **map,
1551 struct got_reflist_head *refs, struct got_repository *repo)
1553 const struct got_error *err = NULL;
1554 struct got_object_idset *idset;
1555 struct got_object_id *id = NULL;
1556 struct got_reflist_entry *re;
1558 idset = got_object_idset_alloc();
1559 if (idset == NULL)
1560 return got_error_from_errno("got_object_idset_alloc");
1562 *map = malloc(sizeof(**map));
1563 if (*map == NULL) {
1564 got_object_idset_free(idset);
1565 return got_error_from_errno("malloc");
1567 (*map)->idset = idset;
1569 TAILQ_FOREACH(re, refs, entry) {
1570 struct got_tag_object *tag = NULL;
1572 err = got_ref_resolve(&id, repo, re->ref);
1573 if (err)
1574 goto done;
1576 err = add_object_id_map_entry(idset, id, re);
1577 if (err)
1578 goto done;
1580 if (strstr(got_ref_get_name(re->ref), "/tags/") == NULL) {
1581 free(id);
1582 id = NULL;
1583 continue;
1586 err = got_object_open_as_tag(&tag, repo, id);
1587 if (err) {
1588 if (err->code != GOT_ERR_OBJ_TYPE)
1589 goto done;
1590 /* Ref points at something other than a tag. */
1591 err = NULL;
1592 tag = NULL;
1593 free(id);
1594 id = NULL;
1595 continue;
1598 err = add_object_id_map_entry(idset,
1599 got_object_tag_get_object_id(tag), re);
1600 got_object_tag_close(tag);
1601 if (err)
1602 goto done;
1604 free(id);
1605 id = NULL;
1607 done:
1608 free(id);
1609 if (err) {
1610 got_reflist_object_id_map_free(*map);
1611 *map = NULL;
1613 return err;
1616 struct got_reflist_head *
1617 got_reflist_object_id_map_lookup(struct got_reflist_object_id_map *map,
1618 struct got_object_id *id)
1620 struct got_reflist_object_id_map_entry *ent;
1621 ent = got_object_idset_get(map->idset, id);
1622 if (ent)
1623 return &ent->refs;
1624 return NULL;
1627 static const struct got_error *
1628 free_id_map_entry(struct got_object_id *id, void *data, void *arg)
1630 struct got_reflist_object_id_map_entry *ent = data;
1632 got_ref_list_free(&ent->refs);
1633 free(ent);
1634 return NULL;
1637 void
1638 got_reflist_object_id_map_free(struct got_reflist_object_id_map *map)
1640 got_object_idset_for_each(map->idset, free_id_map_entry, NULL);
1641 got_object_idset_free(map->idset);
1642 free(map);