From: Junio C Hamano Date: Fri, 26 Sep 2014 21:39:46 +0000 (-0700) Subject: Merge branch 'sb/help-unknown-command-sort-fix' X-Git-Tag: v2.2.0-rc0~81 X-Git-Url: https://git.lorimer.id.au/gitweb.git/diff_plain/5d7f49dc79edf476a0b9266ea5f076b723eca6ec Merge branch 'sb/help-unknown-command-sort-fix' Code cleanup. * sb/help-unknown-command-sort-fix: help: fix the size passed to qsort --- 5d7f49dc79edf476a0b9266ea5f076b723eca6ec diff --cc help.c index 7af65e205e,3678d6a98b..2072a873e2 --- a/help.c +++ b/help.c @@@ -305,39 -312,13 +305,39 @@@ const char *help_unknown_cmd(const cha add_cmd_list(&main_cmds, &aliases); add_cmd_list(&main_cmds, &other_cmds); qsort(main_cmds.names, main_cmds.cnt, - sizeof(main_cmds.names), cmdname_compare); + sizeof(*main_cmds.names), cmdname_compare); uniq(&main_cmds); - /* This reuses cmdname->len for similarity index */ - for (i = 0; i < main_cmds.cnt; ++i) + /* This abuses cmdname->len for levenshtein distance */ + for (i = 0, n = 0; i < main_cmds.cnt; i++) { + int cmp = 0; /* avoid compiler stupidity */ + const char *candidate = main_cmds.names[i]->name; + + /* + * An exact match means we have the command, but + * for some reason exec'ing it gave us ENOENT; probably + * it's a bad interpreter in the #! line. + */ + if (!strcmp(candidate, cmd)) + die(_(bad_interpreter_advice), cmd, cmd); + + /* Does the candidate appear in common_cmds list? */ + while (n < ARRAY_SIZE(common_cmds) && + (cmp = strcmp(common_cmds[n].name, candidate)) < 0) + n++; + if ((n < ARRAY_SIZE(common_cmds)) && !cmp) { + /* Yes, this is one of the common commands */ + n++; /* use the entry from common_cmds[] */ + if (starts_with(candidate, cmd)) { + /* Give prefix match a very good score */ + main_cmds.names[i]->len = 0; + continue; + } + } + main_cmds.names[i]->len = - levenshtein(cmd, main_cmds.names[i]->name, 0, 2, 1, 4); + levenshtein(cmd, candidate, 0, 2, 1, 3) + 1; + } qsort(main_cmds.names, main_cmds.cnt, sizeof(*main_cmds.names), levenshtein_compare);