git-help: add -w|--web option to display html man page in a browser.
[git/vmiklos.git] / git-browse-help.sh
blob76eff01bab707f76a147160e55430868ae3dce97
1 #!/bin/sh
3 # This program launch a web browser on the html page
4 # describing a git command.
6 # Copyright (c) 2007 Christian Couder
7 # Copyright (c) 2006 Theodore Y. Ts'o
9 # This file is heavily stolen from git-mergetool.sh, by
10 # Theodore Y. Ts'o (thanks) that is:
12 # Copyright (c) 2006 Theodore Y. Ts'o
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.
19 USAGE='[--browser=browser|--tool=browser] [cmd to display] ...'
20 SUBDIRECTORY_OK=Yes
21 OPTIONS_SPEC=
22 . git-sh-setup
24 # Install data.
25 html_dir="@@HTMLDIR@@"
27 test -f "$html_dir/git.html" || die "No documentation directory found."
29 valid_tool() {
30 case "$1" in
31 firefox | iceweasel | konqueror | w3m | links | lynx | dillo)
32 ;; # happy
34 return 1
36 esac
39 init_browser_path() {
40 browser_path=`git config browser.$1.path`
41 test -z "$browser_path" && browser_path=$1
44 while test $# != 0
46 case "$1" in
47 -b|--browser*|-t|--tool*)
48 case "$#,$1" in
49 *,*=*)
50 browser=`expr "z$1" : 'z-[^=]*=\(.*\)'`
52 1,*)
53 usage ;;
55 browser="$2"
56 shift ;;
57 esac
59 --)
60 break
62 -*)
63 usage
66 break
68 esac
69 shift
70 done
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
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"
87 else
88 browser_candidates="w3m links lynx"
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
97 done
98 test -z "$browser" && die "No known browser available."
99 else
100 valid_tool "$browser" || die "Unknown browser '$browser'."
102 init_browser_path "$browser"
104 if ! type "$browser_path" > /dev/null 2>&1; then
105 die "The browser $browser is not available as '$browser_path'."
109 pages=$(for p in "$@"; do echo "$html_dir/$p.html" ; done)
110 test -z "$pages" && pages="$html_dir/git.html"
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 &
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
128 kfmclient)
129 eval "$browser_path" newTab $pages
132 nohup "$browser_path" $pages &
134 esac
136 w3m|links|lynx)
137 eval "$browser_path" $pages
139 dillo)
140 nohup "$browser_path" $pages &
142 esac