Skip to content

Commit 8024ae7

Browse files
committed
help: add prompt-yes setting for autocorrect
The help.autocorrect functionality is really useful, saving frustration when a dev fat-fingers a command, and git has a pretty good idea what was originally intended. The config settings are a nice selection, with "prompt" asking the user to confirm that they want to run the assumed command. However, with "prompt", the choice defaults to "No" - that is, hitting return will _not_ run the command. For me at least, if git is confident it knows which command I wanted, it's usually right, and the golden path would be to run the command. Therefore this patch adds "prompt-yes" as a counterpart config setting for help.autocorrect, which does the same as "prompt", but defaults to "Yes" - hitting return will run the assumed command. I have not added any tests because the test suite doesn't have any tests (that I could find) for the "prompt" behaviour - I'm assuming this is because it's hard/impossible to simulate the interactive terminal prompt Signed-off-by: Chris Howlett <[email protected]>
1 parent 0394451 commit 8024ae7

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

Documentation/config/help.adoc

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ immediately.
1818
deciseconds (0.1 sec).
1919
- "never": don't run or show any suggested command.
2020
- "prompt": show the suggestion and prompt for confirmation to run
21-
the command.
21+
the command. The default choice at the prompt is "No"
22+
- "prompt-yes": show the suggestion and prompt for confirmation to run
23+
the command. The default choice at the prompt is "Yes"
2224

2325
help.htmlPath::
2426
Specify the path where the HTML documentation resides. File system paths

help.c

+15
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,7 @@ struct help_unknown_cmd_config {
552552
struct cmdnames aliases;
553553
};
554554

555+
#define AUTOCORRECT_PROMPT_YES (-5)
555556
#define AUTOCORRECT_SHOW (-4)
556557
#define AUTOCORRECT_PROMPT (-3)
557558
#define AUTOCORRECT_NEVER (-2)
@@ -570,6 +571,8 @@ static int parse_autocorrect(const char *value)
570571

571572
if (!strcmp(value, "prompt"))
572573
return AUTOCORRECT_PROMPT;
574+
if (!strcmp(value, "prompt-yes"))
575+
return AUTOCORRECT_PROMPT_YES;
573576
if (!strcmp(value, "never"))
574577
return AUTOCORRECT_NEVER;
575578
if (!strcmp(value, "immediate"))
@@ -650,6 +653,9 @@ char *help_unknown_cmd(const char *cmd)
650653
if ((cfg.autocorrect == AUTOCORRECT_PROMPT) && (!isatty(0) || !isatty(2)))
651654
cfg.autocorrect = AUTOCORRECT_NEVER;
652655

656+
if ((cfg.autocorrect == AUTOCORRECT_PROMPT_YES) && (!isatty(0) || !isatty(2)))
657+
cfg.autocorrect = AUTOCORRECT_IMMEDIATELY;
658+
653659
if (cfg.autocorrect == AUTOCORRECT_NEVER) {
654660
fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd);
655661
exit(1);
@@ -738,6 +744,15 @@ char *help_unknown_cmd(const char *cmd)
738744
if (!(starts_with(answer, "y") ||
739745
starts_with(answer, "Y")))
740746
exit(1);
747+
} else if (cfg.autocorrect == AUTOCORRECT_PROMPT_YES) {
748+
char *answer;
749+
struct strbuf msg = STRBUF_INIT;
750+
strbuf_addf(&msg, _("Run '%s' instead [Y/n]? "), assumed);
751+
answer = git_prompt(msg.buf, PROMPT_ECHO);
752+
strbuf_release(&msg);
753+
if (starts_with(answer, "n") ||
754+
starts_with(answer, "N"))
755+
exit(1);
741756
} else {
742757
fprintf_ln(stderr,
743758
_("Continuing in %0.1f seconds, "

0 commit comments

Comments
 (0)