Code

Merge branch 'ls/mailinfo'
[git.git] / help.c
diff --git a/help.c b/help.c
index af80979fcb177faace150a7cfa7cb66daeb59f49..bfc84aed10d49b1eb641d920a102e94d2e905fda 100644 (file)
--- a/help.c
+++ b/help.c
@@ -40,7 +40,7 @@ static struct option builtin_help_options[] = {
 };
 
 static const char * const builtin_help_usage[] = {
-       "git-help [--all] [--man|--web|--info] [command]",
+       "git help [--all] [--man|--web|--info] [command]",
        NULL
 };
 
@@ -252,7 +252,7 @@ static int add_man_viewer_info(const char *var, const char *value)
        return 0;
 }
 
-static int git_help_config(const char *var, const char *value)
+static int git_help_config(const char *var, const char *value, void *cb)
 {
        if (!strcmp(var, "help.format")) {
                if (!value)
@@ -269,7 +269,7 @@ static int git_help_config(const char *var, const char *value)
        if (!prefixcmp(var, "man."))
                return add_man_viewer_info(var, value);
 
-       return git_default_config(var, value);
+       return git_default_config(var, value, cb);
 }
 
 /* most GUI terminals set COLUMNS (although some don't export it) */
@@ -391,6 +391,32 @@ static void pretty_print_string_list(struct cmdnames *cmds, int longest)
        }
 }
 
+static int is_executable(const char *name)
+{
+       struct stat st;
+
+       if (stat(name, &st) || /* stat, not lstat */
+           !S_ISREG(st.st_mode))
+               return 0;
+
+#ifdef __MINGW32__
+       /* cannot trust the executable bit, peek into the file instead */
+       char buf[3] = { 0 };
+       int n;
+       int fd = open(name, O_RDONLY);
+       st.st_mode &= ~S_IXUSR;
+       if (fd >= 0) {
+               n = read(fd, buf, 2);
+               if (n == 2)
+                       /* DOS executables start with "MZ" */
+                       if (!strcmp(buf, "#!") || !strcmp(buf, "MZ"))
+                               st.st_mode |= S_IXUSR;
+               close(fd);
+       }
+#endif
+       return st.st_mode & S_IXUSR;
+}
+
 static unsigned int list_commands_in_dir(struct cmdnames *cmds,
                                         const char *path)
 {
@@ -404,15 +430,12 @@ static unsigned int list_commands_in_dir(struct cmdnames *cmds,
                return 0;
 
        while ((de = readdir(dir)) != NULL) {
-               struct stat st;
                int entlen;
 
                if (prefixcmp(de->d_name, prefix))
                        continue;
 
-               if (stat(de->d_name, &st) || /* stat, not lstat */
-                   !S_ISREG(st.st_mode) ||
-                   !(st.st_mode & S_IXUSR))
+               if (!is_executable(de->d_name))
                        continue;
 
                entlen = strlen(de->d_name) - prefix_len;
@@ -447,7 +470,7 @@ static unsigned int load_command_list(void)
 
        path = paths = xstrdup(env_path);
        while (1) {
-               if ((colon = strchr(path, ':')))
+               if ((colon = strchr(path, PATH_SEP)))
                        *colon = 0;
 
                len = list_commands_in_dir(&other_cmds, path);
@@ -527,20 +550,26 @@ static int is_git_command(const char *s)
                is_in_cmdlist(&other_cmds, s);
 }
 
+static const char *prepend(const char *prefix, const char *cmd)
+{
+       size_t pre_len = strlen(prefix);
+       size_t cmd_len = strlen(cmd);
+       char *p = xmalloc(pre_len + cmd_len + 1);
+       memcpy(p, prefix, pre_len);
+       strcpy(p + pre_len, cmd);
+       return p;
+}
+
 static const char *cmd_to_page(const char *git_cmd)
 {
        if (!git_cmd)
                return "git";
        else if (!prefixcmp(git_cmd, "git"))
                return git_cmd;
-       else {
-               int page_len = strlen(git_cmd) + 4;
-               char *p = xmalloc(page_len + 1);
-               strcpy(p, "git-");
-               strcpy(p + 4, git_cmd);
-               p[page_len] = 0;
-               return p;
-       }
+       else if (is_git_command(git_cmd))
+               return prepend("git-", git_cmd);
+       else
+               return prepend("git", git_cmd);
 }
 
 static void setup_man_path(void)
@@ -604,14 +633,28 @@ static void show_info_page(const char *git_cmd)
 static void get_html_page_path(struct strbuf *page_path, const char *page)
 {
        struct stat st;
+       const char *html_path = system_path(GIT_HTML_PATH);
 
        /* Check that we have a git documentation directory. */
-       if (stat(GIT_HTML_PATH "/git.html", &st) || !S_ISREG(st.st_mode))
-               die("'%s': not a documentation directory.", GIT_HTML_PATH);
+       if (stat(mkpath("%s/git.html", html_path), &st)
+           || !S_ISREG(st.st_mode))
+               die("'%s': not a documentation directory.", html_path);
 
        strbuf_init(page_path, 0);
-       strbuf_addf(page_path, GIT_HTML_PATH "/%s.html", page);
+       strbuf_addf(page_path, "%s/%s.html", html_path, page);
+}
+
+/*
+ * If open_html is not defined in a platform-specific way (see for
+ * example compat/mingw.h), we use the script web--browse to display
+ * HTML.
+ */
+#ifndef open_html
+void open_html(const char *path)
+{
+       execl_git_cmd("web--browse", "-c", "help.browser", path, NULL);
 }
+#endif
 
 static void show_html_page(const char *git_cmd)
 {
@@ -620,7 +663,7 @@ static void show_html_page(const char *git_cmd)
 
        get_html_page_path(&page_path, page);
 
-       execl_git_cmd("web--browse", "-c", "help.browser", page_path.buf, NULL);
+       open_html(page_path.buf);
 }
 
 void help_unknown_cmd(const char *cmd)
@@ -641,7 +684,7 @@ int cmd_help(int argc, const char **argv, const char *prefix)
        const char *alias;
 
        setup_git_directory_gently(&nongit);
-       git_config(git_help_config);
+       git_config(git_help_config, NULL);
 
        argc = parse_options(argc, argv, builtin_help_options,
                        builtin_help_usage, 0);
@@ -649,12 +692,14 @@ int cmd_help(int argc, const char **argv, const char *prefix)
        if (show_all) {
                printf("usage: %s\n\n", git_usage_string);
                list_commands();
+               printf("%s\n", git_more_info_string);
                return 0;
        }
 
        if (!argv[0]) {
                printf("usage: %s\n\n", git_usage_string);
                list_common_cmds_help();
+               printf("\n%s\n", git_more_info_string);
                return 0;
        }