Blob


1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/stat.h>
21 #include <errno.h>
22 #include <dirent.h>
23 #include <limits.h>
24 #include <sha1.h>
25 #include <sha2.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_hash.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 struct got_object_id id;
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;
92 /* Cached timestamp for got_ref_cmp_by_commit_timestamp_descending() */
93 time_t committer_time;
94 };
96 static const struct got_error *
97 alloc_ref(struct got_reference **ref, const char *name,
98 struct got_object_id *id, int flags, time_t mtime)
99 {
100 const struct got_error *err = NULL;
102 *ref = calloc(1, sizeof(**ref));
103 if (*ref == NULL)
104 return got_error_from_errno("calloc");
106 memcpy(&(*ref)->ref.ref.id, id, sizeof((*ref)->ref.ref.id));
107 (*ref)->flags = flags;
108 (*ref)->ref.ref.name = strdup(name);
109 (*ref)->mtime = mtime;
110 if ((*ref)->ref.ref.name == NULL) {
111 err = got_error_from_errno("strdup");
112 got_ref_close(*ref);
113 *ref = NULL;
115 return err;
118 static const struct got_error *
119 alloc_symref(struct got_reference **ref, const char *name,
120 const char *target_ref, int flags)
122 const struct got_error *err = NULL;
124 *ref = calloc(1, sizeof(**ref));
125 if (*ref == NULL)
126 return got_error_from_errno("calloc");
128 (*ref)->flags = GOT_REF_IS_SYMBOLIC | flags;
129 (*ref)->ref.symref.name = strdup(name);
130 if ((*ref)->ref.symref.name == NULL) {
131 err = got_error_from_errno("strdup");
132 got_ref_close(*ref);
133 *ref = NULL;
134 return err;
136 (*ref)->ref.symref.ref = strdup(target_ref);
137 if ((*ref)->ref.symref.ref == NULL) {
138 err = got_error_from_errno("strdup");
139 got_ref_close(*ref);
140 *ref = NULL;
142 return err;
145 static const struct got_error *
146 parse_symref(struct got_reference **ref, const char *name, const char *line)
148 if (line[0] == '\0')
149 return got_error(GOT_ERR_BAD_REF_DATA);
151 return alloc_symref(ref, name, line, 0);
154 static const struct got_error *
155 parse_ref_line(struct got_reference **ref, const char *name, const char *line,
156 time_t mtime)
158 struct got_object_id id;
160 memset(&id, 0, sizeof(id));
162 if (strncmp(line, "ref: ", 5) == 0) {
163 line += 5;
164 return parse_symref(ref, name, line);
167 if (got_parse_sha256_digest(id.hash, line))
168 id.algo = GOT_HASH_SHA256;
169 else if (got_parse_sha1_digest(id.hash, line))
170 id.algo = GOT_HASH_SHA1;
171 else
172 return got_error(GOT_ERR_BAD_REF_DATA);
174 return alloc_ref(ref, name, &id, 0, mtime);
177 static const struct got_error *
178 parse_ref_file(struct got_reference **ref, const char *name,
179 const char *absname, const char *abspath, int lock)
181 const struct got_error *err = NULL;
182 FILE *f;
183 char *line = NULL;
184 size_t linesize = 0;
185 ssize_t linelen;
186 struct got_lockfile *lf = NULL;
187 struct stat sb;
189 if (lock) {
190 err = got_lockfile_lock(&lf, abspath, -1);
191 if (err) {
192 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
193 err = got_error_not_ref(name);
194 return err;
198 f = fopen(abspath, "rbe");
199 if (f == NULL) {
200 if (errno != ENOTDIR && errno != ENOENT)
201 err = got_error_from_errno2("fopen", abspath);
202 else
203 err = got_error_not_ref(name);
204 if (lock)
205 got_lockfile_unlock(lf, -1);
206 return err;
208 if (fstat(fileno(f), &sb) == -1) {
209 err = got_error_from_errno2("fstat", abspath);
210 goto done;
213 linelen = getline(&line, &linesize, f);
214 if (linelen == -1) {
215 if (feof(f))
216 err = NULL; /* ignore empty files (could be locks) */
217 else {
218 if (errno == EISDIR)
219 err = got_error(GOT_ERR_NOT_REF);
220 else if (ferror(f))
221 err = got_ferror(f, GOT_ERR_IO);
222 else
223 err = got_error_from_errno2("getline", abspath);
225 if (lock)
226 got_lockfile_unlock(lf, -1);
227 goto done;
229 while (linelen > 0 && line[linelen - 1] == '\n') {
230 line[linelen - 1] = '\0';
231 linelen--;
234 err = parse_ref_line(ref, absname, line, sb.st_mtime);
235 if (lock) {
236 if (err)
237 got_lockfile_unlock(lf, -1);
238 else {
239 if (*ref)
240 (*ref)->lf = lf;
241 else
242 got_lockfile_unlock(lf, -1);
245 done:
246 free(line);
247 if (fclose(f) == EOF && err == NULL) {
248 err = got_error_from_errno("fclose");
249 if (*ref) {
250 if (lock)
251 got_ref_unlock(*ref);
252 got_ref_close(*ref);
253 *ref = NULL;
256 return err;
259 static int
260 is_well_known_ref(const char *refname)
262 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
263 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
264 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
265 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
268 static char *
269 get_refs_dir_path(struct got_repository *repo, const char *refname)
271 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
272 return strdup(got_repo_get_path_git_dir(repo));
274 return got_repo_get_path_refs(repo);
277 const struct got_error *
278 got_ref_alloc(struct got_reference **ref, const char *name,
279 struct got_object_id *id)
281 if (!got_ref_name_is_valid(name))
282 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
284 return alloc_ref(ref, name, id, 0, 0);
287 const struct got_error *
288 got_ref_alloc_symref(struct got_reference **ref, const char *name,
289 struct got_reference *target_ref)
291 if (!got_ref_name_is_valid(name))
292 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
294 return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
297 static const struct got_error *
298 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
299 const char *line, time_t mtime)
301 struct got_object_id id;
302 const char *name;
304 *ref = NULL;
306 if (line[0] == '#' || line[0] == '^')
307 return NULL;
309 if (!got_parse_sha1_digest(id.hash, line))
310 return got_error(GOT_ERR_BAD_REF_DATA);
312 if (abs_refname) {
313 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
314 return NULL;
315 name = abs_refname;
316 } else
317 name = line + SHA1_DIGEST_STRING_LENGTH;
319 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED, mtime);
322 static const struct got_error *
323 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
324 int nsubdirs, const char *refname, time_t mtime)
326 const struct got_error *err = NULL;
327 char *abs_refname;
328 char *line = NULL;
329 size_t linesize = 0;
330 ssize_t linelen;
331 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
333 *ref = NULL;
335 if (ref_is_absolute)
336 abs_refname = (char *)refname;
337 do {
338 linelen = getline(&line, &linesize, f);
339 if (linelen == -1) {
340 if (feof(f))
341 break;
342 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
343 break;
345 if (linelen > 0 && line[linelen - 1] == '\n')
346 line[linelen - 1] = '\0';
347 for (i = 0; i < nsubdirs; i++) {
348 if (!ref_is_absolute &&
349 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
350 refname) == -1)
351 return got_error_from_errno("asprintf");
352 err = parse_packed_ref_line(ref, abs_refname, line,
353 mtime);
354 if (!ref_is_absolute)
355 free(abs_refname);
356 if (err || *ref != NULL)
357 break;
359 if (err)
360 break;
361 } while (*ref == NULL);
362 free(line);
364 return err;
367 static const struct got_error *
368 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
369 const char *name, int lock)
371 const struct got_error *err = NULL;
372 char *path = NULL;
373 char *absname = NULL;
374 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
375 int ref_is_well_known = (subdir[0] == '\0' && is_well_known_ref(name));
377 *ref = NULL;
379 if (!got_ref_name_is_valid(name))
380 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
382 if (ref_is_absolute || ref_is_well_known) {
383 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
384 return got_error_from_errno("asprintf");
385 absname = (char *)name;
386 } else {
387 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
388 subdir[0] ? "/" : "", name) == -1)
389 return got_error_from_errno("asprintf");
391 if (asprintf(&absname, "refs/%s%s%s",
392 subdir, subdir[0] ? "/" : "", name) == -1) {
393 err = got_error_from_errno("asprintf");
394 goto done;
398 err = parse_ref_file(ref, name, absname, path, lock);
399 done:
400 if (!ref_is_absolute && !ref_is_well_known)
401 free(absname);
402 free(path);
403 return err;
406 const struct got_error *
407 got_ref_open(struct got_reference **ref, struct got_repository *repo,
408 const char *refname, int lock)
410 const struct got_error *err = NULL;
411 char *packed_refs_path = NULL, *path_refs = NULL;
412 const char *subdirs[] = {
413 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
414 };
415 size_t i;
416 int well_known = is_well_known_ref(refname);
417 struct got_lockfile *lf = NULL;
419 *ref = NULL;
421 path_refs = get_refs_dir_path(repo, refname);
422 if (path_refs == NULL) {
423 err = got_error_from_errno2("get_refs_dir_path", refname);
424 goto done;
427 if (well_known) {
428 err = open_ref(ref, path_refs, "", refname, lock);
429 } else {
430 FILE *f;
432 /* Search on-disk refs before packed refs! */
433 for (i = 0; i < nitems(subdirs); i++) {
434 err = open_ref(ref, path_refs, subdirs[i], refname,
435 lock);
436 if ((err && err->code != GOT_ERR_NOT_REF) || *ref)
437 goto done;
440 packed_refs_path = got_repo_get_path_packed_refs(repo);
441 if (packed_refs_path == NULL) {
442 err = got_error_from_errno(
443 "got_repo_get_path_packed_refs");
444 goto done;
447 if (lock) {
448 err = got_lockfile_lock(&lf, packed_refs_path, -1);
449 if (err)
450 goto done;
452 f = fopen(packed_refs_path, "rbe");
453 if (f != NULL) {
454 struct stat sb;
455 if (fstat(fileno(f), &sb) == -1) {
456 err = got_error_from_errno2("fstat",
457 packed_refs_path);
458 goto done;
460 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
461 refname, sb.st_mtime);
462 if (!err) {
463 if (fclose(f) == EOF) {
464 err = got_error_from_errno("fclose");
465 got_ref_close(*ref);
466 *ref = NULL;
467 } else if (*ref)
468 (*ref)->lf = lf;
472 done:
473 if (!err && *ref == NULL)
474 err = got_error_not_ref(refname);
475 if (err && lf)
476 got_lockfile_unlock(lf, -1);
477 free(packed_refs_path);
478 free(path_refs);
479 return err;
482 void
483 got_ref_close(struct got_reference *ref)
485 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
486 free(ref->ref.symref.name);
487 free(ref->ref.symref.ref);
488 } else
489 free(ref->ref.ref.name);
490 free(ref);
493 struct got_reference *
494 got_ref_dup(struct got_reference *ref)
496 struct got_reference *ret;
498 ret = calloc(1, sizeof(*ret));
499 if (ret == NULL)
500 return NULL;
502 ret->flags = ref->flags;
503 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
504 ret->ref.symref.name = strdup(ref->ref.symref.name);
505 if (ret->ref.symref.name == NULL) {
506 free(ret);
507 return NULL;
509 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
510 if (ret->ref.symref.ref == NULL) {
511 free(ret->ref.symref.name);
512 free(ret);
513 return NULL;
515 } else {
516 ret->ref.ref.name = strdup(ref->ref.ref.name);
517 if (ret->ref.ref.name == NULL) {
518 free(ret);
519 return NULL;
521 memcpy(&ret->ref.ref.id, &ref->ref.ref.id,
522 sizeof(ret->ref.ref.id));
525 return ret;
528 const struct got_error *
529 got_reflist_entry_dup(struct got_reflist_entry **newp,
530 struct got_reflist_entry *re)
532 const struct got_error *err = NULL;
533 struct got_reflist_entry *new;
535 *newp = NULL;
537 new = malloc(sizeof(*new));
538 if (new == NULL)
539 return got_error_from_errno("malloc");
541 new->ref = got_ref_dup(re->ref);
542 if (new->ref == NULL) {
543 err = got_error_from_errno("got_ref_dup");
544 free(new);
545 return err;
548 *newp = new;
549 return NULL;
552 const struct got_error *
553 got_ref_resolve_symbolic(struct got_reference **resolved,
554 struct got_repository *repo, struct got_reference *ref)
556 struct got_reference *nextref;
557 const struct got_error *err;
559 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
560 if (err)
561 return err;
563 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
564 err = got_ref_resolve_symbolic(resolved, repo, nextref);
565 else
566 *resolved = got_ref_dup(nextref);
568 got_ref_close(nextref);
569 return err;
572 static const struct got_error *
573 ref_resolve(struct got_object_id **id, struct got_repository *repo,
574 struct got_reference *ref, int recursion)
576 const struct got_error *err;
578 if (recursion <= 0)
579 return got_error_msg(GOT_ERR_RECURSION,
580 "reference recursion limit reached");
582 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
583 struct got_reference *resolved = NULL;
584 err = got_ref_resolve_symbolic(&resolved, repo, ref);
585 if (err == NULL)
586 err = ref_resolve(id, repo, resolved, --recursion);
587 if (resolved)
588 got_ref_close(resolved);
589 return err;
592 *id = calloc(1, sizeof(**id));
593 if (*id == NULL)
594 return got_error_from_errno("calloc");
595 memcpy(*id, &ref->ref.ref.id, sizeof(**id));
596 return NULL;
599 const struct got_error *
600 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
601 struct got_reference *ref)
603 return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
606 char *
607 got_ref_to_str(struct got_reference *ref)
609 char *str;
611 if (ref->flags & GOT_REF_IS_SYMBOLIC)
612 return strdup(ref->ref.symref.ref);
614 if (got_object_id_str(&str, &ref->ref.ref.id) != NULL)
615 return NULL;
617 return str;
620 const char *
621 got_ref_get_name(struct got_reference *ref)
623 if (ref->flags & GOT_REF_IS_SYMBOLIC)
624 return ref->ref.symref.name;
626 return ref->ref.ref.name;
629 const char *
630 got_ref_get_symref_target(struct got_reference *ref)
632 if (ref->flags & GOT_REF_IS_SYMBOLIC)
633 return ref->ref.symref.ref;
635 return NULL;
638 time_t
639 got_ref_get_mtime(struct got_reference *ref)
641 return ref->mtime;
644 const struct got_error *
645 got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
646 struct got_reference* re2)
648 const char *name1 = got_ref_get_name(re1);
649 const char *name2 = got_ref_get_name(re2);
651 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
652 return NULL;
655 const struct got_error *
656 got_ref_cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
657 struct got_reference *ref2)
659 const struct got_error *err = NULL;
660 struct got_repository *repo = arg;
661 struct got_object_id *id1, *id2 = NULL;
662 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
663 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
664 time_t time1, time2;
666 *cmp = 0;
668 err = got_ref_resolve(&id1, repo, ref1);
669 if (err)
670 return err;
671 err = got_object_open_as_tag(&tag1, repo, id1);
672 if (err) {
673 if (err->code != GOT_ERR_OBJ_TYPE)
674 goto done;
675 /* "lightweight" tag */
676 err = got_object_open_as_commit(&commit1, repo, id1);
677 if (err)
678 goto done;
679 time1 = got_object_commit_get_committer_time(commit1);
680 } else
681 time1 = got_object_tag_get_tagger_time(tag1);
683 err = got_ref_resolve(&id2, repo, ref2);
684 if (err)
685 goto done;
686 err = got_object_open_as_tag(&tag2, repo, id2);
687 if (err) {
688 if (err->code != GOT_ERR_OBJ_TYPE)
689 goto done;
690 /* "lightweight" tag */
691 err = got_object_open_as_commit(&commit2, repo, id2);
692 if (err)
693 goto done;
694 time2 = got_object_commit_get_committer_time(commit2);
695 } else
696 time2 = got_object_tag_get_tagger_time(tag2);
698 /* Put latest tags first. */
699 if (time1 < time2)
700 *cmp = 1;
701 else if (time1 > time2)
702 *cmp = -1;
703 else
704 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
705 done:
706 free(id1);
707 free(id2);
708 if (tag1)
709 got_object_tag_close(tag1);
710 if (tag2)
711 got_object_tag_close(tag2);
712 if (commit1)
713 got_object_commit_close(commit1);
714 if (commit2)
715 got_object_commit_close(commit2);
716 return err;
719 static const struct got_error *
720 get_committer_time(struct got_reference *ref, struct got_repository *repo)
722 const struct got_error *err = NULL;
723 int obj_type;
724 struct got_commit_object *commit = NULL;
725 struct got_tag_object *tag = NULL;
726 struct got_object_id *id = NULL;
728 err = got_ref_resolve(&id, repo, ref);
729 if (err)
730 return err;
732 err = got_object_get_type(&obj_type, repo, id);
733 if (err)
734 goto done;
736 switch (obj_type) {
737 case GOT_OBJ_TYPE_COMMIT:
738 err = got_object_open_as_commit(&commit, repo, id);
739 if (err)
740 goto done;
741 ref->committer_time =
742 got_object_commit_get_committer_time(commit);
743 break;
744 case GOT_OBJ_TYPE_TAG:
745 err = got_object_open_as_tag(&tag, repo, id);
746 if (err)
747 goto done;
748 ref->committer_time = got_object_tag_get_tagger_time(tag);
749 break;
750 default:
751 /* best effort for other object types */
752 ref->committer_time = got_ref_get_mtime(ref);
753 break;
755 done:
756 free(id);
757 if (commit)
758 got_object_commit_close(commit);
759 if (tag)
760 got_object_tag_close(tag);
761 return err;
764 const struct got_error *
765 got_ref_cmp_by_commit_timestamp_descending(void *arg, int *cmp,
766 struct got_reference *ref1, struct got_reference *ref2)
768 const struct got_error *err = NULL;
769 struct got_repository *repo = arg;
771 *cmp = 0;
773 if (ref1->committer_time == 0) {
774 err = get_committer_time(ref1, repo);
775 if (err)
776 return err;
778 if (ref2->committer_time == 0) {
779 err = get_committer_time(ref2, repo);
780 if (err)
781 return err;
784 if (ref1->committer_time < ref2->committer_time)
785 *cmp = 1;
786 else if (ref2->committer_time < ref1->committer_time)
787 *cmp = -1;
788 else
789 return got_ref_cmp_by_name(arg, cmp, ref1, ref2);
791 return err;
794 const struct got_error *
795 got_reflist_insert(struct got_reflist_entry **newp,
796 struct got_reflist_head *refs, struct got_reference *ref,
797 got_ref_cmp_cb cmp_cb, void *cmp_arg)
799 const struct got_error *err;
800 struct got_reflist_entry *new, *re;
801 int cmp;
803 *newp = NULL;
805 if (cmp_cb != got_ref_cmp_by_name && (ref->flags & GOT_REF_IS_PACKED)) {
806 /*
807 * If we are not sorting elements by name then we must still
808 * detect collisions between a packed ref and an on-disk ref
809 * using the same name. On-disk refs take precedence and are
810 * already present on the list before packed refs get added.
811 */
812 TAILQ_FOREACH(re, refs, entry) {
813 err = got_ref_cmp_by_name(NULL, &cmp, re->ref, ref);
814 if (err)
815 return err;
816 if (cmp == 0)
817 return 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 const struct got_error *
860 got_reflist_sort(struct got_reflist_head *refs,
861 got_ref_cmp_cb cmp_cb, void *cmp_arg)
863 const struct got_error *err = NULL;
864 struct got_reflist_entry *re, *tmp, *new;
865 struct got_reflist_head sorted;
867 TAILQ_INIT(&sorted);
869 TAILQ_FOREACH_SAFE(re, refs, entry, tmp) {
870 struct got_reference *ref = re->ref;
871 TAILQ_REMOVE(refs, re, entry);
872 free(re);
873 err = got_reflist_insert(&new, &sorted, ref, cmp_cb, cmp_arg);
874 if (err || new == NULL /* duplicate */)
875 got_ref_close(ref);
876 if (err)
877 return err;
880 TAILQ_CONCAT(refs, &sorted, entry);
881 return NULL;
884 static const struct got_error *
885 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
886 const char *subdir, struct got_repository *repo,
887 got_ref_cmp_cb cmp_cb, void *cmp_arg)
889 const struct got_error *err = NULL;
890 DIR *d = NULL;
891 char *path_subdir;
893 while (subdir[0] == '/')
894 subdir++;
896 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
897 return got_error_from_errno("asprintf");
899 d = opendir(path_subdir);
900 if (d == NULL)
901 goto done;
903 for (;;) {
904 struct dirent *dent;
905 struct got_reference *ref;
906 char *child;
907 int type;
909 dent = readdir(d);
910 if (dent == NULL)
911 break;
913 if (strcmp(dent->d_name, ".") == 0 ||
914 strcmp(dent->d_name, "..") == 0)
915 continue;
917 err = got_path_dirent_type(&type, path_subdir, dent);
918 if (err)
919 break;
921 switch (type) {
922 case DT_REG:
923 err = open_ref(&ref, path_refs, subdir, dent->d_name,
924 0);
925 if (err)
926 goto done;
927 if (ref) {
928 struct got_reflist_entry *new;
929 err = got_reflist_insert(&new, refs, ref,
930 cmp_cb, cmp_arg);
931 if (err || new == NULL /* duplicate */)
932 got_ref_close(ref);
933 if (err)
934 goto done;
936 break;
937 case DT_DIR:
938 if (asprintf(&child, "%s%s%s", subdir,
939 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
940 err = got_error_from_errno("asprintf");
941 break;
943 err = gather_on_disk_refs(refs, path_refs, child, repo,
944 cmp_cb, cmp_arg);
945 free(child);
946 break;
947 default:
948 break;
951 done:
952 if (d)
953 closedir(d);
954 free(path_subdir);
955 return err;
958 const struct got_error *
959 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
960 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
962 const struct got_error *err;
963 char *packed_refs_path = NULL, *path_refs = NULL;
964 char *abs_namespace = NULL, *buf = NULL;
965 const char *ondisk_ref_namespace = NULL;
966 char *line = NULL;
967 FILE *f = NULL;
968 struct got_reference *ref;
969 struct got_reflist_entry *new;
971 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
972 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
973 if (path_refs == NULL) {
974 err = got_error_from_errno("get_refs_dir_path");
975 goto done;
977 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
978 if (err)
979 goto done;
980 err = got_reflist_insert(&new, refs, ref, cmp_cb, cmp_arg);
981 if (err || new == NULL /* duplicate */)
982 got_ref_close(ref);
983 if (err && err->code != GOT_ERR_NOT_REF)
984 goto done;
985 } else {
986 /* Try listing a single reference. */
987 const char *refname = ref_namespace;
988 path_refs = get_refs_dir_path(repo, refname);
989 if (path_refs == NULL) {
990 err = got_error_from_errno("get_refs_dir_path");
991 goto done;
993 err = open_ref(&ref, path_refs, "", refname, 0);
994 if (err) {
995 if (err->code != GOT_ERR_NOT_REF)
996 goto done;
997 /* Try to look up references in a given namespace. */
998 } else {
999 err = got_reflist_insert(&new, refs, ref,
1000 cmp_cb, cmp_arg);
1001 if (err || new == NULL /* duplicate */)
1002 got_ref_close(ref);
1003 return err;
1007 if (ref_namespace) {
1008 size_t len;
1009 /* Canonicalize the path to eliminate double-slashes if any. */
1010 if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
1011 err = got_error_from_errno("asprintf");
1012 goto done;
1014 len = strlen(abs_namespace) + 1;
1015 buf = malloc(len);
1016 if (buf == NULL) {
1017 err = got_error_from_errno("malloc");
1018 goto done;
1020 err = got_canonpath(abs_namespace, buf, len);
1021 if (err)
1022 goto done;
1023 ondisk_ref_namespace = buf;
1024 while (ondisk_ref_namespace[0] == '/')
1025 ondisk_ref_namespace++;
1026 if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
1027 ondisk_ref_namespace += 5;
1028 else if (strcmp(ondisk_ref_namespace, "refs") == 0)
1029 ondisk_ref_namespace = "";
1032 /* Gather on-disk refs before parsing packed-refs. */
1033 free(path_refs);
1034 path_refs = get_refs_dir_path(repo, "");
1035 if (path_refs == NULL) {
1036 err = got_error_from_errno("get_refs_dir_path");
1037 goto done;
1039 err = gather_on_disk_refs(refs, path_refs,
1040 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
1041 cmp_cb, cmp_arg);
1042 if (err)
1043 goto done;
1046 * The packed-refs file may contain redundant entries, in which
1047 * case on-disk refs take precedence.
1049 packed_refs_path = got_repo_get_path_packed_refs(repo);
1050 if (packed_refs_path == NULL) {
1051 err = got_error_from_errno("got_repo_get_path_packed_refs");
1052 goto done;
1055 f = fopen(packed_refs_path, "re");
1056 if (f) {
1057 size_t linesize = 0;
1058 ssize_t linelen;
1059 struct stat sb;
1061 if (fstat(fileno(f), &sb) == -1) {
1062 err = got_error_from_errno2("fstat", packed_refs_path);
1063 goto done;
1065 for (;;) {
1066 linelen = getline(&line, &linesize, f);
1067 if (linelen == -1) {
1068 if (feof(f))
1069 break;
1070 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1071 goto done;
1073 if (linelen > 0 && line[linelen - 1] == '\n')
1074 line[linelen - 1] = '\0';
1075 err = parse_packed_ref_line(&ref, NULL, line,
1076 sb.st_mtime);
1077 if (err)
1078 goto done;
1079 if (ref) {
1080 if (ref_namespace) {
1081 const char *name;
1082 name = got_ref_get_name(ref);
1083 if (!got_path_is_child(name,
1084 ref_namespace,
1085 strlen(ref_namespace))) {
1086 got_ref_close(ref);
1087 continue;
1090 err = got_reflist_insert(&new, refs, ref,
1091 cmp_cb, cmp_arg);
1092 if (err || new == NULL /* duplicate */)
1093 got_ref_close(ref);
1094 if (err)
1095 goto done;
1099 done:
1100 free(packed_refs_path);
1101 free(abs_namespace);
1102 free(buf);
1103 free(line);
1104 free(path_refs);
1105 if (f && fclose(f) == EOF && err == NULL)
1106 err = got_error_from_errno("fclose");
1107 return err;
1110 void
1111 got_ref_list_free(struct got_reflist_head *refs)
1113 struct got_reflist_entry *re;
1115 while ((re = TAILQ_FIRST(refs))) {
1116 TAILQ_REMOVE(refs, re, entry);
1117 got_ref_close(re->ref);
1118 free(re);
1122 int
1123 got_ref_is_symbolic(struct got_reference *ref)
1125 return (ref->flags & GOT_REF_IS_SYMBOLIC);
1128 const struct got_error *
1129 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1131 if (ref->flags & GOT_REF_IS_SYMBOLIC)
1132 return got_error(GOT_ERR_BAD_REF_TYPE);
1134 memcpy(&ref->ref.ref.id, id, sizeof(ref->ref.ref.id));
1135 return NULL;
1138 const struct got_error *
1139 got_ref_change_symref(struct got_reference *ref, const char *refname)
1141 char *new_name;
1143 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1144 return got_error(GOT_ERR_BAD_REF_TYPE);
1146 new_name = strdup(refname);
1147 if (new_name == NULL)
1148 return got_error_from_errno("strdup");
1150 free(ref->ref.symref.ref);
1151 ref->ref.symref.ref = new_name;
1152 return NULL;
1155 const struct got_error *
1156 got_ref_change_symref_to_ref(struct got_reference *symref,
1157 struct got_object_id *id)
1159 if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1160 return got_error(GOT_ERR_BAD_REF_TYPE);
1162 symref->ref.ref.name = symref->ref.symref.name;
1163 memcpy(&symref->ref.ref.id, id, sizeof(symref->ref.ref.id));
1164 symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1165 return NULL;
1168 const struct got_error *
1169 got_ref_write(struct got_reference *ref, struct got_repository *repo)
1171 const struct got_error *err = NULL, *unlock_err = NULL;
1172 const char *name = got_ref_get_name(ref);
1173 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1174 struct got_lockfile *lf = NULL;
1175 FILE *f = NULL;
1176 size_t n;
1177 struct stat sb;
1179 path_refs = get_refs_dir_path(repo, name);
1180 if (path_refs == NULL) {
1181 err = got_error_from_errno2("get_refs_dir_path", name);
1182 goto done;
1185 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1186 err = got_error_from_errno("asprintf");
1187 goto done;
1190 err = got_opentemp_named(&tmppath, &f, path, "");
1191 if (err) {
1192 char *parent;
1193 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1194 goto done;
1195 err = got_path_dirname(&parent, path);
1196 if (err)
1197 goto done;
1198 err = got_path_mkdir(parent);
1199 free(parent);
1200 if (err)
1201 goto done;
1202 err = got_opentemp_named(&tmppath, &f, path, "");
1203 if (err)
1204 goto done;
1207 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1208 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1209 if (n != strlen(ref->ref.symref.ref) + 6) {
1210 err = got_ferror(f, GOT_ERR_IO);
1211 goto done;
1213 } else {
1214 char *hex;
1215 size_t len;
1217 err = got_object_id_str(&hex, &ref->ref.ref.id);
1218 if (err)
1219 goto done;
1220 len = strlen(hex);
1221 n = fprintf(f, "%s\n", hex);
1222 free(hex);
1223 if (n != len + 1) {
1224 err = got_ferror(f, GOT_ERR_IO);
1225 goto done;
1229 if (ref->lf == NULL) {
1230 err = got_lockfile_lock(&lf, path, -1);
1231 if (err)
1232 goto done;
1235 /* XXX: check if old content matches our expectations? */
1237 if (stat(path, &sb) != 0) {
1238 if (errno != ENOENT) {
1239 err = got_error_from_errno2("stat", path);
1240 goto done;
1242 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1245 if (fchmod(fileno(f), sb.st_mode) != 0) {
1246 err = got_error_from_errno2("fchmod", tmppath);
1247 goto done;
1250 if (rename(tmppath, path) != 0) {
1251 err = got_error_from_errno3("rename", tmppath, path);
1252 goto done;
1254 free(tmppath);
1255 tmppath = NULL;
1257 if (stat(path, &sb) == -1) {
1258 err = got_error_from_errno2("stat", path);
1259 goto done;
1261 ref->mtime = sb.st_mtime;
1262 done:
1263 if (ref->lf == NULL && lf)
1264 unlock_err = got_lockfile_unlock(lf, -1);
1265 if (f) {
1266 if (fclose(f) == EOF && err == NULL)
1267 err = got_error_from_errno("fclose");
1269 free(path_refs);
1270 free(path);
1271 if (tmppath) {
1272 if (unlink(tmppath) == -1 && err == NULL)
1273 err = got_error_from_errno2("unlink", tmppath);
1274 free(tmppath);
1276 return err ? err : unlock_err;
1279 static const struct got_error *
1280 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1282 const struct got_error *err = NULL, *unlock_err = NULL;
1283 struct got_lockfile *lf = NULL;
1284 FILE *f = NULL, *tmpf = NULL;
1285 char *line = NULL, *packed_refs_path, *tmppath = NULL;
1286 size_t linesize = 0;
1287 struct got_reflist_head refs;
1288 int found_delref = 0;
1290 /* The packed-refs file does not cotain symbolic references. */
1291 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1292 return got_error(GOT_ERR_BAD_REF_DATA);
1294 TAILQ_INIT(&refs);
1296 packed_refs_path = got_repo_get_path_packed_refs(repo);
1297 if (packed_refs_path == NULL)
1298 return got_error_from_errno("got_repo_get_path_packed_refs");
1300 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path, "");
1301 if (err)
1302 goto done;
1304 if (delref->lf == NULL) {
1305 err = got_lockfile_lock(&lf, packed_refs_path, -1);
1306 if (err)
1307 goto done;
1310 f = fopen(packed_refs_path, "re");
1311 if (f == NULL) {
1312 err = got_error_from_errno2("fopen", packed_refs_path);
1313 goto done;
1315 for (;;) {
1316 ssize_t linelen;
1317 struct got_reference *ref;
1318 struct got_reflist_entry *new;
1320 linelen = getline(&line, &linesize, f);
1321 if (linelen == -1) {
1322 if (feof(f))
1323 break;
1324 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1325 goto done;
1327 if (linelen > 0 && line[linelen - 1] == '\n')
1328 line[linelen - 1] = '\0';
1329 err = parse_packed_ref_line(&ref, NULL, line, 0);
1330 if (err)
1331 goto done;
1332 if (ref == NULL)
1333 continue;
1335 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1336 got_object_id_cmp(&ref->ref.ref.id,
1337 &delref->ref.ref.id) == 0) {
1338 found_delref = 1;
1339 got_ref_close(ref);
1340 continue;
1343 err = got_reflist_insert(&new, &refs, ref,
1344 got_ref_cmp_by_name, NULL);
1345 if (err || new == NULL /* duplicate */)
1346 got_ref_close(ref);
1347 if (err)
1348 goto done;
1351 if (found_delref) {
1352 struct got_reflist_entry *re;
1353 size_t n;
1354 struct stat sb;
1356 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1357 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1358 err = got_ferror(f, GOT_ERR_IO);
1359 goto done;
1362 TAILQ_FOREACH(re, &refs, entry) {
1363 char *hex;
1364 size_t len;
1366 err = got_object_id_str(&hex, &re->ref->ref.ref.id);
1367 if (err)
1368 goto done;
1369 len = strlen(hex);
1370 n = fprintf(tmpf, "%s ", hex);
1371 free(hex);
1372 if (n != len + 1) {
1373 err = got_ferror(f, GOT_ERR_IO);
1374 goto done;
1377 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1378 if (n != strlen(re->ref->ref.ref.name) + 1) {
1379 err = got_ferror(f, GOT_ERR_IO);
1380 goto done;
1384 if (fflush(tmpf) != 0) {
1385 err = got_error_from_errno("fflush");
1386 goto done;
1389 if (fstat(fileno(f), &sb) != 0) {
1390 if (errno != ENOENT) {
1391 err = got_error_from_errno2("fstat",
1392 packed_refs_path);
1393 goto done;
1395 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1398 if (fchmod(fileno(tmpf), sb.st_mode) != 0) {
1399 err = got_error_from_errno2("fchmod", tmppath);
1400 goto done;
1403 if (rename(tmppath, packed_refs_path) != 0) {
1404 err = got_error_from_errno3("rename", tmppath,
1405 packed_refs_path);
1406 goto done;
1408 free(tmppath);
1409 tmppath = NULL;
1411 done:
1412 if (delref->lf == NULL && lf)
1413 unlock_err = got_lockfile_unlock(lf, -1);
1414 if (f) {
1415 if (fclose(f) == EOF && err == NULL)
1416 err = got_error_from_errno("fclose");
1418 if (tmppath && unlink(tmppath) == -1 && err == NULL)
1419 err = got_error_from_errno2("unlink", tmppath);
1420 if (tmpf && fclose(tmpf) == EOF && err == NULL)
1421 err = got_error_from_errno("fclose");
1422 free(tmppath);
1423 free(packed_refs_path);
1424 free(line);
1425 got_ref_list_free(&refs);
1426 return err ? err : unlock_err;
1429 static const struct got_error *
1430 delete_loose_ref(struct got_reference *ref, struct got_repository *repo)
1432 const struct got_error *err = NULL, *unlock_err = NULL;
1433 const char *name = got_ref_get_name(ref);
1434 char *path_refs = NULL, *path = NULL;
1435 struct got_lockfile *lf = NULL;
1437 path_refs = get_refs_dir_path(repo, name);
1438 if (path_refs == NULL) {
1439 err = got_error_from_errno2("get_refs_dir_path", name);
1440 goto done;
1443 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1444 err = got_error_from_errno("asprintf");
1445 goto done;
1448 if (ref->lf == NULL) {
1449 err = got_lockfile_lock(&lf, path, -1);
1450 if (err)
1451 goto done;
1454 /* XXX: check if old content matches our expectations? */
1456 if (unlink(path) == -1)
1457 err = got_error_from_errno2("unlink", path);
1458 done:
1459 if (ref->lf == NULL && lf)
1460 unlock_err = got_lockfile_unlock(lf, -1);
1462 free(path_refs);
1463 free(path);
1464 return err ? err : unlock_err;
1467 const struct got_error *
1468 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1470 const struct got_error *err = NULL;
1471 struct got_reference *ref2;
1473 if (ref->flags & GOT_REF_IS_PACKED) {
1474 err = delete_packed_ref(ref, repo);
1475 if (err)
1476 return err;
1478 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1479 if (err) {
1480 if (err->code == GOT_ERR_NOT_REF)
1481 return NULL;
1482 return err;
1485 err = delete_loose_ref(ref2, repo);
1486 got_ref_close(ref2);
1487 return err;
1488 } else {
1489 err = delete_loose_ref(ref, repo);
1490 if (err)
1491 return err;
1493 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1494 if (err) {
1495 if (err->code == GOT_ERR_NOT_REF)
1496 return NULL;
1497 return err;
1500 err = delete_packed_ref(ref2, repo);
1501 got_ref_close(ref2);
1502 return err;
1506 const struct got_error *
1507 got_ref_unlock(struct got_reference *ref)
1509 const struct got_error *err;
1510 err = got_lockfile_unlock(ref->lf, -1);
1511 ref->lf = NULL;
1512 return err;
1515 struct got_reflist_object_id_map {
1516 struct got_object_idset *idset;
1519 struct got_reflist_object_id_map_entry {
1520 struct got_reflist_head refs;
1523 static const struct got_error *
1524 add_object_id_map_entry(struct got_object_idset *idset,
1525 struct got_object_id *id, struct got_reflist_entry *re)
1527 const struct got_error *err = NULL;
1528 struct got_reflist_object_id_map_entry *ent;
1529 struct got_reflist_entry *new;
1531 ent = got_object_idset_get(idset, id);
1532 if (ent == NULL) {
1533 ent = malloc(sizeof(*ent));
1534 if (ent == NULL)
1535 return got_error_from_errno("malloc");
1537 TAILQ_INIT(&ent->refs);
1538 err = got_object_idset_add(idset, id, ent);
1539 if (err) {
1540 free(ent);
1541 return err;
1545 err = got_reflist_entry_dup(&new, re);
1546 if (err)
1547 return err;
1549 TAILQ_INSERT_TAIL(&ent->refs, new, entry);
1550 return NULL;
1553 const struct got_error *
1554 got_reflist_object_id_map_create(struct got_reflist_object_id_map **map,
1555 struct got_reflist_head *refs, struct got_repository *repo)
1557 const struct got_error *err = NULL;
1558 struct got_object_idset *idset;
1559 struct got_object_id *id = NULL;
1560 struct got_reflist_entry *re;
1562 idset = got_object_idset_alloc();
1563 if (idset == NULL)
1564 return got_error_from_errno("got_object_idset_alloc");
1566 *map = malloc(sizeof(**map));
1567 if (*map == NULL) {
1568 got_object_idset_free(idset);
1569 return got_error_from_errno("malloc");
1571 (*map)->idset = idset;
1573 TAILQ_FOREACH(re, refs, entry) {
1574 struct got_tag_object *tag = NULL;
1576 err = got_ref_resolve(&id, repo, re->ref);
1577 if (err)
1578 goto done;
1580 err = add_object_id_map_entry(idset, id, re);
1581 if (err)
1582 goto done;
1584 if (strstr(got_ref_get_name(re->ref), "/tags/") == NULL) {
1585 free(id);
1586 id = NULL;
1587 continue;
1590 err = got_object_open_as_tag(&tag, repo, id);
1591 if (err) {
1592 if (err->code != GOT_ERR_OBJ_TYPE)
1593 goto done;
1594 /* Ref points at something other than a tag. */
1595 err = NULL;
1596 tag = NULL;
1597 free(id);
1598 id = NULL;
1599 continue;
1602 err = add_object_id_map_entry(idset,
1603 got_object_tag_get_object_id(tag), re);
1604 got_object_tag_close(tag);
1605 if (err)
1606 goto done;
1608 free(id);
1609 id = NULL;
1611 done:
1612 free(id);
1613 if (err) {
1614 got_reflist_object_id_map_free(*map);
1615 *map = NULL;
1617 return err;
1620 struct got_reflist_head *
1621 got_reflist_object_id_map_lookup(struct got_reflist_object_id_map *map,
1622 struct got_object_id *id)
1624 struct got_reflist_object_id_map_entry *ent;
1625 ent = got_object_idset_get(map->idset, id);
1626 if (ent)
1627 return &ent->refs;
1628 return NULL;
1631 static const struct got_error *
1632 free_id_map_entry(struct got_object_id *id, void *data, void *arg)
1634 struct got_reflist_object_id_map_entry *ent = data;
1636 got_ref_list_free(&ent->refs);
1637 free(ent);
1638 return NULL;
1641 void
1642 got_reflist_object_id_map_free(struct got_reflist_object_id_map *map)
1644 got_object_idset_for_each(map->idset, free_id_map_entry, NULL);
1645 got_object_idset_free(map->idset);
1646 free(map);