git-instaweb: Remove pidfile after stopping web server
[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="\
1b1dd23f 9git instaweb [options] (--start | --stop | --restart)
c2db2e0e
PH
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"
43d60d2e
FP
25local="$(git config --bool --get instaweb.local)"
26httpd="$(git config --get instaweb.httpd)"
c0cb4ed3 27root="$(git config --get instaweb.gitwebdir)"
43d60d2e
FP
28port=$(git config --get instaweb.port)
29module_path="$(git config --get instaweb.modulepath)"
a51d37c1 30
9126425d 31conf="$GIT_DIR/gitweb/httpd.conf"
a51d37c1
EW
32
33# Defaults:
34
82e5a82f 35# if installed, it doesn't need further configuration (module_path)
a51d37c1
EW
36test -z "$httpd" && httpd='lighttpd -f'
37
c0cb4ed3
PKS
38# Default is @@GITWEBDIR@@
39test -z "$root" && root='@@GITWEBDIR@@'
40
a51d37c1
EW
41# any untaken local port will do...
42test -z "$port" && port=1234
43
43d60d2e
FP
44resolve_full_httpd () {
45 case "$httpd" in
46 *apache2*|*lighttpd*)
47 # ensure that the apache2/lighttpd command ends with "-f"
e1622bfc 48 if ! echo "$httpd" | sane_grep -- '-f *$' >/dev/null 2>&1
43d60d2e
FP
49 then
50 httpd="$httpd -f"
51 fi
52 ;;
53 esac
54
55 httpd_only="$(echo $httpd | cut -f1 -d' ')"
e47eec8f 56 if case "$httpd_only" in /*) : ;; *) which $httpd_only >/dev/null 2>&1;; esac
a51d37c1 57 then
43d60d2e 58 full_httpd=$httpd
a51d37c1
EW
59 else
60 # many httpds are installed in /usr/sbin or /usr/local/sbin
61 # these days and those are not in most users $PATHs
14b45b66
MD
62 # in addition, we may have generated a server script
63 # in $fqgitdir/gitweb.
c0cb4ed3 64 for i in /usr/local/sbin /usr/sbin "$root" "$fqgitdir/gitweb"
a51d37c1
EW
65 do
66 if test -x "$i/$httpd_only"
67 then
43d60d2e 68 full_httpd=$i/$httpd
a51d37c1
EW
69 return
70 fi
71 done
43d60d2e
FP
72
73 echo >&2 "$httpd_only not found. Install $httpd_only or use" \
74 "--httpd to specify another httpd daemon."
f281e3a1 75 exit 1
a51d37c1 76 fi
43d60d2e
FP
77}
78
79start_httpd () {
0b624b4c
SB
80 if test -f "$fqgitdir/pid"; then
81 say "Instance already running. Restarting..."
82 stop_httpd
83 fi
84
43d60d2e
FP
85 # here $httpd should have a meaningful value
86 resolve_full_httpd
87
88 # don't quote $full_httpd, there can be arguments to it (-f)
0ded4758
WL
89 case "$httpd" in
90 *mongoose*)
91 #The mongoose server doesn't have a daemon mode so we'll have to fork it
92 $full_httpd "$fqgitdir/gitweb/httpd.conf" &
93 #Save the pid before doing anything else (we'll print it later)
94 pid=$!
95
96 if test $? != 0; then
97 echo "Could not execute http daemon $httpd."
98 exit 1
99 fi
100
101 cat > "$fqgitdir/pid" <<EOF
102$pid
103EOF
104 ;;
105 *)
106 $full_httpd "$fqgitdir/gitweb/httpd.conf"
107 if test $? != 0; then
108 echo "Could not execute http daemon $httpd."
109 exit 1
110 fi
111 ;;
112 esac
a51d37c1
EW
113}
114
115stop_httpd () {
43d60d2e 116 test -f "$fqgitdir/pid" && kill $(cat "$fqgitdir/pid")
d1127622 117 rm -f "$fqgitdir/pid"
a51d37c1
EW
118}
119
822f7c73 120while test $# != 0
a51d37c1
EW
121do
122 case "$1" in
123 --stop|stop)
124 stop_httpd
125 exit 0
126 ;;
127 --start|start)
128 start_httpd
129 exit 0
130 ;;
131 --restart|restart)
132 stop_httpd
133 start_httpd
134 exit 0
135 ;;
c2db2e0e 136 -l|--local)
a51d37c1
EW
137 local=true
138 ;;
c2db2e0e
PH
139 -d|--httpd)
140 shift
141 httpd="$1"
142 ;;
143 -b|--browser)
144 shift
145 browser="$1"
a51d37c1 146 ;;
c2db2e0e
PH
147 -p|--port)
148 shift
149 port="$1"
a51d37c1 150 ;;
c2db2e0e
PH
151 -m|--module-path)
152 shift
153 module_path="$1"
a51d37c1 154 ;;
c2db2e0e 155 --)
a51d37c1
EW
156 ;;
157 *)
158 usage
159 ;;
160 esac
161 shift
162done
163
164mkdir -p "$GIT_DIR/gitweb/tmp"
43d60d2e 165GIT_EXEC_PATH="$(git --exec-path)"
a51d37c1 166GIT_DIR="$fqgitdir"
c0cb4ed3
PKS
167GITWEB_CONFIG="$fqgitdir/gitweb/gitweb_config.perl"
168export GIT_EXEC_PATH GIT_DIR GITWEB_CONFIG
a51d37c1 169
425b78e8
MD
170webrick_conf () {
171 # generate a standalone server script in $fqgitdir/gitweb.
172 cat >"$fqgitdir/gitweb/$httpd.rb" <<EOF
173require 'webrick'
174require 'yaml'
175options = YAML::load_file(ARGV[0])
176options[:StartCallback] = proc do
177 File.open(options[:PidFile],"w") do |f|
178 f.puts Process.pid
179 end
180end
181options[:ServerType] = WEBrick::Daemon
182server = WEBrick::HTTPServer.new(options)
183['INT', 'TERM'].each do |signal|
184 trap(signal) {server.shutdown}
185end
186server.start
187EOF
188 # generate a shell script to invoke the above ruby script,
189 # which assumes _ruby_ is in the user's $PATH. that's _one_
190 # portable way to run ruby, which could be installed anywhere,
191 # really.
192 cat >"$fqgitdir/gitweb/$httpd" <<EOF
193#!/bin/sh
194exec ruby "$fqgitdir/gitweb/$httpd.rb" \$*
195EOF
196 chmod +x "$fqgitdir/gitweb/$httpd"
197
198 cat >"$conf" <<EOF
199:Port: $port
c0cb4ed3 200:DocumentRoot: "$root"
425b78e8
MD
201:DirectoryIndex: ["gitweb.cgi"]
202:PidFile: "$fqgitdir/pid"
203EOF
204 test "$local" = true && echo ':BindAddress: "127.0.0.1"' >> "$conf"
205}
206
a51d37c1
EW
207lighttpd_conf () {
208 cat > "$conf" <<EOF
c0cb4ed3 209server.document-root = "$root"
a51d37c1 210server.port = $port
e47eec8f 211server.modules = ( "mod_setenv", "mod_cgi" )
a51d37c1
EW
212server.indexfiles = ( "gitweb.cgi" )
213server.pid-file = "$fqgitdir/pid"
be5347b3 214server.errorlog = "$fqgitdir/gitweb/$httpd_only/error.log"
e47eec8f
RJ
215
216# to enable, add "mod_access", "mod_accesslog" to server.modules
217# variable above and uncomment this
be5347b3 218#accesslog.filename = "$fqgitdir/gitweb/$httpd_only/access.log"
e47eec8f 219
c0cb4ed3 220setenv.add-environment = ( "PATH" => env.PATH, "GITWEB_CONFIG" => env.GITWEB_CONFIG )
e47eec8f 221
a51d37c1 222cgi.assign = ( ".cgi" => "" )
e47eec8f
RJ
223
224# mimetype mapping
225mimetype.assign = (
226 ".pdf" => "application/pdf",
227 ".sig" => "application/pgp-signature",
228 ".spl" => "application/futuresplash",
229 ".class" => "application/octet-stream",
230 ".ps" => "application/postscript",
231 ".torrent" => "application/x-bittorrent",
232 ".dvi" => "application/x-dvi",
233 ".gz" => "application/x-gzip",
234 ".pac" => "application/x-ns-proxy-autoconfig",
235 ".swf" => "application/x-shockwave-flash",
236 ".tar.gz" => "application/x-tgz",
237 ".tgz" => "application/x-tgz",
238 ".tar" => "application/x-tar",
239 ".zip" => "application/zip",
240 ".mp3" => "audio/mpeg",
241 ".m3u" => "audio/x-mpegurl",
242 ".wma" => "audio/x-ms-wma",
243 ".wax" => "audio/x-ms-wax",
244 ".ogg" => "application/ogg",
245 ".wav" => "audio/x-wav",
246 ".gif" => "image/gif",
247 ".jpg" => "image/jpeg",
248 ".jpeg" => "image/jpeg",
249 ".png" => "image/png",
250 ".xbm" => "image/x-xbitmap",
251 ".xpm" => "image/x-xpixmap",
252 ".xwd" => "image/x-xwindowdump",
253 ".css" => "text/css",
254 ".html" => "text/html",
255 ".htm" => "text/html",
256 ".js" => "text/javascript",
257 ".asc" => "text/plain",
258 ".c" => "text/plain",
259 ".cpp" => "text/plain",
260 ".log" => "text/plain",
261 ".conf" => "text/plain",
262 ".text" => "text/plain",
263 ".txt" => "text/plain",
264 ".dtd" => "text/xml",
265 ".xml" => "text/xml",
266 ".mpeg" => "video/mpeg",
267 ".mpg" => "video/mpeg",
268 ".mov" => "video/quicktime",
269 ".qt" => "video/quicktime",
270 ".avi" => "video/x-msvideo",
271 ".asf" => "video/x-ms-asf",
272 ".asx" => "video/x-ms-asf",
273 ".wmv" => "video/x-ms-wmv",
274 ".bz2" => "application/x-bzip",
275 ".tbz" => "application/x-bzip-compressed-tar",
276 ".tar.bz2" => "application/x-bzip-compressed-tar",
277 "" => "text/plain"
278 )
a51d37c1 279EOF
9126425d 280 test x"$local" = xtrue && echo 'server.bind = "127.0.0.1"' >> "$conf"
a51d37c1
EW
281}
282
283apache2_conf () {
284 test -z "$module_path" && module_path=/usr/lib/apache2/modules
a51d37c1 285 bind=
9126425d 286 test x"$local" = xtrue && bind='127.0.0.1:'
e24c76bf 287 echo 'text/css css' > "$fqgitdir/mime.types"
a51d37c1 288 cat > "$conf" <<EOF
44a167b0 289ServerName "git-instaweb"
c0cb4ed3
PKS
290ServerRoot "$root"
291DocumentRoot "$root"
be5347b3
PKS
292ErrorLog "$fqgitdir/gitweb/$httpd_only/error.log"
293CustomLog "$fqgitdir/gitweb/$httpd_only/access.log" combined
a51d37c1
EW
294PidFile "$fqgitdir/pid"
295Listen $bind$port
44a167b0
EW
296EOF
297
298 for mod in mime dir; do
299 if test -e $module_path/mod_${mod}.so; then
300 echo "LoadModule ${mod}_module " \
301 "$module_path/mod_${mod}.so" >> "$conf"
302 fi
303 done
304 cat >> "$conf" <<EOF
e24c76bf 305TypesConfig "$fqgitdir/mime.types"
a51d37c1
EW
306DirectoryIndex gitweb.cgi
307EOF
308
309 # check to see if Dennis Stosberg's mod_perl compatibility patch
310 # (<20060621130708.Gcbc6e5c@leonov.stosberg.net>) has been applied
e1622bfc 311 if test -f "$module_path/mod_perl.so" &&
c0cb4ed3 312 sane_grep 'MOD_PERL' "$root/gitweb.cgi" >/dev/null
a51d37c1
EW
313 then
314 # favor mod_perl if available
315 cat >> "$conf" <<EOF
316LoadModule perl_module $module_path/mod_perl.so
317PerlPassEnv GIT_DIR
318PerlPassEnv GIT_EXEC_DIR
c0cb4ed3 319PerlPassEnv GITWEB_CONFIG
a51d37c1
EW
320<Location /gitweb.cgi>
321 SetHandler perl-script
322 PerlResponseHandler ModPerl::Registry
323 PerlOptions +ParseHeaders
324 Options +ExecCGI
325</Location>
326EOF
327 else
328 # plain-old CGI
43d60d2e 329 resolve_full_httpd
9524cf29 330 list_mods=$(echo "$full_httpd" | sed 's/-f$/-l/')
e1622bfc 331 $list_mods | sane_grep 'mod_cgi\.c' >/dev/null 2>&1 || \
10d1432a
MR
332 if test -f "$module_path/mod_cgi.so"
333 then
334 echo "LoadModule cgi_module $module_path/mod_cgi.so" >> "$conf"
335 else
336 $list_mods | grep 'mod_cgid\.c' >/dev/null 2>&1 || \
337 if test -f "$module_path/mod_cgid.so"
338 then
339 echo "LoadModule cgid_module $module_path/mod_cgid.so" \
340 >> "$conf"
341 else
342 echo "You have no CGI support!"
343 exit 2
344 fi
345 echo "ScriptSock logs/gitweb.sock" >> "$conf"
346 fi
a51d37c1 347 cat >> "$conf" <<EOF
a51d37c1
EW
348AddHandler cgi-script .cgi
349<Location /gitweb.cgi>
350 Options +ExecCGI
351</Location>
352EOF
353 fi
354}
355
0ded4758
WL
356mongoose_conf() {
357 cat > "$conf" <<EOF
358# Mongoose web server configuration file.
359# Lines starting with '#' and empty lines are ignored.
360# For detailed description of every option, visit
361# http://code.google.com/p/mongoose/wiki/MongooseManual
362
c0cb4ed3 363root $root
0ded4758
WL
364ports $port
365index_files gitweb.cgi
366#ssl_cert $fqgitdir/gitweb/ssl_cert.pem
be5347b3
PKS
367error_log $fqgitdir/gitweb/$httpd_only/error.log
368access_log $fqgitdir/gitweb/$httpd_only/access.log
0ded4758
WL
369
370#cgi setup
c0cb4ed3 371cgi_env PATH=$PATH,GIT_DIR=$GIT_DIR,GIT_EXEC_PATH=$GIT_EXEC_PATH,GITWEB_CONFIG=$GITWEB_CONFIG
0ded4758
WL
372cgi_interp $PERL
373cgi_ext cgi,pl
374
375# mimetype mapping
376mime_types .gz=application/x-gzip,.tar.gz=application/x-tgz,.tgz=application/x-tgz,.tar=application/x-tar,.zip=application/zip,.gif=image/gif,.jpg=image/jpeg,.jpeg=image/jpeg,.png=image/png,.css=text/css,.html=text/html,.htm=text/html,.js=text/javascript,.c=text/plain,.cpp=text/plain,.log=text/plain,.conf=text/plain,.text=text/plain,.txt=text/plain,.dtd=text/xml,.bz2=application/x-bzip,.tbz=application/x-bzip-compressed-tar,.tar.bz2=application/x-bzip-compressed-tar
377EOF
378}
379
c0cb4ed3
PKS
380gitweb_conf() {
381 cat > "$fqgitdir/gitweb/gitweb_config.perl" <<EOF
382#!/usr/bin/perl
383our \$projectroot = "$(dirname "$fqgitdir")";
384our \$git_temp = "$fqgitdir/gitweb/tmp";
385our \$projects_list = \$projectroot;
386EOF
4af819d4
JN
387}
388
c0cb4ed3 389gitweb_conf
a51d37c1 390
be5347b3
PKS
391resolve_full_httpd
392mkdir -p "$fqgitdir/gitweb/$httpd_only"
393
a51d37c1
EW
394case "$httpd" in
395*lighttpd*)
396 lighttpd_conf
397 ;;
398*apache2*)
399 apache2_conf
400 ;;
425b78e8
MD
401webrick)
402 webrick_conf
403 ;;
0ded4758
WL
404*mongoose*)
405 mongoose_conf
406 ;;
a51d37c1
EW
407*)
408 echo "Unknown httpd specified: $httpd"
409 exit 1
410 ;;
411esac
412
413start_httpd
5209eda8 414url=http://127.0.0.1:$port
2e0c2902
CC
415
416if test -n "$browser"; then
417 git web--browse -b "$browser" $url || echo $url
418else
419 git web--browse -c "instaweb.browser" $url || echo $url
420fi