]> git.ipfire.org Git - thirdparty/git.git/blame - git-instaweb.sh
builtin-mailinfo.c: check error status from rewind and ftruncate
[thirdparty/git.git] / git-instaweb.sh
CommitLineData
a51d37c1
EW
1#!/bin/sh
2#
3# Copyright (c) 2006 Eric Wong
4#
c2db2e0e 5
c5699693 6PERL='@@PERL@@'
c2db2e0e
PH
7OPTIONS_KEEPDASHDASH=
8OPTIONS_SPEC="\
1b1dd23f 9git instaweb [options] (--start | --stop | --restart)
c2db2e0e
PH
10--
11l,local only bind on 127.0.0.1
12p,port= the port to bind to
13d,httpd= the command to launch
14b,browser= the browser to launch
15m,module-path= the module path (only needed for apache2)
16 Action
17stop stop the web server
18start start the web server
19restart restart the web server
20"
a51d37c1
EW
21
22. git-sh-setup
23
b2bc9a30 24fqgitdir="$GIT_DIR"
43d60d2e
FP
25local="$(git config --bool --get instaweb.local)"
26httpd="$(git config --get instaweb.httpd)"
27port=$(git config --get instaweb.port)
28module_path="$(git config --get instaweb.modulepath)"
a51d37c1 29
9126425d 30conf="$GIT_DIR/gitweb/httpd.conf"
a51d37c1
EW
31
32# Defaults:
33
82e5a82f 34# if installed, it doesn't need further configuration (module_path)
a51d37c1
EW
35test -z "$httpd" && httpd='lighttpd -f'
36
a51d37c1
EW
37# any untaken local port will do...
38test -z "$port" && port=1234
39
43d60d2e
FP
40resolve_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"
47 fi
48 ;;
49 esac
50
51 httpd_only="$(echo $httpd | cut -f1 -d' ')"
e47eec8f 52 if case "$httpd_only" in /*) : ;; *) which $httpd_only >/dev/null 2>&1;; esac
a51d37c1 53 then
43d60d2e 54 full_httpd=$httpd
a51d37c1
EW
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
14b45b66
MD
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"
a51d37c1
EW
61 do
62 if test -x "$i/$httpd_only"
63 then
43d60d2e 64 full_httpd=$i/$httpd
a51d37c1
EW
65 return
66 fi
67 done
43d60d2e
FP
68
69 echo >&2 "$httpd_only not found. Install $httpd_only or use" \
70 "--httpd to specify another httpd daemon."
f281e3a1 71 exit 1
a51d37c1 72 fi
43d60d2e
FP
73}
74
75start_httpd () {
76 # here $httpd should have a meaningful value
77 resolve_full_httpd
78
79 # don't quote $full_httpd, there can be arguments to it (-f)
0ded4758
WL
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=$!
86
87 if test $? != 0; then
88 echo "Could not execute http daemon $httpd."
89 exit 1
90 fi
91
92 cat > "$fqgitdir/pid" <<EOF
93$pid
94EOF
95 ;;
96 *)
97 $full_httpd "$fqgitdir/gitweb/httpd.conf"
98 if test $? != 0; then
99 echo "Could not execute http daemon $httpd."
100 exit 1
101 fi
102 ;;
103 esac
a51d37c1
EW
104}
105
106stop_httpd () {
43d60d2e 107 test -f "$fqgitdir/pid" && kill $(cat "$fqgitdir/pid")
a51d37c1
EW
108}
109
822f7c73 110while test $# != 0
a51d37c1
EW
111do
112 case "$1" in
113 --stop|stop)
114 stop_httpd
115 exit 0
116 ;;
117 --start|start)
118 start_httpd
119 exit 0
120 ;;
121 --restart|restart)
122 stop_httpd
123 start_httpd
124 exit 0
125 ;;
c2db2e0e 126 -l|--local)
a51d37c1
EW
127 local=true
128 ;;
c2db2e0e
PH
129 -d|--httpd)
130 shift
131 httpd="$1"
132 ;;
133 -b|--browser)
134 shift
135 browser="$1"
a51d37c1 136 ;;
c2db2e0e
PH
137 -p|--port)
138 shift
139 port="$1"
a51d37c1 140 ;;
c2db2e0e
PH
141 -m|--module-path)
142 shift
143 module_path="$1"
a51d37c1 144 ;;
c2db2e0e 145 --)
a51d37c1
EW
146 ;;
147 *)
148 usage
149 ;;
150 esac
151 shift
152done
153
154mkdir -p "$GIT_DIR/gitweb/tmp"
43d60d2e 155GIT_EXEC_PATH="$(git --exec-path)"
a51d37c1
EW
156GIT_DIR="$fqgitdir"
157export GIT_EXEC_PATH GIT_DIR
158
159
425b78e8
MD
160webrick_conf () {
161 # generate a standalone server script in $fqgitdir/gitweb.
162 cat >"$fqgitdir/gitweb/$httpd.rb" <<EOF
163require 'webrick'
164require 'yaml'
165options = YAML::load_file(ARGV[0])
166options[:StartCallback] = proc do
167 File.open(options[:PidFile],"w") do |f|
168 f.puts Process.pid
169 end
170end
171options[:ServerType] = WEBrick::Daemon
172server = WEBrick::HTTPServer.new(options)
173['INT', 'TERM'].each do |signal|
174 trap(signal) {server.shutdown}
175end
176server.start
177EOF
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
184exec ruby "$fqgitdir/gitweb/$httpd.rb" \$*
185EOF
186 chmod +x "$fqgitdir/gitweb/$httpd"
187
188 cat >"$conf" <<EOF
189:Port: $port
190:DocumentRoot: "$fqgitdir/gitweb"
191:DirectoryIndex: ["gitweb.cgi"]
192:PidFile: "$fqgitdir/pid"
193EOF
194 test "$local" = true && echo ':BindAddress: "127.0.0.1"' >> "$conf"
195}
196
a51d37c1
EW
197lighttpd_conf () {
198 cat > "$conf" <<EOF
199server.document-root = "$fqgitdir/gitweb"
200server.port = $port
e47eec8f 201server.modules = ( "mod_setenv", "mod_cgi" )
a51d37c1
EW
202server.indexfiles = ( "gitweb.cgi" )
203server.pid-file = "$fqgitdir/pid"
e47eec8f
RJ
204server.errorlog = "$fqgitdir/gitweb/error.log"
205
206# to enable, add "mod_access", "mod_accesslog" to server.modules
207# variable above and uncomment this
208#accesslog.filename = "$fqgitdir/gitweb/access.log"
209
210setenv.add-environment = ( "PATH" => "/usr/local/bin:/usr/bin:/bin" )
211
a51d37c1 212cgi.assign = ( ".cgi" => "" )
e47eec8f
RJ
213
214# mimetype mapping
215mimetype.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"
268 )
a51d37c1 269EOF
9126425d 270 test x"$local" = xtrue && echo 'server.bind = "127.0.0.1"' >> "$conf"
a51d37c1
EW
271}
272
273apache2_conf () {
274 test -z "$module_path" && module_path=/usr/lib/apache2/modules
275 mkdir -p "$GIT_DIR/gitweb/logs"
276 bind=
9126425d 277 test x"$local" = xtrue && bind='127.0.0.1:'
e24c76bf 278 echo 'text/css css' > "$fqgitdir/mime.types"
a51d37c1 279 cat > "$conf" <<EOF
44a167b0 280ServerName "git-instaweb"
a51d37c1
EW
281ServerRoot "$fqgitdir/gitweb"
282DocumentRoot "$fqgitdir/gitweb"
283PidFile "$fqgitdir/pid"
284Listen $bind$port
44a167b0
EW
285EOF
286
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"
291 fi
292 done
293 cat >> "$conf" <<EOF
e24c76bf 294TypesConfig "$fqgitdir/mime.types"
a51d37c1
EW
295DirectoryIndex gitweb.cgi
296EOF
297
298 # check to see if Dennis Stosberg's mod_perl compatibility patch
299 # (<20060621130708.Gcbc6e5c@leonov.stosberg.net>) has been applied
f0e588df 300 if test -f "$module_path/mod_perl.so" && grep 'MOD_PERL' \
a51d37c1
EW
301 "$GIT_DIR/gitweb/gitweb.cgi" >/dev/null
302 then
303 # favor mod_perl if available
304 cat >> "$conf" <<EOF
305LoadModule perl_module $module_path/mod_perl.so
306PerlPassEnv GIT_DIR
307PerlPassEnv GIT_EXEC_DIR
308<Location /gitweb.cgi>
309 SetHandler perl-script
310 PerlResponseHandler ModPerl::Registry
311 PerlOptions +ParseHeaders
312 Options +ExecCGI
313</Location>
314EOF
315 else
316 # plain-old CGI
43d60d2e
FP
317 resolve_full_httpd
318 list_mods=$(echo "$full_httpd" | sed "s/-f$/-l/")
5209eda8 319 $list_mods | grep 'mod_cgi\.c' >/dev/null 2>&1 || \
2b5d2d87 320 echo "LoadModule cgi_module $module_path/mod_cgi.so" >> "$conf"
a51d37c1 321 cat >> "$conf" <<EOF
a51d37c1
EW
322AddHandler cgi-script .cgi
323<Location /gitweb.cgi>
324 Options +ExecCGI
325</Location>
326EOF
327 fi
328}
329
0ded4758
WL
330mongoose_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
336
337root $fqgitdir/gitweb
338ports $port
339index_files gitweb.cgi
340#ssl_cert $fqgitdir/gitweb/ssl_cert.pem
341error_log $fqgitdir/gitweb/error.log
342access_log $fqgitdir/gitweb/access.log
343
344#cgi setup
345cgi_env PATH=/usr/local/bin:/usr/bin:/bin,GIT_DIR=$GIT_DIR,GIT_EXEC_PATH=$GIT_EXEC_PATH
346cgi_interp $PERL
347cgi_ext cgi,pl
348
349# mimetype mapping
350mime_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
351EOF
352}
353
354
a51d37c1 355script='
c5699693
CB
356s#^(my|our) \$projectroot =.*#$1 \$projectroot = "'$(dirname "$fqgitdir")'";#;
357s#(my|our) \$gitbin =.*#$1 \$gitbin = "'$GIT_EXEC_PATH'";#;
358s#(my|our) \$projects_list =.*#$1 \$projects_list = \$projectroot;#;
359s#(my|our) \$git_temp =.*#$1 \$git_temp = "'$fqgitdir/gitweb/tmp'";#;'
a51d37c1
EW
360
361gitweb_cgi () {
362 cat > "$1.tmp" <<\EOFGITWEB
363@@GITWEB_CGI@@
364EOFGITWEB
c5699693
CB
365 # Use the configured full path to perl to match the generated
366 # scripts' 'hashpling' line
367 "$PERL" -p -e "$script" "$1.tmp" > "$1"
a51d37c1
EW
368 chmod +x "$1"
369 rm -f "$1.tmp"
370}
371
372gitweb_css () {
373 cat > "$1" <<\EOFGITWEB
374@@GITWEB_CSS@@
375EOFGITWEB
376}
377
9126425d
JF
378gitweb_cgi "$GIT_DIR/gitweb/gitweb.cgi"
379gitweb_css "$GIT_DIR/gitweb/gitweb.css"
a51d37c1
EW
380
381case "$httpd" in
382*lighttpd*)
383 lighttpd_conf
384 ;;
385*apache2*)
386 apache2_conf
387 ;;
425b78e8
MD
388webrick)
389 webrick_conf
390 ;;
0ded4758
WL
391*mongoose*)
392 mongoose_conf
393 ;;
a51d37c1
EW
394*)
395 echo "Unknown httpd specified: $httpd"
396 exit 1
397 ;;
398esac
399
400start_httpd
5209eda8 401url=http://127.0.0.1:$port
2e0c2902
CC
402
403if test -n "$browser"; then
404 git web--browse -b "$browser" $url || echo $url
405else
406 git web--browse -c "instaweb.browser" $url || echo $url
407fi