Code

whitespace: fix config.txt description of indent-with-non-tab
[git.git] / git-instaweb.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2006 Eric Wong
4 #
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
19 "
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 test -z "$browser" && browser="`git config --get web.browser`"
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 # probably the most popular browser among gitweb users
39 test -z "$browser" && browser='firefox'
41 # any untaken local port will do...
42 test -z "$port" && port=1234
44 start_httpd () {
45         httpd_only="`echo $httpd | cut -f1 -d' '`"
46         if case "$httpd_only" in /*) : ;; *) which $httpd_only >/dev/null;; esac
47         then
48                 $httpd "$fqgitdir/gitweb/httpd.conf"
49         else
50                 # many httpds are installed in /usr/sbin or /usr/local/sbin
51                 # these days and those are not in most users $PATHs
52                 # in addition, we may have generated a server script
53                 # in $fqgitdir/gitweb.
54                 for i in /usr/local/sbin /usr/sbin "$fqgitdir/gitweb"
55                 do
56                         if test -x "$i/$httpd_only"
57                         then
58                                 # don't quote $httpd, there can be
59                                 # arguments to it (-f)
60                                 $i/$httpd "$fqgitdir/gitweb/httpd.conf"
61                                 return
62                         fi
63                 done
64                 echo "$httpd_only not found. Install $httpd_only or use" \
65                      "--httpd to specify another http daemon."
66                 exit 1
67         fi
68         if test $? != 0; then
69                 echo "Could not execute http daemon $httpd."
70                 exit 1
71         fi
72 }
74 stop_httpd () {
75         test -f "$fqgitdir/pid" && kill `cat "$fqgitdir/pid"`
76 }
78 while test $# != 0
79 do
80         case "$1" in
81         --stop|stop)
82                 stop_httpd
83                 exit 0
84                 ;;
85         --start|start)
86                 start_httpd
87                 exit 0
88                 ;;
89         --restart|restart)
90                 stop_httpd
91                 start_httpd
92                 exit 0
93                 ;;
94         -l|--local)
95                 local=true
96                 ;;
97         -d|--httpd)
98                 shift
99                 httpd="$1"
100                 ;;
101         -b|--browser)
102                 shift
103                 browser="$1"
104                 ;;
105         -p|--port)
106                 shift
107                 port="$1"
108                 ;;
109         -m|--module-path)
110                 shift
111                 module_path="$1"
112                 ;;
113         --)
114                 ;;
115         *)
116                 usage
117                 ;;
118         esac
119         shift
120 done
122 mkdir -p "$GIT_DIR/gitweb/tmp"
123 GIT_EXEC_PATH="`git --exec-path`"
124 GIT_DIR="$fqgitdir"
125 export GIT_EXEC_PATH GIT_DIR
128 webrick_conf () {
129         # generate a standalone server script in $fqgitdir/gitweb.
130         cat >"$fqgitdir/gitweb/$httpd.rb" <<EOF
131 require 'webrick'
132 require 'yaml'
133 options = YAML::load_file(ARGV[0])
134 options[:StartCallback] = proc do
135   File.open(options[:PidFile],"w") do |f|
136     f.puts Process.pid
137   end
138 end
139 options[:ServerType] = WEBrick::Daemon
140 server = WEBrick::HTTPServer.new(options)
141 ['INT', 'TERM'].each do |signal|
142   trap(signal) {server.shutdown}
143 end
144 server.start
145 EOF
146         # generate a shell script to invoke the above ruby script,
147         # which assumes _ruby_ is in the user's $PATH. that's _one_
148         # portable way to run ruby, which could be installed anywhere,
149         # really.
150         cat >"$fqgitdir/gitweb/$httpd" <<EOF
151 #!/bin/sh
152 exec ruby "$fqgitdir/gitweb/$httpd.rb" \$*
153 EOF
154         chmod +x "$fqgitdir/gitweb/$httpd"
156         cat >"$conf" <<EOF
157 :Port: $port
158 :DocumentRoot: "$fqgitdir/gitweb"
159 :DirectoryIndex: ["gitweb.cgi"]
160 :PidFile: "$fqgitdir/pid"
161 EOF
162         test "$local" = true && echo ':BindAddress: "127.0.0.1"' >> "$conf"
165 lighttpd_conf () {
166         cat > "$conf" <<EOF
167 server.document-root = "$fqgitdir/gitweb"
168 server.port = $port
169 server.modules = ( "mod_cgi" )
170 server.indexfiles = ( "gitweb.cgi" )
171 server.pid-file = "$fqgitdir/pid"
172 cgi.assign = ( ".cgi" => "" )
173 mimetype.assign = ( ".css" => "text/css" )
174 EOF
175         test x"$local" = xtrue && echo 'server.bind = "127.0.0.1"' >> "$conf"
178 apache2_conf () {
179         test -z "$module_path" && module_path=/usr/lib/apache2/modules
180         mkdir -p "$GIT_DIR/gitweb/logs"
181         bind=
182         test x"$local" = xtrue && bind='127.0.0.1:'
183         echo 'text/css css' > $fqgitdir/mime.types
184         cat > "$conf" <<EOF
185 ServerName "git-instaweb"
186 ServerRoot "$fqgitdir/gitweb"
187 DocumentRoot "$fqgitdir/gitweb"
188 PidFile "$fqgitdir/pid"
189 Listen $bind$port
190 EOF
192         for mod in mime dir; do
193                 if test -e $module_path/mod_${mod}.so; then
194                         echo "LoadModule ${mod}_module " \
195                              "$module_path/mod_${mod}.so" >> "$conf"
196                 fi
197         done
198         cat >> "$conf" <<EOF
199 TypesConfig $fqgitdir/mime.types
200 DirectoryIndex gitweb.cgi
201 EOF
203         # check to see if Dennis Stosberg's mod_perl compatibility patch
204         # (<20060621130708.Gcbc6e5c@leonov.stosberg.net>) has been applied
205         if test -f "$module_path/mod_perl.so" && grep '^our $gitbin' \
206                                 "$GIT_DIR/gitweb/gitweb.cgi" >/dev/null
207         then
208                 # favor mod_perl if available
209                 cat >> "$conf" <<EOF
210 LoadModule perl_module $module_path/mod_perl.so
211 PerlPassEnv GIT_DIR
212 PerlPassEnv GIT_EXEC_DIR
213 <Location /gitweb.cgi>
214         SetHandler perl-script
215         PerlResponseHandler ModPerl::Registry
216         PerlOptions +ParseHeaders
217         Options +ExecCGI
218 </Location>
219 EOF
220         else
221                 # plain-old CGI
222                 list_mods=`echo "$httpd" | sed "s/-f$/-l/"`
223                 $list_mods | grep 'mod_cgi\.c' >/dev/null 2>&1 || \
224                 echo "LoadModule cgi_module $module_path/mod_cgi.so" >> "$conf"
225                 cat >> "$conf" <<EOF
226 AddHandler cgi-script .cgi
227 <Location /gitweb.cgi>
228         Options +ExecCGI
229 </Location>
230 EOF
231         fi
234 script='
235 s#^\(my\|our\) $projectroot =.*#\1 $projectroot = "'$(dirname "$fqgitdir")'";#
236 s#\(my\|our\) $gitbin =.*#\1 $gitbin = "'$GIT_EXEC_PATH'";#
237 s#\(my\|our\) $projects_list =.*#\1 $projects_list = $projectroot;#
238 s#\(my\|our\) $git_temp =.*#\1 $git_temp = "'$fqgitdir/gitweb/tmp'";#'
240 gitweb_cgi () {
241         cat > "$1.tmp" <<\EOFGITWEB
242 @@GITWEB_CGI@@
243 EOFGITWEB
244         sed "$script" "$1.tmp"  > "$1"
245         chmod +x "$1"
246         rm -f "$1.tmp"
249 gitweb_css () {
250         cat > "$1" <<\EOFGITWEB
251 @@GITWEB_CSS@@
252 EOFGITWEB
255 gitweb_cgi "$GIT_DIR/gitweb/gitweb.cgi"
256 gitweb_css "$GIT_DIR/gitweb/gitweb.css"
258 case "$httpd" in
259 *lighttpd*)
260         lighttpd_conf
261         ;;
262 *apache2*)
263         apache2_conf
264         ;;
265 webrick)
266         webrick_conf
267         ;;
268 *)
269         echo "Unknown httpd specified: $httpd"
270         exit 1
271         ;;
272 esac
274 start_httpd
275 url=http://127.0.0.1:$port
276 "$browser" $url || echo $url