Merge branch 'maint'
[git/git.git] / git-instaweb.sh
CommitLineData
a51d37c1
EW
1#!/bin/sh
2#
3# Copyright (c) 2006 Eric Wong
4#
c2db2e0e 5
c5699693 6PERL='@@PERL@@'
c2db2e0e
PH
7OPTIONS_KEEPDASHDASH=
8OPTIONS_SPEC="\
1b1dd23f 9git instaweb [options] (--start | --stop | --restart)
c2db2e0e
PH
10--
11l,local only bind on 127.0.0.1
12p,port= the port to bind to
13d,httpd= the command to launch
14b,browser= the browser to launch
15m,module-path= the module path (only needed for apache2)
16 Action
17stop stop the web server
18start start the web server
19restart restart the web server
20"
a51d37c1
EW
21
22. git-sh-setup
23
b2bc9a30 24fqgitdir="$GIT_DIR"
43d60d2e
FP
25local="$(git config --bool --get instaweb.local)"
26httpd="$(git config --get instaweb.httpd)"
c0cb4ed3 27root="$(git config --get instaweb.gitwebdir)"
43d60d2e
FP
28port=$(git config --get instaweb.port)
29module_path="$(git config --get instaweb.modulepath)"
a51d37c1 30
9126425d 31conf="$GIT_DIR/gitweb/httpd.conf"
a51d37c1
EW
32
33# Defaults:
34
82e5a82f 35# if installed, it doesn't need further configuration (module_path)
a51d37c1
EW
36test -z "$httpd" && httpd='lighttpd -f'
37
c0cb4ed3
PKS
38# Default is @@GITWEBDIR@@
39test -z "$root" && root='@@GITWEBDIR@@'
40
a51d37c1
EW
41# any untaken local port will do...
42test -z "$port" && port=1234
43
43d60d2e
FP
44resolve_full_httpd () {
45 case "$httpd" in
4bdf8599
DM
46 *apache2*|*lighttpd*|*httpd*)
47 # yes, *httpd* covers *lighttpd* above, but it is there for clarity
43d60d2e 48 # ensure that the apache2/lighttpd command ends with "-f"
e1622bfc 49 if ! echo "$httpd" | sane_grep -- '-f *$' >/dev/null 2>&1
43d60d2e
FP
50 then
51 httpd="$httpd -f"
52 fi
53 ;;
78646987
JN
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
59 ;;
43d60d2e
FP
60 esac
61
62 httpd_only="$(echo $httpd | cut -f1 -d' ')"
e47eec8f 63 if case "$httpd_only" in /*) : ;; *) which $httpd_only >/dev/null 2>&1;; esac
a51d37c1 64 then
43d60d2e 65 full_httpd=$httpd
a51d37c1
EW
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
14b45b66
MD
69 # in addition, we may have generated a server script
70 # in $fqgitdir/gitweb.
c0cb4ed3 71 for i in /usr/local/sbin /usr/sbin "$root" "$fqgitdir/gitweb"
a51d37c1
EW
72 do
73 if test -x "$i/$httpd_only"
74 then
43d60d2e 75 full_httpd=$i/$httpd
a51d37c1
EW
76 return
77 fi
78 done
43d60d2e
FP
79
80 echo >&2 "$httpd_only not found. Install $httpd_only or use" \
81 "--httpd to specify another httpd daemon."
f281e3a1 82 exit 1
a51d37c1 83 fi
43d60d2e
FP
84}
85
86start_httpd () {
0b624b4c
SB
87 if test -f "$fqgitdir/pid"; then
88 say "Instance already running. Restarting..."
89 stop_httpd
90 fi
91
43d60d2e
FP
92 # here $httpd should have a meaningful value
93 resolve_full_httpd
94
95 # don't quote $full_httpd, there can be arguments to it (-f)
0ded4758 96 case "$httpd" in
78646987
JN
97 *mongoose*|*plackup*)
98 #These servers don't have a daemon mode so we'll have to fork it
0ded4758
WL
99 $full_httpd "$fqgitdir/gitweb/httpd.conf" &
100 #Save the pid before doing anything else (we'll print it later)
101 pid=$!
102
103 if test $? != 0; then
104 echo "Could not execute http daemon $httpd."
105 exit 1
106 fi
107
108 cat > "$fqgitdir/pid" <<EOF
109$pid
110EOF
111 ;;
112 *)
113 $full_httpd "$fqgitdir/gitweb/httpd.conf"
114 if test $? != 0; then
115 echo "Could not execute http daemon $httpd."
116 exit 1
117 fi
118 ;;
119 esac
a51d37c1
EW
120}
121
122stop_httpd () {
43d60d2e 123 test -f "$fqgitdir/pid" && kill $(cat "$fqgitdir/pid")
d1127622 124 rm -f "$fqgitdir/pid"
a51d37c1
EW
125}
126
d94775e1
JN
127httpd_is_ready () {
128 "$PERL" -MIO::Socket::INET -e "
129local \$| = 1; # turn on autoflush
130exit if (IO::Socket::INET->new('127.0.0.1:$port'));
131print 'Waiting for \'$httpd\' to start ..';
132do {
133 print '.';
134 sleep(1);
135} until (IO::Socket::INET->new('127.0.0.1:$port'));
136print qq! (done)\n!;
137"
138}
139
822f7c73 140while test $# != 0
a51d37c1
EW
141do
142 case "$1" in
143 --stop|stop)
144 stop_httpd
145 exit 0
146 ;;
147 --start|start)
148 start_httpd
149 exit 0
150 ;;
151 --restart|restart)
152 stop_httpd
153 start_httpd
154 exit 0
155 ;;
c2db2e0e 156 -l|--local)
a51d37c1
EW
157 local=true
158 ;;
c2db2e0e
PH
159 -d|--httpd)
160 shift
161 httpd="$1"
162 ;;
163 -b|--browser)
164 shift
165 browser="$1"
a51d37c1 166 ;;
c2db2e0e
PH
167 -p|--port)
168 shift
169 port="$1"
a51d37c1 170 ;;
c2db2e0e
PH
171 -m|--module-path)
172 shift
173 module_path="$1"
a51d37c1 174 ;;
c2db2e0e 175 --)
a51d37c1
EW
176 ;;
177 *)
178 usage
179 ;;
180 esac
181 shift
182done
183
184mkdir -p "$GIT_DIR/gitweb/tmp"
43d60d2e 185GIT_EXEC_PATH="$(git --exec-path)"
a51d37c1 186GIT_DIR="$fqgitdir"
c0cb4ed3
PKS
187GITWEB_CONFIG="$fqgitdir/gitweb/gitweb_config.perl"
188export GIT_EXEC_PATH GIT_DIR GITWEB_CONFIG
a51d37c1 189
425b78e8
MD
190webrick_conf () {
191 # generate a standalone server script in $fqgitdir/gitweb.
192 cat >"$fqgitdir/gitweb/$httpd.rb" <<EOF
193require 'webrick'
194require 'yaml'
195options = YAML::load_file(ARGV[0])
196options[:StartCallback] = proc do
197 File.open(options[:PidFile],"w") do |f|
198 f.puts Process.pid
199 end
200end
201options[:ServerType] = WEBrick::Daemon
202server = WEBrick::HTTPServer.new(options)
203['INT', 'TERM'].each do |signal|
204 trap(signal) {server.shutdown}
205end
206server.start
207EOF
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
214exec ruby "$fqgitdir/gitweb/$httpd.rb" \$*
215EOF
216 chmod +x "$fqgitdir/gitweb/$httpd"
217
218 cat >"$conf" <<EOF
219:Port: $port
c0cb4ed3 220:DocumentRoot: "$root"
425b78e8
MD
221:DirectoryIndex: ["gitweb.cgi"]
222:PidFile: "$fqgitdir/pid"
223EOF
224 test "$local" = true && echo ':BindAddress: "127.0.0.1"' >> "$conf"
225}
226
a51d37c1
EW
227lighttpd_conf () {
228 cat > "$conf" <<EOF
c0cb4ed3 229server.document-root = "$root"
a51d37c1 230server.port = $port
e47eec8f 231server.modules = ( "mod_setenv", "mod_cgi" )
a51d37c1
EW
232server.indexfiles = ( "gitweb.cgi" )
233server.pid-file = "$fqgitdir/pid"
be5347b3 234server.errorlog = "$fqgitdir/gitweb/$httpd_only/error.log"
e47eec8f
RJ
235
236# to enable, add "mod_access", "mod_accesslog" to server.modules
237# variable above and uncomment this
be5347b3 238#accesslog.filename = "$fqgitdir/gitweb/$httpd_only/access.log"
e47eec8f 239
c0cb4ed3 240setenv.add-environment = ( "PATH" => env.PATH, "GITWEB_CONFIG" => env.GITWEB_CONFIG )
e47eec8f 241
a51d37c1 242cgi.assign = ( ".cgi" => "" )
e47eec8f
RJ
243
244# mimetype mapping
245mimetype.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"
298 )
a51d37c1 299EOF
9126425d 300 test x"$local" = xtrue && echo 'server.bind = "127.0.0.1"' >> "$conf"
a51d37c1
EW
301}
302
303apache2_conf () {
4bdf8599
DM
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"
310 fi
a51d37c1 311 bind=
9126425d 312 test x"$local" = xtrue && bind='127.0.0.1:'
e24c76bf 313 echo 'text/css css' > "$fqgitdir/mime.types"
a51d37c1 314 cat > "$conf" <<EOF
44a167b0 315ServerName "git-instaweb"
c0cb4ed3
PKS
316ServerRoot "$root"
317DocumentRoot "$root"
be5347b3
PKS
318ErrorLog "$fqgitdir/gitweb/$httpd_only/error.log"
319CustomLog "$fqgitdir/gitweb/$httpd_only/access.log" combined
a51d37c1
EW
320PidFile "$fqgitdir/pid"
321Listen $bind$port
44a167b0
EW
322EOF
323
4bdf8599
DM
324 for mod in mime dir env log_config
325 do
326 if test -e $module_path/mod_${mod}.so
327 then
44a167b0
EW
328 echo "LoadModule ${mod}_module " \
329 "$module_path/mod_${mod}.so" >> "$conf"
330 fi
331 done
332 cat >> "$conf" <<EOF
e24c76bf 333TypesConfig "$fqgitdir/mime.types"
a51d37c1
EW
334DirectoryIndex gitweb.cgi
335EOF
336
337 # check to see if Dennis Stosberg's mod_perl compatibility patch
338 # (<20060621130708.Gcbc6e5c@leonov.stosberg.net>) has been applied
e1622bfc 339 if test -f "$module_path/mod_perl.so" &&
c0cb4ed3 340 sane_grep 'MOD_PERL' "$root/gitweb.cgi" >/dev/null
a51d37c1
EW
341 then
342 # favor mod_perl if available
343 cat >> "$conf" <<EOF
344LoadModule perl_module $module_path/mod_perl.so
345PerlPassEnv GIT_DIR
2989f516 346PerlPassEnv GIT_EXEC_PATH
c0cb4ed3 347PerlPassEnv GITWEB_CONFIG
a51d37c1
EW
348<Location /gitweb.cgi>
349 SetHandler perl-script
350 PerlResponseHandler ModPerl::Registry
351 PerlOptions +ParseHeaders
352 Options +ExecCGI
353</Location>
354EOF
355 else
356 # plain-old CGI
43d60d2e 357 resolve_full_httpd
9524cf29 358 list_mods=$(echo "$full_httpd" | sed 's/-f$/-l/')
e1622bfc 359 $list_mods | sane_grep 'mod_cgi\.c' >/dev/null 2>&1 || \
10d1432a
MR
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
372 fi
373 echo "ScriptSock logs/gitweb.sock" >> "$conf"
374 fi
a51d37c1 375 cat >> "$conf" <<EOF
2989f516
DM
376PassEnv GIT_DIR
377PassEnv GIT_EXEC_PATH
378PassEnv GITWEB_CONFIG
a51d37c1
EW
379AddHandler cgi-script .cgi
380<Location /gitweb.cgi>
381 Options +ExecCGI
382</Location>
383EOF
384 fi
385}
386
0ded4758
WL
387mongoose_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
393
c0cb4ed3 394root $root
0ded4758
WL
395ports $port
396index_files gitweb.cgi
397#ssl_cert $fqgitdir/gitweb/ssl_cert.pem
be5347b3
PKS
398error_log $fqgitdir/gitweb/$httpd_only/error.log
399access_log $fqgitdir/gitweb/$httpd_only/access.log
0ded4758
WL
400
401#cgi setup
c0cb4ed3 402cgi_env PATH=$PATH,GIT_DIR=$GIT_DIR,GIT_EXEC_PATH=$GIT_EXEC_PATH,GITWEB_CONFIG=$GITWEB_CONFIG
0ded4758
WL
403cgi_interp $PERL
404cgi_ext cgi,pl
405
406# mimetype mapping
407mime_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
408EOF
409}
410
78646987
JN
411plackup_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
416
417# gitweb - simple web interface to track changes in git repositories
418# PSGI wrapper and server starter (see http://plackperl.org)
419
420use strict;
421
422use IO::Handle;
423use Plack::MIME;
424use Plack::Builder;
425use Plack::App::WrapCGI;
426use CGI::Emulate::PSGI 0.07; # minimum version required to work with gitweb
427
428# mimetype mapping (from lighttpd_conf)
429Plack::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"
482);
483
484my \$app = builder {
485 # to be able to override \$SIG{__WARN__} to log build time warnings
486 use CGI::Carp; # it sets \$SIG{__WARN__} itself
487
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': \$!";
493
494 \$access_log_fh->autoflush(1);
495 \$error_log_fh->autoflush(1);
496
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;
504 };
505
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);
516 }
517 };
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);
528 }
529 };
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;
537};
538
539# make it runnable as standalone app,
540# like it would be run via 'plackup' utility
541if (__FILE__ eq \$0) {
542 require Plack::Runner;
543
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);
548}
549__END__
550EOF
551
552 chmod a+x "$fqgitdir/gitweb/gitweb.psgi"
553 # configuration is embedded in server script file, gitweb.psgi
554 rm -f "$conf"
555}
556
c0cb4ed3
PKS
557gitweb_conf() {
558 cat > "$fqgitdir/gitweb/gitweb_config.perl" <<EOF
559#!/usr/bin/perl
560our \$projectroot = "$(dirname "$fqgitdir")";
561our \$git_temp = "$fqgitdir/gitweb/tmp";
562our \$projects_list = \$projectroot;
563EOF
4af819d4
JN
564}
565
c0cb4ed3 566gitweb_conf
a51d37c1 567
be5347b3
PKS
568resolve_full_httpd
569mkdir -p "$fqgitdir/gitweb/$httpd_only"
570
a51d37c1
EW
571case "$httpd" in
572*lighttpd*)
573 lighttpd_conf
574 ;;
4bdf8599 575*apache2*|*httpd*)
a51d37c1
EW
576 apache2_conf
577 ;;
425b78e8
MD
578webrick)
579 webrick_conf
580 ;;
0ded4758
WL
581*mongoose*)
582 mongoose_conf
583 ;;
78646987
JN
584*plackup*)
585 plackup_conf
586 ;;
a51d37c1
EW
587*)
588 echo "Unknown httpd specified: $httpd"
589 exit 1
590 ;;
591esac
592
593start_httpd
5209eda8 594url=http://127.0.0.1:$port
2e0c2902
CC
595
596if test -n "$browser"; then
d94775e1 597 httpd_is_ready && git web--browse -b "$browser" $url || echo $url
2e0c2902 598else
d94775e1 599 httpd_is_ready && git web--browse -c "instaweb.browser" $url || echo $url
2e0c2902 600fi