help.c: expand options for help.autocorrect
PATCH v4 1/1 help.c: expand options for help.autocorrect
Some users would prefer to never suggest corrections at all. This updates help.autocorrect to accept the strings "immediate" and "never", the former assuming the prior behavior of negative values for autocorrect, and the latter skipping auto-correction entirely.
--- Documentation/config/help.txt | 16 +++++++++------- help.c | 25 ++++++++++++++++++++++--- 2 files changed, 31 insertions(+), 10 deletions(-)
Documentation/config/help.txt +9 -7
@@ -8,13 +8,15 @@ help.format:: the default. 'web' and 'html' are the same. help.autoCorrect:: - Automatically correct and execute mistyped commands after - waiting for the given number of deciseconds (0.1 sec). If more - than one command can be deduced from the entered text, nothing - will be executed. If the value of this option is negative, - the corrected command will be executed immediately. If the - value is 0 - the command will be just shown but not executed. - This is the default. + If git detects typos and can identify exactly one valid command similar + to the error, git will automatically run the intended command after + waiting a duration of time defined by this configuration value in + deciseconds (0.1 sec). If this value is 0, the suggested corrections + will be shown, but not executed. If "immediate", the suggested command + is run immediately. If "never", suggestions are not shown at all. The + default value is zero. ++ +Negative integers are interpreted as "immediately" for historical reasons. help.htmlPath:: Specify the path where the HTML documentation resides. File system paths
help.c +22 -3
@@ -472,12 +472,26 @@ int is_in_cmdlist(struct cmdnames *c, const char *s)static int autocorrect;static struct cmdnames aliases; +#define AUTOCORRECT_NEVER (-2) +#define AUTOCORRECT_IMMEDIATELY (-1) +static int git_unknown_cmd_config (const char *var,const char *value,void *cb) {const char *p; -if (!strcmp (var,"help.autocorrect" )) - autocorrect =git_config_int (var,value); +if (!strcmp (var,"help.autocorrect" )) { +if (!value) +return config_error_nonbool (var); +if (!strcmp (value,"never" )) { + autocorrect =AUTOCORRECT_NEVER ; + }else if (!strcmp (value,"immediate" )) { + autocorrect =AUTOCORRECT_IMMEDIATELY ; + }else { +int v =git_config_int (var, value); + autocorrect = (v <0 ) + ?AUTOCORRECT_IMMEDIATELY : v; + } + }/* Also use aliases for command lookup */ if (skip_prefix (var,"alias." , &p))add_cmdname (&aliases, p,strlen (p));
@@ -525,6 +539,11 @@ const char *help_unknown_cmd(const char *cmd)read_early_config (git_unknown_cmd_config,NULL ); +if (autocorrect ==AUTOCORRECT_NEVER ) { +fprintf_ln (stderr,_ ("git: '%s' is not a git command. See 'git --help'." ), cmd); +exit (1 ); + } +load_command_list ("git-" , &main_cmds, &other_cmds);add_cmd_list (&main_cmds, &aliases);
@@ -594,7 +613,7 @@ const char *help_unknown_cmd(const char *cmd)_ ("WARNING: You called a Git command named '%s', " "which does not exist." ), cmd); -if (autocorrect <0 ) +if (autocorrect ==AUTOCORRECT_IMMEDIATELY ) fprintf_ln(stderr, _("Continuing under the assumption that " "you meant '%s'."),
Signature · 1 line
2.29.2
Quoted 1 line · Drew DeVault
Some users would prefer to never suggest corrections at all.
Here is how I would have phrased it.
Key points:
- The log message starts with the description of the current state of the system and introduces the problem the patch tries to solve.
- Then gives orders to the codebase to "be like so"
- The addition of "never" is the primary change to solve the stated problem. "immediate" is not the essential part but is merely adding a synonym while we are in the vicinity.
Thanks.
--- >8 ------ >8 ------ >8 --- cut here --- >8 ------ >8 ------ >8 ---
Subject: [PATCH] help.c: allow help.autocorrect to say "do not even compute suggestions"
While help.autocorrect can be set to 0 to decline auto-execution of possibly mistyped commands, it still spends cycles to compute the suggestions, and it wastes screen real estate.
Update help.autocorrect to accept the string "never" to just exit with error upon mistyped commands to help users who prefer to never see suggested corrections at all.
While at it, introduce "immediate" as a more readable way to immediately execute the auto-corrected command, which can be done with negative value.
---
Range-diff:
1: 036b766035 ! 1: cfe85a6e16 help.c: expand options for help.autocorrect
@@ Metadata
Author: Drew DeVault <[email protected]>
## Commit message ##
- help.c: expand options for help.autocorrect
+ help.c: allow help.autocorrect to say "do not even compute suggestions"
- Some users would prefer to never suggest corrections at all. This
- updates help.autocorrect to accept the strings "immediate" and "never",
- the former assuming the prior behavior of negative values for
- autocorrect, and the latter skipping auto-correction entirely.
+ While help.autocorrect can be set to 0 to decline auto-execution of
+ possibly mistyped commands, it still spends cycles to compute the
+ suggestions, and it wastes screen real estates.
+
+ Update help.autocorrect to accept the string "never" to just exit
+ with error upon mistyped commands to help users who prefer to never
+ see suggested corrections at all.
+
+ While at it, introduce "immediate" as a more readable way to
+ immediately execute the auto-corrected command, which can be done
+ with negative value.
@@ Documentation/config/help.txt: help.format::
+ to the error, git will automatically run the intended command after
+ waiting a duration of time defined by this configuration value in
+ deciseconds (0.1 sec). If this value is 0, the suggested corrections
-+ will be shown, but not executed. If "immediate", the suggested command
++ will be shown, but not executed. If it is a negative integer, or
++ "immediate", the suggested command
+ is run immediately. If "never", suggestions are not shown at all. The
+ default value is zero.
-++
-+Negative integers are interpreted as "immediately" for historical reasons.
help.htmlPath::
Specify the path where the HTML documentation resides. File system paths
Documentation/config/help.txt | 15 ++++++++------- help.c | 25 ++++++++++++++++++++++--- 2 files changed, 30 insertions(+), 10 deletions(-)
Documentation/config/help.txt +8 -7
@@ -8,13 +8,14 @@ help.format:: the default. 'web' and 'html' are the same. help.autoCorrect:: - Automatically correct and execute mistyped commands after - waiting for the given number of deciseconds (0.1 sec). If more - than one command can be deduced from the entered text, nothing - will be executed. If the value of this option is negative, - the corrected command will be executed immediately. If the - value is 0 - the command will be just shown but not executed. - This is the default. + If git detects typos and can identify exactly one valid command similar + to the error, git will automatically run the intended command after + waiting a duration of time defined by this configuration value in + deciseconds (0.1 sec). If this value is 0, the suggested corrections + will be shown, but not executed. If it is a negative integer, or + "immediate", the suggested command + is run immediately. If "never", suggestions are not shown at all. The + default value is zero. help.htmlPath:: Specify the path where the HTML documentation resides. File system paths
help.c +22 -3
@@ -472,12 +472,26 @@ int is_in_cmdlist(struct cmdnames *c, const char *s)static int autocorrect;static struct cmdnames aliases; +#define AUTOCORRECT_NEVER (-2) +#define AUTOCORRECT_IMMEDIATELY (-1) +static int git_unknown_cmd_config (const char *var,const char *value,void *cb) {const char *p; -if (!strcmp (var,"help.autocorrect" )) - autocorrect =git_config_int (var,value); +if (!strcmp (var,"help.autocorrect" )) { +if (!value) +return config_error_nonbool (var); +if (!strcmp (value,"never" )) { + autocorrect =AUTOCORRECT_NEVER ; + }else if (!strcmp (value,"immediate" )) { + autocorrect =AUTOCORRECT_IMMEDIATELY ; + }else { +int v =git_config_int (var, value); + autocorrect = (v <0 ) + ?AUTOCORRECT_IMMEDIATELY : v; + } + }/* Also use aliases for command lookup */ if (skip_prefix (var,"alias." , &p))add_cmdname (&aliases, p,strlen (p));
@@ -525,6 +539,11 @@ const char *help_unknown_cmd(const char *cmd)read_early_config (git_unknown_cmd_config,NULL ); +if (autocorrect ==AUTOCORRECT_NEVER ) { +fprintf_ln (stderr,_ ("git: '%s' is not a git command. See 'git --help'." ), cmd); +exit (1 ); + } +load_command_list ("git-" , &main_cmds, &other_cmds);add_cmd_list (&main_cmds, &aliases);
@@ -594,7 +613,7 @@ const char *help_unknown_cmd(const char *cmd)_ ("WARNING: You called a Git command named '%s', " "which does not exist." ), cmd); -if (autocorrect <0 ) +if (autocorrect ==AUTOCORRECT_IMMEDIATELY ) fprintf_ln(stderr, _("Continuing under the assumption that " "you meant '%s'."),
Signature · 1 line
2.29.2-461-g858fb5bde9
Quoted 12 lines · Junio C Hamano
Quoted 1 line · Drew DeVault
Some users would prefer to never suggest corrections at all.
Here is how I would have phrased it.
Key points:
- The log message starts with the description of the current state of the system and introduces the problem the patch tries to solve.
Quoted 7 lines
- Then gives orders to the codebase to "be like so"
- The addition of "never" is the primary change to solve the stated problem. "immediate" is not the essential part but is merely adding a synonym while we are in the vicinity.
Thanks.
Also, I think we would want _some_ test, as the base form of help.autocorrect is already tested. Perhaps something like this.
t/t9003-help-autocorrect.sh | 13 +++++++++++++ 1 file changed, 13 insertions(+)
t/t9003-help-autocorrect.sh +13 -0
@@ -49,4 +49,17 @@ test_expect_success 'autocorrect running commands' ' test_cmp expect actual ' +test_expect_success 'autocorrect can be declined altogether' ' + git config help.autocorrect never && + + test_must_fail git lfg 2>actual && + if test_have_prereq C_LOCALE_OUTPUT + then + : cannot test with poisoned i18n + else + grep "is not a git command" actual && + test_line_count = 1 actual + fi +' + test_done
Quoted 14 lines · Junio C Hamano
Also, I think we would want _some_ test, as the base form of help.autocorrect is already tested. Perhaps something like this.
t/t9003-help-autocorrect.sh | 13 +++++++++++++ 1 file changed, 13 insertions(+)t/t9003-help-autocorrect.sh +0
-0@@ -49,4 +49,17 @@ test_expect_success 'autocorrect running commands' ' test_cmp expect actual '
t/t9003-help-autocorrect.sh test_expect_success 'autocorrect running commands' ' · line 61
Quoted 10 lines
t/t9003-help-autocorrect.sh +10
-0+test_expect_success 'autocorrect can be declined altogether' ' + git config help.autocorrect never && + + test_must_fail git lfg 2>actual && + if test_have_prereq C_LOCALE_OUTPUT + then + : cannot test with poisoned i18n + else + grep "is not a git command" actual && + test_line_count = 1 actual
The two branches of this condition are in the wrong order, as it doesn't check the expected error message in normal mode, but it does grep for the specific translated error message when run with GIT_TEST_GETTEXT_POISON=1 which then fails the test.
I think we should simply use 'test_i18ngrep' instead of that whole if-else block.
I wonder what that 'test_line_count = 1' is supposed to check, and am not sure that it's really necessary. Anyway, the gettext poisoned output should be a single line as well, so we can use that same check both with and without C_LOCALE_OUTPUT.
Quoted 4 lines
t/t9003-help-autocorrect.sh +3
-0+ fi +' + test_done
Quoted 7 lines · SZEDER Gábor
t/t9003-help-autocorrect.sh test_expect_success 'autocorrect running commands' ' · line 61
Quoted 2 lines
t/t9003-help-autocorrect.sh +2
-0+ grep "is not a git command" actual && + test_line_count = 1 actualThe two branches of this condition are in the wrong order, as it doesn't check the expected error message in normal mode, but it does grep for the specific translated error message when run with GIT_TEST_GETTEXT_POISON=1 which then fails the test.
Correct.
Quoted 5 lines
I think we should simply use '
test_i18ngrep' instead of that whole if-else block.
...
I wonder what that 'test_line_count= 1' is supposed to check, and am not sure that it's really necessary.
I think it is to make sure that there is no other message emitted.
I prefer not to rely on the current implementation of poison that makes any message a single line message "# GETTEXT POISON #" that is an incomplete line. In other words,
main() {
printf(_("%s.\n"), "hello");
error(_("goodbye"));
exit(0);
}would give you a single line output with the current implementation, but there is no guarantee we won't improve the poisoning in the future.