Skip to content

Commit 746c221

Browse files
pietergitster
authored andcommitted
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>
1 parent da06a80 commit 746c221

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

help.c

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,8 @@ void load_command_list(const char *prefix,
204204
while (1) {
205205
if ((colon = strchr(path, PATH_SEP)))
206206
*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);
209209

210210
if (!colon)
211211
break;
@@ -262,11 +262,15 @@ int is_in_cmdlist(struct cmdnames *c, const char *s)
262262
}
263263

264264
static int autocorrect;
265+
static struct cmdnames aliases;
265266

266267
static int git_unknown_cmd_config(const char *var, const char *value, void *cb)
267268
{
268269
if (!strcmp(var, "help.autocorrect"))
269270
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));
270274

271275
return git_default_config(var, value, cb);
272276
}
@@ -280,24 +284,36 @@ static int levenshtein_compare(const void *p1, const void *p2)
280284
return l1 != l2 ? l1 - l2 : strcmp(s1, s2);
281285
}
282286

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+
283299
const char *help_unknown_cmd(const char *cmd)
284300
{
285301
int i, n, best_similarity = 0;
286302
struct cmdnames main_cmds, other_cmds;
287303

288304
memset(&main_cmds, 0, sizeof(main_cmds));
289305
memset(&other_cmds, 0, sizeof(main_cmds));
306+
memset(&aliases, 0, sizeof(aliases));
290307

291308
git_config(git_unknown_cmd_config, NULL);
292309

293310
load_command_list("git-", &main_cmds, &other_cmds);
294311

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);
301317

302318
/* This reuses cmdname->len for similarity index */
303319
for (i = 0; i < main_cmds.cnt; ++i)

0 commit comments

Comments
 (0)