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
() {
49 # ensure that the apache2/lighttpd command ends with "-f"
50 if ! echo "$httpd" | sane_grep
-- '-f *$' >/dev
/null
2>&1
56 # server is started by running via generated gitweb.psgi in $fqgitdir/gitweb
57 full_httpd
="$fqgitdir/gitweb/gitweb.psgi"
58 httpd_only
="${httpd%% *}" # cut on first space
63 httpd_only
="$(echo $httpd | cut -f1 -d' ')"
64 if case "$httpd_only" in /*) : ;; *) which $httpd_only >/dev
/null
2>&1;; esac
68 # many httpds are installed in /usr/sbin or /usr/local/sbin
69 # these days and those are not in most users $PATHs
70 # in addition, we may have generated a server script
71 # in $fqgitdir/gitweb.
72 for i
in /usr
/local
/sbin
/usr
/sbin
"$root" "$fqgitdir/gitweb"
74 if test -x "$i/$httpd_only"
81 echo >&2 "$httpd_only not found. Install $httpd_only or use" \
82 "--httpd to specify another httpd daemon."
88 if test -f "$fqgitdir/pid"; then
89 say
"Instance already running. Restarting..."
93 # here $httpd should have a meaningful value
96 # don't quote $full_httpd, there can be arguments to it (-f)
99 #These servers don't have a daemon mode so we'll have to fork it
100 $full_httpd "$fqgitdir/gitweb/httpd.conf" &
101 #Save the pid before doing anything else (we'll print it later)
104 if test $?
!= 0; then
105 echo "Could not execute http daemon $httpd."
109 cat > "$fqgitdir/pid" <<EOF
114 $full_httpd "$fqgitdir/gitweb/httpd.conf"
115 if test $?
!= 0; then
116 echo "Could not execute http daemon $httpd."
124 test -f "$fqgitdir/pid" && kill $
(cat "$fqgitdir/pid")
125 rm -f "$fqgitdir/pid"
129 "$PERL" -MIO::Socket
::INET
-e "
130 local \$| = 1; # turn on autoflush
131 exit if (IO::Socket::INET->new('127.0.0.1:$port'));
132 print 'Waiting for \'$httpd\' to start ..';
136 } until (IO::Socket::INET->new('127.0.0.1:$port'));
189 mkdir
-p "$GIT_DIR/gitweb/tmp"
190 GIT_EXEC_PATH
="$(git --exec-path)"
192 GITWEB_CONFIG
="$fqgitdir/gitweb/gitweb_config.perl"
193 export GIT_EXEC_PATH GIT_DIR GITWEB_CONFIG
196 # generate a standalone server script in $fqgitdir/gitweb.
197 cat >"$fqgitdir/gitweb/$httpd.rb" <<EOF
200 options = YAML::load_file(ARGV[0])
201 options[:StartCallback] = proc do
202 File.open(options[:PidFile],"w") do |f|
206 options[:ServerType] = WEBrick::Daemon
207 server = WEBrick::HTTPServer.new(options)
208 ['INT', 'TERM'].each do |signal|
209 trap(signal) {server.shutdown}
213 # generate a shell script to invoke the above ruby script,
214 # which assumes _ruby_ is in the user's $PATH. that's _one_
215 # portable way to run ruby, which could be installed anywhere,
217 cat >"$fqgitdir/gitweb/$httpd" <<EOF
219 exec ruby "$fqgitdir/gitweb/$httpd.rb" \$*
221 chmod +x
"$fqgitdir/gitweb/$httpd"
225 :DocumentRoot: "$root"
226 :DirectoryIndex: ["gitweb.cgi"]
227 :PidFile: "$fqgitdir/pid"
229 test "$local" = true
&& echo ':BindAddress: "127.0.0.1"' >> "$conf"
234 server.document-root = "$root"
236 server.modules = ( "mod_setenv", "mod_cgi" )
237 server.indexfiles = ( "gitweb.cgi" )
238 server.pid-file = "$fqgitdir/pid"
239 server.errorlog = "$fqgitdir/gitweb/$httpd_only/error.log"
241 # to enable, add "mod_access", "mod_accesslog" to server.modules
242 # variable above and uncomment this
243 #accesslog.filename = "$fqgitdir/gitweb/$httpd_only/access.log"
245 setenv.add-environment = ( "PATH" => env.PATH, "GITWEB_CONFIG" => env.GITWEB_CONFIG )
247 cgi.assign = ( ".cgi" => "" )
251 ".pdf" => "application/pdf",
252 ".sig" => "application/pgp-signature",
253 ".spl" => "application/futuresplash",
254 ".class" => "application/octet-stream",
255 ".ps" => "application/postscript",
256 ".torrent" => "application/x-bittorrent",
257 ".dvi" => "application/x-dvi",
258 ".gz" => "application/x-gzip",
259 ".pac" => "application/x-ns-proxy-autoconfig",
260 ".swf" => "application/x-shockwave-flash",
261 ".tar.gz" => "application/x-tgz",
262 ".tgz" => "application/x-tgz",
263 ".tar" => "application/x-tar",
264 ".zip" => "application/zip",
265 ".mp3" => "audio/mpeg",
266 ".m3u" => "audio/x-mpegurl",
267 ".wma" => "audio/x-ms-wma",
268 ".wax" => "audio/x-ms-wax",
269 ".ogg" => "application/ogg",
270 ".wav" => "audio/x-wav",
271 ".gif" => "image/gif",
272 ".jpg" => "image/jpeg",
273 ".jpeg" => "image/jpeg",
274 ".png" => "image/png",
275 ".xbm" => "image/x-xbitmap",
276 ".xpm" => "image/x-xpixmap",
277 ".xwd" => "image/x-xwindowdump",
278 ".css" => "text/css",
279 ".html" => "text/html",
280 ".htm" => "text/html",
281 ".js" => "text/javascript",
282 ".asc" => "text/plain",
283 ".c" => "text/plain",
284 ".cpp" => "text/plain",
285 ".log" => "text/plain",
286 ".conf" => "text/plain",
287 ".text" => "text/plain",
288 ".txt" => "text/plain",
289 ".dtd" => "text/xml",
290 ".xml" => "text/xml",
291 ".mpeg" => "video/mpeg",
292 ".mpg" => "video/mpeg",
293 ".mov" => "video/quicktime",
294 ".qt" => "video/quicktime",
295 ".avi" => "video/x-msvideo",
296 ".asf" => "video/x-ms-asf",
297 ".asx" => "video/x-ms-asf",
298 ".wmv" => "video/x-ms-wmv",
299 ".bz2" => "application/x-bzip",
300 ".tbz" => "application/x-bzip-compressed-tar",
301 ".tar.bz2" => "application/x-bzip-compressed-tar",
305 test x
"$local" = xtrue
&& echo 'server.bind = "127.0.0.1"' >> "$conf"
309 test -z "$module_path" && module_path
=/usr
/lib
/apache
2/modules
311 test x
"$local" = xtrue
&& bind='127.0.0.1:'
312 echo 'text/css css' > "$fqgitdir/mime.types"
314 ServerName "git-instaweb"
317 ErrorLog "$fqgitdir/gitweb/$httpd_only/error.log"
318 CustomLog "$fqgitdir/gitweb/$httpd_only/access.log" combined
319 PidFile "$fqgitdir/pid"
323 for mod
in mime dir
; do
324 if test -e $module_path/mod_
${mod}.so
; then
325 echo "LoadModule ${mod}_module " \
326 "$module_path/mod_${mod}.so" >> "$conf"
330 TypesConfig "$fqgitdir/mime.types"
331 DirectoryIndex gitweb.cgi
334 # check to see if Dennis Stosberg's mod_perl compatibility patch
335 # (<20060621130708.Gcbc6e5c@leonov.stosberg.net>) has been applied
336 if test -f "$module_path/mod_perl.so" &&
337 sane_grep
'MOD_PERL' "$root/gitweb.cgi" >/dev
/null
339 # favor mod_perl if available
341 LoadModule perl_module $module_path/mod_perl.so
343 PerlPassEnv GIT_EXEC_DIR
344 PerlPassEnv GITWEB_CONFIG
345 <Location /gitweb.cgi>
346 SetHandler perl-script
347 PerlResponseHandler ModPerl::Registry
348 PerlOptions +ParseHeaders
355 list_mods
=$
(echo "$full_httpd" |
sed 's/-f$/-l/')
356 $list_mods | sane_grep
'mod_cgi\.c' >/dev
/null
2>&1 || \
357 if test -f "$module_path/mod_cgi.so"
359 echo "LoadModule cgi_module $module_path/mod_cgi.so" >> "$conf"
361 $list_mods |
grep 'mod_cgid\.c' >/dev
/null
2>&1 || \
362 if test -f "$module_path/mod_cgid.so"
364 echo "LoadModule cgid_module $module_path/mod_cgid.so" \
367 echo "You have no CGI support!"
370 echo "ScriptSock logs/gitweb.sock" >> "$conf"
373 AddHandler cgi-script .cgi
374 <Location /gitweb.cgi>
383 # Mongoose web server configuration file.
384 # Lines starting with '#' and empty lines are ignored.
385 # For detailed description of every option, visit
386 # http://code.google.com/p/mongoose/wiki/MongooseManual
390 index_files gitweb.cgi
391 #ssl_cert $fqgitdir/gitweb/ssl_cert.pem
392 error_log $fqgitdir/gitweb/$httpd_only/error.log
393 access_log $fqgitdir/gitweb/$httpd_only/access.log
396 cgi_env PATH=$PATH,GIT_DIR=$GIT_DIR,GIT_EXEC_PATH=$GIT_EXEC_PATH,GITWEB_CONFIG=$GITWEB_CONFIG
401 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
406 # generate a standalone 'plackup' server script in $fqgitdir/gitweb
407 # with embedded configuration; it does not use "$conf" file
408 cat > "$fqgitdir/gitweb/gitweb.psgi" <<EOF
411 # gitweb - simple web interface to track changes in git repositories
412 # PSGI wrapper and server starter (see http://plackperl.org)
419 use Plack::App::WrapCGI;
420 use CGI::Emulate::PSGI 0.07; # minimum version required to work with gitweb
422 # mimetype mapping (from lighttpd_conf)
423 Plack::MIME->add_type(
424 ".pdf" => "application/pdf",
425 ".sig" => "application/pgp-signature",
426 ".spl" => "application/futuresplash",
427 ".class" => "application/octet-stream",
428 ".ps" => "application/postscript",
429 ".torrent" => "application/x-bittorrent",
430 ".dvi" => "application/x-dvi",
431 ".gz" => "application/x-gzip",
432 ".pac" => "application/x-ns-proxy-autoconfig",
433 ".swf" => "application/x-shockwave-flash",
434 ".tar.gz" => "application/x-tgz",
435 ".tgz" => "application/x-tgz",
436 ".tar" => "application/x-tar",
437 ".zip" => "application/zip",
438 ".mp3" => "audio/mpeg",
439 ".m3u" => "audio/x-mpegurl",
440 ".wma" => "audio/x-ms-wma",
441 ".wax" => "audio/x-ms-wax",
442 ".ogg" => "application/ogg",
443 ".wav" => "audio/x-wav",
444 ".gif" => "image/gif",
445 ".jpg" => "image/jpeg",
446 ".jpeg" => "image/jpeg",
447 ".png" => "image/png",
448 ".xbm" => "image/x-xbitmap",
449 ".xpm" => "image/x-xpixmap",
450 ".xwd" => "image/x-xwindowdump",
451 ".css" => "text/css",
452 ".html" => "text/html",
453 ".htm" => "text/html",
454 ".js" => "text/javascript",
455 ".asc" => "text/plain",
456 ".c" => "text/plain",
457 ".cpp" => "text/plain",
458 ".log" => "text/plain",
459 ".conf" => "text/plain",
460 ".text" => "text/plain",
461 ".txt" => "text/plain",
462 ".dtd" => "text/xml",
463 ".xml" => "text/xml",
464 ".mpeg" => "video/mpeg",
465 ".mpg" => "video/mpeg",
466 ".mov" => "video/quicktime",
467 ".qt" => "video/quicktime",
468 ".avi" => "video/x-msvideo",
469 ".asf" => "video/x-ms-asf",
470 ".asx" => "video/x-ms-asf",
471 ".wmv" => "video/x-ms-wmv",
472 ".bz2" => "application/x-bzip",
473 ".tbz" => "application/x-bzip-compressed-tar",
474 ".tar.bz2" => "application/x-bzip-compressed-tar",
479 # to be able to override \$SIG{__WARN__} to log build time warnings
480 use CGI::Carp; # it sets \$SIG{__WARN__} itself
482 my \$logdir = "$fqgitdir/gitweb/$httpd_only";
483 open my \$access_log_fh, '>>', "\$logdir/access.log"
484 or die "Couldn't open access log '\$logdir/access.log': \$!";
485 open my \$error_log_fh, '>>', "\$logdir/error.log"
486 or die "Couldn't open error log '\$logdir/error.log': \$!";
488 \$access_log_fh->autoflush(1);
489 \$error_log_fh->autoflush(1);
491 # redirect build time warnings to error.log
492 \$SIG{'__WARN__'} = sub {
494 # timestamp warning like in CGI::Carp::warn
495 my \$stamp = CGI::Carp::stamp();
496 \$msg =~ s/^/\$stamp/gm;
497 print \$error_log_fh \$msg;
500 # write errors to error.log, access to access.log
502 format => "combined",
503 logger => sub { print \$access_log_fh @_; };
508 \$env->{'psgi.errors'} = \$error_log_fh;
512 # gitweb currently doesn't work with $SIG{CHLD} set to 'IGNORE',
513 # because it uses 'close $fd or die...' on piped filehandle $fh
514 # (which causes the parent process to wait for child to finish).
515 enable_if { \$SIG{'CHLD'} eq 'IGNORE' } sub {
519 local \$SIG{'CHLD'} = 'DEFAULT';
520 local \$SIG{'CLD'} = 'DEFAULT';
524 # serve static files, i.e. stylesheet, images, script
526 path => sub { m!\.(js|css|png)\$! && s!^/gitweb/!! },
528 encoding => 'utf-8'; # encoding for 'text/plain' files
529 # convert CGI application to PSGI app
530 Plack::App::WrapCGI->new(script => "$root/gitweb.cgi")->to_app;
533 # make it runnable as standalone app,
534 # like it would be run via 'plackup' utility
535 if (__FILE__ eq \$0) {
536 require Plack::Runner;
538 my \$runner = Plack::Runner->new();
539 \$runner->parse_options(qw(--env deployment --port $port),
540 "$local" ? qw(--host 127.0.0.1) : ());
541 \$runner->run(\$app);
546 chmod a
+x
"$fqgitdir/gitweb/gitweb.psgi"
547 # configuration is embedded in server script file, gitweb.psgi
552 cat > "$fqgitdir/gitweb/gitweb_config.perl" <<EOF
554 our \$projectroot = "$(dirname "$fqgitdir")";
555 our \$git_temp = "$fqgitdir/gitweb/tmp";
556 our \$projects_list = \$projectroot;
560 test "$no_reuse" = true ||
test ! -e "$GITWEB_CONFIG" && gitweb_conf
563 mkdir
-p "$fqgitdir/gitweb/$httpd_only"
582 echo "Unknown httpd specified: $httpd"
588 url
=http
://127.0.0.1:$port
590 if test -n "$browser"; then
591 httpd_is_ready
&& git web--browse
-b "$browser" $url ||
echo $url
593 httpd_is_ready
&& git web--browse
-c "instaweb.browser" $url ||
echo $url