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 <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <util.h>
30 #include <zlib.h>
31 #include <time.h>
32 #include <libgen.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_sha1.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 struct got_object_id id;
159 if (strncmp(line, "ref: ", 5) == 0) {
160 line += 5;
161 return parse_symref(ref, name, line);
164 if (!got_parse_sha1_digest(id.sha1, line))
165 return got_error(GOT_ERR_BAD_REF_DATA);
167 return alloc_ref(ref, name, &id, 0, mtime);
170 static const struct got_error *
171 parse_ref_file(struct got_reference **ref, const char *name,
172 const char *absname, const char *abspath, int lock)
174 const struct got_error *err = NULL;
175 FILE *f;
176 char *line = NULL;
177 size_t linesize = 0;
178 ssize_t linelen;
179 struct got_lockfile *lf = NULL;
180 struct stat sb;
182 if (lock) {
183 err = got_lockfile_lock(&lf, abspath, -1);
184 if (err) {
185 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
186 err = got_error_not_ref(name);
187 return err;
191 f = fopen(abspath, "rbe");
192 if (f == NULL) {
193 if (errno != ENOTDIR && errno != ENOENT)
194 err = got_error_from_errno2("fopen", abspath);
195 else
196 err = got_error_not_ref(name);
197 if (lock)
198 got_lockfile_unlock(lf, -1);
199 return err;
201 if (fstat(fileno(f), &sb) == -1) {
202 err = got_error_from_errno2("fstat", abspath);
203 goto done;
206 linelen = getline(&line, &linesize, f);
207 if (linelen == -1) {
208 if (feof(f))
209 err = NULL; /* ignore empty files (could be locks) */
210 else {
211 if (errno == EISDIR)
212 err = got_error(GOT_ERR_NOT_REF);
213 else if (ferror(f))
214 err = got_ferror(f, GOT_ERR_IO);
215 else
216 err = got_error_from_errno2("getline", abspath);
218 if (lock)
219 got_lockfile_unlock(lf, -1);
220 goto done;
222 while (linelen > 0 && line[linelen - 1] == '\n') {
223 line[linelen - 1] = '\0';
224 linelen--;
227 err = parse_ref_line(ref, absname, line, sb.st_mtime);
228 if (lock) {
229 if (err)
230 got_lockfile_unlock(lf, -1);
231 else {
232 if (*ref)
233 (*ref)->lf = lf;
234 else
235 got_lockfile_unlock(lf, -1);
238 done:
239 free(line);
240 if (fclose(f) == EOF && err == NULL) {
241 err = got_error_from_errno("fclose");
242 if (*ref) {
243 if (lock)
244 got_ref_unlock(*ref);
245 got_ref_close(*ref);
246 *ref = NULL;
249 return err;
252 static int
253 is_well_known_ref(const char *refname)
255 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
256 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
257 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
258 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
261 static char *
262 get_refs_dir_path(struct got_repository *repo, const char *refname)
264 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
265 return strdup(got_repo_get_path_git_dir(repo));
267 return got_repo_get_path_refs(repo);
270 const struct got_error *
271 got_ref_alloc(struct got_reference **ref, const char *name,
272 struct got_object_id *id)
274 if (!got_ref_name_is_valid(name))
275 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
277 return alloc_ref(ref, name, id, 0, 0);
280 const struct got_error *
281 got_ref_alloc_symref(struct got_reference **ref, const char *name,
282 struct got_reference *target_ref)
284 if (!got_ref_name_is_valid(name))
285 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
287 return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
290 static const struct got_error *
291 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
292 const char *line, time_t mtime)
294 struct got_object_id id;
295 const char *name;
297 *ref = NULL;
299 if (line[0] == '#' || line[0] == '^')
300 return NULL;
302 if (!got_parse_sha1_digest(id.sha1, line))
303 return got_error(GOT_ERR_BAD_REF_DATA);
305 if (abs_refname) {
306 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
307 return NULL;
308 name = abs_refname;
309 } else
310 name = line + SHA1_DIGEST_STRING_LENGTH;
312 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED, mtime);
315 static const struct got_error *
316 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
317 int nsubdirs, const char *refname, time_t mtime)
319 const struct got_error *err = NULL;
320 char *abs_refname;
321 char *line = NULL;
322 size_t linesize = 0;
323 ssize_t linelen;
324 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
326 *ref = NULL;
328 if (ref_is_absolute)
329 abs_refname = (char *)refname;
330 do {
331 linelen = getline(&line, &linesize, f);
332 if (linelen == -1) {
333 if (feof(f))
334 break;
335 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
336 break;
338 if (linelen > 0 && line[linelen - 1] == '\n')
339 line[linelen - 1] = '\0';
340 for (i = 0; i < nsubdirs; i++) {
341 if (!ref_is_absolute &&
342 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
343 refname) == -1)
344 return got_error_from_errno("asprintf");
345 err = parse_packed_ref_line(ref, abs_refname, line,
346 mtime);
347 if (!ref_is_absolute)
348 free(abs_refname);
349 if (err || *ref != NULL)
350 break;
352 if (err)
353 break;
354 } while (*ref == NULL);
355 free(line);
357 return err;
360 static const struct got_error *
361 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
362 const char *name, int lock)
364 const struct got_error *err = NULL;
365 char *path = NULL;
366 char *absname = NULL;
367 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
368 int ref_is_well_known = (subdir[0] == '\0' && is_well_known_ref(name));
370 *ref = NULL;
372 if (!got_ref_name_is_valid(name))
373 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
375 if (ref_is_absolute || ref_is_well_known) {
376 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
377 return got_error_from_errno("asprintf");
378 absname = (char *)name;
379 } else {
380 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
381 subdir[0] ? "/" : "", name) == -1)
382 return got_error_from_errno("asprintf");
384 if (asprintf(&absname, "refs/%s%s%s",
385 subdir, subdir[0] ? "/" : "", name) == -1) {
386 err = got_error_from_errno("asprintf");
387 goto done;
391 err = parse_ref_file(ref, name, absname, path, lock);
392 done:
393 if (!ref_is_absolute && !ref_is_well_known)
394 free(absname);
395 free(path);
396 return err;
399 const struct got_error *
400 got_ref_open(struct got_reference **ref, struct got_repository *repo,
401 const char *refname, int lock)
403 const struct got_error *err = NULL;
404 char *packed_refs_path = NULL, *path_refs = NULL;
405 const char *subdirs[] = {
406 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
407 };
408 size_t i;
409 int well_known = is_well_known_ref(refname);
410 struct got_lockfile *lf = NULL;
412 *ref = NULL;
414 path_refs = get_refs_dir_path(repo, refname);
415 if (path_refs == NULL) {
416 err = got_error_from_errno2("get_refs_dir_path", refname);
417 goto done;
420 if (well_known) {
421 err = open_ref(ref, path_refs, "", refname, lock);
422 } else {
423 FILE *f;
425 /* Search on-disk refs before packed refs! */
426 for (i = 0; i < nitems(subdirs); i++) {
427 err = open_ref(ref, path_refs, subdirs[i], refname,
428 lock);
429 if ((err && err->code != GOT_ERR_NOT_REF) || *ref)
430 goto done;
433 packed_refs_path = got_repo_get_path_packed_refs(repo);
434 if (packed_refs_path == NULL) {
435 err = got_error_from_errno(
436 "got_repo_get_path_packed_refs");
437 goto done;
440 if (lock) {
441 err = got_lockfile_lock(&lf, packed_refs_path, -1);
442 if (err)
443 goto done;
445 f = fopen(packed_refs_path, "rbe");
446 if (f != NULL) {
447 struct stat sb;
448 if (fstat(fileno(f), &sb) == -1) {
449 err = got_error_from_errno2("fstat",
450 packed_refs_path);
451 goto done;
453 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
454 refname, sb.st_mtime);
455 if (!err) {
456 if (fclose(f) == EOF) {
457 err = got_error_from_errno("fclose");
458 got_ref_close(*ref);
459 *ref = NULL;
460 } else if (*ref)
461 (*ref)->lf = lf;
465 done:
466 if (!err && *ref == NULL)
467 err = got_error_not_ref(refname);
468 if (err && lf)
469 got_lockfile_unlock(lf, -1);
470 free(packed_refs_path);
471 free(path_refs);
472 return err;
475 void
476 got_ref_close(struct got_reference *ref)
478 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
479 free(ref->ref.symref.name);
480 free(ref->ref.symref.ref);
481 } else
482 free(ref->ref.ref.name);
483 free(ref);
486 struct got_reference *
487 got_ref_dup(struct got_reference *ref)
489 struct got_reference *ret;
491 ret = calloc(1, sizeof(*ret));
492 if (ret == NULL)
493 return NULL;
495 ret->flags = ref->flags;
496 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
497 ret->ref.symref.name = strdup(ref->ref.symref.name);
498 if (ret->ref.symref.name == NULL) {
499 free(ret);
500 return NULL;
502 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
503 if (ret->ref.symref.ref == NULL) {
504 free(ret->ref.symref.name);
505 free(ret);
506 return NULL;
508 } else {
509 ret->ref.ref.name = strdup(ref->ref.ref.name);
510 if (ret->ref.ref.name == NULL) {
511 free(ret);
512 return NULL;
514 memcpy(&ret->ref.ref.id, &ref->ref.ref.id,
515 sizeof(ret->ref.ref.id));
518 return ret;
521 const struct got_error *
522 got_reflist_entry_dup(struct got_reflist_entry **newp,
523 struct got_reflist_entry *re)
525 const struct got_error *err = NULL;
526 struct got_reflist_entry *new;
528 *newp = NULL;
530 new = malloc(sizeof(*new));
531 if (new == NULL)
532 return got_error_from_errno("malloc");
534 new->ref = got_ref_dup(re->ref);
535 if (new->ref == NULL) {
536 err = got_error_from_errno("got_ref_dup");
537 free(new);
538 return err;
541 *newp = new;
542 return NULL;
545 const struct got_error *
546 got_ref_resolve_symbolic(struct got_reference **resolved,
547 struct got_repository *repo, struct got_reference *ref)
549 struct got_reference *nextref;
550 const struct got_error *err;
552 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
553 if (err)
554 return err;
556 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
557 err = got_ref_resolve_symbolic(resolved, repo, nextref);
558 else
559 *resolved = got_ref_dup(nextref);
561 got_ref_close(nextref);
562 return err;
565 static const struct got_error *
566 ref_resolve(struct got_object_id **id, struct got_repository *repo,
567 struct got_reference *ref, int recursion)
569 const struct got_error *err;
571 if (recursion <= 0)
572 return got_error_msg(GOT_ERR_RECURSION,
573 "reference recursion limit reached");
575 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
576 struct got_reference *resolved = NULL;
577 err = got_ref_resolve_symbolic(&resolved, repo, ref);
578 if (err == NULL)
579 err = ref_resolve(id, repo, resolved, --recursion);
580 if (resolved)
581 got_ref_close(resolved);
582 return err;
585 *id = calloc(1, sizeof(**id));
586 if (*id == NULL)
587 return got_error_from_errno("calloc");
588 memcpy(*id, &ref->ref.ref.id, sizeof(**id));
589 return NULL;
592 const struct got_error *
593 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
594 struct got_reference *ref)
596 return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
599 char *
600 got_ref_to_str(struct got_reference *ref)
602 char *str;
604 if (ref->flags & GOT_REF_IS_SYMBOLIC)
605 return strdup(ref->ref.symref.ref);
607 if (got_object_id_str(&str, &ref->ref.ref.id) != NULL)
608 return NULL;
610 return str;
613 const char *
614 got_ref_get_name(struct got_reference *ref)
616 if (ref->flags & GOT_REF_IS_SYMBOLIC)
617 return ref->ref.symref.name;
619 return ref->ref.ref.name;
622 const char *
623 got_ref_get_symref_target(struct got_reference *ref)
625 if (ref->flags & GOT_REF_IS_SYMBOLIC)
626 return ref->ref.symref.ref;
628 return NULL;
631 time_t
632 got_ref_get_mtime(struct got_reference *ref)
634 return ref->mtime;
637 const struct got_error *
638 got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
639 struct got_reference* re2)
641 const char *name1 = got_ref_get_name(re1);
642 const char *name2 = got_ref_get_name(re2);
644 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
645 return NULL;
648 const struct got_error *
649 got_ref_cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
650 struct got_reference *ref2)
652 const struct got_error *err = NULL;
653 struct got_repository *repo = arg;
654 struct got_object_id *id1, *id2 = NULL;
655 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
656 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
657 time_t time1, time2;
659 *cmp = 0;
661 err = got_ref_resolve(&id1, repo, ref1);
662 if (err)
663 return err;
664 err = got_object_open_as_tag(&tag1, repo, id1);
665 if (err) {
666 if (err->code != GOT_ERR_OBJ_TYPE)
667 goto done;
668 /* "lightweight" tag */
669 err = got_object_open_as_commit(&commit1, repo, id1);
670 if (err)
671 goto done;
672 time1 = got_object_commit_get_committer_time(commit1);
673 } else
674 time1 = got_object_tag_get_tagger_time(tag1);
676 err = got_ref_resolve(&id2, repo, ref2);
677 if (err)
678 goto done;
679 err = got_object_open_as_tag(&tag2, repo, id2);
680 if (err) {
681 if (err->code != GOT_ERR_OBJ_TYPE)
682 goto done;
683 /* "lightweight" tag */
684 err = got_object_open_as_commit(&commit2, repo, id2);
685 if (err)
686 goto done;
687 time2 = got_object_commit_get_committer_time(commit2);
688 } else
689 time2 = got_object_tag_get_tagger_time(tag2);
691 /* Put latest tags first. */
692 if (time1 < time2)
693 *cmp = 1;
694 else if (time1 > time2)
695 *cmp = -1;
696 else
697 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
698 done:
699 free(id1);
700 free(id2);
701 if (tag1)
702 got_object_tag_close(tag1);
703 if (tag2)
704 got_object_tag_close(tag2);
705 if (commit1)
706 got_object_commit_close(commit1);
707 if (commit2)
708 got_object_commit_close(commit2);
709 return err;
712 static const struct got_error *
713 get_committer_time(struct got_reference *ref, struct got_repository *repo)
715 const struct got_error *err = NULL;
716 int obj_type;
717 struct got_commit_object *commit = NULL;
718 struct got_tag_object *tag = NULL;
719 struct got_object_id *id = NULL;
721 err = got_ref_resolve(&id, repo, ref);
722 if (err)
723 return err;
725 err = got_object_get_type(&obj_type, repo, id);
726 if (err)
727 goto done;
729 switch (obj_type) {
730 case GOT_OBJ_TYPE_COMMIT:
731 err = got_object_open_as_commit(&commit, repo, id);
732 if (err)
733 goto done;
734 ref->committer_time =
735 got_object_commit_get_committer_time(commit);
736 break;
737 case GOT_OBJ_TYPE_TAG:
738 err = got_object_open_as_tag(&tag, repo, id);
739 if (err)
740 goto done;
741 ref->committer_time = got_object_tag_get_tagger_time(tag);
742 break;
743 default:
744 /* best effort for other object types */
745 ref->committer_time = got_ref_get_mtime(ref);
746 break;
748 done:
749 free(id);
750 if (commit)
751 got_object_commit_close(commit);
752 if (tag)
753 got_object_tag_close(tag);
754 return err;
757 const struct got_error *
758 got_ref_cmp_by_commit_timestamp_descending(void *arg, int *cmp,
759 struct got_reference *ref1, struct got_reference *ref2)
761 const struct got_error *err = NULL;
762 struct got_repository *repo = arg;
764 *cmp = 0;
766 if (ref1->committer_time == 0) {
767 err = get_committer_time(ref1, repo);
768 if (err)
769 return err;
771 if (ref2->committer_time == 0) {
772 err = get_committer_time(ref2, repo);
773 if (err)
774 return err;
777 if (ref1->committer_time < ref2->committer_time)
778 *cmp = 1;
779 else if (ref2->committer_time < ref1->committer_time)
780 *cmp = -1;
781 else
782 return got_ref_cmp_by_name(arg, cmp, ref1, ref2);
784 return err;
787 const struct got_error *
788 got_reflist_insert(struct got_reflist_entry **newp,
789 struct got_reflist_head *refs, struct got_reference *ref,
790 got_ref_cmp_cb cmp_cb, void *cmp_arg)
792 const struct got_error *err;
793 struct got_reflist_entry *new, *re;
794 int cmp;
796 *newp = NULL;
798 if (cmp_cb != got_ref_cmp_by_name && (ref->flags & GOT_REF_IS_PACKED)) {
799 /*
800 * If we are not sorting elements by name then we must still
801 * detect collisions between a packed ref and an on-disk ref
802 * using the same name. On-disk refs take precedence and are
803 * already present on the list before packed refs get added.
804 */
805 TAILQ_FOREACH(re, refs, entry) {
806 err = got_ref_cmp_by_name(NULL, &cmp, re->ref, ref);
807 if (err)
808 return err;
809 if (cmp == 0)
810 return NULL;
814 new = malloc(sizeof(*new));
815 if (new == NULL)
816 return got_error_from_errno("malloc");
817 new->ref = ref;
818 *newp = new;
820 /*
821 * We must de-duplicate entries on insert because packed-refs may
822 * contain redundant entries. On-disk refs take precedence.
823 * This code assumes that on-disk revs are read before packed-refs.
824 * We're iterating the list anyway, so insert elements sorted by name.
826 * Many callers will provide paths in a somewhat sorted order.
827 * Iterating backwards from the tail of the list should be more
828 * efficient than traversing through the entire list each time
829 * an element is inserted.
830 */
831 re = TAILQ_LAST(refs, got_reflist_head);
832 while (re) {
833 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
834 if (err)
835 return err;
836 if (cmp == 0) {
837 /* duplicate */
838 free(new);
839 *newp = NULL;
840 return NULL;
841 } else if (cmp < 0) {
842 TAILQ_INSERT_AFTER(refs, re, new, entry);
843 return NULL;
845 re = TAILQ_PREV(re, got_reflist_head, entry);
848 TAILQ_INSERT_HEAD(refs, new, entry);
849 return NULL;
852 const struct got_error *
853 got_reflist_sort(struct got_reflist_head *refs,
854 got_ref_cmp_cb cmp_cb, void *cmp_arg)
856 const struct got_error *err = NULL;
857 struct got_reflist_entry *re, *tmp, *new;
858 struct got_reflist_head sorted;
860 TAILQ_INIT(&sorted);
862 TAILQ_FOREACH_SAFE(re, refs, entry, tmp) {
863 struct got_reference *ref = re->ref;
864 TAILQ_REMOVE(refs, re, entry);
865 free(re);
866 err = got_reflist_insert(&new, &sorted, ref, cmp_cb, cmp_arg);
867 if (err || new == NULL /* duplicate */)
868 got_ref_close(ref);
869 if (err)
870 return err;
873 TAILQ_CONCAT(refs, &sorted, entry);
874 return NULL;
877 static const struct got_error *
878 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
879 const char *subdir, struct got_repository *repo,
880 got_ref_cmp_cb cmp_cb, void *cmp_arg)
882 const struct got_error *err = NULL;
883 DIR *d = NULL;
884 char *path_subdir;
886 while (subdir[0] == '/')
887 subdir++;
889 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
890 return got_error_from_errno("asprintf");
892 d = opendir(path_subdir);
893 if (d == NULL)
894 goto done;
896 for (;;) {
897 struct dirent *dent;
898 struct got_reference *ref;
899 char *child;
900 int type;
902 dent = readdir(d);
903 if (dent == NULL)
904 break;
906 if (strcmp(dent->d_name, ".") == 0 ||
907 strcmp(dent->d_name, "..") == 0)
908 continue;
910 err = got_path_dirent_type(&type, path_subdir, dent);
911 if (err)
912 break;
914 switch (type) {
915 case DT_REG:
916 err = open_ref(&ref, path_refs, subdir, dent->d_name,
917 0);
918 if (err)
919 goto done;
920 if (ref) {
921 struct got_reflist_entry *new;
922 err = got_reflist_insert(&new, refs, ref,
923 cmp_cb, cmp_arg);
924 if (err || new == NULL /* duplicate */)
925 got_ref_close(ref);
926 if (err)
927 goto done;
929 break;
930 case DT_DIR:
931 if (asprintf(&child, "%s%s%s", subdir,
932 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
933 err = got_error_from_errno("asprintf");
934 break;
936 err = gather_on_disk_refs(refs, path_refs, child, repo,
937 cmp_cb, cmp_arg);
938 free(child);
939 break;
940 default:
941 break;
944 done:
945 if (d)
946 closedir(d);
947 free(path_subdir);
948 return err;
951 const struct got_error *
952 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
953 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
955 const struct got_error *err;
956 char *packed_refs_path = NULL, *path_refs = NULL;
957 char *abs_namespace = NULL, *buf = NULL;
958 const char *ondisk_ref_namespace = NULL;
959 char *line = NULL;
960 FILE *f = NULL;
961 struct got_reference *ref;
962 struct got_reflist_entry *new;
964 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
965 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
966 if (path_refs == NULL) {
967 err = got_error_from_errno("get_refs_dir_path");
968 goto done;
970 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
971 if (err)
972 goto done;
973 err = got_reflist_insert(&new, refs, ref, cmp_cb, cmp_arg);
974 if (err || new == NULL /* duplicate */)
975 got_ref_close(ref);
976 if (err && err->code != GOT_ERR_NOT_REF)
977 goto done;
978 } else {
979 /* Try listing a single reference. */
980 const char *refname = ref_namespace;
981 path_refs = get_refs_dir_path(repo, refname);
982 if (path_refs == NULL) {
983 err = got_error_from_errno("get_refs_dir_path");
984 goto done;
986 err = open_ref(&ref, path_refs, "", refname, 0);
987 if (err) {
988 if (err->code != GOT_ERR_NOT_REF)
989 goto done;
990 /* Try to look up references in a given namespace. */
991 } else {
992 err = got_reflist_insert(&new, refs, ref,
993 cmp_cb, cmp_arg);
994 if (err || new == NULL /* duplicate */)
995 got_ref_close(ref);
996 return err;
1000 if (ref_namespace) {
1001 size_t len;
1002 /* Canonicalize the path to eliminate double-slashes if any. */
1003 if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
1004 err = got_error_from_errno("asprintf");
1005 goto done;
1007 len = strlen(abs_namespace) + 1;
1008 buf = malloc(len);
1009 if (buf == NULL) {
1010 err = got_error_from_errno("malloc");
1011 goto done;
1013 err = got_canonpath(abs_namespace, buf, len);
1014 if (err)
1015 goto done;
1016 ondisk_ref_namespace = buf;
1017 while (ondisk_ref_namespace[0] == '/')
1018 ondisk_ref_namespace++;
1019 if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
1020 ondisk_ref_namespace += 5;
1021 else if (strcmp(ondisk_ref_namespace, "refs") == 0)
1022 ondisk_ref_namespace = "";
1025 /* Gather on-disk refs before parsing packed-refs. */
1026 free(path_refs);
1027 path_refs = get_refs_dir_path(repo, "");
1028 if (path_refs == NULL) {
1029 err = got_error_from_errno("get_refs_dir_path");
1030 goto done;
1032 err = gather_on_disk_refs(refs, path_refs,
1033 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
1034 cmp_cb, cmp_arg);
1035 if (err)
1036 goto done;
1039 * The packed-refs file may contain redundant entries, in which
1040 * case on-disk refs take precedence.
1042 packed_refs_path = got_repo_get_path_packed_refs(repo);
1043 if (packed_refs_path == NULL) {
1044 err = got_error_from_errno("got_repo_get_path_packed_refs");
1045 goto done;
1048 f = fopen(packed_refs_path, "re");
1049 if (f) {
1050 size_t linesize = 0;
1051 ssize_t linelen;
1052 struct stat sb;
1054 if (fstat(fileno(f), &sb) == -1) {
1055 err = got_error_from_errno2("fstat", packed_refs_path);
1056 goto done;
1058 for (;;) {
1059 linelen = getline(&line, &linesize, f);
1060 if (linelen == -1) {
1061 if (feof(f))
1062 break;
1063 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1064 goto done;
1066 if (linelen > 0 && line[linelen - 1] == '\n')
1067 line[linelen - 1] = '\0';
1068 err = parse_packed_ref_line(&ref, NULL, line,
1069 sb.st_mtime);
1070 if (err)
1071 goto done;
1072 if (ref) {
1073 if (ref_namespace) {
1074 const char *name;
1075 name = got_ref_get_name(ref);
1076 if (!got_path_is_child(name,
1077 ref_namespace,
1078 strlen(ref_namespace))) {
1079 got_ref_close(ref);
1080 continue;
1083 err = got_reflist_insert(&new, refs, ref,
1084 cmp_cb, cmp_arg);
1085 if (err || new == NULL /* duplicate */)
1086 got_ref_close(ref);
1087 if (err)
1088 goto done;
1092 done:
1093 free(packed_refs_path);
1094 free(abs_namespace);
1095 free(buf);
1096 free(line);
1097 free(path_refs);
1098 if (f && fclose(f) == EOF && err == NULL)
1099 err = got_error_from_errno("fclose");
1100 return err;
1103 void
1104 got_ref_list_free(struct got_reflist_head *refs)
1106 struct got_reflist_entry *re;
1108 while ((re = TAILQ_FIRST(refs))) {
1109 TAILQ_REMOVE(refs, re, entry);
1110 got_ref_close(re->ref);
1111 free(re);
1115 int
1116 got_ref_is_symbolic(struct got_reference *ref)
1118 return (ref->flags & GOT_REF_IS_SYMBOLIC);
1121 const struct got_error *
1122 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1124 if (ref->flags & GOT_REF_IS_SYMBOLIC)
1125 return got_error(GOT_ERR_BAD_REF_TYPE);
1127 memcpy(&ref->ref.ref.id, id, sizeof(ref->ref.ref.id));
1128 return NULL;
1131 const struct got_error *
1132 got_ref_change_symref(struct got_reference *ref, const char *refname)
1134 char *new_name;
1136 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1137 return got_error(GOT_ERR_BAD_REF_TYPE);
1139 new_name = strdup(refname);
1140 if (new_name == NULL)
1141 return got_error_from_errno("strdup");
1143 free(ref->ref.symref.ref);
1144 ref->ref.symref.ref = new_name;
1145 return NULL;
1148 const struct got_error *
1149 got_ref_change_symref_to_ref(struct got_reference *symref,
1150 struct got_object_id *id)
1152 if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1153 return got_error(GOT_ERR_BAD_REF_TYPE);
1155 symref->ref.ref.name = symref->ref.symref.name;
1156 memcpy(&symref->ref.ref.id, id, sizeof(symref->ref.ref.id));
1157 symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1158 return NULL;
1161 const struct got_error *
1162 got_ref_write(struct got_reference *ref, struct got_repository *repo)
1164 const struct got_error *err = NULL, *unlock_err = NULL;
1165 const char *name = got_ref_get_name(ref);
1166 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1167 struct got_lockfile *lf = NULL;
1168 FILE *f = NULL;
1169 size_t n;
1170 struct stat sb;
1172 path_refs = get_refs_dir_path(repo, name);
1173 if (path_refs == NULL) {
1174 err = got_error_from_errno2("get_refs_dir_path", name);
1175 goto done;
1178 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1179 err = got_error_from_errno("asprintf");
1180 goto done;
1183 err = got_opentemp_named(&tmppath, &f, path, "");
1184 if (err) {
1185 char *parent;
1186 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1187 goto done;
1188 err = got_path_dirname(&parent, path);
1189 if (err)
1190 goto done;
1191 err = got_path_mkdir(parent);
1192 free(parent);
1193 if (err)
1194 goto done;
1195 err = got_opentemp_named(&tmppath, &f, path, "");
1196 if (err)
1197 goto done;
1200 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1201 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1202 if (n != strlen(ref->ref.symref.ref) + 6) {
1203 err = got_ferror(f, GOT_ERR_IO);
1204 goto done;
1206 } else {
1207 char *hex;
1208 size_t len;
1210 err = got_object_id_str(&hex, &ref->ref.ref.id);
1211 if (err)
1212 goto done;
1213 len = strlen(hex);
1214 n = fprintf(f, "%s\n", hex);
1215 free(hex);
1216 if (n != len + 1) {
1217 err = got_ferror(f, GOT_ERR_IO);
1218 goto done;
1222 if (ref->lf == NULL) {
1223 err = got_lockfile_lock(&lf, path, -1);
1224 if (err)
1225 goto done;
1228 /* XXX: check if old content matches our expectations? */
1230 if (stat(path, &sb) != 0) {
1231 if (errno != ENOENT) {
1232 err = got_error_from_errno2("stat", path);
1233 goto done;
1235 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1238 if (fchmod(fileno(f), sb.st_mode) != 0) {
1239 err = got_error_from_errno2("fchmod", tmppath);
1240 goto done;
1243 if (rename(tmppath, path) != 0) {
1244 err = got_error_from_errno3("rename", tmppath, path);
1245 goto done;
1247 free(tmppath);
1248 tmppath = NULL;
1250 if (stat(path, &sb) == -1) {
1251 err = got_error_from_errno2("stat", path);
1252 goto done;
1254 ref->mtime = sb.st_mtime;
1255 done:
1256 if (ref->lf == NULL && lf)
1257 unlock_err = got_lockfile_unlock(lf, -1);
1258 if (f) {
1259 if (fclose(f) == EOF && err == NULL)
1260 err = got_error_from_errno("fclose");
1262 free(path_refs);
1263 free(path);
1264 if (tmppath) {
1265 if (unlink(tmppath) == -1 && err == NULL)
1266 err = got_error_from_errno2("unlink", tmppath);
1267 free(tmppath);
1269 return err ? err : unlock_err;
1272 static const struct got_error *
1273 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1275 const struct got_error *err = NULL, *unlock_err = NULL;
1276 struct got_lockfile *lf = NULL;
1277 FILE *f = NULL, *tmpf = NULL;
1278 char *line = NULL, *packed_refs_path, *tmppath = NULL;
1279 size_t linesize = 0;
1280 struct got_reflist_head refs;
1281 int found_delref = 0;
1283 /* The packed-refs file does not cotain symbolic references. */
1284 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1285 return got_error(GOT_ERR_BAD_REF_DATA);
1287 TAILQ_INIT(&refs);
1289 packed_refs_path = got_repo_get_path_packed_refs(repo);
1290 if (packed_refs_path == NULL)
1291 return got_error_from_errno("got_repo_get_path_packed_refs");
1293 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path, "");
1294 if (err)
1295 goto done;
1297 if (delref->lf == NULL) {
1298 err = got_lockfile_lock(&lf, packed_refs_path, -1);
1299 if (err)
1300 goto done;
1303 f = fopen(packed_refs_path, "re");
1304 if (f == NULL) {
1305 err = got_error_from_errno2("fopen", packed_refs_path);
1306 goto done;
1308 for (;;) {
1309 ssize_t linelen;
1310 struct got_reference *ref;
1311 struct got_reflist_entry *new;
1313 linelen = getline(&line, &linesize, f);
1314 if (linelen == -1) {
1315 if (feof(f))
1316 break;
1317 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1318 goto done;
1320 if (linelen > 0 && line[linelen - 1] == '\n')
1321 line[linelen - 1] = '\0';
1322 err = parse_packed_ref_line(&ref, NULL, line, 0);
1323 if (err)
1324 goto done;
1325 if (ref == NULL)
1326 continue;
1328 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1329 got_object_id_cmp(&ref->ref.ref.id,
1330 &delref->ref.ref.id) == 0) {
1331 found_delref = 1;
1332 got_ref_close(ref);
1333 continue;
1336 err = got_reflist_insert(&new, &refs, ref,
1337 got_ref_cmp_by_name, NULL);
1338 if (err || new == NULL /* duplicate */)
1339 got_ref_close(ref);
1340 if (err)
1341 goto done;
1344 if (found_delref) {
1345 struct got_reflist_entry *re;
1346 size_t n;
1347 struct stat sb;
1349 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1350 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1351 err = got_ferror(f, GOT_ERR_IO);
1352 goto done;
1355 TAILQ_FOREACH(re, &refs, entry) {
1356 char *hex;
1357 size_t len;
1359 err = got_object_id_str(&hex, &re->ref->ref.ref.id);
1360 if (err)
1361 goto done;
1362 len = strlen(hex);
1363 n = fprintf(tmpf, "%s ", hex);
1364 free(hex);
1365 if (n != len + 1) {
1366 err = got_ferror(f, GOT_ERR_IO);
1367 goto done;
1370 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1371 if (n != strlen(re->ref->ref.ref.name) + 1) {
1372 err = got_ferror(f, GOT_ERR_IO);
1373 goto done;
1377 if (fflush(tmpf) != 0) {
1378 err = got_error_from_errno("fflush");
1379 goto done;
1382 if (fstat(fileno(f), &sb) != 0) {
1383 if (errno != ENOENT) {
1384 err = got_error_from_errno2("fstat",
1385 packed_refs_path);
1386 goto done;
1388 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1391 if (fchmod(fileno(tmpf), sb.st_mode) != 0) {
1392 err = got_error_from_errno2("fchmod", tmppath);
1393 goto done;
1396 if (rename(tmppath, packed_refs_path) != 0) {
1397 err = got_error_from_errno3("rename", tmppath,
1398 packed_refs_path);
1399 goto done;
1401 free(tmppath);
1402 tmppath = NULL;
1404 done:
1405 if (delref->lf == NULL && lf)
1406 unlock_err = got_lockfile_unlock(lf, -1);
1407 if (f) {
1408 if (fclose(f) == EOF && err == NULL)
1409 err = got_error_from_errno("fclose");
1411 if (tmppath && unlink(tmppath) == -1 && err == NULL)
1412 err = got_error_from_errno2("unlink", tmppath);
1413 if (tmpf && fclose(tmpf) == EOF && err == NULL)
1414 err = got_error_from_errno("fclose");
1415 free(tmppath);
1416 free(packed_refs_path);
1417 free(line);
1418 got_ref_list_free(&refs);
1419 return err ? err : unlock_err;
1422 static const struct got_error *
1423 delete_loose_ref(struct got_reference *ref, struct got_repository *repo)
1425 const struct got_error *err = NULL, *unlock_err = NULL;
1426 const char *name = got_ref_get_name(ref);
1427 char *path_refs = NULL, *path = NULL;
1428 struct got_lockfile *lf = NULL;
1430 path_refs = get_refs_dir_path(repo, name);
1431 if (path_refs == NULL) {
1432 err = got_error_from_errno2("get_refs_dir_path", name);
1433 goto done;
1436 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1437 err = got_error_from_errno("asprintf");
1438 goto done;
1441 if (ref->lf == NULL) {
1442 err = got_lockfile_lock(&lf, path, -1);
1443 if (err)
1444 goto done;
1447 /* XXX: check if old content matches our expectations? */
1449 if (unlink(path) == -1)
1450 err = got_error_from_errno2("unlink", path);
1451 done:
1452 if (ref->lf == NULL && lf)
1453 unlock_err = got_lockfile_unlock(lf, -1);
1455 free(path_refs);
1456 free(path);
1457 return err ? err : unlock_err;
1460 const struct got_error *
1461 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1463 const struct got_error *err = NULL;
1464 struct got_reference *ref2;
1466 if (ref->flags & GOT_REF_IS_PACKED) {
1467 err = delete_packed_ref(ref, repo);
1468 if (err)
1469 return err;
1471 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1472 if (err) {
1473 if (err->code == GOT_ERR_NOT_REF)
1474 return NULL;
1475 return err;
1478 err = delete_loose_ref(ref2, repo);
1479 got_ref_close(ref2);
1480 return err;
1481 } else {
1482 err = delete_loose_ref(ref, repo);
1483 if (err)
1484 return err;
1486 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1487 if (err) {
1488 if (err->code == GOT_ERR_NOT_REF)
1489 return NULL;
1490 return err;
1493 err = delete_packed_ref(ref2, repo);
1494 got_ref_close(ref2);
1495 return err;
1499 const struct got_error *
1500 got_ref_unlock(struct got_reference *ref)
1502 const struct got_error *err;
1503 err = got_lockfile_unlock(ref->lf, -1);
1504 ref->lf = NULL;
1505 return err;
1508 struct got_reflist_object_id_map {
1509 struct got_object_idset *idset;
1512 struct got_reflist_object_id_map_entry {
1513 struct got_reflist_head refs;
1516 static const struct got_error *
1517 add_object_id_map_entry(struct got_object_idset *idset,
1518 struct got_object_id *id, struct got_reflist_entry *re)
1520 const struct got_error *err = NULL;
1521 struct got_reflist_object_id_map_entry *ent;
1522 struct got_reflist_entry *new;
1524 ent = got_object_idset_get(idset, id);
1525 if (ent == NULL) {
1526 ent = malloc(sizeof(*ent));
1527 if (ent == NULL)
1528 return got_error_from_errno("malloc");
1530 TAILQ_INIT(&ent->refs);
1531 err = got_object_idset_add(idset, id, ent);
1532 if (err) {
1533 free(ent);
1534 return err;
1538 err = got_reflist_entry_dup(&new, re);
1539 if (err)
1540 return err;
1542 TAILQ_INSERT_TAIL(&ent->refs, new, entry);
1543 return NULL;
1546 const struct got_error *
1547 got_reflist_object_id_map_create(struct got_reflist_object_id_map **map,
1548 struct got_reflist_head *refs, struct got_repository *repo)
1550 const struct got_error *err = NULL;
1551 struct got_object_idset *idset;
1552 struct got_object_id *id = NULL;
1553 struct got_reflist_entry *re;
1555 idset = got_object_idset_alloc();
1556 if (idset == NULL)
1557 return got_error_from_errno("got_object_idset_alloc");
1559 *map = malloc(sizeof(**map));
1560 if (*map == NULL) {
1561 got_object_idset_free(idset);
1562 return got_error_from_errno("malloc");
1564 (*map)->idset = idset;
1566 TAILQ_FOREACH(re, refs, entry) {
1567 struct got_tag_object *tag = NULL;
1569 err = got_ref_resolve(&id, repo, re->ref);
1570 if (err)
1571 goto done;
1573 err = add_object_id_map_entry(idset, id, re);
1574 if (err)
1575 goto done;
1577 if (strstr(got_ref_get_name(re->ref), "/tags/") == NULL) {
1578 free(id);
1579 id = NULL;
1580 continue;
1583 err = got_object_open_as_tag(&tag, repo, id);
1584 if (err) {
1585 if (err->code != GOT_ERR_OBJ_TYPE)
1586 goto done;
1587 /* Ref points at something other than a tag. */
1588 err = NULL;
1589 tag = NULL;
1590 free(id);
1591 id = NULL;
1592 continue;
1595 err = add_object_id_map_entry(idset,
1596 got_object_tag_get_object_id(tag), re);
1597 got_object_tag_close(tag);
1598 if (err)
1599 goto done;
1601 free(id);
1602 id = NULL;
1604 done:
1605 free(id);
1606 if (err) {
1607 got_reflist_object_id_map_free(*map);
1608 *map = NULL;
1610 return err;
1613 struct got_reflist_head *
1614 got_reflist_object_id_map_lookup(struct got_reflist_object_id_map *map,
1615 struct got_object_id *id)
1617 struct got_reflist_object_id_map_entry *ent;
1618 ent = got_object_idset_get(map->idset, id);
1619 if (ent)
1620 return &ent->refs;
1621 return NULL;
1624 static const struct got_error *
1625 free_id_map_entry(struct got_object_id *id, void *data, void *arg)
1627 struct got_reflist_object_id_map_entry *ent = data;
1629 got_ref_list_free(&ent->refs);
1630 free(ent);
1631 return NULL;
1634 void
1635 got_reflist_object_id_map_free(struct got_reflist_object_id_map *map)
1637 got_object_idset_for_each(map->idset, free_id_map_entry, NULL);
1638 got_object_idset_free(map->idset);
1639 free(map);