blob: 9f446798d473cee7dedc82d5d2c820cb0967b375 [file] [log] [blame]
Christian Couder5d6491c2007-12-02 06:07:55 +01001#!/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
Christian Coudercaa87712008-02-02 07:32:41 +010019USAGE='[--browser=browser|--tool=browser] [--config=conf.var] url/file ...'
Junio C Hamano22c90712007-12-12 17:34:30 -080020
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
Christian Couder5d6491c2007-12-02 06:07:55 +010024. git-sh-setup
25
Christian Couder77e21532008-03-14 05:56:49 +010026valid_custom_tool()
27{
28 browser_cmd="$(git config "browser.$1.cmd")"
29 test -n "$browser_cmd"
30}
31
Christian Couder5d6491c2007-12-02 06:07:55 +010032valid_tool() {
33 case "$1" in
Giuseppe Bilotta14ea67c2010-12-03 17:47:39 +010034 firefox | iceweasel | seamonkey | iceape | \
35 chrome | google-chrome | chromium | chromium-browser |\
Giuseppe Bilotta81f42f12010-12-03 17:47:38 +010036 konqueror | opera | w3m | elinks | links | lynx | dillo | open | start)
Giuseppe Bilottaa1800552010-12-03 17:47:36 +010037 ;; # happy
38 *)
39 valid_custom_tool "$1" || return 1
40 ;;
Christian Couder5d6491c2007-12-02 06:07:55 +010041 esac
42}
43
44init_browser_path() {
jaysoffian+git@gmail.com8e086892008-02-11 10:57:34 -050045 browser_path=$(git config "browser.$1.path")
Giuseppe Bilotta14ea67c2010-12-03 17:47:39 +010046 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"}
Christian Couder5d6491c2007-12-02 06:07:55 +010053}
54
55while test $# != 0
56do
Giuseppe Bilottaa1800552010-12-03 17:47:36 +010057 case "$1" in
Christian Couder5d6491c2007-12-02 06:07:55 +010058 -b|--browser*|-t|--tool*)
Giuseppe Bilottaa1800552010-12-03 17:47:36 +010059 case "$#,$1" in
Christian Couder5d6491c2007-12-02 06:07:55 +010060 *,*=*)
Giuseppe Bilottaa1800552010-12-03 17:47:36 +010061 browser=`expr "z$1" : 'z-[^=]*=\(.*\)'`
62 ;;
Christian Couder5d6491c2007-12-02 06:07:55 +010063 1,*)
Giuseppe Bilottaa1800552010-12-03 17:47:36 +010064 usage ;;
Christian Couder5d6491c2007-12-02 06:07:55 +010065 *)
Giuseppe Bilottaa1800552010-12-03 17:47:36 +010066 browser="$2"
67 shift ;;
68 esac
69 ;;
Christian Coudercaa87712008-02-02 07:32:41 +010070 -c|--config*)
Giuseppe Bilottaa1800552010-12-03 17:47:36 +010071 case "$#,$1" in
Christian Coudercaa87712008-02-02 07:32:41 +010072 *,*=*)
Giuseppe Bilottaa1800552010-12-03 17:47:36 +010073 conf=`expr "z$1" : 'z-[^=]*=\(.*\)'`
74 ;;
Christian Coudercaa87712008-02-02 07:32:41 +010075 1,*)
Giuseppe Bilottaa1800552010-12-03 17:47:36 +010076 usage ;;
Christian Coudercaa87712008-02-02 07:32:41 +010077 *)
Giuseppe Bilottaa1800552010-12-03 17:47:36 +010078 conf="$2"
79 shift ;;
80 esac
81 ;;
Christian Couder5d6491c2007-12-02 06:07:55 +010082 --)
Giuseppe Bilottaa1800552010-12-03 17:47:36 +010083 break
84 ;;
Christian Couder5d6491c2007-12-02 06:07:55 +010085 -*)
Giuseppe Bilottaa1800552010-12-03 17:47:36 +010086 usage
87 ;;
Christian Couder5d6491c2007-12-02 06:07:55 +010088 *)
Giuseppe Bilottaa1800552010-12-03 17:47:36 +010089 break
90 ;;
91 esac
92 shift
Christian Couder5d6491c2007-12-02 06:07:55 +010093done
94
Christian Couder482cce82008-02-02 07:32:33 +010095test $# = 0 && usage
96
Christian Couder70087cd2007-12-15 05:57:28 +010097if test -z "$browser"
Junio C Hamano22c90712007-12-12 17:34:30 -080098then
Giuseppe Bilottaa1800552010-12-03 17:47:36 +010099 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
Christian Couder5d6491c2007-12-02 06:07:55 +0100110fi
111
112if test -z "$browser" ; then
Giuseppe Bilottaa1800552010-12-03 17:47:36 +0100113 if test -n "$DISPLAY"; then
Giuseppe Bilotta14ea67c2010-12-03 17:47:39 +0100114 browser_candidates="firefox iceweasel google-chrome chrome chromium chromium-browser konqueror opera seamonkey iceape w3m elinks links lynx dillo"
Giuseppe Bilottaa1800552010-12-03 17:47:36 +0100115 if test "$KDE_FULL_SESSION" = "true"; then
116 browser_candidates="konqueror $browser_candidates"
117 fi
118 else
Giuseppe Bilotta81f42f12010-12-03 17:47:38 +0100119 browser_candidates="w3m elinks links lynx"
Christian Couder5d6491c2007-12-02 06:07:55 +0100120 fi
Giuseppe Bilottaa1800552010-12-03 17:47:36 +0100121 # SECURITYSESSIONID indicates an OS X GUI login session
John Szakmeisterbe537e42013-03-25 06:13:18 -0400122 if test -n "$SECURITYSESSIONID" || test -n "$TERM_PROGRAM"
123 then
Giuseppe Bilottaa1800552010-12-03 17:47:36 +0100124 browser_candidates="open $browser_candidates"
125 fi
126 # /bin/start indicates MinGW
127 if test -x /bin/start; then
128 browser_candidates="start $browser_candidates"
129 fi
Junio C Hamano22c90712007-12-12 17:34:30 -0800130
Giuseppe Bilottaa1800552010-12-03 17:47:36 +0100131 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."
Christian Couder5d6491c2007-12-02 06:07:55 +0100139else
Giuseppe Bilottaa1800552010-12-03 17:47:36 +0100140 valid_tool "$browser" || die "Unknown browser '$browser'."
Christian Couder5d6491c2007-12-02 06:07:55 +0100141
Giuseppe Bilottaa1800552010-12-03 17:47:36 +0100142 init_browser_path "$browser"
Christian Couder5d6491c2007-12-02 06:07:55 +0100143
Giuseppe Bilottaa1800552010-12-03 17:47:36 +0100144 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
Christian Couder5d6491c2007-12-02 06:07:55 +0100147fi
148
Christian Couder5d6491c2007-12-02 06:07:55 +0100149case "$browser" in
Giuseppe Bilotta81f42f12010-12-03 17:47:38 +0100150firefox|iceweasel|seamonkey|iceape)
Christian Couder5d6491c2007-12-02 06:07:55 +0100151 # 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=''
Dmitry Potapova0685a42008-02-09 23:22:22 -0800155 "$browser_path" $NEWTAB "$@" &
Christian Couder5d6491c2007-12-02 06:07:55 +0100156 ;;
Giuseppe Bilotta14ea67c2010-12-03 17:47:39 +0100157google-chrome|chrome|chromium|chromium-browser)
Pavan Kumar Sunkara0b3261b2010-05-30 00:06:51 +0530158 # No need to specify newTab. It's default in chromium
Chris Packham480f0622011-10-02 13:44:17 +1300159 "$browser_path" "$@" &
Pavan Kumar Sunkara0b3261b2010-05-30 00:06:51 +0530160 ;;
Giuseppe Bilottaa1800552010-12-03 17:47:36 +0100161konqueror)
Christian Couder5d6491c2007-12-02 06:07:55 +0100162 case "$(basename "$browser_path")" in
Giuseppe Bilottaa1800552010-12-03 17:47:36 +0100163 konqueror)
Christian Couder5d6491c2007-12-02 06:07:55 +0100164 # 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."
Chris Packham480f0622011-10-02 13:44:17 +1300167 "$browser_path" newTab "$@" &
Christian Couder5d6491c2007-12-02 06:07:55 +0100168 ;;
Giuseppe Bilottaa1800552010-12-03 17:47:36 +0100169 kfmclient)
Chris Packham480f0622011-10-02 13:44:17 +1300170 "$browser_path" newTab "$@" &
Christian Couder5d6491c2007-12-02 06:07:55 +0100171 ;;
Giuseppe Bilottaa1800552010-12-03 17:47:36 +0100172 *)
Dmitry Potapova0685a42008-02-09 23:22:22 -0800173 "$browser_path" "$@" &
Christian Couder5d6491c2007-12-02 06:07:55 +0100174 ;;
175 esac
176 ;;
Giuseppe Bilotta81f42f12010-12-03 17:47:38 +0100177w3m|elinks|links|lynx|open)
Chris Packham480f0622011-10-02 13:44:17 +1300178 "$browser_path" "$@"
Christian Couder5d6491c2007-12-02 06:07:55 +0100179 ;;
Giuseppe Bilottaa1800552010-12-03 17:47:36 +0100180start)
181 exec "$browser_path" '"web-browse"' "$@"
182 ;;
Giuseppe Bilotta81f42f12010-12-03 17:47:38 +0100183opera|dillo)
Dmitry Potapova0685a42008-02-09 23:22:22 -0800184 "$browser_path" "$@" &
Christian Couder5d6491c2007-12-02 06:07:55 +0100185 ;;
Giuseppe Bilottaa1800552010-12-03 17:47:36 +0100186*)
Christian Couder77e21532008-03-14 05:56:49 +0100187 if test -n "$browser_cmd"; then
Chris Packham480f0622011-10-02 13:44:17 +1300188 ( eval "$browser_cmd \"\$@\"" )
Christian Couder77e21532008-03-14 05:56:49 +0100189 fi
190 ;;
Christian Couder5d6491c2007-12-02 06:07:55 +0100191esac