f03465c13ba1f6f9de137241a8ca011e336fae58
   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)"
  27port=$(git config --get instaweb.port)
  28module_path="$(git config --get instaweb.modulepath)"
  29
  30conf="$GIT_DIR/gitweb/httpd.conf"
  31
  32# Defaults:
  33
  34# if installed, it doesn't need further configuration (module_path)
  35test -z "$httpd" && httpd='lighttpd -f'
  36
  37# any untaken local port will do...
  38test -z "$port" && port=1234
  39
  40resolve_full_httpd () {
  41        case "$httpd" in
  42        *apache2*|*lighttpd*)
  43                # ensure that the apache2/lighttpd command ends with "-f"
  44                if ! echo "$httpd" | sane_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' ')"
  52        if case "$httpd_only" in /*) : ;; *) which $httpd_only >/dev/null 2>&1;; esac
  53        then
  54                full_httpd=$httpd
  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
  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"
  61                do
  62                        if test -x "$i/$httpd_only"
  63                        then
  64                                full_httpd=$i/$httpd
  65                                return
  66                        fi
  67                done
  68
  69                echo >&2 "$httpd_only not found. Install $httpd_only or use" \
  70                     "--httpd to specify another httpd daemon."
  71                exit 1
  72        fi
  73}
  74
  75start_httpd () {
  76        if test -f "$fqgitdir/pid"; then
  77                say "Instance already running. Restarting..."
  78                stop_httpd
  79        fi
  80
  81        # here $httpd should have a meaningful value
  82        resolve_full_httpd
  83
  84        # don't quote $full_httpd, there can be arguments to it (-f)
  85        case "$httpd" in
  86        *mongoose*)
  87                #The mongoose server doesn't have a daemon mode so we'll have to fork it
  88                $full_httpd "$fqgitdir/gitweb/httpd.conf" &
  89                #Save the pid before doing anything else (we'll print it later)
  90                pid=$!
  91
  92                if test $? != 0; then
  93                        echo "Could not execute http daemon $httpd."
  94                        exit 1
  95                fi
  96
  97                cat > "$fqgitdir/pid" <<EOF
  98$pid
  99EOF
 100                ;;
 101        *)
 102                $full_httpd "$fqgitdir/gitweb/httpd.conf"
 103                if test $? != 0; then
 104                        echo "Could not execute http daemon $httpd."
 105                        exit 1
 106                fi
 107                ;;
 108        esac
 109}
 110
 111stop_httpd () {
 112        test -f "$fqgitdir/pid" && kill $(cat "$fqgitdir/pid")
 113}
 114
 115while test $# != 0
 116do
 117        case "$1" in
 118        --stop|stop)
 119                stop_httpd
 120                exit 0
 121                ;;
 122        --start|start)
 123                start_httpd
 124                exit 0
 125                ;;
 126        --restart|restart)
 127                stop_httpd
 128                start_httpd
 129                exit 0
 130                ;;
 131        -l|--local)
 132                local=true
 133                ;;
 134        -d|--httpd)
 135                shift
 136                httpd="$1"
 137                ;;
 138        -b|--browser)
 139                shift
 140                browser="$1"
 141                ;;
 142        -p|--port)
 143                shift
 144                port="$1"
 145                ;;
 146        -m|--module-path)
 147                shift
 148                module_path="$1"
 149                ;;
 150        --)
 151                ;;
 152        *)
 153                usage
 154                ;;
 155        esac
 156        shift
 157done
 158
 159mkdir -p "$GIT_DIR/gitweb/tmp"
 160GIT_EXEC_PATH="$(git --exec-path)"
 161GIT_DIR="$fqgitdir"
 162export GIT_EXEC_PATH GIT_DIR
 163
 164
 165webrick_conf () {
 166        # generate a standalone server script in $fqgitdir/gitweb.
 167        cat >"$fqgitdir/gitweb/$httpd.rb" <<EOF
 168require 'webrick'
 169require 'yaml'
 170options = YAML::load_file(ARGV[0])
 171options[:StartCallback] = proc do
 172  File.open(options[:PidFile],"w") do |f|
 173    f.puts Process.pid
 174  end
 175end
 176options[:ServerType] = WEBrick::Daemon
 177server = WEBrick::HTTPServer.new(options)
 178['INT', 'TERM'].each do |signal|
 179  trap(signal) {server.shutdown}
 180end
 181server.start
 182EOF
 183        # generate a shell script to invoke the above ruby script,
 184        # which assumes _ruby_ is in the user's $PATH. that's _one_
 185        # portable way to run ruby, which could be installed anywhere,
 186        # really.
 187        cat >"$fqgitdir/gitweb/$httpd" <<EOF
 188#!/bin/sh
 189exec ruby "$fqgitdir/gitweb/$httpd.rb" \$*
 190EOF
 191        chmod +x "$fqgitdir/gitweb/$httpd"
 192
 193        cat >"$conf" <<EOF
 194:Port: $port
 195:DocumentRoot: "$fqgitdir/gitweb"
 196:DirectoryIndex: ["gitweb.cgi"]
 197:PidFile: "$fqgitdir/pid"
 198EOF
 199        test "$local" = true && echo ':BindAddress: "127.0.0.1"' >> "$conf"
 200}
 201
 202lighttpd_conf () {
 203        cat > "$conf" <<EOF
 204server.document-root = "$fqgitdir/gitweb"
 205server.port = $port
 206server.modules = ( "mod_setenv", "mod_cgi" )
 207server.indexfiles = ( "gitweb.cgi" )
 208server.pid-file = "$fqgitdir/pid"
 209server.errorlog = "$fqgitdir/gitweb/$httpd_only/error.log"
 210
 211# to enable, add "mod_access", "mod_accesslog" to server.modules
 212# variable above and uncomment this
 213#accesslog.filename = "$fqgitdir/gitweb/$httpd_only/access.log"
 214
 215setenv.add-environment = ( "PATH" => env.PATH )
 216
 217cgi.assign = ( ".cgi" => "" )
 218
 219# mimetype mapping
 220mimetype.assign             = (
 221  ".pdf"          =>      "application/pdf",
 222  ".sig"          =>      "application/pgp-signature",
 223  ".spl"          =>      "application/futuresplash",
 224  ".class"        =>      "application/octet-stream",
 225  ".ps"           =>      "application/postscript",
 226  ".torrent"      =>      "application/x-bittorrent",
 227  ".dvi"          =>      "application/x-dvi",
 228  ".gz"           =>      "application/x-gzip",
 229  ".pac"          =>      "application/x-ns-proxy-autoconfig",
 230  ".swf"          =>      "application/x-shockwave-flash",
 231  ".tar.gz"       =>      "application/x-tgz",
 232  ".tgz"          =>      "application/x-tgz",
 233  ".tar"          =>      "application/x-tar",
 234  ".zip"          =>      "application/zip",
 235  ".mp3"          =>      "audio/mpeg",
 236  ".m3u"          =>      "audio/x-mpegurl",
 237  ".wma"          =>      "audio/x-ms-wma",
 238  ".wax"          =>      "audio/x-ms-wax",
 239  ".ogg"          =>      "application/ogg",
 240  ".wav"          =>      "audio/x-wav",
 241  ".gif"          =>      "image/gif",
 242  ".jpg"          =>      "image/jpeg",
 243  ".jpeg"         =>      "image/jpeg",
 244  ".png"          =>      "image/png",
 245  ".xbm"          =>      "image/x-xbitmap",
 246  ".xpm"          =>      "image/x-xpixmap",
 247  ".xwd"          =>      "image/x-xwindowdump",
 248  ".css"          =>      "text/css",
 249  ".html"         =>      "text/html",
 250  ".htm"          =>      "text/html",
 251  ".js"           =>      "text/javascript",
 252  ".asc"          =>      "text/plain",
 253  ".c"            =>      "text/plain",
 254  ".cpp"          =>      "text/plain",
 255  ".log"          =>      "text/plain",
 256  ".conf"         =>      "text/plain",
 257  ".text"         =>      "text/plain",
 258  ".txt"          =>      "text/plain",
 259  ".dtd"          =>      "text/xml",
 260  ".xml"          =>      "text/xml",
 261  ".mpeg"         =>      "video/mpeg",
 262  ".mpg"          =>      "video/mpeg",
 263  ".mov"          =>      "video/quicktime",
 264  ".qt"           =>      "video/quicktime",
 265  ".avi"          =>      "video/x-msvideo",
 266  ".asf"          =>      "video/x-ms-asf",
 267  ".asx"          =>      "video/x-ms-asf",
 268  ".wmv"          =>      "video/x-ms-wmv",
 269  ".bz2"          =>      "application/x-bzip",
 270  ".tbz"          =>      "application/x-bzip-compressed-tar",
 271  ".tar.bz2"      =>      "application/x-bzip-compressed-tar",
 272  ""              =>      "text/plain"
 273 )
 274EOF
 275        test x"$local" = xtrue && echo 'server.bind = "127.0.0.1"' >> "$conf"
 276}
 277
 278apache2_conf () {
 279        test -z "$module_path" && module_path=/usr/lib/apache2/modules
 280        bind=
 281        test x"$local" = xtrue && bind='127.0.0.1:'
 282        echo 'text/css css' > "$fqgitdir/mime.types"
 283        cat > "$conf" <<EOF
 284ServerName "git-instaweb"
 285ServerRoot "$fqgitdir/gitweb"
 286DocumentRoot "$fqgitdir/gitweb"
 287ErrorLog "$fqgitdir/gitweb/$httpd_only/error.log"
 288CustomLog "$fqgitdir/gitweb/$httpd_only/access.log" combined
 289PidFile "$fqgitdir/pid"
 290Listen $bind$port
 291EOF
 292
 293        for mod in mime dir; do
 294                if test -e $module_path/mod_${mod}.so; then
 295                        echo "LoadModule ${mod}_module " \
 296                             "$module_path/mod_${mod}.so" >> "$conf"
 297                fi
 298        done
 299        cat >> "$conf" <<EOF
 300TypesConfig "$fqgitdir/mime.types"
 301DirectoryIndex gitweb.cgi
 302EOF
 303
 304        # check to see if Dennis Stosberg's mod_perl compatibility patch
 305        # (<20060621130708.Gcbc6e5c@leonov.stosberg.net>) has been applied
 306        if test -f "$module_path/mod_perl.so" &&
 307           sane_grep 'MOD_PERL' "$GIT_DIR/gitweb/gitweb.cgi" >/dev/null
 308        then
 309                # favor mod_perl if available
 310                cat >> "$conf" <<EOF
 311LoadModule perl_module $module_path/mod_perl.so
 312PerlPassEnv GIT_DIR
 313PerlPassEnv GIT_EXEC_DIR
 314<Location /gitweb.cgi>
 315        SetHandler perl-script
 316        PerlResponseHandler ModPerl::Registry
 317        PerlOptions +ParseHeaders
 318        Options +ExecCGI
 319</Location>
 320EOF
 321        else
 322                # plain-old CGI
 323                resolve_full_httpd
 324                list_mods=$(echo "$full_httpd" | sed 's/-f$/-l/')
 325                $list_mods | sane_grep 'mod_cgi\.c' >/dev/null 2>&1 || \
 326                if test -f "$module_path/mod_cgi.so"
 327                then
 328                        echo "LoadModule cgi_module $module_path/mod_cgi.so" >> "$conf"
 329                else
 330                        $list_mods | grep 'mod_cgid\.c' >/dev/null 2>&1 || \
 331                        if test -f "$module_path/mod_cgid.so"
 332                        then
 333                                echo "LoadModule cgid_module $module_path/mod_cgid.so" \
 334                                        >> "$conf"
 335                        else
 336                                echo "You have no CGI support!"
 337                                exit 2
 338                        fi
 339                        echo "ScriptSock logs/gitweb.sock" >> "$conf"
 340                fi
 341                cat >> "$conf" <<EOF
 342AddHandler cgi-script .cgi
 343<Location /gitweb.cgi>
 344        Options +ExecCGI
 345</Location>
 346EOF
 347        fi
 348}
 349
 350mongoose_conf() {
 351        cat > "$conf" <<EOF
 352# Mongoose web server configuration file.
 353# Lines starting with '#' and empty lines are ignored.
 354# For detailed description of every option, visit
 355# http://code.google.com/p/mongoose/wiki/MongooseManual
 356
 357root            $fqgitdir/gitweb
 358ports           $port
 359index_files     gitweb.cgi
 360#ssl_cert       $fqgitdir/gitweb/ssl_cert.pem
 361error_log       $fqgitdir/gitweb/$httpd_only/error.log
 362access_log      $fqgitdir/gitweb/$httpd_only/access.log
 363
 364#cgi setup
 365cgi_env         PATH=$PATH,GIT_DIR=$GIT_DIR,GIT_EXEC_PATH=$GIT_EXEC_PATH
 366cgi_interp      $PERL
 367cgi_ext         cgi,pl
 368
 369# mimetype mapping
 370mime_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
 371EOF
 372}
 373
 374
 375script='
 376s#^(my|our) \$projectroot =.*#$1 \$projectroot = "'$(dirname "$fqgitdir")'";#;
 377s#(my|our) \$gitbin =.*#$1 \$gitbin = "'$GIT_EXEC_PATH'";#;
 378s#(my|our) \$projects_list =.*#$1 \$projects_list = \$projectroot;#;
 379s#(my|our) \$git_temp =.*#$1 \$git_temp = "'$fqgitdir/gitweb/tmp'";#;'
 380
 381gitweb_cgi () {
 382        cat > "$1.tmp" <<\EOFGITWEB
 383@@GITWEB_CGI@@
 384EOFGITWEB
 385        # Use the configured full path to perl to match the generated
 386        # scripts' 'hashpling' line
 387        "$PERL" -p -e "$script" "$1.tmp"  > "$1"
 388        chmod +x "$1"
 389        rm -f "$1.tmp"
 390}
 391
 392gitweb_css () {
 393        cat > "$1" <<\EOFGITWEB
 394@@GITWEB_CSS@@
 395
 396EOFGITWEB
 397}
 398
 399gitweb_js () {
 400        cat > "$1" <<\EOFGITWEB
 401@@GITWEB_JS@@
 402
 403EOFGITWEB
 404}
 405
 406gitweb_cgi "$GIT_DIR/gitweb/gitweb.cgi"
 407gitweb_css "$GIT_DIR/@@GITWEB_CSS_NAME@@"
 408gitweb_js  "$GIT_DIR/@@GITWEB_JS_NAME@@"
 409
 410resolve_full_httpd
 411mkdir -p "$fqgitdir/gitweb/$httpd_only"
 412
 413case "$httpd" in
 414*lighttpd*)
 415        lighttpd_conf
 416        ;;
 417*apache2*)
 418        apache2_conf
 419        ;;
 420webrick)
 421        webrick_conf
 422        ;;
 423*mongoose*)
 424        mongoose_conf
 425        ;;
 426*)
 427        echo "Unknown httpd specified: $httpd"
 428        exit 1
 429        ;;
 430esac
 431
 432start_httpd
 433url=http://127.0.0.1:$port
 434
 435if test -n "$browser"; then
 436        git web--browse -b "$browser" $url || echo $url
 437else
 438        git web--browse -c "instaweb.browser" $url || echo $url
 439fi