git-shell: accept "git foo" form
[git/git-svn.git] / git-instaweb.sh
blob3e4452bc4bc1fe57c75848ccd9c073aeb065a980
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 browser="`git config --get instaweb.browser`"
28 test -z "$browser" && browser="`git config --get web.browser`"
29 port=`git config --get instaweb.port`
30 module_path="`git config --get instaweb.modulepath`"
32 conf="$GIT_DIR/gitweb/httpd.conf"
34 # Defaults:
36 # if installed, it doesn't need further configuration (module_path)
37 test -z "$httpd" && httpd='lighttpd -f'
39 # probably the most popular browser among gitweb users
40 test -z "$browser" && browser='firefox'
42 # any untaken local port will do...
43 test -z "$port" && port=1234
45 start_httpd () {
46 httpd_only="`echo $httpd | cut -f1 -d' '`"
47 if case "$httpd_only" in /*) : ;; *) which $httpd_only >/dev/null;; esac
48 then
49 $httpd "$fqgitdir/gitweb/httpd.conf"
50 else
51 # many httpds are installed in /usr/sbin or /usr/local/sbin
52 # these days and those are not in most users $PATHs
53 # in addition, we may have generated a server script
54 # in $fqgitdir/gitweb.
55 for i in /usr/local/sbin /usr/sbin "$fqgitdir/gitweb"
57 if test -x "$i/$httpd_only"
58 then
59 # don't quote $httpd, there can be
60 # arguments to it (-f)
61 $i/$httpd "$fqgitdir/gitweb/httpd.conf"
62 return
64 done
65 echo "$httpd_only not found. Install $httpd_only or use" \
66 "--httpd to specify another http daemon."
67 exit 1
69 if test $? != 0; then
70 echo "Could not execute http daemon $httpd."
71 exit 1
75 stop_httpd () {
76 test -f "$fqgitdir/pid" && kill `cat "$fqgitdir/pid"`
79 while test $# != 0
81 case "$1" in
82 --stop|stop)
83 stop_httpd
84 exit 0
86 --start|start)
87 start_httpd
88 exit 0
90 --restart|restart)
91 stop_httpd
92 start_httpd
93 exit 0
95 -l|--local)
96 local=true
98 -d|--httpd)
99 shift
100 httpd="$1"
102 -b|--browser)
103 shift
104 browser="$1"
106 -p|--port)
107 shift
108 port="$1"
110 -m|--module-path)
111 shift
112 module_path="$1"
117 usage
119 esac
120 shift
121 done
123 mkdir -p "$GIT_DIR/gitweb/tmp"
124 GIT_EXEC_PATH="`git --exec-path`"
125 GIT_DIR="$fqgitdir"
126 export GIT_EXEC_PATH GIT_DIR
129 webrick_conf () {
130 # generate a standalone server script in $fqgitdir/gitweb.
131 cat >"$fqgitdir/gitweb/$httpd.rb" <<EOF
132 require 'webrick'
133 require 'yaml'
134 options = YAML::load_file(ARGV[0])
135 options[:StartCallback] = proc do
136 File.open(options[:PidFile],"w") do |f|
137 f.puts Process.pid
140 options[:ServerType] = WEBrick::Daemon
141 server = WEBrick::HTTPServer.new(options)
142 ['INT', 'TERM'].each do |signal|
143 trap(signal) {server.shutdown}
145 server.start
147 # generate a shell script to invoke the above ruby script,
148 # which assumes _ruby_ is in the user's $PATH. that's _one_
149 # portable way to run ruby, which could be installed anywhere,
150 # really.
151 cat >"$fqgitdir/gitweb/$httpd" <<EOF
152 #!/bin/sh
153 exec ruby "$fqgitdir/gitweb/$httpd.rb" \$*
155 chmod +x "$fqgitdir/gitweb/$httpd"
157 cat >"$conf" <<EOF
158 :Port: $port
159 :DocumentRoot: "$fqgitdir/gitweb"
160 :DirectoryIndex: ["gitweb.cgi"]
161 :PidFile: "$fqgitdir/pid"
163 test "$local" = true && echo ':BindAddress: "127.0.0.1"' >> "$conf"
166 lighttpd_conf () {
167 cat > "$conf" <<EOF
168 server.document-root = "$fqgitdir/gitweb"
169 server.port = $port
170 server.modules = ( "mod_cgi" )
171 server.indexfiles = ( "gitweb.cgi" )
172 server.pid-file = "$fqgitdir/pid"
173 cgi.assign = ( ".cgi" => "" )
174 mimetype.assign = ( ".css" => "text/css" )
176 test x"$local" = xtrue && echo 'server.bind = "127.0.0.1"' >> "$conf"
179 apache2_conf () {
180 test -z "$module_path" && module_path=/usr/lib/apache2/modules
181 mkdir -p "$GIT_DIR/gitweb/logs"
182 bind=
183 test x"$local" = xtrue && bind='127.0.0.1:'
184 echo 'text/css css' > $fqgitdir/mime.types
185 cat > "$conf" <<EOF
186 ServerName "git-instaweb"
187 ServerRoot "$fqgitdir/gitweb"
188 DocumentRoot "$fqgitdir/gitweb"
189 PidFile "$fqgitdir/pid"
190 Listen $bind$port
193 for mod in mime dir; do
194 if test -e $module_path/mod_${mod}.so; then
195 echo "LoadModule ${mod}_module " \
196 "$module_path/mod_${mod}.so" >> "$conf"
198 done
199 cat >> "$conf" <<EOF
200 TypesConfig $fqgitdir/mime.types
201 DirectoryIndex gitweb.cgi
204 # check to see if Dennis Stosberg's mod_perl compatibility patch
205 # (<20060621130708.Gcbc6e5c@leonov.stosberg.net>) has been applied
206 if test -f "$module_path/mod_perl.so" && grep '^our $gitbin' \
207 "$GIT_DIR/gitweb/gitweb.cgi" >/dev/null
208 then
209 # favor mod_perl if available
210 cat >> "$conf" <<EOF
211 LoadModule perl_module $module_path/mod_perl.so
212 PerlPassEnv GIT_DIR
213 PerlPassEnv GIT_EXEC_DIR
214 <Location /gitweb.cgi>
215 SetHandler perl-script
216 PerlResponseHandler ModPerl::Registry
217 PerlOptions +ParseHeaders
218 Options +ExecCGI
219 </Location>
221 else
222 # plain-old CGI
223 list_mods=`echo "$httpd" | sed "s/-f$/-l/"`
224 $list_mods | grep 'mod_cgi\.c' >/dev/null 2>&1 || \
225 echo "LoadModule cgi_module $module_path/mod_cgi.so" >> "$conf"
226 cat >> "$conf" <<EOF
227 AddHandler cgi-script .cgi
228 <Location /gitweb.cgi>
229 Options +ExecCGI
230 </Location>
235 script='
236 s#^(my|our) \$projectroot =.*#$1 \$projectroot = "'$(dirname "$fqgitdir")'";#;
237 s#(my|our) \$gitbin =.*#$1 \$gitbin = "'$GIT_EXEC_PATH'";#;
238 s#(my|our) \$projects_list =.*#$1 \$projects_list = \$projectroot;#;
239 s#(my|our) \$git_temp =.*#$1 \$git_temp = "'$fqgitdir/gitweb/tmp'";#;'
241 gitweb_cgi () {
242 cat > "$1.tmp" <<\EOFGITWEB
243 @@GITWEB_CGI@@
244 EOFGITWEB
245 # Use the configured full path to perl to match the generated
246 # scripts' 'hashpling' line
247 "$PERL" -p -e "$script" "$1.tmp" > "$1"
248 chmod +x "$1"
249 rm -f "$1.tmp"
252 gitweb_css () {
253 cat > "$1" <<\EOFGITWEB
254 @@GITWEB_CSS@@
255 EOFGITWEB
258 gitweb_cgi "$GIT_DIR/gitweb/gitweb.cgi"
259 gitweb_css "$GIT_DIR/gitweb/gitweb.css"
261 case "$httpd" in
262 *lighttpd*)
263 lighttpd_conf
265 *apache2*)
266 apache2_conf
268 webrick)
269 webrick_conf
272 echo "Unknown httpd specified: $httpd"
273 exit 1
275 esac
277 init_browser_path() {
278 browser_path="`git config browser.$1.path`"
279 test -z "$browser_path" && browser_path="$1"
282 start_httpd
283 url=http://127.0.0.1:$port
284 test -n "$browser" && {
285 init_browser_path "$browser"
286 "$browser_path" $url
287 } || echo $url