Skip to content

Commit b87c772

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 fbe8d30 commit b87c772

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

Documentation/config/help.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ help.autoCorrect::
1616
deciseconds (0.1 sec).
1717
- "immediate": run the suggested command immediately.
1818
- "prompt": show the suggestion and prompt for confirmation to run
19-
the command.
19+
the command. The default choice at the prompt is "No"
20+
- "prompt-yes": show the suggestion and prompt for confirmation to run
21+
the command. The default choice at the prompt is "Yes"
2022
- "never": don't run or show any suggested command.
2123

2224
help.htmlPath::

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 (-4)
555556
#define AUTOCORRECT_PROMPT (-3)
556557
#define AUTOCORRECT_NEVER (-2)
557558
#define AUTOCORRECT_IMMEDIATELY (-1)
@@ -572,6 +573,8 @@ static int git_unknown_cmd_config(const char *var, const char *value,
572573
cfg->autocorrect = AUTOCORRECT_IMMEDIATELY;
573574
} else if (!strcmp(value, "prompt")) {
574575
cfg->autocorrect = AUTOCORRECT_PROMPT;
576+
} else if (!strcmp(value, "prompt-yes")) {
577+
cfg->autocorrect = AUTOCORRECT_PROMPT_YES;
575578
} else {
576579
int v = git_config_int(var, value, ctx->kvi);
577580
cfg->autocorrect = (v < 0)
@@ -629,6 +632,9 @@ char *help_unknown_cmd(const char *cmd)
629632
if ((cfg.autocorrect == AUTOCORRECT_PROMPT) && (!isatty(0) || !isatty(2)))
630633
cfg.autocorrect = AUTOCORRECT_NEVER;
631634

635+
if ((cfg.autocorrect == AUTOCORRECT_PROMPT_YES) && (!isatty(0) || !isatty(2)))
636+
cfg.autocorrect = AUTOCORRECT_IMMEDIATELY;
637+
632638
if (cfg.autocorrect == AUTOCORRECT_NEVER) {
633639
fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd);
634640
exit(1);
@@ -716,6 +722,15 @@ char *help_unknown_cmd(const char *cmd)
716722
if (!(starts_with(answer, "y") ||
717723
starts_with(answer, "Y")))
718724
exit(1);
725+
} else if (cfg.autocorrect == AUTOCORRECT_PROMPT_YES) {
726+
char *answer;
727+
struct strbuf msg = STRBUF_INIT;
728+
strbuf_addf(&msg, _("Run '%s' instead [Y/n]? "), assumed);
729+
answer = git_prompt(msg.buf, PROMPT_ECHO);
730+
strbuf_release(&msg);
731+
if (starts_with(answer, "n") ||
732+
starts_with(answer, "N"))
733+
exit(1);
719734
} else {
720735
fprintf_ln(stderr,
721736
_("Continuing in %0.1f seconds, "

0 commit comments

Comments
 (0)