Git pre-receive hook启动PHP CodeSniffer

rza*_*jac 8 php git codesniffer

我想检查使用PHP CodeSniffer提交到我的远程git存储库的代码,如果代码标准有任何问题,请拒绝它.有没有人有一个例子如何在git远程存储库上使用它或者可能的例子如何使用预接收钩子?谢谢.

Ruf*_*nus 3

也许这给你指出了正确的方向:(原文来自: http: //www.squatlabs.de/versionierung/arbeiten-git-hooks德语)

#!/usr/bin/php
<?php

$output = array();
$rc     = 0;
exec('git rev-parse --verify HEAD 2> /dev/null', $output, $rc);
if ($rc == 0)  $against = 'HEAD';
else           $against = '4b825dc642cb6eb9a060e54bf8d69288fbee4904';

exec('git diff-index --cached --name-only '. $against, $output);

$needle            = '/(\.php|\.module|\.install)$/';
$exit_status = 0;

foreach ($output as $file) {
        if (!preg_match($needle, $file)) {
                // only check php files
                continue;
        }

        $lint_output = array();
        $rc              = 0;
        exec('php -l '. escapeshellarg($file), $lint_output, $rc);
        if ($rc == 0) {
                continue;
        }
        # echo implode("\n", $lint_output), "\n";
        $exit_status = 1;
}

exit($exit_status);
Run Code Online (Sandbox Code Playgroud)

您必须编辑 exec 行 exec('php -l... 以指向您的 codeniffer 安装。