help--browse: add '--config' option to check a config option for a browser.
[git/trast.git] / git-help--browse.sh
blob8ed489d8827f974b9a11ca5789a9fe3df9c9866b
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] [--config=conf.var] url/file ...'
21 # This must be capable of running outside of git directory, so
22 # the vanilla git-sh-setup should not be used.
23 NONGIT_OK=Yes
24 . git-sh-setup
26 valid_tool() {
27 case "$1" in
28 firefox | iceweasel | konqueror | w3m | links | lynx | dillo)
29 ;; # happy
31 return 1
33 esac
36 init_browser_path() {
37 browser_path=`git config browser.$1.path`
38 test -z "$browser_path" && browser_path=$1
41 while test $# != 0
43 case "$1" in
44 -b|--browser*|-t|--tool*)
45 case "$#,$1" in
46 *,*=*)
47 browser=`expr "z$1" : 'z-[^=]*=\(.*\)'`
49 1,*)
50 usage ;;
52 browser="$2"
53 shift ;;
54 esac
56 -c|--config*)
57 case "$#,$1" in
58 *,*=*)
59 conf=`expr "z$1" : 'z-[^=]*=\(.*\)'`
61 1,*)
62 usage ;;
64 conf="$2"
65 shift ;;
66 esac
68 --)
69 break
71 -*)
72 usage
75 break
77 esac
78 shift
79 done
81 test $# = 0 && usage
83 if test -z "$browser"
84 then
85 for opt in "$conf" "web.browser"
87 test -z "$opt" && continue
88 browser="`git config $opt`"
89 test -z "$browser" || break
90 done
91 if test -n "$browser" && ! valid_tool "$browser"; then
92 echo >&2 "git config option $opt set to unknown browser: $browser"
93 echo >&2 "Resetting to default..."
94 unset browser
98 if test -z "$browser" ; then
99 if test -n "$DISPLAY"; then
100 browser_candidates="firefox iceweasel konqueror w3m links lynx dillo"
101 if test "$KDE_FULL_SESSION" = "true"; then
102 browser_candidates="konqueror $browser_candidates"
104 else
105 browser_candidates="w3m links lynx"
108 for i in $browser_candidates; do
109 init_browser_path $i
110 if type "$browser_path" > /dev/null 2>&1; then
111 browser=$i
112 break
114 done
115 test -z "$browser" && die "No known browser available."
116 else
117 valid_tool "$browser" || die "Unknown browser '$browser'."
119 init_browser_path "$browser"
121 if ! type "$browser_path" > /dev/null 2>&1; then
122 die "The browser $browser is not available as '$browser_path'."
126 case "$browser" in
127 firefox|iceweasel)
128 # Check version because firefox < 2.0 does not support "-new-tab".
129 vers=$(expr "$($browser_path -version)" : '.* \([0-9][0-9]*\)\..*')
130 NEWTAB='-new-tab'
131 test "$vers" -lt 2 && NEWTAB=''
132 nohup "$browser_path" $NEWTAB "$@" &
134 konqueror)
135 case "$(basename "$browser_path")" in
136 konqueror)
137 # It's simpler to use kfmclient to open a new tab in konqueror.
138 browser_path="$(echo "$browser_path" | sed -e 's/konqueror$/kfmclient/')"
139 type "$browser_path" > /dev/null 2>&1 || die "No '$browser_path' found."
140 eval "$browser_path" newTab "$@"
142 kfmclient)
143 eval "$browser_path" newTab "$@"
146 nohup "$browser_path" "$@" &
148 esac
150 w3m|links|lynx)
151 eval "$browser_path" "$@"
153 dillo)
154 nohup "$browser_path" "$@" &
156 esac