Blob


1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/stat.h>
21 #include <errno.h>
22 #include <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"
57 /*
58 * We do not resolve tags yet, and don't yet care about sorting refs either,
59 * so packed-refs files we write contain a minimal header which disables all
60 * packed-refs "traits" supported by Git.
61 */
62 #define GOT_PACKED_REFS_HEADER "# pack-refs with:"
64 /* A symbolic reference. */
65 struct got_symref {
66 char *name;
67 char *ref;
68 };
70 #define GOT_REF_RECURSE_MAX 20
72 /* A non-symbolic reference (there is no better designation). */
73 struct got_ref {
74 char *name;
75 u_int8_t sha1[SHA1_DIGEST_LENGTH];
76 };
78 /* A reference which points to an arbitrary object. */
79 struct got_reference {
80 unsigned int flags;
81 #define GOT_REF_IS_SYMBOLIC 0x01
82 #define GOT_REF_IS_PACKED 0x02
84 union {
85 struct got_ref ref;
86 struct got_symref symref;
87 } ref;
89 struct got_lockfile *lf;
90 time_t mtime;
92 /* Cached timestamp for got_ref_cmp_by_commit_timestamp_descending() */
93 time_t committer_time;
94 };
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)
99 {
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);
271 int
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 *packed_refs_path = NULL, *path_refs = NULL;
457 const char *subdirs[] = {
458 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
459 };
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 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, "rbe");
498 if (f != NULL) {
499 struct stat sb;
500 if (fstat(fileno(f), &sb) == -1) {
501 err = got_error_from_errno2("fstat",
502 packed_refs_path);
503 goto done;
505 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
506 refname, sb.st_mtime);
507 if (!err) {
508 if (fclose(f) == EOF) {
509 err = got_error_from_errno("fclose");
510 got_ref_close(*ref);
511 *ref = NULL;
512 } else if (*ref)
513 (*ref)->lf = lf;
517 done:
518 if (!err && *ref == NULL)
519 err = got_error_not_ref(refname);
520 if (err && lf)
521 got_lockfile_unlock(lf, -1);
522 free(packed_refs_path);
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,
848 struct got_reflist_head *refs, struct got_reference *ref,
849 got_ref_cmp_cb cmp_cb, void *cmp_arg)
851 const struct got_error *err;
852 struct got_reflist_entry *new, *re;
853 int cmp;
855 *newp = NULL;
857 if (cmp_cb != got_ref_cmp_by_name && (ref->flags & GOT_REF_IS_PACKED)) {
858 /*
859 * If we are not sorting elements by name then we must still
860 * detect collisions between a packed ref and an on-disk ref
861 * using the same name. On-disk refs take precedence and are
862 * already present on the list before packed refs get added.
863 */
864 TAILQ_FOREACH(re, refs, entry) {
865 err = got_ref_cmp_by_name(NULL, &cmp, re->ref, ref);
866 if (err)
867 return err;
868 if (cmp == 0)
869 return NULL;
873 new = malloc(sizeof(*new));
874 if (new == NULL)
875 return got_error_from_errno("malloc");
876 new->ref = ref;
877 *newp = new;
879 /*
880 * We must de-duplicate entries on insert because packed-refs may
881 * contain redundant entries. On-disk refs take precedence.
882 * This code assumes that on-disk revs are read before packed-refs.
883 * We're iterating the list anyway, so insert elements sorted by name.
885 * Many callers will provide paths in a somewhat sorted order.
886 * Iterating backwards from the tail of the list should be more
887 * efficient than traversing through the entire list each time
888 * an element is inserted.
889 */
890 re = TAILQ_LAST(refs, got_reflist_head);
891 while (re) {
892 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
893 if (err)
894 return err;
895 if (cmp == 0) {
896 /* duplicate */
897 free(new);
898 *newp = NULL;
899 return NULL;
900 } else if (cmp < 0) {
901 TAILQ_INSERT_AFTER(refs, re, new, entry);
902 return NULL;
904 re = TAILQ_PREV(re, got_reflist_head, entry);
907 TAILQ_INSERT_HEAD(refs, new, entry);
908 return NULL;
911 const struct got_error *
912 got_reflist_sort(struct got_reflist_head *refs,
913 got_ref_cmp_cb cmp_cb, void *cmp_arg)
915 const struct got_error *err = NULL;
916 struct got_reflist_entry *re, *tmp, *new;
917 struct got_reflist_head sorted;
919 TAILQ_INIT(&sorted);
921 TAILQ_FOREACH_SAFE(re, refs, entry, tmp) {
922 struct got_reference *ref = re->ref;
923 TAILQ_REMOVE(refs, re, entry);
924 free(re);
925 err = got_reflist_insert(&new, &sorted, ref, cmp_cb, cmp_arg);
926 if (err || new == NULL /* duplicate */)
927 got_ref_close(ref);
928 if (err)
929 return err;
932 TAILQ_CONCAT(refs, &sorted, entry);
933 return NULL;
936 static const struct got_error *
937 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
938 const char *subdir, struct got_repository *repo,
939 got_ref_cmp_cb cmp_cb, void *cmp_arg)
941 const struct got_error *err = NULL;
942 DIR *d = NULL;
943 char *path_subdir;
945 while (subdir[0] == '/')
946 subdir++;
948 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
949 return got_error_from_errno("asprintf");
951 d = opendir(path_subdir);
952 if (d == NULL)
953 goto done;
955 for (;;) {
956 struct dirent *dent;
957 struct got_reference *ref;
958 char *child;
959 int type;
961 dent = readdir(d);
962 if (dent == NULL)
963 break;
965 if (strcmp(dent->d_name, ".") == 0 ||
966 strcmp(dent->d_name, "..") == 0)
967 continue;
969 err = got_path_dirent_type(&type, path_subdir, dent);
970 if (err)
971 break;
973 switch (type) {
974 case DT_REG:
975 err = open_ref(&ref, path_refs, subdir, dent->d_name,
976 0);
977 if (err)
978 goto done;
979 if (ref) {
980 struct got_reflist_entry *new;
981 err = got_reflist_insert(&new, refs, ref,
982 cmp_cb, cmp_arg);
983 if (err || new == NULL /* duplicate */)
984 got_ref_close(ref);
985 if (err)
986 goto done;
988 break;
989 case DT_DIR:
990 if (asprintf(&child, "%s%s%s", subdir,
991 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
992 err = got_error_from_errno("asprintf");
993 break;
995 err = gather_on_disk_refs(refs, path_refs, child, repo,
996 cmp_cb, cmp_arg);
997 free(child);
998 break;
999 default:
1000 break;
1003 done:
1004 if (d)
1005 closedir(d);
1006 free(path_subdir);
1007 return err;
1010 const struct got_error *
1011 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
1012 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
1014 const struct got_error *err;
1015 char *packed_refs_path = NULL, *path_refs = NULL;
1016 char *abs_namespace = NULL, *buf = NULL;
1017 const char *ondisk_ref_namespace = NULL;
1018 char *line = NULL;
1019 FILE *f = NULL;
1020 struct got_reference *ref;
1021 struct got_reflist_entry *new;
1023 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
1024 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
1025 if (path_refs == NULL) {
1026 err = got_error_from_errno("get_refs_dir_path");
1027 goto done;
1029 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
1030 if (err)
1031 goto done;
1032 err = got_reflist_insert(&new, refs, ref, cmp_cb, cmp_arg);
1033 if (err || new == NULL /* duplicate */)
1034 got_ref_close(ref);
1035 if (err && err->code != GOT_ERR_NOT_REF)
1036 goto done;
1037 } else {
1038 /* Try listing a single reference. */
1039 const char *refname = ref_namespace;
1040 path_refs = get_refs_dir_path(repo, refname);
1041 if (path_refs == NULL) {
1042 err = got_error_from_errno("get_refs_dir_path");
1043 goto done;
1045 err = open_ref(&ref, path_refs, "", refname, 0);
1046 if (err) {
1047 if (err->code != GOT_ERR_NOT_REF)
1048 goto done;
1049 /* Try to look up references in a given namespace. */
1050 } else {
1051 err = got_reflist_insert(&new, refs, ref,
1052 cmp_cb, cmp_arg);
1053 if (err || new == NULL /* duplicate */)
1054 got_ref_close(ref);
1055 return err;
1059 if (ref_namespace) {
1060 size_t len;
1061 /* Canonicalize the path to eliminate double-slashes if any. */
1062 if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
1063 err = got_error_from_errno("asprintf");
1064 goto done;
1066 len = strlen(abs_namespace) + 1;
1067 buf = malloc(len);
1068 if (buf == NULL) {
1069 err = got_error_from_errno("malloc");
1070 goto done;
1072 err = got_canonpath(abs_namespace, buf, len);
1073 if (err)
1074 goto done;
1075 ondisk_ref_namespace = buf;
1076 while (ondisk_ref_namespace[0] == '/')
1077 ondisk_ref_namespace++;
1078 if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
1079 ondisk_ref_namespace += 5;
1080 else if (strcmp(ondisk_ref_namespace, "refs") == 0)
1081 ondisk_ref_namespace = "";
1084 /* Gather on-disk refs before parsing packed-refs. */
1085 free(path_refs);
1086 path_refs = get_refs_dir_path(repo, "");
1087 if (path_refs == NULL) {
1088 err = got_error_from_errno("get_refs_dir_path");
1089 goto done;
1091 err = gather_on_disk_refs(refs, path_refs,
1092 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
1093 cmp_cb, cmp_arg);
1094 if (err)
1095 goto done;
1098 * The packed-refs file may contain redundant entries, in which
1099 * case on-disk refs take precedence.
1101 packed_refs_path = got_repo_get_path_packed_refs(repo);
1102 if (packed_refs_path == NULL) {
1103 err = got_error_from_errno("got_repo_get_path_packed_refs");
1104 goto done;
1107 f = fopen(packed_refs_path, "re");
1108 if (f) {
1109 size_t linesize = 0;
1110 ssize_t linelen;
1111 struct stat sb;
1113 if (fstat(fileno(f), &sb) == -1) {
1114 err = got_error_from_errno2("fstat", packed_refs_path);
1115 goto done;
1117 for (;;) {
1118 linelen = getline(&line, &linesize, f);
1119 if (linelen == -1) {
1120 if (feof(f))
1121 break;
1122 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1123 goto done;
1125 if (linelen > 0 && line[linelen - 1] == '\n')
1126 line[linelen - 1] = '\0';
1127 err = parse_packed_ref_line(&ref, NULL, line,
1128 sb.st_mtime);
1129 if (err)
1130 goto done;
1131 if (ref) {
1132 if (ref_namespace) {
1133 const char *name;
1134 name = got_ref_get_name(ref);
1135 if (!got_path_is_child(name,
1136 ref_namespace,
1137 strlen(ref_namespace))) {
1138 got_ref_close(ref);
1139 continue;
1142 err = got_reflist_insert(&new, refs, ref,
1143 cmp_cb, cmp_arg);
1144 if (err || new == NULL /* duplicate */)
1145 got_ref_close(ref);
1146 if (err)
1147 goto done;
1151 done:
1152 free(packed_refs_path);
1153 free(abs_namespace);
1154 free(buf);
1155 free(line);
1156 free(path_refs);
1157 if (f && fclose(f) == EOF && err == NULL)
1158 err = got_error_from_errno("fclose");
1159 return err;
1162 void
1163 got_ref_list_free(struct got_reflist_head *refs)
1165 struct got_reflist_entry *re;
1167 while ((re = TAILQ_FIRST(refs))) {
1168 TAILQ_REMOVE(refs, re, entry);
1169 got_ref_close(re->ref);
1170 free(re);
1175 int
1176 got_ref_is_symbolic(struct got_reference *ref)
1178 return (ref->flags & GOT_REF_IS_SYMBOLIC);
1181 const struct got_error *
1182 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1184 if (ref->flags & GOT_REF_IS_SYMBOLIC)
1185 return got_error(GOT_ERR_BAD_REF_TYPE);
1187 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
1188 return NULL;
1191 const struct got_error *
1192 got_ref_change_symref(struct got_reference *ref, const char *refname)
1194 char *new_name;
1196 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1197 return got_error(GOT_ERR_BAD_REF_TYPE);
1199 new_name = strdup(refname);
1200 if (new_name == NULL)
1201 return got_error_from_errno("strdup");
1203 free(ref->ref.symref.ref);
1204 ref->ref.symref.ref = new_name;
1205 return NULL;
1208 const struct got_error *
1209 got_ref_change_symref_to_ref(struct got_reference *symref,
1210 struct got_object_id *id)
1212 if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1213 return got_error(GOT_ERR_BAD_REF_TYPE);
1215 symref->ref.ref.name = symref->ref.symref.name;
1216 memcpy(symref->ref.ref.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1217 symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1218 return NULL;
1221 const struct got_error *
1222 got_ref_write(struct got_reference *ref, struct got_repository *repo)
1224 const struct got_error *err = NULL, *unlock_err = NULL;
1225 const char *name = got_ref_get_name(ref);
1226 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1227 struct got_lockfile *lf = NULL;
1228 FILE *f = NULL;
1229 size_t n;
1230 struct stat sb;
1232 path_refs = get_refs_dir_path(repo, name);
1233 if (path_refs == NULL) {
1234 err = got_error_from_errno2("get_refs_dir_path", name);
1235 goto done;
1238 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1239 err = got_error_from_errno("asprintf");
1240 goto done;
1243 err = got_opentemp_named(&tmppath, &f, path);
1244 if (err) {
1245 char *parent;
1246 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1247 goto done;
1248 err = got_path_dirname(&parent, path);
1249 if (err)
1250 goto done;
1251 err = got_path_mkdir(parent);
1252 free(parent);
1253 if (err)
1254 goto done;
1255 err = got_opentemp_named(&tmppath, &f, path);
1256 if (err)
1257 goto done;
1260 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1261 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1262 if (n != strlen(ref->ref.symref.ref) + 6) {
1263 err = got_ferror(f, GOT_ERR_IO);
1264 goto done;
1266 } else {
1267 char hex[SHA1_DIGEST_STRING_LENGTH];
1268 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
1269 sizeof(hex)) == NULL) {
1270 err = got_error(GOT_ERR_BAD_REF_DATA);
1271 goto done;
1273 n = fprintf(f, "%s\n", hex);
1274 if (n != sizeof(hex)) {
1275 err = got_ferror(f, GOT_ERR_IO);
1276 goto done;
1280 if (ref->lf == NULL) {
1281 err = got_lockfile_lock(&lf, path, -1);
1282 if (err)
1283 goto done;
1286 /* XXX: check if old content matches our expectations? */
1288 if (stat(path, &sb) != 0) {
1289 if (errno != ENOENT) {
1290 err = got_error_from_errno2("stat", path);
1291 goto done;
1293 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1296 if (fchmod(fileno(f), sb.st_mode) != 0) {
1297 err = got_error_from_errno2("fchmod", tmppath);
1298 goto done;
1301 if (rename(tmppath, path) != 0) {
1302 err = got_error_from_errno3("rename", tmppath, path);
1303 goto done;
1305 free(tmppath);
1306 tmppath = NULL;
1308 if (stat(path, &sb) == -1) {
1309 err = got_error_from_errno2("stat", path);
1310 goto done;
1312 ref->mtime = sb.st_mtime;
1313 done:
1314 if (ref->lf == NULL && lf)
1315 unlock_err = got_lockfile_unlock(lf, -1);
1316 if (f) {
1317 if (fclose(f) == EOF && err == NULL)
1318 err = got_error_from_errno("fclose");
1320 free(path_refs);
1321 free(path);
1322 if (tmppath) {
1323 if (unlink(tmppath) != 0 && err == NULL)
1324 err = got_error_from_errno2("unlink", tmppath);
1325 free(tmppath);
1327 return err ? err : unlock_err;
1330 static const struct got_error *
1331 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1333 const struct got_error *err = NULL, *unlock_err = NULL;
1334 struct got_lockfile *lf = NULL;
1335 FILE *f = NULL, *tmpf = NULL;
1336 char *line = NULL, *packed_refs_path, *tmppath = NULL;
1337 size_t linesize = 0;
1338 struct got_reflist_head refs;
1339 int found_delref = 0;
1341 /* The packed-refs file does not cotain symbolic references. */
1342 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1343 return got_error(GOT_ERR_BAD_REF_DATA);
1345 TAILQ_INIT(&refs);
1347 packed_refs_path = got_repo_get_path_packed_refs(repo);
1348 if (packed_refs_path == NULL)
1349 return got_error_from_errno("got_repo_get_path_packed_refs");
1351 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
1352 if (err)
1353 goto done;
1355 if (delref->lf == NULL) {
1356 err = got_lockfile_lock(&lf, packed_refs_path, -1);
1357 if (err)
1358 goto done;
1361 f = fopen(packed_refs_path, "re");
1362 if (f == NULL) {
1363 err = got_error_from_errno2("fopen", packed_refs_path);
1364 goto done;
1366 for (;;) {
1367 ssize_t linelen;
1368 struct got_reference *ref;
1369 struct got_reflist_entry *new;
1371 linelen = getline(&line, &linesize, f);
1372 if (linelen == -1) {
1373 if (feof(f))
1374 break;
1375 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1376 goto done;
1378 if (linelen > 0 && line[linelen - 1] == '\n')
1379 line[linelen - 1] = '\0';
1380 err = parse_packed_ref_line(&ref, NULL, line, 0);
1381 if (err)
1382 goto done;
1383 if (ref == NULL)
1384 continue;
1386 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1387 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1388 sizeof(delref->ref.ref.sha1)) == 0) {
1389 found_delref = 1;
1390 got_ref_close(ref);
1391 continue;
1394 err = got_reflist_insert(&new, &refs, ref,
1395 got_ref_cmp_by_name, NULL);
1396 if (err || new == NULL /* duplicate */)
1397 got_ref_close(ref);
1398 if (err)
1399 goto done;
1402 if (found_delref) {
1403 struct got_reflist_entry *re;
1404 size_t n;
1405 struct stat sb;
1407 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1408 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1409 err = got_ferror(f, GOT_ERR_IO);
1410 goto done;
1413 TAILQ_FOREACH(re, &refs, entry) {
1414 char hex[SHA1_DIGEST_STRING_LENGTH];
1416 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1417 sizeof(hex)) == NULL) {
1418 err = got_error(GOT_ERR_BAD_REF_DATA);
1419 goto done;
1421 n = fprintf(tmpf, "%s ", hex);
1422 if (n != sizeof(hex)) {
1423 err = got_ferror(f, GOT_ERR_IO);
1424 goto done;
1426 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1427 if (n != strlen(re->ref->ref.ref.name) + 1) {
1428 err = got_ferror(f, GOT_ERR_IO);
1429 goto done;
1433 if (fflush(tmpf) != 0) {
1434 err = got_error_from_errno("fflush");
1435 goto done;
1438 if (fstat(fileno(f), &sb) != 0) {
1439 if (errno != ENOENT) {
1440 err = got_error_from_errno2("fstat",
1441 packed_refs_path);
1442 goto done;
1444 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1447 if (fchmod(fileno(tmpf), sb.st_mode) != 0) {
1448 err = got_error_from_errno2("fchmod", tmppath);
1449 goto done;
1452 if (rename(tmppath, packed_refs_path) != 0) {
1453 err = got_error_from_errno3("rename", tmppath,
1454 packed_refs_path);
1455 goto done;
1458 done:
1459 if (delref->lf == NULL && lf)
1460 unlock_err = got_lockfile_unlock(lf, -1);
1461 if (f) {
1462 if (fclose(f) == EOF && err == NULL)
1463 err = got_error_from_errno("fclose");
1465 if (tmpf) {
1466 unlink(tmppath);
1467 if (fclose(tmpf) == EOF && err == NULL)
1468 err = got_error_from_errno("fclose");
1470 free(tmppath);
1471 free(packed_refs_path);
1472 free(line);
1473 got_ref_list_free(&refs);
1474 return err ? err : unlock_err;
1477 static const struct got_error *
1478 delete_loose_ref(struct got_reference *ref, struct got_repository *repo)
1480 const struct got_error *err = NULL, *unlock_err = NULL;
1481 const char *name = got_ref_get_name(ref);
1482 char *path_refs = NULL, *path = NULL;
1483 struct got_lockfile *lf = NULL;
1485 path_refs = get_refs_dir_path(repo, name);
1486 if (path_refs == NULL) {
1487 err = got_error_from_errno2("get_refs_dir_path", name);
1488 goto done;
1491 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1492 err = got_error_from_errno("asprintf");
1493 goto done;
1496 if (ref->lf == NULL) {
1497 err = got_lockfile_lock(&lf, path, -1);
1498 if (err)
1499 goto done;
1502 /* XXX: check if old content matches our expectations? */
1504 if (unlink(path) != 0)
1505 err = got_error_from_errno2("unlink", path);
1506 done:
1507 if (ref->lf == NULL && lf)
1508 unlock_err = got_lockfile_unlock(lf, -1);
1510 free(path_refs);
1511 free(path);
1512 return err ? err : unlock_err;
1515 const struct got_error *
1516 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1518 const struct got_error *err = NULL;
1519 struct got_reference *ref2;
1521 if (ref->flags & GOT_REF_IS_PACKED) {
1522 err = delete_packed_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_loose_ref(ref2, repo);
1534 got_ref_close(ref2);
1535 return err;
1536 } else {
1537 err = delete_loose_ref(ref, repo);
1538 if (err)
1539 return err;
1541 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1542 if (err) {
1543 if (err->code == GOT_ERR_NOT_REF)
1544 return NULL;
1545 return err;
1548 err = delete_packed_ref(ref2, repo);
1549 got_ref_close(ref2);
1550 return err;
1554 const struct got_error *
1555 got_ref_unlock(struct got_reference *ref)
1557 const struct got_error *err;
1558 err = got_lockfile_unlock(ref->lf, -1);
1559 ref->lf = NULL;
1560 return err;
1563 struct got_reflist_object_id_map {
1564 struct got_object_idset *idset;
1567 struct got_reflist_object_id_map_entry {
1568 struct got_reflist_head refs;
1571 static const struct got_error *
1572 add_object_id_map_entry(struct got_object_idset *idset,
1573 struct got_object_id *id, struct got_reflist_entry *re)
1575 const struct got_error *err = NULL;
1576 struct got_reflist_object_id_map_entry *ent;
1577 struct got_reflist_entry *new;
1579 ent = got_object_idset_get(idset, id);
1580 if (ent == NULL) {
1581 ent = malloc(sizeof(*ent));
1582 if (ent == NULL)
1583 return got_error_from_errno("malloc");
1585 TAILQ_INIT(&ent->refs);
1586 err = got_object_idset_add(idset, id, ent);
1587 if (err) {
1588 free(ent);
1589 return err;
1593 err = got_reflist_entry_dup(&new, re);
1594 if (err)
1595 return err;
1597 TAILQ_INSERT_TAIL(&ent->refs, new, entry);
1598 return NULL;
1601 const struct got_error *
1602 got_reflist_object_id_map_create(struct got_reflist_object_id_map **map,
1603 struct got_reflist_head *refs, struct got_repository *repo)
1605 const struct got_error *err = NULL;
1606 struct got_object_idset *idset;
1607 struct got_object_id *id = NULL;
1608 struct got_reflist_entry *re;
1610 idset = got_object_idset_alloc();
1611 if (idset == NULL)
1612 return got_error_from_errno("got_object_idset_alloc");
1614 *map = malloc(sizeof(**map));
1615 if (*map == NULL) {
1616 got_object_idset_free(idset);
1617 return got_error_from_errno("malloc");
1619 (*map)->idset = idset;
1621 TAILQ_FOREACH(re, refs, entry) {
1622 struct got_tag_object *tag = NULL;
1624 err = got_ref_resolve(&id, repo, re->ref);
1625 if (err)
1626 goto done;
1628 err = add_object_id_map_entry(idset, id, re);
1629 if (err)
1630 goto done;
1632 if (strstr(got_ref_get_name(re->ref), "/tags/") == NULL) {
1633 free(id);
1634 id = NULL;
1635 continue;
1638 err = got_object_open_as_tag(&tag, repo, id);
1639 if (err) {
1640 if (err->code != GOT_ERR_OBJ_TYPE)
1641 goto done;
1642 /* Ref points at something other than a tag. */
1643 err = NULL;
1644 tag = NULL;
1645 free(id);
1646 id = NULL;
1647 continue;
1650 err = add_object_id_map_entry(idset,
1651 got_object_tag_get_object_id(tag), re);
1652 got_object_tag_close(tag);
1653 if (err)
1654 goto done;
1656 free(id);
1657 id = NULL;
1659 done:
1660 free(id);
1661 if (err) {
1662 got_reflist_object_id_map_free(*map);
1663 *map = NULL;
1665 return err;
1668 struct got_reflist_head *
1669 got_reflist_object_id_map_lookup(struct got_reflist_object_id_map *map,
1670 struct got_object_id *id)
1672 struct got_reflist_object_id_map_entry *ent;
1673 ent = got_object_idset_get(map->idset, id);
1674 if (ent)
1675 return &ent->refs;
1676 return NULL;
1679 static const struct got_error *
1680 free_id_map_entry(struct got_object_id *id, void *data, void *arg)
1682 struct got_reflist_object_id_map_entry *ent = data;
1684 got_ref_list_free(&ent->refs);
1685 free(ent);
1686 return NULL;
1689 void
1690 got_reflist_object_id_map_free(struct got_reflist_object_id_map *map)
1692 got_object_idset_for_each(map->idset, free_id_map_entry, NULL);
1693 got_object_idset_free(map->idset);
1694 free(map);