-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre-commit
More file actions
executable file
·154 lines (127 loc) · 3.86 KB
/
pre-commit
File metadata and controls
executable file
·154 lines (127 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env php
<?php
//项目的路径
if(is_dir(dirname(__DIR__) . '/vendor')) {
define('PROJECT_ROOT', dirname(__DIR__));
} else {
define('PROJECT_ROOT', dirname(dirname(dirname(__DIR__))));
}
define('PHP_CS_CHECK_CMD', PROJECT_ROOT . '/vendor/bin/phpcs'); //执行检查的命令
define('PHP_CS_FIX_CMD', PROJECT_ROOT . '/vendor/bin/phpcbf'); //修复的命令
define('PHP_CS_RULESET', __DIR__ . '/fp-ruleset.xml'); //检查和修复使用的rule set,为空时使用默认的
define('AUTO_FIX_CS', TRUE); //是否自动修复文件
if (!is_file(PHP_CS_CHECK_CMD) || !is_file(PHP_CS_FIX_CMD)) {
PreCommit::colorEcho('Inspection or repair script does not exist, the hook is not executed', 'yellow');
exit(0);
}
if (!is_file(PHP_CS_RULESET)) {
PreCommit::colorEcho('rule set configuration file does not exist, the hook is not executed', 'yellow');
exit(0);
}
//获取需要提交的php文件
$lChangeFile = PreCommit::getChangeFile();
if (!$lChangeFile) {
exit(0);
}
$bError = FALSE;
do {
if (!AUTO_FIX_CS) {
//如果不自动修复,就只做phpcs
PreCommit::displayTitle('PHP code style check:');
$lFail = PreCommit::csCheck($lChangeFile);
} else {
//如果自动修复,就直接先调用phpcbf,再phpcs
PreCommit::displayTitle('PHP code style check and Try to fix:');
foreach ($lChangeFile as $iKey => $lFilePath) {
$sRealPath = PROJECT_ROOT . '/' . $lFilePath;
$bOk = PreCommit::csCheckOne($sRealPath)[1];
if ($bOk) {
echo 'Check: ', $lFilePath . " OK\n";
unset($lChangeFile[$iKey]);
} else {
$bError = TRUE;
echo 'Fix: ', $lFilePath;
PreCommit::fixPhpFile($sRealPath);
echo " DONE\n";
}
}
$lFail = PreCommit::csCheck($lChangeFile);
}
//如果有错误则终止
if ($lFail) {
$bError = TRUE;
break;
}
if ($bError) {
//可以提交
PreCommit::displayTitle('Have fixed, please commit again');
} else {
//可以提交
PreCommit::displayTitle('Commit to git');
}
} while (FALSE);
exit($bError ? 1 : 0);
class PreCommit {
public static function displayTitle($sMsg) {
static $iNum = 0;
$iNum++;
self::colorEcho("\n${iNum}. ${sMsg}", 'green');
}
public static function getChangeFile() {
$lExclude = require_once __DIR__ . '/exclude.php';
$lDiffFile = [];
exec('git diff --cached --name-only ' . PROJECT_ROOT, $lDiffFile, $retVal);
//过滤掉非php文件,
$lChangeFile = array_filter(
$lDiffFile,
function($sFileName) use ($lExclude) {
foreach ($lExclude as $sExclude) {
if (strpos(PROJECT_ROOT . '/' . $sFileName, PROJECT_ROOT . '/' . ltrim($sExclude, '/')) === 0) {
return FALSE;
}
}
return is_file(PROJECT_ROOT . '/' . $sFileName) && substr($sFileName, -4) === '.php';
}
);
return $lChangeFile;
}
public static function csCheck($lFileList) {
$lFail = [];
foreach ($lFileList as $lFilePath) {
list($lOutput, $bOk) = self::csCheckOne(PROJECT_ROOT . '/' . $lFilePath);
if (!$bOk) {
echo implode("\n", $lOutput);
$lFail[] = $lFilePath;
}
}
if ($lFail) {
self::colorEcho("\n\nSummary Error : " . count($lFail) . ' file(s)', 'violet');
}
return $lFail;
}
public static function csCheckOne($sFile) {
exec(PHP_CS_CHECK_CMD . (PHP_CS_RULESET ? ' -n --standard=' . PHP_CS_RULESET . ' ' : ' ') . $sFile, $lOutput, $retVal);
return [
$lOutput,
$retVal === 0,
];
}
public static function fixPhpFile($sPath) {
exec(PHP_CS_FIX_CMD . (PHP_CS_RULESET ? ' --standard=' . PHP_CS_RULESET . ' ' : ' ') . $sPath, $lOutput);
}
public static function colorEcho($sContent, $color = 'white') {
static $lColor = [
'black' => '30',
'red' => '31',
'green' => '32',
'yellow' => '33',
'blue' => '34',
'violet' => '35',
'cyan' => '36',
'white' => '37',
];
$color = isset($lColor[$color]) ? $lColor[$color] : 37;
$sCmd = "echo \"\033[" . $color . "m" . $sContent . "\n" . " \033[0m\"";
passthru($sCmd);
}
}