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;
90 };
92 static const struct got_error *
93 alloc_ref(struct got_reference **ref, const char *name,
94 struct got_object_id *id, int flags, time_t mtime)
95 {
96 const struct got_error *err = NULL;
98 *ref = calloc(1, sizeof(**ref));
99 if (*ref == NULL)
100 return got_error_from_errno("calloc");
102 memcpy((*ref)->ref.ref.sha1, id->sha1, sizeof((*ref)->ref.ref.sha1));
103 (*ref)->flags = flags;
104 (*ref)->ref.ref.name = strdup(name);
105 (*ref)->mtime = mtime;
106 if ((*ref)->ref.ref.name == NULL) {
107 err = got_error_from_errno("strdup");
108 got_ref_close(*ref);
109 *ref = NULL;
111 return err;
114 static const struct got_error *
115 alloc_symref(struct got_reference **ref, const char *name,
116 const char *target_ref, int flags)
118 const struct got_error *err = NULL;
120 *ref = calloc(1, sizeof(**ref));
121 if (*ref == NULL)
122 return got_error_from_errno("calloc");
124 (*ref)->flags = GOT_REF_IS_SYMBOLIC | flags;
125 (*ref)->ref.symref.name = strdup(name);
126 if ((*ref)->ref.symref.name == NULL) {
127 err = got_error_from_errno("strdup");
128 got_ref_close(*ref);
129 *ref = NULL;
130 return err;
132 (*ref)->ref.symref.ref = strdup(target_ref);
133 if ((*ref)->ref.symref.ref == NULL) {
134 err = got_error_from_errno("strdup");
135 got_ref_close(*ref);
136 *ref = NULL;
138 return err;
141 static const struct got_error *
142 parse_symref(struct got_reference **ref, const char *name, const char *line)
144 if (line[0] == '\0')
145 return got_error(GOT_ERR_BAD_REF_DATA);
147 return alloc_symref(ref, name, line, 0);
150 static const struct got_error *
151 parse_ref_line(struct got_reference **ref, const char *name, const char *line,
152 time_t mtime)
154 struct got_object_id id;
156 if (strncmp(line, "ref: ", 5) == 0) {
157 line += 5;
158 return parse_symref(ref, name, line);
161 if (!got_parse_sha1_digest(id.sha1, line))
162 return got_error(GOT_ERR_BAD_REF_DATA);
164 return alloc_ref(ref, name, &id, 0, mtime);
167 static const struct got_error *
168 parse_ref_file(struct got_reference **ref, const char *name,
169 const char *absname, const char *abspath, int lock)
171 const struct got_error *err = NULL;
172 FILE *f;
173 char *line = NULL;
174 size_t linesize = 0;
175 ssize_t linelen;
176 struct got_lockfile *lf = NULL;
177 struct stat sb;
179 if (lock) {
180 err = got_lockfile_lock(&lf, abspath, -1);
181 if (err) {
182 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
183 err = got_error_not_ref(name);
184 return err;
188 f = fopen(abspath, "rb");
189 if (f == NULL) {
190 if (errno != ENOTDIR && errno != ENOENT)
191 err = got_error_from_errno2("fopen", abspath);
192 else
193 err = got_error_not_ref(name);
194 if (lock)
195 got_lockfile_unlock(lf, -1);
196 return err;
198 if (fstat(fileno(f), &sb) == -1) {
199 err = got_error_from_errno2("fstat", abspath);
200 goto done;
203 linelen = getline(&line, &linesize, f);
204 if (linelen == -1) {
205 if (feof(f))
206 err = NULL; /* ignore empty files (could be locks) */
207 else {
208 if (errno == EISDIR)
209 err = got_error(GOT_ERR_NOT_REF);
210 else if (ferror(f))
211 err = got_ferror(f, GOT_ERR_IO);
212 else
213 err = got_error_from_errno2("getline", abspath);
215 if (lock)
216 got_lockfile_unlock(lf, -1);
217 goto done;
219 while (linelen > 0 && line[linelen - 1] == '\n') {
220 line[linelen - 1] = '\0';
221 linelen--;
224 err = parse_ref_line(ref, absname, line, sb.st_mtime);
225 if (lock) {
226 if (err)
227 got_lockfile_unlock(lf, -1);
228 else {
229 if (*ref)
230 (*ref)->lf = lf;
231 else
232 got_lockfile_unlock(lf, -1);
235 done:
236 free(line);
237 if (fclose(f) == EOF && err == NULL) {
238 err = got_error_from_errno("fclose");
239 if (*ref) {
240 if (lock)
241 got_ref_unlock(*ref);
242 got_ref_close(*ref);
243 *ref = NULL;
246 return err;
249 static int
250 is_well_known_ref(const char *refname)
252 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
253 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
254 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
255 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
258 static char *
259 get_refs_dir_path(struct got_repository *repo, const char *refname)
261 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
262 return strdup(got_repo_get_path_git_dir(repo));
264 return got_repo_get_path_refs(repo);
267 int
268 got_ref_name_is_valid(const char *name)
270 const char *s, *seg;
271 const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
272 const char *forbidden_seq[] = { "//", "..", "@{" };
273 const char *lfs = GOT_LOCKFILE_SUFFIX;
274 const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
275 size_t i;
277 if (name[0] == '@' && name[1] == '\0')
278 return 0;
280 s = name;
281 seg = s;
282 if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
283 return 0;
284 while (*s) {
285 for (i = 0; i < nitems(forbidden); i++) {
286 if (*s == forbidden[i])
287 return 0;
289 for (i = 0; i < nitems(forbidden_seq); i++) {
290 if (s[0] == forbidden_seq[i][0] &&
291 s[1] == forbidden_seq[i][1])
292 return 0;
294 if (iscntrl((unsigned char)s[0]))
295 return 0;
296 if (s[0] == '.' && s[1] == '\0')
297 return 0;
298 if (*s == '/') {
299 const char *nextseg = s + 1;
300 if (nextseg[0] == '\0' || nextseg[0] == '.' ||
301 nextseg[0] == '/')
302 return 0;
303 if (seg <= s - lfs_len &&
304 strncmp(s - lfs_len, lfs, lfs_len) == 0)
305 return 0;
306 seg = nextseg;
308 s++;
311 if (seg <= s - lfs_len &&
312 strncmp(s - lfs_len, lfs, lfs_len) == 0)
313 return 0;
315 return 1;
318 const struct got_error *
319 got_ref_alloc(struct got_reference **ref, const char *name,
320 struct got_object_id *id)
322 if (!got_ref_name_is_valid(name))
323 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
325 return alloc_ref(ref, name, id, 0, 0);
328 const struct got_error *
329 got_ref_alloc_symref(struct got_reference **ref, const char *name,
330 struct got_reference *target_ref)
332 if (!got_ref_name_is_valid(name))
333 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
335 return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
338 static const struct got_error *
339 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
340 const char *line, time_t mtime)
342 struct got_object_id id;
343 const char *name;
345 *ref = NULL;
347 if (line[0] == '#' || line[0] == '^')
348 return NULL;
350 if (!got_parse_sha1_digest(id.sha1, line))
351 return got_error(GOT_ERR_BAD_REF_DATA);
353 if (abs_refname) {
354 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
355 return NULL;
356 name = abs_refname;
357 } else
358 name = line + SHA1_DIGEST_STRING_LENGTH;
360 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED, mtime);
363 static const struct got_error *
364 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
365 int nsubdirs, const char *refname, time_t mtime)
367 const struct got_error *err = NULL;
368 char *abs_refname;
369 char *line = NULL;
370 size_t linesize = 0;
371 ssize_t linelen;
372 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
374 *ref = NULL;
376 if (ref_is_absolute)
377 abs_refname = (char *)refname;
378 do {
379 linelen = getline(&line, &linesize, f);
380 if (linelen == -1) {
381 if (feof(f))
382 break;
383 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
384 break;
386 if (linelen > 0 && line[linelen - 1] == '\n')
387 line[linelen - 1] = '\0';
388 for (i = 0; i < nsubdirs; i++) {
389 if (!ref_is_absolute &&
390 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
391 refname) == -1)
392 return got_error_from_errno("asprintf");
393 err = parse_packed_ref_line(ref, abs_refname, line,
394 mtime);
395 if (!ref_is_absolute)
396 free(abs_refname);
397 if (err || *ref != NULL)
398 break;
400 if (err)
401 break;
402 } while (*ref == NULL);
403 free(line);
405 return err;
408 static const struct got_error *
409 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
410 const char *name, int lock)
412 const struct got_error *err = NULL;
413 char *path = NULL;
414 char *absname = NULL;
415 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
416 int ref_is_well_known = (subdir[0] == '\0' && is_well_known_ref(name));
418 *ref = NULL;
420 if (!got_ref_name_is_valid(name))
421 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
423 if (ref_is_absolute || ref_is_well_known) {
424 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
425 return got_error_from_errno("asprintf");
426 absname = (char *)name;
427 } else {
428 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
429 subdir[0] ? "/" : "", name) == -1)
430 return got_error_from_errno("asprintf");
432 if (asprintf(&absname, "refs/%s%s%s",
433 subdir, subdir[0] ? "/" : "", name) == -1) {
434 err = got_error_from_errno("asprintf");
435 goto done;
439 err = parse_ref_file(ref, name, absname, path, lock);
440 done:
441 if (!ref_is_absolute && !ref_is_well_known)
442 free(absname);
443 free(path);
444 return err;
447 const struct got_error *
448 got_ref_open(struct got_reference **ref, struct got_repository *repo,
449 const char *refname, int lock)
451 const struct got_error *err = NULL;
452 char *path_refs = NULL;
453 const char *subdirs[] = {
454 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
455 };
456 size_t i;
457 int well_known = is_well_known_ref(refname);
458 struct got_lockfile *lf = NULL;
460 *ref = NULL;
462 path_refs = get_refs_dir_path(repo, refname);
463 if (path_refs == NULL) {
464 err = got_error_from_errno2("get_refs_dir_path", refname);
465 goto done;
468 if (well_known) {
469 err = open_ref(ref, path_refs, "", refname, lock);
470 } else {
471 char *packed_refs_path;
472 FILE *f;
474 /* Search on-disk refs before packed refs! */
475 for (i = 0; i < nitems(subdirs); i++) {
476 err = open_ref(ref, path_refs, subdirs[i], refname,
477 lock);
478 if ((err && err->code != GOT_ERR_NOT_REF) || *ref)
479 goto done;
482 packed_refs_path = got_repo_get_path_packed_refs(repo);
483 if (packed_refs_path == NULL) {
484 err = got_error_from_errno(
485 "got_repo_get_path_packed_refs");
486 goto done;
489 if (lock) {
490 err = got_lockfile_lock(&lf, packed_refs_path, -1);
491 if (err)
492 goto done;
494 f = fopen(packed_refs_path, "rb");
495 free(packed_refs_path);
496 if (f != NULL) {
497 struct stat sb;
498 if (fstat(fileno(f), &sb) == -1) {
499 err = got_error_from_errno2("fstat",
500 packed_refs_path);
501 goto done;
503 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
504 refname, sb.st_mtime);
505 if (!err) {
506 if (fclose(f) == EOF) {
507 err = got_error_from_errno("fclose");
508 got_ref_close(*ref);
509 *ref = NULL;
510 } else if (*ref)
511 (*ref)->lf = lf;
515 done:
516 if (!err && *ref == NULL)
517 err = got_error_not_ref(refname);
518 if (err && lf)
519 got_lockfile_unlock(lf, -1);
520 free(path_refs);
521 return err;
524 void
525 got_ref_close(struct got_reference *ref)
527 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
528 free(ref->ref.symref.name);
529 free(ref->ref.symref.ref);
530 } else
531 free(ref->ref.ref.name);
532 free(ref);
535 struct got_reference *
536 got_ref_dup(struct got_reference *ref)
538 struct got_reference *ret;
540 ret = calloc(1, sizeof(*ret));
541 if (ret == NULL)
542 return NULL;
544 ret->flags = ref->flags;
545 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
546 ret->ref.symref.name = strdup(ref->ref.symref.name);
547 if (ret->ref.symref.name == NULL) {
548 free(ret);
549 return NULL;
551 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
552 if (ret->ref.symref.ref == NULL) {
553 free(ret->ref.symref.name);
554 free(ret);
555 return NULL;
557 } else {
558 ret->ref.ref.name = strdup(ref->ref.ref.name);
559 if (ret->ref.ref.name == NULL) {
560 free(ret);
561 return NULL;
563 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
564 sizeof(ret->ref.ref.sha1));
567 return ret;
570 const struct got_error *
571 got_reflist_entry_dup(struct got_reflist_entry **newp,
572 struct got_reflist_entry *re)
574 const struct got_error *err = NULL;
575 struct got_reflist_entry *new;
577 *newp = NULL;
579 new = malloc(sizeof(*new));
580 if (new == NULL)
581 return got_error_from_errno("malloc");
583 new->ref = got_ref_dup(re->ref);
584 if (new->ref == NULL) {
585 err = got_error_from_errno("got_ref_dup");
586 free(new);
587 return err;
590 *newp = new;
591 return NULL;
594 const struct got_error *
595 got_ref_resolve_symbolic(struct got_reference **resolved,
596 struct got_repository *repo, struct got_reference *ref)
598 struct got_reference *nextref;
599 const struct got_error *err;
601 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
602 if (err)
603 return err;
605 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
606 err = got_ref_resolve_symbolic(resolved, repo, nextref);
607 else
608 *resolved = got_ref_dup(nextref);
610 got_ref_close(nextref);
611 return err;
614 static const struct got_error *
615 ref_resolve(struct got_object_id **id, struct got_repository *repo,
616 struct got_reference *ref, int recursion)
618 const struct got_error *err;
620 if (recursion <= 0)
621 return got_error_msg(GOT_ERR_RECURSION,
622 "reference recursion limit reached");
624 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
625 struct got_reference *resolved = NULL;
626 err = got_ref_resolve_symbolic(&resolved, repo, ref);
627 if (err == NULL)
628 err = ref_resolve(id, repo, resolved, --recursion);
629 if (resolved)
630 got_ref_close(resolved);
631 return err;
634 *id = calloc(1, sizeof(**id));
635 if (*id == NULL)
636 return got_error_from_errno("calloc");
637 memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
638 return NULL;
641 const struct got_error *
642 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
643 struct got_reference *ref)
645 return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
648 char *
649 got_ref_to_str(struct got_reference *ref)
651 char *str;
653 if (ref->flags & GOT_REF_IS_SYMBOLIC)
654 return strdup(ref->ref.symref.ref);
656 str = malloc(SHA1_DIGEST_STRING_LENGTH);
657 if (str == NULL)
658 return NULL;
660 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
661 SHA1_DIGEST_STRING_LENGTH) == NULL) {
662 free(str);
663 return NULL;
666 return str;
669 const char *
670 got_ref_get_name(struct got_reference *ref)
672 if (ref->flags & GOT_REF_IS_SYMBOLIC)
673 return ref->ref.symref.name;
675 return ref->ref.ref.name;
678 const char *
679 got_ref_get_symref_target(struct got_reference *ref)
681 if (ref->flags & GOT_REF_IS_SYMBOLIC)
682 return ref->ref.symref.ref;
684 return NULL;
687 time_t
688 got_ref_get_mtime(struct got_reference *ref)
690 return ref->mtime;
693 const struct got_error *
694 got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
695 struct got_reference* re2)
697 const char *name1 = got_ref_get_name(re1);
698 const char *name2 = got_ref_get_name(re2);
700 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
701 return NULL;
704 const struct got_error *
705 got_ref_cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
706 struct got_reference *ref2)
708 const struct got_error *err = NULL;
709 struct got_repository *repo = arg;
710 struct got_object_id *id1, *id2 = NULL;
711 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
712 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
713 time_t time1, time2;
715 *cmp = 0;
717 err = got_ref_resolve(&id1, repo, ref1);
718 if (err)
719 return err;
720 err = got_object_open_as_tag(&tag1, repo, id1);
721 if (err) {
722 if (err->code != GOT_ERR_OBJ_TYPE)
723 goto done;
724 /* "lightweight" tag */
725 err = got_object_open_as_commit(&commit1, repo, id1);
726 if (err)
727 goto done;
728 time1 = got_object_commit_get_committer_time(commit1);
729 } else
730 time1 = got_object_tag_get_tagger_time(tag1);
732 err = got_ref_resolve(&id2, repo, ref2);
733 if (err)
734 goto done;
735 err = got_object_open_as_tag(&tag2, repo, id2);
736 if (err) {
737 if (err->code != GOT_ERR_OBJ_TYPE)
738 goto done;
739 /* "lightweight" tag */
740 err = got_object_open_as_commit(&commit2, repo, id2);
741 if (err)
742 goto done;
743 time2 = got_object_commit_get_committer_time(commit2);
744 } else
745 time2 = got_object_tag_get_tagger_time(tag2);
747 /* Put latest tags first. */
748 if (time1 < time2)
749 *cmp = 1;
750 else if (time1 > time2)
751 *cmp = -1;
752 else
753 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
754 done:
755 free(id1);
756 free(id2);
757 if (tag1)
758 got_object_tag_close(tag1);
759 if (tag2)
760 got_object_tag_close(tag2);
761 if (commit1)
762 got_object_commit_close(commit1);
763 if (commit2)
764 got_object_commit_close(commit2);
765 return err;
768 const struct got_error *
769 got_ref_cmp_by_commit_timestamp_descending(void *arg, int *cmp,
770 struct got_reference *ref1, struct got_reference *ref2)
772 const struct got_error *err;
773 struct got_repository *repo = arg;
774 struct got_object_id *id1, *id2 = NULL;
775 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
776 time_t time1, time2;
778 *cmp = 0;
780 err = got_ref_resolve(&id1, repo, ref1);
781 if (err)
782 return err;
783 err = got_ref_resolve(&id2, repo, ref2);
784 if (err)
785 goto done;
787 err = got_object_open_as_commit(&commit1, repo, id1);
788 if (err)
789 goto done;
790 err = got_object_open_as_commit(&commit2, repo, id2);
791 if (err)
792 goto done;
794 time1 = got_object_commit_get_committer_time(commit1);
795 time2 = got_object_commit_get_committer_time(commit2);
796 if (time1 < time2)
797 *cmp = 1;
798 else if (time2 < time1)
799 *cmp = -1;
800 done:
801 free(id1);
802 free(id2);
803 if (commit1)
804 got_object_commit_close(commit1);
805 if (commit2)
806 got_object_commit_close(commit2);
807 return err;
810 const struct got_error *
811 got_reflist_insert(struct got_reflist_entry **newp, struct got_reflist_head *refs,
812 struct got_reference *ref, got_ref_cmp_cb cmp_cb, void *cmp_arg)
814 const struct got_error *err;
815 struct got_reflist_entry *new, *re;
816 int cmp;
818 *newp = NULL;
820 new = malloc(sizeof(*new));
821 if (new == NULL)
822 return got_error_from_errno("malloc");
823 new->ref = ref;
824 *newp = new;
826 /*
827 * We must de-duplicate entries on insert because packed-refs may
828 * contain redundant entries. On-disk refs take precedence.
829 * This code assumes that on-disk revs are read before packed-refs.
830 * We're iterating the list anyway, so insert elements sorted by name.
832 * Many callers will provide paths in a somewhat sorted order.
833 * Iterating backwards from the tail of the list should be more
834 * efficient than traversing through the entire list each time
835 * an element is inserted.
836 */
837 re = TAILQ_LAST(refs, got_reflist_head);
838 while (re) {
839 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
840 if (err)
841 return err;
842 if (cmp == 0) {
843 /* duplicate */
844 free(new);
845 *newp = NULL;
846 return NULL;
847 } else if (cmp < 0) {
848 TAILQ_INSERT_AFTER(refs, re, new, entry);
849 return NULL;
851 re = TAILQ_PREV(re, got_reflist_head, entry);
854 TAILQ_INSERT_HEAD(refs, new, entry);
855 return NULL;
858 static const struct got_error *
859 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
860 const char *subdir, struct got_repository *repo,
861 got_ref_cmp_cb cmp_cb, void *cmp_arg)
863 const struct got_error *err = NULL;
864 DIR *d = NULL;
865 char *path_subdir;
867 while (subdir[0] == '/')
868 subdir++;
870 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
871 return got_error_from_errno("asprintf");
873 d = opendir(path_subdir);
874 if (d == NULL)
875 goto done;
877 for (;;) {
878 struct dirent *dent;
879 struct got_reference *ref;
880 char *child;
881 int type;
883 dent = readdir(d);
884 if (dent == NULL)
885 break;
887 if (strcmp(dent->d_name, ".") == 0 ||
888 strcmp(dent->d_name, "..") == 0)
889 continue;
891 err = got_path_dirent_type(&type, path_subdir, dent);
892 if (err)
893 break;
895 switch (type) {
896 case DT_REG:
897 err = open_ref(&ref, path_refs, subdir, dent->d_name,
898 0);
899 if (err)
900 goto done;
901 if (ref) {
902 struct got_reflist_entry *new;
903 err = got_reflist_insert(&new, refs, ref,
904 cmp_cb, cmp_arg);
905 if (err || new == NULL /* duplicate */)
906 got_ref_close(ref);
907 if (err)
908 goto done;
910 break;
911 case DT_DIR:
912 if (asprintf(&child, "%s%s%s", subdir,
913 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
914 err = got_error_from_errno("asprintf");
915 break;
917 err = gather_on_disk_refs(refs, path_refs, child, repo,
918 cmp_cb, cmp_arg);
919 free(child);
920 break;
921 default:
922 break;
925 done:
926 if (d)
927 closedir(d);
928 free(path_subdir);
929 return err;
932 const struct got_error *
933 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
934 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
936 const struct got_error *err;
937 char *packed_refs_path, *path_refs = NULL;
938 char *abs_namespace = NULL;
939 char *buf = NULL, *ondisk_ref_namespace = NULL;
940 char *line = NULL;
941 FILE *f = NULL;
942 struct got_reference *ref;
943 struct got_reflist_entry *new;
945 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
946 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
947 if (path_refs == NULL) {
948 err = got_error_from_errno("get_refs_dir_path");
949 goto done;
951 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
952 if (err)
953 goto done;
954 err = got_reflist_insert(&new, refs, ref, cmp_cb, cmp_arg);
955 if (err || new == NULL /* duplicate */)
956 got_ref_close(ref);
957 if (err && err->code != GOT_ERR_NOT_REF)
958 goto done;
959 } else {
960 /* Try listing a single reference. */
961 const char *refname = ref_namespace;
962 path_refs = get_refs_dir_path(repo, refname);
963 if (path_refs == NULL) {
964 err = got_error_from_errno("get_refs_dir_path");
965 goto done;
967 err = open_ref(&ref, path_refs, "", refname, 0);
968 if (err) {
969 if (err->code != GOT_ERR_NOT_REF)
970 goto done;
971 /* Try to look up references in a given namespace. */
972 } else {
973 err = got_reflist_insert(&new, refs, ref,
974 cmp_cb, cmp_arg);
975 if (err || new == NULL /* duplicate */)
976 got_ref_close(ref);
977 return err;
981 if (ref_namespace) {
982 size_t len;
983 /* Canonicalize the path to eliminate double-slashes if any. */
984 if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
985 err = got_error_from_errno("asprintf");
986 goto done;
988 len = strlen(abs_namespace) + 1;
989 buf = malloc(len);
990 if (buf == NULL) {
991 err = got_error_from_errno("malloc");
992 goto done;
994 err = got_canonpath(abs_namespace, buf, len);
995 if (err)
996 goto done;
997 ondisk_ref_namespace = buf;
998 while (ondisk_ref_namespace[0] == '/')
999 ondisk_ref_namespace++;
1000 if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
1001 ondisk_ref_namespace += 5;
1002 else if (strcmp(ondisk_ref_namespace, "refs") == 0)
1003 ondisk_ref_namespace = "";
1006 /* Gather on-disk refs before parsing packed-refs. */
1007 free(path_refs);
1008 path_refs = get_refs_dir_path(repo, "");
1009 if (path_refs == NULL) {
1010 err = got_error_from_errno("get_refs_dir_path");
1011 goto done;
1013 err = gather_on_disk_refs(refs, path_refs,
1014 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
1015 cmp_cb, cmp_arg);
1016 if (err)
1017 goto done;
1020 * The packed-refs file may contain redundant entries, in which
1021 * case on-disk refs take precedence.
1023 packed_refs_path = got_repo_get_path_packed_refs(repo);
1024 if (packed_refs_path == NULL) {
1025 err = got_error_from_errno("got_repo_get_path_packed_refs");
1026 goto done;
1029 f = fopen(packed_refs_path, "r");
1030 free(packed_refs_path);
1031 if (f) {
1032 size_t linesize = 0;
1033 ssize_t linelen;
1034 struct stat sb;
1036 if (fstat(fileno(f), &sb) == -1) {
1037 err = got_error_from_errno2("fstat", packed_refs_path);
1038 goto done;
1040 for (;;) {
1041 linelen = getline(&line, &linesize, f);
1042 if (linelen == -1) {
1043 if (feof(f))
1044 break;
1045 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1046 goto done;
1048 if (linelen > 0 && line[linelen - 1] == '\n')
1049 line[linelen - 1] = '\0';
1050 err = parse_packed_ref_line(&ref, NULL, line,
1051 sb.st_mtime);
1052 if (err)
1053 goto done;
1054 if (ref) {
1055 if (ref_namespace) {
1056 const char *name;
1057 name = got_ref_get_name(ref);
1058 if (!got_path_is_child(name,
1059 ref_namespace,
1060 strlen(ref_namespace))) {
1061 got_ref_close(ref);
1062 continue;
1065 err = got_reflist_insert(&new, refs, ref,
1066 cmp_cb, cmp_arg);
1067 if (err || new == NULL /* duplicate */)
1068 got_ref_close(ref);
1069 if (err)
1070 goto done;
1074 done:
1075 free(abs_namespace);
1076 free(buf);
1077 free(line);
1078 free(path_refs);
1079 if (f && fclose(f) == EOF && err == NULL)
1080 err = got_error_from_errno("fclose");
1081 return err;
1084 void
1085 got_ref_list_free(struct got_reflist_head *refs)
1087 struct got_reflist_entry *re;
1089 while ((re = TAILQ_FIRST(refs))) {
1090 TAILQ_REMOVE(refs, re, entry);
1091 got_ref_close(re->ref);
1092 free(re);
1097 int
1098 got_ref_is_symbolic(struct got_reference *ref)
1100 return (ref->flags & GOT_REF_IS_SYMBOLIC);
1103 const struct got_error *
1104 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1106 if (ref->flags & GOT_REF_IS_SYMBOLIC)
1107 return got_error(GOT_ERR_BAD_REF_TYPE);
1109 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
1110 return NULL;
1113 const struct got_error *
1114 got_ref_change_symref(struct got_reference *ref, const char *refname)
1116 char *new_name;
1118 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1119 return got_error(GOT_ERR_BAD_REF_TYPE);
1121 new_name = strdup(refname);
1122 if (new_name == NULL)
1123 return got_error_from_errno("strdup");
1125 free(ref->ref.symref.ref);
1126 ref->ref.symref.ref = new_name;
1127 return NULL;
1130 const struct got_error *
1131 got_ref_change_symref_to_ref(struct got_reference *symref,
1132 struct got_object_id *id)
1134 if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1135 return got_error(GOT_ERR_BAD_REF_TYPE);
1137 symref->ref.ref.name = symref->ref.symref.name;
1138 memcpy(symref->ref.ref.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1139 symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1140 return NULL;
1143 const struct got_error *
1144 got_ref_write(struct got_reference *ref, struct got_repository *repo)
1146 const struct got_error *err = NULL, *unlock_err = NULL;
1147 const char *name = got_ref_get_name(ref);
1148 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1149 struct got_lockfile *lf = NULL;
1150 FILE *f = NULL;
1151 size_t n;
1152 struct stat sb;
1154 path_refs = get_refs_dir_path(repo, name);
1155 if (path_refs == NULL) {
1156 err = got_error_from_errno2("get_refs_dir_path", name);
1157 goto done;
1160 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1161 err = got_error_from_errno("asprintf");
1162 goto done;
1165 err = got_opentemp_named(&tmppath, &f, path);
1166 if (err) {
1167 char *parent;
1168 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1169 goto done;
1170 err = got_path_dirname(&parent, path);
1171 if (err)
1172 goto done;
1173 err = got_path_mkdir(parent);
1174 free(parent);
1175 if (err)
1176 goto done;
1177 err = got_opentemp_named(&tmppath, &f, path);
1178 if (err)
1179 goto done;
1182 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1183 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1184 if (n != strlen(ref->ref.symref.ref) + 6) {
1185 err = got_ferror(f, GOT_ERR_IO);
1186 goto done;
1188 } else {
1189 char hex[SHA1_DIGEST_STRING_LENGTH];
1190 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
1191 sizeof(hex)) == NULL) {
1192 err = got_error(GOT_ERR_BAD_REF_DATA);
1193 goto done;
1195 n = fprintf(f, "%s\n", hex);
1196 if (n != sizeof(hex)) {
1197 err = got_ferror(f, GOT_ERR_IO);
1198 goto done;
1202 if (ref->lf == NULL) {
1203 err = got_lockfile_lock(&lf, path, -1);
1204 if (err)
1205 goto done;
1208 /* XXX: check if old content matches our expectations? */
1210 if (stat(path, &sb) != 0) {
1211 if (errno != ENOENT) {
1212 err = got_error_from_errno2("stat", path);
1213 goto done;
1215 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1218 if (fchmod(fileno(f), sb.st_mode) != 0) {
1219 err = got_error_from_errno2("fchmod", tmppath);
1220 goto done;
1223 if (rename(tmppath, path) != 0) {
1224 err = got_error_from_errno3("rename", tmppath, path);
1225 goto done;
1227 free(tmppath);
1228 tmppath = NULL;
1230 if (stat(path, &sb) == -1) {
1231 err = got_error_from_errno2("stat", path);
1232 goto done;
1234 ref->mtime = sb.st_mtime;
1235 done:
1236 if (ref->lf == NULL && lf)
1237 unlock_err = got_lockfile_unlock(lf, -1);
1238 if (f) {
1239 if (fclose(f) == EOF && err == NULL)
1240 err = got_error_from_errno("fclose");
1242 free(path_refs);
1243 free(path);
1244 if (tmppath) {
1245 if (unlink(tmppath) != 0 && err == NULL)
1246 err = got_error_from_errno2("unlink", tmppath);
1247 free(tmppath);
1249 return err ? err : unlock_err;
1252 static const struct got_error *
1253 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1255 const struct got_error *err = NULL, *unlock_err = NULL;
1256 struct got_lockfile *lf = NULL;
1257 FILE *f = NULL, *tmpf = NULL;
1258 char *line = NULL, *packed_refs_path, *tmppath = NULL;
1259 size_t linesize = 0;
1260 struct got_reflist_head refs;
1261 int found_delref = 0;
1263 /* The packed-refs file does not cotain symbolic references. */
1264 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1265 return got_error(GOT_ERR_BAD_REF_DATA);
1267 TAILQ_INIT(&refs);
1269 packed_refs_path = got_repo_get_path_packed_refs(repo);
1270 if (packed_refs_path == NULL)
1271 return got_error_from_errno("got_repo_get_path_packed_refs");
1273 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
1274 if (err)
1275 goto done;
1277 if (delref->lf == NULL) {
1278 err = got_lockfile_lock(&lf, packed_refs_path, -1);
1279 if (err)
1280 goto done;
1283 f = fopen(packed_refs_path, "r");
1284 if (f == NULL) {
1285 err = got_error_from_errno2("fopen", packed_refs_path);
1286 goto done;
1288 for (;;) {
1289 ssize_t linelen;
1290 struct got_reference *ref;
1291 struct got_reflist_entry *new;
1293 linelen = getline(&line, &linesize, f);
1294 if (linelen == -1) {
1295 if (feof(f))
1296 break;
1297 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1298 goto done;
1300 if (linelen > 0 && line[linelen - 1] == '\n')
1301 line[linelen - 1] = '\0';
1302 err = parse_packed_ref_line(&ref, NULL, line, 0);
1303 if (err)
1304 goto done;
1305 if (ref == NULL)
1306 continue;
1308 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1309 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1310 sizeof(delref->ref.ref.sha1)) == 0) {
1311 found_delref = 1;
1312 got_ref_close(ref);
1313 continue;
1316 err = got_reflist_insert(&new, &refs, ref,
1317 got_ref_cmp_by_name, NULL);
1318 if (err || new == NULL /* duplicate */)
1319 got_ref_close(ref);
1320 if (err)
1321 goto done;
1324 if (found_delref) {
1325 struct got_reflist_entry *re;
1326 size_t n;
1327 struct stat sb;
1329 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1330 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1331 err = got_ferror(f, GOT_ERR_IO);
1332 goto done;
1335 TAILQ_FOREACH(re, &refs, entry) {
1336 char hex[SHA1_DIGEST_STRING_LENGTH];
1338 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1339 sizeof(hex)) == NULL) {
1340 err = got_error(GOT_ERR_BAD_REF_DATA);
1341 goto done;
1343 n = fprintf(tmpf, "%s ", hex);
1344 if (n != sizeof(hex)) {
1345 err = got_ferror(f, GOT_ERR_IO);
1346 goto done;
1348 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1349 if (n != strlen(re->ref->ref.ref.name) + 1) {
1350 err = got_ferror(f, GOT_ERR_IO);
1351 goto done;
1355 if (fflush(tmpf) != 0) {
1356 err = got_error_from_errno("fflush");
1357 goto done;
1360 if (fstat(fileno(f), &sb) != 0) {
1361 if (errno != ENOENT) {
1362 err = got_error_from_errno2("fstat",
1363 packed_refs_path);
1364 goto done;
1366 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1369 if (fchmod(fileno(tmpf), sb.st_mode) != 0) {
1370 err = got_error_from_errno2("fchmod", tmppath);
1371 goto done;
1374 if (rename(tmppath, packed_refs_path) != 0) {
1375 err = got_error_from_errno3("rename", tmppath,
1376 packed_refs_path);
1377 goto done;
1380 done:
1381 if (delref->lf == NULL && lf)
1382 unlock_err = got_lockfile_unlock(lf, -1);
1383 if (f) {
1384 if (fclose(f) == EOF && err == NULL)
1385 err = got_error_from_errno("fclose");
1387 if (tmpf) {
1388 unlink(tmppath);
1389 if (fclose(tmpf) == EOF && err == NULL)
1390 err = got_error_from_errno("fclose");
1392 free(tmppath);
1393 free(packed_refs_path);
1394 free(line);
1395 got_ref_list_free(&refs);
1396 return err ? err : unlock_err;
1399 static const struct got_error *
1400 delete_loose_ref(struct got_reference *ref, struct got_repository *repo)
1402 const struct got_error *err = NULL, *unlock_err = NULL;
1403 const char *name = got_ref_get_name(ref);
1404 char *path_refs = NULL, *path = NULL;
1405 struct got_lockfile *lf = NULL;
1407 path_refs = get_refs_dir_path(repo, name);
1408 if (path_refs == NULL) {
1409 err = got_error_from_errno2("get_refs_dir_path", name);
1410 goto done;
1413 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1414 err = got_error_from_errno("asprintf");
1415 goto done;
1418 if (ref->lf == NULL) {
1419 err = got_lockfile_lock(&lf, path, -1);
1420 if (err)
1421 goto done;
1424 /* XXX: check if old content matches our expectations? */
1426 if (unlink(path) != 0)
1427 err = got_error_from_errno2("unlink", path);
1428 done:
1429 if (ref->lf == NULL && lf)
1430 unlock_err = got_lockfile_unlock(lf, -1);
1432 free(path_refs);
1433 free(path);
1434 return err ? err : unlock_err;
1437 const struct got_error *
1438 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1440 const struct got_error *err = NULL;
1441 struct got_reference *ref2;
1443 if (ref->flags & GOT_REF_IS_PACKED) {
1444 err = delete_packed_ref(ref, repo);
1445 if (err)
1446 return err;
1448 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1449 if (err) {
1450 if (err->code == GOT_ERR_NOT_REF)
1451 return NULL;
1452 return err;
1455 err = delete_loose_ref(ref2, repo);
1456 got_ref_close(ref2);
1457 return err;
1458 } else {
1459 err = delete_loose_ref(ref, repo);
1460 if (err)
1461 return err;
1463 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1464 if (err) {
1465 if (err->code == GOT_ERR_NOT_REF)
1466 return NULL;
1467 return err;
1470 err = delete_packed_ref(ref2, repo);
1471 got_ref_close(ref2);
1472 return err;
1476 const struct got_error *
1477 got_ref_unlock(struct got_reference *ref)
1479 const struct got_error *err;
1480 err = got_lockfile_unlock(ref->lf, -1);
1481 ref->lf = NULL;
1482 return err;
1485 struct got_reflist_object_id_map {
1486 struct got_object_idset *idset;
1489 struct got_reflist_object_id_map_entry {
1490 struct got_reflist_head refs;
1493 static const struct got_error *
1494 add_object_id_map_entry(struct got_object_idset *idset,
1495 struct got_object_id *id, struct got_reflist_entry *re)
1497 const struct got_error *err = NULL;
1498 struct got_reflist_object_id_map_entry *ent;
1499 struct got_reflist_entry *new;
1501 ent = got_object_idset_get(idset, id);
1502 if (ent == NULL) {
1503 ent = malloc(sizeof(*ent));
1504 if (ent == NULL)
1505 return got_error_from_errno("malloc");
1507 TAILQ_INIT(&ent->refs);
1508 err = got_object_idset_add(idset, id, ent);
1509 if (err)
1510 return err;
1513 err = got_reflist_entry_dup(&new, re);
1514 if (err)
1515 return err;
1517 TAILQ_INSERT_TAIL(&ent->refs, new, entry);
1518 return NULL;
1521 const struct got_error *
1522 got_reflist_object_id_map_create(struct got_reflist_object_id_map **map,
1523 struct got_reflist_head *refs, struct got_repository *repo)
1525 const struct got_error *err = NULL;
1526 struct got_object_idset *idset;
1527 struct got_object_id *id = NULL;
1528 struct got_reflist_entry *re;
1530 idset = got_object_idset_alloc();
1531 if (idset == NULL)
1532 return got_error_from_errno("got_object_idset_alloc");
1534 *map = malloc(sizeof(**map));
1535 if (*map == NULL) {
1536 got_object_idset_free(idset);
1537 return got_error_from_errno("malloc");
1539 (*map)->idset = idset;
1541 TAILQ_FOREACH(re, refs, entry) {
1542 struct got_tag_object *tag = NULL;
1544 err = got_ref_resolve(&id, repo, re->ref);
1545 if (err)
1546 goto done;
1548 err = add_object_id_map_entry(idset, id, re);
1549 if (err)
1550 goto done;
1552 if (strstr(got_ref_get_name(re->ref), "/tags/") == NULL) {
1553 free(id);
1554 id = NULL;
1555 continue;
1558 err = got_object_open_as_tag(&tag, repo, id);
1559 if (err) {
1560 if (err->code != GOT_ERR_OBJ_TYPE)
1561 goto done;
1562 /* Ref points at something other than a tag. */
1563 err = NULL;
1564 tag = NULL;
1565 free(id);
1566 id = NULL;
1567 continue;
1570 err = add_object_id_map_entry(idset,
1571 got_object_tag_get_object_id(tag), re);
1572 got_object_tag_close(tag);
1573 if (err)
1574 goto done;
1576 free(id);
1577 id = NULL;
1579 done:
1580 free(id);
1581 if (err) {
1582 got_reflist_object_id_map_free(*map);
1583 *map = NULL;
1585 return err;
1588 struct got_reflist_head *
1589 got_reflist_object_id_map_lookup(struct got_reflist_object_id_map *map,
1590 struct got_object_id *id)
1592 struct got_reflist_object_id_map_entry *ent;
1593 ent = got_object_idset_get(map->idset, id);
1594 if (ent)
1595 return &ent->refs;
1596 return NULL;
1599 static const struct got_error *
1600 free_id_map_entry(struct got_object_id *id, void *data, void *arg)
1602 struct got_reflist_object_id_map_entry *ent = data;
1604 got_ref_list_free(&ent->refs);
1605 free(ent);
1606 return NULL;
1609 void
1610 got_reflist_object_id_map_free(struct got_reflist_object_id_map *map)
1612 got_object_idset_for_each(map->idset, free_id_map_entry, NULL);
1613 got_object_idset_free(map->idset);
1614 free(map);