On Wed May 22, 2024 at 1:38 PM EEST, Patrick Steinhardt wrote: > It was reported that git-init(1) can fail when initializing an existing > directory in case the config contains an "includeIf.onbranch:" > condition: > > ```shell > $ mkdir repo > $ git -c includeIf.onbranch:main.path=nonexistent init repo > BUG: refs.c:2056: reference backend is unknown > ``` > > The same error can also be triggered when re-initializing an already > existing repository. > > The bug has been introduced in 173761e21b (setup: start tracking ref > storage format, 2023-12-29), which wired up the ref storage format. The > root cause is in `init_db()`, which tries to read the config before we > have initialized `the_repository` and most importantly its ref storage > format. We eventually end up calling `include_by_branch()` and execute > `refs_resolve_ref_unsafe()`, but because we have not initialized the ref > storage format yet this will trigger the above bug. > > Interestingly, `include_by_branch()` has a mechanism that will only > cause us to resolve the ref when `the_repository->gitdir` is set. This > is also the reason why this only happens when we initialize an already > existing directory or repository: `gitdir` is set in those cases, but > not when creating a new directory. > > Now there are two ways to address the issue: > > - We can adapt `include_by_branch()` to also make the code conditional > on whether `the_repository->ref_storage_format` is set. > > - We can shift around code such that we initialize the repository > format before we read the config. > > While the first approach would be safe, it may also cause us to paper > over issues where a ref store should have been set up. In our case for > example, it may be reasonable to expect that re-initializing the repo > will cause the "onbranch:" condition to trigger, but we would not do > that if the ref storage format was not set up yet. This also used to > work before the above commit that introduced this bug. > > Rearrange the code such that we set up the repository format before > reading the config. This fixes the bug and ensures that "onbranch:" > conditions can trigger. > > Reported-by: Heghedus Razvan <heghedus.razvan@xxxxxxxxxxxxxx> > Signed-off-by: Patrick Steinhardt <ps@xxxxxx> I can confirm it's fixing the issue. Feel free to add: Tested-by: Heghedus Razvan <heghedus.razvan@xxxxxxxxxxxxxx>