title: How to change owner after executing post-update hook?
body: <p>Gitolite and a Web-server stored on one server. Directory <code>/var/www/site</code> is clone of repo "site". A Git user consist in group www-data. I have in <code>/home/git/repositories/site.git/hooks/post-update hook</code>:</p>
  
  <pre><code>#!/bin/sh    
  unset GIT_DIR
  cd /var/www/site &amp;&amp; git pull origin master
  </code></pre>
  
  <p>All worked perfectly, but after "pull" all updated or new files change owner to git:git. I don't know how change owner without privileges of root user. Any suggestions?</p>
  
  <p>I found good approach (post-update):</p>
  
  <pre><code>#!/bin/sh
  
  PATH=/usr/bin:/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin:$PATH
  PROJECT="projectname"
  GIT_URL="http://factory.domain.ru/git"
  
  git update-server-info
  
  if [ ! -f $1 ]; then
      exit 0
  fi
  
  while [ -n "$1" ]
  do
      REF=`echo $1 | awk --field-separator="/" '{print $2}'`
      if [ $REF = "branches" -o $REF = "heads" ]; then
          BRANCH=`echo $1 | awk --field-separator="/" '{print $3}'`
  
          if [ ! -d /srv/www/$PROJECT/repo/master ]; then
              mkdir -p /srv/www/$PROJECT/repo
              GIT_SSL_NO_VERIFY=true git clone $GIT_URL/$PROJECT /srv/www/$PROJECT/repo/master
          fi
  
          if [ ! -d /srv/www/$PROJECT/repo/$BRANCH ]; then
              GIT_SSL_NO_VERIFY=true git clone -b $BRANCH $GIT_URL/$PROJECT /srv/www/$PROJECT/repo/$BRANCH
          else
              cd /srv/www/$PROJECT/repo/$BRANCH
              GIT_SSL_NO_VERIFY=true git fetch origin
              GIT_SSL_NO_VERIFY=true git reset --hard origin/$BRANCH
              GIT_SSL_NO_VERIFY=true git clean -d -f
              GIT_SSL_NO_VERIFY=true git checkout
              GIT_SSL_NO_VERIFY=true git pull
          fi
      fi
      shift
  done
  </code></pre>
  
  <p>And "update":</p>
  
  <pre><code>#!/bin/sh
  
  refname="$1"
  oldrev="$2"
  newrev="$3"
  
  PROJECT="projectname"
  
  # --- Safety check
  if [ -z "$GIT_DIR" ]; then
          echo "Don't run this script from the command line." &gt;&amp;2
          echo " (if you want, you could supply GIT_DIR then run" &gt;&amp;2
          echo "  $0 &lt;ref&gt; &lt;oldrev&gt; &lt;newrev&gt;)" &gt;&amp;2
          exit 1
  fi
  
  if [ -z "$refname" -o -z "$oldrev" -o -z "$newrev" ]; then
          echo "Usage: $0 &lt;ref&gt; &lt;oldrev&gt; &lt;newrev&gt;" &gt;&amp;2
          exit 1
  fi
  
  # --- Check types
  # if $newrev is 0000...0000, it's a commit to delete a ref.
  zero="0000000000000000000000000000000000000000"
  if [ "$newrev" = "$zero" ]; then
          newrev_type=delete
  else
          newrev_type=$(git cat-file -t $newrev)
  fi
  
  BRANCH=`echo $1 | awk --field-separator="/" '{print $3}'`
  
  delete () {
      mv /srv/www/$PROJECT/repo/$BRANCH /srv/www/$PROJECT/repo/$BRANCH.removed_by_git
      rm -rf /srv/www/$PROJECT/repo/$BRANCH.removed_by_git
  }
  
  case "$refname","$newrev_type" in
          refs/heads/*,delete)
                  # delete branch
                  delete
                  ;;
          refs/remotes/*,delete)
                  # delete tracking branch
                  delete
                  ;;
  esac
  
  exit 0
  </code></pre>
  
  <p><a href="http://habrahabr.ru/post/158855/" rel="nofollow">habrahabr.ru</a></p>