forked from php-enqueue/enqueue-dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre-commit
More file actions
executable file
·156 lines (120 loc) · 3.64 KB
/
pre-commit
File metadata and controls
executable file
·156 lines (120 loc) · 3.64 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
155
156
#!/usr/bin/env php
<?php
use Symfony\Component\Process\PhpExecutableFinder;
/**
* This script fixes code style for files which are going to be committed.
* If errors were found commit will be interrupted and errors will be fixed.
* Then you need to manually stage your changes and run commit again
*
* To install this hook just run "pre-commit -i"
*/
include_once __DIR__.'/../vendor/autoload.php';
$projectRootDir = realpath(__DIR__ . '/..');
if (!$projectRootDir) {
throw new \LogicException('The project root dir was not guessed');
}
$phpExecutableFinder = new PhpExecutableFinder();
$phpBin = $phpExecutableFinder->find();
if (!$phpBin) {
echo 'Php executable could not be found.'.PHP_EOL;
}
$options = getopt('i', array('install'));
if (isset($options['i']) || isset($options['install'])) {
install();
echo "Git hook successfully installed" . PHP_EOL;
exit(1);
}
$options = getopt('u', array('uninstall'));
if (isset($options['u']) || isset($options['uninstall'])) {
uninstall();
echo "Git hook successfully uninstalled" . PHP_EOL;
exit(1);
}
function install()
{
global $projectRootDir;
uninstall();
//create link
$link = $projectRootDir . '/.git/hooks/pre-commit';
exec(sprintf('ln -s %s %s', __FILE__, $link));
}
function uninstall()
{
global $projectRootDir;
//create link
$link = $projectRootDir . '/.git/hooks/pre-commit';
@unlink($link);
}
function getFilesToFix()
{
$files = array();
exec('git diff-index --name-only --cached --diff-filter=ACMR HEAD --', $files);
$stagedFiles = array_filter($files, function ($file) {
return (bool) preg_match('/\.(php|twig|translations\/*.yml)$/', $file);
});
echo sprintf('Found %s staged files', count($stagedFiles)).PHP_EOL;
return $stagedFiles;
}
function runPhpLint()
{
global $phpBin, $projectRootDir;
$filesWithErrors = array();
foreach (getFilesToFix() as $file) {
$output = '';
$returnCode = null;
exec(sprintf('%s -l %s 2>/dev/null', $phpBin, $projectRootDir.'/'.$file), $output, $returnCode);
if ($returnCode) {
$filesWithErrors[] = $file;
}
}
return $filesWithErrors;
}
function runPhpCsFixer()
{
global $phpBin, $projectRootDir;
$phpCsFixerBin = $projectRootDir . '/bin/php-cs-fixer';
if (!file_exists($phpCsFixerBin)) {
echo $phpCsFixerBin.' File could not be found. Make sure you run command from project\'s root directory'.PHP_EOL;
}
$filesWithErrors = array();
foreach (getFilesToFix() as $file) {
$output = '';
$returnCode = null;
exec(sprintf(
'%s %s fix %s --dry-run',
$phpBin,
$phpCsFixerBin,
$projectRootDir.'/'.$file
), $output, $returnCode);
if ($returnCode) {
$output = '';
exec(sprintf(
'%s %s fix %s',
$phpBin,
$phpCsFixerBin,
$projectRootDir.'/'.$file
), $output);
$filesWithErrors[] = $file;
}
}
return $filesWithErrors;
}
$phpSyntaxErrors = runPhpLint();
if ($phpSyntaxErrors) {
echo "Php syntax errors were found in next files:" . PHP_EOL;
foreach ($phpSyntaxErrors as $error) {
echo $error . PHP_EOL;
}
exit(1);
}
$phpCSFixerErrors = runPhpCsFixer();
if ($phpCSFixerErrors) {
echo "Incorrect coding standards were detected and fixed." . PHP_EOL;
echo "Please stash changes and run commit again." . PHP_EOL;
echo "List of changed files:" . PHP_EOL;
foreach ($phpCSFixerErrors as $error) {
echo $error . PHP_EOL;
}
exit(1);
}
exit(0);