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