git-instaweb.shon commit config: add test cases for empty value and no value config variables. (d8e8757)
   1#!/bin/sh
   2#
   3# Copyright (c) 2006 Eric Wong
   4#
   5
   6PERL='@@PERL@@'
   7OPTIONS_KEEPDASHDASH=
   8OPTIONS_SPEC="\
   9git-instaweb [options] (--start | --stop | --restart)
  10--
  11l,local        only bind on 127.0.0.1
  12p,port=        the port to bind to
  13d,httpd=       the command to launch
  14b,browser=     the browser to launch
  15m,module-path= the module path (only needed for apache2)
  16 Action
  17stop           stop the web server
  18start          start the web server
  19restart        restart the web server
  20"
  21
  22. git-sh-setup
  23
  24fqgitdir="$GIT_DIR"
  25local="`git config --bool --get instaweb.local`"
  26httpd="`git config --get instaweb.httpd`"
  27browser="`git config --get instaweb.browser`"
  28test -z "$browser" && browser="`git config --get web.browser`"
  29port=`git config --get instaweb.port`
  30module_path="`git config --get instaweb.modulepath`"
  31
  32conf="$GIT_DIR/gitweb/httpd.conf"
  33
  34# Defaults:
  35
  36# if installed, it doesn't need further configuration (module_path)
  37test -z "$httpd" && httpd='lighttpd -f'
  38
  39# probably the most popular browser among gitweb users
  40test -z "$browser" && browser='firefox'
  41
  42# any untaken local port will do...
  43test -z "$port" && port=1234
  44
  45start_httpd () {
  46        httpd_only="`echo $httpd | cut -f1 -d' '`"
  47        if case "$httpd_only" in /*) : ;; *) which $httpd_only >/dev/null;; esac
  48        then
  49                $httpd "$fqgitdir/gitweb/httpd.conf"
  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
  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"
  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
  65                echo "$httpd_only not found. Install $httpd_only or use" \
  66                     "--httpd to specify another http daemon."
  67                exit 1
  68        fi
  69        if test $? != 0; then
  70                echo "Could not execute http daemon $httpd."
  71                exit 1
  72        fi
  73}
  74
  75stop_httpd () {
  76        test -f "$fqgitdir/pid" && kill `cat "$fqgitdir/pid"`
  77}
  78
  79while test $# != 0
  80do
  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                ;;
  95        -l|--local)
  96                local=true
  97                ;;
  98        -d|--httpd)
  99                shift
 100                httpd="$1"
 101                ;;
 102        -b|--browser)
 103                shift
 104                browser="$1"
 105                ;;
 106        -p|--port)
 107                shift
 108                port="$1"
 109                ;;
 110        -m|--module-path)
 111                shift
 112                module_path="$1"
 113                ;;
 114        --)
 115                ;;
 116        *)
 117                usage
 118                ;;
 119        esac
 120        shift
 121done
 122
 123mkdir -p "$GIT_DIR/gitweb/tmp"
 124GIT_EXEC_PATH="`git --exec-path`"
 125GIT_DIR="$fqgitdir"
 126export GIT_EXEC_PATH GIT_DIR
 127
 128
 129webrick_conf () {
 130        # generate a standalone server script in $fqgitdir/gitweb.
 131        cat >"$fqgitdir/gitweb/$httpd.rb" <<EOF
 132require 'webrick'
 133require 'yaml'
 134options = YAML::load_file(ARGV[0])
 135options[:StartCallback] = proc do
 136  File.open(options[:PidFile],"w") do |f|
 137    f.puts Process.pid
 138  end
 139end
 140options[:ServerType] = WEBrick::Daemon
 141server = WEBrick::HTTPServer.new(options)
 142['INT', 'TERM'].each do |signal|
 143  trap(signal) {server.shutdown}
 144end
 145server.start
 146EOF
 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
 153exec ruby "$fqgitdir/gitweb/$httpd.rb" \$*
 154EOF
 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"
 162EOF
 163        test "$local" = true && echo ':BindAddress: "127.0.0.1"' >> "$conf"
 164}
 165
 166lighttpd_conf () {
 167        cat > "$conf" <<EOF
 168server.document-root = "$fqgitdir/gitweb"
 169server.port = $port
 170server.modules = ( "mod_cgi" )
 171server.indexfiles = ( "gitweb.cgi" )
 172server.pid-file = "$fqgitdir/pid"
 173cgi.assign = ( ".cgi" => "" )
 174mimetype.assign = ( ".css" => "text/css" )
 175EOF
 176        test x"$local" = xtrue && echo 'server.bind = "127.0.0.1"' >> "$conf"
 177}
 178
 179apache2_conf () {
 180        test -z "$module_path" && module_path=/usr/lib/apache2/modules
 181        mkdir -p "$GIT_DIR/gitweb/logs"
 182        bind=
 183        test x"$local" = xtrue && bind='127.0.0.1:'
 184        echo 'text/css css' > $fqgitdir/mime.types
 185        cat > "$conf" <<EOF
 186ServerName "git-instaweb"
 187ServerRoot "$fqgitdir/gitweb"
 188DocumentRoot "$fqgitdir/gitweb"
 189PidFile "$fqgitdir/pid"
 190Listen $bind$port
 191EOF
 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
 200TypesConfig $fqgitdir/mime.types
 201DirectoryIndex gitweb.cgi
 202EOF
 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
 211LoadModule perl_module $module_path/mod_perl.so
 212PerlPassEnv GIT_DIR
 213PerlPassEnv GIT_EXEC_DIR
 214<Location /gitweb.cgi>
 215        SetHandler perl-script
 216        PerlResponseHandler ModPerl::Registry
 217        PerlOptions +ParseHeaders
 218        Options +ExecCGI
 219</Location>
 220EOF
 221        else
 222                # plain-old CGI
 223                list_mods=`echo "$httpd" | sed "s/-f$/-l/"`
 224                $list_mods | grep 'mod_cgi\.c' >/dev/null 2>&1 || \
 225                echo "LoadModule cgi_module $module_path/mod_cgi.so" >> "$conf"
 226                cat >> "$conf" <<EOF
 227AddHandler cgi-script .cgi
 228<Location /gitweb.cgi>
 229        Options +ExecCGI
 230</Location>
 231EOF
 232        fi
 233}
 234
 235script='
 236s#^(my|our) \$projectroot =.*#$1 \$projectroot = "'$(dirname "$fqgitdir")'";#;
 237s#(my|our) \$gitbin =.*#$1 \$gitbin = "'$GIT_EXEC_PATH'";#;
 238s#(my|our) \$projects_list =.*#$1 \$projects_list = \$projectroot;#;
 239s#(my|our) \$git_temp =.*#$1 \$git_temp = "'$fqgitdir/gitweb/tmp'";#;'
 240
 241gitweb_cgi () {
 242        cat > "$1.tmp" <<\EOFGITWEB
 243@@GITWEB_CGI@@
 244EOFGITWEB
 245        # Use the configured full path to perl to match the generated
 246        # scripts' 'hashpling' line
 247        "$PERL" -p -e "$script" "$1.tmp"  > "$1"
 248        chmod +x "$1"
 249        rm -f "$1.tmp"
 250}
 251
 252gitweb_css () {
 253        cat > "$1" <<\EOFGITWEB
 254@@GITWEB_CSS@@
 255EOFGITWEB
 256}
 257
 258gitweb_cgi "$GIT_DIR/gitweb/gitweb.cgi"
 259gitweb_css "$GIT_DIR/gitweb/gitweb.css"
 260
 261case "$httpd" in
 262*lighttpd*)
 263        lighttpd_conf
 264        ;;
 265*apache2*)
 266        apache2_conf
 267        ;;
 268webrick)
 269        webrick_conf
 270        ;;
 271*)
 272        echo "Unknown httpd specified: $httpd"
 273        exit 1
 274        ;;
 275esac
 276
 277init_browser_path() {
 278        browser_path="`git config browser.$1.path`"
 279        test -z "$browser_path" && browser_path="$1"
 280}
 281
 282start_httpd
 283url=http://127.0.0.1:$port
 284test -n "$browser" && {
 285        init_browser_path "$browser"
 286        "$browser_path" $url
 287} || echo $url