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/stat.h>
20 #include <errno.h>
21 #include <ctype.h>
22 #include <dirent.h>
23 #include <limits.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <zlib.h>
29 #include <time.h>
30 #include <libgen.h>
32 #include "got_compat.h"
34 #include "got_error.h"
35 #include "got_object.h"
36 #include "got_repository.h"
37 #include "got_reference.h"
38 #include "got_opentemp.h"
39 #include "got_path.h"
41 #include "got_lib_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 u_int8_t sha1[SHA1_DIGEST_LENGTH];
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.sha1, id->sha1, sizeof((*ref)->ref.ref.sha1));
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, "rb");
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 int
271 got_ref_name_is_valid(const char *name)
273 const char *s, *seg;
274 const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
275 const char *forbidden_seq[] = { "//", "..", "@{" };
276 const char *lfs = GOT_LOCKFILE_SUFFIX;
277 const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
278 size_t i;
280 if (name[0] == '@' && name[1] == '\0')
281 return 0;
283 s = name;
284 seg = s;
285 if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
286 return 0;
287 while (*s) {
288 for (i = 0; i < nitems(forbidden); i++) {
289 if (*s == forbidden[i])
290 return 0;
292 for (i = 0; i < nitems(forbidden_seq); i++) {
293 if (s[0] == forbidden_seq[i][0] &&
294 s[1] == forbidden_seq[i][1])
295 return 0;
297 if (iscntrl((unsigned char)s[0]))
298 return 0;
299 if (s[0] == '.' && s[1] == '\0')
300 return 0;
301 if (*s == '/') {
302 const char *nextseg = s + 1;
303 if (nextseg[0] == '\0' || nextseg[0] == '.' ||
304 nextseg[0] == '/')
305 return 0;
306 if (seg <= s - lfs_len &&
307 strncmp(s - lfs_len, lfs, lfs_len) == 0)
308 return 0;
309 seg = nextseg;
311 s++;
314 if (seg <= s - lfs_len &&
315 strncmp(s - lfs_len, lfs, lfs_len) == 0)
316 return 0;
318 return 1;
321 const struct got_error *
322 got_ref_alloc(struct got_reference **ref, const char *name,
323 struct got_object_id *id)
325 if (!got_ref_name_is_valid(name))
326 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
328 return alloc_ref(ref, name, id, 0, 0);
331 const struct got_error *
332 got_ref_alloc_symref(struct got_reference **ref, const char *name,
333 struct got_reference *target_ref)
335 if (!got_ref_name_is_valid(name))
336 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
338 return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
341 static const struct got_error *
342 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
343 const char *line, time_t mtime)
345 struct got_object_id id;
346 const char *name;
348 *ref = NULL;
350 if (line[0] == '#' || line[0] == '^')
351 return NULL;
353 if (!got_parse_sha1_digest(id.sha1, line))
354 return got_error(GOT_ERR_BAD_REF_DATA);
356 if (abs_refname) {
357 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
358 return NULL;
359 name = abs_refname;
360 } else
361 name = line + SHA1_DIGEST_STRING_LENGTH;
363 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED, mtime);
366 static const struct got_error *
367 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
368 int nsubdirs, const char *refname, time_t mtime)
370 const struct got_error *err = NULL;
371 char *abs_refname;
372 char *line = NULL;
373 size_t linesize = 0;
374 ssize_t linelen;
375 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
377 *ref = NULL;
379 if (ref_is_absolute)
380 abs_refname = (char *)refname;
381 do {
382 linelen = getline(&line, &linesize, f);
383 if (linelen == -1) {
384 if (feof(f))
385 break;
386 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
387 break;
389 if (linelen > 0 && line[linelen - 1] == '\n')
390 line[linelen - 1] = '\0';
391 for (i = 0; i < nsubdirs; i++) {
392 if (!ref_is_absolute &&
393 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
394 refname) == -1)
395 return got_error_from_errno("asprintf");
396 err = parse_packed_ref_line(ref, abs_refname, line,
397 mtime);
398 if (!ref_is_absolute)
399 free(abs_refname);
400 if (err || *ref != NULL)
401 break;
403 if (err)
404 break;
405 } while (*ref == NULL);
406 free(line);
408 return err;
411 static const struct got_error *
412 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
413 const char *name, int lock)
415 const struct got_error *err = NULL;
416 char *path = NULL;
417 char *absname = NULL;
418 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
419 int ref_is_well_known = (subdir[0] == '\0' && is_well_known_ref(name));
421 *ref = NULL;
423 if (!got_ref_name_is_valid(name))
424 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
426 if (ref_is_absolute || ref_is_well_known) {
427 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
428 return got_error_from_errno("asprintf");
429 absname = (char *)name;
430 } else {
431 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
432 subdir[0] ? "/" : "", name) == -1)
433 return got_error_from_errno("asprintf");
435 if (asprintf(&absname, "refs/%s%s%s",
436 subdir, subdir[0] ? "/" : "", name) == -1) {
437 err = got_error_from_errno("asprintf");
438 goto done;
442 err = parse_ref_file(ref, name, absname, path, lock);
443 done:
444 if (!ref_is_absolute && !ref_is_well_known)
445 free(absname);
446 free(path);
447 return err;
450 const struct got_error *
451 got_ref_open(struct got_reference **ref, struct got_repository *repo,
452 const char *refname, int lock)
454 const struct got_error *err = NULL;
455 char *path_refs = NULL;
456 const char *subdirs[] = {
457 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
458 };
459 size_t i;
460 int well_known = is_well_known_ref(refname);
461 struct got_lockfile *lf = NULL;
463 *ref = NULL;
465 path_refs = get_refs_dir_path(repo, refname);
466 if (path_refs == NULL) {
467 err = got_error_from_errno2("get_refs_dir_path", refname);
468 goto done;
471 if (well_known) {
472 err = open_ref(ref, path_refs, "", refname, lock);
473 } else {
474 char *packed_refs_path;
475 FILE *f;
477 /* Search on-disk refs before packed refs! */
478 for (i = 0; i < nitems(subdirs); i++) {
479 err = open_ref(ref, path_refs, subdirs[i], refname,
480 lock);
481 if ((err && err->code != GOT_ERR_NOT_REF) || *ref)
482 goto done;
485 packed_refs_path = got_repo_get_path_packed_refs(repo);
486 if (packed_refs_path == NULL) {
487 err = got_error_from_errno(
488 "got_repo_get_path_packed_refs");
489 goto done;
492 if (lock) {
493 err = got_lockfile_lock(&lf, packed_refs_path, -1);
494 if (err)
495 goto done;
497 f = fopen(packed_refs_path, "rb");
498 free(packed_refs_path);
499 if (f != NULL) {
500 struct stat sb;
501 if (fstat(fileno(f), &sb) == -1) {
502 err = got_error_from_errno2("fstat",
503 packed_refs_path);
504 goto done;
506 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
507 refname, sb.st_mtime);
508 if (!err) {
509 if (fclose(f) == EOF) {
510 err = got_error_from_errno("fclose");
511 got_ref_close(*ref);
512 *ref = NULL;
513 } else if (*ref)
514 (*ref)->lf = lf;
518 done:
519 if (!err && *ref == NULL)
520 err = got_error_not_ref(refname);
521 if (err && lf)
522 got_lockfile_unlock(lf, -1);
523 free(path_refs);
524 return err;
527 void
528 got_ref_close(struct got_reference *ref)
530 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
531 free(ref->ref.symref.name);
532 free(ref->ref.symref.ref);
533 } else
534 free(ref->ref.ref.name);
535 free(ref);
538 struct got_reference *
539 got_ref_dup(struct got_reference *ref)
541 struct got_reference *ret;
543 ret = calloc(1, sizeof(*ret));
544 if (ret == NULL)
545 return NULL;
547 ret->flags = ref->flags;
548 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
549 ret->ref.symref.name = strdup(ref->ref.symref.name);
550 if (ret->ref.symref.name == NULL) {
551 free(ret);
552 return NULL;
554 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
555 if (ret->ref.symref.ref == NULL) {
556 free(ret->ref.symref.name);
557 free(ret);
558 return NULL;
560 } else {
561 ret->ref.ref.name = strdup(ref->ref.ref.name);
562 if (ret->ref.ref.name == NULL) {
563 free(ret);
564 return NULL;
566 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
567 sizeof(ret->ref.ref.sha1));
570 return ret;
573 const struct got_error *
574 got_reflist_entry_dup(struct got_reflist_entry **newp,
575 struct got_reflist_entry *re)
577 const struct got_error *err = NULL;
578 struct got_reflist_entry *new;
580 *newp = NULL;
582 new = malloc(sizeof(*new));
583 if (new == NULL)
584 return got_error_from_errno("malloc");
586 new->ref = got_ref_dup(re->ref);
587 if (new->ref == NULL) {
588 err = got_error_from_errno("got_ref_dup");
589 free(new);
590 return err;
593 *newp = new;
594 return NULL;
597 const struct got_error *
598 got_ref_resolve_symbolic(struct got_reference **resolved,
599 struct got_repository *repo, struct got_reference *ref)
601 struct got_reference *nextref;
602 const struct got_error *err;
604 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
605 if (err)
606 return err;
608 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
609 err = got_ref_resolve_symbolic(resolved, repo, nextref);
610 else
611 *resolved = got_ref_dup(nextref);
613 got_ref_close(nextref);
614 return err;
617 static const struct got_error *
618 ref_resolve(struct got_object_id **id, struct got_repository *repo,
619 struct got_reference *ref, int recursion)
621 const struct got_error *err;
623 if (recursion <= 0)
624 return got_error_msg(GOT_ERR_RECURSION,
625 "reference recursion limit reached");
627 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
628 struct got_reference *resolved = NULL;
629 err = got_ref_resolve_symbolic(&resolved, repo, ref);
630 if (err == NULL)
631 err = ref_resolve(id, repo, resolved, --recursion);
632 if (resolved)
633 got_ref_close(resolved);
634 return err;
637 *id = calloc(1, sizeof(**id));
638 if (*id == NULL)
639 return got_error_from_errno("calloc");
640 memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
641 return NULL;
644 const struct got_error *
645 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
646 struct got_reference *ref)
648 return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
651 char *
652 got_ref_to_str(struct got_reference *ref)
654 char *str;
656 if (ref->flags & GOT_REF_IS_SYMBOLIC)
657 return strdup(ref->ref.symref.ref);
659 str = malloc(SHA1_DIGEST_STRING_LENGTH);
660 if (str == NULL)
661 return NULL;
663 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
664 SHA1_DIGEST_STRING_LENGTH) == NULL) {
665 free(str);
666 return NULL;
669 return str;
672 const char *
673 got_ref_get_name(struct got_reference *ref)
675 if (ref->flags & GOT_REF_IS_SYMBOLIC)
676 return ref->ref.symref.name;
678 return ref->ref.ref.name;
681 const char *
682 got_ref_get_symref_target(struct got_reference *ref)
684 if (ref->flags & GOT_REF_IS_SYMBOLIC)
685 return ref->ref.symref.ref;
687 return NULL;
690 time_t
691 got_ref_get_mtime(struct got_reference *ref)
693 return ref->mtime;
696 const struct got_error *
697 got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
698 struct got_reference* re2)
700 const char *name1 = got_ref_get_name(re1);
701 const char *name2 = got_ref_get_name(re2);
703 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
704 return NULL;
707 const struct got_error *
708 got_ref_cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
709 struct got_reference *ref2)
711 const struct got_error *err = NULL;
712 struct got_repository *repo = arg;
713 struct got_object_id *id1, *id2 = NULL;
714 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
715 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
716 time_t time1, time2;
718 *cmp = 0;
720 err = got_ref_resolve(&id1, repo, ref1);
721 if (err)
722 return err;
723 err = got_object_open_as_tag(&tag1, repo, id1);
724 if (err) {
725 if (err->code != GOT_ERR_OBJ_TYPE)
726 goto done;
727 /* "lightweight" tag */
728 err = got_object_open_as_commit(&commit1, repo, id1);
729 if (err)
730 goto done;
731 time1 = got_object_commit_get_committer_time(commit1);
732 } else
733 time1 = got_object_tag_get_tagger_time(tag1);
735 err = got_ref_resolve(&id2, repo, ref2);
736 if (err)
737 goto done;
738 err = got_object_open_as_tag(&tag2, repo, id2);
739 if (err) {
740 if (err->code != GOT_ERR_OBJ_TYPE)
741 goto done;
742 /* "lightweight" tag */
743 err = got_object_open_as_commit(&commit2, repo, id2);
744 if (err)
745 goto done;
746 time2 = got_object_commit_get_committer_time(commit2);
747 } else
748 time2 = got_object_tag_get_tagger_time(tag2);
750 /* Put latest tags first. */
751 if (time1 < time2)
752 *cmp = 1;
753 else if (time1 > time2)
754 *cmp = -1;
755 else
756 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
757 done:
758 free(id1);
759 free(id2);
760 if (tag1)
761 got_object_tag_close(tag1);
762 if (tag2)
763 got_object_tag_close(tag2);
764 if (commit1)
765 got_object_commit_close(commit1);
766 if (commit2)
767 got_object_commit_close(commit2);
768 return err;
771 const struct got_error *
772 got_ref_cmp_by_commit_timestamp_descending(void *arg, int *cmp,
773 struct got_reference *ref1, struct got_reference *ref2)
775 const struct got_error *err;
776 struct got_repository *repo = arg;
777 struct got_object_id *id1 = NULL, *id2 = NULL;
778 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
780 *cmp = 0;
782 if (ref1->committer_time == 0) {
783 err = got_ref_resolve(&id1, repo, ref1);
784 if (err)
785 return err;
786 err = got_object_open_as_commit(&commit1, repo, id1);
787 if (err)
788 goto done;
789 ref1->committer_time =
790 got_object_commit_get_committer_time(commit1);
793 if (ref2->committer_time == 0) {
794 err = got_ref_resolve(&id2, repo, ref2);
795 if (err)
796 return err;
797 err = got_object_open_as_commit(&commit2, repo, id2);
798 if (err)
799 goto done;
800 ref2->committer_time =
801 got_object_commit_get_committer_time(commit2);
804 if (ref1->committer_time < ref2->committer_time)
805 *cmp = 1;
806 else if (ref2->committer_time < ref1->committer_time)
807 *cmp = -1;
808 done:
809 free(id1);
810 free(id2);
811 if (commit1)
812 got_object_commit_close(commit1);
813 if (commit2)
814 got_object_commit_close(commit2);
815 return err;
818 const struct got_error *
819 got_reflist_insert(struct got_reflist_entry **newp, struct got_reflist_head *refs,
820 struct got_reference *ref, got_ref_cmp_cb cmp_cb, void *cmp_arg)
822 const struct got_error *err;
823 struct got_reflist_entry *new, *re;
824 int cmp;
826 *newp = NULL;
828 new = malloc(sizeof(*new));
829 if (new == NULL)
830 return got_error_from_errno("malloc");
831 new->ref = ref;
832 *newp = new;
834 /*
835 * We must de-duplicate entries on insert because packed-refs may
836 * contain redundant entries. On-disk refs take precedence.
837 * This code assumes that on-disk revs are read before packed-refs.
838 * We're iterating the list anyway, so insert elements sorted by name.
840 * Many callers will provide paths in a somewhat sorted order.
841 * Iterating backwards from the tail of the list should be more
842 * efficient than traversing through the entire list each time
843 * an element is inserted.
844 */
845 re = TAILQ_LAST(refs, got_reflist_head);
846 while (re) {
847 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
848 if (err)
849 return err;
850 if (cmp == 0) {
851 /* duplicate */
852 free(new);
853 *newp = NULL;
854 return NULL;
855 } else if (cmp < 0) {
856 TAILQ_INSERT_AFTER(refs, re, new, entry);
857 return NULL;
859 re = TAILQ_PREV(re, got_reflist_head, entry);
862 TAILQ_INSERT_HEAD(refs, new, entry);
863 return NULL;
866 static const struct got_error *
867 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
868 const char *subdir, struct got_repository *repo,
869 got_ref_cmp_cb cmp_cb, void *cmp_arg)
871 const struct got_error *err = NULL;
872 DIR *d = NULL;
873 char *path_subdir;
875 while (subdir[0] == '/')
876 subdir++;
878 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
879 return got_error_from_errno("asprintf");
881 d = opendir(path_subdir);
882 if (d == NULL)
883 goto done;
885 for (;;) {
886 struct dirent *dent;
887 struct got_reference *ref;
888 char *child;
889 int type;
891 dent = readdir(d);
892 if (dent == NULL)
893 break;
895 if (strcmp(dent->d_name, ".") == 0 ||
896 strcmp(dent->d_name, "..") == 0)
897 continue;
899 err = got_path_dirent_type(&type, path_subdir, dent);
900 if (err)
901 break;
903 switch (type) {
904 case DT_REG:
905 err = open_ref(&ref, path_refs, subdir, dent->d_name,
906 0);
907 if (err)
908 goto done;
909 if (ref) {
910 struct got_reflist_entry *new;
911 err = got_reflist_insert(&new, refs, ref,
912 cmp_cb, cmp_arg);
913 if (err || new == NULL /* duplicate */)
914 got_ref_close(ref);
915 if (err)
916 goto done;
918 break;
919 case DT_DIR:
920 if (asprintf(&child, "%s%s%s", subdir,
921 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
922 err = got_error_from_errno("asprintf");
923 break;
925 err = gather_on_disk_refs(refs, path_refs, child, repo,
926 cmp_cb, cmp_arg);
927 free(child);
928 break;
929 default:
930 break;
933 done:
934 if (d)
935 closedir(d);
936 free(path_subdir);
937 return err;
940 const struct got_error *
941 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
942 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
944 const struct got_error *err;
945 char *packed_refs_path, *path_refs = NULL;
946 char *abs_namespace = NULL;
947 char *buf = NULL, *ondisk_ref_namespace = NULL;
948 char *line = NULL;
949 FILE *f = NULL;
950 struct got_reference *ref;
951 struct got_reflist_entry *new;
953 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
954 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
955 if (path_refs == NULL) {
956 err = got_error_from_errno("get_refs_dir_path");
957 goto done;
959 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
960 if (err)
961 goto done;
962 err = got_reflist_insert(&new, refs, ref, cmp_cb, cmp_arg);
963 if (err || new == NULL /* duplicate */)
964 got_ref_close(ref);
965 if (err && err->code != GOT_ERR_NOT_REF)
966 goto done;
967 } else {
968 /* Try listing a single reference. */
969 const char *refname = ref_namespace;
970 path_refs = get_refs_dir_path(repo, refname);
971 if (path_refs == NULL) {
972 err = got_error_from_errno("get_refs_dir_path");
973 goto done;
975 err = open_ref(&ref, path_refs, "", refname, 0);
976 if (err) {
977 if (err->code != GOT_ERR_NOT_REF)
978 goto done;
979 /* Try to look up references in a given namespace. */
980 } else {
981 err = got_reflist_insert(&new, refs, ref,
982 cmp_cb, cmp_arg);
983 if (err || new == NULL /* duplicate */)
984 got_ref_close(ref);
985 return err;
989 if (ref_namespace) {
990 size_t len;
991 /* Canonicalize the path to eliminate double-slashes if any. */
992 if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
993 err = got_error_from_errno("asprintf");
994 goto done;
996 len = strlen(abs_namespace) + 1;
997 buf = malloc(len);
998 if (buf == NULL) {
999 err = got_error_from_errno("malloc");
1000 goto done;
1002 err = got_canonpath(abs_namespace, buf, len);
1003 if (err)
1004 goto done;
1005 ondisk_ref_namespace = buf;
1006 while (ondisk_ref_namespace[0] == '/')
1007 ondisk_ref_namespace++;
1008 if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
1009 ondisk_ref_namespace += 5;
1010 else if (strcmp(ondisk_ref_namespace, "refs") == 0)
1011 ondisk_ref_namespace = "";
1014 /* Gather on-disk refs before parsing packed-refs. */
1015 free(path_refs);
1016 path_refs = get_refs_dir_path(repo, "");
1017 if (path_refs == NULL) {
1018 err = got_error_from_errno("get_refs_dir_path");
1019 goto done;
1021 err = gather_on_disk_refs(refs, path_refs,
1022 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
1023 cmp_cb, cmp_arg);
1024 if (err)
1025 goto done;
1028 * The packed-refs file may contain redundant entries, in which
1029 * case on-disk refs take precedence.
1031 packed_refs_path = got_repo_get_path_packed_refs(repo);
1032 if (packed_refs_path == NULL) {
1033 err = got_error_from_errno("got_repo_get_path_packed_refs");
1034 goto done;
1037 f = fopen(packed_refs_path, "r");
1038 free(packed_refs_path);
1039 if (f) {
1040 size_t linesize = 0;
1041 ssize_t linelen;
1042 struct stat sb;
1044 if (fstat(fileno(f), &sb) == -1) {
1045 err = got_error_from_errno2("fstat", packed_refs_path);
1046 goto done;
1048 for (;;) {
1049 linelen = getline(&line, &linesize, f);
1050 if (linelen == -1) {
1051 if (feof(f))
1052 break;
1053 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1054 goto done;
1056 if (linelen > 0 && line[linelen - 1] == '\n')
1057 line[linelen - 1] = '\0';
1058 err = parse_packed_ref_line(&ref, NULL, line,
1059 sb.st_mtime);
1060 if (err)
1061 goto done;
1062 if (ref) {
1063 if (ref_namespace) {
1064 const char *name;
1065 name = got_ref_get_name(ref);
1066 if (!got_path_is_child(name,
1067 ref_namespace,
1068 strlen(ref_namespace))) {
1069 got_ref_close(ref);
1070 continue;
1073 err = got_reflist_insert(&new, refs, ref,
1074 cmp_cb, cmp_arg);
1075 if (err || new == NULL /* duplicate */)
1076 got_ref_close(ref);
1077 if (err)
1078 goto done;
1082 done:
1083 free(abs_namespace);
1084 free(buf);
1085 free(line);
1086 free(path_refs);
1087 if (f && fclose(f) == EOF && err == NULL)
1088 err = got_error_from_errno("fclose");
1089 return err;
1092 void
1093 got_ref_list_free(struct got_reflist_head *refs)
1095 struct got_reflist_entry *re;
1097 while ((re = TAILQ_FIRST(refs))) {
1098 TAILQ_REMOVE(refs, re, entry);
1099 got_ref_close(re->ref);
1100 free(re);
1105 int
1106 got_ref_is_symbolic(struct got_reference *ref)
1108 return (ref->flags & GOT_REF_IS_SYMBOLIC);
1111 const struct got_error *
1112 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1114 if (ref->flags & GOT_REF_IS_SYMBOLIC)
1115 return got_error(GOT_ERR_BAD_REF_TYPE);
1117 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
1118 return NULL;
1121 const struct got_error *
1122 got_ref_change_symref(struct got_reference *ref, const char *refname)
1124 char *new_name;
1126 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1127 return got_error(GOT_ERR_BAD_REF_TYPE);
1129 new_name = strdup(refname);
1130 if (new_name == NULL)
1131 return got_error_from_errno("strdup");
1133 free(ref->ref.symref.ref);
1134 ref->ref.symref.ref = new_name;
1135 return NULL;
1138 const struct got_error *
1139 got_ref_change_symref_to_ref(struct got_reference *symref,
1140 struct got_object_id *id)
1142 if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1143 return got_error(GOT_ERR_BAD_REF_TYPE);
1145 symref->ref.ref.name = symref->ref.symref.name;
1146 memcpy(symref->ref.ref.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1147 symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1148 return NULL;
1151 const struct got_error *
1152 got_ref_write(struct got_reference *ref, struct got_repository *repo)
1154 const struct got_error *err = NULL, *unlock_err = NULL;
1155 const char *name = got_ref_get_name(ref);
1156 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1157 struct got_lockfile *lf = NULL;
1158 FILE *f = NULL;
1159 size_t n;
1160 struct stat sb;
1162 path_refs = get_refs_dir_path(repo, name);
1163 if (path_refs == NULL) {
1164 err = got_error_from_errno2("get_refs_dir_path", name);
1165 goto done;
1168 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1169 err = got_error_from_errno("asprintf");
1170 goto done;
1173 err = got_opentemp_named(&tmppath, &f, path);
1174 if (err) {
1175 char *parent;
1176 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1177 goto done;
1178 err = got_path_dirname(&parent, path);
1179 if (err)
1180 goto done;
1181 err = got_path_mkdir(parent);
1182 free(parent);
1183 if (err)
1184 goto done;
1185 err = got_opentemp_named(&tmppath, &f, path);
1186 if (err)
1187 goto done;
1190 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1191 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1192 if (n != strlen(ref->ref.symref.ref) + 6) {
1193 err = got_ferror(f, GOT_ERR_IO);
1194 goto done;
1196 } else {
1197 char hex[SHA1_DIGEST_STRING_LENGTH];
1198 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
1199 sizeof(hex)) == NULL) {
1200 err = got_error(GOT_ERR_BAD_REF_DATA);
1201 goto done;
1203 n = fprintf(f, "%s\n", hex);
1204 if (n != sizeof(hex)) {
1205 err = got_ferror(f, GOT_ERR_IO);
1206 goto done;
1210 if (ref->lf == NULL) {
1211 err = got_lockfile_lock(&lf, path, -1);
1212 if (err)
1213 goto done;
1216 /* XXX: check if old content matches our expectations? */
1218 if (stat(path, &sb) != 0) {
1219 if (errno != ENOENT) {
1220 err = got_error_from_errno2("stat", path);
1221 goto done;
1223 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1226 if (fchmod(fileno(f), sb.st_mode) != 0) {
1227 err = got_error_from_errno2("fchmod", tmppath);
1228 goto done;
1231 if (rename(tmppath, path) != 0) {
1232 err = got_error_from_errno3("rename", tmppath, path);
1233 goto done;
1235 free(tmppath);
1236 tmppath = NULL;
1238 if (stat(path, &sb) == -1) {
1239 err = got_error_from_errno2("stat", path);
1240 goto done;
1242 ref->mtime = sb.st_mtime;
1243 done:
1244 if (ref->lf == NULL && lf)
1245 unlock_err = got_lockfile_unlock(lf, -1);
1246 if (f) {
1247 if (fclose(f) == EOF && err == NULL)
1248 err = got_error_from_errno("fclose");
1250 free(path_refs);
1251 free(path);
1252 if (tmppath) {
1253 if (unlink(tmppath) != 0 && err == NULL)
1254 err = got_error_from_errno2("unlink", tmppath);
1255 free(tmppath);
1257 return err ? err : unlock_err;
1260 static const struct got_error *
1261 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1263 const struct got_error *err = NULL, *unlock_err = NULL;
1264 struct got_lockfile *lf = NULL;
1265 FILE *f = NULL, *tmpf = NULL;
1266 char *line = NULL, *packed_refs_path, *tmppath = NULL;
1267 size_t linesize = 0;
1268 struct got_reflist_head refs;
1269 int found_delref = 0;
1271 /* The packed-refs file does not cotain symbolic references. */
1272 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1273 return got_error(GOT_ERR_BAD_REF_DATA);
1275 TAILQ_INIT(&refs);
1277 packed_refs_path = got_repo_get_path_packed_refs(repo);
1278 if (packed_refs_path == NULL)
1279 return got_error_from_errno("got_repo_get_path_packed_refs");
1281 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
1282 if (err)
1283 goto done;
1285 if (delref->lf == NULL) {
1286 err = got_lockfile_lock(&lf, packed_refs_path, -1);
1287 if (err)
1288 goto done;
1291 f = fopen(packed_refs_path, "r");
1292 if (f == NULL) {
1293 err = got_error_from_errno2("fopen", packed_refs_path);
1294 goto done;
1296 for (;;) {
1297 ssize_t linelen;
1298 struct got_reference *ref;
1299 struct got_reflist_entry *new;
1301 linelen = getline(&line, &linesize, f);
1302 if (linelen == -1) {
1303 if (feof(f))
1304 break;
1305 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1306 goto done;
1308 if (linelen > 0 && line[linelen - 1] == '\n')
1309 line[linelen - 1] = '\0';
1310 err = parse_packed_ref_line(&ref, NULL, line, 0);
1311 if (err)
1312 goto done;
1313 if (ref == NULL)
1314 continue;
1316 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1317 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1318 sizeof(delref->ref.ref.sha1)) == 0) {
1319 found_delref = 1;
1320 got_ref_close(ref);
1321 continue;
1324 err = got_reflist_insert(&new, &refs, ref,
1325 got_ref_cmp_by_name, NULL);
1326 if (err || new == NULL /* duplicate */)
1327 got_ref_close(ref);
1328 if (err)
1329 goto done;
1332 if (found_delref) {
1333 struct got_reflist_entry *re;
1334 size_t n;
1335 struct stat sb;
1337 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1338 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1339 err = got_ferror(f, GOT_ERR_IO);
1340 goto done;
1343 TAILQ_FOREACH(re, &refs, entry) {
1344 char hex[SHA1_DIGEST_STRING_LENGTH];
1346 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1347 sizeof(hex)) == NULL) {
1348 err = got_error(GOT_ERR_BAD_REF_DATA);
1349 goto done;
1351 n = fprintf(tmpf, "%s ", hex);
1352 if (n != sizeof(hex)) {
1353 err = got_ferror(f, GOT_ERR_IO);
1354 goto done;
1356 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1357 if (n != strlen(re->ref->ref.ref.name) + 1) {
1358 err = got_ferror(f, GOT_ERR_IO);
1359 goto done;
1363 if (fflush(tmpf) != 0) {
1364 err = got_error_from_errno("fflush");
1365 goto done;
1368 if (fstat(fileno(f), &sb) != 0) {
1369 if (errno != ENOENT) {
1370 err = got_error_from_errno2("fstat",
1371 packed_refs_path);
1372 goto done;
1374 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1377 if (fchmod(fileno(tmpf), sb.st_mode) != 0) {
1378 err = got_error_from_errno2("fchmod", tmppath);
1379 goto done;
1382 if (rename(tmppath, packed_refs_path) != 0) {
1383 err = got_error_from_errno3("rename", tmppath,
1384 packed_refs_path);
1385 goto done;
1388 done:
1389 if (delref->lf == NULL && lf)
1390 unlock_err = got_lockfile_unlock(lf, -1);
1391 if (f) {
1392 if (fclose(f) == EOF && err == NULL)
1393 err = got_error_from_errno("fclose");
1395 if (tmpf) {
1396 unlink(tmppath);
1397 if (fclose(tmpf) == EOF && err == NULL)
1398 err = got_error_from_errno("fclose");
1400 free(tmppath);
1401 free(packed_refs_path);
1402 free(line);
1403 got_ref_list_free(&refs);
1404 return err ? err : unlock_err;
1407 static const struct got_error *
1408 delete_loose_ref(struct got_reference *ref, struct got_repository *repo)
1410 const struct got_error *err = NULL, *unlock_err = NULL;
1411 const char *name = got_ref_get_name(ref);
1412 char *path_refs = NULL, *path = NULL;
1413 struct got_lockfile *lf = NULL;
1415 path_refs = get_refs_dir_path(repo, name);
1416 if (path_refs == NULL) {
1417 err = got_error_from_errno2("get_refs_dir_path", name);
1418 goto done;
1421 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1422 err = got_error_from_errno("asprintf");
1423 goto done;
1426 if (ref->lf == NULL) {
1427 err = got_lockfile_lock(&lf, path, -1);
1428 if (err)
1429 goto done;
1432 /* XXX: check if old content matches our expectations? */
1434 if (unlink(path) != 0)
1435 err = got_error_from_errno2("unlink", path);
1436 done:
1437 if (ref->lf == NULL && lf)
1438 unlock_err = got_lockfile_unlock(lf, -1);
1440 free(path_refs);
1441 free(path);
1442 return err ? err : unlock_err;
1445 const struct got_error *
1446 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1448 const struct got_error *err = NULL;
1449 struct got_reference *ref2;
1451 if (ref->flags & GOT_REF_IS_PACKED) {
1452 err = delete_packed_ref(ref, repo);
1453 if (err)
1454 return err;
1456 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1457 if (err) {
1458 if (err->code == GOT_ERR_NOT_REF)
1459 return NULL;
1460 return err;
1463 err = delete_loose_ref(ref2, repo);
1464 got_ref_close(ref2);
1465 return err;
1466 } else {
1467 err = delete_loose_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_packed_ref(ref2, repo);
1479 got_ref_close(ref2);
1480 return err;
1484 const struct got_error *
1485 got_ref_unlock(struct got_reference *ref)
1487 const struct got_error *err;
1488 err = got_lockfile_unlock(ref->lf, -1);
1489 ref->lf = NULL;
1490 return err;
1493 struct got_reflist_object_id_map {
1494 struct got_object_idset *idset;
1497 struct got_reflist_object_id_map_entry {
1498 struct got_reflist_head refs;
1501 static const struct got_error *
1502 add_object_id_map_entry(struct got_object_idset *idset,
1503 struct got_object_id *id, struct got_reflist_entry *re)
1505 const struct got_error *err = NULL;
1506 struct got_reflist_object_id_map_entry *ent;
1507 struct got_reflist_entry *new;
1509 ent = got_object_idset_get(idset, id);
1510 if (ent == NULL) {
1511 ent = malloc(sizeof(*ent));
1512 if (ent == NULL)
1513 return got_error_from_errno("malloc");
1515 TAILQ_INIT(&ent->refs);
1516 err = got_object_idset_add(idset, id, ent);
1517 if (err)
1518 return err;
1521 err = got_reflist_entry_dup(&new, re);
1522 if (err)
1523 return err;
1525 TAILQ_INSERT_TAIL(&ent->refs, new, entry);
1526 return NULL;
1529 const struct got_error *
1530 got_reflist_object_id_map_create(struct got_reflist_object_id_map **map,
1531 struct got_reflist_head *refs, struct got_repository *repo)
1533 const struct got_error *err = NULL;
1534 struct got_object_idset *idset;
1535 struct got_object_id *id = NULL;
1536 struct got_reflist_entry *re;
1538 idset = got_object_idset_alloc();
1539 if (idset == NULL)
1540 return got_error_from_errno("got_object_idset_alloc");
1542 *map = malloc(sizeof(**map));
1543 if (*map == NULL) {
1544 got_object_idset_free(idset);
1545 return got_error_from_errno("malloc");
1547 (*map)->idset = idset;
1549 TAILQ_FOREACH(re, refs, entry) {
1550 struct got_tag_object *tag = NULL;
1552 err = got_ref_resolve(&id, repo, re->ref);
1553 if (err)
1554 goto done;
1556 err = add_object_id_map_entry(idset, id, re);
1557 if (err)
1558 goto done;
1560 if (strstr(got_ref_get_name(re->ref), "/tags/") == NULL) {
1561 free(id);
1562 id = NULL;
1563 continue;
1566 err = got_object_open_as_tag(&tag, repo, id);
1567 if (err) {
1568 if (err->code != GOT_ERR_OBJ_TYPE)
1569 goto done;
1570 /* Ref points at something other than a tag. */
1571 err = NULL;
1572 tag = NULL;
1573 free(id);
1574 id = NULL;
1575 continue;
1578 err = add_object_id_map_entry(idset,
1579 got_object_tag_get_object_id(tag), re);
1580 got_object_tag_close(tag);
1581 if (err)
1582 goto done;
1584 free(id);
1585 id = NULL;
1587 done:
1588 free(id);
1589 if (err) {
1590 got_reflist_object_id_map_free(*map);
1591 *map = NULL;
1593 return err;
1596 struct got_reflist_head *
1597 got_reflist_object_id_map_lookup(struct got_reflist_object_id_map *map,
1598 struct got_object_id *id)
1600 struct got_reflist_object_id_map_entry *ent;
1601 ent = got_object_idset_get(map->idset, id);
1602 if (ent)
1603 return &ent->refs;
1604 return NULL;
1607 static const struct got_error *
1608 free_id_map_entry(struct got_object_id *id, void *data, void *arg)
1610 struct got_reflist_object_id_map_entry *ent = data;
1612 got_ref_list_free(&ent->refs);
1613 free(ent);
1614 return NULL;
1617 void
1618 got_reflist_object_id_map_free(struct got_reflist_object_id_map *map)
1620 got_object_idset_for_each(map->idset, free_id_map_entry, NULL);
1621 got_object_idset_free(map->idset);
1622 free(map);