t0050: perl portability fix
[git/peff.git] / git-instaweb.sh
blob6f91c8f845b2c4ef426a6451ac07abae2bce9488
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 start_httpd () {
41 httpd_only="`echo $httpd | cut -f1 -d' '`"
42 if case "$httpd_only" in /*) : ;; *) which $httpd_only >/dev/null;; esac
43 then
44 $httpd "$fqgitdir/gitweb/httpd.conf"
45 else
46 # many httpds are installed in /usr/sbin or /usr/local/sbin
47 # these days and those are not in most users $PATHs
48 # in addition, we may have generated a server script
49 # in $fqgitdir/gitweb.
50 for i in /usr/local/sbin /usr/sbin "$fqgitdir/gitweb"
52 if test -x "$i/$httpd_only"
53 then
54 # don't quote $httpd, there can be
55 # arguments to it (-f)
56 $i/$httpd "$fqgitdir/gitweb/httpd.conf"
57 return
59 done
60 echo "$httpd_only not found. Install $httpd_only or use" \
61 "--httpd to specify another http daemon."
62 exit 1
64 if test $? != 0; then
65 echo "Could not execute http daemon $httpd."
66 exit 1
70 stop_httpd () {
71 test -f "$fqgitdir/pid" && kill `cat "$fqgitdir/pid"`
74 while test $# != 0
76 case "$1" in
77 --stop|stop)
78 stop_httpd
79 exit 0
81 --start|start)
82 start_httpd
83 exit 0
85 --restart|restart)
86 stop_httpd
87 start_httpd
88 exit 0
90 -l|--local)
91 local=true
93 -d|--httpd)
94 shift
95 httpd="$1"
97 -b|--browser)
98 shift
99 browser="$1"
101 -p|--port)
102 shift
103 port="$1"
105 -m|--module-path)
106 shift
107 module_path="$1"
112 usage
114 esac
115 shift
116 done
118 mkdir -p "$GIT_DIR/gitweb/tmp"
119 GIT_EXEC_PATH="`git --exec-path`"
120 GIT_DIR="$fqgitdir"
121 export GIT_EXEC_PATH GIT_DIR
124 webrick_conf () {
125 # generate a standalone server script in $fqgitdir/gitweb.
126 cat >"$fqgitdir/gitweb/$httpd.rb" <<EOF
127 require 'webrick'
128 require 'yaml'
129 options = YAML::load_file(ARGV[0])
130 options[:StartCallback] = proc do
131 File.open(options[:PidFile],"w") do |f|
132 f.puts Process.pid
135 options[:ServerType] = WEBrick::Daemon
136 server = WEBrick::HTTPServer.new(options)
137 ['INT', 'TERM'].each do |signal|
138 trap(signal) {server.shutdown}
140 server.start
142 # generate a shell script to invoke the above ruby script,
143 # which assumes _ruby_ is in the user's $PATH. that's _one_
144 # portable way to run ruby, which could be installed anywhere,
145 # really.
146 cat >"$fqgitdir/gitweb/$httpd" <<EOF
147 #!/bin/sh
148 exec ruby "$fqgitdir/gitweb/$httpd.rb" \$*
150 chmod +x "$fqgitdir/gitweb/$httpd"
152 cat >"$conf" <<EOF
153 :Port: $port
154 :DocumentRoot: "$fqgitdir/gitweb"
155 :DirectoryIndex: ["gitweb.cgi"]
156 :PidFile: "$fqgitdir/pid"
158 test "$local" = true && echo ':BindAddress: "127.0.0.1"' >> "$conf"
161 lighttpd_conf () {
162 cat > "$conf" <<EOF
163 server.document-root = "$fqgitdir/gitweb"
164 server.port = $port
165 server.modules = ( "mod_cgi" )
166 server.indexfiles = ( "gitweb.cgi" )
167 server.pid-file = "$fqgitdir/pid"
168 cgi.assign = ( ".cgi" => "" )
169 mimetype.assign = ( ".css" => "text/css" )
171 test x"$local" = xtrue && echo 'server.bind = "127.0.0.1"' >> "$conf"
174 apache2_conf () {
175 test -z "$module_path" && module_path=/usr/lib/apache2/modules
176 mkdir -p "$GIT_DIR/gitweb/logs"
177 bind=
178 test x"$local" = xtrue && bind='127.0.0.1:'
179 echo 'text/css css' > $fqgitdir/mime.types
180 cat > "$conf" <<EOF
181 ServerName "git-instaweb"
182 ServerRoot "$fqgitdir/gitweb"
183 DocumentRoot "$fqgitdir/gitweb"
184 PidFile "$fqgitdir/pid"
185 Listen $bind$port
188 for mod in mime dir; do
189 if test -e $module_path/mod_${mod}.so; then
190 echo "LoadModule ${mod}_module " \
191 "$module_path/mod_${mod}.so" >> "$conf"
193 done
194 cat >> "$conf" <<EOF
195 TypesConfig $fqgitdir/mime.types
196 DirectoryIndex gitweb.cgi
199 # check to see if Dennis Stosberg's mod_perl compatibility patch
200 # (<20060621130708.Gcbc6e5c@leonov.stosberg.net>) has been applied
201 if test -f "$module_path/mod_perl.so" && grep '^our $gitbin' \
202 "$GIT_DIR/gitweb/gitweb.cgi" >/dev/null
203 then
204 # favor mod_perl if available
205 cat >> "$conf" <<EOF
206 LoadModule perl_module $module_path/mod_perl.so
207 PerlPassEnv GIT_DIR
208 PerlPassEnv GIT_EXEC_DIR
209 <Location /gitweb.cgi>
210 SetHandler perl-script
211 PerlResponseHandler ModPerl::Registry
212 PerlOptions +ParseHeaders
213 Options +ExecCGI
214 </Location>
216 else
217 # plain-old CGI
218 list_mods=`echo "$httpd" | sed "s/-f$/-l/"`
219 $list_mods | grep 'mod_cgi\.c' >/dev/null 2>&1 || \
220 echo "LoadModule cgi_module $module_path/mod_cgi.so" >> "$conf"
221 cat >> "$conf" <<EOF
222 AddHandler cgi-script .cgi
223 <Location /gitweb.cgi>
224 Options +ExecCGI
225 </Location>
230 script='
231 s#^(my|our) \$projectroot =.*#$1 \$projectroot = "'$(dirname "$fqgitdir")'";#;
232 s#(my|our) \$gitbin =.*#$1 \$gitbin = "'$GIT_EXEC_PATH'";#;
233 s#(my|our) \$projects_list =.*#$1 \$projects_list = \$projectroot;#;
234 s#(my|our) \$git_temp =.*#$1 \$git_temp = "'$fqgitdir/gitweb/tmp'";#;'
236 gitweb_cgi () {
237 cat > "$1.tmp" <<\EOFGITWEB
238 @@GITWEB_CGI@@
239 EOFGITWEB
240 # Use the configured full path to perl to match the generated
241 # scripts' 'hashpling' line
242 "$PERL" -p -e "$script" "$1.tmp" > "$1"
243 chmod +x "$1"
244 rm -f "$1.tmp"
247 gitweb_css () {
248 cat > "$1" <<\EOFGITWEB
249 @@GITWEB_CSS@@
250 EOFGITWEB
253 gitweb_cgi "$GIT_DIR/gitweb/gitweb.cgi"
254 gitweb_css "$GIT_DIR/gitweb/gitweb.css"
256 case "$httpd" in
257 *lighttpd*)
258 lighttpd_conf
260 *apache2*)
261 apache2_conf
263 webrick)
264 webrick_conf
267 echo "Unknown httpd specified: $httpd"
268 exit 1
270 esac
272 start_httpd
273 url=http://127.0.0.1:$port
275 if test -n "$browser"; then
276 git web--browse -b "$browser" $url || echo $url
277 else
278 git web--browse -c "instaweb.browser" $url || echo $url