Code

git-instaweb: Remove pidfile after stopping web server
[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 while test $# != 0
121 do
122         case "$1" in
123         --stop|stop)
124                 stop_httpd
125                 exit 0
126                 ;;
127         --start|start)
128                 start_httpd
129                 exit 0
130                 ;;
131         --restart|restart)
132                 stop_httpd
133                 start_httpd
134                 exit 0
135                 ;;
136         -l|--local)
137                 local=true
138                 ;;
139         -d|--httpd)
140                 shift
141                 httpd="$1"
142                 ;;
143         -b|--browser)
144                 shift
145                 browser="$1"
146                 ;;
147         -p|--port)
148                 shift
149                 port="$1"
150                 ;;
151         -m|--module-path)
152                 shift
153                 module_path="$1"
154                 ;;
155         --)
156                 ;;
157         *)
158                 usage
159                 ;;
160         esac
161         shift
162 done
164 mkdir -p "$GIT_DIR/gitweb/tmp"
165 GIT_EXEC_PATH="$(git --exec-path)"
166 GIT_DIR="$fqgitdir"
167 GITWEB_CONFIG="$fqgitdir/gitweb/gitweb_config.perl"
168 export GIT_EXEC_PATH GIT_DIR GITWEB_CONFIG
170 webrick_conf () {
171         # generate a standalone server script in $fqgitdir/gitweb.
172         cat >"$fqgitdir/gitweb/$httpd.rb" <<EOF
173 require 'webrick'
174 require 'yaml'
175 options = YAML::load_file(ARGV[0])
176 options[:StartCallback] = proc do
177   File.open(options[:PidFile],"w") do |f|
178     f.puts Process.pid
179   end
180 end
181 options[:ServerType] = WEBrick::Daemon
182 server = WEBrick::HTTPServer.new(options)
183 ['INT', 'TERM'].each do |signal|
184   trap(signal) {server.shutdown}
185 end
186 server.start
187 EOF
188         # generate a shell script to invoke the above ruby script,
189         # which assumes _ruby_ is in the user's $PATH. that's _one_
190         # portable way to run ruby, which could be installed anywhere,
191         # really.
192         cat >"$fqgitdir/gitweb/$httpd" <<EOF
193 #!/bin/sh
194 exec ruby "$fqgitdir/gitweb/$httpd.rb" \$*
195 EOF
196         chmod +x "$fqgitdir/gitweb/$httpd"
198         cat >"$conf" <<EOF
199 :Port: $port
200 :DocumentRoot: "$root"
201 :DirectoryIndex: ["gitweb.cgi"]
202 :PidFile: "$fqgitdir/pid"
203 EOF
204         test "$local" = true && echo ':BindAddress: "127.0.0.1"' >> "$conf"
207 lighttpd_conf () {
208         cat > "$conf" <<EOF
209 server.document-root = "$root"
210 server.port = $port
211 server.modules = ( "mod_setenv", "mod_cgi" )
212 server.indexfiles = ( "gitweb.cgi" )
213 server.pid-file = "$fqgitdir/pid"
214 server.errorlog = "$fqgitdir/gitweb/$httpd_only/error.log"
216 # to enable, add "mod_access", "mod_accesslog" to server.modules
217 # variable above and uncomment this
218 #accesslog.filename = "$fqgitdir/gitweb/$httpd_only/access.log"
220 setenv.add-environment = ( "PATH" => env.PATH, "GITWEB_CONFIG" => env.GITWEB_CONFIG )
222 cgi.assign = ( ".cgi" => "" )
224 # mimetype mapping
225 mimetype.assign             = (
226   ".pdf"          =>      "application/pdf",
227   ".sig"          =>      "application/pgp-signature",
228   ".spl"          =>      "application/futuresplash",
229   ".class"        =>      "application/octet-stream",
230   ".ps"           =>      "application/postscript",
231   ".torrent"      =>      "application/x-bittorrent",
232   ".dvi"          =>      "application/x-dvi",
233   ".gz"           =>      "application/x-gzip",
234   ".pac"          =>      "application/x-ns-proxy-autoconfig",
235   ".swf"          =>      "application/x-shockwave-flash",
236   ".tar.gz"       =>      "application/x-tgz",
237   ".tgz"          =>      "application/x-tgz",
238   ".tar"          =>      "application/x-tar",
239   ".zip"          =>      "application/zip",
240   ".mp3"          =>      "audio/mpeg",
241   ".m3u"          =>      "audio/x-mpegurl",
242   ".wma"          =>      "audio/x-ms-wma",
243   ".wax"          =>      "audio/x-ms-wax",
244   ".ogg"          =>      "application/ogg",
245   ".wav"          =>      "audio/x-wav",
246   ".gif"          =>      "image/gif",
247   ".jpg"          =>      "image/jpeg",
248   ".jpeg"         =>      "image/jpeg",
249   ".png"          =>      "image/png",
250   ".xbm"          =>      "image/x-xbitmap",
251   ".xpm"          =>      "image/x-xpixmap",
252   ".xwd"          =>      "image/x-xwindowdump",
253   ".css"          =>      "text/css",
254   ".html"         =>      "text/html",
255   ".htm"          =>      "text/html",
256   ".js"           =>      "text/javascript",
257   ".asc"          =>      "text/plain",
258   ".c"            =>      "text/plain",
259   ".cpp"          =>      "text/plain",
260   ".log"          =>      "text/plain",
261   ".conf"         =>      "text/plain",
262   ".text"         =>      "text/plain",
263   ".txt"          =>      "text/plain",
264   ".dtd"          =>      "text/xml",
265   ".xml"          =>      "text/xml",
266   ".mpeg"         =>      "video/mpeg",
267   ".mpg"          =>      "video/mpeg",
268   ".mov"          =>      "video/quicktime",
269   ".qt"           =>      "video/quicktime",
270   ".avi"          =>      "video/x-msvideo",
271   ".asf"          =>      "video/x-ms-asf",
272   ".asx"          =>      "video/x-ms-asf",
273   ".wmv"          =>      "video/x-ms-wmv",
274   ".bz2"          =>      "application/x-bzip",
275   ".tbz"          =>      "application/x-bzip-compressed-tar",
276   ".tar.bz2"      =>      "application/x-bzip-compressed-tar",
277   ""              =>      "text/plain"
278  )
279 EOF
280         test x"$local" = xtrue && echo 'server.bind = "127.0.0.1"' >> "$conf"
283 apache2_conf () {
284         test -z "$module_path" && module_path=/usr/lib/apache2/modules
285         bind=
286         test x"$local" = xtrue && bind='127.0.0.1:'
287         echo 'text/css css' > "$fqgitdir/mime.types"
288         cat > "$conf" <<EOF
289 ServerName "git-instaweb"
290 ServerRoot "$root"
291 DocumentRoot "$root"
292 ErrorLog "$fqgitdir/gitweb/$httpd_only/error.log"
293 CustomLog "$fqgitdir/gitweb/$httpd_only/access.log" combined
294 PidFile "$fqgitdir/pid"
295 Listen $bind$port
296 EOF
298         for mod in mime dir; do
299                 if test -e $module_path/mod_${mod}.so; then
300                         echo "LoadModule ${mod}_module " \
301                              "$module_path/mod_${mod}.so" >> "$conf"
302                 fi
303         done
304         cat >> "$conf" <<EOF
305 TypesConfig "$fqgitdir/mime.types"
306 DirectoryIndex gitweb.cgi
307 EOF
309         # check to see if Dennis Stosberg's mod_perl compatibility patch
310         # (<20060621130708.Gcbc6e5c@leonov.stosberg.net>) has been applied
311         if test -f "$module_path/mod_perl.so" &&
312            sane_grep 'MOD_PERL' "$root/gitweb.cgi" >/dev/null
313         then
314                 # favor mod_perl if available
315                 cat >> "$conf" <<EOF
316 LoadModule perl_module $module_path/mod_perl.so
317 PerlPassEnv GIT_DIR
318 PerlPassEnv GIT_EXEC_DIR
319 PerlPassEnv GITWEB_CONFIG
320 <Location /gitweb.cgi>
321         SetHandler perl-script
322         PerlResponseHandler ModPerl::Registry
323         PerlOptions +ParseHeaders
324         Options +ExecCGI
325 </Location>
326 EOF
327         else
328                 # plain-old CGI
329                 resolve_full_httpd
330                 list_mods=$(echo "$full_httpd" | sed 's/-f$/-l/')
331                 $list_mods | sane_grep 'mod_cgi\.c' >/dev/null 2>&1 || \
332                 if test -f "$module_path/mod_cgi.so"
333                 then
334                         echo "LoadModule cgi_module $module_path/mod_cgi.so" >> "$conf"
335                 else
336                         $list_mods | grep 'mod_cgid\.c' >/dev/null 2>&1 || \
337                         if test -f "$module_path/mod_cgid.so"
338                         then
339                                 echo "LoadModule cgid_module $module_path/mod_cgid.so" \
340                                         >> "$conf"
341                         else
342                                 echo "You have no CGI support!"
343                                 exit 2
344                         fi
345                         echo "ScriptSock logs/gitweb.sock" >> "$conf"
346                 fi
347                 cat >> "$conf" <<EOF
348 AddHandler cgi-script .cgi
349 <Location /gitweb.cgi>
350         Options +ExecCGI
351 </Location>
352 EOF
353         fi
356 mongoose_conf() {
357         cat > "$conf" <<EOF
358 # Mongoose web server configuration file.
359 # Lines starting with '#' and empty lines are ignored.
360 # For detailed description of every option, visit
361 # http://code.google.com/p/mongoose/wiki/MongooseManual
363 root            $root
364 ports           $port
365 index_files     gitweb.cgi
366 #ssl_cert       $fqgitdir/gitweb/ssl_cert.pem
367 error_log       $fqgitdir/gitweb/$httpd_only/error.log
368 access_log      $fqgitdir/gitweb/$httpd_only/access.log
370 #cgi setup
371 cgi_env         PATH=$PATH,GIT_DIR=$GIT_DIR,GIT_EXEC_PATH=$GIT_EXEC_PATH,GITWEB_CONFIG=$GITWEB_CONFIG
372 cgi_interp      $PERL
373 cgi_ext         cgi,pl
375 # mimetype mapping
376 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
377 EOF
380 gitweb_conf() {
381         cat > "$fqgitdir/gitweb/gitweb_config.perl" <<EOF
382 #!/usr/bin/perl
383 our \$projectroot = "$(dirname "$fqgitdir")";
384 our \$git_temp = "$fqgitdir/gitweb/tmp";
385 our \$projects_list = \$projectroot;
386 EOF
389 gitweb_conf
391 resolve_full_httpd
392 mkdir -p "$fqgitdir/gitweb/$httpd_only"
394 case "$httpd" in
395 *lighttpd*)
396         lighttpd_conf
397         ;;
398 *apache2*)
399         apache2_conf
400         ;;
401 webrick)
402         webrick_conf
403         ;;
404 *mongoose*)
405         mongoose_conf
406         ;;
407 *)
408         echo "Unknown httpd specified: $httpd"
409         exit 1
410         ;;
411 esac
413 start_httpd
414 url=http://127.0.0.1:$port
416 if test -n "$browser"; then
417         git web--browse -b "$browser" $url || echo $url
418 else
419         git web--browse -c "instaweb.browser" $url || echo $url
420 fi