help: show the suggested command when help.autocorrect is false
Make the handling of false boolean values for help.autocorrect consistent with the handling of value 0 by showing the suggested commands but not running them. Suggested-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: David Aguilar <davvid@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
David Aguilar committed
Feb 1, 2025 at 13:33 UTC
e21bf2c4314840dba4f45c6e7bbc96a6fc0a8f61
3 files changed
+16
-11
Documentation/config/help.txt
+2
-2
@@ -11,12 +11,12 @@ help.autoCorrect::
11
If git detects typos and can identify exactly one valid command similar
12
to the error, git will try to suggest the correct command or even
13
run the suggestion automatically. Possible config values are:
14
- - 0: show the suggested command (default).
14
+ - 0, "false", "off", "no": show the suggested command (default).
15
- 1, "true", "on", "yes", "immediate": run the suggested command
16
immediately.
17
- positive number > 1: run the suggested command after specified
18
deciseconds (0.1 sec).
19
- - "false", "off", "no", "never": don't run or show any suggested command.
19
+ - "never": don't run or show any suggested command.
20
- "prompt": show the suggestion and prompt for confirmation to run
21
the command.
22
help.c
+4
-2
@@ -552,6 +552,7 @@ struct help_unknown_cmd_config {
552
struct cmdnames aliases;
553
};
554
555
+#define AUTOCORRECT_SHOW (-4)
556
#define AUTOCORRECT_PROMPT (-3)
557
#define AUTOCORRECT_NEVER (-2)
558
#define AUTOCORRECT_IMMEDIATELY (-1)
@@ -562,7 +563,7 @@ static int parse_autocorrect(const char *value)
563
case 1:
564
return AUTOCORRECT_IMMEDIATELY;
565
case 0:
565
- return AUTOCORRECT_NEVER;
566
+ return AUTOCORRECT_SHOW;
567
default: /* other random text */
568
break;
569
}
@@ -713,7 +714,8 @@ char *help_unknown_cmd(const char *cmd)
714
n++)
715
; /* still counting */
716
}
716
- if (cfg.autocorrect && n == 1 && SIMILAR_ENOUGH(best_similarity)) {
717
+ if (cfg.autocorrect && cfg.autocorrect != AUTOCORRECT_SHOW && n == 1 &&
718
+ SIMILAR_ENOUGH(best_similarity)) {
719
char *assumed = xstrdup(main_cmds.names[0]->name);
720
721
fprintf_ln(stderr,
t/t9003-help-autocorrect.sh
+10
-7
@@ -28,15 +28,18 @@ test_expect_success 'setup' '
28
test_cmp expect actual
29
'
30
31
-test_expect_success 'autocorrect showing candidates' '
32
- git config help.autocorrect 0 &&
31
+for show in false no off 0
32
+do
33
+ test_expect_success 'autocorrect showing candidates' '
34
+ git config help.autocorrect $show &&
35
34
- test_must_fail git lfg 2>actual &&
35
- grep "^ lgf" actual &&
36
+ test_must_fail git lfg 2>actual &&
37
+ grep "^ lgf" actual &&
38
37
- test_must_fail git distimdist 2>actual &&
38
- grep "^ distimdistim" actual
39
-'
39
+ test_must_fail git distimdist 2>actual &&
40
+ grep "^ distimdistim" actual
41
+ '
42
+done
43
44
for immediate in -1 immediate
45
do