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 if (!got_parse_object_id(&id, line, algo))
166 return got_error(GOT_ERR_BAD_REF_DATA);
168 return alloc_ref(ref, name, &id, 0, mtime);
171 static const struct got_error *
172 parse_ref_file(struct got_reference **ref, const char *name,
173 const char *absname, const char *abspath, int lock,
174 enum got_hash_algorithm algo)
176 const struct got_error *err = NULL;
177 FILE *f;
178 char *line = NULL;
179 size_t linesize = 0;
180 ssize_t linelen;
181 struct got_lockfile *lf = NULL;
182 struct stat sb;
184 if (lock) {
185 err = got_lockfile_lock(&lf, abspath, -1);
186 if (err) {
187 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
188 err = got_error_not_ref(name);
189 return err;
193 f = fopen(abspath, "rbe");
194 if (f == NULL) {
195 if (errno != ENOTDIR && errno != ENOENT)
196 err = got_error_from_errno2("fopen", abspath);
197 else
198 err = got_error_not_ref(name);
199 if (lock)
200 got_lockfile_unlock(lf, -1);
201 return err;
203 if (fstat(fileno(f), &sb) == -1) {
204 err = got_error_from_errno2("fstat", abspath);
205 goto done;
208 linelen = getline(&line, &linesize, f);
209 if (linelen == -1) {
210 if (feof(f))
211 err = NULL; /* ignore empty files (could be locks) */
212 else {
213 if (errno == EISDIR)
214 err = got_error(GOT_ERR_NOT_REF);
215 else if (ferror(f))
216 err = got_ferror(f, GOT_ERR_IO);
217 else
218 err = got_error_from_errno2("getline", abspath);
220 if (lock)
221 got_lockfile_unlock(lf, -1);
222 goto done;
224 while (linelen > 0 && line[linelen - 1] == '\n') {
225 line[linelen - 1] = '\0';
226 linelen--;
229 err = parse_ref_line(ref, absname, line, sb.st_mtime, algo);
230 if (lock) {
231 if (err)
232 got_lockfile_unlock(lf, -1);
233 else {
234 if (*ref)
235 (*ref)->lf = lf;
236 else
237 got_lockfile_unlock(lf, -1);
240 done:
241 free(line);
242 if (fclose(f) == EOF && err == NULL) {
243 err = got_error_from_errno("fclose");
244 if (*ref) {
245 if (lock)
246 got_ref_unlock(*ref);
247 got_ref_close(*ref);
248 *ref = NULL;
251 return err;
254 static int
255 is_well_known_ref(const char *refname)
257 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
258 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
259 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
260 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
263 static char *
264 get_refs_dir_path(struct got_repository *repo, const char *refname)
266 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
267 return strdup(got_repo_get_path_git_dir(repo));
269 return got_repo_get_path_refs(repo);
272 const struct got_error *
273 got_ref_alloc(struct got_reference **ref, const char *name,
274 struct got_object_id *id)
276 if (!got_ref_name_is_valid(name))
277 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
279 return alloc_ref(ref, name, id, 0, 0);
282 const struct got_error *
283 got_ref_alloc_symref(struct got_reference **ref, const char *name,
284 struct got_reference *target_ref)
286 if (!got_ref_name_is_valid(name))
287 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
289 return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
292 static const struct got_error *
293 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
294 const char *line, time_t mtime, enum got_hash_algorithm algo)
296 struct got_object_id id;
297 const char *name;
299 *ref = NULL;
301 if (line[0] == '#' || line[0] == '^')
302 return NULL;
304 if (!got_parse_object_id(&id, line, algo))
305 return got_error(GOT_ERR_BAD_REF_DATA);
307 if (abs_refname) {
308 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
309 return NULL;
310 name = abs_refname;
311 } else
312 name = line + SHA1_DIGEST_STRING_LENGTH;
314 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED, mtime);
317 static const struct got_error *
318 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
319 int nsubdirs, const char *refname, time_t mtime,
320 enum got_hash_algorithm algo)
322 const struct got_error *err = NULL;
323 char *abs_refname;
324 char *line = NULL;
325 size_t linesize = 0;
326 ssize_t linelen;
327 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
329 *ref = NULL;
331 if (ref_is_absolute)
332 abs_refname = (char *)refname;
333 do {
334 linelen = getline(&line, &linesize, f);
335 if (linelen == -1) {
336 if (feof(f))
337 break;
338 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
339 break;
341 if (linelen > 0 && line[linelen - 1] == '\n')
342 line[linelen - 1] = '\0';
343 for (i = 0; i < nsubdirs; i++) {
344 if (!ref_is_absolute &&
345 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
346 refname) == -1)
347 return got_error_from_errno("asprintf");
348 err = parse_packed_ref_line(ref, abs_refname, line,
349 mtime, algo);
350 if (!ref_is_absolute)
351 free(abs_refname);
352 if (err || *ref != NULL)
353 break;
355 if (err)
356 break;
357 } while (*ref == NULL);
358 free(line);
360 return err;
363 static const struct got_error *
364 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
365 const char *name, int lock, enum got_hash_algorithm algo)
367 const struct got_error *err = NULL;
368 char *path = NULL;
369 char *absname = NULL;
370 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
371 int ref_is_well_known = (subdir[0] == '\0' && is_well_known_ref(name));
373 *ref = NULL;
375 if (!got_ref_name_is_valid(name))
376 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
378 if (ref_is_absolute || ref_is_well_known) {
379 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
380 return got_error_from_errno("asprintf");
381 absname = (char *)name;
382 } else {
383 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
384 subdir[0] ? "/" : "", name) == -1)
385 return got_error_from_errno("asprintf");
387 if (asprintf(&absname, "refs/%s%s%s",
388 subdir, subdir[0] ? "/" : "", name) == -1) {
389 err = got_error_from_errno("asprintf");
390 goto done;
394 err = parse_ref_file(ref, name, absname, path, lock, algo);
395 done:
396 if (!ref_is_absolute && !ref_is_well_known)
397 free(absname);
398 free(path);
399 return err;
402 const struct got_error *
403 got_ref_open(struct got_reference **ref, struct got_repository *repo,
404 const char *refname, int lock)
406 const struct got_error *err = NULL;
407 char *packed_refs_path = NULL, *path_refs = NULL;
408 const char *subdirs[] = {
409 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
410 };
411 size_t i;
412 int well_known = is_well_known_ref(refname);
413 struct got_lockfile *lf = NULL;
415 *ref = NULL;
417 path_refs = get_refs_dir_path(repo, refname);
418 if (path_refs == NULL) {
419 err = got_error_from_errno2("get_refs_dir_path", refname);
420 goto done;
423 if (well_known) {
424 err = open_ref(ref, path_refs, "", refname, lock,
425 got_repo_get_object_format(repo));
426 } else {
427 FILE *f;
429 /* Search on-disk refs before packed refs! */
430 for (i = 0; i < nitems(subdirs); i++) {
431 err = open_ref(ref, path_refs, subdirs[i], refname,
432 lock, got_repo_get_object_format(repo));
433 if ((err && err->code != GOT_ERR_NOT_REF) || *ref)
434 goto done;
437 packed_refs_path = got_repo_get_path_packed_refs(repo);
438 if (packed_refs_path == NULL) {
439 err = got_error_from_errno(
440 "got_repo_get_path_packed_refs");
441 goto done;
444 if (lock) {
445 err = got_lockfile_lock(&lf, packed_refs_path, -1);
446 if (err)
447 goto done;
449 f = fopen(packed_refs_path, "rbe");
450 if (f != NULL) {
451 struct stat sb;
452 if (fstat(fileno(f), &sb) == -1) {
453 err = got_error_from_errno2("fstat",
454 packed_refs_path);
455 goto done;
457 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
458 refname, sb.st_mtime,
459 got_repo_get_object_format(repo));
460 if (!err) {
461 if (fclose(f) == EOF) {
462 err = got_error_from_errno("fclose");
463 got_ref_close(*ref);
464 *ref = NULL;
465 } else if (*ref)
466 (*ref)->lf = lf;
470 done:
471 if (!err && *ref == NULL)
472 err = got_error_not_ref(refname);
473 if (err && lf)
474 got_lockfile_unlock(lf, -1);
475 free(packed_refs_path);
476 free(path_refs);
477 return err;
480 void
481 got_ref_close(struct got_reference *ref)
483 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
484 free(ref->ref.symref.name);
485 free(ref->ref.symref.ref);
486 } else
487 free(ref->ref.ref.name);
488 free(ref);
491 struct got_reference *
492 got_ref_dup(struct got_reference *ref)
494 struct got_reference *ret;
496 ret = calloc(1, sizeof(*ret));
497 if (ret == NULL)
498 return NULL;
500 ret->flags = ref->flags;
501 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
502 ret->ref.symref.name = strdup(ref->ref.symref.name);
503 if (ret->ref.symref.name == NULL) {
504 free(ret);
505 return NULL;
507 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
508 if (ret->ref.symref.ref == NULL) {
509 free(ret->ref.symref.name);
510 free(ret);
511 return NULL;
513 } else {
514 ret->ref.ref.name = strdup(ref->ref.ref.name);
515 if (ret->ref.ref.name == NULL) {
516 free(ret);
517 return NULL;
519 memcpy(&ret->ref.ref.id, &ref->ref.ref.id,
520 sizeof(ret->ref.ref.id));
523 return ret;
526 const struct got_error *
527 got_reflist_entry_dup(struct got_reflist_entry **newp,
528 struct got_reflist_entry *re)
530 const struct got_error *err = NULL;
531 struct got_reflist_entry *new;
533 *newp = NULL;
535 new = malloc(sizeof(*new));
536 if (new == NULL)
537 return got_error_from_errno("malloc");
539 new->ref = got_ref_dup(re->ref);
540 if (new->ref == NULL) {
541 err = got_error_from_errno("got_ref_dup");
542 free(new);
543 return err;
546 *newp = new;
547 return NULL;
550 const struct got_error *
551 got_ref_resolve_symbolic(struct got_reference **resolved,
552 struct got_repository *repo, struct got_reference *ref)
554 struct got_reference *nextref;
555 const struct got_error *err;
557 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
558 if (err)
559 return err;
561 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
562 err = got_ref_resolve_symbolic(resolved, repo, nextref);
563 else
564 *resolved = got_ref_dup(nextref);
566 got_ref_close(nextref);
567 return err;
570 static const struct got_error *
571 ref_resolve(struct got_object_id **id, struct got_repository *repo,
572 struct got_reference *ref, int recursion)
574 const struct got_error *err;
576 if (recursion <= 0)
577 return got_error_msg(GOT_ERR_RECURSION,
578 "reference recursion limit reached");
580 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
581 struct got_reference *resolved = NULL;
582 err = got_ref_resolve_symbolic(&resolved, repo, ref);
583 if (err == NULL)
584 err = ref_resolve(id, repo, resolved, --recursion);
585 if (resolved)
586 got_ref_close(resolved);
587 return err;
590 *id = calloc(1, sizeof(**id));
591 if (*id == NULL)
592 return got_error_from_errno("calloc");
593 memcpy(*id, &ref->ref.ref.id, sizeof(**id));
594 return NULL;
597 const struct got_error *
598 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
599 struct got_reference *ref)
601 return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
604 char *
605 got_ref_to_str(struct got_reference *ref)
607 char *str;
609 if (ref->flags & GOT_REF_IS_SYMBOLIC)
610 return strdup(ref->ref.symref.ref);
612 if (got_object_id_str(&str, &ref->ref.ref.id) != NULL)
613 return NULL;
615 return str;
618 const char *
619 got_ref_get_name(struct got_reference *ref)
621 if (ref->flags & GOT_REF_IS_SYMBOLIC)
622 return ref->ref.symref.name;
624 return ref->ref.ref.name;
627 const char *
628 got_ref_get_symref_target(struct got_reference *ref)
630 if (ref->flags & GOT_REF_IS_SYMBOLIC)
631 return ref->ref.symref.ref;
633 return NULL;
636 time_t
637 got_ref_get_mtime(struct got_reference *ref)
639 return ref->mtime;
642 const struct got_error *
643 got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
644 struct got_reference* re2)
646 const char *name1 = got_ref_get_name(re1);
647 const char *name2 = got_ref_get_name(re2);
649 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
650 return NULL;
653 const struct got_error *
654 got_ref_cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
655 struct got_reference *ref2)
657 const struct got_error *err = NULL;
658 struct got_repository *repo = arg;
659 struct got_object_id *id1, *id2 = NULL;
660 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
661 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
662 time_t time1, time2;
664 *cmp = 0;
666 err = got_ref_resolve(&id1, repo, ref1);
667 if (err)
668 return err;
669 err = got_object_open_as_tag(&tag1, repo, id1);
670 if (err) {
671 if (err->code != GOT_ERR_OBJ_TYPE)
672 goto done;
673 /* "lightweight" tag */
674 err = got_object_open_as_commit(&commit1, repo, id1);
675 if (err)
676 goto done;
677 time1 = got_object_commit_get_committer_time(commit1);
678 } else
679 time1 = got_object_tag_get_tagger_time(tag1);
681 err = got_ref_resolve(&id2, repo, ref2);
682 if (err)
683 goto done;
684 err = got_object_open_as_tag(&tag2, repo, id2);
685 if (err) {
686 if (err->code != GOT_ERR_OBJ_TYPE)
687 goto done;
688 /* "lightweight" tag */
689 err = got_object_open_as_commit(&commit2, repo, id2);
690 if (err)
691 goto done;
692 time2 = got_object_commit_get_committer_time(commit2);
693 } else
694 time2 = got_object_tag_get_tagger_time(tag2);
696 /* Put latest tags first. */
697 if (time1 < time2)
698 *cmp = 1;
699 else if (time1 > time2)
700 *cmp = -1;
701 else
702 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
703 done:
704 free(id1);
705 free(id2);
706 if (tag1)
707 got_object_tag_close(tag1);
708 if (tag2)
709 got_object_tag_close(tag2);
710 if (commit1)
711 got_object_commit_close(commit1);
712 if (commit2)
713 got_object_commit_close(commit2);
714 return err;
717 static const struct got_error *
718 get_committer_time(struct got_reference *ref, struct got_repository *repo)
720 const struct got_error *err = NULL;
721 int obj_type;
722 struct got_commit_object *commit = NULL;
723 struct got_tag_object *tag = NULL;
724 struct got_object_id *id = NULL;
726 err = got_ref_resolve(&id, repo, ref);
727 if (err)
728 return err;
730 err = got_object_get_type(&obj_type, repo, id);
731 if (err)
732 goto done;
734 switch (obj_type) {
735 case GOT_OBJ_TYPE_COMMIT:
736 err = got_object_open_as_commit(&commit, repo, id);
737 if (err)
738 goto done;
739 ref->committer_time =
740 got_object_commit_get_committer_time(commit);
741 break;
742 case GOT_OBJ_TYPE_TAG:
743 err = got_object_open_as_tag(&tag, repo, id);
744 if (err)
745 goto done;
746 ref->committer_time = got_object_tag_get_tagger_time(tag);
747 break;
748 default:
749 /* best effort for other object types */
750 ref->committer_time = got_ref_get_mtime(ref);
751 break;
753 done:
754 free(id);
755 if (commit)
756 got_object_commit_close(commit);
757 if (tag)
758 got_object_tag_close(tag);
759 return err;
762 const struct got_error *
763 got_ref_cmp_by_commit_timestamp_descending(void *arg, int *cmp,
764 struct got_reference *ref1, struct got_reference *ref2)
766 const struct got_error *err = NULL;
767 struct got_repository *repo = arg;
769 *cmp = 0;
771 if (ref1->committer_time == 0) {
772 err = get_committer_time(ref1, repo);
773 if (err)
774 return err;
776 if (ref2->committer_time == 0) {
777 err = get_committer_time(ref2, repo);
778 if (err)
779 return err;
782 if (ref1->committer_time < ref2->committer_time)
783 *cmp = 1;
784 else if (ref2->committer_time < ref1->committer_time)
785 *cmp = -1;
786 else
787 return got_ref_cmp_by_name(arg, cmp, ref1, ref2);
789 return err;
792 const struct got_error *
793 got_reflist_insert(struct got_reflist_entry **newp,
794 struct got_reflist_head *refs, struct got_reference *ref,
795 got_ref_cmp_cb cmp_cb, void *cmp_arg)
797 const struct got_error *err;
798 struct got_reflist_entry *new, *re;
799 int cmp;
801 *newp = NULL;
803 if (cmp_cb != got_ref_cmp_by_name && (ref->flags & GOT_REF_IS_PACKED)) {
804 /*
805 * If we are not sorting elements by name then we must still
806 * detect collisions between a packed ref and an on-disk ref
807 * using the same name. On-disk refs take precedence and are
808 * already present on the list before packed refs get added.
809 */
810 TAILQ_FOREACH(re, refs, entry) {
811 err = got_ref_cmp_by_name(NULL, &cmp, re->ref, ref);
812 if (err)
813 return err;
814 if (cmp == 0)
815 return NULL;
819 new = malloc(sizeof(*new));
820 if (new == NULL)
821 return got_error_from_errno("malloc");
822 new->ref = ref;
823 *newp = new;
825 /*
826 * We must de-duplicate entries on insert because packed-refs may
827 * contain redundant entries. On-disk refs take precedence.
828 * This code assumes that on-disk revs are read before packed-refs.
829 * We're iterating the list anyway, so insert elements sorted by name.
831 * Many callers will provide paths in a somewhat sorted order.
832 * Iterating backwards from the tail of the list should be more
833 * efficient than traversing through the entire list each time
834 * an element is inserted.
835 */
836 re = TAILQ_LAST(refs, got_reflist_head);
837 while (re) {
838 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
839 if (err)
840 return err;
841 if (cmp == 0) {
842 /* duplicate */
843 free(new);
844 *newp = NULL;
845 return NULL;
846 } else if (cmp < 0) {
847 TAILQ_INSERT_AFTER(refs, re, new, entry);
848 return NULL;
850 re = TAILQ_PREV(re, got_reflist_head, entry);
853 TAILQ_INSERT_HEAD(refs, new, entry);
854 return NULL;
857 const struct got_error *
858 got_reflist_sort(struct got_reflist_head *refs,
859 got_ref_cmp_cb cmp_cb, void *cmp_arg)
861 const struct got_error *err = NULL;
862 struct got_reflist_entry *re, *tmp, *new;
863 struct got_reflist_head sorted;
865 TAILQ_INIT(&sorted);
867 TAILQ_FOREACH_SAFE(re, refs, entry, tmp) {
868 struct got_reference *ref = re->ref;
869 TAILQ_REMOVE(refs, re, entry);
870 free(re);
871 err = got_reflist_insert(&new, &sorted, ref, cmp_cb, cmp_arg);
872 if (err || new == NULL /* duplicate */)
873 got_ref_close(ref);
874 if (err)
875 return err;
878 TAILQ_CONCAT(refs, &sorted, entry);
879 return NULL;
882 static const struct got_error *
883 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
884 const char *subdir, struct got_repository *repo,
885 got_ref_cmp_cb cmp_cb, void *cmp_arg)
887 const struct got_error *err = NULL;
888 DIR *d = NULL;
889 char *path_subdir;
891 while (subdir[0] == '/')
892 subdir++;
894 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
895 return got_error_from_errno("asprintf");
897 d = opendir(path_subdir);
898 if (d == NULL)
899 goto done;
901 for (;;) {
902 struct dirent *dent;
903 struct got_reference *ref;
904 char *child;
905 int type;
907 dent = readdir(d);
908 if (dent == NULL)
909 break;
911 if (strcmp(dent->d_name, ".") == 0 ||
912 strcmp(dent->d_name, "..") == 0)
913 continue;
915 err = got_path_dirent_type(&type, path_subdir, dent);
916 if (err)
917 break;
919 switch (type) {
920 case DT_REG:
921 err = open_ref(&ref, path_refs, subdir, dent->d_name,
922 0, got_repo_get_object_format(repo));
923 if (err && err->code == GOT_ERR_BAD_REF_NAME)
924 break;
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 got_repo_get_object_format(repo));
979 if (err)
980 goto done;
981 err = got_reflist_insert(&new, refs, ref, cmp_cb, cmp_arg);
982 if (err || new == NULL /* duplicate */)
983 got_ref_close(ref);
984 if (err && err->code != GOT_ERR_NOT_REF)
985 goto done;
986 } else {
987 /* Try listing a single reference. */
988 const char *refname = ref_namespace;
989 path_refs = get_refs_dir_path(repo, refname);
990 if (path_refs == NULL) {
991 err = got_error_from_errno("get_refs_dir_path");
992 goto done;
994 err = open_ref(&ref, path_refs, "", refname, 0,
995 got_repo_get_object_format(repo));
996 if (err) {
997 if (err->code != GOT_ERR_NOT_REF)
998 goto done;
999 /* Try to look up references in a given namespace. */
1000 } else {
1001 err = got_reflist_insert(&new, refs, ref,
1002 cmp_cb, cmp_arg);
1003 if (err || new == NULL /* duplicate */)
1004 got_ref_close(ref);
1005 return err;
1009 if (ref_namespace) {
1010 size_t len;
1011 /* Canonicalize the path to eliminate double-slashes if any. */
1012 if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
1013 err = got_error_from_errno("asprintf");
1014 goto done;
1016 len = strlen(abs_namespace) + 1;
1017 buf = malloc(len);
1018 if (buf == NULL) {
1019 err = got_error_from_errno("malloc");
1020 goto done;
1022 err = got_canonpath(abs_namespace, buf, len);
1023 if (err)
1024 goto done;
1025 ondisk_ref_namespace = buf;
1026 while (ondisk_ref_namespace[0] == '/')
1027 ondisk_ref_namespace++;
1028 if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
1029 ondisk_ref_namespace += 5;
1030 else if (strcmp(ondisk_ref_namespace, "refs") == 0)
1031 ondisk_ref_namespace = "";
1034 /* Gather on-disk refs before parsing packed-refs. */
1035 free(path_refs);
1036 path_refs = get_refs_dir_path(repo, "");
1037 if (path_refs == NULL) {
1038 err = got_error_from_errno("get_refs_dir_path");
1039 goto done;
1041 err = gather_on_disk_refs(refs, path_refs,
1042 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
1043 cmp_cb, cmp_arg);
1044 if (err)
1045 goto done;
1048 * The packed-refs file may contain redundant entries, in which
1049 * case on-disk refs take precedence.
1051 packed_refs_path = got_repo_get_path_packed_refs(repo);
1052 if (packed_refs_path == NULL) {
1053 err = got_error_from_errno("got_repo_get_path_packed_refs");
1054 goto done;
1057 f = fopen(packed_refs_path, "re");
1058 if (f) {
1059 size_t linesize = 0;
1060 ssize_t linelen;
1061 struct stat sb;
1063 if (fstat(fileno(f), &sb) == -1) {
1064 err = got_error_from_errno2("fstat", packed_refs_path);
1065 goto done;
1067 for (;;) {
1068 linelen = getline(&line, &linesize, f);
1069 if (linelen == -1) {
1070 if (feof(f))
1071 break;
1072 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1073 goto done;
1075 if (linelen > 0 && line[linelen - 1] == '\n')
1076 line[linelen - 1] = '\0';
1077 err = parse_packed_ref_line(&ref, NULL, line,
1078 sb.st_mtime, got_repo_get_object_format(repo));
1079 if (err)
1080 goto done;
1081 if (ref) {
1082 if (ref_namespace) {
1083 const char *name;
1084 name = got_ref_get_name(ref);
1085 if (!got_path_is_child(name,
1086 ref_namespace,
1087 strlen(ref_namespace))) {
1088 got_ref_close(ref);
1089 continue;
1092 err = got_reflist_insert(&new, refs, ref,
1093 cmp_cb, cmp_arg);
1094 if (err || new == NULL /* duplicate */)
1095 got_ref_close(ref);
1096 if (err)
1097 goto done;
1101 done:
1102 free(packed_refs_path);
1103 free(abs_namespace);
1104 free(buf);
1105 free(line);
1106 free(path_refs);
1107 if (f && fclose(f) == EOF && err == NULL)
1108 err = got_error_from_errno("fclose");
1109 return err;
1112 void
1113 got_ref_list_free(struct got_reflist_head *refs)
1115 struct got_reflist_entry *re;
1117 while ((re = TAILQ_FIRST(refs))) {
1118 TAILQ_REMOVE(refs, re, entry);
1119 got_ref_close(re->ref);
1120 free(re);
1124 int
1125 got_ref_is_symbolic(struct got_reference *ref)
1127 return (ref->flags & GOT_REF_IS_SYMBOLIC);
1130 const struct got_error *
1131 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1133 if (ref->flags & GOT_REF_IS_SYMBOLIC)
1134 return got_error(GOT_ERR_BAD_REF_TYPE);
1136 memcpy(&ref->ref.ref.id, id, sizeof(ref->ref.ref.id));
1137 return NULL;
1140 const struct got_error *
1141 got_ref_change_symref(struct got_reference *ref, const char *refname)
1143 char *new_name;
1145 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1146 return got_error(GOT_ERR_BAD_REF_TYPE);
1148 new_name = strdup(refname);
1149 if (new_name == NULL)
1150 return got_error_from_errno("strdup");
1152 free(ref->ref.symref.ref);
1153 ref->ref.symref.ref = new_name;
1154 return NULL;
1157 const struct got_error *
1158 got_ref_change_symref_to_ref(struct got_reference *symref,
1159 struct got_object_id *id)
1161 if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1162 return got_error(GOT_ERR_BAD_REF_TYPE);
1164 symref->ref.ref.name = symref->ref.symref.name;
1165 memcpy(&symref->ref.ref.id, id, sizeof(symref->ref.ref.id));
1166 symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1167 return NULL;
1170 const struct got_error *
1171 got_ref_write(struct got_reference *ref, struct got_repository *repo)
1173 const struct got_error *err = NULL, *unlock_err = NULL;
1174 const char *name = got_ref_get_name(ref);
1175 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1176 struct got_lockfile *lf = NULL;
1177 FILE *f = NULL;
1178 size_t n;
1179 struct stat sb;
1181 path_refs = get_refs_dir_path(repo, name);
1182 if (path_refs == NULL) {
1183 err = got_error_from_errno2("get_refs_dir_path", name);
1184 goto done;
1187 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1188 err = got_error_from_errno("asprintf");
1189 goto done;
1192 err = got_opentemp_named(&tmppath, &f, path, "");
1193 if (err) {
1194 char *parent;
1195 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1196 goto done;
1197 err = got_path_dirname(&parent, path);
1198 if (err)
1199 goto done;
1200 err = got_path_mkdir(parent);
1201 free(parent);
1202 if (err)
1203 goto done;
1204 err = got_opentemp_named(&tmppath, &f, path, "");
1205 if (err)
1206 goto done;
1209 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1210 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1211 if (n != strlen(ref->ref.symref.ref) + 6) {
1212 err = got_ferror(f, GOT_ERR_IO);
1213 goto done;
1215 } else {
1216 char *hex;
1217 size_t len;
1219 err = got_object_id_str(&hex, &ref->ref.ref.id);
1220 if (err)
1221 goto done;
1222 len = strlen(hex);
1223 n = fprintf(f, "%s\n", hex);
1224 free(hex);
1225 if (n != len + 1) {
1226 err = got_ferror(f, GOT_ERR_IO);
1227 goto done;
1231 if (ref->lf == NULL) {
1232 err = got_lockfile_lock(&lf, path, -1);
1233 if (err)
1234 goto done;
1237 /* XXX: check if old content matches our expectations? */
1239 if (stat(path, &sb) != 0) {
1240 if (errno != ENOENT) {
1241 err = got_error_from_errno2("stat", path);
1242 goto done;
1244 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1247 if (fchmod(fileno(f), sb.st_mode) != 0) {
1248 err = got_error_from_errno2("fchmod", tmppath);
1249 goto done;
1252 if (rename(tmppath, path) != 0) {
1253 err = got_error_from_errno3("rename", tmppath, path);
1254 goto done;
1256 free(tmppath);
1257 tmppath = NULL;
1259 if (stat(path, &sb) == -1) {
1260 err = got_error_from_errno2("stat", path);
1261 goto done;
1263 ref->mtime = sb.st_mtime;
1264 done:
1265 if (ref->lf == NULL && lf)
1266 unlock_err = got_lockfile_unlock(lf, -1);
1267 if (f) {
1268 if (fclose(f) == EOF && err == NULL)
1269 err = got_error_from_errno("fclose");
1271 free(path_refs);
1272 free(path);
1273 if (tmppath) {
1274 if (unlink(tmppath) == -1 && err == NULL)
1275 err = got_error_from_errno2("unlink", tmppath);
1276 free(tmppath);
1278 return err ? err : unlock_err;
1281 static const struct got_error *
1282 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1284 const struct got_error *err = NULL, *unlock_err = NULL;
1285 struct got_lockfile *lf = NULL;
1286 FILE *f = NULL, *tmpf = NULL;
1287 char *line = NULL, *packed_refs_path, *tmppath = NULL;
1288 size_t linesize = 0;
1289 struct got_reflist_head refs;
1290 int found_delref = 0;
1292 /* The packed-refs file does not cotain symbolic references. */
1293 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1294 return got_error(GOT_ERR_BAD_REF_DATA);
1296 TAILQ_INIT(&refs);
1298 packed_refs_path = got_repo_get_path_packed_refs(repo);
1299 if (packed_refs_path == NULL)
1300 return got_error_from_errno("got_repo_get_path_packed_refs");
1302 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path, "");
1303 if (err)
1304 goto done;
1306 if (delref->lf == NULL) {
1307 err = got_lockfile_lock(&lf, packed_refs_path, -1);
1308 if (err)
1309 goto done;
1312 f = fopen(packed_refs_path, "re");
1313 if (f == NULL) {
1314 err = got_error_from_errno2("fopen", packed_refs_path);
1315 goto done;
1317 for (;;) {
1318 ssize_t linelen;
1319 struct got_reference *ref;
1320 struct got_reflist_entry *new;
1322 linelen = getline(&line, &linesize, f);
1323 if (linelen == -1) {
1324 if (feof(f))
1325 break;
1326 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1327 goto done;
1329 if (linelen > 0 && line[linelen - 1] == '\n')
1330 line[linelen - 1] = '\0';
1331 err = parse_packed_ref_line(&ref, NULL, line, 0,
1332 got_repo_get_object_format(repo));
1333 if (err)
1334 goto done;
1335 if (ref == NULL)
1336 continue;
1338 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1339 got_object_id_cmp(&ref->ref.ref.id,
1340 &delref->ref.ref.id) == 0) {
1341 found_delref = 1;
1342 got_ref_close(ref);
1343 continue;
1346 err = got_reflist_insert(&new, &refs, ref,
1347 got_ref_cmp_by_name, NULL);
1348 if (err || new == NULL /* duplicate */)
1349 got_ref_close(ref);
1350 if (err)
1351 goto done;
1354 if (found_delref) {
1355 struct got_reflist_entry *re;
1356 size_t n;
1357 struct stat sb;
1359 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1360 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1361 err = got_ferror(f, GOT_ERR_IO);
1362 goto done;
1365 TAILQ_FOREACH(re, &refs, entry) {
1366 char *hex;
1367 size_t len;
1369 err = got_object_id_str(&hex, &re->ref->ref.ref.id);
1370 if (err)
1371 goto done;
1372 len = strlen(hex);
1373 n = fprintf(tmpf, "%s ", hex);
1374 free(hex);
1375 if (n != len + 1) {
1376 err = got_ferror(f, GOT_ERR_IO);
1377 goto done;
1380 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1381 if (n != strlen(re->ref->ref.ref.name) + 1) {
1382 err = got_ferror(f, GOT_ERR_IO);
1383 goto done;
1387 if (fflush(tmpf) != 0) {
1388 err = got_error_from_errno("fflush");
1389 goto done;
1392 if (fstat(fileno(f), &sb) != 0) {
1393 if (errno != ENOENT) {
1394 err = got_error_from_errno2("fstat",
1395 packed_refs_path);
1396 goto done;
1398 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1401 if (fchmod(fileno(tmpf), sb.st_mode) != 0) {
1402 err = got_error_from_errno2("fchmod", tmppath);
1403 goto done;
1406 if (rename(tmppath, packed_refs_path) != 0) {
1407 err = got_error_from_errno3("rename", tmppath,
1408 packed_refs_path);
1409 goto done;
1411 free(tmppath);
1412 tmppath = NULL;
1414 done:
1415 if (delref->lf == NULL && lf)
1416 unlock_err = got_lockfile_unlock(lf, -1);
1417 if (f) {
1418 if (fclose(f) == EOF && err == NULL)
1419 err = got_error_from_errno("fclose");
1421 if (tmppath && unlink(tmppath) == -1 && err == NULL)
1422 err = got_error_from_errno2("unlink", tmppath);
1423 if (tmpf && fclose(tmpf) == EOF && err == NULL)
1424 err = got_error_from_errno("fclose");
1425 free(tmppath);
1426 free(packed_refs_path);
1427 free(line);
1428 got_ref_list_free(&refs);
1429 return err ? err : unlock_err;
1432 static const struct got_error *
1433 delete_loose_ref(struct got_reference *ref, struct got_repository *repo)
1435 const struct got_error *err = NULL, *unlock_err = NULL;
1436 const char *name = got_ref_get_name(ref);
1437 char *path_refs = NULL, *path = NULL;
1438 struct got_lockfile *lf = NULL;
1440 path_refs = get_refs_dir_path(repo, name);
1441 if (path_refs == NULL) {
1442 err = got_error_from_errno2("get_refs_dir_path", name);
1443 goto done;
1446 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1447 err = got_error_from_errno("asprintf");
1448 goto done;
1451 if (ref->lf == NULL) {
1452 err = got_lockfile_lock(&lf, path, -1);
1453 if (err)
1454 goto done;
1457 /* XXX: check if old content matches our expectations? */
1459 if (unlink(path) == -1)
1460 err = got_error_from_errno2("unlink", path);
1461 done:
1462 if (ref->lf == NULL && lf)
1463 unlock_err = got_lockfile_unlock(lf, -1);
1465 free(path_refs);
1466 free(path);
1467 return err ? err : unlock_err;
1470 const struct got_error *
1471 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1473 const struct got_error *err = NULL;
1474 struct got_reference *ref2;
1476 if (ref->flags & GOT_REF_IS_PACKED) {
1477 err = delete_packed_ref(ref, repo);
1478 if (err)
1479 return err;
1481 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1482 if (err) {
1483 if (err->code == GOT_ERR_NOT_REF)
1484 return NULL;
1485 return err;
1488 err = delete_loose_ref(ref2, repo);
1489 got_ref_close(ref2);
1490 return err;
1491 } else {
1492 err = delete_loose_ref(ref, repo);
1493 if (err)
1494 return err;
1496 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1497 if (err) {
1498 if (err->code == GOT_ERR_NOT_REF)
1499 return NULL;
1500 return err;
1503 err = delete_packed_ref(ref2, repo);
1504 got_ref_close(ref2);
1505 return err;
1509 const struct got_error *
1510 got_ref_unlock(struct got_reference *ref)
1512 const struct got_error *err;
1513 err = got_lockfile_unlock(ref->lf, -1);
1514 ref->lf = NULL;
1515 return err;
1518 struct got_reflist_object_id_map {
1519 struct got_object_idset *idset;
1522 struct got_reflist_object_id_map_entry {
1523 struct got_reflist_head refs;
1526 static const struct got_error *
1527 add_object_id_map_entry(struct got_object_idset *idset,
1528 struct got_object_id *id, struct got_reflist_entry *re)
1530 const struct got_error *err = NULL;
1531 struct got_reflist_object_id_map_entry *ent;
1532 struct got_reflist_entry *new;
1534 ent = got_object_idset_get(idset, id);
1535 if (ent == NULL) {
1536 ent = malloc(sizeof(*ent));
1537 if (ent == NULL)
1538 return got_error_from_errno("malloc");
1540 TAILQ_INIT(&ent->refs);
1541 err = got_object_idset_add(idset, id, ent);
1542 if (err) {
1543 free(ent);
1544 return err;
1548 err = got_reflist_entry_dup(&new, re);
1549 if (err)
1550 return err;
1552 TAILQ_INSERT_TAIL(&ent->refs, new, entry);
1553 return NULL;
1556 const struct got_error *
1557 got_reflist_object_id_map_create(struct got_reflist_object_id_map **map,
1558 struct got_reflist_head *refs, struct got_repository *repo)
1560 const struct got_error *err = NULL;
1561 struct got_object_idset *idset;
1562 struct got_object_id *id = NULL;
1563 struct got_reflist_entry *re;
1565 idset = got_object_idset_alloc();
1566 if (idset == NULL)
1567 return got_error_from_errno("got_object_idset_alloc");
1569 *map = malloc(sizeof(**map));
1570 if (*map == NULL) {
1571 got_object_idset_free(idset);
1572 return got_error_from_errno("malloc");
1574 (*map)->idset = idset;
1576 TAILQ_FOREACH(re, refs, entry) {
1577 struct got_tag_object *tag = NULL;
1579 err = got_ref_resolve(&id, repo, re->ref);
1580 if (err)
1581 goto done;
1583 err = add_object_id_map_entry(idset, id, re);
1584 if (err)
1585 goto done;
1587 if (strstr(got_ref_get_name(re->ref), "/tags/") == NULL) {
1588 free(id);
1589 id = NULL;
1590 continue;
1593 err = got_object_open_as_tag(&tag, repo, id);
1594 if (err) {
1595 if (err->code != GOT_ERR_OBJ_TYPE)
1596 goto done;
1597 /* Ref points at something other than a tag. */
1598 err = NULL;
1599 tag = NULL;
1600 free(id);
1601 id = NULL;
1602 continue;
1605 err = add_object_id_map_entry(idset,
1606 got_object_tag_get_object_id(tag), re);
1607 got_object_tag_close(tag);
1608 if (err)
1609 goto done;
1611 free(id);
1612 id = NULL;
1614 done:
1615 free(id);
1616 if (err) {
1617 got_reflist_object_id_map_free(*map);
1618 *map = NULL;
1620 return err;
1623 struct got_reflist_head *
1624 got_reflist_object_id_map_lookup(struct got_reflist_object_id_map *map,
1625 struct got_object_id *id)
1627 struct got_reflist_object_id_map_entry *ent;
1628 ent = got_object_idset_get(map->idset, id);
1629 if (ent)
1630 return &ent->refs;
1631 return NULL;
1634 static const struct got_error *
1635 free_id_map_entry(struct got_object_id *id, void *data, void *arg)
1637 struct got_reflist_object_id_map_entry *ent = data;
1639 got_ref_list_free(&ent->refs);
1640 free(ent);
1641 return NULL;
1644 void
1645 got_reflist_object_id_map_free(struct got_reflist_object_id_map *map)
1647 got_object_idset_for_each(map->idset, free_id_map_entry, NULL);
1648 got_object_idset_free(map->idset);
1649 free(map);