]> asedeno.scripts.mit.edu Git - git.git/blobdiff - help.c
Merge branch 'jc/rerere'
[git.git] / help.c
diff --git a/help.c b/help.c
index 483e1e9c3824a39d231fca353c2fd68f1a68f840..ca9632b6c58267776086f48e87e2236ff1f6053f 100644 (file)
--- a/help.c
+++ b/help.c
@@ -163,7 +163,15 @@ static void exec_man_man(const char* path, const char *page)
        warning("failed to exec '%s': %s", path, strerror(errno));
 }
 
-static void do_add_man_viewer(const char *name)
+static void exec_man_cmd(const char *cmd, const char *page)
+{
+       struct strbuf shell_cmd = STRBUF_INIT;
+       strbuf_addf(&shell_cmd, "%s %s", cmd, page);
+       execl("/bin/sh", "sh", "-c", shell_cmd.buf, NULL);
+       warning("failed to exec '%s': %s", cmd, strerror(errno));
+}
+
+static void add_man_viewer(const char *name)
 {
        struct man_viewer_list **p = &man_viewer_list;
        size_t len = strlen(name);
@@ -181,16 +189,6 @@ static int supported_man_viewer(const char *name, size_t len)
                !strncasecmp("konqueror", name, len));
 }
 
-static int add_man_viewer(const char *value)
-{
-       if (supported_man_viewer(value, strlen(value)))
-               do_add_man_viewer(value);
-       else
-               warning("'%s': unsupported man viewer.", value);
-
-       return 0;
-}
-
 static void do_add_man_viewer_info(const char *name,
                                   size_t len,
                                   const char *value)
@@ -210,7 +208,23 @@ static int add_man_viewer_path(const char *name,
        if (supported_man_viewer(name, len))
                do_add_man_viewer_info(name, len, value);
        else
-               warning("'%s': path for unsupported man viewer.", name);
+               warning("'%s': path for unsupported man viewer.\n"
+                       "Please consider using 'man.<tool>.cmd' instead.",
+                       name);
+
+       return 0;
+}
+
+static int add_man_viewer_cmd(const char *name,
+                             size_t len,
+                             const char *value)
+{
+       if (supported_man_viewer(name, len))
+               warning("'%s': cmd for supported man viewer.\n"
+                       "Please consider using 'man.<tool>.path' instead.",
+                       name);
+       else
+               do_add_man_viewer_info(name, len, value);
 
        return 0;
 }
@@ -228,12 +242,17 @@ static int add_man_viewer_info(const char *var, const char *value)
                        return config_error_nonbool(var);
                return add_man_viewer_path(name, subkey - name, value);
        }
+       if (!strcmp(subkey, ".cmd")) {
+               if (!value)
+                       return config_error_nonbool(var);
+               return add_man_viewer_cmd(name, subkey - name, value);
+       }
 
        warning("'%s': unsupported man viewer sub key.", subkey);
        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)
@@ -244,12 +263,13 @@ static int git_help_config(const char *var, const char *value)
        if (!strcmp(var, "man.viewer")) {
                if (!value)
                        return config_error_nonbool(var);
-               return add_man_viewer(value);
+               add_man_viewer(value);
+               return 0;
        }
        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) */
@@ -371,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)
 {
@@ -384,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;
@@ -427,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);
@@ -507,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)
@@ -546,16 +595,18 @@ static void setup_man_path(void)
 
 static void exec_viewer(const char *name, const char *page)
 {
-       const char *path = get_man_viewer_info(name);
+       const char *info = get_man_viewer_info(name);
 
        if (!strcasecmp(name, "man"))
-               exec_man_man(path, page);
+               exec_man_man(info, page);
        else if (!strcasecmp(name, "woman"))
-               exec_woman_emacs(path, page);
+               exec_woman_emacs(info, page);
        else if (!strcasecmp(name, "konqueror"))
-               exec_man_konqueror(path, page);
+               exec_man_konqueror(info, page);
+       else if (info)
+               exec_man_cmd(info, page);
        else
-               warning("'%s': unsupported man viewer.", name);
+               warning("'%s': unknown man viewer.", name);
 }
 
 static void show_man_page(const char *git_cmd)
@@ -619,7 +670,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);
@@ -627,12 +678,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;
        }