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, enum got_hash_algorithm algo)
158 struct got_object_id id;
160 if (strncmp(line, "ref: ", 5) == 0) {
161 line += 5;
162 return parse_symref(ref, name, line);
165 memset(&id, 0, sizeof(id));
166 id.algo = algo;
167 if (!got_parse_hash_digest(id.hash, line, algo))
168 return got_error(GOT_ERR_BAD_REF_DATA);
170 return alloc_ref(ref, name, &id, 0, mtime);
173 static const struct got_error *
174 parse_ref_file(struct got_reference **ref, const char *name,
175 const char *absname, const char *abspath, int lock,
176 enum got_hash_algorithm algo)
178 const struct got_error *err = NULL;
179 FILE *f;
180 char *line = NULL;
181 size_t linesize = 0;
182 ssize_t linelen;
183 struct got_lockfile *lf = NULL;
184 struct stat sb;
186 if (lock) {
187 err = got_lockfile_lock(&lf, abspath, -1);
188 if (err) {
189 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
190 err = got_error_not_ref(name);
191 return err;
195 f = fopen(abspath, "rbe");
196 if (f == NULL) {
197 if (errno != ENOTDIR && errno != ENOENT)
198 err = got_error_from_errno2("fopen", abspath);
199 else
200 err = got_error_not_ref(name);
201 if (lock)
202 got_lockfile_unlock(lf, -1);
203 return err;
205 if (fstat(fileno(f), &sb) == -1) {
206 err = got_error_from_errno2("fstat", abspath);
207 goto done;
210 linelen = getline(&line, &linesize, f);
211 if (linelen == -1) {
212 if (feof(f))
213 err = NULL; /* ignore empty files (could be locks) */
214 else {
215 if (errno == EISDIR)
216 err = got_error(GOT_ERR_NOT_REF);
217 else if (ferror(f))
218 err = got_ferror(f, GOT_ERR_IO);
219 else
220 err = got_error_from_errno2("getline", abspath);
222 if (lock)
223 got_lockfile_unlock(lf, -1);
224 goto done;
226 while (linelen > 0 && line[linelen - 1] == '\n') {
227 line[linelen - 1] = '\0';
228 linelen--;
231 err = parse_ref_line(ref, absname, line, sb.st_mtime, algo);
232 if (lock) {
233 if (err)
234 got_lockfile_unlock(lf, -1);
235 else {
236 if (*ref)
237 (*ref)->lf = lf;
238 else
239 got_lockfile_unlock(lf, -1);
242 done:
243 free(line);
244 if (fclose(f) == EOF && err == NULL) {
245 err = got_error_from_errno("fclose");
246 if (*ref) {
247 if (lock)
248 got_ref_unlock(*ref);
249 got_ref_close(*ref);
250 *ref = NULL;
253 return err;
256 static int
257 is_well_known_ref(const char *refname)
259 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
260 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
261 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
262 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
265 static char *
266 get_refs_dir_path(struct got_repository *repo, const char *refname)
268 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
269 return strdup(got_repo_get_path_git_dir(repo));
271 return got_repo_get_path_refs(repo);
274 const struct got_error *
275 got_ref_alloc(struct got_reference **ref, const char *name,
276 struct got_object_id *id)
278 if (!got_ref_name_is_valid(name))
279 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
281 return alloc_ref(ref, name, id, 0, 0);
284 const struct got_error *
285 got_ref_alloc_symref(struct got_reference **ref, const char *name,
286 struct got_reference *target_ref)
288 if (!got_ref_name_is_valid(name))
289 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
291 return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
294 static const struct got_error *
295 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
296 const char *line, time_t mtime, enum got_hash_algorithm algo)
298 struct got_object_id id;
299 const char *name;
301 *ref = NULL;
303 if (line[0] == '#' || line[0] == '^')
304 return NULL;
306 memset(&id, 0, sizeof(id));
307 id.algo = algo;
308 if (!got_parse_hash_digest(id.hash, line, algo))
309 return got_error(GOT_ERR_BAD_REF_DATA);
311 if (abs_refname) {
312 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
313 return NULL;
314 name = abs_refname;
315 } else
316 name = line + SHA1_DIGEST_STRING_LENGTH;
318 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED, mtime);
321 static const struct got_error *
322 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
323 int nsubdirs, const char *refname, time_t mtime,
324 enum got_hash_algorithm algo)
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, algo);
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, enum got_hash_algorithm algo)
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, algo);
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 got_repo_get_object_format(repo));
430 } else {
431 FILE *f;
433 /* Search on-disk refs before packed refs! */
434 for (i = 0; i < nitems(subdirs); i++) {
435 err = open_ref(ref, path_refs, subdirs[i], refname,
436 lock, got_repo_get_object_format(repo));
437 if ((err && err->code != GOT_ERR_NOT_REF) || *ref)
438 goto done;
441 packed_refs_path = got_repo_get_path_packed_refs(repo);
442 if (packed_refs_path == NULL) {
443 err = got_error_from_errno(
444 "got_repo_get_path_packed_refs");
445 goto done;
448 if (lock) {
449 err = got_lockfile_lock(&lf, packed_refs_path, -1);
450 if (err)
451 goto done;
453 f = fopen(packed_refs_path, "rbe");
454 if (f != NULL) {
455 struct stat sb;
456 if (fstat(fileno(f), &sb) == -1) {
457 err = got_error_from_errno2("fstat",
458 packed_refs_path);
459 goto done;
461 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
462 refname, sb.st_mtime,
463 got_repo_get_object_format(repo));
464 if (!err) {
465 if (fclose(f) == EOF) {
466 err = got_error_from_errno("fclose");
467 got_ref_close(*ref);
468 *ref = NULL;
469 } else if (*ref)
470 (*ref)->lf = lf;
474 done:
475 if (!err && *ref == NULL)
476 err = got_error_not_ref(refname);
477 if (err && lf)
478 got_lockfile_unlock(lf, -1);
479 free(packed_refs_path);
480 free(path_refs);
481 return err;
484 void
485 got_ref_close(struct got_reference *ref)
487 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
488 free(ref->ref.symref.name);
489 free(ref->ref.symref.ref);
490 } else
491 free(ref->ref.ref.name);
492 free(ref);
495 struct got_reference *
496 got_ref_dup(struct got_reference *ref)
498 struct got_reference *ret;
500 ret = calloc(1, sizeof(*ret));
501 if (ret == NULL)
502 return NULL;
504 ret->flags = ref->flags;
505 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
506 ret->ref.symref.name = strdup(ref->ref.symref.name);
507 if (ret->ref.symref.name == NULL) {
508 free(ret);
509 return NULL;
511 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
512 if (ret->ref.symref.ref == NULL) {
513 free(ret->ref.symref.name);
514 free(ret);
515 return NULL;
517 } else {
518 ret->ref.ref.name = strdup(ref->ref.ref.name);
519 if (ret->ref.ref.name == NULL) {
520 free(ret);
521 return NULL;
523 memcpy(&ret->ref.ref.id, &ref->ref.ref.id,
524 sizeof(ret->ref.ref.id));
527 return ret;
530 const struct got_error *
531 got_reflist_entry_dup(struct got_reflist_entry **newp,
532 struct got_reflist_entry *re)
534 const struct got_error *err = NULL;
535 struct got_reflist_entry *new;
537 *newp = NULL;
539 new = malloc(sizeof(*new));
540 if (new == NULL)
541 return got_error_from_errno("malloc");
543 new->ref = got_ref_dup(re->ref);
544 if (new->ref == NULL) {
545 err = got_error_from_errno("got_ref_dup");
546 free(new);
547 return err;
550 *newp = new;
551 return NULL;
554 const struct got_error *
555 got_ref_resolve_symbolic(struct got_reference **resolved,
556 struct got_repository *repo, struct got_reference *ref)
558 struct got_reference *nextref;
559 const struct got_error *err;
561 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
562 if (err)
563 return err;
565 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
566 err = got_ref_resolve_symbolic(resolved, repo, nextref);
567 else
568 *resolved = got_ref_dup(nextref);
570 got_ref_close(nextref);
571 return err;
574 static const struct got_error *
575 ref_resolve(struct got_object_id **id, struct got_repository *repo,
576 struct got_reference *ref, int recursion)
578 const struct got_error *err;
580 if (recursion <= 0)
581 return got_error_msg(GOT_ERR_RECURSION,
582 "reference recursion limit reached");
584 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
585 struct got_reference *resolved = NULL;
586 err = got_ref_resolve_symbolic(&resolved, repo, ref);
587 if (err == NULL)
588 err = ref_resolve(id, repo, resolved, --recursion);
589 if (resolved)
590 got_ref_close(resolved);
591 return err;
594 *id = calloc(1, sizeof(**id));
595 if (*id == NULL)
596 return got_error_from_errno("calloc");
597 memcpy(*id, &ref->ref.ref.id, sizeof(**id));
599 return NULL;
602 const struct got_error *
603 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
604 struct got_reference *ref)
606 return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
609 char *
610 got_ref_to_str(struct got_reference *ref)
612 char *str;
614 if (ref->flags & GOT_REF_IS_SYMBOLIC)
615 return strdup(ref->ref.symref.ref);
617 if (got_object_id_str(&str, &ref->ref.ref.id) != NULL)
618 return NULL;
620 return str;
623 const char *
624 got_ref_get_name(struct got_reference *ref)
626 if (ref->flags & GOT_REF_IS_SYMBOLIC)
627 return ref->ref.symref.name;
629 return ref->ref.ref.name;
632 const char *
633 got_ref_get_symref_target(struct got_reference *ref)
635 if (ref->flags & GOT_REF_IS_SYMBOLIC)
636 return ref->ref.symref.ref;
638 return NULL;
641 time_t
642 got_ref_get_mtime(struct got_reference *ref)
644 return ref->mtime;
647 const struct got_error *
648 got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
649 struct got_reference* re2)
651 const char *name1 = got_ref_get_name(re1);
652 const char *name2 = got_ref_get_name(re2);
654 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
655 return NULL;
658 const struct got_error *
659 got_ref_cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
660 struct got_reference *ref2)
662 const struct got_error *err = NULL;
663 struct got_repository *repo = arg;
664 struct got_object_id *id1, *id2 = NULL;
665 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
666 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
667 time_t time1, time2;
669 *cmp = 0;
671 err = got_ref_resolve(&id1, repo, ref1);
672 if (err)
673 return err;
674 err = got_object_open_as_tag(&tag1, repo, id1);
675 if (err) {
676 if (err->code != GOT_ERR_OBJ_TYPE)
677 goto done;
678 /* "lightweight" tag */
679 err = got_object_open_as_commit(&commit1, repo, id1);
680 if (err)
681 goto done;
682 time1 = got_object_commit_get_committer_time(commit1);
683 } else
684 time1 = got_object_tag_get_tagger_time(tag1);
686 err = got_ref_resolve(&id2, repo, ref2);
687 if (err)
688 goto done;
689 err = got_object_open_as_tag(&tag2, repo, id2);
690 if (err) {
691 if (err->code != GOT_ERR_OBJ_TYPE)
692 goto done;
693 /* "lightweight" tag */
694 err = got_object_open_as_commit(&commit2, repo, id2);
695 if (err)
696 goto done;
697 time2 = got_object_commit_get_committer_time(commit2);
698 } else
699 time2 = got_object_tag_get_tagger_time(tag2);
701 /* Put latest tags first. */
702 if (time1 < time2)
703 *cmp = 1;
704 else if (time1 > time2)
705 *cmp = -1;
706 else
707 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
708 done:
709 free(id1);
710 free(id2);
711 if (tag1)
712 got_object_tag_close(tag1);
713 if (tag2)
714 got_object_tag_close(tag2);
715 if (commit1)
716 got_object_commit_close(commit1);
717 if (commit2)
718 got_object_commit_close(commit2);
719 return err;
722 static const struct got_error *
723 get_committer_time(struct got_reference *ref, struct got_repository *repo)
725 const struct got_error *err = NULL;
726 int obj_type;
727 struct got_commit_object *commit = NULL;
728 struct got_tag_object *tag = NULL;
729 struct got_object_id *id = NULL;
731 err = got_ref_resolve(&id, repo, ref);
732 if (err)
733 return err;
735 err = got_object_get_type(&obj_type, repo, id);
736 if (err)
737 goto done;
739 switch (obj_type) {
740 case GOT_OBJ_TYPE_COMMIT:
741 err = got_object_open_as_commit(&commit, repo, id);
742 if (err)
743 goto done;
744 ref->committer_time =
745 got_object_commit_get_committer_time(commit);
746 break;
747 case GOT_OBJ_TYPE_TAG:
748 err = got_object_open_as_tag(&tag, repo, id);
749 if (err)
750 goto done;
751 ref->committer_time = got_object_tag_get_tagger_time(tag);
752 break;
753 default:
754 /* best effort for other object types */
755 ref->committer_time = got_ref_get_mtime(ref);
756 break;
758 done:
759 free(id);
760 if (commit)
761 got_object_commit_close(commit);
762 if (tag)
763 got_object_tag_close(tag);
764 return err;
767 const struct got_error *
768 got_ref_cmp_by_commit_timestamp_descending(void *arg, int *cmp,
769 struct got_reference *ref1, struct got_reference *ref2)
771 const struct got_error *err = NULL;
772 struct got_repository *repo = arg;
774 *cmp = 0;
776 if (ref1->committer_time == 0) {
777 err = get_committer_time(ref1, repo);
778 if (err)
779 return err;
781 if (ref2->committer_time == 0) {
782 err = get_committer_time(ref2, repo);
783 if (err)
784 return err;
787 if (ref1->committer_time < ref2->committer_time)
788 *cmp = 1;
789 else if (ref2->committer_time < ref1->committer_time)
790 *cmp = -1;
791 else
792 return got_ref_cmp_by_name(arg, cmp, ref1, ref2);
794 return err;
797 const struct got_error *
798 got_reflist_insert(struct got_reflist_entry **newp,
799 struct got_reflist_head *refs, struct got_reference *ref,
800 got_ref_cmp_cb cmp_cb, void *cmp_arg)
802 const struct got_error *err;
803 struct got_reflist_entry *new, *re;
804 int cmp;
806 *newp = NULL;
808 if (cmp_cb != got_ref_cmp_by_name && (ref->flags & GOT_REF_IS_PACKED)) {
809 /*
810 * If we are not sorting elements by name then we must still
811 * detect collisions between a packed ref and an on-disk ref
812 * using the same name. On-disk refs take precedence and are
813 * already present on the list before packed refs get added.
814 */
815 TAILQ_FOREACH(re, refs, entry) {
816 err = got_ref_cmp_by_name(NULL, &cmp, re->ref, ref);
817 if (err)
818 return err;
819 if (cmp == 0)
820 return NULL;
824 new = malloc(sizeof(*new));
825 if (new == NULL)
826 return got_error_from_errno("malloc");
827 new->ref = ref;
828 *newp = new;
830 /*
831 * We must de-duplicate entries on insert because packed-refs may
832 * contain redundant entries. On-disk refs take precedence.
833 * This code assumes that on-disk revs are read before packed-refs.
834 * We're iterating the list anyway, so insert elements sorted by name.
836 * Many callers will provide paths in a somewhat sorted order.
837 * Iterating backwards from the tail of the list should be more
838 * efficient than traversing through the entire list each time
839 * an element is inserted.
840 */
841 re = TAILQ_LAST(refs, got_reflist_head);
842 while (re) {
843 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
844 if (err)
845 return err;
846 if (cmp == 0) {
847 /* duplicate */
848 free(new);
849 *newp = NULL;
850 return NULL;
851 } else if (cmp < 0) {
852 TAILQ_INSERT_AFTER(refs, re, new, entry);
853 return NULL;
855 re = TAILQ_PREV(re, got_reflist_head, entry);
858 TAILQ_INSERT_HEAD(refs, new, entry);
859 return NULL;
862 const struct got_error *
863 got_reflist_sort(struct got_reflist_head *refs,
864 got_ref_cmp_cb cmp_cb, void *cmp_arg)
866 const struct got_error *err = NULL;
867 struct got_reflist_entry *re, *tmp, *new;
868 struct got_reflist_head sorted;
870 TAILQ_INIT(&sorted);
872 TAILQ_FOREACH_SAFE(re, refs, entry, tmp) {
873 struct got_reference *ref = re->ref;
874 TAILQ_REMOVE(refs, re, entry);
875 free(re);
876 err = got_reflist_insert(&new, &sorted, ref, cmp_cb, cmp_arg);
877 if (err || new == NULL /* duplicate */)
878 got_ref_close(ref);
879 if (err)
880 return err;
883 TAILQ_CONCAT(refs, &sorted, entry);
884 return NULL;
887 static const struct got_error *
888 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
889 const char *subdir, struct got_repository *repo,
890 got_ref_cmp_cb cmp_cb, void *cmp_arg)
892 const struct got_error *err = NULL;
893 DIR *d = NULL;
894 char *path_subdir;
896 while (subdir[0] == '/')
897 subdir++;
899 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
900 return got_error_from_errno("asprintf");
902 d = opendir(path_subdir);
903 if (d == NULL)
904 goto done;
906 for (;;) {
907 struct dirent *dent;
908 struct got_reference *ref;
909 char *child;
910 int type;
912 dent = readdir(d);
913 if (dent == NULL)
914 break;
916 if (strcmp(dent->d_name, ".") == 0 ||
917 strcmp(dent->d_name, "..") == 0)
918 continue;
920 err = got_path_dirent_type(&type, path_subdir, dent);
921 if (err)
922 break;
924 switch (type) {
925 case DT_REG:
926 err = open_ref(&ref, path_refs, subdir, dent->d_name,
927 0, got_repo_get_object_format(repo));
928 if (err)
929 goto done;
930 if (ref) {
931 struct got_reflist_entry *new;
932 err = got_reflist_insert(&new, refs, ref,
933 cmp_cb, cmp_arg);
934 if (err || new == NULL /* duplicate */)
935 got_ref_close(ref);
936 if (err)
937 goto done;
939 break;
940 case DT_DIR:
941 if (asprintf(&child, "%s%s%s", subdir,
942 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
943 err = got_error_from_errno("asprintf");
944 break;
946 err = gather_on_disk_refs(refs, path_refs, child, repo,
947 cmp_cb, cmp_arg);
948 free(child);
949 break;
950 default:
951 break;
954 done:
955 if (d)
956 closedir(d);
957 free(path_subdir);
958 return err;
961 const struct got_error *
962 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
963 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
965 const struct got_error *err;
966 char *packed_refs_path = NULL, *path_refs = NULL;
967 char *abs_namespace = NULL, *buf = NULL;
968 const char *ondisk_ref_namespace = NULL;
969 char *line = NULL;
970 FILE *f = NULL;
971 struct got_reference *ref;
972 struct got_reflist_entry *new;
974 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
975 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
976 if (path_refs == NULL) {
977 err = got_error_from_errno("get_refs_dir_path");
978 goto done;
980 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0,
981 got_repo_get_object_format(repo));
982 if (err)
983 goto done;
984 err = got_reflist_insert(&new, refs, ref, cmp_cb, cmp_arg);
985 if (err || new == NULL /* duplicate */)
986 got_ref_close(ref);
987 if (err && err->code != GOT_ERR_NOT_REF)
988 goto done;
989 } else {
990 /* Try listing a single reference. */
991 const char *refname = ref_namespace;
992 path_refs = get_refs_dir_path(repo, refname);
993 if (path_refs == NULL) {
994 err = got_error_from_errno("get_refs_dir_path");
995 goto done;
997 err = open_ref(&ref, path_refs, "", refname, 0,
998 got_repo_get_object_format(repo));
999 if (err) {
1000 if (err->code != GOT_ERR_NOT_REF)
1001 goto done;
1002 /* Try to look up references in a given namespace. */
1003 } else {
1004 err = got_reflist_insert(&new, refs, ref,
1005 cmp_cb, cmp_arg);
1006 if (err || new == NULL /* duplicate */)
1007 got_ref_close(ref);
1008 return err;
1012 if (ref_namespace) {
1013 size_t len;
1014 /* Canonicalize the path to eliminate double-slashes if any. */
1015 if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
1016 err = got_error_from_errno("asprintf");
1017 goto done;
1019 len = strlen(abs_namespace) + 1;
1020 buf = malloc(len);
1021 if (buf == NULL) {
1022 err = got_error_from_errno("malloc");
1023 goto done;
1025 err = got_canonpath(abs_namespace, buf, len);
1026 if (err)
1027 goto done;
1028 ondisk_ref_namespace = buf;
1029 while (ondisk_ref_namespace[0] == '/')
1030 ondisk_ref_namespace++;
1031 if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
1032 ondisk_ref_namespace += 5;
1033 else if (strcmp(ondisk_ref_namespace, "refs") == 0)
1034 ondisk_ref_namespace = "";
1037 /* Gather on-disk refs before parsing packed-refs. */
1038 free(path_refs);
1039 path_refs = get_refs_dir_path(repo, "");
1040 if (path_refs == NULL) {
1041 err = got_error_from_errno("get_refs_dir_path");
1042 goto done;
1044 err = gather_on_disk_refs(refs, path_refs,
1045 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
1046 cmp_cb, cmp_arg);
1047 if (err)
1048 goto done;
1051 * The packed-refs file may contain redundant entries, in which
1052 * case on-disk refs take precedence.
1054 packed_refs_path = got_repo_get_path_packed_refs(repo);
1055 if (packed_refs_path == NULL) {
1056 err = got_error_from_errno("got_repo_get_path_packed_refs");
1057 goto done;
1060 f = fopen(packed_refs_path, "re");
1061 if (f) {
1062 size_t linesize = 0;
1063 ssize_t linelen;
1064 struct stat sb;
1066 if (fstat(fileno(f), &sb) == -1) {
1067 err = got_error_from_errno2("fstat", packed_refs_path);
1068 goto done;
1070 for (;;) {
1071 linelen = getline(&line, &linesize, f);
1072 if (linelen == -1) {
1073 if (feof(f))
1074 break;
1075 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1076 goto done;
1078 if (linelen > 0 && line[linelen - 1] == '\n')
1079 line[linelen - 1] = '\0';
1080 err = parse_packed_ref_line(&ref, NULL, line,
1081 sb.st_mtime, got_repo_get_object_format(repo));
1082 if (err)
1083 goto done;
1084 if (ref) {
1085 if (ref_namespace) {
1086 const char *name;
1087 name = got_ref_get_name(ref);
1088 if (!got_path_is_child(name,
1089 ref_namespace,
1090 strlen(ref_namespace))) {
1091 got_ref_close(ref);
1092 continue;
1095 err = got_reflist_insert(&new, refs, ref,
1096 cmp_cb, cmp_arg);
1097 if (err || new == NULL /* duplicate */)
1098 got_ref_close(ref);
1099 if (err)
1100 goto done;
1104 done:
1105 free(packed_refs_path);
1106 free(abs_namespace);
1107 free(buf);
1108 free(line);
1109 free(path_refs);
1110 if (f && fclose(f) == EOF && err == NULL)
1111 err = got_error_from_errno("fclose");
1112 return err;
1115 void
1116 got_ref_list_free(struct got_reflist_head *refs)
1118 struct got_reflist_entry *re;
1120 while ((re = TAILQ_FIRST(refs))) {
1121 TAILQ_REMOVE(refs, re, entry);
1122 got_ref_close(re->ref);
1123 free(re);
1127 int
1128 got_ref_is_symbolic(struct got_reference *ref)
1130 return (ref->flags & GOT_REF_IS_SYMBOLIC);
1133 const struct got_error *
1134 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1136 if (ref->flags & GOT_REF_IS_SYMBOLIC)
1137 return got_error(GOT_ERR_BAD_REF_TYPE);
1139 memcpy(&ref->ref.ref.id, id, sizeof(ref->ref.ref.id));
1140 return NULL;
1143 const struct got_error *
1144 got_ref_change_symref(struct got_reference *ref, const char *refname)
1146 char *new_name;
1148 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1149 return got_error(GOT_ERR_BAD_REF_TYPE);
1151 new_name = strdup(refname);
1152 if (new_name == NULL)
1153 return got_error_from_errno("strdup");
1155 free(ref->ref.symref.ref);
1156 ref->ref.symref.ref = new_name;
1157 return NULL;
1160 const struct got_error *
1161 got_ref_change_symref_to_ref(struct got_reference *symref,
1162 struct got_object_id *id)
1164 if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1165 return got_error(GOT_ERR_BAD_REF_TYPE);
1167 symref->ref.ref.name = symref->ref.symref.name;
1168 memcpy(&symref->ref.ref.id, id, sizeof(symref->ref.ref.id));
1169 symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1170 return NULL;
1173 const struct got_error *
1174 got_ref_write(struct got_reference *ref, struct got_repository *repo)
1176 const struct got_error *err = NULL, *unlock_err = NULL;
1177 const char *name = got_ref_get_name(ref);
1178 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1179 struct got_lockfile *lf = NULL;
1180 FILE *f = NULL;
1181 size_t n;
1182 struct stat sb;
1184 path_refs = get_refs_dir_path(repo, name);
1185 if (path_refs == NULL) {
1186 err = got_error_from_errno2("get_refs_dir_path", name);
1187 goto done;
1190 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1191 err = got_error_from_errno("asprintf");
1192 goto done;
1195 err = got_opentemp_named(&tmppath, &f, path, "");
1196 if (err) {
1197 char *parent;
1198 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1199 goto done;
1200 err = got_path_dirname(&parent, path);
1201 if (err)
1202 goto done;
1203 err = got_path_mkdir(parent);
1204 free(parent);
1205 if (err)
1206 goto done;
1207 err = got_opentemp_named(&tmppath, &f, path, "");
1208 if (err)
1209 goto done;
1212 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1213 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1214 if (n != strlen(ref->ref.symref.ref) + 6) {
1215 err = got_ferror(f, GOT_ERR_IO);
1216 goto done;
1218 } else {
1219 char *hex;
1220 size_t len;
1222 err = got_object_id_str(&hex, &ref->ref.ref.id);
1223 if (err)
1224 goto done;
1225 len = strlen(hex);
1226 n = fprintf(f, "%s\n", hex);
1227 free(hex);
1228 if (n != len + 1) {
1229 err = got_ferror(f, GOT_ERR_IO);
1230 goto done;
1234 if (ref->lf == NULL) {
1235 err = got_lockfile_lock(&lf, path, -1);
1236 if (err)
1237 goto done;
1240 /* XXX: check if old content matches our expectations? */
1242 if (stat(path, &sb) != 0) {
1243 if (errno != ENOENT) {
1244 err = got_error_from_errno2("stat", path);
1245 goto done;
1247 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1250 if (fchmod(fileno(f), sb.st_mode) != 0) {
1251 err = got_error_from_errno2("fchmod", tmppath);
1252 goto done;
1255 if (rename(tmppath, path) != 0) {
1256 err = got_error_from_errno3("rename", tmppath, path);
1257 goto done;
1259 free(tmppath);
1260 tmppath = NULL;
1262 if (stat(path, &sb) == -1) {
1263 err = got_error_from_errno2("stat", path);
1264 goto done;
1266 ref->mtime = sb.st_mtime;
1267 done:
1268 if (ref->lf == NULL && lf)
1269 unlock_err = got_lockfile_unlock(lf, -1);
1270 if (f) {
1271 if (fclose(f) == EOF && err == NULL)
1272 err = got_error_from_errno("fclose");
1274 free(path_refs);
1275 free(path);
1276 if (tmppath) {
1277 if (unlink(tmppath) == -1 && err == NULL)
1278 err = got_error_from_errno2("unlink", tmppath);
1279 free(tmppath);
1281 return err ? err : unlock_err;
1284 static const struct got_error *
1285 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1287 const struct got_error *err = NULL, *unlock_err = NULL;
1288 struct got_lockfile *lf = NULL;
1289 FILE *f = NULL, *tmpf = NULL;
1290 char *line = NULL, *packed_refs_path, *tmppath = NULL;
1291 size_t linesize = 0;
1292 struct got_reflist_head refs;
1293 int found_delref = 0;
1295 /* The packed-refs file does not cotain symbolic references. */
1296 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1297 return got_error(GOT_ERR_BAD_REF_DATA);
1299 TAILQ_INIT(&refs);
1301 packed_refs_path = got_repo_get_path_packed_refs(repo);
1302 if (packed_refs_path == NULL)
1303 return got_error_from_errno("got_repo_get_path_packed_refs");
1305 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path, "");
1306 if (err)
1307 goto done;
1309 if (delref->lf == NULL) {
1310 err = got_lockfile_lock(&lf, packed_refs_path, -1);
1311 if (err)
1312 goto done;
1315 f = fopen(packed_refs_path, "re");
1316 if (f == NULL) {
1317 err = got_error_from_errno2("fopen", packed_refs_path);
1318 goto done;
1320 for (;;) {
1321 ssize_t linelen;
1322 struct got_reference *ref;
1323 struct got_reflist_entry *new;
1325 linelen = getline(&line, &linesize, f);
1326 if (linelen == -1) {
1327 if (feof(f))
1328 break;
1329 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1330 goto done;
1332 if (linelen > 0 && line[linelen - 1] == '\n')
1333 line[linelen - 1] = '\0';
1334 err = parse_packed_ref_line(&ref, NULL, line, 0,
1335 got_repo_get_object_format(repo));
1336 if (err)
1337 goto done;
1338 if (ref == NULL)
1339 continue;
1341 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1342 got_object_id_cmp(&ref->ref.ref.id,
1343 &delref->ref.ref.id) == 0) {
1344 found_delref = 1;
1345 got_ref_close(ref);
1346 continue;
1349 err = got_reflist_insert(&new, &refs, ref,
1350 got_ref_cmp_by_name, NULL);
1351 if (err || new == NULL /* duplicate */)
1352 got_ref_close(ref);
1353 if (err)
1354 goto done;
1357 if (found_delref) {
1358 struct got_reflist_entry *re;
1359 size_t n;
1360 struct stat sb;
1362 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1363 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1364 err = got_ferror(f, GOT_ERR_IO);
1365 goto done;
1368 TAILQ_FOREACH(re, &refs, entry) {
1369 char *hex;
1370 size_t len;
1372 err = got_object_id_str(&hex, &re->ref->ref.ref.id);
1373 if (err)
1374 goto done;
1375 len = strlen(hex);
1376 n = fprintf(tmpf, "%s ", hex);
1377 free(hex);
1378 if (n != len + 1) {
1379 err = got_ferror(f, GOT_ERR_IO);
1380 goto done;
1383 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1384 if (n != strlen(re->ref->ref.ref.name) + 1) {
1385 err = got_ferror(f, GOT_ERR_IO);
1386 goto done;
1390 if (fflush(tmpf) != 0) {
1391 err = got_error_from_errno("fflush");
1392 goto done;
1395 if (fstat(fileno(f), &sb) != 0) {
1396 if (errno != ENOENT) {
1397 err = got_error_from_errno2("fstat",
1398 packed_refs_path);
1399 goto done;
1401 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1404 if (fchmod(fileno(tmpf), sb.st_mode) != 0) {
1405 err = got_error_from_errno2("fchmod", tmppath);
1406 goto done;
1409 if (rename(tmppath, packed_refs_path) != 0) {
1410 err = got_error_from_errno3("rename", tmppath,
1411 packed_refs_path);
1412 goto done;
1414 free(tmppath);
1415 tmppath = NULL;
1417 done:
1418 if (delref->lf == NULL && lf)
1419 unlock_err = got_lockfile_unlock(lf, -1);
1420 if (f) {
1421 if (fclose(f) == EOF && err == NULL)
1422 err = got_error_from_errno("fclose");
1424 if (tmppath && unlink(tmppath) == -1 && err == NULL)
1425 err = got_error_from_errno2("unlink", tmppath);
1426 if (tmpf && fclose(tmpf) == EOF && err == NULL)
1427 err = got_error_from_errno("fclose");
1428 free(tmppath);
1429 free(packed_refs_path);
1430 free(line);
1431 got_ref_list_free(&refs);
1432 return err ? err : unlock_err;
1435 static const struct got_error *
1436 delete_loose_ref(struct got_reference *ref, struct got_repository *repo)
1438 const struct got_error *err = NULL, *unlock_err = NULL;
1439 const char *name = got_ref_get_name(ref);
1440 char *path_refs = NULL, *path = NULL;
1441 struct got_lockfile *lf = NULL;
1443 path_refs = get_refs_dir_path(repo, name);
1444 if (path_refs == NULL) {
1445 err = got_error_from_errno2("get_refs_dir_path", name);
1446 goto done;
1449 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1450 err = got_error_from_errno("asprintf");
1451 goto done;
1454 if (ref->lf == NULL) {
1455 err = got_lockfile_lock(&lf, path, -1);
1456 if (err)
1457 goto done;
1460 /* XXX: check if old content matches our expectations? */
1462 if (unlink(path) == -1)
1463 err = got_error_from_errno2("unlink", path);
1464 done:
1465 if (ref->lf == NULL && lf)
1466 unlock_err = got_lockfile_unlock(lf, -1);
1468 free(path_refs);
1469 free(path);
1470 return err ? err : unlock_err;
1473 const struct got_error *
1474 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1476 const struct got_error *err = NULL;
1477 struct got_reference *ref2;
1479 if (ref->flags & GOT_REF_IS_PACKED) {
1480 err = delete_packed_ref(ref, repo);
1481 if (err)
1482 return err;
1484 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1485 if (err) {
1486 if (err->code == GOT_ERR_NOT_REF)
1487 return NULL;
1488 return err;
1491 err = delete_loose_ref(ref2, repo);
1492 got_ref_close(ref2);
1493 return err;
1494 } else {
1495 err = delete_loose_ref(ref, repo);
1496 if (err)
1497 return err;
1499 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1500 if (err) {
1501 if (err->code == GOT_ERR_NOT_REF)
1502 return NULL;
1503 return err;
1506 err = delete_packed_ref(ref2, repo);
1507 got_ref_close(ref2);
1508 return err;
1512 const struct got_error *
1513 got_ref_unlock(struct got_reference *ref)
1515 const struct got_error *err;
1516 err = got_lockfile_unlock(ref->lf, -1);
1517 ref->lf = NULL;
1518 return err;
1521 struct got_reflist_object_id_map {
1522 struct got_object_idset *idset;
1525 struct got_reflist_object_id_map_entry {
1526 struct got_reflist_head refs;
1529 static const struct got_error *
1530 add_object_id_map_entry(struct got_object_idset *idset,
1531 struct got_object_id *id, struct got_reflist_entry *re)
1533 const struct got_error *err = NULL;
1534 struct got_reflist_object_id_map_entry *ent;
1535 struct got_reflist_entry *new;
1537 ent = got_object_idset_get(idset, id);
1538 if (ent == NULL) {
1539 ent = malloc(sizeof(*ent));
1540 if (ent == NULL)
1541 return got_error_from_errno("malloc");
1543 TAILQ_INIT(&ent->refs);
1544 err = got_object_idset_add(idset, id, ent);
1545 if (err) {
1546 free(ent);
1547 return err;
1551 err = got_reflist_entry_dup(&new, re);
1552 if (err)
1553 return err;
1555 TAILQ_INSERT_TAIL(&ent->refs, new, entry);
1556 return NULL;
1559 const struct got_error *
1560 got_reflist_object_id_map_create(struct got_reflist_object_id_map **map,
1561 struct got_reflist_head *refs, struct got_repository *repo)
1563 const struct got_error *err = NULL;
1564 struct got_object_idset *idset;
1565 struct got_object_id *id = NULL;
1566 struct got_reflist_entry *re;
1568 idset = got_object_idset_alloc();
1569 if (idset == NULL)
1570 return got_error_from_errno("got_object_idset_alloc");
1572 *map = malloc(sizeof(**map));
1573 if (*map == NULL) {
1574 got_object_idset_free(idset);
1575 return got_error_from_errno("malloc");
1577 (*map)->idset = idset;
1579 TAILQ_FOREACH(re, refs, entry) {
1580 struct got_tag_object *tag = NULL;
1582 err = got_ref_resolve(&id, repo, re->ref);
1583 if (err)
1584 goto done;
1586 err = add_object_id_map_entry(idset, id, re);
1587 if (err)
1588 goto done;
1590 if (strstr(got_ref_get_name(re->ref), "/tags/") == NULL) {
1591 free(id);
1592 id = NULL;
1593 continue;
1596 err = got_object_open_as_tag(&tag, repo, id);
1597 if (err) {
1598 if (err->code != GOT_ERR_OBJ_TYPE)
1599 goto done;
1600 /* Ref points at something other than a tag. */
1601 err = NULL;
1602 tag = NULL;
1603 free(id);
1604 id = NULL;
1605 continue;
1608 err = add_object_id_map_entry(idset,
1609 got_object_tag_get_object_id(tag), re);
1610 got_object_tag_close(tag);
1611 if (err)
1612 goto done;
1614 free(id);
1615 id = NULL;
1617 done:
1618 free(id);
1619 if (err) {
1620 got_reflist_object_id_map_free(*map);
1621 *map = NULL;
1623 return err;
1626 struct got_reflist_head *
1627 got_reflist_object_id_map_lookup(struct got_reflist_object_id_map *map,
1628 struct got_object_id *id)
1630 struct got_reflist_object_id_map_entry *ent;
1631 ent = got_object_idset_get(map->idset, id);
1632 if (ent)
1633 return &ent->refs;
1634 return NULL;
1637 static const struct got_error *
1638 free_id_map_entry(struct got_object_id *id, void *data, void *arg)
1640 struct got_reflist_object_id_map_entry *ent = data;
1642 got_ref_list_free(&ent->refs);
1643 free(ent);
1644 return NULL;
1647 void
1648 got_reflist_object_id_map_free(struct got_reflist_object_id_map *map)
1650 got_object_idset_for_each(map->idset, free_id_map_entry, NULL);
1651 got_object_idset_free(map->idset);
1652 free(map);