Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
GitHub for Windows预提交钩子_Git_Github - Fatal编程技术网

GitHub for Windows预提交钩子

GitHub for Windows预提交钩子,git,github,Git,Github,我们有一个开发团队,在GitHub for windows和Bash shell的Git管理中各占一半 我们实现了一个预提交钩子(设计用于运行单元测试,并在测试失败时使提交失败)。作为一个简化版,我附上了一个精简版,下面演示我们的问题 #!/bin/sh exit 1 如果尝试从bashshell提交,则按预期提交失败。但是,如果我们从GitHub for windows应用程序执行相同的提交,它将成功地提交到本地repo 那么,有人知道我们如何从GitHub应用程序中获得相同的功能吗?不幸的

我们有一个开发团队,在GitHub for windows和Bash shell的Git管理中各占一半

我们实现了一个预提交钩子(设计用于运行单元测试,并在测试失败时使提交失败)。作为一个简化版,我附上了一个精简版,下面演示我们的问题

#!/bin/sh
exit 1
如果尝试从bashshell提交,则按预期提交失败。但是,如果我们从GitHub for windows应用程序执行相同的提交,它将成功地提交到本地repo

那么,有人知道我们如何从GitHub应用程序中获得相同的功能吗?不幸的是,我们无法将用户从应用程序中移开,这显然是一个漏洞


感谢您的帮助。

很抱歉带来了坏消息,但GitHub for Windows不支持预提交挂钩,因为它使用libgit2进行提交

使用Git shell,您可以拥有提交挂钩。我在PowerShell中找到了一些提交钩子。我发现了一个执行lint的脚本,我将其扩展为运行phpunit和phpcs(路径是硬编码的,因此需要进行调整):

预提交文件:

#!/bin/sh
echo 
exec powershell.exe -ExecutionPolicy RemoteSigned -File '.\.git\hooks\pre-commit-hook.ps1'
exit
pre-commit.ps1文件:

###############################################################################
#
# PHP Syntax Check for Git pre-commit hook for Windows PowerShell
#
# Author: Vojtech Kusy <wojtha@gmail.com>
# Author: Chuck "MANCHUCK" Reeves <chuck@manchuck.com>
#
###############################################################################

### INSTRUCTIONS ###

# Place the code to file "pre-commit" (no extension) and add it to the one of 
# the following locations:
# 1) Repository hooks folder - C:\Path\To\Repository\.git\hooks
# 2) User profile template   - C:\Users\<USER>\.git\templates\hooks 
# 3) Global shared templates - C:\Program Files (x86)\Git\share\git-core\templates\hooks
# 
# The hooks from user profile or from shared templates are copied from there
# each time you create or clone new repository.

### SETTINGS ###

# Path to the php.exe
$php_exe = "C:\php\php.exe";

# Path to the phpcs
$php_cs = "C:\Includes\PEAR\phpcs.bat";

# Path to the phpunit
$php_unit = "C:\Includes\PEAR\phpunit.bat";

# Path to the phpunit bootstrap file
$bootstrap = "tests\bootstrap.php";

# Flag, if set to 1 require test file to exist, set to 0 to disable
$requireTest = 1;

# Extensions of the PHP files 
$php_ext = "php|phtml"

# Flag, if set to 1 git will unstage all files with errors, set to 0 to disable
$unstage_on_error = 0;

### FUNCTIONS ###

function php_syntax_check {
    param([string]$php_bin, [string]$extensions, [int]$reset) 

    $err_counter = 0;

    write-host "Pre-commit PHP syntax check:" -foregroundcolor "white" -backgroundcolor "black"

    git diff-index --name-only --cached HEAD -- | foreach {             
        if ($_ -match ".*\.($extensions)$") {
            $file = $matches[0];
            $errors = & $php_bin -l $file   
            $testFileExists = (Test-Path $file -PathType Leaf)
            write-host $file ": "  -foregroundcolor "gray"  -backgroundcolor "black" -NoNewline
            if ($testFileExists) {
                if ($errors -match "No syntax errors detected in $file") {
                    write-host "OK!" -foregroundcolor "green" -backgroundcolor "black"
                }
                else {              
                    write-host "ERROR! " $errors -foregroundcolor "red" -backgroundcolor "black"
                    if ($reset) {
                        git reset -q HEAD $file
                        write-host "Unstaging ..." -foregroundcolor "magenta" -backgroundcolor "black"
                    }
                    $err_counter++
                }
            } else {
                write-host "OK! (file deleted)" -foregroundcolor "green" -backgroundcolor "black"
            }
        }
    }

    if ($err_counter -gt 0) {
        write-host "Some File(s) have syntax errors. Please fix then commit" -foregroundcolor "red" -backgroundcolor "black"
        exit 1
    }    
}

