# [PATCH] help.c: help.autocorrect=prompt waits for user action

12 messages from 2021-08-11 to 2021-09-08. Participants: Azeem Bande-Ali via GitGitGadget, Bagas Sanjaya, Junio C Hamano, Azeem Bande-Ali, Johannes Schindelin.
Thread: https://gitlist.dev/t/56252

## Azeem Bande-Ali via GitGitGadget, 2021-08-11 00:15

Subject: [PATCH] help.c: help.autocorrect=prompt waits for user action
Message-ID: <pull.1012.git.1628640954160.gitgitgadget@gmail.com>
URL: https://gitlist.dev/e/pull.1012.git.1628640954160.gitgitgadget%40gmail.com

```
From: Azeem Bande-Ali <me@azeemba.com>

If help.autocorrect is set to 'prompt', the user is prompted
before the suggested action is executed.

Based on original patch by David Barr
https://lore.kernel.org/git/1283758030-13345-1-git-send-email-david.barr@cordelta.com/

Signed-off-by: Azeem Bande-Ali <me@azeemba.com>
---
    New config for help.autocorrect to prompt user before action
    
    Currently the prompt setting will wait X amount of time before taking an
    action. Adding a new config that will instead prompt the user for a
    confirmation before taking the action.

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1012%2Fazeemba%2Fautoprompt-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1012/azeemba/autoprompt-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/1012

 Documentation/config/help.txt | 20 +++++++++++++-------
 help.c                        | 17 ++++++++++++++++-
 2 files changed, 29 insertions(+), 8 deletions(-)

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'."),

base-commit: 2d755dfac9aadab25c3e025b849252b8c0a61465
-- 
gitgitgadget

```

## Bagas Sanjaya, 2021-08-12 10:52

Subject: Re: [PATCH] help.c: help.autocorrect=prompt waits for user action
Message-ID: <4f72cd3d-bcc7-a9b0-cc11-f0d7c5650365@gmail.com>
URL: https://gitlist.dev/e/4f72cd3d-bcc7-a9b0-cc11-f0d7c5650365%40gmail.com
In-Reply-To: <pull.1012.git.1628640954160.gitgitgadget@gmail.com>

```
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
ˋˋˋ

-- 
An old man doll... just what I always wanted! - Clara

```

## Junio C Hamano, 2021-08-12 19:37

Subject: Re: [PATCH] help.c: help.autocorrect=prompt waits for user action
Message-ID: <xmqqh7fu314a.fsf@gitster.g>
URL: https://gitlist.dev/e/xmqqh7fu314a.fsf%40gitster.g
In-Reply-To: <pull.1012.git.1628640954160.gitgitgadget@gmail.com>

```
> @@ -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);




```

## Azeem Bande-Ali, 2021-08-14 02:57

Subject: Re: [PATCH] help.c: help.autocorrect=prompt waits for user action
Message-ID: <CABye916n=EuKU3u2ybNtmT8Te9y0C__NsPR7Vdj9LKrf-y9coQ@mail.gmail.com>
URL: https://gitlist.dev/e/CABye916n%3DEuKU3u2ybNtmT8Te9y0C__NsPR7Vdj9LKrf-y9coQ%40mail.gmail.com
In-Reply-To: <4f72cd3d-bcc7-a9b0-cc11-f0d7c5650365@gmail.com>

```
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!

```

## Azeem Bande-Ali, 2021-08-14 03:07

Subject: Re: [PATCH] help.c: help.autocorrect=prompt waits for user action
Message-ID: <CABye9140=jnkERq8MLEcr_umW1+oaE5Wi39_iQnM3jevV3Hraw@mail.gmail.com>
URL: https://gitlist.dev/e/CABye9140%3DjnkERq8MLEcr_umW1%2BoaE5Wi39_iQnM3jevV3Hraw%40mail.gmail.com
In-Reply-To: <xmqqh7fu314a.fsf@gitster.g>

```
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.

```

## Azeem Bande-Ali via GitGitGadget, 2021-08-14 05:11

