fix number of queued ids passed from findtwixt() to got_pack_paint_commits()
[got-portable.git] / lib / reference.c
blob5d1c2e817373f8450d2af8dc48ea04aeada52476
1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include "got_compat.h"
19 #include <sys/types.h>
20 #include <sys/queue.h>
21 #include <sys/stat.h>
23 #include <errno.h>
24 #include <dirent.h>
25 #include <limits.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <zlib.h>
31 #include <time.h>
32 #include <libgen.h>
34 #include "got_error.h"
35 #include "got_object.h"
36 #include "got_repository.h"
37 #include "got_reference.h"
38 #include "got_opentemp.h"
39 #include "got_path.h"
41 #include "got_lib_hash.h"
42 #include "got_lib_delta.h"
43 #include "got_lib_inflate.h"
44 #include "got_lib_object.h"
45 #include "got_lib_object_idset.h"
46 #include "got_lib_lockfile.h"
48 #ifndef nitems
49 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
50 #endif
52 #define GOT_REF_HEADS "heads"
53 #define GOT_REF_TAGS "tags"
54 #define GOT_REF_REMOTES "remotes"
57 * We do not resolve tags yet, and don't yet care about sorting refs either,
58 * so packed-refs files we write contain a minimal header which disables all
59 * packed-refs "traits" supported by Git.
61 #define GOT_PACKED_REFS_HEADER "# pack-refs with:"
63 /* A symbolic reference. */
64 struct got_symref {
65 char *name;
66 char *ref;
69 #define GOT_REF_RECURSE_MAX 20
71 /* A non-symbolic reference (there is no better designation). */
72 struct got_ref {
73 char *name;
74 struct got_object_id id;
77 /* A reference which points to an arbitrary object. */
78 struct got_reference {
79 unsigned int flags;
80 #define GOT_REF_IS_SYMBOLIC 0x01
81 #define GOT_REF_IS_PACKED 0x02
83 union {
84 struct got_ref ref;
85 struct got_symref symref;
86 } ref;
88 struct got_lockfile *lf;
89 time_t mtime;
92 * Cached timestamp for got_ref_cmp_by_commit_timestamp_descending()
93 * and got_ref_cmp_tags().
95 time_t committer_time;
98 static const struct got_error *
99 alloc_ref(struct got_reference **ref, const char *name,
100 struct got_object_id *id, int flags, time_t mtime)
102 const struct got_error *err = NULL;
104 *ref = calloc(1, sizeof(**ref));
105 if (*ref == NULL)
106 return got_error_from_errno("calloc");
108 memcpy(&(*ref)->ref.ref.id, id, sizeof((*ref)->ref.ref.id));
109 (*ref)->flags = flags;
110 (*ref)->ref.ref.name = strdup(name);
111 (*ref)->mtime = mtime;
112 if ((*ref)->ref.ref.name == NULL) {
113 err = got_error_from_errno("strdup");
114 got_ref_close(*ref);
115 *ref = NULL;
117 return err;
120 static const struct got_error *
121 alloc_symref(struct got_reference **ref, const char *name,
122 const char *target_ref, int flags)
124 const struct got_error *err = NULL;
126 *ref = calloc(1, sizeof(**ref));
127 if (*ref == NULL)
128 return got_error_from_errno("calloc");
130 (*ref)->flags = GOT_REF_IS_SYMBOLIC | flags;
131 (*ref)->ref.symref.name = strdup(name);
132 if ((*ref)->ref.symref.name == NULL) {
133 err = got_error_from_errno("strdup");
134 got_ref_close(*ref);
135 *ref = NULL;
136 return err;
138 (*ref)->ref.symref.ref = strdup(target_ref);
139 if ((*ref)->ref.symref.ref == NULL) {
140 err = got_error_from_errno("strdup");
141 got_ref_close(*ref);
142 *ref = NULL;
144 return err;
147 static const struct got_error *
148 parse_symref(struct got_reference **ref, const char *name, const char *line)
150 if (line[0] == '\0')
151 return got_error(GOT_ERR_BAD_REF_DATA);
153 return alloc_symref(ref, name, line, 0);
156 static const struct got_error *
157 parse_ref_line(struct got_reference **ref, const char *name, const char *line,
158 time_t mtime, enum got_hash_algorithm algo)
160 struct got_object_id id;
162 if (strncmp(line, "ref: ", 5) == 0) {
163 line += 5;
164 return parse_symref(ref, name, line);
167 if (!got_parse_object_id(&id, 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;
300 size_t digest_string_len;
302 *ref = NULL;
303 digest_string_len = got_hash_digest_string_length(algo);
305 if (line[0] == '#' || line[0] == '^')
306 return NULL;
308 if (!got_parse_object_id(&id, line, algo))
309 return got_error(GOT_ERR_BAD_REF_DATA);
311 if (abs_refname) {
312 if (strcmp(line + digest_string_len, abs_refname) != 0)
313 return NULL;
314 name = abs_refname;
315 } else
316 name = line + digest_string_len;
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
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));
598 return NULL;
601 const struct got_error *
602 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
603 struct got_reference *ref)
605 return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
608 char *
609 got_ref_to_str(struct got_reference *ref)
611 char *str;
613 if (ref->flags & GOT_REF_IS_SYMBOLIC)
614 return strdup(ref->ref.symref.ref);
616 if (got_object_id_str(&str, &ref->ref.ref.id) != NULL)
617 return NULL;
619 return str;
622 const char *
623 got_ref_get_name(struct got_reference *ref)
625 if (ref->flags & GOT_REF_IS_SYMBOLIC)
626 return ref->ref.symref.name;
628 return ref->ref.ref.name;
631 const char *
632 got_ref_get_symref_target(struct got_reference *ref)
634 if (ref->flags & GOT_REF_IS_SYMBOLIC)
635 return ref->ref.symref.ref;
637 return NULL;
640 time_t
641 got_ref_get_mtime(struct got_reference *ref)
643 return ref->mtime;
646 time_t
647 got_ref_get_cached_committer_time(struct got_reference *ref)
649 return ref->committer_time;
652 const struct got_error *
653 got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
654 struct got_reference* re2)
656 const char *name1 = got_ref_get_name(re1);
657 const char *name2 = got_ref_get_name(re2);
659 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
660 return NULL;
663 static const struct got_error *
664 get_committer_time(struct got_reference *ref, struct got_repository *repo)
666 const struct got_error *err = NULL;
667 int obj_type;
668 struct got_commit_object *commit = NULL;
669 struct got_tag_object *tag = NULL;
670 struct got_object_id *id = NULL;
672 err = got_ref_resolve(&id, repo, ref);
673 if (err)
674 return err;
676 err = got_object_get_type(&obj_type, repo, id);
677 if (err)
678 goto done;
680 switch (obj_type) {
681 case GOT_OBJ_TYPE_COMMIT:
682 err = got_object_open_as_commit(&commit, repo, id);
683 if (err)
684 goto done;
685 ref->committer_time =
686 got_object_commit_get_committer_time(commit);
687 break;
688 case GOT_OBJ_TYPE_TAG:
689 err = got_object_open_as_tag(&tag, repo, id);
690 if (err)
691 goto done;
692 ref->committer_time = got_object_tag_get_tagger_time(tag);
693 break;
694 default:
695 /* best effort for other object types */
696 ref->committer_time = got_ref_get_mtime(ref);
697 break;
699 done:
700 free(id);
701 if (commit)
702 got_object_commit_close(commit);
703 if (tag)
704 got_object_tag_close(tag);
705 return err;
708 const struct got_error *
709 got_ref_cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
710 struct got_reference *ref2)
712 const struct got_error *err = NULL;
713 struct got_repository *repo = arg;
715 *cmp = 0;
717 if (ref1->committer_time == 0) {
718 err = get_committer_time(ref1, repo);
719 if (err)
720 return err;
722 if (ref2->committer_time == 0) {
723 err = get_committer_time(ref2, repo);
724 if (err)
725 return err;
728 /* Put latest tags first. */
729 if (ref1->committer_time < ref2->committer_time)
730 *cmp = 1;
731 else if (ref1->committer_time > ref2->committer_time)
732 *cmp = -1;
733 else
734 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
736 return err;
739 const struct got_error *
740 got_ref_cmp_by_commit_timestamp_descending(void *arg, int *cmp,
741 struct got_reference *ref1, struct got_reference *ref2)
743 const struct got_error *err = NULL;
744 struct got_repository *repo = arg;
746 *cmp = 0;
748 if (ref1->committer_time == 0) {
749 err = get_committer_time(ref1, repo);
750 if (err)
751 return err;
753 if (ref2->committer_time == 0) {
754 err = get_committer_time(ref2, repo);
755 if (err)
756 return err;
759 if (ref1->committer_time < ref2->committer_time)
760 *cmp = 1;
761 else if (ref2->committer_time < ref1->committer_time)
762 *cmp = -1;
763 else
764 return got_ref_cmp_by_name(arg, cmp, ref1, ref2);
766 return err;
769 const struct got_error *
770 got_reflist_insert(struct got_reflist_entry **newp,
771 struct got_reflist_head *refs, struct got_reference *ref,
772 got_ref_cmp_cb cmp_cb, void *cmp_arg)
774 const struct got_error *err;
775 struct got_reflist_entry *new, *re;
776 int cmp;
778 *newp = NULL;
780 if (cmp_cb != got_ref_cmp_by_name && (ref->flags & GOT_REF_IS_PACKED)) {
782 * If we are not sorting elements by name then we must still
783 * detect collisions between a packed ref and an on-disk ref
784 * using the same name. On-disk refs take precedence and are
785 * already present on the list before packed refs get added.
787 TAILQ_FOREACH(re, refs, entry) {
788 err = got_ref_cmp_by_name(NULL, &cmp, re->ref, ref);
789 if (err)
790 return err;
791 if (cmp == 0)
792 return NULL;
796 new = malloc(sizeof(*new));
797 if (new == NULL)
798 return got_error_from_errno("malloc");
799 new->ref = ref;
800 *newp = new;
803 * We must de-duplicate entries on insert because packed-refs may
804 * contain redundant entries. On-disk refs take precedence.
805 * This code assumes that on-disk revs are read before packed-refs.
806 * We're iterating the list anyway, so insert elements sorted by name.
808 * Many callers will provide paths in a somewhat sorted order.
809 * Iterating backwards from the tail of the list should be more
810 * efficient than traversing through the entire list each time
811 * an element is inserted.
813 re = TAILQ_LAST(refs, got_reflist_head);
814 while (re) {
815 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
816 if (err)
817 return err;
818 if (cmp == 0) {
819 /* duplicate */
820 free(new);
821 *newp = NULL;
822 return NULL;
823 } else if (cmp < 0) {
824 TAILQ_INSERT_AFTER(refs, re, new, entry);
825 return NULL;
827 re = TAILQ_PREV(re, got_reflist_head, entry);
830 TAILQ_INSERT_HEAD(refs, new, entry);
831 return NULL;
834 const struct got_error *
835 got_reflist_sort(struct got_reflist_head *refs,
836 got_ref_cmp_cb cmp_cb, void *cmp_arg)
838 const struct got_error *err = NULL;
839 struct got_reflist_entry *re, *tmp, *new;
840 struct got_reflist_head sorted;
842 TAILQ_INIT(&sorted);
844 TAILQ_FOREACH_SAFE(re, refs, entry, tmp) {
845 struct got_reference *ref = re->ref;
846 TAILQ_REMOVE(refs, re, entry);
847 free(re);
848 err = got_reflist_insert(&new, &sorted, ref, cmp_cb, cmp_arg);
849 if (err || new == NULL /* duplicate */)
850 got_ref_close(ref);
851 if (err)
852 return err;
855 TAILQ_CONCAT(refs, &sorted, entry);
856 return NULL;
859 static const struct got_error *
860 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
861 const char *subdir, struct got_repository *repo,
862 got_ref_cmp_cb cmp_cb, void *cmp_arg)
864 const struct got_error *err = NULL;
865 DIR *d = NULL;
866 char *path_subdir;
867 struct got_reference *ref;
868 struct got_reflist_entry *new;
870 while (subdir[0] == '/')
871 subdir++;
873 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
874 return got_error_from_errno("asprintf");
876 d = opendir(path_subdir);
877 if (d == NULL) {
878 char *refname;
880 if (errno != ENOTDIR)
881 goto done;
883 /* This could be a regular on-disk reference file. */
884 free(path_subdir);
885 err = got_path_dirname(&path_subdir, subdir);
886 if (err)
887 return err;
888 err = got_path_basename(&refname, subdir);
889 if (err) {
890 free(path_subdir);
891 return err;
893 err = open_ref(&ref, path_refs, path_subdir, refname,
894 0, got_repo_get_object_format(repo));
895 free(path_subdir);
896 free(refname);
897 if (err) {
898 if (err->code == GOT_ERR_NOT_REF)
899 return NULL;
900 return err;
902 err = got_reflist_insert(&new, refs, ref,
903 cmp_cb, cmp_arg);
904 if (err || new == NULL /* duplicate */)
905 got_ref_close(ref);
906 return err;
909 for (;;) {
910 struct dirent *dent;
911 char *child;
912 int type;
914 dent = readdir(d);
915 if (dent == NULL)
916 break;
918 if (strcmp(dent->d_name, ".") == 0 ||
919 strcmp(dent->d_name, "..") == 0)
920 continue;
922 err = got_path_dirent_type(&type, path_subdir, dent);
923 if (err)
924 break;
926 switch (type) {
927 case DT_REG:
928 err = open_ref(&ref, path_refs, subdir, dent->d_name,
929 0, got_repo_get_object_format(repo));
930 if (err && err->code == GOT_ERR_BAD_REF_NAME)
931 break;
932 if (err)
933 goto done;
934 if (ref) {
935 err = got_reflist_insert(&new, refs, ref,
936 cmp_cb, cmp_arg);
937 if (err || new == NULL /* duplicate */)
938 got_ref_close(ref);
939 if (err)
940 goto done;
942 break;
943 case DT_DIR:
944 if (asprintf(&child, "%s%s%s", subdir,
945 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
946 err = got_error_from_errno("asprintf");
947 break;
949 err = gather_on_disk_refs(refs, path_refs, child, repo,
950 cmp_cb, cmp_arg);
951 free(child);
952 break;
953 default:
954 break;
957 done:
958 if (d)
959 closedir(d);
960 free(path_subdir);
961 return err;
964 static int
965 match_packed_ref(struct got_reference *ref, const char *ref_namespace)
967 const char *name = got_ref_get_name(ref);
968 int namespace_is_absolute = (strncmp(ref_namespace, "refs/", 5) == 0);
970 if (namespace_is_absolute) {
971 return (strcmp(name, ref_namespace) == 0 ||
972 got_path_is_child(name, ref_namespace,
973 strlen(ref_namespace)));
976 /* Match all "subdirectories" as we do with on-disk refs. */
977 while (*name != '\0') {
978 while (*name == '/')
979 name++;
980 if (strcmp(name, ref_namespace) == 0 ||
981 got_path_is_child(name, ref_namespace,
982 strlen(ref_namespace)))
983 return 1;
984 while (*name != '\0' && *name != '/')
985 name++;
988 return 0;
991 const struct got_error *
992 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
993 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
995 const struct got_error *err;
996 char *packed_refs_path = NULL, *path_refs = NULL;
997 char *abs_namespace = NULL, *buf = NULL;
998 const char *ondisk_ref_namespace = NULL;
999 char *line = NULL;
1000 FILE *f = NULL;
1001 struct got_reference *ref;
1002 struct got_reflist_entry *new;
1004 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
1005 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
1006 if (path_refs == NULL) {
1007 err = got_error_from_errno("get_refs_dir_path");
1008 goto done;
1010 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0,
1011 got_repo_get_object_format(repo));
1012 if (err)
1013 goto done;
1014 err = got_reflist_insert(&new, refs, ref, cmp_cb, cmp_arg);
1015 if (err || new == NULL /* duplicate */)
1016 got_ref_close(ref);
1017 if (err && err->code != GOT_ERR_NOT_REF)
1018 goto done;
1019 } else {
1020 /* Try listing a single reference. */
1021 err = got_ref_open(&ref, repo, ref_namespace, 0);
1022 if (err) {
1023 if (err->code != GOT_ERR_NOT_REF)
1024 goto done;
1025 /* Try to look up references in a given namespace. */
1026 } else {
1027 err = got_reflist_insert(&new, refs, ref,
1028 cmp_cb, cmp_arg);
1029 if (err || new == NULL /* duplicate */)
1030 got_ref_close(ref);
1031 return err;
1035 if (ref_namespace) {
1036 size_t len;
1037 /* Canonicalize the path to eliminate double-slashes if any. */
1038 if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
1039 err = got_error_from_errno("asprintf");
1040 goto done;
1042 len = strlen(abs_namespace) + 1;
1043 buf = malloc(len);
1044 if (buf == NULL) {
1045 err = got_error_from_errno("malloc");
1046 goto done;
1048 err = got_canonpath(abs_namespace, buf, len);
1049 if (err)
1050 goto done;
1051 ondisk_ref_namespace = buf;
1052 while (ondisk_ref_namespace[0] == '/')
1053 ondisk_ref_namespace++;
1054 if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
1055 ondisk_ref_namespace += 5;
1056 else if (strcmp(ondisk_ref_namespace, "refs") == 0)
1057 ondisk_ref_namespace = "";
1060 /* Gather on-disk refs before parsing packed-refs. */
1061 free(path_refs);
1062 path_refs = get_refs_dir_path(repo, "");
1063 if (path_refs == NULL) {
1064 err = got_error_from_errno("get_refs_dir_path");
1065 goto done;
1067 err = gather_on_disk_refs(refs, path_refs,
1068 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
1069 cmp_cb, cmp_arg);
1070 if (err)
1071 goto done;
1074 * The packed-refs file may contain redundant entries, in which
1075 * case on-disk refs take precedence.
1077 packed_refs_path = got_repo_get_path_packed_refs(repo);
1078 if (packed_refs_path == NULL) {
1079 err = got_error_from_errno("got_repo_get_path_packed_refs");
1080 goto done;
1083 f = fopen(packed_refs_path, "re");
1084 if (f) {
1085 size_t linesize = 0;
1086 ssize_t linelen;
1087 struct stat sb;
1089 if (fstat(fileno(f), &sb) == -1) {
1090 err = got_error_from_errno2("fstat", packed_refs_path);
1091 goto done;
1093 for (;;) {
1094 linelen = getline(&line, &linesize, f);
1095 if (linelen == -1) {
1096 if (feof(f))
1097 break;
1098 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1099 goto done;
1101 if (linelen > 0 && line[linelen - 1] == '\n')
1102 line[linelen - 1] = '\0';
1103 err = parse_packed_ref_line(&ref, NULL, line,
1104 sb.st_mtime, got_repo_get_object_format(repo));
1105 if (err)
1106 goto done;
1107 if (ref) {
1108 if (ref_namespace &&
1109 !match_packed_ref(ref, ref_namespace)) {
1110 got_ref_close(ref);
1111 continue;
1113 err = got_reflist_insert(&new, refs, ref,
1114 cmp_cb, cmp_arg);
1115 if (err || new == NULL /* duplicate */)
1116 got_ref_close(ref);
1117 if (err)
1118 goto done;
1122 done:
1123 free(packed_refs_path);
1124 free(abs_namespace);
1125 free(buf);
1126 free(line);
1127 free(path_refs);
1128 if (f && fclose(f) == EOF && err == NULL)
1129 err = got_error_from_errno("fclose");
1130 return err;
1133 void
1134 got_ref_list_free(struct got_reflist_head *refs)
1136 struct got_reflist_entry *re;
1138 while ((re = TAILQ_FIRST(refs))) {
1139 TAILQ_REMOVE(refs, re, entry);
1140 got_ref_close(re->ref);
1141 free(re);
1146 got_ref_is_symbolic(struct got_reference *ref)
1148 return (ref->flags & GOT_REF_IS_SYMBOLIC);
1151 const struct got_error *
1152 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1154 if (ref->flags & GOT_REF_IS_SYMBOLIC)
1155 return got_error(GOT_ERR_BAD_REF_TYPE);
1157 memcpy(&ref->ref.ref.id, id, sizeof(ref->ref.ref.id));
1158 return NULL;
1161 const struct got_error *
1162 got_ref_change_symref(struct got_reference *ref, const char *refname)
1164 char *new_name;
1166 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1167 return got_error(GOT_ERR_BAD_REF_TYPE);
1169 new_name = strdup(refname);
1170 if (new_name == NULL)
1171 return got_error_from_errno("strdup");
1173 free(ref->ref.symref.ref);
1174 ref->ref.symref.ref = new_name;
1175 return NULL;
1178 const struct got_error *
1179 got_ref_change_symref_to_ref(struct got_reference *symref,
1180 struct got_object_id *id)
1182 if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1183 return got_error(GOT_ERR_BAD_REF_TYPE);
1185 symref->ref.ref.name = symref->ref.symref.name;
1186 memcpy(&symref->ref.ref.id, id, sizeof(symref->ref.ref.id));
1187 symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1188 return NULL;
1191 const struct got_error *
1192 got_ref_write(struct got_reference *ref, struct got_repository *repo)
1194 const struct got_error *err = NULL, *unlock_err = NULL;
1195 const char *name = got_ref_get_name(ref);
1196 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1197 struct got_lockfile *lf = NULL;
1198 FILE *f = NULL;
1199 size_t n;
1200 struct stat sb;
1202 path_refs = get_refs_dir_path(repo, name);
1203 if (path_refs == NULL) {
1204 err = got_error_from_errno2("get_refs_dir_path", name);
1205 goto done;
1208 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1209 err = got_error_from_errno("asprintf");
1210 goto done;
1213 err = got_opentemp_named(&tmppath, &f, path, "");
1214 if (err) {
1215 char *parent;
1216 if (err->code == GOT_ERR_ERRNO && errno == ENOTDIR) {
1217 err = got_error_fmt(GOT_ERR_BAD_REF_NAME,
1218 "collision with an existing reference: %s",
1219 got_ref_get_name(ref));
1220 goto done;
1222 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1223 goto done;
1224 err = got_path_dirname(&parent, path);
1225 if (err)
1226 goto done;
1227 err = got_path_mkdir(parent);
1228 free(parent);
1229 if (err)
1230 goto done;
1231 err = got_opentemp_named(&tmppath, &f, path, "");
1232 if (err)
1233 goto done;
1236 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1237 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1238 if (n != strlen(ref->ref.symref.ref) + 6) {
1239 err = got_ferror(f, GOT_ERR_IO);
1240 goto done;
1242 } else {
1243 char *hex;
1244 size_t len;
1246 err = got_object_id_str(&hex, &ref->ref.ref.id);
1247 if (err)
1248 goto done;
1249 len = strlen(hex);
1250 n = fprintf(f, "%s\n", hex);
1251 free(hex);
1252 if (n != len + 1) {
1253 err = got_ferror(f, GOT_ERR_IO);
1254 goto done;
1258 if (ref->lf == NULL) {
1259 err = got_lockfile_lock(&lf, path, -1);
1260 if (err)
1261 goto done;
1264 /* XXX: check if old content matches our expectations? */
1266 if (stat(path, &sb) != 0) {
1267 if (errno != ENOENT) {
1268 err = got_error_from_errno2("stat", path);
1269 goto done;
1271 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1274 if (fchmod(fileno(f), sb.st_mode) != 0) {
1275 err = got_error_from_errno2("fchmod", tmppath);
1276 goto done;
1279 if (rename(tmppath, path) != 0) {
1280 err = got_error_from_errno3("rename", tmppath, path);
1281 goto done;
1283 free(tmppath);
1284 tmppath = NULL;
1286 if (stat(path, &sb) == -1) {
1287 err = got_error_from_errno2("stat", path);
1288 goto done;
1290 ref->mtime = sb.st_mtime;
1291 done:
1292 if (ref->lf == NULL && lf)
1293 unlock_err = got_lockfile_unlock(lf, -1);
1294 if (f) {
1295 if (fclose(f) == EOF && err == NULL)
1296 err = got_error_from_errno("fclose");
1298 free(path_refs);
1299 free(path);
1300 if (tmppath) {
1301 if (unlink(tmppath) == -1 && err == NULL)
1302 err = got_error_from_errno2("unlink", tmppath);
1303 free(tmppath);
1305 return err ? err : unlock_err;
1308 static const struct got_error *
1309 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1311 const struct got_error *err = NULL, *unlock_err = NULL;
1312 struct got_lockfile *lf = NULL;
1313 FILE *f = NULL, *tmpf = NULL;
1314 char *line = NULL, *packed_refs_path, *tmppath = NULL;
1315 size_t linesize = 0;
1316 struct got_reflist_head refs;
1317 int found_delref = 0;
1319 /* The packed-refs file does not contain symbolic references. */
1320 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1321 return got_error(GOT_ERR_BAD_REF_DATA);
1323 TAILQ_INIT(&refs);
1325 packed_refs_path = got_repo_get_path_packed_refs(repo);
1326 if (packed_refs_path == NULL)
1327 return got_error_from_errno("got_repo_get_path_packed_refs");
1329 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path, "");
1330 if (err)
1331 goto done;
1333 if (delref->lf == NULL) {
1334 err = got_lockfile_lock(&lf, packed_refs_path, -1);
1335 if (err)
1336 goto done;
1339 f = fopen(packed_refs_path, "re");
1340 if (f == NULL) {
1341 err = got_error_from_errno2("fopen", packed_refs_path);
1342 goto done;
1344 for (;;) {
1345 ssize_t linelen;
1346 struct got_reference *ref;
1347 struct got_reflist_entry *new;
1349 linelen = getline(&line, &linesize, f);
1350 if (linelen == -1) {
1351 if (feof(f))
1352 break;
1353 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1354 goto done;
1356 if (linelen > 0 && line[linelen - 1] == '\n')
1357 line[linelen - 1] = '\0';
1358 err = parse_packed_ref_line(&ref, NULL, line, 0,
1359 got_repo_get_object_format(repo));
1360 if (err)
1361 goto done;
1362 if (ref == NULL)
1363 continue;
1365 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1366 got_object_id_cmp(&ref->ref.ref.id,
1367 &delref->ref.ref.id) == 0) {
1368 found_delref = 1;
1369 got_ref_close(ref);
1370 continue;
1373 err = got_reflist_insert(&new, &refs, ref,
1374 got_ref_cmp_by_name, NULL);
1375 if (err || new == NULL /* duplicate */)
1376 got_ref_close(ref);
1377 if (err)
1378 goto done;
1381 if (found_delref) {
1382 struct got_reflist_entry *re;
1383 size_t n;
1384 struct stat sb;
1386 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1387 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1388 err = got_ferror(f, GOT_ERR_IO);
1389 goto done;
1392 TAILQ_FOREACH(re, &refs, entry) {
1393 char *hex;
1394 size_t len;
1396 err = got_object_id_str(&hex, &re->ref->ref.ref.id);
1397 if (err)
1398 goto done;
1399 len = strlen(hex);
1400 n = fprintf(tmpf, "%s ", hex);
1401 free(hex);
1402 if (n != len + 1) {
1403 err = got_ferror(f, GOT_ERR_IO);
1404 goto done;
1407 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1408 if (n != strlen(re->ref->ref.ref.name) + 1) {
1409 err = got_ferror(f, GOT_ERR_IO);
1410 goto done;
1414 if (fflush(tmpf) != 0) {
1415 err = got_error_from_errno("fflush");
1416 goto done;
1419 if (fstat(fileno(f), &sb) != 0) {
1420 if (errno != ENOENT) {
1421 err = got_error_from_errno2("fstat",
1422 packed_refs_path);
1423 goto done;
1425 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1428 if (fchmod(fileno(tmpf), sb.st_mode) != 0) {
1429 err = got_error_from_errno2("fchmod", tmppath);
1430 goto done;
1433 if (rename(tmppath, packed_refs_path) != 0) {
1434 err = got_error_from_errno3("rename", tmppath,
1435 packed_refs_path);
1436 goto done;
1438 free(tmppath);
1439 tmppath = NULL;
1441 done:
1442 if (delref->lf == NULL && lf)
1443 unlock_err = got_lockfile_unlock(lf, -1);
1444 if (f) {
1445 if (fclose(f) == EOF && err == NULL)
1446 err = got_error_from_errno("fclose");
1448 if (tmppath && unlink(tmppath) == -1 && err == NULL)
1449 err = got_error_from_errno2("unlink", tmppath);
1450 if (tmpf && fclose(tmpf) == EOF && err == NULL)
1451 err = got_error_from_errno("fclose");
1452 free(tmppath);
1453 free(packed_refs_path);
1454 free(line);
1455 got_ref_list_free(&refs);
1456 return err ? err : unlock_err;
1459 static const struct got_error *
1460 delete_loose_ref(struct got_reference *ref, struct got_repository *repo)
1462 const struct got_error *err = NULL, *unlock_err = NULL;
1463 const char *name = got_ref_get_name(ref);
1464 char *path_refs = NULL, *path = NULL;
1465 struct got_lockfile *lf = NULL;
1467 path_refs = get_refs_dir_path(repo, name);
1468 if (path_refs == NULL) {
1469 err = got_error_from_errno2("get_refs_dir_path", name);
1470 goto done;
1473 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1474 err = got_error_from_errno("asprintf");
1475 goto done;
1478 if (ref->lf == NULL) {
1479 err = got_lockfile_lock(&lf, path, -1);
1480 if (err)
1481 goto done;
1484 /* XXX: check if old content matches our expectations? */
1486 if (unlink(path) == -1)
1487 err = got_error_from_errno2("unlink", path);
1488 done:
1489 if (ref->lf == NULL && lf)
1490 unlock_err = got_lockfile_unlock(lf, -1);
1492 free(path_refs);
1493 free(path);
1494 return err ? err : unlock_err;
1497 const struct got_error *
1498 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1500 const struct got_error *err = NULL;
1501 struct got_reference *ref2;
1503 if (ref->flags & GOT_REF_IS_PACKED) {
1504 err = delete_packed_ref(ref, repo);
1505 if (err)
1506 return err;
1508 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1509 if (err) {
1510 if (err->code == GOT_ERR_NOT_REF)
1511 return NULL;
1512 return err;
1515 err = delete_loose_ref(ref2, repo);
1516 got_ref_close(ref2);
1517 return err;
1518 } else {
1519 err = delete_loose_ref(ref, repo);
1520 if (err)
1521 return err;
1523 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1524 if (err) {
1525 if (err->code == GOT_ERR_NOT_REF)
1526 return NULL;
1527 return err;
1530 err = delete_packed_ref(ref2, repo);
1531 got_ref_close(ref2);
1532 return err;
1536 const struct got_error *
1537 got_ref_unlock(struct got_reference *ref)
1539 const struct got_error *err;
1540 err = got_lockfile_unlock(ref->lf, -1);
1541 ref->lf = NULL;
1542 return err;
1545 struct got_reflist_object_id_map {
1546 struct got_object_idset *idset;
1549 struct got_reflist_object_id_map_entry {
1550 struct got_reflist_head refs;
1553 static const struct got_error *
1554 add_object_id_map_entry(struct got_object_idset *idset,
1555 struct got_object_id *id, struct got_reflist_entry *re)
1557 const struct got_error *err = NULL;
1558 struct got_reflist_object_id_map_entry *ent;
1559 struct got_reflist_entry *new;
1561 ent = got_object_idset_get(idset, id);
1562 if (ent == NULL) {
1563 ent = malloc(sizeof(*ent));
1564 if (ent == NULL)
1565 return got_error_from_errno("malloc");
1567 TAILQ_INIT(&ent->refs);
1568 err = got_object_idset_add(idset, id, ent);
1569 if (err) {
1570 free(ent);
1571 return err;
1575 err = got_reflist_entry_dup(&new, re);
1576 if (err)
1577 return err;
1579 TAILQ_INSERT_TAIL(&ent->refs, new, entry);
1580 return NULL;
1583 const struct got_error *
1584 got_reflist_object_id_map_create(struct got_reflist_object_id_map **map,
1585 struct got_reflist_head *refs, struct got_repository *repo)
1587 const struct got_error *err = NULL;
1588 struct got_object_idset *idset;
1589 struct got_object_id *id = NULL;
1590 struct got_reflist_entry *re;
1592 idset = got_object_idset_alloc();
1593 if (idset == NULL)
1594 return got_error_from_errno("got_object_idset_alloc");
1596 *map = malloc(sizeof(**map));
1597 if (*map == NULL) {
1598 got_object_idset_free(idset);
1599 return got_error_from_errno("malloc");
1601 (*map)->idset = idset;
1603 TAILQ_FOREACH(re, refs, entry) {
1604 struct got_tag_object *tag = NULL;
1606 err = got_ref_resolve(&id, repo, re->ref);
1607 if (err)
1608 goto done;
1610 err = add_object_id_map_entry(idset, id, re);
1611 if (err)
1612 goto done;
1614 if (strstr(got_ref_get_name(re->ref), "/tags/") == NULL) {
1615 free(id);
1616 id = NULL;
1617 continue;
1620 err = got_object_open_as_tag(&tag, repo, id);
1621 if (err) {
1622 if (err->code != GOT_ERR_OBJ_TYPE)
1623 goto done;
1624 /* Ref points at something other than a tag. */
1625 err = NULL;
1626 tag = NULL;
1627 free(id);
1628 id = NULL;
1629 continue;
1632 err = add_object_id_map_entry(idset,
1633 got_object_tag_get_object_id(tag), re);
1634 got_object_tag_close(tag);
1635 if (err)
1636 goto done;
1638 free(id);
1639 id = NULL;
1641 done:
1642 free(id);
1643 if (err) {
1644 got_reflist_object_id_map_free(*map);
1645 *map = NULL;
1647 return err;
1650 struct got_reflist_head *
1651 got_reflist_object_id_map_lookup(struct got_reflist_object_id_map *map,
1652 struct got_object_id *id)
1654 struct got_reflist_object_id_map_entry *ent;
1655 ent = got_object_idset_get(map->idset, id);
1656 if (ent)
1657 return &ent->refs;
1658 return NULL;
1661 static const struct got_error *
1662 free_id_map_entry(struct got_object_id *id, void *data, void *arg)
1664 struct got_reflist_object_id_map_entry *ent = data;
1666 got_ref_list_free(&ent->refs);
1667 free(ent);
1668 return NULL;
1671 void
1672 got_reflist_object_id_map_free(struct got_reflist_object_id_map *map)
1674 got_object_idset_for_each(map->idset, free_id_map_entry, NULL);
1675 got_object_idset_free(map->idset);
1676 free(map);