Merge branch 'ks/no-textconv-symlink'
[git/kirr.git] / git-instaweb.sh
blobe6f6ecda177c66a79b18359f7bf8cad9c4744c8c
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.rb in
62 # $fqgitdir/gitweb
63 full_httpd="$fqgitdir/gitweb/webrick.rb"
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 #!/usr/bin/env ruby
218 require 'webrick'
219 require 'logger'
220 options = {
221 :Port => $port,
222 :DocumentRoot => "$root",
223 :Logger => Logger.new('$fqgitdir/gitweb/error.log'),
224 :AccessLog => [
225 [ Logger.new('$fqgitdir/gitweb/access.log'),
226 WEBrick::AccessLog::COMBINED_LOG_FORMAT ]
228 :DirectoryIndex => ["gitweb.cgi"],
229 :CGIInterpreter => "$wrapper",
230 :StartCallback => lambda do
231 File.open("$fqgitdir/pid", "w") { |f| f.puts Process.pid }
232 end,
233 :ServerType => WEBrick::Daemon,
235 options[:BindAddress] = '127.0.0.1' if "$local" == "true"
236 server = WEBrick::HTTPServer.new(options)
237 ['INT', 'TERM'].each do |signal|
238 trap(signal) {server.shutdown}
240 server.start
242 chmod +x "$fqgitdir/gitweb/$httpd.rb"
243 # configuration is embedded in server script file, webrick.rb
244 rm -f "$conf"
247 lighttpd_conf () {
248 cat > "$conf" <<EOF
249 server.document-root = "$root"
250 server.port = $port
251 server.modules = ( "mod_setenv", "mod_cgi" )
252 server.indexfiles = ( "gitweb.cgi" )
253 server.pid-file = "$fqgitdir/pid"
254 server.errorlog = "$fqgitdir/gitweb/$httpd_only/error.log"
256 # to enable, add "mod_access", "mod_accesslog" to server.modules
257 # variable above and uncomment this
258 #accesslog.filename = "$fqgitdir/gitweb/$httpd_only/access.log"
260 setenv.add-environment = ( "PATH" => env.PATH, "GITWEB_CONFIG" => env.GITWEB_CONFIG )
262 cgi.assign = ( ".cgi" => "" )
264 # mimetype mapping
265 mimetype.assign = (
266 ".pdf" => "application/pdf",
267 ".sig" => "application/pgp-signature",
268 ".spl" => "application/futuresplash",
269 ".class" => "application/octet-stream",
270 ".ps" => "application/postscript",
271 ".torrent" => "application/x-bittorrent",
272 ".dvi" => "application/x-dvi",
273 ".gz" => "application/x-gzip",
274 ".pac" => "application/x-ns-proxy-autoconfig",
275 ".swf" => "application/x-shockwave-flash",
276 ".tar.gz" => "application/x-tgz",
277 ".tgz" => "application/x-tgz",
278 ".tar" => "application/x-tar",
279 ".zip" => "application/zip",
280 ".mp3" => "audio/mpeg",
281 ".m3u" => "audio/x-mpegurl",
282 ".wma" => "audio/x-ms-wma",
283 ".wax" => "audio/x-ms-wax",
284 ".ogg" => "application/ogg",
285 ".wav" => "audio/x-wav",
286 ".gif" => "image/gif",
287 ".jpg" => "image/jpeg",
288 ".jpeg" => "image/jpeg",
289 ".png" => "image/png",
290 ".xbm" => "image/x-xbitmap",
291 ".xpm" => "image/x-xpixmap",
292 ".xwd" => "image/x-xwindowdump",
293 ".css" => "text/css",
294 ".html" => "text/html",
295 ".htm" => "text/html",
296 ".js" => "text/javascript",
297 ".asc" => "text/plain",
298 ".c" => "text/plain",
299 ".cpp" => "text/plain",
300 ".log" => "text/plain",
301 ".conf" => "text/plain",
302 ".text" => "text/plain",
303 ".txt" => "text/plain",
304 ".dtd" => "text/xml",
305 ".xml" => "text/xml",
306 ".mpeg" => "video/mpeg",
307 ".mpg" => "video/mpeg",
308 ".mov" => "video/quicktime",
309 ".qt" => "video/quicktime",
310 ".avi" => "video/x-msvideo",
311 ".asf" => "video/x-ms-asf",
312 ".asx" => "video/x-ms-asf",
313 ".wmv" => "video/x-ms-wmv",
314 ".bz2" => "application/x-bzip",
315 ".tbz" => "application/x-bzip-compressed-tar",
316 ".tar.bz2" => "application/x-bzip-compressed-tar",
317 "" => "text/plain"
320 test x"$local" = xtrue && echo 'server.bind = "127.0.0.1"' >> "$conf"
323 apache2_conf () {
324 if test -z "$module_path"
325 then
326 test -d "/usr/lib/httpd/modules" &&
327 module_path="/usr/lib/httpd/modules"
328 test -d "/usr/lib/apache2/modules" &&
329 module_path="/usr/lib/apache2/modules"
331 bind=
332 test x"$local" = xtrue && bind='127.0.0.1:'
333 echo 'text/css css' > "$fqgitdir/mime.types"
334 cat > "$conf" <<EOF
335 ServerName "git-instaweb"
336 ServerRoot "$root"
337 DocumentRoot "$root"
338 ErrorLog "$fqgitdir/gitweb/$httpd_only/error.log"
339 CustomLog "$fqgitdir/gitweb/$httpd_only/access.log" combined
340 PidFile "$fqgitdir/pid"
341 Listen $bind$port
344 for mod in mime dir env log_config
346 if test -e $module_path/mod_${mod}.so
347 then
348 echo "LoadModule ${mod}_module " \
349 "$module_path/mod_${mod}.so" >> "$conf"
351 done
352 cat >> "$conf" <<EOF
353 TypesConfig "$fqgitdir/mime.types"
354 DirectoryIndex gitweb.cgi
357 # check to see if Dennis Stosberg's mod_perl compatibility patch
358 # (<20060621130708.Gcbc6e5c@leonov.stosberg.net>) has been applied
359 if test -f "$module_path/mod_perl.so" &&
360 sane_grep 'MOD_PERL' "$root/gitweb.cgi" >/dev/null
361 then
362 # favor mod_perl if available
363 cat >> "$conf" <<EOF
364 LoadModule perl_module $module_path/mod_perl.so
365 PerlPassEnv GIT_DIR
366 PerlPassEnv GIT_EXEC_PATH
367 PerlPassEnv GITWEB_CONFIG
368 <Location /gitweb.cgi>
369 SetHandler perl-script
370 PerlResponseHandler ModPerl::Registry
371 PerlOptions +ParseHeaders
372 Options +ExecCGI
373 </Location>
375 else
376 # plain-old CGI
377 resolve_full_httpd
378 list_mods=$(echo "$full_httpd" | sed 's/-f$/-l/')
379 $list_mods | sane_grep 'mod_cgi\.c' >/dev/null 2>&1 || \
380 if test -f "$module_path/mod_cgi.so"
381 then
382 echo "LoadModule cgi_module $module_path/mod_cgi.so" >> "$conf"
383 else
384 $list_mods | grep 'mod_cgid\.c' >/dev/null 2>&1 || \
385 if test -f "$module_path/mod_cgid.so"
386 then
387 echo "LoadModule cgid_module $module_path/mod_cgid.so" \
388 >> "$conf"
389 else
390 echo "You have no CGI support!"
391 exit 2
393 echo "ScriptSock logs/gitweb.sock" >> "$conf"
395 cat >> "$conf" <<EOF
396 PassEnv GIT_DIR
397 PassEnv GIT_EXEC_PATH
398 PassEnv GITWEB_CONFIG
399 AddHandler cgi-script .cgi
400 <Location /gitweb.cgi>
401 Options +ExecCGI
402 </Location>
407 mongoose_conf() {
408 cat > "$conf" <<EOF
409 # Mongoose web server configuration file.
410 # Lines starting with '#' and empty lines are ignored.
411 # For detailed description of every option, visit
412 # http://code.google.com/p/mongoose/wiki/MongooseManual
414 root $root
415 ports $port
416 index_files gitweb.cgi
417 #ssl_cert $fqgitdir/gitweb/ssl_cert.pem
418 error_log $fqgitdir/gitweb/$httpd_only/error.log
419 access_log $fqgitdir/gitweb/$httpd_only/access.log
421 #cgi setup
422 cgi_env PATH=$PATH,GIT_DIR=$GIT_DIR,GIT_EXEC_PATH=$GIT_EXEC_PATH,GITWEB_CONFIG=$GITWEB_CONFIG
423 cgi_interp $PERL
424 cgi_ext cgi,pl
426 # mimetype mapping
427 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
431 plackup_conf () {
432 # generate a standalone 'plackup' server script in $fqgitdir/gitweb
433 # with embedded configuration; it does not use "$conf" file
434 cat > "$fqgitdir/gitweb/gitweb.psgi" <<EOF
435 #!$PERL
437 # gitweb - simple web interface to track changes in git repositories
438 # PSGI wrapper and server starter (see http://plackperl.org)
440 use strict;
442 use IO::Handle;
443 use Plack::MIME;
444 use Plack::Builder;
445 use Plack::App::WrapCGI;
446 use CGI::Emulate::PSGI 0.07; # minimum version required to work with gitweb
448 # mimetype mapping (from lighttpd_conf)
449 Plack::MIME->add_type(
450 ".pdf" => "application/pdf",
451 ".sig" => "application/pgp-signature",
452 ".spl" => "application/futuresplash",
453 ".class" => "application/octet-stream",
454 ".ps" => "application/postscript",
455 ".torrent" => "application/x-bittorrent",
456 ".dvi" => "application/x-dvi",
457 ".gz" => "application/x-gzip",
458 ".pac" => "application/x-ns-proxy-autoconfig",
459 ".swf" => "application/x-shockwave-flash",
460 ".tar.gz" => "application/x-tgz",
461 ".tgz" => "application/x-tgz",
462 ".tar" => "application/x-tar",
463 ".zip" => "application/zip",
464 ".mp3" => "audio/mpeg",
465 ".m3u" => "audio/x-mpegurl",
466 ".wma" => "audio/x-ms-wma",
467 ".wax" => "audio/x-ms-wax",
468 ".ogg" => "application/ogg",
469 ".wav" => "audio/x-wav",
470 ".gif" => "image/gif",
471 ".jpg" => "image/jpeg",
472 ".jpeg" => "image/jpeg",
473 ".png" => "image/png",
474 ".xbm" => "image/x-xbitmap",
475 ".xpm" => "image/x-xpixmap",
476 ".xwd" => "image/x-xwindowdump",
477 ".css" => "text/css",
478 ".html" => "text/html",
479 ".htm" => "text/html",
480 ".js" => "text/javascript",
481 ".asc" => "text/plain",
482 ".c" => "text/plain",
483 ".cpp" => "text/plain",
484 ".log" => "text/plain",
485 ".conf" => "text/plain",
486 ".text" => "text/plain",
487 ".txt" => "text/plain",
488 ".dtd" => "text/xml",
489 ".xml" => "text/xml",
490 ".mpeg" => "video/mpeg",
491 ".mpg" => "video/mpeg",
492 ".mov" => "video/quicktime",
493 ".qt" => "video/quicktime",
494 ".avi" => "video/x-msvideo",
495 ".asf" => "video/x-ms-asf",
496 ".asx" => "video/x-ms-asf",
497 ".wmv" => "video/x-ms-wmv",
498 ".bz2" => "application/x-bzip",
499 ".tbz" => "application/x-bzip-compressed-tar",
500 ".tar.bz2" => "application/x-bzip-compressed-tar",
501 "" => "text/plain"
504 my \$app = builder {
505 # to be able to override \$SIG{__WARN__} to log build time warnings
506 use CGI::Carp; # it sets \$SIG{__WARN__} itself
508 my \$logdir = "$fqgitdir/gitweb/$httpd_only";
509 open my \$access_log_fh, '>>', "\$logdir/access.log"
510 or die "Couldn't open access log '\$logdir/access.log': \$!";
511 open my \$error_log_fh, '>>', "\$logdir/error.log"
512 or die "Couldn't open error log '\$logdir/error.log': \$!";
514 \$access_log_fh->autoflush(1);
515 \$error_log_fh->autoflush(1);
517 # redirect build time warnings to error.log
518 \$SIG{'__WARN__'} = sub {
519 my \$msg = shift;
520 # timestamp warning like in CGI::Carp::warn
521 my \$stamp = CGI::Carp::stamp();
522 \$msg =~ s/^/\$stamp/gm;
523 print \$error_log_fh \$msg;
526 # write errors to error.log, access to access.log
527 enable 'AccessLog',
528 format => "combined",
529 logger => sub { print \$access_log_fh @_; };
530 enable sub {
531 my \$app = shift;
532 sub {
533 my \$env = shift;
534 \$env->{'psgi.errors'} = \$error_log_fh;
535 \$app->(\$env);
538 # gitweb currently doesn't work with $SIG{CHLD} set to 'IGNORE',
539 # because it uses 'close $fd or die...' on piped filehandle $fh
540 # (which causes the parent process to wait for child to finish).
541 enable_if { \$SIG{'CHLD'} eq 'IGNORE' } sub {
542 my \$app = shift;
543 sub {
544 my \$env = shift;
545 local \$SIG{'CHLD'} = 'DEFAULT';
546 local \$SIG{'CLD'} = 'DEFAULT';
547 \$app->(\$env);
550 # serve static files, i.e. stylesheet, images, script
551 enable 'Static',
552 path => sub { m!\.(js|css|png)\$! && s!^/gitweb/!! },
553 root => "$root/",
554 encoding => 'utf-8'; # encoding for 'text/plain' files
555 # convert CGI application to PSGI app
556 Plack::App::WrapCGI->new(script => "$root/gitweb.cgi")->to_app;
559 # make it runnable as standalone app,
560 # like it would be run via 'plackup' utility
561 if (__FILE__ eq \$0) {
562 require Plack::Runner;
564 my \$runner = Plack::Runner->new();
565 \$runner->parse_options(qw(--env deployment --port $port),
566 "$local" ? qw(--host 127.0.0.1) : ());
567 \$runner->run(\$app);
569 __END__
572 chmod a+x "$fqgitdir/gitweb/gitweb.psgi"
573 # configuration is embedded in server script file, gitweb.psgi
574 rm -f "$conf"
577 gitweb_conf() {
578 cat > "$fqgitdir/gitweb/gitweb_config.perl" <<EOF
579 #!/usr/bin/perl
580 our \$projectroot = "$(dirname "$fqgitdir")";
581 our \$git_temp = "$fqgitdir/gitweb/tmp";
582 our \$projects_list = \$projectroot;
586 gitweb_conf
588 resolve_full_httpd
589 mkdir -p "$fqgitdir/gitweb/$httpd_only"
591 case "$httpd" in
592 *lighttpd*)
593 lighttpd_conf
595 *apache2*|*httpd*)
596 apache2_conf
598 webrick)
599 webrick_conf
601 *mongoose*)
602 mongoose_conf
604 *plackup*)
605 plackup_conf
608 echo "Unknown httpd specified: $httpd"
609 exit 1
611 esac
613 start_httpd
614 url=http://127.0.0.1:$port
616 if test -n "$browser"; then
617 httpd_is_ready && git web--browse -b "$browser" $url || echo $url
618 else
619 httpd_is_ready && git web--browse -c "instaweb.browser" $url || echo $url