20

I have a central git repo set up using gitolite.

I want to set up a hook such that whenever a user pushes to the repo, it performs a pull elsewhere followed by some automated testing.

So far, I only want to it perform the pull.

In the hooks directory I created the following script names post-update:

#!/bin/sh  
cd /home/git/www/epicac
git pull

When I invoke this script using ./post-update, it does exactly what I want.

However, whenever it's invoked automatically as I hook, I get: fatal: Not a git repository: '.'

Any idea why this might be happening?

Josh Lee
  • 171,072
  • 38
  • 269
  • 275
Kevin Dolan
  • 421
  • 1
  • 4
  • 7

4 Answers4

28

You have various diagnostics to run as suggested in this SO answer.

In particular, check out the the value of GIT_DIR and GIT_WORK_TREE.

While the hook is running, GIT_DIR and (if the worktree can't be inferred from GIT_DIR) GIT_WORK_TREE are set.
That means your pull won't run with the repository in the directory you changed to.


See also blog post Using Git Inside a Git Hook:

Eventually we got our linux guru over and he noticed that the environment under which the git user runs is totally different when inside a hook.
Gitolite does a bunch of things to the env, but the one that was screwing us up was the setting of the GIT_DIR.
After we figured that out, the solution was as easy as:

ENV.delete 'GIT_DIR'

in our ruby script that is triggered by the 'post-receive' hook.


Same deal in Git Tip: Auto update working tree via post-receive hook, but with an elegant way out of this:

The solution?
It turns out the post-receive hook starts out with the GIT_DIR environment variable set to the repo/.git folder, so no matter what path you 'cd' into it will always try to run any following git commands there.
Fixing this is simply a matter of unsetting the GIT_DIR
(thanks to Ulrich Petri for the elegant env -i solution):

#!/bin/sh
cd ..
env -i git reset --hard
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
7

How about specifying the --git-dir.

#!/bin/sh
cd /home/git/www/epicac
git --git-dir=.git pull
satomacoto
  • 11,349
  • 2
  • 16
  • 13
1

Never mind... found it here: https://serverfault.com/questions/107608/git-post-receive-hook-with-git-pull-failed-to-find-a-valid-git-directory

Community
  • 1
  • 1
Kevin Dolan
  • 421
  • 1
  • 4
  • 7
  • 2
    VonC linked that in his above answer. either accept your answer or his one so it'll be helpful to other people looking at this question – corroded Apr 27 '11 at 10:49
0

This should do the trick in just one line:

git -C /home/git/www/epicac pull
Fran Marzoa
  • 4,293
  • 1
  • 37
  • 53