Merge branch 'tf/doc-pt-br'
[gitweb.git] / help.c
diff --git a/help.c b/help.c
index 300cd38a9fc2afa61d4390a8fee590997bb6e590..e8db31f60f010887ccb3a943344da566e0937403 100644 (file)
--- a/help.c
+++ b/help.c
@@ -100,7 +100,7 @@ static void pretty_print_string_list(struct cmdnames *cmds, int longest)
 
        if (space < max_cols)
                cols = max_cols / space;
-       rows = (cmds->cnt + cols - 1) / cols;
+       rows = DIV_ROUND_UP(cmds->cnt, cols);
 
        for (i = 0; i < rows; i++) {
                printf("  ");
@@ -126,8 +126,8 @@ static int is_executable(const char *name)
            !S_ISREG(st.st_mode))
                return 0;
 
-#ifdef __MINGW32__
-       /* cannot trust the executable bit, peek into the file instead */
+#ifdef WIN32
+{      /* cannot trust the executable bit, peek into the file instead */
        char buf[3] = { 0 };
        int n;
        int fd = open(name, O_RDONLY);
@@ -140,6 +140,7 @@ static int is_executable(const char *name)
                                st.st_mode |= S_IXUSR;
                close(fd);
        }
+}
 #endif
        return st.st_mode & S_IXUSR;
 }
@@ -204,8 +205,8 @@ void load_command_list(const char *prefix,
                while (1) {
                        if ((colon = strchr(path, PATH_SEP)))
                                *colon = 0;
-
-                       list_commands_in_dir(other_cmds, path, prefix);
+                       if (!exec_path || strcmp(path, exec_path))
+                               list_commands_in_dir(other_cmds, path, prefix);
 
                        if (!colon)
                                break;
@@ -262,11 +263,15 @@ int is_in_cmdlist(struct cmdnames *c, const char *s)
 }
 
 static int autocorrect;
+static struct cmdnames aliases;
 
 static int git_unknown_cmd_config(const char *var, const char *value, void *cb)
 {
        if (!strcmp(var, "help.autocorrect"))
                autocorrect = git_config_int(var,value);
+       /* Also use aliases for command lookup */
+       if (!prefixcmp(var, "alias."))
+               add_cmdname(&aliases, var + 6, strlen(var + 6));
 
        return git_default_config(var, value, cb);
 }
@@ -280,24 +285,36 @@ static int levenshtein_compare(const void *p1, const void *p2)
        return l1 != l2 ? l1 - l2 : strcmp(s1, s2);
 }
 
+static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
+{
+       int i;
+       ALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc);
+
+       for (i = 0; i < old->cnt; i++)
+               cmds->names[cmds->cnt++] = old->names[i];
+       free(old->names);
+       old->cnt = 0;
+       old->names = NULL;
+}
+
 const char *help_unknown_cmd(const char *cmd)
 {
        int i, n, best_similarity = 0;
        struct cmdnames main_cmds, other_cmds;
 
        memset(&main_cmds, 0, sizeof(main_cmds));
-       memset(&other_cmds, 0, sizeof(main_cmds));
+       memset(&other_cmds, 0, sizeof(other_cmds));
+       memset(&aliases, 0, sizeof(aliases));
 
        git_config(git_unknown_cmd_config, NULL);
 
        load_command_list("git-", &main_cmds, &other_cmds);
 
-       ALLOC_GROW(main_cmds.names, main_cmds.cnt + other_cmds.cnt,
-                  main_cmds.alloc);
-       memcpy(main_cmds.names + main_cmds.cnt, other_cmds.names,
-              other_cmds.cnt * sizeof(other_cmds.names[0]));
-       main_cmds.cnt += other_cmds.cnt;
-       free(other_cmds.names);
+       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);
+       uniq(&main_cmds);
 
        /* This reuses cmdname->len for similarity index */
        for (i = 0; i < main_cmds.cnt; ++i)
@@ -318,7 +335,7 @@ const char *help_unknown_cmd(const char *cmd)
                const char *assumed = main_cmds.names[0]->name;
                main_cmds.names[0] = NULL;
                clean_cmdnames(&main_cmds);
-               fprintf(stderr, "WARNING: You called a Git program named '%s', "
+               fprintf(stderr, "WARNING: You called a Git command named '%s', "
                        "which does not exist.\n"
                        "Continuing under the assumption that you meant '%s'\n",
                        cmd, assumed);