Christian Couder | 5d6491c | 2007-12-02 06:07:55 +0100 | [diff] [blame] | 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 | |
Christian Couder | caa8771 | 2008-02-02 07:32:41 +0100 | [diff] [blame] | 19 | USAGE='[--browser=browser|--tool=browser] [--config=conf.var] url/file ...' |
Junio C Hamano | 22c9071 | 2007-12-12 17:34:30 -0800 | [diff] [blame] | 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 |
Christian Couder | 5d6491c | 2007-12-02 06:07:55 +0100 | [diff] [blame] | 24 | . git-sh-setup |
| 25 | |
Christian Couder | 77e2153 | 2008-03-14 05:56:49 +0100 | [diff] [blame] | 26 | valid_custom_tool() |
| 27 | { |
| 28 | browser_cmd="$(git config "browser.$1.cmd")" |
| 29 | test -n "$browser_cmd" |
| 30 | } |
| 31 | |
Christian Couder | 5d6491c | 2007-12-02 06:07:55 +0100 | [diff] [blame] | 32 | valid_tool() { |
| 33 | case "$1" in |
Giuseppe Bilotta | 14ea67c | 2010-12-03 17:47:39 +0100 | [diff] [blame] | 34 | firefox | iceweasel | seamonkey | iceape | \ |
Yaakov Selkowitz | cc2f6b6 | 2013-06-21 02:24:32 -0500 | [diff] [blame] | 35 | chrome | google-chrome | chromium | chromium-browser | \ |
| 36 | konqueror | opera | w3m | elinks | links | lynx | dillo | open | \ |
Rüdiger Sonderfeld | 6ca0f80 | 2013-10-26 19:43:54 +0200 | [diff] [blame] | 37 | start | cygstart | xdg-open) |
Giuseppe Bilotta | a180055 | 2010-12-03 17:47:36 +0100 | [diff] [blame] | 38 | ;; # happy |
| 39 | *) |
| 40 | valid_custom_tool "$1" || return 1 |
| 41 | ;; |
Christian Couder | 5d6491c | 2007-12-02 06:07:55 +0100 | [diff] [blame] | 42 | esac |
| 43 | } |
| 44 | |
| 45 | init_browser_path() { |
jaysoffian+git@gmail.com | 8e08689 | 2008-02-11 10:57:34 -0500 | [diff] [blame] | 46 | browser_path=$(git config "browser.$1.path") |
Giuseppe Bilotta | 14ea67c | 2010-12-03 17:47:39 +0100 | [diff] [blame] | 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"} |
Christian Couder | 5d6491c | 2007-12-02 06:07:55 +0100 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | while test $# != 0 |
| 57 | do |
Giuseppe Bilotta | a180055 | 2010-12-03 17:47:36 +0100 | [diff] [blame] | 58 | case "$1" in |
Christian Couder | 5d6491c | 2007-12-02 06:07:55 +0100 | [diff] [blame] | 59 | -b|--browser*|-t|--tool*) |
Giuseppe Bilotta | a180055 | 2010-12-03 17:47:36 +0100 | [diff] [blame] | 60 | case "$#,$1" in |
Christian Couder | 5d6491c | 2007-12-02 06:07:55 +0100 | [diff] [blame] | 61 | *,*=*) |
Elia Pinto | 9e5878f | 2014-04-23 06:44:02 -0700 | [diff] [blame] | 62 | browser=$(expr "z$1" : 'z-[^=]*=\(.*\)') |
Giuseppe Bilotta | a180055 | 2010-12-03 17:47:36 +0100 | [diff] [blame] | 63 | ;; |
Christian Couder | 5d6491c | 2007-12-02 06:07:55 +0100 | [diff] [blame] | 64 | 1,*) |
Giuseppe Bilotta | a180055 | 2010-12-03 17:47:36 +0100 | [diff] [blame] | 65 | usage ;; |
Christian Couder | 5d6491c | 2007-12-02 06:07:55 +0100 | [diff] [blame] | 66 | *) |
Giuseppe Bilotta | a180055 | 2010-12-03 17:47:36 +0100 | [diff] [blame] | 67 | browser="$2" |
| 68 | shift ;; |
| 69 | esac |
| 70 | ;; |
Christian Couder | caa8771 | 2008-02-02 07:32:41 +0100 | [diff] [blame] | 71 | -c|--config*) |
Giuseppe Bilotta | a180055 | 2010-12-03 17:47:36 +0100 | [diff] [blame] | 72 | case "$#,$1" in |
Christian Couder | caa8771 | 2008-02-02 07:32:41 +0100 | [diff] [blame] | 73 | *,*=*) |
Elia Pinto | 9e5878f | 2014-04-23 06:44:02 -0700 | [diff] [blame] | 74 | conf=$(expr "z$1" : 'z-[^=]*=\(.*\)') |
Giuseppe Bilotta | a180055 | 2010-12-03 17:47:36 +0100 | [diff] [blame] | 75 | ;; |
Christian Couder | caa8771 | 2008-02-02 07:32:41 +0100 | [diff] [blame] | 76 | 1,*) |
Giuseppe Bilotta | a180055 | 2010-12-03 17:47:36 +0100 | [diff] [blame] | 77 | usage ;; |
Christian Couder | caa8771 | 2008-02-02 07:32:41 +0100 | [diff] [blame] | 78 | *) |
Giuseppe Bilotta | a180055 | 2010-12-03 17:47:36 +0100 | [diff] [blame] | 79 | conf="$2" |
| 80 | shift ;; |
| 81 | esac |
| 82 | ;; |
Christian Couder | 5d6491c | 2007-12-02 06:07:55 +0100 | [diff] [blame] | 83 | --) |
Giuseppe Bilotta | a180055 | 2010-12-03 17:47:36 +0100 | [diff] [blame] | 84 | break |
| 85 | ;; |
Christian Couder | 5d6491c | 2007-12-02 06:07:55 +0100 | [diff] [blame] | 86 | -*) |
Giuseppe Bilotta | a180055 | 2010-12-03 17:47:36 +0100 | [diff] [blame] | 87 | usage |
| 88 | ;; |
Christian Couder | 5d6491c | 2007-12-02 06:07:55 +0100 | [diff] [blame] | 89 | *) |
Giuseppe Bilotta | a180055 | 2010-12-03 17:47:36 +0100 | [diff] [blame] | 90 | break |
| 91 | ;; |
| 92 | esac |
| 93 | shift |
Christian Couder | 5d6491c | 2007-12-02 06:07:55 +0100 | [diff] [blame] | 94 | done |
| 95 | |
Christian Couder | 482cce8 | 2008-02-02 07:32:33 +0100 | [diff] [blame] | 96 | test $# = 0 && usage |
| 97 | |
Christian Couder | 70087cd | 2007-12-15 05:57:28 +0100 | [diff] [blame] | 98 | if test -z "$browser" |
Junio C Hamano | 22c9071 | 2007-12-12 17:34:30 -0800 | [diff] [blame] | 99 | then |
Giuseppe Bilotta | a180055 | 2010-12-03 17:47:36 +0100 | [diff] [blame] | 100 | for opt in "$conf" "web.browser" |
| 101 | do |
| 102 | test -z "$opt" && continue |
Elia Pinto | 9e5878f | 2014-04-23 06:44:02 -0700 | [diff] [blame] | 103 | browser="$(git config $opt)" |
Giuseppe Bilotta | a180055 | 2010-12-03 17:47:36 +0100 | [diff] [blame] | 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 |
Christian Couder | 5d6491c | 2007-12-02 06:07:55 +0100 | [diff] [blame] | 111 | fi |
| 112 | |
| 113 | if test -z "$browser" ; then |
Giuseppe Bilotta | a180055 | 2010-12-03 17:47:36 +0100 | [diff] [blame] | 114 | if test -n "$DISPLAY"; then |
Rüdiger Sonderfeld | 6ca0f80 | 2013-10-26 19:43:54 +0200 | [diff] [blame] | 115 | browser_candidates="firefox iceweasel google-chrome chrome chromium chromium-browser konqueror opera seamonkey iceape w3m elinks links lynx dillo xdg-open" |
Giuseppe Bilotta | a180055 | 2010-12-03 17:47:36 +0100 | [diff] [blame] | 116 | if test "$KDE_FULL_SESSION" = "true"; then |
| 117 | browser_candidates="konqueror $browser_candidates" |
| 118 | fi |
| 119 | else |
Giuseppe Bilotta | 81f42f1 | 2010-12-03 17:47:38 +0100 | [diff] [blame] | 120 | browser_candidates="w3m elinks links lynx" |
Christian Couder | 5d6491c | 2007-12-02 06:07:55 +0100 | [diff] [blame] | 121 | fi |
Giuseppe Bilotta | a180055 | 2010-12-03 17:47:36 +0100 | [diff] [blame] | 122 | # SECURITYSESSIONID indicates an OS X GUI login session |
John Szakmeister | be537e4 | 2013-03-25 06:13:18 -0400 | [diff] [blame] | 123 | if test -n "$SECURITYSESSIONID" || test -n "$TERM_PROGRAM" |
| 124 | then |
Giuseppe Bilotta | a180055 | 2010-12-03 17:47:36 +0100 | [diff] [blame] | 125 | browser_candidates="open $browser_candidates" |
| 126 | fi |
| 127 | # /bin/start indicates MinGW |
| 128 | if test -x /bin/start; then |
| 129 | browser_candidates="start $browser_candidates" |
| 130 | fi |
Yaakov Selkowitz | cc2f6b6 | 2013-06-21 02:24:32 -0500 | [diff] [blame] | 131 | # /usr/bin/cygstart indicates Cygwin |
| 132 | if test -x /usr/bin/cygstart; then |
| 133 | browser_candidates="cygstart $browser_candidates" |
| 134 | fi |
Junio C Hamano | 22c9071 | 2007-12-12 17:34:30 -0800 | [diff] [blame] | 135 | |
Giuseppe Bilotta | a180055 | 2010-12-03 17:47:36 +0100 | [diff] [blame] | 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." |
Christian Couder | 5d6491c | 2007-12-02 06:07:55 +0100 | [diff] [blame] | 144 | else |
Giuseppe Bilotta | a180055 | 2010-12-03 17:47:36 +0100 | [diff] [blame] | 145 | valid_tool "$browser" || die "Unknown browser '$browser'." |
Christian Couder | 5d6491c | 2007-12-02 06:07:55 +0100 | [diff] [blame] | 146 | |
Giuseppe Bilotta | a180055 | 2010-12-03 17:47:36 +0100 | [diff] [blame] | 147 | init_browser_path "$browser" |
Christian Couder | 5d6491c | 2007-12-02 06:07:55 +0100 | [diff] [blame] | 148 | |
Giuseppe Bilotta | a180055 | 2010-12-03 17:47:36 +0100 | [diff] [blame] | 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 |
Christian Couder | 5d6491c | 2007-12-02 06:07:55 +0100 | [diff] [blame] | 152 | fi |
| 153 | |
Christian Couder | 5d6491c | 2007-12-02 06:07:55 +0100 | [diff] [blame] | 154 | case "$browser" in |
Giuseppe Bilotta | 81f42f1 | 2010-12-03 17:47:38 +0100 | [diff] [blame] | 155 | firefox|iceweasel|seamonkey|iceape) |
Christian Couder | 5d6491c | 2007-12-02 06:07:55 +0100 | [diff] [blame] | 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='' |
Dmitry Potapov | a0685a4 | 2008-02-09 23:22:22 -0800 | [diff] [blame] | 160 | "$browser_path" $NEWTAB "$@" & |
Christian Couder | 5d6491c | 2007-12-02 06:07:55 +0100 | [diff] [blame] | 161 | ;; |
Giuseppe Bilotta | 14ea67c | 2010-12-03 17:47:39 +0100 | [diff] [blame] | 162 | google-chrome|chrome|chromium|chromium-browser) |
Pavan Kumar Sunkara | 0b3261b | 2010-05-30 00:06:51 +0530 | [diff] [blame] | 163 | # No need to specify newTab. It's default in chromium |
Chris Packham | 480f062 | 2011-10-02 13:44:17 +1300 | [diff] [blame] | 164 | "$browser_path" "$@" & |
Pavan Kumar Sunkara | 0b3261b | 2010-05-30 00:06:51 +0530 | [diff] [blame] | 165 | ;; |
Giuseppe Bilotta | a180055 | 2010-12-03 17:47:36 +0100 | [diff] [blame] | 166 | konqueror) |
Christian Couder | 5d6491c | 2007-12-02 06:07:55 +0100 | [diff] [blame] | 167 | case "$(basename "$browser_path")" in |
Giuseppe Bilotta | a180055 | 2010-12-03 17:47:36 +0100 | [diff] [blame] | 168 | konqueror) |
Christian Couder | 5d6491c | 2007-12-02 06:07:55 +0100 | [diff] [blame] | 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." |
Chris Packham | 480f062 | 2011-10-02 13:44:17 +1300 | [diff] [blame] | 172 | "$browser_path" newTab "$@" & |
Christian Couder | 5d6491c | 2007-12-02 06:07:55 +0100 | [diff] [blame] | 173 | ;; |
Giuseppe Bilotta | a180055 | 2010-12-03 17:47:36 +0100 | [diff] [blame] | 174 | kfmclient) |
Chris Packham | 480f062 | 2011-10-02 13:44:17 +1300 | [diff] [blame] | 175 | "$browser_path" newTab "$@" & |
Christian Couder | 5d6491c | 2007-12-02 06:07:55 +0100 | [diff] [blame] | 176 | ;; |
Giuseppe Bilotta | a180055 | 2010-12-03 17:47:36 +0100 | [diff] [blame] | 177 | *) |
Dmitry Potapov | a0685a4 | 2008-02-09 23:22:22 -0800 | [diff] [blame] | 178 | "$browser_path" "$@" & |
Christian Couder | 5d6491c | 2007-12-02 06:07:55 +0100 | [diff] [blame] | 179 | ;; |
| 180 | esac |
| 181 | ;; |
Rüdiger Sonderfeld | 6ca0f80 | 2013-10-26 19:43:54 +0200 | [diff] [blame] | 182 | w3m|elinks|links|lynx|open|cygstart|xdg-open) |
Chris Packham | 480f062 | 2011-10-02 13:44:17 +1300 | [diff] [blame] | 183 | "$browser_path" "$@" |
Christian Couder | 5d6491c | 2007-12-02 06:07:55 +0100 | [diff] [blame] | 184 | ;; |
Giuseppe Bilotta | a180055 | 2010-12-03 17:47:36 +0100 | [diff] [blame] | 185 | start) |
| 186 | exec "$browser_path" '"web-browse"' "$@" |
| 187 | ;; |
Giuseppe Bilotta | 81f42f1 | 2010-12-03 17:47:38 +0100 | [diff] [blame] | 188 | opera|dillo) |
Dmitry Potapov | a0685a4 | 2008-02-09 23:22:22 -0800 | [diff] [blame] | 189 | "$browser_path" "$@" & |
Christian Couder | 5d6491c | 2007-12-02 06:07:55 +0100 | [diff] [blame] | 190 | ;; |
Giuseppe Bilotta | a180055 | 2010-12-03 17:47:36 +0100 | [diff] [blame] | 191 | *) |
Christian Couder | 77e2153 | 2008-03-14 05:56:49 +0100 | [diff] [blame] | 192 | if test -n "$browser_cmd"; then |
Chris Packham | 480f062 | 2011-10-02 13:44:17 +1300 | [diff] [blame] | 193 | ( eval "$browser_cmd \"\$@\"" ) |
Christian Couder | 77e2153 | 2008-03-14 05:56:49 +0100 | [diff] [blame] | 194 | fi |
| 195 | ;; |
Christian Couder | 5d6491c | 2007-12-02 06:07:55 +0100 | [diff] [blame] | 196 | esac |