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 <sha1.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <util.h>
31 #include <zlib.h>
32 #include <time.h>
33 #include <libgen.h>
35 #include "got_error.h"
36 #include "got_object.h"
37 #include "got_repository.h"
38 #include "got_reference.h"
39 #include "got_opentemp.h"
40 #include "got_path.h"
42 #include "got_lib_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 new = malloc(sizeof(*new));
858 if (new == NULL)
859 return got_error_from_errno("malloc");
860 new->ref = ref;
861 *newp = new;
863 if (cmp_cb != got_ref_cmp_by_name &&
864 (new->ref->flags & GOT_REF_IS_PACKED)) {
865 /*
866 * If we are not sorting elements by name then we must still
867 * detect collisions between a packed ref and an on-disk ref
868 * using the same name. On-disk refs take precedence and are
869 * already present on the list before packed refs get added.
870 */
871 TAILQ_FOREACH(re, refs, entry) {
872 err = got_ref_cmp_by_name(NULL, &cmp,
873 re->ref, new->ref);
874 if (err)
875 return err;
876 if (cmp == 0) {
877 free(new);
878 *newp = NULL;
879 return NULL;
884 /*
885 * We must de-duplicate entries on insert because packed-refs may
886 * contain redundant entries. On-disk refs take precedence.
887 * This code assumes that on-disk revs are read before packed-refs.
888 * We're iterating the list anyway, so insert elements sorted by name.
890 * Many callers will provide paths in a somewhat sorted order.
891 * Iterating backwards from the tail of the list should be more
892 * efficient than traversing through the entire list each time
893 * an element is inserted.
894 */
895 re = TAILQ_LAST(refs, got_reflist_head);
896 while (re) {
897 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
898 if (err)
899 return err;
900 if (cmp == 0) {
901 /* duplicate */
902 free(new);
903 *newp = NULL;
904 return NULL;
905 } else if (cmp < 0) {
906 TAILQ_INSERT_AFTER(refs, re, new, entry);
907 return NULL;
909 re = TAILQ_PREV(re, got_reflist_head, entry);
912 TAILQ_INSERT_HEAD(refs, new, entry);
913 return NULL;
916 const struct got_error *
917 got_reflist_sort(struct got_reflist_head *refs,
918 got_ref_cmp_cb cmp_cb, void *cmp_arg)
920 const struct got_error *err = NULL;
921 struct got_reflist_entry *re, *tmp, *new;
922 struct got_reflist_head sorted;
924 TAILQ_INIT(&sorted);
926 TAILQ_FOREACH_SAFE(re, refs, entry, tmp) {
927 struct got_reference *ref = re->ref;
928 TAILQ_REMOVE(refs, re, entry);
929 free(re);
930 err = got_reflist_insert(&new, &sorted, ref, cmp_cb, cmp_arg);
931 if (err || new == NULL /* duplicate */)
932 got_ref_close(ref);
933 if (err)
934 return err;
937 TAILQ_CONCAT(refs, &sorted, entry);
938 return NULL;
941 static const struct got_error *
942 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
943 const char *subdir, struct got_repository *repo,
944 got_ref_cmp_cb cmp_cb, void *cmp_arg)
946 const struct got_error *err = NULL;
947 DIR *d = NULL;
948 char *path_subdir;
950 while (subdir[0] == '/')
951 subdir++;
953 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
954 return got_error_from_errno("asprintf");
956 d = opendir(path_subdir);
957 if (d == NULL)
958 goto done;
960 for (;;) {
961 struct dirent *dent;
962 struct got_reference *ref;
963 char *child;
964 int type;
966 dent = readdir(d);
967 if (dent == NULL)
968 break;
970 if (strcmp(dent->d_name, ".") == 0 ||
971 strcmp(dent->d_name, "..") == 0)
972 continue;
974 err = got_path_dirent_type(&type, path_subdir, dent);
975 if (err)
976 break;
978 switch (type) {
979 case DT_REG:
980 err = open_ref(&ref, path_refs, subdir, dent->d_name,
981 0);
982 if (err)
983 goto done;
984 if (ref) {
985 struct got_reflist_entry *new;
986 err = got_reflist_insert(&new, refs, ref,
987 cmp_cb, cmp_arg);
988 if (err || new == NULL /* duplicate */)
989 got_ref_close(ref);
990 if (err)
991 goto done;
993 break;
994 case DT_DIR:
995 if (asprintf(&child, "%s%s%s", subdir,
996 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
997 err = got_error_from_errno("asprintf");
998 break;
1000 err = gather_on_disk_refs(refs, path_refs, child, repo,
1001 cmp_cb, cmp_arg);
1002 free(child);
1003 break;
1004 default:
1005 break;
1008 done:
1009 if (d)
1010 closedir(d);
1011 free(path_subdir);
1012 return err;
1015 const struct got_error *
1016 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
1017 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
1019 const struct got_error *err;
1020 char *packed_refs_path = NULL, *path_refs = NULL;
1021 char *abs_namespace = NULL, *buf = NULL;
1022 const char *ondisk_ref_namespace = NULL;
1023 char *line = NULL;
1024 FILE *f = NULL;
1025 struct got_reference *ref;
1026 struct got_reflist_entry *new;
1028 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
1029 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
1030 if (path_refs == NULL) {
1031 err = got_error_from_errno("get_refs_dir_path");
1032 goto done;
1034 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0);
1035 if (err)
1036 goto done;
1037 err = got_reflist_insert(&new, refs, ref, cmp_cb, cmp_arg);
1038 if (err || new == NULL /* duplicate */)
1039 got_ref_close(ref);
1040 if (err && err->code != GOT_ERR_NOT_REF)
1041 goto done;
1042 } else {
1043 /* Try listing a single reference. */
1044 const char *refname = ref_namespace;
1045 path_refs = get_refs_dir_path(repo, refname);
1046 if (path_refs == NULL) {
1047 err = got_error_from_errno("get_refs_dir_path");
1048 goto done;
1050 err = open_ref(&ref, path_refs, "", refname, 0);
1051 if (err) {
1052 if (err->code != GOT_ERR_NOT_REF)
1053 goto done;
1054 /* Try to look up references in a given namespace. */
1055 } else {
1056 err = got_reflist_insert(&new, refs, ref,
1057 cmp_cb, cmp_arg);
1058 if (err || new == NULL /* duplicate */)
1059 got_ref_close(ref);
1060 return err;
1064 if (ref_namespace) {
1065 size_t len;
1066 /* Canonicalize the path to eliminate double-slashes if any. */
1067 if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
1068 err = got_error_from_errno("asprintf");
1069 goto done;
1071 len = strlen(abs_namespace) + 1;
1072 buf = malloc(len);
1073 if (buf == NULL) {
1074 err = got_error_from_errno("malloc");
1075 goto done;
1077 err = got_canonpath(abs_namespace, buf, len);
1078 if (err)
1079 goto done;
1080 ondisk_ref_namespace = buf;
1081 while (ondisk_ref_namespace[0] == '/')
1082 ondisk_ref_namespace++;
1083 if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
1084 ondisk_ref_namespace += 5;
1085 else if (strcmp(ondisk_ref_namespace, "refs") == 0)
1086 ondisk_ref_namespace = "";
1089 /* Gather on-disk refs before parsing packed-refs. */
1090 free(path_refs);
1091 path_refs = get_refs_dir_path(repo, "");
1092 if (path_refs == NULL) {
1093 err = got_error_from_errno("get_refs_dir_path");
1094 goto done;
1096 err = gather_on_disk_refs(refs, path_refs,
1097 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
1098 cmp_cb, cmp_arg);
1099 if (err)
1100 goto done;
1103 * The packed-refs file may contain redundant entries, in which
1104 * case on-disk refs take precedence.
1106 packed_refs_path = got_repo_get_path_packed_refs(repo);
1107 if (packed_refs_path == NULL) {
1108 err = got_error_from_errno("got_repo_get_path_packed_refs");
1109 goto done;
1112 f = fopen(packed_refs_path, "re");
1113 if (f) {
1114 size_t linesize = 0;
1115 ssize_t linelen;
1116 struct stat sb;
1118 if (fstat(fileno(f), &sb) == -1) {
1119 err = got_error_from_errno2("fstat", packed_refs_path);
1120 goto done;
1122 for (;;) {
1123 linelen = getline(&line, &linesize, f);
1124 if (linelen == -1) {
1125 if (feof(f))
1126 break;
1127 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1128 goto done;
1130 if (linelen > 0 && line[linelen - 1] == '\n')
1131 line[linelen - 1] = '\0';
1132 err = parse_packed_ref_line(&ref, NULL, line,
1133 sb.st_mtime);
1134 if (err)
1135 goto done;
1136 if (ref) {
1137 if (ref_namespace) {
1138 const char *name;
1139 name = got_ref_get_name(ref);
1140 if (!got_path_is_child(name,
1141 ref_namespace,
1142 strlen(ref_namespace))) {
1143 got_ref_close(ref);
1144 continue;
1147 err = got_reflist_insert(&new, refs, ref,
1148 cmp_cb, cmp_arg);
1149 if (err || new == NULL /* duplicate */)
1150 got_ref_close(ref);
1151 if (err)
1152 goto done;
1156 done:
1157 free(packed_refs_path);
1158 free(abs_namespace);
1159 free(buf);
1160 free(line);
1161 free(path_refs);
1162 if (f && fclose(f) == EOF && err == NULL)
1163 err = got_error_from_errno("fclose");
1164 return err;
1167 void
1168 got_ref_list_free(struct got_reflist_head *refs)
1170 struct got_reflist_entry *re;
1172 while ((re = TAILQ_FIRST(refs))) {
1173 TAILQ_REMOVE(refs, re, entry);
1174 got_ref_close(re->ref);
1175 free(re);
1180 int
1181 got_ref_is_symbolic(struct got_reference *ref)
1183 return (ref->flags & GOT_REF_IS_SYMBOLIC);
1186 const struct got_error *
1187 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1189 if (ref->flags & GOT_REF_IS_SYMBOLIC)
1190 return got_error(GOT_ERR_BAD_REF_TYPE);
1192 memcpy(ref->ref.ref.sha1, id->sha1, sizeof(ref->ref.ref.sha1));
1193 return NULL;
1196 const struct got_error *
1197 got_ref_change_symref(struct got_reference *ref, const char *refname)
1199 char *new_name;
1201 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1202 return got_error(GOT_ERR_BAD_REF_TYPE);
1204 new_name = strdup(refname);
1205 if (new_name == NULL)
1206 return got_error_from_errno("strdup");
1208 free(ref->ref.symref.ref);
1209 ref->ref.symref.ref = new_name;
1210 return NULL;
1213 const struct got_error *
1214 got_ref_change_symref_to_ref(struct got_reference *symref,
1215 struct got_object_id *id)
1217 if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1218 return got_error(GOT_ERR_BAD_REF_TYPE);
1220 symref->ref.ref.name = symref->ref.symref.name;
1221 memcpy(symref->ref.ref.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1222 symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1223 return NULL;
1226 const struct got_error *
1227 got_ref_write(struct got_reference *ref, struct got_repository *repo)
1229 const struct got_error *err = NULL, *unlock_err = NULL;
1230 const char *name = got_ref_get_name(ref);
1231 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1232 struct got_lockfile *lf = NULL;
1233 FILE *f = NULL;
1234 size_t n;
1235 struct stat sb;
1237 path_refs = get_refs_dir_path(repo, name);
1238 if (path_refs == NULL) {
1239 err = got_error_from_errno2("get_refs_dir_path", name);
1240 goto done;
1243 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1244 err = got_error_from_errno("asprintf");
1245 goto done;
1248 err = got_opentemp_named(&tmppath, &f, path);
1249 if (err) {
1250 char *parent;
1251 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1252 goto done;
1253 err = got_path_dirname(&parent, path);
1254 if (err)
1255 goto done;
1256 err = got_path_mkdir(parent);
1257 free(parent);
1258 if (err)
1259 goto done;
1260 err = got_opentemp_named(&tmppath, &f, path);
1261 if (err)
1262 goto done;
1265 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1266 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1267 if (n != strlen(ref->ref.symref.ref) + 6) {
1268 err = got_ferror(f, GOT_ERR_IO);
1269 goto done;
1271 } else {
1272 char hex[SHA1_DIGEST_STRING_LENGTH];
1273 if (got_sha1_digest_to_str(ref->ref.ref.sha1, hex,
1274 sizeof(hex)) == NULL) {
1275 err = got_error(GOT_ERR_BAD_REF_DATA);
1276 goto done;
1278 n = fprintf(f, "%s\n", hex);
1279 if (n != sizeof(hex)) {
1280 err = got_ferror(f, GOT_ERR_IO);
1281 goto done;
1285 if (ref->lf == NULL) {
1286 err = got_lockfile_lock(&lf, path, -1);
1287 if (err)
1288 goto done;
1291 /* XXX: check if old content matches our expectations? */
1293 if (stat(path, &sb) != 0) {
1294 if (errno != ENOENT) {
1295 err = got_error_from_errno2("stat", path);
1296 goto done;
1298 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1301 if (fchmod(fileno(f), sb.st_mode) != 0) {
1302 err = got_error_from_errno2("fchmod", tmppath);
1303 goto done;
1306 if (rename(tmppath, path) != 0) {
1307 err = got_error_from_errno3("rename", tmppath, path);
1308 goto done;
1310 free(tmppath);
1311 tmppath = NULL;
1313 if (stat(path, &sb) == -1) {
1314 err = got_error_from_errno2("stat", path);
1315 goto done;
1317 ref->mtime = sb.st_mtime;
1318 done:
1319 if (ref->lf == NULL && lf)
1320 unlock_err = got_lockfile_unlock(lf, -1);
1321 if (f) {
1322 if (fclose(f) == EOF && err == NULL)
1323 err = got_error_from_errno("fclose");
1325 free(path_refs);
1326 free(path);
1327 if (tmppath) {
1328 if (unlink(tmppath) != 0 && err == NULL)
1329 err = got_error_from_errno2("unlink", tmppath);
1330 free(tmppath);
1332 return err ? err : unlock_err;
1335 static const struct got_error *
1336 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1338 const struct got_error *err = NULL, *unlock_err = NULL;
1339 struct got_lockfile *lf = NULL;
1340 FILE *f = NULL, *tmpf = NULL;
1341 char *line = NULL, *packed_refs_path, *tmppath = NULL;
1342 size_t linesize = 0;
1343 struct got_reflist_head refs;
1344 int found_delref = 0;
1346 /* The packed-refs file does not cotain symbolic references. */
1347 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1348 return got_error(GOT_ERR_BAD_REF_DATA);
1350 TAILQ_INIT(&refs);
1352 packed_refs_path = got_repo_get_path_packed_refs(repo);
1353 if (packed_refs_path == NULL)
1354 return got_error_from_errno("got_repo_get_path_packed_refs");
1356 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path);
1357 if (err)
1358 goto done;
1360 if (delref->lf == NULL) {
1361 err = got_lockfile_lock(&lf, packed_refs_path, -1);
1362 if (err)
1363 goto done;
1366 f = fopen(packed_refs_path, "re");
1367 if (f == NULL) {
1368 err = got_error_from_errno2("fopen", packed_refs_path);
1369 goto done;
1371 for (;;) {
1372 ssize_t linelen;
1373 struct got_reference *ref;
1374 struct got_reflist_entry *new;
1376 linelen = getline(&line, &linesize, f);
1377 if (linelen == -1) {
1378 if (feof(f))
1379 break;
1380 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1381 goto done;
1383 if (linelen > 0 && line[linelen - 1] == '\n')
1384 line[linelen - 1] = '\0';
1385 err = parse_packed_ref_line(&ref, NULL, line, 0);
1386 if (err)
1387 goto done;
1388 if (ref == NULL)
1389 continue;
1391 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1392 memcmp(ref->ref.ref.sha1, delref->ref.ref.sha1,
1393 sizeof(delref->ref.ref.sha1)) == 0) {
1394 found_delref = 1;
1395 got_ref_close(ref);
1396 continue;
1399 err = got_reflist_insert(&new, &refs, ref,
1400 got_ref_cmp_by_name, NULL);
1401 if (err || new == NULL /* duplicate */)
1402 got_ref_close(ref);
1403 if (err)
1404 goto done;
1407 if (found_delref) {
1408 struct got_reflist_entry *re;
1409 size_t n;
1410 struct stat sb;
1412 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1413 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1414 err = got_ferror(f, GOT_ERR_IO);
1415 goto done;
1418 TAILQ_FOREACH(re, &refs, entry) {
1419 char hex[SHA1_DIGEST_STRING_LENGTH];
1421 if (got_sha1_digest_to_str(re->ref->ref.ref.sha1, hex,
1422 sizeof(hex)) == NULL) {
1423 err = got_error(GOT_ERR_BAD_REF_DATA);
1424 goto done;
1426 n = fprintf(tmpf, "%s ", hex);
1427 if (n != sizeof(hex)) {
1428 err = got_ferror(f, GOT_ERR_IO);
1429 goto done;
1431 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1432 if (n != strlen(re->ref->ref.ref.name) + 1) {
1433 err = got_ferror(f, GOT_ERR_IO);
1434 goto done;
1438 if (fflush(tmpf) != 0) {
1439 err = got_error_from_errno("fflush");
1440 goto done;
1443 if (fstat(fileno(f), &sb) != 0) {
1444 if (errno != ENOENT) {
1445 err = got_error_from_errno2("fstat",
1446 packed_refs_path);
1447 goto done;
1449 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1452 if (fchmod(fileno(tmpf), sb.st_mode) != 0) {
1453 err = got_error_from_errno2("fchmod", tmppath);
1454 goto done;
1457 if (rename(tmppath, packed_refs_path) != 0) {
1458 err = got_error_from_errno3("rename", tmppath,
1459 packed_refs_path);
1460 goto done;
1463 done:
1464 if (delref->lf == NULL && lf)
1465 unlock_err = got_lockfile_unlock(lf, -1);
1466 if (f) {
1467 if (fclose(f) == EOF && err == NULL)
1468 err = got_error_from_errno("fclose");
1470 if (tmpf) {
1471 unlink(tmppath);
1472 if (fclose(tmpf) == EOF && err == NULL)
1473 err = got_error_from_errno("fclose");
1475 free(tmppath);
1476 free(packed_refs_path);
1477 free(line);
1478 got_ref_list_free(&refs);
1479 return err ? err : unlock_err;
1482 static const struct got_error *
1483 delete_loose_ref(struct got_reference *ref, struct got_repository *repo)
1485 const struct got_error *err = NULL, *unlock_err = NULL;
1486 const char *name = got_ref_get_name(ref);
1487 char *path_refs = NULL, *path = NULL;
1488 struct got_lockfile *lf = NULL;
1490 path_refs = get_refs_dir_path(repo, name);
1491 if (path_refs == NULL) {
1492 err = got_error_from_errno2("get_refs_dir_path", name);
1493 goto done;
1496 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1497 err = got_error_from_errno("asprintf");
1498 goto done;
1501 if (ref->lf == NULL) {
1502 err = got_lockfile_lock(&lf, path, -1);
1503 if (err)
1504 goto done;
1507 /* XXX: check if old content matches our expectations? */
1509 if (unlink(path) != 0)
1510 err = got_error_from_errno2("unlink", path);
1511 done:
1512 if (ref->lf == NULL && lf)
1513 unlock_err = got_lockfile_unlock(lf, -1);
1515 free(path_refs);
1516 free(path);
1517 return err ? err : unlock_err;
1520 const struct got_error *
1521 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1523 const struct got_error *err = NULL;
1524 struct got_reference *ref2;
1526 if (ref->flags & GOT_REF_IS_PACKED) {
1527 err = delete_packed_ref(ref, repo);
1528 if (err)
1529 return err;
1531 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1532 if (err) {
1533 if (err->code == GOT_ERR_NOT_REF)
1534 return NULL;
1535 return err;
1538 err = delete_loose_ref(ref2, repo);
1539 got_ref_close(ref2);
1540 return err;
1541 } else {
1542 err = delete_loose_ref(ref, repo);
1543 if (err)
1544 return err;
1546 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1547 if (err) {
1548 if (err->code == GOT_ERR_NOT_REF)
1549 return NULL;
1550 return err;
1553 err = delete_packed_ref(ref2, repo);
1554 got_ref_close(ref2);
1555 return err;
1559 const struct got_error *
1560 got_ref_unlock(struct got_reference *ref)
1562 const struct got_error *err;
1563 err = got_lockfile_unlock(ref->lf, -1);
1564 ref->lf = NULL;
1565 return err;
1568 struct got_reflist_object_id_map {
1569 struct got_object_idset *idset;
1572 struct got_reflist_object_id_map_entry {
1573 struct got_reflist_head refs;
1576 static const struct got_error *
1577 add_object_id_map_entry(struct got_object_idset *idset,
1578 struct got_object_id *id, struct got_reflist_entry *re)
1580 const struct got_error *err = NULL;
1581 struct got_reflist_object_id_map_entry *ent;
1582 struct got_reflist_entry *new;
1584 ent = got_object_idset_get(idset, id);
1585 if (ent == NULL) {
1586 ent = malloc(sizeof(*ent));
1587 if (ent == NULL)
1588 return got_error_from_errno("malloc");
1590 TAILQ_INIT(&ent->refs);
1591 err = got_object_idset_add(idset, id, ent);
1592 if (err) {
1593 free(ent);
1594 return err;
1598 err = got_reflist_entry_dup(&new, re);
1599 if (err)
1600 return err;
1602 TAILQ_INSERT_TAIL(&ent->refs, new, entry);
1603 return NULL;
1606 const struct got_error *
1607 got_reflist_object_id_map_create(struct got_reflist_object_id_map **map,
1608 struct got_reflist_head *refs, struct got_repository *repo)
1610 const struct got_error *err = NULL;
1611 struct got_object_idset *idset;
1612 struct got_object_id *id = NULL;
1613 struct got_reflist_entry *re;
1615 idset = got_object_idset_alloc();
1616 if (idset == NULL)
1617 return got_error_from_errno("got_object_idset_alloc");
1619 *map = malloc(sizeof(**map));
1620 if (*map == NULL) {
1621 got_object_idset_free(idset);
1622 return got_error_from_errno("malloc");
1624 (*map)->idset = idset;
1626 TAILQ_FOREACH(re, refs, entry) {
1627 struct got_tag_object *tag = NULL;
1629 err = got_ref_resolve(&id, repo, re->ref);
1630 if (err)
1631 goto done;
1633 err = add_object_id_map_entry(idset, id, re);
1634 if (err)
1635 goto done;
1637 if (strstr(got_ref_get_name(re->ref), "/tags/") == NULL) {
1638 free(id);
1639 id = NULL;
1640 continue;
1643 err = got_object_open_as_tag(&tag, repo, id);
1644 if (err) {
1645 if (err->code != GOT_ERR_OBJ_TYPE)
1646 goto done;
1647 /* Ref points at something other than a tag. */
1648 err = NULL;
1649 tag = NULL;
1650 free(id);
1651 id = NULL;
1652 continue;
1655 err = add_object_id_map_entry(idset,
1656 got_object_tag_get_object_id(tag), re);
1657 got_object_tag_close(tag);
1658 if (err)
1659 goto done;
1661 free(id);
1662 id = NULL;
1664 done:
1665 free(id);
1666 if (err) {
1667 got_reflist_object_id_map_free(*map);
1668 *map = NULL;
1670 return err;
1673 struct got_reflist_head *
1674 got_reflist_object_id_map_lookup(struct got_reflist_object_id_map *map,
1675 struct got_object_id *id)
1677 struct got_reflist_object_id_map_entry *ent;
1678 ent = got_object_idset_get(map->idset, id);
1679 if (ent)
1680 return &ent->refs;
1681 return NULL;
1684 static const struct got_error *
1685 free_id_map_entry(struct got_object_id *id, void *data, void *arg)
1687 struct got_reflist_object_id_map_entry *ent = data;
1689 got_ref_list_free(&ent->refs);
1690 free(ent);
1691 return NULL;
1694 void
1695 got_reflist_object_id_map_free(struct got_reflist_object_id_map *map)
1697 got_object_idset_for_each(map->idset, free_id_map_entry, NULL);
1698 got_object_idset_free(map->idset);
1699 free(map);