这是为了规范上线代码自己写的一个php脚本,在这里做一下备份,以备不时之需。
<?php
//获取提交的参数
$fp = fopen('php://stdin', 'r');
$input = fgets($fp);
$params = explode(" ", $input);
$old_commitid = isset($params[0]) ? $params[0] : '';
$commitid = isset($params[1]) ? $params[1] : '';
//这里是新创建分支的情况
if ($old_commitid == '0000000000000000000000000000000000000000') {
$cmd = "git diff --cached --name-only $commitid";
$file_names = shell_exec($cmd);
} else {
$cmd = "git diff --name-only $commitid $old_commitid";
$file_names = shell_exec($cmd);
}
$files = explode("\n", $file_names);
$phpcs_path = '/opt/codesniffer/vendor/bin';
$tmp_content_path = "/tmp/tmp_content.php";
$err_str = '';
if ($files) {
foreach ($files as $_file) {
$_file = ltrim($_file, 'remote:');
$_file = trim($_file);
if (!empty($_file)) {
if (filter_special($_file)) {
continue;
}
$tmp_content = shell_exec("git show $commitid:$_file");
if (match_special($_file)) {
$tmp_content = str_replace("<?php", "<?php\nnamespace testnamespace;", $tmp_content);
}
file_put_contents($tmp_content_path, $tmp_content);
$cmd = '/opt/codesniffer/vendor/bin/phpcs /tmp/tmp_content.php --standard=PSR2';
$verify_ret = shell_exec($cmd);
$verify_ret = shell_exec("$phpcs_path/phpcs $tmp_content_path --standard=PSR2");
$verify_ret = str_replace("$tmp_content_path", "$_file", $verify_ret);
$err_str .= $verify_ret;
}
}
}
if (file_exists($tmp_content_path)) {
unlink($tmp_content_path);
}
echo $err_str;
/**
* 过滤特殊文件
* @param [type] $filepath [description]
* @return [type] [description]
*/
function filter_special($filepath) {
if (empty($filepath)) {
return true;
}
$special_str = [
'Bootstrap.php',
'web/index.php',
'api/index.php',
'admin/index.php',
'client/index.php',
'seller/index.php',
'app/views',
'app/modules',
'app/plugins',
'app/source',
'README.md',
'web/conf/app.ini',
'library/helper',
'CMQ'
];
foreach ($special_str as $_special) {
if (strpos($filepath, $_special) !== false) {
return true;
}
}
return false;
}
/**
* 框架controller不能加namespace
* [match_special description]
* @param [type] $filepath [description]
* @return [type] [description]
*/
function match_special($filepath) {
if (empty($filepath)) {
return false;
}
$special_str = [
'controllers',
'Controller.php',
'Index.php'
];
foreach ($special_str as $_special) {
if (strpos($filepath, $_special) !== false) {
return true;
}
}
return false;
}