Skip to content

Commit 06500a0

Browse files
j6tgitster
authored andcommitted
help.autocorrect: do not run a command if the command given is junk
If a given command is not found, then help.c tries to guess which one the user could have meant. If help.autocorrect is 0 or unset, then a list of suggestions is given as long as the dissimilarity between the given command and the candidates is not excessively high. But if help.autocorrect was non-zero (i.e., a delay after which the command is run automatically), the latter restriction on dissimilarity was not obeyed. In my case, this happened: $ git ..daab02 WARNING: You called a Git command named '..daab02', which does not exist. Continuing under the assumption that you meant 'read-tree' in 4.0 seconds automatically... The patch reuses the similarity limit that is also applied when the list of suggested commands is printed. Signed-off-by: Johannes Sixt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d79f5d1 commit 06500a0

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

help.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,9 @@ static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
297297
old->names = NULL;
298298
}
299299

300+
/* An empirically derived magic number */
301+
#define SIMILAR_ENOUGH(x) ((x) < 6)
302+
300303
const char *help_unknown_cmd(const char *cmd)
301304
{
302305
int i, n, best_similarity = 0;
@@ -331,7 +334,7 @@ const char *help_unknown_cmd(const char *cmd)
331334
n = 1;
332335
while (n < main_cmds.cnt && best_similarity == main_cmds.names[n]->len)
333336
++n;
334-
if (autocorrect && n == 1) {
337+
if (autocorrect && n == 1 && SIMILAR_ENOUGH(best_similarity)) {
335338
const char *assumed = main_cmds.names[0]->name;
336339
main_cmds.names[0] = NULL;
337340
clean_cmdnames(&main_cmds);
@@ -349,7 +352,7 @@ const char *help_unknown_cmd(const char *cmd)
349352

350353
fprintf(stderr, "git: '%s' is not a git-command. See 'git --help'.\n", cmd);
351354

352-
if (best_similarity < 6) {
355+
if (SIMILAR_ENOUGH(best_similarity)) {
353356
fprintf(stderr, "\nDid you mean %s?\n",
354357
n < 2 ? "this": "one of these");
355358

0 commit comments

Comments
 (0)