compat/nedmalloc: don't force NDEBUG on the rest of git
[git/dscho.git] / git-instaweb.sh
blobb7342e22c88993756e7beb6582a896087e52a034
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 esac
62 httpd_only="$(echo $httpd | cut -f1 -d' ')"
63 if case "$httpd_only" in /*) : ;; *) which $httpd_only >/dev/null 2>&1;; esac
64 then
65 full_httpd=$httpd
66 else
67 # many httpds are installed in /usr/sbin or /usr/local/sbin
68 # these days and those are not in most users $PATHs
69 # in addition, we may have generated a server script
70 # in $fqgitdir/gitweb.
71 for i in /usr/local/sbin /usr/sbin "$root" "$fqgitdir/gitweb"
73 if test -x "$i/$httpd_only"
74 then
75 full_httpd=$i/$httpd
76 return
78 done
80 echo >&2 "$httpd_only not found. Install $httpd_only or use" \
81 "--httpd to specify another httpd daemon."
82 exit 1
86 start_httpd () {
87 if test -f "$fqgitdir/pid"; then
88 say "Instance already running. Restarting..."
89 stop_httpd
92 # here $httpd should have a meaningful value
93 resolve_full_httpd
95 # don't quote $full_httpd, there can be arguments to it (-f)
96 case "$httpd" in
97 *mongoose*|*plackup*)
98 #These servers don't have a daemon mode so we'll have to fork it
99 $full_httpd "$fqgitdir/gitweb/httpd.conf" &
100 #Save the pid before doing anything else (we'll print it later)
101 pid=$!
103 if test $? != 0; then
104 echo "Could not execute http daemon $httpd."
105 exit 1
108 cat > "$fqgitdir/pid" <<EOF
109 $pid
113 $full_httpd "$fqgitdir/gitweb/httpd.conf"
114 if test $? != 0; then
115 echo "Could not execute http daemon $httpd."
116 exit 1
119 esac
122 stop_httpd () {
123 test -f "$fqgitdir/pid" && kill $(cat "$fqgitdir/pid")
124 rm -f "$fqgitdir/pid"
127 httpd_is_ready () {
128 "$PERL" -MIO::Socket::INET -e "
129 local \$| = 1; # turn on autoflush
130 exit if (IO::Socket::INET->new('127.0.0.1:$port'));
131 print 'Waiting for \'$httpd\' to start ..';
132 do {
133 print '.';
134 sleep(1);
135 } until (IO::Socket::INET->new('127.0.0.1:$port'));
136 print qq! (done)\n!;
140 while test $# != 0
142 case "$1" in
143 --stop|stop)
144 stop_httpd
145 exit 0
147 --start|start)
148 start_httpd
149 exit 0
151 --restart|restart)
152 stop_httpd
153 start_httpd
154 exit 0
156 -l|--local)
157 local=true
159 -d|--httpd)
160 shift
161 httpd="$1"
163 -b|--browser)
164 shift
165 browser="$1"
167 -p|--port)
168 shift
169 port="$1"
171 -m|--module-path)
172 shift
173 module_path="$1"
178 usage
180 esac
181 shift
182 done
184 mkdir -p "$GIT_DIR/gitweb/tmp"
185 GIT_EXEC_PATH="$(git --exec-path)"
186 GIT_DIR="$fqgitdir"
187 GITWEB_CONFIG="$fqgitdir/gitweb/gitweb_config.perl"
188 export GIT_EXEC_PATH GIT_DIR GITWEB_CONFIG
190 webrick_conf () {
191 # generate a standalone server script in $fqgitdir/gitweb.
192 cat >"$fqgitdir/gitweb/$httpd.rb" <<EOF
193 require 'webrick'
194 require 'yaml'
195 options = YAML::load_file(ARGV[0])
196 options[:StartCallback] = proc do
197 File.open(options[:PidFile],"w") do |f|
198 f.puts Process.pid
201 options[:ServerType] = WEBrick::Daemon
202 server = WEBrick::HTTPServer.new(options)
203 ['INT', 'TERM'].each do |signal|
204 trap(signal) {server.shutdown}
206 server.start
208 # generate a shell script to invoke the above ruby script,
209 # which assumes _ruby_ is in the user's $PATH. that's _one_
210 # portable way to run ruby, which could be installed anywhere,
211 # really.
212 cat >"$fqgitdir/gitweb/$httpd" <<EOF
213 #!/bin/sh
214 exec ruby "$fqgitdir/gitweb/$httpd.rb" \$*
216 chmod +x "$fqgitdir/gitweb/$httpd"
218 cat >"$conf" <<EOF
219 :Port: $port
220 :DocumentRoot: "$root"
221 :DirectoryIndex: ["gitweb.cgi"]
222 :PidFile: "$fqgitdir/pid"
224 test "$local" = true && echo ':BindAddress: "127.0.0.1"' >> "$conf"
227 lighttpd_conf () {
228 cat > "$conf" <<EOF
229 server.document-root = "$root"
230 server.port = $port
231 server.modules = ( "mod_setenv", "mod_cgi" )
232 server.indexfiles = ( "gitweb.cgi" )
233 server.pid-file = "$fqgitdir/pid"
234 server.errorlog = "$fqgitdir/gitweb/$httpd_only/error.log"
236 # to enable, add "mod_access", "mod_accesslog" to server.modules
237 # variable above and uncomment this
238 #accesslog.filename = "$fqgitdir/gitweb/$httpd_only/access.log"
240 setenv.add-environment = ( "PATH" => env.PATH, "GITWEB_CONFIG" => env.GITWEB_CONFIG )
242 cgi.assign = ( ".cgi" => "" )
244 # mimetype mapping
245 mimetype.assign = (
246 ".pdf" => "application/pdf",
247 ".sig" => "application/pgp-signature",
248 ".spl" => "application/futuresplash",
249 ".class" => "application/octet-stream",
250 ".ps" => "application/postscript",
251 ".torrent" => "application/x-bittorrent",
252 ".dvi" => "application/x-dvi",
253 ".gz" => "application/x-gzip",
254 ".pac" => "application/x-ns-proxy-autoconfig",
255 ".swf" => "application/x-shockwave-flash",
256 ".tar.gz" => "application/x-tgz",
257 ".tgz" => "application/x-tgz",
258 ".tar" => "application/x-tar",
259 ".zip" => "application/zip",
260 ".mp3" => "audio/mpeg",
261 ".m3u" => "audio/x-mpegurl",
262 ".wma" => "audio/x-ms-wma",
263 ".wax" => "audio/x-ms-wax",
264 ".ogg" => "application/ogg",
265 ".wav" => "audio/x-wav",
266 ".gif" => "image/gif",
267 ".jpg" => "image/jpeg",
268 ".jpeg" => "image/jpeg",
269 ".png" => "image/png",
270 ".xbm" => "image/x-xbitmap",
271 ".xpm" => "image/x-xpixmap",
272 ".xwd" => "image/x-xwindowdump",
273 ".css" => "text/css",
274 ".html" => "text/html",
275 ".htm" => "text/html",
276 ".js" => "text/javascript",
277 ".asc" => "text/plain",
278 ".c" => "text/plain",
279 ".cpp" => "text/plain",
280 ".log" => "text/plain",
281 ".conf" => "text/plain",
282 ".text" => "text/plain",
283 ".txt" => "text/plain",
284 ".dtd" => "text/xml",
285 ".xml" => "text/xml",
286 ".mpeg" => "video/mpeg",
287 ".mpg" => "video/mpeg",
288 ".mov" => "video/quicktime",
289 ".qt" => "video/quicktime",
290 ".avi" => "video/x-msvideo",
291 ".asf" => "video/x-ms-asf",
292 ".asx" => "video/x-ms-asf",
293 ".wmv" => "video/x-ms-wmv",
294 ".bz2" => "application/x-bzip",
295 ".tbz" => "application/x-bzip-compressed-tar",
296 ".tar.bz2" => "application/x-bzip-compressed-tar",
297 "" => "text/plain"
300 test x"$local" = xtrue && echo 'server.bind = "127.0.0.1"' >> "$conf"
303 apache2_conf () {
304 if test -z "$module_path"
305 then
306 test -d "/usr/lib/httpd/modules" &&
307 module_path="/usr/lib/httpd/modules"
308 test -d "/usr/lib/apache2/modules" &&
309 module_path="/usr/lib/apache2/modules"
311 bind=
312 test x"$local" = xtrue && bind='127.0.0.1:'
313 echo 'text/css css' > "$fqgitdir/mime.types"
314 cat > "$conf" <<EOF
315 ServerName "git-instaweb"
316 ServerRoot "$root"
317 DocumentRoot "$root"
318 ErrorLog "$fqgitdir/gitweb/$httpd_only/error.log"
319 CustomLog "$fqgitdir/gitweb/$httpd_only/access.log" combined
320 PidFile "$fqgitdir/pid"
321 Listen $bind$port
324 for mod in mime dir env log_config
326 if test -e $module_path/mod_${mod}.so
327 then
328 echo "LoadModule ${mod}_module " \
329 "$module_path/mod_${mod}.so" >> "$conf"
331 done
332 cat >> "$conf" <<EOF
333 TypesConfig "$fqgitdir/mime.types"
334 DirectoryIndex gitweb.cgi
337 # check to see if Dennis Stosberg's mod_perl compatibility patch
338 # (<20060621130708.Gcbc6e5c@leonov.stosberg.net>) has been applied
339 if test -f "$module_path/mod_perl.so" &&
340 sane_grep 'MOD_PERL' "$root/gitweb.cgi" >/dev/null
341 then
342 # favor mod_perl if available
343 cat >> "$conf" <<EOF
344 LoadModule perl_module $module_path/mod_perl.so
345 PerlPassEnv GIT_DIR
346 PerlPassEnv GIT_EXEC_PATH
347 PerlPassEnv GITWEB_CONFIG
348 <Location /gitweb.cgi>
349 SetHandler perl-script
350 PerlResponseHandler ModPerl::Registry
351 PerlOptions +ParseHeaders
352 Options +ExecCGI
353 </Location>
355 else
356 # plain-old CGI
357 resolve_full_httpd
358 list_mods=$(echo "$full_httpd" | sed 's/-f$/-l/')
359 $list_mods | sane_grep 'mod_cgi\.c' >/dev/null 2>&1 || \
360 if test -f "$module_path/mod_cgi.so"
361 then
362 echo "LoadModule cgi_module $module_path/mod_cgi.so" >> "$conf"
363 else
364 $list_mods | grep 'mod_cgid\.c' >/dev/null 2>&1 || \
365 if test -f "$module_path/mod_cgid.so"
366 then
367 echo "LoadModule cgid_module $module_path/mod_cgid.so" \
368 >> "$conf"
369 else
370 echo "You have no CGI support!"
371 exit 2
373 echo "ScriptSock logs/gitweb.sock" >> "$conf"
375 cat >> "$conf" <<EOF
376 PassEnv GIT_DIR
377 PassEnv GIT_EXEC_PATH
378 PassEnv GITWEB_CONFIG
379 AddHandler cgi-script .cgi
380 <Location /gitweb.cgi>
381 Options +ExecCGI
382 </Location>
387 mongoose_conf() {
388 cat > "$conf" <<EOF
389 # Mongoose web server configuration file.
390 # Lines starting with '#' and empty lines are ignored.
391 # For detailed description of every option, visit
392 # http://code.google.com/p/mongoose/wiki/MongooseManual
394 root $root
395 ports $port
396 index_files gitweb.cgi
397 #ssl_cert $fqgitdir/gitweb/ssl_cert.pem
398 error_log $fqgitdir/gitweb/$httpd_only/error.log
399 access_log $fqgitdir/gitweb/$httpd_only/access.log
401 #cgi setup
402 cgi_env PATH=$PATH,GIT_DIR=$GIT_DIR,GIT_EXEC_PATH=$GIT_EXEC_PATH,GITWEB_CONFIG=$GITWEB_CONFIG
403 cgi_interp $PERL
404 cgi_ext cgi,pl
406 # mimetype mapping
407 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
411 plackup_conf () {
412 # generate a standalone 'plackup' server script in $fqgitdir/gitweb
413 # with embedded configuration; it does not use "$conf" file
414 cat > "$fqgitdir/gitweb/gitweb.psgi" <<EOF
415 #!$PERL
417 # gitweb - simple web interface to track changes in git repositories
418 # PSGI wrapper and server starter (see http://plackperl.org)
420 use strict;
422 use IO::Handle;
423 use Plack::MIME;
424 use Plack::Builder;
425 use Plack::App::WrapCGI;
426 use CGI::Emulate::PSGI 0.07; # minimum version required to work with gitweb
428 # mimetype mapping (from lighttpd_conf)
429 Plack::MIME->add_type(
430 ".pdf" => "application/pdf",
431 ".sig" => "application/pgp-signature",
432 ".spl" => "application/futuresplash",
433 ".class" => "application/octet-stream",
434 ".ps" => "application/postscript",
435 ".torrent" => "application/x-bittorrent",
436 ".dvi" => "application/x-dvi",
437 ".gz" => "application/x-gzip",
438 ".pac" => "application/x-ns-proxy-autoconfig",
439 ".swf" => "application/x-shockwave-flash",
440 ".tar.gz" => "application/x-tgz",
441 ".tgz" => "application/x-tgz",
442 ".tar" => "application/x-tar",
443 ".zip" => "application/zip",
444 ".mp3" => "audio/mpeg",
445 ".m3u" => "audio/x-mpegurl",
446 ".wma" => "audio/x-ms-wma",
447 ".wax" => "audio/x-ms-wax",
448 ".ogg" => "application/ogg",
449 ".wav" => "audio/x-wav",
450 ".gif" => "image/gif",
451 ".jpg" => "image/jpeg",
452 ".jpeg" => "image/jpeg",
453 ".png" => "image/png",
454 ".xbm" => "image/x-xbitmap",
455 ".xpm" => "image/x-xpixmap",
456 ".xwd" => "image/x-xwindowdump",
457 ".css" => "text/css",
458 ".html" => "text/html",
459 ".htm" => "text/html",
460 ".js" => "text/javascript",
461 ".asc" => "text/plain",
462 ".c" => "text/plain",
463 ".cpp" => "text/plain",
464 ".log" => "text/plain",
465 ".conf" => "text/plain",
466 ".text" => "text/plain",
467 ".txt" => "text/plain",
468 ".dtd" => "text/xml",
469 ".xml" => "text/xml",
470 ".mpeg" => "video/mpeg",
471 ".mpg" => "video/mpeg",
472 ".mov" => "video/quicktime",
473 ".qt" => "video/quicktime",
474 ".avi" => "video/x-msvideo",
475 ".asf" => "video/x-ms-asf",
476 ".asx" => "video/x-ms-asf",
477 ".wmv" => "video/x-ms-wmv",
478 ".bz2" => "application/x-bzip",
479 ".tbz" => "application/x-bzip-compressed-tar",
480 ".tar.bz2" => "application/x-bzip-compressed-tar",
481 "" => "text/plain"
484 my \$app = builder {
485 # to be able to override \$SIG{__WARN__} to log build time warnings
486 use CGI::Carp; # it sets \$SIG{__WARN__} itself
488 my \$logdir = "$fqgitdir/gitweb/$httpd_only";
489 open my \$access_log_fh, '>>', "\$logdir/access.log"
490 or die "Couldn't open access log '\$logdir/access.log': \$!";
491 open my \$error_log_fh, '>>', "\$logdir/error.log"
492 or die "Couldn't open error log '\$logdir/error.log': \$!";
494 \$access_log_fh->autoflush(1);
495 \$error_log_fh->autoflush(1);
497 # redirect build time warnings to error.log
498 \$SIG{'__WARN__'} = sub {
499 my \$msg = shift;
500 # timestamp warning like in CGI::Carp::warn
501 my \$stamp = CGI::Carp::stamp();
502 \$msg =~ s/^/\$stamp/gm;
503 print \$error_log_fh \$msg;
506 # write errors to error.log, access to access.log
507 enable 'AccessLog',
508 format => "combined",
509 logger => sub { print \$access_log_fh @_; };
510 enable sub {
511 my \$app = shift;
512 sub {
513 my \$env = shift;
514 \$env->{'psgi.errors'} = \$error_log_fh;
515 \$app->(\$env);
518 # gitweb currently doesn't work with $SIG{CHLD} set to 'IGNORE',
519 # because it uses 'close $fd or die...' on piped filehandle $fh
520 # (which causes the parent process to wait for child to finish).
521 enable_if { \$SIG{'CHLD'} eq 'IGNORE' } sub {
522 my \$app = shift;
523 sub {
524 my \$env = shift;
525 local \$SIG{'CHLD'} = 'DEFAULT';
526 local \$SIG{'CLD'} = 'DEFAULT';
527 \$app->(\$env);
530 # serve static files, i.e. stylesheet, images, script
531 enable 'Static',
532 path => sub { m!\.(js|css|png)\$! && s!^/gitweb/!! },
533 root => "$root/",
534 encoding => 'utf-8'; # encoding for 'text/plain' files
535 # convert CGI application to PSGI app
536 Plack::App::WrapCGI->new(script => "$root/gitweb.cgi")->to_app;
539 # make it runnable as standalone app,
540 # like it would be run via 'plackup' utility
541 if (__FILE__ eq \$0) {
542 require Plack::Runner;
544 my \$runner = Plack::Runner->new();
545 \$runner->parse_options(qw(--env deployment --port $port),
546 "$local" ? qw(--host 127.0.0.1) : ());
547 \$runner->run(\$app);
549 __END__
552 chmod a+x "$fqgitdir/gitweb/gitweb.psgi"
553 # configuration is embedded in server script file, gitweb.psgi
554 rm -f "$conf"
557 gitweb_conf() {
558 cat > "$fqgitdir/gitweb/gitweb_config.perl" <<EOF
559 #!/usr/bin/perl
560 our \$projectroot = "$(dirname "$fqgitdir")";
561 our \$git_temp = "$fqgitdir/gitweb/tmp";
562 our \$projects_list = \$projectroot;
566 gitweb_conf
568 resolve_full_httpd
569 mkdir -p "$fqgitdir/gitweb/$httpd_only"
571 case "$httpd" in
572 *lighttpd*)
573 lighttpd_conf
575 *apache2*|*httpd*)
576 apache2_conf
578 webrick)
579 webrick_conf
581 *mongoose*)
582 mongoose_conf
584 *plackup*)
585 plackup_conf
588 echo "Unknown httpd specified: $httpd"
589 exit 1
591 esac
593 start_httpd
594 url=http://127.0.0.1:$port
596 if test -n "$browser"; then
597 httpd_is_ready && git web--browse -b "$browser" $url || echo $url
598 else
599 httpd_is_ready && git web--browse -c "instaweb.browser" $url || echo $url