Skip to content

Commit 85fe0e8

Browse files
dschogitster
authored andcommitted
config: work around bug with includeif:onbranch and early config
Since 07b2c0e (config: learn the "onbranch:" includeIf condition, 2019-06-05), there is a potential catch-22 in the early config path: if the `include.onbranch:` feature is used, Git assumes that the Git directory has been initialized already. However, in the early config code path that is not true. One way to trigger this is to call the following commands in any repository: git config includeif.onbranch:refs/heads/master.path broken git help -a The symptom triggered by the `git help -a` invocation reads like this: BUG: refs.c:1851: attempting to get main_ref_store outside of repository Let's work around this, simply by ignoring the `includeif.onbranch:` setting when parsing the config when the ref store has not been initialized (yet). Technically, there is a way to solve this properly: teach the refs machinery to initialize the ref_store from a given gitdir/commondir pair (which we _do_ have in the early config code path), and then use that in `include_by_branch()`. This, however, is a pretty involved project, and we're already in the feature freeze for Git v2.23.0. Note: when calling above-mentioned two commands _outside_ of any Git worktree (passing the `--global` flag to `git config`, as there is obviously no repository config available), at the point when `include_by_branch()` is called, `the_repository` is `NULL`, therefore we have to be extra careful not to dereference it in that case. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f36d08d commit 85fe0e8

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

config.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,8 @@ static int include_by_branch(const char *cond, size_t cond_len)
275275
int flags;
276276
int ret;
277277
struct strbuf pattern = STRBUF_INIT;
278-
const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, &flags);
278+
const char *refname = !the_repository || !the_repository->gitdir ?
279+
NULL : resolve_ref_unsafe("HEAD", 0, NULL, &flags);
279280
const char *shortname;
280281

281282
if (!refname || !(flags & REF_ISSYMREF) ||

t/t1309-early-config.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,9 @@ test_expect_failure 'ignore .git/ with invalid config' '
8989
test_with_config "["
9090
'
9191

92+
test_expect_success 'early config and onbranch' '
93+
echo "[broken" >broken &&
94+
test_with_config "[includeif \"onbranch:refs/heads/master\"]path=../broken"
95+
'
96+
9297
test_done

0 commit comments

Comments
 (0)