Eric Wong | a51d37c | 2006-07-01 15:14:14 -0700 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # |
| 3 | # Copyright (c) 2006 Eric Wong |
| 4 | # |
Pierre Habouzit | c2db2e0 | 2007-11-04 11:30:59 +0100 | [diff] [blame] | 5 | |
Charles Bailey | c569969 | 2007-12-19 12:25:27 +0000 | [diff] [blame] | 6 | PERL='@@PERL@@' |
Pierre Habouzit | c2db2e0 | 2007-11-04 11:30:59 +0100 | [diff] [blame] | 7 | OPTIONS_KEEPDASHDASH= |
Nicolas Vigier | 51ba8ce | 2014-02-01 02:17:59 +0000 | [diff] [blame] | 8 | OPTIONS_STUCKLONG= |
Pierre Habouzit | c2db2e0 | 2007-11-04 11:30:59 +0100 | [diff] [blame] | 9 | OPTIONS_SPEC="\ |
Stephan Beyer | 1b1dd23 | 2008-07-13 15:36:15 +0200 | [diff] [blame] | 10 | git instaweb [options] (--start | --stop | --restart) |
Pierre Habouzit | c2db2e0 | 2007-11-04 11:30:59 +0100 | [diff] [blame] | 11 | -- |
| 12 | l,local only bind on 127.0.0.1 |
| 13 | p,port= the port to bind to |
| 14 | d,httpd= the command to launch |
| 15 | b,browser= the browser to launch |
| 16 | m,module-path= the module path (only needed for apache2) |
| 17 | Action |
| 18 | stop stop the web server |
| 19 | start start the web server |
| 20 | restart restart the web server |
| 21 | " |
Eric Wong | a51d37c | 2006-07-01 15:14:14 -0700 | [diff] [blame] | 22 | |
Kyle J. McKay | 130e475 | 2015-03-07 21:04:09 -0800 | [diff] [blame] | 23 | SUBDIRECTORY_OK=Yes |
Eric Wong | a51d37c | 2006-07-01 15:14:14 -0700 | [diff] [blame] | 24 | . git-sh-setup |
| 25 | |
David Kastrup | b2bc9a3 | 2007-08-11 15:36:28 +0200 | [diff] [blame] | 26 | fqgitdir="$GIT_DIR" |
Flavio Poletti | 43d60d2 | 2008-06-12 23:54:55 +0200 | [diff] [blame] | 27 | local="$(git config --bool --get instaweb.local)" |
| 28 | httpd="$(git config --get instaweb.httpd)" |
Pavan Kumar Sunkara | c0cb4ed | 2010-05-28 11:55:52 +0530 | [diff] [blame] | 29 | root="$(git config --get instaweb.gitwebdir)" |
Flavio Poletti | 43d60d2 | 2008-06-12 23:54:55 +0200 | [diff] [blame] | 30 | port=$(git config --get instaweb.port) |
| 31 | module_path="$(git config --get instaweb.modulepath)" |
Jakub Narebski | c0175f9 | 2011-06-23 22:59:26 +0200 | [diff] [blame] | 32 | action="browse" |
Eric Wong | a51d37c | 2006-07-01 15:14:14 -0700 | [diff] [blame] | 33 | |
Jonas Fonseca | 9126425 | 2007-11-09 00:21:42 +0100 | [diff] [blame] | 34 | conf="$GIT_DIR/gitweb/httpd.conf" |
Eric Wong | a51d37c | 2006-07-01 15:14:14 -0700 | [diff] [blame] | 35 | |
| 36 | # Defaults: |
| 37 | |
Pavel Roskin | 82e5a82 | 2006-07-10 01:50:18 -0400 | [diff] [blame] | 38 | # if installed, it doesn't need further configuration (module_path) |
Eric Wong | a51d37c | 2006-07-01 15:14:14 -0700 | [diff] [blame] | 39 | test -z "$httpd" && httpd='lighttpd -f' |
| 40 | |
Pavan Kumar Sunkara | c0cb4ed | 2010-05-28 11:55:52 +0530 | [diff] [blame] | 41 | # Default is @@GITWEBDIR@@ |
| 42 | test -z "$root" && root='@@GITWEBDIR@@' |
| 43 | |
Eric Wong | a51d37c | 2006-07-01 15:14:14 -0700 | [diff] [blame] | 44 | # any untaken local port will do... |
| 45 | test -z "$port" && port=1234 |
| 46 | |
Flavio Poletti | 43d60d2 | 2008-06-12 23:54:55 +0200 | [diff] [blame] | 47 | resolve_full_httpd () { |
| 48 | case "$httpd" in |
Dan McGee | 4bdf859 | 2010-06-30 07:29:08 -0500 | [diff] [blame] | 49 | *apache2*|*lighttpd*|*httpd*) |
| 50 | # yes, *httpd* covers *lighttpd* above, but it is there for clarity |
Flavio Poletti | 43d60d2 | 2008-06-12 23:54:55 +0200 | [diff] [blame] | 51 | # ensure that the apache2/lighttpd command ends with "-f" |
Junio C Hamano | e1622bf | 2009-11-23 15:56:32 -0800 | [diff] [blame] | 52 | if ! echo "$httpd" | sane_grep -- '-f *$' >/dev/null 2>&1 |
Flavio Poletti | 43d60d2 | 2008-06-12 23:54:55 +0200 | [diff] [blame] | 53 | then |
| 54 | httpd="$httpd -f" |
| 55 | fi |
| 56 | ;; |
Jakub Narebski | 7864698 | 2010-05-28 21:11:25 +0200 | [diff] [blame] | 57 | *plackup*) |
| 58 | # server is started by running via generated gitweb.psgi in $fqgitdir/gitweb |
| 59 | full_httpd="$fqgitdir/gitweb/gitweb.psgi" |
| 60 | httpd_only="${httpd%% *}" # cut on first space |
| 61 | return |
| 62 | ;; |
Eric Wong | 422bff2 | 2010-08-04 09:51:25 +0000 | [diff] [blame] | 63 | *webrick*) |
Eric Wong | f46e130 | 2010-08-05 08:35:45 +0000 | [diff] [blame] | 64 | # server is started by running via generated webrick.rb in |
Eric Wong | 422bff2 | 2010-08-04 09:51:25 +0000 | [diff] [blame] | 65 | # $fqgitdir/gitweb |
Eric Wong | f46e130 | 2010-08-05 08:35:45 +0000 | [diff] [blame] | 66 | full_httpd="$fqgitdir/gitweb/webrick.rb" |
Eric Wong | 422bff2 | 2010-08-04 09:51:25 +0000 | [diff] [blame] | 67 | httpd_only="${httpd%% *}" # cut on first space |
| 68 | return |
| 69 | ;; |
Flavio Poletti | 43d60d2 | 2008-06-12 23:54:55 +0200 | [diff] [blame] | 70 | esac |
| 71 | |
| 72 | httpd_only="$(echo $httpd | cut -f1 -d' ')" |
Ramsay Jones | e47eec8 | 2009-03-09 18:31:55 +0000 | [diff] [blame] | 73 | if case "$httpd_only" in /*) : ;; *) which $httpd_only >/dev/null 2>&1;; esac |
Eric Wong | a51d37c | 2006-07-01 15:14:14 -0700 | [diff] [blame] | 74 | then |
Flavio Poletti | 43d60d2 | 2008-06-12 23:54:55 +0200 | [diff] [blame] | 75 | full_httpd=$httpd |
Eric Wong | a51d37c | 2006-07-01 15:14:14 -0700 | [diff] [blame] | 76 | else |
| 77 | # many httpds are installed in /usr/sbin or /usr/local/sbin |
| 78 | # these days and those are not in most users $PATHs |
Mike Dalessio | 14b45b6 | 2007-10-06 13:29:48 -0400 | [diff] [blame] | 79 | # in addition, we may have generated a server script |
| 80 | # in $fqgitdir/gitweb. |
Pavan Kumar Sunkara | c0cb4ed | 2010-05-28 11:55:52 +0530 | [diff] [blame] | 81 | for i in /usr/local/sbin /usr/sbin "$root" "$fqgitdir/gitweb" |
Eric Wong | a51d37c | 2006-07-01 15:14:14 -0700 | [diff] [blame] | 82 | do |
| 83 | if test -x "$i/$httpd_only" |
| 84 | then |
Flavio Poletti | 43d60d2 | 2008-06-12 23:54:55 +0200 | [diff] [blame] | 85 | full_httpd=$i/$httpd |
Eric Wong | a51d37c | 2006-07-01 15:14:14 -0700 | [diff] [blame] | 86 | return |
| 87 | fi |
| 88 | done |
Flavio Poletti | 43d60d2 | 2008-06-12 23:54:55 +0200 | [diff] [blame] | 89 | |
| 90 | echo >&2 "$httpd_only not found. Install $httpd_only or use" \ |
| 91 | "--httpd to specify another httpd daemon." |
Fredrik Kuivinen | f281e3a | 2007-01-06 11:40:06 +0100 | [diff] [blame] | 92 | exit 1 |
Eric Wong | a51d37c | 2006-07-01 15:14:14 -0700 | [diff] [blame] | 93 | fi |
Flavio Poletti | 43d60d2 | 2008-06-12 23:54:55 +0200 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | start_httpd () { |
Stephen Boyd | 0b624b4 | 2009-11-22 23:09:12 -0800 | [diff] [blame] | 97 | if test -f "$fqgitdir/pid"; then |
| 98 | say "Instance already running. Restarting..." |
| 99 | stop_httpd |
| 100 | fi |
| 101 | |
Flavio Poletti | 43d60d2 | 2008-06-12 23:54:55 +0200 | [diff] [blame] | 102 | # here $httpd should have a meaningful value |
| 103 | resolve_full_httpd |
Jakub Narebski | 5ad6d38 | 2011-06-23 23:01:03 +0200 | [diff] [blame] | 104 | mkdir -p "$fqgitdir/gitweb/$httpd_only" |
| 105 | conf="$fqgitdir/gitweb/$httpd_only.conf" |
| 106 | |
| 107 | # generate correct config file if it doesn't exist |
| 108 | test -f "$conf" || configure_httpd |
| 109 | test -f "$fqgitdir/gitweb/gitweb_config.perl" || gitweb_conf |
Flavio Poletti | 43d60d2 | 2008-06-12 23:54:55 +0200 | [diff] [blame] | 110 | |
| 111 | # don't quote $full_httpd, there can be arguments to it (-f) |
Wilhansen Li | 0ded475 | 2009-08-21 22:24:48 +0800 | [diff] [blame] | 112 | case "$httpd" in |
Jakub Narebski | 7864698 | 2010-05-28 21:11:25 +0200 | [diff] [blame] | 113 | *mongoose*|*plackup*) |
| 114 | #These servers don't have a daemon mode so we'll have to fork it |
Jakub Narebski | 48bf76c | 2011-06-23 21:56:37 +0200 | [diff] [blame] | 115 | $full_httpd "$conf" & |
Wilhansen Li | 0ded475 | 2009-08-21 22:24:48 +0800 | [diff] [blame] | 116 | #Save the pid before doing anything else (we'll print it later) |
| 117 | pid=$! |
| 118 | |
| 119 | if test $? != 0; then |
| 120 | echo "Could not execute http daemon $httpd." |
| 121 | exit 1 |
| 122 | fi |
| 123 | |
| 124 | cat > "$fqgitdir/pid" <<EOF |
| 125 | $pid |
| 126 | EOF |
| 127 | ;; |
| 128 | *) |
Jakub Narebski | 48bf76c | 2011-06-23 21:56:37 +0200 | [diff] [blame] | 129 | $full_httpd "$conf" |
Wilhansen Li | 0ded475 | 2009-08-21 22:24:48 +0800 | [diff] [blame] | 130 | if test $? != 0; then |
| 131 | echo "Could not execute http daemon $httpd." |
| 132 | exit 1 |
| 133 | fi |
| 134 | ;; |
| 135 | esac |
Eric Wong | a51d37c | 2006-07-01 15:14:14 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | stop_httpd () { |
Flavio Poletti | 43d60d2 | 2008-06-12 23:54:55 +0200 | [diff] [blame] | 139 | test -f "$fqgitdir/pid" && kill $(cat "$fqgitdir/pid") |
Jakub Narebski | d112762 | 2010-05-28 21:11:23 +0200 | [diff] [blame] | 140 | rm -f "$fqgitdir/pid" |
Eric Wong | a51d37c | 2006-07-01 15:14:14 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Jakub Narebski | d94775e | 2010-05-28 21:11:24 +0200 | [diff] [blame] | 143 | httpd_is_ready () { |
| 144 | "$PERL" -MIO::Socket::INET -e " |
| 145 | local \$| = 1; # turn on autoflush |
| 146 | exit if (IO::Socket::INET->new('127.0.0.1:$port')); |
| 147 | print 'Waiting for \'$httpd\' to start ..'; |
| 148 | do { |
| 149 | print '.'; |
| 150 | sleep(1); |
| 151 | } until (IO::Socket::INET->new('127.0.0.1:$port')); |
| 152 | print qq! (done)\n!; |
| 153 | " |
| 154 | } |
| 155 | |
David Kastrup | 822f7c7 | 2007-09-23 22:42:08 +0200 | [diff] [blame] | 156 | while test $# != 0 |
Eric Wong | a51d37c | 2006-07-01 15:14:14 -0700 | [diff] [blame] | 157 | do |
| 158 | case "$1" in |
| 159 | --stop|stop) |
Jakub Narebski | c0175f9 | 2011-06-23 22:59:26 +0200 | [diff] [blame] | 160 | action="stop" |
Eric Wong | a51d37c | 2006-07-01 15:14:14 -0700 | [diff] [blame] | 161 | ;; |
| 162 | --start|start) |
Jakub Narebski | c0175f9 | 2011-06-23 22:59:26 +0200 | [diff] [blame] | 163 | action="start" |
Eric Wong | a51d37c | 2006-07-01 15:14:14 -0700 | [diff] [blame] | 164 | ;; |
| 165 | --restart|restart) |
Jakub Narebski | c0175f9 | 2011-06-23 22:59:26 +0200 | [diff] [blame] | 166 | action="restart" |
Eric Wong | a51d37c | 2006-07-01 15:14:14 -0700 | [diff] [blame] | 167 | ;; |
Pierre Habouzit | c2db2e0 | 2007-11-04 11:30:59 +0100 | [diff] [blame] | 168 | -l|--local) |
Eric Wong | a51d37c | 2006-07-01 15:14:14 -0700 | [diff] [blame] | 169 | local=true |
| 170 | ;; |
Pierre Habouzit | c2db2e0 | 2007-11-04 11:30:59 +0100 | [diff] [blame] | 171 | -d|--httpd) |
| 172 | shift |
| 173 | httpd="$1" |
Eric Wong | a51d37c | 2006-07-01 15:14:14 -0700 | [diff] [blame] | 174 | ;; |
Pierre Habouzit | c2db2e0 | 2007-11-04 11:30:59 +0100 | [diff] [blame] | 175 | -b|--browser) |
| 176 | shift |
| 177 | browser="$1" |
Eric Wong | a51d37c | 2006-07-01 15:14:14 -0700 | [diff] [blame] | 178 | ;; |
Pierre Habouzit | c2db2e0 | 2007-11-04 11:30:59 +0100 | [diff] [blame] | 179 | -p|--port) |
| 180 | shift |
| 181 | port="$1" |
Eric Wong | a51d37c | 2006-07-01 15:14:14 -0700 | [diff] [blame] | 182 | ;; |
Pierre Habouzit | c2db2e0 | 2007-11-04 11:30:59 +0100 | [diff] [blame] | 183 | -m|--module-path) |
| 184 | shift |
| 185 | module_path="$1" |
| 186 | ;; |
| 187 | --) |
Eric Wong | a51d37c | 2006-07-01 15:14:14 -0700 | [diff] [blame] | 188 | ;; |
| 189 | *) |
| 190 | usage |
| 191 | ;; |
| 192 | esac |
| 193 | shift |
| 194 | done |
| 195 | |
| 196 | mkdir -p "$GIT_DIR/gitweb/tmp" |
Flavio Poletti | 43d60d2 | 2008-06-12 23:54:55 +0200 | [diff] [blame] | 197 | GIT_EXEC_PATH="$(git --exec-path)" |
Eric Wong | a51d37c | 2006-07-01 15:14:14 -0700 | [diff] [blame] | 198 | GIT_DIR="$fqgitdir" |
Pavan Kumar Sunkara | c0cb4ed | 2010-05-28 11:55:52 +0530 | [diff] [blame] | 199 | GITWEB_CONFIG="$fqgitdir/gitweb/gitweb_config.perl" |
| 200 | export GIT_EXEC_PATH GIT_DIR GITWEB_CONFIG |
Eric Wong | a51d37c | 2006-07-01 15:14:14 -0700 | [diff] [blame] | 201 | |
Mike Dalessio | 425b78e | 2007-10-06 13:29:49 -0400 | [diff] [blame] | 202 | webrick_conf () { |
Eric Wong | 422bff2 | 2010-08-04 09:51:25 +0000 | [diff] [blame] | 203 | # webrick seems to have no way of passing arbitrary environment |
| 204 | # variables to the underlying CGI executable, so we wrap the |
| 205 | # actual gitweb.cgi using a shell script to force it |
| 206 | wrapper="$fqgitdir/gitweb/$httpd/wrapper.sh" |
| 207 | cat > "$wrapper" <<EOF |
Kyle J. McKay | ff7a9dc | 2015-03-07 21:05:38 -0800 | [diff] [blame] | 208 | #!@SHELL_PATH@ |
Eric Wong | 422bff2 | 2010-08-04 09:51:25 +0000 | [diff] [blame] | 209 | # we use this shell script wrapper around the real gitweb.cgi since |
| 210 | # there appears to be no other way to pass arbitrary environment variables |
| 211 | # into the CGI process |
| 212 | GIT_EXEC_PATH=$GIT_EXEC_PATH GIT_DIR=$GIT_DIR GITWEB_CONFIG=$GITWEB_CONFIG |
| 213 | export GIT_EXEC_PATH GIT_DIR GITWEB_CONFIG |
| 214 | exec $root/gitweb.cgi |
| 215 | EOF |
| 216 | chmod +x "$wrapper" |
| 217 | |
| 218 | # This assumes _ruby_ is in the user's $PATH. that's _one_ |
| 219 | # portable way to run ruby, which could be installed anywhere, really. |
Mike Dalessio | 425b78e | 2007-10-06 13:29:49 -0400 | [diff] [blame] | 220 | # generate a standalone server script in $fqgitdir/gitweb. |
| 221 | cat >"$fqgitdir/gitweb/$httpd.rb" <<EOF |
Eric Wong | f46e130 | 2010-08-05 08:35:45 +0000 | [diff] [blame] | 222 | #!/usr/bin/env ruby |
Mike Dalessio | 425b78e | 2007-10-06 13:29:49 -0400 | [diff] [blame] | 223 | require 'webrick' |
Eric Wong | e9323e7 | 2010-08-05 08:46:04 +0000 | [diff] [blame] | 224 | require 'logger' |
Eric Wong | f46e130 | 2010-08-05 08:35:45 +0000 | [diff] [blame] | 225 | options = { |
| 226 | :Port => $port, |
| 227 | :DocumentRoot => "$root", |
Eric Wong | e9323e7 | 2010-08-05 08:46:04 +0000 | [diff] [blame] | 228 | :Logger => Logger.new('$fqgitdir/gitweb/error.log'), |
| 229 | :AccessLog => [ |
| 230 | [ Logger.new('$fqgitdir/gitweb/access.log'), |
| 231 | WEBrick::AccessLog::COMBINED_LOG_FORMAT ] |
| 232 | ], |
Eric Wong | f46e130 | 2010-08-05 08:35:45 +0000 | [diff] [blame] | 233 | :DirectoryIndex => ["gitweb.cgi"], |
| 234 | :CGIInterpreter => "$wrapper", |
| 235 | :StartCallback => lambda do |
| 236 | File.open("$fqgitdir/pid", "w") { |f| f.puts Process.pid } |
| 237 | end, |
| 238 | :ServerType => WEBrick::Daemon, |
| 239 | } |
| 240 | options[:BindAddress] = '127.0.0.1' if "$local" == "true" |
Mike Dalessio | 425b78e | 2007-10-06 13:29:49 -0400 | [diff] [blame] | 241 | server = WEBrick::HTTPServer.new(options) |
| 242 | ['INT', 'TERM'].each do |signal| |
| 243 | trap(signal) {server.shutdown} |
| 244 | end |
| 245 | server.start |
| 246 | EOF |
Eric Wong | f46e130 | 2010-08-05 08:35:45 +0000 | [diff] [blame] | 247 | chmod +x "$fqgitdir/gitweb/$httpd.rb" |
| 248 | # configuration is embedded in server script file, webrick.rb |
| 249 | rm -f "$conf" |
Mike Dalessio | 425b78e | 2007-10-06 13:29:49 -0400 | [diff] [blame] | 250 | } |
| 251 | |
Eric Wong | a51d37c | 2006-07-01 15:14:14 -0700 | [diff] [blame] | 252 | lighttpd_conf () { |
| 253 | cat > "$conf" <<EOF |
Pavan Kumar Sunkara | c0cb4ed | 2010-05-28 11:55:52 +0530 | [diff] [blame] | 254 | server.document-root = "$root" |
Eric Wong | a51d37c | 2006-07-01 15:14:14 -0700 | [diff] [blame] | 255 | server.port = $port |
Ramsay Jones | e47eec8 | 2009-03-09 18:31:55 +0000 | [diff] [blame] | 256 | server.modules = ( "mod_setenv", "mod_cgi" ) |
Eric Wong | a51d37c | 2006-07-01 15:14:14 -0700 | [diff] [blame] | 257 | server.indexfiles = ( "gitweb.cgi" ) |
| 258 | server.pid-file = "$fqgitdir/pid" |
Pavan Kumar Sunkara | be5347b | 2010-05-28 11:55:51 +0530 | [diff] [blame] | 259 | server.errorlog = "$fqgitdir/gitweb/$httpd_only/error.log" |
Ramsay Jones | e47eec8 | 2009-03-09 18:31:55 +0000 | [diff] [blame] | 260 | |
| 261 | # to enable, add "mod_access", "mod_accesslog" to server.modules |
| 262 | # variable above and uncomment this |
Pavan Kumar Sunkara | be5347b | 2010-05-28 11:55:51 +0530 | [diff] [blame] | 263 | #accesslog.filename = "$fqgitdir/gitweb/$httpd_only/access.log" |
Ramsay Jones | e47eec8 | 2009-03-09 18:31:55 +0000 | [diff] [blame] | 264 | |
Pavan Kumar Sunkara | c0cb4ed | 2010-05-28 11:55:52 +0530 | [diff] [blame] | 265 | setenv.add-environment = ( "PATH" => env.PATH, "GITWEB_CONFIG" => env.GITWEB_CONFIG ) |
Ramsay Jones | e47eec8 | 2009-03-09 18:31:55 +0000 | [diff] [blame] | 266 | |
Eric Wong | a51d37c | 2006-07-01 15:14:14 -0700 | [diff] [blame] | 267 | cgi.assign = ( ".cgi" => "" ) |
Ramsay Jones | e47eec8 | 2009-03-09 18:31:55 +0000 | [diff] [blame] | 268 | |
| 269 | # mimetype mapping |
| 270 | mimetype.assign = ( |
| 271 | ".pdf" => "application/pdf", |
| 272 | ".sig" => "application/pgp-signature", |
| 273 | ".spl" => "application/futuresplash", |
| 274 | ".class" => "application/octet-stream", |
| 275 | ".ps" => "application/postscript", |
| 276 | ".torrent" => "application/x-bittorrent", |
| 277 | ".dvi" => "application/x-dvi", |
| 278 | ".gz" => "application/x-gzip", |
| 279 | ".pac" => "application/x-ns-proxy-autoconfig", |
| 280 | ".swf" => "application/x-shockwave-flash", |
| 281 | ".tar.gz" => "application/x-tgz", |
| 282 | ".tgz" => "application/x-tgz", |
| 283 | ".tar" => "application/x-tar", |
| 284 | ".zip" => "application/zip", |
| 285 | ".mp3" => "audio/mpeg", |
| 286 | ".m3u" => "audio/x-mpegurl", |
| 287 | ".wma" => "audio/x-ms-wma", |
| 288 | ".wax" => "audio/x-ms-wax", |
| 289 | ".ogg" => "application/ogg", |
| 290 | ".wav" => "audio/x-wav", |
| 291 | ".gif" => "image/gif", |
| 292 | ".jpg" => "image/jpeg", |
| 293 | ".jpeg" => "image/jpeg", |
| 294 | ".png" => "image/png", |
| 295 | ".xbm" => "image/x-xbitmap", |
| 296 | ".xpm" => "image/x-xpixmap", |
| 297 | ".xwd" => "image/x-xwindowdump", |
| 298 | ".css" => "text/css", |
| 299 | ".html" => "text/html", |
| 300 | ".htm" => "text/html", |
| 301 | ".js" => "text/javascript", |
| 302 | ".asc" => "text/plain", |
| 303 | ".c" => "text/plain", |
| 304 | ".cpp" => "text/plain", |
| 305 | ".log" => "text/plain", |
| 306 | ".conf" => "text/plain", |
| 307 | ".text" => "text/plain", |
| 308 | ".txt" => "text/plain", |
| 309 | ".dtd" => "text/xml", |
| 310 | ".xml" => "text/xml", |
| 311 | ".mpeg" => "video/mpeg", |
| 312 | ".mpg" => "video/mpeg", |
| 313 | ".mov" => "video/quicktime", |
| 314 | ".qt" => "video/quicktime", |
| 315 | ".avi" => "video/x-msvideo", |
| 316 | ".asf" => "video/x-ms-asf", |
| 317 | ".asx" => "video/x-ms-asf", |
| 318 | ".wmv" => "video/x-ms-wmv", |
| 319 | ".bz2" => "application/x-bzip", |
| 320 | ".tbz" => "application/x-bzip-compressed-tar", |
| 321 | ".tar.bz2" => "application/x-bzip-compressed-tar", |
| 322 | "" => "text/plain" |
| 323 | ) |
Eric Wong | a51d37c | 2006-07-01 15:14:14 -0700 | [diff] [blame] | 324 | EOF |
Jonas Fonseca | 9126425 | 2007-11-09 00:21:42 +0100 | [diff] [blame] | 325 | test x"$local" = xtrue && echo 'server.bind = "127.0.0.1"' >> "$conf" |
Eric Wong | a51d37c | 2006-07-01 15:14:14 -0700 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | apache2_conf () { |
Dan McGee | 4bdf859 | 2010-06-30 07:29:08 -0500 | [diff] [blame] | 329 | if test -z "$module_path" |
| 330 | then |
| 331 | test -d "/usr/lib/httpd/modules" && |
| 332 | module_path="/usr/lib/httpd/modules" |
| 333 | test -d "/usr/lib/apache2/modules" && |
| 334 | module_path="/usr/lib/apache2/modules" |
| 335 | fi |
Eric Wong | a51d37c | 2006-07-01 15:14:14 -0700 | [diff] [blame] | 336 | bind= |
Jonas Fonseca | 9126425 | 2007-11-09 00:21:42 +0100 | [diff] [blame] | 337 | test x"$local" = xtrue && bind='127.0.0.1:' |
Sean Estabrooks | e24c76b | 2009-07-18 09:45:44 -0700 | [diff] [blame] | 338 | echo 'text/css css' > "$fqgitdir/mime.types" |
Eric Wong | a51d37c | 2006-07-01 15:14:14 -0700 | [diff] [blame] | 339 | cat > "$conf" <<EOF |
Eric Wong | 44a167b | 2007-01-02 00:57:11 -0800 | [diff] [blame] | 340 | ServerName "git-instaweb" |
Pavan Kumar Sunkara | c0cb4ed | 2010-05-28 11:55:52 +0530 | [diff] [blame] | 341 | ServerRoot "$root" |
| 342 | DocumentRoot "$root" |
Pavan Kumar Sunkara | be5347b | 2010-05-28 11:55:51 +0530 | [diff] [blame] | 343 | ErrorLog "$fqgitdir/gitweb/$httpd_only/error.log" |
| 344 | CustomLog "$fqgitdir/gitweb/$httpd_only/access.log" combined |
Eric Wong | a51d37c | 2006-07-01 15:14:14 -0700 | [diff] [blame] | 345 | PidFile "$fqgitdir/pid" |
| 346 | Listen $bind$port |
Eric Wong | 44a167b | 2007-01-02 00:57:11 -0800 | [diff] [blame] | 347 | EOF |
| 348 | |
Jonathan McCrohan | f8ee1f0 | 2014-05-27 02:18:10 +0100 | [diff] [blame] | 349 | for mod in mpm_event mpm_prefork mpm_worker |
| 350 | do |
| 351 | if test -e $module_path/mod_${mod}.so |
| 352 | then |
| 353 | echo "LoadModule ${mod}_module " \ |
| 354 | "$module_path/mod_${mod}.so" >> "$conf" |
| 355 | # only one mpm module permitted |
| 356 | break |
| 357 | fi |
| 358 | done |
| 359 | for mod in mime dir env log_config authz_core |
Dan McGee | 4bdf859 | 2010-06-30 07:29:08 -0500 | [diff] [blame] | 360 | do |
| 361 | if test -e $module_path/mod_${mod}.so |
| 362 | then |
Eric Wong | 44a167b | 2007-01-02 00:57:11 -0800 | [diff] [blame] | 363 | echo "LoadModule ${mod}_module " \ |
| 364 | "$module_path/mod_${mod}.so" >> "$conf" |
| 365 | fi |
| 366 | done |
| 367 | cat >> "$conf" <<EOF |
Sean Estabrooks | e24c76b | 2009-07-18 09:45:44 -0700 | [diff] [blame] | 368 | TypesConfig "$fqgitdir/mime.types" |
Eric Wong | a51d37c | 2006-07-01 15:14:14 -0700 | [diff] [blame] | 369 | DirectoryIndex gitweb.cgi |
| 370 | EOF |
| 371 | |
| 372 | # check to see if Dennis Stosberg's mod_perl compatibility patch |
| 373 | # (<20060621130708.Gcbc6e5c@leonov.stosberg.net>) has been applied |
Junio C Hamano | e1622bf | 2009-11-23 15:56:32 -0800 | [diff] [blame] | 374 | if test -f "$module_path/mod_perl.so" && |
Pavan Kumar Sunkara | c0cb4ed | 2010-05-28 11:55:52 +0530 | [diff] [blame] | 375 | sane_grep 'MOD_PERL' "$root/gitweb.cgi" >/dev/null |
Eric Wong | a51d37c | 2006-07-01 15:14:14 -0700 | [diff] [blame] | 376 | then |
| 377 | # favor mod_perl if available |
| 378 | cat >> "$conf" <<EOF |
| 379 | LoadModule perl_module $module_path/mod_perl.so |
| 380 | PerlPassEnv GIT_DIR |
Dan McGee | 2989f51 | 2010-06-29 21:44:59 -0500 | [diff] [blame] | 381 | PerlPassEnv GIT_EXEC_PATH |
Pavan Kumar Sunkara | c0cb4ed | 2010-05-28 11:55:52 +0530 | [diff] [blame] | 382 | PerlPassEnv GITWEB_CONFIG |
Eric Wong | a51d37c | 2006-07-01 15:14:14 -0700 | [diff] [blame] | 383 | <Location /gitweb.cgi> |
| 384 | SetHandler perl-script |
| 385 | PerlResponseHandler ModPerl::Registry |
| 386 | PerlOptions +ParseHeaders |
| 387 | Options +ExecCGI |
| 388 | </Location> |
| 389 | EOF |
| 390 | else |
| 391 | # plain-old CGI |
Flavio Poletti | 43d60d2 | 2008-06-12 23:54:55 +0200 | [diff] [blame] | 392 | resolve_full_httpd |
Stephen Boyd | 9524cf2 | 2010-01-26 15:08:31 -0800 | [diff] [blame] | 393 | list_mods=$(echo "$full_httpd" | sed 's/-f$/-l/') |
Junio C Hamano | e1622bf | 2009-11-23 15:56:32 -0800 | [diff] [blame] | 394 | $list_mods | sane_grep 'mod_cgi\.c' >/dev/null 2>&1 || \ |
Mark Rada | 10d1432 | 2009-09-26 14:12:32 -0400 | [diff] [blame] | 395 | if test -f "$module_path/mod_cgi.so" |
| 396 | then |
| 397 | echo "LoadModule cgi_module $module_path/mod_cgi.so" >> "$conf" |
| 398 | else |
| 399 | $list_mods | grep 'mod_cgid\.c' >/dev/null 2>&1 || \ |
| 400 | if test -f "$module_path/mod_cgid.so" |
| 401 | then |
| 402 | echo "LoadModule cgid_module $module_path/mod_cgid.so" \ |
| 403 | >> "$conf" |
| 404 | else |
| 405 | echo "You have no CGI support!" |
| 406 | exit 2 |
| 407 | fi |
| 408 | echo "ScriptSock logs/gitweb.sock" >> "$conf" |
| 409 | fi |
Eric Wong | a51d37c | 2006-07-01 15:14:14 -0700 | [diff] [blame] | 410 | cat >> "$conf" <<EOF |
Dan McGee | 2989f51 | 2010-06-29 21:44:59 -0500 | [diff] [blame] | 411 | PassEnv GIT_DIR |
| 412 | PassEnv GIT_EXEC_PATH |
| 413 | PassEnv GITWEB_CONFIG |
Eric Wong | a51d37c | 2006-07-01 15:14:14 -0700 | [diff] [blame] | 414 | AddHandler cgi-script .cgi |
| 415 | <Location /gitweb.cgi> |
| 416 | Options +ExecCGI |
| 417 | </Location> |
| 418 | EOF |
| 419 | fi |
| 420 | } |
| 421 | |
Wilhansen Li | 0ded475 | 2009-08-21 22:24:48 +0800 | [diff] [blame] | 422 | mongoose_conf() { |
| 423 | cat > "$conf" <<EOF |
| 424 | # Mongoose web server configuration file. |
| 425 | # Lines starting with '#' and empty lines are ignored. |
| 426 | # For detailed description of every option, visit |
| 427 | # http://code.google.com/p/mongoose/wiki/MongooseManual |
| 428 | |
Pavan Kumar Sunkara | c0cb4ed | 2010-05-28 11:55:52 +0530 | [diff] [blame] | 429 | root $root |
Wilhansen Li | 0ded475 | 2009-08-21 22:24:48 +0800 | [diff] [blame] | 430 | ports $port |
| 431 | index_files gitweb.cgi |
| 432 | #ssl_cert $fqgitdir/gitweb/ssl_cert.pem |
Pavan Kumar Sunkara | be5347b | 2010-05-28 11:55:51 +0530 | [diff] [blame] | 433 | error_log $fqgitdir/gitweb/$httpd_only/error.log |
| 434 | access_log $fqgitdir/gitweb/$httpd_only/access.log |
Wilhansen Li | 0ded475 | 2009-08-21 22:24:48 +0800 | [diff] [blame] | 435 | |
| 436 | #cgi setup |
Pavan Kumar Sunkara | c0cb4ed | 2010-05-28 11:55:52 +0530 | [diff] [blame] | 437 | cgi_env PATH=$PATH,GIT_DIR=$GIT_DIR,GIT_EXEC_PATH=$GIT_EXEC_PATH,GITWEB_CONFIG=$GITWEB_CONFIG |
Wilhansen Li | 0ded475 | 2009-08-21 22:24:48 +0800 | [diff] [blame] | 438 | cgi_interp $PERL |
| 439 | cgi_ext cgi,pl |
| 440 | |
| 441 | # mimetype mapping |
| 442 | mime_types .gz=application/x-gzip,.tar.gz=application/x-tgz,.tgz=application/x-tgz,.tar=application/x-tar,.zip=application/zip,.gif=image/gif,.jpg=image/jpeg,.jpeg=image/jpeg,.png=image/png,.css=text/css,.html=text/html,.htm=text/html,.js=text/javascript,.c=text/plain,.cpp=text/plain,.log=text/plain,.conf=text/plain,.text=text/plain,.txt=text/plain,.dtd=text/xml,.bz2=application/x-bzip,.tbz=application/x-bzip-compressed-tar,.tar.bz2=application/x-bzip-compressed-tar |
| 443 | EOF |
| 444 | } |
| 445 | |
Jakub Narebski | 7864698 | 2010-05-28 21:11:25 +0200 | [diff] [blame] | 446 | plackup_conf () { |
| 447 | # generate a standalone 'plackup' server script in $fqgitdir/gitweb |
| 448 | # with embedded configuration; it does not use "$conf" file |
| 449 | cat > "$fqgitdir/gitweb/gitweb.psgi" <<EOF |
| 450 | #!$PERL |
| 451 | |
| 452 | # gitweb - simple web interface to track changes in git repositories |
| 453 | # PSGI wrapper and server starter (see http://plackperl.org) |
| 454 | |
| 455 | use strict; |
| 456 | |
| 457 | use IO::Handle; |
| 458 | use Plack::MIME; |
| 459 | use Plack::Builder; |
| 460 | use Plack::App::WrapCGI; |
| 461 | use CGI::Emulate::PSGI 0.07; # minimum version required to work with gitweb |
| 462 | |
| 463 | # mimetype mapping (from lighttpd_conf) |
| 464 | Plack::MIME->add_type( |
| 465 | ".pdf" => "application/pdf", |
| 466 | ".sig" => "application/pgp-signature", |
| 467 | ".spl" => "application/futuresplash", |
| 468 | ".class" => "application/octet-stream", |
| 469 | ".ps" => "application/postscript", |
| 470 | ".torrent" => "application/x-bittorrent", |
| 471 | ".dvi" => "application/x-dvi", |
| 472 | ".gz" => "application/x-gzip", |
| 473 | ".pac" => "application/x-ns-proxy-autoconfig", |
| 474 | ".swf" => "application/x-shockwave-flash", |
| 475 | ".tar.gz" => "application/x-tgz", |
| 476 | ".tgz" => "application/x-tgz", |
| 477 | ".tar" => "application/x-tar", |
| 478 | ".zip" => "application/zip", |
| 479 | ".mp3" => "audio/mpeg", |
| 480 | ".m3u" => "audio/x-mpegurl", |
| 481 | ".wma" => "audio/x-ms-wma", |
| 482 | ".wax" => "audio/x-ms-wax", |
| 483 | ".ogg" => "application/ogg", |
| 484 | ".wav" => "audio/x-wav", |
| 485 | ".gif" => "image/gif", |
| 486 | ".jpg" => "image/jpeg", |
| 487 | ".jpeg" => "image/jpeg", |
| 488 | ".png" => "image/png", |
| 489 | ".xbm" => "image/x-xbitmap", |
| 490 | ".xpm" => "image/x-xpixmap", |
| 491 | ".xwd" => "image/x-xwindowdump", |
| 492 | ".css" => "text/css", |
| 493 | ".html" => "text/html", |
| 494 | ".htm" => "text/html", |
| 495 | ".js" => "text/javascript", |
| 496 | ".asc" => "text/plain", |
| 497 | ".c" => "text/plain", |
| 498 | ".cpp" => "text/plain", |
| 499 | ".log" => "text/plain", |
| 500 | ".conf" => "text/plain", |
| 501 | ".text" => "text/plain", |
| 502 | ".txt" => "text/plain", |
| 503 | ".dtd" => "text/xml", |
| 504 | ".xml" => "text/xml", |
| 505 | ".mpeg" => "video/mpeg", |
| 506 | ".mpg" => "video/mpeg", |
| 507 | ".mov" => "video/quicktime", |
| 508 | ".qt" => "video/quicktime", |
| 509 | ".avi" => "video/x-msvideo", |
| 510 | ".asf" => "video/x-ms-asf", |
| 511 | ".asx" => "video/x-ms-asf", |
| 512 | ".wmv" => "video/x-ms-wmv", |
| 513 | ".bz2" => "application/x-bzip", |
| 514 | ".tbz" => "application/x-bzip-compressed-tar", |
| 515 | ".tar.bz2" => "application/x-bzip-compressed-tar", |
| 516 | "" => "text/plain" |
| 517 | ); |
| 518 | |
| 519 | my \$app = builder { |
| 520 | # to be able to override \$SIG{__WARN__} to log build time warnings |
| 521 | use CGI::Carp; # it sets \$SIG{__WARN__} itself |
| 522 | |
| 523 | my \$logdir = "$fqgitdir/gitweb/$httpd_only"; |
| 524 | open my \$access_log_fh, '>>', "\$logdir/access.log" |
| 525 | or die "Couldn't open access log '\$logdir/access.log': \$!"; |
| 526 | open my \$error_log_fh, '>>', "\$logdir/error.log" |
| 527 | or die "Couldn't open error log '\$logdir/error.log': \$!"; |
| 528 | |
| 529 | \$access_log_fh->autoflush(1); |
| 530 | \$error_log_fh->autoflush(1); |
| 531 | |
| 532 | # redirect build time warnings to error.log |
| 533 | \$SIG{'__WARN__'} = sub { |
| 534 | my \$msg = shift; |
| 535 | # timestamp warning like in CGI::Carp::warn |
| 536 | my \$stamp = CGI::Carp::stamp(); |
| 537 | \$msg =~ s/^/\$stamp/gm; |
| 538 | print \$error_log_fh \$msg; |
| 539 | }; |
| 540 | |
| 541 | # write errors to error.log, access to access.log |
| 542 | enable 'AccessLog', |
| 543 | format => "combined", |
| 544 | logger => sub { print \$access_log_fh @_; }; |
| 545 | enable sub { |
| 546 | my \$app = shift; |
| 547 | sub { |
| 548 | my \$env = shift; |
| 549 | \$env->{'psgi.errors'} = \$error_log_fh; |
| 550 | \$app->(\$env); |
| 551 | } |
| 552 | }; |
| 553 | # gitweb currently doesn't work with $SIG{CHLD} set to 'IGNORE', |
| 554 | # because it uses 'close $fd or die...' on piped filehandle $fh |
| 555 | # (which causes the parent process to wait for child to finish). |
| 556 | enable_if { \$SIG{'CHLD'} eq 'IGNORE' } sub { |
| 557 | my \$app = shift; |
| 558 | sub { |
| 559 | my \$env = shift; |
| 560 | local \$SIG{'CHLD'} = 'DEFAULT'; |
| 561 | local \$SIG{'CLD'} = 'DEFAULT'; |
| 562 | \$app->(\$env); |
| 563 | } |
| 564 | }; |
| 565 | # serve static files, i.e. stylesheet, images, script |
| 566 | enable 'Static', |
| 567 | path => sub { m!\.(js|css|png)\$! && s!^/gitweb/!! }, |
| 568 | root => "$root/", |
| 569 | encoding => 'utf-8'; # encoding for 'text/plain' files |
| 570 | # convert CGI application to PSGI app |
| 571 | Plack::App::WrapCGI->new(script => "$root/gitweb.cgi")->to_app; |
| 572 | }; |
| 573 | |
| 574 | # make it runnable as standalone app, |
| 575 | # like it would be run via 'plackup' utility |
Jakub Narebski | 20e7ab8 | 2011-02-26 23:32:33 +0100 | [diff] [blame] | 576 | if (caller) { |
| 577 | return \$app; |
| 578 | } else { |
Jakub Narebski | 7864698 | 2010-05-28 21:11:25 +0200 | [diff] [blame] | 579 | require Plack::Runner; |
| 580 | |
| 581 | my \$runner = Plack::Runner->new(); |
| 582 | \$runner->parse_options(qw(--env deployment --port $port), |
Jakub Narebski | 20e7ab8 | 2011-02-26 23:32:33 +0100 | [diff] [blame] | 583 | "$local" ? qw(--host 127.0.0.1) : ()); |
Jakub Narebski | 7864698 | 2010-05-28 21:11:25 +0200 | [diff] [blame] | 584 | \$runner->run(\$app); |
| 585 | } |
| 586 | __END__ |
| 587 | EOF |
| 588 | |
| 589 | chmod a+x "$fqgitdir/gitweb/gitweb.psgi" |
| 590 | # configuration is embedded in server script file, gitweb.psgi |
| 591 | rm -f "$conf" |
| 592 | } |
| 593 | |
Pavan Kumar Sunkara | c0cb4ed | 2010-05-28 11:55:52 +0530 | [diff] [blame] | 594 | gitweb_conf() { |
| 595 | cat > "$fqgitdir/gitweb/gitweb_config.perl" <<EOF |
Jeff King | fcb06a8 | 2013-10-28 21:19:59 -0400 | [diff] [blame] | 596 | #!@@PERL@@ |
Pavan Kumar Sunkara | c0cb4ed | 2010-05-28 11:55:52 +0530 | [diff] [blame] | 597 | our \$projectroot = "$(dirname "$fqgitdir")"; |
| 598 | our \$git_temp = "$fqgitdir/gitweb/tmp"; |
| 599 | our \$projects_list = \$projectroot; |
Giuseppe Bilotta | fc5b8e0 | 2010-11-11 13:26:18 +0100 | [diff] [blame] | 600 | |
| 601 | \$feature{'remote_heads'}{'default'} = [1]; |
Pavan Kumar Sunkara | c0cb4ed | 2010-05-28 11:55:52 +0530 | [diff] [blame] | 602 | EOF |
Eric Wong | a51d37c | 2006-07-01 15:14:14 -0700 | [diff] [blame] | 603 | } |
| 604 | |
Jakub Narebski | db61f06 | 2011-06-23 21:55:00 +0200 | [diff] [blame] | 605 | configure_httpd() { |
| 606 | case "$httpd" in |
| 607 | *lighttpd*) |
| 608 | lighttpd_conf |
| 609 | ;; |
| 610 | *apache2*|*httpd*) |
| 611 | apache2_conf |
| 612 | ;; |
| 613 | webrick) |
| 614 | webrick_conf |
| 615 | ;; |
| 616 | *mongoose*) |
| 617 | mongoose_conf |
| 618 | ;; |
| 619 | *plackup*) |
| 620 | plackup_conf |
| 621 | ;; |
| 622 | *) |
| 623 | echo "Unknown httpd specified: $httpd" |
| 624 | exit 1 |
| 625 | ;; |
| 626 | esac |
| 627 | } |
| 628 | |
Jakub Narebski | c0175f9 | 2011-06-23 22:59:26 +0200 | [diff] [blame] | 629 | case "$action" in |
| 630 | stop) |
| 631 | stop_httpd |
| 632 | exit 0 |
| 633 | ;; |
| 634 | start) |
| 635 | start_httpd |
| 636 | exit 0 |
| 637 | ;; |
| 638 | restart) |
| 639 | stop_httpd |
| 640 | start_httpd |
| 641 | exit 0 |
| 642 | ;; |
| 643 | esac |
| 644 | |
Pavan Kumar Sunkara | c0cb4ed | 2010-05-28 11:55:52 +0530 | [diff] [blame] | 645 | gitweb_conf |
Eric Wong | a51d37c | 2006-07-01 15:14:14 -0700 | [diff] [blame] | 646 | |
Pavan Kumar Sunkara | be5347b | 2010-05-28 11:55:51 +0530 | [diff] [blame] | 647 | resolve_full_httpd |
| 648 | mkdir -p "$fqgitdir/gitweb/$httpd_only" |
Jakub Narebski | 5ad6d38 | 2011-06-23 23:01:03 +0200 | [diff] [blame] | 649 | conf="$fqgitdir/gitweb/$httpd_only.conf" |
Pavan Kumar Sunkara | be5347b | 2010-05-28 11:55:51 +0530 | [diff] [blame] | 650 | |
Jakub Narebski | db61f06 | 2011-06-23 21:55:00 +0200 | [diff] [blame] | 651 | configure_httpd |
Eric Wong | a51d37c | 2006-07-01 15:14:14 -0700 | [diff] [blame] | 652 | |
| 653 | start_httpd |
Johannes Schindelin | 5209eda | 2006-07-26 23:11:46 +0200 | [diff] [blame] | 654 | url=http://127.0.0.1:$port |
Christian Couder | 2e0c290 | 2008-02-02 07:32:56 +0100 | [diff] [blame] | 655 | |
| 656 | if test -n "$browser"; then |
Jakub Narebski | d94775e | 2010-05-28 21:11:24 +0200 | [diff] [blame] | 657 | httpd_is_ready && git web--browse -b "$browser" $url || echo $url |
Christian Couder | 2e0c290 | 2008-02-02 07:32:56 +0100 | [diff] [blame] | 658 | else |
Jakub Narebski | d94775e | 2010-05-28 21:11:24 +0200 | [diff] [blame] | 659 | httpd_is_ready && git web--browse -c "instaweb.browser" $url || echo $url |
Christian Couder | 2e0c290 | 2008-02-02 07:32:56 +0100 | [diff] [blame] | 660 | fi |