make got-read-object clear its imsgbuf before exit in an error case
[got-portable.git] / lib / reference.c
blob94a14d77b8962a1b29ada3bf495d6d0ae39fe5a2
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/tree.h>
22 #include <sys/stat.h>
24 #include <errno.h>
25 #include <dirent.h>
26 #include <limits.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.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"
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.
62 #define GOT_PACKED_REFS_HEADER "# pack-refs with:"
64 /* A symbolic reference. */
65 struct got_symref {
66 char *name;
67 char *ref;
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;
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;
93 * Cached timestamp for got_ref_cmp_by_commit_timestamp_descending()
94 * and got_ref_cmp_tags().
96 time_t committer_time;
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;
301 size_t digest_string_len;
303 *ref = NULL;
304 digest_string_len = got_hash_digest_string_length(algo);
306 if (line[0] == '#' || line[0] == '^')
307 return NULL;
309 if (!got_parse_object_id(&id, line, algo))
310 return got_error(GOT_ERR_BAD_REF_DATA);
312 if (abs_refname) {
313 if (strcmp(line + digest_string_len, abs_refname) != 0)
314 return NULL;
315 name = abs_refname;
316 } else
317 name = line + digest_string_len;
319 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED, mtime);
322 static const struct got_error *
323 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
324 int nsubdirs, const char *refname, time_t mtime,
325 enum got_hash_algorithm algo)
327 const struct got_error *err = NULL;
328 char *abs_refname;
329 char *line = NULL;
330 size_t linesize = 0;
331 ssize_t linelen;
332 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
334 *ref = NULL;
336 if (ref_is_absolute)
337 abs_refname = (char *)refname;
338 do {
339 linelen = getline(&line, &linesize, f);
340 if (linelen == -1) {
341 if (feof(f))
342 break;
343 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
344 break;
346 if (linelen > 0 && line[linelen - 1] == '\n')
347 line[linelen - 1] = '\0';
348 for (i = 0; i < nsubdirs; i++) {
349 if (!ref_is_absolute &&
350 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
351 refname) == -1)
352 return got_error_from_errno("asprintf");
353 err = parse_packed_ref_line(ref, abs_refname, line,
354 mtime, algo);
355 if (!ref_is_absolute)
356 free(abs_refname);
357 if (err || *ref != NULL)
358 break;
360 if (err)
361 break;
362 } while (*ref == NULL);
363 free(line);
365 return err;
368 static const struct got_error *
369 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
370 const char *name, int lock, enum got_hash_algorithm algo)
372 const struct got_error *err = NULL;
373 char *path = NULL;
374 char *absname = NULL;
375 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
376 int ref_is_well_known = (subdir[0] == '\0' && is_well_known_ref(name));
378 *ref = NULL;
380 if (!got_ref_name_is_valid(name))
381 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
383 if (ref_is_absolute || ref_is_well_known) {
384 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
385 return got_error_from_errno("asprintf");
386 absname = (char *)name;
387 } else {
388 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
389 subdir[0] ? "/" : "", name) == -1)
390 return got_error_from_errno("asprintf");
392 if (asprintf(&absname, "refs/%s%s%s",
393 subdir, subdir[0] ? "/" : "", name) == -1) {
394 err = got_error_from_errno("asprintf");
395 goto done;
399 err = parse_ref_file(ref, name, absname, path, lock, algo);
400 done:
401 if (!ref_is_absolute && !ref_is_well_known)
402 free(absname);
403 free(path);
404 return err;
407 const struct got_error *
408 got_ref_open(struct got_reference **ref, struct got_repository *repo,
409 const char *refname, int lock)
411 const struct got_error *err = NULL;
412 char *packed_refs_path = NULL, *path_refs = NULL;
413 const char *subdirs[] = {
414 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
416 size_t i;
417 int well_known = is_well_known_ref(refname);
418 struct got_lockfile *lf = NULL;
420 *ref = NULL;
422 path_refs = get_refs_dir_path(repo, refname);
423 if (path_refs == NULL) {
424 err = got_error_from_errno2("get_refs_dir_path", refname);
425 goto done;
428 if (well_known) {
429 err = open_ref(ref, path_refs, "", refname, lock,
430 got_repo_get_object_format(repo));
431 } else {
432 FILE *f;
434 /* Search on-disk refs before packed refs! */
435 for (i = 0; i < nitems(subdirs); i++) {
436 err = open_ref(ref, path_refs, subdirs[i], refname,
437 lock, got_repo_get_object_format(repo));
438 if ((err && err->code != GOT_ERR_NOT_REF) || *ref)
439 goto done;
442 packed_refs_path = got_repo_get_path_packed_refs(repo);
443 if (packed_refs_path == NULL) {
444 err = got_error_from_errno(
445 "got_repo_get_path_packed_refs");
446 goto done;
449 if (lock) {
450 err = got_lockfile_lock(&lf, packed_refs_path, -1);
451 if (err)
452 goto done;
454 f = fopen(packed_refs_path, "rbe");
455 if (f != NULL) {
456 struct stat sb;
457 if (fstat(fileno(f), &sb) == -1) {
458 err = got_error_from_errno2("fstat",
459 packed_refs_path);
460 goto done;
462 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
463 refname, sb.st_mtime,
464 got_repo_get_object_format(repo));
465 if (!err) {
466 if (fclose(f) == EOF) {
467 err = got_error_from_errno("fclose");
468 got_ref_close(*ref);
469 *ref = NULL;
470 } else if (*ref)
471 (*ref)->lf = lf;
475 done:
476 if (!err && *ref == NULL)
477 err = got_error_not_ref(refname);
478 if (err && lf)
479 got_lockfile_unlock(lf, -1);
480 free(packed_refs_path);
481 free(path_refs);
482 return err;
485 void
486 got_ref_close(struct got_reference *ref)
488 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
489 free(ref->ref.symref.name);
490 free(ref->ref.symref.ref);
491 } else
492 free(ref->ref.ref.name);
493 free(ref);
496 struct got_reference *
497 got_ref_dup(struct got_reference *ref)
499 struct got_reference *ret;
501 ret = calloc(1, sizeof(*ret));
502 if (ret == NULL)
503 return NULL;
505 ret->flags = ref->flags;
506 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
507 ret->ref.symref.name = strdup(ref->ref.symref.name);
508 if (ret->ref.symref.name == NULL) {
509 free(ret);
510 return NULL;
512 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
513 if (ret->ref.symref.ref == NULL) {
514 free(ret->ref.symref.name);
515 free(ret);
516 return NULL;
518 } else {
519 ret->ref.ref.name = strdup(ref->ref.ref.name);
520 if (ret->ref.ref.name == NULL) {
521 free(ret);
522 return NULL;
524 memcpy(&ret->ref.ref.id, &ref->ref.ref.id,
525 sizeof(ret->ref.ref.id));
528 return ret;
531 const struct got_error *
532 got_reflist_entry_dup(struct got_reflist_entry **newp,
533 struct got_reflist_entry *re)
535 const struct got_error *err = NULL;
536 struct got_reflist_entry *new;
538 *newp = NULL;
540 new = malloc(sizeof(*new));
541 if (new == NULL)
542 return got_error_from_errno("malloc");
544 new->ref = got_ref_dup(re->ref);
545 if (new->ref == NULL) {
546 err = got_error_from_errno("got_ref_dup");
547 free(new);
548 return err;
551 *newp = new;
552 return NULL;
555 const struct got_error *
556 got_ref_resolve_symbolic(struct got_reference **resolved,
557 struct got_repository *repo, struct got_reference *ref)
559 struct got_reference *nextref;
560 const struct got_error *err;
562 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
563 if (err)
564 return err;
566 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
567 err = got_ref_resolve_symbolic(resolved, repo, nextref);
568 else
569 *resolved = got_ref_dup(nextref);
571 got_ref_close(nextref);
572 return err;
575 static const struct got_error *
576 ref_resolve(struct got_object_id **id, struct got_repository *repo,
577 struct got_reference *ref, int recursion)
579 const struct got_error *err;
581 if (recursion <= 0)
582 return got_error_msg(GOT_ERR_RECURSION,
583 "reference recursion limit reached");
585 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
586 struct got_reference *resolved = NULL;
587 err = got_ref_resolve_symbolic(&resolved, repo, ref);
588 if (err == NULL)
589 err = ref_resolve(id, repo, resolved, --recursion);
590 if (resolved)
591 got_ref_close(resolved);
592 return err;
595 *id = calloc(1, sizeof(**id));
596 if (*id == NULL)
597 return got_error_from_errno("calloc");
598 memcpy(*id, &ref->ref.ref.id, sizeof(**id));
599 return NULL;
602 const struct got_error *
603 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
604 struct got_reference *ref)
606 return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
609 char *
610 got_ref_to_str(struct got_reference *ref)
612 char *str;
614 if (ref->flags & GOT_REF_IS_SYMBOLIC)
615 return strdup(ref->ref.symref.ref);
617 if (got_object_id_str(&str, &ref->ref.ref.id) != NULL)
618 return NULL;
620 return str;
623 const char *
624 got_ref_get_name(struct got_reference *ref)
626 if (ref->flags & GOT_REF_IS_SYMBOLIC)
627 return ref->ref.symref.name;
629 return ref->ref.ref.name;
632 const char *
633 got_ref_get_symref_target(struct got_reference *ref)
635 if (ref->flags & GOT_REF_IS_SYMBOLIC)
636 return ref->ref.symref.ref;
638 return NULL;
641 time_t
642 got_ref_get_mtime(struct got_reference *ref)
644 return ref->mtime;
647 time_t
648 got_ref_get_cached_committer_time(struct got_reference *ref)
650 return ref->committer_time;
653 const struct got_error *
654 got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
655 struct got_reference* re2)
657 const char *name1 = got_ref_get_name(re1);
658 const char *name2 = got_ref_get_name(re2);
660 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
661 return NULL;
664 static const struct got_error *
665 get_committer_time(struct got_reference *ref, struct got_repository *repo)
667 const struct got_error *err = NULL;
668 int obj_type;
669 struct got_commit_object *commit = NULL;
670 struct got_tag_object *tag = NULL;
671 struct got_object_id *id = NULL;
673 err = got_ref_resolve(&id, repo, ref);
674 if (err)
675 return err;
677 err = got_object_get_type(&obj_type, repo, id);
678 if (err)
679 goto done;
681 switch (obj_type) {
682 case GOT_OBJ_TYPE_COMMIT:
683 err = got_object_open_as_commit(&commit, repo, id);
684 if (err)
685 goto done;
686 ref->committer_time =
687 got_object_commit_get_committer_time(commit);
688 break;
689 case GOT_OBJ_TYPE_TAG:
690 err = got_object_open_as_tag(&tag, repo, id);
691 if (err)
692 goto done;
693 ref->committer_time = got_object_tag_get_tagger_time(tag);
694 break;
695 default:
696 /* best effort for other object types */
697 ref->committer_time = got_ref_get_mtime(ref);
698 break;
700 done:
701 free(id);
702 if (commit)
703 got_object_commit_close(commit);
704 if (tag)
705 got_object_tag_close(tag);
706 return err;
709 const struct got_error *
710 got_ref_cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
711 struct got_reference *ref2)
713 const struct got_error *err = NULL;
714 struct got_repository *repo = arg;
716 *cmp = 0;
718 if (ref1->committer_time == 0) {
719 err = get_committer_time(ref1, repo);
720 if (err)
721 return err;
723 if (ref2->committer_time == 0) {
724 err = get_committer_time(ref2, repo);
725 if (err)
726 return err;
729 /* Put latest tags first. */
730 if (ref1->committer_time < ref2->committer_time)
731 *cmp = 1;
732 else if (ref1->committer_time > ref2->committer_time)
733 *cmp = -1;
734 else
735 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
737 return err;
740 const struct got_error *
741 got_ref_cmp_by_commit_timestamp_descending(void *arg, int *cmp,
742 struct got_reference *ref1, struct got_reference *ref2)
744 const struct got_error *err = NULL;
745 struct got_repository *repo = arg;
747 *cmp = 0;
749 if (ref1->committer_time == 0) {
750 err = get_committer_time(ref1, repo);
751 if (err)
752 return err;
754 if (ref2->committer_time == 0) {
755 err = get_committer_time(ref2, repo);
756 if (err)
757 return err;
760 if (ref1->committer_time < ref2->committer_time)
761 *cmp = 1;
762 else if (ref2->committer_time < ref1->committer_time)
763 *cmp = -1;
764 else
765 return got_ref_cmp_by_name(arg, cmp, ref1, ref2);
767 return err;
770 const struct got_error *
771 got_reflist_insert(struct got_reflist_entry **newp,
772 struct got_reflist_head *refs, struct got_reference *ref,
773 got_ref_cmp_cb cmp_cb, void *cmp_arg)
775 const struct got_error *err;
776 struct got_reflist_entry *new, *re;
777 int cmp;
779 *newp = NULL;
781 if (cmp_cb != got_ref_cmp_by_name && (ref->flags & GOT_REF_IS_PACKED)) {
783 * If we are not sorting elements by name then we must still
784 * detect collisions between a packed ref and an on-disk ref
785 * using the same name. On-disk refs take precedence and are
786 * already present on the list before packed refs get added.
788 TAILQ_FOREACH(re, refs, entry) {
789 err = got_ref_cmp_by_name(NULL, &cmp, re->ref, ref);
790 if (err)
791 return err;
792 if (cmp == 0)
793 return NULL;
797 new = malloc(sizeof(*new));
798 if (new == NULL)
799 return got_error_from_errno("malloc");
800 new->ref = ref;
801 *newp = new;
804 * We must de-duplicate entries on insert because packed-refs may
805 * contain redundant entries. On-disk refs take precedence.
806 * This code assumes that on-disk revs are read before packed-refs.
807 * We're iterating the list anyway, so insert elements sorted by name.
809 * Many callers will provide paths in a somewhat sorted order.
810 * Iterating backwards from the tail of the list should be more
811 * efficient than traversing through the entire list each time
812 * an element is inserted.
814 re = TAILQ_LAST(refs, got_reflist_head);
815 while (re) {
816 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
817 if (err)
818 return err;
819 if (cmp == 0) {
820 /* duplicate */
821 free(new);
822 *newp = NULL;
823 return NULL;
824 } else if (cmp < 0) {
825 TAILQ_INSERT_AFTER(refs, re, new, entry);
826 return NULL;
828 re = TAILQ_PREV(re, got_reflist_head, entry);
831 TAILQ_INSERT_HEAD(refs, new, entry);
832 return NULL;
835 const struct got_error *
836 got_reflist_sort(struct got_reflist_head *refs,
837 got_ref_cmp_cb cmp_cb, void *cmp_arg)
839 const struct got_error *err = NULL;
840 struct got_reflist_entry *re, *tmp, *new;
841 struct got_reflist_head sorted;
843 TAILQ_INIT(&sorted);
845 TAILQ_FOREACH_SAFE(re, refs, entry, tmp) {
846 struct got_reference *ref = re->ref;
847 TAILQ_REMOVE(refs, re, entry);
848 free(re);
849 err = got_reflist_insert(&new, &sorted, ref, cmp_cb, cmp_arg);
850 if (err || new == NULL /* duplicate */)
851 got_ref_close(ref);
852 if (err)
853 return err;
856 TAILQ_CONCAT(refs, &sorted, entry);
857 return NULL;
860 static const struct got_error *
861 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
862 const char *subdir, struct got_repository *repo,
863 got_ref_cmp_cb cmp_cb, void *cmp_arg)
865 const struct got_error *err = NULL;
866 DIR *d = NULL;
867 char *path_subdir;
868 struct got_reference *ref;
869 struct got_reflist_entry *new;
871 while (subdir[0] == '/')
872 subdir++;
874 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
875 return got_error_from_errno("asprintf");
877 d = opendir(path_subdir);
878 if (d == NULL) {
879 char *refname;
881 if (errno != ENOTDIR)
882 goto done;
884 /* This could be a regular on-disk reference file. */
885 free(path_subdir);
886 err = got_path_dirname(&path_subdir, subdir);
887 if (err)
888 return err;
889 err = got_path_basename(&refname, subdir);
890 if (err) {
891 free(path_subdir);
892 return err;
894 err = open_ref(&ref, path_refs, path_subdir, refname,
895 0, got_repo_get_object_format(repo));
896 free(path_subdir);
897 free(refname);
898 if (err) {
899 if (err->code == GOT_ERR_NOT_REF)
900 return NULL;
901 return err;
903 err = got_reflist_insert(&new, refs, ref,
904 cmp_cb, cmp_arg);
905 if (err || new == NULL /* duplicate */)
906 got_ref_close(ref);
907 return err;
910 for (;;) {
911 struct dirent *dent;
912 char *child;
913 int type;
915 dent = readdir(d);
916 if (dent == NULL)
917 break;
919 if (strcmp(dent->d_name, ".") == 0 ||
920 strcmp(dent->d_name, "..") == 0)
921 continue;
923 err = got_path_dirent_type(&type, path_subdir, dent);
924 if (err)
925 break;
927 switch (type) {
928 case DT_REG:
929 err = open_ref(&ref, path_refs, subdir, dent->d_name,
930 0, got_repo_get_object_format(repo));
931 if (err && err->code == GOT_ERR_BAD_REF_NAME)
932 break;
933 if (err)
934 goto done;
935 if (ref) {
936 err = got_reflist_insert(&new, refs, ref,
937 cmp_cb, cmp_arg);
938 if (err || new == NULL /* duplicate */)
939 got_ref_close(ref);
940 if (err)
941 goto done;
943 break;
944 case DT_DIR:
945 if (asprintf(&child, "%s%s%s", subdir,
946 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
947 err = got_error_from_errno("asprintf");
948 break;
950 err = gather_on_disk_refs(refs, path_refs, child, repo,
951 cmp_cb, cmp_arg);
952 free(child);
953 break;
954 default:
955 break;
958 done:
959 if (d)
960 closedir(d);
961 free(path_subdir);
962 return err;
965 static int
966 match_packed_ref(struct got_reference *ref, const char *ref_namespace)
968 const char *name = got_ref_get_name(ref);
969 int namespace_is_absolute = (strncmp(ref_namespace, "refs/", 5) == 0);
971 if (namespace_is_absolute) {
972 return (strcmp(name, ref_namespace) == 0 ||
973 got_path_is_child(name, ref_namespace,
974 strlen(ref_namespace)));
977 /* Match all "subdirectories" as we do with on-disk refs. */
978 while (*name != '\0') {
979 while (*name == '/')
980 name++;
981 if (strcmp(name, ref_namespace) == 0 ||
982 got_path_is_child(name, ref_namespace,
983 strlen(ref_namespace)))
984 return 1;
985 while (*name != '\0' && *name != '/')
986 name++;
989 return 0;
992 const struct got_error *
993 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
994 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
996 const struct got_error *err;
997 char *packed_refs_path = NULL, *path_refs = NULL;
998 char *abs_namespace = NULL, *buf = NULL;
999 const char *ondisk_ref_namespace = NULL;
1000 char *line = NULL;
1001 FILE *f = NULL;
1002 struct got_reference *ref;
1003 struct got_reflist_entry *new;
1005 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
1006 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
1007 if (path_refs == NULL) {
1008 err = got_error_from_errno("get_refs_dir_path");
1009 goto done;
1011 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0,
1012 got_repo_get_object_format(repo));
1013 if (err)
1014 goto done;
1015 err = got_reflist_insert(&new, refs, ref, cmp_cb, cmp_arg);
1016 if (err || new == NULL /* duplicate */)
1017 got_ref_close(ref);
1018 if (err && err->code != GOT_ERR_NOT_REF)
1019 goto done;
1020 } else {
1021 /* Try listing a single reference. */
1022 err = got_ref_open(&ref, repo, ref_namespace, 0);
1023 if (err) {
1024 if (err->code != GOT_ERR_NOT_REF)
1025 goto done;
1026 /* Try to look up references in a given namespace. */
1027 } else {
1028 err = got_reflist_insert(&new, refs, ref,
1029 cmp_cb, cmp_arg);
1030 if (err || new == NULL /* duplicate */)
1031 got_ref_close(ref);
1032 return err;
1036 if (ref_namespace) {
1037 size_t len;
1038 /* Canonicalize the path to eliminate double-slashes if any. */
1039 if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
1040 err = got_error_from_errno("asprintf");
1041 goto done;
1043 len = strlen(abs_namespace) + 1;
1044 buf = malloc(len);
1045 if (buf == NULL) {
1046 err = got_error_from_errno("malloc");
1047 goto done;
1049 err = got_canonpath(abs_namespace, buf, len);
1050 if (err)
1051 goto done;
1052 ondisk_ref_namespace = buf;
1053 while (ondisk_ref_namespace[0] == '/')
1054 ondisk_ref_namespace++;
1055 if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
1056 ondisk_ref_namespace += 5;
1057 else if (strcmp(ondisk_ref_namespace, "refs") == 0)
1058 ondisk_ref_namespace = "";
1061 /* Gather on-disk refs before parsing packed-refs. */
1062 free(path_refs);
1063 path_refs = get_refs_dir_path(repo, "");
1064 if (path_refs == NULL) {
1065 err = got_error_from_errno("get_refs_dir_path");
1066 goto done;
1068 err = gather_on_disk_refs(refs, path_refs,
1069 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
1070 cmp_cb, cmp_arg);
1071 if (err)
1072 goto done;
1075 * The packed-refs file may contain redundant entries, in which
1076 * case on-disk refs take precedence.
1078 packed_refs_path = got_repo_get_path_packed_refs(repo);
1079 if (packed_refs_path == NULL) {
1080 err = got_error_from_errno("got_repo_get_path_packed_refs");
1081 goto done;
1084 f = fopen(packed_refs_path, "re");
1085 if (f) {
1086 size_t linesize = 0;
1087 ssize_t linelen;
1088 struct stat sb;
1090 if (fstat(fileno(f), &sb) == -1) {
1091 err = got_error_from_errno2("fstat", packed_refs_path);
1092 goto done;
1094 for (;;) {
1095 linelen = getline(&line, &linesize, f);
1096 if (linelen == -1) {
1097 if (feof(f))
1098 break;
1099 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1100 goto done;
1102 if (linelen > 0 && line[linelen - 1] == '\n')
1103 line[linelen - 1] = '\0';
1104 err = parse_packed_ref_line(&ref, NULL, line,
1105 sb.st_mtime, got_repo_get_object_format(repo));
1106 if (err)
1107 goto done;
1108 if (ref) {
1109 if (ref_namespace &&
1110 !match_packed_ref(ref, ref_namespace)) {
1111 got_ref_close(ref);
1112 continue;
1114 err = got_reflist_insert(&new, refs, ref,
1115 cmp_cb, cmp_arg);
1116 if (err || new == NULL /* duplicate */)
1117 got_ref_close(ref);
1118 if (err)
1119 goto done;
1123 done:
1124 free(packed_refs_path);
1125 free(abs_namespace);
1126 free(buf);
1127 free(line);
1128 free(path_refs);
1129 if (f && fclose(f) == EOF && err == NULL)
1130 err = got_error_from_errno("fclose");
1131 return err;
1134 void
1135 got_ref_list_free(struct got_reflist_head *refs)
1137 struct got_reflist_entry *re;
1139 while ((re = TAILQ_FIRST(refs))) {
1140 TAILQ_REMOVE(refs, re, entry);
1141 got_ref_close(re->ref);
1142 free(re);
1147 got_ref_is_symbolic(struct got_reference *ref)
1149 return (ref->flags & GOT_REF_IS_SYMBOLIC);
1152 const struct got_error *
1153 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1155 if (ref->flags & GOT_REF_IS_SYMBOLIC)
1156 return got_error(GOT_ERR_BAD_REF_TYPE);
1158 memcpy(&ref->ref.ref.id, id, sizeof(ref->ref.ref.id));
1159 return NULL;
1162 const struct got_error *
1163 got_ref_change_symref(struct got_reference *ref, const char *refname)
1165 char *new_name;
1167 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1168 return got_error(GOT_ERR_BAD_REF_TYPE);
1170 new_name = strdup(refname);
1171 if (new_name == NULL)
1172 return got_error_from_errno("strdup");
1174 free(ref->ref.symref.ref);
1175 ref->ref.symref.ref = new_name;
1176 return NULL;
1179 const struct got_error *
1180 got_ref_change_symref_to_ref(struct got_reference *symref,
1181 struct got_object_id *id)
1183 if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1184 return got_error(GOT_ERR_BAD_REF_TYPE);
1186 symref->ref.ref.name = symref->ref.symref.name;
1187 memcpy(&symref->ref.ref.id, id, sizeof(symref->ref.ref.id));
1188 symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1189 return NULL;
1192 const struct got_error *
1193 got_ref_write(struct got_reference *ref, struct got_repository *repo)
1195 const struct got_error *err = NULL, *unlock_err = NULL;
1196 const char *name = got_ref_get_name(ref);
1197 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1198 struct got_lockfile *lf = NULL;
1199 FILE *f = NULL;
1200 size_t n;
1201 struct stat sb;
1203 path_refs = get_refs_dir_path(repo, name);
1204 if (path_refs == NULL) {
1205 err = got_error_from_errno2("get_refs_dir_path", name);
1206 goto done;
1209 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1210 err = got_error_from_errno("asprintf");
1211 goto done;
1214 err = got_opentemp_named(&tmppath, &f, path, "");
1215 if (err) {
1216 char *parent;
1217 if (err->code == GOT_ERR_ERRNO && errno == ENOTDIR) {
1218 err = got_error_fmt(GOT_ERR_BAD_REF_NAME,
1219 "collision with an existing reference: %s",
1220 got_ref_get_name(ref));
1221 goto done;
1223 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1224 goto done;
1225 err = got_path_dirname(&parent, path);
1226 if (err)
1227 goto done;
1228 err = got_path_mkdir(parent);
1229 free(parent);
1230 if (err)
1231 goto done;
1232 err = got_opentemp_named(&tmppath, &f, path, "");
1233 if (err)
1234 goto done;
1237 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1238 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1239 if (n != strlen(ref->ref.symref.ref) + 6) {
1240 err = got_ferror(f, GOT_ERR_IO);
1241 goto done;
1243 } else {
1244 char *hex;
1245 size_t len;
1247 err = got_object_id_str(&hex, &ref->ref.ref.id);
1248 if (err)
1249 goto done;
1250 len = strlen(hex);
1251 n = fprintf(f, "%s\n", hex);
1252 free(hex);
1253 if (n != len + 1) {
1254 err = got_ferror(f, GOT_ERR_IO);
1255 goto done;
1259 if (ref->lf == NULL) {
1260 err = got_lockfile_lock(&lf, path, -1);
1261 if (err)
1262 goto done;
1265 /* XXX: check if old content matches our expectations? */
1267 if (stat(path, &sb) != 0) {
1268 if (errno != ENOENT) {
1269 err = got_error_from_errno2("stat", path);
1270 goto done;
1272 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1275 if (fchmod(fileno(f), sb.st_mode) != 0) {
1276 err = got_error_from_errno2("fchmod", tmppath);
1277 goto done;
1280 if (rename(tmppath, path) != 0) {
1281 err = got_error_from_errno3("rename", tmppath, path);
1282 goto done;
1284 free(tmppath);
1285 tmppath = NULL;
1287 if (stat(path, &sb) == -1) {
1288 err = got_error_from_errno2("stat", path);
1289 goto done;
1291 ref->mtime = sb.st_mtime;
1292 done:
1293 if (ref->lf == NULL && lf)
1294 unlock_err = got_lockfile_unlock(lf, -1);
1295 if (f) {
1296 if (fclose(f) == EOF && err == NULL)
1297 err = got_error_from_errno("fclose");
1299 free(path_refs);
1300 free(path);
1301 if (tmppath) {
1302 if (unlink(tmppath) == -1 && err == NULL)
1303 err = got_error_from_errno2("unlink", tmppath);
1304 free(tmppath);
1306 return err ? err : unlock_err;
1309 static const struct got_error *
1310 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1312 const struct got_error *err = NULL, *unlock_err = NULL;
1313 struct got_lockfile *lf = NULL;
1314 FILE *f = NULL, *tmpf = NULL;
1315 char *line = NULL, *packed_refs_path, *tmppath = NULL;
1316 size_t linesize = 0;
1317 struct got_reflist_head refs;
1318 int found_delref = 0;
1320 /* The packed-refs file does not contain symbolic references. */
1321 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1322 return got_error(GOT_ERR_BAD_REF_DATA);
1324 TAILQ_INIT(&refs);
1326 packed_refs_path = got_repo_get_path_packed_refs(repo);
1327 if (packed_refs_path == NULL)
1328 return got_error_from_errno("got_repo_get_path_packed_refs");
1330 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path, "");
1331 if (err)
1332 goto done;
1334 if (delref->lf == NULL) {
1335 err = got_lockfile_lock(&lf, packed_refs_path, -1);
1336 if (err)
1337 goto done;
1340 f = fopen(packed_refs_path, "re");
1341 if (f == NULL) {
1342 err = got_error_from_errno2("fopen", packed_refs_path);
1343 goto done;
1345 for (;;) {
1346 ssize_t linelen;
1347 struct got_reference *ref;
1348 struct got_reflist_entry *new;
1350 linelen = getline(&line, &linesize, f);
1351 if (linelen == -1) {
1352 if (feof(f))
1353 break;
1354 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1355 goto done;
1357 if (linelen > 0 && line[linelen - 1] == '\n')
1358 line[linelen - 1] = '\0';
1359 err = parse_packed_ref_line(&ref, NULL, line, 0,
1360 got_repo_get_object_format(repo));
1361 if (err)
1362 goto done;
1363 if (ref == NULL)
1364 continue;
1366 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1367 got_object_id_cmp(&ref->ref.ref.id,
1368 &delref->ref.ref.id) == 0) {
1369 found_delref = 1;
1370 got_ref_close(ref);
1371 continue;
1374 err = got_reflist_insert(&new, &refs, ref,
1375 got_ref_cmp_by_name, NULL);
1376 if (err || new == NULL /* duplicate */)
1377 got_ref_close(ref);
1378 if (err)
1379 goto done;
1382 if (found_delref) {
1383 struct got_reflist_entry *re;
1384 size_t n;
1385 struct stat sb;
1387 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1388 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1389 err = got_ferror(f, GOT_ERR_IO);
1390 goto done;
1393 TAILQ_FOREACH(re, &refs, entry) {
1394 char *hex;
1395 size_t len;
1397 err = got_object_id_str(&hex, &re->ref->ref.ref.id);
1398 if (err)
1399 goto done;
1400 len = strlen(hex);
1401 n = fprintf(tmpf, "%s ", hex);
1402 free(hex);
1403 if (n != len + 1) {
1404 err = got_ferror(f, GOT_ERR_IO);
1405 goto done;
1408 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1409 if (n != strlen(re->ref->ref.ref.name) + 1) {
1410 err = got_ferror(f, GOT_ERR_IO);
1411 goto done;
1415 if (fflush(tmpf) != 0) {
1416 err = got_error_from_errno("fflush");
1417 goto done;
1420 if (fstat(fileno(f), &sb) != 0) {
1421 if (errno != ENOENT) {
1422 err = got_error_from_errno2("fstat",
1423 packed_refs_path);
1424 goto done;
1426 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1429 if (fchmod(fileno(tmpf), sb.st_mode) != 0) {
1430 err = got_error_from_errno2("fchmod", tmppath);
1431 goto done;
1434 if (rename(tmppath, packed_refs_path) != 0) {
1435 err = got_error_from_errno3("rename", tmppath,
1436 packed_refs_path);
1437 goto done;
1439 free(tmppath);
1440 tmppath = NULL;
1442 done:
1443 if (delref->lf == NULL && lf)
1444 unlock_err = got_lockfile_unlock(lf, -1);
1445 if (f) {
1446 if (fclose(f) == EOF && err == NULL)
1447 err = got_error_from_errno("fclose");
1449 if (tmppath && unlink(tmppath) == -1 && err == NULL)
1450 err = got_error_from_errno2("unlink", tmppath);
1451 if (tmpf && fclose(tmpf) == EOF && err == NULL)
1452 err = got_error_from_errno("fclose");
1453 free(tmppath);
1454 free(packed_refs_path);
1455 free(line);
1456 got_ref_list_free(&refs);
1457 return err ? err : unlock_err;
1460 static const struct got_error *
1461 delete_loose_ref(struct got_reference *ref, struct got_repository *repo)
1463 const struct got_error *err = NULL, *unlock_err = NULL;
1464 const char *name = got_ref_get_name(ref);
1465 char *path_refs = NULL, *path = NULL;
1466 struct got_lockfile *lf = NULL;
1468 path_refs = get_refs_dir_path(repo, name);
1469 if (path_refs == NULL) {
1470 err = got_error_from_errno2("get_refs_dir_path", name);
1471 goto done;
1474 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1475 err = got_error_from_errno("asprintf");
1476 goto done;
1479 if (ref->lf == NULL) {
1480 err = got_lockfile_lock(&lf, path, -1);
1481 if (err)
1482 goto done;
1485 /* XXX: check if old content matches our expectations? */
1487 if (unlink(path) == -1)
1488 err = got_error_from_errno2("unlink", path);
1489 done:
1490 if (ref->lf == NULL && lf)
1491 unlock_err = got_lockfile_unlock(lf, -1);
1493 free(path_refs);
1494 free(path);
1495 return err ? err : unlock_err;
1498 const struct got_error *
1499 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1501 const struct got_error *err = NULL;
1502 struct got_reference *ref2;
1504 if (ref->flags & GOT_REF_IS_PACKED) {
1505 err = delete_packed_ref(ref, repo);
1506 if (err)
1507 return err;
1509 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1510 if (err) {
1511 if (err->code == GOT_ERR_NOT_REF)
1512 return NULL;
1513 return err;
1516 err = delete_loose_ref(ref2, repo);
1517 got_ref_close(ref2);
1518 return err;
1519 } else {
1520 err = delete_loose_ref(ref, repo);
1521 if (err)
1522 return err;
1524 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1525 if (err) {
1526 if (err->code == GOT_ERR_NOT_REF)
1527 return NULL;
1528 return err;
1531 err = delete_packed_ref(ref2, repo);
1532 got_ref_close(ref2);
1533 return err;
1537 const struct got_error *
1538 got_ref_unlock(struct got_reference *ref)
1540 const struct got_error *err;
1541 err = got_lockfile_unlock(ref->lf, -1);
1542 ref->lf = NULL;
1543 return err;
1546 struct got_reflist_object_id_map {
1547 struct got_object_idset *idset;
1550 struct got_reflist_object_id_map_entry {
1551 struct got_reflist_head refs;
1554 static const struct got_error *
1555 add_object_id_map_entry(struct got_object_idset *idset,
1556 struct got_object_id *id, struct got_reflist_entry *re)
1558 const struct got_error *err = NULL;
1559 struct got_reflist_object_id_map_entry *ent;
1560 struct got_reflist_entry *new;
1562 ent = got_object_idset_get(idset, id);
1563 if (ent == NULL) {
1564 ent = malloc(sizeof(*ent));
1565 if (ent == NULL)
1566 return got_error_from_errno("malloc");
1568 TAILQ_INIT(&ent->refs);
1569 err = got_object_idset_add(idset, id, ent);
1570 if (err) {
1571 free(ent);
1572 return err;
1576 err = got_reflist_entry_dup(&new, re);
1577 if (err)
1578 return err;
1580 TAILQ_INSERT_TAIL(&ent->refs, new, entry);
1581 return NULL;
1584 const struct got_error *
1585 got_reflist_object_id_map_create(struct got_reflist_object_id_map **map,
1586 struct got_reflist_head *refs, struct got_repository *repo)
1588 const struct got_error *err = NULL;
1589 struct got_object_idset *idset;
1590 struct got_object_id *id = NULL;
1591 struct got_reflist_entry *re;
1593 idset = got_object_idset_alloc();
1594 if (idset == NULL)
1595 return got_error_from_errno("got_object_idset_alloc");
1597 *map = malloc(sizeof(**map));
1598 if (*map == NULL) {
1599 got_object_idset_free(idset);
1600 return got_error_from_errno("malloc");
1602 (*map)->idset = idset;
1604 TAILQ_FOREACH(re, refs, entry) {
1605 struct got_tag_object *tag = NULL;
1607 err = got_ref_resolve(&id, repo, re->ref);
1608 if (err)
1609 goto done;
1611 err = add_object_id_map_entry(idset, id, re);
1612 if (err)
1613 goto done;
1615 if (strstr(got_ref_get_name(re->ref), "/tags/") == NULL) {
1616 free(id);
1617 id = NULL;
1618 continue;
1621 err = got_object_open_as_tag(&tag, repo, id);
1622 if (err) {
1623 if (err->code != GOT_ERR_OBJ_TYPE)
1624 goto done;
1625 /* Ref points at something other than a tag. */
1626 err = NULL;
1627 tag = NULL;
1628 free(id);
1629 id = NULL;
1630 continue;
1633 err = add_object_id_map_entry(idset,
1634 got_object_tag_get_object_id(tag), re);
1635 got_object_tag_close(tag);
1636 if (err)
1637 goto done;
1639 free(id);
1640 id = NULL;
1642 done:
1643 free(id);
1644 if (err) {
1645 got_reflist_object_id_map_free(*map);
1646 *map = NULL;
1648 return err;
1651 struct got_reflist_head *
1652 got_reflist_object_id_map_lookup(struct got_reflist_object_id_map *map,
1653 struct got_object_id *id)
1655 struct got_reflist_object_id_map_entry *ent;
1656 ent = got_object_idset_get(map->idset, id);
1657 if (ent)
1658 return &ent->refs;
1659 return NULL;
1662 static const struct got_error *
1663 free_id_map_entry(struct got_object_id *id, void *data, void *arg)
1665 struct got_reflist_object_id_map_entry *ent = data;
1667 got_ref_list_free(&ent->refs);
1668 free(ent);
1669 return NULL;
1672 void
1673 got_reflist_object_id_map_free(struct got_reflist_object_id_map *map)
1675 got_object_idset_for_each(map->idset, free_id_map_entry, NULL);
1676 got_object_idset_free(map->idset);
1677 free(map);