msysgit error with hooks: "git error: cannot spawn .git/hooks/post-commit: No such file or directory"
Asked Answered
H

9

57

I'm having trouble getting post-recieve and post-commit hooks to work correctly with msysgit (Windows 7 Pro/32 and Ultimate/64). For post-commit hook I get the above error if I commit from either git-bash or the console, but it works fine if I commit through git-gui. For a post-recieve hook, all three give the same error.

I'm thinking this is some sort of permission or path error, but don't really have any clue where to start here.

Hymanhymen answered 17/4, 2011 at 23:34 Comment(0)
R
112

Add the SHEBANG to the first line of hook, like so:

#!/bin/sh
echo "executing post-commit"
exit 0

This had me stumped for a while as well and I saw that adding the shebang fixed it. In SVN world, while in *nix we have a "pre-commit" script and in Windows we had "pre-commit.bat" and SVN automatically picked up the bat file in Windows. Git doesn't seem to pick up a pre-commit.bat ( or any hook ) and adding the shebang to the hook file worked.

Raggedy answered 18/4, 2011 at 2:35 Comment(3)
And by that he means the very first line. I accidentally had an empty new-line at the top and was getting the same error.Ur
Just FYI - the hook file must also be ASCII encoded, I had a tool generating a unicode format hook file, which was getting me the same error since I had some formatting bytes before the bang.Deadman
Also FYI: On my system, I had a hook with a perl shebang. But the perl.exe was not in the path. After adding it to the env variable, the error went away.Ridinger
R
11

I'm using SourceTree and git LFS and had a similar issue: cannot spawn .git/hooks/pre-push.

The fix was to delete the pre-push file (opening it revealed it was badly corrupted) and restart SourceTree at which point it regenerates the pre-push file and everything is back to normal.

Rhynchocephalian answered 14/2, 2017 at 0:1 Comment(1)
Thank you very much. I had a power outage and for me it was the post-checkout file that was corrupted.Sherfield
C
6

If you have the SHEBANG and it still fails, make sure you have <path_to_git>\bin set in your path environment variable.

You'll probably also have <path_to_git>\cmd if you installed it to work from the command-line.

Capitulary answered 22/2, 2012 at 11:38 Comment(0)
F
3

This is an old question, but I've been fighting with this exact problem and this SO question popped up, so I thought it worth the effort to record what worked for me.

In short: I needed to run Apache as a regular user instead of Local System. This was on a legacy test VM I was playing with, so it was only running Windows XP, but it appears that at least on that platform (and possibly others), msysgit just doesn't work properly when running under the Local System account (presumably the root filesystem isn't mapped properly). As a result, no shebang line will work as git-http-backend simply can't execute any msysgit binaries (even with absolute Windows paths).

Switching Apache to run as a regular user account fixed this problem completely. Obviously you need to ensure that the user Apache is running as has permissions to read/write the git repositories, but beyond that, just make sure your shebang line is #!/bin/sh and everything should be copacetic.

Lastly, yeah, this is a big hammer. Ideally you'd be able to use something like suexec on Windows, but a quick googling doesn't indicate an obvious path forward, there. Of course, if anyone has any ideas, I'd be interested.

For now, this works for me, but it doesn't seem ideal.

Fault answered 28/8, 2012 at 14:22 Comment(2)
I believe the problem indeed is to have <path_to_git>\bin in your path. I used a regular user account, and got the same error. Once I have the bin in path, I can run as regular user account or Local System without any problem. In you case, you might have set the path in your regular user account only.Gonsalve
I'm curious as to where Apache fits into all of this. It isn't part of my workflow, but other than that all the symptoms match. Git's there in my global environment PATH (C:\Program Files (x86)\Git\bin and ..\cmd). Am I misunderstanding something?Thaliathalidomide
L
3

Got this in a repo using LFS, got rid of it with git lfs update --force

Lundy answered 24/1, 2020 at 11:29 Comment(0)
O
2

If someone, like me run into a similar problem with accessing git repositories through apache, you should set the PATH in Apache config, like:

SetEnv PATH "c:/Program Files (x86)/Git/bin;"
Overhappy answered 8/5, 2013 at 14:33 Comment(1)
Thanks @Xmister. We added this to httpd.conf and this appeared to address the issue.Pleurisy
S
1

Using tortoisegit and LFS, for me just had to remove the files inside of the .git/hooks folder.

Superdominant answered 20/6, 2018 at 3:58 Comment(0)
P
1

If you are using Android studio, you can remove this error by un-check the checkbox "Run Git hooks":

Un-check the checkbox "Run Git hooks", this will solve your problem

Plenitude answered 22/6, 2020 at 8:34 Comment(0)
M
0

For me, removing a comment line on front of the shebang line fixed the error. Oddly, the script ran fine from the shell, just errored out when run as a hook.

Margotmargrave answered 12/1, 2019 at 0:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.