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