threads / patch / 15476

patchgit wrapper: also uses aliases to suggest mistyped commands

Subject: [PATCH] git wrapper: also uses aliases to suggest mistyped commands

## tl;dr

3 messages between Sep 10, 2008 and Sep 11, 2008. Diffs are folded; open one to read it.

replies: 2people: 2as markdown or json

Pieter de Bie· Sep 10, 2008, 15:54 UTC · lore
Signed-off-by: Pieter de Bie <pdebie@ai.rug.nl>
---
 help.c |   15 +++++++++++++++
 1 files changed, 15 insertions(+), 0 deletions(-)
Show changes to help.c +15 −0
diff --git a/help.c b/help.c
index 300cd38..24904d4 100644
--- a/help.c
+++ b/help.c
@@ -262,11 +262,15 @@ int is_in_cmdlist(struct cmdnames *c, const char *s)
 }
 
 static int autocorrect;
+static struct cmdnames aliases;
 
 static int git_unknown_cmd_config(const char *var, const char *value, void *cb)
 {
 	if (!strcmp(var, "help.autocorrect"))
 		autocorrect = git_config_int(var,value);
+	/* Also use aliases for command lookup */
+	if (!prefixcmp(var, "alias."))
+		add_cmdname(&aliases, var + 6, strlen(var + 6));
 
 	return git_default_config(var, value, cb);
 }
@@ -280,6 +284,15 @@ static int levenshtein_compare(const void *p1, const void *p2)
 	return l1 != l2 ? l1 - l2 : strcmp(s1, s2);
 }
 
+static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
+{
+	int i;
+	ALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc);
+
+	for (i = 0; i < old->cnt; i++)
+		cmds->names[cmds->cnt++] = old->names[i];
+}
+
 const char *help_unknown_cmd(const char *cmd)
 {
 	int i, n, best_similarity = 0;
@@ -287,11 +300,13 @@ const char *help_unknown_cmd(const char *cmd)
 
 	memset(&main_cmds, 0, sizeof(main_cmds));
 	memset(&other_cmds, 0, sizeof(main_cmds));
+	memset(&aliases, 0, sizeof(aliases));
 
 	git_config(git_unknown_cmd_config, NULL);
 
 	load_command_list("git-", &main_cmds, &other_cmds);
 
+	add_cmd_list(&main_cmds, &aliases);
 	ALLOC_GROW(main_cmds.names, main_cmds.cnt + other_cmds.cnt,
 		   main_cmds.alloc);
 	memcpy(main_cmds.names + main_cmds.cnt, other_cmds.names,
-- 
1.6.0.1.346.g880d9.dirty
Junio C Hamano· Sep 10, 2008, 21:44 UTC · re: Pieter de Bie · lore

Re: [PATCH] git wrapper: also uses aliases to suggest mistyped commands

Pieter de Bie <pdebie@ai.rug.nl> writes:
Show 30 quoted lines
> @@ -280,6 +284,15 @@ static int levenshtein_compare(const void *p1, const void *p2)
>  	return l1 != l2 ? l1 - l2 : strcmp(s1, s2);
>  }
>  
> +static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
> +{
> +	int i;
> +	ALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc);
> +
> +	for (i = 0; i < old->cnt; i++)
> +		cmds->names[cmds->cnt++] = old->names[i];
> +}
> +
>  const char *help_unknown_cmd(const char *cmd)
>  {
>  	int i, n, best_similarity = 0;
> @@ -287,11 +300,13 @@ const char *help_unknown_cmd(const char *cmd)
>  
>  	memset(&main_cmds, 0, sizeof(main_cmds));
>  	memset(&other_cmds, 0, sizeof(main_cmds));
> +	memset(&aliases, 0, sizeof(aliases));
>  
>  	git_config(git_unknown_cmd_config, NULL);
>  
>  	load_command_list("git-", &main_cmds, &other_cmds);
>  
> +	add_cmd_list(&main_cmds, &aliases);
>  	ALLOC_GROW(main_cmds.names, main_cmds.cnt + other_cmds.cnt,
>  		   main_cmds.alloc);
>  	memcpy(main_cmds.names + main_cmds.cnt, other_cmds.names,

I think your add_cmd_list() to smash two lists into one is a good abstraction to use here, but the existing code that can be seen at the tail end of the context already does that between main and other command list in a slightly different way.

Aliases should not hide the commands available elsewhere, and the actual execution codepath around ll.480-490 in git.c avoids getting fooled by misconfigured aliases, but you do not protect yourself from that kind of misconfiguration in this patch. You can have both "git-foo" command on your private $PATH and alias.foo in your configuration, and they will have the same levenshtein score. I suspect this will cause the same "foo" suggested twice when the user types "git fo" from the command line.

Here is a suggested fix-up on top of your patch to address these issues.
---
 help.c |   13 +++++++------
 1 files changed, 7 insertions(+), 6 deletions(-)
Show changes to diff +7 −6
diff --git i/help.c w/help.c
index 595342f..fd87bb5 100644
--- i/help.c
+++ w/help.c
@@ -291,6 +291,9 @@ static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
 
 	for (i = 0; i < old->cnt; i++)
 		cmds->names[cmds->cnt++] = old->names[i];
+	free(old->names);
+	old->cnt = 0;
+	old->names = NULL;
 }
 
 const char *help_unknown_cmd(const char *cmd)
@@ -307,12 +310,10 @@ const char *help_unknown_cmd(const char *cmd)
 	load_command_list("git-", &main_cmds, &other_cmds);
 
 	add_cmd_list(&main_cmds, &aliases);
-	ALLOC_GROW(main_cmds.names, main_cmds.cnt + other_cmds.cnt,
-		   main_cmds.alloc);
-	memcpy(main_cmds.names + main_cmds.cnt, other_cmds.names,
-	       other_cmds.cnt * sizeof(other_cmds.names[0]));
-	main_cmds.cnt += other_cmds.cnt;
-	free(other_cmds.names);
+	add_cmd_list(&main_cmds, &other_cmds);
+	qsort(main_cmds.names, main_cmds.cnt,
+	      sizeof(main_cmds.names), cmdname_compare);
+	uniq(&main_cmds);
 
 	/* This reuses cmdname->len for similarity index */
 	for (i = 0; i < main_cmds.cnt; ++i)
Pieter de Bie· Sep 11, 2008, 10:37 UTC · re: Junio C Hamano · lore

Re: [PATCH] git wrapper: also uses aliases to suggest mistyped commands

On 10 sep 2008, at 23:44, Junio C Hamano wrote:
Show 11 quoted lines
> Aliases should not hide the commands available elsewhere, and the  
> actual
> execution codepath around ll.480-490 in git.c avoids getting fooled by
> misconfigured aliases, but you do not protect yourself from that  
> kind of
> misconfiguration in this patch.  You can have both "git-foo" command  
> on
> your private $PATH and alias.foo in your configuration, and they  
> will have
> the same levenshtein score.  I suspect this will cause the same "foo"
> suggested twice when the user types "git fo" from the command line.
Yes, I didn't think of that.
> Here is a suggested fix-up on top of your patch to address these  
> issues.
Looks good. Do you want me to resend the patch or will you squash it?
- Pieter

← back to recent threads