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