Message ID | pull.1012.git.1628640954160.gitgitgadget@gmail.com (mailing list archive) |
---|---|
State | Superseded |
Headers | show |
Series | help.c: help.autocorrect=prompt waits for user action | expand |
On 11/08/21 07.15, Azeem Bande-Ali via GitGitGadget wrote: > help.autoCorrect:: > If git detects typos and can identify exactly one valid command similar > - to the error, git will automatically run the intended command after > - waiting a duration of time defined by this configuration value in > - deciseconds (0.1 sec). If this value is 0, the suggested corrections > - will be shown, but not executed. If it is a negative integer, or > - "immediate", the suggested command > - is run immediately. If "never", suggestions are not shown at all. The > - default value is zero. > + to the error, git will try to suggest the correct command or even > + run the intended command. > + If this config value is 0, then the suggested command will be shown. > + If it is positive, the suggested command will automatically > + run after waiting for that many deciseconds (0.1 sec). > + If it is "immediate", the suggested command will be > + run immediately. > + If it is "prompt", then the user will be shown the > + suggestion and will be prompted for confirmation before the command > + is run. > + If it is "never", then no suggestion will be shown and no command > + will be run. > + 0 is the default value for this config. I think it's better to use bullet lists, e.g.: ``` If git detects typos and ... <skip> ... git will try to suggest the correct command or even run the intended command. Possible values are: - 0 (default): show the suggested command - positive number: run the suggested command after specified deciseconds (0.1 sec). - "immediate": run the suggested command immediately - "prompt": show the suggestion and prompt for confirmation to run the command - "never": don't show any suggestions nor run them ```
> @@ -618,7 +622,18 @@ const char *help_unknown_cmd(const char *cmd) > _("Continuing under the assumption that " > "you meant '%s'."), > assumed); > - else { > + else if (autocorrect == AUTOCORRECT_PROMPT) { > + if (!isatty(STDIN_FILENO) | !isatty(STDERR_FILENO)) Don't do bitwise or when you only care about boolean outcome. > + exit(1); This is not a new issue, but I think it is probably a bug in the original design of the help-unknown that we do not do this check much early in help_unknown_cmd(), to disable auto-correction in a non-interactive session. If we did so, we do not have to have this check here. If we cannot yet come to consensus that disabling autocorrection when running non-interactively is a good idea, at least we should be able to do so only for _PROMPT case, like the attached patch at the end. > + char *answer; This is decl-after-statement, but as I am suggesting to get rid of the tty check done inside this block, it will become OK. > + fprintf_ln(stderr, _("Assuming you meant: '%s'."), > + assumed); > + answer = git_prompt(_("Continue? (y/N)"), PROMPT_ECHO); Hmph, the above does not look WRONG per-se, but I wonder if it is easier for the users to see a single line, e.g. struct strbuf msg = STRBUF_INIT; strbuf_addf(&msg, _("Run '%s' instead? (y/N)"), assumed); answer = git_prompt(msg.buf, PROMPT_ECHO); strbuf_release(&msg); Thanks. diff --git i/help.c w/help.c index e22ba1d246..3629cb8796 100644 --- i/help.c +++ w/help.c @@ -540,6 +540,13 @@ const char *help_unknown_cmd(const char *cmd) 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) { fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd); exit(1);
On Thu, Aug 12, 2021 at 6:52 AM Bagas Sanjaya <bagasdotme@gmail.com> wrote: > I think it's better to use bullet lists, e.g.: > > ``` > If git detects typos and ... > <skip> > ... git will try to suggest the correct command or even run the intended > command. Possible values are: > - 0 (default): show the suggested command > - positive number: run the suggested command after specified > deciseconds (0.1 sec). > - "immediate": run the suggested command immediately > - "prompt": show the suggestion and prompt for confirmation to run > the command > - "never": don't show any suggestions nor run them > ``` Thanks for the feedback. I agree the bullets do look much better!
On Thu, Aug 12, 2021 at 3:38 PM Junio C Hamano <gitster@pobox.com> wrote: > > If we cannot yet come to consensus that disabling autocorrection > when running non-interactively is a good idea, at least we should be > able to do so only for _PROMPT case, like the attached patch at the > end. Thanks for the suggestion. I think it makes sense to skip the autocorrection completely when the setting is "prompt" and the user is not running interactively. > > + fprintf_ln(stderr, _("Assuming you meant: '%s'."), > > + assumed); > > + answer = git_prompt(_("Continue? (y/N)"), PROMPT_ECHO); > > Hmph, the above does not look WRONG per-se, but I wonder if it is > easier for the users to see a single line, e.g. > > struct strbuf msg = STRBUF_INIT; > strbuf_addf(&msg, _("Run '%s' instead? (y/N)"), assumed); > answer = git_prompt(msg.buf, PROMPT_ECHO); > strbuf_release(&msg); > I looked at the behavior for other settings and they all behave pretty compactly. So I think this suggested solution does fit better. > + /* > + * Disable autocorrection prompt in a non-interactive session > + */ > + if ((autocorrect != AUTOCORRECT_PROMPT) && > + (!isatty(0) || !isatty(2))) > + autocorrect = AUTOCORRECT_NEVER; > + That should be `(autocorrect == AUTOCORRECT_PROMPT)` right? So we skip the autocorrect if the user is not running interactively and has the "prompt" setting. Thanks for all the feedback! I will send out a patch incorporating this feedback soon.
diff --git a/Documentation/config/help.txt b/Documentation/config/help.txt index 783a90a0f93..d36d98dd119 100644 --- a/Documentation/config/help.txt +++ b/Documentation/config/help.txt @@ -9,13 +9,19 @@ help.format:: help.autoCorrect:: If git detects typos and can identify exactly one valid command similar - to the error, git will automatically run the intended command after - waiting a duration of time defined by this configuration value in - deciseconds (0.1 sec). If this value is 0, the suggested corrections - will be shown, but not executed. If it is a negative integer, or - "immediate", the suggested command - is run immediately. If "never", suggestions are not shown at all. The - default value is zero. + to the error, git will try to suggest the correct command or even + run the intended command. + If this config value is 0, then the suggested command will be shown. + If it is positive, the suggested command will automatically + run after waiting for that many deciseconds (0.1 sec). + If it is "immediate", the suggested command will be + run immediately. + If it is "prompt", then the user will be shown the + suggestion and will be prompted for confirmation before the command + is run. + If it is "never", then no suggestion will be shown and no command + will be run. + 0 is the default value for this config. help.htmlPath:: Specify the path where the HTML documentation resides. File system paths diff --git a/help.c b/help.c index 3c3bdec2135..079156e0421 100644 --- a/help.c +++ b/help.c @@ -11,6 +11,7 @@ #include "version.h" #include "refs.h" #include "parse-options.h" +#include "prompt.h" struct category_description { uint32_t category; @@ -472,6 +473,7 @@ int is_in_cmdlist(struct cmdnames *c, const char *s) static int autocorrect; static struct cmdnames aliases; +#define AUTOCORRECT_PROMPT (-3) #define AUTOCORRECT_NEVER (-2) #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; } else if (!strcmp(value, "immediate")) { autocorrect = AUTOCORRECT_IMMEDIATELY; + } else if (!strcmp(value, "prompt")) { + autocorrect = AUTOCORRECT_PROMPT; } else { int v = git_config_int(var, value); autocorrect = (v < 0) @@ -618,7 +622,18 @@ const char *help_unknown_cmd(const char *cmd) _("Continuing under the assumption that " "you meant '%s'."), assumed); - else { + else if (autocorrect == AUTOCORRECT_PROMPT) { + if (!isatty(STDIN_FILENO) | !isatty(STDERR_FILENO)) + exit(1); + + char *answer; + fprintf_ln(stderr, _("Assuming you meant: '%s'."), + assumed); + answer = git_prompt(_("Continue? (y/N)"), PROMPT_ECHO); + if (!(starts_with(answer, "y") || + starts_with(answer, "Y"))) + exit(1); + } else { fprintf_ln(stderr, _("Continuing in %0.1f seconds, " "assuming that you meant '%s'."),