]>
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="\ | |
1b1dd23f | 9 | git instaweb [options] (--start | --stop | --restart) |
c2db2e0e PH |
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" |
43d60d2e FP |
25 | local="$(git config --bool --get instaweb.local)" |
26 | httpd="$(git config --get instaweb.httpd)" | |
27 | port=$(git config --get instaweb.port) | |
28 | module_path="$(git config --get instaweb.modulepath)" | |
a51d37c1 | 29 | |
9126425d | 30 | conf="$GIT_DIR/gitweb/httpd.conf" |
a51d37c1 EW |
31 | |
32 | # Defaults: | |
33 | ||
82e5a82f | 34 | # if installed, it doesn't need further configuration (module_path) |
a51d37c1 EW |
35 | test -z "$httpd" && httpd='lighttpd -f' |
36 | ||
a51d37c1 EW |
37 | # any untaken local port will do... |
38 | test -z "$port" && port=1234 | |
39 | ||
43d60d2e FP |
40 | resolve_full_httpd () { |
41 | case "$httpd" in | |
42 | *apache2*|*lighttpd*) | |
43 | # ensure that the apache2/lighttpd command ends with "-f" | |
44 | if ! echo "$httpd" | grep -- '-f *$' >/dev/null 2>&1 | |
45 | then | |
46 | httpd="$httpd -f" | |
47 | fi | |
48 | ;; | |
49 | esac | |
50 | ||
51 | httpd_only="$(echo $httpd | cut -f1 -d' ')" | |
f5f30699 | 52 | if case "$httpd_only" in /*) : ;; *) which $httpd_only >/dev/null;; esac |
a51d37c1 | 53 | then |
43d60d2e | 54 | full_httpd=$httpd |
a51d37c1 EW |
55 | else |
56 | # many httpds are installed in /usr/sbin or /usr/local/sbin | |
57 | # these days and those are not in most users $PATHs | |
14b45b66 MD |
58 | # in addition, we may have generated a server script |
59 | # in $fqgitdir/gitweb. | |
60 | for i in /usr/local/sbin /usr/sbin "$fqgitdir/gitweb" | |
a51d37c1 EW |
61 | do |
62 | if test -x "$i/$httpd_only" | |
63 | then | |
43d60d2e | 64 | full_httpd=$i/$httpd |
a51d37c1 EW |
65 | return |
66 | fi | |
67 | done | |
43d60d2e FP |
68 | |
69 | echo >&2 "$httpd_only not found. Install $httpd_only or use" \ | |
70 | "--httpd to specify another httpd daemon." | |
f281e3a1 | 71 | exit 1 |
a51d37c1 | 72 | fi |
43d60d2e FP |
73 | } |
74 | ||
75 | start_httpd () { | |
76 | # here $httpd should have a meaningful value | |
77 | resolve_full_httpd | |
78 | ||
79 | # don't quote $full_httpd, there can be arguments to it (-f) | |
80 | $full_httpd "$fqgitdir/gitweb/httpd.conf" | |
5209eda8 JS |
81 | if test $? != 0; then |
82 | echo "Could not execute http daemon $httpd." | |
83 | exit 1 | |
84 | fi | |
a51d37c1 EW |
85 | } |
86 | ||
87 | stop_httpd () { | |
43d60d2e | 88 | test -f "$fqgitdir/pid" && kill $(cat "$fqgitdir/pid") |
a51d37c1 EW |
89 | } |
90 | ||
822f7c73 | 91 | while test $# != 0 |
a51d37c1 EW |
92 | do |
93 | case "$1" in | |
94 | --stop|stop) | |
95 | stop_httpd | |
96 | exit 0 | |
97 | ;; | |
98 | --start|start) | |
99 | start_httpd | |
100 | exit 0 | |
101 | ;; | |
102 | --restart|restart) | |
103 | stop_httpd | |
104 | start_httpd | |
105 | exit 0 | |
106 | ;; | |
c2db2e0e | 107 | -l|--local) |
a51d37c1 EW |
108 | local=true |
109 | ;; | |
c2db2e0e PH |
110 | -d|--httpd) |
111 | shift | |
112 | httpd="$1" | |
113 | ;; | |
114 | -b|--browser) | |
115 | shift | |
116 | browser="$1" | |
a51d37c1 | 117 | ;; |
c2db2e0e PH |
118 | -p|--port) |
119 | shift | |
120 | port="$1" | |
a51d37c1 | 121 | ;; |
c2db2e0e PH |
122 | -m|--module-path) |
123 | shift | |
124 | module_path="$1" | |
a51d37c1 | 125 | ;; |
c2db2e0e | 126 | --) |
a51d37c1 EW |
127 | ;; |
128 | *) | |
129 | usage | |
130 | ;; | |
131 | esac | |
132 | shift | |
133 | done | |
134 | ||
135 | mkdir -p "$GIT_DIR/gitweb/tmp" | |
43d60d2e | 136 | GIT_EXEC_PATH="$(git --exec-path)" |
a51d37c1 EW |
137 | GIT_DIR="$fqgitdir" |
138 | export GIT_EXEC_PATH GIT_DIR | |
139 | ||
140 | ||
425b78e8 MD |
141 | webrick_conf () { |
142 | # generate a standalone server script in $fqgitdir/gitweb. | |
143 | cat >"$fqgitdir/gitweb/$httpd.rb" <<EOF | |
144 | require 'webrick' | |
145 | require 'yaml' | |
146 | options = YAML::load_file(ARGV[0]) | |
147 | options[:StartCallback] = proc do | |
148 | File.open(options[:PidFile],"w") do |f| | |
149 | f.puts Process.pid | |
150 | end | |
151 | end | |
152 | options[:ServerType] = WEBrick::Daemon | |
153 | server = WEBrick::HTTPServer.new(options) | |
154 | ['INT', 'TERM'].each do |signal| | |
155 | trap(signal) {server.shutdown} | |
156 | end | |
157 | server.start | |
158 | EOF | |
159 | # generate a shell script to invoke the above ruby script, | |
160 | # which assumes _ruby_ is in the user's $PATH. that's _one_ | |
161 | # portable way to run ruby, which could be installed anywhere, | |
162 | # really. | |
163 | cat >"$fqgitdir/gitweb/$httpd" <<EOF | |
164 | #!/bin/sh | |
165 | exec ruby "$fqgitdir/gitweb/$httpd.rb" \$* | |
166 | EOF | |
167 | chmod +x "$fqgitdir/gitweb/$httpd" | |
168 | ||
169 | cat >"$conf" <<EOF | |
170 | :Port: $port | |
171 | :DocumentRoot: "$fqgitdir/gitweb" | |
172 | :DirectoryIndex: ["gitweb.cgi"] | |
173 | :PidFile: "$fqgitdir/pid" | |
174 | EOF | |
175 | test "$local" = true && echo ':BindAddress: "127.0.0.1"' >> "$conf" | |
176 | } | |
177 | ||
a51d37c1 EW |
178 | lighttpd_conf () { |
179 | cat > "$conf" <<EOF | |
180 | server.document-root = "$fqgitdir/gitweb" | |
181 | server.port = $port | |
182 | server.modules = ( "mod_cgi" ) | |
183 | server.indexfiles = ( "gitweb.cgi" ) | |
184 | server.pid-file = "$fqgitdir/pid" | |
185 | cgi.assign = ( ".cgi" => "" ) | |
186 | mimetype.assign = ( ".css" => "text/css" ) | |
187 | EOF | |
9126425d | 188 | test x"$local" = xtrue && echo 'server.bind = "127.0.0.1"' >> "$conf" |
a51d37c1 EW |
189 | } |
190 | ||
191 | apache2_conf () { | |
192 | test -z "$module_path" && module_path=/usr/lib/apache2/modules | |
193 | mkdir -p "$GIT_DIR/gitweb/logs" | |
194 | bind= | |
9126425d | 195 | test x"$local" = xtrue && bind='127.0.0.1:' |
a51d37c1 EW |
196 | echo 'text/css css' > $fqgitdir/mime.types |
197 | cat > "$conf" <<EOF | |
44a167b0 | 198 | ServerName "git-instaweb" |
a51d37c1 EW |
199 | ServerRoot "$fqgitdir/gitweb" |
200 | DocumentRoot "$fqgitdir/gitweb" | |
201 | PidFile "$fqgitdir/pid" | |
202 | Listen $bind$port | |
44a167b0 EW |
203 | EOF |
204 | ||
205 | for mod in mime dir; do | |
206 | if test -e $module_path/mod_${mod}.so; then | |
207 | echo "LoadModule ${mod}_module " \ | |
208 | "$module_path/mod_${mod}.so" >> "$conf" | |
209 | fi | |
210 | done | |
211 | cat >> "$conf" <<EOF | |
a51d37c1 EW |
212 | TypesConfig $fqgitdir/mime.types |
213 | DirectoryIndex gitweb.cgi | |
214 | EOF | |
215 | ||
216 | # check to see if Dennis Stosberg's mod_perl compatibility patch | |
217 | # (<20060621130708.Gcbc6e5c@leonov.stosberg.net>) has been applied | |
218 | if test -f "$module_path/mod_perl.so" && grep '^our $gitbin' \ | |
219 | "$GIT_DIR/gitweb/gitweb.cgi" >/dev/null | |
220 | then | |
221 | # favor mod_perl if available | |
222 | cat >> "$conf" <<EOF | |
223 | LoadModule perl_module $module_path/mod_perl.so | |
224 | PerlPassEnv GIT_DIR | |
225 | PerlPassEnv GIT_EXEC_DIR | |
226 | <Location /gitweb.cgi> | |
227 | SetHandler perl-script | |
228 | PerlResponseHandler ModPerl::Registry | |
229 | PerlOptions +ParseHeaders | |
230 | Options +ExecCGI | |
231 | </Location> | |
232 | EOF | |
233 | else | |
234 | # plain-old CGI | |
43d60d2e FP |
235 | resolve_full_httpd |
236 | list_mods=$(echo "$full_httpd" | sed "s/-f$/-l/") | |
5209eda8 | 237 | $list_mods | grep 'mod_cgi\.c' >/dev/null 2>&1 || \ |
2b5d2d87 | 238 | echo "LoadModule cgi_module $module_path/mod_cgi.so" >> "$conf" |
a51d37c1 | 239 | cat >> "$conf" <<EOF |
a51d37c1 EW |
240 | AddHandler cgi-script .cgi |
241 | <Location /gitweb.cgi> | |
242 | Options +ExecCGI | |
243 | </Location> | |
244 | EOF | |
245 | fi | |
246 | } | |
247 | ||
248 | script=' | |
c5699693 CB |
249 | s#^(my|our) \$projectroot =.*#$1 \$projectroot = "'$(dirname "$fqgitdir")'";#; |
250 | s#(my|our) \$gitbin =.*#$1 \$gitbin = "'$GIT_EXEC_PATH'";#; | |
251 | s#(my|our) \$projects_list =.*#$1 \$projects_list = \$projectroot;#; | |
252 | s#(my|our) \$git_temp =.*#$1 \$git_temp = "'$fqgitdir/gitweb/tmp'";#;' | |
a51d37c1 EW |
253 | |
254 | gitweb_cgi () { | |
255 | cat > "$1.tmp" <<\EOFGITWEB | |
256 | @@GITWEB_CGI@@ | |
257 | EOFGITWEB | |
c5699693 CB |
258 | # Use the configured full path to perl to match the generated |
259 | # scripts' 'hashpling' line | |
260 | "$PERL" -p -e "$script" "$1.tmp" > "$1" | |
a51d37c1 EW |
261 | chmod +x "$1" |
262 | rm -f "$1.tmp" | |
263 | } | |
264 | ||
265 | gitweb_css () { | |
266 | cat > "$1" <<\EOFGITWEB | |
267 | @@GITWEB_CSS@@ | |
268 | EOFGITWEB | |
269 | } | |
270 | ||
9126425d JF |
271 | gitweb_cgi "$GIT_DIR/gitweb/gitweb.cgi" |
272 | gitweb_css "$GIT_DIR/gitweb/gitweb.css" | |
a51d37c1 EW |
273 | |
274 | case "$httpd" in | |
275 | *lighttpd*) | |
276 | lighttpd_conf | |
277 | ;; | |
278 | *apache2*) | |
279 | apache2_conf | |
280 | ;; | |
425b78e8 MD |
281 | webrick) |
282 | webrick_conf | |
283 | ;; | |
a51d37c1 EW |
284 | *) |
285 | echo "Unknown httpd specified: $httpd" | |
286 | exit 1 | |
287 | ;; | |
288 | esac | |
289 | ||
290 | start_httpd | |
5209eda8 | 291 | url=http://127.0.0.1:$port |
2e0c2902 CC |
292 | |
293 | if test -n "$browser"; then | |
294 | git web--browse -b "$browser" $url || echo $url | |
295 | else | |
296 | git web--browse -c "instaweb.browser" $url || echo $url | |
297 | fi |