@matheusbsbs / matheus-bagliano / commits / 746c221ad0
Fork 0

git wrapper: also use aliases to correct mistyped commands

Signed-off-by: Pieter de Bie <pdebie@ai.rug.nl> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Pieter de Bie committed Sep 10, 2008 at 17:54 UTC 746c221ad063854e23661f6898c002c8726f5dad
1 file changed +24 -8
help.c
+24 -8
@@ -204,8 +204,8 @@ void load_command_list(const char *prefix,
204 while (1) {
205 if ((colon = strchr(path, PATH_SEP)))
206 *colon = 0;
207 -
208 - list_commands_in_dir(other_cmds, path, prefix);
207 + if (!exec_path || strcmp(path, exec_path))
208 + list_commands_in_dir(other_cmds, path, prefix);
209
210 if (!colon)
211 break;
@@ -262,11 +262,15 @@ int is_in_cmdlist(struct cmdnames *c, const char *s)
262 }
263
264 static int autocorrect;
265 +static struct cmdnames aliases;
266
267 static int git_unknown_cmd_config(const char *var, const char *value, void *cb)
268 {
269 if (!strcmp(var, "help.autocorrect"))
270 autocorrect = git_config_int(var,value);
271 + /* Also use aliases for command lookup */
272 + if (!prefixcmp(var, "alias."))
273 + add_cmdname(&aliases, var + 6, strlen(var + 6));
274
275 return git_default_config(var, value, cb);
276 }
@@ -280,6 +284,18 @@ static int levenshtein_compare(const void *p1, const void *p2)
284 return l1 != l2 ? l1 - l2 : strcmp(s1, s2);
285 }
286
287 +static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
288 +{
289 + int i;
290 + ALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc);
291 +
292 + for (i = 0; i < old->cnt; i++)
293 + cmds->names[cmds->cnt++] = old->names[i];
294 + free(old->names);
295 + old->cnt = 0;
296 + old->names = NULL;
297 +}
298 +
299 const char *help_unknown_cmd(const char *cmd)
300 {
301 int i, n, best_similarity = 0;
@@ -287,17 +303,17 @@ const char *help_unknown_cmd(const char *cmd)
303
304 memset(&main_cmds, 0, sizeof(main_cmds));
305 memset(&other_cmds, 0, sizeof(main_cmds));
306 + memset(&aliases, 0, sizeof(aliases));
307
308 git_config(git_unknown_cmd_config, NULL);
309
310 load_command_list("git-", &main_cmds, &other_cmds);
311
295 - ALLOC_GROW(main_cmds.names, main_cmds.cnt + other_cmds.cnt,
296 - main_cmds.alloc);
297 - memcpy(main_cmds.names + main_cmds.cnt, other_cmds.names,
298 - other_cmds.cnt * sizeof(other_cmds.names[0]));
299 - main_cmds.cnt += other_cmds.cnt;
300 - free(other_cmds.names);
312 + add_cmd_list(&main_cmds, &aliases);
313 + add_cmd_list(&main_cmds, &other_cmds);
314 + qsort(main_cmds.names, main_cmds.cnt,
315 + sizeof(main_cmds.names), cmdname_compare);
316 + uniq(&main_cmds);
317
318 /* This reuses cmdname->len for similarity index */
319 for (i = 0; i < main_cmds.cnt; ++i)