2

I've created a repository in /var/repo/myrepo.git using

git init --bare

and a post-receive hooks with inside:

#!/bin/sh
git --work-tree=/var/www/domain.com --git-dir=/var/repo/myrepo.git checkout -f

Then:

chmod +x post-receive

Now, push from local to remote works properly and I know that because I can see my local branch in

/var/repo/myrepo.git/refs/heads

But the problem is that the hooks do not work.

Then if I run from terminal:

git --work-tree=/var/www/domain.com --git-dir=/var/repo/myrepo.git checkout -f

all the file from the repo are copied to /var/www/domain.com

So why the hook do not work but the command inside do if executed from bash?

UPDATE_1

As suggested, inside /var/repo/myrepo.git/hooks/post-receive I'm using:

git --work-tree=/var/www/domain.com --git-dir=/var/repo/myrepo.git checkout -f >/tmp/mylogfile 2>/tmp/mylogfile

The file is executable becouse I can run it with:

./post-receive

This copy all the repo file inside my warking dir as expected, but the file "/tmp/mylogfile" is empty.

ciccioassenza
  • 233
  • 1
  • 7
  • 17
  • could it be that `git` is in the `$PATH` in your terminal but is not when the hook is run? – eckes Sep 21 '15 at 10:49
  • Honestly I don't know...how can I check that? I've installed git with "apt-get install" – ciccioassenza Sep 21 '15 at 10:54
  • inside your `post-receive` hook do an `echo $PATH > /tmp/mypath && type git >> /tmp/mypath`. If the `git` command is known, `type git` will print out the path. If the command is not known, `type git` will print an error. Do a `git push`, then inspect the content of `/tmp/mypath`. If the file is missing, the hook wasn't run at all. Try what `type git` prints out on your normal terminal. – eckes Sep 21 '15 at 10:57
  • I've done what you suggest but "ls -l /tmp" return 0 files... "type git" on terminale returns "/usr/bin/git" – ciccioassenza Sep 21 '15 at 11:08
  • your hook is not run. that's why there is no file. http://stackoverflow.com/questions/8206023/git-post-receive-hook-not-running might give you some useful hints – eckes Sep 21 '15 at 12:39
  • From the link you posted I see the solving answear is: "The issue was related to the mounting of the filesystem. The partition was mounted as noexec, and therefore no files could be executed. This caused the hook not to run. I removed the noexec flag and it now works just fine." The file has execute privilege (chmod +x post-receive) but I don't know how to remove the "noexec" from the partition... – ciccioassenza Sep 21 '15 at 13:34

3 Answers3

2

If the hooks/post-receive doesn't work and you have set the file as executable correctly and you see this message:

Everything up-to-date

It can be because that there is no new changes in your push. So change a file and commit it and push again.

If you saw this message:

remote: fatal: You are on a branch yet to be born

You can solve it by first push your master branch before pushing your desired branch:

git push serverRemote master

After this, the files in the work-tree directory will be the master branch files. If you want to update the files after each push with your desired branch files, you must checkout on your desired branch from the bare repository in server (you must enter your work-tree directory in the command):

git --work-tree=/path/to/work/tree checkout yourDesiredBranch
Ashkan
  • 846
  • 11
  • 20
1

You could try debugging your script by outputting stdout and stderr to a file

#!/bin/sh
git --work-tree=/var/www/domain.com --git-dir=/var/repo/myrepo.git checkout -f >/tmp/mylogfile 2>/tmp/mylogfile

If /tmp/mylogfile is empty after running this hook, the script is not run at all, or it will contain a helpful error message.

edi9999
  • 19,701
  • 13
  • 88
  • 127
  • I've tried what you suggest but no file is created under /tmp. I understand the script is not run but I don't know why since I've done (chmod +x post-receive) – ciccioassenza Sep 21 '15 at 13:36
  • Where is the hook file located ? Can you give the full path ? – edi9999 Sep 21 '15 at 14:18
  • "/var/repo/myrepo.git/hooks/post-receive". I push from sourcetree (with no error) using "admin" user: ssh://admin@mydomain.com/var/repo/myrepo.git ; "admin" user is the owner of both the repo and the working directory. – ciccioassenza Sep 21 '15 at 14:26
1

I had the same issue as the OP and realized this answer was my solution:

https://stackoverflow.com/a/32337074/2100999

Instead of post-receive I named the hook post-receieve. When I renamed the file, my hook worked perfectly.

The Lesson: Spelling matters!

jakesantos
  • 111
  • 4