我在Windows上执行Git挂钩时遇到问题。我有一个空的repo,在它的"hooks"文件夹中,我将以下内容放入"更新"answers"预推送"文件中,但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'
所以我知道"更新"是以某种方式执行的。当我删除那个文件时,推送工作得很好。
默认情况下,Git for Windows使用自己的bash
外壳的Windows端口执行钩子脚本。当然,Unix shell对%1
一无所知。据推测,Git for Windows有额外的技巧来检测"常见"的文件扩展名—例如CCD_ 3—并且在这种情况下采取替代路线。
我认为你对自己程序的修复是最好的,但另一种方法是重写你的脚本以读取
#!/bin/sh
c:/Programs/PHP/php.exe c:/Data/Scripts/git-pre-push.phpcli "$@"
(在Windows下,shebang这句话除了暗示下一个人编辑剧本内容的含义之外,没有什么特别的意义)。
在Windows上,通常使用特殊的'#!'(称为shebang)PHP脚本中的第一行并没有任何效果。然而,Windows上的Git能够识别shebang的第一行。您可以使用PHP编写如下的提交消息挂钩,它将在Windows上正常运行。
#!D:/php/php.exe
<?php
echo 'commit-msg';
exit( 0 );
查看有关windows 上git挂钩的更多信息
#!/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
我相信秘密就在shebang部分-在windows上,你需要提供sh.exe的完整路径,如下所示:
#!/bin/sh; C:/path/to/Git/bin/sh.exe
如果安装了cygwin,也可以指向位于cygwin/bin中的sh.exe或bash.exe您甚至可以使用cyqwin支持的其他脚本语言,例如ruby:
#!/usr/bin/env ruby; C:/path/to/cygwin/bin/ruby.exe
puts "RUBY HOOK RUNNING"