18

I'm having trouble executing Git hooks on Windows. I have a bare repo and in it's "hooks" folder I put the following into both the "update" and "pre-push" files but the PHP script is never being executed:

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

Any ideas as to why the PHP script isn't executed?

In the Git console window I see the following when I try to push something to the bare repo:

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'

...so I know that the "update" is somehow being executed. When I remove that file the push works just fine.

4

4 回答 4

26

默认情况下,Windows 版 Git 使用自己的bashshell 端口执行钩子脚本。当然,Unix shell 不知道%1. 据说,Windows 版 Git 有额外的技巧来检测“常见”文件扩展名——例如.bat——并在这种情况下采取替代路线。

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

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

(shebang 行在 Windows 下没有真正的特殊意义,只是提示下一个人编辑脚本关于其内容的含义)。

于 2013-08-16T16:31:03.220 回答
1

在 Windows 上,通常是特殊的 '#!' (称为shebang)PHP脚本中的第一行没有效果。但是,Windows 上的 Git 能够识别 shebang 第一行。您可以使用 PHP 编写如下的 commit-msg 钩子,它将在 Windows 上正常运行。

#!D:/php/php.exe

<?php
echo 'commit-msg';
exit( 0 );    

查看更多 关于 Windows 上的 git 钩子的信息

于 2020-03-19T13:50:02.913 回答
1
#!/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
于 2018-07-14T07:19:11.093 回答
0

我相信秘密在于 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"
于 2020-02-04T09:21:48.150 回答