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 "got_compat.h"
19 #include <sys/types.h>
20 #include <sys/queue.h>
21 #include <sys/stat.h>
23 #include <errno.h>
24 #include <dirent.h>
25 #include <limits.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <zlib.h>
31 #include <time.h>
32 #include <libgen.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_hash.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 struct got_object_id id;
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 /*
92 * Cached timestamp for got_ref_cmp_by_commit_timestamp_descending()
93 * and got_ref_cmp_tags().
94 */
95 time_t committer_time;
96 };
98 static const struct got_error *
99 alloc_ref(struct got_reference **ref, const char *name,
100 struct got_object_id *id, int flags, time_t mtime)
102 const struct got_error *err = NULL;
104 *ref = calloc(1, sizeof(**ref));
105 if (*ref == NULL)
106 return got_error_from_errno("calloc");
108 memcpy(&(*ref)->ref.ref.id, id, sizeof((*ref)->ref.ref.id));
109 (*ref)->flags = flags;
110 (*ref)->ref.ref.name = strdup(name);
111 (*ref)->mtime = mtime;
112 if ((*ref)->ref.ref.name == NULL) {
113 err = got_error_from_errno("strdup");
114 got_ref_close(*ref);
115 *ref = NULL;
117 return err;
120 static const struct got_error *
121 alloc_symref(struct got_reference **ref, const char *name,
122 const char *target_ref, int flags)
124 const struct got_error *err = NULL;
126 *ref = calloc(1, sizeof(**ref));
127 if (*ref == NULL)
128 return got_error_from_errno("calloc");
130 (*ref)->flags = GOT_REF_IS_SYMBOLIC | flags;
131 (*ref)->ref.symref.name = strdup(name);
132 if ((*ref)->ref.symref.name == NULL) {
133 err = got_error_from_errno("strdup");
134 got_ref_close(*ref);
135 *ref = NULL;
136 return err;
138 (*ref)->ref.symref.ref = strdup(target_ref);
139 if ((*ref)->ref.symref.ref == NULL) {
140 err = got_error_from_errno("strdup");
141 got_ref_close(*ref);
142 *ref = NULL;
144 return err;
147 static const struct got_error *
148 parse_symref(struct got_reference **ref, const char *name, const char *line)
150 if (line[0] == '\0')
151 return got_error(GOT_ERR_BAD_REF_DATA);
153 return alloc_symref(ref, name, line, 0);
156 static const struct got_error *
157 parse_ref_line(struct got_reference **ref, const char *name, const char *line,
158 time_t mtime, enum got_hash_algorithm algo)
160 struct got_object_id id;
162 if (strncmp(line, "ref: ", 5) == 0) {
163 line += 5;
164 return parse_symref(ref, name, line);
167 if (!got_parse_object_id(&id, line, algo))
168 return got_error(GOT_ERR_BAD_REF_DATA);
170 return alloc_ref(ref, name, &id, 0, mtime);
173 static const struct got_error *
174 parse_ref_file(struct got_reference **ref, const char *name,
175 const char *absname, const char *abspath, int lock,
176 enum got_hash_algorithm algo)
178 const struct got_error *err = NULL;
179 FILE *f;
180 char *line = NULL;
181 size_t linesize = 0;
182 ssize_t linelen;
183 struct got_lockfile *lf = NULL;
184 struct stat sb;
186 if (lock) {
187 err = got_lockfile_lock(&lf, abspath, -1);
188 if (err) {
189 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
190 err = got_error_not_ref(name);
191 return err;
195 f = fopen(abspath, "rbe");
196 if (f == NULL) {
197 if (errno != ENOTDIR && errno != ENOENT)
198 err = got_error_from_errno2("fopen", abspath);
199 else
200 err = got_error_not_ref(name);
201 if (lock)
202 got_lockfile_unlock(lf, -1);
203 return err;
205 if (fstat(fileno(f), &sb) == -1) {
206 err = got_error_from_errno2("fstat", abspath);
207 goto done;
210 linelen = getline(&line, &linesize, f);
211 if (linelen == -1) {
212 if (feof(f))
213 err = NULL; /* ignore empty files (could be locks) */
214 else {
215 if (errno == EISDIR)
216 err = got_error(GOT_ERR_NOT_REF);
217 else if (ferror(f))
218 err = got_ferror(f, GOT_ERR_IO);
219 else
220 err = got_error_from_errno2("getline", abspath);
222 if (lock)
223 got_lockfile_unlock(lf, -1);
224 goto done;
226 while (linelen > 0 && line[linelen - 1] == '\n') {
227 line[linelen - 1] = '\0';
228 linelen--;
231 err = parse_ref_line(ref, absname, line, sb.st_mtime, algo);
232 if (lock) {
233 if (err)
234 got_lockfile_unlock(lf, -1);
235 else {
236 if (*ref)
237 (*ref)->lf = lf;
238 else
239 got_lockfile_unlock(lf, -1);
242 done:
243 free(line);
244 if (fclose(f) == EOF && err == NULL) {
245 err = got_error_from_errno("fclose");
246 if (*ref) {
247 if (lock)
248 got_ref_unlock(*ref);
249 got_ref_close(*ref);
250 *ref = NULL;
253 return err;
256 static int
257 is_well_known_ref(const char *refname)
259 return (strcmp(refname, GOT_REF_HEAD) == 0 ||
260 strcmp(refname, GOT_REF_ORIG_HEAD) == 0 ||
261 strcmp(refname, GOT_REF_MERGE_HEAD) == 0 ||
262 strcmp(refname, GOT_REF_FETCH_HEAD) == 0);
265 static char *
266 get_refs_dir_path(struct got_repository *repo, const char *refname)
268 if (is_well_known_ref(refname) || strncmp(refname, "refs/", 5) == 0)
269 return strdup(got_repo_get_path_git_dir(repo));
271 return got_repo_get_path_refs(repo);
274 const struct got_error *
275 got_ref_alloc(struct got_reference **ref, const char *name,
276 struct got_object_id *id)
278 if (!got_ref_name_is_valid(name))
279 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
281 return alloc_ref(ref, name, id, 0, 0);
284 const struct got_error *
285 got_ref_alloc_symref(struct got_reference **ref, const char *name,
286 struct got_reference *target_ref)
288 if (!got_ref_name_is_valid(name))
289 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
291 return alloc_symref(ref, name, got_ref_get_name(target_ref), 0);
294 static const struct got_error *
295 parse_packed_ref_line(struct got_reference **ref, const char *abs_refname,
296 const char *line, time_t mtime, enum got_hash_algorithm algo)
298 struct got_object_id id;
299 const char *name;
301 *ref = NULL;
303 if (line[0] == '#' || line[0] == '^')
304 return NULL;
306 if (!got_parse_object_id(&id, line, algo))
307 return got_error(GOT_ERR_BAD_REF_DATA);
309 if (abs_refname) {
310 if (strcmp(line + SHA1_DIGEST_STRING_LENGTH, abs_refname) != 0)
311 return NULL;
312 name = abs_refname;
313 } else
314 name = line + SHA1_DIGEST_STRING_LENGTH;
316 return alloc_ref(ref, name, &id, GOT_REF_IS_PACKED, mtime);
319 static const struct got_error *
320 open_packed_ref(struct got_reference **ref, FILE *f, const char **subdirs,
321 int nsubdirs, const char *refname, time_t mtime,
322 enum got_hash_algorithm algo)
324 const struct got_error *err = NULL;
325 char *abs_refname;
326 char *line = NULL;
327 size_t linesize = 0;
328 ssize_t linelen;
329 int i, ref_is_absolute = (strncmp(refname, "refs/", 5) == 0);
331 *ref = NULL;
333 if (ref_is_absolute)
334 abs_refname = (char *)refname;
335 do {
336 linelen = getline(&line, &linesize, f);
337 if (linelen == -1) {
338 if (feof(f))
339 break;
340 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
341 break;
343 if (linelen > 0 && line[linelen - 1] == '\n')
344 line[linelen - 1] = '\0';
345 for (i = 0; i < nsubdirs; i++) {
346 if (!ref_is_absolute &&
347 asprintf(&abs_refname, "refs/%s/%s", subdirs[i],
348 refname) == -1)
349 return got_error_from_errno("asprintf");
350 err = parse_packed_ref_line(ref, abs_refname, line,
351 mtime, algo);
352 if (!ref_is_absolute)
353 free(abs_refname);
354 if (err || *ref != NULL)
355 break;
357 if (err)
358 break;
359 } while (*ref == NULL);
360 free(line);
362 return err;
365 static const struct got_error *
366 open_ref(struct got_reference **ref, const char *path_refs, const char *subdir,
367 const char *name, int lock, enum got_hash_algorithm algo)
369 const struct got_error *err = NULL;
370 char *path = NULL;
371 char *absname = NULL;
372 int ref_is_absolute = (strncmp(name, "refs/", 5) == 0);
373 int ref_is_well_known = (subdir[0] == '\0' && is_well_known_ref(name));
375 *ref = NULL;
377 if (!got_ref_name_is_valid(name))
378 return got_error_path(name, GOT_ERR_BAD_REF_NAME);
380 if (ref_is_absolute || ref_is_well_known) {
381 if (asprintf(&path, "%s/%s", path_refs, name) == -1)
382 return got_error_from_errno("asprintf");
383 absname = (char *)name;
384 } else {
385 if (asprintf(&path, "%s/%s%s%s", path_refs, subdir,
386 subdir[0] ? "/" : "", name) == -1)
387 return got_error_from_errno("asprintf");
389 if (asprintf(&absname, "refs/%s%s%s",
390 subdir, subdir[0] ? "/" : "", name) == -1) {
391 err = got_error_from_errno("asprintf");
392 goto done;
396 err = parse_ref_file(ref, name, absname, path, lock, algo);
397 done:
398 if (!ref_is_absolute && !ref_is_well_known)
399 free(absname);
400 free(path);
401 return err;
404 const struct got_error *
405 got_ref_open(struct got_reference **ref, struct got_repository *repo,
406 const char *refname, int lock)
408 const struct got_error *err = NULL;
409 char *packed_refs_path = NULL, *path_refs = NULL;
410 const char *subdirs[] = {
411 GOT_REF_HEADS, GOT_REF_TAGS, GOT_REF_REMOTES
412 };
413 size_t i;
414 int well_known = is_well_known_ref(refname);
415 struct got_lockfile *lf = NULL;
417 *ref = NULL;
419 path_refs = get_refs_dir_path(repo, refname);
420 if (path_refs == NULL) {
421 err = got_error_from_errno2("get_refs_dir_path", refname);
422 goto done;
425 if (well_known) {
426 err = open_ref(ref, path_refs, "", refname, lock,
427 got_repo_get_object_format(repo));
428 } else {
429 FILE *f;
431 /* Search on-disk refs before packed refs! */
432 for (i = 0; i < nitems(subdirs); i++) {
433 err = open_ref(ref, path_refs, subdirs[i], refname,
434 lock, got_repo_get_object_format(repo));
435 if ((err && err->code != GOT_ERR_NOT_REF) || *ref)
436 goto done;
439 packed_refs_path = got_repo_get_path_packed_refs(repo);
440 if (packed_refs_path == NULL) {
441 err = got_error_from_errno(
442 "got_repo_get_path_packed_refs");
443 goto done;
446 if (lock) {
447 err = got_lockfile_lock(&lf, packed_refs_path, -1);
448 if (err)
449 goto done;
451 f = fopen(packed_refs_path, "rbe");
452 if (f != NULL) {
453 struct stat sb;
454 if (fstat(fileno(f), &sb) == -1) {
455 err = got_error_from_errno2("fstat",
456 packed_refs_path);
457 goto done;
459 err = open_packed_ref(ref, f, subdirs, nitems(subdirs),
460 refname, sb.st_mtime,
461 got_repo_get_object_format(repo));
462 if (!err) {
463 if (fclose(f) == EOF) {
464 err = got_error_from_errno("fclose");
465 got_ref_close(*ref);
466 *ref = NULL;
467 } else if (*ref)
468 (*ref)->lf = lf;
472 done:
473 if (!err && *ref == NULL)
474 err = got_error_not_ref(refname);
475 if (err && lf)
476 got_lockfile_unlock(lf, -1);
477 free(packed_refs_path);
478 free(path_refs);
479 return err;
482 void
483 got_ref_close(struct got_reference *ref)
485 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
486 free(ref->ref.symref.name);
487 free(ref->ref.symref.ref);
488 } else
489 free(ref->ref.ref.name);
490 free(ref);
493 struct got_reference *
494 got_ref_dup(struct got_reference *ref)
496 struct got_reference *ret;
498 ret = calloc(1, sizeof(*ret));
499 if (ret == NULL)
500 return NULL;
502 ret->flags = ref->flags;
503 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
504 ret->ref.symref.name = strdup(ref->ref.symref.name);
505 if (ret->ref.symref.name == NULL) {
506 free(ret);
507 return NULL;
509 ret->ref.symref.ref = strdup(ref->ref.symref.ref);
510 if (ret->ref.symref.ref == NULL) {
511 free(ret->ref.symref.name);
512 free(ret);
513 return NULL;
515 } else {
516 ret->ref.ref.name = strdup(ref->ref.ref.name);
517 if (ret->ref.ref.name == NULL) {
518 free(ret);
519 return NULL;
521 memcpy(&ret->ref.ref.id, &ref->ref.ref.id,
522 sizeof(ret->ref.ref.id));
525 return ret;
528 const struct got_error *
529 got_reflist_entry_dup(struct got_reflist_entry **newp,
530 struct got_reflist_entry *re)
532 const struct got_error *err = NULL;
533 struct got_reflist_entry *new;
535 *newp = NULL;
537 new = malloc(sizeof(*new));
538 if (new == NULL)
539 return got_error_from_errno("malloc");
541 new->ref = got_ref_dup(re->ref);
542 if (new->ref == NULL) {
543 err = got_error_from_errno("got_ref_dup");
544 free(new);
545 return err;
548 *newp = new;
549 return NULL;
552 const struct got_error *
553 got_ref_resolve_symbolic(struct got_reference **resolved,
554 struct got_repository *repo, struct got_reference *ref)
556 struct got_reference *nextref;
557 const struct got_error *err;
559 err = got_ref_open(&nextref, repo, ref->ref.symref.ref, 0);
560 if (err)
561 return err;
563 if (nextref->flags & GOT_REF_IS_SYMBOLIC)
564 err = got_ref_resolve_symbolic(resolved, repo, nextref);
565 else
566 *resolved = got_ref_dup(nextref);
568 got_ref_close(nextref);
569 return err;
572 static const struct got_error *
573 ref_resolve(struct got_object_id **id, struct got_repository *repo,
574 struct got_reference *ref, int recursion)
576 const struct got_error *err;
578 if (recursion <= 0)
579 return got_error_msg(GOT_ERR_RECURSION,
580 "reference recursion limit reached");
582 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
583 struct got_reference *resolved = NULL;
584 err = got_ref_resolve_symbolic(&resolved, repo, ref);
585 if (err == NULL)
586 err = ref_resolve(id, repo, resolved, --recursion);
587 if (resolved)
588 got_ref_close(resolved);
589 return err;
592 *id = calloc(1, sizeof(**id));
593 if (*id == NULL)
594 return got_error_from_errno("calloc");
595 memcpy(*id, &ref->ref.ref.id, sizeof(**id));
596 return NULL;
599 const struct got_error *
600 got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
601 struct got_reference *ref)
603 return ref_resolve(id, repo, ref, GOT_REF_RECURSE_MAX);
606 char *
607 got_ref_to_str(struct got_reference *ref)
609 char *str;
611 if (ref->flags & GOT_REF_IS_SYMBOLIC)
612 return strdup(ref->ref.symref.ref);
614 if (got_object_id_str(&str, &ref->ref.ref.id) != NULL)
615 return NULL;
617 return str;
620 const char *
621 got_ref_get_name(struct got_reference *ref)
623 if (ref->flags & GOT_REF_IS_SYMBOLIC)
624 return ref->ref.symref.name;
626 return ref->ref.ref.name;
629 const char *
630 got_ref_get_symref_target(struct got_reference *ref)
632 if (ref->flags & GOT_REF_IS_SYMBOLIC)
633 return ref->ref.symref.ref;
635 return NULL;
638 time_t
639 got_ref_get_mtime(struct got_reference *ref)
641 return ref->mtime;
644 const struct got_error *
645 got_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
646 struct got_reference* re2)
648 const char *name1 = got_ref_get_name(re1);
649 const char *name2 = got_ref_get_name(re2);
651 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
652 return NULL;
655 static const struct got_error *
656 get_committer_time(struct got_reference *ref, struct got_repository *repo)
658 const struct got_error *err = NULL;
659 int obj_type;
660 struct got_commit_object *commit = NULL;
661 struct got_tag_object *tag = NULL;
662 struct got_object_id *id = NULL;
664 err = got_ref_resolve(&id, repo, ref);
665 if (err)
666 return err;
668 err = got_object_get_type(&obj_type, repo, id);
669 if (err)
670 goto done;
672 switch (obj_type) {
673 case GOT_OBJ_TYPE_COMMIT:
674 err = got_object_open_as_commit(&commit, repo, id);
675 if (err)
676 goto done;
677 ref->committer_time =
678 got_object_commit_get_committer_time(commit);
679 break;
680 case GOT_OBJ_TYPE_TAG:
681 err = got_object_open_as_tag(&tag, repo, id);
682 if (err)
683 goto done;
684 ref->committer_time = got_object_tag_get_tagger_time(tag);
685 break;
686 default:
687 /* best effort for other object types */
688 ref->committer_time = got_ref_get_mtime(ref);
689 break;
691 done:
692 free(id);
693 if (commit)
694 got_object_commit_close(commit);
695 if (tag)
696 got_object_tag_close(tag);
697 return err;
700 const struct got_error *
701 got_ref_cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
702 struct got_reference *ref2)
704 const struct got_error *err = NULL;
705 struct got_repository *repo = arg;
707 *cmp = 0;
709 if (ref1->committer_time == 0) {
710 err = get_committer_time(ref1, repo);
711 if (err)
712 return err;
714 if (ref2->committer_time == 0) {
715 err = get_committer_time(ref2, repo);
716 if (err)
717 return err;
720 /* Put latest tags first. */
721 if (ref1->committer_time < ref2->committer_time)
722 *cmp = 1;
723 else if (ref1->committer_time > ref2->committer_time)
724 *cmp = -1;
725 else
726 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
728 return err;
731 const struct got_error *
732 got_ref_cmp_by_commit_timestamp_descending(void *arg, int *cmp,
733 struct got_reference *ref1, struct got_reference *ref2)
735 const struct got_error *err = NULL;
736 struct got_repository *repo = arg;
738 *cmp = 0;
740 if (ref1->committer_time == 0) {
741 err = get_committer_time(ref1, repo);
742 if (err)
743 return err;
745 if (ref2->committer_time == 0) {
746 err = get_committer_time(ref2, repo);
747 if (err)
748 return err;
751 if (ref1->committer_time < ref2->committer_time)
752 *cmp = 1;
753 else if (ref2->committer_time < ref1->committer_time)
754 *cmp = -1;
755 else
756 return got_ref_cmp_by_name(arg, cmp, ref1, ref2);
758 return err;
761 const struct got_error *
762 got_reflist_insert(struct got_reflist_entry **newp,
763 struct got_reflist_head *refs, struct got_reference *ref,
764 got_ref_cmp_cb cmp_cb, void *cmp_arg)
766 const struct got_error *err;
767 struct got_reflist_entry *new, *re;
768 int cmp;
770 *newp = NULL;
772 if (cmp_cb != got_ref_cmp_by_name && (ref->flags & GOT_REF_IS_PACKED)) {
773 /*
774 * If we are not sorting elements by name then we must still
775 * detect collisions between a packed ref and an on-disk ref
776 * using the same name. On-disk refs take precedence and are
777 * already present on the list before packed refs get added.
778 */
779 TAILQ_FOREACH(re, refs, entry) {
780 err = got_ref_cmp_by_name(NULL, &cmp, re->ref, ref);
781 if (err)
782 return err;
783 if (cmp == 0)
784 return NULL;
788 new = malloc(sizeof(*new));
789 if (new == NULL)
790 return got_error_from_errno("malloc");
791 new->ref = ref;
792 *newp = new;
794 /*
795 * We must de-duplicate entries on insert because packed-refs may
796 * contain redundant entries. On-disk refs take precedence.
797 * This code assumes that on-disk revs are read before packed-refs.
798 * We're iterating the list anyway, so insert elements sorted by name.
800 * Many callers will provide paths in a somewhat sorted order.
801 * Iterating backwards from the tail of the list should be more
802 * efficient than traversing through the entire list each time
803 * an element is inserted.
804 */
805 re = TAILQ_LAST(refs, got_reflist_head);
806 while (re) {
807 err = (*cmp_cb)(cmp_arg, &cmp, re->ref, new->ref);
808 if (err)
809 return err;
810 if (cmp == 0) {
811 /* duplicate */
812 free(new);
813 *newp = NULL;
814 return NULL;
815 } else if (cmp < 0) {
816 TAILQ_INSERT_AFTER(refs, re, new, entry);
817 return NULL;
819 re = TAILQ_PREV(re, got_reflist_head, entry);
822 TAILQ_INSERT_HEAD(refs, new, entry);
823 return NULL;
826 const struct got_error *
827 got_reflist_sort(struct got_reflist_head *refs,
828 got_ref_cmp_cb cmp_cb, void *cmp_arg)
830 const struct got_error *err = NULL;
831 struct got_reflist_entry *re, *tmp, *new;
832 struct got_reflist_head sorted;
834 TAILQ_INIT(&sorted);
836 TAILQ_FOREACH_SAFE(re, refs, entry, tmp) {
837 struct got_reference *ref = re->ref;
838 TAILQ_REMOVE(refs, re, entry);
839 free(re);
840 err = got_reflist_insert(&new, &sorted, ref, cmp_cb, cmp_arg);
841 if (err || new == NULL /* duplicate */)
842 got_ref_close(ref);
843 if (err)
844 return err;
847 TAILQ_CONCAT(refs, &sorted, entry);
848 return NULL;
851 static const struct got_error *
852 gather_on_disk_refs(struct got_reflist_head *refs, const char *path_refs,
853 const char *subdir, struct got_repository *repo,
854 got_ref_cmp_cb cmp_cb, void *cmp_arg)
856 const struct got_error *err = NULL;
857 DIR *d = NULL;
858 char *path_subdir;
859 struct got_reference *ref;
860 struct got_reflist_entry *new;
862 while (subdir[0] == '/')
863 subdir++;
865 if (asprintf(&path_subdir, "%s/%s", path_refs, subdir) == -1)
866 return got_error_from_errno("asprintf");
868 d = opendir(path_subdir);
869 if (d == NULL) {
870 char *refname;
872 if (errno != ENOTDIR)
873 goto done;
875 /* This could be a regular on-disk reference file. */
876 free(path_subdir);
877 err = got_path_dirname(&path_subdir, subdir);
878 if (err)
879 return err;
880 err = got_path_basename(&refname, subdir);
881 if (err) {
882 free(path_subdir);
883 return err;
885 err = open_ref(&ref, path_refs, path_subdir, refname,
886 0, got_repo_get_object_format(repo));
887 free(path_subdir);
888 free(refname);
889 if (err) {
890 if (err->code == GOT_ERR_NOT_REF)
891 return NULL;
892 return err;
894 err = got_reflist_insert(&new, refs, ref,
895 cmp_cb, cmp_arg);
896 if (err || new == NULL /* duplicate */)
897 got_ref_close(ref);
898 return err;
901 for (;;) {
902 struct dirent *dent;
903 char *child;
904 int type;
906 dent = readdir(d);
907 if (dent == NULL)
908 break;
910 if (strcmp(dent->d_name, ".") == 0 ||
911 strcmp(dent->d_name, "..") == 0)
912 continue;
914 err = got_path_dirent_type(&type, path_subdir, dent);
915 if (err)
916 break;
918 switch (type) {
919 case DT_REG:
920 err = open_ref(&ref, path_refs, subdir, dent->d_name,
921 0, got_repo_get_object_format(repo));
922 if (err && err->code == GOT_ERR_BAD_REF_NAME)
923 break;
924 if (err)
925 goto done;
926 if (ref) {
927 err = got_reflist_insert(&new, refs, ref,
928 cmp_cb, cmp_arg);
929 if (err || new == NULL /* duplicate */)
930 got_ref_close(ref);
931 if (err)
932 goto done;
934 break;
935 case DT_DIR:
936 if (asprintf(&child, "%s%s%s", subdir,
937 subdir[0] == '\0' ? "" : "/", dent->d_name) == -1) {
938 err = got_error_from_errno("asprintf");
939 break;
941 err = gather_on_disk_refs(refs, path_refs, child, repo,
942 cmp_cb, cmp_arg);
943 free(child);
944 break;
945 default:
946 break;
949 done:
950 if (d)
951 closedir(d);
952 free(path_subdir);
953 return err;
956 static int
957 match_packed_ref(struct got_reference *ref, const char *ref_namespace)
959 const char *name = got_ref_get_name(ref);
960 int namespace_is_absolute = (strncmp(ref_namespace, "refs/", 5) == 0);
962 if (namespace_is_absolute) {
963 return (strcmp(name, ref_namespace) == 0 ||
964 got_path_is_child(name, ref_namespace,
965 strlen(ref_namespace)));
968 /* Match all "subdirectories" as we do with on-disk refs. */
969 while (*name != '\0') {
970 while (*name == '/')
971 name++;
972 if (strcmp(name, ref_namespace) == 0 ||
973 got_path_is_child(name, ref_namespace,
974 strlen(ref_namespace)))
975 return 1;
976 while (*name != '\0' && *name != '/')
977 name++;
980 return 0;
983 const struct got_error *
984 got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
985 const char *ref_namespace, got_ref_cmp_cb cmp_cb, void *cmp_arg)
987 const struct got_error *err;
988 char *packed_refs_path = NULL, *path_refs = NULL;
989 char *abs_namespace = NULL, *buf = NULL;
990 const char *ondisk_ref_namespace = NULL;
991 char *line = NULL;
992 FILE *f = NULL;
993 struct got_reference *ref;
994 struct got_reflist_entry *new;
996 if (ref_namespace == NULL || ref_namespace[0] == '\0') {
997 path_refs = get_refs_dir_path(repo, GOT_REF_HEAD);
998 if (path_refs == NULL) {
999 err = got_error_from_errno("get_refs_dir_path");
1000 goto done;
1002 err = open_ref(&ref, path_refs, "", GOT_REF_HEAD, 0,
1003 got_repo_get_object_format(repo));
1004 if (err)
1005 goto done;
1006 err = got_reflist_insert(&new, refs, ref, cmp_cb, cmp_arg);
1007 if (err || new == NULL /* duplicate */)
1008 got_ref_close(ref);
1009 if (err && err->code != GOT_ERR_NOT_REF)
1010 goto done;
1011 } else {
1012 /* Try listing a single reference. */
1013 err = got_ref_open(&ref, repo, ref_namespace, 0);
1014 if (err) {
1015 if (err->code != GOT_ERR_NOT_REF)
1016 goto done;
1017 /* Try to look up references in a given namespace. */
1018 } else {
1019 err = got_reflist_insert(&new, refs, ref,
1020 cmp_cb, cmp_arg);
1021 if (err || new == NULL /* duplicate */)
1022 got_ref_close(ref);
1023 return err;
1027 if (ref_namespace) {
1028 size_t len;
1029 /* Canonicalize the path to eliminate double-slashes if any. */
1030 if (asprintf(&abs_namespace, "/%s", ref_namespace) == -1) {
1031 err = got_error_from_errno("asprintf");
1032 goto done;
1034 len = strlen(abs_namespace) + 1;
1035 buf = malloc(len);
1036 if (buf == NULL) {
1037 err = got_error_from_errno("malloc");
1038 goto done;
1040 err = got_canonpath(abs_namespace, buf, len);
1041 if (err)
1042 goto done;
1043 ondisk_ref_namespace = buf;
1044 while (ondisk_ref_namespace[0] == '/')
1045 ondisk_ref_namespace++;
1046 if (strncmp(ondisk_ref_namespace, "refs/", 5) == 0)
1047 ondisk_ref_namespace += 5;
1048 else if (strcmp(ondisk_ref_namespace, "refs") == 0)
1049 ondisk_ref_namespace = "";
1052 /* Gather on-disk refs before parsing packed-refs. */
1053 free(path_refs);
1054 path_refs = get_refs_dir_path(repo, "");
1055 if (path_refs == NULL) {
1056 err = got_error_from_errno("get_refs_dir_path");
1057 goto done;
1059 err = gather_on_disk_refs(refs, path_refs,
1060 ondisk_ref_namespace ? ondisk_ref_namespace : "", repo,
1061 cmp_cb, cmp_arg);
1062 if (err)
1063 goto done;
1066 * The packed-refs file may contain redundant entries, in which
1067 * case on-disk refs take precedence.
1069 packed_refs_path = got_repo_get_path_packed_refs(repo);
1070 if (packed_refs_path == NULL) {
1071 err = got_error_from_errno("got_repo_get_path_packed_refs");
1072 goto done;
1075 f = fopen(packed_refs_path, "re");
1076 if (f) {
1077 size_t linesize = 0;
1078 ssize_t linelen;
1079 struct stat sb;
1081 if (fstat(fileno(f), &sb) == -1) {
1082 err = got_error_from_errno2("fstat", packed_refs_path);
1083 goto done;
1085 for (;;) {
1086 linelen = getline(&line, &linesize, f);
1087 if (linelen == -1) {
1088 if (feof(f))
1089 break;
1090 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1091 goto done;
1093 if (linelen > 0 && line[linelen - 1] == '\n')
1094 line[linelen - 1] = '\0';
1095 err = parse_packed_ref_line(&ref, NULL, line,
1096 sb.st_mtime, got_repo_get_object_format(repo));
1097 if (err)
1098 goto done;
1099 if (ref) {
1100 if (ref_namespace &&
1101 !match_packed_ref(ref, ref_namespace)) {
1102 got_ref_close(ref);
1103 continue;
1105 err = got_reflist_insert(&new, refs, ref,
1106 cmp_cb, cmp_arg);
1107 if (err || new == NULL /* duplicate */)
1108 got_ref_close(ref);
1109 if (err)
1110 goto done;
1114 done:
1115 free(packed_refs_path);
1116 free(abs_namespace);
1117 free(buf);
1118 free(line);
1119 free(path_refs);
1120 if (f && fclose(f) == EOF && err == NULL)
1121 err = got_error_from_errno("fclose");
1122 return err;
1125 void
1126 got_ref_list_free(struct got_reflist_head *refs)
1128 struct got_reflist_entry *re;
1130 while ((re = TAILQ_FIRST(refs))) {
1131 TAILQ_REMOVE(refs, re, entry);
1132 got_ref_close(re->ref);
1133 free(re);
1137 int
1138 got_ref_is_symbolic(struct got_reference *ref)
1140 return (ref->flags & GOT_REF_IS_SYMBOLIC);
1143 const struct got_error *
1144 got_ref_change_ref(struct got_reference *ref, struct got_object_id *id)
1146 if (ref->flags & GOT_REF_IS_SYMBOLIC)
1147 return got_error(GOT_ERR_BAD_REF_TYPE);
1149 memcpy(&ref->ref.ref.id, id, sizeof(ref->ref.ref.id));
1150 return NULL;
1153 const struct got_error *
1154 got_ref_change_symref(struct got_reference *ref, const char *refname)
1156 char *new_name;
1158 if ((ref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1159 return got_error(GOT_ERR_BAD_REF_TYPE);
1161 new_name = strdup(refname);
1162 if (new_name == NULL)
1163 return got_error_from_errno("strdup");
1165 free(ref->ref.symref.ref);
1166 ref->ref.symref.ref = new_name;
1167 return NULL;
1170 const struct got_error *
1171 got_ref_change_symref_to_ref(struct got_reference *symref,
1172 struct got_object_id *id)
1174 if ((symref->flags & GOT_REF_IS_SYMBOLIC) == 0)
1175 return got_error(GOT_ERR_BAD_REF_TYPE);
1177 symref->ref.ref.name = symref->ref.symref.name;
1178 memcpy(&symref->ref.ref.id, id, sizeof(symref->ref.ref.id));
1179 symref->flags &= ~GOT_REF_IS_SYMBOLIC;
1180 return NULL;
1183 const struct got_error *
1184 got_ref_write(struct got_reference *ref, struct got_repository *repo)
1186 const struct got_error *err = NULL, *unlock_err = NULL;
1187 const char *name = got_ref_get_name(ref);
1188 char *path_refs = NULL, *path = NULL, *tmppath = NULL;
1189 struct got_lockfile *lf = NULL;
1190 FILE *f = NULL;
1191 size_t n;
1192 struct stat sb;
1194 path_refs = get_refs_dir_path(repo, name);
1195 if (path_refs == NULL) {
1196 err = got_error_from_errno2("get_refs_dir_path", name);
1197 goto done;
1200 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1201 err = got_error_from_errno("asprintf");
1202 goto done;
1205 err = got_opentemp_named(&tmppath, &f, path, "");
1206 if (err) {
1207 char *parent;
1208 if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
1209 goto done;
1210 err = got_path_dirname(&parent, path);
1211 if (err)
1212 goto done;
1213 err = got_path_mkdir(parent);
1214 free(parent);
1215 if (err)
1216 goto done;
1217 err = got_opentemp_named(&tmppath, &f, path, "");
1218 if (err)
1219 goto done;
1222 if (ref->flags & GOT_REF_IS_SYMBOLIC) {
1223 n = fprintf(f, "ref: %s\n", ref->ref.symref.ref);
1224 if (n != strlen(ref->ref.symref.ref) + 6) {
1225 err = got_ferror(f, GOT_ERR_IO);
1226 goto done;
1228 } else {
1229 char *hex;
1230 size_t len;
1232 err = got_object_id_str(&hex, &ref->ref.ref.id);
1233 if (err)
1234 goto done;
1235 len = strlen(hex);
1236 n = fprintf(f, "%s\n", hex);
1237 free(hex);
1238 if (n != len + 1) {
1239 err = got_ferror(f, GOT_ERR_IO);
1240 goto done;
1244 if (ref->lf == NULL) {
1245 err = got_lockfile_lock(&lf, path, -1);
1246 if (err)
1247 goto done;
1250 /* XXX: check if old content matches our expectations? */
1252 if (stat(path, &sb) != 0) {
1253 if (errno != ENOENT) {
1254 err = got_error_from_errno2("stat", path);
1255 goto done;
1257 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1260 if (fchmod(fileno(f), sb.st_mode) != 0) {
1261 err = got_error_from_errno2("fchmod", tmppath);
1262 goto done;
1265 if (rename(tmppath, path) != 0) {
1266 err = got_error_from_errno3("rename", tmppath, path);
1267 goto done;
1269 free(tmppath);
1270 tmppath = NULL;
1272 if (stat(path, &sb) == -1) {
1273 err = got_error_from_errno2("stat", path);
1274 goto done;
1276 ref->mtime = sb.st_mtime;
1277 done:
1278 if (ref->lf == NULL && lf)
1279 unlock_err = got_lockfile_unlock(lf, -1);
1280 if (f) {
1281 if (fclose(f) == EOF && err == NULL)
1282 err = got_error_from_errno("fclose");
1284 free(path_refs);
1285 free(path);
1286 if (tmppath) {
1287 if (unlink(tmppath) == -1 && err == NULL)
1288 err = got_error_from_errno2("unlink", tmppath);
1289 free(tmppath);
1291 return err ? err : unlock_err;
1294 static const struct got_error *
1295 delete_packed_ref(struct got_reference *delref, struct got_repository *repo)
1297 const struct got_error *err = NULL, *unlock_err = NULL;
1298 struct got_lockfile *lf = NULL;
1299 FILE *f = NULL, *tmpf = NULL;
1300 char *line = NULL, *packed_refs_path, *tmppath = NULL;
1301 size_t linesize = 0;
1302 struct got_reflist_head refs;
1303 int found_delref = 0;
1305 /* The packed-refs file does not cotain symbolic references. */
1306 if (delref->flags & GOT_REF_IS_SYMBOLIC)
1307 return got_error(GOT_ERR_BAD_REF_DATA);
1309 TAILQ_INIT(&refs);
1311 packed_refs_path = got_repo_get_path_packed_refs(repo);
1312 if (packed_refs_path == NULL)
1313 return got_error_from_errno("got_repo_get_path_packed_refs");
1315 err = got_opentemp_named(&tmppath, &tmpf, packed_refs_path, "");
1316 if (err)
1317 goto done;
1319 if (delref->lf == NULL) {
1320 err = got_lockfile_lock(&lf, packed_refs_path, -1);
1321 if (err)
1322 goto done;
1325 f = fopen(packed_refs_path, "re");
1326 if (f == NULL) {
1327 err = got_error_from_errno2("fopen", packed_refs_path);
1328 goto done;
1330 for (;;) {
1331 ssize_t linelen;
1332 struct got_reference *ref;
1333 struct got_reflist_entry *new;
1335 linelen = getline(&line, &linesize, f);
1336 if (linelen == -1) {
1337 if (feof(f))
1338 break;
1339 err = got_ferror(f, GOT_ERR_BAD_REF_DATA);
1340 goto done;
1342 if (linelen > 0 && line[linelen - 1] == '\n')
1343 line[linelen - 1] = '\0';
1344 err = parse_packed_ref_line(&ref, NULL, line, 0,
1345 got_repo_get_object_format(repo));
1346 if (err)
1347 goto done;
1348 if (ref == NULL)
1349 continue;
1351 if (strcmp(ref->ref.ref.name, delref->ref.ref.name) == 0 &&
1352 got_object_id_cmp(&ref->ref.ref.id,
1353 &delref->ref.ref.id) == 0) {
1354 found_delref = 1;
1355 got_ref_close(ref);
1356 continue;
1359 err = got_reflist_insert(&new, &refs, ref,
1360 got_ref_cmp_by_name, NULL);
1361 if (err || new == NULL /* duplicate */)
1362 got_ref_close(ref);
1363 if (err)
1364 goto done;
1367 if (found_delref) {
1368 struct got_reflist_entry *re;
1369 size_t n;
1370 struct stat sb;
1372 n = fprintf(tmpf, "%s\n", GOT_PACKED_REFS_HEADER);
1373 if (n != sizeof(GOT_PACKED_REFS_HEADER)) {
1374 err = got_ferror(f, GOT_ERR_IO);
1375 goto done;
1378 TAILQ_FOREACH(re, &refs, entry) {
1379 char *hex;
1380 size_t len;
1382 err = got_object_id_str(&hex, &re->ref->ref.ref.id);
1383 if (err)
1384 goto done;
1385 len = strlen(hex);
1386 n = fprintf(tmpf, "%s ", hex);
1387 free(hex);
1388 if (n != len + 1) {
1389 err = got_ferror(f, GOT_ERR_IO);
1390 goto done;
1393 n = fprintf(tmpf, "%s\n", re->ref->ref.ref.name);
1394 if (n != strlen(re->ref->ref.ref.name) + 1) {
1395 err = got_ferror(f, GOT_ERR_IO);
1396 goto done;
1400 if (fflush(tmpf) != 0) {
1401 err = got_error_from_errno("fflush");
1402 goto done;
1405 if (fstat(fileno(f), &sb) != 0) {
1406 if (errno != ENOENT) {
1407 err = got_error_from_errno2("fstat",
1408 packed_refs_path);
1409 goto done;
1411 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1414 if (fchmod(fileno(tmpf), sb.st_mode) != 0) {
1415 err = got_error_from_errno2("fchmod", tmppath);
1416 goto done;
1419 if (rename(tmppath, packed_refs_path) != 0) {
1420 err = got_error_from_errno3("rename", tmppath,
1421 packed_refs_path);
1422 goto done;
1424 free(tmppath);
1425 tmppath = NULL;
1427 done:
1428 if (delref->lf == NULL && lf)
1429 unlock_err = got_lockfile_unlock(lf, -1);
1430 if (f) {
1431 if (fclose(f) == EOF && err == NULL)
1432 err = got_error_from_errno("fclose");
1434 if (tmppath && unlink(tmppath) == -1 && err == NULL)
1435 err = got_error_from_errno2("unlink", tmppath);
1436 if (tmpf && fclose(tmpf) == EOF && err == NULL)
1437 err = got_error_from_errno("fclose");
1438 free(tmppath);
1439 free(packed_refs_path);
1440 free(line);
1441 got_ref_list_free(&refs);
1442 return err ? err : unlock_err;
1445 static const struct got_error *
1446 delete_loose_ref(struct got_reference *ref, struct got_repository *repo)
1448 const struct got_error *err = NULL, *unlock_err = NULL;
1449 const char *name = got_ref_get_name(ref);
1450 char *path_refs = NULL, *path = NULL;
1451 struct got_lockfile *lf = NULL;
1453 path_refs = get_refs_dir_path(repo, name);
1454 if (path_refs == NULL) {
1455 err = got_error_from_errno2("get_refs_dir_path", name);
1456 goto done;
1459 if (asprintf(&path, "%s/%s", path_refs, name) == -1) {
1460 err = got_error_from_errno("asprintf");
1461 goto done;
1464 if (ref->lf == NULL) {
1465 err = got_lockfile_lock(&lf, path, -1);
1466 if (err)
1467 goto done;
1470 /* XXX: check if old content matches our expectations? */
1472 if (unlink(path) == -1)
1473 err = got_error_from_errno2("unlink", path);
1474 done:
1475 if (ref->lf == NULL && lf)
1476 unlock_err = got_lockfile_unlock(lf, -1);
1478 free(path_refs);
1479 free(path);
1480 return err ? err : unlock_err;
1483 const struct got_error *
1484 got_ref_delete(struct got_reference *ref, struct got_repository *repo)
1486 const struct got_error *err = NULL;
1487 struct got_reference *ref2;
1489 if (ref->flags & GOT_REF_IS_PACKED) {
1490 err = delete_packed_ref(ref, repo);
1491 if (err)
1492 return err;
1494 err = got_ref_open(&ref2, repo, got_ref_get_name(ref), 0);
1495 if (err) {
1496 if (err->code == GOT_ERR_NOT_REF)
1497 return NULL;
1498 return err;
1501 err = delete_loose_ref(ref2, repo);
1502 got_ref_close(ref2);
1503 return err;
1504 } else {
1505 err = delete_loose_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_packed_ref(ref2, repo);
1517 got_ref_close(ref2);
1518 return err;
1522 const struct got_error *
1523 got_ref_unlock(struct got_reference *ref)
1525 const struct got_error *err;
1526 err = got_lockfile_unlock(ref->lf, -1);
1527 ref->lf = NULL;
1528 return err;
1531 struct got_reflist_object_id_map {
1532 struct got_object_idset *idset;
1535 struct got_reflist_object_id_map_entry {
1536 struct got_reflist_head refs;
1539 static const struct got_error *
1540 add_object_id_map_entry(struct got_object_idset *idset,
1541 struct got_object_id *id, struct got_reflist_entry *re)
1543 const struct got_error *err = NULL;
1544 struct got_reflist_object_id_map_entry *ent;
1545 struct got_reflist_entry *new;
1547 ent = got_object_idset_get(idset, id);
1548 if (ent == NULL) {
1549 ent = malloc(sizeof(*ent));
1550 if (ent == NULL)
1551 return got_error_from_errno("malloc");
1553 TAILQ_INIT(&ent->refs);
1554 err = got_object_idset_add(idset, id, ent);
1555 if (err) {
1556 free(ent);
1557 return err;
1561 err = got_reflist_entry_dup(&new, re);
1562 if (err)
1563 return err;
1565 TAILQ_INSERT_TAIL(&ent->refs, new, entry);
1566 return NULL;
1569 const struct got_error *
1570 got_reflist_object_id_map_create(struct got_reflist_object_id_map **map,
1571 struct got_reflist_head *refs, struct got_repository *repo)
1573 const struct got_error *err = NULL;
1574 struct got_object_idset *idset;
1575 struct got_object_id *id = NULL;
1576 struct got_reflist_entry *re;
1578 idset = got_object_idset_alloc();
1579 if (idset == NULL)
1580 return got_error_from_errno("got_object_idset_alloc");
1582 *map = malloc(sizeof(**map));
1583 if (*map == NULL) {
1584 got_object_idset_free(idset);
1585 return got_error_from_errno("malloc");
1587 (*map)->idset = idset;
1589 TAILQ_FOREACH(re, refs, entry) {
1590 struct got_tag_object *tag = NULL;
1592 err = got_ref_resolve(&id, repo, re->ref);
1593 if (err)
1594 goto done;
1596 err = add_object_id_map_entry(idset, id, re);
1597 if (err)
1598 goto done;
1600 if (strstr(got_ref_get_name(re->ref), "/tags/") == NULL) {
1601 free(id);
1602 id = NULL;
1603 continue;
1606 err = got_object_open_as_tag(&tag, repo, id);
1607 if (err) {
1608 if (err->code != GOT_ERR_OBJ_TYPE)
1609 goto done;
1610 /* Ref points at something other than a tag. */
1611 err = NULL;
1612 tag = NULL;
1613 free(id);
1614 id = NULL;
1615 continue;
1618 err = add_object_id_map_entry(idset,
1619 got_object_tag_get_object_id(tag), re);
1620 got_object_tag_close(tag);
1621 if (err)
1622 goto done;
1624 free(id);
1625 id = NULL;
1627 done:
1628 free(id);
1629 if (err) {
1630 got_reflist_object_id_map_free(*map);
1631 *map = NULL;
1633 return err;
1636 struct got_reflist_head *
1637 got_reflist_object_id_map_lookup(struct got_reflist_object_id_map *map,
1638 struct got_object_id *id)
1640 struct got_reflist_object_id_map_entry *ent;
1641 ent = got_object_idset_get(map->idset, id);
1642 if (ent)
1643 return &ent->refs;
1644 return NULL;
1647 static const struct got_error *
1648 free_id_map_entry(struct got_object_id *id, void *data, void *arg)
1650 struct got_reflist_object_id_map_entry *ent = data;
1652 got_ref_list_free(&ent->refs);
1653 free(ent);
1654 return NULL;
1657 void
1658 got_reflist_object_id_map_free(struct got_reflist_object_id_map *map)
1660 got_object_idset_for_each(map->idset, free_id_map_entry, NULL);
1661 got_object_idset_free(map->idset);
1662 free(map);