function php_cs_check {
    param([string]$php_cs, [string]$extensions, [int]$reset) 

    $err_counter = 0;

    write-host "Pre-commit PHP codesniffer check:" -foregroundcolor "white" -backgroundcolor "black"

    git diff-index --name-only --cached HEAD -- | foreach {     
        if ($_ -match ".*\.($extensions)$") {
            $file = $matches[0];

            write-host $file ": "  -foregroundcolor "gray"  -backgroundcolor "black" -NoNewline
            if ($file -match "tests\/") {
                write-host "PASSED! (test file)" -foregroundcolor "green" -backgroundcolor "black"
            } else {
                $errors = & $php_cs --standard=Zend $file           

                if ($LastExitCode) {
                    write-host "FAILED! (contains errors)"  -foregroundcolor "red" -backgroundcolor "black"
                    if ($reset) {
                        git reset -q HEAD $file
                        write-host "Unstaging ..." -foregroundcolor "magenta" -backgroundcolor "black"
                    }
                    $err_counter++
                } else {                
                    write-host "PASSED!" -foregroundcolor "green" -backgroundcolor "black"
                }
            }
        }
    }

    if ($err_counter -gt 0) {
        write-host "Some File(s) are not following proper codeing standards. Please fix then commit" -foregroundcolor "red" -backgroundcolor "black"
        exit 1
    }    
}

function php_unit_check {
    param([string]$php_unit, [string]$bootstrap, [string]$extensions, [int]$reset, [int]$requireTest) 

    $err_counter = 0;

    write-host "Pre-commit PHP unit check:" -foregroundcolor "white" -backgroundcolor "black"

    git diff-index --name-only --cached HEAD -- | foreach {     
        if ($_ -match ".*\.($extensions)$") {
            $file = $matches[0];

            write-host $file ": "  -foregroundcolor "gray"  -backgroundcolor "black" -NoNewline
            if ($file -match "tests\/") {
                write-host "SKIPPED! (test file)" -foregroundcolor "green" -backgroundcolor "black"
            } elseif ($file -match ".*Bootstrap.php") {
                write-host "SKIPPED! (bootstrap file)" -foregroundcolor "green" -backgroundcolor "black"
            } elseif ($file -match "([application|library\\NDX].*)(.($extensions))$") {

                $testFile = 'tests/' + $matches[1] + "Test.php";
                $testFileExists = (Test-Path $testFile -PathType Leaf)

                if ($testFileExists) {
                    $errors = & $php_unit --bootstrap $bootstrap $testFile
                    if ($LastExitCode) {
                        write-host "FAILED! (" $testFile ")"  -foregroundcolor "red" -backgroundcolor "black"
                        if ($reset) {
                            git reset -q HEAD $file
                            write-host "Unstaging ..." -foregroundcolor "magenta" -backgroundcolor "black"
                        }
                        $err_counter++
                    } else {
                        write-host "PASSED!" -foregroundcolor "green" -backgroundcolor "black"

                    }
                } elseif($requireTest) {
                    write-host "FAILED! Test file Not found: (" $testFile ")"  -foregroundcolor "red" -backgroundcolor "black"
                    if ($reset) {
                        git reset -q HEAD $file
                        write-host "Unstaging ..." -foregroundcolor "magenta" -backgroundcolor "black"
                    }
                    $err_counter++
                } else {
                    write-host "PASSED! (Test file not found and not required)" -foregroundcolor "darkGreen" -backgroundcolor "black"
                }
            } else {
                write-host "IGNORED!" -foregroundcolor "darkGreen" -backgroundcolor "black"
            }
        }
    }

    if ($err_counter -gt 0) {
        write-host "Some File(s) failed unit testing. Please fix then commit" -foregroundcolor "red" -backgroundcolor "black"
        exit 1
    }    
}
### MAIN ###

