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)
16 reuse-config reuse previous gitweb_config.perl from GIT_DIR
18 stop stop the web server
19 start start the web server
20 restart restart the web server
26 local="$(git config --bool --get instaweb.local)"
27 httpd
="$(git config --get instaweb.httpd)"
28 root
="$(git config --get instaweb.gitwebdir)"
29 port
=$
(git config
--get instaweb.port
)
30 module_path
="$(git config --get instaweb.modulepath)"
33 conf
="$GIT_DIR/gitweb/httpd.conf"
37 # if installed, it doesn't need further configuration (module_path)
38 test -z "$httpd" && httpd
='lighttpd -f'
40 # Default is @@GITWEBDIR@@
41 test -z "$root" && root
='@@GITWEBDIR@@'
43 # any untaken local port will do...
44 test -z "$port" && port
=1234
46 resolve_full_httpd
() {
48 *apache2
*|
*lighttpd
*|
*httpd
*)
49 # yes, *httpd* covers *lighttpd* above, but it is there for clarity
50 # ensure that the apache2/lighttpd command ends with "-f"
51 if ! echo "$httpd" | sane_grep
-- '-f *$' >/dev
/null
2>&1
57 # server is started by running via generated gitweb.psgi in $fqgitdir/gitweb
58 full_httpd
="$fqgitdir/gitweb/gitweb.psgi"
59 httpd_only
="${httpd%% *}" # cut on first space
64 httpd_only
="$(echo $httpd | cut -f1 -d' ')"
65 if case "$httpd_only" in /*) : ;; *) which $httpd_only >/dev
/null
2>&1;; esac
69 # many httpds are installed in /usr/sbin or /usr/local/sbin
70 # these days and those are not in most users $PATHs
71 # in addition, we may have generated a server script
72 # in $fqgitdir/gitweb.
73 for i
in /usr
/local
/sbin
/usr
/sbin
"$root" "$fqgitdir/gitweb"
75 if test -x "$i/$httpd_only"
82 echo >&2 "$httpd_only not found. Install $httpd_only or use" \
83 "--httpd to specify another httpd daemon."
89 if test -f "$fqgitdir/pid"; then
90 say
"Instance already running. Restarting..."
94 # here $httpd should have a meaningful value
97 # don't quote $full_httpd, there can be arguments to it (-f)
100 #These servers don't have a daemon mode so we'll have to fork it
101 $full_httpd "$fqgitdir/gitweb/httpd.conf" &
102 #Save the pid before doing anything else (we'll print it later)
105 if test $?
!= 0; then
106 echo "Could not execute http daemon $httpd."
110 cat > "$fqgitdir/pid" <<EOF
115 $full_httpd "$fqgitdir/gitweb/httpd.conf"
116 if test $?
!= 0; then
117 echo "Could not execute http daemon $httpd."
125 test -f "$fqgitdir/pid" && kill $
(cat "$fqgitdir/pid")
126 rm -f "$fqgitdir/pid"
130 "$PERL" -MIO::Socket
::INET
-e "
131 local \$| = 1; # turn on autoflush
132 exit if (IO::Socket::INET->new('127.0.0.1:$port'));
133 print 'Waiting for \'$httpd\' to start ..';
137 } until (IO::Socket::INET->new('127.0.0.1:$port'));
190 mkdir
-p "$GIT_DIR/gitweb/tmp"
191 GIT_EXEC_PATH
="$(git --exec-path)"
193 GITWEB_CONFIG
="$fqgitdir/gitweb/gitweb_config.perl"
194 export GIT_EXEC_PATH GIT_DIR GITWEB_CONFIG
197 # generate a standalone server script in $fqgitdir/gitweb.
198 cat >"$fqgitdir/gitweb/$httpd.rb" <<EOF
201 options = YAML::load_file(ARGV[0])
202 options[:StartCallback] = proc do
203 File.open(options[:PidFile],"w") do |f|
207 options[:ServerType] = WEBrick::Daemon
208 server = WEBrick::HTTPServer.new(options)
209 ['INT', 'TERM'].each do |signal|
210 trap(signal) {server.shutdown}
214 # generate a shell script to invoke the above ruby script,
215 # which assumes _ruby_ is in the user's $PATH. that's _one_
216 # portable way to run ruby, which could be installed anywhere,
218 cat >"$fqgitdir/gitweb/$httpd" <<EOF
220 exec ruby "$fqgitdir/gitweb/$httpd.rb" \$*
222 chmod +x
"$fqgitdir/gitweb/$httpd"
226 :DocumentRoot: "$root"
227 :DirectoryIndex: ["gitweb.cgi"]
228 :PidFile: "$fqgitdir/pid"
230 test "$local" = true
&& echo ':BindAddress: "127.0.0.1"' >> "$conf"
235 server.document-root = "$root"
237 server.modules = ( "mod_setenv", "mod_cgi" )
238 server.indexfiles = ( "gitweb.cgi" )
239 server.pid-file = "$fqgitdir/pid"
240 server.errorlog = "$fqgitdir/gitweb/$httpd_only/error.log"
242 # to enable, add "mod_access", "mod_accesslog" to server.modules
243 # variable above and uncomment this
244 #accesslog.filename = "$fqgitdir/gitweb/$httpd_only/access.log"
246 setenv.add-environment = ( "PATH" => env.PATH, "GITWEB_CONFIG" => env.GITWEB_CONFIG )
248 cgi.assign = ( ".cgi" => "" )
252 ".pdf" => "application/pdf",
253 ".sig" => "application/pgp-signature",
254 ".spl" => "application/futuresplash",
255 ".class" => "application/octet-stream",
256 ".ps" => "application/postscript",
257 ".torrent" => "application/x-bittorrent",
258 ".dvi" => "application/x-dvi",
259 ".gz" => "application/x-gzip",
260 ".pac" => "application/x-ns-proxy-autoconfig",
261 ".swf" => "application/x-shockwave-flash",
262 ".tar.gz" => "application/x-tgz",
263 ".tgz" => "application/x-tgz",
264 ".tar" => "application/x-tar",
265 ".zip" => "application/zip",
266 ".mp3" => "audio/mpeg",
267 ".m3u" => "audio/x-mpegurl",
268 ".wma" => "audio/x-ms-wma",
269 ".wax" => "audio/x-ms-wax",
270 ".ogg" => "application/ogg",
271 ".wav" => "audio/x-wav",
272 ".gif" => "image/gif",
273 ".jpg" => "image/jpeg",
274 ".jpeg" => "image/jpeg",
275 ".png" => "image/png",
276 ".xbm" => "image/x-xbitmap",
277 ".xpm" => "image/x-xpixmap",
278 ".xwd" => "image/x-xwindowdump",
279 ".css" => "text/css",
280 ".html" => "text/html",
281 ".htm" => "text/html",
282 ".js" => "text/javascript",
283 ".asc" => "text/plain",
284 ".c" => "text/plain",
285 ".cpp" => "text/plain",
286 ".log" => "text/plain",
287 ".conf" => "text/plain",
288 ".text" => "text/plain",
289 ".txt" => "text/plain",
290 ".dtd" => "text/xml",
291 ".xml" => "text/xml",
292 ".mpeg" => "video/mpeg",
293 ".mpg" => "video/mpeg",
294 ".mov" => "video/quicktime",
295 ".qt" => "video/quicktime",
296 ".avi" => "video/x-msvideo",
297 ".asf" => "video/x-ms-asf",
298 ".asx" => "video/x-ms-asf",
299 ".wmv" => "video/x-ms-wmv",
300 ".bz2" => "application/x-bzip",
301 ".tbz" => "application/x-bzip-compressed-tar",
302 ".tar.bz2" => "application/x-bzip-compressed-tar",
306 test x
"$local" = xtrue
&& echo 'server.bind = "127.0.0.1"' >> "$conf"
310 if test -z "$module_path"
312 test -d "/usr/lib/httpd/modules" &&
313 module_path
="/usr/lib/httpd/modules"
314 test -d "/usr/lib/apache2/modules" &&
315 module_path
="/usr/lib/apache2/modules"
318 test x
"$local" = xtrue
&& bind='127.0.0.1:'
319 echo 'text/css css' > "$fqgitdir/mime.types"
321 ServerName "git-instaweb"
324 ErrorLog "$fqgitdir/gitweb/$httpd_only/error.log"
325 CustomLog "$fqgitdir/gitweb/$httpd_only/access.log" combined
326 PidFile "$fqgitdir/pid"
330 for mod
in mime dir env log_config
332 if test -e $module_path/mod_
${mod}.so
334 echo "LoadModule ${mod}_module " \
335 "$module_path/mod_${mod}.so" >> "$conf"
339 TypesConfig "$fqgitdir/mime.types"
340 DirectoryIndex gitweb.cgi
343 # check to see if Dennis Stosberg's mod_perl compatibility patch
344 # (<20060621130708.Gcbc6e5c@leonov.stosberg.net>) has been applied
345 if test -f "$module_path/mod_perl.so" &&
346 sane_grep
'MOD_PERL' "$root/gitweb.cgi" >/dev
/null
348 # favor mod_perl if available
350 LoadModule perl_module $module_path/mod_perl.so
352 PerlPassEnv GIT_EXEC_PATH
353 PerlPassEnv GITWEB_CONFIG
354 <Location /gitweb.cgi>
355 SetHandler perl-script
356 PerlResponseHandler ModPerl::Registry
357 PerlOptions +ParseHeaders
364 list_mods
=$
(echo "$full_httpd" |
sed 's/-f$/-l/')
365 $list_mods | sane_grep
'mod_cgi\.c' >/dev
/null
2>&1 || \
366 if test -f "$module_path/mod_cgi.so"
368 echo "LoadModule cgi_module $module_path/mod_cgi.so" >> "$conf"
370 $list_mods |
grep 'mod_cgid\.c' >/dev
/null
2>&1 || \
371 if test -f "$module_path/mod_cgid.so"
373 echo "LoadModule cgid_module $module_path/mod_cgid.so" \
376 echo "You have no CGI support!"
379 echo "ScriptSock logs/gitweb.sock" >> "$conf"
383 PassEnv GIT_EXEC_PATH
384 PassEnv GITWEB_CONFIG
385 AddHandler cgi-script .cgi
386 <Location /gitweb.cgi>
395 # Mongoose web server configuration file.
396 # Lines starting with '#' and empty lines are ignored.
397 # For detailed description of every option, visit
398 # http://code.google.com/p/mongoose/wiki/MongooseManual
402 index_files gitweb.cgi
403 #ssl_cert $fqgitdir/gitweb/ssl_cert.pem
404 error_log $fqgitdir/gitweb/$httpd_only/error.log
405 access_log $fqgitdir/gitweb/$httpd_only/access.log
408 cgi_env PATH=$PATH,GIT_DIR=$GIT_DIR,GIT_EXEC_PATH=$GIT_EXEC_PATH,GITWEB_CONFIG=$GITWEB_CONFIG
413 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
418 # generate a standalone 'plackup' server script in $fqgitdir/gitweb
419 # with embedded configuration; it does not use "$conf" file
420 cat > "$fqgitdir/gitweb/gitweb.psgi" <<EOF
423 # gitweb - simple web interface to track changes in git repositories
424 # PSGI wrapper and server starter (see http://plackperl.org)
431 use Plack::App::WrapCGI;
432 use CGI::Emulate::PSGI 0.07; # minimum version required to work with gitweb
434 # mimetype mapping (from lighttpd_conf)
435 Plack::MIME->add_type(
436 ".pdf" => "application/pdf",
437 ".sig" => "application/pgp-signature",
438 ".spl" => "application/futuresplash",
439 ".class" => "application/octet-stream",
440 ".ps" => "application/postscript",
441 ".torrent" => "application/x-bittorrent",
442 ".dvi" => "application/x-dvi",
443 ".gz" => "application/x-gzip",
444 ".pac" => "application/x-ns-proxy-autoconfig",
445 ".swf" => "application/x-shockwave-flash",
446 ".tar.gz" => "application/x-tgz",
447 ".tgz" => "application/x-tgz",
448 ".tar" => "application/x-tar",
449 ".zip" => "application/zip",
450 ".mp3" => "audio/mpeg",
451 ".m3u" => "audio/x-mpegurl",
452 ".wma" => "audio/x-ms-wma",
453 ".wax" => "audio/x-ms-wax",
454 ".ogg" => "application/ogg",
455 ".wav" => "audio/x-wav",
456 ".gif" => "image/gif",
457 ".jpg" => "image/jpeg",
458 ".jpeg" => "image/jpeg",
459 ".png" => "image/png",
460 ".xbm" => "image/x-xbitmap",
461 ".xpm" => "image/x-xpixmap",
462 ".xwd" => "image/x-xwindowdump",
463 ".css" => "text/css",
464 ".html" => "text/html",
465 ".htm" => "text/html",
466 ".js" => "text/javascript",
467 ".asc" => "text/plain",
468 ".c" => "text/plain",
469 ".cpp" => "text/plain",
470 ".log" => "text/plain",
471 ".conf" => "text/plain",
472 ".text" => "text/plain",
473 ".txt" => "text/plain",
474 ".dtd" => "text/xml",
475 ".xml" => "text/xml",
476 ".mpeg" => "video/mpeg",
477 ".mpg" => "video/mpeg",
478 ".mov" => "video/quicktime",
479 ".qt" => "video/quicktime",
480 ".avi" => "video/x-msvideo",
481 ".asf" => "video/x-ms-asf",
482 ".asx" => "video/x-ms-asf",
483 ".wmv" => "video/x-ms-wmv",
484 ".bz2" => "application/x-bzip",
485 ".tbz" => "application/x-bzip-compressed-tar",
486 ".tar.bz2" => "application/x-bzip-compressed-tar",
491 # to be able to override \$SIG{__WARN__} to log build time warnings
492 use CGI::Carp; # it sets \$SIG{__WARN__} itself
494 my \$logdir = "$fqgitdir/gitweb/$httpd_only";
495 open my \$access_log_fh, '>>', "\$logdir/access.log"
496 or die "Couldn't open access log '\$logdir/access.log': \$!";
497 open my \$error_log_fh, '>>', "\$logdir/error.log"
498 or die "Couldn't open error log '\$logdir/error.log': \$!";
500 \$access_log_fh->autoflush(1);
501 \$error_log_fh->autoflush(1);
503 # redirect build time warnings to error.log
504 \$SIG{'__WARN__'} = sub {
506 # timestamp warning like in CGI::Carp::warn
507 my \$stamp = CGI::Carp::stamp();
508 \$msg =~ s/^/\$stamp/gm;
509 print \$error_log_fh \$msg;
512 # write errors to error.log, access to access.log
514 format => "combined",
515 logger => sub { print \$access_log_fh @_; };
520 \$env->{'psgi.errors'} = \$error_log_fh;
524 # gitweb currently doesn't work with $SIG{CHLD} set to 'IGNORE',
525 # because it uses 'close $fd or die...' on piped filehandle $fh
526 # (which causes the parent process to wait for child to finish).
527 enable_if { \$SIG{'CHLD'} eq 'IGNORE' } sub {
531 local \$SIG{'CHLD'} = 'DEFAULT';
532 local \$SIG{'CLD'} = 'DEFAULT';
536 # serve static files, i.e. stylesheet, images, script
538 path => sub { m!\.(js|css|png)\$! && s!^/gitweb/!! },
540 encoding => 'utf-8'; # encoding for 'text/plain' files
541 # convert CGI application to PSGI app
542 Plack::App::WrapCGI->new(script => "$root/gitweb.cgi")->to_app;
545 # make it runnable as standalone app,
546 # like it would be run via 'plackup' utility
547 if (__FILE__ eq \$0) {
548 require Plack::Runner;
550 my \$runner = Plack::Runner->new();
551 \$runner->parse_options(qw(--env deployment --port $port),
552 "$local" ? qw(--host 127.0.0.1) : ());
553 \$runner->run(\$app);
558 chmod a
+x
"$fqgitdir/gitweb/gitweb.psgi"
559 # configuration is embedded in server script file, gitweb.psgi
564 cat > "$fqgitdir/gitweb/gitweb_config.perl" <<EOF
566 our \$projectroot = "$(dirname "$fqgitdir")";
567 our \$git_temp = "$fqgitdir/gitweb/tmp";
568 our \$projects_list = \$projectroot;
572 test "$no_reuse" = true ||
test ! -e "$GITWEB_CONFIG" && gitweb_conf
575 mkdir
-p "$fqgitdir/gitweb/$httpd_only"
594 echo "Unknown httpd specified: $httpd"
600 url
=http
://127.0.0.1:$port
602 if test -n "$browser"; then
603 httpd_is_ready
&& git web--browse
-b "$browser" $url ||
echo $url
605 httpd_is_ready
&& git web--browse
-c "instaweb.browser" $url ||
echo $url