GitHub for Windows 预提交钩子

9
我们有一个开发团队,他们在Git管理方面使用50/50的GitHub for windows和Bash shell。我们实施了一个预提交钩子(旨在运行单元测试并在测试失败时失败提交)。作为简化版本,我附上了下面削减版,展示了我们的问题。
#!/bin/sh
exit 1

如果我们从bash shell尝试提交,那么提交会失败,这是预期的。但是,如果我们从GitHub for windows应用程序执行相同的提交,则可以成功提交到本地存储库。
那么,有人知道我们如何从GitHub应用程序中获得相同的功能吗?不幸的是,我们不能让用户放弃使用该应用程序,这现在是一个明显的漏洞。
感谢您的帮助。

虽然我喜欢 GitHub for Windows 的想法,但我个人发现它有时很慢甚至有些错误。我目前不推荐生产团队使用它。 - orlp
2
你尝试过联系 GitHub for Windows 团队寻求支持吗?你可以发送邮件至 support@github.com 与他们联系。 - ksol
@nightcracker - 新版本似乎更加稳定,但由于它很容易下载和安装,我们需要堵上这个漏洞。 - Steve Goodman
@ksol - 谢谢,我们已经尝试联系他们了,但是还没有收到回复,所以现在向社区开放。 - Steve Goodman
4个回答

23

很抱歉带来不好的消息,但是GitHub for Windows不支持预提交挂钩(pre-commit hooks),因为它使用libgit2进行提交。


3
您说得很正确,我们从 Github 收到了以下回复:不幸的是,GitHub for Windows 不支持 pre-commit hooks,因为我们实际上不使用 git.exe 进行提交(我们使用一个名为 libgit2 的项目进行提交,它比 git.exe 快得多,但不支持某些功能)。如果您依赖这些功能,可能需要改用 Git Shell 进行提交。 - Steve Goodman
13
我相当确信我写了那个回复 :) - Ana Betts
Github for Windows 只无法执行 pre-commit 钩子吗? - Bogdan Kulynych
虽然某些其他钩子可能当前正在执行,但它们将来可能不会执行。 - Ana Betts

2
使用Git shell可以设置提交钩子。我已经使用PowerShell成功设置了一些提交钩子。我发现了一个PowerShell脚本(链接),它可以进行代码检查,我将其扩展以运行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

1

现在不再将答案添加为当前选定的答案。我能够编写一个预提交钩子,它可以在GitHub Desktop for Windows、SourceTree for Windows和Git Bash for Windows上运行。

我不确定Windows提交钩子是否需要一个确切的sh.exe参考,但我包含了一个确切的sh.exe参考,如下所示:

#!C:/Program\ Files/Git/usr/bin/sh.exe

而且它运行良好!


0

您可以考虑使用Visual Studio(或您的构建工具)的预构建事件,但是如果需要完全自动化,则也存在一些问题。这里有一个示例用法。 现在,该解决方案还需要在路径上安装git.exe,但用户不需要与其交互。如果有比我更聪明的人能够解决写作中提到的反斜杠问题,那将是很好的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接