threads / patch / 54655

patchhelp.c: add advice.correctTypos option

Subject: [PATCH] help.c: add advice.correctTypos option

## tl;dr

2 messages between Nov 16, 2020 and Nov 16, 2020. Diffs are folded; open one to read it.

replies: 1people: 2as markdown or json

Drew DeVault· Nov 16, 2020, 18:55 UTC · lore

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

- Is advice.* the right namespace?
- How should this interact with help.autocorrect?
 Documentation/config/advice.txt |  2 ++
 help.c                          | 19 ++++++++++++++++---
 2 files changed, 18 insertions(+), 3 deletions(-)
Show changes to 2 files +18 −3

Documentation/config/advice.txt, help.c

diff --git a/Documentation/config/advice.txt b/Documentation/config/advice.txt
index acbd0c09aa..135d1345af 100644
--- a/Documentation/config/advice.txt
+++ b/Documentation/config/advice.txt
@@ -119,4 +119,6 @@ advice.*::
 	addEmptyPathspec::
 		Advice shown if a user runs the add command without providing
 		the pathspec parameter.
+	correctTypos::
+		Detect typos and suggest corrections.
 --
diff --git a/help.c b/help.c
index 919cbb9206..c35c4c99da 100644
--- a/help.c
+++ b/help.c
@@ -515,10 +515,16 @@ static const char bad_interpreter_advice[] =
 
 const char *help_unknown_cmd(const char *cmd)
 {
-	int i, n, best_similarity = 0;
+	int i, n, best_similarity = 0, enable = 1;
 	struct cmdnames main_cmds, other_cmds;
 	struct cmdname_help *common_cmds;
 
+	git_config_get_bool("advice.correctTypos", &enable);
+	if (!enable) {
+		fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd);
+		exit(1);
+	}
+
 	memset(&main_cmds, 0, sizeof(main_cmds));
 	memset(&other_cmds, 0, sizeof(other_cmds));
 	memset(&aliases, 0, sizeof(aliases));
@@ -705,11 +711,18 @@ static struct string_list guess_refs(const char *ref)
 NORETURN void help_unknown_ref(const char *ref, const char *cmd,
 			       const char *error)
 {
-	int i;
-	struct string_list suggested_refs = guess_refs(ref);
+	int i, enable = 1;
+	struct string_list suggested_refs;
 
 	fprintf_ln(stderr, _("%s: %s - %s"), cmd, ref, error);
 
+	git_config_get_bool("advice.correctTypos", &enable);
+	if (!enable) {
+		exit(1);
+	}
+
+	suggested_refs = guess_refs(ref);
+
 	if (suggested_refs.nr > 0) {
 		fprintf_ln(stderr,
 			   Q_("\nDid you mean this?",
-- 
2.29.2
Junio C Hamano· Nov 16, 2020, 23:41 UTC · re: Drew DeVault · lore

Re: [PATCH] help.c: add advice.correctTypos option

Drew DeVault <sir@cmpwn.com> writes:
Show 7 quoted lines
> This allows users to disable guessing the commands or options that they
> meant to use.
> ---
> Questions:
>
> - Is advice.* the right namespace?
> - How should this interact with help.autocorrect?

If you are declining help.autocorrect altogether, do you need to still invent a new and separate configuration variable? Isn't a new value (e.g. 'never') given to help.autocorrect sufficient?

Something along this line (not even compile tested though)?
 help.c | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)
Show changes to diff +19 −3
diff --git c/help.c w/help.c
index 4fb93d5560..06f86152a6 100644
--- c/help.c
+++ w/help.c
@@ -469,6 +469,8 @@ int is_in_cmdlist(struct cmdnames *c, const char *s)
 	return 0;
 }
 
+#define AUTOCORRECT_NEVER (-2)
+#define AUTOCORRECT_IMMEDIATELY (-1)
 static int autocorrect;
 static struct cmdnames aliases;
 
@@ -476,8 +478,19 @@ 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 {
+			int v = git_config_int(var,value);
+			if (v < 0)
+				autocorrect = AUTOCORRECT_IMMEDIATELY;
+			else
+				autocorrect = v;
+		}
+	}
 	/* Also use aliases for command lookup */
 	if (skip_prefix(var, "alias.", &p))
 		add_cmdname(&aliases, p, strlen(p));
@@ -519,6 +532,9 @@ const char *help_unknown_cmd(const char *cmd)
 	struct cmdnames main_cmds, other_cmds;
 	struct cmdname_help *common_cmds;
 
+	if (autocorrect == AUTOCORRECT_NEVER)
+		exit(1);
+
 	memset(&main_cmds, 0, sizeof(main_cmds));
 	memset(&other_cmds, 0, sizeof(other_cmds));
 	memset(&aliases, 0, sizeof(aliases));
@@ -594,7 +610,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'."),

← back to recent threads