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
62 httpd_only
="$(echo $httpd | cut -f1 -d' ')"
63 if case "$httpd_only" in /*) : ;; *) which $httpd_only >/dev
/null
2>&1;; esac
67 # many httpds are installed in /usr/sbin or /usr/local/sbin
68 # these days and those are not in most users $PATHs
69 # in addition, we may have generated a server script
70 # in $fqgitdir/gitweb.
71 for i
in /usr
/local
/sbin
/usr
/sbin
"$root" "$fqgitdir/gitweb"
73 if test -x "$i/$httpd_only"
80 echo >&2 "$httpd_only not found. Install $httpd_only or use" \
81 "--httpd to specify another httpd daemon."
87 if test -f "$fqgitdir/pid"; then
88 say
"Instance already running. Restarting..."
92 # here $httpd should have a meaningful value
95 # don't quote $full_httpd, there can be arguments to it (-f)
98 #These servers don't have a daemon mode so we'll have to fork it
99 $full_httpd "$fqgitdir/gitweb/httpd.conf" &
100 #Save the pid before doing anything else (we'll print it later)
103 if test $?
!= 0; then
104 echo "Could not execute http daemon $httpd."
108 cat > "$fqgitdir/pid" <<EOF
113 $full_httpd "$fqgitdir/gitweb/httpd.conf"
114 if test $?
!= 0; then
115 echo "Could not execute http daemon $httpd."
123 test -f "$fqgitdir/pid" && kill $
(cat "$fqgitdir/pid")
124 rm -f "$fqgitdir/pid"
128 "$PERL" -MIO::Socket
::INET
-e "
129 local \$| = 1; # turn on autoflush
130 exit if (IO::Socket::INET->new('127.0.0.1:$port'));
131 print 'Waiting for \'$httpd\' to start ..';
135 } until (IO::Socket::INET->new('127.0.0.1:$port'));
184 mkdir
-p "$GIT_DIR/gitweb/tmp"
185 GIT_EXEC_PATH
="$(git --exec-path)"
187 GITWEB_CONFIG
="$fqgitdir/gitweb/gitweb_config.perl"
188 export GIT_EXEC_PATH GIT_DIR GITWEB_CONFIG
191 # generate a standalone server script in $fqgitdir/gitweb.
192 cat >"$fqgitdir/gitweb/$httpd.rb" <<EOF
195 options = YAML::load_file(ARGV[0])
196 options[:StartCallback] = proc do
197 File.open(options[:PidFile],"w") do |f|
201 options[:ServerType] = WEBrick::Daemon
202 server = WEBrick::HTTPServer.new(options)
203 ['INT', 'TERM'].each do |signal|
204 trap(signal) {server.shutdown}
208 # generate a shell script to invoke the above ruby script,
209 # which assumes _ruby_ is in the user's $PATH. that's _one_
210 # portable way to run ruby, which could be installed anywhere,
212 cat >"$fqgitdir/gitweb/$httpd" <<EOF
214 exec ruby "$fqgitdir/gitweb/$httpd.rb" \$*
216 chmod +x
"$fqgitdir/gitweb/$httpd"
220 :DocumentRoot: "$root"
221 :DirectoryIndex: ["gitweb.cgi"]
222 :PidFile: "$fqgitdir/pid"
224 test "$local" = true
&& echo ':BindAddress: "127.0.0.1"' >> "$conf"
229 server.document-root = "$root"
231 server.modules = ( "mod_setenv", "mod_cgi" )
232 server.indexfiles = ( "gitweb.cgi" )
233 server.pid-file = "$fqgitdir/pid"
234 server.errorlog = "$fqgitdir/gitweb/$httpd_only/error.log"
236 # to enable, add "mod_access", "mod_accesslog" to server.modules
237 # variable above and uncomment this
238 #accesslog.filename = "$fqgitdir/gitweb/$httpd_only/access.log"
240 setenv.add-environment = ( "PATH" => env.PATH, "GITWEB_CONFIG" => env.GITWEB_CONFIG )
242 cgi.assign = ( ".cgi" => "" )
246 ".pdf" => "application/pdf",
247 ".sig" => "application/pgp-signature",
248 ".spl" => "application/futuresplash",
249 ".class" => "application/octet-stream",
250 ".ps" => "application/postscript",
251 ".torrent" => "application/x-bittorrent",
252 ".dvi" => "application/x-dvi",
253 ".gz" => "application/x-gzip",
254 ".pac" => "application/x-ns-proxy-autoconfig",
255 ".swf" => "application/x-shockwave-flash",
256 ".tar.gz" => "application/x-tgz",
257 ".tgz" => "application/x-tgz",
258 ".tar" => "application/x-tar",
259 ".zip" => "application/zip",
260 ".mp3" => "audio/mpeg",
261 ".m3u" => "audio/x-mpegurl",
262 ".wma" => "audio/x-ms-wma",
263 ".wax" => "audio/x-ms-wax",
264 ".ogg" => "application/ogg",
265 ".wav" => "audio/x-wav",
266 ".gif" => "image/gif",
267 ".jpg" => "image/jpeg",
268 ".jpeg" => "image/jpeg",
269 ".png" => "image/png",
270 ".xbm" => "image/x-xbitmap",
271 ".xpm" => "image/x-xpixmap",
272 ".xwd" => "image/x-xwindowdump",
273 ".css" => "text/css",
274 ".html" => "text/html",
275 ".htm" => "text/html",
276 ".js" => "text/javascript",
277 ".asc" => "text/plain",
278 ".c" => "text/plain",
279 ".cpp" => "text/plain",
280 ".log" => "text/plain",
281 ".conf" => "text/plain",
282 ".text" => "text/plain",
283 ".txt" => "text/plain",
284 ".dtd" => "text/xml",
285 ".xml" => "text/xml",
286 ".mpeg" => "video/mpeg",
287 ".mpg" => "video/mpeg",
288 ".mov" => "video/quicktime",
289 ".qt" => "video/quicktime",
290 ".avi" => "video/x-msvideo",
291 ".asf" => "video/x-ms-asf",
292 ".asx" => "video/x-ms-asf",
293 ".wmv" => "video/x-ms-wmv",
294 ".bz2" => "application/x-bzip",
295 ".tbz" => "application/x-bzip-compressed-tar",
296 ".tar.bz2" => "application/x-bzip-compressed-tar",
300 test x
"$local" = xtrue
&& echo 'server.bind = "127.0.0.1"' >> "$conf"
304 if test -z "$module_path"
306 test -d "/usr/lib/httpd/modules" &&
307 module_path
="/usr/lib/httpd/modules"
308 test -d "/usr/lib/apache2/modules" &&
309 module_path
="/usr/lib/apache2/modules"
312 test x
"$local" = xtrue
&& bind='127.0.0.1:'
313 echo 'text/css css' > "$fqgitdir/mime.types"
315 ServerName "git-instaweb"
318 ErrorLog "$fqgitdir/gitweb/$httpd_only/error.log"
319 CustomLog "$fqgitdir/gitweb/$httpd_only/access.log" combined
320 PidFile "$fqgitdir/pid"
324 for mod
in mime dir env log_config
326 if test -e $module_path/mod_
${mod}.so
328 echo "LoadModule ${mod}_module " \
329 "$module_path/mod_${mod}.so" >> "$conf"
333 TypesConfig "$fqgitdir/mime.types"
334 DirectoryIndex gitweb.cgi
337 # check to see if Dennis Stosberg's mod_perl compatibility patch
338 # (<20060621130708.Gcbc6e5c@leonov.stosberg.net>) has been applied
339 if test -f "$module_path/mod_perl.so" &&
340 sane_grep
'MOD_PERL' "$root/gitweb.cgi" >/dev
/null
342 # favor mod_perl if available
344 LoadModule perl_module $module_path/mod_perl.so
346 PerlPassEnv GIT_EXEC_PATH
347 PerlPassEnv GITWEB_CONFIG
348 <Location /gitweb.cgi>
349 SetHandler perl-script
350 PerlResponseHandler ModPerl::Registry
351 PerlOptions +ParseHeaders
358 list_mods
=$
(echo "$full_httpd" |
sed 's/-f$/-l/')
359 $list_mods | sane_grep
'mod_cgi\.c' >/dev
/null
2>&1 || \
360 if test -f "$module_path/mod_cgi.so"
362 echo "LoadModule cgi_module $module_path/mod_cgi.so" >> "$conf"
364 $list_mods |
grep 'mod_cgid\.c' >/dev
/null
2>&1 || \
365 if test -f "$module_path/mod_cgid.so"
367 echo "LoadModule cgid_module $module_path/mod_cgid.so" \
370 echo "You have no CGI support!"
373 echo "ScriptSock logs/gitweb.sock" >> "$conf"
377 PassEnv GIT_EXEC_PATH
378 PassEnv GITWEB_CONFIG
379 AddHandler cgi-script .cgi
380 <Location /gitweb.cgi>
389 # Mongoose web server configuration file.
390 # Lines starting with '#' and empty lines are ignored.
391 # For detailed description of every option, visit
392 # http://code.google.com/p/mongoose/wiki/MongooseManual
396 index_files gitweb.cgi
397 #ssl_cert $fqgitdir/gitweb/ssl_cert.pem
398 error_log $fqgitdir/gitweb/$httpd_only/error.log
399 access_log $fqgitdir/gitweb/$httpd_only/access.log
402 cgi_env PATH=$PATH,GIT_DIR=$GIT_DIR,GIT_EXEC_PATH=$GIT_EXEC_PATH,GITWEB_CONFIG=$GITWEB_CONFIG
407 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
412 # generate a standalone 'plackup' server script in $fqgitdir/gitweb
413 # with embedded configuration; it does not use "$conf" file
414 cat > "$fqgitdir/gitweb/gitweb.psgi" <<EOF
417 # gitweb - simple web interface to track changes in git repositories
418 # PSGI wrapper and server starter (see http://plackperl.org)
425 use Plack::App::WrapCGI;
426 use CGI::Emulate::PSGI 0.07; # minimum version required to work with gitweb
428 # mimetype mapping (from lighttpd_conf)
429 Plack::MIME->add_type(
430 ".pdf" => "application/pdf",
431 ".sig" => "application/pgp-signature",
432 ".spl" => "application/futuresplash",
433 ".class" => "application/octet-stream",
434 ".ps" => "application/postscript",
435 ".torrent" => "application/x-bittorrent",
436 ".dvi" => "application/x-dvi",
437 ".gz" => "application/x-gzip",
438 ".pac" => "application/x-ns-proxy-autoconfig",
439 ".swf" => "application/x-shockwave-flash",
440 ".tar.gz" => "application/x-tgz",
441 ".tgz" => "application/x-tgz",
442 ".tar" => "application/x-tar",
443 ".zip" => "application/zip",
444 ".mp3" => "audio/mpeg",
445 ".m3u" => "audio/x-mpegurl",
446 ".wma" => "audio/x-ms-wma",
447 ".wax" => "audio/x-ms-wax",
448 ".ogg" => "application/ogg",
449 ".wav" => "audio/x-wav",
450 ".gif" => "image/gif",
451 ".jpg" => "image/jpeg",
452 ".jpeg" => "image/jpeg",
453 ".png" => "image/png",
454 ".xbm" => "image/x-xbitmap",
455 ".xpm" => "image/x-xpixmap",
456 ".xwd" => "image/x-xwindowdump",
457 ".css" => "text/css",
458 ".html" => "text/html",
459 ".htm" => "text/html",
460 ".js" => "text/javascript",
461 ".asc" => "text/plain",
462 ".c" => "text/plain",
463 ".cpp" => "text/plain",
464 ".log" => "text/plain",
465 ".conf" => "text/plain",
466 ".text" => "text/plain",
467 ".txt" => "text/plain",
468 ".dtd" => "text/xml",
469 ".xml" => "text/xml",
470 ".mpeg" => "video/mpeg",
471 ".mpg" => "video/mpeg",
472 ".mov" => "video/quicktime",
473 ".qt" => "video/quicktime",
474 ".avi" => "video/x-msvideo",
475 ".asf" => "video/x-ms-asf",
476 ".asx" => "video/x-ms-asf",
477 ".wmv" => "video/x-ms-wmv",
478 ".bz2" => "application/x-bzip",
479 ".tbz" => "application/x-bzip-compressed-tar",
480 ".tar.bz2" => "application/x-bzip-compressed-tar",
485 # to be able to override \$SIG{__WARN__} to log build time warnings
486 use CGI::Carp; # it sets \$SIG{__WARN__} itself
488 my \$logdir = "$fqgitdir/gitweb/$httpd_only";
489 open my \$access_log_fh, '>>', "\$logdir/access.log"
490 or die "Couldn't open access log '\$logdir/access.log': \$!";
491 open my \$error_log_fh, '>>', "\$logdir/error.log"
492 or die "Couldn't open error log '\$logdir/error.log': \$!";
494 \$access_log_fh->autoflush(1);
495 \$error_log_fh->autoflush(1);
497 # redirect build time warnings to error.log
498 \$SIG{'__WARN__'} = sub {
500 # timestamp warning like in CGI::Carp::warn
501 my \$stamp = CGI::Carp::stamp();
502 \$msg =~ s/^/\$stamp/gm;
503 print \$error_log_fh \$msg;
506 # write errors to error.log, access to access.log
508 format => "combined",
509 logger => sub { print \$access_log_fh @_; };
514 \$env->{'psgi.errors'} = \$error_log_fh;
518 # gitweb currently doesn't work with $SIG{CHLD} set to 'IGNORE',
519 # because it uses 'close $fd or die...' on piped filehandle $fh
520 # (which causes the parent process to wait for child to finish).
521 enable_if { \$SIG{'CHLD'} eq 'IGNORE' } sub {
525 local \$SIG{'CHLD'} = 'DEFAULT';
526 local \$SIG{'CLD'} = 'DEFAULT';
530 # serve static files, i.e. stylesheet, images, script
532 path => sub { m!\.(js|css|png)\$! && s!^/gitweb/!! },
534 encoding => 'utf-8'; # encoding for 'text/plain' files
535 # convert CGI application to PSGI app
536 Plack::App::WrapCGI->new(script => "$root/gitweb.cgi")->to_app;
539 # make it runnable as standalone app,
540 # like it would be run via 'plackup' utility
541 if (__FILE__ eq \$0) {
542 require Plack::Runner;
544 my \$runner = Plack::Runner->new();
545 \$runner->parse_options(qw(--env deployment --port $port),
546 "$local" ? qw(--host 127.0.0.1) : ());
547 \$runner->run(\$app);
552 chmod a
+x
"$fqgitdir/gitweb/gitweb.psgi"
553 # configuration is embedded in server script file, gitweb.psgi
558 cat > "$fqgitdir/gitweb/gitweb_config.perl" <<EOF
560 our \$projectroot = "$(dirname "$fqgitdir")";
561 our \$git_temp = "$fqgitdir/gitweb/tmp";
562 our \$projects_list = \$projectroot;
569 mkdir
-p "$fqgitdir/gitweb/$httpd_only"
588 echo "Unknown httpd specified: $httpd"
594 url
=http
://127.0.0.1:$port
596 if test -n "$browser"; then
597 httpd_is_ready
&& git web--browse
-b "$browser" $url ||
echo $url
599 httpd_is_ready
&& git web--browse
-c "instaweb.browser" $url ||
echo $url