Merge branch 'mr/autoconf-fread'
[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
5d6491c7
CC
26valid_tool() {
27 case "$1" in
969702a9 28 firefox | iceweasel | konqueror | w3m | links | lynx | dillo | open)
5d6491c7
CC
29 ;; # happy
30 *)
31 return 1
32 ;;
33 esac
34}
35
36init_browser_path() {
8e086899 37 browser_path=$(git config "browser.$1.path")
193ad4f6 38 test -z "$browser_path" && browser_path="$1"
5d6491c7
CC
39}
40
41while test $# != 0
42do
43 case "$1" in
44 -b|--browser*|-t|--tool*)
45 case "$#,$1" in
46 *,*=*)
47 browser=`expr "z$1" : 'z-[^=]*=\(.*\)'`
48 ;;
49 1,*)
50 usage ;;
51 *)
52 browser="$2"
53 shift ;;
54 esac
55 ;;
caa87713
CC
56 -c|--config*)
57 case "$#,$1" in
58 *,*=*)
59 conf=`expr "z$1" : 'z-[^=]*=\(.*\)'`
60 ;;
61 1,*)
62 usage ;;
63 *)
64 conf="$2"
65 shift ;;
66 esac
67 ;;
5d6491c7
CC
68 --)
69 break
70 ;;
71 -*)
72 usage
73 ;;
74 *)
75 break
76 ;;
77 esac
78 shift
79done
80
482cce82
CC
81test $# = 0 && usage
82
70087cdb 83if test -z "$browser"
22c90717 84then
caa87713 85 for opt in "$conf" "web.browser"
d3a866bc 86 do
caa87713 87 test -z "$opt" && continue
d3a866bc
CC
88 browser="`git config $opt`"
89 test -z "$browser" || break
90 done
5d6491c7 91 if test -n "$browser" && ! valid_tool "$browser"; then
caa87713
CC
92 echo >&2 "git config option $opt set to unknown browser: $browser"
93 echo >&2 "Resetting to default..."
94 unset browser
5d6491c7
CC
95 fi
96fi
97
98if test -z "$browser" ; then
99 if test -n "$DISPLAY"; then
100 browser_candidates="firefox iceweasel konqueror w3m links lynx dillo"
101 if test "$KDE_FULL_SESSION" = "true"; then
102 browser_candidates="konqueror $browser_candidates"
103 fi
104 else
105 browser_candidates="w3m links lynx"
106 fi
969702a9
JS
107 # SECURITYSESSIONID indicates an OS X GUI login session
108 if test -n "$SECURITYSESSIONID"; then
109 browser_candidates="open $browser_candidates"
110 fi
22c90717 111
5d6491c7
CC
112 for i in $browser_candidates; do
113 init_browser_path $i
114 if type "$browser_path" > /dev/null 2>&1; then
115 browser=$i
116 break
117 fi
118 done
119 test -z "$browser" && die "No known browser available."
120else
121 valid_tool "$browser" || die "Unknown browser '$browser'."
122
123 init_browser_path "$browser"
124
125 if ! type "$browser_path" > /dev/null 2>&1; then
126 die "The browser $browser is not available as '$browser_path'."
127 fi
128fi
129
5d6491c7
CC
130case "$browser" in
131 firefox|iceweasel)
132 # Check version because firefox < 2.0 does not support "-new-tab".
133 vers=$(expr "$($browser_path -version)" : '.* \([0-9][0-9]*\)\..*')
134 NEWTAB='-new-tab'
135 test "$vers" -lt 2 && NEWTAB=''
a0685a4f 136 "$browser_path" $NEWTAB "$@" &
5d6491c7
CC
137 ;;
138 konqueror)
139 case "$(basename "$browser_path")" in
140 konqueror)
141 # It's simpler to use kfmclient to open a new tab in konqueror.
142 browser_path="$(echo "$browser_path" | sed -e 's/konqueror$/kfmclient/')"
143 type "$browser_path" > /dev/null 2>&1 || die "No '$browser_path' found."
482cce82 144 eval "$browser_path" newTab "$@"
5d6491c7
CC
145 ;;
146 kfmclient)
482cce82 147 eval "$browser_path" newTab "$@"
5d6491c7
CC
148 ;;
149 *)
a0685a4f 150 "$browser_path" "$@" &
5d6491c7
CC
151 ;;
152 esac
153 ;;
969702a9 154 w3m|links|lynx|open)
482cce82 155 eval "$browser_path" "$@"
5d6491c7
CC
156 ;;
157 dillo)
a0685a4f 158 "$browser_path" "$@" &
5d6491c7
CC
159 ;;
160esac