Merge branch 'ps/includeif-onbranch-cornercase-fix'

"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
This commit is contained in:
Junio C Hamano 2024-09-30 16:16:17 -07:00
commit 92198dd335
2 changed files with 49 additions and 6 deletions

View file

@ -306,13 +306,16 @@ static int include_by_branch(struct config_include_data *data,
int flags;
int ret;
struct strbuf pattern = STRBUF_INIT;
const char *refname = (!data->repo || !data->repo->gitdir) ?
NULL : refs_resolve_ref_unsafe(get_main_ref_store(data->repo),
"HEAD", 0, NULL, &flags);
const char *shortname;
const char *refname, *shortname;
if (!refname || !(flags & REF_ISSYMREF) ||
!skip_prefix(refname, "refs/heads/", &shortname))
if (!data->repo || data->repo->ref_storage_format == REF_STORAGE_FORMAT_UNKNOWN)
return 0;
refname = refs_resolve_ref_unsafe(get_main_ref_store(data->repo),
"HEAD", 0, NULL, &flags);
if (!refname ||
!(flags & REF_ISSYMREF) ||
!skip_prefix(refname, "refs/heads/", &shortname))
return 0;
strbuf_add(&pattern, cond, cond_len);

View file

@ -357,4 +357,44 @@ test_expect_success 'include cycles are detected' '
grep "exceeded maximum include depth" stderr
'
test_expect_success 'onbranch with unborn branch' '
test_when_finished "rm -rf repo" &&
git init repo &&
(
cd repo &&
git config set includeIf.onbranch:"*".path config.inc &&
git config set -f .git/config.inc foo.bar baz &&
git config get foo.bar
)
'
test_expect_success 'onbranch with detached HEAD' '
test_when_finished "rm -rf repo" &&
git init repo &&
(
cd repo &&
git config set "includeIf.onbranch:*.path" config.inc &&
git config set -f .git/config.inc foo.bar baz &&
test_commit initial &&
git switch --detach HEAD &&
test_must_fail git config get foo.bar
)
'
test_expect_success 'onbranch without repository' '
test_when_finished "rm -f .gitconfig config.inc" &&
git config set -f .gitconfig "includeIf.onbranch:**.path" config.inc &&
git config set -f config.inc foo.bar baz &&
git config get foo.bar &&
test_must_fail nongit git config get foo.bar
'
test_expect_success 'onbranch without repository but explicit nonexistent Git directory' '
test_when_finished "rm -f .gitconfig config.inc" &&
git config set -f .gitconfig "includeIf.onbranch:**.path" config.inc &&
git config set -f config.inc foo.bar baz &&
git config get foo.bar &&
test_must_fail nongit git --git-dir=nonexistent config get foo.bar
'
test_done