2

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
Andrew
  • 14,204
  • 15
  • 60
  • 104
  • What if you add `-C $staging_path` to your first git commands? – VonC Oct 21 '14 at 08:11
  • Same thing. It seems `-C $staging_path` (and `-C $staging_repo`) fails, but `--git-dir` works fine. This would be fine, but it looks like `--git-dir` works for only a few git commands. – Andrew Oct 21 '14 at 08:33
  • Is your non-bare repo in `/tmp/git/origin` or `/tmp/git/origin/original`? – VonC Oct 21 '14 at 08:37
  • @VonC - I have edited the end of my question with the actual values of the four shell variables. I get the same values when doing a push as I do when I run the hook script manually. – Andrew Oct 21 '14 at 08:41
  • @VonC - Both actually. On my Mac, it was `/tmp/git/origin/original` but on Ubuntu it is `/tmp/git/origin`. The text I copied above was from my Mac. – Andrew Oct 21 '14 at 08:43
  • Ok. Can you unset `GIT_DIR` and `GIT_WORK_TREE`, just to be sure? (as in http://stackoverflow.com/q/5283262/6309 or http://stackoverflow.com/a/9905975/6309) – VonC Oct 21 '14 at 08:44
  • That did it! If you add this as an answer, I will gladly accept it. – Andrew Oct 21 '14 at 08:48

1 Answers1

3

I always prefer, to be sure, to unset GIT_DIR and GIT_WORK_TREE environment variables, in order to give git (through --git-dir or --work-tree, or both since git 1.8.5 with -C) the exact paths I want to use in a hook.

Those variables can be set by Git because of a Git event triggering a hook, as opposed to manually launching the same hook in a shell session.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250