1 #!/bin/sh
2 #
3 # Copyright (c) 2006 Eric Wong
4 #
6 PERL='@@PERL@@'
7 OPTIONS_KEEPDASHDASH=
8 OPTIONS_SPEC="\
9 git instaweb [options] (--start | --stop | --restart)
10 --
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
20 "
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"
52 fi
53 ;;
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 ;;
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
66 ;;
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"
79 do
80 if test -x "$i/$httpd_only"
81 then
82 full_httpd=$i/$httpd
83 return
84 fi
85 done
87 echo >&2 "$httpd_only not found. Install $httpd_only or use" \
88 "--httpd to specify another httpd daemon."
89 exit 1
90 fi
91 }
93 start_httpd () {
94 if test -f "$fqgitdir/pid"; then
95 say "Instance already running. Restarting..."
96 stop_httpd
97 fi
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
113 fi
115 cat > "$fqgitdir/pid" <<EOF
116 $pid
117 EOF
118 ;;
119 *)
120 $full_httpd "$fqgitdir/gitweb/httpd.conf"
121 if test $? != 0; then
122 echo "Could not execute http daemon $httpd."
123 exit 1
124 fi
125 ;;
126 esac
127 }
129 stop_httpd () {
130 test -f "$fqgitdir/pid" && kill $(cat "$fqgitdir/pid")
131 rm -f "$fqgitdir/pid"
132 }
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!;
144 "
145 }
147 while test $# != 0
148 do
149 case "$1" in
150 --stop|stop)
151 stop_httpd
152 exit 0
153 ;;
154 --start|start)
155 start_httpd
156 exit 0
157 ;;
158 --restart|restart)
159 stop_httpd
160 start_httpd
161 exit 0
162 ;;
163 -l|--local)
164 local=true
165 ;;
166 -d|--httpd)
167 shift
168 httpd="$1"
169 ;;
170 -b|--browser)
171 shift
172 browser="$1"
173 ;;
174 -p|--port)
175 shift
176 port="$1"
177 ;;
178 -m|--module-path)
179 shift
180 module_path="$1"
181 ;;
182 --)
183 ;;
184 *)
185 usage
186 ;;
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
210 EOF
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 options = {
220 :Port => $port,
221 :DocumentRoot => "$root",
222 :DirectoryIndex => ["gitweb.cgi"],
223 :CGIInterpreter => "$wrapper",
224 :StartCallback => lambda do
225 File.open("$fqgitdir/pid", "w") { |f| f.puts Process.pid }
226 end,
227 :ServerType => WEBrick::Daemon,
228 }
229 options[:BindAddress] = '127.0.0.1' if "$local" == "true"
230 server = WEBrick::HTTPServer.new(options)
231 ['INT', 'TERM'].each do |signal|
232 trap(signal) {server.shutdown}
233 end
234 server.start
235 EOF
236 chmod +x "$fqgitdir/gitweb/$httpd.rb"
237 # configuration is embedded in server script file, webrick.rb
238 rm -f "$conf"
239 }
241 lighttpd_conf () {
242 cat > "$conf" <<EOF
243 server.document-root = "$root"
244 server.port = $port
245 server.modules = ( "mod_setenv", "mod_cgi" )
246 server.indexfiles = ( "gitweb.cgi" )
247 server.pid-file = "$fqgitdir/pid"
248 server.errorlog = "$fqgitdir/gitweb/$httpd_only/error.log"
250 # to enable, add "mod_access", "mod_accesslog" to server.modules
251 # variable above and uncomment this
252 #accesslog.filename = "$fqgitdir/gitweb/$httpd_only/access.log"
254 setenv.add-environment = ( "PATH" => env.PATH, "GITWEB_CONFIG" => env.GITWEB_CONFIG )
256 cgi.assign = ( ".cgi" => "" )
258 # mimetype mapping
259 mimetype.assign = (
260 ".pdf" => "application/pdf",
261 ".sig" => "application/pgp-signature",
262 ".spl" => "application/futuresplash",
263 ".class" => "application/octet-stream",
264 ".ps" => "application/postscript",
265 ".torrent" => "application/x-bittorrent",
266 ".dvi" => "application/x-dvi",
267 ".gz" => "application/x-gzip",
268 ".pac" => "application/x-ns-proxy-autoconfig",
269 ".swf" => "application/x-shockwave-flash",
270 ".tar.gz" => "application/x-tgz",
271 ".tgz" => "application/x-tgz",
272 ".tar" => "application/x-tar",
273 ".zip" => "application/zip",
274 ".mp3" => "audio/mpeg",
275 ".m3u" => "audio/x-mpegurl",
276 ".wma" => "audio/x-ms-wma",
277 ".wax" => "audio/x-ms-wax",
278 ".ogg" => "application/ogg",
279 ".wav" => "audio/x-wav",
280 ".gif" => "image/gif",
281 ".jpg" => "image/jpeg",
282 ".jpeg" => "image/jpeg",
283 ".png" => "image/png",
284 ".xbm" => "image/x-xbitmap",
285 ".xpm" => "image/x-xpixmap",
286 ".xwd" => "image/x-xwindowdump",
287 ".css" => "text/css",
288 ".html" => "text/html",
289 ".htm" => "text/html",
290 ".js" => "text/javascript",
291 ".asc" => "text/plain",
292 ".c" => "text/plain",
293 ".cpp" => "text/plain",
294 ".log" => "text/plain",
295 ".conf" => "text/plain",
296 ".text" => "text/plain",
297 ".txt" => "text/plain",
298 ".dtd" => "text/xml",
299 ".xml" => "text/xml",
300 ".mpeg" => "video/mpeg",
301 ".mpg" => "video/mpeg",
302 ".mov" => "video/quicktime",
303 ".qt" => "video/quicktime",
304 ".avi" => "video/x-msvideo",
305 ".asf" => "video/x-ms-asf",
306 ".asx" => "video/x-ms-asf",
307 ".wmv" => "video/x-ms-wmv",
308 ".bz2" => "application/x-bzip",
309 ".tbz" => "application/x-bzip-compressed-tar",
310 ".tar.bz2" => "application/x-bzip-compressed-tar",
311 "" => "text/plain"
312 )
313 EOF
314 test x"$local" = xtrue && echo 'server.bind = "127.0.0.1"' >> "$conf"
315 }
317 apache2_conf () {
318 if test -z "$module_path"
319 then
320 test -d "/usr/lib/httpd/modules" &&
321 module_path="/usr/lib/httpd/modules"
322 test -d "/usr/lib/apache2/modules" &&
323 module_path="/usr/lib/apache2/modules"
324 fi
325 bind=
326 test x"$local" = xtrue && bind='127.0.0.1:'
327 echo 'text/css css' > "$fqgitdir/mime.types"
328 cat > "$conf" <<EOF
329 ServerName "git-instaweb"
330 ServerRoot "$root"
331 DocumentRoot "$root"
332 ErrorLog "$fqgitdir/gitweb/$httpd_only/error.log"
333 CustomLog "$fqgitdir/gitweb/$httpd_only/access.log" combined
334 PidFile "$fqgitdir/pid"
335 Listen $bind$port
336 EOF
338 for mod in mime dir env log_config
339 do
340 if test -e $module_path/mod_${mod}.so
341 then
342 echo "LoadModule ${mod}_module " \
343 "$module_path/mod_${mod}.so" >> "$conf"
344 fi
345 done
346 cat >> "$conf" <<EOF
347 TypesConfig "$fqgitdir/mime.types"
348 DirectoryIndex gitweb.cgi
349 EOF
351 # check to see if Dennis Stosberg's mod_perl compatibility patch
352 # (<20060621130708.Gcbc6e5c@leonov.stosberg.net>) has been applied
353 if test -f "$module_path/mod_perl.so" &&
354 sane_grep 'MOD_PERL' "$root/gitweb.cgi" >/dev/null
355 then
356 # favor mod_perl if available
357 cat >> "$conf" <<EOF
358 LoadModule perl_module $module_path/mod_perl.so
359 PerlPassEnv GIT_DIR
360 PerlPassEnv GIT_EXEC_PATH
361 PerlPassEnv GITWEB_CONFIG
362 <Location /gitweb.cgi>
363 SetHandler perl-script
364 PerlResponseHandler ModPerl::Registry
365 PerlOptions +ParseHeaders
366 Options +ExecCGI
367 </Location>
368 EOF
369 else
370 # plain-old CGI
371 resolve_full_httpd
372 list_mods=$(echo "$full_httpd" | sed 's/-f$/-l/')
373 $list_mods | sane_grep 'mod_cgi\.c' >/dev/null 2>&1 || \
374 if test -f "$module_path/mod_cgi.so"
375 then
376 echo "LoadModule cgi_module $module_path/mod_cgi.so" >> "$conf"
377 else
378 $list_mods | grep 'mod_cgid\.c' >/dev/null 2>&1 || \
379 if test -f "$module_path/mod_cgid.so"
380 then
381 echo "LoadModule cgid_module $module_path/mod_cgid.so" \
382 >> "$conf"
383 else
384 echo "You have no CGI support!"
385 exit 2
386 fi
387 echo "ScriptSock logs/gitweb.sock" >> "$conf"
388 fi
389 cat >> "$conf" <<EOF
390 PassEnv GIT_DIR
391 PassEnv GIT_EXEC_PATH
392 PassEnv GITWEB_CONFIG
393 AddHandler cgi-script .cgi
394 <Location /gitweb.cgi>
395 Options +ExecCGI
396 </Location>
397 EOF
398 fi
399 }
401 mongoose_conf() {
402 cat > "$conf" <<EOF
403 # Mongoose web server configuration file.
404 # Lines starting with '#' and empty lines are ignored.
405 # For detailed description of every option, visit
406 # http://code.google.com/p/mongoose/wiki/MongooseManual
408 root $root
409 ports $port
410 index_files gitweb.cgi
411 #ssl_cert $fqgitdir/gitweb/ssl_cert.pem
412 error_log $fqgitdir/gitweb/$httpd_only/error.log
413 access_log $fqgitdir/gitweb/$httpd_only/access.log
415 #cgi setup
416 cgi_env PATH=$PATH,GIT_DIR=$GIT_DIR,GIT_EXEC_PATH=$GIT_EXEC_PATH,GITWEB_CONFIG=$GITWEB_CONFIG
417 cgi_interp $PERL
418 cgi_ext cgi,pl
420 # mimetype mapping
421 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
422 EOF
423 }
425 plackup_conf () {
426 # generate a standalone 'plackup' server script in $fqgitdir/gitweb
427 # with embedded configuration; it does not use "$conf" file
428 cat > "$fqgitdir/gitweb/gitweb.psgi" <<EOF
429 #!$PERL
431 # gitweb - simple web interface to track changes in git repositories
432 # PSGI wrapper and server starter (see http://plackperl.org)
434 use strict;
436 use IO::Handle;
437 use Plack::MIME;
438 use Plack::Builder;
439 use Plack::App::WrapCGI;
440 use CGI::Emulate::PSGI 0.07; # minimum version required to work with gitweb
442 # mimetype mapping (from lighttpd_conf)
443 Plack::MIME->add_type(
444 ".pdf" => "application/pdf",
445 ".sig" => "application/pgp-signature",
446 ".spl" => "application/futuresplash",
447 ".class" => "application/octet-stream",
448 ".ps" => "application/postscript",
449 ".torrent" => "application/x-bittorrent",
450 ".dvi" => "application/x-dvi",
451 ".gz" => "application/x-gzip",
452 ".pac" => "application/x-ns-proxy-autoconfig",
453 ".swf" => "application/x-shockwave-flash",
454 ".tar.gz" => "application/x-tgz",
455 ".tgz" => "application/x-tgz",
456 ".tar" => "application/x-tar",
457 ".zip" => "application/zip",
458 ".mp3" => "audio/mpeg",
459 ".m3u" => "audio/x-mpegurl",
460 ".wma" => "audio/x-ms-wma",
461 ".wax" => "audio/x-ms-wax",
462 ".ogg" => "application/ogg",
463 ".wav" => "audio/x-wav",
464 ".gif" => "image/gif",
465 ".jpg" => "image/jpeg",
466 ".jpeg" => "image/jpeg",
467 ".png" => "image/png",
468 ".xbm" => "image/x-xbitmap",
469 ".xpm" => "image/x-xpixmap",
470 ".xwd" => "image/x-xwindowdump",
471 ".css" => "text/css",
472 ".html" => "text/html",
473 ".htm" => "text/html",
474 ".js" => "text/javascript",
475 ".asc" => "text/plain",
476 ".c" => "text/plain",
477 ".cpp" => "text/plain",
478 ".log" => "text/plain",
479 ".conf" => "text/plain",
480 ".text" => "text/plain",
481 ".txt" => "text/plain",
482 ".dtd" => "text/xml",
483 ".xml" => "text/xml",
484 ".mpeg" => "video/mpeg",
485 ".mpg" => "video/mpeg",
486 ".mov" => "video/quicktime",
487 ".qt" => "video/quicktime",
488 ".avi" => "video/x-msvideo",
489 ".asf" => "video/x-ms-asf",
490 ".asx" => "video/x-ms-asf",
491 ".wmv" => "video/x-ms-wmv",
492 ".bz2" => "application/x-bzip",
493 ".tbz" => "application/x-bzip-compressed-tar",
494 ".tar.bz2" => "application/x-bzip-compressed-tar",
495 "" => "text/plain"
496 );
498 my \$app = builder {
499 # to be able to override \$SIG{__WARN__} to log build time warnings
500 use CGI::Carp; # it sets \$SIG{__WARN__} itself
502 my \$logdir = "$fqgitdir/gitweb/$httpd_only";
503 open my \$access_log_fh, '>>', "\$logdir/access.log"
504 or die "Couldn't open access log '\$logdir/access.log': \$!";
505 open my \$error_log_fh, '>>', "\$logdir/error.log"
506 or die "Couldn't open error log '\$logdir/error.log': \$!";
508 \$access_log_fh->autoflush(1);
509 \$error_log_fh->autoflush(1);
511 # redirect build time warnings to error.log
512 \$SIG{'__WARN__'} = sub {
513 my \$msg = shift;
514 # timestamp warning like in CGI::Carp::warn
515 my \$stamp = CGI::Carp::stamp();
516 \$msg =~ s/^/\$stamp/gm;
517 print \$error_log_fh \$msg;
518 };
520 # write errors to error.log, access to access.log
521 enable 'AccessLog',
522 format => "combined",
523 logger => sub { print \$access_log_fh @_; };
524 enable sub {
525 my \$app = shift;
526 sub {
527 my \$env = shift;
528 \$env->{'psgi.errors'} = \$error_log_fh;
529 \$app->(\$env);
530 }
531 };
532 # gitweb currently doesn't work with $SIG{CHLD} set to 'IGNORE',
533 # because it uses 'close $fd or die...' on piped filehandle $fh
534 # (which causes the parent process to wait for child to finish).
535 enable_if { \$SIG{'CHLD'} eq 'IGNORE' } sub {
536 my \$app = shift;
537 sub {
538 my \$env = shift;
539 local \$SIG{'CHLD'} = 'DEFAULT';
540 local \$SIG{'CLD'} = 'DEFAULT';
541 \$app->(\$env);
542 }
543 };
544 # serve static files, i.e. stylesheet, images, script
545 enable 'Static',
546 path => sub { m!\.(js|css|png)\$! && s!^/gitweb/!! },
547 root => "$root/",
548 encoding => 'utf-8'; # encoding for 'text/plain' files
549 # convert CGI application to PSGI app
550 Plack::App::WrapCGI->new(script => "$root/gitweb.cgi")->to_app;
551 };
553 # make it runnable as standalone app,
554 # like it would be run via 'plackup' utility
555 if (__FILE__ eq \$0) {
556 require Plack::Runner;
558 my \$runner = Plack::Runner->new();
559 \$runner->parse_options(qw(--env deployment --port $port),
560 "$local" ? qw(--host 127.0.0.1) : ());
561 \$runner->run(\$app);
562 }
563 __END__
564 EOF
566 chmod a+x "$fqgitdir/gitweb/gitweb.psgi"
567 # configuration is embedded in server script file, gitweb.psgi
568 rm -f "$conf"
569 }
571 gitweb_conf() {
572 cat > "$fqgitdir/gitweb/gitweb_config.perl" <<EOF
573 #!/usr/bin/perl
574 our \$projectroot = "$(dirname "$fqgitdir")";
575 our \$git_temp = "$fqgitdir/gitweb/tmp";
576 our \$projects_list = \$projectroot;
577 EOF
578 }
580 gitweb_conf
582 resolve_full_httpd
583 mkdir -p "$fqgitdir/gitweb/$httpd_only"
585 case "$httpd" in
586 *lighttpd*)
587 lighttpd_conf
588 ;;
589 *apache2*|*httpd*)
590 apache2_conf
591 ;;
592 webrick)
593 webrick_conf
594 ;;
595 *mongoose*)
596 mongoose_conf
597 ;;
598 *plackup*)
599 plackup_conf
600 ;;
601 *)
602 echo "Unknown httpd specified: $httpd"
603 exit 1
604 ;;
605 esac
607 start_httpd
608 url=http://127.0.0.1:$port
610 if test -n "$browser"; then
611 httpd_is_ready && git web--browse -b "$browser" $url || echo $url
612 else
613 httpd_is_ready && git web--browse -c "instaweb.browser" $url || echo $url
614 fi