gitweb: Fix passing parameters to git_project_search_form
[git/jnareb-git.git] / git-instaweb.sh
blob01a1b05e6bdcd12f82f70282975780d3a19d910d
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 Action
17 stop stop the web server
18 start start the web server
19 restart restart the web server
22 . git-sh-setup
24 fqgitdir="$GIT_DIR"
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)"
30 action="browse"
32 conf="$GIT_DIR/gitweb/httpd.conf"
34 # Defaults:
36 # if installed, it doesn't need further configuration (module_path)
37 test -z "$httpd" && httpd='lighttpd -f'
39 # Default is @@GITWEBDIR@@
40 test -z "$root" && root='@@GITWEBDIR@@'
42 # any untaken local port will do...
43 test -z "$port" && port=1234
45 resolve_full_httpd () {
46 case "$httpd" in
47 *apache2*|*lighttpd*|*httpd*)
48 # yes, *httpd* covers *lighttpd* above, but it is there for clarity
49 # ensure that the apache2/lighttpd command ends with "-f"
50 if ! echo "$httpd" | sane_grep -- '-f *$' >/dev/null 2>&1
51 then
52 httpd="$httpd -f"
55 *plackup*)
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
59 return
61 *webrick*)
62 # server is started by running via generated webrick.rb in
63 # $fqgitdir/gitweb
64 full_httpd="$fqgitdir/gitweb/webrick.rb"
65 httpd_only="${httpd%% *}" # cut on first space
66 return
68 esac
70 httpd_only="$(echo $httpd | cut -f1 -d' ')"
71 if case "$httpd_only" in /*) : ;; *) which $httpd_only >/dev/null 2>&1;; esac
72 then
73 full_httpd=$httpd
74 else
75 # many httpds are installed in /usr/sbin or /usr/local/sbin
76 # these days and those are not in most users $PATHs
77 # in addition, we may have generated a server script
78 # in $fqgitdir/gitweb.
79 for i in /usr/local/sbin /usr/sbin "$root" "$fqgitdir/gitweb"
81 if test -x "$i/$httpd_only"
82 then
83 full_httpd=$i/$httpd
84 return
86 done
88 echo >&2 "$httpd_only not found. Install $httpd_only or use" \
89 "--httpd to specify another httpd daemon."
90 exit 1
94 start_httpd () {
95 if test -f "$fqgitdir/pid"; then
96 say "Instance already running. Restarting..."
97 stop_httpd
100 # here $httpd should have a meaningful value
101 resolve_full_httpd
102 mkdir -p "$fqgitdir/gitweb/$httpd_only"
103 conf="$fqgitdir/gitweb/$httpd_only.conf"
105 # generate correct config file if it doesn't exist
106 test -f "$conf" || configure_httpd
107 test -f "$fqgitdir/gitweb/gitweb_config.perl" || gitweb_conf
109 # don't quote $full_httpd, there can be arguments to it (-f)
110 case "$httpd" in
111 *mongoose*|*plackup*)
112 #These servers don't have a daemon mode so we'll have to fork it
113 $full_httpd "$conf" &
114 #Save the pid before doing anything else (we'll print it later)
115 pid=$!
117 if test $? != 0; then
118 echo "Could not execute http daemon $httpd."
119 exit 1
122 cat > "$fqgitdir/pid" <<EOF
123 $pid
127 $full_httpd "$conf"
128 if test $? != 0; then
129 echo "Could not execute http daemon $httpd."
130 exit 1
133 esac
136 stop_httpd () {
137 test -f "$fqgitdir/pid" && kill $(cat "$fqgitdir/pid")
138 rm -f "$fqgitdir/pid"
141 httpd_is_ready () {
142 "$PERL" -MIO::Socket::INET -e "
143 local \$| = 1; # turn on autoflush
144 exit if (IO::Socket::INET->new('127.0.0.1:$port'));
145 print 'Waiting for \'$httpd\' to start ..';
146 do {
147 print '.';
148 sleep(1);
149 } until (IO::Socket::INET->new('127.0.0.1:$port'));
150 print qq! (done)\n!;
154 while test $# != 0
156 case "$1" in
157 --stop|stop)
158 action="stop"
160 --start|start)
161 action="start"
163 --restart|restart)
164 action="restart"
166 -l|--local)
167 local=true
169 -d|--httpd)
170 shift
171 httpd="$1"
173 -b|--browser)
174 shift
175 browser="$1"
177 -p|--port)
178 shift
179 port="$1"
181 -m|--module-path)
182 shift
183 module_path="$1"
188 usage
190 esac
191 shift
192 done
194 mkdir -p "$GIT_DIR/gitweb/tmp"
195 GIT_EXEC_PATH="$(git --exec-path)"
196 GIT_DIR="$fqgitdir"
197 GITWEB_CONFIG="$fqgitdir/gitweb/gitweb_config.perl"
198 export GIT_EXEC_PATH GIT_DIR GITWEB_CONFIG
200 webrick_conf () {
201 # webrick seems to have no way of passing arbitrary environment
202 # variables to the underlying CGI executable, so we wrap the
203 # actual gitweb.cgi using a shell script to force it
204 wrapper="$fqgitdir/gitweb/$httpd/wrapper.sh"
205 cat > "$wrapper" <<EOF
206 #!/bin/sh
207 # we use this shell script wrapper around the real gitweb.cgi since
208 # there appears to be no other way to pass arbitrary environment variables
209 # into the CGI process
210 GIT_EXEC_PATH=$GIT_EXEC_PATH GIT_DIR=$GIT_DIR GITWEB_CONFIG=$GITWEB_CONFIG
211 export GIT_EXEC_PATH GIT_DIR GITWEB_CONFIG
212 exec $root/gitweb.cgi
214 chmod +x "$wrapper"
216 # This assumes _ruby_ is in the user's $PATH. that's _one_
217 # portable way to run ruby, which could be installed anywhere, really.
218 # generate a standalone server script in $fqgitdir/gitweb.
219 cat >"$fqgitdir/gitweb/$httpd.rb" <<EOF
220 #!/usr/bin/env ruby
221 require 'webrick'
222 require 'logger'
223 options = {
224 :Port => $port,
225 :DocumentRoot => "$root",
226 :Logger => Logger.new('$fqgitdir/gitweb/error.log'),
227 :AccessLog => [
228 [ Logger.new('$fqgitdir/gitweb/access.log'),
229 WEBrick::AccessLog::COMBINED_LOG_FORMAT ]
231 :DirectoryIndex => ["gitweb.cgi"],
232 :CGIInterpreter => "$wrapper",
233 :StartCallback => lambda do
234 File.open("$fqgitdir/pid", "w") { |f| f.puts Process.pid }
235 end,
236 :ServerType => WEBrick::Daemon,
238 options[:BindAddress] = '127.0.0.1' if "$local" == "true"
239 server = WEBrick::HTTPServer.new(options)
240 ['INT', 'TERM'].each do |signal|
241 trap(signal) {server.shutdown}
243 server.start
245 chmod +x "$fqgitdir/gitweb/$httpd.rb"
246 # configuration is embedded in server script file, webrick.rb
247 rm -f "$conf"
250 lighttpd_conf () {
251 cat > "$conf" <<EOF
252 server.document-root = "$root"
253 server.port = $port
254 server.modules = ( "mod_setenv", "mod_cgi" )
255 server.indexfiles = ( "gitweb.cgi" )
256 server.pid-file = "$fqgitdir/pid"
257 server.errorlog = "$fqgitdir/gitweb/$httpd_only/error.log"
259 # to enable, add "mod_access", "mod_accesslog" to server.modules
260 # variable above and uncomment this
261 #accesslog.filename = "$fqgitdir/gitweb/$httpd_only/access.log"
263 setenv.add-environment = ( "PATH" => env.PATH, "GITWEB_CONFIG" => env.GITWEB_CONFIG )
265 cgi.assign = ( ".cgi" => "" )
267 # mimetype mapping
268 mimetype.assign = (
269 ".pdf" => "application/pdf",
270 ".sig" => "application/pgp-signature",
271 ".spl" => "application/futuresplash",
272 ".class" => "application/octet-stream",
273 ".ps" => "application/postscript",
274 ".torrent" => "application/x-bittorrent",
275 ".dvi" => "application/x-dvi",
276 ".gz" => "application/x-gzip",
277 ".pac" => "application/x-ns-proxy-autoconfig",
278 ".swf" => "application/x-shockwave-flash",
279 ".tar.gz" => "application/x-tgz",
280 ".tgz" => "application/x-tgz",
281 ".tar" => "application/x-tar",
282 ".zip" => "application/zip",
283 ".mp3" => "audio/mpeg",
284 ".m3u" => "audio/x-mpegurl",
285 ".wma" => "audio/x-ms-wma",
286 ".wax" => "audio/x-ms-wax",
287 ".ogg" => "application/ogg",
288 ".wav" => "audio/x-wav",
289 ".gif" => "image/gif",
290 ".jpg" => "image/jpeg",
291 ".jpeg" => "image/jpeg",
292 ".png" => "image/png",
293 ".xbm" => "image/x-xbitmap",
294 ".xpm" => "image/x-xpixmap",
295 ".xwd" => "image/x-xwindowdump",
296 ".css" => "text/css",
297 ".html" => "text/html",
298 ".htm" => "text/html",
299 ".js" => "text/javascript",
300 ".asc" => "text/plain",
301 ".c" => "text/plain",
302 ".cpp" => "text/plain",
303 ".log" => "text/plain",
304 ".conf" => "text/plain",
305 ".text" => "text/plain",
306 ".txt" => "text/plain",
307 ".dtd" => "text/xml",
308 ".xml" => "text/xml",
309 ".mpeg" => "video/mpeg",
310 ".mpg" => "video/mpeg",
311 ".mov" => "video/quicktime",
312 ".qt" => "video/quicktime",
313 ".avi" => "video/x-msvideo",
314 ".asf" => "video/x-ms-asf",
315 ".asx" => "video/x-ms-asf",
316 ".wmv" => "video/x-ms-wmv",
317 ".bz2" => "application/x-bzip",
318 ".tbz" => "application/x-bzip-compressed-tar",
319 ".tar.bz2" => "application/x-bzip-compressed-tar",
320 "" => "text/plain"
323 test x"$local" = xtrue && echo 'server.bind = "127.0.0.1"' >> "$conf"
326 apache2_conf () {
327 if test -z "$module_path"
328 then
329 test -d "/usr/lib/httpd/modules" &&
330 module_path="/usr/lib/httpd/modules"
331 test -d "/usr/lib/apache2/modules" &&
332 module_path="/usr/lib/apache2/modules"
334 bind=
335 test x"$local" = xtrue && bind='127.0.0.1:'
336 echo 'text/css css' > "$fqgitdir/mime.types"
337 cat > "$conf" <<EOF
338 ServerName "git-instaweb"
339 ServerRoot "$root"
340 DocumentRoot "$root"
341 ErrorLog "$fqgitdir/gitweb/$httpd_only/error.log"
342 CustomLog "$fqgitdir/gitweb/$httpd_only/access.log" combined
343 PidFile "$fqgitdir/pid"
344 Listen $bind$port
347 for mod in mime dir env log_config
349 if test -e $module_path/mod_${mod}.so
350 then
351 echo "LoadModule ${mod}_module " \
352 "$module_path/mod_${mod}.so" >> "$conf"
354 done
355 cat >> "$conf" <<EOF
356 TypesConfig "$fqgitdir/mime.types"
357 DirectoryIndex gitweb.cgi
360 # check to see if Dennis Stosberg's mod_perl compatibility patch
361 # (<20060621130708.Gcbc6e5c@leonov.stosberg.net>) has been applied
362 if test -f "$module_path/mod_perl.so" &&
363 sane_grep 'MOD_PERL' "$root/gitweb.cgi" >/dev/null
364 then
365 # favor mod_perl if available
366 cat >> "$conf" <<EOF
367 LoadModule perl_module $module_path/mod_perl.so
368 PerlPassEnv GIT_DIR
369 PerlPassEnv GIT_EXEC_PATH
370 PerlPassEnv GITWEB_CONFIG
371 <Location /gitweb.cgi>
372 SetHandler perl-script
373 PerlResponseHandler ModPerl::Registry
374 PerlOptions +ParseHeaders
375 Options +ExecCGI
376 </Location>
378 else
379 # plain-old CGI
380 resolve_full_httpd
381 list_mods=$(echo "$full_httpd" | sed 's/-f$/-l/')
382 $list_mods | sane_grep 'mod_cgi\.c' >/dev/null 2>&1 || \
383 if test -f "$module_path/mod_cgi.so"
384 then
385 echo "LoadModule cgi_module $module_path/mod_cgi.so" >> "$conf"
386 else
387 $list_mods | grep 'mod_cgid\.c' >/dev/null 2>&1 || \
388 if test -f "$module_path/mod_cgid.so"
389 then
390 echo "LoadModule cgid_module $module_path/mod_cgid.so" \
391 >> "$conf"
392 else
393 echo "You have no CGI support!"
394 exit 2
396 echo "ScriptSock logs/gitweb.sock" >> "$conf"
398 cat >> "$conf" <<EOF
399 PassEnv GIT_DIR
400 PassEnv GIT_EXEC_PATH
401 PassEnv GITWEB_CONFIG
402 AddHandler cgi-script .cgi
403 <Location /gitweb.cgi>
404 Options +ExecCGI
405 </Location>
410 mongoose_conf() {
411 cat > "$conf" <<EOF
412 # Mongoose web server configuration file.
413 # Lines starting with '#' and empty lines are ignored.
414 # For detailed description of every option, visit
415 # http://code.google.com/p/mongoose/wiki/MongooseManual
417 root $root
418 ports $port
419 index_files gitweb.cgi
420 #ssl_cert $fqgitdir/gitweb/ssl_cert.pem
421 error_log $fqgitdir/gitweb/$httpd_only/error.log
422 access_log $fqgitdir/gitweb/$httpd_only/access.log
424 #cgi setup
425 cgi_env PATH=$PATH,GIT_DIR=$GIT_DIR,GIT_EXEC_PATH=$GIT_EXEC_PATH,GITWEB_CONFIG=$GITWEB_CONFIG
426 cgi_interp $PERL
427 cgi_ext cgi,pl
429 # mimetype mapping
430 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
434 plackup_conf () {
435 # generate a standalone 'plackup' server script in $fqgitdir/gitweb
436 # with embedded configuration; it does not use "$conf" file
437 cat > "$fqgitdir/gitweb/gitweb.psgi" <<EOF
438 #!$PERL
440 # gitweb - simple web interface to track changes in git repositories
441 # PSGI wrapper and server starter (see http://plackperl.org)
443 use strict;
445 use IO::Handle;
446 use Plack::MIME;
447 use Plack::Builder;
448 use Plack::App::WrapCGI;
449 use CGI::Emulate::PSGI 0.07; # minimum version required to work with gitweb
451 # mimetype mapping (from lighttpd_conf)
452 Plack::MIME->add_type(
453 ".pdf" => "application/pdf",
454 ".sig" => "application/pgp-signature",
455 ".spl" => "application/futuresplash",
456 ".class" => "application/octet-stream",
457 ".ps" => "application/postscript",
458 ".torrent" => "application/x-bittorrent",
459 ".dvi" => "application/x-dvi",
460 ".gz" => "application/x-gzip",
461 ".pac" => "application/x-ns-proxy-autoconfig",
462 ".swf" => "application/x-shockwave-flash",
463 ".tar.gz" => "application/x-tgz",
464 ".tgz" => "application/x-tgz",
465 ".tar" => "application/x-tar",
466 ".zip" => "application/zip",
467 ".mp3" => "audio/mpeg",
468 ".m3u" => "audio/x-mpegurl",
469 ".wma" => "audio/x-ms-wma",
470 ".wax" => "audio/x-ms-wax",
471 ".ogg" => "application/ogg",
472 ".wav" => "audio/x-wav",
473 ".gif" => "image/gif",
474 ".jpg" => "image/jpeg",
475 ".jpeg" => "image/jpeg",
476 ".png" => "image/png",
477 ".xbm" => "image/x-xbitmap",
478 ".xpm" => "image/x-xpixmap",
479 ".xwd" => "image/x-xwindowdump",
480 ".css" => "text/css",
481 ".html" => "text/html",
482 ".htm" => "text/html",
483 ".js" => "text/javascript",
484 ".asc" => "text/plain",
485 ".c" => "text/plain",
486 ".cpp" => "text/plain",
487 ".log" => "text/plain",
488 ".conf" => "text/plain",
489 ".text" => "text/plain",
490 ".txt" => "text/plain",
491 ".dtd" => "text/xml",
492 ".xml" => "text/xml",
493 ".mpeg" => "video/mpeg",
494 ".mpg" => "video/mpeg",
495 ".mov" => "video/quicktime",
496 ".qt" => "video/quicktime",
497 ".avi" => "video/x-msvideo",
498 ".asf" => "video/x-ms-asf",
499 ".asx" => "video/x-ms-asf",
500 ".wmv" => "video/x-ms-wmv",
501 ".bz2" => "application/x-bzip",
502 ".tbz" => "application/x-bzip-compressed-tar",
503 ".tar.bz2" => "application/x-bzip-compressed-tar",
504 "" => "text/plain"
507 my \$app = builder {
508 # to be able to override \$SIG{__WARN__} to log build time warnings
509 use CGI::Carp; # it sets \$SIG{__WARN__} itself
511 my \$logdir = "$fqgitdir/gitweb/$httpd_only";
512 open my \$access_log_fh, '>>', "\$logdir/access.log"
513 or die "Couldn't open access log '\$logdir/access.log': \$!";
514 open my \$error_log_fh, '>>', "\$logdir/error.log"
515 or die "Couldn't open error log '\$logdir/error.log': \$!";
517 \$access_log_fh->autoflush(1);
518 \$error_log_fh->autoflush(1);
520 # redirect build time warnings to error.log
521 \$SIG{'__WARN__'} = sub {
522 my \$msg = shift;
523 # timestamp warning like in CGI::Carp::warn
524 my \$stamp = CGI::Carp::stamp();
525 \$msg =~ s/^/\$stamp/gm;
526 print \$error_log_fh \$msg;
529 # write errors to error.log, access to access.log
530 enable 'AccessLog',
531 format => "combined",
532 logger => sub { print \$access_log_fh @_; };
533 enable sub {
534 my \$app = shift;
535 sub {
536 my \$env = shift;
537 \$env->{'psgi.errors'} = \$error_log_fh;
538 \$app->(\$env);
541 # gitweb currently doesn't work with $SIG{CHLD} set to 'IGNORE',
542 # because it uses 'close $fd or die...' on piped filehandle $fh
543 # (which causes the parent process to wait for child to finish).
544 enable_if { \$SIG{'CHLD'} eq 'IGNORE' } sub {
545 my \$app = shift;
546 sub {
547 my \$env = shift;
548 local \$SIG{'CHLD'} = 'DEFAULT';
549 local \$SIG{'CLD'} = 'DEFAULT';
550 \$app->(\$env);
553 # serve static files, i.e. stylesheet, images, script
554 enable 'Static',
555 path => sub { m!\.(js|css|png)\$! && s!^/gitweb/!! },
556 root => "$root/",
557 encoding => 'utf-8'; # encoding for 'text/plain' files
558 # convert CGI application to PSGI app
559 Plack::App::WrapCGI->new(script => "$root/gitweb.cgi")->to_app;
562 # make it runnable as standalone app,
563 # like it would be run via 'plackup' utility
564 if (caller) {
565 return \$app;
566 } else {
567 require Plack::Runner;
569 my \$runner = Plack::Runner->new();
570 \$runner->parse_options(qw(--env deployment --port $port),
571 "$local" ? qw(--host 127.0.0.1) : ());
572 \$runner->run(\$app);
574 __END__
577 chmod a+x "$fqgitdir/gitweb/gitweb.psgi"
578 # configuration is embedded in server script file, gitweb.psgi
579 rm -f "$conf"
582 gitweb_conf() {
583 cat > "$fqgitdir/gitweb/gitweb_config.perl" <<EOF
584 #!/usr/bin/perl
585 our \$projectroot = "$(dirname "$fqgitdir")";
586 our \$git_temp = "$fqgitdir/gitweb/tmp";
587 our \$projects_list = \$projectroot;
589 \$feature{'remote_heads'}{'default'} = [1];
593 configure_httpd() {
594 case "$httpd" in
595 *lighttpd*)
596 lighttpd_conf
598 *apache2*|*httpd*)
599 apache2_conf
601 webrick)
602 webrick_conf
604 *mongoose*)
605 mongoose_conf
607 *plackup*)
608 plackup_conf
611 echo "Unknown httpd specified: $httpd"
612 exit 1
614 esac
617 case "$action" in
618 stop)
619 stop_httpd
620 exit 0
622 start)
623 start_httpd
624 exit 0
626 restart)
627 stop_httpd
628 start_httpd
629 exit 0
631 esac
633 gitweb_conf
635 resolve_full_httpd
636 mkdir -p "$fqgitdir/gitweb/$httpd_only"
637 conf="$fqgitdir/gitweb/$httpd_only.conf"
639 configure_httpd
641 start_httpd
642 url=http://127.0.0.1:$port
644 if test -n "$browser"; then
645 httpd_is_ready && git web--browse -b "$browser" $url || echo $url
646 else
647 httpd_is_ready && git web--browse -c "instaweb.browser" $url || echo $url