php 在 Windows 上执行 Git 钩子

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18277429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:17:06  来源:igfitidea点击:

Executing Git hooks on Windows

phpwindowsgithook

提问by TheStoryCoder

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:

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

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

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

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

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

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

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

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

回答by kostix

By default, Git for Windows executes hook scripts using its own Windows port of the bashshell. Certainly, a Unix shell has no idea about %1. Supposedly, Git for Windows has extra hacks in place to detect "common" filename extensions — such as .bat— and take an alternate route in such a case.

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

I think your fix to your own program is the best, but another approach would be to rewrite your script to read

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

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

(the shebang line has no real special sense under Windows other than hinting the next person to edit the script about the meaning of its content).

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

回答by Rohit Jangid

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

回答by Arleth

I believe the secret is in the shebang part - on windows you will need to give the full path to your sh.exe like this:

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

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

If you have cygwin installed you can also point to sh.exe or bash.exe located in cygwin/bin You can even utilize other scripting languages supported via cyqwin - e.g. ruby:

如果您安装了 cygwin,您还可以指向位于 cygwin/bin 中的 sh.exe 或 bash.exe 您甚至可以使用 cyqwin 支持的其他脚本语言 - 例如 ruby​​:

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

回答by zhangyu12

On Windows, normally the special '#!' (called shebang) first line in PHP script has no effect . However, Git on Windows is able to recognize the shebang first line. You can write a commit-msg hook as below using PHP, it will be running on Windows properly.

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

#!D:/php/php.exe

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

See more on git hooks on windows

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