git-gui: Avoid using the <<Copy>> binding as a menu accelerator on win32
[git/dscho.git] / git-instaweb.sh
blob98e54db7af63c757a669c136fd16f499f8c73dc5
1 #!/bin/sh
3 # Copyright (c) 2006 Eric Wong
6 PERL='@@PERL@@'
7 OPTIONS_KEEPDASHDASH=
8 OPTIONS_SPEC="\
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
17 Action
18 stop stop the web server
19 start start the web server
20 restart restart the web server
23 . git-sh-setup
25 fqgitdir="$GIT_DIR"
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)"
31 no_reuse=true
33 conf="$GIT_DIR/gitweb/httpd.conf"
35 # Defaults:
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 () {
47 case "$httpd" in
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
52 then
53 httpd="$httpd -f"
56 *plackup*)
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
60 return
62 esac
64 httpd_only="$(echo $httpd | cut -f1 -d' ')"
65 if case "$httpd_only" in /*) : ;; *) which $httpd_only >/dev/null 2>&1;; esac
66 then
67 full_httpd=$httpd
68 else
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"
76 then
77 full_httpd=$i/$httpd
78 return
80 done
82 echo >&2 "$httpd_only not found. Install $httpd_only or use" \
83 "--httpd to specify another httpd daemon."
84 exit 1
88 start_httpd () {
89 if test -f "$fqgitdir/pid"; then
90 say "Instance already running. Restarting..."
91 stop_httpd
94 # here $httpd should have a meaningful value
95 resolve_full_httpd
97 # don't quote $full_httpd, there can be arguments to it (-f)
98 case "$httpd" in
99 *mongoose*|*plackup*)
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)
103 pid=$!
105 if test $? != 0; then
106 echo "Could not execute http daemon $httpd."
107 exit 1
110 cat > "$fqgitdir/pid" <<EOF
111 $pid
115 $full_httpd "$fqgitdir/gitweb/httpd.conf"
116 if test $? != 0; then
117 echo "Could not execute http daemon $httpd."
118 exit 1
121 esac
124 stop_httpd () {
125 test -f "$fqgitdir/pid" && kill $(cat "$fqgitdir/pid")
126 rm -f "$fqgitdir/pid"
129 httpd_is_ready () {
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 ..';
134 do {
135 print '.';
136 sleep(1);
137 } until (IO::Socket::INET->new('127.0.0.1:$port'));
138 print qq! (done)\n!;
142 while test $# != 0
144 case "$1" in
145 --stop|stop)
146 stop_httpd
147 exit 0
149 --start|start)
150 start_httpd
151 exit 0
153 --restart|restart)
154 stop_httpd
155 start_httpd
156 exit 0
158 -l|--local)
159 local=true
161 -d|--httpd)
162 shift
163 httpd="$1"
165 -b|--browser)
166 shift
167 browser="$1"
169 -p|--port)
170 shift
171 port="$1"
173 -m|--module-path)
174 shift
175 module_path="$1"
177 --reuse-config)
178 shift
179 no_reuse=false
184 usage
186 esac
187 shift
188 done
190 mkdir -p "$GIT_DIR/gitweb/tmp"
191 GIT_EXEC_PATH="$(git --exec-path)"
192 GIT_DIR="$fqgitdir"
193 GITWEB_CONFIG="$fqgitdir/gitweb/gitweb_config.perl"
194 export GIT_EXEC_PATH GIT_DIR GITWEB_CONFIG
196 webrick_conf () {
197 # generate a standalone server script in $fqgitdir/gitweb.
198 cat >"$fqgitdir/gitweb/$httpd.rb" <<EOF
199 require 'webrick'
200 require 'yaml'
201 options = YAML::load_file(ARGV[0])
202 options[:StartCallback] = proc do
203 File.open(options[:PidFile],"w") do |f|
204 f.puts Process.pid
207 options[:ServerType] = WEBrick::Daemon
208 server = WEBrick::HTTPServer.new(options)
209 ['INT', 'TERM'].each do |signal|
210 trap(signal) {server.shutdown}
212 server.start
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,
217 # really.
218 cat >"$fqgitdir/gitweb/$httpd" <<EOF
219 #!/bin/sh
220 exec ruby "$fqgitdir/gitweb/$httpd.rb" \$*
222 chmod +x "$fqgitdir/gitweb/$httpd"
224 cat >"$conf" <<EOF
225 :Port: $port
226 :DocumentRoot: "$root"
227 :DirectoryIndex: ["gitweb.cgi"]
228 :PidFile: "$fqgitdir/pid"
230 test "$local" = true && echo ':BindAddress: "127.0.0.1"' >> "$conf"
233 lighttpd_conf () {
234 cat > "$conf" <<EOF
235 server.document-root = "$root"
236 server.port = $port
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" => "" )
250 # mimetype mapping
251 mimetype.assign = (
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",
303 "" => "text/plain"
306 test x"$local" = xtrue && echo 'server.bind = "127.0.0.1"' >> "$conf"
309 apache2_conf () {
310 if test -z "$module_path"
311 then
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"
317 bind=
318 test x"$local" = xtrue && bind='127.0.0.1:'
319 echo 'text/css css' > "$fqgitdir/mime.types"
320 cat > "$conf" <<EOF
321 ServerName "git-instaweb"
322 ServerRoot "$root"
323 DocumentRoot "$root"
324 ErrorLog "$fqgitdir/gitweb/$httpd_only/error.log"
325 CustomLog "$fqgitdir/gitweb/$httpd_only/access.log" combined
326 PidFile "$fqgitdir/pid"
327 Listen $bind$port
330 for mod in mime dir env log_config
332 if test -e $module_path/mod_${mod}.so
333 then
334 echo "LoadModule ${mod}_module " \
335 "$module_path/mod_${mod}.so" >> "$conf"
337 done
338 cat >> "$conf" <<EOF
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
347 then
348 # favor mod_perl if available
349 cat >> "$conf" <<EOF
350 LoadModule perl_module $module_path/mod_perl.so
351 PerlPassEnv GIT_DIR
352 PerlPassEnv GIT_EXEC_PATH
353 PerlPassEnv GITWEB_CONFIG
354 <Location /gitweb.cgi>
355 SetHandler perl-script
356 PerlResponseHandler ModPerl::Registry
357 PerlOptions +ParseHeaders
358 Options +ExecCGI
359 </Location>
361 else
362 # plain-old CGI
363 resolve_full_httpd
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"
367 then
368 echo "LoadModule cgi_module $module_path/mod_cgi.so" >> "$conf"
369 else
370 $list_mods | grep 'mod_cgid\.c' >/dev/null 2>&1 || \
371 if test -f "$module_path/mod_cgid.so"
372 then
373 echo "LoadModule cgid_module $module_path/mod_cgid.so" \
374 >> "$conf"
375 else
376 echo "You have no CGI support!"
377 exit 2
379 echo "ScriptSock logs/gitweb.sock" >> "$conf"
381 cat >> "$conf" <<EOF
382 PassEnv GIT_DIR
383 PassEnv GIT_EXEC_PATH
384 PassEnv GITWEB_CONFIG
385 AddHandler cgi-script .cgi
386 <Location /gitweb.cgi>
387 Options +ExecCGI
388 </Location>
393 mongoose_conf() {
394 cat > "$conf" <<EOF
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
400 root $root
401 ports $port
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
407 #cgi setup
408 cgi_env PATH=$PATH,GIT_DIR=$GIT_DIR,GIT_EXEC_PATH=$GIT_EXEC_PATH,GITWEB_CONFIG=$GITWEB_CONFIG
409 cgi_interp $PERL
410 cgi_ext cgi,pl
412 # mimetype mapping
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
417 plackup_conf () {
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
421 #!$PERL
423 # gitweb - simple web interface to track changes in git repositories
424 # PSGI wrapper and server starter (see http://plackperl.org)
426 use strict;
428 use IO::Handle;
429 use Plack::MIME;
430 use Plack::Builder;
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",
487 "" => "text/plain"
490 my \$app = builder {
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 {
505 my \$msg = shift;
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
513 enable 'AccessLog',
514 format => "combined",
515 logger => sub { print \$access_log_fh @_; };
516 enable sub {
517 my \$app = shift;
518 sub {
519 my \$env = shift;
520 \$env->{'psgi.errors'} = \$error_log_fh;
521 \$app->(\$env);
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 {
528 my \$app = shift;
529 sub {
530 my \$env = shift;
531 local \$SIG{'CHLD'} = 'DEFAULT';
532 local \$SIG{'CLD'} = 'DEFAULT';
533 \$app->(\$env);
536 # serve static files, i.e. stylesheet, images, script
537 enable 'Static',
538 path => sub { m!\.(js|css|png)\$! && s!^/gitweb/!! },
539 root => "$root/",
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);
555 __END__
558 chmod a+x "$fqgitdir/gitweb/gitweb.psgi"
559 # configuration is embedded in server script file, gitweb.psgi
560 rm -f "$conf"
563 gitweb_conf() {
564 cat > "$fqgitdir/gitweb/gitweb_config.perl" <<EOF
565 #!/usr/bin/perl
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
574 resolve_full_httpd
575 mkdir -p "$fqgitdir/gitweb/$httpd_only"
577 case "$httpd" in
578 *lighttpd*)
579 lighttpd_conf
581 *apache2*|*httpd*)
582 apache2_conf
584 webrick)
585 webrick_conf
587 *mongoose*)
588 mongoose_conf
590 *plackup*)
591 plackup_conf
594 echo "Unknown httpd specified: $httpd"
595 exit 1
597 esac
599 start_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
604 else
605 httpd_is_ready && git web--browse -c "instaweb.browser" $url || echo $url