get_shallow_commits: Avoid memory leak if a commit has been reached already.
[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 case "$GIT_DIR" in
12 /*)
13         fqgitdir="$GIT_DIR" ;;
14 *)
15         fqgitdir="$PWD/$GIT_DIR" ;;
16 esac
17
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`"
23
24 conf=$GIT_DIR/gitweb/httpd.conf
25
26 # Defaults:
27
28 # if installed, it doesn't need further configuration (module_path)
29 test -z "$httpd" && httpd='lighttpd -f'
30
31 # probably the most popular browser among gitweb users
32 test -z "$browser" && browser='firefox'
33
34 # any untaken local port will do...
35 test -z "$port" && port=1234
36
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 }
62
63 stop_httpd () {
64         test -f "$fqgitdir/pid" && kill `cat "$fqgitdir/pid"`
65 }
66
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
136
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
141
142
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"
154 }
155
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 ServerRoot "$fqgitdir/gitweb"
164 DocumentRoot "$fqgitdir/gitweb"
165 PidFile "$fqgitdir/pid"
166 Listen $bind$port
167 TypesConfig $fqgitdir/mime.types
168 DirectoryIndex gitweb.cgi
169 EOF
170
171         # check to see if Dennis Stosberg's mod_perl compatibility patch
172         # (<20060621130708.Gcbc6e5c@leonov.stosberg.net>) has been applied
173         if test -f "$module_path/mod_perl.so" && grep '^our $gitbin' \
174                                 "$GIT_DIR/gitweb/gitweb.cgi" >/dev/null
175         then
176                 # favor mod_perl if available
177                 cat >> "$conf" <<EOF
178 LoadModule perl_module $module_path/mod_perl.so
179 PerlPassEnv GIT_DIR
180 PerlPassEnv GIT_EXEC_DIR
181 <Location /gitweb.cgi>
182         SetHandler perl-script
183         PerlResponseHandler ModPerl::Registry
184         PerlOptions +ParseHeaders
185         Options +ExecCGI
186 </Location>
187 EOF
188         else
189                 # plain-old CGI
190                 list_mods=`echo "$httpd" | sed "s/-f$/-l/"`
191                 $list_mods | grep 'mod_cgi\.c' >/dev/null 2>&1 || \
192                 echo "LoadModule cgi_module $module_path/mod_cgi.so" >> "$conf"
193                 cat >> "$conf" <<EOF
194 AddHandler cgi-script .cgi
195 <Location /gitweb.cgi>
196         Options +ExecCGI
197 </Location>
198 EOF
199         fi
200 }
201
202 script='
203 s#^\(my\|our\) $projectroot =.*#\1 $projectroot = "'`dirname $fqgitdir`'";#
204 s#\(my\|our\) $gitbin =.*#\1 $gitbin = "'$GIT_EXEC_PATH'";#
205 s#\(my\|our\) $projects_list =.*#\1 $projects_list = $projectroot;#
206 s#\(my\|our\) $git_temp =.*#\1 $git_temp = "'$fqgitdir/gitweb/tmp'";#'
207
208 gitweb_cgi () {
209         cat > "$1.tmp" <<\EOFGITWEB
210 @@GITWEB_CGI@@
211 EOFGITWEB
212         sed "$script" "$1.tmp"  > "$1"
213         chmod +x "$1"
214         rm -f "$1.tmp"
215 }
216
217 gitweb_css () {
218         cat > "$1" <<\EOFGITWEB
219 @@GITWEB_CSS@@
220 EOFGITWEB
221 }
222
223 gitweb_cgi $GIT_DIR/gitweb/gitweb.cgi
224 gitweb_css $GIT_DIR/gitweb/gitweb.css
225
226 case "$httpd" in
227 *lighttpd*)
228         lighttpd_conf
229         ;;
230 *apache2*)
231         apache2_conf
232         ;;
233 *)
234         echo "Unknown httpd specified: $httpd"
235         exit 1
236         ;;
237 esac
238
239 start_httpd
240 test -z "$browser" && browser=echo
241 url=http://127.0.0.1:$port
242 $browser $url || echo $url