git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] Add help.autocorrect to enable/disable autocorrecting

From
Alex Riesen <raa.lkml@gmail.com>
Date
Jul 22, 2008, 22:25 UTC
Message-ID
<20080722222510.GA4474@blimp.local>
In-Reply-To
<alpine.DEB.1.00.0807222242160.8986@racer>

It is off by default, to avoid scaring people unless they asked to. ---

Johannes Schindelin, Tue, Jul 22, 2008 23:44:50 +0200:
Show 18 quoted lines
> On Tue, 22 Jul 2008, Alex Riesen wrote:
> 
> > @@ -704,9 +707,10 @@ const char *help_unknown_cmd(const char *cmd)
> >  
> >  	if (!main_cmds.cnt)
> >  		die ("Uh oh.  Your system reports no Git commands at all.");
> > +	git_config(git_help_config, NULL);
> >  	best_similarity = similarity(main_cmds.names[0]->name);
> > -	if (main_cmds.cnt < 2 || best_similarity <
> > -			similarity(main_cmds.names[1]->name)) {
> > +	if (autocorrect && (main_cmds.cnt < 2 ||
> > +		best_similarity < similarity(main_cmds.names[1]->name))) {
> >  		if (!*cwd)
> >  			exit(1);
> >  		if (chdir(cwd))
> 
> This "if" already checks if there is only one candidate.  So you should 
> just add an inner "if (autocorrect) ... else single = 1;" or some such.
Oh right, stupid me.
> However, I think that the intention of this patch is too much DWIMery, 
> which might be good for me (just like my "git add remote" patch), but not 
> for the general audience.

Mustn't be good for all (for the "general audience" it is even common practice to forget to thank. It may be even a sign of bad manners for it). It is good for me though. And thanks for sharing.

Moved git_config before the calls where current directory is changed: so that it has the same filesystem context as in normal case. Less surprises.

 help.c |   19 +++++++++++++------
 1 files changed, 13 insertions(+), 6 deletions(-)
diff --git a/help.c b/help.c
index 480befe..8b25a55 100644
--- a/help.c
+++ b/help.c
@@ -28,6 +28,7 @@ enum help_format {
 	HELP_FORMAT_WEB,
 };
 
+static int autocorrect;
 static int show_all = 0;
 static enum help_format help_format = HELP_FORMAT_MAN;
 static struct option builtin_help_options[] = {
@@ -269,6 +270,8 @@ static int git_help_config(const char *var, const char *value, void *cb)
 	}
 	if (!prefixcmp(var, "man."))
 		return add_man_viewer_info(var, value);
+	if (!strcmp(var, "help.autocorrect"))
+		autocorrect = git_config_bool(var,value);
 
 	return git_default_config(var, value, cb);
 }
@@ -683,7 +686,7 @@ static int levenshtein_compare(const void *p1, const void *p2)
 
 const char *help_unknown_cmd(const char *cmd)
 {
-	int i, best_similarity = 0;
+	int i, best_similarity = 0, n;
 	char cwd[PATH_MAX];
 
 	if (!getcwd(cwd, sizeof(cwd))) {
@@ -691,6 +694,7 @@ const char *help_unknown_cmd(const char *cmd)
 		cwd[0] = '\0';
 	}
 
+	git_config(git_help_config, NULL);
 	load_command_list();
 	ALLOC_GROW(main_cmds.names, main_cmds.cnt + other_cmds.cnt,
 			main_cmds.alloc);
@@ -705,8 +709,11 @@ const char *help_unknown_cmd(const char *cmd)
 	if (!main_cmds.cnt)
 		die ("Uh oh.  Your system reports no Git commands at all.");
 	best_similarity = similarity(main_cmds.names[0]->name);
-	if (main_cmds.cnt < 2 || best_similarity <
-			similarity(main_cmds.names[1]->name)) {
+	n = 1;
+	while (n < main_cmds.cnt &&
+		best_similarity == similarity(main_cmds.names[n]->name))
+		++n;
+	if (autocorrect && n == 1) {
 		if (!*cwd)
 			exit(1);
 		if (chdir(cwd))
@@ -721,10 +728,10 @@ const char *help_unknown_cmd(const char *cmd)
 	fprintf(stderr, "git: '%s' is not a git-command. See 'git --help'.\n", cmd);
 
 	if (best_similarity < 6) {
-		fprintf(stderr, "\nDid you mean one of these?\n");
+		fprintf(stderr, "\nDid you mean %s?\n",
+			n < 2 ? "this": "one of these");
 
-		for (i = 0; i < main_cmds.cnt && best_similarity ==
-				similarity(main_cmds.names[i]->name); i++)
+		for (i = 0; i < n; i++)
 			fprintf(stderr, "\t%s\n", main_cmds.names[i]->name);
 	}
 
-- 
1.6.0.rc0.48.ga184
Previous: Johannes SchindelinNext: Johannes Schindelin
Message 10 of 20 in “git wrapper: DWIM mistyped commands”
  1. git wrapper: DWIM mistyped commandsJohannes Schindelin, Jul 22, 2008
  2. [SCNR] Re: [FYI PATCH] git wrapper: DWIM mistyped commandsPierre Habouzit, Jul 22, 2008
  3. Johannes SchindelinJul 22, 2008
  4. Pierre HabouzitJul 22, 2008
  5. Alex RiesenJul 22, 2008
  6. Add help.autocorrect to enable/disable autocorrectingAlex Riesen, Jul 22, 2008
  7. Johannes SchindelinJul 22, 2008
  8. Add help.autocorrect to enable/disable autocorrectingAlex Riesen, Jul 22, 2008
  9. Johannes SchindelinJul 22, 2008
  10. Add help.autocorrect to enable/disable autocorrectingAlex Riesen, Jul 22, 2008
  11. Johannes SchindelinJul 23, 2008
  12. Alex RiesenJul 23, 2008
  13. Johannes SchindelinJul 23, 2008
  14. Johannes SchindelinJul 23, 2008
  15. Junio C HamanoJul 22, 2008
  16. Wait help.autocorrect deciseconds before running corrected commandAlex Riesen, Jul 23, 2008
  17. Johannes SchindelinJul 23, 2008
  18. Alex RiesenJul 23, 2008
  19. Junio C HamanoJul 22, 2008
  20. Sverre RabbelierJul 22, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.