title: Git post-receive hook not working
body: <p>We're using git with a central repo (using Gitosis). I've created a post-receive hook to generate an email to the dev mailing list whenever changes are pushed to the central repo, and to generate documentation from the documentation folder in the git repo.</p>
  
  <p>Therefore, in ~git/ I've got a directory, we'll call it 'a' that contains a clone of the git repo. The post-receive hook looks like:</p>
  
  <pre><code>#!/bin/bash
  cd ~git/repositories/a.git
  . ~git/post-receive-email &amp;&gt; /dev/null
  ( cd ~git/a &amp;&amp; git pull &amp;&gt; ~git/pull_log.log &amp;&amp; php ~git/a/scripts/generate_markdown_documentation.php &amp;&gt; ~git/doc_log.log )
  </code></pre>
  
  <p>The email script is working, but the documentation generation is not. The content of pull_log.log is:</p>
  
  <pre><code>fatal: Not a git repository: '.'
  </code></pre>
  
  <p>Which makes me think that it's not changing to the correct directory in line 5 of the above script. Am I wrong? How can I get this to work?</p>
  
  <p><strong>Edit:</strong> I've updated the post-receive hook as suggested in the responses. The script is now:</p>
  
  <pre><code>#!/bin/bash
  function die {
    echo "$*" &gt;&amp;2; exit 1
  }
  
  function checkgit {
    [ -d "$1/.git" ] || die "$1 could not possibly be a git repo; $1/.git is not a dir"
  }
  
  
  cd ~git/repositories/a.git
  . ~git/post-receive-email &amp;&gt; /dev/null
  ( set -x
  checkgit ~git/a
  cd ~git/a
  checkgit .
  pwd
  git pull
  php ~git/a/scripts/generate_markdown_documentation.php )
  </code></pre>
  
  <p>And I get the following output from git push:</p>
  
  <pre><code>+ checkgit /var/git/a
  + '[' -d /var/git/a/.git ']'
  + cd /var/git/a
  + checkgit .
  + '[' -d ./.git ']'
  + pwd
  /var/git/a
  + git pull
  fatal: Not a git repository: '.'
  + php /var/git/a/scripts/generate_markdown_documentation.php
  </code></pre>
  
  <p>Any more help?</p>
  
  <p>Oh, and if I run the script myself, it works (I run it by saying hooks/post-receive)</p>
  
  <p>Discovered the problem, thanks to <a href="https://serverfault.com/questions/107608/git-post-receive-hook-with-git-pull-failed-to-find-a-valid-git-directory">serverfault</a> -- basically, environment variables <code>GIT_DIR</code> and <code>GIT_WORK_TREE</code> are set when the hook runs, and these affect git pull adversely.  Unsetting the variables fixes the problem.</p>