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

[PATCH 4/4] allow recovery from command name typos

From
Tay Ray Chuan <rctay89@gmail.com>
Date
May 6, 2012, 06:55 UTC
Message-ID
<1336287330-7215-5-git-send-email-rctay89@gmail.com>
In-Reply-To
<1336287330-7215-4-git-send-email-rctay89@gmail.com>

If suggestions are available (based on Levenshtein distance) and if the terminal isatty(), present a prompt to the user to select one of the computed suggestions.

In the case where there is a single suggestion, present the prompt "[Y/n]", such that "", "y" and "Y" as input leads git to proceed executing the suggestion, while everything else (possibly "n") leads git to terminate.

In the case where there are multiple suggestions, number the suggestions 1 to n, and accept as input one of the numbers, while everything else (possibly "n") leads git to terminate. In this case there is no default; that is, an empty input leads git to terminate. A sample run:

  $ git sh --pretty=oneline
  git: 'sh' is not a git command. See 'git --help'.
  Did you mean one of these?
  1:	show
  2:	push
  [1/2/.../n] 1
Signed-off-by: Tay Ray Chuan <rctay89@gmail.com>
---
 help.c | 37 +++++++++++++++++++++++++++++++++++--
 1 file changed, 35 insertions(+), 2 deletions(-)
diff --git a/help.c b/help.c
index f296d95..4aa1d88 100644
--- a/help.c
+++ b/help.c
@@ -6,6 +6,7 @@
 #include "common-cmds.h"
 #include "string-list.h"
 #include "column.h"
+#include "compat/terminal.h"
 
 void add_cmdname(struct cmdnames *cmds, const char *name, int len)
 {
@@ -383,8 +384,40 @@ const char *help_unknown_cmd(const char *cmd)
 			      "\nDid you mean one of these?",
 			   n));
 
-		for (i = 0; i < n; i++)
-			fprintf(stderr, "\t%s\n", main_cmds.names[i]->name);
+		if (!isatty(2))
+			for (i = 0; i < n; i++)
+				fprintf(stderr, "\t%s\n", main_cmds.names[i]->name);
+		else if (n == 1) {
+			char *in;
+			const char *ret;
+			fprintf(stderr, "\t%s\n", main_cmds.names[0]->name);
+			in = git_terminal_prompt("[Y/n] ", 1);
+			switch (in[0]) {
+			case 'y': case 'Y': case 0:
+				ret = xstrdup(main_cmds.names[0]->name);
+				clean_cmdnames(&main_cmds);
+				return ret;
+			/* otherwise, don't do anything */
+			}
+		} else {
+			char *in;
+			const char *ret;
+			int opt;
+			for (i = 0; i < n; i++)
+				fprintf(stderr, "%d:\t%s\n", i + 1, main_cmds.names[i]->name);
+			in = git_terminal_prompt("[1/2/.../n] ", 1);
+			switch (in[0]) {
+				case 'n': case 'N': case 0:
+					;
+				default:
+					opt = atoi(in);
+					if (0 < opt && opt <= n) {
+						ret = xstrdup(main_cmds.names[opt - 1]->name);
+						clean_cmdnames(&main_cmds);
+						return ret;
+					}
+			}
+		}
 	}
 
 	exit(1);
-- 
1.7.10.1.611.g8a79d96
Previous: Tay Ray ChuanNext: Jeff King
Message 5 of 37 in “allow recovery from command name typos”
  1. 0/4 allow recovery from command name typosTay Ray Chuan, May 6, 2012
  2. 1/4 help.c::uniq: plug a leakTay Ray Chuan, May 6, 2012
  3. 2/4 help.c::exclude_cmds: plug a leakTay Ray Chuan, May 6, 2012
  4. 3/4 help.c: plug a leak when help.autocorrect is setTay Ray Chuan, May 6, 2012
  5. 4/4 allow recovery from command name typosTay Ray Chuan, May 6, 2012
  6. Jeff KingMay 6, 2012
  7. Tay Ray ChuanMay 6, 2012
  8. Thomas RastMay 7, 2012
  9. Tay Ray ChuanMay 7, 2012
  10. Junio C HamanoMay 7, 2012
  11. Tay Ray ChuanMay 9, 2012
  12. Junio C HamanoMay 9, 2012
  13. Jeff KingMay 6, 2012
  14. Tay Ray ChuanMay 6, 2012
  15. Jeff KingMay 7, 2012
  16. 0/4 allow recovery from command name typosTay Ray Chuan, Jul 25, 2012
  17. 1/4 help.c::uniq: plug a leakTay Ray Chuan, Jul 25, 2012
  18. 2/4 help.c::exclude_cmds: realloc() before copy, plug a leakTay Ray Chuan, Jul 25, 2012
  19. 3/4 help.c: plug leaks with(out) help.autocorrectTay Ray Chuan, Jul 25, 2012
  20. 4/4 allow recovery from command name typosTay Ray Chuan, Jul 25, 2012
  21. Junio C HamanoJul 25, 2012
  22. Tay Ray ChuanJul 26, 2012
  23. Jeff KingJul 26, 2012
  24. Junio C HamanoJul 26, 2012
  25. Jeff KingJul 26, 2012
  26. Junio C HamanoJul 26, 2012
  27. Junio C HamanoJul 25, 2012
  28. Junio C HamanoJul 25, 2012
  29. 0/2 allow recovery from command name typosTay Ray Chuan, Aug 5, 2012
  30. 1/2 add interface for /dev/tty interactionTay Ray Chuan, Aug 5, 2012
  31. 2/2 allow recovery from command name typosTay Ray Chuan, Aug 5, 2012
  32. Junio C HamanoAug 6, 2012
  33. Junio C HamanoAug 5, 2012
  34. Jeff KingAug 6, 2012
  35. Jeff KingAug 6, 2012
  36. Junio C HamanoAug 6, 2012
  37. Tay Ray ChuanMay 6, 2012

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.