3 # Copyright (c) 2006 Eric Wong
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)
17 stop stop the web server
18 start start the web server
19 restart restart the web server
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"
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 () {
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
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
61 # server is started by running via generated webrick.sh in
63 full_httpd="$fqgitdir/gitweb/webrick.sh"
64 httpd_only="${httpd%% *}" # cut on first space
69 httpd_only="$(echo $httpd | cut -f1 -d' ')"
70 if case "$httpd_only" in /*) : ;; *) which $httpd_only >/dev/null 2>&1;; esac
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"
80 if test -x "$i/$httpd_only"
87 echo >&2 "$httpd_only not found. Install $httpd_only or use" \
88 "--httpd to specify another httpd daemon."
94 if test -f "$fqgitdir/pid"; then
95 say "Instance already running. Restarting..."
99 # here $httpd should have a meaningful value
102 # don't quote $full_httpd, there can be arguments to it (-f)
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)
110 if test $? != 0; then
111 echo "Could not execute http daemon $httpd."
115 cat > "$fqgitdir/pid" <<EOF
120 $full_httpd "$fqgitdir/gitweb/httpd.conf"
121 if test $? != 0; then
122 echo "Could not execute http daemon $httpd."
130 test -f "$fqgitdir/pid" && kill $(cat "$fqgitdir/pid")
131 rm -f "$fqgitdir/pid"
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 ..';
142 } until (IO::Socket::INET->new('127.0.0.1:$port'));
191 mkdir -p "$GIT_DIR/gitweb/tmp"
192 GIT_EXEC_PATH="$(git --exec-path)"
194 GITWEB_CONFIG="$fqgitdir/gitweb/gitweb_config.perl"
195 export GIT_EXEC_PATH GIT_DIR GITWEB_CONFIG
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
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
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
219 options = YAML::load_file(ARGV[0])
220 options[:StartCallback] = proc do
221 File.open(options[:PidFile],"w") do |f|
225 options[:ServerType] = WEBrick::Daemon
226 server = WEBrick::HTTPServer.new(options)
227 ['INT', 'TERM'].each do |signal|
228 trap(signal) {server.shutdown}
232 # generate a shell script to invoke the above ruby script,
233 # which assumes _ruby_ is in the user's $PATH. that's _one_
234 # portable way to run ruby, which could be installed anywhere,
236 cat >"$fqgitdir/gitweb/$httpd.sh" <<EOF
238 exec ruby "$fqgitdir/gitweb/$httpd.rb" \$*
240 chmod +x "$fqgitdir/gitweb/$httpd.sh"
244 :DocumentRoot: "$root"
245 :DirectoryIndex: ["gitweb.cgi"]
246 :PidFile: "$fqgitdir/pid"
247 :CGIInterpreter: "$wrapper"
249 test "$local" = true && echo ':BindAddress: "127.0.0.1"' >> "$conf"
254 server.document-root = "$root"
256 server.modules = ( "mod_setenv", "mod_cgi" )
257 server.indexfiles = ( "gitweb.cgi" )
258 server.pid-file = "$fqgitdir/pid"
259 server.errorlog = "$fqgitdir/gitweb/$httpd_only/error.log"
261 # to enable, add "mod_access", "mod_accesslog" to server.modules
262 # variable above and uncomment this
263 #accesslog.filename = "$fqgitdir/gitweb/$httpd_only/access.log"
265 setenv.add-environment = ( "PATH" => env.PATH, "GITWEB_CONFIG" => env.GITWEB_CONFIG )
267 cgi.assign = ( ".cgi" => "" )
271 ".pdf" => "application/pdf",
272 ".sig" => "application/pgp-signature",
273 ".spl" => "application/futuresplash",
274 ".class" => "application/octet-stream",
275 ".ps" => "application/postscript",
276 ".torrent" => "application/x-bittorrent",
277 ".dvi" => "application/x-dvi",
278 ".gz" => "application/x-gzip",
279 ".pac" => "application/x-ns-proxy-autoconfig",
280 ".swf" => "application/x-shockwave-flash",
281 ".tar.gz" => "application/x-tgz",
282 ".tgz" => "application/x-tgz",
283 ".tar" => "application/x-tar",
284 ".zip" => "application/zip",
285 ".mp3" => "audio/mpeg",
286 ".m3u" => "audio/x-mpegurl",
287 ".wma" => "audio/x-ms-wma",
288 ".wax" => "audio/x-ms-wax",
289 ".ogg" => "application/ogg",
290 ".wav" => "audio/x-wav",
291 ".gif" => "image/gif",
292 ".jpg" => "image/jpeg",
293 ".jpeg" => "image/jpeg",
294 ".png" => "image/png",
295 ".xbm" => "image/x-xbitmap",
296 ".xpm" => "image/x-xpixmap",
297 ".xwd" => "image/x-xwindowdump",
298 ".css" => "text/css",
299 ".html" => "text/html",
300 ".htm" => "text/html",
301 ".js" => "text/javascript",
302 ".asc" => "text/plain",
303 ".c" => "text/plain",
304 ".cpp" => "text/plain",
305 ".log" => "text/plain",
306 ".conf" => "text/plain",
307 ".text" => "text/plain",
308 ".txt" => "text/plain",
309 ".dtd" => "text/xml",
310 ".xml" => "text/xml",
311 ".mpeg" => "video/mpeg",
312 ".mpg" => "video/mpeg",
313 ".mov" => "video/quicktime",
314 ".qt" => "video/quicktime",
315 ".avi" => "video/x-msvideo",
316 ".asf" => "video/x-ms-asf",
317 ".asx" => "video/x-ms-asf",
318 ".wmv" => "video/x-ms-wmv",
319 ".bz2" => "application/x-bzip",
320 ".tbz" => "application/x-bzip-compressed-tar",
321 ".tar.bz2" => "application/x-bzip-compressed-tar",
325 test x"$local" = xtrue && echo 'server.bind = "127.0.0.1"' >> "$conf"
329 if test -z "$module_path"
331 test -d "/usr/lib/httpd/modules" &&
332 module_path="/usr/lib/httpd/modules"
333 test -d "/usr/lib/apache2/modules" &&
334 module_path="/usr/lib/apache2/modules"
337 test x"$local" = xtrue && bind='127.0.0.1:'
338 echo 'text/css css' > "$fqgitdir/mime.types"
340 ServerName "git-instaweb"
343 ErrorLog "$fqgitdir/gitweb/$httpd_only/error.log"
344 CustomLog "$fqgitdir/gitweb/$httpd_only/access.log" combined
345 PidFile "$fqgitdir/pid"
349 for mod in mime dir env log_config
351 if test -e $module_path/mod_${mod}.so
353 echo "LoadModule ${mod}_module " \
354 "$module_path/mod_${mod}.so" >> "$conf"
358 TypesConfig "$fqgitdir/mime.types"
359 DirectoryIndex gitweb.cgi
362 # check to see if Dennis Stosberg's mod_perl compatibility patch
363 # (<20060621130708.Gcbc6e5c@leonov.stosberg.net>) has been applied
364 if test -f "$module_path/mod_perl.so" &&
365 sane_grep 'MOD_PERL' "$root/gitweb.cgi" >/dev/null
367 # favor mod_perl if available
369 LoadModule perl_module $module_path/mod_perl.so
371 PerlPassEnv GIT_EXEC_PATH
372 PerlPassEnv GITWEB_CONFIG
373 <Location /gitweb.cgi>
374 SetHandler perl-script
375 PerlResponseHandler ModPerl::Registry
376 PerlOptions +ParseHeaders
383 list_mods=$(echo "$full_httpd" | sed 's/-f$/-l/')
384 $list_mods | sane_grep 'mod_cgi\.c' >/dev/null 2>&1 || \
385 if test -f "$module_path/mod_cgi.so"
387 echo "LoadModule cgi_module $module_path/mod_cgi.so" >> "$conf"
389 $list_mods | grep 'mod_cgid\.c' >/dev/null 2>&1 || \
390 if test -f "$module_path/mod_cgid.so"
392 echo "LoadModule cgid_module $module_path/mod_cgid.so" \
395 echo "You have no CGI support!"
398 echo "ScriptSock logs/gitweb.sock" >> "$conf"
402 PassEnv GIT_EXEC_PATH
403 PassEnv GITWEB_CONFIG
404 AddHandler cgi-script .cgi
405 <Location /gitweb.cgi>
414 # Mongoose web server configuration file.
415 # Lines starting with '#' and empty lines are ignored.
416 # For detailed description of every option, visit
417 # http://code.google.com/p/mongoose/wiki/MongooseManual
421 index_files gitweb.cgi
422 #ssl_cert $fqgitdir/gitweb/ssl_cert.pem
423 error_log $fqgitdir/gitweb/$httpd_only/error.log
424 access_log $fqgitdir/gitweb/$httpd_only/access.log
427 cgi_env PATH=$PATH,GIT_DIR=$GIT_DIR,GIT_EXEC_PATH=$GIT_EXEC_PATH,GITWEB_CONFIG=$GITWEB_CONFIG
432 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
437 # generate a standalone 'plackup' server script in $fqgitdir/gitweb
438 # with embedded configuration; it does not use "$conf" file
439 cat > "$fqgitdir/gitweb/gitweb.psgi" <<EOF
442 # gitweb - simple web interface to track changes in git repositories
443 # PSGI wrapper and server starter (see http://plackperl.org)
450 use Plack::App::WrapCGI;
451 use CGI::Emulate::PSGI 0.07; # minimum version required to work with gitweb
453 # mimetype mapping (from lighttpd_conf)
454 Plack::MIME->add_type(
455 ".pdf" => "application/pdf",
456 ".sig" => "application/pgp-signature",
457 ".spl" => "application/futuresplash",
458 ".class" => "application/octet-stream",
459 ".ps" => "application/postscript",
460 ".torrent" => "application/x-bittorrent",
461 ".dvi" => "application/x-dvi",
462 ".gz" => "application/x-gzip",
463 ".pac" => "application/x-ns-proxy-autoconfig",
464 ".swf" => "application/x-shockwave-flash",
465 ".tar.gz" => "application/x-tgz",
466 ".tgz" => "application/x-tgz",
467 ".tar" => "application/x-tar",
468 ".zip" => "application/zip",
469 ".mp3" => "audio/mpeg",
470 ".m3u" => "audio/x-mpegurl",
471 ".wma" => "audio/x-ms-wma",
472 ".wax" => "audio/x-ms-wax",
473 ".ogg" => "application/ogg",
474 ".wav" => "audio/x-wav",
475 ".gif" => "image/gif",
476 ".jpg" => "image/jpeg",
477 ".jpeg" => "image/jpeg",
478 ".png" => "image/png",
479 ".xbm" => "image/x-xbitmap",
480 ".xpm" => "image/x-xpixmap",
481 ".xwd" => "image/x-xwindowdump",
482 ".css" => "text/css",
483 ".html" => "text/html",
484 ".htm" => "text/html",
485 ".js" => "text/javascript",
486 ".asc" => "text/plain",
487 ".c" => "text/plain",
488 ".cpp" => "text/plain",
489 ".log" => "text/plain",
490 ".conf" => "text/plain",
491 ".text" => "text/plain",
492 ".txt" => "text/plain",
493 ".dtd" => "text/xml",
494 ".xml" => "text/xml",
495 ".mpeg" => "video/mpeg",
496 ".mpg" => "video/mpeg",
497 ".mov" => "video/quicktime",
498 ".qt" => "video/quicktime",
499 ".avi" => "video/x-msvideo",
500 ".asf" => "video/x-ms-asf",
501 ".asx" => "video/x-ms-asf",
502 ".wmv" => "video/x-ms-wmv",
503 ".bz2" => "application/x-bzip",
504 ".tbz" => "application/x-bzip-compressed-tar",
505 ".tar.bz2" => "application/x-bzip-compressed-tar",
510 # to be able to override \$SIG{__WARN__} to log build time warnings
511 use CGI::Carp; # it sets \$SIG{__WARN__} itself
513 my \$logdir = "$fqgitdir/gitweb/$httpd_only";
514 open my \$access_log_fh, '>>', "\$logdir/access.log"
515 or die "Couldn't open access log '\$logdir/access.log': \$!";
516 open my \$error_log_fh, '>>', "\$logdir/error.log"
517 or die "Couldn't open error log '\$logdir/error.log': \$!";
519 \$access_log_fh->autoflush(1);
520 \$error_log_fh->autoflush(1);
522 # redirect build time warnings to error.log
523 \$SIG{'__WARN__'} = sub {
525 # timestamp warning like in CGI::Carp::warn
526 my \$stamp = CGI::Carp::stamp();
527 \$msg =~ s/^/\$stamp/gm;
528 print \$error_log_fh \$msg;
531 # write errors to error.log, access to access.log
533 format => "combined",
534 logger => sub { print \$access_log_fh @_; };
539 \$env->{'psgi.errors'} = \$error_log_fh;
543 # gitweb currently doesn't work with $SIG{CHLD} set to 'IGNORE',
544 # because it uses 'close $fd or die...' on piped filehandle $fh
545 # (which causes the parent process to wait for child to finish).
546 enable_if { \$SIG{'CHLD'} eq 'IGNORE' } sub {
550 local \$SIG{'CHLD'} = 'DEFAULT';
551 local \$SIG{'CLD'} = 'DEFAULT';
555 # serve static files, i.e. stylesheet, images, script
557 path => sub { m!\.(js|css|png)\$! && s!^/gitweb/!! },
559 encoding => 'utf-8'; # encoding for 'text/plain' files
560 # convert CGI application to PSGI app
561 Plack::App::WrapCGI->new(script => "$root/gitweb.cgi")->to_app;
564 # make it runnable as standalone app,
565 # like it would be run via 'plackup' utility
566 if (__FILE__ eq \$0) {
567 require Plack::Runner;
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);
577 chmod a+x "$fqgitdir/gitweb/gitweb.psgi"
578 # configuration is embedded in server script file, gitweb.psgi
583 cat > "$fqgitdir/gitweb/gitweb_config.perl" <<EOF
585 our \$projectroot = "$(dirname "$fqgitdir")";
586 our \$git_temp = "$fqgitdir/gitweb/tmp";
587 our \$projects_list = \$projectroot;
594 mkdir -p "$fqgitdir/gitweb/$httpd_only"
613 echo "Unknown httpd specified: $httpd"
619 url=http://127.0.0.1:$port
621 if test -n "$browser"; then
622 httpd_is_ready && git web--browse -b "$browser" $url || echo $url
624 httpd_is_ready && git web--browse -c "instaweb.browser" $url || echo $url