Git预先接收钩子以启动PHP CodeSniffer [英] Git pre-receive hook to launch PHP CodeSniffer
问题描述
我想用PHP CodeSniffer来检查提交给我的远程git仓库的代码,并在代码标准出现问题时拒绝它。有没有人有一个例子如何使用它在git远程存储库或者例如如何使用它与预接收挂钩?谢谢。
I'd like to check code committed to my remote git repository with PHP CodeSniffer and reject it if there are any problems code standards. Does anyone have an example how to use it on git remote repository or maybe example how to use it with pre-receive hook? Thanks.
推荐答案
也许这一点你在正确的方向:(Orginal from: http://www.squatlabs.de/versionierung/arbeiten-git-hooks 德文)
Maybe this point you in the right direction: (Orginal from: http://www.squatlabs.de/versionierung/arbeiten-git-hooks in German)
#!/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);
您必须编辑exec行exec('php -l ...指向您的codesniffer安装。
You will have to edit the exec line exec('php -l... to point to your codesniffer installation.
这篇关于Git预先接收钩子以启动PHP CodeSniffer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!