Skip to content

Commit dc66e3c

Browse files
azeembagitster
authored andcommitted
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/[email protected]/ Signed-off-by: Azeem Bande-Ali <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5d213e4 commit dc66e3c

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

Documentation/config/help.txt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ help.format::
99

1010
help.autoCorrect::
1111
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.
1921

2022
help.htmlPath::
2123
Specify the path where the HTML documentation resides. File system paths

help.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "version.h"
1212
#include "refs.h"
1313
#include "parse-options.h"
14+
#include "prompt.h"
1415

1516
struct category_description {
1617
uint32_t category;
@@ -472,6 +473,7 @@ int is_in_cmdlist(struct cmdnames *c, const char *s)
472473
static int autocorrect;
473474
static struct cmdnames aliases;
474475

476+
#define AUTOCORRECT_PROMPT (-3)
475477
#define AUTOCORRECT_NEVER (-2)
476478
#define AUTOCORRECT_IMMEDIATELY (-1)
477479

@@ -486,6 +488,8 @@ static int git_unknown_cmd_config(const char *var, const char *value, void *cb)
486488
autocorrect = AUTOCORRECT_NEVER;
487489
} else if (!strcmp(value, "immediate")) {
488490
autocorrect = AUTOCORRECT_IMMEDIATELY;
491+
} else if (!strcmp(value, "prompt")) {
492+
autocorrect = AUTOCORRECT_PROMPT;
489493
} else {
490494
int v = git_config_int(var, value);
491495
autocorrect = (v < 0)
@@ -539,6 +543,12 @@ const char *help_unknown_cmd(const char *cmd)
539543

540544
read_early_config(git_unknown_cmd_config, NULL);
541545

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+
542552
if (autocorrect == AUTOCORRECT_NEVER) {
543553
fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd);
544554
exit(1);
@@ -618,7 +628,16 @@ const char *help_unknown_cmd(const char *cmd)
618628
_("Continuing under the assumption that "
619629
"you meant '%s'."),
620630
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 {
622641
fprintf_ln(stderr,
623642
_("Continuing in %0.1f seconds, "
624643
"assuming that you meant '%s'."),

0 commit comments

Comments
 (0)