Search code examples
msysgit

How do I execute a post-receive hook on msysgit on Windows hosted by Apache?


I'm setting up a Git server on a Windows host. I have installed the latest Apache, and have it working with msysGit. I'm not using SSH at all; I can push and pull through HTTP.

Now I want to add a post-receive hook to notify my build server, but I can't figure out how to do that. I see the sample hook scripts in the repository on the server, but I'm confused about what to do there. Do I put a Windows batch file there, named post-receive.bat, or do something else?

I'm a bit fuzzy on details of what this is all doing, but Apache is executing c:\Program Files\git\libexec\git-core\git-http-backend.exe when it sees a Git URL. Is git-http-backend.exe going to trigger the post-receive hook?

Update I'm getting closer. Here's my hook, in hooks/post-receive in my repo:

#!/c/Program Files/Git/bin/sh
curl http://mybuildserver:8080/job/Whazzup/build

I changed the shebang from #!/bin/sh because on Windows I don't have that. Now in the Apache error log I get the message error: cannot spawn hooks/post-receive: No such file or directory

Incidentally, Git bash's chmod does not seem to work. But I was able to get the permission on post-receive to rwxr-xr-x by renaming the sample file.

Update I changed the shebang line back to #!/bin/sh, but I still get the same error in the Apache error log: error: cannot spawn hooks/post-receive: No such file or directory. As a test I opened a Git bash prompt in the hooks folder, and executed the hook with ./post-receive, and it worked.

Update Now I'm wondering if I have a different problem. Taking VonC's advice, I tried running Apache httpd from the command line under my own account, instead of as a service under LocalSystem. Still the same thing. Pushing and pulling work fine, but the hook doesn't execute. Then I tried getting Apache out of the equation. From a Git bash prompt on the same computer as the repo, I did a clone (via filesystem), modify, commit, and push. But the hook still didn't execute.

Update OK, I had a silly problem in my hook script, but now at least it executes when I push to the repo from the same computer (via filesystem). But not when I push through Apache. Apache is now running under a regular account, and the Apache account has full control of the repository. The push works fine, but the post-receive hook doesn't execute.


Solution

  • Apache is executing c:\Program Files\git\libexec\git-core\git-http-backend.exe when it sees a Git URL. Is git-http-backend.exe going to trigger the post-receive hook?

    No, it will pass the command (clone, push, pull) to git itself.
    The post-receive hook will be executed after the push has been completed, and it is a bash (unix shell) script, as illustrated in " post-receive hook on Windows - GIT_WORK_DIR: no such file or directory ".

    See also " git windows post pull " to see where you can create that post-receive script (in the .git/hooks of your repo): it has nothing to do with your http Apache service in front of the repos.


    Regarding the error message "cannot spawn hooks/post-receive: No such file or directory", you can refer to " msysgit error with hooks: "git error: cannot spawn .git/hooks/post-commit: No such file or directory" ":

    • The shebang must be #!/bin/sh
    • Apache must run as a regular user instead of Local System, in order to benefit from the environment variables defined for said regular user.
    • <path_to_git>\bin must be in the PATH