This is very confusing.
I have a git post-update hook script running on a bare repository. Its job is to tell a downstream (non-bare) git repo to update itself (sort of a push-to-deploy script for a versioned staging server). If I run the script from the terminal, it works fine. But when it is run by git after a push, I get an error that the git paths are wrong.
I've tried this on Ubuntu 14.04 + git 1.91 and Mac git 1.9.3 (Apple Git-50)
I'm clearly doing something wrong but I don't know enough about git hooks and can't see where to start.
Here's the script:
#!/bin/sh
staging_path="/tmp/git/copy"
staging_repo="$staging_path/.git"
staging_branch=$(git --git-dir=$staging_repo rev-parse --abbrev-ref HEAD)
upstream_branch=$(git rev-parse --abbrev-ref HEAD)
if [ "$staging_branch" != "master" ]
then
echo "Staging site is not on master. Cannot initiate pull."
exit 0
fi
if [ "$upstream_branch" != "master" ]
then
echo "Main repo is not on master -- not sure why that would be. Cannot initiate pull."
exit 0
fi
git -C $staging_path fetch --all
echo "git status after fetch --all:"
echo $(git -C $staging_path status)
git -C $staging_path reset --hard origin/master
echo ""
echo "git status after reset --hard:"
echo $(git -C $staging_path status)
This is what happens after I do a push:
$ git push origin master
Counting objects: 5, done.
Writing objects: 100% (3/3), 272 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: fatal: Not a git repository: '.'
remote: git status after fetch --all:
remote: fatal: Not a git repository: '.'
remote:
remote: fatal: Not a git repository: '.'
remote:
remote: git status after reset --hard:
remote: fatal: Not a git repository: '.'
remote:
To /tmp/git/origin/original.git
8eef13d..96c9847 master -> master
But when I run this manually, I see it complete normally:
$ ./post-update
Fetching origin
remote: Counting objects: 8, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 6 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (6/6), done.
From /tmp/git/origin/original
4e92291..96c9847 master -> origin/master
git status after fetch --all:
On branch master Your branch is behind 'origin/master' by 2 commits, and can be fast-forwarded. (use "git pull" to update your local branch) nothing to commit, working directory clean
HEAD is now at 96c9847 seven
git status after reset --hard:
On branch master Your branch is up-to-date with 'origin/master'. nothing to commit, working directory clean
Edit: I have logged out the values of the shell variables just to make sure they were giving what I thought. Those values are:
remote: $staging_path: /tmp/git/copy
remote: $staging_repo: /tmp/git/copy/.git
remote: $staging_branch: master
remote: $upstream_branch: master