make USE_NSEC work as expected
[gitweb.git] / help.c
diff --git a/help.c b/help.c
index b1ebca4091e9e1cb623ff89a69306cdb00aa9c68..fd87bb5aeec82beec600be46248b19b13bb33804 100644 (file)
--- a/help.c
+++ b/help.c
@@ -144,11 +144,10 @@ static int is_executable(const char *name)
        return st.st_mode & S_IXUSR;
 }
 
-static unsigned int list_commands_in_dir(struct cmdnames *cmds,
+static void list_commands_in_dir(struct cmdnames *cmds,
                                         const char *path,
                                         const char *prefix)
 {
-       unsigned int longest = 0;
        int prefix_len;
        DIR *dir = opendir(path);
        struct dirent *de;
@@ -156,7 +155,7 @@ static unsigned int list_commands_in_dir(struct cmdnames *cmds,
        int len;
 
        if (!dir)
-               return 0;
+               return;
        if (!prefix)
                prefix = "git-";
        prefix_len = strlen(prefix);
@@ -179,68 +178,62 @@ static unsigned int list_commands_in_dir(struct cmdnames *cmds,
                if (has_extension(de->d_name, ".exe"))
                        entlen -= 4;
 
-               if (longest < entlen)
-                       longest = entlen;
-
                add_cmdname(cmds, de->d_name + prefix_len, entlen);
        }
        closedir(dir);
        strbuf_release(&buf);
-
-       return longest;
 }
 
-unsigned int load_command_list(const char *prefix,
+void load_command_list(const char *prefix,
                struct cmdnames *main_cmds,
                struct cmdnames *other_cmds)
 {
-       unsigned int longest = 0;
-       unsigned int len;
        const char *env_path = getenv("PATH");
-       char *paths, *path, *colon;
        const char *exec_path = git_exec_path();
 
-       if (exec_path)
-               longest = list_commands_in_dir(main_cmds, exec_path, prefix);
-
-       if (!env_path) {
-               fprintf(stderr, "PATH not set\n");
-               exit(1);
+       if (exec_path) {
+               list_commands_in_dir(main_cmds, exec_path, prefix);
+               qsort(main_cmds->names, main_cmds->cnt,
+                     sizeof(*main_cmds->names), cmdname_compare);
+               uniq(main_cmds);
        }
 
-       path = paths = xstrdup(env_path);
-       while (1) {
-               if ((colon = strchr(path, PATH_SEP)))
-                       *colon = 0;
+       if (env_path) {
+               char *paths, *path, *colon;
+               path = paths = xstrdup(env_path);
+               while (1) {
+                       if ((colon = strchr(path, PATH_SEP)))
+                               *colon = 0;
+                       if (!exec_path || strcmp(path, exec_path))
+                               list_commands_in_dir(other_cmds, path, prefix);
 
-               len = list_commands_in_dir(other_cmds, path, prefix);
-               if (len > longest)
-                       longest = len;
+                       if (!colon)
+                               break;
+                       path = colon + 1;
+               }
+               free(paths);
 
-               if (!colon)
-                       break;
-               path = colon + 1;
+               qsort(other_cmds->names, other_cmds->cnt,
+                     sizeof(*other_cmds->names), cmdname_compare);
+               uniq(other_cmds);
        }
-       free(paths);
-
-       qsort(main_cmds->names, main_cmds->cnt,
-             sizeof(*main_cmds->names), cmdname_compare);
-       uniq(main_cmds);
-
-       qsort(other_cmds->names, other_cmds->cnt,
-             sizeof(*other_cmds->names), cmdname_compare);
-       uniq(other_cmds);
        exclude_cmds(other_cmds, main_cmds);
-
-       return longest;
 }
 
-void list_commands(const char *title, unsigned int longest,
-               struct cmdnames *main_cmds, struct cmdnames *other_cmds)
+void list_commands(const char *title, struct cmdnames *main_cmds,
+                  struct cmdnames *other_cmds)
 {
-       const char *exec_path = git_exec_path();
+       int i, longest = 0;
+
+       for (i = 0; i < main_cmds->cnt; i++)
+               if (longest < main_cmds->names[i]->len)
+                       longest = main_cmds->names[i]->len;
+       for (i = 0; i < other_cmds->cnt; i++)
+               if (longest < other_cmds->names[i]->len)
+                       longest = other_cmds->names[i]->len;
 
        if (main_cmds->cnt) {
+               const char *exec_path = git_exec_path();
                printf("available %s in '%s'\n", title, exec_path);
                printf("----------------");
                mput_char('-', strlen(title) + strlen(exec_path));
@@ -268,6 +261,20 @@ int is_in_cmdlist(struct cmdnames *c, const char *s)
        return 0;
 }
 
+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);
+}
+
 static int levenshtein_compare(const void *p1, const void *p2)
 {
        const struct cmdname *const *c1 = p1, *const *c2 = p2;
@@ -277,6 +284,18 @@ 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;
@@ -284,15 +303,17 @@ const char *help_unknown_cmd(const char *cmd)
 
        memset(&main_cmds, 0, sizeof(main_cmds));
        memset(&other_cmds, 0, sizeof(main_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)
@@ -309,7 +330,7 @@ const char *help_unknown_cmd(const char *cmd)
        n = 1;
        while (n < main_cmds.cnt && best_similarity == main_cmds.names[n]->len)
                ++n;
-       if (n == 1) {
+       if (autocorrect && n == 1) {
                const char *assumed = main_cmds.names[0]->name;
                main_cmds.names[0] = NULL;
                clean_cmdnames(&main_cmds);
@@ -317,6 +338,11 @@ const char *help_unknown_cmd(const char *cmd)
                        "which does not exist.\n"
                        "Continuing under the assumption that you meant '%s'\n",
                        cmd, assumed);
+               if (autocorrect > 0) {
+                       fprintf(stderr, "in %0.1f seconds automatically...\n",
+                               (float)autocorrect/10.0);
+                       poll(NULL, 0, autocorrect * 100);
+               }
                return assumed;
        }