Code

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