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 int
269 got_ref_name_is_valid(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 (!got_ref_name_is_valid(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 (!got_ref_name_is_valid(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 (!got_ref_name_is_valid(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 const struct got_error *
596 got_ref_resolve_symbolic(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 = got_ref_resolve_symbolic(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 = got_ref_resolve_symbolic(&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, got_ref_cmp_cb cmp_cb, void *cmp_arg)
815 const struct got_error *err;
816 struct got_reflist_entry *new, *re;
817 int cmp;
819 *newp = NULL;
821 new = malloc(sizeof(*new));
822 if (new == NULL)
823 return got_error_from_errno("malloc");
824 new->ref = ref;
825 *newp = new;
827 /*
828 * We must de-duplicate entries on insert because packed-refs may
829 * contain redundant entries. On-disk refs take precedence.
830 * This code assumes that on-disk revs are read before packed-refs.
831 * We're iterating the list anyway, so insert elements sorted by name.
833 * Many callers will provide paths in a somewhat sorted order.
834 * Iterating backwards from the tail of the list should be more
835 * efficient than traversing through the entire list each time
836 * an element is inserted.
837 */
838 re = TAILQ_LAST(refs, got_reflist_head);
839 while (re) {
840 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
841 if (err)
842 return err;
843 if (cmp == 0) {
844 /* duplicate */
845 free(new);
846 *newp = NULL;
847 return NULL;
848 } else if (cmp < 0) {
849 TAILQ_INSERT_AFTER(refs, re, new, entry);
850 return NULL;
852 re = TAILQ_PREV(re, got_reflist_head, entry);
855 TAILQ_INSERT_HEAD(refs, new, entry);
856 return NULL;
859 static const struct got_error *
860 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
861 const char *subdir, struct got_repository *repo,
862 got_ref_cmp_cb cmp_cb, void *cmp_arg)
864 const struct got_error *err = NULL;
865 DIR *d = NULL;
866 char *path_subdir;
868 while (subdir[0] == '/')
869 subdir++;
871 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
872 return got_error_from_errno("asprintf");
874 d = opendir(path_subdir);
875 if (d == NULL)
876 goto done;
878 for (;;) {
879 struct dirent *dent;
880 struct got_reference *ref;
881 char *child;
882 int type;
884 dent = readdir(d);
885 if (dent == NULL)
886 break;
888 if (strcmp(dent->d_name, ".") == 0 ||
889 strcmp(dent->d_name, "..") == 0)
890 continue;
892 err = got_path_dirent_type(&type, path_subdir, dent);
893 if (err)
894 break;
896 switch (type) {
897 case DT_REG:
898 err = open_ref(&ref, path_refs, subdir, dent->d_name,
899 0);
900 if (err)
901 goto done;
902 if (ref) {
903 struct got_reflist_entry *new;
904 err = got_reflist_insert(&new, refs, ref,
905 cmp_cb, cmp_arg);
906 if (err || new == NULL /* duplicate */)
907 got_ref_close(ref);
908 if (err)
909 goto done;
911 break;
912 case DT_DIR:
913 if (asprintf(&child, "%s%s%s", subdir,
914 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
915 err = got_error_from_errno("asprintf");
916 break;
918 err = gather_on_disk_refs(refs, path_refs, child, repo,
919 cmp_cb, cmp_arg);
920 free(child);
921 break;
922 default:
923 break;
926 done:
927 if (d)
928 closedir(d);
929 free(path_subdir);
930 return err;
933 const struct got_error *
934 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
935 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
937 const struct got_error *err;
938 char *packed_refs_path, *path_refs = NULL;
939 char *abs_namespace = NULL;
940 char *buf = NULL, *ondisk_ref_namespace = NULL;
941 char *line = NULL;
942 FILE *f = NULL;
943 struct got_reference *ref;
944 struct got_reflist_entry *new;
946 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
947 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
948 if (path_refs == NULL) {
949 err = got_error_from_errno("get_refs_dir_path");
950 goto done;
952 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
953 if (err)
954 goto done;
955 err = got_reflist_insert(&new, refs, ref, cmp_cb, cmp_arg);
956 if (err || new == NULL /* duplicate */)
957 got_ref_close(ref);
958 if (err && err->code != GOT_ERR_NOT_REF)
959 goto done;
960 } else {
961 /* Try listing a single reference. */
962 const char *refname = ref_namespace;
963 path_refs = get_refs_dir_path(repo, refname);
964 if (path_refs == NULL) {
965 err = got_error_from_errno("get_refs_dir_path");
966 goto done;
968 err = open_ref(&ref, path_refs, "", refname, 0);
969 if (err) {
970 if (err->code != GOT_ERR_NOT_REF)
971 goto done;
972 /* Try to look up references in a given namespace. */
973 } else {
974 err = got_reflist_insert(&new, refs, ref,
975 cmp_cb, cmp_arg);
976 if (err || new == NULL /* duplicate */)
977 got_ref_close(ref);
978 return err;
982 if (ref_namespace) {
983 size_t len;
984 /* Canonicalize the path to eliminate double-slashes if any. */
985 if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
986 err = got_error_from_errno("asprintf");
987 goto done;
989 len = strlen(abs_namespace) + 1;
990 buf = malloc(len);
991 if (buf == NULL) {
992 err = got_error_from_errno("malloc");
993 goto done;
995 err = got_canonpath(abs_namespace, buf, len);
996 if (err)
997 goto done;
998 ondisk_ref_namespace = buf;
999 while (ondisk_ref_namespace[0] == '/')
1000 ondisk_ref_namespace++;
1001 if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
1002 ondisk_ref_namespace += 5;
1003 else if (strcmp(ondisk_ref_namespace, "refs") == 0)
1004 ondisk_ref_namespace = "";
1007 /* Gather on-disk refs before parsing packed-refs. */
1008 free(path_refs);
1009 path_refs = get_refs_dir_path(repo, "");
1010 if (path_refs == NULL) {
1011 err = got_error_from_errno("get_refs_dir_path");
1012 goto done;
1014 err = gather_on_disk_refs(refs, path_refs,
1015 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
1016 cmp_cb, cmp_arg);
1017 if (err)
1018 goto done;
1021 * The packed-refs file may contain redundant entries, in which
1022 * case on-disk refs take precedence.
1024 packed_refs_path = got_repo_get_path_packed_refs(repo);
1025 if (packed_refs_path == NULL) {
1026 err = got_error_from_errno("got_repo_get_path_packed_refs");
1027 goto done;
1030 f = fopen(packed_refs_path, "r");
1031 free(packed_refs_path);
1032 if (f) {
1033 size_t linesize = 0;
1034 ssize_t linelen;
1035 struct stat sb;
1037 if (fstat(fileno(f), &sb) == -1) {
1038 err = got_error_from_errno2("fstat", packed_refs_path);
1039 goto done;
1041 for (;;) {
1042 linelen = getline(&line, &linesize, f);
1043 if (linelen == -1) {
1044 if (feof(f))
1045 break;
1046 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1047 goto done;
1049 if (linelen > 0 && line[linelen - 1] == '\n')
1050 line[linelen - 1] = '\0';
1051 err = parse_packed_ref_line(&ref, NULL, line,
1052 sb.st_mtime);
1053 if (err)
1054 goto done;
1055 if (ref) {
1056 if (ref_namespace) {
1057 const char *name;
1058 name = got_ref_get_name(ref);
1059 if (!got_path_is_child(name,
1060 ref_namespace,
1061 strlen(ref_namespace))) {
1062 got_ref_close(ref);
1063 continue;
1066 err = got_reflist_insert(&new, refs, ref,
1067 cmp_cb, cmp_arg);
1068 if (err || new == NULL /* duplicate */)
1069 got_ref_close(ref);
1070 if (err)
1071 goto done;
1075 done:
1076 free(abs_namespace);
1077 free(buf);
1078 free(line);
1079 free(path_refs);
1080 if (f && fclose(f) == EOF && err == NULL)
1081 err = got_error_from_errno("fclose");
1082 return err;
1085 void
1086 got_ref_list_free(struct got_reflist_head *refs)
1088 struct got_reflist_entry *re;
1090 while ((re = TAILQ_FIRST(refs))) {
1091 TAILQ_REMOVE(refs, re, entry);
1092 got_ref_close(re->ref);
1093 free(re);
1098 int
1099 got_ref_is_symbolic(struct got_reference *ref)
1101 return (ref->flags & GOT_REF_IS_SYMBOLIC);
1104 const struct got_error *
1105 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1107 if (ref->flags & GOT_REF_IS_SYMBOLIC)
1108 return got_error(GOT_ERR_BAD_REF_TYPE);
1110 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
1111 return NULL;
1114 const struct got_error *
1115 got_ref_change_symref(struct got_reference *ref, const char *refname)
1117 char *new_name;
1119 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1120 return got_error(GOT_ERR_BAD_REF_TYPE);
1122 new_name = strdup(refname);
1123 if (new_name == NULL)
1124 return got_error_from_errno("strdup");
1126 free(ref->ref.symref.ref);
1127 ref->ref.symref.ref = new_name;
1128 return NULL;
1131 const struct got_error *
1132 got_ref_change_symref_to_ref(struct got_reference *symref,
1133 struct got_object_id *id)
1135 if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1136 return got_error(GOT_ERR_BAD_REF_TYPE);
1138 symref->ref.ref.name = symref->ref.symref.name;
1139 memcpy(symref->ref.ref.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1140 symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1141 return NULL;
1144 const struct got_error *
1145 got_ref_write(struct got_reference *ref, struct got_repository *repo)
1147 const struct got_error *err = NULL, *unlock_err = NULL;
1148 const char *name = got_ref_get_name(ref);
1149 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1150 struct got_lockfile *lf = NULL;
1151 FILE *f = NULL;
1152 size_t n;
1153 struct stat sb;
1155 path_refs = get_refs_dir_path(repo, name);
1156 if (path_refs == NULL) {
1157 err = got_error_from_errno2("get_refs_dir_path", name);
1158 goto done;
1161 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1162 err = got_error_from_errno("asprintf");
1163 goto done;
1166 err = got_opentemp_named(&tmppath, &f, path);
1167 if (err) {
1168 char *parent;
1169 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1170 goto done;
1171 err = got_path_dirname(&parent, path);
1172 if (err)
1173 goto done;
1174 err = got_path_mkdir(parent);
1175 free(parent);
1176 if (err)
1177 goto done;
1178 err = got_opentemp_named(&tmppath, &f, path);
1179 if (err)
1180 goto done;
1183 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1184 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1185 if (n != strlen(ref->ref.symref.ref) + 6) {
1186 err = got_ferror(f, GOT_ERR_IO);
1187 goto done;
1189 } else {
1190 char hex[SHA1_DIGEST_STRING_LENGTH];
1191 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
1192 sizeof(hex)) == NULL) {
1193 err = got_error(GOT_ERR_BAD_REF_DATA);
1194 goto done;
1196 n = fprintf(f, "%s\n", hex);
1197 if (n != sizeof(hex)) {
1198 err = got_ferror(f, GOT_ERR_IO);
1199 goto done;
1203 if (ref->lf == NULL) {
1204 err = got_lockfile_lock(&lf, path, -1);
1205 if (err)
1206 goto done;
1209 /* XXX: check if old content matches our expectations? */
1211 if (stat(path, &sb) != 0) {
1212 if (errno != ENOENT) {
1213 err = got_error_from_errno2("stat", path);
1214 goto done;
1216 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1219 if (fchmod(fileno(f), sb.st_mode) != 0) {
1220 err = got_error_from_errno2("fchmod", tmppath);
1221 goto done;
1224 if (rename(tmppath, path) != 0) {
1225 err = got_error_from_errno3("rename", tmppath, path);
1226 goto done;
1228 free(tmppath);
1229 tmppath = NULL;
1231 if (stat(path, &sb) == -1) {
1232 err = got_error_from_errno2("stat", path);
1233 goto done;
1235 ref->mtime = sb.st_mtime;
1236 done:
1237 if (ref->lf == NULL && lf)
1238 unlock_err = got_lockfile_unlock(lf, -1);
1239 if (f) {
1240 if (fclose(f) == EOF && err == NULL)
1241 err = got_error_from_errno("fclose");
1243 free(path_refs);
1244 free(path);
1245 if (tmppath) {
1246 if (unlink(tmppath) != 0 && err == NULL)
1247 err = got_error_from_errno2("unlink", tmppath);
1248 free(tmppath);
1250 return err ? err : unlock_err;
1253 static const struct got_error *
1254 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1256 const struct got_error *err = NULL, *unlock_err = NULL;
1257 struct got_lockfile *lf = NULL;
1258 FILE *f = NULL, *tmpf = NULL;
1259 char *line = NULL, *packed_refs_path, *tmppath = NULL;
1260 size_t linesize = 0;
1261 struct got_reflist_head refs;
1262 int found_delref = 0;
1264 /* The packed-refs file does not cotain symbolic references. */
1265 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1266 return got_error(GOT_ERR_BAD_REF_DATA);
1268 TAILQ_INIT(&refs);
1270 packed_refs_path = got_repo_get_path_packed_refs(repo);
1271 if (packed_refs_path == NULL)
1272 return got_error_from_errno("got_repo_get_path_packed_refs");
1274 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
1275 if (err)
1276 goto done;
1278 if (delref->lf == NULL) {
1279 err = got_lockfile_lock(&lf, packed_refs_path, -1);
1280 if (err)
1281 goto done;
1284 f = fopen(packed_refs_path, "r");
1285 if (f == NULL) {
1286 err = got_error_from_errno2("fopen", packed_refs_path);
1287 goto done;
1289 for (;;) {
1290 ssize_t linelen;
1291 struct got_reference *ref;
1292 struct got_reflist_entry *new;
1294 linelen = getline(&line, &linesize, f);
1295 if (linelen == -1) {
1296 if (feof(f))
1297 break;
1298 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1299 goto done;
1301 if (linelen > 0 && line[linelen - 1] == '\n')
1302 line[linelen - 1] = '\0';
1303 err = parse_packed_ref_line(&ref, NULL, line, 0);
1304 if (err)
1305 goto done;
1306 if (ref == NULL)
1307 continue;
1309 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1310 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1311 sizeof(delref->ref.ref.sha1)) == 0) {
1312 found_delref = 1;
1313 got_ref_close(ref);
1314 continue;
1317 err = got_reflist_insert(&new, &refs, ref,
1318 got_ref_cmp_by_name, NULL);
1319 if (err || new == NULL /* duplicate */)
1320 got_ref_close(ref);
1321 if (err)
1322 goto done;
1325 if (found_delref) {
1326 struct got_reflist_entry *re;
1327 size_t n;
1328 struct stat sb;
1330 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1331 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1332 err = got_ferror(f, GOT_ERR_IO);
1333 goto done;
1336 TAILQ_FOREACH(re, &refs, entry) {
1337 uint8_t hex[SHA1_DIGEST_STRING_LENGTH];
1339 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1340 sizeof(hex)) == NULL) {
1341 err = got_error(GOT_ERR_BAD_REF_DATA);
1342 goto done;
1344 n = fprintf(tmpf, "%s ", hex);
1345 if (n != sizeof(hex)) {
1346 err = got_ferror(f, GOT_ERR_IO);
1347 goto done;
1349 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1350 if (n != strlen(re->ref->ref.ref.name) + 1) {
1351 err = got_ferror(f, GOT_ERR_IO);
1352 goto done;
1356 if (fflush(tmpf) != 0) {
1357 err = got_error_from_errno("fflush");
1358 goto done;
1361 if (fstat(fileno(f), &sb) != 0) {
1362 if (errno != ENOENT) {
1363 err = got_error_from_errno2("fstat",
1364 packed_refs_path);
1365 goto done;
1367 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1370 if (fchmod(fileno(tmpf), sb.st_mode) != 0) {
1371 err = got_error_from_errno2("fchmod", tmppath);
1372 goto done;
1375 if (rename(tmppath, packed_refs_path) != 0) {
1376 err = got_error_from_errno3("rename", tmppath,
1377 packed_refs_path);
1378 goto done;
1381 done:
1382 if (delref->lf == NULL && lf)
1383 unlock_err = got_lockfile_unlock(lf, -1);
1384 if (f) {
1385 if (fclose(f) == EOF && err == NULL)
1386 err = got_error_from_errno("fclose");
1388 if (tmpf) {
1389 unlink(tmppath);
1390 if (fclose(tmpf) == EOF && err == NULL)
1391 err = got_error_from_errno("fclose");
1393 free(tmppath);
1394 free(packed_refs_path);
1395 free(line);
1396 got_ref_list_free(&refs);
1397 return err ? err : unlock_err;
1400 static const struct got_error *
1401 delete_loose_ref(struct got_reference *ref, struct got_repository *repo)
1403 const struct got_error *err = NULL, *unlock_err = NULL;
1404 const char *name = got_ref_get_name(ref);
1405 char *path_refs = NULL, *path = NULL;
1406 struct got_lockfile *lf = NULL;
1408 path_refs = get_refs_dir_path(repo, name);
1409 if (path_refs == NULL) {
1410 err = got_error_from_errno2("get_refs_dir_path", name);
1411 goto done;
1414 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1415 err = got_error_from_errno("asprintf");
1416 goto done;
1419 if (ref->lf == NULL) {
1420 err = got_lockfile_lock(&lf, path, -1);
1421 if (err)
1422 goto done;
1425 /* XXX: check if old content matches our expectations? */
1427 if (unlink(path) != 0)
1428 err = got_error_from_errno2("unlink", path);
1429 done:
1430 if (ref->lf == NULL && lf)
1431 unlock_err = got_lockfile_unlock(lf, -1);
1433 free(path_refs);
1434 free(path);
1435 return err ? err : unlock_err;
1438 const struct got_error *
1439 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1441 const struct got_error *err = NULL;
1442 struct got_reference *ref2;
1444 if (ref->flags & GOT_REF_IS_PACKED) {
1445 err = delete_packed_ref(ref, repo);
1446 if (err)
1447 return err;
1449 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1450 if (err) {
1451 if (err->code == GOT_ERR_NOT_REF)
1452 return NULL;
1453 return err;
1456 err = delete_loose_ref(ref2, repo);
1457 got_ref_close(ref2);
1458 return err;
1459 } else {
1460 err = delete_loose_ref(ref, repo);
1461 if (err)
1462 return err;
1464 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1465 if (err) {
1466 if (err->code == GOT_ERR_NOT_REF)
1467 return NULL;
1468 return err;
1471 err = delete_packed_ref(ref2, repo);
1472 got_ref_close(ref2);
1473 return err;
1477 const struct got_error *
1478 got_ref_unlock(struct got_reference *ref)
1480 const struct got_error *err;
1481 err = got_lockfile_unlock(ref->lf, -1);
1482 ref->lf = NULL;
1483 return err;
1486 struct got_reflist_object_id_map {
1487 struct got_object_idset *idset;
1490 struct got_reflist_object_id_map_entry {
1491 struct got_reflist_head refs;
1494 static const struct got_error *
1495 add_object_id_map_entry(struct got_object_idset *idset,
1496 struct got_object_id *id, struct got_reflist_entry *re)
1498 const struct got_error *err = NULL;
1499 struct got_reflist_object_id_map_entry *ent;
1500 struct got_reflist_entry *new;
1502 ent = got_object_idset_get(idset, id);
1503 if (ent == NULL) {
1504 ent = malloc(sizeof(*ent));
1505 if (ent == NULL)
1506 return got_error_from_errno("malloc");
1508 TAILQ_INIT(&ent->refs);
1509 err = got_object_idset_add(idset, id, ent);
1510 if (err)
1511 return err;
1514 err = got_reflist_entry_dup(&new, re);
1515 if (err)
1516 return err;
1518 TAILQ_INSERT_TAIL(&ent->refs, new, entry);
1519 return NULL;
1522 const struct got_error *
1523 got_reflist_object_id_map_create(struct got_reflist_object_id_map **map,
1524 struct got_reflist_head *refs, struct got_repository *repo)
1526 const struct got_error *err = NULL;
1527 struct got_object_idset *idset;
1528 struct got_object_id *id = NULL;
1529 struct got_reflist_entry *re;
1531 idset = got_object_idset_alloc();
1532 if (idset == NULL)
1533 return got_error_from_errno("got_object_idset_alloc");
1535 *map = malloc(sizeof(**map));
1536 if (*map == NULL) {
1537 got_object_idset_free(idset);
1538 return got_error_from_errno("malloc");
1540 (*map)->idset = idset;
1542 TAILQ_FOREACH(re, refs, entry) {
1543 struct got_tag_object *tag = NULL;
1545 err = got_ref_resolve(&id, repo, re->ref);
1546 if (err)
1547 goto done;
1549 err = add_object_id_map_entry(idset, id, re);
1550 if (err)
1551 goto done;
1553 if (strstr(got_ref_get_name(re->ref), "/tags/") == NULL) {
1554 free(id);
1555 id = NULL;
1556 continue;
1559 err = got_object_open_as_tag(&tag, repo, id);
1560 if (err) {
1561 if (err->code != GOT_ERR_OBJ_TYPE)
1562 goto done;
1563 /* Ref points at something other than a tag. */
1564 err = NULL;
1565 tag = NULL;
1566 free(id);
1567 id = NULL;
1568 continue;
1571 err = add_object_id_map_entry(idset,
1572 got_object_tag_get_object_id(tag), re);
1573 got_object_tag_close(tag);
1574 if (err)
1575 goto done;
1577 free(id);
1578 id = NULL;
1580 done:
1581 free(id);
1582 if (err) {
1583 got_reflist_object_id_map_free(*map);
1584 *map = NULL;
1586 return err;
1589 struct got_reflist_head *
1590 got_reflist_object_id_map_lookup(struct got_reflist_object_id_map *map,
1591 struct got_object_id *id)
1593 struct got_reflist_object_id_map_entry *ent;
1594 ent = got_object_idset_get(map->idset, id);
1595 if (ent)
1596 return &ent->refs;
1597 return NULL;
1600 static const struct got_error *
1601 free_id_map_entry(struct got_object_id *id, void *data, void *arg)
1603 struct got_reflist_object_id_map_entry *ent = data;
1605 got_ref_list_free(&ent->refs);
1606 free(ent);
1607 return NULL;
1610 void
1611 got_reflist_object_id_map_free(struct got_reflist_object_id_map *map)
1613 got_object_idset_for_each(map->idset, free_id_map_entry, NULL);
1614 got_object_idset_free(map->idset);
1615 free(map);