portable: release 0.93
[got-portable.git] / lib / reference.c
blob2861fcb92918d1fb25573f0745ee98c008a4b0c9
1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 #include "got_compat.h"
19 #include <sys/types.h>
20 #include <sys/queue.h>
21 #include <sys/stat.h>
23 #include <errno.h>
24 #include <dirent.h>
25 #include <limits.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <zlib.h>
31 #include <time.h>
32 #include <libgen.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_hash.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"
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.
61 #define GOT_PACKED_REFS_HEADER "# pack-refs with:"
63 /* A symbolic reference. */
64 struct got_symref {
65 char *name;
66 char *ref;
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 struct got_object_id id;
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;
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)
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.id, id, sizeof((*ref)->ref.ref.id));
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, enum got_hash_algorithm algo)
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_object_id(&id, line, algo))
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,
173 enum got_hash_algorithm algo)
175 const struct got_error *err = NULL;
176 FILE *f;
177 char *line = NULL;
178 size_t linesize = 0;
179 ssize_t linelen;
180 struct got_lockfile *lf = NULL;
181 struct stat sb;
183 if (lock) {
184 err = got_lockfile_lock(&lf, abspath, -1);
185 if (err) {
186 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
187 err = got_error_not_ref(name);
188 return err;
192 f = fopen(abspath, "rbe");
193 if (f == NULL) {
194 if (errno != ENOTDIR && errno != ENOENT)
195 err = got_error_from_errno2("fopen", abspath);
196 else
197 err = got_error_not_ref(name);
198 if (lock)
199 got_lockfile_unlock(lf, -1);
200 return err;
202 if (fstat(fileno(f), &sb) == -1) {
203 err = got_error_from_errno2("fstat", abspath);
204 goto done;
207 linelen = getline(&line, &linesize, f);
208 if (linelen == -1) {
209 if (feof(f))
210 err = NULL; /* ignore empty files (could be locks) */
211 else {
212 if (errno == EISDIR)
213 err = got_error(GOT_ERR_NOT_REF);
214 else if (ferror(f))
215 err = got_ferror(f, GOT_ERR_IO);
216 else
217 err = got_error_from_errno2("getline", abspath);
219 if (lock)
220 got_lockfile_unlock(lf, -1);
221 goto done;
223 while (linelen > 0 && line[linelen - 1] == '\n') {
224 line[linelen - 1] = '\0';
225 linelen--;
228 err = parse_ref_line(ref, absname, line, sb.st_mtime, algo);
229 if (lock) {
230 if (err)
231 got_lockfile_unlock(lf, -1);
232 else {
233 if (*ref)
234 (*ref)->lf = lf;
235 else
236 got_lockfile_unlock(lf, -1);
239 done:
240 free(line);
241 if (fclose(f) == EOF && err == NULL) {
242 err = got_error_from_errno("fclose");
243 if (*ref) {
244 if (lock)
245 got_ref_unlock(*ref);
246 got_ref_close(*ref);
247 *ref = NULL;
250 return err;
253 static int
254 is_well_known_ref(const char *refname)
256 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
257 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
258 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
259 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
262 static char *
263 get_refs_dir_path(struct got_repository *repo, const char *refname)
265 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
266 return strdup(got_repo_get_path_git_dir(repo));
268 return got_repo_get_path_refs(repo);
271 const struct got_error *
272 got_ref_alloc(struct got_reference **ref, const char *name,
273 struct got_object_id *id)
275 if (!got_ref_name_is_valid(name))
276 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
278 return alloc_ref(ref, name, id, 0, 0);
281 const struct got_error *
282 got_ref_alloc_symref(struct got_reference **ref, const char *name,
283 struct got_reference *target_ref)
285 if (!got_ref_name_is_valid(name))
286 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
288 return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
291 static const struct got_error *
292 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
293 const char *line, time_t mtime, enum got_hash_algorithm algo)
295 struct got_object_id id;
296 const char *name;
298 *ref = NULL;
300 if (line[0] == '#' || line[0] == '^')
301 return NULL;
303 if (!got_parse_object_id(&id, line, algo))
304 return got_error(GOT_ERR_BAD_REF_DATA);
306 if (abs_refname) {
307 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
308 return NULL;
309 name = abs_refname;
310 } else
311 name = line + SHA1_DIGEST_STRING_LENGTH;
313 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED, mtime);
316 static const struct got_error *
317 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
318 int nsubdirs, const char *refname, time_t mtime,
319 enum got_hash_algorithm algo)
321 const struct got_error *err = NULL;
322 char *abs_refname;
323 char *line = NULL;
324 size_t linesize = 0;
325 ssize_t linelen;
326 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
328 *ref = NULL;
330 if (ref_is_absolute)
331 abs_refname = (char *)refname;
332 do {
333 linelen = getline(&line, &linesize, f);
334 if (linelen == -1) {
335 if (feof(f))
336 break;
337 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
338 break;
340 if (linelen > 0 && line[linelen - 1] == '\n')
341 line[linelen - 1] = '\0';
342 for (i = 0; i < nsubdirs; i++) {
343 if (!ref_is_absolute &&
344 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
345 refname) == -1)
346 return got_error_from_errno("asprintf");
347 err = parse_packed_ref_line(ref, abs_refname, line,
348 mtime, algo);
349 if (!ref_is_absolute)
350 free(abs_refname);
351 if (err || *ref != NULL)
352 break;
354 if (err)
355 break;
356 } while (*ref == NULL);
357 free(line);
359 return err;
362 static const struct got_error *
363 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
364 const char *name, int lock, enum got_hash_algorithm algo)
366 const struct got_error *err = NULL;
367 char *path = NULL;
368 char *absname = NULL;
369 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
370 int ref_is_well_known = (subdir[0] == '\0' && is_well_known_ref(name));
372 *ref = NULL;
374 if (!got_ref_name_is_valid(name))
375 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
377 if (ref_is_absolute || ref_is_well_known) {
378 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
379 return got_error_from_errno("asprintf");
380 absname = (char *)name;
381 } else {
382 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
383 subdir[0] ? "/" : "", name) == -1)
384 return got_error_from_errno("asprintf");
386 if (asprintf(&absname, "refs/%s%s%s",
387 subdir, subdir[0] ? "/" : "", name) == -1) {
388 err = got_error_from_errno("asprintf");
389 goto done;
393 err = parse_ref_file(ref, name, absname, path, lock, algo);
394 done:
395 if (!ref_is_absolute && !ref_is_well_known)
396 free(absname);
397 free(path);
398 return err;
401 const struct got_error *
402 got_ref_open(struct got_reference **ref, struct got_repository *repo,
403 const char *refname, int lock)
405 const struct got_error *err = NULL;
406 char *packed_refs_path = NULL, *path_refs = NULL;
407 const char *subdirs[] = {
408 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
410 size_t i;
411 int well_known = is_well_known_ref(refname);
412 struct got_lockfile *lf = NULL;
414 *ref = NULL;
416 path_refs = get_refs_dir_path(repo, refname);
417 if (path_refs == NULL) {
418 err = got_error_from_errno2("get_refs_dir_path", refname);
419 goto done;
422 if (well_known) {
423 err = open_ref(ref, path_refs, "", refname, lock,
424 got_repo_get_object_format(repo));
425 } else {
426 FILE *f;
428 /* Search on-disk refs before packed refs! */
429 for (i = 0; i < nitems(subdirs); i++) {
430 err = open_ref(ref, path_refs, subdirs[i], refname,
431 lock, got_repo_get_object_format(repo));
432 if ((err && err->code != GOT_ERR_NOT_REF) || *ref)
433 goto done;
436 packed_refs_path = got_repo_get_path_packed_refs(repo);
437 if (packed_refs_path == NULL) {
438 err = got_error_from_errno(
439 "got_repo_get_path_packed_refs");
440 goto done;
443 if (lock) {
444 err = got_lockfile_lock(&lf, packed_refs_path, -1);
445 if (err)
446 goto done;
448 f = fopen(packed_refs_path, "rbe");
449 if (f != NULL) {
450 struct stat sb;
451 if (fstat(fileno(f), &sb) == -1) {
452 err = got_error_from_errno2("fstat",
453 packed_refs_path);
454 goto done;
456 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
457 refname, sb.st_mtime,
458 got_repo_get_object_format(repo));
459 if (!err) {
460 if (fclose(f) == EOF) {
461 err = got_error_from_errno("fclose");
462 got_ref_close(*ref);
463 *ref = NULL;
464 } else if (*ref)
465 (*ref)->lf = lf;
469 done:
470 if (!err && *ref == NULL)
471 err = got_error_not_ref(refname);
472 if (err && lf)
473 got_lockfile_unlock(lf, -1);
474 free(packed_refs_path);
475 free(path_refs);
476 return err;
479 void
480 got_ref_close(struct got_reference *ref)
482 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
483 free(ref->ref.symref.name);
484 free(ref->ref.symref.ref);
485 } else
486 free(ref->ref.ref.name);
487 free(ref);
490 struct got_reference *
491 got_ref_dup(struct got_reference *ref)
493 struct got_reference *ret;
495 ret = calloc(1, sizeof(*ret));
496 if (ret == NULL)
497 return NULL;
499 ret->flags = ref->flags;
500 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
501 ret->ref.symref.name = strdup(ref->ref.symref.name);
502 if (ret->ref.symref.name == NULL) {
503 free(ret);
504 return NULL;
506 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
507 if (ret->ref.symref.ref == NULL) {
508 free(ret->ref.symref.name);
509 free(ret);
510 return NULL;
512 } else {
513 ret->ref.ref.name = strdup(ref->ref.ref.name);
514 if (ret->ref.ref.name == NULL) {
515 free(ret);
516 return NULL;
518 memcpy(&ret->ref.ref.id, &ref->ref.ref.id,
519 sizeof(ret->ref.ref.id));
522 return ret;
525 const struct got_error *
526 got_reflist_entry_dup(struct got_reflist_entry **newp,
527 struct got_reflist_entry *re)
529 const struct got_error *err = NULL;
530 struct got_reflist_entry *new;
532 *newp = NULL;
534 new = malloc(sizeof(*new));
535 if (new == NULL)
536 return got_error_from_errno("malloc");
538 new->ref = got_ref_dup(re->ref);
539 if (new->ref == NULL) {
540 err = got_error_from_errno("got_ref_dup");
541 free(new);
542 return err;
545 *newp = new;
546 return NULL;
549 const struct got_error *
550 got_ref_resolve_symbolic(struct got_reference **resolved,
551 struct got_repository *repo, struct got_reference *ref)
553 struct got_reference *nextref;
554 const struct got_error *err;
556 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
557 if (err)
558 return err;
560 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
561 err = got_ref_resolve_symbolic(resolved, repo, nextref);
562 else
563 *resolved = got_ref_dup(nextref);
565 got_ref_close(nextref);
566 return err;
569 static const struct got_error *
570 ref_resolve(struct got_object_id **id, struct got_repository *repo,
571 struct got_reference *ref, int recursion)
573 const struct got_error *err;
575 if (recursion <= 0)
576 return got_error_msg(GOT_ERR_RECURSION,
577 "reference recursion limit reached");
579 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
580 struct got_reference *resolved = NULL;
581 err = got_ref_resolve_symbolic(&resolved, repo, ref);
582 if (err == NULL)
583 err = ref_resolve(id, repo, resolved, --recursion);
584 if (resolved)
585 got_ref_close(resolved);
586 return err;
589 *id = calloc(1, sizeof(**id));
590 if (*id == NULL)
591 return got_error_from_errno("calloc");
592 memcpy(*id, &ref->ref.ref.id, sizeof(**id));
593 return NULL;
596 const struct got_error *
597 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
598 struct got_reference *ref)
600 return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
603 char *
604 got_ref_to_str(struct got_reference *ref)
606 char *str;
608 if (ref->flags & GOT_REF_IS_SYMBOLIC)
609 return strdup(ref->ref.symref.ref);
611 if (got_object_id_str(&str, &ref->ref.ref.id) != NULL)
612 return NULL;
614 return str;
617 const char *
618 got_ref_get_name(struct got_reference *ref)
620 if (ref->flags & GOT_REF_IS_SYMBOLIC)
621 return ref->ref.symref.name;
623 return ref->ref.ref.name;
626 const char *
627 got_ref_get_symref_target(struct got_reference *ref)
629 if (ref->flags & GOT_REF_IS_SYMBOLIC)
630 return ref->ref.symref.ref;
632 return NULL;
635 time_t
636 got_ref_get_mtime(struct got_reference *ref)
638 return ref->mtime;
641 const struct got_error *
642 got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
643 struct got_reference* re2)
645 const char *name1 = got_ref_get_name(re1);
646 const char *name2 = got_ref_get_name(re2);
648 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
649 return NULL;
652 const struct got_error *
653 got_ref_cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
654 struct got_reference *ref2)
656 const struct got_error *err = NULL;
657 struct got_repository *repo = arg;
658 struct got_object_id *id1, *id2 = NULL;
659 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
660 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
661 time_t time1, time2;
663 *cmp = 0;
665 err = got_ref_resolve(&id1, repo, ref1);
666 if (err)
667 return err;
668 err = got_object_open_as_tag(&tag1, repo, id1);
669 if (err) {
670 if (err->code != GOT_ERR_OBJ_TYPE)
671 goto done;
672 /* "lightweight" tag */
673 err = got_object_open_as_commit(&commit1, repo, id1);
674 if (err)
675 goto done;
676 time1 = got_object_commit_get_committer_time(commit1);
677 } else
678 time1 = got_object_tag_get_tagger_time(tag1);
680 err = got_ref_resolve(&id2, repo, ref2);
681 if (err)
682 goto done;
683 err = got_object_open_as_tag(&tag2, repo, id2);
684 if (err) {
685 if (err->code != GOT_ERR_OBJ_TYPE)
686 goto done;
687 /* "lightweight" tag */
688 err = got_object_open_as_commit(&commit2, repo, id2);
689 if (err)
690 goto done;
691 time2 = got_object_commit_get_committer_time(commit2);
692 } else
693 time2 = got_object_tag_get_tagger_time(tag2);
695 /* Put latest tags first. */
696 if (time1 < time2)
697 *cmp = 1;
698 else if (time1 > time2)
699 *cmp = -1;
700 else
701 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
702 done:
703 free(id1);
704 free(id2);
705 if (tag1)
706 got_object_tag_close(tag1);
707 if (tag2)
708 got_object_tag_close(tag2);
709 if (commit1)
710 got_object_commit_close(commit1);
711 if (commit2)
712 got_object_commit_close(commit2);
713 return err;
716 static const struct got_error *
717 get_committer_time(struct got_reference *ref, struct got_repository *repo)
719 const struct got_error *err = NULL;
720 int obj_type;
721 struct got_commit_object *commit = NULL;
722 struct got_tag_object *tag = NULL;
723 struct got_object_id *id = NULL;
725 err = got_ref_resolve(&id, repo, ref);
726 if (err)
727 return err;
729 err = got_object_get_type(&obj_type, repo, id);
730 if (err)
731 goto done;
733 switch (obj_type) {
734 case GOT_OBJ_TYPE_COMMIT:
735 err = got_object_open_as_commit(&commit, repo, id);
736 if (err)
737 goto done;
738 ref->committer_time =
739 got_object_commit_get_committer_time(commit);
740 break;
741 case GOT_OBJ_TYPE_TAG:
742 err = got_object_open_as_tag(&tag, repo, id);
743 if (err)
744 goto done;
745 ref->committer_time = got_object_tag_get_tagger_time(tag);
746 break;
747 default:
748 /* best effort for other object types */
749 ref->committer_time = got_ref_get_mtime(ref);
750 break;
752 done:
753 free(id);
754 if (commit)
755 got_object_commit_close(commit);
756 if (tag)
757 got_object_tag_close(tag);
758 return err;
761 const struct got_error *
762 got_ref_cmp_by_commit_timestamp_descending(void *arg, int *cmp,
763 struct got_reference *ref1, struct got_reference *ref2)
765 const struct got_error *err = NULL;
766 struct got_repository *repo = arg;
768 *cmp = 0;
770 if (ref1->committer_time == 0) {
771 err = get_committer_time(ref1, repo);
772 if (err)
773 return err;
775 if (ref2->committer_time == 0) {
776 err = get_committer_time(ref2, repo);
777 if (err)
778 return err;
781 if (ref1->committer_time < ref2->committer_time)
782 *cmp = 1;
783 else if (ref2->committer_time < ref1->committer_time)
784 *cmp = -1;
785 else
786 return got_ref_cmp_by_name(arg, cmp, ref1, ref2);
788 return err;
791 const struct got_error *
792 got_reflist_insert(struct got_reflist_entry **newp,
793 struct got_reflist_head *refs, struct got_reference *ref,
794 got_ref_cmp_cb cmp_cb, void *cmp_arg)
796 const struct got_error *err;
797 struct got_reflist_entry *new, *re;
798 int cmp;
800 *newp = NULL;
802 if (cmp_cb != got_ref_cmp_by_name && (ref->flags & GOT_REF_IS_PACKED)) {
804 * If we are not sorting elements by name then we must still
805 * detect collisions between a packed ref and an on-disk ref
806 * using the same name. On-disk refs take precedence and are
807 * already present on the list before packed refs get added.
809 TAILQ_FOREACH(re, refs, entry) {
810 err = got_ref_cmp_by_name(NULL, &cmp, re->ref, ref);
811 if (err)
812 return err;
813 if (cmp == 0)
814 return NULL;
818 new = malloc(sizeof(*new));
819 if (new == NULL)
820 return got_error_from_errno("malloc");
821 new->ref = ref;
822 *newp = new;
825 * We must de-duplicate entries on insert because packed-refs may
826 * contain redundant entries. On-disk refs take precedence.
827 * This code assumes that on-disk revs are read before packed-refs.
828 * We're iterating the list anyway, so insert elements sorted by name.
830 * Many callers will provide paths in a somewhat sorted order.
831 * Iterating backwards from the tail of the list should be more
832 * efficient than traversing through the entire list each time
833 * an element is inserted.
835 re = TAILQ_LAST(refs, got_reflist_head);
836 while (re) {
837 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
838 if (err)
839 return err;
840 if (cmp == 0) {
841 /* duplicate */
842 free(new);
843 *newp = NULL;
844 return NULL;
845 } else if (cmp < 0) {
846 TAILQ_INSERT_AFTER(refs, re, new, entry);
847 return NULL;
849 re = TAILQ_PREV(re, got_reflist_head, entry);
852 TAILQ_INSERT_HEAD(refs, new, entry);
853 return NULL;
856 const struct got_error *
857 got_reflist_sort(struct got_reflist_head *refs,
858 got_ref_cmp_cb cmp_cb, void *cmp_arg)
860 const struct got_error *err = NULL;
861 struct got_reflist_entry *re, *tmp, *new;
862 struct got_reflist_head sorted;
864 TAILQ_INIT(&sorted);
866 TAILQ_FOREACH_SAFE(re, refs, entry, tmp) {
867 struct got_reference *ref = re->ref;
868 TAILQ_REMOVE(refs, re, entry);
869 free(re);
870 err = got_reflist_insert(&new, &sorted, ref, cmp_cb, cmp_arg);
871 if (err || new == NULL /* duplicate */)
872 got_ref_close(ref);
873 if (err)
874 return err;
877 TAILQ_CONCAT(refs, &sorted, entry);
878 return NULL;
881 static const struct got_error *
882 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
883 const char *subdir, struct got_repository *repo,
884 got_ref_cmp_cb cmp_cb, void *cmp_arg)
886 const struct got_error *err = NULL;
887 DIR *d = NULL;
888 char *path_subdir;
890 while (subdir[0] == '/')
891 subdir++;
893 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
894 return got_error_from_errno("asprintf");
896 d = opendir(path_subdir);
897 if (d == NULL)
898 goto done;
900 for (;;) {
901 struct dirent *dent;
902 struct got_reference *ref;
903 char *child;
904 int type;
906 dent = readdir(d);
907 if (dent == NULL)
908 break;
910 if (strcmp(dent->d_name, ".") == 0 ||
911 strcmp(dent->d_name, "..") == 0)
912 continue;
914 err = got_path_dirent_type(&type, path_subdir, dent);
915 if (err)
916 break;
918 switch (type) {
919 case DT_REG:
920 err = open_ref(&ref, path_refs, subdir, dent->d_name,
921 0, got_repo_get_object_format(repo));
922 if (err && err->code == GOT_ERR_BAD_REF_NAME)
923 break;
924 if (err)
925 goto done;
926 if (ref) {
927 struct got_reflist_entry *new;
928 err = got_reflist_insert(&new, refs, ref,
929 cmp_cb, cmp_arg);
930 if (err || new == NULL /* duplicate */)
931 got_ref_close(ref);
932 if (err)
933 goto done;
935 break;
936 case DT_DIR:
937 if (asprintf(&child, "%s%s%s", subdir,
938 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
939 err = got_error_from_errno("asprintf");
940 break;
942 err = gather_on_disk_refs(refs, path_refs, child, repo,
943 cmp_cb, cmp_arg);
944 free(child);
945 break;
946 default:
947 break;
950 done:
951 if (d)
952 closedir(d);
953 free(path_subdir);
954 return err;
957 const struct got_error *
958 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
959 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
961 const struct got_error *err;
962 char *packed_refs_path = NULL, *path_refs = NULL;
963 char *abs_namespace = NULL, *buf = NULL;
964 const char *ondisk_ref_namespace = NULL;
965 char *line = NULL;
966 FILE *f = NULL;
967 struct got_reference *ref;
968 struct got_reflist_entry *new;
970 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
971 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
972 if (path_refs == NULL) {
973 err = got_error_from_errno("get_refs_dir_path");
974 goto done;
976 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0,
977 got_repo_get_object_format(repo));
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 got_repo_get_object_format(repo));
995 if (err) {
996 if (err->code != GOT_ERR_NOT_REF)
997 goto done;
998 /* Try to look up references in a given namespace. */
999 } else {
1000 err = got_reflist_insert(&new, refs, ref,
1001 cmp_cb, cmp_arg);
1002 if (err || new == NULL /* duplicate */)
1003 got_ref_close(ref);
1004 return err;
1008 if (ref_namespace) {
1009 size_t len;
1010 /* Canonicalize the path to eliminate double-slashes if any. */
1011 if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
1012 err = got_error_from_errno("asprintf");
1013 goto done;
1015 len = strlen(abs_namespace) + 1;
1016 buf = malloc(len);
1017 if (buf == NULL) {
1018 err = got_error_from_errno("malloc");
1019 goto done;
1021 err = got_canonpath(abs_namespace, buf, len);
1022 if (err)
1023 goto done;
1024 ondisk_ref_namespace = buf;
1025 while (ondisk_ref_namespace[0] == '/')
1026 ondisk_ref_namespace++;
1027 if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
1028 ondisk_ref_namespace += 5;
1029 else if (strcmp(ondisk_ref_namespace, "refs") == 0)
1030 ondisk_ref_namespace = "";
1033 /* Gather on-disk refs before parsing packed-refs. */
1034 free(path_refs);
1035 path_refs = get_refs_dir_path(repo, "");
1036 if (path_refs == NULL) {
1037 err = got_error_from_errno("get_refs_dir_path");
1038 goto done;
1040 err = gather_on_disk_refs(refs, path_refs,
1041 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
1042 cmp_cb, cmp_arg);
1043 if (err)
1044 goto done;
1047 * The packed-refs file may contain redundant entries, in which
1048 * case on-disk refs take precedence.
1050 packed_refs_path = got_repo_get_path_packed_refs(repo);
1051 if (packed_refs_path == NULL) {
1052 err = got_error_from_errno("got_repo_get_path_packed_refs");
1053 goto done;
1056 f = fopen(packed_refs_path, "re");
1057 if (f) {
1058 size_t linesize = 0;
1059 ssize_t linelen;
1060 struct stat sb;
1062 if (fstat(fileno(f), &sb) == -1) {
1063 err = got_error_from_errno2("fstat", packed_refs_path);
1064 goto done;
1066 for (;;) {
1067 linelen = getline(&line, &linesize, f);
1068 if (linelen == -1) {
1069 if (feof(f))
1070 break;
1071 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1072 goto done;
1074 if (linelen > 0 && line[linelen - 1] == '\n')
1075 line[linelen - 1] = '\0';
1076 err = parse_packed_ref_line(&ref, NULL, line,
1077 sb.st_mtime, got_repo_get_object_format(repo));
1078 if (err)
1079 goto done;
1080 if (ref) {
1081 if (ref_namespace) {
1082 const char *name;
1083 name = got_ref_get_name(ref);
1084 if (!got_path_is_child(name,
1085 ref_namespace,
1086 strlen(ref_namespace))) {
1087 got_ref_close(ref);
1088 continue;
1091 err = got_reflist_insert(&new, refs, ref,
1092 cmp_cb, cmp_arg);
1093 if (err || new == NULL /* duplicate */)
1094 got_ref_close(ref);
1095 if (err)
1096 goto done;
1100 done:
1101 free(packed_refs_path);
1102 free(abs_namespace);
1103 free(buf);
1104 free(line);
1105 free(path_refs);
1106 if (f && fclose(f) == EOF && err == NULL)
1107 err = got_error_from_errno("fclose");
1108 return err;
1111 void
1112 got_ref_list_free(struct got_reflist_head *refs)
1114 struct got_reflist_entry *re;
1116 while ((re = TAILQ_FIRST(refs))) {
1117 TAILQ_REMOVE(refs, re, entry);
1118 got_ref_close(re->ref);
1119 free(re);
1124 got_ref_is_symbolic(struct got_reference *ref)
1126 return (ref->flags & GOT_REF_IS_SYMBOLIC);
1129 const struct got_error *
1130 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1132 if (ref->flags & GOT_REF_IS_SYMBOLIC)
1133 return got_error(GOT_ERR_BAD_REF_TYPE);
1135 memcpy(&ref->ref.ref.id, id, sizeof(ref->ref.ref.id));
1136 return NULL;
1139 const struct got_error *
1140 got_ref_change_symref(struct got_reference *ref, const char *refname)
1142 char *new_name;
1144 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1145 return got_error(GOT_ERR_BAD_REF_TYPE);
1147 new_name = strdup(refname);
1148 if (new_name == NULL)
1149 return got_error_from_errno("strdup");
1151 free(ref->ref.symref.ref);
1152 ref->ref.symref.ref = new_name;
1153 return NULL;
1156 const struct got_error *
1157 got_ref_change_symref_to_ref(struct got_reference *symref,
1158 struct got_object_id *id)
1160 if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1161 return got_error(GOT_ERR_BAD_REF_TYPE);
1163 symref->ref.ref.name = symref->ref.symref.name;
1164 memcpy(&symref->ref.ref.id, id, sizeof(symref->ref.ref.id));
1165 symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1166 return NULL;
1169 const struct got_error *
1170 got_ref_write(struct got_reference *ref, struct got_repository *repo)
1172 const struct got_error *err = NULL, *unlock_err = NULL;
1173 const char *name = got_ref_get_name(ref);
1174 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1175 struct got_lockfile *lf = NULL;
1176 FILE *f = NULL;
1177 size_t n;
1178 struct stat sb;
1180 path_refs = get_refs_dir_path(repo, name);
1181 if (path_refs == NULL) {
1182 err = got_error_from_errno2("get_refs_dir_path", name);
1183 goto done;
1186 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1187 err = got_error_from_errno("asprintf");
1188 goto done;
1191 err = got_opentemp_named(&tmppath, &f, path, "");
1192 if (err) {
1193 char *parent;
1194 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1195 goto done;
1196 err = got_path_dirname(&parent, path);
1197 if (err)
1198 goto done;
1199 err = got_path_mkdir(parent);
1200 free(parent);
1201 if (err)
1202 goto done;
1203 err = got_opentemp_named(&tmppath, &f, path, "");
1204 if (err)
1205 goto done;
1208 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1209 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1210 if (n != strlen(ref->ref.symref.ref) + 6) {
1211 err = got_ferror(f, GOT_ERR_IO);
1212 goto done;
1214 } else {
1215 char *hex;
1216 size_t len;
1218 err = got_object_id_str(&hex, &ref->ref.ref.id);
1219 if (err)
1220 goto done;
1221 len = strlen(hex);
1222 n = fprintf(f, "%s\n", hex);
1223 free(hex);
1224 if (n != len + 1) {
1225 err = got_ferror(f, GOT_ERR_IO);
1226 goto done;
1230 if (ref->lf == NULL) {
1231 err = got_lockfile_lock(&lf, path, -1);
1232 if (err)
1233 goto done;
1236 /* XXX: check if old content matches our expectations? */
1238 if (stat(path, &sb) != 0) {
1239 if (errno != ENOENT) {
1240 err = got_error_from_errno2("stat", path);
1241 goto done;
1243 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1246 if (fchmod(fileno(f), sb.st_mode) != 0) {
1247 err = got_error_from_errno2("fchmod", tmppath);
1248 goto done;
1251 if (rename(tmppath, path) != 0) {
1252 err = got_error_from_errno3("rename", tmppath, path);
1253 goto done;
1255 free(tmppath);
1256 tmppath = NULL;
1258 if (stat(path, &sb) == -1) {
1259 err = got_error_from_errno2("stat", path);
1260 goto done;
1262 ref->mtime = sb.st_mtime;
1263 done:
1264 if (ref->lf == NULL && lf)
1265 unlock_err = got_lockfile_unlock(lf, -1);
1266 if (f) {
1267 if (fclose(f) == EOF && err == NULL)
1268 err = got_error_from_errno("fclose");
1270 free(path_refs);
1271 free(path);
1272 if (tmppath) {
1273 if (unlink(tmppath) == -1 && err == NULL)
1274 err = got_error_from_errno2("unlink", tmppath);
1275 free(tmppath);
1277 return err ? err : unlock_err;
1280 static const struct got_error *
1281 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1283 const struct got_error *err = NULL, *unlock_err = NULL;
1284 struct got_lockfile *lf = NULL;
1285 FILE *f = NULL, *tmpf = NULL;
1286 char *line = NULL, *packed_refs_path, *tmppath = NULL;
1287 size_t linesize = 0;
1288 struct got_reflist_head refs;
1289 int found_delref = 0;
1291 /* The packed-refs file does not cotain symbolic references. */
1292 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1293 return got_error(GOT_ERR_BAD_REF_DATA);
1295 TAILQ_INIT(&refs);
1297 packed_refs_path = got_repo_get_path_packed_refs(repo);
1298 if (packed_refs_path == NULL)
1299 return got_error_from_errno("got_repo_get_path_packed_refs");
1301 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path, "");
1302 if (err)
1303 goto done;
1305 if (delref->lf == NULL) {
1306 err = got_lockfile_lock(&lf, packed_refs_path, -1);
1307 if (err)
1308 goto done;
1311 f = fopen(packed_refs_path, "re");
1312 if (f == NULL) {
1313 err = got_error_from_errno2("fopen", packed_refs_path);
1314 goto done;
1316 for (;;) {
1317 ssize_t linelen;
1318 struct got_reference *ref;
1319 struct got_reflist_entry *new;
1321 linelen = getline(&line, &linesize, f);
1322 if (linelen == -1) {
1323 if (feof(f))
1324 break;
1325 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1326 goto done;
1328 if (linelen > 0 && line[linelen - 1] == '\n')
1329 line[linelen - 1] = '\0';
1330 err = parse_packed_ref_line(&ref, NULL, line, 0,
1331 got_repo_get_object_format(repo));
1332 if (err)
1333 goto done;
1334 if (ref == NULL)
1335 continue;
1337 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1338 got_object_id_cmp(&ref->ref.ref.id,
1339 &delref->ref.ref.id) == 0) {
1340 found_delref = 1;
1341 got_ref_close(ref);
1342 continue;
1345 err = got_reflist_insert(&new, &refs, ref,
1346 got_ref_cmp_by_name, NULL);
1347 if (err || new == NULL /* duplicate */)
1348 got_ref_close(ref);
1349 if (err)
1350 goto done;
1353 if (found_delref) {
1354 struct got_reflist_entry *re;
1355 size_t n;
1356 struct stat sb;
1358 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1359 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1360 err = got_ferror(f, GOT_ERR_IO);
1361 goto done;
1364 TAILQ_FOREACH(re, &refs, entry) {
1365 char *hex;
1366 size_t len;
1368 err = got_object_id_str(&hex, &re->ref->ref.ref.id);
1369 if (err)
1370 goto done;
1371 len = strlen(hex);
1372 n = fprintf(tmpf, "%s ", hex);
1373 free(hex);
1374 if (n != len + 1) {
1375 err = got_ferror(f, GOT_ERR_IO);
1376 goto done;
1379 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1380 if (n != strlen(re->ref->ref.ref.name) + 1) {
1381 err = got_ferror(f, GOT_ERR_IO);
1382 goto done;
1386 if (fflush(tmpf) != 0) {
1387 err = got_error_from_errno("fflush");
1388 goto done;
1391 if (fstat(fileno(f), &sb) != 0) {
1392 if (errno != ENOENT) {
1393 err = got_error_from_errno2("fstat",
1394 packed_refs_path);
1395 goto done;
1397 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1400 if (fchmod(fileno(tmpf), sb.st_mode) != 0) {
1401 err = got_error_from_errno2("fchmod", tmppath);
1402 goto done;
1405 if (rename(tmppath, packed_refs_path) != 0) {
1406 err = got_error_from_errno3("rename", tmppath,
1407 packed_refs_path);
1408 goto done;
1410 free(tmppath);
1411 tmppath = NULL;
1413 done:
1414 if (delref->lf == NULL && lf)
1415 unlock_err = got_lockfile_unlock(lf, -1);
1416 if (f) {
1417 if (fclose(f) == EOF && err == NULL)
1418 err = got_error_from_errno("fclose");
1420 if (tmppath && unlink(tmppath) == -1 && err == NULL)
1421 err = got_error_from_errno2("unlink", tmppath);
1422 if (tmpf && fclose(tmpf) == EOF && err == NULL)
1423 err = got_error_from_errno("fclose");
1424 free(tmppath);
1425 free(packed_refs_path);
1426 free(line);
1427 got_ref_list_free(&refs);
1428 return err ? err : unlock_err;
1431 static const struct got_error *
1432 delete_loose_ref(struct got_reference *ref, struct got_repository *repo)
1434 const struct got_error *err = NULL, *unlock_err = NULL;
1435 const char *name = got_ref_get_name(ref);
1436 char *path_refs = NULL, *path = NULL;
1437 struct got_lockfile *lf = NULL;
1439 path_refs = get_refs_dir_path(repo, name);
1440 if (path_refs == NULL) {
1441 err = got_error_from_errno2("get_refs_dir_path", name);
1442 goto done;
1445 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1446 err = got_error_from_errno("asprintf");
1447 goto done;
1450 if (ref->lf == NULL) {
1451 err = got_lockfile_lock(&lf, path, -1);
1452 if (err)
1453 goto done;
1456 /* XXX: check if old content matches our expectations? */
1458 if (unlink(path) == -1)
1459 err = got_error_from_errno2("unlink", path);
1460 done:
1461 if (ref->lf == NULL && lf)
1462 unlock_err = got_lockfile_unlock(lf, -1);
1464 free(path_refs);
1465 free(path);
1466 return err ? err : unlock_err;
1469 const struct got_error *
1470 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1472 const struct got_error *err = NULL;
1473 struct got_reference *ref2;
1475 if (ref->flags & GOT_REF_IS_PACKED) {
1476 err = delete_packed_ref(ref, repo);
1477 if (err)
1478 return err;
1480 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1481 if (err) {
1482 if (err->code == GOT_ERR_NOT_REF)
1483 return NULL;
1484 return err;
1487 err = delete_loose_ref(ref2, repo);
1488 got_ref_close(ref2);
1489 return err;
1490 } else {
1491 err = delete_loose_ref(ref, repo);
1492 if (err)
1493 return err;
1495 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1496 if (err) {
1497 if (err->code == GOT_ERR_NOT_REF)
1498 return NULL;
1499 return err;
1502 err = delete_packed_ref(ref2, repo);
1503 got_ref_close(ref2);
1504 return err;
1508 const struct got_error *
1509 got_ref_unlock(struct got_reference *ref)
1511 const struct got_error *err;
1512 err = got_lockfile_unlock(ref->lf, -1);
1513 ref->lf = NULL;
1514 return err;
1517 struct got_reflist_object_id_map {
1518 struct got_object_idset *idset;
1521 struct got_reflist_object_id_map_entry {
1522 struct got_reflist_head refs;
1525 static const struct got_error *
1526 add_object_id_map_entry(struct got_object_idset *idset,
1527 struct got_object_id *id, struct got_reflist_entry *re)
1529 const struct got_error *err = NULL;
1530 struct got_reflist_object_id_map_entry *ent;
1531 struct got_reflist_entry *new;
1533 ent = got_object_idset_get(idset, id);
1534 if (ent == NULL) {
1535 ent = malloc(sizeof(*ent));
1536 if (ent == NULL)
1537 return got_error_from_errno("malloc");
1539 TAILQ_INIT(&ent->refs);
1540 err = got_object_idset_add(idset, id, ent);
1541 if (err) {
1542 free(ent);
1543 return err;
1547 err = got_reflist_entry_dup(&new, re);
1548 if (err)
1549 return err;
1551 TAILQ_INSERT_TAIL(&ent->refs, new, entry);
1552 return NULL;
1555 const struct got_error *
1556 got_reflist_object_id_map_create(struct got_reflist_object_id_map **map,
1557 struct got_reflist_head *refs, struct got_repository *repo)
1559 const struct got_error *err = NULL;
1560 struct got_object_idset *idset;
1561 struct got_object_id *id = NULL;
1562 struct got_reflist_entry *re;
1564 idset = got_object_idset_alloc();
1565 if (idset == NULL)
1566 return got_error_from_errno("got_object_idset_alloc");
1568 *map = malloc(sizeof(**map));
1569 if (*map == NULL) {
1570 got_object_idset_free(idset);
1571 return got_error_from_errno("malloc");
1573 (*map)->idset = idset;
1575 TAILQ_FOREACH(re, refs, entry) {
1576 struct got_tag_object *tag = NULL;
1578 err = got_ref_resolve(&id, repo, re->ref);
1579 if (err)
1580 goto done;
1582 err = add_object_id_map_entry(idset, id, re);
1583 if (err)
1584 goto done;
1586 if (strstr(got_ref_get_name(re->ref), "/tags/") == NULL) {
1587 free(id);
1588 id = NULL;
1589 continue;
1592 err = got_object_open_as_tag(&tag, repo, id);
1593 if (err) {
1594 if (err->code != GOT_ERR_OBJ_TYPE)
1595 goto done;
1596 /* Ref points at something other than a tag. */
1597 err = NULL;
1598 tag = NULL;
1599 free(id);
1600 id = NULL;
1601 continue;
1604 err = add_object_id_map_entry(idset,
1605 got_object_tag_get_object_id(tag), re);
1606 got_object_tag_close(tag);
1607 if (err)
1608 goto done;
1610 free(id);
1611 id = NULL;
1613 done:
1614 free(id);
1615 if (err) {
1616 got_reflist_object_id_map_free(*map);
1617 *map = NULL;
1619 return err;
1622 struct got_reflist_head *
1623 got_reflist_object_id_map_lookup(struct got_reflist_object_id_map *map,
1624 struct got_object_id *id)
1626 struct got_reflist_object_id_map_entry *ent;
1627 ent = got_object_idset_get(map->idset, id);
1628 if (ent)
1629 return &ent->refs;
1630 return NULL;
1633 static const struct got_error *
1634 free_id_map_entry(struct got_object_id *id, void *data, void *arg)
1636 struct got_reflist_object_id_map_entry *ent = data;
1638 got_ref_list_free(&ent->refs);
1639 free(ent);
1640 return NULL;
1643 void
1644 got_reflist_object_id_map_free(struct got_reflist_object_id_map *map)
1646 got_object_idset_for_each(map->idset, free_id_map_entry, NULL);
1647 got_object_idset_free(map->idset);
1648 free(map);