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.rb in
63 full_httpd
="$fqgitdir/gitweb/webrick.rb"
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
222 :DocumentRoot => "$root",
223 :Logger => Logger.new('$fqgitdir/gitweb/error.log'),
225 [ Logger.new('$fqgitdir/gitweb/access.log'),
226 WEBrick::AccessLog::COMBINED_LOG_FORMAT ]
228 :DirectoryIndex => ["gitweb.cgi"],
229 :CGIInterpreter => "$wrapper",
230 :StartCallback => lambda do
231 File.open("$fqgitdir/pid", "w") { |f| f.puts Process.pid }
233 :ServerType => WEBrick::Daemon,
235 options[:BindAddress] = '127.0.0.1' if "$local" == "true"
236 server = WEBrick::HTTPServer.new(options)
237 ['INT', 'TERM'].each do |signal|
238 trap(signal) {server.shutdown}
242 chmod +x
"$fqgitdir/gitweb/$httpd.rb"
243 # configuration is embedded in server script file, webrick.rb
249 server.document-root = "$root"
251 server.modules = ( "mod_setenv", "mod_cgi" )
252 server.indexfiles = ( "gitweb.cgi" )
253 server.pid-file = "$fqgitdir/pid"
254 server.errorlog = "$fqgitdir/gitweb/$httpd_only/error.log"
256 # to enable, add "mod_access", "mod_accesslog" to server.modules
257 # variable above and uncomment this
258 #accesslog.filename = "$fqgitdir/gitweb/$httpd_only/access.log"
260 setenv.add-environment = ( "PATH" => env.PATH, "GITWEB_CONFIG" => env.GITWEB_CONFIG )
262 cgi.assign = ( ".cgi" => "" )
266 ".pdf" => "application/pdf",
267 ".sig" => "application/pgp-signature",
268 ".spl" => "application/futuresplash",
269 ".class" => "application/octet-stream",
270 ".ps" => "application/postscript",
271 ".torrent" => "application/x-bittorrent",
272 ".dvi" => "application/x-dvi",
273 ".gz" => "application/x-gzip",
274 ".pac" => "application/x-ns-proxy-autoconfig",
275 ".swf" => "application/x-shockwave-flash",
276 ".tar.gz" => "application/x-tgz",
277 ".tgz" => "application/x-tgz",
278 ".tar" => "application/x-tar",
279 ".zip" => "application/zip",
280 ".mp3" => "audio/mpeg",
281 ".m3u" => "audio/x-mpegurl",
282 ".wma" => "audio/x-ms-wma",
283 ".wax" => "audio/x-ms-wax",
284 ".ogg" => "application/ogg",
285 ".wav" => "audio/x-wav",
286 ".gif" => "image/gif",
287 ".jpg" => "image/jpeg",
288 ".jpeg" => "image/jpeg",
289 ".png" => "image/png",
290 ".xbm" => "image/x-xbitmap",
291 ".xpm" => "image/x-xpixmap",
292 ".xwd" => "image/x-xwindowdump",
293 ".css" => "text/css",
294 ".html" => "text/html",
295 ".htm" => "text/html",
296 ".js" => "text/javascript",
297 ".asc" => "text/plain",
298 ".c" => "text/plain",
299 ".cpp" => "text/plain",
300 ".log" => "text/plain",
301 ".conf" => "text/plain",
302 ".text" => "text/plain",
303 ".txt" => "text/plain",
304 ".dtd" => "text/xml",
305 ".xml" => "text/xml",
306 ".mpeg" => "video/mpeg",
307 ".mpg" => "video/mpeg",
308 ".mov" => "video/quicktime",
309 ".qt" => "video/quicktime",
310 ".avi" => "video/x-msvideo",
311 ".asf" => "video/x-ms-asf",
312 ".asx" => "video/x-ms-asf",
313 ".wmv" => "video/x-ms-wmv",
314 ".bz2" => "application/x-bzip",
315 ".tbz" => "application/x-bzip-compressed-tar",
316 ".tar.bz2" => "application/x-bzip-compressed-tar",
320 test x
"$local" = xtrue
&& echo 'server.bind = "127.0.0.1"' >> "$conf"
324 if test -z "$module_path"
326 test -d "/usr/lib/httpd/modules" &&
327 module_path
="/usr/lib/httpd/modules"
328 test -d "/usr/lib/apache2/modules" &&
329 module_path
="/usr/lib/apache2/modules"
332 test x
"$local" = xtrue
&& bind='127.0.0.1:'
333 echo 'text/css css' > "$fqgitdir/mime.types"
335 ServerName "git-instaweb"
338 ErrorLog "$fqgitdir/gitweb/$httpd_only/error.log"
339 CustomLog "$fqgitdir/gitweb/$httpd_only/access.log" combined
340 PidFile "$fqgitdir/pid"
344 for mod
in mime dir env log_config
346 if test -e $module_path/mod_
${mod}.so
348 echo "LoadModule ${mod}_module " \
349 "$module_path/mod_${mod}.so" >> "$conf"
353 TypesConfig "$fqgitdir/mime.types"
354 DirectoryIndex gitweb.cgi
357 # check to see if Dennis Stosberg's mod_perl compatibility patch
358 # (<20060621130708.Gcbc6e5c@leonov.stosberg.net>) has been applied
359 if test -f "$module_path/mod_perl.so" &&
360 sane_grep
'MOD_PERL' "$root/gitweb.cgi" >/dev
/null
362 # favor mod_perl if available
364 LoadModule perl_module $module_path/mod_perl.so
366 PerlPassEnv GIT_EXEC_PATH
367 PerlPassEnv GITWEB_CONFIG
368 <Location /gitweb.cgi>
369 SetHandler perl-script
370 PerlResponseHandler ModPerl::Registry
371 PerlOptions +ParseHeaders
378 list_mods
=$
(echo "$full_httpd" |
sed 's/-f$/-l/')
379 $list_mods | sane_grep
'mod_cgi\.c' >/dev
/null
2>&1 || \
380 if test -f "$module_path/mod_cgi.so"
382 echo "LoadModule cgi_module $module_path/mod_cgi.so" >> "$conf"
384 $list_mods |
grep 'mod_cgid\.c' >/dev
/null
2>&1 || \
385 if test -f "$module_path/mod_cgid.so"
387 echo "LoadModule cgid_module $module_path/mod_cgid.so" \
390 echo "You have no CGI support!"
393 echo "ScriptSock logs/gitweb.sock" >> "$conf"
397 PassEnv GIT_EXEC_PATH
398 PassEnv GITWEB_CONFIG
399 AddHandler cgi-script .cgi
400 <Location /gitweb.cgi>
409 # Mongoose web server configuration file.
410 # Lines starting with '#' and empty lines are ignored.
411 # For detailed description of every option, visit
412 # http://code.google.com/p/mongoose/wiki/MongooseManual
416 index_files gitweb.cgi
417 #ssl_cert $fqgitdir/gitweb/ssl_cert.pem
418 error_log $fqgitdir/gitweb/$httpd_only/error.log
419 access_log $fqgitdir/gitweb/$httpd_only/access.log
422 cgi_env PATH=$PATH,GIT_DIR=$GIT_DIR,GIT_EXEC_PATH=$GIT_EXEC_PATH,GITWEB_CONFIG=$GITWEB_CONFIG
427 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
432 # generate a standalone 'plackup' server script in $fqgitdir/gitweb
433 # with embedded configuration; it does not use "$conf" file
434 cat > "$fqgitdir/gitweb/gitweb.psgi" <<EOF
437 # gitweb - simple web interface to track changes in git repositories
438 # PSGI wrapper and server starter (see http://plackperl.org)
445 use Plack::App::WrapCGI;
446 use CGI::Emulate::PSGI 0.07; # minimum version required to work with gitweb
448 # mimetype mapping (from lighttpd_conf)
449 Plack::MIME->add_type(
450 ".pdf" => "application/pdf",
451 ".sig" => "application/pgp-signature",
452 ".spl" => "application/futuresplash",
453 ".class" => "application/octet-stream",
454 ".ps" => "application/postscript",
455 ".torrent" => "application/x-bittorrent",
456 ".dvi" => "application/x-dvi",
457 ".gz" => "application/x-gzip",
458 ".pac" => "application/x-ns-proxy-autoconfig",
459 ".swf" => "application/x-shockwave-flash",
460 ".tar.gz" => "application/x-tgz",
461 ".tgz" => "application/x-tgz",
462 ".tar" => "application/x-tar",
463 ".zip" => "application/zip",
464 ".mp3" => "audio/mpeg",
465 ".m3u" => "audio/x-mpegurl",
466 ".wma" => "audio/x-ms-wma",
467 ".wax" => "audio/x-ms-wax",
468 ".ogg" => "application/ogg",
469 ".wav" => "audio/x-wav",
470 ".gif" => "image/gif",
471 ".jpg" => "image/jpeg",
472 ".jpeg" => "image/jpeg",
473 ".png" => "image/png",
474 ".xbm" => "image/x-xbitmap",
475 ".xpm" => "image/x-xpixmap",
476 ".xwd" => "image/x-xwindowdump",
477 ".css" => "text/css",
478 ".html" => "text/html",
479 ".htm" => "text/html",
480 ".js" => "text/javascript",
481 ".asc" => "text/plain",
482 ".c" => "text/plain",
483 ".cpp" => "text/plain",
484 ".log" => "text/plain",
485 ".conf" => "text/plain",
486 ".text" => "text/plain",
487 ".txt" => "text/plain",
488 ".dtd" => "text/xml",
489 ".xml" => "text/xml",
490 ".mpeg" => "video/mpeg",
491 ".mpg" => "video/mpeg",
492 ".mov" => "video/quicktime",
493 ".qt" => "video/quicktime",
494 ".avi" => "video/x-msvideo",
495 ".asf" => "video/x-ms-asf",
496 ".asx" => "video/x-ms-asf",
497 ".wmv" => "video/x-ms-wmv",
498 ".bz2" => "application/x-bzip",
499 ".tbz" => "application/x-bzip-compressed-tar",
500 ".tar.bz2" => "application/x-bzip-compressed-tar",
505 # to be able to override \$SIG{__WARN__} to log build time warnings
506 use CGI::Carp; # it sets \$SIG{__WARN__} itself
508 my \$logdir = "$fqgitdir/gitweb/$httpd_only";
509 open my \$access_log_fh, '>>', "\$logdir/access.log"
510 or die "Couldn't open access log '\$logdir/access.log': \$!";
511 open my \$error_log_fh, '>>', "\$logdir/error.log"
512 or die "Couldn't open error log '\$logdir/error.log': \$!";
514 \$access_log_fh->autoflush(1);
515 \$error_log_fh->autoflush(1);
517 # redirect build time warnings to error.log
518 \$SIG{'__WARN__'} = sub {
520 # timestamp warning like in CGI::Carp::warn
521 my \$stamp = CGI::Carp::stamp();
522 \$msg =~ s/^/\$stamp/gm;
523 print \$error_log_fh \$msg;
526 # write errors to error.log, access to access.log
528 format => "combined",
529 logger => sub { print \$access_log_fh @_; };
534 \$env->{'psgi.errors'} = \$error_log_fh;
538 # gitweb currently doesn't work with $SIG{CHLD} set to 'IGNORE',
539 # because it uses 'close $fd or die...' on piped filehandle $fh
540 # (which causes the parent process to wait for child to finish).
541 enable_if { \$SIG{'CHLD'} eq 'IGNORE' } sub {
545 local \$SIG{'CHLD'} = 'DEFAULT';
546 local \$SIG{'CLD'} = 'DEFAULT';
550 # serve static files, i.e. stylesheet, images, script
552 path => sub { m!\.(js|css|png)\$! && s!^/gitweb/!! },
554 encoding => 'utf-8'; # encoding for 'text/plain' files
555 # convert CGI application to PSGI app
556 Plack::App::WrapCGI->new(script => "$root/gitweb.cgi")->to_app;
559 # make it runnable as standalone app,
560 # like it would be run via 'plackup' utility
561 if (__FILE__ eq \$0) {
562 require Plack::Runner;
564 my \$runner = Plack::Runner->new();
565 \$runner->parse_options(qw(--env deployment --port $port),
566 "$local" ? qw(--host 127.0.0.1) : ());
567 \$runner->run(\$app);
572 chmod a
+x
"$fqgitdir/gitweb/gitweb.psgi"
573 # configuration is embedded in server script file, gitweb.psgi
578 cat > "$fqgitdir/gitweb/gitweb_config.perl" <<EOF
580 our \$projectroot = "$(dirname "$fqgitdir")";
581 our \$git_temp = "$fqgitdir/gitweb/tmp";
582 our \$projects_list = \$projectroot;
584 \$feature{'remote_heads'}{'default'} = [1];
591 mkdir
-p "$fqgitdir/gitweb/$httpd_only"
610 echo "Unknown httpd specified: $httpd"
616 url
=http
://127.0.0.1:$port
618 if test -n "$browser"; then
619 httpd_is_ready
&& git web--browse
-b "$browser" $url ||
echo $url
621 httpd_is_ready
&& git web--browse
-c "instaweb.browser" $url ||
echo $url