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

[PATCH] help: always suggest common-cmds if prefix of cmd

From
Erik Faye-Lund <kusmabite@gmail.com>
Date
Nov 24, 2010, 23:53 UTC
Message-ID
<1290642821-2984-1-git-send-email-kusmabite@gmail.com>
In-Reply-To
<AANLkTi=nxcODCvQ6hmaQe=q38e=bF7cRHWrRaFr+zen6@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>
---
Here's an updated version, with the issues Junio fixed.
I hope this is a bit clearer.
 Makefile |    2 ++
 help.c   |   26 ++++++++++++++++++++------
 2 files changed, 22 insertions(+), 6 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..dff5d6b 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,16 @@ 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 = 0;
+	while (n < main_cmds.cnt && !main_cmds.names[n]->len)
 		++n;
+	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
Previous: Erik Faye-LundNext: Junio C Hamano
Message 16 of 27 in “bug: unexpected output for "git st" + suggestion”
  1. Tarek ZiadéNov 23, 2010
  2. Nguyen Thai Ngoc DuyNov 23, 2010
  3. Tarek ZiadéNov 23, 2010
  4. Nguyen Thai Ngoc DuyNov 23, 2010
  5. Tarek ZiadéNov 23, 2010
  6. Nguyen Thai Ngoc DuyNov 23, 2010
  7. Andreas SchwabNov 23, 2010
  8. Sylvain RabotNov 23, 2010
  9. Erik Faye-LundNov 23, 2010
  10. Tarek ZiadéNov 23, 2010
  11. Erik Faye-LundNov 23, 2010
  12. Tarek ZiadéNov 23, 2010
  13. help: always suggest common-cmds if prefix of cmdErik Faye-Lund, Nov 23, 2010
  14. Junio C HamanoNov 24, 2010
  15. Erik Faye-LundNov 24, 2010
  16. help: always suggest common-cmds if prefix of cmdErik Faye-Lund, Nov 24, 2010
  17. Junio C HamanoNov 25, 2010
  18. Erik Faye-LundNov 25, 2010
  19. help: always suggest common-cmds if prefix of cmdErik Faye-Lund, Nov 26, 2010
  20. Junio C HamanoNov 27, 2010
  21. Erik Faye-LundNov 29, 2010
  22. Jonathan NiederNov 29, 2010
  23. Erik Faye-LundNov 29, 2010
  24. Junio C HamanoNov 29, 2010
  25. Erik Faye-LundDec 1, 2010
  26. help.autoCorrect prefix selection considered a bit dangerousÆvar Arnfjörð Bjarmason, Nov 19, 2018
  27. Junio C HamanoNov 20, 2018

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.