git@vger.kernel.org list mirror (unofficial, one of many)
 help / color / mirror / code / Atom feed
* [PATCH v2] help.c: configurable suggestions
@ 2020-11-17 15:25 Drew DeVault
  2020-11-17 20:04 ` Junio C Hamano
  2020-11-17 21:25 ` Junio C Hamano
  0 siblings, 2 replies; 4+ messages in thread
From: Drew DeVault @ 2020-11-17 15:25 UTC (permalink / raw)
  To: Junio C Hamano, git; +Cc: Drew DeVault, lanodan

This allows users to disable guessing the commands or options that they
meant to use.
---
 Documentation/config/help.txt | 14 +++++++-------
 help.c                        | 27 ++++++++++++++++++++++-----
 2 files changed, 29 insertions(+), 12 deletions(-)

diff --git a/Documentation/config/help.txt b/Documentation/config/help.txt
index 224bbf5a28..99d1e33750 100644
--- a/Documentation/config/help.txt
+++ b/Documentation/config/help.txt
@@ -8,13 +8,13 @@ help.format::
 	the default. 'web' and 'html' are the same.
 
 help.autoCorrect::
-	Automatically correct and execute mistyped commands after
-	waiting for the given number of deciseconds (0.1 sec). If more
-	than one command can be deduced from the entered text, nothing
-	will be executed.  If the value of this option is negative,
-	the corrected command will be executed immediately. If the
-	value is 0 - the command will be just shown but not executed.
-	This is the default.
+	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 -1, the suggested command is run
+	immediately. If -2, suggestions are not shown at all. The default value
+	is zero.
 
 help.htmlPath::
 	Specify the path where the HTML documentation resides. File system paths
diff --git a/help.c b/help.c
index 919cbb9206..2fd27cffb0 100644
--- a/help.c
+++ b/help.c
@@ -472,12 +472,18 @@ int is_in_cmdlist(struct cmdnames *c, const char *s)
 static int autocorrect;
 static struct cmdnames aliases;
 
+#define AUTOCORRECT_NEVER (-2)
+#define AUTOCORRECT_IMMEDIATELY (-1)
+
 static int git_unknown_cmd_config(const char *var, const char *value, void *cb)
 {
 	const char *p;
 
-	if (!strcmp(var, "help.autocorrect"))
-		autocorrect = git_config_int(var,value);
+	if (!strcasecmp(var, "help.autocorrect")) {
+		if (!value)
+			return config_error_nonbool(var);
+		autocorrect = git_config_int(var, value);
+	}
 	/* Also use aliases for command lookup */
 	if (skip_prefix(var, "alias.", &p))
 		add_cmdname(&aliases, p, strlen(p));
@@ -525,6 +531,11 @@ const char *help_unknown_cmd(const char *cmd)
 
 	read_early_config(git_unknown_cmd_config, NULL);
 
+	if (autocorrect == AUTOCORRECT_NEVER) {
+		fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd);
+		exit(1);
+	}
+
 	load_command_list("git-", &main_cmds, &other_cmds);
 
 	add_cmd_list(&main_cmds, &aliases);
@@ -586,7 +597,7 @@ const char *help_unknown_cmd(const char *cmd)
 		     n++)
 			; /* still counting */
 	}
