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