git@vger.kernel.org list mirror (unofficial, one of many)
 help / color / mirror / code / Atom feed
* [PATCH v5] help.c: expand options for help.autocorrect
@ 2020-11-18 17:49 Drew DeVault
  0 siblings, 0 replies; 3+ messages in thread
From: Drew DeVault @ 2020-11-18 17:49 UTC (permalink / raw)
  To: Junio C Hamano, git; +Cc: Drew DeVault, lanodan

Some users would prefer to never suggest corrections at all. For
example, they may want the mistake over with sooner so that they can
issue the correct command, especially on low-end hardware which may take
a while to calculate suggestions.

This updates help.autocorrect to accept the strings "immediate" and
"never" for this purpose, the former assuming the prior behavior of
negative values for autocorrect, and the latter skipping auto-correction
entirely.

Signed-off-by: Drew DeVault <sir@cmpwn.com>
---
v4 -> v5: More detailed commit message.

 Documentation/config/help.txt | 16 +++++++++-------
 help.c                        | 25 ++++++++++++++++++++++---
 2 files changed, 31 insertions(+), 10 deletions(-)

diff --git a/Documentation/config/help.txt b/Documentation/config/help.txt
index 224bbf5a28..e07abd32d7 100644
--- a/Documentation/config/help.txt
+++ b/Documentation/config/help.txt
@@ -8,13 +8,15 @@ 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.
++
+Negative integers are interpreted as "immediately" for historical reasons.
 
 help.htmlPath::
 	Specify the path where the HTML documentation resides. File system paths
diff --git a/help.c b/help.c
index 919cbb9206..3c3bdec213 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'."),
-- 
2.29.2


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

* Re: [PATCH v5] help.c: expand options for help.autocorrect
  2020-11-24 16:37 Drew DeVault
