Merge branch 'js/diff-verbose-submodule'
[git] / git-instaweb.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2006 Eric Wong
4 #
5
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 "
21
22 . git-sh-setup
23
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)"
29
30 conf="$GIT_DIR/gitweb/httpd.conf"
31
32 # Defaults:
33
34 # if installed, it doesn't need further configuration (module_path)
35 test -z "$httpd" && httpd='lighttpd -f'
36
37 # any untaken local port will do...
38 test -z "$port" && port=1234
39
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"
47                 fi
48                 ;;
49         esac
50
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"
61                 do
62                         if test -x "$i/$httpd_only"
63                         then
64                                 full_httpd=$i/$httpd
65                                 return
66                         fi
67                 done
68
69                 echo >&2 "$httpd_only not found. Install $httpd_only or use" \
70                      "--httpd to specify another httpd daemon."
71                 exit 1
72         fi
73 }
74
75 start_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)
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
94 EOF
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
104 }
105
106 stop_httpd () {
107         test -f "$fqgitdir/pid" && kill $(cat "$fqgitdir/pid")
108 }
109
110 while test $# != 0
111 do
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                 ;;
126         -l|--local)
127                 local=true
128                 ;;
129         -d|--httpd)
130                 shift
131                 httpd="$1"
132                 ;;
133         -b|--browser)
134                 shift
135                 browser="$1"
136                 ;;
137         -p|--port)
138                 shift
139                 port="$1"
140                 ;;
141         -m|--module-path)
142                 shift
143                 module_path="$1"
144                 ;;
145         --)
146                 ;;
147         *)
148                 usage
149                 ;;
150         esac
151         shift
152 done
153
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
158
159
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
169   end
170 end
171 options[:ServerType] = WEBrick::Daemon
172 server = WEBrick::HTTPServer.new(options)
173 ['INT', 'TERM'].each do |signal|
174   trap(signal) {server.shutdown}
175 end
176 server.start
177 EOF
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" \$*
185 EOF
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"
193 EOF
194         test "$local" = true && echo ':BindAddress: "127.0.0.1"' >> "$conf"
195 }
196
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"
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
210 setenv.add-environment = ( "PATH" => "/usr/local/bin:/usr/bin:/bin" )
211
212 cgi.assign = ( ".cgi" => "" )
213
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"
268  )
269 EOF
270         test x"$local" = xtrue && echo 'server.bind = "127.0.0.1"' >> "$conf"
271 }
272
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
285 EOF
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
294 TypesConfig "$fqgitdir/mime.types"
295 DirectoryIndex gitweb.cgi
296 EOF
297
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>
314 EOF
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                 if test -f "$module_path/mod_cgi.so"
321                 then
322                         echo "LoadModule cgi_module $module_path/mod_cgi.so" >> "$conf"
323                 else
324                         $list_mods | grep 'mod_cgid\.c' >/dev/null 2>&1 || \
325                         if test -f "$module_path/mod_cgid.so"
326                         then
327                                 echo "LoadModule cgid_module $module_path/mod_cgid.so" \
328                                         >> "$conf"
329                         else
330                                 echo "You have no CGI support!"
331                                 exit 2
332                         fi
333                         echo "ScriptSock logs/gitweb.sock" >> "$conf"
334                 fi
335                 cat >> "$conf" <<EOF
336 AddHandler cgi-script .cgi
337 <Location /gitweb.cgi>
338         Options +ExecCGI
339 </Location>
340 EOF
341         fi
342 }
343
344 mongoose_conf() {
345         cat > "$conf" <<EOF
346 # Mongoose web server configuration file.
347 # Lines starting with '#' and empty lines are ignored.
348 # For detailed description of every option, visit
349 # http://code.google.com/p/mongoose/wiki/MongooseManual
350
351 root            $fqgitdir/gitweb
352 ports           $port
353 index_files     gitweb.cgi
354 #ssl_cert       $fqgitdir/gitweb/ssl_cert.pem
355 error_log       $fqgitdir/gitweb/error.log
356 access_log      $fqgitdir/gitweb/access.log
357
358 #cgi setup
359 cgi_env         PATH=/usr/local/bin:/usr/bin:/bin,GIT_DIR=$GIT_DIR,GIT_EXEC_PATH=$GIT_EXEC_PATH
360 cgi_interp      $PERL
361 cgi_ext         cgi,pl
362
363 # mimetype mapping
364 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
365 EOF
366 }
367
368
369 script='
370 s#^(my|our) \$projectroot =.*#$1 \$projectroot = "'$(dirname "$fqgitdir")'";#;
371 s#(my|our) \$gitbin =.*#$1 \$gitbin = "'$GIT_EXEC_PATH'";#;
372 s#(my|our) \$projects_list =.*#$1 \$projects_list = \$projectroot;#;
373 s#(my|our) \$git_temp =.*#$1 \$git_temp = "'$fqgitdir/gitweb/tmp'";#;'
374
375 gitweb_cgi () {
376         cat > "$1.tmp" <<\EOFGITWEB
377 @@GITWEB_CGI@@
378 EOFGITWEB
379         # Use the configured full path to perl to match the generated
380         # scripts' 'hashpling' line
381         "$PERL" -p -e "$script" "$1.tmp"  > "$1"
382         chmod +x "$1"
383         rm -f "$1.tmp"
384 }
385
386 gitweb_css () {
387         cat > "$1" <<\EOFGITWEB
388 @@GITWEB_CSS@@
389 EOFGITWEB
390 }
391
392 gitweb_cgi "$GIT_DIR/gitweb/gitweb.cgi"
393 gitweb_css "$GIT_DIR/gitweb/gitweb.css"
394
395 case "$httpd" in
396 *lighttpd*)
397         lighttpd_conf
398         ;;
399 *apache2*)
400         apache2_conf
401         ;;
402 webrick)
403         webrick_conf
404         ;;
405 *mongoose*)
406         mongoose_conf
407         ;;
408 *)
409         echo "Unknown httpd specified: $httpd"
410         exit 1
411         ;;
412 esac
413
414 start_httpd
415 url=http://127.0.0.1:$port
416
417 if test -n "$browser"; then
418         git web--browse -b "$browser" $url || echo $url
419 else
420         git web--browse -c "instaweb.browser" $url || echo $url
421 fi