Blob


1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/stat.h>
20 #include <errno.h>
21 #include <ctype.h>
22 #include <dirent.h>
23 #include <limits.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <zlib.h>
29 #include <time.h>
30 #include <libgen.h>
32 #include "got_compat.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_sha1.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"
56 /*
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.
60 */
61 #define GOT_PACKED_REFS_HEADER "# pack-refs with:"
63 /* A symbolic reference. */
64 struct got_symref {
65 char *name;
66 char *ref;
67 };
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 u_int8_t sha1[SHA1_DIGEST_LENGTH];
75 };
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;
91 /* Cached timestamp for got_ref_cmp_by_commit_timestamp_descending() */
92 time_t committer_time;
93 };
95 static const struct got_error *
96 alloc_ref(struct got_reference **ref, const char *name,
97 struct got_object_id *id, int flags, time_t mtime)
98 {
99 const struct got_error *err = NULL;
101 *ref = calloc(1, sizeof(**ref));
102 if (*ref == NULL)
103 return got_error_from_errno("calloc");
105 memcpy((*ref)->ref.ref.sha1, id->sha1, sizeof((*ref)->ref.ref.sha1));
106 (*ref)->flags = flags;
107 (*ref)->ref.ref.name = strdup(name);
108 (*ref)->mtime = mtime;
109 if ((*ref)->ref.ref.name == NULL) {
110 err = got_error_from_errno("strdup");
111 got_ref_close(*ref);
112 *ref = NULL;
114 return err;
117 static const struct got_error *
118 alloc_symref(struct got_reference **ref, const char *name,
119 const char *target_ref, int flags)
121 const struct got_error *err = NULL;
123 *ref = calloc(1, sizeof(**ref));
124 if (*ref == NULL)
125 return got_error_from_errno("calloc");
127 (*ref)->flags = GOT_REF_IS_SYMBOLIC | flags;
128 (*ref)->ref.symref.name = strdup(name);
129 if ((*ref)->ref.symref.name == NULL) {
130 err = got_error_from_errno("strdup");
131 got_ref_close(*ref);
132 *ref = NULL;
133 return err;
135 (*ref)->ref.symref.ref = strdup(target_ref);
136 if ((*ref)->ref.symref.ref == NULL) {
137 err = got_error_from_errno("strdup");
138 got_ref_close(*ref);
139 *ref = NULL;
141 return err;
144 static const struct got_error *
145 parse_symref(struct got_reference **ref, const char *name, const char *line)
147 if (line[0] == '\0')
148 return got_error(GOT_ERR_BAD_REF_DATA);
150 return alloc_symref(ref, name, line, 0);
153 static const struct got_error *
154 parse_ref_line(struct got_reference **ref, const char *name, const char *line,
155 time_t mtime)
157 struct got_object_id id;
159 if (strncmp(line, "ref: ", 5) == 0) {
160 line += 5;
161 return parse_symref(ref, name, line);
164 if (!got_parse_sha1_digest(id.sha1, line))
165 return got_error(GOT_ERR_BAD_REF_DATA);
167 return alloc_ref(ref, name, &id, 0, mtime);
170 static const struct got_error *
171 parse_ref_file(struct got_reference **ref, const char *name,
172 const char *absname, const char *abspath, int lock)
174 const struct got_error *err = NULL;
175 FILE *f;
176 char *line = NULL;
177 size_t linesize = 0;
178 ssize_t linelen;
179 struct got_lockfile *lf = NULL;
180 struct stat sb;
182 if (lock) {
183 err = got_lockfile_lock(&lf, abspath, -1);
184 if (err) {
185 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
186 err = got_error_not_ref(name);
187 return err;
191 f = fopen(abspath, "rb");
192 if (f == NULL) {
193 if (errno != ENOTDIR && errno != ENOENT)
194 err = got_error_from_errno2("fopen", abspath);
195 else
196 err = got_error_not_ref(name);
197 if (lock)
198 got_lockfile_unlock(lf, -1);
199 return err;
201 if (fstat(fileno(f), &sb) == -1) {
202 err = got_error_from_errno2("fstat", abspath);
203 goto done;
206 linelen = getline(&line, &linesize, f);
207 if (linelen == -1) {
208 if (feof(f))
209 err = NULL; /* ignore empty files (could be locks) */
210 else {
211 if (errno == EISDIR)
212 err = got_error(GOT_ERR_NOT_REF);
213 else if (ferror(f))
214 err = got_ferror(f, GOT_ERR_IO);
215 else
216 err = got_error_from_errno2("getline", abspath);
218 if (lock)
219 got_lockfile_unlock(lf, -1);
220 goto done;
222 while (linelen > 0 && line[linelen - 1] == '\n') {
223 line[linelen - 1] = '\0';
224 linelen--;
227 err = parse_ref_line(ref, absname, line, sb.st_mtime);
228 if (lock) {
229 if (err)
230 got_lockfile_unlock(lf, -1);
231 else {
232 if (*ref)
233 (*ref)->lf = lf;
234 else
235 got_lockfile_unlock(lf, -1);
238 done:
239 free(line);
240 if (fclose(f) == EOF && err == NULL) {
241 err = got_error_from_errno("fclose");
242 if (*ref) {
243 if (lock)
244 got_ref_unlock(*ref);
245 got_ref_close(*ref);
246 *ref = NULL;
249 return err;
252 static int
253 is_well_known_ref(const char *refname)
255 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
256 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
257 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
258 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
261 static char *
262 get_refs_dir_path(struct got_repository *repo, const char *refname)
264 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
265 return strdup(got_repo_get_path_git_dir(repo));
267 return got_repo_get_path_refs(repo);
270 int
271 got_ref_name_is_valid(const char *name)
273 const char *s, *seg;
274 const char forbidden[] = { ' ', '~', '^', ':', '?', '*', '[' , '\\' };
275 const char *forbidden_seq[] = { "//", "..", "@{" };
276 const char *lfs = GOT_LOCKFILE_SUFFIX;
277 const size_t lfs_len = sizeof(GOT_LOCKFILE_SUFFIX) - 1;
278 size_t i;
280 if (name[0] == '@' && name[1] == '\0')
281 return 0;
283 s = name;
284 seg = s;
285 if (seg[0] == '\0' || seg[0] == '.' || seg[0] == '/')
286 return 0;
287 while (*s) {
288 for (i = 0; i < nitems(forbidden); i++) {
289 if (*s == forbidden[i])
290 return 0;
292 for (i = 0; i < nitems(forbidden_seq); i++) {
293 if (s[0] == forbidden_seq[i][0] &&
294 s[1] == forbidden_seq[i][1])
295 return 0;
297 if (iscntrl((unsigned char)s[0]))
298 return 0;
299 if (s[0] == '.' && s[1] == '\0')
300 return 0;
301 if (*s == '/') {
302 const char *nextseg = s + 1;
303 if (nextseg[0] == '\0' || nextseg[0] == '.' ||
304 nextseg[0] == '/')
305 return 0;
306 if (seg <= s - lfs_len &&
307 strncmp(s - lfs_len, lfs, lfs_len) == 0)
308 return 0;
309 seg = nextseg;
311 s++;
314 if (seg <= s - lfs_len &&
315 strncmp(s - lfs_len, lfs, lfs_len) == 0)
316 return 0;
318 return 1;
321 const struct got_error *
322 got_ref_alloc(struct got_reference **ref, const char *name,
323 struct got_object_id *id)
325 if (!got_ref_name_is_valid(name))
326 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
328 return alloc_ref(ref, name, id, 0, 0);
331 const struct got_error *
332 got_ref_alloc_symref(struct got_reference **ref, const char *name,
333 struct got_reference *target_ref)
335 if (!got_ref_name_is_valid(name))
336 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
338 return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
341 static const struct got_error *
342 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
343 const char *line, time_t mtime)
345 struct got_object_id id;
346 const char *name;
348 *ref = NULL;
350 if (line[0] == '#' || line[0] == '^')
351 return NULL;
353 if (!got_parse_sha1_digest(id.sha1, line))
354 return got_error(GOT_ERR_BAD_REF_DATA);
356 if (abs_refname) {
357 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
358 return NULL;
359 name = abs_refname;
360 } else
361 name = line + SHA1_DIGEST_STRING_LENGTH;
363 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED, mtime);
366 static const struct got_error *
367 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
368 int nsubdirs, const char *refname, time_t mtime)
370 const struct got_error *err = NULL;
371 char *abs_refname;
372 char *line = NULL;
373 size_t linesize = 0;
374 ssize_t linelen;
375 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
377 *ref = NULL;
379 if (ref_is_absolute)
380 abs_refname = (char *)refname;
381 do {
382 linelen = getline(&line, &linesize, f);
383 if (linelen == -1) {
384 if (feof(f))
385 break;
386 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
387 break;
389 if (linelen > 0 && line[linelen - 1] == '\n')
390 line[linelen - 1] = '\0';
391 for (i = 0; i < nsubdirs; i++) {
392 if (!ref_is_absolute &&
393 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
394 refname) == -1)
395 return got_error_from_errno("asprintf");
396 err = parse_packed_ref_line(ref, abs_refname, line,
397 mtime);
398 if (!ref_is_absolute)
399 free(abs_refname);
400 if (err || *ref != NULL)
401 break;
403 if (err)
404 break;
405 } while (*ref == NULL);
406 free(line);
408 return err;
411 static const struct got_error *
412 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
413 const char *name, int lock)
415 const struct got_error *err = NULL;
416 char *path = NULL;
417 char *absname = NULL;
418 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
419 int ref_is_well_known = (subdir[0] == '\0' && is_well_known_ref(name));
421 *ref = NULL;
423 if (!got_ref_name_is_valid(name))
424 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
426 if (ref_is_absolute || ref_is_well_known) {
427 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
428 return got_error_from_errno("asprintf");
429 absname = (char *)name;
430 } else {
431 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
432 subdir[0] ? "/" : "", name) == -1)
433 return got_error_from_errno("asprintf");
435 if (asprintf(&absname, "refs/%s%s%s",
436 subdir, subdir[0] ? "/" : "", name) == -1) {
437 err = got_error_from_errno("asprintf");
438 goto done;
442 err = parse_ref_file(ref, name, absname, path, lock);
443 done:
444 if (!ref_is_absolute && !ref_is_well_known)
445 free(absname);
446 free(path);
447 return err;
450 const struct got_error *
451 got_ref_open(struct got_reference **ref, struct got_repository *repo,
452 const char *refname, int lock)
454 const struct got_error *err = NULL;
455 char *path_refs = NULL;
456 const char *subdirs[] = {
457 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
458 };
459 size_t i;
460 int well_known = is_well_known_ref(refname);
461 struct got_lockfile *lf = NULL;
463 *ref = NULL;
465 path_refs = get_refs_dir_path(repo, refname);
466 if (path_refs == NULL) {
467 err = got_error_from_errno2("get_refs_dir_path", refname);
468 goto done;
471 if (well_known) {
472 err = open_ref(ref, path_refs, "", refname, lock);
473 } else {
474 char *packed_refs_path;
475 FILE *f;
477 /* Search on-disk refs before packed refs! */
478 for (i = 0; i < nitems(subdirs); i++) {
479 err = open_ref(ref, path_refs, subdirs[i], refname,
480 lock);
481 if ((err && err->code != GOT_ERR_NOT_REF) || *ref)
482 goto done;
485 packed_refs_path = got_repo_get_path_packed_refs(repo);
486 if (packed_refs_path == NULL) {
487 err = got_error_from_errno(
488 "got_repo_get_path_packed_refs");
489 goto done;
492 if (lock) {
493 err = got_lockfile_lock(&lf, packed_refs_path, -1);
494 if (err)
495 goto done;
497 f = fopen(packed_refs_path, "rb");
498 free(packed_refs_path);
499 if (f != NULL) {
500 struct stat sb;
501 if (fstat(fileno(f), &sb) == -1) {
502 err = got_error_from_errno2("fstat",
503 packed_refs_path);
504 goto done;
506 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
507 refname, sb.st_mtime);
508 if (!err) {
509 if (fclose(f) == EOF) {
510 err = got_error_from_errno("fclose");
511 got_ref_close(*ref);
512 *ref = NULL;
513 } else if (*ref)
514 (*ref)->lf = lf;
518 done:
519 if (!err && *ref == NULL)
520 err = got_error_not_ref(refname);
521 if (err && lf)
522 got_lockfile_unlock(lf, -1);
523 free(path_refs);
524 return err;
527 void
528 got_ref_close(struct got_reference *ref)
530 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
531 free(ref->ref.symref.name);
532 free(ref->ref.symref.ref);
533 } else
534 free(ref->ref.ref.name);
535 free(ref);
538 struct got_reference *
539 got_ref_dup(struct got_reference *ref)
541 struct got_reference *ret;
543 ret = calloc(1, sizeof(*ret));
544 if (ret == NULL)
545 return NULL;
547 ret->flags = ref->flags;
548 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
549 ret->ref.symref.name = strdup(ref->ref.symref.name);
550 if (ret->ref.symref.name == NULL) {
551 free(ret);
552 return NULL;
554 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
555 if (ret->ref.symref.ref == NULL) {
556 free(ret->ref.symref.name);
557 free(ret);
558 return NULL;
560 } else {
561 ret->ref.ref.name = strdup(ref->ref.ref.name);
562 if (ret->ref.ref.name == NULL) {
563 free(ret);
564 return NULL;
566 memcpy(ret->ref.ref.sha1, ref->ref.ref.sha1,
567 sizeof(ret->ref.ref.sha1));
570 return ret;
573 const struct got_error *
574 got_reflist_entry_dup(struct got_reflist_entry **newp,
575 struct got_reflist_entry *re)
577 const struct got_error *err = NULL;
578 struct got_reflist_entry *new;
580 *newp = NULL;
582 new = malloc(sizeof(*new));
583 if (new == NULL)
584 return got_error_from_errno("malloc");
586 new->ref = got_ref_dup(re->ref);
587 if (new->ref == NULL) {
588 err = got_error_from_errno("got_ref_dup");
589 free(new);
590 return err;
593 *newp = new;
594 return NULL;
597 const struct got_error *
598 got_ref_resolve_symbolic(struct got_reference **resolved,
599 struct got_repository *repo, struct got_reference *ref)
601 struct got_reference *nextref;
602 const struct got_error *err;
604 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
605 if (err)
606 return err;
608 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
609 err = got_ref_resolve_symbolic(resolved, repo, nextref);
610 else
611 *resolved = got_ref_dup(nextref);
613 got_ref_close(nextref);
614 return err;
617 static const struct got_error *
618 ref_resolve(struct got_object_id **id, struct got_repository *repo,
619 struct got_reference *ref, int recursion)
621 const struct got_error *err;
623 if (recursion <= 0)
624 return got_error_msg(GOT_ERR_RECURSION,
625 "reference recursion limit reached");
627 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
628 struct got_reference *resolved = NULL;
629 err = got_ref_resolve_symbolic(&resolved, repo, ref);
630 if (err == NULL)
631 err = ref_resolve(id, repo, resolved, --recursion);
632 if (resolved)
633 got_ref_close(resolved);
634 return err;
637 *id = calloc(1, sizeof(**id));
638 if (*id == NULL)
639 return got_error_from_errno("calloc");
640 memcpy((*id)->sha1, ref->ref.ref.sha1, sizeof((*id)->sha1));
641 return NULL;
644 const struct got_error *
645 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
646 struct got_reference *ref)
648 return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
651 char *
652 got_ref_to_str(struct got_reference *ref)
654 char *str;
656 if (ref->flags & GOT_REF_IS_SYMBOLIC)
657 return strdup(ref->ref.symref.ref);
659 str = malloc(SHA1_DIGEST_STRING_LENGTH);
660 if (str == NULL)
661 return NULL;
663 if (got_sha1_digest_to_str(ref->ref.ref.sha1, str,
664 SHA1_DIGEST_STRING_LENGTH) == NULL) {
665 free(str);
666 return NULL;
669 return str;
672 const char *
673 got_ref_get_name(struct got_reference *ref)
675 if (ref->flags & GOT_REF_IS_SYMBOLIC)
676 return ref->ref.symref.name;
678 return ref->ref.ref.name;
681 const char *
682 got_ref_get_symref_target(struct got_reference *ref)
684 if (ref->flags & GOT_REF_IS_SYMBOLIC)
685 return ref->ref.symref.ref;
687 return NULL;
690 time_t
691 got_ref_get_mtime(struct got_reference *ref)
693 return ref->mtime;
696 const struct got_error *
697 got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
698 struct got_reference* re2)
700 const char *name1 = got_ref_get_name(re1);
701 const char *name2 = got_ref_get_name(re2);
703 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
704 return NULL;
707 const struct got_error *
708 got_ref_cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
709 struct got_reference *ref2)
711 const struct got_error *err = NULL;
712 struct got_repository *repo = arg;
713 struct got_object_id *id1, *id2 = NULL;
714 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
715 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
716 time_t time1, time2;
718 *cmp = 0;
720 err = got_ref_resolve(&id1, repo, ref1);
721 if (err)
722 return err;
723 err = got_object_open_as_tag(&tag1, repo, id1);
724 if (err) {
725 if (err->code != GOT_ERR_OBJ_TYPE)
726 goto done;
727 /* "lightweight" tag */
728 err = got_object_open_as_commit(&commit1, repo, id1);
729 if (err)
730 goto done;
731 time1 = got_object_commit_get_committer_time(commit1);
732 } else
733 time1 = got_object_tag_get_tagger_time(tag1);
735 err = got_ref_resolve(&id2, repo, ref2);
736 if (err)
737 goto done;
738 err = got_object_open_as_tag(&tag2, repo, id2);
739 if (err) {
740 if (err->code != GOT_ERR_OBJ_TYPE)
741 goto done;
742 /* "lightweight" tag */
743 err = got_object_open_as_commit(&commit2, repo, id2);
744 if (err)
745 goto done;
746 time2 = got_object_commit_get_committer_time(commit2);
747 } else
748 time2 = got_object_tag_get_tagger_time(tag2);
750 /* Put latest tags first. */
751 if (time1 < time2)
752 *cmp = 1;
753 else if (time1 > time2)
754 *cmp = -1;
755 else
756 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
757 done:
758 free(id1);
759 free(id2);
760 if (tag1)
761 got_object_tag_close(tag1);
762 if (tag2)
763 got_object_tag_close(tag2);
764 if (commit1)
765 got_object_commit_close(commit1);
766 if (commit2)
767 got_object_commit_close(commit2);
768 return err;
771 static const struct got_error *
772 get_committer_time(struct got_reference *ref, struct got_repository *repo)
774 const struct got_error *err = NULL;
775 int obj_type;
776 struct got_commit_object *commit = NULL;
777 struct got_tag_object *tag = NULL;
778 struct got_object_id *id = NULL;
780 err = got_ref_resolve(&id, repo, ref);
781 if (err)
782 return err;
784 err = got_object_get_type(&obj_type, repo, id);
785 if (err)
786 goto done;
788 switch (obj_type) {
789 case GOT_OBJ_TYPE_COMMIT:
790 err = got_object_open_as_commit(&commit, repo, id);
791 if (err)
792 goto done;
793 ref->committer_time =
794 got_object_commit_get_committer_time(commit);
795 break;
796 case GOT_OBJ_TYPE_TAG:
797 err = got_object_open_as_tag(&tag, repo, id);
798 if (err)
799 goto done;
800 ref->committer_time = got_object_tag_get_tagger_time(tag);
801 break;
802 default:
803 /* best effort for other object types */
804 ref->committer_time = got_ref_get_mtime(ref);
805 break;
807 done:
808 free(id);
809 if (commit)
810 got_object_commit_close(commit);
811 if (tag)
812 got_object_tag_close(tag);
813 return err;
816 const struct got_error *
817 got_ref_cmp_by_commit_timestamp_descending(void *arg, int *cmp,
818 struct got_reference *ref1, struct got_reference *ref2)
820 const struct got_error *err = NULL;
821 struct got_repository *repo = arg;
823 *cmp = 0;
825 if (ref1->committer_time == 0) {
826 err = get_committer_time(ref1, repo);
827 if (err)
828 return err;
830 if (ref2->committer_time == 0) {
831 err = get_committer_time(ref2, repo);
832 if (err)
833 return err;
836 if (ref1->committer_time < ref2->committer_time)
837 *cmp = 1;
838 else if (ref2->committer_time < ref1->committer_time)
839 *cmp = -1;
840 else
841 return got_ref_cmp_by_name(arg, cmp, ref1, ref2);
843 return err;
846 const struct got_error *
847 got_reflist_insert(struct got_reflist_entry **newp, struct got_reflist_head *refs,
848 struct got_reference *ref, got_ref_cmp_cb cmp_cb, void *cmp_arg)
850 const struct got_error *err;
851 struct got_reflist_entry *new, *re;
852 int cmp;
854 *newp = NULL;
856 new = malloc(sizeof(*new));
857 if (new == NULL)
858 return got_error_from_errno("malloc");
859 new->ref = ref;
860 *newp = new;
862 /*
863 * We must de-duplicate entries on insert because packed-refs may
864 * contain redundant entries. On-disk refs take precedence.
865 * This code assumes that on-disk revs are read before packed-refs.
866 * We're iterating the list anyway, so insert elements sorted by name.
868 * Many callers will provide paths in a somewhat sorted order.
869 * Iterating backwards from the tail of the list should be more
870 * efficient than traversing through the entire list each time
871 * an element is inserted.
872 */
873 re = TAILQ_LAST(refs, got_reflist_head);
874 while (re) {
875 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
876 if (err)
877 return err;
878 if (cmp == 0) {
879 /* duplicate */
880 free(new);
881 *newp = NULL;
882 return NULL;
883 } else if (cmp < 0) {
884 TAILQ_INSERT_AFTER(refs, re, new, entry);
885 return NULL;
887 re = TAILQ_PREV(re, got_reflist_head, entry);
890 TAILQ_INSERT_HEAD(refs, new, entry);
891 return NULL;
894 const struct got_error *
895 got_reflist_sort(struct got_reflist_head *refs,
896 got_ref_cmp_cb cmp_cb, void *cmp_arg)
898 const struct got_error *err = NULL;
899 struct got_reflist_entry *re, *tmp, *new;
900 struct got_reflist_head sorted;
902 TAILQ_INIT(&sorted);
904 TAILQ_FOREACH_SAFE(re, refs, entry, tmp) {
905 struct got_reference *ref = re->ref;
906 TAILQ_REMOVE(refs, re, entry);
907 free(re);
908 err = got_reflist_insert(&new, &sorted, ref, cmp_cb, cmp_arg);
909 if (err || new == NULL /* duplicate */)
910 got_ref_close(ref);
911 if (err)
912 return err;
915 TAILQ_CONCAT(refs, &sorted, entry);
916 return NULL;
919 static const struct got_error *
920 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
921 const char *subdir, struct got_repository *repo,
922 got_ref_cmp_cb cmp_cb, void *cmp_arg)
924 const struct got_error *err = NULL;
925 DIR *d = NULL;
926 char *path_subdir;
928 while (subdir[0] == '/')
929 subdir++;
931 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
932 return got_error_from_errno("asprintf");
934 d = opendir(path_subdir);
935 if (d == NULL)
936 goto done;
938 for (;;) {
939 struct dirent *dent;
940 struct got_reference *ref;
941 char *child;
942 int type;
944 dent = readdir(d);
945 if (dent == NULL)
946 break;
948 if (strcmp(dent->d_name, ".") == 0 ||
949 strcmp(dent->d_name, "..") == 0)
950 continue;
952 err = got_path_dirent_type(&type, path_subdir, dent);
953 if (err)
954 break;
956 switch (type) {
957 case DT_REG:
958 err = open_ref(&ref, path_refs, subdir, dent->d_name,
959 0);
960 if (err)
961 goto done;
962 if (ref) {
963 struct got_reflist_entry *new;
964 err = got_reflist_insert(&new, refs, ref,
965 cmp_cb, cmp_arg);
966 if (err || new == NULL /* duplicate */)
967 got_ref_close(ref);
968 if (err)
969 goto done;
971 break;
972 case DT_DIR:
973 if (asprintf(&child, "%s%s%s", subdir,
974 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
975 err = got_error_from_errno("asprintf");
976 break;
978 err = gather_on_disk_refs(refs, path_refs, child, repo,
979 cmp_cb, cmp_arg);
980 free(child);
981 break;
982 default:
983 break;
986 done:
987 if (d)
988 closedir(d);
989 free(path_subdir);
990 return err;
993 const struct got_error *
994 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
995 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
997 const struct got_error *err;
998 char *packed_refs_path, *path_refs = NULL;
999 char *abs_namespace = NULL;
1000 char *buf = NULL, *ondisk_ref_namespace = NULL;
1001 char *line = NULL;
1002 FILE *f = NULL;
1003 struct got_reference *ref;
1004 struct got_reflist_entry *new;
1006 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
1007 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
1008 if (path_refs == NULL) {
1009 err = got_error_from_errno("get_refs_dir_path");
1010 goto done;
1012 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
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 const char *refname = ref_namespace;
1023 path_refs = get_refs_dir_path(repo, refname);
1024 if (path_refs == NULL) {
1025 err = got_error_from_errno("get_refs_dir_path");
1026 goto done;
1028 err = open_ref(&ref, path_refs, "", refname, 0);
1029 if (err) {
1030 if (err->code != GOT_ERR_NOT_REF)
1031 goto done;
1032 /* Try to look up references in a given namespace. */
1033 } else {
1034 err = got_reflist_insert(&new, refs, ref,
1035 cmp_cb, cmp_arg);
1036 if (err || new == NULL /* duplicate */)
1037 got_ref_close(ref);
1038 return err;
1042 if (ref_namespace) {
1043 size_t len;
1044 /* Canonicalize the path to eliminate double-slashes if any. */
1045 if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
1046 err = got_error_from_errno("asprintf");
1047 goto done;
1049 len = strlen(abs_namespace) + 1;
1050 buf = malloc(len);
1051 if (buf == NULL) {
1052 err = got_error_from_errno("malloc");
1053 goto done;
1055 err = got_canonpath(abs_namespace, buf, len);
1056 if (err)
1057 goto done;
1058 ondisk_ref_namespace = buf;
1059 while (ondisk_ref_namespace[0] == '/')
1060 ondisk_ref_namespace++;
1061 if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
1062 ondisk_ref_namespace += 5;
1063 else if (strcmp(ondisk_ref_namespace, "refs") == 0)
1064 ondisk_ref_namespace = "";
1067 /* Gather on-disk refs before parsing packed-refs. */
1068 free(path_refs);
1069 path_refs = get_refs_dir_path(repo, "");
1070 if (path_refs == NULL) {
1071 err = got_error_from_errno("get_refs_dir_path");
1072 goto done;
1074 err = gather_on_disk_refs(refs, path_refs,
1075 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
1076 cmp_cb, cmp_arg);
1077 if (err)
1078 goto done;
1081 * The packed-refs file may contain redundant entries, in which
1082 * case on-disk refs take precedence.
1084 packed_refs_path = got_repo_get_path_packed_refs(repo);
1085 if (packed_refs_path == NULL) {
1086 err = got_error_from_errno("got_repo_get_path_packed_refs");
1087 goto done;
1090 f = fopen(packed_refs_path, "r");
1091 free(packed_refs_path);
1092 if (f) {
1093 size_t linesize = 0;
1094 ssize_t linelen;
1095 struct stat sb;
1097 if (fstat(fileno(f), &sb) == -1) {
1098 err = got_error_from_errno2("fstat", packed_refs_path);
1099 goto done;
1101 for (;;) {
1102 linelen = getline(&line, &linesize, f);
1103 if (linelen == -1) {
1104 if (feof(f))
1105 break;
1106 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1107 goto done;
1109 if (linelen > 0 && line[linelen - 1] == '\n')
1110 line[linelen - 1] = '\0';
1111 err = parse_packed_ref_line(&ref, NULL, line,
1112 sb.st_mtime);
1113 if (err)
1114 goto done;
1115 if (ref) {
1116 if (ref_namespace) {
1117 const char *name;
1118 name = got_ref_get_name(ref);
1119 if (!got_path_is_child(name,
1120 ref_namespace,
1121 strlen(ref_namespace))) {
1122 got_ref_close(ref);
1123 continue;
1126 err = got_reflist_insert(&new, refs, ref,
1127 cmp_cb, cmp_arg);
1128 if (err || new == NULL /* duplicate */)
1129 got_ref_close(ref);
1130 if (err)
1131 goto done;
1135 done:
1136 free(abs_namespace);
1137 free(buf);
1138 free(line);
1139 free(path_refs);
1140 if (f && fclose(f) == EOF && err == NULL)
1141 err = got_error_from_errno("fclose");
1142 return err;
1145 void
1146 got_ref_list_free(struct got_reflist_head *refs)
1148 struct got_reflist_entry *re;
1150 while ((re = TAILQ_FIRST(refs))) {
1151 TAILQ_REMOVE(refs, re, entry);
1152 got_ref_close(re->ref);
1153 free(re);
1158 int
1159 got_ref_is_symbolic(struct got_reference *ref)
1161 return (ref->flags & GOT_REF_IS_SYMBOLIC);
1164 const struct got_error *
1165 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1167 if (ref->flags & GOT_REF_IS_SYMBOLIC)
1168 return got_error(GOT_ERR_BAD_REF_TYPE);
1170 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
1171 return NULL;
1174 const struct got_error *
1175 got_ref_change_symref(struct got_reference *ref, const char *refname)
1177 char *new_name;
1179 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1180 return got_error(GOT_ERR_BAD_REF_TYPE);
1182 new_name = strdup(refname);
1183 if (new_name == NULL)
1184 return got_error_from_errno("strdup");
1186 free(ref->ref.symref.ref);
1187 ref->ref.symref.ref = new_name;
1188 return NULL;
1191 const struct got_error *
1192 got_ref_change_symref_to_ref(struct got_reference *symref,
1193 struct got_object_id *id)
1195 if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1196 return got_error(GOT_ERR_BAD_REF_TYPE);
1198 symref->ref.ref.name = symref->ref.symref.name;
1199 memcpy(symref->ref.ref.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1200 symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1201 return NULL;
1204 const struct got_error *
1205 got_ref_write(struct got_reference *ref, struct got_repository *repo)
1207 const struct got_error *err = NULL, *unlock_err = NULL;
1208 const char *name = got_ref_get_name(ref);
1209 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1210 struct got_lockfile *lf = NULL;
1211 FILE *f = NULL;
1212 size_t n;
1213 struct stat sb;
1215 path_refs = get_refs_dir_path(repo, name);
1216 if (path_refs == NULL) {
1217 err = got_error_from_errno2("get_refs_dir_path", name);
1218 goto done;
1221 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1222 err = got_error_from_errno("asprintf");
1223 goto done;
1226 err = got_opentemp_named(&tmppath, &f, path);
1227 if (err) {
1228 char *parent;
1229 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1230 goto done;
1231 err = got_path_dirname(&parent, path);
1232 if (err)
1233 goto done;
1234 err = got_path_mkdir(parent);
1235 free(parent);
1236 if (err)
1237 goto done;
1238 err = got_opentemp_named(&tmppath, &f, path);
1239 if (err)
1240 goto done;
1243 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1244 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1245 if (n != strlen(ref->ref.symref.ref) + 6) {
1246 err = got_ferror(f, GOT_ERR_IO);
1247 goto done;
1249 } else {
1250 char hex[SHA1_DIGEST_STRING_LENGTH];
1251 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
1252 sizeof(hex)) == NULL) {
1253 err = got_error(GOT_ERR_BAD_REF_DATA);
1254 goto done;
1256 n = fprintf(f, "%s\n", hex);
1257 if (n != sizeof(hex)) {
1258 err = got_ferror(f, GOT_ERR_IO);
1259 goto done;
1263 if (ref->lf == NULL) {
1264 err = got_lockfile_lock(&lf, path, -1);
1265 if (err)
1266 goto done;
1269 /* XXX: check if old content matches our expectations? */
1271 if (stat(path, &sb) != 0) {
1272 if (errno != ENOENT) {
1273 err = got_error_from_errno2("stat", path);
1274 goto done;
1276 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1279 if (fchmod(fileno(f), sb.st_mode) != 0) {
1280 err = got_error_from_errno2("fchmod", tmppath);
1281 goto done;
1284 if (rename(tmppath, path) != 0) {
1285 err = got_error_from_errno3("rename", tmppath, path);
1286 goto done;
1288 free(tmppath);
1289 tmppath = NULL;
1291 if (stat(path, &sb) == -1) {
1292 err = got_error_from_errno2("stat", path);
1293 goto done;
1295 ref->mtime = sb.st_mtime;
1296 done:
1297 if (ref->lf == NULL && lf)
1298 unlock_err = got_lockfile_unlock(lf, -1);
1299 if (f) {
1300 if (fclose(f) == EOF && err == NULL)
1301 err = got_error_from_errno("fclose");
1303 free(path_refs);
1304 free(path);
1305 if (tmppath) {
1306 if (unlink(tmppath) != 0 && err == NULL)
1307 err = got_error_from_errno2("unlink", tmppath);
1308 free(tmppath);
1310 return err ? err : unlock_err;
1313 static const struct got_error *
1314 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1316 const struct got_error *err = NULL, *unlock_err = NULL;
1317 struct got_lockfile *lf = NULL;
1318 FILE *f = NULL, *tmpf = NULL;
1319 char *line = NULL, *packed_refs_path, *tmppath = NULL;
1320 size_t linesize = 0;
1321 struct got_reflist_head refs;
1322 int found_delref = 0;
1324 /* The packed-refs file does not cotain symbolic references. */
1325 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1326 return got_error(GOT_ERR_BAD_REF_DATA);
1328 TAILQ_INIT(&refs);
1330 packed_refs_path = got_repo_get_path_packed_refs(repo);
1331 if (packed_refs_path == NULL)
1332 return got_error_from_errno("got_repo_get_path_packed_refs");
1334 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
1335 if (err)
1336 goto done;
1338 if (delref->lf == NULL) {
1339 err = got_lockfile_lock(&lf, packed_refs_path, -1);
1340 if (err)
1341 goto done;
1344 f = fopen(packed_refs_path, "r");
1345 if (f == NULL) {
1346 err = got_error_from_errno2("fopen", packed_refs_path);
1347 goto done;
1349 for (;;) {
1350 ssize_t linelen;
1351 struct got_reference *ref;
1352 struct got_reflist_entry *new;
1354 linelen = getline(&line, &linesize, f);
1355 if (linelen == -1) {
1356 if (feof(f))
1357 break;
1358 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1359 goto done;
1361 if (linelen > 0 && line[linelen - 1] == '\n')
1362 line[linelen - 1] = '\0';
1363 err = parse_packed_ref_line(&ref, NULL, line, 0);
1364 if (err)
1365 goto done;
1366 if (ref == NULL)
1367 continue;
1369 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1370 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1371 sizeof(delref->ref.ref.sha1)) == 0) {
1372 found_delref = 1;
1373 got_ref_close(ref);
1374 continue;
1377 err = got_reflist_insert(&new, &refs, ref,
1378 got_ref_cmp_by_name, NULL);
1379 if (err || new == NULL /* duplicate */)
1380 got_ref_close(ref);
1381 if (err)
1382 goto done;
1385 if (found_delref) {
1386 struct got_reflist_entry *re;
1387 size_t n;
1388 struct stat sb;
1390 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1391 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1392 err = got_ferror(f, GOT_ERR_IO);
1393 goto done;
1396 TAILQ_FOREACH(re, &refs, entry) {
1397 char hex[SHA1_DIGEST_STRING_LENGTH];
1399 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1400 sizeof(hex)) == NULL) {
1401 err = got_error(GOT_ERR_BAD_REF_DATA);
1402 goto done;
1404 n = fprintf(tmpf, "%s ", hex);
1405 if (n != sizeof(hex)) {
1406 err = got_ferror(f, GOT_ERR_IO);
1407 goto done;
1409 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1410 if (n != strlen(re->ref->ref.ref.name) + 1) {
1411 err = got_ferror(f, GOT_ERR_IO);
1412 goto done;
1416 if (fflush(tmpf) != 0) {
1417 err = got_error_from_errno("fflush");
1418 goto done;
1421 if (fstat(fileno(f), &sb) != 0) {
1422 if (errno != ENOENT) {
1423 err = got_error_from_errno2("fstat",
1424 packed_refs_path);
1425 goto done;
1427 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1430 if (fchmod(fileno(tmpf), sb.st_mode) != 0) {
1431 err = got_error_from_errno2("fchmod", tmppath);
1432 goto done;
1435 if (rename(tmppath, packed_refs_path) != 0) {
1436 err = got_error_from_errno3("rename", tmppath,
1437 packed_refs_path);
1438 goto done;
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 (tmpf) {
1449 unlink(tmppath);
1450 if (fclose(tmpf) == EOF && err == NULL)
1451 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) != 0)
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 return err;
1574 err = got_reflist_entry_dup(&new, re);
1575 if (err)
1576 return err;
1578 TAILQ_INSERT_TAIL(&ent->refs, new, entry);
1579 return NULL;
1582 const struct got_error *
1583 got_reflist_object_id_map_create(struct got_reflist_object_id_map **map,
1584 struct got_reflist_head *refs, struct got_repository *repo)
1586 const struct got_error *err = NULL;
1587 struct got_object_idset *idset;
1588 struct got_object_id *id = NULL;
1589 struct got_reflist_entry *re;
1591 idset = got_object_idset_alloc();
1592 if (idset == NULL)
1593 return got_error_from_errno("got_object_idset_alloc");
1595 *map = malloc(sizeof(**map));
1596 if (*map == NULL) {
1597 got_object_idset_free(idset);
1598 return got_error_from_errno("malloc");
1600 (*map)->idset = idset;
1602 TAILQ_FOREACH(re, refs, entry) {
1603 struct got_tag_object *tag = NULL;
1605 err = got_ref_resolve(&id, repo, re->ref);
1606 if (err)
1607 goto done;
1609 err = add_object_id_map_entry(idset, id, re);
1610 if (err)
1611 goto done;
1613 if (strstr(got_ref_get_name(re->ref), "/tags/") == NULL) {
1614 free(id);
1615 id = NULL;
1616 continue;
1619 err = got_object_open_as_tag(&tag, repo, id);
1620 if (err) {
1621 if (err->code != GOT_ERR_OBJ_TYPE)
1622 goto done;
1623 /* Ref points at something other than a tag. */
1624 err = NULL;
1625 tag = NULL;
1626 free(id);
1627 id = NULL;
1628 continue;
1631 err = add_object_id_map_entry(idset,
1632 got_object_tag_get_object_id(tag), re);
1633 got_object_tag_close(tag);
1634 if (err)
1635 goto done;
1637 free(id);
1638 id = NULL;
1640 done:
1641 free(id);
1642 if (err) {
1643 got_reflist_object_id_map_free(*map);
1644 *map = NULL;
1646 return err;
1649 struct got_reflist_head *
1650 got_reflist_object_id_map_lookup(struct got_reflist_object_id_map *map,
1651 struct got_object_id *id)
1653 struct got_reflist_object_id_map_entry *ent;
1654 ent = got_object_idset_get(map->idset, id);
1655 if (ent)
1656 return &ent->refs;
1657 return NULL;
1660 static const struct got_error *
1661 free_id_map_entry(struct got_object_id *id, void *data, void *arg)
1663 struct got_reflist_object_id_map_entry *ent = data;
1665 got_ref_list_free(&ent->refs);
1666 free(ent);
1667 return NULL;
1670 void
1671 got_reflist_object_id_map_free(struct got_reflist_object_id_map *map)
1673 got_object_idset_for_each(map->idset, free_id_map_entry, NULL);
1674 got_object_idset_free(map->idset);
1675 free(map);