setup: fix "includeIf.onbranch" condition on re-init
It was reported that git-init(1) can fail when initializing an existing directory when 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.
This is caused by init_db()
, which tries to read the config before we
have initialized the_repository
and most importantly its ref storage
format. We will 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 whetherthe_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 safer, 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. This can be achieved
via the second approach, where we first set up the repository format
that has already been discovered via check_repository_format()
.
Rearrange the code accordingly such that we have a properly initialized repository format before we read the repository's config. Add a bunch of tests.
Reported-by: Heghedus Razvan heghedus.razvan@protonmail.com Signed-off-by: Patrick Steinhardt ps@pks.im
Closes Re-initializing a repo triggers bug with `inclu... (#325 - closed).