在Windows上执行Git挂钩

时间:2013-08-16 15:53:40

标签: php windows git hook

我在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'

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

4 个答案:

答案 0 :(得分:16)

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

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

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

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

答案 1 :(得分:1)

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

#!D:/php/php.exe

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

有关git hooks on windows的更多信息

答案 2 :(得分:0)

#!/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

答案 3 :(得分:0)

我相信秘密就在shebang部分-在Windows上,您需要像下面这样提供sh.exe的完整路径:

#!/bin/sh; C:/path/to/Git/bin/sh.exe

如果已安装cygwin,则也可以指向cygwin / bin中的sh.exe或bash.exe。 您甚至可以利用cyqwin支持的其他脚本语言-例如红宝石:

#!/usr/bin/env ruby; C:/path/to/cygwin/bin/ruby.exe
puts "RUBY HOOK RUNNING"