Reduce the number of connects when fetching
[gitweb.git] / help.c
diff --git a/help.c b/help.c
index 8217d97787b2ba38a5327c1204797044b2b4cd72..1302a61c83c524099e7d39257daa273a09edad4f 100644 (file)
--- a/help.c
+++ b/help.c
@@ -7,7 +7,44 @@
 #include "builtin.h"
 #include "exec_cmd.h"
 #include "common-cmds.h"
-#include <sys/ioctl.h>
+
+static const char *help_default_format;
+
+static enum help_format {
+       man_format,
+       info_format,
+       web_format,
+} help_format = man_format;
+
+static void parse_help_format(const char *format)
+{
+       if (!format) {
+               help_format = man_format;
+               return;
+       }
+       if (!strcmp(format, "man")) {
+               help_format = man_format;
+               return;
+       }
+       if (!strcmp(format, "info")) {
+               help_format = info_format;
+               return;
+       }
+       if (!strcmp(format, "web") || !strcmp(format, "html")) {
+               help_format = web_format;
+               return;
+       }
+       die("unrecognized help format '%s'", format);
+}
+
+static int git_help_config(const char *var, const char *value)
+{
+       if (!strcmp(var, "help.format")) {
+               help_default_format = xstrdup(value);
+               return 0;
+       }
+       return git_default_config(var, value);
+}
 
 /* most GUI terminals set COLUMNS (although some don't export it) */
 static int term_columns(void)
@@ -238,27 +275,65 @@ void list_common_cmds_help(void)
                mput_char(' ', longest - strlen(common_cmds[i].name));
                puts(common_cmds[i].help);
        }
-       puts("(use 'git help -a' to get a list of all installed git commands)");
 }
 
-static void show_man_page(const char *git_cmd)
+static const char *cmd_to_page(const char *git_cmd)
 {
-       const char *page;
-
-       if (!prefixcmp(git_cmd, "git"))
-               page = 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;
-               page = p;
+               return p;
        }
+}
+
+static void setup_man_path(void)
+{
+       struct strbuf new_path;
+       const char *old_path = getenv("MANPATH");
+
+       strbuf_init(&new_path, 0);
+
+       /* We should always put ':' after our path. If there is no
+        * old_path, the ':' at the end will let 'man' to try
+        * system-wide paths after ours to find the manual page. If
+        * there is old_path, we need ':' as delimiter. */
+       strbuf_addstr(&new_path, GIT_MAN_PATH);
+       strbuf_addch(&new_path, ':');
+       if (old_path)
+               strbuf_addstr(&new_path, old_path);
+
+       setenv("MANPATH", new_path.buf, 1);
 
+       strbuf_release(&new_path);
+}
+
+static void show_man_page(const char *git_cmd)
+{
+       const char *page = cmd_to_page(git_cmd);
+       setup_man_path();
        execlp("man", "man", page, NULL);
 }
 
+static void show_info_page(const char *git_cmd)
+{
+       const char *page = cmd_to_page(git_cmd);
+       setenv("INFOPATH", GIT_INFO_PATH, 1);
+       execlp("info", "info", "gitman", page, NULL);
+}
+
+static void show_html_page(const char *git_cmd)
+{
+       const char *page = cmd_to_page(git_cmd);
+       execl_git_cmd("help--browse", page, NULL);
+}
+
 void help_unknown_cmd(const char *cmd)
 {
        fprintf(stderr, "git: '%s' is not a git-command. See 'git --help'.\n", cmd);
@@ -273,22 +348,51 @@ int cmd_version(int argc, const char **argv, const char *prefix)
 
 int cmd_help(int argc, const char **argv, const char *prefix)
 {
-       const char *help_cmd = argc > 1 ? argv[1] : NULL;
+       const char *help_cmd = argv[1];
 
-       if (!help_cmd) {
+       if (argc < 2) {
                printf("usage: %s\n\n", git_usage_string);
                list_common_cmds_help();
                exit(0);
        }
 
-       else if (!strcmp(help_cmd, "--all") || !strcmp(help_cmd, "-a")) {
+       if (!strcmp(help_cmd, "--all") || !strcmp(help_cmd, "-a")) {
                printf("usage: %s\n\n", git_usage_string);
                list_commands();
-               exit(0);
        }
 
-       else
-               show_man_page(help_cmd);
+       else if (!strcmp(help_cmd, "--web") || !strcmp(help_cmd, "-w")) {
+               show_html_page(argc > 2 ? argv[2] : NULL);
+       }
+
+       else if (!strcmp(help_cmd, "--info") || !strcmp(help_cmd, "-i")) {
+               show_info_page(argc > 2 ? argv[2] : NULL);
+       }
+
+       else if (!strcmp(help_cmd, "--man") || !strcmp(help_cmd, "-m")) {
+               show_man_page(argc > 2 ? argv[2] : NULL);
+       }
+
+       else {
+               int nongit;
+
+               setup_git_directory_gently(&nongit);
+               git_config(git_help_config);
+               if (help_default_format)
+                       parse_help_format(help_default_format);
+
+               switch (help_format) {
+               case man_format:
+                       show_man_page(help_cmd);
+                       break;
+               case info_format:
+                       show_info_page(help_cmd);
+                       break;
+               case web_format:
+                       show_html_page(help_cmd);
+                       break;
+               }
+       }
 
        return 0;
 }