builtin-mailinfo.c: check error status from rewind and ftruncate
[git/spearce.git] / git-instaweb.sh
blobd96eddbe56783d61304941a134361797c82480e7
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 port=$(git config --get instaweb.port)
28 module_path="$(git config --get instaweb.modulepath)"
30 conf="$GIT_DIR/gitweb/httpd.conf"
32 # Defaults:
34 # if installed, it doesn't need further configuration (module_path)
35 test -z "$httpd" && httpd='lighttpd -f'
37 # any untaken local port will do...
38 test -z "$port" && port=1234
40 resolve_full_httpd () {
41 case "$httpd" in
42 *apache2*|*lighttpd*)
43 # ensure that the apache2/lighttpd command ends with "-f"
44 if ! echo "$httpd" | grep -- '-f *$' >/dev/null 2>&1
45 then
46 httpd="$httpd -f"
49 esac
51 httpd_only="$(echo $httpd | cut -f1 -d' ')"
52 if case "$httpd_only" in /*) : ;; *) which $httpd_only >/dev/null 2>&1;; esac
53 then
54 full_httpd=$httpd
55 else
56 # many httpds are installed in /usr/sbin or /usr/local/sbin
57 # these days and those are not in most users $PATHs
58 # in addition, we may have generated a server script
59 # in $fqgitdir/gitweb.
60 for i in /usr/local/sbin /usr/sbin "$fqgitdir/gitweb"
62 if test -x "$i/$httpd_only"
63 then
64 full_httpd=$i/$httpd
65 return
67 done
69 echo >&2 "$httpd_only not found. Install $httpd_only or use" \
70 "--httpd to specify another httpd daemon."
71 exit 1
75 start_httpd () {
76 # here $httpd should have a meaningful value
77 resolve_full_httpd
79 # don't quote $full_httpd, there can be arguments to it (-f)
80 case "$httpd" in
81 *mongoose*)
82 #The mongoose server doesn't have a daemon mode so we'll have to fork it
83 $full_httpd "$fqgitdir/gitweb/httpd.conf" &
84 #Save the pid before doing anything else (we'll print it later)
85 pid=$!
87 if test $? != 0; then
88 echo "Could not execute http daemon $httpd."
89 exit 1
92 cat > "$fqgitdir/pid" <<EOF
93 $pid
94 EOF
97 $full_httpd "$fqgitdir/gitweb/httpd.conf"
98 if test $? != 0; then
99 echo "Could not execute http daemon $httpd."
100 exit 1
103 esac
106 stop_httpd () {
107 test -f "$fqgitdir/pid" && kill $(cat "$fqgitdir/pid")
110 while test $# != 0
112 case "$1" in
113 --stop|stop)
114 stop_httpd
115 exit 0
117 --start|start)
118 start_httpd
119 exit 0
121 --restart|restart)
122 stop_httpd
123 start_httpd
124 exit 0
126 -l|--local)
127 local=true
129 -d|--httpd)
130 shift
131 httpd="$1"
133 -b|--browser)
134 shift
135 browser="$1"
137 -p|--port)
138 shift
139 port="$1"
141 -m|--module-path)
142 shift
143 module_path="$1"
148 usage
150 esac
151 shift
152 done
154 mkdir -p "$GIT_DIR/gitweb/tmp"
155 GIT_EXEC_PATH="$(git --exec-path)"
156 GIT_DIR="$fqgitdir"
157 export GIT_EXEC_PATH GIT_DIR
160 webrick_conf () {
161 # generate a standalone server script in $fqgitdir/gitweb.
162 cat >"$fqgitdir/gitweb/$httpd.rb" <<EOF
163 require 'webrick'
164 require 'yaml'
165 options = YAML::load_file(ARGV[0])
166 options[:StartCallback] = proc do
167 File.open(options[:PidFile],"w") do |f|
168 f.puts Process.pid
171 options[:ServerType] = WEBrick::Daemon
172 server = WEBrick::HTTPServer.new(options)
173 ['INT', 'TERM'].each do |signal|
174 trap(signal) {server.shutdown}
176 server.start
178 # generate a shell script to invoke the above ruby script,
179 # which assumes _ruby_ is in the user's $PATH. that's _one_
180 # portable way to run ruby, which could be installed anywhere,
181 # really.
182 cat >"$fqgitdir/gitweb/$httpd" <<EOF
183 #!/bin/sh
184 exec ruby "$fqgitdir/gitweb/$httpd.rb" \$*
186 chmod +x "$fqgitdir/gitweb/$httpd"
188 cat >"$conf" <<EOF
189 :Port: $port
190 :DocumentRoot: "$fqgitdir/gitweb"
191 :DirectoryIndex: ["gitweb.cgi"]
192 :PidFile: "$fqgitdir/pid"
194 test "$local" = true && echo ':BindAddress: "127.0.0.1"' >> "$conf"
197 lighttpd_conf () {
198 cat > "$conf" <<EOF
199 server.document-root = "$fqgitdir/gitweb"
200 server.port = $port
201 server.modules = ( "mod_setenv", "mod_cgi" )
202 server.indexfiles = ( "gitweb.cgi" )
203 server.pid-file = "$fqgitdir/pid"
204 server.errorlog = "$fqgitdir/gitweb/error.log"
206 # to enable, add "mod_access", "mod_accesslog" to server.modules
207 # variable above and uncomment this
208 #accesslog.filename = "$fqgitdir/gitweb/access.log"
210 setenv.add-environment = ( "PATH" => "/usr/local/bin:/usr/bin:/bin" )
212 cgi.assign = ( ".cgi" => "" )
214 # mimetype mapping
215 mimetype.assign = (
216 ".pdf" => "application/pdf",
217 ".sig" => "application/pgp-signature",
218 ".spl" => "application/futuresplash",
219 ".class" => "application/octet-stream",
220 ".ps" => "application/postscript",
221 ".torrent" => "application/x-bittorrent",
222 ".dvi" => "application/x-dvi",
223 ".gz" => "application/x-gzip",
224 ".pac" => "application/x-ns-proxy-autoconfig",
225 ".swf" => "application/x-shockwave-flash",
226 ".tar.gz" => "application/x-tgz",
227 ".tgz" => "application/x-tgz",
228 ".tar" => "application/x-tar",
229 ".zip" => "application/zip",
230 ".mp3" => "audio/mpeg",
231 ".m3u" => "audio/x-mpegurl",
232 ".wma" => "audio/x-ms-wma",
233 ".wax" => "audio/x-ms-wax",
234 ".ogg" => "application/ogg",
235 ".wav" => "audio/x-wav",
236 ".gif" => "image/gif",
237 ".jpg" => "image/jpeg",
238 ".jpeg" => "image/jpeg",
239 ".png" => "image/png",
240 ".xbm" => "image/x-xbitmap",
241 ".xpm" => "image/x-xpixmap",
242 ".xwd" => "image/x-xwindowdump",
243 ".css" => "text/css",
244 ".html" => "text/html",
245 ".htm" => "text/html",
246 ".js" => "text/javascript",
247 ".asc" => "text/plain",
248 ".c" => "text/plain",
249 ".cpp" => "text/plain",
250 ".log" => "text/plain",
251 ".conf" => "text/plain",
252 ".text" => "text/plain",
253 ".txt" => "text/plain",
254 ".dtd" => "text/xml",
255 ".xml" => "text/xml",
256 ".mpeg" => "video/mpeg",
257 ".mpg" => "video/mpeg",
258 ".mov" => "video/quicktime",
259 ".qt" => "video/quicktime",
260 ".avi" => "video/x-msvideo",
261 ".asf" => "video/x-ms-asf",
262 ".asx" => "video/x-ms-asf",
263 ".wmv" => "video/x-ms-wmv",
264 ".bz2" => "application/x-bzip",
265 ".tbz" => "application/x-bzip-compressed-tar",
266 ".tar.bz2" => "application/x-bzip-compressed-tar",
267 "" => "text/plain"
270 test x"$local" = xtrue && echo 'server.bind = "127.0.0.1"' >> "$conf"
273 apache2_conf () {
274 test -z "$module_path" && module_path=/usr/lib/apache2/modules
275 mkdir -p "$GIT_DIR/gitweb/logs"
276 bind=
277 test x"$local" = xtrue && bind='127.0.0.1:'
278 echo 'text/css css' > "$fqgitdir/mime.types"
279 cat > "$conf" <<EOF
280 ServerName "git-instaweb"
281 ServerRoot "$fqgitdir/gitweb"
282 DocumentRoot "$fqgitdir/gitweb"
283 PidFile "$fqgitdir/pid"
284 Listen $bind$port
287 for mod in mime dir; do
288 if test -e $module_path/mod_${mod}.so; then
289 echo "LoadModule ${mod}_module " \
290 "$module_path/mod_${mod}.so" >> "$conf"
292 done
293 cat >> "$conf" <<EOF
294 TypesConfig "$fqgitdir/mime.types"
295 DirectoryIndex gitweb.cgi
298 # check to see if Dennis Stosberg's mod_perl compatibility patch
299 # (<20060621130708.Gcbc6e5c@leonov.stosberg.net>) has been applied
300 if test -f "$module_path/mod_perl.so" && grep 'MOD_PERL' \
301 "$GIT_DIR/gitweb/gitweb.cgi" >/dev/null
302 then
303 # favor mod_perl if available
304 cat >> "$conf" <<EOF
305 LoadModule perl_module $module_path/mod_perl.so
306 PerlPassEnv GIT_DIR
307 PerlPassEnv GIT_EXEC_DIR
308 <Location /gitweb.cgi>
309 SetHandler perl-script
310 PerlResponseHandler ModPerl::Registry
311 PerlOptions +ParseHeaders
312 Options +ExecCGI
313 </Location>
315 else
316 # plain-old CGI
317 resolve_full_httpd
318 list_mods=$(echo "$full_httpd" | sed "s/-f$/-l/")
319 $list_mods | grep 'mod_cgi\.c' >/dev/null 2>&1 || \
320 echo "LoadModule cgi_module $module_path/mod_cgi.so" >> "$conf"
321 cat >> "$conf" <<EOF
322 AddHandler cgi-script .cgi
323 <Location /gitweb.cgi>
324 Options +ExecCGI
325 </Location>
330 mongoose_conf() {
331 cat > "$conf" <<EOF
332 # Mongoose web server configuration file.
333 # Lines starting with '#' and empty lines are ignored.
334 # For detailed description of every option, visit
335 # http://code.google.com/p/mongoose/wiki/MongooseManual
337 root $fqgitdir/gitweb
338 ports $port
339 index_files gitweb.cgi
340 #ssl_cert $fqgitdir/gitweb/ssl_cert.pem
341 error_log $fqgitdir/gitweb/error.log
342 access_log $fqgitdir/gitweb/access.log
344 #cgi setup
345 cgi_env PATH=/usr/local/bin:/usr/bin:/bin,GIT_DIR=$GIT_DIR,GIT_EXEC_PATH=$GIT_EXEC_PATH
346 cgi_interp $PERL
347 cgi_ext cgi,pl
349 # mimetype mapping
350 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
355 script='
356 s#^(my|our) \$projectroot =.*#$1 \$projectroot = "'$(dirname "$fqgitdir")'";#;
357 s#(my|our) \$gitbin =.*#$1 \$gitbin = "'$GIT_EXEC_PATH'";#;
358 s#(my|our) \$projects_list =.*#$1 \$projects_list = \$projectroot;#;
359 s#(my|our) \$git_temp =.*#$1 \$git_temp = "'$fqgitdir/gitweb/tmp'";#;'
361 gitweb_cgi () {
362 cat > "$1.tmp" <<\EOFGITWEB
363 @@GITWEB_CGI@@
364 EOFGITWEB
365 # Use the configured full path to perl to match the generated
366 # scripts' 'hashpling' line
367 "$PERL" -p -e "$script" "$1.tmp" > "$1"
368 chmod +x "$1"
369 rm -f "$1.tmp"
372 gitweb_css () {
373 cat > "$1" <<\EOFGITWEB
374 @@GITWEB_CSS@@
375 EOFGITWEB
378 gitweb_cgi "$GIT_DIR/gitweb/gitweb.cgi"
379 gitweb_css "$GIT_DIR/gitweb/gitweb.css"
381 case "$httpd" in
382 *lighttpd*)
383 lighttpd_conf
385 *apache2*)
386 apache2_conf
388 webrick)
389 webrick_conf
391 *mongoose*)
392 mongoose_conf
395 echo "Unknown httpd specified: $httpd"
396 exit 1
398 esac
400 start_httpd
401 url=http://127.0.0.1:$port
403 if test -n "$browser"; then
404 git web--browse -b "$browser" $url || echo $url
405 else
406 git web--browse -c "instaweb.browser" $url || echo $url