-	if (autocorrect && n == 1 && SIMILAR_ENOUGH(best_similarity)) {
+	if (autocorrect != 0 && n == 1 && SIMILAR_ENOUGH(best_similarity)) {
 		const char *assumed = main_cmds.names[0]->name;
 		main_cmds.names[0] = NULL;
 		clean_cmdnames(&main_cmds);
@@ -594,7 +605,7 @@ const char *help_unknown_cmd(const char *cmd)
 			   _("WARNING: You called a Git command named '%s', "
 			     "which does not exist."),
 			   cmd);
-		if (autocorrect < 0)
+		if (autocorrect == AUTOCORRECT_IMMEDIATELY)
 			fprintf_ln(stderr,
 				   _("Continuing under the assumption that "
 				     "you meant '%s'."),
@@ -706,10 +717,16 @@ NORETURN void help_unknown_ref(const char *ref, const char *cmd,
 			       const char *error)
 {
 	int i;
-	struct string_list suggested_refs = guess_refs(ref);
+	struct string_list suggested_refs;
 
 	fprintf_ln(stderr, _("%s: %s - %s"), cmd, ref, error);
 
+	if (autocorrect == AUTOCORRECT_NEVER) {
+		exit(1);
+	}
+
+	suggested_refs = guess_refs(ref);
+
 	if (suggested_refs.nr > 0) {
 		fprintf_ln(stderr,
 			   Q_("\nDid you mean this?",
-- 
2.29.2


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] help.c: configurable suggestions
  2020-11-17 15:25 [PATCH v2] help.c: configurable suggestions Drew DeVault
@ 2020-11-17 20:04 ` Junio C Hamano
  2020-11-17 20:05   ` Drew DeVault
  2020-11-17 21:25 ` Junio C Hamano
  1 sibling, 1 reply; 4+ messages in thread
From: Junio C Hamano @ 2020-11-17 20:04 UTC (permalink / raw)
  To: Drew DeVault; +Cc: git, lanodan

Drew DeVault <sir@cmpwn.com> writes:

> This allows users to disable guessing the commands or options that they
> meant to use.
> ---

Missing sign-off.

>  Documentation/config/help.txt | 14 +++++++-------
>  help.c                        | 27 ++++++++++++++++++++++-----
>  2 files changed, 29 insertions(+), 12 deletions(-)
>
> diff --git a/Documentation/config/help.txt b/Documentation/config/help.txt
> index 224bbf5a28..99d1e33750 100644
> --- a/Documentation/config/help.txt
> +++ b/Documentation/config/help.txt
> @@ -8,13 +8,13 @@ help.format::
>  	the default. 'web' and 'html' are the same.
>  
>  help.autoCorrect::
> -	Automatically correct and execute mistyped commands after
> -	waiting for the given number of deciseconds (0.1 sec). If more
> -	than one command can be deduced from the entered text, nothing
> -	will be executed.  If the value of this option is negative,
> -	the corrected command will be executed immediately. If the
> -	value is 0 - the command will be just shown but not executed.
> -	This is the default.
> +	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 -1, the suggested command is run
> +	immediately. If -2, suggestions are not shown at all. The default value
> +	is zero.

This changes behaviour for those who have relied on our promise that
any negative value means immediate execution.  Now -2 means a totally
different thing.

I thought I already showed you how to make it a "number or keyword"
in my previous review comment to avoid such a needless behaviour
change---did our mails cross?

Thanks.

>  help.htmlPath::
>  	Specify the path where the HTML documentation resides. File system paths
> diff --git a/help.c b/help.c
> index 919cbb9206..2fd27cffb0 100644
> --- a/help.c
> +++ b/help.c
> @@ -472,12 +472,18 @@ int is_in_cmdlist(struct cmdnames *c, const char *s)
>  static int autocorrect;
>  static struct cmdnames aliases;
>  
> +#define AUTOCORRECT_NEVER (-2)
> +#define AUTOCORRECT_IMMEDIATELY (-1)
> +
>  static int git_unknown_cmd_config(const char *var, const char *value, void *cb)
>  {
>  	const char *p;
>  
> -	if (!strcmp(var, "help.autocorrect"))
> -		autocorrect = git_config_int(var,value);
> +	if (!strcasecmp(var, "help.autocorrect")) {
> +		if (!value)
> +			return config_error_nonbool(var);
> +		autocorrect = git_config_int(var, value);
> +	}
>  	/* Also use aliases for command lookup */
>  	if (skip_prefix(var, "alias.", &p))
>  		add_cmdname(&aliases, p, strlen(p));
> @@ -525,6 +531,11 @@ const char *help_unknown_cmd(const char *cmd)
>  
>  	read_early_config(git_unknown_cmd_config, NULL);
>  
> +	if (autocorrect == AUTOCORRECT_NEVER) {
> +		fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd);
> +		exit(1);
> +	}
> +
>  	load_command_list("git-", &main_cmds, &other_cmds);
>  
>  	add_cmd_list(&main_cmds, &aliases);
> @@ -586,7 +597,7 @@ const char *help_unknown_cmd(const char *cmd)
>  		     n++)
>  			; /* still counting */
>  	}
> -	if (autocorrect && n == 1 && SIMILAR_ENOUGH(best_similarity)) {
> +	if (autocorrect != 0 && n == 1 && SIMILAR_ENOUGH(best_similarity)) {

I see there is nothing gained by writing "!= 0" explicitly here.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] help.c: configurable suggestions
  2020-11-17 20:04 ` Junio C Hamano
@ 2020-11-17 20:05   ` Drew DeVault
  0 siblings, 0 replies; 4+ messages in thread
From: Drew DeVault @ 2020-11-17 20:05 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, lanodan

On Tue Nov 17, 2020 at 3:04 PM EST, Junio C Hamano wrote:
> Drew DeVault <sir@cmpwn.com> writes:
>
> > This allows users to disable guessing the commands or options that they
> > meant to use.
> > ---
>
> Missing sign-off.

Whoops!

> This changes behaviour for those who have relied on our promise that
> any negative value means immediate execution. Now -2 means a totally
> different thing.
>
> I thought I already showed you how to make it a "number or keyword"
> in my previous review comment to avoid such a needless behaviour
> change---did our mails cross?

I must not have understood the psuedocode you sent. I'll take another
look at it. Thanks for the feedback!

> > @@ -586,7 +597,7 @@ const char *help_unknown_cmd(const char *cmd)
> >  		     n++)
> >  			; /* still counting */
> >  	}
> > -	if (autocorrect && n == 1 && SIMILAR_ENOUGH(best_similarity)) {
> > +	if (autocorrect != 0 && n == 1 && SIMILAR_ENOUGH(best_similarity)) {
>
> I see there is nothing gained by writing "!= 0" explicitly here.

*shrug*

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] help.c: configurable suggestions
  2020-11-17 15:25 [PATCH v2] help.c: configurable suggestions Drew DeVault
  2020-11-17 20:04 ` Junio C Hamano
@ 2020-11-17 21:25 ` Junio C Hamano
  1 sibling, 0 replies; 4+ messages in thread
From: Junio C Hamano @ 2020-11-17 21:25 UTC (permalink / raw)
  To: Drew DeVault; +Cc: git, lanodan

Drew DeVault <sir@cmpwn.com> writes:

> -	if (!strcmp(var, "help.autocorrect"))
> -		autocorrect = git_config_int(var,value);
> +	if (!strcasecmp(var, "help.autocorrect")) {

This is an unwarranted change.  The first part and the last part of
a configuration variable name are downcased before the config
callback functions are called, so strcmp() is sufficient.

> +		if (!value)
> +			return config_error_nonbool(var);

This is to protect us against

	[help]
		autocorrect

(i.e. "true" expressed without "= true"), which is not strictly
needed when the only thing we do with value is git_config_int()
because it knows how to handle value==NULL case.  But it starts
to matter when parsing variables with more elaborate shape, like
"int or some known token".

And because we want to keep the promise "if you write negative
value, it means immediate" we made to our users, we can instead make
this variable "number or 'never'".  To do so, keep that "barf if
NULL" check above, and then add something like:

		if (!strcmp(value, "never"))
			autocorrect = AUTOCORRECT_NEVER;
		else {
			int v = git_config_int(var, value);
                        autocorrect = (v < 0) 
				? AUTOCORRECT_IMMEDIATELY : v;
		}

The configuration the end user has may say '-2', but that is
different from 'never', so instead of leaving a negative value as
is, like this code below does ...

> +		autocorrect = git_config_int(var, value);

... we normalize any negative value the user gave us to
AUTOCORRECT_IMMEDIATELY.  That way, we can keep configuration the
user has working the same way as before, while allowing a new value
'never' to have a new meaning.

We might want to further make the variable "number, 'never' or
'immediate'" for consistency, with an eye on the possibility that we
might eventually want to deprecate the "negative means immediate".

To do so, we could do:

		if (!strcmp(value, "never")) {
			autocorrect = AUTOCORRECT_NEVER;
		} else if (!strcmp(value, "immediate")) {
			autocorrect = AUTOCORRECT_IMMEDIATELY;
		} else {
			int v = git_config_int(var, value);
                        autocorrect = (v < 0) 
				? AUTOCORRECT_IMMEDIATELY : v;
		}

instead.


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-11-17 21:27 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-17 15:25 [PATCH v2] help.c: configurable suggestions Drew DeVault
2020-11-17 20:04 ` Junio C Hamano
2020-11-17 20:05   ` Drew DeVault
2020-11-17 21:25 ` Junio C Hamano

git@vger.kernel.org list mirror (unofficial, one of many)

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://public-inbox.org/git
	git clone --mirror http://ou63pmih66umazou.onion/git
	git clone --mirror http://czquwvybam4bgbro.onion/git
	git clone --mirror http://hjrcffqmbrq6wope.onion/git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 git git/ https://public-inbox.org/git \
		git@vger.kernel.org
	public-inbox-index git

Example config snippet for mirrors.
Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.version-control.git
	nntp://7fh6tueqddpjyxjmgtdiueylzoqt6pt7hec3pukyptlmohoowvhde4yd.onion/inbox.comp.version-control.git
	nntp://ie5yzdi7fg72h7s4sdcztq5evakq23rdt33mfyfcddc5u3ndnw24ogqd.onion/inbox.comp.version-control.git
	nntp://4uok3hntl7oi7b4uf4rtfwefqeexfzil2w6kgk2jn5z2f764irre7byd.onion/inbox.comp.version-control.git
	nntp://news.gmane.io/gmane.comp.version-control.git
 note: .onion URLs require Tor: https://www.torproject.org/

code repositories for project(s) associated with this inbox:

	https://80x24.org/mirrors/git.git

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git