2

I have a post-receive hook inside a Git repository that clones the repository into another directory and then cds into that directory.

#!/bin/bash --login

GIT_REPO="$HOME/oliverjash.me.git"

source "$HOME/.bash_profile"

checkout () {
  BRANCH="$1"
  TMP_GIT_CLONE="$2"

  git clone $GIT_REPO $TMP_GIT_CLONE
  cd $TMP_GIT_CLONE
  git status
}

checkout master "$HOME/tmp/oliverjash.me"
checkout project "$HOME/tmp/project.oliverjash.me"

exit

If I run this script whilst logged in to SSH, git status works fine. However, when the script is executed as the post-receive hook, git status reports this:

remote: fatal: Not a git repository: '.'

I can't understand why this is!

  • If you `echo $(pwd)` before `git status` is it the directory you expect? If it is, is there a git repository in that directory? – asm Jan 30 '13 at 18:00
  • Yes, I do, and if I run `ls -la` I can clearly see `.git`! –  Jan 30 '13 at 18:01

2 Answers2

1

If you really want to be sure the git command will run properly, you can add:

  • --work-tree=$TMP_GIT_CLONE
  • --git-dir=$TMP_GIT_CLONE/.git

That way, the git commands will know where is the working tree and the git repo to consider.

git --work-tree=... --git-dir=... clone ...

Or, since git 1.8.5, as detailed in this answer:

git -C=$TMP_GIT_CLONE clone ...
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Interesting. How come it doesn't just use the repository that is in the CWD? –  Jan 30 '13 at 20:45
  • 1
    @OliverJosephAsh because the the `GIT_DIR` is set at the execution of the script, not at the `cd` done within the script. But setting both working tree and git dir explicitly is always preferable in scripts/hooks: no ambiguity there. – VonC Jan 30 '13 at 20:50
0

I needed to do unset GIT_DIR at the top of the script.