refs: teach arbitrary repo support to iterators

Note that should_pack_ref() is called when writing refs, which is only supported for the_repository, hence the_repository is hardcoded there. Signed-off-by: Jonathan Tan <jonathantanmy@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jonathan Tan committed Oct 8, 2021 at 14:08 UTC 9bc45a28026eaea42011e8c1884aa2d9dc30f947
4 files changed +11 -4
refs.c
+2 -1
@@ -255,12 +255,13 @@ int refname_is_safe(const char *refname)
255 * does not exist, emit a warning and return false.
256 */
257 int ref_resolves_to_object(const char *refname,
258 + struct repository *repo,
259 const struct object_id *oid,
260 unsigned int flags)
261 {
262 if (flags & REF_ISBROKEN)
263 return 0;
263 - if (!has_object_file(oid)) {
264 + if (!repo_has_object_file(repo, oid)) {
265 error(_("%s does not point to a valid object!"), refname);
266 return 0;
267 }
refs/files-backend.c
+4 -1
@@ -732,6 +732,7 @@ struct files_ref_iterator {
732 struct ref_iterator base;
733
734 struct ref_iterator *iter0;
735 + struct repository *repo;
736 unsigned int flags;
737 };
738
@@ -753,6 +754,7 @@ static int files_ref_iterator_advance(struct ref_iterator *ref_iterator)
754
755 if (!(iter->flags & DO_FOR_EACH_INCLUDE_BROKEN) &&
756 !ref_resolves_to_object(iter->iter0->refname,
757 + iter->repo,
758 iter->iter0->oid,
759 iter->iter0->flags))
760 continue;
@@ -855,6 +857,7 @@ static struct ref_iterator *files_ref_iterator_begin(
857 base_ref_iterator_init(ref_iterator, &files_ref_iterator_vtable,
858 overlay_iter->ordered);
859 iter->iter0 = overlay_iter;
860 + iter->repo = ref_store->repo;
861 iter->flags = flags;
862
863 return ref_iterator;
@@ -1139,7 +1142,7 @@ static int should_pack_ref(const char *refname,
1142 return 0;
1143
1144 /* Do not pack broken refs: */
1142 - if (!ref_resolves_to_object(refname, oid, ref_flags))
1145 + if (!ref_resolves_to_object(refname, the_repository, oid, ref_flags))
1146 return 0;
1147
1148 return 1;
refs/packed-backend.c
+4 -2
@@ -778,6 +778,7 @@ struct packed_ref_iterator {
778 struct object_id oid, peeled;
779 struct strbuf refname_buf;
780
781 + struct repository *repo;
782 unsigned int flags;
783 };
784
@@ -866,8 +867,8 @@ static int packed_ref_iterator_advance(struct ref_iterator *ref_iterator)
867 continue;
868
869 if (!(iter->flags & DO_FOR_EACH_INCLUDE_BROKEN) &&
869 - !ref_resolves_to_object(iter->base.refname, &iter->oid,
870 - iter->flags))
870 + !ref_resolves_to_object(iter->base.refname, iter->repo,
871 + &iter->oid, iter->flags))
872 continue;
873
874 return ITER_OK;
@@ -956,6 +957,7 @@ static struct ref_iterator *packed_ref_iterator_begin(
957
958 iter->base.oid = &iter->oid;
959
960 + iter->repo = ref_store->repo;
961 iter->flags = flags;
962
963 if (prefix && *prefix)
refs/refs-internal.h
+1
@@ -66,6 +66,7 @@ int refname_is_safe(const char *refname);
66 * referred-to object does not exist, emit a warning and return false.
67 */
68 int ref_resolves_to_object(const char *refname,
69 + struct repository *repo,
70 const struct object_id *oid,
71 unsigned int flags);
72