Git预先提交钩子不能在Windows上工作 [英] Git pre-commit hook not working on windows

查看:276
本文介绍了Git预先提交钩子不能在Windows上工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个git pre-commit挂钩,但在提交到本地repo后,脚本不会被调用。这是它的样子,它是在python中,钩子文件的名称被称为pre-commit,没有任何扩展名:

  #!C:/ Users / al / AppData / Local / Programs / Python / Python35-32 python 

import sys,os

sys.exit(1)


解决方案

您的预提交钩可能会遭受以下一项或多项问题:

shebang行是否正确?



shebang行应该是可执行文件的完整路径用于运行脚本。请参阅以了解何时shebang线被使用(或未被使用)。在pre-commit钩子的情况下,只有在脚本上设置了可执行位时才会使用shebang行。



处理shebang行中的空格

h3>

如果解释器的完整路径中有空格,请引用整个路径,如下所示:

 #!C:/ Users / al / AppData / Local / Programs / Python / Python35-32 python

或者,像这样转义路径中的空间:

 #! C:/ Users / al / AppData / Local / Programs / Python / Python35-32\ python 



据报道,在msysgit上使用Windows脚本的python脚本在解释shebang行时存在问题。以下是解决方法。将python precommit-hook(例如 precommit.py )重命名为其他内容,并将以下内容用作预提交钩子:

 #!/ bin / sh 
python .git / hooks / precommit.py $ *



预提交钩子脚本是否可执行?



(这并不严格适用于当前问题,因为OP是在windows上的,但为了完整起见,我将在这里留下下面的内容)

如果没有设置pre-commit钩子,它将不会被执行可执行。我在本地测试了这个,下面的输出证明了这一点:

  $ cd〜/ test-git 
$ cat .git / hooks / pre-commit
#!/ usr / local / bin / python
导入sys,os
打印哟!这是预先提交的钩子
sys.exit(1)

$ chmod -x .git / hooks / pre-commit
$ git commit -amTest
[master root-commit)0b1edce]测试
1个文件已更改,0个插入(+),0个删除( - )
创建模式100644 a

$ chmod + x .git / hooks / pre-commit
$ git commit -am测试
哟!这是预先提交的钩子

另外,从 https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks


要启用钩子脚本,请将文件放入
.git目录的hooks子目录中(没有任何扩展名)
且可执行


I have a git pre-commit hook, but after commiting to the local repo, the script does not get called. This is how it looks, it is in python, the name of the hook file is called pre-commit without any extension:

#!C:/Users/al/AppData/Local/Programs/Python/Python35-32 python

import sys, os

sys.exit(1)

解决方案

Your pre-commit hook might suffer from one or more of the following issues:

Is the shebang line correct?

The shebang line should be the complete path to the executable which is used to run the script. See this to find out when the shebang line is used (or not-used). In the case of the pre-commit hook, the shebang line will be used only when the executable bit is set on the script.

Handling spaces in the shebang line

If there is a space in the interpreter's full path, either quote the whole path like so:

#!"C:/Users/al/AppData/Local/Programs/Python/Python35-32 python"

or, escape the space in the path like so:

#! C:/Users/al/AppData/Local/Programs/Python/Python35-32\ python

Using a python script on Windows with msysgit

msysgit has been reported to have problems interpreting the shebang line. The following is a workaround. Rename the python precommit-hook (example precommit.py) to something else, and use the following as the precommit-hook:

#!/bin/sh
python .git/hooks/pre-commit.py $* 

Is the pre-commit hook script executable?

(This is not strictly applicable to the current question, since the OP is on windows, but for the sake of completeness, I will leave the following here)

The pre-commit hook will not be executed if it is not set to be executable. I tested this locally and the following output proves that:

$ cd ~/test-git 
$ cat .git/hooks/pre-commit
#!/usr/local/bin/python
import sys, os
print "yo! this is the pre-commit hook"
sys.exit(1)

$ chmod -x .git/hooks/pre-commit
$ git commit -am "Test"
[master (root-commit) 0b1edce] Test
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 a

$ chmod +x .git/hooks/pre-commit
$ git commit -am "Test"
yo! this is the pre-commit hook

Also, from https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks

To enable a hook script, put a file in the hooks subdirectory of your .git directory that is named appropriately (without any extension) and is executable.

这篇关于Git预先提交钩子不能在Windows上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