Subject: [PATCH v2] help.c: help.autocorrect=prompt waits for user action
Message-ID: <pull.1012.v2.git.1628917872724.gitgitgadget@gmail.com>
URL: https://gitlist.dev/e/pull.1012.v2.git.1628917872724.gitgitgadget%40gmail.com
In-Reply-To: <pull.1012.git.1628640954160.gitgitgadget@gmail.com>

```
From: Azeem Bande-Ali <me@azeemba.com>

If help.autocorrect is set to 'prompt', the user is prompted
before the suggested action is executed.

Based on original patch by David Barr
https://lore.kernel.org/git/1283758030-13345-1-git-send-email-david.barr@cordelta.com/

Signed-off-by: Azeem Bande-Ali <me@azeemba.com>
---
    New config for help.autocorrect to prompt user before action
    
    Changes since v1:
    
     * Use bullet lists for documenting the various settings now supported
       by help.autoCorrect
     * Add a check for an interactive terminal early to bail if the user has
       the "prompt" setting
     * Make the output more compact so the user can see the suggestion and
       the prompt in one line.

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1012%2Fazeemba%2Fautoprompt-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1012/azeemba/autoprompt-v2
Pull-Request: https://github.com/gitgitgadget/git/pull/1012

Range-diff vs v1:

 1:  7f72819b805 ! 1:  5819b872356 help.c: help.autocorrect=prompt waits for user action
     @@ Documentation/config/help.txt: help.format::
      -	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.
     ++	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
     @@ help.c: static int git_unknown_cmd_config(const char *var, const char *value, vo
       			int v = git_config_int(var, value);
       			autocorrect = (v < 0)
      @@ help.c: 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);
     +@@ help.c: 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);
     ++			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);


 Documentation/config/help.txt | 16 +++++++++-------
 help.c                        | 22 +++++++++++++++++++++-
 2 files changed, 30 insertions(+), 8 deletions(-)

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'."),

base-commit: 2d755dfac9aadab25c3e025b849252b8c0a61465
-- 
gitgitgadget

```

## Bagas Sanjaya, 2021-08-14 05:50

Subject: Re: [PATCH v2] help.c: help.autocorrect=prompt waits for user action
Message-ID: <c3b9aac8-992c-30de-c6a1-852b81ddecff@gmail.com>
URL: https://gitlist.dev/e/c3b9aac8-992c-30de-c6a1-852b81ddecff%40gmail.com
In-Reply-To: <pull.1012.v2.git.1628917872724.gitgitgadget@gmail.com>

```
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>

-- 
An old man doll... just what I always wanted! - Clara

```

## Junio C Hamano, 2021-08-14 18:20

Subject: Re: [PATCH v2] help.c: help.autocorrect=prompt waits for user action
Message-ID: <xmqqmtpjubuv.fsf@gitster.g>
URL: https://gitlist.dev/e/xmqqmtpjubuv.fsf%40gitster.g
In-Reply-To: <pull.1012.v2.git.1628917872724.gitgitgadget@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.

```

## Azeem Bande-Ali, 2021-08-14 18:40

Subject: Re: [PATCH v2] help.c: help.autocorrect=prompt waits for user action
Message-ID: <CABye917ALhCsfg_7JD72TxO0i2bhTc-0pJKKZwt8BVdiv1Qw4w@mail.gmail.com>
URL: https://gitlist.dev/e/CABye917ALhCsfg_7JD72TxO0i2bhTc-0pJKKZwt8BVdiv1Qw4w%40mail.gmail.com
In-Reply-To: <xmqqmtpjubuv.fsf@gitster.g>

```
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?

```

## Azeem Bande-Ali via GitGitGadget, 2021-08-15 01:50

Subject: [PATCH v3] help.c: help.autocorrect=prompt waits for user action
Message-ID: <pull.1012.v3.git.1628992226139.gitgitgadget@gmail.com>
URL: https://gitlist.dev/e/pull.1012.v3.git.1628992226139.gitgitgadget%40gmail.com
In-Reply-To: <pull.1012.v2.git.1628917872724.gitgitgadget@gmail.com>

```
From: Azeem Bande-Ali <me@azeemba.com>

If help.autocorrect is set to 'prompt', the user is prompted
before the suggested action is executed.

