help.c: help.autocorrect=prompt waits for user action
If help.autocorrect is set to 'prompt', the user is prompted before the suggested action is executed. Based on original patch by David Barr https://lore.kernel.org/git/1283758030-13345-1-git-send-email-david.barr@cordelta.com/ Signed-off-by: Azeem Bande-Ali <me@azeemba.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Azeem Bande-Ali committed
Aug 14, 2021 at 05:11 UTC
dc66e3c799cd5d8f3e664e26899bca200ffdfd7b
2 files changed
+29
-8
Documentation/config/help.txt
+9
-7
@@ -9,13 +9,15 @@ help.format::
9
10
help.autoCorrect::
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.
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 (default): show the suggested command.
15
+ - positive number: run the suggested command after specified
16
+deciseconds (0.1 sec).
17
+ - "immediate": run the suggested command immediately.
18
+ - "prompt": show the suggestion and prompt for confirmation to run
19
+the command.
20
+ - "never": don't run or show any suggested command.
21
22
help.htmlPath::
23
Specify the path where the HTML documentation resides. File system paths
help.c
+20
-1
@@ -11,6 +11,7 @@
11
#include "version.h"
12
#include "refs.h"
13
#include "parse-options.h"
14
+#include "prompt.h"
15
16
struct category_description {
17
uint32_t category;
@@ -472,6 +473,7 @@ int is_in_cmdlist(struct cmdnames *c, const char *s)
473
static int autocorrect;
474
static struct cmdnames aliases;
475
476
+#define AUTOCORRECT_PROMPT (-3)
477
#define AUTOCORRECT_NEVER (-2)
478
#define AUTOCORRECT_IMMEDIATELY (-1)
479
@@ -486,6 +488,8 @@ static int git_unknown_cmd_config(const char *var, const char *value, void *cb)
488
autocorrect = AUTOCORRECT_NEVER;
489
} else if (!strcmp(value, "immediate")) {
490
autocorrect = AUTOCORRECT_IMMEDIATELY;
491
+ } else if (!strcmp(value, "prompt")) {
492
+ autocorrect = AUTOCORRECT_PROMPT;
493
} else {
494
int v = git_config_int(var, value);
495
autocorrect = (v < 0)
@@ -539,6 +543,12 @@ const char *help_unknown_cmd(const char *cmd)
543
544
read_early_config(git_unknown_cmd_config, NULL);
545
546
+ /*
547
+ * Disable autocorrection prompt in a non-interactive session
548
+ */
549
+ if ((autocorrect == AUTOCORRECT_PROMPT) && (!isatty(0) || !isatty(2)))
550
+ autocorrect = AUTOCORRECT_NEVER;
551
+
552
if (autocorrect == AUTOCORRECT_NEVER) {
553
fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd);
554
exit(1);
@@ -618,7 +628,16 @@ const char *help_unknown_cmd(const char *cmd)
628
_("Continuing under the assumption that "
629
"you meant '%s'."),
630
assumed);
621
- else {
631
+ else if (autocorrect == AUTOCORRECT_PROMPT) {
632
+ char *answer;
633
+ struct strbuf msg = STRBUF_INIT;
634
+ strbuf_addf(&msg, _("Run '%s' instead? (y/N)"), assumed);
635
+ answer = git_prompt(msg.buf, PROMPT_ECHO);
636
+ strbuf_release(&msg);
637
+ if (!(starts_with(answer, "y") ||
638
+ starts_with(answer, "Y")))
639
+ exit(1);
640
+ } else {
641
fprintf_ln(stderr,
642
_("Continuing in %0.1f seconds, "
643
"assuming that you meant '%s'."),