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