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 () {
47 # ensure that the apache2/lighttpd command ends with "-f"
48 if ! echo "$httpd" | sane_grep -- '-f *$' >/dev/null 2>&1
54 # server is started by running via generated gitweb.psgi in $fqgitdir/gitweb
55 full_httpd="$fqgitdir/gitweb/gitweb.psgi"
56 httpd_only="${httpd%% *}" # cut on first space
61 httpd_only="$(echo $httpd | cut -f1 -d' ')"
62 if case "$httpd_only" in /*) : ;; *) which $httpd_only >/dev/null 2>&1;; esac
66 # many httpds are installed in /usr/sbin or /usr/local/sbin
67 # these days and those are not in most users $PATHs
68 # in addition, we may have generated a server script
69 # in $fqgitdir/gitweb.
70 for i in /usr/local/sbin /usr/sbin "$root" "$fqgitdir/gitweb"
72 if test -x "$i/$httpd_only"
79 echo >&2 "$httpd_only not found. Install $httpd_only or use" \
80 "--httpd to specify another httpd daemon."
86 if test -f "$fqgitdir/pid"; then
87 say "Instance already running. Restarting..."
91 # here $httpd should have a meaningful value
94 # don't quote $full_httpd, there can be arguments to it (-f)
97 #These servers don't have a daemon mode so we'll have to fork it
98 $full_httpd "$fqgitdir/gitweb/httpd.conf" &
99 #Save the pid before doing anything else (we'll print it later)
102 if test $? != 0; then
103 echo "Could not execute http daemon $httpd."
107 cat > "$fqgitdir/pid" <<EOF
112 $full_httpd "$fqgitdir/gitweb/httpd.conf"
113 if test $? != 0; then
114 echo "Could not execute http daemon $httpd."
122 test -f "$fqgitdir/pid" && kill $(cat "$fqgitdir/pid")
123 rm -f "$fqgitdir/pid"
127 "$PERL" -MIO::Socket::INET -e "
128 local \$| = 1; # turn on autoflush
129 exit if (IO::Socket::INET->new('127.0.0.1:$port'));
130 print 'Waiting for \'$httpd\' to start ..';
134 } until (IO::Socket::INET->new('127.0.0.1:$port'));
183 mkdir -p "$GIT_DIR/gitweb/tmp"
184 GIT_EXEC_PATH="$(git --exec-path)"
186 GITWEB_CONFIG="$fqgitdir/gitweb/gitweb_config.perl"
187 export GIT_EXEC_PATH GIT_DIR GITWEB_CONFIG
190 # generate a standalone server script in $fqgitdir/gitweb.
191 cat >"$fqgitdir/gitweb/$httpd.rb" <<EOF
194 options = YAML::load_file(ARGV[0])
195 options[:StartCallback] = proc do
196 File.open(options[:PidFile],"w") do |f|
200 options[:ServerType] = WEBrick::Daemon
201 server = WEBrick::HTTPServer.new(options)
202 ['INT', 'TERM'].each do |signal|
203 trap(signal) {server.shutdown}
207 # generate a shell script to invoke the above ruby script,
208 # which assumes _ruby_ is in the user's $PATH. that's _one_
209 # portable way to run ruby, which could be installed anywhere,
211 cat >"$fqgitdir/gitweb/$httpd" <<EOF
213 exec ruby "$fqgitdir/gitweb/$httpd.rb" \$*
215 chmod +x "$fqgitdir/gitweb/$httpd"
219 :DocumentRoot: "$root"
220 :DirectoryIndex: ["gitweb.cgi"]
221 :PidFile: "$fqgitdir/pid"
223 test "$local" = true && echo ':BindAddress: "127.0.0.1"' >> "$conf"
228 server.document-root = "$root"
230 server.modules = ( "mod_setenv", "mod_cgi" )
231 server.indexfiles = ( "gitweb.cgi" )
232 server.pid-file = "$fqgitdir/pid"
233 server.errorlog = "$fqgitdir/gitweb/$httpd_only/error.log"
235 # to enable, add "mod_access", "mod_accesslog" to server.modules
236 # variable above and uncomment this
237 #accesslog.filename = "$fqgitdir/gitweb/$httpd_only/access.log"
239 setenv.add-environment = ( "PATH" => env.PATH, "GITWEB_CONFIG" => env.GITWEB_CONFIG )
241 cgi.assign = ( ".cgi" => "" )
245 ".pdf" => "application/pdf",
246 ".sig" => "application/pgp-signature",
247 ".spl" => "application/futuresplash",
248 ".class" => "application/octet-stream",
249 ".ps" => "application/postscript",
250 ".torrent" => "application/x-bittorrent",
251 ".dvi" => "application/x-dvi",
252 ".gz" => "application/x-gzip",
253 ".pac" => "application/x-ns-proxy-autoconfig",
254 ".swf" => "application/x-shockwave-flash",
255 ".tar.gz" => "application/x-tgz",
256 ".tgz" => "application/x-tgz",
257 ".tar" => "application/x-tar",
258 ".zip" => "application/zip",
259 ".mp3" => "audio/mpeg",
260 ".m3u" => "audio/x-mpegurl",
261 ".wma" => "audio/x-ms-wma",
262 ".wax" => "audio/x-ms-wax",
263 ".ogg" => "application/ogg",
264 ".wav" => "audio/x-wav",
265 ".gif" => "image/gif",
266 ".jpg" => "image/jpeg",
267 ".jpeg" => "image/jpeg",
268 ".png" => "image/png",
269 ".xbm" => "image/x-xbitmap",
270 ".xpm" => "image/x-xpixmap",
271 ".xwd" => "image/x-xwindowdump",
272 ".css" => "text/css",
273 ".html" => "text/html",
274 ".htm" => "text/html",
275 ".js" => "text/javascript",
276 ".asc" => "text/plain",
277 ".c" => "text/plain",
278 ".cpp" => "text/plain",
279 ".log" => "text/plain",
280 ".conf" => "text/plain",
281 ".text" => "text/plain",
282 ".txt" => "text/plain",
283 ".dtd" => "text/xml",
284 ".xml" => "text/xml",
285 ".mpeg" => "video/mpeg",
286 ".mpg" => "video/mpeg",
287 ".mov" => "video/quicktime",
288 ".qt" => "video/quicktime",
289 ".avi" => "video/x-msvideo",
290 ".asf" => "video/x-ms-asf",
291 ".asx" => "video/x-ms-asf",
292 ".wmv" => "video/x-ms-wmv",
293 ".bz2" => "application/x-bzip",
294 ".tbz" => "application/x-bzip-compressed-tar",
295 ".tar.bz2" => "application/x-bzip-compressed-tar",
299 test x"$local" = xtrue && echo 'server.bind = "127.0.0.1"' >> "$conf"
303 test -z "$module_path" && module_path=/usr/lib/apache2/modules
305 test x"$local" = xtrue && bind='127.0.0.1:'
306 echo 'text/css css' > "$fqgitdir/mime.types"
308 ServerName "git-instaweb"
311 ErrorLog "$fqgitdir/gitweb/$httpd_only/error.log"
312 CustomLog "$fqgitdir/gitweb/$httpd_only/access.log" combined
313 PidFile "$fqgitdir/pid"
317 for mod in mime dir; do
318 if test -e $module_path/mod_${mod}.so; then
319 echo "LoadModule ${mod}_module " \
320 "$module_path/mod_${mod}.so" >> "$conf"
324 TypesConfig "$fqgitdir/mime.types"
325 DirectoryIndex gitweb.cgi
328 # check to see if Dennis Stosberg's mod_perl compatibility patch
329 # (<20060621130708.Gcbc6e5c@leonov.stosberg.net>) has been applied
330 if test -f "$module_path/mod_perl.so" &&
331 sane_grep 'MOD_PERL' "$root/gitweb.cgi" >/dev/null
333 # favor mod_perl if available
335 LoadModule perl_module $module_path/mod_perl.so
337 PerlPassEnv GIT_EXEC_DIR
338 PerlPassEnv GITWEB_CONFIG
339 <Location /gitweb.cgi>
340 SetHandler perl-script
341 PerlResponseHandler ModPerl::Registry
342 PerlOptions +ParseHeaders
349 list_mods=$(echo "$full_httpd" | sed 's/-f$/-l/')
350 $list_mods | sane_grep 'mod_cgi\.c' >/dev/null 2>&1 || \
351 if test -f "$module_path/mod_cgi.so"
353 echo "LoadModule cgi_module $module_path/mod_cgi.so" >> "$conf"
355 $list_mods | grep 'mod_cgid\.c' >/dev/null 2>&1 || \
356 if test -f "$module_path/mod_cgid.so"
358 echo "LoadModule cgid_module $module_path/mod_cgid.so" \
361 echo "You have no CGI support!"
364 echo "ScriptSock logs/gitweb.sock" >> "$conf"
367 AddHandler cgi-script .cgi
368 <Location /gitweb.cgi>
377 # Mongoose web server configuration file.
378 # Lines starting with '#' and empty lines are ignored.
379 # For detailed description of every option, visit
380 # http://code.google.com/p/mongoose/wiki/MongooseManual
384 index_files gitweb.cgi
385 #ssl_cert $fqgitdir/gitweb/ssl_cert.pem
386 error_log $fqgitdir/gitweb/$httpd_only/error.log
387 access_log $fqgitdir/gitweb/$httpd_only/access.log
390 cgi_env PATH=$PATH,GIT_DIR=$GIT_DIR,GIT_EXEC_PATH=$GIT_EXEC_PATH,GITWEB_CONFIG=$GITWEB_CONFIG
395 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
400 # generate a standalone 'plackup' server script in $fqgitdir/gitweb
401 # with embedded configuration; it does not use "$conf" file
402 cat > "$fqgitdir/gitweb/gitweb.psgi" <<EOF
405 # gitweb - simple web interface to track changes in git repositories
406 # PSGI wrapper and server starter (see http://plackperl.org)
413 use Plack::App::WrapCGI;
414 use CGI::Emulate::PSGI 0.07; # minimum version required to work with gitweb
416 # mimetype mapping (from lighttpd_conf)
417 Plack::MIME->add_type(
418 ".pdf" => "application/pdf",
419 ".sig" => "application/pgp-signature",
420 ".spl" => "application/futuresplash",
421 ".class" => "application/octet-stream",
422 ".ps" => "application/postscript",
423 ".torrent" => "application/x-bittorrent",
424 ".dvi" => "application/x-dvi",
425 ".gz" => "application/x-gzip",
426 ".pac" => "application/x-ns-proxy-autoconfig",
427 ".swf" => "application/x-shockwave-flash",
428 ".tar.gz" => "application/x-tgz",
429 ".tgz" => "application/x-tgz",
430 ".tar" => "application/x-tar",
431 ".zip" => "application/zip",
432 ".mp3" => "audio/mpeg",
433 ".m3u" => "audio/x-mpegurl",
434 ".wma" => "audio/x-ms-wma",
435 ".wax" => "audio/x-ms-wax",
436 ".ogg" => "application/ogg",
437 ".wav" => "audio/x-wav",
438 ".gif" => "image/gif",
439 ".jpg" => "image/jpeg",
440 ".jpeg" => "image/jpeg",
441 ".png" => "image/png",
442 ".xbm" => "image/x-xbitmap",
443 ".xpm" => "image/x-xpixmap",
444 ".xwd" => "image/x-xwindowdump",
445 ".css" => "text/css",
446 ".html" => "text/html",
447 ".htm" => "text/html",
448 ".js" => "text/javascript",
449 ".asc" => "text/plain",
450 ".c" => "text/plain",
451 ".cpp" => "text/plain",
452 ".log" => "text/plain",
453 ".conf" => "text/plain",
454 ".text" => "text/plain",
455 ".txt" => "text/plain",
456 ".dtd" => "text/xml",
457 ".xml" => "text/xml",
458 ".mpeg" => "video/mpeg",
459 ".mpg" => "video/mpeg",
460 ".mov" => "video/quicktime",
461 ".qt" => "video/quicktime",
462 ".avi" => "video/x-msvideo",
463 ".asf" => "video/x-ms-asf",
464 ".asx" => "video/x-ms-asf",
465 ".wmv" => "video/x-ms-wmv",
466 ".bz2" => "application/x-bzip",
467 ".tbz" => "application/x-bzip-compressed-tar",
468 ".tar.bz2" => "application/x-bzip-compressed-tar",
473 # to be able to override \$SIG{__WARN__} to log build time warnings
474 use CGI::Carp; # it sets \$SIG{__WARN__} itself
476 my \$logdir = "$fqgitdir/gitweb/$httpd_only";
477 open my \$access_log_fh, '>>', "\$logdir/access.log"
478 or die "Couldn't open access log '\$logdir/access.log': \$!";
479 open my \$error_log_fh, '>>', "\$logdir/error.log"
480 or die "Couldn't open error log '\$logdir/error.log': \$!";
482 \$access_log_fh->autoflush(1);
483 \$error_log_fh->autoflush(1);
485 # redirect build time warnings to error.log
486 \$SIG{'__WARN__'} = sub {
488 # timestamp warning like in CGI::Carp::warn
489 my \$stamp = CGI::Carp::stamp();
490 \$msg =~ s/^/\$stamp/gm;
491 print \$error_log_fh \$msg;
494 # write errors to error.log, access to access.log
496 format => "combined",
497 logger => sub { print \$access_log_fh @_; };
502 \$env->{'psgi.errors'} = \$error_log_fh;
506 # gitweb currently doesn't work with $SIG{CHLD} set to 'IGNORE',
507 # because it uses 'close $fd or die...' on piped filehandle $fh
508 # (which causes the parent process to wait for child to finish).
509 enable_if { \$SIG{'CHLD'} eq 'IGNORE' } sub {
513 local \$SIG{'CHLD'} = 'DEFAULT';
514 local \$SIG{'CLD'} = 'DEFAULT';
518 # serve static files, i.e. stylesheet, images, script
520 path => sub { m!\.(js|css|png)\$! && s!^/gitweb/!! },
522 encoding => 'utf-8'; # encoding for 'text/plain' files
523 # convert CGI application to PSGI app
524 Plack::App::WrapCGI->new(script => "$root/gitweb.cgi")->to_app;
527 # make it runnable as standalone app,
528 # like it would be run via 'plackup' utility
529 if (__FILE__ eq \$0) {
530 require Plack::Runner;
532 my \$runner = Plack::Runner->new();
533 \$runner->parse_options(qw(--env deployment --port $port),
534 "$local" ? qw(--host 127.0.0.1) : ());
535 \$runner->run(\$app);
540 chmod a+x "$fqgitdir/gitweb/gitweb.psgi"
541 # configuration is embedded in server script file, gitweb.psgi
546 cat > "$fqgitdir/gitweb/gitweb_config.perl" <<EOF
548 our \$projectroot = "$(dirname "$fqgitdir")";
549 our \$git_temp = "$fqgitdir/gitweb/tmp";
550 our \$projects_list = \$projectroot;
557 mkdir -p "$fqgitdir/gitweb/$httpd_only"
576 echo "Unknown httpd specified: $httpd"
582 url=http://127.0.0.1:$port
584 if test -n "$browser"; then
585 httpd_is_ready && git web--browse -b "$browser" $url || echo $url
587 httpd_is_ready && git web--browse -c "instaweb.browser" $url || echo $url