@ 2020-11-24 21:43 ` Junio C Hamano
  0 siblings, 0 replies; 3+ messages in thread
From: Junio C Hamano @ 2020-11-24 21:43 UTC (permalink / raw)
  To: Drew DeVault; +Cc: git, lanodan

Drew DeVault <sir@cmpwn.com> writes:

> While help.autocorrect can be set to 0 to decline auto-execution of
> possibly mistyped commands, it still spends cycles to compute the
> suggestions, and it wastes screen real estate.
>
> Update help.autocorrect to accept the string "never" to just exit
> with error upon mistyped commands to help users who prefer to never
> see suggested corrections at all.
>
> While at it, introduce "immediate" as a more readable way to
> immediately execute the auto-corrected command, which can be done
> with negative value.
>
> Signed-off-by: Drew DeVault <sir@cmpwn.com>
> Signed-off-by: Junio C Hamano <gitster@pobox.com>
> ---
> v5 incorporates Junio's suggested commit message and test.

Hmph,...

> +	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.
> ++
> +Negative integers are interpreted as "immediately" for historical reasons.

I do not think we should place too much stress on things that are
there for "historical reasons"---they do not deserve their own
paragraph.  That is why the version I queued with tweak said:

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

> +test_expect_success 'autocorrect can be declined altogether' '
> +	git config help.autocorrect never &&
> +
> +	test_must_fail git lfg 2>actual &&
> +	if test_have_prereq C_LOCALE_OUTPUT
> +	then
> +		: cannot test with poisoned i18n
> +	else
> +		grep "is not a git command" actual &&
> +		test_line_count = 1 actual
> +	fi
> +'

This test for 'never' is good.  We'd probably want to also add a
test for immediate.

Taking them together, perhaps like the attached patch.

Thanks.


--- >8 ------ >8 ------ >8 ------ >8 ------ >8 ------ >8 ------
From 59c61bb206242f750a3b63d9b5e16f4e1cf343e7 Mon Sep 17 00:00:00 2001
From: Drew DeVault <sir@cmpwn.com>
Date: Tue, 24 Nov 2020 11:37:52 -0500
Subject: [PATCH] help.c: help.autocorrect=never means "do not even compute
 suggestions"

While help.autocorrect can be set to 0 to decline auto-execution of
possibly mistyped commands, it still spends cycles to compute the
suggestions, and it wastes screen real estate.

Update help.autocorrect to accept the string "never" to just exit
with error upon mistyped commands to help users who prefer to never
see suggested corrections at all.

While at it, introduce "immediate" as a more readable way to
immediately execute the auto-corrected command, which can be done
with negative value.

Signed-off-by: Drew DeVault <sir@cmpwn.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
Range-diff:
1:  4ea4744d46 ! 1:  59c61bb206 help.c: expand options for help.autocorrect
    @@ Metadata
     Author: Drew DeVault <sir@cmpwn.com>
     
      ## Commit message ##
    -    help.c: expand options for help.autocorrect
    +    help.c: help.autocorrect=never means "do not even compute suggestions"
     
         While help.autocorrect can be set to 0 to decline auto-execution of
         possibly mistyped commands, it still spends cycles to compute the
    @@ Documentation/config/help.txt: help.format::
     +	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
    ++	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.
    -++
    -+Negative integers are interpreted as "immediately" for historical reasons.
      
      help.htmlPath::
      	Specify the path where the HTML documentation resides. File system paths
    @@ help.c: const char *help_unknown_cmd(const char *cmd)
      				     "you meant '%s'."),
     
      ## t/t9003-help-autocorrect.sh ##
    -@@ t/t9003-help-autocorrect.sh: test_expect_success 'autocorrect running commands' '
    - 	test_cmp expect actual
    +@@ t/t9003-help-autocorrect.sh: test_expect_success 'autocorrect showing candidates' '
    + 	grep "^	distimdistim" actual
      '
      
    +-test_expect_success 'autocorrect running commands' '
    +-	git config help.autocorrect -1 &&
    ++for immediate in -1 immediate
    ++do
    ++	test_expect_success 'autocorrect running commands' '
    ++		git config help.autocorrect $immediate &&
    + 
    +-	git lfg >actual &&
    +-	echo "a single log entry" >expect &&
    +-	test_cmp expect actual &&
    ++		git lfg >actual &&
    ++		echo "a single log entry" >expect &&
    ++		test_cmp expect actual &&
    + 
    +-	git distimdist >actual &&
    +-	echo "distimdistim was called" >expect &&
    +-	test_cmp expect actual
    ++		git distimdist >actual &&
    ++		echo "distimdistim was called" >expect &&
    ++		test_cmp expect actual
    ++	'
    ++done
    ++
     +test_expect_success 'autocorrect can be declined altogether' '
     +	git config help.autocorrect never &&
     +
    @@ t/t9003-help-autocorrect.sh: test_expect_success 'autocorrect running commands'
     +		grep "is not a git command" actual &&
     +		test_line_count = 1 actual
     +	fi
    -+'
    -+
    + '
    + 
      test_done

 Documentation/config/help.txt | 15 ++++++++-------
 help.c                        | 25 ++++++++++++++++++++++---
 t/t9003-help-autocorrect.sh   | 32 ++++++++++++++++++++++++--------
 3 files changed, 54 insertions(+), 18 deletions(-)

diff --git a/Documentation/config/help.txt b/Documentation/config/help.txt
index 224bbf5a28..783a90a0f9 100644
--- a/Documentation/config/help.txt
+++ b/Documentation/config/help.txt
@@ -8,13 +8,14 @@ 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 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.
 
 help.htmlPath::
 	Specify the path where the HTML documentation resides. File system paths
diff --git a/help.c b/help.c
index 919cbb9206..3c3bdec213 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'."),
diff --git a/t/t9003-help-autocorrect.sh b/t/t9003-help-autocorrect.sh
index b1c7919c4a..a1b180338f 100755
--- a/t/t9003-help-autocorrect.sh
+++ b/t/t9003-help-autocorrect.sh
@@ -37,16 +37,32 @@ test_expect_success 'autocorrect showing candidates' '
 	grep "^	distimdistim" actual
 '
 
-test_expect_success 'autocorrect running commands' '
-	git config help.autocorrect -1 &&
+for immediate in -1 immediate
+do
+	test_expect_success 'autocorrect running commands' '
+		git config help.autocorrect $immediate &&
 
-	git lfg >actual &&
-	echo "a single log entry" >expect &&
-	test_cmp expect actual &&
+		git lfg >actual &&
+		echo "a single log entry" >expect &&
+		test_cmp expect actual &&
 
-	git distimdist >actual &&
-	echo "distimdistim was called" >expect &&
-	test_cmp expect actual
+		git distimdist >actual &&
+		echo "distimdistim was called" >expect &&
+		test_cmp expect actual
+	'
+done
+
+test_expect_success 'autocorrect can be declined altogether' '
+	git config help.autocorrect never &&
+
+	test_must_fail git lfg 2>actual &&
+	if test_have_prereq C_LOCALE_OUTPUT
+	then
+		: cannot test with poisoned i18n
+	else
+		grep "is not a git command" actual &&
+		test_line_count = 1 actual
+	fi
 '
 
 test_done
-- 
2.29.2-520-gf7cd35e8a7


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

* [PATCH v5] help.c: expand options for help.autocorrect
@ 2020-11-24 16:37 Drew DeVault
  2020-11-24 21:43 ` Junio C Hamano
  0 siblings, 1 reply; 3+ messages in thread
