From: Erik Faye-Lund <kusmabite@gmail.com>
To: git@vger.kernel.org
Cc: gitster@pobox.com, ziade.tarek@gmail.com
Subject: [PATCH v3] help: always suggest common-cmds if prefix of cmd
Date: Fri, 26 Nov 2010 17:00:39 +0100 [thread overview]
Message-ID: <1290787239-4508-1-git-send-email-kusmabite@gmail.com> (raw)
In-Reply-To: <AANLkTinKDqykfuV5=oHav9PRehDtJZct_q=zm7p8PAeo@mail.gmail.com>
If someone runs "git st", the command "git status" is not suggested
because it's not one of the closest levenshtein-neighbour.
Reserve the distance of 0 for common commands where the entered command
is a prefixe, as these are often more likely to be what the user meant.
This way, "git status" is the first suggestion, while a list of possible
typos are still suggested as well.
Signed-off-by: Erik Faye-Lund <kusmabite@gmail.com>
---
Makefile | 2 ++
help.c | 27 ++++++++++++++++++++-------
2 files changed, 22 insertions(+), 7 deletions(-)
diff --git a/Makefile b/Makefile
index 1f1ce04..d6ba349 100644
--- a/Makefile
+++ b/Makefile
@@ -1611,6 +1611,8 @@ git$X: git.o $(BUILTIN_OBJS) $(GITLIBS)
$(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ git.o \
$(BUILTIN_OBJS) $(ALL_LDFLAGS) $(LIBS)
+help.o: common-cmds.h
+
builtin/help.o: common-cmds.h
builtin/help.s builtin/help.o: EXTRA_CPPFLAGS = \
'-DGIT_HTML_PATH="$(htmldir_SQ)"' \
diff --git a/help.c b/help.c
index 7f4928e..0d76a82 100644
--- a/help.c
+++ b/help.c
@@ -3,6 +3,7 @@
#include "exec_cmd.h"
#include "levenshtein.h"
#include "help.h"
+#include "common-cmds.h"
/* most GUI terminals set COLUMNS (although some don't export it) */
static int term_columns(void)
@@ -298,7 +299,7 @@ static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
}
/* An empirically derived magic number */
-#define SIMILAR_ENOUGH(x) ((x) < 6)
+#define SIMILAR_ENOUGH(x) ((x) < 7)
const char *help_unknown_cmd(const char *cmd)
{
@@ -320,9 +321,16 @@ const char *help_unknown_cmd(const char *cmd)
uniq(&main_cmds);
/* This reuses cmdname->len for similarity index */
- for (i = 0; i < main_cmds.cnt; ++i)
- main_cmds.names[i]->len =
+ for (i = 0; i < main_cmds.cnt; ++i) {
+ main_cmds.names[i]->len = 1 +
levenshtein(cmd, main_cmds.names[i]->name, 0, 2, 1, 4);
+ for (n = 0; n < ARRAY_SIZE(common_cmds); ++n) {
+ if (!strcmp(main_cmds.names[i]->name,
+ common_cmds[n].name) &&
+ !prefixcmp(main_cmds.names[i]->name, cmd))
+ main_cmds.names[i]->len = 0;
+ }
+ }
qsort(main_cmds.names, main_cmds.cnt,
sizeof(*main_cmds.names), levenshtein_compare);
@@ -330,10 +338,15 @@ 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 = main_cmds.names[0]->len;
- n = 1;
- while (n < main_cmds.cnt && best_similarity == main_cmds.names[n]->len)
- ++n;
+ for (n = 0; n < main_cmds.cnt && !main_cmds.names[n]->len; ++n)
+ ; /* nothing */
+ if (n < main_cmds.cnt) {
+ best_similarity = main_cmds.names[n++]->len;
+ while (n < main_cmds.cnt &&
+ best_similarity == main_cmds.names[n]->len)
+ ++n;
+ } else
+ best_similarity = 0;
if (autocorrect && n == 1 && SIMILAR_ENOUGH(best_similarity)) {
const char *assumed = main_cmds.names[0]->name;
main_cmds.names[0] = NULL;
--
1.7.3.2
next prev parent reply other threads:[~2010-11-26 16:01 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-11-23 12:23 bug: unexpected output for "git st" + suggestion Tarek Ziadé
2010-11-23 12:40 ` Nguyen Thai Ngoc Duy
2010-11-23 12:49 ` Tarek Ziadé
2010-11-23 13:08 ` Nguyen Thai Ngoc Duy
2010-11-23 13:18 ` Tarek Ziadé
2010-11-23 13:26 ` Nguyen Thai Ngoc Duy
2010-11-23 20:38 ` Andreas Schwab
2010-11-23 12:43 ` Sylvain Rabot
2010-11-23 13:22 ` Erik Faye-Lund
2010-11-23 13:47 ` Tarek Ziadé
2010-11-23 13:56 ` Erik Faye-Lund
2010-11-23 13:58 ` Tarek Ziadé
2010-11-23 19:11 ` [PATCH] help: always suggest common-cmds if prefix of cmd Erik Faye-Lund
2010-11-24 19:49 ` Junio C Hamano
2010-11-24 20:20 ` Erik Faye-Lund
2010-11-24 23:53 ` Erik Faye-Lund
2010-11-25 4:49 ` Junio C Hamano
2010-11-25 10:39 ` Erik Faye-Lund
2010-11-26 16:00 ` Erik Faye-Lund [this message]
2010-11-27 0:18 ` [PATCH v3] " Junio C Hamano
2010-11-29 11:20 ` Erik Faye-Lund
2010-11-29 16:40 ` Jonathan Nieder
2010-11-29 16:53 ` Erik Faye-Lund
2010-11-29 17:44 ` Junio C Hamano
2010-12-01 14:33 ` Erik Faye-Lund
2018-11-19 20:35 ` help.autoCorrect prefix selection considered a bit dangerous Ævar Arnfjörð Bjarmason
2018-11-20 3:23 ` 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=1290787239-4508-1-git-send-email-kusmabite@gmail.com \
--to=kusmabite@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=ziade.tarek@gmail.com \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.