rebase -i: support single-letter abbreviations for the actions
[git] / git-instaweb.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2006 Eric Wong
4 #
5 USAGE='[--start] [--stop] [--restart]
6   [--local] [--httpd=<httpd>] [--port=<port>] [--browser=<browser>]
7   [--module-path=<path> (for Apache2 only)]'
8
9 . git-sh-setup
10
11 fqgitdir="$GIT_DIR"
12 local="`git config --bool --get instaweb.local`"
13 httpd="`git config --get instaweb.httpd`"
14 browser="`git config --get instaweb.browser`"
15 port=`git config --get instaweb.port`
16 module_path="`git config --get instaweb.modulepath`"
17
18 conf=$GIT_DIR/gitweb/httpd.conf
19
20 # Defaults:
21
22 # if installed, it doesn't need further configuration (module_path)
23 test -z "$httpd" && httpd='lighttpd -f'
24
25 # probably the most popular browser among gitweb users
26 test -z "$browser" && browser='firefox'
27
28 # any untaken local port will do...
29 test -z "$port" && port=1234
30
31 start_httpd () {
32         httpd_only="`echo $httpd | cut -f1 -d' '`"
33         if test "`expr index $httpd_only /`" -eq '1' || \
34                                 which $httpd_only >/dev/null
35         then
36                 $httpd $fqgitdir/gitweb/httpd.conf
37         else
38                 # many httpds are installed in /usr/sbin or /usr/local/sbin
39                 # these days and those are not in most users $PATHs
40                 for i in /usr/local/sbin /usr/sbin
41                 do
42                         if test -x "$i/$httpd_only"
43                         then
44                                 # don't quote $httpd, there can be
45                                 # arguments to it (-f)
46                                 $i/$httpd "$fqgitdir/gitweb/httpd.conf"
47                                 return
48                         fi
49                 done
50                 echo "$httpd_only not found. Install $httpd_only or use" \
51                      "--httpd to specify another http daemon."
52                 exit 1
53         fi
54         if test $? != 0; then
55                 echo "Could not execute http daemon $httpd."
56                 exit 1
57         fi
58 }
59
60 stop_httpd () {
61         test -f "$fqgitdir/pid" && kill `cat "$fqgitdir/pid"`
62 }
63
64 while test $# != 0
65 do
66         case "$1" in
67         --stop|stop)
68                 stop_httpd
69                 exit 0
70                 ;;
71         --start|start)
72                 start_httpd
73                 exit 0
74                 ;;
75         --restart|restart)
76                 stop_httpd
77                 start_httpd
78                 exit 0
79                 ;;
80         --local|-l)
81                 local=true
82                 ;;
83         -d|--httpd|--httpd=*)
84                 case "$#,$1" in
85                 *,*=*)
86                         httpd=`expr "$1" : '-[^=]*=\(.*\)'` ;;
87                 1,*)
88                         usage ;;
89                 *)
90                         httpd="$2"
91                         shift ;;
92                 esac
93                 ;;
94         -b|--browser|--browser=*)
95                 case "$#,$1" in
96                 *,*=*)
97                         browser=`expr "$1" : '-[^=]*=\(.*\)'` ;;
98                 1,*)
99                         usage ;;
100                 *)
101                         browser="$2"
102                         shift ;;
103                 esac
104                 ;;
105         -p|--port|--port=*)
106                 case "$#,$1" in
107                 *,*=*)
108                         port=`expr "$1" : '-[^=]*=\(.*\)'` ;;
109                 1,*)
110                         usage ;;
111                 *)
112                         port="$2"
113                         shift ;;
114                 esac
115                 ;;
116         -m|--module-path=*|--module-path)
117                 case "$#,$1" in
118                 *,*=*)
119                         module_path=`expr "$1" : '-[^=]*=\(.*\)'` ;;
120                 1,*)
121                         usage ;;
122                 *)
123                         module_path="$2"
124                         shift ;;
125                 esac
126                 ;;
127         *)
128                 usage
129                 ;;
130         esac
131         shift
132 done
133
134 mkdir -p "$GIT_DIR/gitweb/tmp"
135 GIT_EXEC_PATH="`git --exec-path`"
136 GIT_DIR="$fqgitdir"
137 export GIT_EXEC_PATH GIT_DIR
138
139
140 lighttpd_conf () {
141         cat > "$conf" <<EOF
142 server.document-root = "$fqgitdir/gitweb"
143 server.port = $port
144 server.modules = ( "mod_cgi" )
145 server.indexfiles = ( "gitweb.cgi" )
146 server.pid-file = "$fqgitdir/pid"
147 cgi.assign = ( ".cgi" => "" )
148 mimetype.assign = ( ".css" => "text/css" )
149 EOF
150         test "$local" = true && echo 'server.bind = "127.0.0.1"' >> "$conf"
151 }
152
153 apache2_conf () {
154         test -z "$module_path" && module_path=/usr/lib/apache2/modules
155         mkdir -p "$GIT_DIR/gitweb/logs"
156         bind=
157         test "$local" = true && bind='127.0.0.1:'
158         echo 'text/css css' > $fqgitdir/mime.types
159         cat > "$conf" <<EOF
160 ServerName "git-instaweb"
161 ServerRoot "$fqgitdir/gitweb"
162 DocumentRoot "$fqgitdir/gitweb"
163 PidFile "$fqgitdir/pid"
164 Listen $bind$port
165 EOF
166
167         for mod in mime dir; do
168                 if test -e $module_path/mod_${mod}.so; then
169                         echo "LoadModule ${mod}_module " \
170                              "$module_path/mod_${mod}.so" >> "$conf"
171                 fi
172         done
173         cat >> "$conf" <<EOF
174 TypesConfig $fqgitdir/mime.types
175 DirectoryIndex gitweb.cgi
176 EOF
177
178         # check to see if Dennis Stosberg's mod_perl compatibility patch
179         # (<20060621130708.Gcbc6e5c@leonov.stosberg.net>) has been applied
180         if test -f "$module_path/mod_perl.so" && grep '^our $gitbin' \
181                                 "$GIT_DIR/gitweb/gitweb.cgi" >/dev/null
182         then
183                 # favor mod_perl if available
184                 cat >> "$conf" <<EOF
185 LoadModule perl_module $module_path/mod_perl.so
186 PerlPassEnv GIT_DIR
187 PerlPassEnv GIT_EXEC_DIR
188 <Location /gitweb.cgi>
189         SetHandler perl-script
190         PerlResponseHandler ModPerl::Registry
191         PerlOptions +ParseHeaders
192         Options +ExecCGI
193 </Location>
194 EOF
195         else
196                 # plain-old CGI
197                 list_mods=`echo "$httpd" | sed "s/-f$/-l/"`
198                 $list_mods | grep 'mod_cgi\.c' >/dev/null 2>&1 || \
199                 echo "LoadModule cgi_module $module_path/mod_cgi.so" >> "$conf"
200                 cat >> "$conf" <<EOF
201 AddHandler cgi-script .cgi
202 <Location /gitweb.cgi>
203         Options +ExecCGI
204 </Location>
205 EOF
206         fi
207 }
208
209 script='
210 s#^\(my\|our\) $projectroot =.*#\1 $projectroot = "'`dirname $fqgitdir`'";#
211 s#\(my\|our\) $gitbin =.*#\1 $gitbin = "'$GIT_EXEC_PATH'";#
212 s#\(my\|our\) $projects_list =.*#\1 $projects_list = $projectroot;#
213 s#\(my\|our\) $git_temp =.*#\1 $git_temp = "'$fqgitdir/gitweb/tmp'";#'
214
215 gitweb_cgi () {
216         cat > "$1.tmp" <<\EOFGITWEB
217 @@GITWEB_CGI@@
218 EOFGITWEB
219         sed "$script" "$1.tmp"  > "$1"
220         chmod +x "$1"
221         rm -f "$1.tmp"
222 }
223
224 gitweb_css () {
225         cat > "$1" <<\EOFGITWEB
226 @@GITWEB_CSS@@
227 EOFGITWEB
228 }
229
230 gitweb_cgi $GIT_DIR/gitweb/gitweb.cgi
231 gitweb_css $GIT_DIR/gitweb/gitweb.css
232
233 case "$httpd" in
234 *lighttpd*)
235         lighttpd_conf
236         ;;
237 *apache2*)
238         apache2_conf
239         ;;
240 *)
241         echo "Unknown httpd specified: $httpd"
242         exit 1
243         ;;
244 esac
245
246 start_httpd
247 test -z "$browser" && browser=echo
248 url=http://127.0.0.1:$port
249 $browser $url || echo $url