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 <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <zlib.h>
29 #include <time.h>
30 #include <libgen.h>
32 #include "got_compat.h"
34 #include "got_error.h"
35 #include "got_object.h"
36 #include "got_repository.h"
37 #include "got_reference.h"
38 #include "got_opentemp.h"
39 #include "got_path.h"
41 #include "got_lib_hash.h"
42 #include "got_lib_delta.h"
43 #include "got_lib_inflate.h"
44 #include "got_lib_object.h"
45 #include "got_lib_object_idset.h"
46 #include "got_lib_lockfile.h"
48 #ifndef nitems
49 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
50 #endif
52 #define GOT_REF_HEADS "heads"
53 #define GOT_REF_TAGS "tags"
54 #define GOT_REF_REMOTES "remotes"
56 /*
57 * We do not resolve tags yet, and don't yet care about sorting refs either,
58 * so packed-refs files we write contain a minimal header which disables all
59 * packed-refs "traits" supported by Git.
60 */
61 #define GOT_PACKED_REFS_HEADER "# pack-refs with:"
63 /* A symbolic reference. */
64 struct got_symref {
65 char *name;
66 char *ref;
67 };
69 #define GOT_REF_RECURSE_MAX 20
71 /* A non-symbolic reference (there is no better designation). */
72 struct got_ref {
73 char *name;
74 struct got_object_id id;
75 };
77 /* A reference which points to an arbitrary object. */
78 struct got_reference {
79 unsigned int flags;
80 #define GOT_REF_IS_SYMBOLIC 0x01
81 #define GOT_REF_IS_PACKED 0x02
83 union {
84 struct got_ref ref;
85 struct got_symref symref;
86 } ref;
88 struct got_lockfile *lf;
89 time_t mtime;
91 /* Cached timestamp for got_ref_cmp_by_commit_timestamp_descending() */
92 time_t committer_time;
93 };
95 static const struct got_error *
96 alloc_ref(struct got_reference **ref, const char *name,
97 struct got_object_id *id, int flags, time_t mtime)
98 {
99 const struct got_error *err = NULL;
101 *ref = calloc(1, sizeof(**ref));
102 if (*ref == NULL)
103 return got_error_from_errno("calloc");
105 memcpy(&(*ref)->ref.ref.id, id, sizeof((*ref)->ref.ref.id));
106 (*ref)->flags = flags;
107 (*ref)->ref.ref.name = strdup(name);
108 (*ref)->mtime = mtime;
109 if ((*ref)->ref.ref.name == NULL) {
110 err = got_error_from_errno("strdup");
111 got_ref_close(*ref);
112 *ref = NULL;
114 return err;
117 static const struct got_error *
118 alloc_symref(struct got_reference **ref, const char *name,
119 const char *target_ref, int flags)
121 const struct got_error *err = NULL;
123 *ref = calloc(1, sizeof(**ref));
124 if (*ref == NULL)
125 return got_error_from_errno("calloc");
127 (*ref)->flags = GOT_REF_IS_SYMBOLIC | flags;
128 (*ref)->ref.symref.name = strdup(name);
129 if ((*ref)->ref.symref.name == NULL) {
130 err = got_error_from_errno("strdup");
131 got_ref_close(*ref);
132 *ref = NULL;
133 return err;
135 (*ref)->ref.symref.ref = strdup(target_ref);
136 if ((*ref)->ref.symref.ref == NULL) {
137 err = got_error_from_errno("strdup");
138 got_ref_close(*ref);
139 *ref = NULL;
141 return err;
144 static const struct got_error *
145 parse_symref(struct got_reference **ref, const char *name, const char *line)
147 if (line[0] == '\0')
148 return got_error(GOT_ERR_BAD_REF_DATA);
150 return alloc_symref(ref, name, line, 0);
153 static const struct got_error *
154 parse_ref_line(struct got_reference **ref, const char *name, const char *line,
155 time_t mtime)
157 enum got_hash_algorithm algo = GOT_HASH_SHA1;
158 struct got_object_id id;
160 if (strncmp(line, "ref: ", 5) == 0) {
161 line += 5;
162 return parse_symref(ref, name, line);
165 if (!got_parse_object_id(&id, line, algo))
166 return got_error(GOT_ERR_BAD_REF_DATA);
168 return alloc_ref(ref, name, &id, 0, mtime);
171 static const struct got_error *
172 parse_ref_file(struct got_reference **ref, const char *name,
173 const char *absname, const char *abspath, int lock)
175 const struct got_error *err = NULL;
176 FILE *f;
177 char *line = NULL;
178 size_t linesize = 0;
179 ssize_t linelen;
180 struct got_lockfile *lf = NULL;
181 struct stat sb;
183 if (lock) {
184 err = got_lockfile_lock(&lf, abspath, -1);
185 if (err) {
186 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
187 err = got_error_not_ref(name);
188 return err;
192 f = fopen(abspath, "rbe");
193 if (f == NULL) {
194 if (errno != ENOTDIR && errno != ENOENT)
195 err = got_error_from_errno2("fopen", abspath);
196 else
197 err = got_error_not_ref(name);
198 if (lock)
199 got_lockfile_unlock(lf, -1);
200 return err;
202 if (fstat(fileno(f), &sb) == -1) {
203 err = got_error_from_errno2("fstat", abspath);
204 goto done;
207 linelen = getline(&line, &linesize, f);
208 if (linelen == -1) {
209 if (feof(f))
210 err = NULL; /* ignore empty files (could be locks) */
211 else {
212 if (errno == EISDIR)
213 err = got_error(GOT_ERR_NOT_REF);
214 else if (ferror(f))
215 err = got_ferror(f, GOT_ERR_IO);
216 else
217 err = got_error_from_errno2("getline", abspath);
219 if (lock)
220 got_lockfile_unlock(lf, -1);
221 goto done;
223 while (linelen > 0 && line[linelen - 1] == '\n') {
224 line[linelen - 1] = '\0';
225 linelen--;
228 err = parse_ref_line(ref, absname, line, sb.st_mtime);
229 if (lock) {
230 if (err)
231 got_lockfile_unlock(lf, -1);
232 else {
233 if (*ref)
234 (*ref)->lf = lf;
235 else
236 got_lockfile_unlock(lf, -1);
239 done:
240 free(line);
241 if (fclose(f) == EOF && err == NULL) {
242 err = got_error_from_errno("fclose");
243 if (*ref) {
244 if (lock)
245 got_ref_unlock(*ref);
246 got_ref_close(*ref);
247 *ref = NULL;
250 return err;
253 static int
254 is_well_known_ref(const char *refname)
256 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
257 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
258 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
259 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
262 static char *
263 get_refs_dir_path(struct got_repository *repo, const char *refname)
265 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
266 return strdup(got_repo_get_path_git_dir(repo));
268 return got_repo_get_path_refs(repo);
271 const struct got_error *
272 got_ref_alloc(struct got_reference **ref, const char *name,
273 struct got_object_id *id)
275 if (!got_ref_name_is_valid(name))
276 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
278 return alloc_ref(ref, name, id, 0, 0);
281 const struct got_error *
282 got_ref_alloc_symref(struct got_reference **ref, const char *name,
283 struct got_reference *target_ref)
285 if (!got_ref_name_is_valid(name))
286 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
288 return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
291 static const struct got_error *
292 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
293 const char *line, time_t mtime)
295 enum got_hash_algorithm algo = GOT_HASH_SHA1;
296 struct got_object_id id;
297 const char *name;
299 *ref = NULL;
301 if (line[0] == '#' || line[0] == '^')
302 return NULL;
304 if (!got_parse_object_id(&id, line, algo))
305 return got_error(GOT_ERR_BAD_REF_DATA);
307 if (abs_refname) {
308 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
309 return NULL;
310 name = abs_refname;
311 } else
312 name = line + SHA1_DIGEST_STRING_LENGTH;
314 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED, mtime);
317 static const struct got_error *
318 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
319 int nsubdirs, const char *refname, time_t mtime)
321 const struct got_error *err = NULL;
322 char *abs_refname;
323 char *line = NULL;
324 size_t linesize = 0;
325 ssize_t linelen;
326 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
328 *ref = NULL;
330 if (ref_is_absolute)
331 abs_refname = (char *)refname;
332 do {
333 linelen = getline(&line, &linesize, f);
334 if (linelen == -1) {
335 if (feof(f))
336 break;
337 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
338 break;
340 if (linelen > 0 && line[linelen - 1] == '\n')
341 line[linelen - 1] = '\0';
342 for (i = 0; i < nsubdirs; i++) {
343 if (!ref_is_absolute &&
344 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
345 refname) == -1)
346 return got_error_from_errno("asprintf");
347 err = parse_packed_ref_line(ref, abs_refname, line,
348 mtime);
349 if (!ref_is_absolute)
350 free(abs_refname);
351 if (err || *ref != NULL)
352 break;
354 if (err)
355 break;
356 } while (*ref == NULL);
357 free(line);
359 return err;
362 static const struct got_error *
363 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
364 const char *name, int lock)
366 const struct got_error *err = NULL;
367 char *path = NULL;
368 char *absname = NULL;
369 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
370 int ref_is_well_known = (subdir[0] == '\0' && is_well_known_ref(name));
372 *ref = NULL;
374 if (!got_ref_name_is_valid(name))
375 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
377 if (ref_is_absolute || ref_is_well_known) {
378 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
379 return got_error_from_errno("asprintf");
380 absname = (char *)name;
381 } else {
382 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
383 subdir[0] ? "/" : "", name) == -1)
384 return got_error_from_errno("asprintf");
386 if (asprintf(&absname, "refs/%s%s%s",
387 subdir, subdir[0] ? "/" : "", name) == -1) {
388 err = got_error_from_errno("asprintf");
389 goto done;
393 err = parse_ref_file(ref, name, absname, path, lock);
394 done:
395 if (!ref_is_absolute && !ref_is_well_known)
396 free(absname);
397 free(path);
398 return err;
401 const struct got_error *
402 got_ref_open(struct got_reference **ref, struct got_repository *repo,
403 const char *refname, int lock)
405 const struct got_error *err = NULL;
406 char *packed_refs_path = NULL, *path_refs = NULL;
407 const char *subdirs[] = {
408 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
409 };
410 size_t i;
411 int well_known = is_well_known_ref(refname);
412 struct got_lockfile *lf = NULL;
414 *ref = NULL;
416 path_refs = get_refs_dir_path(repo, refname);
417 if (path_refs == NULL) {
418 err = got_error_from_errno2("get_refs_dir_path", refname);
419 goto done;
422 if (well_known) {
423 err = open_ref(ref, path_refs, "", refname, lock);
424 } else {
425 FILE *f;
427 /* Search on-disk refs before packed refs! */
428 for (i = 0; i < nitems(subdirs); i++) {
429 err = open_ref(ref, path_refs, subdirs[i], refname,
430 lock);
431 if ((err && err->code != GOT_ERR_NOT_REF) || *ref)
432 goto done;
435 packed_refs_path = got_repo_get_path_packed_refs(repo);
436 if (packed_refs_path == NULL) {
437 err = got_error_from_errno(
438 "got_repo_get_path_packed_refs");
439 goto done;
442 if (lock) {
443 err = got_lockfile_lock(&lf, packed_refs_path, -1);
444 if (err)
445 goto done;
447 f = fopen(packed_refs_path, "rbe");
448 if (f != NULL) {
449 struct stat sb;
450 if (fstat(fileno(f), &sb) == -1) {
451 err = got_error_from_errno2("fstat",
452 packed_refs_path);
453 goto done;
455 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
456 refname, sb.st_mtime);
457 if (!err) {
458 if (fclose(f) == EOF) {
459 err = got_error_from_errno("fclose");
460 got_ref_close(*ref);
461 *ref = NULL;
462 } else if (*ref)
463 (*ref)->lf = lf;
467 done:
468 if (!err && *ref == NULL)
469 err = got_error_not_ref(refname);
470 if (err && lf)
471 got_lockfile_unlock(lf, -1);
472 free(packed_refs_path);
473 free(path_refs);
474 return err;
477 void
478 got_ref_close(struct got_reference *ref)
480 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
481 free(ref->ref.symref.name);
482 free(ref->ref.symref.ref);
483 } else
484 free(ref->ref.ref.name);
485 free(ref);
488 struct got_reference *
489 got_ref_dup(struct got_reference *ref)
491 struct got_reference *ret;
493 ret = calloc(1, sizeof(*ret));
494 if (ret == NULL)
495 return NULL;
497 ret->flags = ref->flags;
498 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
499 ret->ref.symref.name = strdup(ref->ref.symref.name);
500 if (ret->ref.symref.name == NULL) {
501 free(ret);
502 return NULL;
504 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
505 if (ret->ref.symref.ref == NULL) {
506 free(ret->ref.symref.name);
507 free(ret);
508 return NULL;
510 } else {
511 ret->ref.ref.name = strdup(ref->ref.ref.name);
512 if (ret->ref.ref.name == NULL) {
513 free(ret);
514 return NULL;
516 memcpy(&ret->ref.ref.id, &ref->ref.ref.id,
517 sizeof(ret->ref.ref.id));
520 return ret;
523 const struct got_error *
524 got_reflist_entry_dup(struct got_reflist_entry **newp,
525 struct got_reflist_entry *re)
527 const struct got_error *err = NULL;
528 struct got_reflist_entry *new;
530 *newp = NULL;
532 new = malloc(sizeof(*new));
533 if (new == NULL)
534 return got_error_from_errno("malloc");
536 new->ref = got_ref_dup(re->ref);
537 if (new->ref == NULL) {
538 err = got_error_from_errno("got_ref_dup");
539 free(new);
540 return err;
543 *newp = new;
544 return NULL;
547 const struct got_error *
548 got_ref_resolve_symbolic(struct got_reference **resolved,
549 struct got_repository *repo, struct got_reference *ref)
551 struct got_reference *nextref;
552 const struct got_error *err;
554 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
555 if (err)
556 return err;
558 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
559 err = got_ref_resolve_symbolic(resolved, repo, nextref);
560 else
561 *resolved = got_ref_dup(nextref);
563 got_ref_close(nextref);
564 return err;
567 static const struct got_error *
568 ref_resolve(struct got_object_id **id, struct got_repository *repo,
569 struct got_reference *ref, int recursion)
571 const struct got_error *err;
573 if (recursion <= 0)
574 return got_error_msg(GOT_ERR_RECURSION,
575 "reference recursion limit reached");
577 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
578 struct got_reference *resolved = NULL;
579 err = got_ref_resolve_symbolic(&resolved, repo, ref);
580 if (err == NULL)
581 err = ref_resolve(id, repo, resolved, --recursion);
582 if (resolved)
583 got_ref_close(resolved);
584 return err;
587 *id = calloc(1, sizeof(**id));
588 if (*id == NULL)
589 return got_error_from_errno("calloc");
590 memcpy(*id, &ref->ref.ref.id, sizeof(**id));
591 return NULL;
594 const struct got_error *
595 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
596 struct got_reference *ref)
598 return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
601 char *
602 got_ref_to_str(struct got_reference *ref)
604 char *str;
606 if (ref->flags & GOT_REF_IS_SYMBOLIC)
607 return strdup(ref->ref.symref.ref);
609 if (got_object_id_str(&str, &ref->ref.ref.id) != NULL)
610 return NULL;
612 return str;
615 const char *
616 got_ref_get_name(struct got_reference *ref)
618 if (ref->flags & GOT_REF_IS_SYMBOLIC)
619 return ref->ref.symref.name;
621 return ref->ref.ref.name;
624 const char *
625 got_ref_get_symref_target(struct got_reference *ref)
627 if (ref->flags & GOT_REF_IS_SYMBOLIC)
628 return ref->ref.symref.ref;
630 return NULL;
633 time_t
634 got_ref_get_mtime(struct got_reference *ref)
636 return ref->mtime;
639 const struct got_error *
640 got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
641 struct got_reference* re2)
643 const char *name1 = got_ref_get_name(re1);
644 const char *name2 = got_ref_get_name(re2);
646 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
647 return NULL;
650 const struct got_error *
651 got_ref_cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
652 struct got_reference *ref2)
654 const struct got_error *err = NULL;
655 struct got_repository *repo = arg;
656 struct got_object_id *id1, *id2 = NULL;
657 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
658 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
659 time_t time1, time2;
661 *cmp = 0;
663 err = got_ref_resolve(&id1, repo, ref1);
664 if (err)
665 return err;
666 err = got_object_open_as_tag(&tag1, repo, id1);
667 if (err) {
668 if (err->code != GOT_ERR_OBJ_TYPE)
669 goto done;
670 /* "lightweight" tag */
671 err = got_object_open_as_commit(&commit1, repo, id1);
672 if (err)
673 goto done;
674 time1 = got_object_commit_get_committer_time(commit1);
675 } else
676 time1 = got_object_tag_get_tagger_time(tag1);
678 err = got_ref_resolve(&id2, repo, ref2);
679 if (err)
680 goto done;
681 err = got_object_open_as_tag(&tag2, repo, id2);
682 if (err) {
683 if (err->code != GOT_ERR_OBJ_TYPE)
684 goto done;
685 /* "lightweight" tag */
686 err = got_object_open_as_commit(&commit2, repo, id2);
687 if (err)
688 goto done;
689 time2 = got_object_commit_get_committer_time(commit2);
690 } else
691 time2 = got_object_tag_get_tagger_time(tag2);
693 /* Put latest tags first. */
694 if (time1 < time2)
695 *cmp = 1;
696 else if (time1 > time2)
697 *cmp = -1;
698 else
699 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
700 done:
701 free(id1);
702 free(id2);
703 if (tag1)
704 got_object_tag_close(tag1);
705 if (tag2)
706 got_object_tag_close(tag2);
707 if (commit1)
708 got_object_commit_close(commit1);
709 if (commit2)
710 got_object_commit_close(commit2);
711 return err;
714 static const struct got_error *
715 get_committer_time(struct got_reference *ref, struct got_repository *repo)
717 const struct got_error *err = NULL;
718 int obj_type;
719 struct got_commit_object *commit = NULL;
720 struct got_tag_object *tag = NULL;
721 struct got_object_id *id = NULL;
723 err = got_ref_resolve(&id, repo, ref);
724 if (err)
725 return err;
727 err = got_object_get_type(&obj_type, repo, id);
728 if (err)
729 goto done;
731 switch (obj_type) {
732 case GOT_OBJ_TYPE_COMMIT:
733 err = got_object_open_as_commit(&commit, repo, id);
734 if (err)
735 goto done;
736 ref->committer_time =
737 got_object_commit_get_committer_time(commit);
738 break;
739 case GOT_OBJ_TYPE_TAG:
740 err = got_object_open_as_tag(&tag, repo, id);
741 if (err)
742 goto done;
743 ref->committer_time = got_object_tag_get_tagger_time(tag);
744 break;
745 default:
746 /* best effort for other object types */
747 ref->committer_time = got_ref_get_mtime(ref);
748 break;
750 done:
751 free(id);
752 if (commit)
753 got_object_commit_close(commit);
754 if (tag)
755 got_object_tag_close(tag);
756 return err;
759 const struct got_error *
760 got_ref_cmp_by_commit_timestamp_descending(void *arg, int *cmp,
761 struct got_reference *ref1, struct got_reference *ref2)
763 const struct got_error *err = NULL;
764 struct got_repository *repo = arg;
766 *cmp = 0;
768 if (ref1->committer_time == 0) {
769 err = get_committer_time(ref1, repo);
770 if (err)
771 return err;
773 if (ref2->committer_time == 0) {
774 err = get_committer_time(ref2, repo);
775 if (err)
776 return err;
779 if (ref1->committer_time < ref2->committer_time)
780 *cmp = 1;
781 else if (ref2->committer_time < ref1->committer_time)
782 *cmp = -1;
783 else
784 return got_ref_cmp_by_name(arg, cmp, ref1, ref2);
786 return err;
789 const struct got_error *
790 got_reflist_insert(struct got_reflist_entry **newp,
791 struct got_reflist_head *refs, struct got_reference *ref,
792 got_ref_cmp_cb cmp_cb, void *cmp_arg)
794 const struct got_error *err;
795 struct got_reflist_entry *new, *re;
796 int cmp;
798 *newp = NULL;
800 if (cmp_cb != got_ref_cmp_by_name && (ref->flags & GOT_REF_IS_PACKED)) {
801 /*
802 * If we are not sorting elements by name then we must still
803 * detect collisions between a packed ref and an on-disk ref
804 * using the same name. On-disk refs take precedence and are
805 * already present on the list before packed refs get added.
806 */
807 TAILQ_FOREACH(re, refs, entry) {
808 err = got_ref_cmp_by_name(NULL, &cmp, re->ref, ref);
809 if (err)
810 return err;
811 if (cmp == 0)
812 return NULL;
816 new = malloc(sizeof(*new));
817 if (new == NULL)
818 return got_error_from_errno("malloc");
819 new->ref = ref;
820 *newp = new;
822 /*
823 * We must de-duplicate entries on insert because packed-refs may
824 * contain redundant entries. On-disk refs take precedence.
825 * This code assumes that on-disk revs are read before packed-refs.
826 * We're iterating the list anyway, so insert elements sorted by name.
828 * Many callers will provide paths in a somewhat sorted order.
829 * Iterating backwards from the tail of the list should be more
830 * efficient than traversing through the entire list each time
831 * an element is inserted.
832 */
833 re = TAILQ_LAST(refs, got_reflist_head);
834 while (re) {
835 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
836 if (err)
837 return err;
838 if (cmp == 0) {
839 /* duplicate */
840 free(new);
841 *newp = NULL;
842 return NULL;
843 } else if (cmp < 0) {
844 TAILQ_INSERT_AFTER(refs, re, new, entry);
845 return NULL;
847 re = TAILQ_PREV(re, got_reflist_head, entry);
850 TAILQ_INSERT_HEAD(refs, new, entry);
851 return NULL;
854 const struct got_error *
855 got_reflist_sort(struct got_reflist_head *refs,
856 got_ref_cmp_cb cmp_cb, void *cmp_arg)
858 const struct got_error *err = NULL;
859 struct got_reflist_entry *re, *tmp, *new;
860 struct got_reflist_head sorted;
862 TAILQ_INIT(&sorted);
864 TAILQ_FOREACH_SAFE(re, refs, entry, tmp) {
865 struct got_reference *ref = re->ref;
866 TAILQ_REMOVE(refs, re, entry);
867 free(re);
868 err = got_reflist_insert(&new, &sorted, ref, cmp_cb, cmp_arg);
869 if (err || new == NULL /* duplicate */)
870 got_ref_close(ref);
871 if (err)
872 return err;
875 TAILQ_CONCAT(refs, &sorted, entry);
876 return NULL;
879 static const struct got_error *
880 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
881 const char *subdir, struct got_repository *repo,
882 got_ref_cmp_cb cmp_cb, void *cmp_arg)
884 const struct got_error *err = NULL;
885 DIR *d = NULL;
886 char *path_subdir;
888 while (subdir[0] == '/')
889 subdir++;
891 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
892 return got_error_from_errno("asprintf");
894 d = opendir(path_subdir);
895 if (d == NULL)
896 goto done;
898 for (;;) {
899 struct dirent *dent;
900 struct got_reference *ref;
901 char *child;
902 int type;
904 dent = readdir(d);
905 if (dent == NULL)
906 break;
908 if (strcmp(dent->d_name, ".") == 0 ||
909 strcmp(dent->d_name, "..") == 0)
910 continue;
912 err = got_path_dirent_type(&type, path_subdir, dent);
913 if (err)
914 break;
916 switch (type) {
917 case DT_REG:
918 err = open_ref(&ref, path_refs, subdir, dent->d_name,
919 0);
920 if (err)
921 goto done;
922 if (ref) {
923 struct got_reflist_entry *new;
924 err = got_reflist_insert(&new, refs, ref,
925 cmp_cb, cmp_arg);
926 if (err || new == NULL /* duplicate */)
927 got_ref_close(ref);
928 if (err)
929 goto done;
931 break;
932 case DT_DIR:
933 if (asprintf(&child, "%s%s%s", subdir,
934 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
935 err = got_error_from_errno("asprintf");
936 break;
938 err = gather_on_disk_refs(refs, path_refs, child, repo,
939 cmp_cb, cmp_arg);
940 free(child);
941 break;
942 default:
943 break;
946 done:
947 if (d)
948 closedir(d);
949 free(path_subdir);
950 return err;
953 const struct got_error *
954 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
955 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
957 const struct got_error *err;
958 char *packed_refs_path = NULL, *path_refs = NULL;
959 char *abs_namespace = NULL, *buf = NULL;
960 const char *ondisk_ref_namespace = NULL;
961 char *line = NULL;
962 FILE *f = NULL;
963 struct got_reference *ref;
964 struct got_reflist_entry *new;
966 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
967 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
968 if (path_refs == NULL) {
969 err = got_error_from_errno("get_refs_dir_path");
970 goto done;
972 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
973 if (err)
974 goto done;
975 err = got_reflist_insert(&new, refs, ref, cmp_cb, cmp_arg);
976 if (err || new == NULL /* duplicate */)
977 got_ref_close(ref);
978 if (err && err->code != GOT_ERR_NOT_REF)
979 goto done;
980 } else {
981 /* Try listing a single reference. */
982 const char *refname = ref_namespace;
983 path_refs = get_refs_dir_path(repo, refname);
984 if (path_refs == NULL) {
985 err = got_error_from_errno("get_refs_dir_path");
986 goto done;
988 err = open_ref(&ref, path_refs, "", refname, 0);
989 if (err) {
990 if (err->code != GOT_ERR_NOT_REF)
991 goto done;
992 /* Try to look up references in a given namespace. */
993 } else {
994 err = got_reflist_insert(&new, refs, ref,
995 cmp_cb, cmp_arg);
996 if (err || new == NULL /* duplicate */)
997 got_ref_close(ref);
998 return err;
1002 if (ref_namespace) {
1003 size_t len;
1004 /* Canonicalize the path to eliminate double-slashes if any. */
1005 if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
1006 err = got_error_from_errno("asprintf");
1007 goto done;
1009 len = strlen(abs_namespace) + 1;
1010 buf = malloc(len);
1011 if (buf == NULL) {
1012 err = got_error_from_errno("malloc");
1013 goto done;
1015 err = got_canonpath(abs_namespace, buf, len);
1016 if (err)
1017 goto done;
1018 ondisk_ref_namespace = buf;
1019 while (ondisk_ref_namespace[0] == '/')
1020 ondisk_ref_namespace++;
1021 if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
1022 ondisk_ref_namespace += 5;
1023 else if (strcmp(ondisk_ref_namespace, "refs") == 0)
1024 ondisk_ref_namespace = "";
1027 /* Gather on-disk refs before parsing packed-refs. */
1028 free(path_refs);
1029 path_refs = get_refs_dir_path(repo, "");
1030 if (path_refs == NULL) {
1031 err = got_error_from_errno("get_refs_dir_path");
1032 goto done;
1034 err = gather_on_disk_refs(refs, path_refs,
1035 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
1036 cmp_cb, cmp_arg);
1037 if (err)
1038 goto done;
1041 * The packed-refs file may contain redundant entries, in which
1042 * case on-disk refs take precedence.
1044 packed_refs_path = got_repo_get_path_packed_refs(repo);
1045 if (packed_refs_path == NULL) {
1046 err = got_error_from_errno("got_repo_get_path_packed_refs");
1047 goto done;
1050 f = fopen(packed_refs_path, "re");
1051 if (f) {
1052 size_t linesize = 0;
1053 ssize_t linelen;
1054 struct stat sb;
1056 if (fstat(fileno(f), &sb) == -1) {
1057 err = got_error_from_errno2("fstat", packed_refs_path);
1058 goto done;
1060 for (;;) {
1061 linelen = getline(&line, &linesize, f);
1062 if (linelen == -1) {
1063 if (feof(f))
1064 break;
1065 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1066 goto done;
1068 if (linelen > 0 && line[linelen - 1] == '\n')
1069 line[linelen - 1] = '\0';
1070 err = parse_packed_ref_line(&ref, NULL, line,
1071 sb.st_mtime);
1072 if (err)
1073 goto done;
1074 if (ref) {
1075 if (ref_namespace) {
1076 const char *name;
1077 name = got_ref_get_name(ref);
1078 if (!got_path_is_child(name,
1079 ref_namespace,
1080 strlen(ref_namespace))) {
1081 got_ref_close(ref);
1082 continue;
1085 err = got_reflist_insert(&new, refs, ref,
1086 cmp_cb, cmp_arg);
1087 if (err || new == NULL /* duplicate */)
1088 got_ref_close(ref);
1089 if (err)
1090 goto done;
1094 done:
1095 free(packed_refs_path);
1096 free(abs_namespace);
1097 free(buf);
1098 free(line);
1099 free(path_refs);
1100 if (f && fclose(f) == EOF && err == NULL)
1101 err = got_error_from_errno("fclose");
1102 return err;
1105 void
1106 got_ref_list_free(struct got_reflist_head *refs)
1108 struct got_reflist_entry *re;
1110 while ((re = TAILQ_FIRST(refs))) {
1111 TAILQ_REMOVE(refs, re, entry);
1112 got_ref_close(re->ref);
1113 free(re);
1117 int
1118 got_ref_is_symbolic(struct got_reference *ref)
1120 return (ref->flags & GOT_REF_IS_SYMBOLIC);
1123 const struct got_error *
1124 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1126 if (ref->flags & GOT_REF_IS_SYMBOLIC)
1127 return got_error(GOT_ERR_BAD_REF_TYPE);
1129 memcpy(&ref->ref.ref.id, id, sizeof(ref->ref.ref.id));
1130 return NULL;
1133 const struct got_error *
1134 got_ref_change_symref(struct got_reference *ref, const char *refname)
1136 char *new_name;
1138 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1139 return got_error(GOT_ERR_BAD_REF_TYPE);
1141 new_name = strdup(refname);
1142 if (new_name == NULL)
1143 return got_error_from_errno("strdup");
1145 free(ref->ref.symref.ref);
1146 ref->ref.symref.ref = new_name;
1147 return NULL;
1150 const struct got_error *
1151 got_ref_change_symref_to_ref(struct got_reference *symref,
1152 struct got_object_id *id)
1154 if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1155 return got_error(GOT_ERR_BAD_REF_TYPE);
1157 symref->ref.ref.name = symref->ref.symref.name;
1158 memcpy(&symref->ref.ref.id, id, sizeof(symref->ref.ref.id));
1159 symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1160 return NULL;
1163 const struct got_error *
1164 got_ref_write(struct got_reference *ref, struct got_repository *repo)
1166 const struct got_error *err = NULL, *unlock_err = NULL;
1167 const char *name = got_ref_get_name(ref);
1168 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1169 struct got_lockfile *lf = NULL;
1170 FILE *f = NULL;
1171 size_t n;
1172 struct stat sb;
1174 path_refs = get_refs_dir_path(repo, name);
1175 if (path_refs == NULL) {
1176 err = got_error_from_errno2("get_refs_dir_path", name);
1177 goto done;
1180 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1181 err = got_error_from_errno("asprintf");
1182 goto done;
1185 err = got_opentemp_named(&tmppath, &f, path, "");
1186 if (err) {
1187 char *parent;
1188 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1189 goto done;
1190 err = got_path_dirname(&parent, path);
1191 if (err)
1192 goto done;
1193 err = got_path_mkdir(parent);
1194 free(parent);
1195 if (err)
1196 goto done;
1197 err = got_opentemp_named(&tmppath, &f, path, "");
1198 if (err)
1199 goto done;
1202 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1203 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1204 if (n != strlen(ref->ref.symref.ref) + 6) {
1205 err = got_ferror(f, GOT_ERR_IO);
1206 goto done;
1208 } else {
1209 char *hex;
1210 size_t len;
1212 err = got_object_id_str(&hex, &ref->ref.ref.id);
1213 if (err)
1214 goto done;
1215 len = strlen(hex);
1216 n = fprintf(f, "%s\n", hex);
1217 free(hex);
1218 if (n != len + 1) {
1219 err = got_ferror(f, GOT_ERR_IO);
1220 goto done;
1224 if (ref->lf == NULL) {
1225 err = got_lockfile_lock(&lf, path, -1);
1226 if (err)
1227 goto done;
1230 /* XXX: check if old content matches our expectations? */
1232 if (stat(path, &sb) != 0) {
1233 if (errno != ENOENT) {
1234 err = got_error_from_errno2("stat", path);
1235 goto done;
1237 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1240 if (fchmod(fileno(f), sb.st_mode) != 0) {
1241 err = got_error_from_errno2("fchmod", tmppath);
1242 goto done;
1245 if (rename(tmppath, path) != 0) {
1246 err = got_error_from_errno3("rename", tmppath, path);
1247 goto done;
1249 free(tmppath);
1250 tmppath = NULL;
1252 if (stat(path, &sb) == -1) {
1253 err = got_error_from_errno2("stat", path);
1254 goto done;
1256 ref->mtime = sb.st_mtime;
1257 done:
1258 if (ref->lf == NULL && lf)
1259 unlock_err = got_lockfile_unlock(lf, -1);
1260 if (f) {
1261 if (fclose(f) == EOF && err == NULL)
1262 err = got_error_from_errno("fclose");
1264 free(path_refs);
1265 free(path);
1266 if (tmppath) {
1267 if (unlink(tmppath) == -1 && err == NULL)
1268 err = got_error_from_errno2("unlink", tmppath);
1269 free(tmppath);
1271 return err ? err : unlock_err;
1274 static const struct got_error *
1275 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1277 const struct got_error *err = NULL, *unlock_err = NULL;
1278 struct got_lockfile *lf = NULL;
1279 FILE *f = NULL, *tmpf = NULL;
1280 char *line = NULL, *packed_refs_path, *tmppath = NULL;
1281 size_t linesize = 0;
1282 struct got_reflist_head refs;
1283 int found_delref = 0;
1285 /* The packed-refs file does not cotain symbolic references. */
1286 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1287 return got_error(GOT_ERR_BAD_REF_DATA);
1289 TAILQ_INIT(&refs);
1291 packed_refs_path = got_repo_get_path_packed_refs(repo);
1292 if (packed_refs_path == NULL)
1293 return got_error_from_errno("got_repo_get_path_packed_refs");
1295 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path, "");
1296 if (err)
1297 goto done;
1299 if (delref->lf == NULL) {
1300 err = got_lockfile_lock(&lf, packed_refs_path, -1);
1301 if (err)
1302 goto done;
1305 f = fopen(packed_refs_path, "re");
1306 if (f == NULL) {
1307 err = got_error_from_errno2("fopen", packed_refs_path);
1308 goto done;
1310 for (;;) {
1311 ssize_t linelen;
1312 struct got_reference *ref;
1313 struct got_reflist_entry *new;
1315 linelen = getline(&line, &linesize, f);
1316 if (linelen == -1) {
1317 if (feof(f))
1318 break;
1319 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1320 goto done;
1322 if (linelen > 0 && line[linelen - 1] == '\n')
1323 line[linelen - 1] = '\0';
1324 err = parse_packed_ref_line(&ref, NULL, line, 0);
1325 if (err)
1326 goto done;
1327 if (ref == NULL)
1328 continue;
1330 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1331 got_object_id_cmp(&ref->ref.ref.id,
1332 &delref->ref.ref.id) == 0) {
1333 found_delref = 1;
1334 got_ref_close(ref);
1335 continue;
1338 err = got_reflist_insert(&new, &refs, ref,
1339 got_ref_cmp_by_name, NULL);
1340 if (err || new == NULL /* duplicate */)
1341 got_ref_close(ref);
1342 if (err)
1343 goto done;
1346 if (found_delref) {
1347 struct got_reflist_entry *re;
1348 size_t n;
1349 struct stat sb;
1351 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1352 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1353 err = got_ferror(f, GOT_ERR_IO);
1354 goto done;
1357 TAILQ_FOREACH(re, &refs, entry) {
1358 char *hex;
1359 size_t len;
1361 err = got_object_id_str(&hex, &re->ref->ref.ref.id);
1362 if (err)
1363 goto done;
1364 len = strlen(hex);
1365 n = fprintf(tmpf, "%s ", hex);
1366 free(hex);
1367 if (n != len + 1) {
1368 err = got_ferror(f, GOT_ERR_IO);
1369 goto done;
1372 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1373 if (n != strlen(re->ref->ref.ref.name) + 1) {
1374 err = got_ferror(f, GOT_ERR_IO);
1375 goto done;
1379 if (fflush(tmpf) != 0) {
1380 err = got_error_from_errno("fflush");
1381 goto done;
1384 if (fstat(fileno(f), &sb) != 0) {
1385 if (errno != ENOENT) {
1386 err = got_error_from_errno2("fstat",
1387 packed_refs_path);
1388 goto done;
1390 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1393 if (fchmod(fileno(tmpf), sb.st_mode) != 0) {
1394 err = got_error_from_errno2("fchmod", tmppath);
1395 goto done;
1398 if (rename(tmppath, packed_refs_path) != 0) {
1399 err = got_error_from_errno3("rename", tmppath,
1400 packed_refs_path);
1401 goto done;
1403 free(tmppath);
1404 tmppath = NULL;
1406 done:
1407 if (delref->lf == NULL && lf)
1408 unlock_err = got_lockfile_unlock(lf, -1);
1409 if (f) {
1410 if (fclose(f) == EOF && err == NULL)
1411 err = got_error_from_errno("fclose");
1413 if (tmppath && unlink(tmppath) == -1 && err == NULL)
1414 err = got_error_from_errno2("unlink", tmppath);
1415 if (tmpf && fclose(tmpf) == EOF && err == NULL)
1416 err = got_error_from_errno("fclose");
1417 free(tmppath);
1418 free(packed_refs_path);
1419 free(line);
1420 got_ref_list_free(&refs);
1421 return err ? err : unlock_err;
1424 static const struct got_error *
1425 delete_loose_ref(struct got_reference *ref, struct got_repository *repo)
1427 const struct got_error *err = NULL, *unlock_err = NULL;
1428 const char *name = got_ref_get_name(ref);
1429 char *path_refs = NULL, *path = NULL;
1430 struct got_lockfile *lf = NULL;
1432 path_refs = get_refs_dir_path(repo, name);
1433 if (path_refs == NULL) {
1434 err = got_error_from_errno2("get_refs_dir_path", name);
1435 goto done;
1438 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1439 err = got_error_from_errno("asprintf");
1440 goto done;
1443 if (ref->lf == NULL) {
1444 err = got_lockfile_lock(&lf, path, -1);
1445 if (err)
1446 goto done;
1449 /* XXX: check if old content matches our expectations? */
1451 if (unlink(path) == -1)
1452 err = got_error_from_errno2("unlink", path);
1453 done:
1454 if (ref->lf == NULL && lf)
1455 unlock_err = got_lockfile_unlock(lf, -1);
1457 free(path_refs);
1458 free(path);
1459 return err ? err : unlock_err;
1462 const struct got_error *
1463 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1465 const struct got_error *err = NULL;
1466 struct got_reference *ref2;
1468 if (ref->flags & GOT_REF_IS_PACKED) {
1469 err = delete_packed_ref(ref, repo);
1470 if (err)
1471 return err;
1473 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1474 if (err) {
1475 if (err->code == GOT_ERR_NOT_REF)
1476 return NULL;
1477 return err;
1480 err = delete_loose_ref(ref2, repo);
1481 got_ref_close(ref2);
1482 return err;
1483 } else {
1484 err = delete_loose_ref(ref, repo);
1485 if (err)
1486 return err;
1488 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1489 if (err) {
1490 if (err->code == GOT_ERR_NOT_REF)
1491 return NULL;
1492 return err;
1495 err = delete_packed_ref(ref2, repo);
1496 got_ref_close(ref2);
1497 return err;
1501 const struct got_error *
1502 got_ref_unlock(struct got_reference *ref)
1504 const struct got_error *err;
1505 err = got_lockfile_unlock(ref->lf, -1);
1506 ref->lf = NULL;
1507 return err;
1510 struct got_reflist_object_id_map {
1511 struct got_object_idset *idset;
1514 struct got_reflist_object_id_map_entry {
1515 struct got_reflist_head refs;
1518 static const struct got_error *
1519 add_object_id_map_entry(struct got_object_idset *idset,
1520 struct got_object_id *id, struct got_reflist_entry *re)
1522 const struct got_error *err = NULL;
1523 struct got_reflist_object_id_map_entry *ent;
1524 struct got_reflist_entry *new;
1526 ent = got_object_idset_get(idset, id);
1527 if (ent == NULL) {
1528 ent = malloc(sizeof(*ent));
1529 if (ent == NULL)
1530 return got_error_from_errno("malloc");
1532 TAILQ_INIT(&ent->refs);
1533 err = got_object_idset_add(idset, id, ent);
1534 if (err) {
1535 free(ent);
1536 return err;
1540 err = got_reflist_entry_dup(&new, re);
1541 if (err)
1542 return err;
1544 TAILQ_INSERT_TAIL(&ent->refs, new, entry);
1545 return NULL;
1548 const struct got_error *
1549 got_reflist_object_id_map_create(struct got_reflist_object_id_map **map,
1550 struct got_reflist_head *refs, struct got_repository *repo)
1552 const struct got_error *err = NULL;
1553 struct got_object_idset *idset;
1554 struct got_object_id *id = NULL;
1555 struct got_reflist_entry *re;
1557 idset = got_object_idset_alloc();
1558 if (idset == NULL)
1559 return got_error_from_errno("got_object_idset_alloc");
1561 *map = malloc(sizeof(**map));
1562 if (*map == NULL) {
1563 got_object_idset_free(idset);
1564 return got_error_from_errno("malloc");
1566 (*map)->idset = idset;
1568 TAILQ_FOREACH(re, refs, entry) {
1569 struct got_tag_object *tag = NULL;
1571 err = got_ref_resolve(&id, repo, re->ref);
1572 if (err)
1573 goto done;
1575 err = add_object_id_map_entry(idset, id, re);
1576 if (err)
1577 goto done;
1579 if (strstr(got_ref_get_name(re->ref), "/tags/") == NULL) {
1580 free(id);
1581 id = NULL;
1582 continue;
1585 err = got_object_open_as_tag(&tag, repo, id);
1586 if (err) {
1587 if (err->code != GOT_ERR_OBJ_TYPE)
1588 goto done;
1589 /* Ref points at something other than a tag. */
1590 err = NULL;
1591 tag = NULL;
1592 free(id);
1593 id = NULL;
1594 continue;
1597 err = add_object_id_map_entry(idset,
1598 got_object_tag_get_object_id(tag), re);
1599 got_object_tag_close(tag);
1600 if (err)
1601 goto done;
1603 free(id);
1604 id = NULL;
1606 done:
1607 free(id);
1608 if (err) {
1609 got_reflist_object_id_map_free(*map);
1610 *map = NULL;
1612 return err;
1615 struct got_reflist_head *
1616 got_reflist_object_id_map_lookup(struct got_reflist_object_id_map *map,
1617 struct got_object_id *id)
1619 struct got_reflist_object_id_map_entry *ent;
1620 ent = got_object_idset_get(map->idset, id);
1621 if (ent)
1622 return &ent->refs;
1623 return NULL;
1626 static const struct got_error *
1627 free_id_map_entry(struct got_object_id *id, void *data, void *arg)
1629 struct got_reflist_object_id_map_entry *ent = data;
1631 got_ref_list_free(&ent->refs);
1632 free(ent);
1633 return NULL;
1636 void
1637 got_reflist_object_id_map_free(struct got_reflist_object_id_map *map)
1639 got_object_idset_for_each(map->idset, free_id_map_entry, NULL);
1640 got_object_idset_free(map->idset);
1641 free(map);