Search code examples
gitshellshgit-pullcd

git pull ignores current work directory in shell script


I'm trying to get some sort of auto-deployment working.

My current post-update hook looks like follows:

#!/bin/sh

for ref in $@
do
        if [ $ref = "refs/heads/master" ]
        then
                echo "Deploying $ref to dev-domain"
                cd ~/www/dev-domain
                echo "" > system/lock
                if ! git pull --ff-only
                then
                        echo "Failed to pull"
                        exit
                fi
                if ! system/migrateDatabase
                then
                        echo "Failed to migrate"
                        exit
                fi
                rm system/lock
        fi
done

But all I get is this:

remote: Deploying refs/heads/master to dev-domain
remote: fatal: Not a git repository: '.'
remote: Failed to pull

The file lock-file will be placed in the correct place, though.

So, to me it seems like git pull somehow ignored the current working directory... How do I get around this? Or am I missing something different?


Solution

  • You need to unset GIT_DIR before the git pull.

    If you don't, git would use the variable GIT_DIR instead of PWD.
    cd-ing changes the PWD not the GIT_DIR.

    As mention in Mark Longair's article "Missing git hooks documentation", GIT_DIR is set by hooks.

    See more in "this answer by Chris Johnsen.