git-instaweb.shon commit git-tar-tree: don't RUN_SETUP (9cb90b8)
   1#!/bin/sh
   2#
   3# Copyright (c) 2006 Eric Wong
   4#
   5USAGE='[--start] [--stop] [--restart]
   6  [--local] [--httpd=<httpd>] [--port=<port>] [--browser=<browser>]
   7  [--module-path=<path> (for Apache2 only)]'
   8
   9. git-sh-setup
  10
  11case "$GIT_DIR" in
  12/*)
  13        fqgitdir="$GIT_DIR" ;;
  14*)
  15        fqgitdir="$PWD/$GIT_DIR" ;;
  16esac
  17
  18local="`git repo-config --bool --get instaweb.local`"
  19httpd="`git repo-config --get instaweb.httpd`"
  20browser="`git repo-config --get instaweb.browser`"
  21port=`git repo-config --get instaweb.port`
  22module_path="`git repo-config --get instaweb.modulepath`"
  23
  24conf=$GIT_DIR/gitweb/httpd.conf
  25
  26# Defaults:
  27
  28# if installed, it doesn't need further configuration (module_path)
  29test -z "$httpd" && httpd='lighttpd -f'
  30
  31# probably the most popular browser among gitweb users
  32test -z "$browser" && browser='firefox'
  33
  34# any untaken local port will do...
  35test -z "$port" && port=1234
  36
  37start_httpd () {
  38        httpd_only="`echo $httpd | cut -f1 -d' '`"
  39        if test "`expr index $httpd_only /`" -eq '1' || \
  40                                which $httpd_only >/dev/null
  41        then
  42                $httpd $fqgitdir/gitweb/httpd.conf
  43        else
  44                # many httpds are installed in /usr/sbin or /usr/local/sbin
  45                # these days and those are not in most users $PATHs
  46                for i in /usr/local/sbin /usr/sbin
  47                do
  48                        if test -x "$i/$httpd_only"
  49                        then
  50                                # don't quote $httpd, there can be
  51                                # arguments to it (-f)
  52                                $i/$httpd "$fqgitdir/gitweb/httpd.conf"
  53                                return
  54                        fi
  55                done
  56        fi
  57        if test $? != 0; then
  58                echo "Could not execute http daemon $httpd."
  59                exit 1
  60        fi
  61}
  62
  63stop_httpd () {
  64        test -f "$fqgitdir/pid" && kill `cat "$fqgitdir/pid"`
  65}
  66
  67while case "$#" in 0) break ;; esac
  68do
  69        case "$1" in
  70        --stop|stop)
  71                stop_httpd
  72                exit 0
  73                ;;
  74        --start|start)
  75                start_httpd
  76                exit 0
  77                ;;
  78        --restart|restart)
  79                stop_httpd
  80                start_httpd
  81                exit 0
  82                ;;
  83        --local|-l)
  84                local=true
  85                ;;
  86        -d|--httpd|--httpd=*)
  87                case "$#,$1" in
  88                *,*=*)
  89                        httpd=`expr "$1" : '-[^=]*=\(.*\)'` ;;
  90                1,*)
  91                        usage ;;
  92                *)
  93                        httpd="$2"
  94                        shift ;;
  95                esac
  96                ;;
  97        -b|--browser|--browser=*)
  98                case "$#,$1" in
  99                *,*=*)
 100                        browser=`expr "$1" : '-[^=]*=\(.*\)'` ;;
 101                1,*)
 102                        usage ;;
 103                *)
 104                        browser="$2"
 105                        shift ;;
 106                esac
 107                ;;
 108        -p|--port|--port=*)
 109                case "$#,$1" in
 110                *,*=*)
 111                        port=`expr "$1" : '-[^=]*=\(.*\)'` ;;
 112                1,*)
 113                        usage ;;
 114                *)
 115                        port="$2"
 116                        shift ;;
 117                esac
 118                ;;
 119        -m|--module-path=*|--module-path)
 120                case "$#,$1" in
 121                *,*=*)
 122                        module_path=`expr "$1" : '-[^=]*=\(.*\)'` ;;
 123                1,*)
 124                        usage ;;
 125                *)
 126                        module_path="$2"
 127                        shift ;;
 128                esac
 129                ;;
 130        *)
 131                usage
 132                ;;
 133        esac
 134        shift
 135done
 136
 137mkdir -p "$GIT_DIR/gitweb/tmp"
 138GIT_EXEC_PATH="`git --exec-path`"
 139GIT_DIR="$fqgitdir"
 140export GIT_EXEC_PATH GIT_DIR
 141
 142
 143lighttpd_conf () {
 144        cat > "$conf" <<EOF
 145server.document-root = "$fqgitdir/gitweb"
 146server.port = $port
 147server.modules = ( "mod_cgi" )
 148server.indexfiles = ( "gitweb.cgi" )
 149server.pid-file = "$fqgitdir/pid"
 150cgi.assign = ( ".cgi" => "" )
 151mimetype.assign = ( ".css" => "text/css" )
 152EOF
 153        test "$local" = true && echo 'server.bind = "127.0.0.1"' >> "$conf"
 154}
 155
 156apache2_conf () {
 157        test -z "$module_path" && module_path=/usr/lib/apache2/modules
 158        mkdir -p "$GIT_DIR/gitweb/logs"
 159        bind=
 160        test "$local" = true && bind='127.0.0.1:'
 161        echo 'text/css css' > $fqgitdir/mime.types
 162        cat > "$conf" <<EOF
 163ServerRoot "$fqgitdir/gitweb"
 164DocumentRoot "$fqgitdir/gitweb"
 165PidFile "$fqgitdir/pid"
 166Listen $bind$port
 167TypesConfig $fqgitdir/mime.types
 168DirectoryIndex gitweb.cgi
 169EOF
 170
 171        # check to see if Dennis Stosberg's mod_perl compatibility patch
 172        # (<20060621130708.Gcbc6e5c@leonov.stosberg.net>) has been applied
 173        if test -f "$module_path/mod_perl.so" && grep '^our $gitbin' \
 174                                "$GIT_DIR/gitweb/gitweb.cgi" >/dev/null
 175        then
 176                # favor mod_perl if available
 177                cat >> "$conf" <<EOF
 178LoadModule perl_module $module_path/mod_perl.so
 179PerlPassEnv GIT_DIR
 180PerlPassEnv GIT_EXEC_DIR
 181<Location /gitweb.cgi>
 182        SetHandler perl-script
 183        PerlResponseHandler ModPerl::Registry
 184        PerlOptions +ParseHeaders
 185        Options +ExecCGI
 186</Location>
 187EOF
 188        else
 189                # plain-old CGI
 190                list_mods=`echo "$httpd" | sed "s/-f$/-l/"`
 191                $list_mods | grep 'mod_cgi\.c' >/dev/null 2>&1 || \
 192                echo "LoadModule cgi_module $module_path/mod_cgi.so" >> "$conf"
 193                cat >> "$conf" <<EOF
 194AddHandler cgi-script .cgi
 195<Location /gitweb.cgi>
 196        Options +ExecCGI
 197</Location>
 198EOF
 199        fi
 200}
 201
 202script='
 203s#^\(my\|our\) $projectroot =.*#\1 $projectroot = "'`dirname $fqgitdir`'";#
 204s#\(my\|our\) $gitbin =.*#\1 $gitbin = "'$GIT_EXEC_PATH'";#
 205s#\(my\|our\) $projects_list =.*#\1 $projects_list = $projectroot;#
 206s#\(my\|our\) $git_temp =.*#\1 $git_temp = "'$fqgitdir/gitweb/tmp'";#'
 207
 208gitweb_cgi () {
 209        cat > "$1.tmp" <<\EOFGITWEB
 210@@GITWEB_CGI@@
 211EOFGITWEB
 212        sed "$script" "$1.tmp"  > "$1"
 213        chmod +x "$1"
 214        rm -f "$1.tmp"
 215}
 216
 217gitweb_css () {
 218        cat > "$1" <<\EOFGITWEB
 219@@GITWEB_CSS@@
 220EOFGITWEB
 221}
 222
 223gitweb_cgi $GIT_DIR/gitweb/gitweb.cgi
 224gitweb_css $GIT_DIR/gitweb/gitweb.css
 225
 226case "$httpd" in
 227*lighttpd*)
 228        lighttpd_conf
 229        ;;
 230*apache2*)
 231        apache2_conf
 232        ;;
 233*)
 234        echo "Unknown httpd specified: $httpd"
 235        exit 1
 236        ;;
 237esac
 238
 239start_httpd
 240test -z "$browser" && browser=echo
 241url=http://127.0.0.1:$port
 242$browser $url || echo $url