contrib: Make remotes2config.sh script more robust
[git/git-svn.git] / git-instaweb.sh
blob8503ae40309e6c8b1547289c9950d99f4bf736b5
1 #!/bin/sh
3 # Copyright (c) 2006 Eric Wong
6 OPTIONS_KEEPDASHDASH=
7 OPTIONS_SPEC="\
8 git-instaweb [options] (--start | --stop | --restart)
9 --
10 l,local only bind on 127.0.0.1
11 p,port= the port to bind to
12 d,httpd= the command to launch
13 b,browser= the browser to launch
14 m,module-path= the module path (only needed for apache2)
15 Action
16 stop stop the web server
17 start start the web server
18 restart restart the web server
21 . git-sh-setup
23 fqgitdir="$GIT_DIR"
24 local="`git config --bool --get instaweb.local`"
25 httpd="`git config --get instaweb.httpd`"
26 browser="`git config --get instaweb.browser`"
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 # probably the most popular browser among gitweb users
38 test -z "$browser" && browser='firefox'
40 # any untaken local port will do...
41 test -z "$port" && port=1234
43 start_httpd () {
44 httpd_only="`echo $httpd | cut -f1 -d' '`"
45 if case "$httpd_only" in /*) : ;; *) which $httpd_only >/dev/null;; esac
46 then
47 $httpd "$fqgitdir/gitweb/httpd.conf"
48 else
49 # many httpds are installed in /usr/sbin or /usr/local/sbin
50 # these days and those are not in most users $PATHs
51 # in addition, we may have generated a server script
52 # in $fqgitdir/gitweb.
53 for i in /usr/local/sbin /usr/sbin "$fqgitdir/gitweb"
55 if test -x "$i/$httpd_only"
56 then
57 # don't quote $httpd, there can be
58 # arguments to it (-f)
59 $i/$httpd "$fqgitdir/gitweb/httpd.conf"
60 return
62 done
63 echo "$httpd_only not found. Install $httpd_only or use" \
64 "--httpd to specify another http daemon."
65 exit 1
67 if test $? != 0; then
68 echo "Could not execute http daemon $httpd."
69 exit 1
73 stop_httpd () {
74 test -f "$fqgitdir/pid" && kill `cat "$fqgitdir/pid"`
77 while test $# != 0
79 case "$1" in
80 --stop|stop)
81 stop_httpd
82 exit 0
84 --start|start)
85 start_httpd
86 exit 0
88 --restart|restart)
89 stop_httpd
90 start_httpd
91 exit 0
93 -l|--local)
94 local=true
96 -d|--httpd)
97 shift
98 httpd="$1"
100 -b|--browser)
101 shift
102 browser="$1"
104 -p|--port)
105 shift
106 port="$1"
108 -m|--module-path)
109 shift
110 module_path="$1"
115 usage
117 esac
118 shift
119 done
121 mkdir -p "$GIT_DIR/gitweb/tmp"
122 GIT_EXEC_PATH="`git --exec-path`"
123 GIT_DIR="$fqgitdir"
124 export GIT_EXEC_PATH GIT_DIR
127 webrick_conf () {
128 # generate a standalone server script in $fqgitdir/gitweb.
129 cat >"$fqgitdir/gitweb/$httpd.rb" <<EOF
130 require 'webrick'
131 require 'yaml'
132 options = YAML::load_file(ARGV[0])
133 options[:StartCallback] = proc do
134 File.open(options[:PidFile],"w") do |f|
135 f.puts Process.pid
138 options[:ServerType] = WEBrick::Daemon
139 server = WEBrick::HTTPServer.new(options)
140 ['INT', 'TERM'].each do |signal|
141 trap(signal) {server.shutdown}
143 server.start
145 # generate a shell script to invoke the above ruby script,
146 # which assumes _ruby_ is in the user's $PATH. that's _one_
147 # portable way to run ruby, which could be installed anywhere,
148 # really.
149 cat >"$fqgitdir/gitweb/$httpd" <<EOF
150 #!/bin/sh
151 exec ruby "$fqgitdir/gitweb/$httpd.rb" \$*
153 chmod +x "$fqgitdir/gitweb/$httpd"
155 cat >"$conf" <<EOF
156 :Port: $port
157 :DocumentRoot: "$fqgitdir/gitweb"
158 :DirectoryIndex: ["gitweb.cgi"]
159 :PidFile: "$fqgitdir/pid"
161 test "$local" = true && echo ':BindAddress: "127.0.0.1"' >> "$conf"
164 lighttpd_conf () {
165 cat > "$conf" <<EOF
166 server.document-root = "$fqgitdir/gitweb"
167 server.port = $port
168 server.modules = ( "mod_cgi" )
169 server.indexfiles = ( "gitweb.cgi" )
170 server.pid-file = "$fqgitdir/pid"
171 cgi.assign = ( ".cgi" => "" )
172 mimetype.assign = ( ".css" => "text/css" )
174 test x"$local" = xtrue && echo 'server.bind = "127.0.0.1"' >> "$conf"
177 apache2_conf () {
178 test -z "$module_path" && module_path=/usr/lib/apache2/modules
179 mkdir -p "$GIT_DIR/gitweb/logs"
180 bind=
181 test x"$local" = xtrue && bind='127.0.0.1:'
182 echo 'text/css css' > $fqgitdir/mime.types
183 cat > "$conf" <<EOF
184 ServerName "git-instaweb"
185 ServerRoot "$fqgitdir/gitweb"
186 DocumentRoot "$fqgitdir/gitweb"
187 PidFile "$fqgitdir/pid"
188 Listen $bind$port
191 for mod in mime dir; do
192 if test -e $module_path/mod_${mod}.so; then
193 echo "LoadModule ${mod}_module " \
194 "$module_path/mod_${mod}.so" >> "$conf"
196 done
197 cat >> "$conf" <<EOF
198 TypesConfig $fqgitdir/mime.types
199 DirectoryIndex gitweb.cgi
202 # check to see if Dennis Stosberg's mod_perl compatibility patch
203 # (<20060621130708.Gcbc6e5c@leonov.stosberg.net>) has been applied
204 if test -f "$module_path/mod_perl.so" && grep '^our $gitbin' \
205 "$GIT_DIR/gitweb/gitweb.cgi" >/dev/null
206 then
207 # favor mod_perl if available
208 cat >> "$conf" <<EOF
209 LoadModule perl_module $module_path/mod_perl.so
210 PerlPassEnv GIT_DIR
211 PerlPassEnv GIT_EXEC_DIR
212 <Location /gitweb.cgi>
213 SetHandler perl-script
214 PerlResponseHandler ModPerl::Registry
215 PerlOptions +ParseHeaders
216 Options +ExecCGI
217 </Location>
219 else
220 # plain-old CGI
221 list_mods=`echo "$httpd" | sed "s/-f$/-l/"`
222 $list_mods | grep 'mod_cgi\.c' >/dev/null 2>&1 || \
223 echo "LoadModule cgi_module $module_path/mod_cgi.so" >> "$conf"
224 cat >> "$conf" <<EOF
225 AddHandler cgi-script .cgi
226 <Location /gitweb.cgi>
227 Options +ExecCGI
228 </Location>
233 script='
234 s#^\(my\|our\) $projectroot =.*#\1 $projectroot = "'$(dirname "$fqgitdir")'";#
235 s#\(my\|our\) $gitbin =.*#\1 $gitbin = "'$GIT_EXEC_PATH'";#
236 s#\(my\|our\) $projects_list =.*#\1 $projects_list = $projectroot;#
237 s#\(my\|our\) $git_temp =.*#\1 $git_temp = "'$fqgitdir/gitweb/tmp'";#'
239 gitweb_cgi () {
240 cat > "$1.tmp" <<\EOFGITWEB
241 @@GITWEB_CGI@@
242 EOFGITWEB
243 sed "$script" "$1.tmp" > "$1"
244 chmod +x "$1"
245 rm -f "$1.tmp"
248 gitweb_css () {
249 cat > "$1" <<\EOFGITWEB
250 @@GITWEB_CSS@@
251 EOFGITWEB
254 gitweb_cgi "$GIT_DIR/gitweb/gitweb.cgi"
255 gitweb_css "$GIT_DIR/gitweb/gitweb.css"
257 case "$httpd" in
258 *lighttpd*)
259 lighttpd_conf
261 *apache2*)
262 apache2_conf
264 webrick)
265 webrick_conf
268 echo "Unknown httpd specified: $httpd"
269 exit 1
271 esac
273 start_httpd
274 url=http://127.0.0.1:$port
275 "$browser" $url || echo $url