Based on original patch by David Barr
https://lore.kernel.org/git/1283758030-13345-1-git-send-email-david.barr@cordelta.com/

Reviewed-by: Bagas Sanjaya <bagasdotme@gmail.com>
Signed-off-by: Azeem Bande-Ali <me@azeemba.com>
---
    New config for help.autocorrect to prompt user before action
    
    Only minor style changes have been made since v2:
    
     * Moved * to variable name instead of the type name
     * Keep code in the same line for readability

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1012%2Fazeemba%2Fautoprompt-v3
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1012/azeemba/autoprompt-v3
Pull-Request: https://github.com/gitgitgadget/git/pull/1012

Range-diff vs v2:

 1:  5819b872356 ! 1:  f3f38f0d141 help.c: help.autocorrect=prompt waits for user action
     @@ Commit message
          Based on original patch by David Barr
          https://lore.kernel.org/git/1283758030-13345-1-git-send-email-david.barr@cordelta.com/
      
     +    Reviewed-by: Bagas Sanjaya <bagasdotme@gmail.com>
          Signed-off-by: Azeem Bande-Ali <me@azeemba.com>
      
       ## Documentation/config/help.txt ##
     @@ help.c: const char *help_unknown_cmd(const char *cmd)
       				   assumed);
      -		else {
      +		else if (autocorrect == AUTOCORRECT_PROMPT) {
     -+			char* answer;
     ++			char *answer;
      +			struct strbuf msg = STRBUF_INIT;
     -+			strbuf_addf(&msg, _("Run '%s' instead? (y/N)"),
     -+				    assumed);
     ++			strbuf_addf(&msg, _("Run '%s' instead? (y/N)"), assumed);
      +			answer = git_prompt(msg.buf, PROMPT_ECHO);
      +			strbuf_release(&msg);
      +			if (!(starts_with(answer, "y") ||


 Documentation/config/help.txt | 16 +++++++++-------
 help.c                        | 21 ++++++++++++++++++++-
 2 files changed, 29 insertions(+), 8 deletions(-)

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..be2fa642415 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,16 @@ 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'."),

base-commit: 2d755dfac9aadab25c3e025b849252b8c0a61465
-- 
gitgitgadget

```

## Johannes Schindelin, 2021-08-16 12:28

Subject: Re: [PATCH v2] help.c: help.autocorrect=prompt waits for user action
Message-ID: <nycvar.QRO.7.76.6.2108161427270.55@tvgsbejvaqbjf.bet>
URL: https://gitlist.dev/e/nycvar.QRO.7.76.6.2108161427270.55%40tvgsbejvaqbjf.bet
In-Reply-To: <xmqqmtpjubuv.fsf@gitster.g>

```
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

```

## Junio C Hamano, 2021-09-08 00:18

Subject: Re: [PATCH v3] help.c: help.autocorrect=prompt waits for user action
Message-ID: <xmqqee9z9ayf.fsf@gitster.g>
URL: https://gitlist.dev/e/xmqqee9z9ayf.fsf%40gitster.g
In-Reply-To: <pull.1012.v3.git.1628992226139.gitgitgadget@gmail.com>

```
"Azeem Bande-Ali via GitGitGadget" <gitgitgadget@gmail.com> writes:

> From: Azeem Bande-Ali <me@azeemba.com>
>
> If help.autocorrect is set to 'prompt', the user is prompted
> before the suggested action is executed.
>
> Based on original patch by David Barr
> https://lore.kernel.org/git/1283758030-13345-1-git-send-email-david.barr@cordelta.com/
>
> Reviewed-by: Bagas Sanjaya <bagasdotme@gmail.com>
> Signed-off-by: Azeem Bande-Ali <me@azeemba.com>
> ---
>     New config for help.autocorrect to prompt user before action
>     
>     Only minor style changes have been made since v2:
>     
>      * Moved * to variable name instead of the type name
>      * Keep code in the same line for readability

I didn't see anybody comment on this round (and do not think I saw
anything glaringly wrong).

Is everybody happy with this version?  I am about to mark it for
'next' in the next issue of "What's cooking" report, so please
holler if I should wait.

Thanks.

```
