# [PATCH] RFC: help.autocorrect prompt

4 messages from 2010-09-06 to 2010-09-06. Participants: David Barr, Jonathan Nieder.
Thread: https://gitlist.dev/t/24998

## David Barr, 2010-09-06 02:28

Subject: [PATCH] RFC: help.autocorrect prompt
Message-ID: <1283740106-11411-1-git-send-email-david.barr@cordelta.com>
URL: https://gitlist.dev/e/1283740106-11411-1-git-send-email-david.barr%40cordelta.com

```
Added a new configuration value for help.autocorrect.
A value of 'prompt' causes git to wait for confirmation
before executing the assumed command.
---
 help.c |   13 ++++++++++---
 1 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/help.c b/help.c
index 7f4928e..924ce60 100644
--- a/help.c
+++ b/help.c
@@ -267,8 +267,12 @@ static struct cmdnames aliases;
 
 static int git_unknown_cmd_config(const char *var, const char *value, void *cb)
 {
-	if (!strcmp(var, "help.autocorrect"))
-		autocorrect = git_config_int(var,value);
+	if (!strcmp(var, "help.autocorrect")) {
+		if (!strcmp(value, "prompt"))
+			autocorrect = INT_MAX;
+		else
+			autocorrect = git_config_int(var,value);
+	}
 	/* Also use aliases for command lookup */
 	if (!prefixcmp(var, "alias."))
 		add_cmdname(&aliases, var + 6, strlen(var + 6));
@@ -342,7 +346,10 @@ const char *help_unknown_cmd(const char *cmd)
 			"which does not exist.\n"
 			"Continuing under the assumption that you meant '%s'\n",
 			cmd, assumed);
-		if (autocorrect > 0) {
+		if (autocorrect == INT_MAX) {
+			if (strcmp("y", git_getpass("Continue? (y/n) ")))
+				exit(1);
+		} else if (autocorrect > 0) {
 			fprintf(stderr, "in %0.1f seconds automatically...\n",
 				(float)autocorrect/10.0);
 			poll(NULL, 0, autocorrect * 100);
-- 
1.7.0.4

```

## Jonathan Nieder, 2010-09-06 02:59

Subject: Re: [PATCH] RFC: help.autocorrect prompt
Message-ID: <20100906025949.GA23594@burratino>
URL: https://gitlist.dev/e/20100906025949.GA23594%40burratino
In-Reply-To: <1283740106-11411-1-git-send-email-david.barr@cordelta.com>

```
(+cc: Dscho and Heiko)

David Barr wrote:

> A value of 'prompt' causes git to wait for confirmation
> before executing the assumed command.

Nice idea.

> ---

Sign-off?

> --- a/help.c
> +++ b/help.c
> @@ -267,8 +267,12 @@ static struct cmdnames aliases;
>  
>  static int git_unknown_cmd_config(const char *var, const char *value, void *cb)
>  {
> -	if (!strcmp(var, "help.autocorrect"))
> -		autocorrect = git_config_int(var,value);
> +	if (!strcmp(var, "help.autocorrect")) {
> +		if (!strcmp(value, "prompt"))
> +			autocorrect = INT_MAX;
> +		else
> +			autocorrect = git_config_int(var,value);

Any particular significance to INT_MAX rather than, e.g., -1 here?
(Just curious; it seems unlikely someone would use INT_MAX and
accidentally trip on this.)

Not a problem introduced by your patch: should we be checking for
out-of-range (e.g., negative) values?

> @@ -342,7 +346,10 @@ const char *help_unknown_cmd(const char *cmd)
>  			"which does not exist.\n"
>  			"Continuing under the assumption that you meant '%s'\n",
>  			cmd, assumed);
> -		if (autocorrect > 0) {
> +		if (autocorrect == INT_MAX) {
> +			if (strcmp("y", git_getpass("Continue? (y/n) ")))
> +				exit(1);

Funny. :)

It might be better to actually always write this prompt to the
terminal, rather than popping up a gui $GIT_ASKPASS if the user has
set that up.  Maybe something like Heiko Voigt's "mingw: make failures
to unlike or move raise a question" (9229029, 2010-02-21 from
4msysgit.git):

	if (!isatty(STDIN_FILENO) || !isatty(STDERR_FILENO))
		exit(1);
	fprintf(stderr, "Continue? (y/n) ");
	if (!fgets(answer, sizeof(answer), stdin))
		exit(1)
	if (*answer != 'y' && *answer != 'Y')
		exit(1);

> +		} else if (autocorrect > 0) {
>  			fprintf(stderr, "in %0.1f seconds automatically...\n",
>  				(float)autocorrect/10.0);
>  			poll(NULL, 0, autocorrect * 100);

Thanks for a pleasant read.
Jonathan

```

