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