portable: add back sys/queue.h
[got-portable.git] / lib / reference.c
blob15007ad091c5bbeac874d3a7f095826bb21f3867
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 <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/stat.h>
21 #include <errno.h>
22 #include <ctype.h>
23 #include <dirent.h>
24 #include <limits.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <zlib.h>
30 #include <time.h>
31 #include <libgen.h>
33 #include "got_compat.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_sha1.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 u_int8_t sha1[SHA1_DIGEST_LENGTH];
78 /* A reference which points to an arbitrary object. */
79 struct got_reference {
80 unsigned int flags;
81 #define GOT_REF_IS_SYMBOLIC 0x01
82 #define GOT_REF_IS_PACKED 0x02
84 union {
85 struct got_ref ref;
86 struct got_symref symref;
87 } ref;
89 struct got_lockfile *lf;
90 time_t mtime;
92 /* Cached timestamp for got_ref_cmp_by_commit_timestamp_descending() */
93 time_t committer_time;
96 static const struct got_error *
97 alloc_ref(struct got_reference **ref, const char *name,
98 struct got_object_id *id, int flags, time_t mtime)
100 const struct got_error *err = NULL;
102 *ref = calloc(1, sizeof(**ref));
103 if (*ref == NULL)
104 return got_error_from_errno("calloc");
106 memcpy((*ref)->ref.ref.sha1, id->sha1, sizeof((*ref)->ref.ref.sha1));
107 (*ref)->flags = flags;
108 (*ref)->ref.ref.name = strdup(name);
109 (*ref)->mtime = mtime;
110 if ((*ref)->ref.ref.name == NULL) {
111 err = got_error_from_errno("strdup");
112 got_ref_close(*ref);
113 *ref = NULL;
115 return err;
118 static const struct got_error *
119 alloc_symref(struct got_reference **ref, const char *name,
120 const char *target_ref, int flags)
122 const struct got_error *err = NULL;
124 *ref = calloc(1, sizeof(**ref));
125 if (*ref == NULL)
126 return got_error_from_errno("calloc");
128 (*ref)->flags = GOT_REF_IS_SYMBOLIC | flags;
129 (*ref)->ref.symref.name = strdup(name);
130 if ((*ref)->ref.symref.name == NULL) {
131 err = got_error_from_errno("strdup");
132 got_ref_close(*ref);
133 *ref = NULL;
134 return err;
136 (*ref)->ref.symref.ref = strdup(target_ref);
137 if ((*ref)->ref.symref.ref == NULL) {
138 err = got_error_from_errno("strdup");
139 got_ref_close(*ref);
140 *ref = NULL;
142 return err;
145 static const struct got_error *
146 parse_symref(struct got_reference **ref, const char *name, const char *line)
148 if (line[0] == '\0')
149 return got_error(GOT_ERR_BAD_REF_DATA);
151 return alloc_symref(ref, name, line, 0);
154 static const struct got_error *
155 parse_ref_line(struct got_reference **ref, const char *name, const char *line,
156 time_t mtime)
158 struct got_object_id id;
160 if (strncmp(line, "ref: ", 5) == 0) {
161 line += 5;
162 return parse_symref(ref, name, line);
165 if (!got_parse_sha1_digest(id.sha1, line))
166 return got_error(GOT_ERR_BAD_REF_DATA);
168 return alloc_ref(ref, name, &id, 0, mtime);
171 static const struct got_error *
172 parse_ref_file(struct got_reference **ref, const char *name,
173 const char *absname, const char *abspath, int lock)
175 const struct got_error *err = NULL;
176 FILE *f;
177 char *line = NULL;
178 size_t linesize = 0;
179 ssize_t linelen;
180 struct got_lockfile *lf = NULL;
181 struct stat sb;
183 if (lock) {
184 err = got_lockfile_lock(&lf, abspath, -1);
185 if (err) {
186 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
187 err = got_error_not_ref(name);
188 return err;
192 f = fopen(abspath, "rbe");
193 if (f == NULL) {
194 if (errno != ENOTDIR && errno != ENOENT)
195 err = got_error_from_errno2("fopen", abspath);
196 else
197 err = got_error_not_ref(name);
198 if (lock)
199 got_lockfile_unlock(lf, -1);
200 return err;
202 if (fstat(fileno(f), &sb) == -1) {
203 err = got_error_from_errno2("fstat", abspath);
204 goto done;
207 linelen = getline(&line, &linesize, f);
208 if (linelen == -1) {
209 if (feof(f))
210 err = NULL; /* ignore empty files (could be locks) */
211 else {
212 if (errno == EISDIR)
213 err = got_error(GOT_ERR_NOT_REF);
214 else if (ferror(f))
215 err = got_ferror(f, GOT_ERR_IO);
216 else
217 err = got_error_from_errno2("getline", abspath);
219 if (lock)
220 got_lockfile_unlock(lf, -1);
221 goto done;
223 while (linelen > 0 && line[linelen - 1] == '\n') {
224 line[linelen - 1] = '\0';
225 linelen--;
228 err = parse_ref_line(ref, absname, line, sb.st_mtime);
229 if (lock) {
230 if (err)
231 got_lockfile_unlock(lf, -1);
232 else {
233 if (*ref)
234 (*ref)->lf = lf;
235 else
236 got_lockfile_unlock(lf, -1);
239 done:
240 free(line);
241 if (fclose(f) == EOF && err == NULL) {
242 err = got_error_from_errno("fclose");
243 if (*ref) {
244 if (lock)
245 got_ref_unlock(*ref);
246 got_ref_close(*ref);
247 *ref = NULL;
250 return err;
253 static int
254 is_well_known_ref(const char *refname)
256 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
257 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
258 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
259 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
262 static char *
263 get_refs_dir_path(struct got_repository *repo, const char *refname)
265 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
266 return strdup(got_repo_get_path_git_dir(repo));
268 return got_repo_get_path_refs(repo);
272 got_ref_name_is_valid(const char *name)
274 const char *s, *seg;
275 const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
276 const char *forbidden_seq[] = { "//", "..", "@{" };
277 const char *lfs = GOT_LOCKFILE_SUFFIX;
278 const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
279 size_t i;
281 if (name[0] == '@' && name[1] == '\0')
282 return 0;
284 s = name;
285 seg = s;
286 if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
287 return 0;
288 while (*s) {
289 for (i = 0; i < nitems(forbidden); i++) {
290 if (*s == forbidden[i])
291 return 0;
293 for (i = 0; i < nitems(forbidden_seq); i++) {
294 if (s[0] == forbidden_seq[i][0] &&
295 s[1] == forbidden_seq[i][1])
296 return 0;
298 if (iscntrl((unsigned char)s[0]))
299 return 0;
300 if (s[0] == '.' && s[1] == '\0')
301 return 0;
302 if (*s == '/') {
303 const char *nextseg = s + 1;
304 if (nextseg[0] == '\0' || nextseg[0] == '.' ||
305 nextseg[0] == '/')
306 return 0;
307 if (seg <= s - lfs_len &&
308 strncmp(s - lfs_len, lfs, lfs_len) == 0)
309 return 0;
310 seg = nextseg;
312 s++;
315 if (seg <= s - lfs_len &&
316 strncmp(s - lfs_len, lfs, lfs_len) == 0)
317 return 0;
319 return 1;
322 const struct got_error *
323 got_ref_alloc(struct got_reference **ref, const char *name,
324 struct got_object_id *id)
326 if (!got_ref_name_is_valid(name))
327 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
329 return alloc_ref(ref, name, id, 0, 0);
332 const struct got_error *
333 got_ref_alloc_symref(struct got_reference **ref, const char *name,
334 struct got_reference *target_ref)
336 if (!got_ref_name_is_valid(name))
337 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
339 return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
342 static const struct got_error *
343 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
344 const char *line, time_t mtime)
346 struct got_object_id id;
347 const char *name;
349 *ref = NULL;
351 if (line[0] == '#' || line[0] == '^')
352 return NULL;
354 if (!got_parse_sha1_digest(id.sha1, line))
355 return got_error(GOT_ERR_BAD_REF_DATA);
357 if (abs_refname) {
358 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
359 return NULL;
360 name = abs_refname;
361 } else
362 name = line + SHA1_DIGEST_STRING_LENGTH;
364 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED, mtime);
367 static const struct got_error *
368 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
369 int nsubdirs, const char *refname, time_t mtime)
371 const struct got_error *err = NULL;
372 char *abs_refname;
373 char *line = NULL;
374 size_t linesize = 0;
375 ssize_t linelen;
376 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
378 *ref = NULL;
380 if (ref_is_absolute)
381 abs_refname = (char *)refname;
382 do {
383 linelen = getline(&line, &linesize, f);
384 if (linelen == -1) {
385 if (feof(f))
386 break;
387 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
388 break;
390 if (linelen > 0 && line[linelen - 1] == '\n')
391 line[linelen - 1] = '\0';
392 for (i = 0; i < nsubdirs; i++) {
393 if (!ref_is_absolute &&
394 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
395 refname) == -1)
396 return got_error_from_errno("asprintf");
397 err = parse_packed_ref_line(ref, abs_refname, line,
398 mtime);
399 if (!ref_is_absolute)
400 free(abs_refname);
401 if (err || *ref != NULL)
402 break;
404 if (err)
405 break;
406 } while (*ref == NULL);
407 free(line);
409 return err;
412 static const struct got_error *
413 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
414 const char *name, int lock)
416 const struct got_error *err = NULL;
417 char *path = NULL;
418 char *absname = NULL;
419 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
420 int ref_is_well_known = (subdir[0] == '\0' && is_well_known_ref(name));
422 *ref = NULL;
424 if (!got_ref_name_is_valid(name))
425 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
427 if (ref_is_absolute || ref_is_well_known) {
428 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
429 return got_error_from_errno("asprintf");
430 absname = (char *)name;
431 } else {
432 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
433 subdir[0] ? "/" : "", name) == -1)
434 return got_error_from_errno("asprintf");
436 if (asprintf(&absname, "refs/%s%s%s",
437 subdir, subdir[0] ? "/" : "", name) == -1) {
438 err = got_error_from_errno("asprintf");
439 goto done;
443 err = parse_ref_file(ref, name, absname, path, lock);
444 done:
445 if (!ref_is_absolute && !ref_is_well_known)
446 free(absname);
447 free(path);
448 return err;
451 const struct got_error *
452 got_ref_open(struct got_reference **ref, struct got_repository *repo,
453 const char *refname, int lock)
455 const struct got_error *err = NULL;
456 char *path_refs = NULL;
457 const char *subdirs[] = {
458 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
460 size_t i;
461 int well_known = is_well_known_ref(refname);
462 struct got_lockfile *lf = NULL;
464 *ref = NULL;
466 path_refs = get_refs_dir_path(repo, refname);
467 if (path_refs == NULL) {
468 err = got_error_from_errno2("get_refs_dir_path", refname);
469 goto done;
472 if (well_known) {
473 err = open_ref(ref, path_refs, "", refname, lock);
474 } else {
475 char *packed_refs_path;
476 FILE *f;
478 /* Search on-disk refs before packed refs! */
479 for (i = 0; i < nitems(subdirs); i++) {
480 err = open_ref(ref, path_refs, subdirs[i], refname,
481 lock);
482 if ((err && err->code != GOT_ERR_NOT_REF) || *ref)
483 goto done;
486 packed_refs_path = got_repo_get_path_packed_refs(repo);
487 if (packed_refs_path == NULL) {
488 err = got_error_from_errno(
489 "got_repo_get_path_packed_refs");
490 goto done;
493 if (lock) {
494 err = got_lockfile_lock(&lf, packed_refs_path, -1);
495 if (err)
496 goto done;
498 f = fopen(packed_refs_path, "rbe");
499 free(packed_refs_path);
500 if (f != NULL) {
501 struct stat sb;
502 if (fstat(fileno(f), &sb) == -1) {
503 err = got_error_from_errno2("fstat",
504 packed_refs_path);
505 goto done;
507 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
508 refname, sb.st_mtime);
509 if (!err) {
510 if (fclose(f) == EOF) {
511 err = got_error_from_errno("fclose");
512 got_ref_close(*ref);
513 *ref = NULL;
514 } else if (*ref)
515 (*ref)->lf = lf;
519 done:
520 if (!err && *ref == NULL)
521 err = got_error_not_ref(refname);
522 if (err && lf)
523 got_lockfile_unlock(lf, -1);
524 free(path_refs);
525 return err;
528 void
529 got_ref_close(struct got_reference *ref)
531 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
532 free(ref->ref.symref.name);
533 free(ref->ref.symref.ref);
534 } else
535 free(ref->ref.ref.name);
536 free(ref);
539 struct got_reference *
540 got_ref_dup(struct got_reference *ref)
542 struct got_reference *ret;
544 ret = calloc(1, sizeof(*ret));
545 if (ret == NULL)
546 return NULL;
548 ret->flags = ref->flags;
549 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
550 ret->ref.symref.name = strdup(ref->ref.symref.name);
551 if (ret->ref.symref.name == NULL) {
552 free(ret);
553 return NULL;
555 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
556 if (ret->ref.symref.ref == NULL) {
557 free(ret->ref.symref.name);
558 free(ret);
559 return NULL;
561 } else {
562 ret->ref.ref.name = strdup(ref->ref.ref.name);
563 if (ret->ref.ref.name == NULL) {
564 free(ret);
565 return NULL;
567 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
568 sizeof(ret->ref.ref.sha1));
571 return ret;
574 const struct got_error *
575 got_reflist_entry_dup(struct got_reflist_entry **newp,
576 struct got_reflist_entry *re)
578 const struct got_error *err = NULL;
579 struct got_reflist_entry *new;
581 *newp = NULL;
583 new = malloc(sizeof(*new));
584 if (new == NULL)
585 return got_error_from_errno("malloc");
587 new->ref = got_ref_dup(re->ref);
588 if (new->ref == NULL) {
589 err = got_error_from_errno("got_ref_dup");
590 free(new);
591 return err;
594 *newp = new;
595 return NULL;
598 const struct got_error *
599 got_ref_resolve_symbolic(struct got_reference **resolved,
600 struct got_repository *repo, struct got_reference *ref)
602 struct got_reference *nextref;
603 const struct got_error *err;
605 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
606 if (err)
607 return err;
609 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
610 err = got_ref_resolve_symbolic(resolved, repo, nextref);
611 else
612 *resolved = got_ref_dup(nextref);
614 got_ref_close(nextref);
615 return err;
618 static const struct got_error *
619 ref_resolve(struct got_object_id **id, struct got_repository *repo,
620 struct got_reference *ref, int recursion)
622 const struct got_error *err;
624 if (recursion <= 0)
625 return got_error_msg(GOT_ERR_RECURSION,
626 "reference recursion limit reached");
628 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
629 struct got_reference *resolved = NULL;
630 err = got_ref_resolve_symbolic(&resolved, repo, ref);
631 if (err == NULL)
632 err = ref_resolve(id, repo, resolved, --recursion);
633 if (resolved)
634 got_ref_close(resolved);
635 return err;
638 *id = calloc(1, sizeof(**id));
639 if (*id == NULL)
640 return got_error_from_errno("calloc");
641 memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
642 return NULL;
645 const struct got_error *
646 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
647 struct got_reference *ref)
649 return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
652 char *
653 got_ref_to_str(struct got_reference *ref)
655 char *str;
657 if (ref->flags & GOT_REF_IS_SYMBOLIC)
658 return strdup(ref->ref.symref.ref);
660 str = malloc(SHA1_DIGEST_STRING_LENGTH);
661 if (str == NULL)
662 return NULL;
664 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
665 SHA1_DIGEST_STRING_LENGTH) == NULL) {
666 free(str);
667 return NULL;
670 return str;
673 const char *
674 got_ref_get_name(struct got_reference *ref)
676 if (ref->flags & GOT_REF_IS_SYMBOLIC)
677 return ref->ref.symref.name;
679 return ref->ref.ref.name;
682 const char *
683 got_ref_get_symref_target(struct got_reference *ref)
685 if (ref->flags & GOT_REF_IS_SYMBOLIC)
686 return ref->ref.symref.ref;
688 return NULL;
691 time_t
692 got_ref_get_mtime(struct got_reference *ref)
694 return ref->mtime;
697 const struct got_error *
698 got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
699 struct got_reference* re2)
701 const char *name1 = got_ref_get_name(re1);
702 const char *name2 = got_ref_get_name(re2);
704 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
705 return NULL;
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;
714 struct got_object_id *id1, *id2 = NULL;
715 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
716 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
717 time_t time1, time2;
719 *cmp = 0;
721 err = got_ref_resolve(&id1, repo, ref1);
722 if (err)
723 return err;
724 err = got_object_open_as_tag(&tag1, repo, id1);
725 if (err) {
726 if (err->code != GOT_ERR_OBJ_TYPE)
727 goto done;
728 /* "lightweight" tag */
729 err = got_object_open_as_commit(&commit1, repo, id1);
730 if (err)
731 goto done;
732 time1 = got_object_commit_get_committer_time(commit1);
733 } else
734 time1 = got_object_tag_get_tagger_time(tag1);
736 err = got_ref_resolve(&id2, repo, ref2);
737 if (err)
738 goto done;
739 err = got_object_open_as_tag(&tag2, repo, id2);
740 if (err) {
741 if (err->code != GOT_ERR_OBJ_TYPE)
742 goto done;
743 /* "lightweight" tag */
744 err = got_object_open_as_commit(&commit2, repo, id2);
745 if (err)
746 goto done;
747 time2 = got_object_commit_get_committer_time(commit2);
748 } else
749 time2 = got_object_tag_get_tagger_time(tag2);
751 /* Put latest tags first. */
752 if (time1 < time2)
753 *cmp = 1;
754 else if (time1 > time2)
755 *cmp = -1;
756 else
757 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
758 done:
759 free(id1);
760 free(id2);
761 if (tag1)
762 got_object_tag_close(tag1);
763 if (tag2)
764 got_object_tag_close(tag2);
765 if (commit1)
766 got_object_commit_close(commit1);
767 if (commit2)
768 got_object_commit_close(commit2);
769 return err;
772 static const struct got_error *
773 get_committer_time(struct got_reference *ref, struct got_repository *repo)
775 const struct got_error *err = NULL;
776 int obj_type;
777 struct got_commit_object *commit = NULL;
778 struct got_tag_object *tag = NULL;
779 struct got_object_id *id = NULL;
781 err = got_ref_resolve(&id, repo, ref);
782 if (err)
783 return err;
785 err = got_object_get_type(&obj_type, repo, id);
786 if (err)
787 goto done;
789 switch (obj_type) {
790 case GOT_OBJ_TYPE_COMMIT:
791 err = got_object_open_as_commit(&commit, repo, id);
792 if (err)
793 goto done;
794 ref->committer_time =
795 got_object_commit_get_committer_time(commit);
796 break;
797 case GOT_OBJ_TYPE_TAG:
798 err = got_object_open_as_tag(&tag, repo, id);
799 if (err)
800 goto done;
801 ref->committer_time = got_object_tag_get_tagger_time(tag);
802 break;
803 default:
804 /* best effort for other object types */
805 ref->committer_time = got_ref_get_mtime(ref);
806 break;
808 done:
809 free(id);
810 if (commit)
811 got_object_commit_close(commit);
812 if (tag)
813 got_object_tag_close(tag);
814 return err;
817 const struct got_error *
818 got_ref_cmp_by_commit_timestamp_descending(void *arg, int *cmp,
819 struct got_reference *ref1, struct got_reference *ref2)
821 const struct got_error *err = NULL;
822 struct got_repository *repo = arg;
824 *cmp = 0;
826 if (ref1->committer_time == 0) {
827 err = get_committer_time(ref1, repo);
828 if (err)
829 return err;
831 if (ref2->committer_time == 0) {
832 err = get_committer_time(ref2, repo);
833 if (err)
834 return err;
837 if (ref1->committer_time < ref2->committer_time)
838 *cmp = 1;
839 else if (ref2->committer_time < ref1->committer_time)
840 *cmp = -1;
841 else
842 return got_ref_cmp_by_name(arg, cmp, ref1, ref2);
844 return err;
847 const struct got_error *
848 got_reflist_insert(struct got_reflist_entry **newp,
849 struct got_reflist_head *refs, struct got_reference *ref,
850 got_ref_cmp_cb cmp_cb, void *cmp_arg)
852 const struct got_error *err;
853 struct got_reflist_entry *new, *re;
854 int cmp;
856 *newp = NULL;
858 new = malloc(sizeof(*new));
859 if (new == NULL)
860 return got_error_from_errno("malloc");
861 new->ref = ref;
862 *newp = new;
865 * We must de-duplicate entries on insert because packed-refs may
866 * contain redundant entries. On-disk refs take precedence.
867 * This code assumes that on-disk revs are read before packed-refs.
868 * We're iterating the list anyway, so insert elements sorted by name.
870 * Many callers will provide paths in a somewhat sorted order.
871 * Iterating backwards from the tail of the list should be more
872 * efficient than traversing through the entire list each time
873 * an element is inserted.
875 re = TAILQ_LAST(refs, got_reflist_head);
876 while (re) {
877 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
878 if (err)
879 return err;
880 if (cmp == 0) {
881 /* duplicate */
882 free(new);
883 *newp = NULL;
884 return NULL;
885 } else if (cmp < 0) {
886 TAILQ_INSERT_AFTER(refs, re, new, entry);
887 return NULL;
889 re = TAILQ_PREV(re, got_reflist_head, entry);
892 TAILQ_INSERT_HEAD(refs, new, entry);
893 return NULL;
896 const struct got_error *
897 got_reflist_sort(struct got_reflist_head *refs,
898 got_ref_cmp_cb cmp_cb, void *cmp_arg)
900 const struct got_error *err = NULL;
901 struct got_reflist_entry *re, *tmp, *new;
902 struct got_reflist_head sorted;
904 TAILQ_INIT(&sorted);
906 TAILQ_FOREACH_SAFE(re, refs, entry, tmp) {
907 struct got_reference *ref = re->ref;
908 TAILQ_REMOVE(refs, re, entry);
909 free(re);
910 err = got_reflist_insert(&new, &sorted, ref, cmp_cb, cmp_arg);
911 if (err || new == NULL /* duplicate */)
912 got_ref_close(ref);
913 if (err)
914 return err;
917 TAILQ_CONCAT(refs, &sorted, entry);
918 return NULL;
921 static const struct got_error *
922 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
923 const char *subdir, struct got_repository *repo,
924 got_ref_cmp_cb cmp_cb, void *cmp_arg)
926 const struct got_error *err = NULL;
927 DIR *d = NULL;
928 char *path_subdir;
930 while (subdir[0] == '/')
931 subdir++;
933 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
934 return got_error_from_errno("asprintf");
936 d = opendir(path_subdir);
937 if (d == NULL)
938 goto done;
940 for (;;) {
941 struct dirent *dent;
942 struct got_reference *ref;
943 char *child;
944 int type;
946 dent = readdir(d);
947 if (dent == NULL)
948 break;
950 if (strcmp(dent->d_name, ".") == 0 ||
951 strcmp(dent->d_name, "..") == 0)
952 continue;
954 err = got_path_dirent_type(&type, path_subdir, dent);
955 if (err)
956 break;
958 switch (type) {
959 case DT_REG:
960 err = open_ref(&ref, path_refs, subdir, dent->d_name,
962 if (err)
963 goto done;
964 if (ref) {
965 struct got_reflist_entry *new;
966 err = got_reflist_insert(&new, refs, ref,
967 cmp_cb, cmp_arg);
968 if (err || new == NULL /* duplicate */)
969 got_ref_close(ref);
970 if (err)
971 goto done;
973 break;
974 case DT_DIR:
975 if (asprintf(&child, "%s%s%s", subdir,
976 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
977 err = got_error_from_errno("asprintf");
978 break;
980 err = gather_on_disk_refs(refs, path_refs, child, repo,
981 cmp_cb, cmp_arg);
982 free(child);
983 break;
984 default:
985 break;
988 done:
989 if (d)
990 closedir(d);
991 free(path_subdir);
992 return err;
995 const struct got_error *
996 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
997 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
999 const struct got_error *err;
1000 char *packed_refs_path, *path_refs = NULL;
1001 char *abs_namespace = NULL, *buf = NULL;
1002 const char *ondisk_ref_namespace = NULL;
1003 char *line = NULL;
1004 FILE *f = NULL;
1005 struct got_reference *ref;
1006 struct got_reflist_entry *new;
1008 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
1009 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
1010 if (path_refs == NULL) {
1011 err = got_error_from_errno("get_refs_dir_path");
1012 goto done;
1014 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
1015 if (err)
1016 goto done;
1017 err = got_reflist_insert(&new, refs, ref, cmp_cb, cmp_arg);
1018 if (err || new == NULL /* duplicate */)
1019 got_ref_close(ref);
1020 if (err && err->code != GOT_ERR_NOT_REF)
1021 goto done;
1022 } else {
1023 /* Try listing a single reference. */
1024 const char *refname = ref_namespace;
1025 path_refs = get_refs_dir_path(repo, refname);
1026 if (path_refs == NULL) {
1027 err = got_error_from_errno("get_refs_dir_path");
1028 goto done;
1030 err = open_ref(&ref, path_refs, "", refname, 0);
1031 if (err) {
1032 if (err->code != GOT_ERR_NOT_REF)
1033 goto done;
1034 /* Try to look up references in a given namespace. */
1035 } else {
1036 err = got_reflist_insert(&new, refs, ref,
1037 cmp_cb, cmp_arg);
1038 if (err || new == NULL /* duplicate */)
1039 got_ref_close(ref);
1040 return err;
1044 if (ref_namespace) {
1045 size_t len;
1046 /* Canonicalize the path to eliminate double-slashes if any. */
1047 if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
1048 err = got_error_from_errno("asprintf");
1049 goto done;
1051 len = strlen(abs_namespace) + 1;
1052 buf = malloc(len);
1053 if (buf == NULL) {
1054 err = got_error_from_errno("malloc");
1055 goto done;
1057 err = got_canonpath(abs_namespace, buf, len);
1058 if (err)
1059 goto done;
1060 ondisk_ref_namespace = buf;
1061 while (ondisk_ref_namespace[0] == '/')
1062 ondisk_ref_namespace++;
1063 if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
1064 ondisk_ref_namespace += 5;
1065 else if (strcmp(ondisk_ref_namespace, "refs") == 0)
1066 ondisk_ref_namespace = "";
1069 /* Gather on-disk refs before parsing packed-refs. */
1070 free(path_refs);
1071 path_refs = get_refs_dir_path(repo, "");
1072 if (path_refs == NULL) {
1073 err = got_error_from_errno("get_refs_dir_path");
1074 goto done;
1076 err = gather_on_disk_refs(refs, path_refs,
1077 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
1078 cmp_cb, cmp_arg);
1079 if (err)
1080 goto done;
1083 * The packed-refs file may contain redundant entries, in which
1084 * case on-disk refs take precedence.
1086 packed_refs_path = got_repo_get_path_packed_refs(repo);
1087 if (packed_refs_path == NULL) {
1088 err = got_error_from_errno("got_repo_get_path_packed_refs");
1089 goto done;
1092 f = fopen(packed_refs_path, "re");
1093 free(packed_refs_path);
1094 if (f) {
1095 size_t linesize = 0;
1096 ssize_t linelen;
1097 struct stat sb;
1099 if (fstat(fileno(f), &sb) == -1) {
1100 err = got_error_from_errno2("fstat", packed_refs_path);
1101 goto done;
1103 for (;;) {
1104 linelen = getline(&line, &linesize, f);
1105 if (linelen == -1) {
1106 if (feof(f))
1107 break;
1108 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1109 goto done;
1111 if (linelen > 0 && line[linelen - 1] == '\n')
1112 line[linelen - 1] = '\0';
1113 err = parse_packed_ref_line(&ref, NULL, line,
1114 sb.st_mtime);
1115 if (err)
1116 goto done;
1117 if (ref) {
1118 if (ref_namespace) {
1119 const char *name;
1120 name = got_ref_get_name(ref);
1121 if (!got_path_is_child(name,
1122 ref_namespace,
1123 strlen(ref_namespace))) {
1124 got_ref_close(ref);
1125 continue;
1128 err = got_reflist_insert(&new, refs, ref,
1129 cmp_cb, cmp_arg);
1130 if (err || new == NULL /* duplicate */)
1131 got_ref_close(ref);
1132 if (err)
1133 goto done;
1137 done:
1138 free(abs_namespace);
1139 free(buf);
1140 free(line);
1141 free(path_refs);
1142 if (f && fclose(f) == EOF && err == NULL)
1143 err = got_error_from_errno("fclose");
1144 return err;
1147 void
1148 got_ref_list_free(struct got_reflist_head *refs)
1150 struct got_reflist_entry *re;
1152 while ((re = TAILQ_FIRST(refs))) {
1153 TAILQ_REMOVE(refs, re, entry);
1154 got_ref_close(re->ref);
1155 free(re);
1161 got_ref_is_symbolic(struct got_reference *ref)
1163 return (ref->flags & GOT_REF_IS_SYMBOLIC);
1166 const struct got_error *
1167 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1169 if (ref->flags & GOT_REF_IS_SYMBOLIC)
1170 return got_error(GOT_ERR_BAD_REF_TYPE);
1172 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
1173 return NULL;
1176 const struct got_error *
1177 got_ref_change_symref(struct got_reference *ref, const char *refname)
1179 char *new_name;
1181 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1182 return got_error(GOT_ERR_BAD_REF_TYPE);
1184 new_name = strdup(refname);
1185 if (new_name == NULL)
1186 return got_error_from_errno("strdup");
1188 free(ref->ref.symref.ref);
1189 ref->ref.symref.ref = new_name;
1190 return NULL;
1193 const struct got_error *
1194 got_ref_change_symref_to_ref(struct got_reference *symref,
1195 struct got_object_id *id)
1197 if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1198 return got_error(GOT_ERR_BAD_REF_TYPE);
1200 symref->ref.ref.name = symref->ref.symref.name;
1201 memcpy(symref->ref.ref.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1202 symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1203 return NULL;
1206 const struct got_error *
1207 got_ref_write(struct got_reference *ref, struct got_repository *repo)
1209 const struct got_error *err = NULL, *unlock_err = NULL;
1210 const char *name = got_ref_get_name(ref);
1211 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1212 struct got_lockfile *lf = NULL;
1213 FILE *f = NULL;
1214 size_t n;
1215 struct stat sb;
1217 path_refs = get_refs_dir_path(repo, name);
1218 if (path_refs == NULL) {
1219 err = got_error_from_errno2("get_refs_dir_path", name);
1220 goto done;
1223 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1224 err = got_error_from_errno("asprintf");
1225 goto done;
1228 err = got_opentemp_named(&tmppath, &f, path);
1229 if (err) {
1230 char *parent;
1231 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1232 goto done;
1233 err = got_path_dirname(&parent, path);
1234 if (err)
1235 goto done;
1236 err = got_path_mkdir(parent);
1237 free(parent);
1238 if (err)
1239 goto done;
1240 err = got_opentemp_named(&tmppath, &f, path);
1241 if (err)
1242 goto done;
1245 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1246 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1247 if (n != strlen(ref->ref.symref.ref) + 6) {
1248 err = got_ferror(f, GOT_ERR_IO);
1249 goto done;
1251 } else {
1252 char hex[SHA1_DIGEST_STRING_LENGTH];
1253 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
1254 sizeof(hex)) == NULL) {
1255 err = got_error(GOT_ERR_BAD_REF_DATA);
1256 goto done;
1258 n = fprintf(f, "%s\n", hex);
1259 if (n != sizeof(hex)) {
1260 err = got_ferror(f, GOT_ERR_IO);
1261 goto done;
1265 if (ref->lf == NULL) {
1266 err = got_lockfile_lock(&lf, path, -1);
1267 if (err)
1268 goto done;
1271 /* XXX: check if old content matches our expectations? */
1273 if (stat(path, &sb) != 0) {
1274 if (errno != ENOENT) {
1275 err = got_error_from_errno2("stat", path);
1276 goto done;
1278 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1281 if (fchmod(fileno(f), sb.st_mode) != 0) {
1282 err = got_error_from_errno2("fchmod", tmppath);
1283 goto done;
1286 if (rename(tmppath, path) != 0) {
1287 err = got_error_from_errno3("rename", tmppath, path);
1288 goto done;
1290 free(tmppath);
1291 tmppath = NULL;
1293 if (stat(path, &sb) == -1) {
1294 err = got_error_from_errno2("stat", path);
1295 goto done;
1297 ref->mtime = sb.st_mtime;
1298 done:
1299 if (ref->lf == NULL && lf)
1300 unlock_err = got_lockfile_unlock(lf, -1);
1301 if (f) {
1302 if (fclose(f) == EOF && err == NULL)
1303 err = got_error_from_errno("fclose");
1305 free(path_refs);
1306 free(path);
1307 if (tmppath) {
1308 if (unlink(tmppath) != 0 && err == NULL)
1309 err = got_error_from_errno2("unlink", tmppath);
1310 free(tmppath);
1312 return err ? err : unlock_err;
1315 static const struct got_error *
1316 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1318 const struct got_error *err = NULL, *unlock_err = NULL;
1319 struct got_lockfile *lf = NULL;
1320 FILE *f = NULL, *tmpf = NULL;
1321 char *line = NULL, *packed_refs_path, *tmppath = NULL;
1322 size_t linesize = 0;
1323 struct got_reflist_head refs;
1324 int found_delref = 0;
1326 /* The packed-refs file does not cotain symbolic references. */
1327 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1328 return got_error(GOT_ERR_BAD_REF_DATA);
1330 TAILQ_INIT(&refs);
1332 packed_refs_path = got_repo_get_path_packed_refs(repo);
1333 if (packed_refs_path == NULL)
1334 return got_error_from_errno("got_repo_get_path_packed_refs");
1336 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
1337 if (err)
1338 goto done;
1340 if (delref->lf == NULL) {
1341 err = got_lockfile_lock(&lf, packed_refs_path, -1);
1342 if (err)
1343 goto done;
1346 f = fopen(packed_refs_path, "re");
1347 if (f == NULL) {
1348 err = got_error_from_errno2("fopen", packed_refs_path);
1349 goto done;
1351 for (;;) {
1352 ssize_t linelen;
1353 struct got_reference *ref;
1354 struct got_reflist_entry *new;
1356 linelen = getline(&line, &linesize, f);
1357 if (linelen == -1) {
1358 if (feof(f))
1359 break;
1360 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1361 goto done;
1363 if (linelen > 0 && line[linelen - 1] == '\n')
1364 line[linelen - 1] = '\0';
1365 err = parse_packed_ref_line(&ref, NULL, line, 0);
1366 if (err)
1367 goto done;
1368 if (ref == NULL)
1369 continue;
1371 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1372 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1373 sizeof(delref->ref.ref.sha1)) == 0) {
1374 found_delref = 1;
1375 got_ref_close(ref);
1376 continue;
1379 err = got_reflist_insert(&new, &refs, ref,
1380 got_ref_cmp_by_name, NULL);
1381 if (err || new == NULL /* duplicate */)
1382 got_ref_close(ref);
1383 if (err)
1384 goto done;
1387 if (found_delref) {
1388 struct got_reflist_entry *re;
1389 size_t n;
1390 struct stat sb;
1392 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1393 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1394 err = got_ferror(f, GOT_ERR_IO);
1395 goto done;
1398 TAILQ_FOREACH(re, &refs, entry) {
1399 char hex[SHA1_DIGEST_STRING_LENGTH];
1401 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1402 sizeof(hex)) == NULL) {
1403 err = got_error(GOT_ERR_BAD_REF_DATA);
1404 goto done;
1406 n = fprintf(tmpf, "%s ", hex);
1407 if (n != sizeof(hex)) {
1408 err = got_ferror(f, GOT_ERR_IO);
1409 goto done;
1411 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1412 if (n != strlen(re->ref->ref.ref.name) + 1) {
1413 err = got_ferror(f, GOT_ERR_IO);
1414 goto done;
1418 if (fflush(tmpf) != 0) {
1419 err = got_error_from_errno("fflush");
1420 goto done;
1423 if (fstat(fileno(f), &sb) != 0) {
1424 if (errno != ENOENT) {
1425 err = got_error_from_errno2("fstat",
1426 packed_refs_path);
1427 goto done;
1429 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1432 if (fchmod(fileno(tmpf), sb.st_mode) != 0) {
1433 err = got_error_from_errno2("fchmod", tmppath);
1434 goto done;
1437 if (rename(tmppath, packed_refs_path) != 0) {
1438 err = got_error_from_errno3("rename", tmppath,
1439 packed_refs_path);
1440 goto done;
1443 done:
1444 if (delref->lf == NULL && lf)
1445 unlock_err = got_lockfile_unlock(lf, -1);
1446 if (f) {
1447 if (fclose(f) == EOF && err == NULL)
1448 err = got_error_from_errno("fclose");
1450 if (tmpf) {
1451 unlink(tmppath);
1452 if (fclose(tmpf) == EOF && err == NULL)
1453 err = got_error_from_errno("fclose");
1455 free(tmppath);
1456 free(packed_refs_path);
1457 free(line);
1458 got_ref_list_free(&refs);
1459 return err ? err : unlock_err;
1462 static const struct got_error *
1463 delete_loose_ref(struct got_reference *ref, struct got_repository *repo)
1465 const struct got_error *err = NULL, *unlock_err = NULL;
1466 const char *name = got_ref_get_name(ref);
1467 char *path_refs = NULL, *path = NULL;
1468 struct got_lockfile *lf = NULL;
1470 path_refs = get_refs_dir_path(repo, name);
1471 if (path_refs == NULL) {
1472 err = got_error_from_errno2("get_refs_dir_path", name);
1473 goto done;
1476 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1477 err = got_error_from_errno("asprintf");
1478 goto done;
1481 if (ref->lf == NULL) {
1482 err = got_lockfile_lock(&lf, path, -1);
1483 if (err)
1484 goto done;
1487 /* XXX: check if old content matches our expectations? */
1489 if (unlink(path) != 0)
1490 err = got_error_from_errno2("unlink", path);
1491 done:
1492 if (ref->lf == NULL && lf)
1493 unlock_err = got_lockfile_unlock(lf, -1);
1495 free(path_refs);
1496 free(path);
1497 return err ? err : unlock_err;
1500 const struct got_error *
1501 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1503 const struct got_error *err = NULL;
1504 struct got_reference *ref2;
1506 if (ref->flags & GOT_REF_IS_PACKED) {
1507 err = delete_packed_ref(ref, repo);
1508 if (err)
1509 return err;
1511 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1512 if (err) {
1513 if (err->code == GOT_ERR_NOT_REF)
1514 return NULL;
1515 return err;
1518 err = delete_loose_ref(ref2, repo);
1519 got_ref_close(ref2);
1520 return err;
1521 } else {
1522 err = delete_loose_ref(ref, repo);
1523 if (err)
1524 return err;
1526 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1527 if (err) {
1528 if (err->code == GOT_ERR_NOT_REF)
1529 return NULL;
1530 return err;
1533 err = delete_packed_ref(ref2, repo);
1534 got_ref_close(ref2);
1535 return err;
1539 const struct got_error *
1540 got_ref_unlock(struct got_reference *ref)
1542 const struct got_error *err;
1543 err = got_lockfile_unlock(ref->lf, -1);
1544 ref->lf = NULL;
1545 return err;
1548 struct got_reflist_object_id_map {
1549 struct got_object_idset *idset;
1552 struct got_reflist_object_id_map_entry {
1553 struct got_reflist_head refs;
1556 static const struct got_error *
1557 add_object_id_map_entry(struct got_object_idset *idset,
1558 struct got_object_id *id, struct got_reflist_entry *re)
1560 const struct got_error *err = NULL;
1561 struct got_reflist_object_id_map_entry *ent;
1562 struct got_reflist_entry *new;
1564 ent = got_object_idset_get(idset, id);
1565 if (ent == NULL) {
1566 ent = malloc(sizeof(*ent));
1567 if (ent == NULL)
1568 return got_error_from_errno("malloc");
1570 TAILQ_INIT(&ent->refs);
1571 err = got_object_idset_add(idset, id, ent);
1572 if (err) {
1573 free(ent);
1574 return err;
1578 err = got_reflist_entry_dup(&new, re);
1579 if (err)
1580 return err;
1582 TAILQ_INSERT_TAIL(&ent->refs, new, entry);
1583 return NULL;
1586 const struct got_error *
1587 got_reflist_object_id_map_create(struct got_reflist_object_id_map **map,
1588 struct got_reflist_head *refs, struct got_repository *repo)
1590 const struct got_error *err = NULL;
1591 struct got_object_idset *idset;
1592 struct got_object_id *id = NULL;
1593 struct got_reflist_entry *re;
1595 idset = got_object_idset_alloc();
1596 if (idset == NULL)
1597 return got_error_from_errno("got_object_idset_alloc");
1599 *map = malloc(sizeof(**map));
1600 if (*map == NULL) {
1601 got_object_idset_free(idset);
1602 return got_error_from_errno("malloc");
1604 (*map)->idset = idset;
1606 TAILQ_FOREACH(re, refs, entry) {
1607 struct got_tag_object *tag = NULL;
1609 err = got_ref_resolve(&id, repo, re->ref);
1610 if (err)
1611 goto done;
1613 err = add_object_id_map_entry(idset, id, re);
1614 if (err)
1615 goto done;
1617 if (strstr(got_ref_get_name(re->ref), "/tags/") == NULL) {
1618 free(id);
1619 id = NULL;
1620 continue;
1623 err = got_object_open_as_tag(&tag, repo, id);
1624 if (err) {
1625 if (err->code != GOT_ERR_OBJ_TYPE)
1626 goto done;
1627 /* Ref points at something other than a tag. */
1628 err = NULL;
1629 tag = NULL;
1630 free(id);
1631 id = NULL;
1632 continue;
1635 err = add_object_id_map_entry(idset,
1636 got_object_tag_get_object_id(tag), re);
1637 got_object_tag_close(tag);
1638 if (err)
1639 goto done;
1641 free(id);
1642 id = NULL;
1644 done:
1645 free(id);
1646 if (err) {
1647 got_reflist_object_id_map_free(*map);
1648 *map = NULL;
1650 return err;
1653 struct got_reflist_head *
1654 got_reflist_object_id_map_lookup(struct got_reflist_object_id_map *map,
1655 struct got_object_id *id)
1657 struct got_reflist_object_id_map_entry *ent;
1658 ent = got_object_idset_get(map->idset, id);
1659 if (ent)
1660 return &ent->refs;
1661 return NULL;
1664 static const struct got_error *
1665 free_id_map_entry(struct got_object_id *id, void *data, void *arg)
1667 struct got_reflist_object_id_map_entry *ent = data;
1669 got_ref_list_free(&ent->refs);
1670 free(ent);
1671 return NULL;
1674 void
1675 got_reflist_object_id_map_free(struct got_reflist_object_id_map *map)
1677 got_object_idset_for_each(map->idset, free_id_map_entry, NULL);
1678 got_object_idset_free(map->idset);
1679 free(map);