Git pre-commit hook not working on windows

别说谁变了你拦得住时间么 提交于 2020-01-01 18:20:11

问题


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)

回答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.



来源:https://stackoverflow.com/questions/34263689/git-pre-commit-hook-not-working-on-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!