Skip to content

Commit 378fe5f

Browse files
matheustavaresgitster
authored andcommitted
config: complain about --worktree outside of a git repo
Running `git config --worktree` outside of a git repository hits a BUG() when trying to enumerate the worktrees. Let's catch this error earlier and die() with a friendlier message. Signed-off-by: Matheus Tavares <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 47ae905 commit 378fe5f

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

builtin/config.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -628,11 +628,15 @@ int cmd_config(int argc, const char **argv, const char *prefix)
628628
usage_builtin_config();
629629
}
630630

631-
if (use_local_config && nongit)
632-
die(_("--local can only be used inside a git repository"));
631+
if (nongit) {
632+
if (use_local_config)
633+
die(_("--local can only be used inside a git repository"));
634+
if (given_config_source.blob)
635+
die(_("--blob can only be used inside a git repository"));
636+
if (use_worktree_config)
637+
die(_("--worktree can only be used inside a git repository"));
633638

634-
if (given_config_source.blob && nongit)
635-
die(_("--blob can only be used inside a git repository"));
639+
}
636640

637641
if (given_config_source.file &&
638642
!strcmp(given_config_source.file, "-")) {

t/t1300-config.sh

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1836,11 +1836,14 @@ test_expect_success '--show-scope with --show-origin' '
18361836
test_cmp expect output
18371837
'
18381838

1839-
test_expect_success '--local requires a repo' '
1840-
# we expect 128 to ensure that we do not simply
1841-
# fail to find anything and return code "1"
1842-
test_expect_code 128 nongit git config --local foo.bar
1843-
'
1839+
for opt in --local --worktree
1840+
do
1841+
test_expect_success "$opt requires a repo" '
1842+
# we expect 128 to ensure that we do not simply
1843+
# fail to find anything and return code "1"
1844+
test_expect_code 128 nongit git config $opt foo.bar
1845+
'
1846+
done
18441847

18451848
cat >.git/config <<-\EOF &&
18461849
[core]

0 commit comments

Comments
 (0)