refs: protect against chicken-and-egg recursion

In the preceding commits we have fixed recursion when creating the reference backends due to a chicken-and-egg situation with "onbranch" conditions. Unfortunately, this issue has existed for a while, and we didn't really have a good mechanism to detect this recursion. Improve the status quo by detecting the recursion when creating the main reference store. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Jun 25, 2026 at 11:20 UTC d6522d01dfd147d18246d308dd6ea24b32d095d2
1 file changed +7
refs.c
+7
@@ -2359,15 +2359,22 @@ void ref_store_release(struct ref_store *ref_store)
2359
2360 struct ref_store *get_main_ref_store(struct repository *r)
2361 {
2362 + static bool initializing;
2363 +
2364 if (r->refs_private)
2365 return r->refs_private;
2366
2367 if (!r->gitdir)
2368 BUG("attempting to get main_ref_store outside of repository");
2369 + if (initializing)
2370 + BUG("initialization of main ref store is recursing");
2371
2372 + initializing = true;
2373 r->refs_private = ref_store_init(r, r->ref_storage_format,
2374 r->gitdir, REF_STORE_ALL_CAPS);
2375 r->refs_private = maybe_debug_wrap_ref_store(r->gitdir, r->refs_private);
2376 + initializing = false;
2377 +
2378 return r->refs_private;
2379 }
2380