]> git.ipfire.org Git - thirdparty/git.git/blob - git-web--browse.sh
web--browse: support opera, seamonkey and elinks
[thirdparty/git.git] / git-web--browse.sh
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] [--config=conf.var] url/file ...'
20
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
25
26 valid_custom_tool()
27 {
28 browser_cmd="$(git config "browser.$1.cmd")"
29 test -n "$browser_cmd"
30 }
31
32 valid_tool() {
33 case "$1" in
34 firefox | iceweasel | seamonkey | iceape | chrome | google-chrome | chromium | \
35 konqueror | opera | w3m | elinks | links | lynx | dillo | open | start)
36 ;; # happy
37 *)
38 valid_custom_tool "$1" || return 1
39 ;;
40 esac
41 }
42
43 init_browser_path() {
44 browser_path=$(git config "browser.$1.path")
45 test -z "$browser_path" && browser_path="$1"
46 }
47
48 while test $# != 0
49 do
50 case "$1" in
51 -b|--browser*|-t|--tool*)
52 case "$#,$1" in
53 *,*=*)
54 browser=`expr "z$1" : 'z-[^=]*=\(.*\)'`
55 ;;
56 1,*)
57 usage ;;
58 *)
59 browser="$2"
60 shift ;;
61 esac
62 ;;
63 -c|--config*)
64 case "$#,$1" in
65 *,*=*)
66 conf=`expr "z$1" : 'z-[^=]*=\(.*\)'`
67 ;;
68 1,*)
69 usage ;;
70 *)
71 conf="$2"
72 shift ;;
73 esac
74 ;;
75 --)
76 break
77 ;;
78 -*)
79 usage
80 ;;
81 *)
82 break
83 ;;
84 esac
85 shift
86 done
87
88 test $# = 0 && usage
89
90 if test -z "$browser"
91 then
92 for opt in "$conf" "web.browser"
93 do
94 test -z "$opt" && continue
95 browser="`git config $opt`"
96 test -z "$browser" || break
97 done
98 if test -n "$browser" && ! valid_tool "$browser"; then
99 echo >&2 "git config option $opt set to unknown browser: $browser"
100 echo >&2 "Resetting to default..."
101 unset browser
102 fi
103 fi
104
105 if test -z "$browser" ; then
106 if test -n "$DISPLAY"; then
107 browser_candidates="firefox iceweasel google-chrome chrome chromium konqueror opera seamonkey iceape w3m elinks links lynx dillo"
108 if test "$KDE_FULL_SESSION" = "true"; then
109 browser_candidates="konqueror $browser_candidates"
110 fi
111 else
112 browser_candidates="w3m elinks links lynx"
113 fi
114 # SECURITYSESSIONID indicates an OS X GUI login session
115 if test -n "$SECURITYSESSIONID" \
116 -o "$TERM_PROGRAM" = "Apple_Terminal" ; then
117 browser_candidates="open $browser_candidates"
118 fi
119 # /bin/start indicates MinGW
120 if test -x /bin/start; then
121 browser_candidates="start $browser_candidates"
122 fi
123
124 for i in $browser_candidates; do
125 init_browser_path $i
126 if type "$browser_path" > /dev/null 2>&1; then
127 browser=$i
128 break
129 fi
130 done
131 test -z "$browser" && die "No known browser available."
132 else
133 valid_tool "$browser" || die "Unknown browser '$browser'."
134
135 init_browser_path "$browser"
136
137 if test -z "$browser_cmd" && ! type "$browser_path" > /dev/null 2>&1; then
138 die "The browser $browser is not available as '$browser_path'."
139 fi
140 fi
141
142 case "$browser" in
143 firefox|iceweasel|seamonkey|iceape)
144 # Check version because firefox < 2.0 does not support "-new-tab".
145 vers=$(expr "$($browser_path -version)" : '.* \([0-9][0-9]*\)\..*')
146 NEWTAB='-new-tab'
147 test "$vers" -lt 2 && NEWTAB=''
148 "$browser_path" $NEWTAB "$@" &
149 ;;
150 google-chrome|chrome|chromium)
151 # Actual command for chromium is chromium-browser.
152 # No need to specify newTab. It's default in chromium
153 eval "$browser_path" "$@" &
154 ;;
155 konqueror)
156 case "$(basename "$browser_path")" in
157 konqueror)
158 # It's simpler to use kfmclient to open a new tab in konqueror.
159 browser_path="$(echo "$browser_path" | sed -e 's/konqueror$/kfmclient/')"
160 type "$browser_path" > /dev/null 2>&1 || die "No '$browser_path' found."
161 eval "$browser_path" newTab "$@"
162 ;;
163 kfmclient)
164 eval "$browser_path" newTab "$@"
165 ;;
166 *)
167 "$browser_path" "$@" &
168 ;;
169 esac
170 ;;
171 w3m|elinks|links|lynx|open)
172 eval "$browser_path" "$@"
173 ;;
174 start)
175 exec "$browser_path" '"web-browse"' "$@"
176 ;;
177 opera|dillo)
178 "$browser_path" "$@" &
179 ;;
180 *)
181 if test -n "$browser_cmd"; then
182 ( eval $browser_cmd "$@" )
183 fi
184 ;;
185 esac