git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH v3] help.c: configurable suggestions
@ 2020-11-18 14:42 Drew DeVault
  2020-11-18 17:16 ` Junio C Hamano
  0 siblings, 1 reply; 5+ messages in thread
From: Drew DeVault @ 2020-11-18 14:42 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.

Signed-off-by: Drew DeVault <sir@cmpwn.com>
---
Thanks for being patient with me, Junio - I didn't entirely grok what
you were getting at in your initial review.

 Documentation/config/help.txt | 14 +++++++-------
 help.c                        | 33 +++++++++++++++++++++++++++++----
 2 files changed, 36 insertions(+), 11 deletions(-)

diff --git a/Documentation/config/help.txt b/Documentation/config/help.txt
index 224bbf5a28..dea236268a 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 "immediate", the suggested command
+	is run immediately. If "never", 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..61a7c1ea17 100644
--- a/help.c
+++ b/help.c
@@ -472,12 +472,26 @@ 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 (!strcmp(var, "help.autocorrect")) {
+		if (!value)
+			return config_error_nonbool(var);
+		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;
+		}
+	}
 	/* Also use aliases for command lookup */
 	if (skip_prefix(var, "alias.", &p))
 		add_cmdname(&aliases, p, strlen(p));
@@ -525,6 +539,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);
@@ -594,7 +613,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 +725,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 related	[flat|nested] 5+ messages in thread

* Re: [PATCH v3] help.c: configurable suggestions
  2020-11-18 14:42 [PATCH v3] help.c: configurable suggestions Drew DeVault
@ 2020-11-18 17:16 ` Junio C Hamano
  2020-11-18 17:17   ` Drew DeVault
  0 siblings, 1 reply; 5+ messages in thread
From: Junio C Hamano @ 2020-11-18 17:16 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.

It is unclear from this description alone why this is needed.  The
seller of this change needs to emphasize how this is better than
setting the variable to "0" (do not autocorrect).  My guess is that
some users do not even need the suggestion of correct spelling when
they made a typo?

>  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 "immediate", the suggested command
> +	is run immediately. If "never", suggestions are not shown at all. The
> +	default value is zero.

Imagine existing users who set the variable to -1 long time ago
wonders what it is doing in their ~/.gitconfig file.  This paragraph
still needs to describe how a negative value is treated.

> diff --git a/help.c b/help.c
> index 919cbb9206..61a7c1ea17 100644
> --- a/help.c
> +++ b/help.c
> @@ -472,12 +472,26 @@ 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 (!strcmp(var, "help.autocorrect")) {
> +		if (!value)
> +			return config_error_nonbool(var);
> +		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;
> +		}
> +	}
>  	/* Also use aliases for command lookup */
>  	if (skip_prefix(var, "alias.", &p))
>  		add_cmdname(&aliases, p, strlen(p));
> @@ -525,6 +539,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);

OK, so when we encounter an unknown word that follows "git" on the
comand line, we immediately exit without doing the guesswork.  This
is needed because we are skipping not just the guessing and
suggesting, but showing this error message by exiting early here.

Makes sense.

> +	}
> +
>  	load_command_list("git-", &main_cmds, &other_cmds);
>  
>  	add_cmd_list(&main_cmds, &aliases);
> @@ -594,7 +613,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 +725,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);
> +	}

I am not sure how the change in this hunk is justifiable.

The auto correction is about guessing mistyped commands and
optionally going forward to execute it.  Some users (like me) who
set it to 0 currently may want to set it to "never", but I'd imagine
that not all of these users would want to lose the local branch vs
remote-tracking branch correction (certainly not me).  It might be
convenient for some folks if both of these can be squelched with a
single knob, but it must be possible to enable them independently.

This is a tangent, but it seems to me that help_unknown_ref() is way
under-used and lacks usefulness too much---probably these contribute
to each other.

It only gets used by "git merge $name" and only when the missing
$name branch exists as remote-tracking branches of some remotes,
i.e.  "you said $name, which does not exist, but you could have
typed $remote/$name to refer to the remote-tracking branch").

I would imagine that it would help users who type "git checkout
mext" to suggest "you may have typoed 'next'", for example, and "git
checkout -b my-topic next" to offer "you said 'next' that does not
exist; did you mean 'origin/next'?"  It might be a good exercise to
first make inventory of other possible places that would benefit,
which may suggest what other sources guess_refs() should grab its
candidates from (#leftoverbits).

> +
> +	suggested_refs = guess_refs(ref);
> +
>  	if (suggested_refs.nr > 0) {
>  		fprintf_ln(stderr,
>  			   Q_("\nDid you mean this?",

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

* Re: [PATCH v3] help.c: configurable suggestions
  2020-11-18 17:16 ` Junio C Hamano
