windows 带有钩子的 msysgit 错误:“git 错误:无法生成 .git/hooks/post-commit:没有这样的文件或目录”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5697210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 08:17:53  来源:igfitidea点击:

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

windowsgitmsysgit

提问by Matt

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.

我无法让 post-receve 和 post-commit 挂钩与 msysgit(Windows 7 Pro/32 和 Ultimate/64)一起正常工作。对于 post-commit 钩子,如果我从 git-bash 或控制台提交,我会收到上述错误,但如果我通过 git-gui 提交,它工作正常。对于 post-receive hook,所有三个都会给出相同的错误。

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

我认为这是某种权限或路径错误,但真的不知道从哪里开始。

回答by manojlds

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

将 SHEBANG 添加到钩子的第一行,如下所示:

#!/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.

这也让我难住了一段时间,我看到添加 shebang 修复了它。在 SVN 世界中,在 *nix 中我们有一个“预提交”脚本,而在 Windows 中我们有“pre-commit.bat”,SVN 会自动在 Windows 中获取 bat 文件。Git似乎没有选择 pre-commit.bat (或任何钩子)并将shebang添加到钩子文件中。

回答by Jarrod Smith

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

我使用SourceTree和git LFS,有一个类似的问题:cannot spawn .git/hooks/pre-push

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

修复方法是删除pre-push文件(打开它发现它已严重损坏)并重新启动 SourceTree,此时它会重新生成pre-push文件,一切都恢复正常。

回答by Nick Robson

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

如果您有 SHEBANG 但它仍然失败,请确保您 <path_to_git>在路径环境变量中设置了 \bin。

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

<path_to_git>如果您安装它以从命令行工作,您可能还会拥有\cmd。

回答by user1630539

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.

这是一个古老的问题,但我一直在与这个确切的问题作斗争,并且出现了这个 SO 问题,所以我认为值得努力记录对我有用的东西。

In short: I needed to run Apacheas a regular user instead of Local System. This was on a legacy test VMI was playing with, so it was only running Windows XP, but it appears that at least on that platform (and possibly others), msysgitjust doesn't work properly when running under the Local Systemaccount (presumably the root filesystemisn'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 Windowspaths).

简而言之:我需要以Apache普通用户的身份而不是Local System. 这是在VM我正在玩的遗留测试中,所以它只是运行Windows XP,但似乎至少在该平台(可能还有其他平台)msysgit下,在Local System帐户下运行时无法正常工作(大概root filesystem没有映射适当地)。因此,没有任何shebang 行可以工作,因为git-http-backend 根本无法执行任何msysgit 二进制文件(即使使用绝对Windows路径)。

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/writethe gitrepositories, but beyond that, just make sure your shebangline is #!/bin/shand everything should be copacetic.

将 Apache 切换为以普通用户帐户运行完全解决了这个问题。显然,你需要确保的Apache作为运行的用户有权限read/writegit仓库,但除此之外,只要确保你的shebang线#!/bin/sh,一切都应该COPACETIC。

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

最后,是的,这是一个大锤子。理想情况下,您可以使用suexecon 之类的东西Windows,但快速googling并不表示明显的前进道路。当然,如果有人有任何想法,我会很感兴趣。

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

目前,这对我有用,但似乎并不理想。

回答by Xmister

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

如果像我这样的人在通过 apache 访问 git 存储库时遇到类似的问题,您应该在 Apache 配置中设置 PATH,例如:

SetEnv PATH "c:/Program Files (x86)/Git/bin;"

回答by Justin Hirsch

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

使用 tortoisegit 和 LFS,对我来说只需要删除 .git/hooks 文件夹中的文件。

回答by T Corman

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.

对我来说,删除 shebang 行前面的注释行修复了错误。奇怪的是,脚本在 shell 中运行良好,只是在作为钩子运行时出错。

回答by Hemaolle

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

在使用 LFS 的 repo 中得到了这个,摆脱了它 git lfs update --force