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