Skip to content

Commit 229c0bf

Browse files
committed
Merge branch 'ps/includeif-onbranch-cornercase-fix' into next
"git --git-dir=nowhere cmd" failed to properly notice that it wasn't in any repository while processing includeIf.onbranch configuration and instead crashed. * ps/includeif-onbranch-cornercase-fix: config: fix evaluating "onbranch" with nonexistent git dir t1305: exercise edge cases of "onbranch" includes
2 parents 379a7a1 + 320c96b commit 229c0bf

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

config.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -306,13 +306,16 @@ static int include_by_branch(struct config_include_data *data,
306306
int flags;
307307
int ret;
308308
struct strbuf pattern = STRBUF_INIT;
309-
const char *refname = (!data->repo || !data->repo->gitdir) ?
310-
NULL : refs_resolve_ref_unsafe(get_main_ref_store(data->repo),
311-
"HEAD", 0, NULL, &flags);
312-
const char *shortname;
309+
const char *refname, *shortname;
313310

314-
if (!refname || !(flags & REF_ISSYMREF) ||
315-
!skip_prefix(refname, "refs/heads/", &shortname))
311+
if (!data->repo || data->repo->ref_storage_format == REF_STORAGE_FORMAT_UNKNOWN)
312+
return 0;
313+
314+
refname = refs_resolve_ref_unsafe(get_main_ref_store(data->repo),
315+
"HEAD", 0, NULL, &flags);
316+
if (!refname ||
317+
!(flags & REF_ISSYMREF) ||
318+
!skip_prefix(refname, "refs/heads/", &shortname))
316319
return 0;
317320

318321
strbuf_add(&pattern, cond, cond_len);

t/t1305-config-include.sh

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,4 +357,44 @@ test_expect_success 'include cycles are detected' '
357357
grep "exceeded maximum include depth" stderr
358358
'
359359

360+
test_expect_success 'onbranch with unborn branch' '
361+
test_when_finished "rm -rf repo" &&
362+
git init repo &&
363+
(
364+
cd repo &&
365+
git config set includeIf.onbranch:"*".path config.inc &&
366+
git config set -f .git/config.inc foo.bar baz &&
367+
git config get foo.bar
368+
)
369+
'
370+
371+
test_expect_success 'onbranch with detached HEAD' '
372+
test_when_finished "rm -rf repo" &&
373+
git init repo &&
374+
(
375+
cd repo &&
376+
git config set "includeIf.onbranch:*.path" config.inc &&
377+
git config set -f .git/config.inc foo.bar baz &&
378+
test_commit initial &&
379+
git switch --detach HEAD &&
380+
test_must_fail git config get foo.bar
381+
)
382+
'
383+
384+
test_expect_success 'onbranch without repository' '
385+
test_when_finished "rm -f .gitconfig config.inc" &&
386+
git config set -f .gitconfig "includeIf.onbranch:**.path" config.inc &&
387+
git config set -f config.inc foo.bar baz &&
388+
git config get foo.bar &&
389+
test_must_fail nongit git config get foo.bar
390+
'
391+
392+
test_expect_success 'onbranch without repository but explicit nonexistent Git directory' '
393+
test_when_finished "rm -f .gitconfig config.inc" &&
394+
git config set -f .gitconfig "includeIf.onbranch:**.path" config.inc &&
395+
git config set -f config.inc foo.bar baz &&
396+
git config get foo.bar &&
397+
test_must_fail nongit git --git-dir=nonexistent config get foo.bar
398+
'
399+
360400
test_done

0 commit comments

Comments
 (0)