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 /*
93 * Cached timestamp for got_ref_cmp_by_commit_timestamp_descending()
94 * and got_ref_cmp_tags().
95 */
96 time_t committer_time;
97 };
99 static const struct got_error *
100 alloc_ref(struct got_reference **ref, const char *name,
101 struct got_object_id *id, int flags, time_t mtime)
103 const struct got_error *err = NULL;
105 *ref = calloc(1, sizeof(**ref));
106 if (*ref == NULL)
107 return got_error_from_errno("calloc");
109 memcpy(&(*ref)->ref.ref.id, id, sizeof((*ref)->ref.ref.id));
110 (*ref)->flags = flags;
111 (*ref)->ref.ref.name = strdup(name);
112 (*ref)->mtime = mtime;
113 if ((*ref)->ref.ref.name == NULL) {
114 err = got_error_from_errno("strdup");
115 got_ref_close(*ref);
116 *ref = NULL;
118 return err;
121 static const struct got_error *
122 alloc_symref(struct got_reference **ref, const char *name,
123 const char *target_ref, int flags)
125 const struct got_error *err = NULL;
127 *ref = calloc(1, sizeof(**ref));
128 if (*ref == NULL)
129 return got_error_from_errno("calloc");
131 (*ref)->flags = GOT_REF_IS_SYMBOLIC | flags;
132 (*ref)->ref.symref.name = strdup(name);
133 if ((*ref)->ref.symref.name == NULL) {
134 err = got_error_from_errno("strdup");
135 got_ref_close(*ref);
136 *ref = NULL;
137 return err;
139 (*ref)->ref.symref.ref = strdup(target_ref);
140 if ((*ref)->ref.symref.ref == NULL) {
141 err = got_error_from_errno("strdup");
142 got_ref_close(*ref);
143 *ref = NULL;
145 return err;
148 static const struct got_error *
149 parse_symref(struct got_reference **ref, const char *name, const char *line)
151 if (line[0] == '\0')
152 return got_error(GOT_ERR_BAD_REF_DATA);
154 return alloc_symref(ref, name, line, 0);
157 static const struct got_error *
158 parse_ref_line(struct got_reference **ref, const char *name, const char *line,
159 time_t mtime, enum got_hash_algorithm algo)
161 struct got_object_id id;
163 if (strncmp(line, "ref: ", 5) == 0) {
164 line += 5;
165 return parse_symref(ref, name, line);
168 if (!got_parse_object_id(&id, line, algo))
169 return got_error(GOT_ERR_BAD_REF_DATA);
171 return alloc_ref(ref, name, &id, 0, mtime);
174 static const struct got_error *
175 parse_ref_file(struct got_reference **ref, const char *name,
176 const char *absname, const char *abspath, int lock,
177 enum got_hash_algorithm algo)
179 const struct got_error *err = NULL;
180 FILE *f;
181 char *line = NULL;
182 size_t linesize = 0;
183 ssize_t linelen;
184 struct got_lockfile *lf = NULL;
185 struct stat sb;
187 if (lock) {
188 err = got_lockfile_lock(&lf, abspath, -1);
189 if (err) {
190 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
191 err = got_error_not_ref(name);
192 return err;
196 f = fopen(abspath, "rbe");
197 if (f == NULL) {
198 if (errno != ENOTDIR && errno != ENOENT)
199 err = got_error_from_errno2("fopen", abspath);
200 else
201 err = got_error_not_ref(name);
202 if (lock)
203 got_lockfile_unlock(lf, -1);
204 return err;
206 if (fstat(fileno(f), &sb) == -1) {
207 err = got_error_from_errno2("fstat", abspath);
208 goto done;
211 linelen = getline(&line, &linesize, f);
212 if (linelen == -1) {
213 if (feof(f))
214 err = NULL; /* ignore empty files (could be locks) */
215 else {
216 if (errno == EISDIR)
217 err = got_error(GOT_ERR_NOT_REF);
218 else if (ferror(f))
219 err = got_ferror(f, GOT_ERR_IO);
220 else
221 err = got_error_from_errno2("getline", abspath);
223 if (lock)
224 got_lockfile_unlock(lf, -1);
225 goto done;
227 while (linelen > 0 && line[linelen - 1] == '\n') {
228 line[linelen - 1] = '\0';
229 linelen--;
232 err = parse_ref_line(ref, absname, line, sb.st_mtime, algo);
233 if (lock) {
234 if (err)
235 got_lockfile_unlock(lf, -1);
236 else {
237 if (*ref)
238 (*ref)->lf = lf;
239 else
240 got_lockfile_unlock(lf, -1);
243 done:
244 free(line);
245 if (fclose(f) == EOF && err == NULL) {
246 err = got_error_from_errno("fclose");
247 if (*ref) {
248 if (lock)
249 got_ref_unlock(*ref);
250 got_ref_close(*ref);
251 *ref = NULL;
254 return err;
257 static int
258 is_well_known_ref(const char *refname)
260 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
261 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
262 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
263 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
266 static char *
267 get_refs_dir_path(struct got_repository *repo, const char *refname)
269 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
270 return strdup(got_repo_get_path_git_dir(repo));
272 return got_repo_get_path_refs(repo);
275 const struct got_error *
276 got_ref_alloc(struct got_reference **ref, const char *name,
277 struct got_object_id *id)
279 if (!got_ref_name_is_valid(name))
280 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
282 return alloc_ref(ref, name, id, 0, 0);
285 const struct got_error *
286 got_ref_alloc_symref(struct got_reference **ref, const char *name,
287 struct got_reference *target_ref)
289 if (!got_ref_name_is_valid(name))
290 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
292 return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
295 static const struct got_error *
296 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
297 const char *line, time_t mtime, enum got_hash_algorithm algo)
299 struct got_object_id id;
300 const char *name;
302 *ref = NULL;
304 if (line[0] == '#' || line[0] == '^')
305 return NULL;
307 if (!got_parse_object_id(&id, line, algo))
308 return got_error(GOT_ERR_BAD_REF_DATA);
310 if (abs_refname) {
311 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
312 return NULL;
313 name = abs_refname;
314 } else
315 name = line + SHA1_DIGEST_STRING_LENGTH;
317 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED, mtime);
320 static const struct got_error *
321 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
322 int nsubdirs, const char *refname, time_t mtime,
323 enum got_hash_algorithm algo)
325 const struct got_error *err = NULL;
326 char *abs_refname;
327 char *line = NULL;
328 size_t linesize = 0;
329 ssize_t linelen;
330 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
332 *ref = NULL;
334 if (ref_is_absolute)
335 abs_refname = (char *)refname;
336 do {
337 linelen = getline(&line, &linesize, f);
338 if (linelen == -1) {
339 if (feof(f))
340 break;
341 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
342 break;
344 if (linelen > 0 && line[linelen - 1] == '\n')
345 line[linelen - 1] = '\0';
346 for (i = 0; i < nsubdirs; i++) {
347 if (!ref_is_absolute &&
348 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
349 refname) == -1)
350 return got_error_from_errno("asprintf");
351 err = parse_packed_ref_line(ref, abs_refname, line,
352 mtime, algo);
353 if (!ref_is_absolute)
354 free(abs_refname);
355 if (err || *ref != NULL)
356 break;
358 if (err)
359 break;
360 } while (*ref == NULL);
361 free(line);
363 return err;
366 static const struct got_error *
367 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
368 const char *name, int lock, enum got_hash_algorithm algo)
370 const struct got_error *err = NULL;
371 char *path = NULL;
372 char *absname = NULL;
373 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
374 int ref_is_well_known = (subdir[0] == '\0' && is_well_known_ref(name));
376 *ref = NULL;
378 if (!got_ref_name_is_valid(name))
379 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
381 if (ref_is_absolute || ref_is_well_known) {
382 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
383 return got_error_from_errno("asprintf");
384 absname = (char *)name;
385 } else {
386 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
387 subdir[0] ? "/" : "", name) == -1)
388 return got_error_from_errno("asprintf");
390 if (asprintf(&absname, "refs/%s%s%s",
391 subdir, subdir[0] ? "/" : "", name) == -1) {
392 err = got_error_from_errno("asprintf");
393 goto done;
397 err = parse_ref_file(ref, name, absname, path, lock, algo);
398 done:
399 if (!ref_is_absolute && !ref_is_well_known)
400 free(absname);
401 free(path);
402 return err;
405 const struct got_error *
406 got_ref_open(struct got_reference **ref, struct got_repository *repo,
407 const char *refname, int lock)
409 const struct got_error *err = NULL;
410 char *packed_refs_path = NULL, *path_refs = NULL;
411 const char *subdirs[] = {
412 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
413 };
414 size_t i;
415 int well_known = is_well_known_ref(refname);
416 struct got_lockfile *lf = NULL;
418 *ref = NULL;
420 path_refs = get_refs_dir_path(repo, refname);
421 if (path_refs == NULL) {
422 err = got_error_from_errno2("get_refs_dir_path", refname);
423 goto done;
426 if (well_known) {
427 err = open_ref(ref, path_refs, "", refname, lock,
428 got_repo_get_object_format(repo));
429 } else {
430 FILE *f;
432 /* Search on-disk refs before packed refs! */
433 for (i = 0; i < nitems(subdirs); i++) {
434 err = open_ref(ref, path_refs, subdirs[i], refname,
435 lock, got_repo_get_object_format(repo));
436 if ((err && err->code != GOT_ERR_NOT_REF) || *ref)
437 goto done;
440 packed_refs_path = got_repo_get_path_packed_refs(repo);
441 if (packed_refs_path == NULL) {
442 err = got_error_from_errno(
443 "got_repo_get_path_packed_refs");
444 goto done;
447 if (lock) {
448 err = got_lockfile_lock(&lf, packed_refs_path, -1);
449 if (err)
450 goto done;
452 f = fopen(packed_refs_path, "rbe");
453 if (f != NULL) {
454 struct stat sb;
455 if (fstat(fileno(f), &sb) == -1) {
456 err = got_error_from_errno2("fstat",
457 packed_refs_path);
458 goto done;
460 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
461 refname, sb.st_mtime,
462 got_repo_get_object_format(repo));
463 if (!err) {
464 if (fclose(f) == EOF) {
465 err = got_error_from_errno("fclose");
466 got_ref_close(*ref);
467 *ref = NULL;
468 } else if (*ref)
469 (*ref)->lf = lf;
473 done:
474 if (!err && *ref == NULL)
475 err = got_error_not_ref(refname);
476 if (err && lf)
477 got_lockfile_unlock(lf, -1);
478 free(packed_refs_path);
479 free(path_refs);
480 return err;
483 void
484 got_ref_close(struct got_reference *ref)
486 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
487 free(ref->ref.symref.name);
488 free(ref->ref.symref.ref);
489 } else
490 free(ref->ref.ref.name);
491 free(ref);
494 struct got_reference *
495 got_ref_dup(struct got_reference *ref)
497 struct got_reference *ret;
499 ret = calloc(1, sizeof(*ret));
500 if (ret == NULL)
501 return NULL;
503 ret->flags = ref->flags;
504 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
505 ret->ref.symref.name = strdup(ref->ref.symref.name);
506 if (ret->ref.symref.name == NULL) {
507 free(ret);
508 return NULL;
510 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
511 if (ret->ref.symref.ref == NULL) {
512 free(ret->ref.symref.name);
513 free(ret);
514 return NULL;
516 } else {
517 ret->ref.ref.name = strdup(ref->ref.ref.name);
518 if (ret->ref.ref.name == NULL) {
519 free(ret);
520 return NULL;
522 memcpy(&ret->ref.ref.id, &ref->ref.ref.id,
523 sizeof(ret->ref.ref.id));
526 return ret;
529 const struct got_error *
530 got_reflist_entry_dup(struct got_reflist_entry **newp,
531 struct got_reflist_entry *re)
533 const struct got_error *err = NULL;
534 struct got_reflist_entry *new;
536 *newp = NULL;
538 new = malloc(sizeof(*new));
539 if (new == NULL)
540 return got_error_from_errno("malloc");
542 new->ref = got_ref_dup(re->ref);
543 if (new->ref == NULL) {
544 err = got_error_from_errno("got_ref_dup");
545 free(new);
546 return err;
549 *newp = new;
550 return NULL;
553 const struct got_error *
554 got_ref_resolve_symbolic(struct got_reference **resolved,
555 struct got_repository *repo, struct got_reference *ref)
557 struct got_reference *nextref;
558 const struct got_error *err;
560 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
561 if (err)
562 return err;
564 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
565 err = got_ref_resolve_symbolic(resolved, repo, nextref);
566 else
567 *resolved = got_ref_dup(nextref);
569 got_ref_close(nextref);
570 return err;
573 static const struct got_error *
574 ref_resolve(struct got_object_id **id, struct got_repository *repo,
575 struct got_reference *ref, int recursion)
577 const struct got_error *err;
579 if (recursion <= 0)
580 return got_error_msg(GOT_ERR_RECURSION,
581 "reference recursion limit reached");
583 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
584 struct got_reference *resolved = NULL;
585 err = got_ref_resolve_symbolic(&resolved, repo, ref);
586 if (err == NULL)
587 err = ref_resolve(id, repo, resolved, --recursion);
588 if (resolved)
589 got_ref_close(resolved);
590 return err;
593 *id = calloc(1, sizeof(**id));
594 if (*id == NULL)
595 return got_error_from_errno("calloc");
596 memcpy(*id, &ref->ref.ref.id, sizeof(**id));
597 return NULL;
600 const struct got_error *
601 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
602 struct got_reference *ref)
604 return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
607 char *
608 got_ref_to_str(struct got_reference *ref)
610 char *str;
612 if (ref->flags & GOT_REF_IS_SYMBOLIC)
613 return strdup(ref->ref.symref.ref);
615 if (got_object_id_str(&str, &ref->ref.ref.id) != NULL)
616 return NULL;
618 return str;
621 const char *
622 got_ref_get_name(struct got_reference *ref)
624 if (ref->flags & GOT_REF_IS_SYMBOLIC)
625 return ref->ref.symref.name;
627 return ref->ref.ref.name;
630 const char *
631 got_ref_get_symref_target(struct got_reference *ref)
633 if (ref->flags & GOT_REF_IS_SYMBOLIC)
634 return ref->ref.symref.ref;
636 return NULL;
639 time_t
640 got_ref_get_mtime(struct got_reference *ref)
642 return ref->mtime;
645 const struct got_error *
646 got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
647 struct got_reference* re2)
649 const char *name1 = got_ref_get_name(re1);
650 const char *name2 = got_ref_get_name(re2);
652 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
653 return NULL;
656 static const struct got_error *
657 get_committer_time(struct got_reference *ref, struct got_repository *repo)
659 const struct got_error *err = NULL;
660 int obj_type;
661 struct got_commit_object *commit = NULL;
662 struct got_tag_object *tag = NULL;
663 struct got_object_id *id = NULL;
665 err = got_ref_resolve(&id, repo, ref);
666 if (err)
667 return err;
669 err = got_object_get_type(&obj_type, repo, id);
670 if (err)
671 goto done;
673 switch (obj_type) {
674 case GOT_OBJ_TYPE_COMMIT:
675 err = got_object_open_as_commit(&commit, repo, id);
676 if (err)
677 goto done;
678 ref->committer_time =
679 got_object_commit_get_committer_time(commit);
680 break;
681 case GOT_OBJ_TYPE_TAG:
682 err = got_object_open_as_tag(&tag, repo, id);
683 if (err)
684 goto done;
685 ref->committer_time = got_object_tag_get_tagger_time(tag);
686 break;
687 default:
688 /* best effort for other object types */
689 ref->committer_time = got_ref_get_mtime(ref);
690 break;
692 done:
693 free(id);
694 if (commit)
695 got_object_commit_close(commit);
696 if (tag)
697 got_object_tag_close(tag);
698 return err;
701 const struct got_error *
702 got_ref_cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
703 struct got_reference *ref2)
705 const struct got_error *err = NULL;
706 struct got_repository *repo = arg;
708 *cmp = 0;
710 if (ref1->committer_time == 0) {
711 err = get_committer_time(ref1, repo);
712 if (err)
713 return err;
715 if (ref2->committer_time == 0) {
716 err = get_committer_time(ref2, repo);
717 if (err)
718 return err;
721 /* Put latest tags first. */
722 if (ref1->committer_time < ref2->committer_time)
723 *cmp = 1;
724 else if (ref1->committer_time > ref2->committer_time)
725 *cmp = -1;
726 else
727 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
729 return err;
732 const struct got_error *
733 got_ref_cmp_by_commit_timestamp_descending(void *arg, int *cmp,
734 struct got_reference *ref1, struct got_reference *ref2)
736 const struct got_error *err = NULL;
737 struct got_repository *repo = arg;
739 *cmp = 0;
741 if (ref1->committer_time == 0) {
742 err = get_committer_time(ref1, repo);
743 if (err)
744 return err;
746 if (ref2->committer_time == 0) {
747 err = get_committer_time(ref2, repo);
748 if (err)
749 return err;
752 if (ref1->committer_time < ref2->committer_time)
753 *cmp = 1;
754 else if (ref2->committer_time < ref1->committer_time)
755 *cmp = -1;
756 else
757 return got_ref_cmp_by_name(arg, cmp, ref1, ref2);
759 return err;
762 const struct got_error *
763 got_reflist_insert(struct got_reflist_entry **newp,
764 struct got_reflist_head *refs, struct got_reference *ref,
765 got_ref_cmp_cb cmp_cb, void *cmp_arg)
767 const struct got_error *err;
768 struct got_reflist_entry *new, *re;
769 int cmp;
771 *newp = NULL;
773 if (cmp_cb != got_ref_cmp_by_name && (ref->flags & GOT_REF_IS_PACKED)) {
774 /*
775 * If we are not sorting elements by name then we must still
776 * detect collisions between a packed ref and an on-disk ref
777 * using the same name. On-disk refs take precedence and are
778 * already present on the list before packed refs get added.
779 */
780 TAILQ_FOREACH(re, refs, entry) {
781 err = got_ref_cmp_by_name(NULL, &cmp, re->ref, ref);
782 if (err)
783 return err;
784 if (cmp == 0)
785 return NULL;
789 new = malloc(sizeof(*new));
790 if (new == NULL)
791 return got_error_from_errno("malloc");
792 new->ref = ref;
793 *newp = new;
795 /*
796 * We must de-duplicate entries on insert because packed-refs may
797 * contain redundant entries. On-disk refs take precedence.
798 * This code assumes that on-disk revs are read before packed-refs.
799 * We're iterating the list anyway, so insert elements sorted by name.
801 * Many callers will provide paths in a somewhat sorted order.
802 * Iterating backwards from the tail of the list should be more
803 * efficient than traversing through the entire list each time
804 * an element is inserted.
805 */
806 re = TAILQ_LAST(refs, got_reflist_head);
807 while (re) {
808 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
809 if (err)
810 return err;
811 if (cmp == 0) {
812 /* duplicate */
813 free(new);
814 *newp = NULL;
815 return NULL;
816 } else if (cmp < 0) {
817 TAILQ_INSERT_AFTER(refs, re, new, entry);
818 return NULL;
820 re = TAILQ_PREV(re, got_reflist_head, entry);
823 TAILQ_INSERT_HEAD(refs, new, entry);
824 return NULL;
827 const struct got_error *
828 got_reflist_sort(struct got_reflist_head *refs,
829 got_ref_cmp_cb cmp_cb, void *cmp_arg)
831 const struct got_error *err = NULL;
832 struct got_reflist_entry *re, *tmp, *new;
833 struct got_reflist_head sorted;
835 TAILQ_INIT(&sorted);
837 TAILQ_FOREACH_SAFE(re, refs, entry, tmp) {
838 struct got_reference *ref = re->ref;
839 TAILQ_REMOVE(refs, re, entry);
840 free(re);
841 err = got_reflist_insert(&new, &sorted, ref, cmp_cb, cmp_arg);
842 if (err || new == NULL /* duplicate */)
843 got_ref_close(ref);
844 if (err)
845 return err;
848 TAILQ_CONCAT(refs, &sorted, entry);
849 return NULL;
852 static const struct got_error *
853 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
854 const char *subdir, struct got_repository *repo,
855 got_ref_cmp_cb cmp_cb, void *cmp_arg)
857 const struct got_error *err = NULL;
858 DIR *d = NULL;
859 char *path_subdir;
860 struct got_reference *ref;
861 struct got_reflist_entry *new;
863 while (subdir[0] == '/')
864 subdir++;
866 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
867 return got_error_from_errno("asprintf");
869 d = opendir(path_subdir);
870 if (d == NULL) {
871 char *refname;
873 if (errno != ENOTDIR)
874 goto done;
876 /* This could be a regular on-disk reference file. */
877 free(path_subdir);
878 err = got_path_dirname(&path_subdir, subdir);
879 if (err)
880 return err;
881 err = got_path_basename(&refname, subdir);
882 if (err) {
883 free(path_subdir);
884 return err;
886 err = open_ref(&ref, path_refs, path_subdir, refname,
887 0, got_repo_get_object_format(repo));
888 free(path_subdir);
889 free(refname);
890 if (err) {
891 if (err->code == GOT_ERR_NOT_REF)
892 return NULL;
893 return err;
895 err = got_reflist_insert(&new, refs, ref,
896 cmp_cb, cmp_arg);
897 if (err || new == NULL /* duplicate */)
898 got_ref_close(ref);
899 return err;
902 for (;;) {
903 struct dirent *dent;
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 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 static int
958 match_packed_ref(struct got_reference *ref, const char *ref_namespace)
960 const char *name = got_ref_get_name(ref);
961 int namespace_is_absolute = (strncmp(ref_namespace, "refs/", 5) == 0);
963 if (namespace_is_absolute) {
964 return (strcmp(name, ref_namespace) == 0 ||
965 got_path_is_child(name, ref_namespace,
966 strlen(ref_namespace)));
969 /* Match all "subdirectories" as we do with on-disk refs. */
970 while (*name != '\0') {
971 while (*name == '/')
972 name++;
973 if (strcmp(name, ref_namespace) == 0 ||
974 got_path_is_child(name, ref_namespace,
975 strlen(ref_namespace)))
976 return 1;
977 while (*name != '\0' && *name != '/')
978 name++;
981 return 0;
984 const struct got_error *
985 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
986 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
988 const struct got_error *err;
989 char *packed_refs_path = NULL, *path_refs = NULL;
990 char *abs_namespace = NULL, *buf = NULL;
991 const char *ondisk_ref_namespace = NULL;
992 char *line = NULL;
993 FILE *f = NULL;
994 struct got_reference *ref;
995 struct got_reflist_entry *new;
997 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
998 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
999 if (path_refs == NULL) {
1000 err = got_error_from_errno("get_refs_dir_path");
1001 goto done;
1003 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0,
1004 got_repo_get_object_format(repo));
1005 if (err)
1006 goto done;
1007 err = got_reflist_insert(&new, refs, ref, cmp_cb, cmp_arg);
1008 if (err || new == NULL /* duplicate */)
1009 got_ref_close(ref);
1010 if (err && err->code != GOT_ERR_NOT_REF)
1011 goto done;
1012 } else {
1013 /* Try listing a single reference. */
1014 err = got_ref_open(&ref, repo, ref_namespace, 0);
1015 if (err) {
1016 if (err->code != GOT_ERR_NOT_REF)
1017 goto done;
1018 /* Try to look up references in a given namespace. */
1019 } else {
1020 err = got_reflist_insert(&new, refs, ref,
1021 cmp_cb, cmp_arg);
1022 if (err || new == NULL /* duplicate */)
1023 got_ref_close(ref);
1024 return err;
1028 if (ref_namespace) {
1029 size_t len;
1030 /* Canonicalize the path to eliminate double-slashes if any. */
1031 if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
1032 err = got_error_from_errno("asprintf");
1033 goto done;
1035 len = strlen(abs_namespace) + 1;
1036 buf = malloc(len);
1037 if (buf == NULL) {
1038 err = got_error_from_errno("malloc");
1039 goto done;
1041 err = got_canonpath(abs_namespace, buf, len);
1042 if (err)
1043 goto done;
1044 ondisk_ref_namespace = buf;
1045 while (ondisk_ref_namespace[0] == '/')
1046 ondisk_ref_namespace++;
1047 if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
1048 ondisk_ref_namespace += 5;
1049 else if (strcmp(ondisk_ref_namespace, "refs") == 0)
1050 ondisk_ref_namespace = "";
1053 /* Gather on-disk refs before parsing packed-refs. */
1054 free(path_refs);
1055 path_refs = get_refs_dir_path(repo, "");
1056 if (path_refs == NULL) {
1057 err = got_error_from_errno("get_refs_dir_path");
1058 goto done;
1060 err = gather_on_disk_refs(refs, path_refs,
1061 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
1062 cmp_cb, cmp_arg);
1063 if (err)
1064 goto done;
1067 * The packed-refs file may contain redundant entries, in which
1068 * case on-disk refs take precedence.
1070 packed_refs_path = got_repo_get_path_packed_refs(repo);
1071 if (packed_refs_path == NULL) {
1072 err = got_error_from_errno("got_repo_get_path_packed_refs");
1073 goto done;
1076 f = fopen(packed_refs_path, "re");
1077 if (f) {
1078 size_t linesize = 0;
1079 ssize_t linelen;
1080 struct stat sb;
1082 if (fstat(fileno(f), &sb) == -1) {
1083 err = got_error_from_errno2("fstat", packed_refs_path);
1084 goto done;
1086 for (;;) {
1087 linelen = getline(&line, &linesize, f);
1088 if (linelen == -1) {
1089 if (feof(f))
1090 break;
1091 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1092 goto done;
1094 if (linelen > 0 && line[linelen - 1] == '\n')
1095 line[linelen - 1] = '\0';
1096 err = parse_packed_ref_line(&ref, NULL, line,
1097 sb.st_mtime, got_repo_get_object_format(repo));
1098 if (err)
1099 goto done;
1100 if (ref) {
1101 if (ref_namespace &&
1102 !match_packed_ref(ref, ref_namespace)) {
1103 got_ref_close(ref);
1104 continue;
1106 err = got_reflist_insert(&new, refs, ref,
1107 cmp_cb, cmp_arg);
1108 if (err || new == NULL /* duplicate */)
1109 got_ref_close(ref);
1110 if (err)
1111 goto done;
1115 done:
1116 free(packed_refs_path);
1117 free(abs_namespace);
1118 free(buf);
1119 free(line);
1120 free(path_refs);
1121 if (f && fclose(f) == EOF && err == NULL)
1122 err = got_error_from_errno("fclose");
1123 return err;
1126 void
1127 got_ref_list_free(struct got_reflist_head *refs)
1129 struct got_reflist_entry *re;
1131 while ((re = TAILQ_FIRST(refs))) {
1132 TAILQ_REMOVE(refs, re, entry);
1133 got_ref_close(re->ref);
1134 free(re);
1138 int
1139 got_ref_is_symbolic(struct got_reference *ref)
1141 return (ref->flags & GOT_REF_IS_SYMBOLIC);
1144 const struct got_error *
1145 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1147 if (ref->flags & GOT_REF_IS_SYMBOLIC)
1148 return got_error(GOT_ERR_BAD_REF_TYPE);
1150 memcpy(&ref->ref.ref.id, id, sizeof(ref->ref.ref.id));
1151 return NULL;
1154 const struct got_error *
1155 got_ref_change_symref(struct got_reference *ref, const char *refname)
1157 char *new_name;
1159 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1160 return got_error(GOT_ERR_BAD_REF_TYPE);
1162 new_name = strdup(refname);
1163 if (new_name == NULL)
1164 return got_error_from_errno("strdup");
1166 free(ref->ref.symref.ref);
1167 ref->ref.symref.ref = new_name;
1168 return NULL;
1171 const struct got_error *
1172 got_ref_change_symref_to_ref(struct got_reference *symref,
1173 struct got_object_id *id)
1175 if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1176 return got_error(GOT_ERR_BAD_REF_TYPE);
1178 symref->ref.ref.name = symref->ref.symref.name;
1179 memcpy(&symref->ref.ref.id, id, sizeof(symref->ref.ref.id));
1180 symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1181 return NULL;
1184 const struct got_error *
1185 got_ref_write(struct got_reference *ref, struct got_repository *repo)
1187 const struct got_error *err = NULL, *unlock_err = NULL;
1188 const char *name = got_ref_get_name(ref);
1189 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1190 struct got_lockfile *lf = NULL;
1191 FILE *f = NULL;
1192 size_t n;
1193 struct stat sb;
1195 path_refs = get_refs_dir_path(repo, name);
1196 if (path_refs == NULL) {
1197 err = got_error_from_errno2("get_refs_dir_path", name);
1198 goto done;
1201 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1202 err = got_error_from_errno("asprintf");
1203 goto done;
1206 err = got_opentemp_named(&tmppath, &f, path, "");
1207 if (err) {
1208 char *parent;
1209 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1210 goto done;
1211 err = got_path_dirname(&parent, path);
1212 if (err)
1213 goto done;
1214 err = got_path_mkdir(parent);
1215 free(parent);
1216 if (err)
1217 goto done;
1218 err = got_opentemp_named(&tmppath, &f, path, "");
1219 if (err)
1220 goto done;
1223 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1224 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1225 if (n != strlen(ref->ref.symref.ref) + 6) {
1226 err = got_ferror(f, GOT_ERR_IO);
1227 goto done;
1229 } else {
1230 char *hex;
1231 size_t len;
1233 err = got_object_id_str(&hex, &ref->ref.ref.id);
1234 if (err)
1235 goto done;
1236 len = strlen(hex);
1237 n = fprintf(f, "%s\n", hex);
1238 free(hex);
1239 if (n != len + 1) {
1240 err = got_ferror(f, GOT_ERR_IO);
1241 goto done;
1245 if (ref->lf == NULL) {
1246 err = got_lockfile_lock(&lf, path, -1);
1247 if (err)
1248 goto done;
1251 /* XXX: check if old content matches our expectations? */
1253 if (stat(path, &sb) != 0) {
1254 if (errno != ENOENT) {
1255 err = got_error_from_errno2("stat", path);
1256 goto done;
1258 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1261 if (fchmod(fileno(f), sb.st_mode) != 0) {
1262 err = got_error_from_errno2("fchmod", tmppath);
1263 goto done;
1266 if (rename(tmppath, path) != 0) {
1267 err = got_error_from_errno3("rename", tmppath, path);
1268 goto done;
1270 free(tmppath);
1271 tmppath = NULL;
1273 if (stat(path, &sb) == -1) {
1274 err = got_error_from_errno2("stat", path);
1275 goto done;
1277 ref->mtime = sb.st_mtime;
1278 done:
1279 if (ref->lf == NULL && lf)
1280 unlock_err = got_lockfile_unlock(lf, -1);
1281 if (f) {
1282 if (fclose(f) == EOF && err == NULL)
1283 err = got_error_from_errno("fclose");
1285 free(path_refs);
1286 free(path);
1287 if (tmppath) {
1288 if (unlink(tmppath) == -1 && err == NULL)
1289 err = got_error_from_errno2("unlink", tmppath);
1290 free(tmppath);
1292 return err ? err : unlock_err;
1295 static const struct got_error *
1296 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1298 const struct got_error *err = NULL, *unlock_err = NULL;
1299 struct got_lockfile *lf = NULL;
1300 FILE *f = NULL, *tmpf = NULL;
1301 char *line = NULL, *packed_refs_path, *tmppath = NULL;
1302 size_t linesize = 0;
1303 struct got_reflist_head refs;
1304 int found_delref = 0;
1306 /* The packed-refs file does not cotain symbolic references. */
1307 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1308 return got_error(GOT_ERR_BAD_REF_DATA);
1310 TAILQ_INIT(&refs);
1312 packed_refs_path = got_repo_get_path_packed_refs(repo);
1313 if (packed_refs_path == NULL)
1314 return got_error_from_errno("got_repo_get_path_packed_refs");
1316 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path, "");
1317 if (err)
1318 goto done;
1320 if (delref->lf == NULL) {
1321 err = got_lockfile_lock(&lf, packed_refs_path, -1);
1322 if (err)
1323 goto done;
1326 f = fopen(packed_refs_path, "re");
1327 if (f == NULL) {
1328 err = got_error_from_errno2("fopen", packed_refs_path);
1329 goto done;
1331 for (;;) {
1332 ssize_t linelen;
1333 struct got_reference *ref;
1334 struct got_reflist_entry *new;
1336 linelen = getline(&line, &linesize, f);
1337 if (linelen == -1) {
1338 if (feof(f))
1339 break;
1340 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1341 goto done;
1343 if (linelen > 0 && line[linelen - 1] == '\n')
1344 line[linelen - 1] = '\0';
1345 err = parse_packed_ref_line(&ref, NULL, line, 0,
1346 got_repo_get_object_format(repo));
1347 if (err)
1348 goto done;
1349 if (ref == NULL)
1350 continue;
1352 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1353 got_object_id_cmp(&ref->ref.ref.id,
1354 &delref->ref.ref.id) == 0) {
1355 found_delref = 1;
1356 got_ref_close(ref);
1357 continue;
1360 err = got_reflist_insert(&new, &refs, ref,
1361 got_ref_cmp_by_name, NULL);
1362 if (err || new == NULL /* duplicate */)
1363 got_ref_close(ref);
1364 if (err)
1365 goto done;
1368 if (found_delref) {
1369 struct got_reflist_entry *re;
1370 size_t n;
1371 struct stat sb;
1373 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1374 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1375 err = got_ferror(f, GOT_ERR_IO);
1376 goto done;
1379 TAILQ_FOREACH(re, &refs, entry) {
1380 char *hex;
1381 size_t len;
1383 err = got_object_id_str(&hex, &re->ref->ref.ref.id);
1384 if (err)
1385 goto done;
1386 len = strlen(hex);
1387 n = fprintf(tmpf, "%s ", hex);
1388 free(hex);
1389 if (n != len + 1) {
1390 err = got_ferror(f, GOT_ERR_IO);
1391 goto done;
1394 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1395 if (n != strlen(re->ref->ref.ref.name) + 1) {
1396 err = got_ferror(f, GOT_ERR_IO);
1397 goto done;
1401 if (fflush(tmpf) != 0) {
1402 err = got_error_from_errno("fflush");
1403 goto done;
1406 if (fstat(fileno(f), &sb) != 0) {
1407 if (errno != ENOENT) {
1408 err = got_error_from_errno2("fstat",
1409 packed_refs_path);
1410 goto done;
1412 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1415 if (fchmod(fileno(tmpf), sb.st_mode) != 0) {
1416 err = got_error_from_errno2("fchmod", tmppath);
1417 goto done;
1420 if (rename(tmppath, packed_refs_path) != 0) {
1421 err = got_error_from_errno3("rename", tmppath,
1422 packed_refs_path);
1423 goto done;
1425 free(tmppath);
1426 tmppath = NULL;
1428 done:
1429 if (delref->lf == NULL && lf)
1430 unlock_err = got_lockfile_unlock(lf, -1);
1431 if (f) {
1432 if (fclose(f) == EOF && err == NULL)
1433 err = got_error_from_errno("fclose");
1435 if (tmppath && unlink(tmppath) == -1 && err == NULL)
1436 err = got_error_from_errno2("unlink", tmppath);
1437 if (tmpf && fclose(tmpf) == EOF && err == NULL)
1438 err = got_error_from_errno("fclose");
1439 free(tmppath);
1440 free(packed_refs_path);
1441 free(line);
1442 got_ref_list_free(&refs);
1443 return err ? err : unlock_err;
1446 static const struct got_error *
1447 delete_loose_ref(struct got_reference *ref, struct got_repository *repo)
1449 const struct got_error *err = NULL, *unlock_err = NULL;
1450 const char *name = got_ref_get_name(ref);
1451 char *path_refs = NULL, *path = NULL;
1452 struct got_lockfile *lf = NULL;
1454 path_refs = get_refs_dir_path(repo, name);
1455 if (path_refs == NULL) {
1456 err = got_error_from_errno2("get_refs_dir_path", name);
1457 goto done;
1460 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1461 err = got_error_from_errno("asprintf");
1462 goto done;
1465 if (ref->lf == NULL) {
1466 err = got_lockfile_lock(&lf, path, -1);
1467 if (err)
1468 goto done;
1471 /* XXX: check if old content matches our expectations? */
1473 if (unlink(path) == -1)
1474 err = got_error_from_errno2("unlink", path);
1475 done:
1476 if (ref->lf == NULL && lf)
1477 unlock_err = got_lockfile_unlock(lf, -1);
1479 free(path_refs);
1480 free(path);
1481 return err ? err : unlock_err;
1484 const struct got_error *
1485 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1487 const struct got_error *err = NULL;
1488 struct got_reference *ref2;
1490 if (ref->flags & GOT_REF_IS_PACKED) {
1491 err = delete_packed_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_loose_ref(ref2, repo);
1503 got_ref_close(ref2);
1504 return err;
1505 } else {
1506 err = delete_loose_ref(ref, repo);
1507 if (err)
1508 return err;
1510 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1511 if (err) {
1512 if (err->code == GOT_ERR_NOT_REF)
1513 return NULL;
1514 return err;
1517 err = delete_packed_ref(ref2, repo);
1518 got_ref_close(ref2);
1519 return err;
1523 const struct got_error *
1524 got_ref_unlock(struct got_reference *ref)
1526 const struct got_error *err;
1527 err = got_lockfile_unlock(ref->lf, -1);
1528 ref->lf = NULL;
1529 return err;
1532 struct got_reflist_object_id_map {
1533 struct got_object_idset *idset;
1536 struct got_reflist_object_id_map_entry {
1537 struct got_reflist_head refs;
1540 static const struct got_error *
1541 add_object_id_map_entry(struct got_object_idset *idset,
1542 struct got_object_id *id, struct got_reflist_entry *re)
1544 const struct got_error *err = NULL;
1545 struct got_reflist_object_id_map_entry *ent;
1546 struct got_reflist_entry *new;
1548 ent = got_object_idset_get(idset, id);
1549 if (ent == NULL) {
1550 ent = malloc(sizeof(*ent));
1551 if (ent == NULL)
1552 return got_error_from_errno("malloc");
1554 TAILQ_INIT(&ent->refs);
1555 err = got_object_idset_add(idset, id, ent);
1556 if (err) {
1557 free(ent);
1558 return err;
1562 err = got_reflist_entry_dup(&new, re);
1563 if (err)
1564 return err;
1566 TAILQ_INSERT_TAIL(&ent->refs, new, entry);
1567 return NULL;
1570 const struct got_error *
1571 got_reflist_object_id_map_create(struct got_reflist_object_id_map **map,
1572 struct got_reflist_head *refs, struct got_repository *repo)
1574 const struct got_error *err = NULL;
1575 struct got_object_idset *idset;
1576 struct got_object_id *id = NULL;
1577 struct got_reflist_entry *re;
1579 idset = got_object_idset_alloc();
1580 if (idset == NULL)
1581 return got_error_from_errno("got_object_idset_alloc");
1583 *map = malloc(sizeof(**map));
1584 if (*map == NULL) {
1585 got_object_idset_free(idset);
1586 return got_error_from_errno("malloc");
1588 (*map)->idset = idset;
1590 TAILQ_FOREACH(re, refs, entry) {
1591 struct got_tag_object *tag = NULL;
1593 err = got_ref_resolve(&id, repo, re->ref);
1594 if (err)
1595 goto done;
1597 err = add_object_id_map_entry(idset, id, re);
1598 if (err)
1599 goto done;
1601 if (strstr(got_ref_get_name(re->ref), "/tags/") == NULL) {
1602 free(id);
1603 id = NULL;
1604 continue;
1607 err = got_object_open_as_tag(&tag, repo, id);
1608 if (err) {
1609 if (err->code != GOT_ERR_OBJ_TYPE)
1610 goto done;
1611 /* Ref points at something other than a tag. */
1612 err = NULL;
1613 tag = NULL;
1614 free(id);
1615 id = NULL;
1616 continue;
1619 err = add_object_id_map_entry(idset,
1620 got_object_tag_get_object_id(tag), re);
1621 got_object_tag_close(tag);
1622 if (err)
1623 goto done;
1625 free(id);
1626 id = NULL;
1628 done:
1629 free(id);
1630 if (err) {
1631 got_reflist_object_id_map_free(*map);
1632 *map = NULL;
1634 return err;
1637 struct got_reflist_head *
1638 got_reflist_object_id_map_lookup(struct got_reflist_object_id_map *map,
1639 struct got_object_id *id)
1641 struct got_reflist_object_id_map_entry *ent;
1642 ent = got_object_idset_get(map->idset, id);
1643 if (ent)
1644 return &ent->refs;
1645 return NULL;
1648 static const struct got_error *
1649 free_id_map_entry(struct got_object_id *id, void *data, void *arg)
1651 struct got_reflist_object_id_map_entry *ent = data;
1653 got_ref_list_free(&ent->refs);
1654 free(ent);
1655 return NULL;
1658 void
1659 got_reflist_object_id_map_free(struct got_reflist_object_id_map *map)
1661 got_object_idset_for_each(map->idset, free_id_map_entry, NULL);
1662 got_object_idset_free(map->idset);
1663 free(map);