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