Code

git-instaweb: Wait for server to start before running web browser
[git.git] / git-instaweb.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2006 Eric Wong
4 #
6 PERL='@@PERL@@'
7 OPTIONS_KEEPDASHDASH=
8 OPTIONS_SPEC="\
9 git instaweb [options] (--start | --stop | --restart)
10 --
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
20 "
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)"
31 conf="$GIT_DIR/gitweb/httpd.conf"
33 # Defaults:
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 () {
45         case "$httpd" in
46         *apache2*|*lighttpd*)
47                 # ensure that the apache2/lighttpd command ends with "-f"
48                 if ! echo "$httpd" | sane_grep -- '-f *$' >/dev/null 2>&1
49                 then
50                         httpd="$httpd -f"
51                 fi
52                 ;;
53         esac
55         httpd_only="$(echo $httpd | cut -f1 -d' ')"
56         if case "$httpd_only" in /*) : ;; *) which $httpd_only >/dev/null 2>&1;; esac
57         then
58                 full_httpd=$httpd
59         else
60                 # many httpds are installed in /usr/sbin or /usr/local/sbin
61                 # these days and those are not in most users $PATHs
62                 # in addition, we may have generated a server script
63                 # in $fqgitdir/gitweb.
64                 for i in /usr/local/sbin /usr/sbin "$root" "$fqgitdir/gitweb"
65                 do
66                         if test -x "$i/$httpd_only"
67                         then
68                                 full_httpd=$i/$httpd
69                                 return
70                         fi
71                 done
73                 echo >&2 "$httpd_only not found. Install $httpd_only or use" \
74                      "--httpd to specify another httpd daemon."
75                 exit 1
76         fi
77 }
79 start_httpd () {
80         if test -f "$fqgitdir/pid"; then
81                 say "Instance already running. Restarting..."
82                 stop_httpd
83         fi
85         # here $httpd should have a meaningful value
86         resolve_full_httpd
88         # don't quote $full_httpd, there can be arguments to it (-f)
89         case "$httpd" in
90         *mongoose*)
91                 #The mongoose server doesn't have a daemon mode so we'll have to fork it
92                 $full_httpd "$fqgitdir/gitweb/httpd.conf" &
93                 #Save the pid before doing anything else (we'll print it later)
94                 pid=$!
96                 if test $? != 0; then
97                         echo "Could not execute http daemon $httpd."
98                         exit 1
99                 fi
101                 cat > "$fqgitdir/pid" <<EOF
102 $pid
103 EOF
104                 ;;
105         *)
106                 $full_httpd "$fqgitdir/gitweb/httpd.conf"
107                 if test $? != 0; then
108                         echo "Could not execute http daemon $httpd."
109                         exit 1
110                 fi
111                 ;;
112         esac
115 stop_httpd () {
116         test -f "$fqgitdir/pid" && kill $(cat "$fqgitdir/pid")
117         rm -f "$fqgitdir/pid"
120 httpd_is_ready () {
121         "$PERL" -MIO::Socket::INET -e "
122 local \$| = 1; # turn on autoflush
123 exit if (IO::Socket::INET->new('127.0.0.1:$port'));
124 print 'Waiting for \'$httpd\' to start ..';
125 do {
126         print '.';
127         sleep(1);
128 } until (IO::Socket::INET->new('127.0.0.1:$port'));
129 print qq! (done)\n!;
133 while test $# != 0
134 do
135         case "$1" in
136         --stop|stop)
137                 stop_httpd
138                 exit 0
139                 ;;
140         --start|start)
141                 start_httpd
142                 exit 0
143                 ;;
144         --restart|restart)
145                 stop_httpd
146                 start_httpd
147                 exit 0
148                 ;;
149         -l|--local)
150                 local=true
151                 ;;
152         -d|--httpd)
153                 shift
154                 httpd="$1"
155                 ;;
156         -b|--browser)
157                 shift
158                 browser="$1"
159                 ;;
160         -p|--port)
161                 shift
162                 port="$1"
163                 ;;
164         -m|--module-path)
165                 shift
166                 module_path="$1"
167                 ;;
168         --)
169                 ;;
170         *)
171                 usage
172                 ;;
173         esac
174         shift
175 done
177 mkdir -p "$GIT_DIR/gitweb/tmp"
178 GIT_EXEC_PATH="$(git --exec-path)"
179 GIT_DIR="$fqgitdir"
180 GITWEB_CONFIG="$fqgitdir/gitweb/gitweb_config.perl"
181 export GIT_EXEC_PATH GIT_DIR GITWEB_CONFIG
183 webrick_conf () {
184         # generate a standalone server script in $fqgitdir/gitweb.
185         cat >"$fqgitdir/gitweb/$httpd.rb" <<EOF
186 require 'webrick'
187 require 'yaml'
188 options = YAML::load_file(ARGV[0])
189 options[:StartCallback] = proc do
190   File.open(options[:PidFile],"w") do |f|
191     f.puts Process.pid
192   end
193 end
194 options[:ServerType] = WEBrick::Daemon
195 server = WEBrick::HTTPServer.new(options)
196 ['INT', 'TERM'].each do |signal|
197   trap(signal) {server.shutdown}
198 end
199 server.start
200 EOF
201         # generate a shell script to invoke the above ruby script,
202         # which assumes _ruby_ is in the user's $PATH. that's _one_
203         # portable way to run ruby, which could be installed anywhere,
204         # really.
205         cat >"$fqgitdir/gitweb/$httpd" <<EOF
206 #!/bin/sh
207 exec ruby "$fqgitdir/gitweb/$httpd.rb" \$*
208 EOF
209         chmod +x "$fqgitdir/gitweb/$httpd"
211         cat >"$conf" <<EOF
212 :Port: $port
213 :DocumentRoot: "$root"
214 :DirectoryIndex: ["gitweb.cgi"]
215 :PidFile: "$fqgitdir/pid"
216 EOF
217         test "$local" = true && echo ':BindAddress: "127.0.0.1"' >> "$conf"
220 lighttpd_conf () {
221         cat > "$conf" <<EOF
222 server.document-root = "$root"
223 server.port = $port
224 server.modules = ( "mod_setenv", "mod_cgi" )
225 server.indexfiles = ( "gitweb.cgi" )
226 server.pid-file = "$fqgitdir/pid"
227 server.errorlog = "$fqgitdir/gitweb/$httpd_only/error.log"
229 # to enable, add "mod_access", "mod_accesslog" to server.modules
230 # variable above and uncomment this
231 #accesslog.filename = "$fqgitdir/gitweb/$httpd_only/access.log"
233 setenv.add-environment = ( "PATH" => env.PATH, "GITWEB_CONFIG" => env.GITWEB_CONFIG )
235 cgi.assign = ( ".cgi" => "" )
237 # mimetype mapping
238 mimetype.assign             = (
239   ".pdf"          =>      "application/pdf",
240   ".sig"          =>      "application/pgp-signature",
241   ".spl"          =>      "application/futuresplash",
242   ".class"        =>      "application/octet-stream",
243   ".ps"           =>      "application/postscript",
244   ".torrent"      =>      "application/x-bittorrent",
245   ".dvi"          =>      "application/x-dvi",
246   ".gz"           =>      "application/x-gzip",
247   ".pac"          =>      "application/x-ns-proxy-autoconfig",
248   ".swf"          =>      "application/x-shockwave-flash",
249   ".tar.gz"       =>      "application/x-tgz",
250   ".tgz"          =>      "application/x-tgz",
251   ".tar"          =>      "application/x-tar",
252   ".zip"          =>      "application/zip",
253   ".mp3"          =>      "audio/mpeg",
254   ".m3u"          =>      "audio/x-mpegurl",
255   ".wma"          =>      "audio/x-ms-wma",
256   ".wax"          =>      "audio/x-ms-wax",
257   ".ogg"          =>      "application/ogg",
258   ".wav"          =>      "audio/x-wav",
259   ".gif"          =>      "image/gif",
260   ".jpg"          =>      "image/jpeg",
261   ".jpeg"         =>      "image/jpeg",
262   ".png"          =>      "image/png",
263   ".xbm"          =>      "image/x-xbitmap",
264   ".xpm"          =>      "image/x-xpixmap",
265   ".xwd"          =>      "image/x-xwindowdump",
266   ".css"          =>      "text/css",
267   ".html"         =>      "text/html",
268   ".htm"          =>      "text/html",
269   ".js"           =>      "text/javascript",
270   ".asc"          =>      "text/plain",
271   ".c"            =>      "text/plain",
272   ".cpp"          =>      "text/plain",
273   ".log"          =>      "text/plain",
274   ".conf"         =>      "text/plain",
275   ".text"         =>      "text/plain",
276   ".txt"          =>      "text/plain",
277   ".dtd"          =>      "text/xml",
278   ".xml"          =>      "text/xml",
279   ".mpeg"         =>      "video/mpeg",
280   ".mpg"          =>      "video/mpeg",
281   ".mov"          =>      "video/quicktime",
282   ".qt"           =>      "video/quicktime",
283   ".avi"          =>      "video/x-msvideo",
284   ".asf"          =>      "video/x-ms-asf",
285   ".asx"          =>      "video/x-ms-asf",
286   ".wmv"          =>      "video/x-ms-wmv",
287   ".bz2"          =>      "application/x-bzip",
288   ".tbz"          =>      "application/x-bzip-compressed-tar",
289   ".tar.bz2"      =>      "application/x-bzip-compressed-tar",
290   ""              =>      "text/plain"
291  )
292 EOF
293         test x"$local" = xtrue && echo 'server.bind = "127.0.0.1"' >> "$conf"
296 apache2_conf () {
297         test -z "$module_path" && module_path=/usr/lib/apache2/modules
298         bind=
299         test x"$local" = xtrue && bind='127.0.0.1:'
300         echo 'text/css css' > "$fqgitdir/mime.types"
301         cat > "$conf" <<EOF
302 ServerName "git-instaweb"
303 ServerRoot "$root"
304 DocumentRoot "$root"
305 ErrorLog "$fqgitdir/gitweb/$httpd_only/error.log"
306 CustomLog "$fqgitdir/gitweb/$httpd_only/access.log" combined
307 PidFile "$fqgitdir/pid"
308 Listen $bind$port
309 EOF
311         for mod in mime dir; do
312                 if test -e $module_path/mod_${mod}.so; then
313                         echo "LoadModule ${mod}_module " \
314                              "$module_path/mod_${mod}.so" >> "$conf"
315                 fi
316         done
317         cat >> "$conf" <<EOF
318 TypesConfig "$fqgitdir/mime.types"
319 DirectoryIndex gitweb.cgi
320 EOF
322         # check to see if Dennis Stosberg's mod_perl compatibility patch
323         # (<20060621130708.Gcbc6e5c@leonov.stosberg.net>) has been applied
324         if test -f "$module_path/mod_perl.so" &&
325            sane_grep 'MOD_PERL' "$root/gitweb.cgi" >/dev/null
326         then
327                 # favor mod_perl if available
328                 cat >> "$conf" <<EOF
329 LoadModule perl_module $module_path/mod_perl.so
330 PerlPassEnv GIT_DIR
331 PerlPassEnv GIT_EXEC_DIR
332 PerlPassEnv GITWEB_CONFIG
333 <Location /gitweb.cgi>
334         SetHandler perl-script
335         PerlResponseHandler ModPerl::Registry
336         PerlOptions +ParseHeaders
337         Options +ExecCGI
338 </Location>
339 EOF
340         else
341                 # plain-old CGI
342                 resolve_full_httpd
343                 list_mods=$(echo "$full_httpd" | sed 's/-f$/-l/')
344                 $list_mods | sane_grep 'mod_cgi\.c' >/dev/null 2>&1 || \
345                 if test -f "$module_path/mod_cgi.so"
346                 then
347                         echo "LoadModule cgi_module $module_path/mod_cgi.so" >> "$conf"
348                 else
349                         $list_mods | grep 'mod_cgid\.c' >/dev/null 2>&1 || \
350                         if test -f "$module_path/mod_cgid.so"
351                         then
352                                 echo "LoadModule cgid_module $module_path/mod_cgid.so" \
353                                         >> "$conf"
354                         else
355                                 echo "You have no CGI support!"
356                                 exit 2
357                         fi
358                         echo "ScriptSock logs/gitweb.sock" >> "$conf"
359                 fi
360                 cat >> "$conf" <<EOF
361 AddHandler cgi-script .cgi
362 <Location /gitweb.cgi>
363         Options +ExecCGI
364 </Location>
365 EOF
366         fi
369 mongoose_conf() {
370         cat > "$conf" <<EOF
371 # Mongoose web server configuration file.
372 # Lines starting with '#' and empty lines are ignored.
373 # For detailed description of every option, visit
374 # http://code.google.com/p/mongoose/wiki/MongooseManual
376 root            $root
377 ports           $port
378 index_files     gitweb.cgi
379 #ssl_cert       $fqgitdir/gitweb/ssl_cert.pem
380 error_log       $fqgitdir/gitweb/$httpd_only/error.log
381 access_log      $fqgitdir/gitweb/$httpd_only/access.log
383 #cgi setup
384 cgi_env         PATH=$PATH,GIT_DIR=$GIT_DIR,GIT_EXEC_PATH=$GIT_EXEC_PATH,GITWEB_CONFIG=$GITWEB_CONFIG
385 cgi_interp      $PERL
386 cgi_ext         cgi,pl
388 # mimetype mapping
389 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
390 EOF
393 gitweb_conf() {
394         cat > "$fqgitdir/gitweb/gitweb_config.perl" <<EOF
395 #!/usr/bin/perl
396 our \$projectroot = "$(dirname "$fqgitdir")";
397 our \$git_temp = "$fqgitdir/gitweb/tmp";
398 our \$projects_list = \$projectroot;
399 EOF
402 gitweb_conf
404 resolve_full_httpd
405 mkdir -p "$fqgitdir/gitweb/$httpd_only"
407 case "$httpd" in
408 *lighttpd*)
409         lighttpd_conf
410         ;;
411 *apache2*)
412         apache2_conf
413         ;;
414 webrick)
415         webrick_conf
416         ;;
417 *mongoose*)
418         mongoose_conf
419         ;;
420 *)
421         echo "Unknown httpd specified: $httpd"
422         exit 1
423         ;;
424 esac
426 start_httpd
427 url=http://127.0.0.1:$port
429 if test -n "$browser"; then
430         httpd_is_ready && git web--browse -b "$browser" $url || echo $url
431 else
432         httpd_is_ready && git web--browse -c "instaweb.browser" $url || echo $url
433 fi