@matheusbsbs / matheus-bagliano / commits / 5d6491c7c7
Fork 0

git-help: add -w|--web option to display html man page in a browser.

Now when using "git help -w cmd", we will try to show the HTML man page "git-cmd.html" in your prefered web browser. To do that "help.c" code will call a new shell script "git-browse-help". This currently works only if the HTML versions of the man page have been installed in $(htmldir) (typically "/usr/share/doc/git-doc"), so new target to do that is added to "Documentation/Makefile". The browser to use can be configured using the "web.browser" config variable. We try to open a new tab in an existing web browser, if possible. The code in "git-browse-help" is heavily stolen from "git-mergetool" by Theodore Y. Ts'o. Thanks. Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Christian Couder committed Dec 2, 2007 at 06:07 UTC 5d6491c7c7536ab930a6e9ce2ec3b5249d4c283f
4 files changed +171 -17
Documentation/Makefile
+4
@@ -29,6 +29,7 @@ DOC_MAN7=$(patsubst %.txt,%.7,$(MAN7_TXT))
29
30 prefix?=$(HOME)
31 bindir?=$(prefix)/bin
32 +htmldir?=$(prefix)/share/doc/git-doc
33 mandir?=$(prefix)/share/man
34 man1dir=$(mandir)/man1
35 man5dir=$(mandir)/man5
@@ -95,6 +96,9 @@ install-info: info
96 echo "No directory found in $(DESTDIR)$(infodir)" >&2 ; \
97 fi
98
99 +install-html: html
100 + sh ./install-webdoc.sh $(DESTDIR)$(htmldir)
101 +
102 ../GIT-VERSION-FILE: .FORCE-GIT-VERSION-FILE
103 $(MAKE) -C ../ GIT-VERSION-FILE
104
Makefile
+6 -2
@@ -157,6 +157,7 @@ bindir = $(prefix)/bin
157 gitexecdir = $(bindir)
158 sharedir = $(prefix)/share
159 template_dir = $(sharedir)/git-core/templates
160 +htmldir=$(sharedir)/doc/git-doc
161 ifeq ($(prefix),/usr)
162 sysconfdir = /etc
163 else
@@ -183,7 +184,7 @@ GITWEB_FAVICON = git-favicon.png
184 GITWEB_SITE_HEADER =
185 GITWEB_SITE_FOOTER =
186
186 -export prefix bindir gitexecdir sharedir template_dir sysconfdir
187 +export prefix bindir gitexecdir sharedir template_dir htmldir sysconfdir
188
189 CC = gcc
190 AR = ar
@@ -223,7 +224,8 @@ SCRIPT_SH = \
224 git-merge-resolve.sh \
225 git-lost-found.sh git-quiltimport.sh git-submodule.sh \
226 git-filter-branch.sh \
226 - git-stash.sh
227 + git-stash.sh \
228 + git-browse-help.sh
229
230 SCRIPT_PERL = \
231 git-add--interactive.perl \
@@ -745,6 +747,7 @@ DESTDIR_SQ = $(subst ','\'',$(DESTDIR))
747 bindir_SQ = $(subst ','\'',$(bindir))
748 gitexecdir_SQ = $(subst ','\'',$(gitexecdir))
749 template_dir_SQ = $(subst ','\'',$(template_dir))
750 +htmldir_SQ = $(subst ','\'',$(htmldir))
751 prefix_SQ = $(subst ','\'',$(prefix))
752
753 SHELL_PATH_SQ = $(subst ','\'',$(SHELL_PATH))
@@ -808,6 +811,7 @@ $(patsubst %.sh,%,$(SCRIPT_SH)) : % : %.sh
811 -e 's|@@PERL@@|$(PERL_PATH_SQ)|g' \
812 -e 's/@@GIT_VERSION@@/$(GIT_VERSION)/g' \
813 -e 's/@@NO_CURL@@/$(NO_CURL)/g' \
814 + -e 's|@@HTMLDIR@@|$(htmldir_SQ)|g' \
815 $@.sh >$@+ && \
816 chmod +x $@+ && \
817 mv $@+ $@
git-browse-help.sh new
+142
@@ -0,0 +1,142 @@
1 +#!/bin/sh
2 +#
3 +# This program launch a web browser on the html page
4 +# describing a git command.
5 +#
6 +# Copyright (c) 2007 Christian Couder
7 +# Copyright (c) 2006 Theodore Y. Ts'o
8 +#
9 +# This file is heavily stolen from git-mergetool.sh, by
10 +# Theodore Y. Ts'o (thanks) that is:
11 +#
12 +# Copyright (c) 2006 Theodore Y. Ts'o
13 +#
14 +# This file is licensed under the GPL v2, or a later version
15 +# at the discretion of Junio C Hamano or any other official
16 +# git maintainer.
17 +#
18 +
19 +USAGE='[--browser=browser|--tool=browser] [cmd to display] ...'
20 +SUBDIRECTORY_OK=Yes
21 +OPTIONS_SPEC=
22 +. git-sh-setup
23 +
24 +# Install data.
25 +html_dir="@@HTMLDIR@@"
26 +
27 +test -f "$html_dir/git.html" || die "No documentation directory found."
28 +
29 +valid_tool() {
30 + case "$1" in
31 + firefox | iceweasel | konqueror | w3m | links | lynx | dillo)
32 + ;; # happy
33 + *)
34 + return 1
35 + ;;
36 + esac
37 +}
38 +
39 +init_browser_path() {
40 + browser_path=`git config browser.$1.path`
41 + test -z "$browser_path" && browser_path=$1
42 +}
43 +
44 +while test $# != 0
45 +do
46 + case "$1" in
47 + -b|--browser*|-t|--tool*)
48 + case "$#,$1" in
49 + *,*=*)
50 + browser=`expr "z$1" : 'z-[^=]*=\(.*\)'`
51 + ;;
52 + 1,*)
53 + usage ;;
54 + *)
55 + browser="$2"
56 + shift ;;
57 + esac
58 + ;;
59 + --)
60 + break
61 + ;;
62 + -*)
63 + usage
64 + ;;
65 + *)
66 + break
67 + ;;
68 + esac
69 + shift
70 +done
71 +
72 +if test -z "$browser"; then
73 + browser=`git config web.browser`
74 + if test -n "$browser" && ! valid_tool "$browser"; then
75 + echo >&2 "git config option web.browser set to unknown browser: $browser"
76 + echo >&2 "Resetting to default..."
77 + unset browser
78 + fi
79 +fi
80 +
81 +if test -z "$browser" ; then
82 + if test -n "$DISPLAY"; then
83 + browser_candidates="firefox iceweasel konqueror w3m links lynx dillo"
84 + if test "$KDE_FULL_SESSION" = "true"; then
85 + browser_candidates="konqueror $browser_candidates"
86 + fi
87 + else
88 + browser_candidates="w3m links lynx"
89 + fi
90 + echo "browser candidates: $browser_candidates"
91 + for i in $browser_candidates; do
92 + init_browser_path $i
93 + if type "$browser_path" > /dev/null 2>&1; then
94 + browser=$i
95 + break
96 + fi
97 + done
98 + test -z "$browser" && die "No known browser available."
99 +else
100 + valid_tool "$browser" || die "Unknown browser '$browser'."
101 +
102 + init_browser_path "$browser"
103 +
104 + if ! type "$browser_path" > /dev/null 2>&1; then
105 + die "The browser $browser is not available as '$browser_path'."
106 + fi
107 +fi
108 +
109 +pages=$(for p in "$@"; do echo "$html_dir/$p.html" ; done)
110 +test -z "$pages" && pages="$html_dir/git.html"
111 +
112 +case "$browser" in
113 + firefox|iceweasel)
114 + # Check version because firefox < 2.0 does not support "-new-tab".
115 + vers=$(expr "$($browser_path -version)" : '.* \([0-9][0-9]*\)\..*')
116 + NEWTAB='-new-tab'
117 + test "$vers" -lt 2 && NEWTAB=''
118 + nohup "$browser_path" $NEWTAB $pages &
119 + ;;
120 + konqueror)
121 + case "$(basename "$browser_path")" in
122 + konqueror)
123 + # It's simpler to use kfmclient to open a new tab in konqueror.
124 + browser_path="$(echo "$browser_path" | sed -e 's/konqueror$/kfmclient/')"
125 + type "$browser_path" > /dev/null 2>&1 || die "No '$browser_path' found."
126 + eval "$browser_path" newTab $pages
127 + ;;
128 + kfmclient)
129 + eval "$browser_path" newTab $pages
130 + ;;
131 + *)
132 + nohup "$browser_path" $pages &
133 + ;;
134 + esac
135 + ;;
136 + w3m|links|lynx)
137 + eval "$browser_path" $pages
138 + ;;
139 + dillo)
140 + nohup "$browser_path" $pages &
141 + ;;
142 +esac
help.c
+19 -15
@@ -241,7 +241,9 @@ void list_common_cmds_help(void)
241
242 static const char *cmd_to_page(const char *git_cmd)
243 {
244 - if (!prefixcmp(git_cmd, "git"))
244 + if (!git_cmd)
245 + return "git";
246 + else if (!prefixcmp(git_cmd, "git"))
247 return git_cmd;
248 else {
249 int page_len = strlen(git_cmd) + 4;
@@ -265,6 +267,12 @@ static void show_info_page(const char *git_cmd)
267 execlp("info", "info", page, NULL);
268 }
269
270 +static void show_html_page(const char *git_cmd)
271 +{
272 + const char *page = cmd_to_page(git_cmd);
273 + execl_git_cmd("browse-help", page, NULL);
274 +}
275 +
276 void help_unknown_cmd(const char *cmd)
277 {
278 fprintf(stderr, "git: '%s' is not a git-command. See 'git --help'.\n", cmd);
@@ -277,31 +285,27 @@ int cmd_version(int argc, const char **argv, const char *prefix)
285 return 0;
286 }
287
280 -static void check_help_cmd(const char *help_cmd)
288 +int cmd_help(int argc, const char **argv, const char *prefix)
289 {
282 - if (!help_cmd) {
290 + const char *help_cmd = argv[1];
291 +
292 + if (argc < 2) {
293 printf("usage: %s\n\n", git_usage_string);
294 list_common_cmds_help();
295 exit(0);
296 }
297
288 - else if (!strcmp(help_cmd, "--all") || !strcmp(help_cmd, "-a")) {
298 + if (!strcmp(help_cmd, "--all") || !strcmp(help_cmd, "-a")) {
299 printf("usage: %s\n\n", git_usage_string);
300 list_commands();
291 - exit(0);
301 }
293 -}
294 -
295 -int cmd_help(int argc, const char **argv, const char *prefix)
296 -{
297 - const char *help_cmd = argc > 1 ? argv[1] : NULL;
298 - check_help_cmd(help_cmd);
302
300 - if (!strcmp(help_cmd, "--info") || !strcmp(help_cmd, "-i")) {
301 - help_cmd = argc > 2 ? argv[2] : NULL;
302 - check_help_cmd(help_cmd);
303 + else if (!strcmp(help_cmd, "--web") || !strcmp(help_cmd, "-w")) {
304 + show_html_page(argc > 2 ? argv[2] : NULL);
305 + }
306
304 - show_info_page(help_cmd);
307 + else if (!strcmp(help_cmd, "--info") || !strcmp(help_cmd, "-i")) {
308 + show_info_page(argc > 2 ? argv[2] : NULL);
309 }
310
311 else