From: Drew DeVault @ 2020-11-24 16:37 UTC (permalink / raw)
  To: Junio C Hamano, git; +Cc: Drew DeVault, lanodan

While help.autocorrect can be set to 0 to decline auto-execution of
possibly mistyped commands, it still spends cycles to compute the
suggestions, and it wastes screen real estate.

Update help.autocorrect to accept the string "never" to just exit
with error upon mistyped commands to help users who prefer to never
see suggested corrections at all.

While at it, introduce "immediate" as a more readable way to
immediately execute the auto-corrected command, which can be done
with negative value.

Signed-off-by: Drew DeVault <sir@cmpwn.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
v5 incorporates Junio's suggested commit message and test.

 Documentation/config/help.txt | 16 +++++++++-------
 help.c                        | 25 ++++++++++++++++++++++---
 t/t9003-help-autocorrect.sh   | 13 +++++++++++++
 3 files changed, 44 insertions(+), 10 deletions(-)

diff --git a/Documentation/config/help.txt b/Documentation/config/help.txt
index 224bbf5a28..e07abd32d7 100644
--- a/Documentation/config/help.txt
+++ b/Documentation/config/help.txt
@@ -8,13 +8,15 @@ 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.
++
+Negative integers are interpreted as "immediately" for historical reasons.
 
 help.htmlPath::
 	Specify the path where the HTML documentation resides. File system paths
diff --git a/help.c b/help.c
index 919cbb9206..3c3bdec213 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'."),
diff --git a/t/t9003-help-autocorrect.sh b/t/t9003-help-autocorrect.sh
index b1c7919c4a..8f1035c3c2 100755
--- a/t/t9003-help-autocorrect.sh
+++ b/t/t9003-help-autocorrect.sh
@@ -49,4 +49,17 @@ test_expect_success 'autocorrect running commands' '
 	test_cmp expect actual
 '
 
+test_expect_success 'autocorrect can be declined altogether' '
+	git config help.autocorrect never &&
+
+	test_must_fail git lfg 2>actual &&
+	if test_have_prereq C_LOCALE_OUTPUT
+	then
+		: cannot test with poisoned i18n
+	else
+		grep "is not a git command" actual &&
+		test_line_count = 1 actual
+	fi
+'
+
 test_done
-- 
2.29.2


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

end of thread, other threads:[~2020-11-24 22:20 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-18 17:49 [PATCH v5] help.c: expand options for help.autocorrect Drew DeVault
2020-11-24 16:37 Drew DeVault
2020-11-24 21:43 ` Junio C Hamano

Code repositories for project(s) associated with this 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).