@ 2020-11-18 17:17   ` Drew DeVault
  2020-11-18 17:46     ` Junio C Hamano
  2020-11-18 18:27     ` Ævar Arnfjörð Bjarmason
  0 siblings, 2 replies; 5+ messages in thread
From: Drew DeVault @ 2020-11-18 17:17 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, lanodan

On Wed Nov 18, 2020 at 12:16 PM EST, Junio C Hamano wrote:
> It is unclear from this description alone why this is needed. The
> seller of this change needs to emphasize how this is better than
> setting the variable to "0" (do not autocorrect). My guess is that
> some users do not even need the suggestion of correct spelling when
> they made a typo?

This is one reason. Another is that spell checking can be
computationally expensive, and take an annoyingly long time on low-end
devices.

> > @@ -706,10 +725,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);
> > +	}
>
> I am not sure how the change in this hunk is justifiable.

I think this was caught up in the move from a new config option to
changing the meaning of help.autocorrect.

> This is a tangent, but it seems to me that help_unknown_ref() is way
> under-used and lacks usefulness too much---probably these contribute
> to each other.

Given that this is under-utilized anyway, and no longer applicable to
the essential purpose of the patch, I'm just going to pull this change
out of the patch. If this function starts to enjoy broader usage within
git, we could revisit a config lever later on.

Thanks for the feedback, sending v4 shortly.

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

* Re: [PATCH v3] help.c: configurable suggestions
  2020-11-18 17:17   ` Drew DeVault
@ 2020-11-18 17:46     ` Junio C Hamano
  2020-11-18 18:27     ` Ævar Arnfjörð Bjarmason
  1 sibling, 0 replies; 5+ messages in thread
From: Junio C Hamano @ 2020-11-18 17:46 UTC (permalink / raw)
  To: Drew DeVault; +Cc: git, lanodan

"Drew DeVault" <sir@cmpwn.com> writes:

> On Wed Nov 18, 2020 at 12:16 PM EST, Junio C Hamano wrote:
>> It is unclear from this description alone why this is needed. The
>> seller of this change needs to emphasize how this is better than
>> setting the variable to "0" (do not autocorrect). My guess is that
>> some users do not even need the suggestion of correct spelling when
>> they made a typo?
>
> This is one reason. Another is that spell checking can be
> computationally expensive, and take an annoyingly long time on low-end
> devices.

Both are good reasons to describe in the log message, so that the
next reader of "git log" won't have to ask "why?"



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

* Re: [PATCH v3] help.c: configurable suggestions
  2020-11-18 17:17   ` Drew DeVault
  2020-11-18 17:46     ` Junio C Hamano
@ 2020-11-18 18:27     ` Ævar Arnfjörð Bjarmason
  1 sibling, 0 replies; 5+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2020-11-18 18:27 UTC (permalink / raw)
  To: Drew DeVault; +Cc: Junio C Hamano, git, lanodan


On Wed, Nov 18 2020, Drew DeVault wrote:

> On Wed Nov 18, 2020 at 12:16 PM EST, Junio C Hamano wrote:
>> It is unclear from this description alone why this is needed. The
>> seller of this change needs to emphasize how this is better than
>> setting the variable to "0" (do not autocorrect). My guess is that
>> some users do not even need the suggestion of correct spelling when
>> they made a typo?
>
> This is one reason. Another is that spell checking can be
> computationally expensive, and take an annoyingly long time on low-end
> devices.

As an aside it would be interesting if you're in a position to test it
whether it's the suggestion code itself that's so expensive, or e.g. the
stat()-ing on "git xyz" looking for "git-xyz" in $PATH. I if it's
FS-limited that there's easy optimization opportunities there.

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

end of thread, other threads:[~2020-11-18 18:28 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-18 14:42 [PATCH v3] help.c: configurable suggestions Drew DeVault
2020-11-18 17:16 ` Junio C Hamano
2020-11-18 17:17   ` Drew DeVault
2020-11-18 17:46     ` Junio C Hamano
2020-11-18 18:27     ` Ævar Arnfjörð Bjarmason

Code repositories for project(s) associated with this public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).