在 Windows 上执行 Git 钩子

2022-08-30 21:01:42

我在Windows上执行Git钩子时遇到问题。我有一个裸存储库,在它的“hooks”文件夹中,我将以下内容放入“更新”和“预推送”文件中,但PHP脚本从未执行过:

"c:/Programs/PHP/php.exe" c:/Data/Scripts/git-pre-push.phpcli %1

关于为什么不执行PHP脚本的任何想法?

在 Git 控制台窗口中,当我尝试将某些内容推送到裸存储库时,我会看到以下内容:

POST git-receive-pack (437 bytes)
remote: error: hook declined to update refs/heads/master
To https://myuser@mydomain/samplerepo
! [remote rejected] master -> master (hook declined)
error: failed to push some refs to 'https://myuser@mydomain/samplerepo'

...所以我知道“更新”正在以某种方式执行。当我删除该文件时,推送工作正常。


答案 1

默认情况下,Git for Windows 使用自己的 shell Windows 端口执行钩子脚本。当然,Unix shell 对 .据推测,Git for Windows有额外的黑客来检测“常见”文件扩展名 - 例如 - 并在这种情况下采取替代途径。bash%1.bat

我认为你对自己程序的修复是最好的,但另一种方法是重写你的脚本来阅读

#!/bin/sh
c:/Programs/PHP/php.exe c:/Data/Scripts/git-pre-push.phpcli "$@"

(Shebang行在Windows下没有真正的特殊意义,除了暗示下一个人编辑脚本关于其内容的含义)。


答案 2
#!/bin/sh
echo "executing pre-commit"

# Instructions:

# Put this file into your .git/hooks folder and set as executable 

 #- for Windows (attrib +x pre-commit)
 #- for ubuntu (chmod +x pre-commit)

# If you want to skip the hook just add the --no-verify: git commit --no-verify

# ---------------------------------------------

# Modify this
# LIST='list\|of\|words\|splitted\|by\|slash\|and\|pipe'
LIST="puts\|debugger;\|binding.pry\|alert(\|console.log("

if git rev-parse --verify HEAD >/dev/null 2>&1; then
    against=HEAD
else
    against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi

for FILE in `git diff-index --name-status --cached $against -- | cut -c3-` ; do
    # Check if the file contains one of the words in LIST
    if grep -w $LIST $FILE; then
      echo $FILE." has unwanted word. Please remove it. If you want to skip that then run git commit -m '"your comment"' --no-verify"
      exit 1
    fi
      done
exit

推荐