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 <ctype.h>
23 #include <dirent.h>
24 #include <limits.h>
25 #include <sha1.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_sha1.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 u_int8_t sha1[SHA1_DIGEST_LENGTH];
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;
91 };
93 static const struct got_error *
94 alloc_ref(struct got_reference **ref, const char *name,
95 struct got_object_id *id, int flags, time_t mtime)
96 {
97 const struct got_error *err = NULL;
99 *ref = calloc(1, sizeof(**ref));
100 if (*ref == NULL)
101 return got_error_from_errno("calloc");
103 memcpy((*ref)->ref.ref.sha1, id->sha1, sizeof((*ref)->ref.ref.sha1));
104 (*ref)->flags = flags;
105 (*ref)->ref.ref.name = strdup(name);
106 (*ref)->mtime = mtime;
107 if ((*ref)->ref.ref.name == NULL) {
108 err = got_error_from_errno("strdup");
109 got_ref_close(*ref);
110 *ref = NULL;
112 return err;
115 static const struct got_error *
116 alloc_symref(struct got_reference **ref, const char *name,
117 const char *target_ref, int flags)
119 const struct got_error *err = NULL;
121 *ref = calloc(1, sizeof(**ref));
122 if (*ref == NULL)
123 return got_error_from_errno("calloc");
125 (*ref)->flags = GOT_REF_IS_SYMBOLIC | flags;
126 (*ref)->ref.symref.name = strdup(name);
127 if ((*ref)->ref.symref.name == NULL) {
128 err = got_error_from_errno("strdup");
129 got_ref_close(*ref);
130 *ref = NULL;
131 return err;
133 (*ref)->ref.symref.ref = strdup(target_ref);
134 if ((*ref)->ref.symref.ref == NULL) {
135 err = got_error_from_errno("strdup");
136 got_ref_close(*ref);
137 *ref = NULL;
139 return err;
142 static const struct got_error *
143 parse_symref(struct got_reference **ref, const char *name, const char *line)
145 if (line[0] == '\0')
146 return got_error(GOT_ERR_BAD_REF_DATA);
148 return alloc_symref(ref, name, line, 0);
151 static const struct got_error *
152 parse_ref_line(struct got_reference **ref, const char *name, const char *line,
153 time_t mtime)
155 struct got_object_id id;
157 if (strncmp(line, "ref: ", 5) == 0) {
158 line += 5;
159 return parse_symref(ref, name, line);
162 if (!got_parse_sha1_digest(id.sha1, line))
163 return got_error(GOT_ERR_BAD_REF_DATA);
165 return alloc_ref(ref, name, &id, 0, mtime);
168 static const struct got_error *
169 parse_ref_file(struct got_reference **ref, const char *name,
170 const char *absname, const char *abspath, int lock)
172 const struct got_error *err = NULL;
173 FILE *f;
174 char *line = NULL;
175 size_t linesize = 0;
176 ssize_t linelen;
177 struct got_lockfile *lf = NULL;
178 struct stat sb;
180 if (lock) {
181 err = got_lockfile_lock(&lf, abspath, -1);
182 if (err) {
183 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
184 err = got_error_not_ref(name);
185 return err;
189 f = fopen(abspath, "rb");
190 if (f == NULL) {
191 if (errno != ENOTDIR && errno != ENOENT)
192 err = got_error_from_errno2("fopen", abspath);
193 else
194 err = got_error_not_ref(name);
195 if (lock)
196 got_lockfile_unlock(lf, -1);
197 return err;
199 if (fstat(fileno(f), &sb) == -1) {
200 err = got_error_from_errno2("fstat", abspath);
201 goto done;
204 linelen = getline(&line, &linesize, f);
205 if (linelen == -1) {
206 if (feof(f))
207 err = NULL; /* ignore empty files (could be locks) */
208 else {
209 if (errno == EISDIR)
210 err = got_error(GOT_ERR_NOT_REF);
211 else if (ferror(f))
212 err = got_ferror(f, GOT_ERR_IO);
213 else
214 err = got_error_from_errno2("getline", abspath);
216 if (lock)
217 got_lockfile_unlock(lf, -1);
218 goto done;
220 while (linelen > 0 && line[linelen - 1] == '\n') {
221 line[linelen - 1] = '\0';
222 linelen--;
225 err = parse_ref_line(ref, absname, line, sb.st_mtime);
226 if (lock) {
227 if (err)
228 got_lockfile_unlock(lf, -1);
229 else {
230 if (*ref)
231 (*ref)->lf = lf;
232 else
233 got_lockfile_unlock(lf, -1);
236 done:
237 free(line);
238 if (fclose(f) == EOF && err == NULL) {
239 err = got_error_from_errno("fclose");
240 if (*ref) {
241 if (lock)
242 got_ref_unlock(*ref);
243 got_ref_close(*ref);
244 *ref = NULL;
247 return err;
250 static int
251 is_well_known_ref(const char *refname)
253 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
254 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
255 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
256 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
259 static char *
260 get_refs_dir_path(struct got_repository *repo, const char *refname)
262 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
263 return strdup(got_repo_get_path_git_dir(repo));
265 return got_repo_get_path_refs(repo);
268 static int
269 is_valid_ref_name(const char *name)
271 const char *s, *seg;
272 const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
273 const char *forbidden_seq[] = { "//", "..", "@{" };
274 const char *lfs = GOT_LOCKFILE_SUFFIX;
275 const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
276 size_t i;
278 if (name[0] == '@' && name[1] == '\0')
279 return 0;
281 s = name;
282 seg = s;
283 if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
284 return 0;
285 while (*s) {
286 for (i = 0; i < nitems(forbidden); i++) {
287 if (*s == forbidden[i])
288 return 0;
290 for (i = 0; i < nitems(forbidden_seq); i++) {
291 if (s[0] == forbidden_seq[i][0] &&
292 s[1] == forbidden_seq[i][1])
293 return 0;
295 if (iscntrl((unsigned char)s[0]))
296 return 0;
297 if (s[0] == '.' && s[1] == '\0')
298 return 0;
299 if (*s == '/') {
300 const char *nextseg = s + 1;
301 if (nextseg[0] == '\0' || nextseg[0] == '.' ||
302 nextseg[0] == '/')
303 return 0;
304 if (seg <= s - lfs_len &&
305 strncmp(s - lfs_len, lfs, lfs_len) == 0)
306 return 0;
307 seg = nextseg;
309 s++;
312 if (seg <= s - lfs_len &&
313 strncmp(s - lfs_len, lfs, lfs_len) == 0)
314 return 0;
316 return 1;
319 const struct got_error *
320 got_ref_alloc(struct got_reference **ref, const char *name,
321 struct got_object_id *id)
323 if (!is_valid_ref_name(name))
324 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
326 return alloc_ref(ref, name, id, 0, 0);
329 const struct got_error *
330 got_ref_alloc_symref(struct got_reference **ref, const char *name,
331 struct got_reference *target_ref)
333 if (!is_valid_ref_name(name))
334 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
336 return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
339 static const struct got_error *
340 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
341 const char *line, time_t mtime)
343 struct got_object_id id;
344 const char *name;
346 *ref = NULL;
348 if (line[0] == '#' || line[0] == '^')
349 return NULL;
351 if (!got_parse_sha1_digest(id.sha1, line))
352 return got_error(GOT_ERR_BAD_REF_DATA);
354 if (abs_refname) {
355 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
356 return NULL;
357 name = abs_refname;
358 } else
359 name = line + SHA1_DIGEST_STRING_LENGTH;
361 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED, mtime);
364 static const struct got_error *
365 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
366 int nsubdirs, const char *refname, time_t mtime)
368 const struct got_error *err = NULL;
369 char *abs_refname;
370 char *line = NULL;
371 size_t linesize = 0;
372 ssize_t linelen;
373 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
375 *ref = NULL;
377 if (ref_is_absolute)
378 abs_refname = (char *)refname;
379 do {
380 linelen = getline(&line, &linesize, f);
381 if (linelen == -1) {
382 if (feof(f))
383 break;
384 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
385 break;
387 if (linelen > 0 && line[linelen - 1] == '\n')
388 line[linelen - 1] = '\0';
389 for (i = 0; i < nsubdirs; i++) {
390 if (!ref_is_absolute &&
391 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
392 refname) == -1)
393 return got_error_from_errno("asprintf");
394 err = parse_packed_ref_line(ref, abs_refname, line,
395 mtime);
396 if (!ref_is_absolute)
397 free(abs_refname);
398 if (err || *ref != NULL)
399 break;
401 if (err)
402 break;
403 } while (*ref == NULL);
404 free(line);
406 return err;
409 static const struct got_error *
410 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
411 const char *name, int lock)
413 const struct got_error *err = NULL;
414 char *path = NULL;
415 char *absname = NULL;
416 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
417 int ref_is_well_known = (subdir[0] == '\0' && is_well_known_ref(name));
419 *ref = NULL;
421 if (!is_valid_ref_name(name))
422 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
424 if (ref_is_absolute || ref_is_well_known) {
425 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
426 return got_error_from_errno("asprintf");
427 absname = (char *)name;
428 } else {
429 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
430 subdir[0] ? "/" : "", name) == -1)
431 return got_error_from_errno("asprintf");
433 if (asprintf(&absname, "refs/%s%s%s",
434 subdir, subdir[0] ? "/" : "", name) == -1) {
435 err = got_error_from_errno("asprintf");
436 goto done;
440 err = parse_ref_file(ref, name, absname, path, lock);
441 done:
442 if (!ref_is_absolute && !ref_is_well_known)
443 free(absname);
444 free(path);
445 return err;
448 const struct got_error *
449 got_ref_open(struct got_reference **ref, struct got_repository *repo,
450 const char *refname, int lock)
452 const struct got_error *err = NULL;
453 char *path_refs = NULL;
454 const char *subdirs[] = {
455 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
456 };
457 size_t i;
458 int well_known = is_well_known_ref(refname);
459 struct got_lockfile *lf = NULL;
461 *ref = NULL;
463 path_refs = get_refs_dir_path(repo, refname);
464 if (path_refs == NULL) {
465 err = got_error_from_errno2("get_refs_dir_path", refname);
466 goto done;
469 if (well_known) {
470 err = open_ref(ref, path_refs, "", refname, lock);
471 } else {
472 char *packed_refs_path;
473 FILE *f;
475 /* Search on-disk refs before packed refs! */
476 for (i = 0; i < nitems(subdirs); i++) {
477 err = open_ref(ref, path_refs, subdirs[i], refname,
478 lock);
479 if ((err && err->code != GOT_ERR_NOT_REF) || *ref)
480 goto done;
483 packed_refs_path = got_repo_get_path_packed_refs(repo);
484 if (packed_refs_path == NULL) {
485 err = got_error_from_errno(
486 "got_repo_get_path_packed_refs");
487 goto done;
490 if (lock) {
491 err = got_lockfile_lock(&lf, packed_refs_path, -1);
492 if (err)
493 goto done;
495 f = fopen(packed_refs_path, "rb");
496 free(packed_refs_path);
497 if (f != NULL) {
498 struct stat sb;
499 if (fstat(fileno(f), &sb) == -1) {
500 err = got_error_from_errno2("fstat",
501 packed_refs_path);
502 goto done;
504 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
505 refname, sb.st_mtime);
506 if (!err) {
507 if (fclose(f) == EOF) {
508 err = got_error_from_errno("fclose");
509 got_ref_close(*ref);
510 *ref = NULL;
511 } else if (*ref)
512 (*ref)->lf = lf;
516 done:
517 if (!err && *ref == NULL)
518 err = got_error_not_ref(refname);
519 if (err && lf)
520 got_lockfile_unlock(lf, -1);
521 free(path_refs);
522 return err;
525 void
526 got_ref_close(struct got_reference *ref)
528 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
529 free(ref->ref.symref.name);
530 free(ref->ref.symref.ref);
531 } else
532 free(ref->ref.ref.name);
533 free(ref);
536 struct got_reference *
537 got_ref_dup(struct got_reference *ref)
539 struct got_reference *ret;
541 ret = calloc(1, sizeof(*ret));
542 if (ret == NULL)
543 return NULL;
545 ret->flags = ref->flags;
546 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
547 ret->ref.symref.name = strdup(ref->ref.symref.name);
548 if (ret->ref.symref.name == NULL) {
549 free(ret);
550 return NULL;
552 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
553 if (ret->ref.symref.ref == NULL) {
554 free(ret->ref.symref.name);
555 free(ret);
556 return NULL;
558 } else {
559 ret->ref.ref.name = strdup(ref->ref.ref.name);
560 if (ret->ref.ref.name == NULL) {
561 free(ret);
562 return NULL;
564 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
565 sizeof(ret->ref.ref.sha1));
568 return ret;
571 const struct got_error *
572 got_reflist_entry_dup(struct got_reflist_entry **newp,
573 struct got_reflist_entry *re)
575 const struct got_error *err = NULL;
576 struct got_reflist_entry *new;
578 *newp = NULL;
580 new = malloc(sizeof(*new));
581 if (new == NULL)
582 return got_error_from_errno("malloc");
584 new->ref = got_ref_dup(re->ref);
585 if (new->ref == NULL) {
586 err = got_error_from_errno("got_ref_dup");
587 free(new);
588 return err;
591 *newp = new;
592 return NULL;
595 static const struct got_error *
596 resolve_symbolic_ref(struct got_reference **resolved,
597 struct got_repository *repo, struct got_reference *ref)
599 struct got_reference *nextref;
600 const struct got_error *err;
602 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
603 if (err)
604 return err;
606 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
607 err = resolve_symbolic_ref(resolved, repo, nextref);
608 else
609 *resolved = got_ref_dup(nextref);
611 got_ref_close(nextref);
612 return err;
615 static const struct got_error *
616 ref_resolve(struct got_object_id **id, struct got_repository *repo,
617 struct got_reference *ref, int recursion)
619 const struct got_error *err;
621 if (recursion <= 0)
622 return got_error_msg(GOT_ERR_RECURSION,
623 "reference recursion limit reached");
625 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
626 struct got_reference *resolved = NULL;
627 err = resolve_symbolic_ref(&resolved, repo, ref);
628 if (err == NULL)
629 err = ref_resolve(id, repo, resolved, --recursion);
630 if (resolved)
631 got_ref_close(resolved);
632 return err;
635 *id = calloc(1, sizeof(**id));
636 if (*id == NULL)
637 return got_error_from_errno("calloc");
638 memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
639 return NULL;
642 const struct got_error *
643 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
644 struct got_reference *ref)
646 return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
649 char *
650 got_ref_to_str(struct got_reference *ref)
652 char *str;
654 if (ref->flags & GOT_REF_IS_SYMBOLIC)
655 return strdup(ref->ref.symref.ref);
657 str = malloc(SHA1_DIGEST_STRING_LENGTH);
658 if (str == NULL)
659 return NULL;
661 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
662 SHA1_DIGEST_STRING_LENGTH) == NULL) {
663 free(str);
664 return NULL;
667 return str;
670 const char *
671 got_ref_get_name(struct got_reference *ref)
673 if (ref->flags & GOT_REF_IS_SYMBOLIC)
674 return ref->ref.symref.name;
676 return ref->ref.ref.name;
679 const char *
680 got_ref_get_symref_target(struct got_reference *ref)
682 if (ref->flags & GOT_REF_IS_SYMBOLIC)
683 return ref->ref.symref.ref;
685 return NULL;
688 time_t
689 got_ref_get_mtime(struct got_reference *ref)
691 return ref->mtime;
694 const struct got_error *
695 got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
696 struct got_reference* re2)
698 const char *name1 = got_ref_get_name(re1);
699 const char *name2 = got_ref_get_name(re2);
701 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
702 return NULL;
705 const struct got_error *
706 got_ref_cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
707 struct got_reference *ref2)
709 const struct got_error *err = NULL;
710 struct got_repository *repo = arg;
711 struct got_object_id *id1, *id2 = NULL;
712 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
713 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
714 time_t time1, time2;
716 *cmp = 0;
718 err = got_ref_resolve(&id1, repo, ref1);
719 if (err)
720 return err;
721 err = got_object_open_as_tag(&tag1, repo, id1);
722 if (err) {
723 if (err->code != GOT_ERR_OBJ_TYPE)
724 goto done;
725 /* "lightweight" tag */
726 err = got_object_open_as_commit(&commit1, repo, id1);
727 if (err)
728 goto done;
729 time1 = got_object_commit_get_committer_time(commit1);
730 } else
731 time1 = got_object_tag_get_tagger_time(tag1);
733 err = got_ref_resolve(&id2, repo, ref2);
734 if (err)
735 goto done;
736 err = got_object_open_as_tag(&tag2, repo, id2);
737 if (err) {
738 if (err->code != GOT_ERR_OBJ_TYPE)
739 goto done;
740 /* "lightweight" tag */
741 err = got_object_open_as_commit(&commit2, repo, id2);
742 if (err)
743 goto done;
744 time2 = got_object_commit_get_committer_time(commit2);
745 } else
746 time2 = got_object_tag_get_tagger_time(tag2);
748 /* Put latest tags first. */
749 if (time1 < time2)
750 *cmp = 1;
751 else if (time1 > time2)
752 *cmp = -1;
753 else
754 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
755 done:
756 free(id1);
757 free(id2);
758 if (tag1)
759 got_object_tag_close(tag1);
760 if (tag2)
761 got_object_tag_close(tag2);
762 if (commit1)
763 got_object_commit_close(commit1);
764 if (commit2)
765 got_object_commit_close(commit2);
766 return err;
769 const struct got_error *
770 got_ref_cmp_by_commit_timestamp_descending(void *arg, int *cmp,
771 struct got_reference *ref1, struct got_reference *ref2)
773 const struct got_error *err;
774 struct got_repository *repo = arg;
775 struct got_object_id *id1, *id2 = NULL;
776 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
777 time_t time1, time2;
779 *cmp = 0;
781 err = got_ref_resolve(&id1, repo, ref1);
782 if (err)
783 return err;
784 err = got_ref_resolve(&id2, repo, ref2);
785 if (err)
786 goto done;
788 err = got_object_open_as_commit(&commit1, repo, id1);
789 if (err)
790 goto done;
791 err = got_object_open_as_commit(&commit2, repo, id2);
792 if (err)
793 goto done;
795 time1 = got_object_commit_get_committer_time(commit1);
796 time2 = got_object_commit_get_committer_time(commit2);
797 if (time1 < time2)
798 *cmp = 1;
799 else if (time2 < time1)
800 *cmp = -1;
801 done:
802 free(id1);
803 free(id2);
804 if (commit1)
805 got_object_commit_close(commit1);
806 if (commit2)
807 got_object_commit_close(commit2);
808 return err;
811 const struct got_error *
812 got_reflist_insert(struct got_reflist_entry **newp, struct got_reflist_head *refs,
813 struct got_reference *ref, struct got_repository *repo,
814 got_ref_cmp_cb cmp_cb, void *cmp_arg)
816 const struct got_error *err;
817 struct got_reflist_entry *new, *re;
818 int cmp;
820 *newp = NULL;
822 new = malloc(sizeof(*new));
823 if (new == NULL)
824 return got_error_from_errno("malloc");
825 new->ref = ref;
826 *newp = new;
828 /*
829 * We must de-duplicate entries on insert because packed-refs may
830 * contain redundant entries. On-disk refs take precedence.
831 * This code assumes that on-disk revs are read before packed-refs.
832 * We're iterating the list anyway, so insert elements sorted by name.
834 * Many callers will provide paths in a somewhat sorted order.
835 * Iterating backwards from the tail of the list should be more
836 * efficient than traversing through the entire list each time
837 * an element is inserted.
838 */
839 re = TAILQ_LAST(refs, got_reflist_head);
840 while (re) {
841 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
842 if (err)
843 return err;
844 if (cmp == 0) {
845 /* duplicate */
846 free(new);
847 *newp = NULL;
848 return NULL;
849 } else if (cmp < 0) {
850 TAILQ_INSERT_AFTER(refs, re, new, entry);
851 return NULL;
853 re = TAILQ_PREV(re, got_reflist_head, entry);
856 TAILQ_INSERT_HEAD(refs, new, entry);
857 return NULL;
860 static const struct got_error *
861 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
862 const char *subdir, struct got_repository *repo,
863 got_ref_cmp_cb cmp_cb, void *cmp_arg)
865 const struct got_error *err = NULL;
866 DIR *d = NULL;
867 char *path_subdir;
869 while (subdir[0] == '/')
870 subdir++;
872 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
873 return got_error_from_errno("asprintf");
875 d = opendir(path_subdir);
876 if (d == NULL)
877 goto done;
879 for (;;) {
880 struct dirent *dent;
881 struct got_reference *ref;
882 char *child;
883 int type;
885 dent = readdir(d);
886 if (dent == NULL)
887 break;
889 if (strcmp(dent->d_name, ".") == 0 ||
890 strcmp(dent->d_name, "..") == 0)
891 continue;
893 err = got_path_dirent_type(&type, path_subdir, dent);
894 if (err)
895 break;
897 switch (type) {
898 case DT_REG:
899 err = open_ref(&ref, path_refs, subdir, dent->d_name,
900 0);
901 if (err)
902 goto done;
903 if (ref) {
904 struct got_reflist_entry *new;
905 err = got_reflist_insert(&new, refs, ref, repo,
906 cmp_cb, cmp_arg);
907 if (err || new == NULL /* duplicate */)
908 got_ref_close(ref);
909 if (err)
910 goto done;
912 break;
913 case DT_DIR:
914 if (asprintf(&child, "%s%s%s", subdir,
915 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
916 err = got_error_from_errno("asprintf");
917 break;
919 err = gather_on_disk_refs(refs, path_refs, child, repo,
920 cmp_cb, cmp_arg);
921 free(child);
922 break;
923 default:
924 break;
927 done:
928 if (d)
929 closedir(d);
930 free(path_subdir);
931 return err;
934 const struct got_error *
935 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
936 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
938 const struct got_error *err;
939 char *packed_refs_path, *path_refs = NULL;
940 char *abs_namespace = NULL;
941 char *buf = NULL, *ondisk_ref_namespace = NULL;
942 char *line = NULL;
943 FILE *f = NULL;
944 struct got_reference *ref;
945 struct got_reflist_entry *new;
947 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
948 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
949 if (path_refs == NULL) {
950 err = got_error_from_errno("get_refs_dir_path");
951 goto done;
953 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
954 if (err)
955 goto done;
956 err = got_reflist_insert(&new, refs, ref, repo,
957 cmp_cb, cmp_arg);
958 if (err || new == NULL /* duplicate */)
959 got_ref_close(ref);
960 if (err && err->code != GOT_ERR_NOT_REF)
961 goto done;
962 } else {
963 /* Try listing a single reference. */
964 const char *refname = ref_namespace;
965 path_refs = get_refs_dir_path(repo, refname);
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, "", refname, 0);
971 if (err) {
972 if (err->code != GOT_ERR_NOT_REF)
973 goto done;
974 /* Try to look up references in a given namespace. */
975 } else {
976 err = got_reflist_insert(&new, refs, ref, repo,
977 cmp_cb, cmp_arg);
978 if (err || new == NULL /* duplicate */)
979 got_ref_close(ref);
980 return err;
984 if (ref_namespace) {
985 size_t len;
986 /* Canonicalize the path to eliminate double-slashes if any. */
987 if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
988 err = got_error_from_errno("asprintf");
989 goto done;
991 len = strlen(abs_namespace) + 1;
992 buf = malloc(len);
993 if (buf == NULL) {
994 err = got_error_from_errno("malloc");
995 goto done;
997 err = got_canonpath(abs_namespace, buf, len);
998 if (err)
999 goto done;
1000 ondisk_ref_namespace = buf;
1001 while (ondisk_ref_namespace[0] == '/')
1002 ondisk_ref_namespace++;
1003 if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
1004 ondisk_ref_namespace += 5;
1005 else if (strcmp(ondisk_ref_namespace, "refs") == 0)
1006 ondisk_ref_namespace = "";
1009 /* Gather on-disk refs before parsing packed-refs. */
1010 free(path_refs);
1011 path_refs = get_refs_dir_path(repo, "");
1012 if (path_refs == NULL) {
1013 err = got_error_from_errno("get_refs_dir_path");
1014 goto done;
1016 err = gather_on_disk_refs(refs, path_refs,
1017 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
1018 cmp_cb, cmp_arg);
1019 if (err)
1020 goto done;
1023 * The packed-refs file may contain redundant entries, in which
1024 * case on-disk refs take precedence.
1026 packed_refs_path = got_repo_get_path_packed_refs(repo);
1027 if (packed_refs_path == NULL) {
1028 err = got_error_from_errno("got_repo_get_path_packed_refs");
1029 goto done;
1032 f = fopen(packed_refs_path, "r");
1033 free(packed_refs_path);
1034 if (f) {
1035 size_t linesize = 0;
1036 ssize_t linelen;
1037 struct stat sb;
1039 if (fstat(fileno(f), &sb) == -1) {
1040 err = got_error_from_errno2("fstat", packed_refs_path);
1041 goto done;
1043 for (;;) {
1044 linelen = getline(&line, &linesize, f);
1045 if (linelen == -1) {
1046 if (feof(f))
1047 break;
1048 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1049 goto done;
1051 if (linelen > 0 && line[linelen - 1] == '\n')
1052 line[linelen - 1] = '\0';
1053 err = parse_packed_ref_line(&ref, NULL, line,
1054 sb.st_mtime);
1055 if (err)
1056 goto done;
1057 if (ref) {
1058 if (ref_namespace) {
1059 const char *name;
1060 name = got_ref_get_name(ref);
1061 if (!got_path_is_child(name,
1062 ref_namespace,
1063 strlen(ref_namespace))) {
1064 got_ref_close(ref);
1065 continue;
1068 err = got_reflist_insert(&new, refs, ref, repo,
1069 cmp_cb, cmp_arg);
1070 if (err || new == NULL /* duplicate */)
1071 got_ref_close(ref);
1072 if (err)
1073 goto done;
1077 done:
1078 free(abs_namespace);
1079 free(buf);
1080 free(line);
1081 free(path_refs);
1082 if (f && fclose(f) == EOF && err == NULL)
1083 err = got_error_from_errno("fclose");
1084 return err;
1087 void
1088 got_ref_list_free(struct got_reflist_head *refs)
1090 struct got_reflist_entry *re;
1092 while ((re = TAILQ_FIRST(refs))) {
1093 TAILQ_REMOVE(refs, re, entry);
1094 got_ref_close(re->ref);
1095 free(re);
1100 int
1101 got_ref_is_symbolic(struct got_reference *ref)
1103 return (ref->flags & GOT_REF_IS_SYMBOLIC);
1106 const struct got_error *
1107 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1109 if (ref->flags & GOT_REF_IS_SYMBOLIC)
1110 return got_error(GOT_ERR_BAD_REF_TYPE);
1112 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
1113 return NULL;
1116 const struct got_error *
1117 got_ref_change_symref(struct got_reference *ref, const char *refname)
1119 char *new_name;
1121 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1122 return got_error(GOT_ERR_BAD_REF_TYPE);
1124 new_name = strdup(refname);
1125 if (new_name == NULL)
1126 return got_error_from_errno("strdup");
1128 free(ref->ref.symref.ref);
1129 ref->ref.symref.ref = new_name;
1130 return NULL;
1133 const struct got_error *
1134 got_ref_change_symref_to_ref(struct got_reference *symref,
1135 struct got_object_id *id)
1137 if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1138 return got_error(GOT_ERR_BAD_REF_TYPE);
1140 symref->ref.ref.name = symref->ref.symref.name;
1141 memcpy(symref->ref.ref.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1142 symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1143 return NULL;
1146 const struct got_error *
1147 got_ref_write(struct got_reference *ref, struct got_repository *repo)
1149 const struct got_error *err = NULL, *unlock_err = NULL;
1150 const char *name = got_ref_get_name(ref);
1151 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1152 struct got_lockfile *lf = NULL;
1153 FILE *f = NULL;
1154 size_t n;
1155 struct stat sb;
1157 path_refs = get_refs_dir_path(repo, name);
1158 if (path_refs == NULL) {
1159 err = got_error_from_errno2("get_refs_dir_path", name);
1160 goto done;
1163 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1164 err = got_error_from_errno("asprintf");
1165 goto done;
1168 err = got_opentemp_named(&tmppath, &f, path);
1169 if (err) {
1170 char *parent;
1171 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1172 goto done;
1173 err = got_path_dirname(&parent, path);
1174 if (err)
1175 goto done;
1176 err = got_path_mkdir(parent);
1177 free(parent);
1178 if (err)
1179 goto done;
1180 err = got_opentemp_named(&tmppath, &f, path);
1181 if (err)
1182 goto done;
1185 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1186 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1187 if (n != strlen(ref->ref.symref.ref) + 6) {
1188 err = got_ferror(f, GOT_ERR_IO);
1189 goto done;
1191 } else {
1192 char hex[SHA1_DIGEST_STRING_LENGTH];
1193 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
1194 sizeof(hex)) == NULL) {
1195 err = got_error(GOT_ERR_BAD_REF_DATA);
1196 goto done;
1198 n = fprintf(f, "%s\n", hex);
1199 if (n != sizeof(hex)) {
1200 err = got_ferror(f, GOT_ERR_IO);
1201 goto done;
1205 if (ref->lf == NULL) {
1206 err = got_lockfile_lock(&lf, path, -1);
1207 if (err)
1208 goto done;
1211 /* XXX: check if old content matches our expectations? */
1213 if (stat(path, &sb) != 0) {
1214 if (errno != ENOENT) {
1215 err = got_error_from_errno2("stat", path);
1216 goto done;
1218 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1221 if (fchmod(fileno(f), sb.st_mode) != 0) {
1222 err = got_error_from_errno2("fchmod", tmppath);
1223 goto done;
1226 if (rename(tmppath, path) != 0) {
1227 err = got_error_from_errno3("rename", tmppath, path);
1228 goto done;
1230 free(tmppath);
1231 tmppath = NULL;
1233 if (stat(path, &sb) == -1) {
1234 err = got_error_from_errno2("stat", path);
1235 goto done;
1237 ref->mtime = sb.st_mtime;
1238 done:
1239 if (ref->lf == NULL && lf)
1240 unlock_err = got_lockfile_unlock(lf, -1);
1241 if (f) {
1242 if (fclose(f) == EOF && err == NULL)
1243 err = got_error_from_errno("fclose");
1245 free(path_refs);
1246 free(path);
1247 if (tmppath) {
1248 if (unlink(tmppath) != 0 && err == NULL)
1249 err = got_error_from_errno2("unlink", tmppath);
1250 free(tmppath);
1252 return err ? err : unlock_err;
1255 static const struct got_error *
1256 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1258 const struct got_error *err = NULL, *unlock_err = NULL;
1259 struct got_lockfile *lf = NULL;
1260 FILE *f = NULL, *tmpf = NULL;
1261 char *line = NULL, *packed_refs_path, *tmppath = NULL;
1262 size_t linesize = 0;
1263 struct got_reflist_head refs;
1264 int found_delref = 0;
1266 /* The packed-refs file does not cotain symbolic references. */
1267 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1268 return got_error(GOT_ERR_BAD_REF_DATA);
1270 TAILQ_INIT(&refs);
1272 packed_refs_path = got_repo_get_path_packed_refs(repo);
1273 if (packed_refs_path == NULL)
1274 return got_error_from_errno("got_repo_get_path_packed_refs");
1276 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
1277 if (err)
1278 goto done;
1280 if (delref->lf == NULL) {
1281 err = got_lockfile_lock(&lf, packed_refs_path, -1);
1282 if (err)
1283 goto done;
1286 f = fopen(packed_refs_path, "r");
1287 if (f == NULL) {
1288 err = got_error_from_errno2("fopen", packed_refs_path);
1289 goto done;
1291 for (;;) {
1292 ssize_t linelen;
1293 struct got_reference *ref;
1294 struct got_reflist_entry *new;
1296 linelen = getline(&line, &linesize, f);
1297 if (linelen == -1) {
1298 if (feof(f))
1299 break;
1300 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1301 goto done;
1303 if (linelen > 0 && line[linelen - 1] == '\n')
1304 line[linelen - 1] = '\0';
1305 err = parse_packed_ref_line(&ref, NULL, line, 0);
1306 if (err)
1307 goto done;
1308 if (ref == NULL)
1309 continue;
1311 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1312 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1313 sizeof(delref->ref.ref.sha1)) == 0) {
1314 found_delref = 1;
1315 got_ref_close(ref);
1316 continue;
1319 err = got_reflist_insert(&new, &refs, ref, repo,
1320 got_ref_cmp_by_name, NULL);
1321 if (err || new == NULL /* duplicate */)
1322 got_ref_close(ref);
1323 if (err)
1324 goto done;
1327 if (found_delref) {
1328 struct got_reflist_entry *re;
1329 size_t n;
1330 struct stat sb;
1332 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1333 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1334 err = got_ferror(f, GOT_ERR_IO);
1335 goto done;
1338 TAILQ_FOREACH(re, &refs, entry) {
1339 uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
1341 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1342 sizeof(hex)) == NULL) {
1343 err = got_error(GOT_ERR_BAD_REF_DATA);
1344 goto done;
1346 n = fprintf(tmpf, "%s ", hex);
1347 if (n != sizeof(hex)) {
1348 err = got_ferror(f, GOT_ERR_IO);
1349 goto done;
1351 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1352 if (n != strlen(re->ref->ref.ref.name) + 1) {
1353 err = got_ferror(f, GOT_ERR_IO);
1354 goto done;
1358 if (fflush(tmpf) != 0) {
1359 err = got_error_from_errno("fflush");
1360 goto done;
1363 if (fstat(fileno(f), &sb) != 0) {
1364 if (errno != ENOENT) {
1365 err = got_error_from_errno2("fstat",
1366 packed_refs_path);
1367 goto done;
1369 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1372 if (fchmod(fileno(tmpf), sb.st_mode) != 0) {
1373 err = got_error_from_errno2("fchmod", tmppath);
1374 goto done;
1377 if (rename(tmppath, packed_refs_path) != 0) {
1378 err = got_error_from_errno3("rename", tmppath,
1379 packed_refs_path);
1380 goto done;
1383 done:
1384 if (delref->lf == NULL && lf)
1385 unlock_err = got_lockfile_unlock(lf, -1);
1386 if (f) {
1387 if (fclose(f) == EOF && err == NULL)
1388 err = got_error_from_errno("fclose");
1390 if (tmpf) {
1391 unlink(tmppath);
1392 if (fclose(tmpf) == EOF && err == NULL)
1393 err = got_error_from_errno("fclose");
1395 free(tmppath);
1396 free(packed_refs_path);
1397 free(line);
1398 got_ref_list_free(&refs);
1399 return err ? err : unlock_err;
1402 static const struct got_error *
1403 delete_loose_ref(struct got_reference *ref, struct got_repository *repo)
1405 const struct got_error *err = NULL, *unlock_err = NULL;
1406 const char *name = got_ref_get_name(ref);
1407 char *path_refs = NULL, *path = NULL;
1408 struct got_lockfile *lf = NULL;
1410 path_refs = get_refs_dir_path(repo, name);
1411 if (path_refs == NULL) {
1412 err = got_error_from_errno2("get_refs_dir_path", name);
1413 goto done;
1416 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1417 err = got_error_from_errno("asprintf");
1418 goto done;
1421 if (ref->lf == NULL) {
1422 err = got_lockfile_lock(&lf, path, -1);
1423 if (err)
1424 goto done;
1427 /* XXX: check if old content matches our expectations? */
1429 if (unlink(path) != 0)
1430 err = got_error_from_errno2("unlink", path);
1431 done:
1432 if (ref->lf == NULL && lf)
1433 unlock_err = got_lockfile_unlock(lf, -1);
1435 free(path_refs);
1436 free(path);
1437 return err ? err : unlock_err;
1440 const struct got_error *
1441 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1443 const struct got_error *err = NULL;
1444 struct got_reference *ref2;
1446 if (ref->flags & GOT_REF_IS_PACKED) {
1447 err = delete_packed_ref(ref, repo);
1448 if (err)
1449 return err;
1451 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1452 if (err) {
1453 if (err->code == GOT_ERR_NOT_REF)
1454 return NULL;
1455 return err;
1458 err = delete_loose_ref(ref2, repo);
1459 got_ref_close(ref2);
1460 return err;
1461 } else {
1462 err = delete_loose_ref(ref, repo);
1463 if (err)
1464 return err;
1466 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1467 if (err) {
1468 if (err->code == GOT_ERR_NOT_REF)
1469 return NULL;
1470 return err;
1473 err = delete_packed_ref(ref2, repo);
1474 got_ref_close(ref2);
1475 return err;
1479 const struct got_error *
1480 got_ref_unlock(struct got_reference *ref)
1482 const struct got_error *err;
1483 err = got_lockfile_unlock(ref->lf, -1);
1484 ref->lf = NULL;
1485 return err;
1488 struct got_reflist_object_id_map {
1489 struct got_object_idset *idset;
1492 struct got_reflist_object_id_map_entry {
1493 struct got_reflist_head refs;
1496 static const struct got_error *
1497 add_object_id_map_entry(struct got_object_idset *idset,
1498 struct got_object_id *id, struct got_reflist_entry *re)
1500 const struct got_error *err = NULL;
1501 struct got_reflist_object_id_map_entry *ent;
1502 struct got_reflist_entry *new;
1504 ent = got_object_idset_get(idset, id);
1505 if (ent == NULL) {
1506 ent = malloc(sizeof(*ent));
1507 if (ent == NULL)
1508 return got_error_from_errno("malloc");
1510 TAILQ_INIT(&ent->refs);
1511 err = got_object_idset_add(idset, id, ent);
1512 if (err)
1513 return err;
1516 err = got_reflist_entry_dup(&new, re);
1517 if (err)
1518 return err;
1520 TAILQ_INSERT_TAIL(&ent->refs, new, entry);
1521 return NULL;
1524 const struct got_error *
1525 got_reflist_object_id_map_create(struct got_reflist_object_id_map **map,
1526 struct got_reflist_head *refs, struct got_repository *repo)
1528 const struct got_error *err = NULL;
1529 struct got_object_idset *idset;
1530 struct got_object_id *id = NULL;
1531 struct got_reflist_entry *re;
1533 idset = got_object_idset_alloc();
1534 if (idset == NULL)
1535 return got_error_from_errno("got_object_idset_alloc");
1537 *map = malloc(sizeof(**map));
1538 if (*map == NULL) {
1539 got_object_idset_free(idset);
1540 return got_error_from_errno("malloc");
1542 (*map)->idset = idset;
1544 TAILQ_FOREACH(re, refs, entry) {
1545 struct got_tag_object *tag = NULL;
1547 err = got_ref_resolve(&id, repo, re->ref);
1548 if (err)
1549 goto done;
1551 err = add_object_id_map_entry(idset, id, re);
1552 if (err)
1553 goto done;
1555 if (strstr(got_ref_get_name(re->ref), "/tags/") == NULL) {
1556 free(id);
1557 id = NULL;
1558 continue;
1561 err = got_object_open_as_tag(&tag, repo, id);
1562 if (err) {
1563 if (err->code != GOT_ERR_OBJ_TYPE)
1564 goto done;
1565 /* Ref points at something other than a tag. */
1566 err = NULL;
1567 tag = NULL;
1568 free(id);
1569 id = NULL;
1570 continue;
1573 err = add_object_id_map_entry(idset,
1574 got_object_tag_get_object_id(tag), re);
1575 got_object_tag_close(tag);
1576 if (err)
1577 goto done;
1579 free(id);
1580 id = NULL;
1582 done:
1583 free(id);
1584 if (err) {
1585 got_reflist_object_id_map_free(*map);
1586 *map = NULL;
1588 return err;
1591 struct got_reflist_head *
1592 got_reflist_object_id_map_lookup(struct got_reflist_object_id_map *map,
1593 struct got_object_id *id)
1595 struct got_reflist_object_id_map_entry *ent;
1596 ent = got_object_idset_get(map->idset, id);
1597 if (ent)
1598 return &ent->refs;
1599 return NULL;
1602 static const struct got_error *
1603 free_id_map_entry(struct got_object_id *id, void *data, void *arg)
1605 struct got_reflist_object_id_map_entry *ent = data;
1607 got_ref_list_free(&ent->refs);
1608 free(ent);
1609 return NULL;
1612 void
1613 got_reflist_object_id_map_free(struct got_reflist_object_id_map *map)
1615 got_object_idset_for_each(map->idset, free_id_map_entry, NULL);
1616 got_object_idset_free(map->idset);
1617 free(map);