instaweb: fix WEBrick server support
[git.git] / git-instaweb.sh
blob5d4ddc8f583c460a0c9a11d0167e4bd6318c7e56
1 #!/bin/sh
3 # Copyright (c) 2006 Eric Wong
6 PERL='@@PERL@@'
7 OPTIONS_KEEPDASHDASH=
8 OPTIONS_SPEC="\
9 git instaweb [options] (--start | --stop | --restart)
11 l,local only bind on 127.0.0.1
12 p,port= the port to bind to
13 d,httpd= the command to launch
14 b,browser= the browser to launch
15 m,module-path= the module path (only needed for apache2)
16 Action
17 stop stop the web server
18 start start the web server
19 restart restart the web server
22 . git-sh-setup
24 fqgitdir="$GIT_DIR"
25 local="$(git config --bool --get instaweb.local)"
26 httpd="$(git config --get instaweb.httpd)"
27 root="$(git config --get instaweb.gitwebdir)"
28 port=$(git config --get instaweb.port)
29 module_path="$(git config --get instaweb.modulepath)"
31 conf="$GIT_DIR/gitweb/httpd.conf"
33 # Defaults:
35 # if installed, it doesn't need further configuration (module_path)
36 test -z "$httpd" && httpd='lighttpd -f'
38 # Default is @@GITWEBDIR@@
39 test -z "$root" && root='@@GITWEBDIR@@'
41 # any untaken local port will do...
42 test -z "$port" && port=1234
44 resolve_full_httpd () {
45 case "$httpd" in
46 *apache2*|*lighttpd*|*httpd*)
47 # yes, *httpd* covers *lighttpd* above, but it is there for clarity
48 # ensure that the apache2/lighttpd command ends with "-f"
49 if ! echo "$httpd" | sane_grep -- '-f *$' >/dev/null 2>&1
50 then
51 httpd="$httpd -f"
54 *plackup*)
55 # server is started by running via generated gitweb.psgi in $fqgitdir/gitweb
56 full_httpd="$fqgitdir/gitweb/gitweb.psgi"
57 httpd_only="${httpd%% *}" # cut on first space
58 return
60 *webrick*)
61 # server is started by running via generated webrick.sh in
62 # $fqgitdir/gitweb
63 full_httpd="$fqgitdir/gitweb/webrick.sh"
64 httpd_only="${httpd%% *}" # cut on first space
65 return
67 esac
69 httpd_only="$(echo $httpd | cut -f1 -d' ')"
70 if case "$httpd_only" in /*) : ;; *) which $httpd_only >/dev/null 2>&1;; esac
71 then
72 full_httpd=$httpd
73 else
74 # many httpds are installed in /usr/sbin or /usr/local/sbin
75 # these days and those are not in most users $PATHs
76 # in addition, we may have generated a server script
77 # in $fqgitdir/gitweb.
78 for i in /usr/local/sbin /usr/sbin "$root" "$fqgitdir/gitweb"
80 if test -x "$i/$httpd_only"
81 then
82 full_httpd=$i/$httpd
83 return
85 done
87 echo >&2 "$httpd_only not found. Install $httpd_only or use" \
88 "--httpd to specify another httpd daemon."
89 exit 1
93 start_httpd () {
94 if test -f "$fqgitdir/pid"; then
95 say "Instance already running. Restarting..."
96 stop_httpd
99 # here $httpd should have a meaningful value
100 resolve_full_httpd
102 # don't quote $full_httpd, there can be arguments to it (-f)
103 case "$httpd" in
104 *mongoose*|*plackup*)
105 #These servers don't have a daemon mode so we'll have to fork it
106 $full_httpd "$fqgitdir/gitweb/httpd.conf" &
107 #Save the pid before doing anything else (we'll print it later)
108 pid=$!
110 if test $? != 0; then
111 echo "Could not execute http daemon $httpd."
112 exit 1
115 cat > "$fqgitdir/pid" <<EOF
116 $pid
120 $full_httpd "$fqgitdir/gitweb/httpd.conf"
121 if test $? != 0; then
122 echo "Could not execute http daemon $httpd."
123 exit 1
126 esac
129 stop_httpd () {
130 test -f "$fqgitdir/pid" && kill $(cat "$fqgitdir/pid")
131 rm -f "$fqgitdir/pid"
134 httpd_is_ready () {
135 "$PERL" -MIO::Socket::INET -e "
136 local \$| = 1; # turn on autoflush
137 exit if (IO::Socket::INET->new('127.0.0.1:$port'));
138 print 'Waiting for \'$httpd\' to start ..';
139 do {
140 print '.';
141 sleep(1);
142 } until (IO::Socket::INET->new('127.0.0.1:$port'));
143 print qq! (done)\n!;
147 while test $# != 0
149 case "$1" in
150 --stop|stop)
151 stop_httpd
152 exit 0
154 --start|start)
155 start_httpd
156 exit 0
158 --restart|restart)
159 stop_httpd
160 start_httpd
161 exit 0
163 -l|--local)
164 local=true
166 -d|--httpd)
167 shift
168 httpd="$1"
170 -b|--browser)
171 shift
172 browser="$1"
174 -p|--port)
175 shift
176 port="$1"
178 -m|--module-path)
179 shift
180 module_path="$1"
185 usage
187 esac
188 shift
189 done
191 mkdir -p "$GIT_DIR/gitweb/tmp"
192 GIT_EXEC_PATH="$(git --exec-path)"
193 GIT_DIR="$fqgitdir"
194 GITWEB_CONFIG="$fqgitdir/gitweb/gitweb_config.perl"
195 export GIT_EXEC_PATH GIT_DIR GITWEB_CONFIG
197 webrick_conf () {
198 # webrick seems to have no way of passing arbitrary environment
199 # variables to the underlying CGI executable, so we wrap the
200 # actual gitweb.cgi using a shell script to force it
201 wrapper="$fqgitdir/gitweb/$httpd/wrapper.sh"
202 cat > "$wrapper" <<EOF
203 #!/bin/sh
204 # we use this shell script wrapper around the real gitweb.cgi since
205 # there appears to be no other way to pass arbitrary environment variables
206 # into the CGI process
207 GIT_EXEC_PATH=$GIT_EXEC_PATH GIT_DIR=$GIT_DIR GITWEB_CONFIG=$GITWEB_CONFIG
208 export GIT_EXEC_PATH GIT_DIR GITWEB_CONFIG
209 exec $root/gitweb.cgi
211 chmod +x "$wrapper"
213 # This assumes _ruby_ is in the user's $PATH. that's _one_
214 # portable way to run ruby, which could be installed anywhere, really.
215 # generate a standalone server script in $fqgitdir/gitweb.
216 cat >"$fqgitdir/gitweb/$httpd.rb" <<EOF
217 require 'webrick'
218 require 'yaml'
219 options = YAML::load_file(ARGV[0])
220 options[:StartCallback] = proc do
221 File.open(options[:PidFile],"w") do |f|
222 f.puts Process.pid
225 options[:ServerType] = WEBrick::Daemon
226 server = WEBrick::HTTPServer.new(options)
227 ['INT', 'TERM'].each do |signal|
228 trap(signal) {server.shutdown}
230 server.start
232 # generate a shell script to invoke the above ruby script,
233 # which assumes _ruby_ is in the user's $PATH. that's _one_
234 # portable way to run ruby, which could be installed anywhere,
235 # really.
236 cat >"$fqgitdir/gitweb/$httpd.sh" <<EOF
237 #!/bin/sh
238 exec ruby "$fqgitdir/gitweb/$httpd.rb" \$*
240 chmod +x "$fqgitdir/gitweb/$httpd.sh"
242 cat >"$conf" <<EOF
243 :Port: $port
244 :DocumentRoot: "$root"
245 :DirectoryIndex: ["gitweb.cgi"]
246 :PidFile: "$fqgitdir/pid"
247 :CGIInterpreter: "$wrapper"
249 test "$local" = true && echo ':BindAddress: "127.0.0.1"' >> "$conf"
252 lighttpd_conf () {
253 cat > "$conf" <<EOF
254 server.document-root = "$root"
255 server.port = $port
256 server.modules = ( "mod_setenv", "mod_cgi" )
257 server.indexfiles = ( "gitweb.cgi" )
258 server.pid-file = "$fqgitdir/pid"
259 server.errorlog = "$fqgitdir/gitweb/$httpd_only/error.log"
261 # to enable, add "mod_access", "mod_accesslog" to server.modules
262 # variable above and uncomment this
263 #accesslog.filename = "$fqgitdir/gitweb/$httpd_only/access.log"
265 setenv.add-environment = ( "PATH" => env.PATH, "GITWEB_CONFIG" => env.GITWEB_CONFIG )
267 cgi.assign = ( ".cgi" => "" )
269 # mimetype mapping
270 mimetype.assign = (
271 ".pdf" => "application/pdf",
272 ".sig" => "application/pgp-signature",
273 ".spl" => "application/futuresplash",
274 ".class" => "application/octet-stream",
275 ".ps" => "application/postscript",
276 ".torrent" => "application/x-bittorrent",
277 ".dvi" => "application/x-dvi",
278 ".gz" => "application/x-gzip",
279 ".pac" => "application/x-ns-proxy-autoconfig",
280 ".swf" => "application/x-shockwave-flash",
281 ".tar.gz" => "application/x-tgz",
282 ".tgz" => "application/x-tgz",
283 ".tar" => "application/x-tar",
284 ".zip" => "application/zip",
285 ".mp3" => "audio/mpeg",
286 ".m3u" => "audio/x-mpegurl",
287 ".wma" => "audio/x-ms-wma",
288 ".wax" => "audio/x-ms-wax",
289 ".ogg" => "application/ogg",
290 ".wav" => "audio/x-wav",
291 ".gif" => "image/gif",
292 ".jpg" => "image/jpeg",
293 ".jpeg" => "image/jpeg",
294 ".png" => "image/png",
295 ".xbm" => "image/x-xbitmap",
296 ".xpm" => "image/x-xpixmap",
297 ".xwd" => "image/x-xwindowdump",
298 ".css" => "text/css",
299 ".html" => "text/html",
300 ".htm" => "text/html",
301 ".js" => "text/javascript",
302 ".asc" => "text/plain",
303 ".c" => "text/plain",
304 ".cpp" => "text/plain",
305 ".log" => "text/plain",
306 ".conf" => "text/plain",
307 ".text" => "text/plain",
308 ".txt" => "text/plain",
309 ".dtd" => "text/xml",
310 ".xml" => "text/xml",
311 ".mpeg" => "video/mpeg",
312 ".mpg" => "video/mpeg",
313 ".mov" => "video/quicktime",
314 ".qt" => "video/quicktime",
315 ".avi" => "video/x-msvideo",
316 ".asf" => "video/x-ms-asf",
317 ".asx" => "video/x-ms-asf",
318 ".wmv" => "video/x-ms-wmv",
319 ".bz2" => "application/x-bzip",
320 ".tbz" => "application/x-bzip-compressed-tar",
321 ".tar.bz2" => "application/x-bzip-compressed-tar",
322 "" => "text/plain"
325 test x"$local" = xtrue && echo 'server.bind = "127.0.0.1"' >> "$conf"
328 apache2_conf () {
329 if test -z "$module_path"
330 then
331 test -d "/usr/lib/httpd/modules" &&
332 module_path="/usr/lib/httpd/modules"
333 test -d "/usr/lib/apache2/modules" &&
334 module_path="/usr/lib/apache2/modules"
336 bind=
337 test x"$local" = xtrue && bind='127.0.0.1:'
338 echo 'text/css css' > "$fqgitdir/mime.types"
339 cat > "$conf" <<EOF
340 ServerName "git-instaweb"
341 ServerRoot "$root"
342 DocumentRoot "$root"
343 ErrorLog "$fqgitdir/gitweb/$httpd_only/error.log"
344 CustomLog "$fqgitdir/gitweb/$httpd_only/access.log" combined
345 PidFile "$fqgitdir/pid"
346 Listen $bind$port
349 for mod in mime dir env log_config
351 if test -e $module_path/mod_${mod}.so
352 then
353 echo "LoadModule ${mod}_module " \
354 "$module_path/mod_${mod}.so" >> "$conf"
356 done
357 cat >> "$conf" <<EOF
358 TypesConfig "$fqgitdir/mime.types"
359 DirectoryIndex gitweb.cgi
362 # check to see if Dennis Stosberg's mod_perl compatibility patch
363 # (<20060621130708.Gcbc6e5c@leonov.stosberg.net>) has been applied
364 if test -f "$module_path/mod_perl.so" &&
365 sane_grep 'MOD_PERL' "$root/gitweb.cgi" >/dev/null
366 then
367 # favor mod_perl if available
368 cat >> "$conf" <<EOF
369 LoadModule perl_module $module_path/mod_perl.so
370 PerlPassEnv GIT_DIR
371 PerlPassEnv GIT_EXEC_PATH
372 PerlPassEnv GITWEB_CONFIG
373 <Location /gitweb.cgi>
374 SetHandler perl-script
375 PerlResponseHandler ModPerl::Registry
376 PerlOptions +ParseHeaders
377 Options +ExecCGI
378 </Location>
380 else
381 # plain-old CGI
382 resolve_full_httpd
383 list_mods=$(echo "$full_httpd" | sed 's/-f$/-l/')
384 $list_mods | sane_grep 'mod_cgi\.c' >/dev/null 2>&1 || \
385 if test -f "$module_path/mod_cgi.so"
386 then
387 echo "LoadModule cgi_module $module_path/mod_cgi.so" >> "$conf"
388 else
389 $list_mods | grep 'mod_cgid\.c' >/dev/null 2>&1 || \
390 if test -f "$module_path/mod_cgid.so"
391 then
392 echo "LoadModule cgid_module $module_path/mod_cgid.so" \
393 >> "$conf"
394 else
395 echo "You have no CGI support!"
396 exit 2
398 echo "ScriptSock logs/gitweb.sock" >> "$conf"
400 cat >> "$conf" <<EOF
401 PassEnv GIT_DIR
402 PassEnv GIT_EXEC_PATH
403 PassEnv GITWEB_CONFIG
404 AddHandler cgi-script .cgi
405 <Location /gitweb.cgi>
406 Options +ExecCGI
407 </Location>
412 mongoose_conf() {
413 cat > "$conf" <<EOF
414 # Mongoose web server configuration file.
415 # Lines starting with '#' and empty lines are ignored.
416 # For detailed description of every option, visit
417 # http://code.google.com/p/mongoose/wiki/MongooseManual
419 root $root
420 ports $port
421 index_files gitweb.cgi
422 #ssl_cert $fqgitdir/gitweb/ssl_cert.pem
423 error_log $fqgitdir/gitweb/$httpd_only/error.log
424 access_log $fqgitdir/gitweb/$httpd_only/access.log
426 #cgi setup
427 cgi_env PATH=$PATH,GIT_DIR=$GIT_DIR,GIT_EXEC_PATH=$GIT_EXEC_PATH,GITWEB_CONFIG=$GITWEB_CONFIG
428 cgi_interp $PERL
429 cgi_ext cgi,pl
431 # mimetype mapping
432 mime_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
436 plackup_conf () {
437 # generate a standalone 'plackup' server script in $fqgitdir/gitweb
438 # with embedded configuration; it does not use "$conf" file
439 cat > "$fqgitdir/gitweb/gitweb.psgi" <<EOF
440 #!$PERL
442 # gitweb - simple web interface to track changes in git repositories
443 # PSGI wrapper and server starter (see http://plackperl.org)
445 use strict;
447 use IO::Handle;
448 use Plack::MIME;
449 use Plack::Builder;
450 use Plack::App::WrapCGI;
451 use CGI::Emulate::PSGI 0.07; # minimum version required to work with gitweb
453 # mimetype mapping (from lighttpd_conf)
454 Plack::MIME->add_type(
455 ".pdf" => "application/pdf",
456 ".sig" => "application/pgp-signature",
457 ".spl" => "application/futuresplash",
458 ".class" => "application/octet-stream",
459 ".ps" => "application/postscript",
460 ".torrent" => "application/x-bittorrent",
461 ".dvi" => "application/x-dvi",
462 ".gz" => "application/x-gzip",
463 ".pac" => "application/x-ns-proxy-autoconfig",
464 ".swf" => "application/x-shockwave-flash",
465 ".tar.gz" => "application/x-tgz",
466 ".tgz" => "application/x-tgz",
467 ".tar" => "application/x-tar",
468 ".zip" => "application/zip",
469 ".mp3" => "audio/mpeg",
470 ".m3u" => "audio/x-mpegurl",
471 ".wma" => "audio/x-ms-wma",
472 ".wax" => "audio/x-ms-wax",
473 ".ogg" => "application/ogg",
474 ".wav" => "audio/x-wav",
475 ".gif" => "image/gif",
476 ".jpg" => "image/jpeg",
477 ".jpeg" => "image/jpeg",
478 ".png" => "image/png",
479 ".xbm" => "image/x-xbitmap",
480 ".xpm" => "image/x-xpixmap",
481 ".xwd" => "image/x-xwindowdump",
482 ".css" => "text/css",
483 ".html" => "text/html",
484 ".htm" => "text/html",
485 ".js" => "text/javascript",
486 ".asc" => "text/plain",
487 ".c" => "text/plain",
488 ".cpp" => "text/plain",
489 ".log" => "text/plain",
490 ".conf" => "text/plain",
491 ".text" => "text/plain",
492 ".txt" => "text/plain",
493 ".dtd" => "text/xml",
494 ".xml" => "text/xml",
495 ".mpeg" => "video/mpeg",
496 ".mpg" => "video/mpeg",
497 ".mov" => "video/quicktime",
498 ".qt" => "video/quicktime",
499 ".avi" => "video/x-msvideo",
500 ".asf" => "video/x-ms-asf",
501 ".asx" => "video/x-ms-asf",
502 ".wmv" => "video/x-ms-wmv",
503 ".bz2" => "application/x-bzip",
504 ".tbz" => "application/x-bzip-compressed-tar",
505 ".tar.bz2" => "application/x-bzip-compressed-tar",
506 "" => "text/plain"
509 my \$app = builder {
510 # to be able to override \$SIG{__WARN__} to log build time warnings
511 use CGI::Carp; # it sets \$SIG{__WARN__} itself
513 my \$logdir = "$fqgitdir/gitweb/$httpd_only";
514 open my \$access_log_fh, '>>', "\$logdir/access.log"
515 or die "Couldn't open access log '\$logdir/access.log': \$!";
516 open my \$error_log_fh, '>>', "\$logdir/error.log"
517 or die "Couldn't open error log '\$logdir/error.log': \$!";
519 \$access_log_fh->autoflush(1);
520 \$error_log_fh->autoflush(1);
522 # redirect build time warnings to error.log
523 \$SIG{'__WARN__'} = sub {
524 my \$msg = shift;
525 # timestamp warning like in CGI::Carp::warn
526 my \$stamp = CGI::Carp::stamp();
527 \$msg =~ s/^/\$stamp/gm;
528 print \$error_log_fh \$msg;
531 # write errors to error.log, access to access.log
532 enable 'AccessLog',
533 format => "combined",
534 logger => sub { print \$access_log_fh @_; };
535 enable sub {
536 my \$app = shift;
537 sub {
538 my \$env = shift;
539 \$env->{'psgi.errors'} = \$error_log_fh;
540 \$app->(\$env);
543 # gitweb currently doesn't work with $SIG{CHLD} set to 'IGNORE',
544 # because it uses 'close $fd or die...' on piped filehandle $fh
545 # (which causes the parent process to wait for child to finish).
546 enable_if { \$SIG{'CHLD'} eq 'IGNORE' } sub {
547 my \$app = shift;
548 sub {
549 my \$env = shift;
550 local \$SIG{'CHLD'} = 'DEFAULT';
551 local \$SIG{'CLD'} = 'DEFAULT';
552 \$app->(\$env);
555 # serve static files, i.e. stylesheet, images, script
556 enable 'Static',
557 path => sub { m!\.(js|css|png)\$! && s!^/gitweb/!! },
558 root => "$root/",
559 encoding => 'utf-8'; # encoding for 'text/plain' files
560 # convert CGI application to PSGI app
561 Plack::App::WrapCGI->new(script => "$root/gitweb.cgi")->to_app;
564 # make it runnable as standalone app,
565 # like it would be run via 'plackup' utility
566 if (__FILE__ eq \$0) {
567 require Plack::Runner;
569 my \$runner = Plack::Runner->new();
570 \$runner->parse_options(qw(--env deployment --port $port),
571 "$local" ? qw(--host 127.0.0.1) : ());
572 \$runner->run(\$app);
574 __END__
577 chmod a+x "$fqgitdir/gitweb/gitweb.psgi"
578 # configuration is embedded in server script file, gitweb.psgi
579 rm -f "$conf"
582 gitweb_conf() {
583 cat > "$fqgitdir/gitweb/gitweb_config.perl" <<EOF
584 #!/usr/bin/perl
585 our \$projectroot = "$(dirname "$fqgitdir")";
586 our \$git_temp = "$fqgitdir/gitweb/tmp";
587 our \$projects_list = \$projectroot;
591 gitweb_conf
593 resolve_full_httpd
594 mkdir -p "$fqgitdir/gitweb/$httpd_only"
596 case "$httpd" in
597 *lighttpd*)
598 lighttpd_conf
600 *apache2*|*httpd*)
601 apache2_conf
603 webrick)
604 webrick_conf
606 *mongoose*)
607 mongoose_conf
609 *plackup*)
610 plackup_conf
613 echo "Unknown httpd specified: $httpd"
614 exit 1
616 esac
618 start_httpd
619 url=http://127.0.0.1:$port
621 if test -n "$browser"; then
622 httpd_is_ready && git web--browse -b "$browser" $url || echo $url
623 else
624 httpd_is_ready && git web--browse -c "instaweb.browser" $url || echo $url