3 # Copyright (c) 2006 Eric Wong
9 git-instaweb [options] (--start | --stop | --restart)
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)
17 stop stop the web server
18 start start the web server
19 restart restart the web server
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`"
30 conf="$GIT_DIR/gitweb/httpd.conf"
34 # if installed, it doesn't need further configuration (module_path)
35 test -z "$httpd" && httpd='lighttpd -f'
37 # any untaken local port will do...
38 test -z "$port" && port=1234
41 httpd_only="`echo $httpd | cut -f1 -d' '`"
42 if case "$httpd_only" in /*) : ;; *) which $httpd_only >/dev/null;; esac
44 $httpd "$fqgitdir/gitweb/httpd.conf"
46 # many httpds are installed in /usr/sbin or /usr/local/sbin
47 # these days and those are not in most users $PATHs
48 # in addition, we may have generated a server script
49 # in $fqgitdir/gitweb.
50 for i in /usr/local/sbin /usr/sbin "$fqgitdir/gitweb"
52 if test -x "$i/$httpd_only"
54 # don't quote $httpd, there can be
55 # arguments to it (-f)
56 $i/$httpd "$fqgitdir/gitweb/httpd.conf"
60 echo "$httpd_only not found. Install $httpd_only or use" \
61 "--httpd to specify another http daemon."
65 echo "Could not execute http daemon $httpd."
71 test -f "$fqgitdir/pid" && kill `cat "$fqgitdir/pid"`
118 mkdir -p "$GIT_DIR/gitweb/tmp"
119 GIT_EXEC_PATH="`git --exec-path`"
121 export GIT_EXEC_PATH GIT_DIR
125 # generate a standalone server script in $fqgitdir/gitweb.
126 cat >"$fqgitdir/gitweb/$httpd.rb" <<EOF
129 options = YAML::load_file(ARGV[0])
130 options[:StartCallback] = proc do
131 File.open(options[:PidFile],"w") do |f|
135 options[:ServerType] = WEBrick::Daemon
136 server = WEBrick::HTTPServer.new(options)
137 ['INT', 'TERM'].each do |signal|
138 trap(signal) {server.shutdown}
142 # generate a shell script to invoke the above ruby script,
143 # which assumes _ruby_ is in the user's $PATH. that's _one_
144 # portable way to run ruby, which could be installed anywhere,
146 cat >"$fqgitdir/gitweb/$httpd" <<EOF
148 exec ruby "$fqgitdir/gitweb/$httpd.rb" \$*
150 chmod +x "$fqgitdir/gitweb/$httpd"
154 :DocumentRoot: "$fqgitdir/gitweb"
155 :DirectoryIndex: ["gitweb.cgi"]
156 :PidFile: "$fqgitdir/pid"
158 test "$local" = true && echo ':BindAddress: "127.0.0.1"' >> "$conf"
163 server.document-root = "$fqgitdir/gitweb"
165 server.modules = ( "mod_cgi" )
166 server.indexfiles = ( "gitweb.cgi" )
167 server.pid-file = "$fqgitdir/pid"
168 cgi.assign = ( ".cgi" => "" )
169 mimetype.assign = ( ".css" => "text/css" )
171 test x"$local" = xtrue && echo 'server.bind = "127.0.0.1"' >> "$conf"
175 test -z "$module_path" && module_path=/usr/lib/apache2/modules
176 mkdir -p "$GIT_DIR/gitweb/logs"
178 test x"$local" = xtrue && bind='127.0.0.1:'
179 echo 'text/css css' > $fqgitdir/mime.types
181 ServerName "git-instaweb"
182 ServerRoot "$fqgitdir/gitweb"
183 DocumentRoot "$fqgitdir/gitweb"
184 PidFile "$fqgitdir/pid"
188 for mod in mime dir; do
189 if test -e $module_path/mod_${mod}.so; then
190 echo "LoadModule ${mod}_module " \
191 "$module_path/mod_${mod}.so" >> "$conf"
195 TypesConfig $fqgitdir/mime.types
196 DirectoryIndex gitweb.cgi
199 # check to see if Dennis Stosberg's mod_perl compatibility patch
200 # (<20060621130708.Gcbc6e5c@leonov.stosberg.net>) has been applied
201 if test -f "$module_path/mod_perl.so" && grep '^our $gitbin' \
202 "$GIT_DIR/gitweb/gitweb.cgi" >/dev/null
204 # favor mod_perl if available
206 LoadModule perl_module $module_path/mod_perl.so
208 PerlPassEnv GIT_EXEC_DIR
209 <Location /gitweb.cgi>
210 SetHandler perl-script
211 PerlResponseHandler ModPerl::Registry
212 PerlOptions +ParseHeaders
218 list_mods=`echo "$httpd" | sed "s/-f$/-l/"`
219 $list_mods | grep 'mod_cgi\.c' >/dev/null 2>&1 || \
220 echo "LoadModule cgi_module $module_path/mod_cgi.so" >> "$conf"
222 AddHandler cgi-script .cgi
223 <Location /gitweb.cgi>
231 s#^(my|our) \$projectroot =.*#$1 \$projectroot = "'$(dirname "$fqgitdir")'";#;
232 s#(my|our) \$gitbin =.*#$1 \$gitbin = "'$GIT_EXEC_PATH'";#;
233 s#(my|our) \$projects_list =.*#$1 \$projects_list = \$projectroot;#;
234 s#(my|our) \$git_temp =.*#$1 \$git_temp = "'$fqgitdir/gitweb/tmp'";#;'
237 cat > "$1.tmp" <<\EOFGITWEB
240 # Use the configured full path to perl to match the generated
241 # scripts' 'hashpling' line
242 "$PERL" -p -e "$script" "$1.tmp" > "$1"
248 cat > "$1" <<\EOFGITWEB
253 gitweb_cgi "$GIT_DIR/gitweb/gitweb.cgi"
254 gitweb_css "$GIT_DIR/gitweb/gitweb.css"
267 echo "Unknown httpd specified: $httpd"
273 url=http://127.0.0.1:$port
275 if test -n "$browser"; then
276 git web--browse -b "$browser" $url || echo $url
278 git web--browse -c "instaweb.browser" $url || echo $url