## David Barr, 2010-09-06 07:16

Subject: Re: [PATCH] RFC: help.autocorrect prompt
Message-ID: <201009061716.22468.david.barr@cordelta.com>
URL: https://gitlist.dev/e/201009061716.22468.david.barr%40cordelta.com
In-Reply-To: <20100906025949.GA23594@burratino>

```
 
> Any particular significance to INT_MAX rather than, e.g., -1 here?
> (Just curious; it seems unlikely someone would use INT_MAX and
> accidentally trip on this.)

> Not a problem introduced by your patch: should we be checking for
> out-of-range (e.g., negative) values?

The existing docs say that negative values correspond to immediate execution,
zero to never, and positive to a delay in deciseconds.

As noted, I chose INT_MAX as a rather unlikely conscious choice (~7 years).

> > @@ -342,7 +346,10 @@ const char *help_unknown_cmd(const char *cmd)
> >                       "which does not exist.\n"
> >                       "Continuing under the assumption that you meant 
'%s'\n",
> >                       cmd, assumed);
> > -             if (autocorrect > 0) {
> > +             if (autocorrect == INT_MAX) {
> > +                     if (strcmp("y", git_getpass("Continue? (y/n) ")))
> > +                             exit(1);
> 
> Funny. :)
> 
> It might be better to actually always write this prompt to the
> terminal, rather than popping up a gui $GIT_ASKPASS if the user has
> set that up.  Maybe something like Heiko Voigt's "mingw: make failures
> to unlike or move raise a question" (9229029, 2010-02-21 from
> 4msysgit.git):
> 
>         if (!isatty(STDIN_FILENO) || !isatty(STDERR_FILENO))
>                 exit(1);
>         fprintf(stderr, "Continue? (y/n) ");
>         if (!fgets(answer, sizeof(answer), stdin))
>                 exit(1)
>         if (*answer != 'y' && *answer != 'Y')
>                 exit(1);
> 
> > +             } else if (autocorrect > 0) {
> >                       fprintf(stderr, "in %0.1f seconds automatically...
\n",
> >                               (float)autocorrect/10.0);
> >                       poll(NULL, 0, autocorrect * 100);

That looks much nicer, I was hoping for such a suggestion.

--
David Barr.

```

## David Barr, 2010-09-06 07:27

Subject: [PATCH] help.autocorrect prompt
Message-ID: <1283758030-13345-1-git-send-email-david.barr@cordelta.com>
URL: https://gitlist.dev/e/1283758030-13345-1-git-send-email-david.barr%40cordelta.com
In-Reply-To: <20100906025949.GA23594@burratino>

```
Added a new configuration value for help.autocorrect.
A value of 'prompt' causes git to wait for confirmation
before executing the assumed command.

Signed-off-by: David Barr <david.barr@cordelta.com>
Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>
---
 help.c |   19 ++++++++++++++++---
 1 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/help.c b/help.c
index 7f4928e..2282885 100644
--- a/help.c
+++ b/help.c
@@ -267,8 +267,12 @@ static struct cmdnames aliases;
 
 static int git_unknown_cmd_config(const char *var, const char *value, void *cb)
 {
-	if (!strcmp(var, "help.autocorrect"))
-		autocorrect = git_config_int(var,value);
+	if (!strcmp(var, "help.autocorrect")) {
+		if (!strcmp(value, "prompt"))
+			autocorrect = INT_MAX;
+		else
+			autocorrect = git_config_int(var,value);
+	}
 	/* Also use aliases for command lookup */
 	if (!prefixcmp(var, "alias."))
 		add_cmdname(&aliases, var + 6, strlen(var + 6));
@@ -342,7 +346,16 @@ const char *help_unknown_cmd(const char *cmd)
 			"which does not exist.\n"
 			"Continuing under the assumption that you meant '%s'\n",
 			cmd, assumed);
-		if (autocorrect > 0) {
+		if (autocorrect == INT_MAX) {
+			char answer[3];
+			if (!isatty(STDIN_FILENO) || !isatty(STDERR_FILENO))
+				exit(1);
+			fprintf(stderr, "Continue? (y/n) ");
+			if (!fgets(answer, sizeof(answer), stdin))
+				exit(1);
+			if (*answer != 'y' && *answer != 'Y')
+				exit(1);
+		} else if (autocorrect > 0) {
 			fprintf(stderr, "in %0.1f seconds automatically...\n",
 				(float)autocorrect/10.0);
 			poll(NULL, 0, autocorrect * 100);
-- 
1.7.2.3.392.g02377.dirty

```
