Rename 'git-help--browse.sh' to 'git-web--browse.sh'.
[git/git.git] / git-instaweb.sh
CommitLineData
a51d37c1
EW
1#!/bin/sh
2#
3# Copyright (c) 2006 Eric Wong
4#
c2db2e0e 5
c5699693 6PERL='@@PERL@@'
c2db2e0e
PH
7OPTIONS_KEEPDASHDASH=
8OPTIONS_SPEC="\
9git-instaweb [options] (--start | --stop | --restart)
10--
11l,local only bind on 127.0.0.1
12p,port= the port to bind to
13d,httpd= the command to launch
14b,browser= the browser to launch
15m,module-path= the module path (only needed for apache2)
16 Action
17stop stop the web server
18start start the web server
19restart restart the web server
20"
a51d37c1
EW
21
22. git-sh-setup
23
b2bc9a30 24fqgitdir="$GIT_DIR"
e0d10e1c
TP
25local="`git config --bool --get instaweb.local`"
26httpd="`git config --get instaweb.httpd`"
27browser="`git config --get instaweb.browser`"
d3a866bc 28test -z "$browser" && browser="`git config --get web.browser`"
e0d10e1c
TP
29port=`git config --get instaweb.port`
30module_path="`git config --get instaweb.modulepath`"
a51d37c1 31
9126425d 32conf="$GIT_DIR/gitweb/httpd.conf"
a51d37c1
EW
33
34# Defaults:
35
82e5a82f 36# if installed, it doesn't need further configuration (module_path)
a51d37c1
EW
37test -z "$httpd" && httpd='lighttpd -f'
38
39# probably the most popular browser among gitweb users
40test -z "$browser" && browser='firefox'
41
42# any untaken local port will do...
43test -z "$port" && port=1234
44
45start_httpd () {
46 httpd_only="`echo $httpd | cut -f1 -d' '`"
f5f30699 47 if case "$httpd_only" in /*) : ;; *) which $httpd_only >/dev/null;; esac
a51d37c1 48 then
9126425d 49 $httpd "$fqgitdir/gitweb/httpd.conf"
a51d37c1
EW
50 else
51 # many httpds are installed in /usr/sbin or /usr/local/sbin
52 # these days and those are not in most users $PATHs
14b45b66
MD
53 # in addition, we may have generated a server script
54 # in $fqgitdir/gitweb.
55 for i in /usr/local/sbin /usr/sbin "$fqgitdir/gitweb"
a51d37c1
EW
56 do
57 if test -x "$i/$httpd_only"
58 then
59 # don't quote $httpd, there can be
60 # arguments to it (-f)
61 $i/$httpd "$fqgitdir/gitweb/httpd.conf"
62 return
63 fi
64 done
f281e3a1
FK
65 echo "$httpd_only not found. Install $httpd_only or use" \
66 "--httpd to specify another http daemon."
67 exit 1
a51d37c1 68 fi
5209eda8
JS
69 if test $? != 0; then
70 echo "Could not execute http daemon $httpd."
71 exit 1
72 fi
a51d37c1
EW
73}
74
75stop_httpd () {
76 test -f "$fqgitdir/pid" && kill `cat "$fqgitdir/pid"`
77}
78
822f7c73 79while test $# != 0
a51d37c1
EW
80do
81 case "$1" in
82 --stop|stop)
83 stop_httpd
84 exit 0
85 ;;
86 --start|start)
87 start_httpd
88 exit 0
89 ;;
90 --restart|restart)
91 stop_httpd
92 start_httpd
93 exit 0
94 ;;
c2db2e0e 95 -l|--local)
a51d37c1
EW
96 local=true
97 ;;
c2db2e0e
PH
98 -d|--httpd)
99 shift
100 httpd="$1"
101 ;;
102 -b|--browser)
103 shift
104 browser="$1"
a51d37c1 105 ;;
c2db2e0e
PH
106 -p|--port)
107 shift
108 port="$1"
a51d37c1 109 ;;
c2db2e0e
PH
110 -m|--module-path)
111 shift
112 module_path="$1"
a51d37c1 113 ;;
c2db2e0e 114 --)
a51d37c1
EW
115 ;;
116 *)
117 usage
118 ;;
119 esac
120 shift
121done
122
123mkdir -p "$GIT_DIR/gitweb/tmp"
124GIT_EXEC_PATH="`git --exec-path`"
125GIT_DIR="$fqgitdir"
126export GIT_EXEC_PATH GIT_DIR
127
128
425b78e8
MD
129webrick_conf () {
130 # generate a standalone server script in $fqgitdir/gitweb.
131 cat >"$fqgitdir/gitweb/$httpd.rb" <<EOF
132require 'webrick'
133require 'yaml'
134options = YAML::load_file(ARGV[0])
135options[:StartCallback] = proc do
136 File.open(options[:PidFile],"w") do |f|
137 f.puts Process.pid
138 end
139end
140options[:ServerType] = WEBrick::Daemon
141server = WEBrick::HTTPServer.new(options)
142['INT', 'TERM'].each do |signal|
143 trap(signal) {server.shutdown}
144end
145server.start
146EOF
147 # generate a shell script to invoke the above ruby script,
148 # which assumes _ruby_ is in the user's $PATH. that's _one_
149 # portable way to run ruby, which could be installed anywhere,
150 # really.
151 cat >"$fqgitdir/gitweb/$httpd" <<EOF
152#!/bin/sh
153exec ruby "$fqgitdir/gitweb/$httpd.rb" \$*
154EOF
155 chmod +x "$fqgitdir/gitweb/$httpd"
156
157 cat >"$conf" <<EOF
158:Port: $port
159:DocumentRoot: "$fqgitdir/gitweb"
160:DirectoryIndex: ["gitweb.cgi"]
161:PidFile: "$fqgitdir/pid"
162EOF
163 test "$local" = true && echo ':BindAddress: "127.0.0.1"' >> "$conf"
164}
165
a51d37c1
EW
166lighttpd_conf () {
167 cat > "$conf" <<EOF
168server.document-root = "$fqgitdir/gitweb"
169server.port = $port
170server.modules = ( "mod_cgi" )
171server.indexfiles = ( "gitweb.cgi" )
172server.pid-file = "$fqgitdir/pid"
173cgi.assign = ( ".cgi" => "" )
174mimetype.assign = ( ".css" => "text/css" )
175EOF
9126425d 176 test x"$local" = xtrue && echo 'server.bind = "127.0.0.1"' >> "$conf"
a51d37c1
EW
177}
178
179apache2_conf () {
180 test -z "$module_path" && module_path=/usr/lib/apache2/modules
181 mkdir -p "$GIT_DIR/gitweb/logs"
182 bind=
9126425d 183 test x"$local" = xtrue && bind='127.0.0.1:'
a51d37c1
EW
184 echo 'text/css css' > $fqgitdir/mime.types
185 cat > "$conf" <<EOF
44a167b0 186ServerName "git-instaweb"
a51d37c1
EW
187ServerRoot "$fqgitdir/gitweb"
188DocumentRoot "$fqgitdir/gitweb"
189PidFile "$fqgitdir/pid"
190Listen $bind$port
44a167b0
EW
191EOF
192
193 for mod in mime dir; do
194 if test -e $module_path/mod_${mod}.so; then
195 echo "LoadModule ${mod}_module " \
196 "$module_path/mod_${mod}.so" >> "$conf"
197 fi
198 done
199 cat >> "$conf" <<EOF
a51d37c1
EW
200TypesConfig $fqgitdir/mime.types
201DirectoryIndex gitweb.cgi
202EOF
203
204 # check to see if Dennis Stosberg's mod_perl compatibility patch
205 # (<20060621130708.Gcbc6e5c@leonov.stosberg.net>) has been applied
206 if test -f "$module_path/mod_perl.so" && grep '^our $gitbin' \
207 "$GIT_DIR/gitweb/gitweb.cgi" >/dev/null
208 then
209 # favor mod_perl if available
210 cat >> "$conf" <<EOF
211LoadModule perl_module $module_path/mod_perl.so
212PerlPassEnv GIT_DIR
213PerlPassEnv GIT_EXEC_DIR
214<Location /gitweb.cgi>
215 SetHandler perl-script
216 PerlResponseHandler ModPerl::Registry
217 PerlOptions +ParseHeaders
218 Options +ExecCGI
219</Location>
220EOF
221 else
222 # plain-old CGI
2b5d2d87 223 list_mods=`echo "$httpd" | sed "s/-f$/-l/"`
5209eda8 224 $list_mods | grep 'mod_cgi\.c' >/dev/null 2>&1 || \
2b5d2d87 225 echo "LoadModule cgi_module $module_path/mod_cgi.so" >> "$conf"
a51d37c1 226 cat >> "$conf" <<EOF
a51d37c1
EW
227AddHandler cgi-script .cgi
228<Location /gitweb.cgi>
229 Options +ExecCGI
230</Location>
231EOF
232 fi
233}
234
235script='
c5699693
CB
236s#^(my|our) \$projectroot =.*#$1 \$projectroot = "'$(dirname "$fqgitdir")'";#;
237s#(my|our) \$gitbin =.*#$1 \$gitbin = "'$GIT_EXEC_PATH'";#;
238s#(my|our) \$projects_list =.*#$1 \$projects_list = \$projectroot;#;
239s#(my|our) \$git_temp =.*#$1 \$git_temp = "'$fqgitdir/gitweb/tmp'";#;'
a51d37c1
EW
240
241gitweb_cgi () {
242 cat > "$1.tmp" <<\EOFGITWEB
243@@GITWEB_CGI@@
244EOFGITWEB
c5699693
CB
245 # Use the configured full path to perl to match the generated
246 # scripts' 'hashpling' line
247 "$PERL" -p -e "$script" "$1.tmp" > "$1"
a51d37c1
EW
248 chmod +x "$1"
249 rm -f "$1.tmp"
250}
251
252gitweb_css () {
253 cat > "$1" <<\EOFGITWEB
254@@GITWEB_CSS@@
255EOFGITWEB
256}
257
9126425d
JF
258gitweb_cgi "$GIT_DIR/gitweb/gitweb.cgi"
259gitweb_css "$GIT_DIR/gitweb/gitweb.css"
a51d37c1
EW
260
261case "$httpd" in
262*lighttpd*)
263 lighttpd_conf
264 ;;
265*apache2*)
266 apache2_conf
267 ;;
425b78e8
MD
268webrick)
269 webrick_conf
270 ;;
a51d37c1
EW
271*)
272 echo "Unknown httpd specified: $httpd"
273 exit 1
274 ;;
275esac
276
ab989adf
CC
277init_browser_path() {
278 browser_path="`git config browser.$1.path`"
279 test -z "$browser_path" && browser_path="$1"
280}
281
a51d37c1 282start_httpd
5209eda8 283url=http://127.0.0.1:$port
ab989adf
CC
284test -n "$browser" && {
285 init_browser_path "$browser"
286 "$browser_path" $url
287} || echo $url