help.c: help.autocorrect=never means "do not 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. Signed-off-by: Drew DeVault <sir@cmpwn.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Drew DeVault committed Nov 25, 2020 at 13:01 UTC 644bb953ce3251f2868ece6b767949828fa32e44
3 files changed +52 -18
Documentation/config/help.txt
+8 -7
@@ -8,13 +8,14 @@ help.format::
8 the default. 'web' and 'html' are the same.
9
10 help.autoCorrect::
11 - Automatically correct and execute mistyped commands after
12 - waiting for the given number of deciseconds (0.1 sec). If more
13 - than one command can be deduced from the entered text, nothing
14 - will be executed. If the value of this option is negative,
15 - the corrected command will be executed immediately. If the
16 - value is 0 - the command will be just shown but not executed.
17 - This is the default.
11 + If git detects typos and can identify exactly one valid command similar
12 + to the error, git will automatically run the intended command after
13 + waiting a duration of time defined by this configuration value in
14 + deciseconds (0.1 sec). If this value is 0, the suggested corrections
15 + will be shown, but not executed. If it is a negative integer, or
16 + "immediate", the suggested command
17 + is run immediately. If "never", suggestions are not shown at all. The
18 + default value is zero.
19
20 help.htmlPath::
21 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)
472 static int autocorrect;
473 static struct cmdnames aliases;
474
475 +#define AUTOCORRECT_NEVER (-2)
476 +#define AUTOCORRECT_IMMEDIATELY (-1)
477 +
478 static int git_unknown_cmd_config(const char *var, const char *value, void *cb)
479 {
480 const char *p;
481
479 - if (!strcmp(var, "help.autocorrect"))
480 - autocorrect = git_config_int(var,value);
482 + if (!strcmp(var, "help.autocorrect")) {
483 + if (!value)
484 + return config_error_nonbool(var);
485 + if (!strcmp(value, "never")) {
486 + autocorrect = AUTOCORRECT_NEVER;
487 + } else if (!strcmp(value, "immediate")) {
488 + autocorrect = AUTOCORRECT_IMMEDIATELY;
489 + } else {
490 + int v = git_config_int(var, value);
491 + autocorrect = (v < 0)
492 + ? AUTOCORRECT_IMMEDIATELY : v;
493 + }
494 + }
495 /* Also use aliases for command lookup */
496 if (skip_prefix(var, "alias.", &p))
497 add_cmdname(&aliases, p, strlen(p));
@@ -525,6 +539,11 @@ const char *help_unknown_cmd(const char *cmd)
539
540 read_early_config(git_unknown_cmd_config, NULL);
541
542 + if (autocorrect == AUTOCORRECT_NEVER) {
543 + fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd);
544 + exit(1);
545 + }
546 +
547 load_command_list("git-", &main_cmds, &other_cmds);
548
549 add_cmd_list(&main_cmds, &aliases);
@@ -594,7 +613,7 @@ const char *help_unknown_cmd(const char *cmd)
613 _("WARNING: You called a Git command named '%s', "
614 "which does not exist."),
615 cmd);
597 - if (autocorrect < 0)
616 + if (autocorrect == AUTOCORRECT_IMMEDIATELY)
617 fprintf_ln(stderr,
618 _("Continuing under the assumption that "
619 "you meant '%s'."),
t/t9003-help-autocorrect.sh
+22 -8
@@ -37,16 +37,30 @@ test_expect_success 'autocorrect showing candidates' '
37 grep "^ distimdistim" actual
38 '
39
40 -test_expect_success 'autocorrect running commands' '
41 - git config help.autocorrect -1 &&
40 +for immediate in -1 immediate
41 +do
42 + test_expect_success 'autocorrect running commands' '
43 + git config help.autocorrect $immediate &&
44
43 - git lfg >actual &&
44 - echo "a single log entry" >expect &&
45 - test_cmp expect actual &&
45 + git lfg >actual &&
46 + echo "a single log entry" >expect &&
47 + test_cmp expect actual &&
48
47 - git distimdist >actual &&
48 - echo "distimdistim was called" >expect &&
49 - test_cmp expect actual
49 + git distimdist >actual &&
50 + echo "distimdistim was called" >expect &&
51 + test_cmp expect actual
52 + '
53 +done
54 +
55 +test_expect_success 'autocorrect can be declined altogether' '
56 + git config help.autocorrect never &&
57 +
58 + test_must_fail git lfg 2>actual &&
59 + if test_have_prereq C_LOCALE_OUTPUT
60 + then
61 + grep "is not a git command" actual &&
62 + test_line_count = 1 actual
63 + fi
64 '
65
66 test_done