-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpre-commit
executable file
·181 lines (131 loc) · 4.42 KB
/
pre-commit
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/php
<?php
echo PHP_EOL;
require('hook-lib.php');
//Get all the file involved in the commit
$arrCommitFiles = getCommitFileList();
//## Running PHP -l on the project
//########################################
echo "Checking PHP syntax error ...";
echo PHP_EOL;
if (PHP_PATH === false) {
echo ' ' . '√ Not configured, skipping.';
echo PHP_EOL;
} else {
//Validate the configuration.
if(!file_exists(PHP_PATH)){
echo "/!\\ PHP could not be located at " . PHPUNIT_PATH . ". Check the .git/hooks/hook-config.php file.";
echo PHP_EOL;
exit(FAILURE);
}
//Check files
$needle = '/(\.php)$/';
foreach ($arrCommitFiles as $file) {
if (!preg_match($needle, $file) || !file_exists($file)) {
// only check php files
continue;
}
echo ' ' . ' - ' . $file . PHP_EOL;
$lint_output = array();
$rc = 0;
exec(PHP_PATH . ' -l '. escapeshellarg($file) . ' 2> /dev/null', $lint_output, $rc);
if ($rc == SUCCESS) {
continue;
} else {
echo PHP_EOL;
echo '(-!-) Syntax check failed :' . PHP_EOL;
echo implode(PHP_EOL, $lint_output);
exit(FAILURE);
}
}
echo ' ' . '√ No syntax error!';
echo PHP_EOL;
}
//## Running PHP CodeSniffer on the project
//########################################
echo "Checking PHP coding standard violation ...";
echo PHP_EOL;
if (PHPCS_PATH === false) {
echo ' ' . '√ Not configured, skipping.';
echo PHP_EOL;
} else {
//Validate the configuration.
if (!file_exists(PHPCS_PATH)) {
echo "/!\\ PHPCodeSniffer could not be located at " . PHPUNIT_PATH . ". Check the .git/hooks/hook-config.php file.";
echo PHP_EOL;
exit(FAILURE);
}
//Check files
$needle = '/(\.php|\.module|\.install)$/';
foreach ($arrCommitFiles as $file) {
if (!preg_match($needle, $file) || !file_exists($file)) {
// only check php files
continue;
}
echo ' ' . ' - ' . $file . PHP_EOL;
$output = array();
$rc = 0;
exec(PHPCS_PATH . ' -l '. escapeshellarg($file) . ' 2> /dev/null', $output, $rc);
if ($rc == SUCCESS) {
continue;
} else {
echo PHP_EOL;
echo '(-!-) Coding standard violation found :' . PHP_EOL;
echo implode(PHP_EOL, $output);
exit(FAILURE);
}
}
echo ' ' . '√ No coding standard violation!';
echo PHP_EOL;
}
//## Running PHPUnit test on the project
//########################################
echo "Processing PHPUnit tests suite ...";
echo PHP_EOL;
if (PHPUNIT_PATH === false) {
echo ' ' . '√ Not configured, skipping.';
echo PHP_EOL;
} else {
//Validate the configuration.
if (!file_exists(PHPUNIT_PATH)) {
echo "/!\\ PHPUnit could not be located at " . PHPUNIT_PATH . ". Check the .git/hooks/hook-config.php file.";
echo PHP_EOL;
exit(FAILURE);
}
$branchName = getCurrentBranchName();
$arrAllowedBranch = explode(';', PHPUNIT_BRANCH);
if (PHPUNIT_BRANCH !== false && !in_array($branchName, $arrAllowedBranch)) {
echo ' ' . '√ This branch does not require unit testing, skipping.';
echo PHP_EOL;
} else {
// execute unit tests
$phpUnitCmd = PHPUNIT_PATH . ' --tap';
if (PHPUNIT_CONFIG !== false) {
if (!file_exists(PHPUNIT_CONFIG)) {
echo "/!\\ PHPUnit configuration could not be located at " . PHPUNIT_CONFIG . ". Check the .git/hooks/hook-config.php file.";
echo PHP_EOL;
exit(FAILURE);
}
$phpUnitCmd .= ' -c ' . PHPUNIT_CONFIG;
}
$output = array();
$rc = 0;
exec($phpUnitCmd, $output, $rc); // cwd is assumed here
// if the build failed, output a summary and fail
if ($rc == FAILURE) {
// output the status
echo '(-!-) Test suite failed :' . PHP_EOL;
foreach ($output as $line) {
if (!in_array(substr($line, 0, 3), array('ok ', 'TAP', ' -'))) {
echo $line . PHP_EOL;
}
}
// abort the commit
exit(FAILURE);
}
echo ' ' . '√ All test succeeded!';
echo PHP_EOL;
}
}
echo PHP_EOL;
exit(SUCCESS);