git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alex Riesen <raa.lkml@gmail.com>
To: Johannes Schindelin <Johannes.Schindelin@gmx.de>
Cc: git@vger.kernel.org
Subject: [PATCH] Add help.autocorrect to enable/disable autocorrecting
Date: Wed, 23 Jul 2008 00:25:10 +0200	[thread overview]
Message-ID: <20080722222510.GA4474@blimp.local> (raw)
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:
> 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

  reply	other threads:[~2008-07-23 16:24 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-07-22 20:01 [FYI PATCH] git wrapper: DWIM mistyped commands Johannes Schindelin
2008-07-22 20:16 ` [SCNR] " Pierre Habouzit
2008-07-22 20:19   ` Johannes Schindelin
2008-07-22 20:34     ` Pierre Habouzit
2008-07-22 20:37 ` Alex Riesen
2008-07-22 21:03   ` [PATCH] Add help.autocorrect to enable/disable autocorrecting Alex Riesen
2008-07-22 21:08     ` Johannes Schindelin
2008-07-22 21:26       ` Alex Riesen
2008-07-22 21:44         ` Johannes Schindelin
2008-07-22 22:25           ` Alex Riesen [this message]
2008-07-23 16:44             ` Johannes Schindelin
2008-07-23 18:44               ` Alex Riesen
2008-07-23 19:00                 ` Johannes Schindelin
2008-07-23 19:04                   ` Johannes Schindelin
2008-07-22 23:08           ` Junio C Hamano
2008-07-23 16:41             ` [PATCH] Wait help.autocorrect deciseconds before running corrected command Alex Riesen
2008-07-23 16:57               ` Johannes Schindelin
2008-07-23 18:45                 ` Alex Riesen
2008-07-22 23:05 ` [FYI PATCH] git wrapper: DWIM mistyped commands Junio C Hamano
2008-07-22 23:10   ` Sverre Rabbelier
  -- strict thread matches above, loose matches on Subject: below --
2008-08-28 17:15 [PATCH] Remove calculation of the longest command name from where it is not used Alex Riesen, Alex Riesen
2008-08-28 21:27 ` [PATCH updated] git wrapper: DWIM mistyped commands Alex Riesen
2008-08-28 21:28   ` [PATCH] Add help.autocorrect to enable/disable autocorrecting Alex Riesen
2008-08-29 10:11     ` Andreas Ericsson
2008-09-08  6:50     ` Junio C Hamano
2008-08-30 15:36   ` [PATCH updated] git wrapper: DWIM mistyped commands Junio C Hamano
2008-08-30 16:44     ` Alex Riesen
2008-08-30 17:13       ` [PATCH] Reuse cmdname->len to store pre-calculated similarity indexes Alex Riesen
2008-08-30 17:26         ` Junio C Hamano
2008-08-31 13:50           ` [PATCH] git wrapper: DWIM mistyped commands Alex Riesen
2008-08-31 13:54             ` [PATCH] Add help.autocorrect to enable/disable autocorrecting Alex Riesen
2008-08-31 14:49               ` Matthieu Moy
2008-08-31 16:33                 ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20080722222510.GA4474@blimp.local \
    --to=raa.lkml@gmail.com \
    --cc=Johannes.Schindelin@gmx.de \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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 NNTP newsgroup(s).