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