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 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_sha1_digest(id.sha1, line))
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 struct got_object_id id;
296 const char *name;
298 *ref = NULL;
300 if (line[0] == '#' || line[0] == '^')
301 return NULL;
303 if (!got_parse_sha1_digest(id.sha1, line))
304 return got_error(GOT_ERR_BAD_REF_DATA);
306 if (abs_refname) {
307 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
308 return NULL;
309 name = abs_refname;
310 } else
311 name = line + SHA1_DIGEST_STRING_LENGTH;
313 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED, mtime);
316 static const struct got_error *
317 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
318 int nsubdirs, const char *refname, time_t mtime)
320 const struct got_error *err = NULL;
321 char *abs_refname;
322 char *line = NULL;
323 size_t linesize = 0;
324 ssize_t linelen;
325 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
327 *ref = NULL;
329 if (ref_is_absolute)
330 abs_refname = (char *)refname;
331 do {
332 linelen = getline(&line, &linesize, f);
333 if (linelen == -1) {
334 if (feof(f))
335 break;
336 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
337 break;
339 if (linelen > 0 && line[linelen - 1] == '\n')
340 line[linelen - 1] = '\0';
341 for (i = 0; i < nsubdirs; i++) {
342 if (!ref_is_absolute &&
343 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
344 refname) == -1)
345 return got_error_from_errno("asprintf");
346 err = parse_packed_ref_line(ref, abs_refname, line,
347 mtime);
348 if (!ref_is_absolute)
349 free(abs_refname);
350 if (err || *ref != NULL)
351 break;
353 if (err)
354 break;
355 } while (*ref == NULL);
356 free(line);
358 return err;
361 static const struct got_error *
362 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
363 const char *name, int lock)
365 const struct got_error *err = NULL;
366 char *path = NULL;
367 char *absname = NULL;
368 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
369 int ref_is_well_known = (subdir[0] == '\0' && is_well_known_ref(name));
371 *ref = NULL;
373 if (!got_ref_name_is_valid(name))
374 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
376 if (ref_is_absolute || ref_is_well_known) {
377 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
378 return got_error_from_errno("asprintf");
379 absname = (char *)name;
380 } else {
381 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
382 subdir[0] ? "/" : "", name) == -1)
383 return got_error_from_errno("asprintf");
385 if (asprintf(&absname, "refs/%s%s%s",
386 subdir, subdir[0] ? "/" : "", name) == -1) {
387 err = got_error_from_errno("asprintf");
388 goto done;
392 err = parse_ref_file(ref, name, absname, path, lock);
393 done:
394 if (!ref_is_absolute && !ref_is_well_known)
395 free(absname);
396 free(path);
397 return err;
400 const struct got_error *
401 got_ref_open(struct got_reference **ref, struct got_repository *repo,
402 const char *refname, int lock)
404 const struct got_error *err = NULL;
405 char *packed_refs_path = NULL, *path_refs = NULL;
406 const char *subdirs[] = {
407 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
408 };
409 size_t i;
410 int well_known = is_well_known_ref(refname);
411 struct got_lockfile *lf = NULL;
413 *ref = NULL;
415 path_refs = get_refs_dir_path(repo, refname);
416 if (path_refs == NULL) {
417 err = got_error_from_errno2("get_refs_dir_path", refname);
418 goto done;
421 if (well_known) {
422 err = open_ref(ref, path_refs, "", refname, lock);
423 } else {
424 FILE *f;
426 /* Search on-disk refs before packed refs! */
427 for (i = 0; i < nitems(subdirs); i++) {
428 err = open_ref(ref, path_refs, subdirs[i], refname,
429 lock);
430 if ((err && err->code != GOT_ERR_NOT_REF) || *ref)
431 goto done;
434 packed_refs_path = got_repo_get_path_packed_refs(repo);
435 if (packed_refs_path == NULL) {
436 err = got_error_from_errno(
437 "got_repo_get_path_packed_refs");
438 goto done;
441 if (lock) {
442 err = got_lockfile_lock(&lf, packed_refs_path, -1);
443 if (err)
444 goto done;
446 f = fopen(packed_refs_path, "rbe");
447 if (f != NULL) {
448 struct stat sb;
449 if (fstat(fileno(f), &sb) == -1) {
450 err = got_error_from_errno2("fstat",
451 packed_refs_path);
452 goto done;
454 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
455 refname, sb.st_mtime);
456 if (!err) {
457 if (fclose(f) == EOF) {
458 err = got_error_from_errno("fclose");
459 got_ref_close(*ref);
460 *ref = NULL;
461 } else if (*ref)
462 (*ref)->lf = lf;
466 done:
467 if (!err && *ref == NULL)
468 err = got_error_not_ref(refname);
469 if (err && lf)
470 got_lockfile_unlock(lf, -1);
471 free(packed_refs_path);
472 free(path_refs);
473 return err;
476 void
477 got_ref_close(struct got_reference *ref)
479 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
480 free(ref->ref.symref.name);
481 free(ref->ref.symref.ref);
482 } else
483 free(ref->ref.ref.name);
484 free(ref);
487 struct got_reference *
488 got_ref_dup(struct got_reference *ref)
490 struct got_reference *ret;
492 ret = calloc(1, sizeof(*ret));
493 if (ret == NULL)
494 return NULL;
496 ret->flags = ref->flags;
497 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
498 ret->ref.symref.name = strdup(ref->ref.symref.name);
499 if (ret->ref.symref.name == NULL) {
500 free(ret);
501 return NULL;
503 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
504 if (ret->ref.symref.ref == NULL) {
505 free(ret->ref.symref.name);
506 free(ret);
507 return NULL;
509 } else {
510 ret->ref.ref.name = strdup(ref->ref.ref.name);
511 if (ret->ref.ref.name == NULL) {
512 free(ret);
513 return NULL;
515 memcpy(&ret->ref.ref.id, &ref->ref.ref.id,
516 sizeof(ret->ref.ref.id));
519 return ret;
522 const struct got_error *
523 got_reflist_entry_dup(struct got_reflist_entry **newp,
524 struct got_reflist_entry *re)
526 const struct got_error *err = NULL;
527 struct got_reflist_entry *new;
529 *newp = NULL;
531 new = malloc(sizeof(*new));
532 if (new == NULL)
533 return got_error_from_errno("malloc");
535 new->ref = got_ref_dup(re->ref);
536 if (new->ref == NULL) {
537 err = got_error_from_errno("got_ref_dup");
538 free(new);
539 return err;
542 *newp = new;
543 return NULL;
546 const struct got_error *
547 got_ref_resolve_symbolic(struct got_reference **resolved,
548 struct got_repository *repo, struct got_reference *ref)
550 struct got_reference *nextref;
551 const struct got_error *err;
553 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
554 if (err)
555 return err;
557 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
558 err = got_ref_resolve_symbolic(resolved, repo, nextref);
559 else
560 *resolved = got_ref_dup(nextref);
562 got_ref_close(nextref);
563 return err;
566 static const struct got_error *
567 ref_resolve(struct got_object_id **id, struct got_repository *repo,
568 struct got_reference *ref, int recursion)
570 const struct got_error *err;
572 if (recursion <= 0)
573 return got_error_msg(GOT_ERR_RECURSION,
574 "reference recursion limit reached");
576 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
577 struct got_reference *resolved = NULL;
578 err = got_ref_resolve_symbolic(&resolved, repo, ref);
579 if (err == NULL)
580 err = ref_resolve(id, repo, resolved, --recursion);
581 if (resolved)
582 got_ref_close(resolved);
583 return err;
586 *id = calloc(1, sizeof(**id));
587 if (*id == NULL)
588 return got_error_from_errno("calloc");
589 memcpy(*id, &ref->ref.ref.id, sizeof(**id));
590 return NULL;
593 const struct got_error *
594 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
595 struct got_reference *ref)
597 return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
600 char *
601 got_ref_to_str(struct got_reference *ref)
603 char *str;
605 if (ref->flags & GOT_REF_IS_SYMBOLIC)
606 return strdup(ref->ref.symref.ref);
608 if (got_object_id_str(&str, &ref->ref.ref.id) != NULL)
609 return NULL;
611 return str;
614 const char *
615 got_ref_get_name(struct got_reference *ref)
617 if (ref->flags & GOT_REF_IS_SYMBOLIC)
618 return ref->ref.symref.name;
620 return ref->ref.ref.name;
623 const char *
624 got_ref_get_symref_target(struct got_reference *ref)
626 if (ref->flags & GOT_REF_IS_SYMBOLIC)
627 return ref->ref.symref.ref;
629 return NULL;
632 time_t
633 got_ref_get_mtime(struct got_reference *ref)
635 return ref->mtime;
638 const struct got_error *
639 got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
640 struct got_reference* re2)
642 const char *name1 = got_ref_get_name(re1);
643 const char *name2 = got_ref_get_name(re2);
645 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
646 return NULL;
649 const struct got_error *
650 got_ref_cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
651 struct got_reference *ref2)
653 const struct got_error *err = NULL;
654 struct got_repository *repo = arg;
655 struct got_object_id *id1, *id2 = NULL;
656 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
657 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
658 time_t time1, time2;
660 *cmp = 0;
662 err = got_ref_resolve(&id1, repo, ref1);
663 if (err)
664 return err;
665 err = got_object_open_as_tag(&tag1, repo, id1);
666 if (err) {
667 if (err->code != GOT_ERR_OBJ_TYPE)
668 goto done;
669 /* "lightweight" tag */
670 err = got_object_open_as_commit(&commit1, repo, id1);
671 if (err)
672 goto done;
673 time1 = got_object_commit_get_committer_time(commit1);
674 } else
675 time1 = got_object_tag_get_tagger_time(tag1);
677 err = got_ref_resolve(&id2, repo, ref2);
678 if (err)
679 goto done;
680 err = got_object_open_as_tag(&tag2, repo, id2);
681 if (err) {
682 if (err->code != GOT_ERR_OBJ_TYPE)
683 goto done;
684 /* "lightweight" tag */
685 err = got_object_open_as_commit(&commit2, repo, id2);
686 if (err)
687 goto done;
688 time2 = got_object_commit_get_committer_time(commit2);
689 } else
690 time2 = got_object_tag_get_tagger_time(tag2);
692 /* Put latest tags first. */
693 if (time1 < time2)
694 *cmp = 1;
695 else if (time1 > time2)
696 *cmp = -1;
697 else
698 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
699 done:
700 free(id1);
701 free(id2);
702 if (tag1)
703 got_object_tag_close(tag1);
704 if (tag2)
705 got_object_tag_close(tag2);
706 if (commit1)
707 got_object_commit_close(commit1);
708 if (commit2)
709 got_object_commit_close(commit2);
710 return err;
713 static const struct got_error *
714 get_committer_time(struct got_reference *ref, struct got_repository *repo)
716 const struct got_error *err = NULL;
717 int obj_type;
718 struct got_commit_object *commit = NULL;
719 struct got_tag_object *tag = NULL;
720 struct got_object_id *id = NULL;
722 err = got_ref_resolve(&id, repo, ref);
723 if (err)
724 return err;
726 err = got_object_get_type(&obj_type, repo, id);
727 if (err)
728 goto done;
730 switch (obj_type) {
731 case GOT_OBJ_TYPE_COMMIT:
732 err = got_object_open_as_commit(&commit, repo, id);
733 if (err)
734 goto done;
735 ref->committer_time =
736 got_object_commit_get_committer_time(commit);
737 break;
738 case GOT_OBJ_TYPE_TAG:
739 err = got_object_open_as_tag(&tag, repo, id);
740 if (err)
741 goto done;
742 ref->committer_time = got_object_tag_get_tagger_time(tag);
743 break;
744 default:
745 /* best effort for other object types */
746 ref->committer_time = got_ref_get_mtime(ref);
747 break;
749 done:
750 free(id);
751 if (commit)
752 got_object_commit_close(commit);
753 if (tag)
754 got_object_tag_close(tag);
755 return err;
758 const struct got_error *
759 got_ref_cmp_by_commit_timestamp_descending(void *arg, int *cmp,
760 struct got_reference *ref1, struct got_reference *ref2)
762 const struct got_error *err = NULL;
763 struct got_repository *repo = arg;
765 *cmp = 0;
767 if (ref1->committer_time == 0) {
768 err = get_committer_time(ref1, repo);
769 if (err)
770 return err;
772 if (ref2->committer_time == 0) {
773 err = get_committer_time(ref2, repo);
774 if (err)
775 return err;
778 if (ref1->committer_time < ref2->committer_time)
779 *cmp = 1;
780 else if (ref2->committer_time < ref1->committer_time)
781 *cmp = -1;
782 else
783 return got_ref_cmp_by_name(arg, cmp, ref1, ref2);
785 return err;
788 const struct got_error *
789 got_reflist_insert(struct got_reflist_entry **newp,
790 struct got_reflist_head *refs, struct got_reference *ref,
791 got_ref_cmp_cb cmp_cb, void *cmp_arg)
793 const struct got_error *err;
794 struct got_reflist_entry *new, *re;
795 int cmp;
797 *newp = NULL;
799 if (cmp_cb != got_ref_cmp_by_name && (ref->flags & GOT_REF_IS_PACKED)) {
800 /*
801 * If we are not sorting elements by name then we must still
802 * detect collisions between a packed ref and an on-disk ref
803 * using the same name. On-disk refs take precedence and are
804 * already present on the list before packed refs get added.
805 */
806 TAILQ_FOREACH(re, refs, entry) {
807 err = got_ref_cmp_by_name(NULL, &cmp, re->ref, ref);
808 if (err)
809 return err;
810 if (cmp == 0)
811 return NULL;
815 new = malloc(sizeof(*new));
816 if (new == NULL)
817 return got_error_from_errno("malloc");
818 new->ref = ref;
819 *newp = new;
821 /*
822 * We must de-duplicate entries on insert because packed-refs may
823 * contain redundant entries. On-disk refs take precedence.
824 * This code assumes that on-disk revs are read before packed-refs.
825 * We're iterating the list anyway, so insert elements sorted by name.
827 * Many callers will provide paths in a somewhat sorted order.
828 * Iterating backwards from the tail of the list should be more
829 * efficient than traversing through the entire list each time
830 * an element is inserted.
831 */
832 re = TAILQ_LAST(refs, got_reflist_head);
833 while (re) {
834 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
835 if (err)
836 return err;
837 if (cmp == 0) {
838 /* duplicate */
839 free(new);
840 *newp = NULL;
841 return NULL;
842 } else if (cmp < 0) {
843 TAILQ_INSERT_AFTER(refs, re, new, entry);
844 return NULL;
846 re = TAILQ_PREV(re, got_reflist_head, entry);
849 TAILQ_INSERT_HEAD(refs, new, entry);
850 return NULL;
853 const struct got_error *
854 got_reflist_sort(struct got_reflist_head *refs,
855 got_ref_cmp_cb cmp_cb, void *cmp_arg)
857 const struct got_error *err = NULL;
858 struct got_reflist_entry *re, *tmp, *new;
859 struct got_reflist_head sorted;
861 TAILQ_INIT(&sorted);
863 TAILQ_FOREACH_SAFE(re, refs, entry, tmp) {
864 struct got_reference *ref = re->ref;
865 TAILQ_REMOVE(refs, re, entry);
866 free(re);
867 err = got_reflist_insert(&new, &sorted, ref, cmp_cb, cmp_arg);
868 if (err || new == NULL /* duplicate */)
869 got_ref_close(ref);
870 if (err)
871 return err;
874 TAILQ_CONCAT(refs, &sorted, entry);
875 return NULL;
878 static const struct got_error *
879 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
880 const char *subdir, struct got_repository *repo,
881 got_ref_cmp_cb cmp_cb, void *cmp_arg)
883 const struct got_error *err = NULL;
884 DIR *d = NULL;
885 char *path_subdir;
887 while (subdir[0] == '/')
888 subdir++;
890 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
891 return got_error_from_errno("asprintf");
893 d = opendir(path_subdir);
894 if (d == NULL)
895 goto done;
897 for (;;) {
898 struct dirent *dent;
899 struct got_reference *ref;
900 char *child;
901 int type;
903 dent = readdir(d);
904 if (dent == NULL)
905 break;
907 if (strcmp(dent->d_name, ".") == 0 ||
908 strcmp(dent->d_name, "..") == 0)
909 continue;
911 err = got_path_dirent_type(&type, path_subdir, dent);
912 if (err)
913 break;
915 switch (type) {
916 case DT_REG:
917 err = open_ref(&ref, path_refs, subdir, dent->d_name,
918 0);
919 if (err)
920 goto done;
921 if (ref) {
922 struct got_reflist_entry *new;
923 err = got_reflist_insert(&new, refs, ref,
924 cmp_cb, cmp_arg);
925 if (err || new == NULL /* duplicate */)
926 got_ref_close(ref);
927 if (err)
928 goto done;
930 break;
931 case DT_DIR:
932 if (asprintf(&child, "%s%s%s", subdir,
933 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
934 err = got_error_from_errno("asprintf");
935 break;
937 err = gather_on_disk_refs(refs, path_refs, child, repo,
938 cmp_cb, cmp_arg);
939 free(child);
940 break;
941 default:
942 break;
945 done:
946 if (d)
947 closedir(d);
948 free(path_subdir);
949 return err;
952 const struct got_error *
953 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
954 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
956 const struct got_error *err;
957 char *packed_refs_path = NULL, *path_refs = NULL;
958 char *abs_namespace = NULL, *buf = NULL;
959 const char *ondisk_ref_namespace = NULL;
960 char *line = NULL;
961 FILE *f = NULL;
962 struct got_reference *ref;
963 struct got_reflist_entry *new;
965 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
966 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
967 if (path_refs == NULL) {
968 err = got_error_from_errno("get_refs_dir_path");
969 goto done;
971 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
972 if (err)
973 goto done;
974 err = got_reflist_insert(&new, refs, ref, cmp_cb, cmp_arg);
975 if (err || new == NULL /* duplicate */)
976 got_ref_close(ref);
977 if (err && err->code != GOT_ERR_NOT_REF)
978 goto done;
979 } else {
980 /* Try listing a single reference. */
981 const char *refname = ref_namespace;
982 path_refs = get_refs_dir_path(repo, refname);
983 if (path_refs == NULL) {
984 err = got_error_from_errno("get_refs_dir_path");
985 goto done;
987 err = open_ref(&ref, path_refs, "", refname, 0);
988 if (err) {
989 if (err->code != GOT_ERR_NOT_REF)
990 goto done;
991 /* Try to look up references in a given namespace. */
992 } else {
993 err = got_reflist_insert(&new, refs, ref,
994 cmp_cb, cmp_arg);
995 if (err || new == NULL /* duplicate */)
996 got_ref_close(ref);
997 return err;
1001 if (ref_namespace) {
1002 size_t len;
1003 /* Canonicalize the path to eliminate double-slashes if any. */
1004 if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
1005 err = got_error_from_errno("asprintf");
1006 goto done;
1008 len = strlen(abs_namespace) + 1;
1009 buf = malloc(len);
1010 if (buf == NULL) {
1011 err = got_error_from_errno("malloc");
1012 goto done;
1014 err = got_canonpath(abs_namespace, buf, len);
1015 if (err)
1016 goto done;
1017 ondisk_ref_namespace = buf;
1018 while (ondisk_ref_namespace[0] == '/')
1019 ondisk_ref_namespace++;
1020 if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
1021 ondisk_ref_namespace += 5;
1022 else if (strcmp(ondisk_ref_namespace, "refs") == 0)
1023 ondisk_ref_namespace = "";
1026 /* Gather on-disk refs before parsing packed-refs. */
1027 free(path_refs);
1028 path_refs = get_refs_dir_path(repo, "");
1029 if (path_refs == NULL) {
1030 err = got_error_from_errno("get_refs_dir_path");
1031 goto done;
1033 err = gather_on_disk_refs(refs, path_refs,
1034 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
1035 cmp_cb, cmp_arg);
1036 if (err)
1037 goto done;
1040 * The packed-refs file may contain redundant entries, in which
1041 * case on-disk refs take precedence.
1043 packed_refs_path = got_repo_get_path_packed_refs(repo);
1044 if (packed_refs_path == NULL) {
1045 err = got_error_from_errno("got_repo_get_path_packed_refs");
1046 goto done;
1049 f = fopen(packed_refs_path, "re");
1050 if (f) {
1051 size_t linesize = 0;
1052 ssize_t linelen;
1053 struct stat sb;
1055 if (fstat(fileno(f), &sb) == -1) {
1056 err = got_error_from_errno2("fstat", packed_refs_path);
1057 goto done;
1059 for (;;) {
1060 linelen = getline(&line, &linesize, f);
1061 if (linelen == -1) {
1062 if (feof(f))
1063 break;
1064 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1065 goto done;
1067 if (linelen > 0 && line[linelen - 1] == '\n')
1068 line[linelen - 1] = '\0';
1069 err = parse_packed_ref_line(&ref, NULL, line,
1070 sb.st_mtime);
1071 if (err)
1072 goto done;
1073 if (ref) {
1074 if (ref_namespace) {
1075 const char *name;
1076 name = got_ref_get_name(ref);
1077 if (!got_path_is_child(name,
1078 ref_namespace,
1079 strlen(ref_namespace))) {
1080 got_ref_close(ref);
1081 continue;
1084 err = got_reflist_insert(&new, refs, ref,
1085 cmp_cb, cmp_arg);
1086 if (err || new == NULL /* duplicate */)
1087 got_ref_close(ref);
1088 if (err)
1089 goto done;
1093 done:
1094 free(packed_refs_path);
1095 free(abs_namespace);
1096 free(buf);
1097 free(line);
1098 free(path_refs);
1099 if (f && fclose(f) == EOF && err == NULL)
1100 err = got_error_from_errno("fclose");
1101 return err;
1104 void
1105 got_ref_list_free(struct got_reflist_head *refs)
1107 struct got_reflist_entry *re;
1109 while ((re = TAILQ_FIRST(refs))) {
1110 TAILQ_REMOVE(refs, re, entry);
1111 got_ref_close(re->ref);
1112 free(re);
1116 int
1117 got_ref_is_symbolic(struct got_reference *ref)
1119 return (ref->flags & GOT_REF_IS_SYMBOLIC);
1122 const struct got_error *
1123 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1125 if (ref->flags & GOT_REF_IS_SYMBOLIC)
1126 return got_error(GOT_ERR_BAD_REF_TYPE);
1128 memcpy(&ref->ref.ref.id, id, sizeof(ref->ref.ref.id));
1129 return NULL;
1132 const struct got_error *
1133 got_ref_change_symref(struct got_reference *ref, const char *refname)
1135 char *new_name;
1137 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1138 return got_error(GOT_ERR_BAD_REF_TYPE);
1140 new_name = strdup(refname);
1141 if (new_name == NULL)
1142 return got_error_from_errno("strdup");
1144 free(ref->ref.symref.ref);
1145 ref->ref.symref.ref = new_name;
1146 return NULL;
1149 const struct got_error *
1150 got_ref_change_symref_to_ref(struct got_reference *symref,
1151 struct got_object_id *id)
1153 if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1154 return got_error(GOT_ERR_BAD_REF_TYPE);
1156 symref->ref.ref.name = symref->ref.symref.name;
1157 memcpy(&symref->ref.ref.id, id, sizeof(symref->ref.ref.id));
1158 symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1159 return NULL;
1162 const struct got_error *
1163 got_ref_write(struct got_reference *ref, struct got_repository *repo)
1165 const struct got_error *err = NULL, *unlock_err = NULL;
1166 const char *name = got_ref_get_name(ref);
1167 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1168 struct got_lockfile *lf = NULL;
1169 FILE *f = NULL;
1170 size_t n;
1171 struct stat sb;
1173 path_refs = get_refs_dir_path(repo, name);
1174 if (path_refs == NULL) {
1175 err = got_error_from_errno2("get_refs_dir_path", name);
1176 goto done;
1179 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1180 err = got_error_from_errno("asprintf");
1181 goto done;
1184 err = got_opentemp_named(&tmppath, &f, path, "");
1185 if (err) {
1186 char *parent;
1187 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1188 goto done;
1189 err = got_path_dirname(&parent, path);
1190 if (err)
1191 goto done;
1192 err = got_path_mkdir(parent);
1193 free(parent);
1194 if (err)
1195 goto done;
1196 err = got_opentemp_named(&tmppath, &f, path, "");
1197 if (err)
1198 goto done;
1201 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1202 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1203 if (n != strlen(ref->ref.symref.ref) + 6) {
1204 err = got_ferror(f, GOT_ERR_IO);
1205 goto done;
1207 } else {
1208 char *hex;
1209 size_t len;
1211 err = got_object_id_str(&hex, &ref->ref.ref.id);
1212 if (err)
1213 goto done;
1214 len = strlen(hex);
1215 n = fprintf(f, "%s\n", hex);
1216 free(hex);
1217 if (n != len + 1) {
1218 err = got_ferror(f, GOT_ERR_IO);
1219 goto done;
1223 if (ref->lf == NULL) {
1224 err = got_lockfile_lock(&lf, path, -1);
1225 if (err)
1226 goto done;
1229 /* XXX: check if old content matches our expectations? */
1231 if (stat(path, &sb) != 0) {
1232 if (errno != ENOENT) {
1233 err = got_error_from_errno2("stat", path);
1234 goto done;
1236 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1239 if (fchmod(fileno(f), sb.st_mode) != 0) {
1240 err = got_error_from_errno2("fchmod", tmppath);
1241 goto done;
1244 if (rename(tmppath, path) != 0) {
1245 err = got_error_from_errno3("rename", tmppath, path);
1246 goto done;
1248 free(tmppath);
1249 tmppath = NULL;
1251 if (stat(path, &sb) == -1) {
1252 err = got_error_from_errno2("stat", path);
1253 goto done;
1255 ref->mtime = sb.st_mtime;
1256 done:
1257 if (ref->lf == NULL && lf)
1258 unlock_err = got_lockfile_unlock(lf, -1);
1259 if (f) {
1260 if (fclose(f) == EOF && err == NULL)
1261 err = got_error_from_errno("fclose");
1263 free(path_refs);
1264 free(path);
1265 if (tmppath) {
1266 if (unlink(tmppath) == -1 && err == NULL)
1267 err = got_error_from_errno2("unlink", tmppath);
1268 free(tmppath);
1270 return err ? err : unlock_err;
1273 static const struct got_error *
1274 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1276 const struct got_error *err = NULL, *unlock_err = NULL;
1277 struct got_lockfile *lf = NULL;
1278 FILE *f = NULL, *tmpf = NULL;
1279 char *line = NULL, *packed_refs_path, *tmppath = NULL;
1280 size_t linesize = 0;
1281 struct got_reflist_head refs;
1282 int found_delref = 0;
1284 /* The packed-refs file does not cotain symbolic references. */
1285 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1286 return got_error(GOT_ERR_BAD_REF_DATA);
1288 TAILQ_INIT(&refs);
1290 packed_refs_path = got_repo_get_path_packed_refs(repo);
1291 if (packed_refs_path == NULL)
1292 return got_error_from_errno("got_repo_get_path_packed_refs");
1294 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path, "");
1295 if (err)
1296 goto done;
1298 if (delref->lf == NULL) {
1299 err = got_lockfile_lock(&lf, packed_refs_path, -1);
1300 if (err)
1301 goto done;
1304 f = fopen(packed_refs_path, "re");
1305 if (f == NULL) {
1306 err = got_error_from_errno2("fopen", packed_refs_path);
1307 goto done;
1309 for (;;) {
1310 ssize_t linelen;
1311 struct got_reference *ref;
1312 struct got_reflist_entry *new;
1314 linelen = getline(&line, &linesize, f);
1315 if (linelen == -1) {
1316 if (feof(f))
1317 break;
1318 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1319 goto done;
1321 if (linelen > 0 && line[linelen - 1] == '\n')
1322 line[linelen - 1] = '\0';
1323 err = parse_packed_ref_line(&ref, NULL, line, 0);
1324 if (err)
1325 goto done;
1326 if (ref == NULL)
1327 continue;
1329 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1330 got_object_id_cmp(&ref->ref.ref.id,
1331 &delref->ref.ref.id) == 0) {
1332 found_delref = 1;
1333 got_ref_close(ref);
1334 continue;
1337 err = got_reflist_insert(&new, &refs, ref,
1338 got_ref_cmp_by_name, NULL);
1339 if (err || new == NULL /* duplicate */)
1340 got_ref_close(ref);
1341 if (err)
1342 goto done;
1345 if (found_delref) {
1346 struct got_reflist_entry *re;
1347 size_t n;
1348 struct stat sb;
1350 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1351 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1352 err = got_ferror(f, GOT_ERR_IO);
1353 goto done;
1356 TAILQ_FOREACH(re, &refs, entry) {
1357 char *hex;
1358 size_t len;
1360 err = got_object_id_str(&hex, &re->ref->ref.ref.id);
1361 if (err)
1362 goto done;
1363 len = strlen(hex);
1364 n = fprintf(tmpf, "%s ", hex);
1365 free(hex);
1366 if (n != len + 1) {
1367 err = got_ferror(f, GOT_ERR_IO);
1368 goto done;
1371 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1372 if (n != strlen(re->ref->ref.ref.name) + 1) {
1373 err = got_ferror(f, GOT_ERR_IO);
1374 goto done;
1378 if (fflush(tmpf) != 0) {
1379 err = got_error_from_errno("fflush");
1380 goto done;
1383 if (fstat(fileno(f), &sb) != 0) {
1384 if (errno != ENOENT) {
1385 err = got_error_from_errno2("fstat",
1386 packed_refs_path);
1387 goto done;
1389 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1392 if (fchmod(fileno(tmpf), sb.st_mode) != 0) {
1393 err = got_error_from_errno2("fchmod", tmppath);
1394 goto done;
1397 if (rename(tmppath, packed_refs_path) != 0) {
1398 err = got_error_from_errno3("rename", tmppath,
1399 packed_refs_path);
1400 goto done;
1402 free(tmppath);
1403 tmppath = NULL;
1405 done:
1406 if (delref->lf == NULL && lf)
1407 unlock_err = got_lockfile_unlock(lf, -1);
1408 if (f) {
1409 if (fclose(f) == EOF && err == NULL)
1410 err = got_error_from_errno("fclose");
1412 if (tmppath && unlink(tmppath) == -1 && err == NULL)
1413 err = got_error_from_errno2("unlink", tmppath);
1414 if (tmpf && fclose(tmpf) == EOF && err == NULL)
1415 err = got_error_from_errno("fclose");
1416 free(tmppath);
1417 free(packed_refs_path);
1418 free(line);
1419 got_ref_list_free(&refs);
1420 return err ? err : unlock_err;
1423 static const struct got_error *
1424 delete_loose_ref(struct got_reference *ref, struct got_repository *repo)
1426 const struct got_error *err = NULL, *unlock_err = NULL;
1427 const char *name = got_ref_get_name(ref);
1428 char *path_refs = NULL, *path = NULL;
1429 struct got_lockfile *lf = NULL;
1431 path_refs = get_refs_dir_path(repo, name);
1432 if (path_refs == NULL) {
1433 err = got_error_from_errno2("get_refs_dir_path", name);
1434 goto done;
1437 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1438 err = got_error_from_errno("asprintf");
1439 goto done;
1442 if (ref->lf == NULL) {
1443 err = got_lockfile_lock(&lf, path, -1);
1444 if (err)
1445 goto done;
1448 /* XXX: check if old content matches our expectations? */
1450 if (unlink(path) == -1)
1451 err = got_error_from_errno2("unlink", path);
1452 done:
1453 if (ref->lf == NULL && lf)
1454 unlock_err = got_lockfile_unlock(lf, -1);
1456 free(path_refs);
1457 free(path);
1458 return err ? err : unlock_err;
1461 const struct got_error *
1462 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1464 const struct got_error *err = NULL;
1465 struct got_reference *ref2;
1467 if (ref->flags & GOT_REF_IS_PACKED) {
1468 err = delete_packed_ref(ref, repo);
1469 if (err)
1470 return err;
1472 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1473 if (err) {
1474 if (err->code == GOT_ERR_NOT_REF)
1475 return NULL;
1476 return err;
1479 err = delete_loose_ref(ref2, repo);
1480 got_ref_close(ref2);
1481 return err;
1482 } else {
1483 err = delete_loose_ref(ref, repo);
1484 if (err)
1485 return err;
1487 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1488 if (err) {
1489 if (err->code == GOT_ERR_NOT_REF)
1490 return NULL;
1491 return err;
1494 err = delete_packed_ref(ref2, repo);
1495 got_ref_close(ref2);
1496 return err;
1500 const struct got_error *
1501 got_ref_unlock(struct got_reference *ref)
1503 const struct got_error *err;
1504 err = got_lockfile_unlock(ref->lf, -1);
1505 ref->lf = NULL;
1506 return err;
1509 struct got_reflist_object_id_map {
1510 struct got_object_idset *idset;
1513 struct got_reflist_object_id_map_entry {
1514 struct got_reflist_head refs;
1517 static const struct got_error *
1518 add_object_id_map_entry(struct got_object_idset *idset,
1519 struct got_object_id *id, struct got_reflist_entry *re)
1521 const struct got_error *err = NULL;
1522 struct got_reflist_object_id_map_entry *ent;
1523 struct got_reflist_entry *new;
1525 ent = got_object_idset_get(idset, id);
1526 if (ent == NULL) {
1527 ent = malloc(sizeof(*ent));
1528 if (ent == NULL)
1529 return got_error_from_errno("malloc");
1531 TAILQ_INIT(&ent->refs);
1532 err = got_object_idset_add(idset, id, ent);
1533 if (err) {
1534 free(ent);
1535 return err;
1539 err = got_reflist_entry_dup(&new, re);
1540 if (err)
1541 return err;
1543 TAILQ_INSERT_TAIL(&ent->refs, new, entry);
1544 return NULL;
1547 const struct got_error *
1548 got_reflist_object_id_map_create(struct got_reflist_object_id_map **map,
1549 struct got_reflist_head *refs, struct got_repository *repo)
1551 const struct got_error *err = NULL;
1552 struct got_object_idset *idset;
1553 struct got_object_id *id = NULL;
1554 struct got_reflist_entry *re;
1556 idset = got_object_idset_alloc();
1557 if (idset == NULL)
1558 return got_error_from_errno("got_object_idset_alloc");
1560 *map = malloc(sizeof(**map));
1561 if (*map == NULL) {
1562 got_object_idset_free(idset);
1563 return got_error_from_errno("malloc");
1565 (*map)->idset = idset;
1567 TAILQ_FOREACH(re, refs, entry) {
1568 struct got_tag_object *tag = NULL;
1570 err = got_ref_resolve(&id, repo, re->ref);
1571 if (err)
1572 goto done;
1574 err = add_object_id_map_entry(idset, id, re);
1575 if (err)
1576 goto done;
1578 if (strstr(got_ref_get_name(re->ref), "/tags/") == NULL) {
1579 free(id);
1580 id = NULL;
1581 continue;
1584 err = got_object_open_as_tag(&tag, repo, id);
1585 if (err) {
1586 if (err->code != GOT_ERR_OBJ_TYPE)
1587 goto done;
1588 /* Ref points at something other than a tag. */
1589 err = NULL;
1590 tag = NULL;
1591 free(id);
1592 id = NULL;
1593 continue;
1596 err = add_object_id_map_entry(idset,
1597 got_object_tag_get_object_id(tag), re);
1598 got_object_tag_close(tag);
1599 if (err)
1600 goto done;
1602 free(id);
1603 id = NULL;
1605 done:
1606 free(id);
1607 if (err) {
1608 got_reflist_object_id_map_free(*map);
1609 *map = NULL;
1611 return err;
1614 struct got_reflist_head *
1615 got_reflist_object_id_map_lookup(struct got_reflist_object_id_map *map,
1616 struct got_object_id *id)
1618 struct got_reflist_object_id_map_entry *ent;
1619 ent = got_object_idset_get(map->idset, id);
1620 if (ent)
1621 return &ent->refs;
1622 return NULL;
1625 static const struct got_error *
1626 free_id_map_entry(struct got_object_id *id, void *data, void *arg)
1628 struct got_reflist_object_id_map_entry *ent = data;
1630 got_ref_list_free(&ent->refs);
1631 free(ent);
1632 return NULL;
1635 void
1636 got_reflist_object_id_map_free(struct got_reflist_object_id_map *map)
1638 got_object_idset_for_each(map->idset, free_id_map_entry, NULL);
1639 got_object_idset_free(map->idset);
1640 free(map);