{"thread":{"id":"54215","subject":"[PATCH] config: complain about --worktree outside of a git repo","startedAt":"2020-09-09T13:19:43Z","lastAt":"2020-09-09T19:47:40Z","messageCount":2,"participants":["Matheus Tavares","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"405244","messageId":"3dfd33eda3f9cbd0d87ba7bb31f4501aa7b8b8a8.1599657308.git.matheus.bernardino@usp.br","threadId":"54215","inReplyTo":null,"subject":"[PATCH] config: complain about --worktree outside of a git repo","fromName":"Matheus Tavares","fromEmail":"matheus.bernardino@usp.br","sentAt":"2020-09-09T13:16:08Z","receivedAt":"2020-09-09T13:19:43Z","isPatch":true,"sender":{"key":"matheus.tavb@gmail.com","avatar":"https://avatars.githubusercontent.com/u/12701583?v=4"},"body":"Running `git config --worktree` outside of a git repository hits a BUG()\nwhen trying to enumerate the worktrees. Let's catch this error earlier\nand die() with a friendlier message.\n\nSigned-off-by: Matheus Tavares <matheus.bernardino@usp.br>\n---\n builtin/config.c  | 12 ++++++++----\n t/t1300-config.sh | 13 ++++++++-----\n 2 files changed, 16 insertions(+), 9 deletions(-)\n\ndiff --git a/builtin/config.c b/builtin/config.c\nindex 5e39f61885..53e411d68a 100644\n--- a/builtin/config.c\n+++ b/builtin/config.c\n@@ -628,11 +628,15 @@ int cmd_config(int argc, const char **argv, const char *prefix)\n \t\tusage_builtin_config();\n \t}\n \n-\tif (use_local_config && nongit)\n-\t\tdie(_(\"--local can only be used inside a git repository\"));\n+\tif (nongit) {\n+\t\tif (use_local_config)\n+\t\t\tdie(_(\"--local can only be used inside a git repository\"));\n+\t\tif (given_config_source.blob)\n+\t\t\tdie(_(\"--blob can only be used inside a git repository\"));\n+\t\tif (use_worktree_config)\n+\t\t\tdie(_(\"--worktree can only be used inside a git repository\"));\n \n-\tif (given_config_source.blob && nongit)\n-\t\tdie(_(\"--blob can only be used inside a git repository\"));\n+\t}\n \n \tif (given_config_source.file &&\n \t\t\t!strcmp(given_config_source.file, \"-\")) {\ndiff --git a/t/t1300-config.sh b/t/t1300-config.sh\nindex 97ebfe1f9d..825d9a184f 100755\n--- a/t/t1300-config.sh\n+++ b/t/t1300-config.sh\n@@ -1836,11 +1836,14 @@ test_expect_success '--show-scope with --show-origin' '\n \ttest_cmp expect output\n '\n \n-test_expect_success '--local requires a repo' '\n-\t# we expect 128 to ensure that we do not simply\n-\t# fail to find anything and return code \"1\"\n-\ttest_expect_code 128 nongit git config --local foo.bar\n-'\n+for opt in --local --worktree\n+do\n+\ttest_expect_success \"$opt requires a repo\" '\n+\t\t# we expect 128 to ensure that we do not simply\n+\t\t# fail to find anything and return code \"1\"\n+\t\ttest_expect_code 128 nongit git config $opt foo.bar\n+\t'\n+done\n \n cat >.git/config <<-\\EOF &&\n [core]\n-- \n2.28.0\n\n"},{"id":"405275","messageId":"xmqqeenavijx.fsf@gitster.c.googlers.com","threadId":"54215","inReplyTo":"3dfd33eda3f9cbd0d87ba7bb31f4501aa7b8b8a8.1599657308.git.matheus.bernardino@usp.br","subject":"Re: [PATCH] config: complain about --worktree outside of a git repo","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2020-09-09T19:47:30Z","receivedAt":"2020-09-09T19:47:40Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Matheus Tavares <matheus.bernardino@usp.br> writes:\n\n> Running `git config --worktree` outside of a git repository hits a BUG()\n> when trying to enumerate the worktrees. Let's catch this error earlier\n> and die() with a friendlier message.\n>\n> Signed-off-by: Matheus Tavares <matheus.bernardino@usp.br>\n> ---\n>  builtin/config.c  | 12 ++++++++----\n>  t/t1300-config.sh | 13 ++++++++-----\n>  2 files changed, 16 insertions(+), 9 deletions(-)\n>\n> diff --git a/builtin/config.c b/builtin/config.c\n> index 5e39f61885..53e411d68a 100644\n> --- a/builtin/config.c\n> +++ b/builtin/config.c\n> @@ -628,11 +628,15 @@ int cmd_config(int argc, const char **argv, const char *prefix)\n>  \t\tusage_builtin_config();\n>  \t}\n>  \n> -\tif (use_local_config && nongit)\n> -\t\tdie(_(\"--local can only be used inside a git repository\"));\n> +\tif (nongit) {\n> +\t\tif (use_local_config)\n> +\t\t\tdie(_(\"--local can only be used inside a git repository\"));\n> +\t\tif (given_config_source.blob)\n> +\t\t\tdie(_(\"--blob can only be used inside a git repository\"));\n> +\t\tif (use_worktree_config)\n> +\t\t\tdie(_(\"--worktree can only be used inside a git repository\"));\n>  \n> -\tif (given_config_source.blob && nongit)\n> -\t\tdie(_(\"--blob can only be used inside a git repository\"));\n> +\t}\n\nOK.  The updated structure makes it more obvious that we handle\nthree special cases.  Good jobs.\n\nThanks.\n\n>  \n>  \tif (given_config_source.file &&\n>  \t\t\t!strcmp(given_config_source.file, \"-\")) {\n> diff --git a/t/t1300-config.sh b/t/t1300-config.sh\n> index 97ebfe1f9d..825d9a184f 100755\n> --- a/t/t1300-config.sh\n> +++ b/t/t1300-config.sh\n> @@ -1836,11 +1836,14 @@ test_expect_success '--show-scope with --show-origin' '\n>  \ttest_cmp expect output\n>  '\n>  \n> -test_expect_success '--local requires a repo' '\n> -\t# we expect 128 to ensure that we do not simply\n> -\t# fail to find anything and return code \"1\"\n> -\ttest_expect_code 128 nongit git config --local foo.bar\n> -'\n> +for opt in --local --worktree\n> +do\n> +\ttest_expect_success \"$opt requires a repo\" '\n> +\t\t# we expect 128 to ensure that we do not simply\n> +\t\t# fail to find anything and return code \"1\"\n> +\t\ttest_expect_code 128 nongit git config $opt foo.bar\n> +\t'\n> +done\n>  \n>  cat >.git/config <<-\\EOF &&\n>  [core]\n"}]}