php_syntax_check $php_exe "php|phtml" $unstage_on_error
write-host
php_cs_check $php_cs "php" $unstage_on_error
write-host
php_unit_check $php_unit $bootstrap "php" $unstage_on_error $requireTest
###############################################################################
#
#Windows PowerShell Git预提交钩子的PHP语法检查
#
#作者:Vojtech Kusy
#作者:Chuck“Manchick”Reeves
#
###############################################################################
###指示###
#将代码放入文件“pre-commit”(无扩展名)并将其添加到
#下列地点:
#1)存储库挂钩文件夹-C:\Path\To\Repository\.git\hooks
#2)用户配置文件模板-C:\Users\\.git\templates\hooks
#3)全局共享模板-C:\ProgramFiles(x86)\Git\share\Git core\templates\hooks
# 
#从用户配置文件或共享模板中复制挂钩
#每次创建或克隆新存储库时。
###背景###
#指向php.exe的路径
$php_exe=“C:\php\php.exe”;
#通往phpcs的路径
$php\u cs=“C:\Includes\PEAR\phpcs.bat”;
#通往phpunit的道路
$php\u unit=“C:\Includes\PEAR\phpunit.bat”;
#phpunit引导文件的路径
$bootstrap=“tests\bootstrap.php”;
#标志,如果设置为1要求测试文件存在,则设置为0禁用
$requireTest=1;
#PHP文件的扩展名
$php_ext=“php | phtml”
#标志,如果设置为1 git,将取消显示所有有错误的文件,设置为0将禁用
$unstage\u on\u错误=0;
###功能###
函数php\u语法检查{
参数([string]$php_bin,[string]$extensions,[int]$reset)
$err_计数器=0;
编写主机“预提交PHP语法检查”:-foregroundcolor“白色”-backgroundcolor“黑色”
git diff index--仅限名称--缓存头--| foreach{
如果($\匹配“*\($extensions)$”){
$file=$matches[0];
$errors=&$php_bin-l$文件
$testFileExists=(测试路径$file-路径类型叶)
写入主机$file“:”-前底色“灰色”-背景色“黑色”-非蓝色
如果($testFileExists){
if($errors-匹配“在$file中未检测到语法错误”){
写主机“OK!”-背景颜色“绿色”-背景颜色“黑色”
}
否则{
写入主机“ERROR!”$errors-foregroundcolor“red”-backgroundcolor“black”
如果($重置){
git重置-q头$file
写主机“Unstaging…”-背景色“洋红”-背景色“黑色”
}
$err_计数器++
}
}否则{
写主机“OK!(文件已删除)”-背景颜色“绿色”-背景颜色“黑色”
}
}
}
如果($err_计数器-gt 0){
write host“某些文件有语法错误。请修复然后提交”-foregroundcolor“红色”-backgroundcolor“黑色”
出口1
}    
}
函数php\u cs\u check{
参数([string]$php_cs,[string]$extensions,[int]$reset)
$err_计数器=0;
编写主机“预提交PHP代码嗅探器检查”:-foregroundcolor“白色”-backgroundcolor“黑色”
git diff index--仅限名称--缓存头--| foreach{
如果($\匹配“*\($extensions)$”){
$file=$matches[0];
写入主机$file“:”-前底色“灰色”-背景色“黑色”-非蓝色
如果($文件-匹配“测试\/”){
写入主机“通过!”(测试文件)“-背景颜色“绿色”-背景颜色“黑色”
}否则{
$errors=&$php_cs--standard=Zend$file
if($LastExitCode){
写入主机“失败!(包含错误)”-foregroundcolor“红色”-backgroundcolor“黑色”
如果($重置){
git重置-q头$file
写主机“Unstaging…”-背景色“洋红”-背景色“黑色”
}
$err_计数器++
}否则{
写主机“通过!”-背景颜色“绿色”-背景颜色“黑色”
}
}
}
}
如果($err_计数器-gt 0){
write host“某些文件未遵循正确的编码标准。请修复并提交”-foregroundcolor“红色”-backgroundcolor“黑色”
出口1
}    
}
函数php\u单元检查{
参数([string]$php_unit,[string]$bootstrap,[string]$extensions,[int]$reset,[int]$requireTest)
$err_计数器=0;
编写主机“预提交PHP单元检查”:-foregroundcolor“白色”-backgroundcolor“黑色”
git diff index--仅限名称--缓存头--| foreach{
如果($\匹配“*\($extensions)$”){
$file=$matches[0];
写入主机$file“:”-前底色“灰色”-背景色“黑色”-非蓝色
如果($文件-匹配“测试\/”){
“写入主机”已跳过!
#!C:/Program\ Files/Git/usr/bin/sh.exe