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