Message ID | pull.1012.v2.git.1628917872724.gitgitgadget@gmail.com (mailing list archive) |
---|---|
State | Superseded |
Headers | show |
Series | [v2] help.c: help.autocorrect=prompt waits for user action | expand |
On 14/08/21 12.11, 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 suggestion automatically. Possible config 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 run or show any suggested command. Looks OK. Reviewed-by: Bagas Sanjaya <bagasdotme@gmail.com>
"Azeem Bande-Ali via GitGitGadget" <gitgitgadget@gmail.com> writes: > @@ -618,7 +628,17 @@ const char *help_unknown_cmd(const char *cmd) > _("Continuing under the assumption that " > "you meant '%s'."), > assumed); > - else { > + else if (autocorrect == AUTOCORRECT_PROMPT) { > + char* answer; Some people seem to make an asterisk stick to types like this, but in our codebase written in C, an asterisk sticks to the identifier that it makes into a pointer, i.e. char *answer; This is because doing so differently would confuse novices with constructs like this: int* a, b; where only 'a' is a pointer, and 'b' is not. > + struct strbuf msg = STRBUF_INIT; > + strbuf_addf(&msg, _("Run '%s' instead? (y/N)"), > + assumed); I think these should be kept on a single line for readability, i.e. strbuf_addf(&msg, _("Run '%s' instead? (y/N)"), assumed); as I see a fairly long line after this block already. fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd); Other than these cosmetic bits, this round looks good to me, including the documentation update. Thanks.
On Sat, Aug 14, 2021 at 2:20 PM Junio C Hamano <gitster@pobox.com> wrote: > > "Azeem Bande-Ali via GitGitGadget" <gitgitgadget@gmail.com> writes: > > > + char* answer; > > Some people seem to make an asterisk stick to types like this, but > in our codebase written in C, an asterisk sticks to the identifier > that it makes into a pointer, i.e. > > char *answer; > Thanks! Will fix. I appreciate you providing the motivation for the policy as well! > > + struct strbuf msg = STRBUF_INIT; > > + strbuf_addf(&msg, _("Run '%s' instead? (y/N)"), > > + assumed); > > I think these should be kept on a single line for readability, i.e. > > strbuf_addf(&msg, _("Run '%s' instead? (y/N)"), assumed); > > as I see a fairly long line after this block already. > > fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd); The line split was done by clang-format via `make style`. Is it okay to ignore the clang-format recommendation? (Incidentally `make style` would have also caught the first issue, I seem to have not run it after making a change in that line.) > Other than these cosmetic bits, this round looks good to me, > including the documentation update. Great thanks! This is my first patch so I just wanted to confirm: should I add a "Reviewed-by:" line to the commit with your name as well?
Hi, On Sat, 14 Aug 2021, Junio C Hamano wrote: > "Azeem Bande-Ali via GitGitGadget" <gitgitgadget@gmail.com> writes: > > > @@ -618,7 +628,17 @@ const char *help_unknown_cmd(const char *cmd) > > _("Continuing under the assumption that " > > "you meant '%s'."), > > assumed); > > - else { > > + else if (autocorrect == AUTOCORRECT_PROMPT) { > > + char* answer; > > Some people seem to make an asterisk stick to types like this, but > in our codebase written in C, an asterisk sticks to the identifier > that it makes into a pointer, i.e. > > char *answer; > > This is because doing so differently would confuse novices with > constructs like this: > > int* a, b; > > where only 'a' is a pointer, and 'b' is not. > > > + struct strbuf msg = STRBUF_INIT; > > + strbuf_addf(&msg, _("Run '%s' instead? (y/N)"), > > + assumed); > > I think these should be kept on a single line for readability, i.e. > > strbuf_addf(&msg, _("Run '%s' instead? (y/N)"), assumed); It might even make sense to use the `xstrfmt()` function instead: char *msg = xstrfmt(_("Run '%s' instead? (y/N)"), assumed); [...] free(msg); Ciao, Dscho
diff --git a/Documentation/config/help.txt b/Documentation/config/help.txt index 783a90a0f93..610701f9a37 100644 --- a/Documentation/config/help.txt +++ b/Documentation/config/help.txt @@ -9,13 +9,15 @@ 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 suggestion automatically. Possible config 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 run or show any suggested command. help.htmlPath:: Specify the path where the HTML documentation resides. File system paths diff --git a/help.c b/help.c index 3c3bdec2135..22bb05e8e3d 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) @@ -539,6 +543,12 @@ 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); @@ -618,7 +628,17 @@ const char *help_unknown_cmd(const char *cmd) _("Continuing under the assumption that " "you meant '%s'."), 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, _("Continuing in %0.1f seconds, " "assuming that you meant '%s'."),