Skip to content

Commit 78abcff

Browse files
committed
Merge branch 'dd/help-autocorrect-never'
"git $cmd $args", when $cmd is not a recognised subcommand, by default tries to see if $cmd is a typo of an existing subcommand and optionally executes the corrected command if there is only one possibility, depending on the setting of help.autocorrect; the users can now disable the whole thing, including the cycles spent to find a likely typo, by setting the configuration variable to 'never'. * dd/help-autocorrect-never: help.c: help.autocorrect=never means "do not compute suggestions"
2 parents 3cf5978 + 644bb95 commit 78abcff

File tree

3 files changed

+52
-18
lines changed

3 files changed

+52
-18
lines changed

Documentation/config/help.txt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ help.format::
88
the default. 'web' and 'html' are the same.
99

1010
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.
1819

1920
help.htmlPath::
2021
Specify the path where the HTML documentation resides. File system paths

help.c

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -472,12 +472,26 @@ int is_in_cmdlist(struct cmdnames *c, const char *s)
472472
static int autocorrect;
473473
static struct cmdnames aliases;
474474

475+
#define AUTOCORRECT_NEVER (-2)
476+
#define AUTOCORRECT_IMMEDIATELY (-1)
477+
475478
static int git_unknown_cmd_config(const char *var, const char *value, void *cb)
476479
{
477480
const char *p;
478481

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+
}
481495
/* Also use aliases for command lookup */
482496
if (skip_prefix(var, "alias.", &p))
483497
add_cmdname(&aliases, p, strlen(p));
@@ -525,6 +539,11 @@ const char *help_unknown_cmd(const char *cmd)
525539

526540
read_early_config(git_unknown_cmd_config, NULL);
527541

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+
528547
load_command_list("git-", &main_cmds, &other_cmds);
529548

530549
add_cmd_list(&main_cmds, &aliases);
@@ -594,7 +613,7 @@ const char *help_unknown_cmd(const char *cmd)
594613
_("WARNING: You called a Git command named '%s', "
595614
"which does not exist."),
596615
cmd);
597-
if (autocorrect < 0)
616+
if (autocorrect == AUTOCORRECT_IMMEDIATELY)
598617
fprintf_ln(stderr,
599618
_("Continuing under the assumption that "
600619
"you meant '%s'."),

t/t9003-help-autocorrect.sh

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,30 @@ test_expect_success 'autocorrect showing candidates' '
3737
grep "^ distimdistim" actual
3838
'
3939

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 &&
4244
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 &&
4648
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
5064
'
5165

5266
test_done

0 commit comments

Comments
 (0)