{"thread":{"id":"8451","subject":"[PATCH 1/2] Show html help with git-help --html","startedAt":"2007-06-05T18:34:20Z","lastAt":"2007-06-09T15:03:22Z","messageCount":6,"participants":["Nguyen Thai Ngoc Duy","Frank Lichtenheld","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":2},"messages":[{"id":"44092","messageId":"20070605183420.GA8450@localhost","threadId":"8451","inReplyTo":null,"subject":"[PATCH 1/2] Show html help with git-help --html","fromName":"Nguyen Thai Ngoc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2007-06-05T18:34:20Z","receivedAt":"2007-06-05T18:34:20Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"This patch was inspired by ClearCase command 'ct man', which\nopens an html help file on Windows. I at first attempted to\nimplement it for MinGW port only but found it so useful that I\nwanted to have it even in Linux.\n\nA new option '--html' is added to git. When git-help is called\nwith --html, it will try to open an html file located at\n$(docdir)/html using xdg-open. HTML files are not installed\nby default so users have to install them manually or have their\ndistributors to do that.\n\nThere are two new config options introduced in this patch. The\nfirst is core.help. It has one of three values: html, auto or\nman. 'html' has the same effect as git-help --html. 'auto'\nwill in addition fall back to man pages if possible. 'man'\nis 'I hate html, give me my man pages'.  The second option is\ncore.htmlprogram, used to specify the program you want to\nopen html files instead of xdg-open. You can override the default\nprogram by appending HTML_PROGRAM=blah when calling make.\ncore.htmlprogram can contain %p, %f or %b.  If none is given,\n%p will be appended.\n\nSigned-off-by: Nguyen Thai Ngoc Duy <pclouds@gmail.com>\n---\n\nSorry the previous patch had broken commit message.\n\n Documentation/config.txt |   16 +++++\n Documentation/git.txt    |    5 +-\n Makefile                 |    5 +-\n cache.h                  |    2 +\n config.c                 |   17 +++++\n config.mak.in            |    3 +-\n environment.c            |    2 +\n git.c                    |    2 +-\n help.c                   |  166 +++++++++++++++++++++++++++++++++++++++++++++-\n 9 files changed, 212 insertions(+), 6 deletions(-)\n\ndiff --git a/Documentation/config.txt b/Documentation/config.txt\nindex 5868d58..7687750 100644\n--- a/Documentation/config.txt\n+++ b/Documentation/config.txt\n@@ -262,6 +262,22 @@ core.excludeFile::\n \tof files which are not meant to be tracked.  See\n \tgitlink:gitignore[5].\n \n+core.help::\n+\tIf 'html', it is equivalent to 'git-help' with option --html.\n+\tIf 'auto', it tries to open html files first. If that attempt fails\n+\t(the html program does not exist or the program return non-zero\n+\tvalue), then it will fall back to man pages. If 'man', always use\n+\tman pages as usual.\n+\n+core.htmlprogram::\n+\tSpecify the program used to open html help files when 'git-help'\n+\tis called with option --html or core.help is other than 'man'.\n+\tBy default, xdg-open will be used.\n+\tSpecial strings '%p', '%f' and '%b' will be replaced with html\n+\tfull path, file name and git command (without .html suffix)\n+\trespectively. If none is given, '%p' will be automatically appended\n+\tto the command line.\n+\n alias.*::\n \tCommand aliases for the gitlink:git[1] command wrapper - e.g.\n \tafter defining \"alias.last = cat-file commit HEAD\", the invocation\ndiff --git a/Documentation/git.txt b/Documentation/git.txt\nindex 98860af..589ebea 100644\n--- a/Documentation/git.txt\n+++ b/Documentation/git.txt\n@@ -10,7 +10,7 @@ SYNOPSIS\n --------\n [verse]\n 'git' [--version] [--exec-path[=GIT_EXEC_PATH]] [-p|--paginate]\n-    [--bare] [--git-dir=GIT_DIR] [--help] COMMAND [ARGS]\n+    [--bare] [--git-dir=GIT_DIR] [--help [--html]] COMMAND [ARGS]\n \n DESCRIPTION\n -----------\n@@ -87,6 +87,9 @@ OPTIONS\n \tcommands.  If a git command is named this option will bring up\n \tthe man-page for that command. If the option '--all' or '-a' is\n \tgiven then all available commands are printed.\n+\tIf option '--html' is given, try opening html files instead of\n+\tusing 'man'. The default program to open html files is xdg-open.\n+\tSee 'git-config' to know how to change html program.\n \n --exec-path::\n \tPath to wherever your core git programs are installed.\ndiff --git a/Makefile b/Makefile\nindex a11ff60..744a732 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -186,6 +186,7 @@ export TCL_PATH TCLTK_PATH\n # explicitly what architecture to check for. Fix this up for yours..\n SPARSE_FLAGS = -D__BIG_ENDIAN__ -D__powerpc__\n \n+HTML_PROGRAM=xdg-open\n \n \n ### --- END CONFIGURATION SECTION ---\n@@ -693,6 +694,7 @@ ETC_GITCONFIG_SQ = $(subst ','\\'',$(ETC_GITCONFIG))\n DESTDIR_SQ = $(subst ','\\'',$(DESTDIR))\n bindir_SQ = $(subst ','\\'',$(bindir))\n gitexecdir_SQ = $(subst ','\\'',$(gitexecdir))\n+html_dir_SQ = $(subst ','\\'',$(html_dir))\n template_dir_SQ = $(subst ','\\'',$(template_dir))\n prefix_SQ = $(subst ','\\'',$(prefix))\n \n@@ -741,7 +743,8 @@ git$X: git.c common-cmds.h $(BUILTIN_OBJS) $(GITLIBS) GIT-CFLAGS\n \t\t$(ALL_CFLAGS) -o $@ $(filter %.c,$^) \\\n \t\t$(BUILTIN_OBJS) $(ALL_LDFLAGS) $(LIBS)\n \n-help.o: common-cmds.h\n+help.o: help.c common-cmds.h\n+\t$(QUIET_CC)$(CC) -o $*.o -c $(ALL_CFLAGS) -DHTML_DIR='\"$(html_dir_SQ)\"' -DHTML_PROGRAM='\"$(HTML_PROGRAM)\"' $<\n \n git-merge-subtree$X: git-merge-recursive$X\n \t$(QUIET_BUILT_IN)rm -f $@ && ln git-merge-recursive$X $@\ndiff --git a/cache.h b/cache.h\nindex 8a9d1f3..dbaae6a 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -288,6 +288,8 @@ extern size_t packed_git_window_size;\n extern size_t packed_git_limit;\n extern size_t delta_base_cache_limit;\n extern int auto_crlf;\n+extern int show_html_help;\n+extern const char *html_help_program;\n \n #define GIT_REPO_VERSION 0\n extern int repository_format_version;\ndiff --git a/config.c b/config.c\nindex 0614c2b..d216ab7 100644\n--- a/config.c\n+++ b/config.c\n@@ -382,6 +382,23 @@ int git_default_config(const char *var, const char *value)\n \t\treturn 0;\n \t}\n \n+\tif (!strcmp(var, \"core.help\")) {\n+\t\tif (!strcmp(value, \"auto\"))\n+\t\t\tshow_html_help = 2;\n+\t\telse if (!strcmp(value, \"html\"))\n+\t\t\tshow_html_help = 1;\n+\t\telse if (!strcmp(value, \"man\"))\n+\t\t\tshow_html_help = 0;\n+\t\telse\n+\t\t\treturn 1;\n+\t\treturn 0;\n+\t}\n+\n+\tif (!strcmp(var, \"core.htmlprogram\")) {\n+\t\thtml_help_program = xstrdup(value);\n+\t\treturn 0;\n+\t}\n+\n \t/* Add other config variables here and to Documentation/config.txt. */\n \treturn 0;\n }\ndiff --git a/config.mak.in b/config.mak.in\nindex eb9d7a5..c3e410d 100644\n--- a/config.mak.in\n+++ b/config.mak.in\n@@ -8,12 +8,14 @@ TAR = @TAR@\n #INSTALL = @INSTALL@\t\t# needs install-sh or install.sh in sources\n TCLTK_PATH = @TCLTK_PATH@\n \n+PACKAGE_TARNAME=@PACKAGE_TARNAME@\n prefix = @prefix@\n exec_prefix = @exec_prefix@\n bindir = @bindir@\n #gitexecdir = @libexecdir@/git-core/\n datarootdir = @datarootdir@\n template_dir = @datadir@/git-core/templates/\n+html_dir = @docdir@/html/\n \n mandir=@mandir@\n \n@@ -38,4 +40,3 @@ NO_STRCASESTR=@NO_STRCASESTR@\n NO_STRLCPY=@NO_STRLCPY@\n NO_SETENV=@NO_SETENV@\n NO_ICONV=@NO_ICONV@\n-\ndiff --git a/environment.c b/environment.c\nindex 9d3e5eb..e905972 100644\n--- a/environment.c\n+++ b/environment.c\n@@ -32,6 +32,8 @@ size_t delta_base_cache_limit = 16 * 1024 * 1024;\n int pager_in_use;\n int pager_use_color = 1;\n int auto_crlf = 0;\t/* 1: both ways, -1: only when adding git objects */\n+int show_html_help = 0;\n+const char *html_help_program = NULL;\n \n static const char *git_dir;\n static char *git_object_dir, *git_index_file, *git_refs_dir, *git_graft_file;\ndiff --git a/git.c b/git.c\nindex 29b55a1..c3c0fe8 100644\n--- a/git.c\n+++ b/git.c\n@@ -4,7 +4,7 @@\n #include \"quote.h\"\n \n const char git_usage_string[] =\n-\t\"git [--version] [--exec-path[=GIT_EXEC_PATH]] [-p|--paginate] [--bare] [--git-dir=GIT_DIR] [--help] COMMAND [ARGS]\";\n+\t\"git [--version] [--exec-path[=GIT_EXEC_PATH]] [-p|--paginate] [--bare] [--git-dir=GIT_DIR] [--help [--html]] COMMAND [ARGS]\";\n \n static void prepend_to_path(const char *dir, int len)\n {\ndiff --git a/help.c b/help.c\nindex 6a9af4d..78b1023 100644\n--- a/help.c\n+++ b/help.c\n@@ -9,6 +9,14 @@\n #include \"common-cmds.h\"\n #include <sys/ioctl.h>\n \n+#ifndef HTML_DIR\n+#define HTML_DIR \"/usr/share/html/\"\n+#endif\n+\n+#ifndef HTML_PROGRAM\n+#define HTML_PROGRAM \"xdg-open\"\n+#endif\n+\n /* most GUI terminals set COLUMNS (although some don't export it) */\n static int term_columns(void)\n {\n@@ -183,6 +191,145 @@ static void show_man_page(const char *git_cmd)\n \texeclp(\"man\", \"man\", page, NULL);\n }\n \n+static void show_html_page(const char *git_cmd)\n+{\n+\tconst char *html_dir;\n+\tint len, ret, nr_quotes;\n+\tchar *p, *p2;\n+\tconst char *cp, *cp2;\n+\tstruct stat st;\n+\tchar *quoted_git_cmd;\n+\tchar *default_command = NULL;\n+\tconst char *command;\n+\n+\thtml_dir = HTML_DIR;\n+\tcommand = html_help_program ? html_help_program : HTML_PROGRAM;\n+\n+\tnr_quotes = 0;\n+\tfor (cp = git_cmd; *cp; cp++)\n+\t\tif (*cp == '\\'') nr_quotes ++;\n+\n+\tlen = strlen(git_cmd) + nr_quotes*2 + 2 + 4; /* two quotes and git- */\n+\tquoted_git_cmd = p2 = xmalloc(len + 1);\n+\t*p2++ = '\\'';\n+\tif (prefixcmp(git_cmd, \"git\")) {\n+\t\tstrcpy(p2,\"git-\");\n+\t\tp2 += 4;\n+\t}\n+\tfor (cp = git_cmd; *cp; cp ++) {\n+\t\tif (*cp == '\\'')\n+\t\t\t*p2++ = '\\\\';\n+\t\t*p2++ = *cp;\n+\t}\n+\t*p2++ = '\\'';\n+\t*p2 = 0;\n+\n+\t/* append %p if there is not any %x */\n+\tif (!strchr(command, '%')) {\n+\t\tlen = strlen(command);\n+\t\tdefault_command = xmalloc(len + 3 + 1); /* space %p */\n+\t\tstrcpy(default_command, command);\n+\t\tstrcat(default_command, \" %p\");\n+\t\tcommand = default_command;\n+\t}\n+\n+\t/* first pass, calculate command length */\n+\tcp = command;\n+\tlen = 0;\n+\twhile (*cp && (cp2 = strchr(cp, '%'))) {\n+\t\tlen += cp2 - cp;\n+\n+\t\tif (!cp2[1]) {\n+\t\t\tlen ++;\n+\t\t\tbreak;\n+\t\t}\n+\n+\t\tswitch (cp2[1]) {\n+\t\t\tcase 'p':\n+\t\t\t\tlen += strlen(html_dir) + strlen(quoted_git_cmd) + 5; /* .html */\n+\t\t\t\tbreak;\n+\n+\t\t\tcase 'f':\n+\t\t\t\tlen += strlen(quoted_git_cmd) + 5; /* .html */\n+\t\t\t\tbreak;\n+\n+\t\t\tcase 'b':\n+\t\t\t\tlen += strlen(quoted_git_cmd);\n+\t\t\t\tbreak;\n+\n+\t\t\tdefault:\n+\t\t\t\tlen += 2;\n+\t\t}\n+\t\tcp = cp2 + 2;\n+\t}\n+\n+\t/* second pass */\n+\tcp = command;\n+\tp = p2 = xmalloc(len + 1);\n+\twhile (*cp && (cp2 = strchr(cp, '%'))) {\n+\t\tlen = cp2 - cp;\n+\t\tmemcpy(p2, cp, len);\n+\t\tp2 += len;\n+\n+\t\tif (!cp2[1]) {\n+\t\t\t*p2++ = *cp2;\n+\t\t\tbreak;\n+\t\t}\n+\n+\t\tswitch (cp2[1]) {\n+\t\t\tcase 'p':\n+\t\t\t\tlen = strlen(html_dir);\n+\t\t\t\tmemcpy(p2, html_dir, len);\n+\t\t\t\tp2 += len;\n+\n+\t\t\t\tlen = strlen(quoted_git_cmd);\n+\t\t\t\tmemcpy(p2, quoted_git_cmd, len);\n+\t\t\t\tp2 += len;\n+\n+\t\t\t\tmemcpy(p2, \".html\", 5);\n+\t\t\t\tp2 += 5;\n+\t\t\t\tbreak;\n+\n+\t\t\tcase 'f':\n+\t\t\t\tlen = strlen(quoted_git_cmd);\n+\t\t\t\tmemcpy(p2, quoted_git_cmd, len);\n+\t\t\t\tp2 += len;\n+\n+\t\t\t\tmemcpy(p2, \".html\", 5);\n+\t\t\t\tp2 += 5;\n+\t\t\t\tbreak;\n+\n+\t\t\tcase 'b':\n+\t\t\t\tlen = strlen(quoted_git_cmd);\n+\t\t\t\tmemcpy(p2, quoted_git_cmd, len);\n+\t\t\t\tp2 += len;\n+\t\t\t\tbreak;\n+\n+\t\t\tdefault:\n+\t\t\t\t*p2++ = cp2[0];\n+\t\t\t\t*p2++ = cp2[1];\n+\t\t}\n+\t\tcp = cp2+2;\n+\t}\n+\t*p2 = 0;\n+\n+\tfree(quoted_git_cmd);\n+\tif (default_command)\n+\t\tfree(default_command);\n+\n+\tret = system(p);\n+\n+\tif (ret == -1)\n+\t\terror(\"Failed to run %s\", p);\n+\n+\tfree(p);\n+\n+\t/* fallback to man pages */\n+\tif (show_html_help > 1 && (ret == -1 || ret > 0))\n+\t\tshow_man_page(git_cmd);\n+\texit(ret);\n+}\n+\n void help_unknown_cmd(const char *cmd)\n {\n \tprintf(\"git: '%s' is not a git-command\\n\\n\", cmd);\n@@ -214,8 +361,23 @@ int cmd_help(int argc, const char **argv, const char *prefix)\n \t\texit(1);\n \t}\n \n-\telse\n-\t\tshow_man_page(help_cmd);\n+\telse {\n+\t\tgit_config(git_default_config);\n+\t\tif (!strcmp(help_cmd, \"--html\")) {\n+\t\t\thelp_cmd = argc > 2 ? argv[2] : NULL;\n+\t\t\tif (!help_cmd) {\n+\t\t\t\tprintf(\"usage: %s\\n\\n\", git_usage_string);\n+\t\t\t\tlist_common_cmds_help();\n+\t\t\t\texit(1);\n+\t\t\t}\n+\t\t\tshow_html_help = 1;\n+\t\t}\n+\n+\t\tif (show_html_help)\n+\t\t\tshow_html_page(help_cmd);\n+\t\telse\n+\t\t\tshow_man_page(help_cmd);\n+\t}\n \n \treturn 0;\n }\n-- \n1.5.0.7\n"},{"id":"44095","messageId":"20070605193451.GG17377@planck.djpig.de","threadId":"8451","inReplyTo":"20070605183420.GA8450@localhost","subject":"Re: [PATCH 1/2] Show html help with git-help --html","fromName":"Frank Lichtenheld","fromEmail":"frank@lichtenheld.de","sentAt":"2007-06-05T19:34:51Z","receivedAt":"2007-06-05T19:34:51Z","isPatch":true,"sender":{"key":"frank@lichtenheld.de","avatar":"https://gravatar.com/avatar/b9f1d4b120e138f157c9e480d0818197c474628923786adb98f30017cdb99c3c?d=mp&s=160"},"body":"On Tue, Jun 05, 2007 at 06:34:20PM +0000, Nguyen Thai Ngoc Duy wrote:\n> +core.help::\n> +\tIf 'html', it is equivalent to 'git-help' with option --html.\n> +\tIf 'auto', it tries to open html files first. If that attempt fails\n> +\t(the html program does not exist or the program return non-zero\n> +\tvalue), then it will fall back to man pages. If 'man', always use\n> +\tman pages as usual.\n\nThis should mention the default value. (Which is 'man', if I read the\ncode correctly, isn't it?)\n\n> +core.htmlprogram::\n> +\tSpecify the program used to open html help files when 'git-help'\n> +\tis called with option --html or core.help is other than 'man'.\n> +\tBy default, xdg-open will be used.\n> +\tSpecial strings '%p', '%f' and '%b' will be replaced with html\n> +\tfull path, file name and git command (without .html suffix)\n> +\trespectively. If none is given, '%p' will be automatically appended\n> +\tto the command line.\n\nhtmlprogram sounds kludgy to me. I would've used htmlcommand or perhaps\nhtmlviewer. But I'm no native English speaker, so I might be entirely\nwrong about that.\n\nGruesse,\n-- \nFrank Lichtenheld <frank@lichtenheld.de>\nwww: http://www.djpig.de/\n"},{"id":"44096","messageId":"fcaeb9bf0706051242o7ded089dwabd281226befe63d@mail.gmail.com","threadId":"8451","inReplyTo":"20070605193451.GG17377@planck.djpig.de","subject":"Re: [PATCH 1/2] Show html help with git-help --html","fromName":"Nguyen Thai Ngoc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2007-06-05T19:42:13Z","receivedAt":"2007-06-05T19:42:13Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On 6/5/07, Frank Lichtenheld <frank@lichtenheld.de> wrote:\n> On Tue, Jun 05, 2007 at 06:34:20PM +0000, Nguyen Thai Ngoc Duy wrote:\n> > +core.help::\n> > +     If 'html', it is equivalent to 'git-help' with option --html.\n> > +     If 'auto', it tries to open html files first. If that attempt fails\n> > +     (the html program does not exist or the program return non-zero\n> > +     value), then it will fall back to man pages. If 'man', always use\n> > +     man pages as usual.\n>\n> This should mention the default value. (Which is 'man', if I read the\n> code correctly, isn't it?)\n\nOk.\n\n>\n> > +core.htmlprogram::\n> > +     Specify the program used to open html help files when 'git-help'\n> > +     is called with option --html or core.help is other than 'man'.\n> > +     By default, xdg-open will be used.\n> > +     Special strings '%p', '%f' and '%b' will be replaced with html\n> > +     full path, file name and git command (without .html suffix)\n> > +     respectively. If none is given, '%p' will be automatically appended\n> > +     to the command line.\n>\n> htmlprogram sounds kludgy to me. I would've used htmlcommand or perhaps\n> htmlviewer. But I'm no native English speaker, so I might be entirely\n> wrong about that.\n\nWill vote for any better name than htmlprogram as I see it awkward too :)\nhtmlviewer seems better (or htmlviewercommand, which is too long).\nAnyway I'm not a native English speaker either.\n\n>\n> Gruesse,\n> --\n> Frank Lichtenheld <frank@lichtenheld.de>\n> www: http://www.djpig.de/\n>\n\n\n-- \nDuy\n"},{"id":"44102","messageId":"7vzm3e9lui.fsf@assigned-by-dhcp.cox.net","threadId":"8451","inReplyTo":"20070605183420.GA8450@localhost","subject":"Re: [PATCH 1/2] Show html help with git-help --html","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2007-06-05T20:40:05Z","receivedAt":"2007-06-05T20:40:05Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Nguyen Thai Ngoc Duy <pclouds@gmail.com> writes:\n\n> This patch was inspired by ClearCase command 'ct man',...\n\n> diff --git a/help.c b/help.c\n> index 6a9af4d..78b1023 100644\n> --- a/help.c\n> +++ b/help.c\n> @@ -9,6 +9,14 @@\n>  #include \"common-cmds.h\"\n>  #include <sys/ioctl.h>\n>  \n> +#ifndef HTML_DIR\n> +#define HTML_DIR \"/usr/share/html/\"\n> +#endif\n> +\n> +#ifndef HTML_PROGRAM\n> +#define HTML_PROGRAM \"xdg-open\"\n> +#endif\n> +\n>  /* most GUI terminals set COLUMNS (although some don't export it) */\n>  static int term_columns(void)\n>  {\n> @@ -183,6 +191,145 @@ static void show_man_page(const char *git_cmd)\n>  \texeclp(\"man\", \"man\", page, NULL);\n>  }\n>  \n> +static void show_html_page(const char *git_cmd)\n> +{\n> +\tconst char *html_dir;\n> +\tint len, ret, nr_quotes;\n> +\tchar *p, *p2;\n> +\tconst char *cp, *cp2;\n> +\tstruct stat st;\n> +\tchar *quoted_git_cmd;\n> +\tchar *default_command = NULL;\n> +\tconst char *command;\n>...\n> +\t/* append %p if there is not any %x */\n> +\tif (!strchr(command, '%')) {\n> +\t\tlen = strlen(command);\n> +\t\tdefault_command = xmalloc(len + 3 + 1); /* space %p */\n> +\t\tstrcpy(default_command, command);\n> +\t\tstrcat(default_command, \" %p\");\n> +\t\tcommand = default_command;\n> +\t}\n> +\n> +\t/* first pass, calculate command length */\n> ...\n\nHmmm.  Makes one wonder why interpolate is not used....\n"},{"id":"44157","messageId":"fcaeb9bf0706060407k26bfc0baw91c08f907384949b@mail.gmail.com","threadId":"8451","inReplyTo":"7vzm3e9lui.fsf@assigned-by-dhcp.cox.net","subject":"Re: [PATCH 1/2] Show html help with git-help --html","fromName":"Nguyen Thai Ngoc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2007-06-06T11:07:35Z","receivedAt":"2007-06-06T11:07:35Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On 6/5/07, Junio C Hamano <gitster@pobox.com> wrote:\n> Nguyen Thai Ngoc Duy <pclouds@gmail.com> writes:\n>\n> > This patch was inspired by ClearCase command 'ct man',...\n>\n> > diff --git a/help.c b/help.c\n> > index 6a9af4d..78b1023 100644\n> > --- a/help.c\n> > +++ b/help.c\n> > @@ -9,6 +9,14 @@\n> >  #include \"common-cmds.h\"\n> >  #include <sys/ioctl.h>\n> >\n> > +#ifndef HTML_DIR\n> > +#define HTML_DIR \"/usr/share/html/\"\n> > +#endif\n> > +\n> > +#ifndef HTML_PROGRAM\n> > +#define HTML_PROGRAM \"xdg-open\"\n> > +#endif\n> > +\n> >  /* most GUI terminals set COLUMNS (although some don't export it) */\n> >  static int term_columns(void)\n> >  {\n> > @@ -183,6 +191,145 @@ static void show_man_page(const char *git_cmd)\n> >       execlp(\"man\", \"man\", page, NULL);\n> >  }\n> >\n> > +static void show_html_page(const char *git_cmd)\n> > +{\n> > +     const char *html_dir;\n> > +     int len, ret, nr_quotes;\n> > +     char *p, *p2;\n> > +     const char *cp, *cp2;\n> > +     struct stat st;\n> > +     char *quoted_git_cmd;\n> > +     char *default_command = NULL;\n> > +     const char *command;\n> >...\n> > +     /* append %p if there is not any %x */\n> > +     if (!strchr(command, '%')) {\n> > +             len = strlen(command);\n> > +             default_command = xmalloc(len + 3 + 1); /* space %p */\n> > +             strcpy(default_command, command);\n> > +             strcat(default_command, \" %p\");\n> > +             command = default_command;\n> > +     }\n> > +\n> > +     /* first pass, calculate command length */\n> > ...\n>\n> Hmmm.  Makes one wonder why interpolate is not used....\n\nSorry for my ignorant. I have no idea where to use interpolate :( Did\nyou mean to pretend adding %p to command without alloc new buffer\n(default_command) ?\n-- \nDuy\n"},{"id":"44448","messageId":"20070609150322.GB9316@localhost.myhome.westell.com","threadId":"8451","inReplyTo":"20070605183420.GA8450@localhost","subject":"[PATCH 1/2] Show html help with git-help --html","fromName":"Nguyen Thai Ngoc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2007-06-09T15:03:22Z","receivedAt":"2007-06-09T15:03:22Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"This patch was inspired by ClearCase command 'ct man', which\nopens an html help file on Windows. I at first attempted to\nimplement it for MinGW port only but found it so useful that I\nwanted to have it even in Linux.\n\nA new option '--html' is added to git. When git-help is called\nwith --html, it will try to open an html file located at\n$(docdir)/html using xdg-open. HTML files are not installed\nby default so users have to install them manually or have their\ndistributors to do that.\n\nThere are two new config options introduced in this patch. The\nfirst is core.help. It has one of three values: html, auto or\nman. 'html' has the same effect as git-help --html. 'auto'\nwill in addition fall back to man pages if possible. 'man'\nis 'I hate html, give me my man pages'.  The second option is\ncore.htmlviewer, used to specify the program you want to\nopen html files instead of xdg-open. You can override the default\nprogram by appending HTML_VIEWER=blah when calling make.\ncore.htmlviewer can contain %p, %f or %b.  If none is given,\n%p will be appended.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n This patch changes core.htmlprogram to core.htmlviewer and\n mentions 'man' as default value for core.help as suggested\n by Frank.\n It also fixes a bug ignoring the remaining string after the\n last %x in html command\n\n\n Documentation/config.txt |   16 +++++\n Documentation/git.txt    |    5 +-\n Makefile                 |    5 +-\n cache.h                  |    2 +\n config.c                 |   17 +++++\n config.mak.in            |    2 +\n environment.c            |    2 +\n git.c                    |    2 +-\n help.c                   |  161 +++++++++++++++++++++++++++++++++++++++++++++-\n 9 files changed, 207 insertions(+), 5 deletions(-)\n\ndiff --git a/Documentation/config.txt b/Documentation/config.txt\nindex de408b6..2489b8e 100644\n--- a/Documentation/config.txt\n+++ b/Documentation/config.txt\n@@ -262,6 +262,22 @@ core.excludeFile::\n \tof files which are not meant to be tracked.  See\n \tgitlink:gitignore[5].\n \n+core.help::\n+\tIf 'html', it is equivalent to 'git-help' with option --html.\n+\tIf 'auto', it tries to open html files first. If that attempt fails\n+\t(the html program does not exist or the program return non-zero\n+\tvalue), then it will fall back to man pages. If 'man', always use\n+\tman pages as usual. Default is 'man'.\n+\n+core.htmlviewer::\n+\tSpecify the program used to open html help files when 'git-help'\n+\tis called with option --html or core.help is other than 'man'.\n+\tBy default, xdg-open will be used.\n+\tSpecial strings '%p', '%f' and '%b' will be replaced with html\n+\tfull path, file name and git command (without .html suffix)\n+\trespectively. If none is given, '%p' will be automatically appended\n+\tto the command line.\n+\n alias.*::\n \tCommand aliases for the gitlink:git[1] command wrapper - e.g.\n \tafter defining \"alias.last = cat-file commit HEAD\", the invocation\ndiff --git a/Documentation/git.txt b/Documentation/git.txt\nindex ba077c3..f1df402 100644\n--- a/Documentation/git.txt\n+++ b/Documentation/git.txt\n@@ -10,7 +10,7 @@ SYNOPSIS\n --------\n [verse]\n 'git' [--version] [--exec-path[=GIT_EXEC_PATH]] [-p|--paginate]\n-    [--bare] [--git-dir=GIT_DIR] [--help] COMMAND [ARGS]\n+    [--bare] [--git-dir=GIT_DIR] [--help [--html]] COMMAND [ARGS]\n \n DESCRIPTION\n -----------\n@@ -87,6 +87,9 @@ OPTIONS\n \tcommands.  If a git command is named this option will bring up\n \tthe man-page for that command. If the option '--all' or '-a' is\n \tgiven then all available commands are printed.\n+\tIf option '--html' is given, try opening html files instead of\n+\tusing 'man'. The default program to open html files is xdg-open.\n+\tSee 'git-config' to know how to change html program.\n \n --exec-path::\n \tPath to wherever your core git programs are installed.\ndiff --git a/Makefile b/Makefile\nindex 0f75955..7b3180a 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -186,6 +186,7 @@ export TCL_PATH TCLTK_PATH\n # explicitly what architecture to check for. Fix this up for yours..\n SPARSE_FLAGS = -D__BIG_ENDIAN__ -D__powerpc__\n \n+HTML_VIEWER=xdg-open\n \n \n ### --- END CONFIGURATION SECTION ---\n@@ -692,6 +693,7 @@ ETC_GITCONFIG_SQ = $(subst ','\\'',$(ETC_GITCONFIG))\n DESTDIR_SQ = $(subst ','\\'',$(DESTDIR))\n bindir_SQ = $(subst ','\\'',$(bindir))\n gitexecdir_SQ = $(subst ','\\'',$(gitexecdir))\n+html_dir_SQ = $(subst ','\\'',$(html_dir))\n template_dir_SQ = $(subst ','\\'',$(template_dir))\n prefix_SQ = $(subst ','\\'',$(prefix))\n \n@@ -740,7 +742,8 @@ git$X: git.c common-cmds.h $(BUILTIN_OBJS) $(GITLIBS) GIT-CFLAGS\n \t\t$(ALL_CFLAGS) -o $@ $(filter %.c,$^) \\\n \t\t$(BUILTIN_OBJS) $(ALL_LDFLAGS) $(LIBS)\n \n-help.o: common-cmds.h\n+help.o: help.c common-cmds.h\n+\t$(QUIET_CC)$(CC) -o $*.o -c $(ALL_CFLAGS) -DHTML_DIR='\"$(html_dir_SQ)\"' -DHTML_VIEWER='\"$(HTML_VIEWER)\"' $<\n \n git-merge-subtree$X: git-merge-recursive$X\n \t$(QUIET_BUILT_IN)rm -f $@ && ln git-merge-recursive$X $@\ndiff --git a/cache.h b/cache.h\nindex 5e7381e..60e586c 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -288,6 +288,8 @@ extern size_t packed_git_window_size;\n extern size_t packed_git_limit;\n extern size_t delta_base_cache_limit;\n extern int auto_crlf;\n+extern int show_html_help;\n+extern const char *html_help_program;\n \n #define GIT_REPO_VERSION 0\n extern int repository_format_version;\ndiff --git a/config.c b/config.c\nindex 58d3ed5..9d91a06 100644\n--- a/config.c\n+++ b/config.c\n@@ -382,6 +382,23 @@ int git_default_config(const char *var, const char *value)\n \t\treturn 0;\n \t}\n \n+\tif (!strcmp(var, \"core.help\")) {\n+\t\tif (!strcmp(value, \"auto\"))\n+\t\t\tshow_html_help = 2;\n+\t\telse if (!strcmp(value, \"html\"))\n+\t\t\tshow_html_help = 1;\n+\t\telse if (!strcmp(value, \"man\"))\n+\t\t\tshow_html_help = 0;\n+\t\telse\n+\t\t\treturn 1;\n+\t\treturn 0;\n+\t}\n+\n+\tif (!strcmp(var, \"core.htmlviewer\")) {\n+\t\thtml_help_program = xstrdup(value);\n+\t\treturn 0;\n+\t}\n+\n \t/* Add other config variables here and to Documentation/config.txt. */\n \treturn 0;\n }\ndiff --git a/config.mak.in b/config.mak.in\nindex a3032e3..c3e410d 100644\n--- a/config.mak.in\n+++ b/config.mak.in\n@@ -8,12 +8,14 @@ TAR = @TAR@\n #INSTALL = @INSTALL@\t\t# needs install-sh or install.sh in sources\n TCLTK_PATH = @TCLTK_PATH@\n \n+PACKAGE_TARNAME=@PACKAGE_TARNAME@\n prefix = @prefix@\n exec_prefix = @exec_prefix@\n bindir = @bindir@\n #gitexecdir = @libexecdir@/git-core/\n datarootdir = @datarootdir@\n template_dir = @datadir@/git-core/templates/\n+html_dir = @docdir@/html/\n \n mandir=@mandir@\n \ndiff --git a/environment.c b/environment.c\nindex 8b9b89d..aa22c68 100644\n--- a/environment.c\n+++ b/environment.c\n@@ -32,6 +32,8 @@ size_t delta_base_cache_limit = 16 * 1024 * 1024;\n int pager_in_use;\n int pager_use_color = 1;\n int auto_crlf = 0;\t/* 1: both ways, -1: only when adding git objects */\n+int show_html_help = 0;\n+const char *html_help_program = NULL;\n \n static const char *git_dir;\n static char *git_object_dir, *git_index_file, *git_refs_dir, *git_graft_file;\ndiff --git a/git.c b/git.c\nindex 29b55a1..c3c0fe8 100644\n--- a/git.c\n+++ b/git.c\n@@ -4,7 +4,7 @@\n #include \"quote.h\"\n \n const char git_usage_string[] =\n-\t\"git [--version] [--exec-path[=GIT_EXEC_PATH]] [-p|--paginate] [--bare] [--git-dir=GIT_DIR] [--help] COMMAND [ARGS]\";\n+\t\"git [--version] [--exec-path[=GIT_EXEC_PATH]] [-p|--paginate] [--bare] [--git-dir=GIT_DIR] [--help [--html]] COMMAND [ARGS]\";\n \n static void prepend_to_path(const char *dir, int len)\n {\ndiff --git a/help.c b/help.c\nindex 1cd33ec..52474e9 100644\n--- a/help.c\n+++ b/help.c\n@@ -9,6 +9,14 @@\n #include \"common-cmds.h\"\n #include <sys/ioctl.h>\n \n+#ifndef HTML_DIR\n+#define HTML_DIR \"/usr/share/html/\"\n+#endif\n+\n+#ifndef HTML_VIEWER\n+#define HTML_VIEWER \"xdg-open\"\n+#endif\n+\n /* most GUI terminals set COLUMNS (although some don't export it) */\n static int term_columns(void)\n {\n@@ -183,6 +191,140 @@ static void show_man_page(const char *git_cmd)\n \texeclp(\"man\", \"man\", page, NULL);\n }\n \n+static void show_html_page(const char *git_cmd)\n+{\n+\tconst char *html_dir;\n+\tint len, ret, nr_quotes;\n+\tchar *p, *p2;\n+\tconst char *cp, *cp2;\n+\tstruct stat st;\n+\tchar *quoted_git_cmd;\n+\tconst char *command;\n+\n+\thtml_dir = HTML_DIR;\n+\tcommand = html_help_program ? html_help_program : HTML_VIEWER;\n+\n+\tnr_quotes = 0;\n+\tfor (cp = git_cmd; *cp; cp++)\n+\t\tif (*cp == '\\'') nr_quotes ++;\n+\n+\tlen = strlen(git_cmd) + nr_quotes*2 + 2 + 4; /* two quotes and git- */\n+\tquoted_git_cmd = p2 = xmalloc(len + 1);\n+\t*p2++ = '\\'';\n+\tif (prefixcmp(git_cmd, \"git\")) {\n+\t\tstrcpy(p2,\"git-\");\n+\t\tp2 += 4;\n+\t}\n+\tfor (cp = git_cmd; *cp; cp ++) {\n+\t\tif (*cp == '\\'')\n+\t\t\t*p2++ = '\\\\';\n+\t\t*p2++ = *cp;\n+\t}\n+\t*p2++ = '\\'';\n+\t*p2 = 0;\n+\n+\t/* first pass, calculate command length */\n+\tcp = command;\n+\tlen = 0;\n+\twhile (*cp && (cp2 = strchr(cp, '%'))) {\n+\t\tlen += cp2 - cp;\n+\n+\t\tif (!cp2[1]) break;\n+\n+\t\tswitch (cp2[1]) {\n+\t\t\tcase 'p':\n+\t\t\t\tlen += strlen(html_dir) + strlen(quoted_git_cmd) + 5; /* .html */\n+\t\t\t\tbreak;\n+\n+\t\t\tcase 'f':\n+\t\t\t\tlen += strlen(quoted_git_cmd) + 5; /* .html */\n+\t\t\t\tbreak;\n+\n+\t\t\tcase 'b':\n+\t\t\t\tlen += strlen(quoted_git_cmd);\n+\t\t\t\tbreak;\n+\n+\t\t\tdefault:\n+\t\t\t\tlen += 2;\n+\t\t}\n+\t\tcp = cp2 + 2;\n+\t}\n+\tif (!len) /* no expansion, append %p */\n+\t\tlen += 1 + strlen(html_dir) + strlen(quoted_git_cmd) + 5;\n+\tif (*cp)\n+\t\tlen += strlen(cp);\n+\n+\t/* second pass */\n+\tcp = command;\n+\tp = p2 = xmalloc(len + 1);\n+\twhile (*cp && (cp2 = strchr(cp, '%'))) {\n+\t\tlen = cp2 - cp;\n+\t\tmemcpy(p2, cp, len);\n+\t\tp2 += len;\n+\n+\t\tif (!cp2[1]) break;\n+\n+\t\tswitch (cp2[1]) {\n+\t\t\tcase 'p':\n+\t\t\t\tlen = strlen(html_dir);\n+\t\t\t\tmemcpy(p2, html_dir, len);\n+\t\t\t\tp2 += len;\n+\n+\t\t\t\tlen = strlen(quoted_git_cmd);\n+\t\t\t\tmemcpy(p2, quoted_git_cmd, len);\n+\t\t\t\tp2 += len;\n+\n+\t\t\t\tmemcpy(p2, \".html\", 5);\n+\t\t\t\tp2 += 5;\n+\t\t\t\tbreak;\n+\n+\t\t\tcase 'f':\n+\t\t\t\tlen = strlen(quoted_git_cmd);\n+\t\t\t\tmemcpy(p2, quoted_git_cmd, len);\n+\t\t\t\tp2 += len;\n+\n+\t\t\t\tmemcpy(p2, \".html\", 5);\n+\t\t\t\tp2 += 5;\n+\t\t\t\tbreak;\n+\n+\t\t\tcase 'b':\n+\t\t\t\tlen = strlen(quoted_git_cmd);\n+\t\t\t\tmemcpy(p2, quoted_git_cmd, len);\n+\t\t\t\tp2 += len;\n+\t\t\t\tbreak;\n+\n+\t\t\tdefault:\n+\t\t\t\t*p2++ = cp2[0];\n+\t\t\t\t*p2++ = cp2[1];\n+\t\t}\n+\t\tcp = cp2+2;\n+\t}\n+\tif (*cp)\n+\t\tstrcpy(p2,cp);\n+\telse\n+\t\t*p2 = 0;\n+\tif (p == p2) { /* no expansion, append %p */\n+\t\tstrcat(p2, \" \");\n+\t\tstrcat(p2, html_dir);\n+\t\tstrcat(p2, quoted_git_cmd);\n+\t\tstrcat(p2, \".html\");\n+\t}\n+\n+\tfree(quoted_git_cmd);\n+\n+\tret = system(p);\n+\n+\tif (ret == -1)\n+\t\terror(\"Failed to run %s\", p);\n+\n+\tfree(p);\n+\n+\t/* fallback to man pages */\n+\tif (show_html_help > 1 && (ret == -1 || ret > 0))\n+\t\tshow_man_page(git_cmd);\n+\texit(ret);\n+}\n+\n void help_unknown_cmd(const char *cmd)\n {\n \tprintf(\"git: '%s' is not a git-command\\n\\n\", cmd);\n@@ -214,8 +356,23 @@ int cmd_help(int argc, const char **argv, const char *prefix)\n \t\texit(1);\n \t}\n \n-\telse\n-\t\tshow_man_page(help_cmd);\n+\telse {\n+\t\tgit_config(git_default_config);\n+\t\tif (!strcmp(help_cmd, \"--html\")) {\n+\t\t\thelp_cmd = argc > 2 ? argv[2] : NULL;\n+\t\t\tif (!help_cmd) {\n+\t\t\t\tprintf(\"usage: %s\\n\\n\", git_usage_string);\n+\t\t\t\tlist_common_cmds_help();\n+\t\t\t\texit(1);\n+\t\t\t}\n+\t\t\tshow_html_help = 1;\n+\t\t}\n+\n+\t\tif (show_html_help)\n+\t\t\tshow_html_page(help_cmd);\n+\t\telse\n+\t\t\tshow_man_page(help_cmd);\n+\t}\n \n \treturn 0;\n }\n-- \n1.5.2\n"}]}