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 <matheus.bernardino@usp.br> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Matheus Tavares committed
Sep 9, 2020 at 10:16 UTC
378fe5fc3d5e415b2d7062e29bc99029dc3418d5
2 files changed
+16
-9
builtin/config.c
+8
-4
@@ -628,11 +628,15 @@ int cmd_config(int argc, const char **argv, const char *prefix)
628
usage_builtin_config();
629
}
630
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"));
638
634
- if (given_config_source.blob && nongit)
635
- die(_("--blob can only be used inside a git repository"));
639
+ }
640
641
if (given_config_source.file &&
642
!strcmp(given_config_source.file, "-")) {
t/t1300-config.sh
+8
-5
@@ -1836,11 +1836,14 @@ test_expect_success '--show-scope with --show-origin' '
1836
test_cmp expect output
1837
'
1838
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
1847
1848
cat >.git/config <<-\EOF &&
1849
[core]