gitweb: Add git_merge sub
[git/gsoc2010-gitweb.git] / git-instaweb.sh
blob36359747d546beafbb2b5375b6517fdd3cf399ea
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="$(git config --bool --get instaweb.overwrite)"
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 # Default is true -> overwrite gitweb_config.perl
47 test -z "$no_reuse" && no_reuse=true
49 resolve_full_httpd () {
50 case "$httpd" in
51 *apache2*|*lighttpd*)
52 # ensure that the apache2/lighttpd command ends with "-f"
53 if ! echo "$httpd" | sane_grep -- '-f *$' >/dev/null 2>&1
54 then
55 httpd="$httpd -f"
58 *plackup*)
59 # server is started by running via generated gitweb.psgi in $fqgitdir/gitweb
60 full_httpd="$fqgitdir/gitweb/gitweb.psgi"
61 httpd_only="${httpd%% *}" # cut on first space
62 return
64 esac
66 httpd_only="$(echo $httpd | cut -f1 -d' ')"
67 if case "$httpd_only" in /*) : ;; *) which $httpd_only >/dev/null 2>&1;; esac
68 then
69 full_httpd=$httpd
70 else
71 # many httpds are installed in /usr/sbin or /usr/local/sbin
72 # these days and those are not in most users $PATHs
73 # in addition, we may have generated a server script
74 # in $fqgitdir/gitweb.
75 for i in /usr/local/sbin /usr/sbin "$root" "$fqgitdir/gitweb"
77 if test -x "$i/$httpd_only"
78 then
79 full_httpd=$i/$httpd
80 return
82 done
84 echo >&2 "$httpd_only not found. Install $httpd_only or use" \
85 "--httpd to specify another httpd daemon."
86 exit 1
90 start_httpd () {
91 if test -f "$fqgitdir/pid"; then
92 say "Instance already running. Restarting..."
93 stop_httpd
96 # here $httpd should have a meaningful value
97 resolve_full_httpd
99 # don't quote $full_httpd, there can be arguments to it (-f)
100 case "$httpd" in
101 *mongoose*|*plackup*)
102 #These servers don't have a daemon mode so we'll have to fork it
103 $full_httpd "$fqgitdir/gitweb/httpd.conf" &
104 #Save the pid before doing anything else (we'll print it later)
105 pid=$!
107 if test $? != 0; then
108 echo "Could not execute http daemon $httpd."
109 exit 1
112 cat > "$fqgitdir/pid" <<EOF
113 $pid
117 $full_httpd "$fqgitdir/gitweb/httpd.conf"
118 if test $? != 0; then
119 echo "Could not execute http daemon $httpd."
120 exit 1
123 esac
126 stop_httpd () {
127 test -f "$fqgitdir/pid" && kill $(cat "$fqgitdir/pid")
128 rm -f "$fqgitdir/pid"
131 httpd_is_ready () {
132 "$PERL" -MIO::Socket::INET -e "
133 local \$| = 1; # turn on autoflush
134 exit if (IO::Socket::INET->new('127.0.0.1:$port'));
135 print 'Waiting for \'$httpd\' to start ..';
136 do {
137 print '.';
138 sleep(1);
139 } until (IO::Socket::INET->new('127.0.0.1:$port'));
140 print qq! (done)\n!;
144 while test $# != 0
146 case "$1" in
147 --stop|stop)
148 stop_httpd
149 exit 0
151 --start|start)
152 start_httpd
153 exit 0
155 --restart|restart)
156 stop_httpd
157 start_httpd
158 exit 0
160 -l|--local)
161 local=true
163 -d|--httpd)
164 shift
165 httpd="$1"
167 -b|--browser)
168 shift
169 browser="$1"
171 -p|--port)
172 shift
173 port="$1"
175 -m|--module-path)
176 shift
177 module_path="$1"
179 --reuse-config)
180 shift
181 no_reuse=false
186 usage
188 esac
189 shift
190 done
192 mkdir -p "$GIT_DIR/gitweb/tmp"
193 GIT_EXEC_PATH="$(git --exec-path)"
194 GIT_DIR="$fqgitdir"
195 GITWEB_CONFIG="$fqgitdir/gitweb/gitweb_config.perl"
196 export GIT_EXEC_PATH GIT_DIR GITWEB_CONFIG
198 webrick_conf () {
199 # generate a standalone server script in $fqgitdir/gitweb.
200 cat >"$fqgitdir/gitweb/$httpd.rb" <<EOF
201 require 'webrick'
202 require 'yaml'
203 options = YAML::load_file(ARGV[0])
204 options[:StartCallback] = proc do
205 File.open(options[:PidFile],"w") do |f|
206 f.puts Process.pid
209 options[:ServerType] = WEBrick::Daemon
210 server = WEBrick::HTTPServer.new(options)
211 ['INT', 'TERM'].each do |signal|
212 trap(signal) {server.shutdown}
214 server.start
216 # generate a shell script to invoke the above ruby script,
217 # which assumes _ruby_ is in the user's $PATH. that's _one_
218 # portable way to run ruby, which could be installed anywhere,
219 # really.
220 cat >"$fqgitdir/gitweb/$httpd" <<EOF
221 #!/bin/sh
222 exec ruby "$fqgitdir/gitweb/$httpd.rb" \$*
224 chmod +x "$fqgitdir/gitweb/$httpd"
226 cat >"$conf" <<EOF
227 :Port: $port
228 :DocumentRoot: "$root"
229 :DirectoryIndex: ["gitweb.cgi"]
230 :PidFile: "$fqgitdir/pid"
232 test "$local" = true && echo ':BindAddress: "127.0.0.1"' >> "$conf"
235 lighttpd_conf () {
236 cat > "$conf" <<EOF
237 server.document-root = "$root"
238 server.port = $port
239 server.modules = ( "mod_setenv", "mod_cgi" )
240 server.indexfiles = ( "gitweb.cgi" )
241 server.pid-file = "$fqgitdir/pid"
242 server.errorlog = "$fqgitdir/gitweb/$httpd_only/error.log"
244 # to enable, add "mod_access", "mod_accesslog" to server.modules
245 # variable above and uncomment this
246 #accesslog.filename = "$fqgitdir/gitweb/$httpd_only/access.log"
248 setenv.add-environment = ( "PATH" => env.PATH, "GITWEB_CONFIG" => env.GITWEB_CONFIG )
250 cgi.assign = ( ".cgi" => "" )
252 # mimetype mapping
253 mimetype.assign = (
254 ".pdf" => "application/pdf",
255 ".sig" => "application/pgp-signature",
256 ".spl" => "application/futuresplash",
257 ".class" => "application/octet-stream",
258 ".ps" => "application/postscript",
259 ".torrent" => "application/x-bittorrent",
260 ".dvi" => "application/x-dvi",
261 ".gz" => "application/x-gzip",
262 ".pac" => "application/x-ns-proxy-autoconfig",
263 ".swf" => "application/x-shockwave-flash",
264 ".tar.gz" => "application/x-tgz",
265 ".tgz" => "application/x-tgz",
266 ".tar" => "application/x-tar",
267 ".zip" => "application/zip",
268 ".mp3" => "audio/mpeg",
269 ".m3u" => "audio/x-mpegurl",
270 ".wma" => "audio/x-ms-wma",
271 ".wax" => "audio/x-ms-wax",
272 ".ogg" => "application/ogg",
273 ".wav" => "audio/x-wav",
274 ".gif" => "image/gif",
275 ".jpg" => "image/jpeg",
276 ".jpeg" => "image/jpeg",
277 ".png" => "image/png",
278 ".xbm" => "image/x-xbitmap",
279 ".xpm" => "image/x-xpixmap",
280 ".xwd" => "image/x-xwindowdump",
281 ".css" => "text/css",
282 ".html" => "text/html",
283 ".htm" => "text/html",
284 ".js" => "text/javascript",
285 ".asc" => "text/plain",
286 ".c" => "text/plain",
287 ".cpp" => "text/plain",
288 ".log" => "text/plain",
289 ".conf" => "text/plain",
290 ".text" => "text/plain",
291 ".txt" => "text/plain",
292 ".dtd" => "text/xml",
293 ".xml" => "text/xml",
294 ".mpeg" => "video/mpeg",
295 ".mpg" => "video/mpeg",
296 ".mov" => "video/quicktime",
297 ".qt" => "video/quicktime",
298 ".avi" => "video/x-msvideo",
299 ".asf" => "video/x-ms-asf",
300 ".asx" => "video/x-ms-asf",
301 ".wmv" => "video/x-ms-wmv",
302 ".bz2" => "application/x-bzip",
303 ".tbz" => "application/x-bzip-compressed-tar",
304 ".tar.bz2" => "application/x-bzip-compressed-tar",
305 "" => "text/plain"
308 test x"$local" = xtrue && echo 'server.bind = "127.0.0.1"' >> "$conf"
311 apache2_conf () {
312 test -z "$module_path" && module_path=/usr/lib/apache2/modules
313 bind=
314 test x"$local" = xtrue && bind='127.0.0.1:'
315 echo 'text/css css' > "$fqgitdir/mime.types"
316 cat > "$conf" <<EOF
317 ServerName "git-instaweb"
318 ServerRoot "$root"
319 DocumentRoot "$root"
320 ErrorLog "$fqgitdir/gitweb/$httpd_only/error.log"
321 CustomLog "$fqgitdir/gitweb/$httpd_only/access.log" combined
322 PidFile "$fqgitdir/pid"
323 Listen $bind$port
326 for mod in mime dir; do
327 if test -e $module_path/mod_${mod}.so; then
328 echo "LoadModule ${mod}_module " \
329 "$module_path/mod_${mod}.so" >> "$conf"
331 done
332 cat >> "$conf" <<EOF
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
341 then
342 # favor mod_perl if available
343 cat >> "$conf" <<EOF
344 LoadModule perl_module $module_path/mod_perl.so
345 PerlPassEnv GIT_DIR
346 PerlPassEnv GIT_EXEC_DIR
347 PerlPassEnv GITWEB_CONFIG
348 <Location /gitweb.cgi>
349 SetHandler perl-script
350 PerlResponseHandler ModPerl::Registry
351 PerlOptions +ParseHeaders
352 Options +ExecCGI
353 </Location>
355 else
356 # plain-old CGI
357 resolve_full_httpd
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"
361 then
362 echo "LoadModule cgi_module $module_path/mod_cgi.so" >> "$conf"
363 else
364 $list_mods | grep 'mod_cgid\.c' >/dev/null 2>&1 || \
365 if test -f "$module_path/mod_cgid.so"
366 then
367 echo "LoadModule cgid_module $module_path/mod_cgid.so" \
368 >> "$conf"
369 else
370 echo "You have no CGI support!"
371 exit 2
373 echo "ScriptSock logs/gitweb.sock" >> "$conf"
375 cat >> "$conf" <<EOF
376 AddHandler cgi-script .cgi
377 <Location /gitweb.cgi>
378 Options +ExecCGI
379 </Location>
384 mongoose_conf() {
385 cat > "$conf" <<EOF
386 # Mongoose web server configuration file.
387 # Lines starting with '#' and empty lines are ignored.
388 # For detailed description of every option, visit
389 # http://code.google.com/p/mongoose/wiki/MongooseManual
391 root $root
392 ports $port
393 index_files gitweb.cgi
394 #ssl_cert $fqgitdir/gitweb/ssl_cert.pem
395 error_log $fqgitdir/gitweb/$httpd_only/error.log
396 access_log $fqgitdir/gitweb/$httpd_only/access.log
398 #cgi setup
399 cgi_env PATH=$PATH,GIT_DIR=$GIT_DIR,GIT_EXEC_PATH=$GIT_EXEC_PATH,GITWEB_CONFIG=$GITWEB_CONFIG
400 cgi_interp $PERL
401 cgi_ext cgi,pl
403 # mimetype mapping
404 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
408 plackup_conf () {
409 # generate a standalone 'plackup' server script in $fqgitdir/gitweb
410 # with embedded configuration; it does not use "$conf" file
411 cat > "$fqgitdir/gitweb/gitweb.psgi" <<EOF
412 #!$PERL
414 # gitweb - simple web interface to track changes in git repositories
415 # PSGI wrapper and server starter (see http://plackperl.org)
417 use strict;
419 use IO::Handle;
420 use Plack::MIME;
421 use Plack::Builder;
422 use Plack::App::WrapCGI;
423 use CGI::Emulate::PSGI 0.07; # minimum version required to work with gitweb
425 # mimetype mapping (from lighttpd_conf)
426 Plack::MIME->add_type(
427 ".pdf" => "application/pdf",
428 ".sig" => "application/pgp-signature",
429 ".spl" => "application/futuresplash",
430 ".class" => "application/octet-stream",
431 ".ps" => "application/postscript",
432 ".torrent" => "application/x-bittorrent",
433 ".dvi" => "application/x-dvi",
434 ".gz" => "application/x-gzip",
435 ".pac" => "application/x-ns-proxy-autoconfig",
436 ".swf" => "application/x-shockwave-flash",
437 ".tar.gz" => "application/x-tgz",
438 ".tgz" => "application/x-tgz",
439 ".tar" => "application/x-tar",
440 ".zip" => "application/zip",
441 ".mp3" => "audio/mpeg",
442 ".m3u" => "audio/x-mpegurl",
443 ".wma" => "audio/x-ms-wma",
444 ".wax" => "audio/x-ms-wax",
445 ".ogg" => "application/ogg",
446 ".wav" => "audio/x-wav",
447 ".gif" => "image/gif",
448 ".jpg" => "image/jpeg",
449 ".jpeg" => "image/jpeg",
450 ".png" => "image/png",
451 ".xbm" => "image/x-xbitmap",
452 ".xpm" => "image/x-xpixmap",
453 ".xwd" => "image/x-xwindowdump",
454 ".css" => "text/css",
455 ".html" => "text/html",
456 ".htm" => "text/html",
457 ".js" => "text/javascript",
458 ".asc" => "text/plain",
459 ".c" => "text/plain",
460 ".cpp" => "text/plain",
461 ".log" => "text/plain",
462 ".conf" => "text/plain",
463 ".text" => "text/plain",
464 ".txt" => "text/plain",
465 ".dtd" => "text/xml",
466 ".xml" => "text/xml",
467 ".mpeg" => "video/mpeg",
468 ".mpg" => "video/mpeg",
469 ".mov" => "video/quicktime",
470 ".qt" => "video/quicktime",
471 ".avi" => "video/x-msvideo",
472 ".asf" => "video/x-ms-asf",
473 ".asx" => "video/x-ms-asf",
474 ".wmv" => "video/x-ms-wmv",
475 ".bz2" => "application/x-bzip",
476 ".tbz" => "application/x-bzip-compressed-tar",
477 ".tar.bz2" => "application/x-bzip-compressed-tar",
478 "" => "text/plain"
481 my \$app = builder {
482 # to be able to override \$SIG{__WARN__} to log build time warnings
483 use CGI::Carp; # it sets \$SIG{__WARN__} itself
485 my \$logdir = "$fqgitdir/gitweb/$httpd_only";
486 open my \$access_log_fh, '>', "\$logdir/access.log"
487 or die "Couldn't open access log '\$logdir/access.log': \$!";
488 open my \$error_log_fh, '>', "\$logdir/error.log"
489 or die "Couldn't open error log '\$logdir/error.log': \$!";
491 \$access_log_fh->autoflush(1);
492 \$error_log_fh->autoflush(1);
494 # redirect build time warnings to error.log
495 \$SIG{'__WARN__'} = sub {
496 my \$msg = shift;
497 # timestamp warning like in CGI::Carp::warn
498 my \$stamp = CGI::Carp::stamp();
499 \$msg =~ s/^/\$stamp/gm;
500 print \$error_log_fh \$msg;
503 # write errors to error.log, access to access.log
504 enable 'AccessLog',
505 format => "combined",
506 logger => sub { print \$access_log_fh @_; };
507 enable sub {
508 my \$app = shift;
509 sub {
510 my \$env = shift;
511 \$env->{'psgi.errors'} = \$error_log_fh;
512 \$app->(\$env);
515 # gitweb currently doesn't work with $SIG{CHLD} set to 'IGNORE',
516 # because it uses 'close $fd or die...' on piped filehandle $fh
517 # (which causes the parent process to wait for child to finish).
518 enable_if { \$SIG{'CHLD'} eq 'IGNORE' } sub {
519 my \$app = shift;
520 sub {
521 my \$env = shift;
522 local \$SIG{'CHLD'} = 'DEFAULT';
523 local \$SIG{'CLD'} = 'DEFAULT';
524 \$app->(\$env);
527 # serve static files, i.e. stylesheet, images, script
528 enable 'Static',
529 path => sub { m!\.(js|css|png)\$! && s!^/gitweb/!! },
530 root => "$root/",
531 encoding => 'utf-8'; # encoding for 'text/plain' files
532 # convert CGI application to PSGI app
533 Plack::App::WrapCGI->new(script => "$root/gitweb.cgi")->to_app;
536 # make it runnable as standalone app,
537 # like it would be run via 'plackup' utility
538 if (__FILE__ eq \$0) {
539 require Plack::Runner;
541 my \$runner = Plack::Runner->new();
542 \$runner->parse_options(qw(--env deployment --port $port),
543 "$local" ? qw(--host 127.0.0.1) : ());
544 \$runner->run(\$app);
546 __END__
549 chmod a+x "$fqgitdir/gitweb/gitweb.psgi"
550 # configuration is embedded in server script file, gitweb.psgi
551 rm -f "$conf"
554 gitweb_conf() {
555 cat > "$fqgitdir/gitweb/gitweb_config.perl" <<EOF
556 #!/usr/bin/perl
557 our \$projectroot = "$(dirname "$fqgitdir")";
558 our \$git_temp = "$fqgitdir/gitweb/tmp";
559 our \$projects_list = \$projectroot;
563 test "$no_reuse" = true || test ! -e "$GITWEB_CONFIG" && gitweb_conf
565 resolve_full_httpd
566 mkdir -p "$fqgitdir/gitweb/$httpd_only"
568 case "$httpd" in
569 *lighttpd*)
570 lighttpd_conf
572 *apache2*)
573 apache2_conf
575 webrick)
576 webrick_conf
578 *mongoose*)
579 mongoose_conf
581 *plackup*)
582 plackup_conf
585 echo "Unknown httpd specified: $httpd"
586 exit 1
588 esac
590 start_httpd
591 url=http://127.0.0.1:$port
593 if test -n "$browser"; then
594 httpd_is_ready && git web--browse -b "$browser" $url || echo $url
595 else
596 httpd_is_ready && git web--browse -c "instaweb.browser" $url || echo $url