web--browse: support opera, seamonkey and elinks
[git/git.git] / git-web--browse.sh
CommitLineData
5d6491c7
CC
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
caa87713 19USAGE='[--browser=browser|--tool=browser] [--config=conf.var] url/file ...'
22c90717
JH
20
21# This must be capable of running outside of git directory, so
22# the vanilla git-sh-setup should not be used.
23NONGIT_OK=Yes
5d6491c7
CC
24. git-sh-setup
25
77e21533
CC
26valid_custom_tool()
27{
28 browser_cmd="$(git config "browser.$1.cmd")"
29 test -n "$browser_cmd"
30}
31
5d6491c7
CC
32valid_tool() {
33 case "$1" in
81f42f11
GB
34 firefox | iceweasel | seamonkey | iceape | chrome | google-chrome | chromium | \
35 konqueror | opera | w3m | elinks | links | lynx | dillo | open | start)
a180055a
GB
36 ;; # happy
37 *)
38 valid_custom_tool "$1" || return 1
39 ;;
5d6491c7
CC
40 esac
41}
42
43init_browser_path() {
8e086899 44 browser_path=$(git config "browser.$1.path")
193ad4f6 45 test -z "$browser_path" && browser_path="$1"
5d6491c7
CC
46}
47
48while test $# != 0
49do
a180055a 50 case "$1" in
5d6491c7 51 -b|--browser*|-t|--tool*)
a180055a 52 case "$#,$1" in
5d6491c7 53 *,*=*)
a180055a
GB
54 browser=`expr "z$1" : 'z-[^=]*=\(.*\)'`
55 ;;
5d6491c7 56 1,*)
a180055a 57 usage ;;
5d6491c7 58 *)
a180055a
GB
59 browser="$2"
60 shift ;;
61 esac
62 ;;
caa87713 63 -c|--config*)
a180055a 64 case "$#,$1" in
caa87713 65 *,*=*)
a180055a
GB
66 conf=`expr "z$1" : 'z-[^=]*=\(.*\)'`
67 ;;
caa87713 68 1,*)
a180055a 69 usage ;;
caa87713 70 *)
a180055a
GB
71 conf="$2"
72 shift ;;
73 esac
74 ;;
5d6491c7 75 --)
a180055a
GB
76 break
77 ;;
5d6491c7 78 -*)
a180055a
GB
79 usage
80 ;;
5d6491c7 81 *)
a180055a
GB
82 break
83 ;;
84 esac
85 shift
5d6491c7
CC
86done
87
482cce82
CC
88test $# = 0 && usage
89
70087cdb 90if test -z "$browser"
22c90717 91then
a180055a
GB
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
5d6491c7
CC
103fi
104
105if test -z "$browser" ; then
a180055a 106 if test -n "$DISPLAY"; then
81f42f11 107 browser_candidates="firefox iceweasel google-chrome chrome chromium konqueror opera seamonkey iceape w3m elinks links lynx dillo"
a180055a
GB
108 if test "$KDE_FULL_SESSION" = "true"; then
109 browser_candidates="konqueror $browser_candidates"
110 fi
111 else
81f42f11 112 browser_candidates="w3m elinks links lynx"
5d6491c7 113 fi
a180055a
GB
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"
5d6491c7 118 fi
a180055a
GB
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."
5d6491c7 132else
a180055a 133 valid_tool "$browser" || die "Unknown browser '$browser'."
5d6491c7 134
a180055a 135 init_browser_path "$browser"
5d6491c7 136
a180055a
GB
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
5d6491c7
CC
140fi
141
5d6491c7 142case "$browser" in
81f42f11 143firefox|iceweasel|seamonkey|iceape)
5d6491c7
CC
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=''
a0685a4f 148 "$browser_path" $NEWTAB "$@" &
5d6491c7 149 ;;
a180055a 150google-chrome|chrome|chromium)
0b3261b8
PKS
151 # Actual command for chromium is chromium-browser.
152 # No need to specify newTab. It's default in chromium
153 eval "$browser_path" "$@" &
154 ;;
a180055a 155konqueror)
5d6491c7 156 case "$(basename "$browser_path")" in
a180055a 157 konqueror)
5d6491c7
CC
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."
482cce82 161 eval "$browser_path" newTab "$@"
5d6491c7 162 ;;
a180055a 163 kfmclient)
482cce82 164 eval "$browser_path" newTab "$@"
5d6491c7 165 ;;
a180055a 166 *)
a0685a4f 167 "$browser_path" "$@" &
5d6491c7
CC
168 ;;
169 esac
170 ;;
81f42f11 171w3m|elinks|links|lynx|open)
482cce82 172 eval "$browser_path" "$@"
5d6491c7 173 ;;
a180055a
GB
174start)
175 exec "$browser_path" '"web-browse"' "$@"
176 ;;
81f42f11 177opera|dillo)
a0685a4f 178 "$browser_path" "$@" &
5d6491c7 179 ;;
a180055a 180*)
77e21533 181 if test -n "$browser_cmd"; then
a180055a 182 ( eval $browser_cmd "$@" )
77e21533
CC
183 fi
184 ;;
5d6491c7 185esac