Merge branch 'ab/help-autocorrect-prompt'

The logic for auto-correction of misspelt subcommands learned to go
interactive when the help.autocorrect configuration variable is set
to 'prompt'.

* ab/help-autocorrect-prompt:
  help.c: help.autocorrect=prompt waits for user action
This commit is contained in:
Junio C Hamano 2021-09-10 11:46:33 -07:00
commit 96ac07f4a9
2 changed files with 29 additions and 8 deletions

View file

@ -9,13 +9,15 @@ help.format::
help.autoCorrect:: help.autoCorrect::
If git detects typos and can identify exactly one valid command similar If git detects typos and can identify exactly one valid command similar
to the error, git will automatically run the intended command after to the error, git will try to suggest the correct command or even
waiting a duration of time defined by this configuration value in run the suggestion automatically. Possible config values are:
deciseconds (0.1 sec). If this value is 0, the suggested corrections - 0 (default): show the suggested command.
will be shown, but not executed. If it is a negative integer, or - positive number: run the suggested command after specified
"immediate", the suggested command deciseconds (0.1 sec).
is run immediately. If "never", suggestions are not shown at all. The - "immediate": run the suggested command immediately.
default value is zero. - "prompt": show the suggestion and prompt for confirmation to run
the command.
- "never": don't run or show any suggested command.
help.htmlPath:: help.htmlPath::
Specify the path where the HTML documentation resides. File system paths Specify the path where the HTML documentation resides. File system paths

21
help.c
View file

@ -11,6 +11,7 @@
#include "version.h" #include "version.h"
#include "refs.h" #include "refs.h"
#include "parse-options.h" #include "parse-options.h"
#include "prompt.h"
struct category_description { struct category_description {
uint32_t category; uint32_t category;
@ -472,6 +473,7 @@ int is_in_cmdlist(struct cmdnames *c, const char *s)
static int autocorrect; static int autocorrect;
static struct cmdnames aliases; static struct cmdnames aliases;
#define AUTOCORRECT_PROMPT (-3)
#define AUTOCORRECT_NEVER (-2) #define AUTOCORRECT_NEVER (-2)
#define AUTOCORRECT_IMMEDIATELY (-1) #define AUTOCORRECT_IMMEDIATELY (-1)
@ -486,6 +488,8 @@ static int git_unknown_cmd_config(const char *var, const char *value, void *cb)
autocorrect = AUTOCORRECT_NEVER; autocorrect = AUTOCORRECT_NEVER;
} else if (!strcmp(value, "immediate")) { } else if (!strcmp(value, "immediate")) {
autocorrect = AUTOCORRECT_IMMEDIATELY; autocorrect = AUTOCORRECT_IMMEDIATELY;
} else if (!strcmp(value, "prompt")) {
autocorrect = AUTOCORRECT_PROMPT;
} else { } else {
int v = git_config_int(var, value); int v = git_config_int(var, value);
autocorrect = (v < 0) autocorrect = (v < 0)
@ -539,6 +543,12 @@ const char *help_unknown_cmd(const char *cmd)
read_early_config(git_unknown_cmd_config, NULL); read_early_config(git_unknown_cmd_config, NULL);
/*
* Disable autocorrection prompt in a non-interactive session
*/
if ((autocorrect == AUTOCORRECT_PROMPT) && (!isatty(0) || !isatty(2)))
autocorrect = AUTOCORRECT_NEVER;
if (autocorrect == AUTOCORRECT_NEVER) { if (autocorrect == AUTOCORRECT_NEVER) {
fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd); fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd);
exit(1); exit(1);
@ -618,7 +628,16 @@ const char *help_unknown_cmd(const char *cmd)
_("Continuing under the assumption that " _("Continuing under the assumption that "
"you meant '%s'."), "you meant '%s'."),
assumed); assumed);
else { else if (autocorrect == AUTOCORRECT_PROMPT) {
char *answer;
struct strbuf msg = STRBUF_INIT;
strbuf_addf(&msg, _("Run '%s' instead? (y/N)"), assumed);
answer = git_prompt(msg.buf, PROMPT_ECHO);
strbuf_release(&msg);
if (!(starts_with(answer, "y") ||
starts_with(answer, "Y")))
exit(1);
} else {
fprintf_ln(stderr, fprintf_ln(stderr,
_("Continuing in %0.1f seconds, " _("Continuing in %0.1f seconds, "
"assuming that you meant '%s'."), "assuming that you meant '%s'."),