Heru S
Heru S

Reputation: 1323

Git Hook `post-receive` with `git pull` runs well via command line but returns empty string on automatic hook

Just like many, I am trying to create a post-receive hook on a server-side remote git. The following is my code:

cd [LIVE SITE DIR] && git pull origin master | wall

I have added the wall so that it can be broadcasted and I can receive the notice. One thing that I noticed is when I run ./post-receive, it all works fine.

Broadcast message from root (Tue Oct  8 14:58:13 2013):

Updating 7ebd925..1a25791
Fast-forward
 html/header.php | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

But, when I tried to do a git push , ./post-receive seems to be called but I received the following empty broadcast:

Broadcast message from root (Tue Oct  8 14:53:32 2013):

I have tried to include pwd, ls command into the post-receive and everything works fine for both situations. Except for the git pull that it seems to return an empty output.

Have gone through all the suggestions provided in SO but to no avail :(. Any help or suggestion is much appreciated.

Thanks!

Upvotes: 3

Views: 5188

Answers (2)

Heru S
Heru S

Reputation: 1323

The problem seems because one of the files in .git which is HEAD has root as the file owner. The above shell script was executed using another user.

Upvotes: 1

VonC
VonC

Reputation: 1324258

Make sure your post-receive hook unset GIT_DIR, or it will still refer to the git repo of the hook, instead of the git repo of the live site you just cd in.

cd [LIVE SITE DIR]
unset GIT_DIR
git pull

Or:

cd [LIVE SITE DIR]
env -i origin master

Or:

cd [LIVE SITE DIR]
git --git-dir=[LIVE SITE DIR]/.git pull

Upvotes: 11

Related Questions