{"thread":{"id":"54666","subject":"[PATCH v4] help.c: expand options for help.autocorrect","startedAt":"2020-11-18T17:28:54Z","lastAt":"2020-11-25T20:49:52Z","messageCount":5,"participants":["Drew DeVault","Junio C Hamano","SZEDER Gábor"],"isPatch":true,"patchVersion":4,"patchTotal":null},"messages":[{"id":"410243","messageId":"20201118172849.24715-1-sir@cmpwn.com","threadId":"54666","inReplyTo":null,"subject":"[PATCH v4] help.c: expand options for help.autocorrect","fromName":"Drew DeVault","fromEmail":"sir@cmpwn.com","sentAt":"2020-11-18T17:28:49Z","receivedAt":"2020-11-18T17:28:54Z","isPatch":true,"sender":{"key":"sir@cmpwn.com","avatar":"https://avatars.githubusercontent.com/u/1310872?v=4"},"body":"Some users would prefer to never suggest corrections at all. This\nupdates help.autocorrect to accept the strings \"immediate\" and \"never\",\nthe former assuming the prior behavior of negative values for\nautocorrect, and the latter skipping auto-correction entirely.\n\nSigned-off-by: Drew DeVault <sir@cmpwn.com>\n---\n Documentation/config/help.txt | 16 +++++++++-------\n help.c                        | 25 ++++++++++++++++++++++---\n 2 files changed, 31 insertions(+), 10 deletions(-)\n\ndiff --git a/Documentation/config/help.txt b/Documentation/config/help.txt\nindex 224bbf5a28..e07abd32d7 100644\n--- a/Documentation/config/help.txt\n+++ b/Documentation/config/help.txt\n@@ -8,13 +8,15 @@ help.format::\n \tthe default. 'web' and 'html' are the same.\n \n help.autoCorrect::\n-\tAutomatically correct and execute mistyped commands after\n-\twaiting for the given number of deciseconds (0.1 sec). If more\n-\tthan one command can be deduced from the entered text, nothing\n-\twill be executed.  If the value of this option is negative,\n-\tthe corrected command will be executed immediately. If the\n-\tvalue is 0 - the command will be just shown but not executed.\n-\tThis is the default.\n+\tIf git detects typos and can identify exactly one valid command similar\n+\tto the error, git will automatically run the intended command after\n+\twaiting a duration of time defined by this configuration value in\n+\tdeciseconds (0.1 sec).  If this value is 0, the suggested corrections\n+\twill be shown, but not executed. If \"immediate\", the suggested command\n+\tis run immediately. If \"never\", suggestions are not shown at all. The\n+\tdefault value is zero.\n++\n+Negative integers are interpreted as \"immediately\" for historical reasons.\n \n help.htmlPath::\n \tSpecify the path where the HTML documentation resides. File system paths\ndiff --git a/help.c b/help.c\nindex 919cbb9206..3c3bdec213 100644\n--- a/help.c\n+++ b/help.c\n@@ -472,12 +472,26 @@ int is_in_cmdlist(struct cmdnames *c, const char *s)\n static int autocorrect;\n static struct cmdnames aliases;\n \n+#define AUTOCORRECT_NEVER (-2)\n+#define AUTOCORRECT_IMMEDIATELY (-1)\n+\n static int git_unknown_cmd_config(const char *var, const char *value, void *cb)\n {\n \tconst char *p;\n \n-\tif (!strcmp(var, \"help.autocorrect\"))\n-\t\tautocorrect = git_config_int(var,value);\n+\tif (!strcmp(var, \"help.autocorrect\")) {\n+\t\tif (!value)\n+\t\t\treturn config_error_nonbool(var);\n+\t\tif (!strcmp(value, \"never\")) {\n+\t\t\tautocorrect = AUTOCORRECT_NEVER;\n+\t\t} else if (!strcmp(value, \"immediate\")) {\n+\t\t\tautocorrect = AUTOCORRECT_IMMEDIATELY;\n+\t\t} else {\n+\t\t\tint v = git_config_int(var, value);\n+\t\t\tautocorrect = (v < 0)\n+\t\t\t\t? AUTOCORRECT_IMMEDIATELY : v;\n+\t\t}\n+\t}\n \t/* Also use aliases for command lookup */\n \tif (skip_prefix(var, \"alias.\", &p))\n \t\tadd_cmdname(&aliases, p, strlen(p));\n@@ -525,6 +539,11 @@ const char *help_unknown_cmd(const char *cmd)\n \n \tread_early_config(git_unknown_cmd_config, NULL);\n \n+\tif (autocorrect == AUTOCORRECT_NEVER) {\n+\t\tfprintf_ln(stderr, _(\"git: '%s' is not a git command. See 'git --help'.\"), cmd);\n+\t\texit(1);\n+\t}\n+\n \tload_command_list(\"git-\", &main_cmds, &other_cmds);\n \n \tadd_cmd_list(&main_cmds, &aliases);\n@@ -594,7 +613,7 @@ const char *help_unknown_cmd(const char *cmd)\n \t\t\t   _(\"WARNING: You called a Git command named '%s', \"\n \t\t\t     \"which does not exist.\"),\n \t\t\t   cmd);\n-\t\tif (autocorrect < 0)\n+\t\tif (autocorrect == AUTOCORRECT_IMMEDIATELY)\n \t\t\tfprintf_ln(stderr,\n \t\t\t\t   _(\"Continuing under the assumption that \"\n \t\t\t\t     \"you meant '%s'.\"),\n-- \n2.29.2\n\n"},{"id":"410249","messageId":"xmqq1rgq35my.fsf@gitster.c.googlers.com","threadId":"54666","inReplyTo":"20201118172849.24715-1-sir@cmpwn.com","subject":"Re: [PATCH v4] help.c: expand options for help.autocorrect","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2020-11-18T18:02:29Z","receivedAt":"2020-11-18T18:02:50Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Drew DeVault <sir@cmpwn.com> writes:\n\n> Some users would prefer to never suggest corrections at all.\n\nHere is how I would have phrased it.\n\nKey points:\n\n - The log message starts with the description of the current state\n   of the system and introduces the problem the patch tries to\n   solve.\n\n - Then gives orders to the codebase to \"be like so\"\n\n - The addition of \"never\" is the primary change to solve the stated\n   problem.  \"immediate\" is not the essential part but is merely\n   adding a synonym while we are in the vicinity.\n\nThanks.\n\n\n--- >8 ------ >8 ------ >8 --- cut here --- >8 ------ >8 ------ >8 ---\nSubject: [PATCH] help.c: allow help.autocorrect to say \"do not even compute  suggestions\"\n\nWhile help.autocorrect can be set to 0 to decline auto-execution of\npossibly mistyped commands, it still spends cycles to compute the\nsuggestions, and it wastes screen real estate.\n\nUpdate help.autocorrect to accept the string \"never\" to just exit\nwith error upon mistyped commands to help users who prefer to never\nsee suggested corrections at all.\n\nWhile at it, introduce \"immediate\" as a more readable way to\nimmediately execute the auto-corrected command, which can be done\nwith negative value.\n\nSigned-off-by: Drew DeVault <sir@cmpwn.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\nRange-diff:\n1:  036b766035 ! 1:  cfe85a6e16 help.c: expand options for help.autocorrect\n    @@ Metadata\n     Author: Drew DeVault <sir@cmpwn.com>\n     \n      ## Commit message ##\n    -    help.c: expand options for help.autocorrect\n    +    help.c: allow help.autocorrect to say \"do not even compute suggestions\"\n     \n    -    Some users would prefer to never suggest corrections at all. This\n    -    updates help.autocorrect to accept the strings \"immediate\" and \"never\",\n    -    the former assuming the prior behavior of negative values for\n    -    autocorrect, and the latter skipping auto-correction entirely.\n    +    While help.autocorrect can be set to 0 to decline auto-execution of\n    +    possibly mistyped commands, it still spends cycles to compute the\n    +    suggestions, and it wastes screen real estates.\n    +\n    +    Update help.autocorrect to accept the string \"never\" to just exit\n    +    with error upon mistyped commands to help users who prefer to never\n    +    see suggested corrections at all.\n    +\n    +    While at it, introduce \"immediate\" as a more readable way to\n    +    immediately execute the auto-corrected command, which can be done\n    +    with negative value.\n     \n         Signed-off-by: Drew DeVault <sir@cmpwn.com>\n         Signed-off-by: Junio C Hamano <gitster@pobox.com>\n    @@ Documentation/config/help.txt: help.format::\n     +\tto the error, git will automatically run the intended command after\n     +\twaiting a duration of time defined by this configuration value in\n     +\tdeciseconds (0.1 sec).  If this value is 0, the suggested corrections\n    -+\twill be shown, but not executed. If \"immediate\", the suggested command\n    ++\twill be shown, but not executed. If it is a negative integer, or\n    ++\t\"immediate\", the suggested command\n     +\tis run immediately. If \"never\", suggestions are not shown at all. The\n     +\tdefault value is zero.\n    -++\n    -+Negative integers are interpreted as \"immediately\" for historical reasons.\n      \n      help.htmlPath::\n      \tSpecify the path where the HTML documentation resides. File system paths\n\n Documentation/config/help.txt | 15 ++++++++-------\n help.c                        | 25 ++++++++++++++++++++++---\n 2 files changed, 30 insertions(+), 10 deletions(-)\n\ndiff --git a/Documentation/config/help.txt b/Documentation/config/help.txt\nindex 224bbf5a28..783a90a0f9 100644\n--- a/Documentation/config/help.txt\n+++ b/Documentation/config/help.txt\n@@ -8,13 +8,14 @@ help.format::\n \tthe default. 'web' and 'html' are the same.\n \n help.autoCorrect::\n-\tAutomatically correct and execute mistyped commands after\n-\twaiting for the given number of deciseconds (0.1 sec). If more\n-\tthan one command can be deduced from the entered text, nothing\n-\twill be executed.  If the value of this option is negative,\n-\tthe corrected command will be executed immediately. If the\n-\tvalue is 0 - the command will be just shown but not executed.\n-\tThis is the default.\n+\tIf git detects typos and can identify exactly one valid command similar\n+\tto the error, git will automatically run the intended command after\n+\twaiting a duration of time defined by this configuration value in\n+\tdeciseconds (0.1 sec).  If this value is 0, the suggested corrections\n+\twill be shown, but not executed. If it is a negative integer, or\n+\t\"immediate\", the suggested command\n+\tis run immediately. If \"never\", suggestions are not shown at all. The\n+\tdefault value is zero.\n \n help.htmlPath::\n \tSpecify the path where the HTML documentation resides. File system paths\ndiff --git a/help.c b/help.c\nindex 919cbb9206..3c3bdec213 100644\n--- a/help.c\n+++ b/help.c\n@@ -472,12 +472,26 @@ int is_in_cmdlist(struct cmdnames *c, const char *s)\n static int autocorrect;\n static struct cmdnames aliases;\n \n+#define AUTOCORRECT_NEVER (-2)\n+#define AUTOCORRECT_IMMEDIATELY (-1)\n+\n static int git_unknown_cmd_config(const char *var, const char *value, void *cb)\n {\n \tconst char *p;\n \n-\tif (!strcmp(var, \"help.autocorrect\"))\n-\t\tautocorrect = git_config_int(var,value);\n+\tif (!strcmp(var, \"help.autocorrect\")) {\n+\t\tif (!value)\n+\t\t\treturn config_error_nonbool(var);\n+\t\tif (!strcmp(value, \"never\")) {\n+\t\t\tautocorrect = AUTOCORRECT_NEVER;\n+\t\t} else if (!strcmp(value, \"immediate\")) {\n+\t\t\tautocorrect = AUTOCORRECT_IMMEDIATELY;\n+\t\t} else {\n+\t\t\tint v = git_config_int(var, value);\n+\t\t\tautocorrect = (v < 0)\n+\t\t\t\t? AUTOCORRECT_IMMEDIATELY : v;\n+\t\t}\n+\t}\n \t/* Also use aliases for command lookup */\n \tif (skip_prefix(var, \"alias.\", &p))\n \t\tadd_cmdname(&aliases, p, strlen(p));\n@@ -525,6 +539,11 @@ const char *help_unknown_cmd(const char *cmd)\n \n \tread_early_config(git_unknown_cmd_config, NULL);\n \n+\tif (autocorrect == AUTOCORRECT_NEVER) {\n+\t\tfprintf_ln(stderr, _(\"git: '%s' is not a git command. See 'git --help'.\"), cmd);\n+\t\texit(1);\n+\t}\n+\n \tload_command_list(\"git-\", &main_cmds, &other_cmds);\n \n \tadd_cmd_list(&main_cmds, &aliases);\n@@ -594,7 +613,7 @@ const char *help_unknown_cmd(const char *cmd)\n \t\t\t   _(\"WARNING: You called a Git command named '%s', \"\n \t\t\t     \"which does not exist.\"),\n \t\t\t   cmd);\n-\t\tif (autocorrect < 0)\n+\t\tif (autocorrect == AUTOCORRECT_IMMEDIATELY)\n \t\t\tfprintf_ln(stderr,\n \t\t\t\t   _(\"Continuing under the assumption that \"\n \t\t\t\t     \"you meant '%s'.\"),\n-- \n2.29.2-461-g858fb5bde9\n\n"},{"id":"410251","messageId":"xmqqwnyi1qgs.fsf@gitster.c.googlers.com","threadId":"54666","inReplyTo":"xmqq1rgq35my.fsf@gitster.c.googlers.com","subject":"Re: [PATCH v4] help.c: expand options for help.autocorrect","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2020-11-18T18:15:31Z","receivedAt":"2020-11-18T18:16:00Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Junio C Hamano <gitster@pobox.com> writes:\n\n> Drew DeVault <sir@cmpwn.com> writes:\n>\n>> Some users would prefer to never suggest corrections at all.\n>\n> Here is how I would have phrased it.\n>\n> Key points:\n>\n>  - The log message starts with the description of the current state\n>    of the system and introduces the problem the patch tries to\n>    solve.\n>\n>  - Then gives orders to the codebase to \"be like so\"\n>\n>  - The addition of \"never\" is the primary change to solve the stated\n>    problem.  \"immediate\" is not the essential part but is merely\n>    adding a synonym while we are in the vicinity.\n>\n> Thanks.\n\nAlso, I think we would want _some_ test, as the base form of\nhelp.autocorrect is already tested.  Perhaps something like this.\n\n t/t9003-help-autocorrect.sh | 13 +++++++++++++\n 1 file changed, 13 insertions(+)\n\ndiff --git c/t/t9003-help-autocorrect.sh w/t/t9003-help-autocorrect.sh\nindex b1c7919c4a..8f1035c3c2 100755\n--- c/t/t9003-help-autocorrect.sh\n+++ w/t/t9003-help-autocorrect.sh\n@@ -49,4 +49,17 @@ test_expect_success 'autocorrect running commands' '\n \ttest_cmp expect actual\n '\n \n+test_expect_success 'autocorrect can be declined altogether' '\n+\tgit config help.autocorrect never &&\n+\n+\ttest_must_fail git lfg 2>actual &&\n+\tif test_have_prereq C_LOCALE_OUTPUT\n+\tthen\n+\t\t: cannot test with poisoned i18n\n+\telse\n+\t\tgrep \"is not a git command\" actual &&\n+\t\ttest_line_count = 1 actual\n+\tfi\n+'\n+\n test_done\n"},{"id":"410792","messageId":"20201125183309.GF8396@szeder.dev","threadId":"54666","inReplyTo":"xmqqwnyi1qgs.fsf@gitster.c.googlers.com","subject":"Re: [PATCH v4] help.c: expand options for help.autocorrect","fromName":"SZEDER Gábor","fromEmail":"szeder.dev@gmail.com","sentAt":"2020-11-25T18:33:09Z","receivedAt":"2020-11-25T18:33:06Z","isPatch":true,"sender":{"key":"szeder.dev@gmail.com","avatar":"https://avatars.githubusercontent.com/u/116324?v=4"},"body":"On Wed, Nov 18, 2020 at 10:15:31AM -0800, Junio C Hamano wrote:\n> Also, I think we would want _some_ test, as the base form of\n> help.autocorrect is already tested.  Perhaps something like this.\n> \n>  t/t9003-help-autocorrect.sh | 13 +++++++++++++\n>  1 file changed, 13 insertions(+)\n> \n> diff --git c/t/t9003-help-autocorrect.sh w/t/t9003-help-autocorrect.sh\n> index b1c7919c4a..8f1035c3c2 100755\n> --- c/t/t9003-help-autocorrect.sh\n> +++ w/t/t9003-help-autocorrect.sh\n> @@ -49,4 +49,17 @@ test_expect_success 'autocorrect running commands' '\n>  \ttest_cmp expect actual\n>  '\n>  \n> +test_expect_success 'autocorrect can be declined altogether' '\n> +\tgit config help.autocorrect never &&\n> +\n> +\ttest_must_fail git lfg 2>actual &&\n> +\tif test_have_prereq C_LOCALE_OUTPUT\n> +\tthen\n> +\t\t: cannot test with poisoned i18n\n> +\telse\n> +\t\tgrep \"is not a git command\" actual &&\n> +\t\ttest_line_count = 1 actual\n\nThe two branches of this condition are in the wrong order, as it\ndoesn't check the expected error message in normal mode, but it does\ngrep for the specific translated error message when run with\nGIT_TEST_GETTEXT_POISON=1 which then fails the test.\n\nI think we should simply use 'test_i18ngrep' instead of that whole\nif-else block.  \n\nI wonder what that 'test_line_count = 1' is supposed to check, and am\nnot sure that it's really necessary.  Anyway, the gettext poisoned\noutput should be a single line as well, so we can use that same check\nboth with and without C_LOCALE_OUTPUT.\n\n> +\tfi\n> +'\n> +\n>  test_done\n"},{"id":"410800","messageId":"xmqqk0u9duvo.fsf@gitster.c.googlers.com","threadId":"54666","inReplyTo":"20201125183309.GF8396@szeder.dev","subject":"Re: [PATCH v4] help.c: expand options for help.autocorrect","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2020-11-25T20:49:47Z","receivedAt":"2020-11-25T20:49:52Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"SZEDER Gábor <szeder.dev@gmail.com> writes:\n\n>> +\t\tgrep \"is not a git command\" actual &&\n>> +\t\ttest_line_count = 1 actual\n>\n> The two branches of this condition are in the wrong order, as it\n> doesn't check the expected error message in normal mode, but it does\n> grep for the specific translated error message when run with\n> GIT_TEST_GETTEXT_POISON=1 which then fails the test.\n\nCorrect.\n\n> I think we should simply use 'test_i18ngrep' instead of that whole\n> if-else block.  \n> ...\n> I wonder what that 'test_line_count = 1' is supposed to check, and am\n> not sure that it's really necessary.\n\nI think it is to make sure that there is no other message emitted.\n\nI prefer not to rely on the current implementation of poison that\nmakes any message a single line message \"# GETTEXT POISON #\" that is\nan incomplete line.  In other words,\n\n\tmain() {\n\n\tprintf(_(\"%s.\\n\"), \"hello\");\n\terror(_(\"goodbye\"));\n\n\texit(0);\n\t}\n\nwould give you a single line output with the current implementation,\nbut there is no guarantee we won't improve the poisoning in the\nfuture.\n\n"}]}