{"thread":{"id":"15476","subject":"[PATCH] git wrapper: also uses aliases to suggest mistyped commands","startedAt":"2008-09-10T15:54:28Z","lastAt":"2008-09-11T10:37:35Z","messageCount":3,"participants":["Pieter de Bie","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"90367","messageId":"1221062068-5660-1-git-send-email-pdebie@ai.rug.nl","threadId":"15476","inReplyTo":null,"subject":"[PATCH] git wrapper: also uses aliases to suggest mistyped commands","fromName":"Pieter de Bie","fromEmail":"pdebie@ai.rug.nl","sentAt":"2008-09-10T15:54:28Z","receivedAt":"2008-09-10T15:54:28Z","isPatch":true,"sender":{"key":"pdebie@ai.rug.nl","avatar":null},"body":"\nSigned-off-by: Pieter de Bie <pdebie@ai.rug.nl>\n---\n help.c |   15 +++++++++++++++\n 1 files changed, 15 insertions(+), 0 deletions(-)\n\ndiff --git a/help.c b/help.c\nindex 300cd38..24904d4 100644\n--- a/help.c\n+++ b/help.c\n@@ -262,11 +262,15 @@ int is_in_cmdlist(struct cmdnames *c, const char *s)\n }\n \n static int autocorrect;\n+static struct cmdnames aliases;\n \n static int git_unknown_cmd_config(const char *var, const char *value, void *cb)\n {\n \tif (!strcmp(var, \"help.autocorrect\"))\n \t\tautocorrect = git_config_int(var,value);\n+\t/* Also use aliases for command lookup */\n+\tif (!prefixcmp(var, \"alias.\"))\n+\t\tadd_cmdname(&aliases, var + 6, strlen(var + 6));\n \n \treturn git_default_config(var, value, cb);\n }\n@@ -280,6 +284,15 @@ static int levenshtein_compare(const void *p1, const void *p2)\n \treturn l1 != l2 ? l1 - l2 : strcmp(s1, s2);\n }\n \n+static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)\n+{\n+\tint i;\n+\tALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc);\n+\n+\tfor (i = 0; i < old->cnt; i++)\n+\t\tcmds->names[cmds->cnt++] = old->names[i];\n+}\n+\n const char *help_unknown_cmd(const char *cmd)\n {\n \tint i, n, best_similarity = 0;\n@@ -287,11 +300,13 @@ const char *help_unknown_cmd(const char *cmd)\n \n \tmemset(&main_cmds, 0, sizeof(main_cmds));\n \tmemset(&other_cmds, 0, sizeof(main_cmds));\n+\tmemset(&aliases, 0, sizeof(aliases));\n \n \tgit_config(git_unknown_cmd_config, NULL);\n \n \tload_command_list(\"git-\", &main_cmds, &other_cmds);\n \n+\tadd_cmd_list(&main_cmds, &aliases);\n \tALLOC_GROW(main_cmds.names, main_cmds.cnt + other_cmds.cnt,\n \t\t   main_cmds.alloc);\n \tmemcpy(main_cmds.names + main_cmds.cnt, other_cmds.names,\n-- \n1.6.0.1.346.g880d9.dirty\n"},{"id":"90388","messageId":"7vtzcnvfq4.fsf@gitster.siamese.dyndns.org","threadId":"15476","inReplyTo":"1221062068-5660-1-git-send-email-pdebie@ai.rug.nl","subject":"Re: [PATCH] git wrapper: also uses aliases to suggest mistyped commands","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2008-09-10T21:44:03Z","receivedAt":"2008-09-10T21:44:03Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Pieter de Bie <pdebie@ai.rug.nl> writes:\n\n> @@ -280,6 +284,15 @@ static int levenshtein_compare(const void *p1, const void *p2)\n>  \treturn l1 != l2 ? l1 - l2 : strcmp(s1, s2);\n>  }\n>  \n> +static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)\n> +{\n> +\tint i;\n> +\tALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc);\n> +\n> +\tfor (i = 0; i < old->cnt; i++)\n> +\t\tcmds->names[cmds->cnt++] = old->names[i];\n> +}\n> +\n>  const char *help_unknown_cmd(const char *cmd)\n>  {\n>  \tint i, n, best_similarity = 0;\n> @@ -287,11 +300,13 @@ const char *help_unknown_cmd(const char *cmd)\n>  \n>  \tmemset(&main_cmds, 0, sizeof(main_cmds));\n>  \tmemset(&other_cmds, 0, sizeof(main_cmds));\n> +\tmemset(&aliases, 0, sizeof(aliases));\n>  \n>  \tgit_config(git_unknown_cmd_config, NULL);\n>  \n>  \tload_command_list(\"git-\", &main_cmds, &other_cmds);\n>  \n> +\tadd_cmd_list(&main_cmds, &aliases);\n>  \tALLOC_GROW(main_cmds.names, main_cmds.cnt + other_cmds.cnt,\n>  \t\t   main_cmds.alloc);\n>  \tmemcpy(main_cmds.names + main_cmds.cnt, other_cmds.names,\n\nI think your add_cmd_list() to smash two lists into one is a good\nabstraction to use here, but the existing code that can be seen at the\ntail end of the context already does that between main and other command\nlist in a slightly different way.\n\nAliases should not hide the commands available elsewhere, and the actual\nexecution codepath around ll.480-490 in git.c avoids getting fooled by\nmisconfigured aliases, but you do not protect yourself from that kind of\nmisconfiguration in this patch.  You can have both \"git-foo\" command on\nyour private $PATH and alias.foo in your configuration, and they will have\nthe same levenshtein score.  I suspect this will cause the same \"foo\"\nsuggested twice when the user types \"git fo\" from the command line.\n\nHere is a suggested fix-up on top of your patch to address these issues.\n\n---\n help.c |   13 +++++++------\n 1 files changed, 7 insertions(+), 6 deletions(-)\n\ndiff --git i/help.c w/help.c\nindex 595342f..fd87bb5 100644\n--- i/help.c\n+++ w/help.c\n@@ -291,6 +291,9 @@ static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)\n \n \tfor (i = 0; i < old->cnt; i++)\n \t\tcmds->names[cmds->cnt++] = old->names[i];\n+\tfree(old->names);\n+\told->cnt = 0;\n+\told->names = NULL;\n }\n \n const char *help_unknown_cmd(const char *cmd)\n@@ -307,12 +310,10 @@ const char *help_unknown_cmd(const char *cmd)\n \tload_command_list(\"git-\", &main_cmds, &other_cmds);\n \n \tadd_cmd_list(&main_cmds, &aliases);\n-\tALLOC_GROW(main_cmds.names, main_cmds.cnt + other_cmds.cnt,\n-\t\t   main_cmds.alloc);\n-\tmemcpy(main_cmds.names + main_cmds.cnt, other_cmds.names,\n-\t       other_cmds.cnt * sizeof(other_cmds.names[0]));\n-\tmain_cmds.cnt += other_cmds.cnt;\n-\tfree(other_cmds.names);\n+\tadd_cmd_list(&main_cmds, &other_cmds);\n+\tqsort(main_cmds.names, main_cmds.cnt,\n+\t      sizeof(main_cmds.names), cmdname_compare);\n+\tuniq(&main_cmds);\n \n \t/* This reuses cmdname->len for similarity index */\n \tfor (i = 0; i < main_cmds.cnt; ++i)\n"},{"id":"90432","messageId":"9FACD5FA-7BAE-40B9-B378-165BAEAF2045@ai.rug.nl","threadId":"15476","inReplyTo":"7vtzcnvfq4.fsf@gitster.siamese.dyndns.org","subject":"Re: [PATCH] git wrapper: also uses aliases to suggest mistyped commands","fromName":"Pieter de Bie","fromEmail":"pdebie@ai.rug.nl","sentAt":"2008-09-11T10:37:35Z","receivedAt":"2008-09-11T10:37:35Z","isPatch":true,"sender":{"key":"pdebie@ai.rug.nl","avatar":null},"body":"\nOn 10 sep 2008, at 23:44, Junio C Hamano wrote:\n\n> Aliases should not hide the commands available elsewhere, and the  \n> actual\n> execution codepath around ll.480-490 in git.c avoids getting fooled by\n> misconfigured aliases, but you do not protect yourself from that  \n> kind of\n> misconfiguration in this patch.  You can have both \"git-foo\" command  \n> on\n> your private $PATH and alias.foo in your configuration, and they  \n> will have\n> the same levenshtein score.  I suspect this will cause the same \"foo\"\n> suggested twice when the user types \"git fo\" from the command line.\n\nYes, I didn't think of that.\n\n> Here is a suggested fix-up on top of your patch to address these  \n> issues.\n\nLooks good. Do you want me to resend the patch or will you squash it?\n\n- Pieter\n"}]}