Merge branch 'sb/help-unknown-command-sort-fix'
authorJunio C Hamano <gitster@pobox.com>
Fri, 26 Sep 2014 21:39:46 +0000 (14:39 -0700)
committerJunio C Hamano <gitster@pobox.com>
Fri, 26 Sep 2014 21:39:49 +0000 (14:39 -0700)
Code cleanup.

* sb/help-unknown-command-sort-fix:
help: fix the size passed to qsort

1  2 
help.c
diff --cc help.c
index 7af65e205ecdf1a01dce009cbf0ada15de68c844,3678d6a98b6ca119d24be58783450c167673de9b..2072a873e2db784ca66c0f1736b12e523a96b7db
--- 1/help.c
--- 2/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);