]>
Commit | Line | Data |
---|---|---|
a51d37c1 EW |
1 | #!/bin/sh |
2 | # | |
3 | # Copyright (c) 2006 Eric Wong | |
4 | # | |
c2db2e0e | 5 | |
c5699693 | 6 | PERL='@@PERL@@' |
c2db2e0e PH |
7 | OPTIONS_KEEPDASHDASH= |
8 | OPTIONS_SPEC="\ | |
9 | git-instaweb [options] (--start | --stop | --restart) | |
10 | -- | |
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 | |
20 | " | |
a51d37c1 EW |
21 | |
22 | . git-sh-setup | |
23 | ||
b2bc9a30 | 24 | fqgitdir="$GIT_DIR" |
e0d10e1c TP |
25 | local="`git config --bool --get instaweb.local`" |
26 | httpd="`git config --get instaweb.httpd`" | |
27 | browser="`git config --get instaweb.browser`" | |
d3a866bc | 28 | test -z "$browser" && browser="`git config --get web.browser`" |
e0d10e1c TP |
29 | port=`git config --get instaweb.port` |
30 | module_path="`git config --get instaweb.modulepath`" | |
a51d37c1 | 31 | |
9126425d | 32 | conf="$GIT_DIR/gitweb/httpd.conf" |
a51d37c1 EW |
33 | |
34 | # Defaults: | |
35 | ||
82e5a82f | 36 | # if installed, it doesn't need further configuration (module_path) |
a51d37c1 EW |
37 | test -z "$httpd" && httpd='lighttpd -f' |
38 | ||
39 | # probably the most popular browser among gitweb users | |
40 | test -z "$browser" && browser='firefox' | |
41 | ||
42 | # any untaken local port will do... | |
43 | test -z "$port" && port=1234 | |
44 | ||
45 | start_httpd () { | |
46 | httpd_only="`echo $httpd | cut -f1 -d' '`" | |
f5f30699 | 47 | if case "$httpd_only" in /*) : ;; *) which $httpd_only >/dev/null;; esac |
a51d37c1 | 48 | then |
9126425d | 49 | $httpd "$fqgitdir/gitweb/httpd.conf" |
a51d37c1 EW |
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 | |
14b45b66 MD |
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" | |
a51d37c1 EW |
56 | do |
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 | |
63 | fi | |
64 | done | |
f281e3a1 FK |
65 | echo "$httpd_only not found. Install $httpd_only or use" \ |
66 | "--httpd to specify another http daemon." | |
67 | exit 1 | |
a51d37c1 | 68 | fi |
5209eda8 JS |
69 | if test $? != 0; then |
70 | echo "Could not execute http daemon $httpd." | |
71 | exit 1 | |
72 | fi | |
a51d37c1 EW |
73 | } |
74 | ||
75 | stop_httpd () { | |
76 | test -f "$fqgitdir/pid" && kill `cat "$fqgitdir/pid"` | |
77 | } | |
78 | ||
822f7c73 | 79 | while test $# != 0 |
a51d37c1 EW |
80 | do |
81 | case "$1" in | |
82 | --stop|stop) | |
83 | stop_httpd | |
84 | exit 0 | |
85 | ;; | |
86 | --start|start) | |
87 | start_httpd | |
88 | exit 0 | |
89 | ;; | |
90 | --restart|restart) | |
91 | stop_httpd | |
92 | start_httpd | |
93 | exit 0 | |
94 | ;; | |
c2db2e0e | 95 | -l|--local) |
a51d37c1 EW |
96 | local=true |
97 | ;; | |
c2db2e0e PH |
98 | -d|--httpd) |
99 | shift | |
100 | httpd="$1" | |
101 | ;; | |
102 | -b|--browser) | |
103 | shift | |
104 | browser="$1" | |
a51d37c1 | 105 | ;; |
c2db2e0e PH |
106 | -p|--port) |
107 | shift | |
108 | port="$1" | |
a51d37c1 | 109 | ;; |
c2db2e0e PH |
110 | -m|--module-path) |
111 | shift | |
112 | module_path="$1" | |
a51d37c1 | 113 | ;; |
c2db2e0e | 114 | --) |
a51d37c1 EW |
115 | ;; |
116 | *) | |
117 | usage | |
118 | ;; | |
119 | esac | |
120 | shift | |
121 | done | |
122 | ||
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 | |
127 | ||
128 | ||
425b78e8 MD |
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 | |
138 | end | |
139 | end | |
140 | options[:ServerType] = WEBrick::Daemon | |
141 | server = WEBrick::HTTPServer.new(options) | |
142 | ['INT', 'TERM'].each do |signal| | |
143 | trap(signal) {server.shutdown} | |
144 | end | |
145 | server.start | |
146 | EOF | |
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" \$* | |
154 | EOF | |
155 | chmod +x "$fqgitdir/gitweb/$httpd" | |
156 | ||
157 | cat >"$conf" <<EOF | |
158 | :Port: $port | |
159 | :DocumentRoot: "$fqgitdir/gitweb" | |
160 | :DirectoryIndex: ["gitweb.cgi"] | |
161 | :PidFile: "$fqgitdir/pid" | |
162 | EOF | |
163 | test "$local" = true && echo ':BindAddress: "127.0.0.1"' >> "$conf" | |
164 | } | |
165 | ||
a51d37c1 EW |
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" ) | |
175 | EOF | |
9126425d | 176 | test x"$local" = xtrue && echo 'server.bind = "127.0.0.1"' >> "$conf" |
a51d37c1 EW |
177 | } |
178 | ||
179 | apache2_conf () { | |
180 | test -z "$module_path" && module_path=/usr/lib/apache2/modules | |
181 | mkdir -p "$GIT_DIR/gitweb/logs" | |
182 | bind= | |
9126425d | 183 | test x"$local" = xtrue && bind='127.0.0.1:' |
a51d37c1 EW |
184 | echo 'text/css css' > $fqgitdir/mime.types |
185 | cat > "$conf" <<EOF | |
44a167b0 | 186 | ServerName "git-instaweb" |
a51d37c1 EW |
187 | ServerRoot "$fqgitdir/gitweb" |
188 | DocumentRoot "$fqgitdir/gitweb" | |
189 | PidFile "$fqgitdir/pid" | |
190 | Listen $bind$port | |
44a167b0 EW |
191 | EOF |
192 | ||
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" | |
197 | fi | |
198 | done | |
199 | cat >> "$conf" <<EOF | |
a51d37c1 EW |
200 | TypesConfig $fqgitdir/mime.types |
201 | DirectoryIndex gitweb.cgi | |
202 | EOF | |
203 | ||
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> | |
220 | EOF | |
221 | else | |
222 | # plain-old CGI | |
2b5d2d87 | 223 | list_mods=`echo "$httpd" | sed "s/-f$/-l/"` |
5209eda8 | 224 | $list_mods | grep 'mod_cgi\.c' >/dev/null 2>&1 || \ |
2b5d2d87 | 225 | echo "LoadModule cgi_module $module_path/mod_cgi.so" >> "$conf" |
a51d37c1 | 226 | cat >> "$conf" <<EOF |
a51d37c1 EW |
227 | AddHandler cgi-script .cgi |
228 | <Location /gitweb.cgi> | |
229 | Options +ExecCGI | |
230 | </Location> | |
231 | EOF | |
232 | fi | |
233 | } | |
234 | ||
235 | script=' | |
c5699693 CB |
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'";#;' | |
a51d37c1 EW |
240 | |
241 | gitweb_cgi () { | |
242 | cat > "$1.tmp" <<\EOFGITWEB | |
243 | @@GITWEB_CGI@@ | |
244 | EOFGITWEB | |
c5699693 CB |
245 | # Use the configured full path to perl to match the generated |
246 | # scripts' 'hashpling' line | |
247 | "$PERL" -p -e "$script" "$1.tmp" > "$1" | |
a51d37c1 EW |
248 | chmod +x "$1" |
249 | rm -f "$1.tmp" | |
250 | } | |
251 | ||
252 | gitweb_css () { | |
253 | cat > "$1" <<\EOFGITWEB | |
254 | @@GITWEB_CSS@@ | |
255 | EOFGITWEB | |
256 | } | |
257 | ||
9126425d JF |
258 | gitweb_cgi "$GIT_DIR/gitweb/gitweb.cgi" |
259 | gitweb_css "$GIT_DIR/gitweb/gitweb.css" | |
a51d37c1 EW |
260 | |
261 | case "$httpd" in | |
262 | *lighttpd*) | |
263 | lighttpd_conf | |
264 | ;; | |
265 | *apache2*) | |
266 | apache2_conf | |
267 | ;; | |
425b78e8 MD |
268 | webrick) |
269 | webrick_conf | |
270 | ;; | |
a51d37c1 EW |
271 | *) |
272 | echo "Unknown httpd specified: $httpd" | |
273 | exit 1 | |
274 | ;; | |
275 | esac | |
276 | ||
ab989adf CC |
277 | init_browser_path() { |
278 | browser_path="`git config browser.$1.path`" | |
279 | test -z "$browser_path" && browser_path="$1" | |
280 | } | |
281 | ||
a51d37c1 | 282 | start_httpd |
5209eda8 | 283 | url=http://127.0.0.1:$port |
ab989adf CC |
284 | test -n "$browser" && { |
285 | init_browser_path "$browser" | |
286 | "$browser_path" $url | |
287 | } || echo $url |