Skip to content

Commit 28d4db0

Browse files
committed
Refactoring git pre-commit hook to improve performance. (acquia#27)
1 parent c43430c commit 28d4db0

File tree

4 files changed

+143
-23
lines changed

4 files changed

+143
-23
lines changed

template/build/core/phing/build.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
<includepath classpath="${core.phing.dir}" />
55
<taskdef name="behat" classname="phingcludes.BehatTask"/>
66
<taskdef name="drush" classname="phingcludes.DrushTask"/>
7+
<taskdef name="filterFileListByFileSet" classname="phingcludes.FilterFileListByFileSetTask"/>
78
<taskdef name="phpVariable" classname="phingcludes.PhpVariableTask"/>
8-
<typedef name="fileInFileset" classname="phingcludes.FileInFilesetCondition"/>
99

1010
<!-- Include task partials -->
1111

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
3+
/**
4+
* Returns the intersection of a file list and set of filesets.
5+
*
6+
* Example usage:
7+
*
8+
* <filterFileListByFileSet fileList="${files}" returnProperty="filteredFileList" root="${repo.root}">
9+
* <fileset refid="files.php.custom.modules"/>
10+
* <fileset refid="files.php.custom.themes"/>
11+
* <fileset refid="files.php.tests"/>
12+
* </filterFileListByFileSet>
13+
*/
14+
require_once 'phing/Task.php';
15+
16+
class FilterFileListByFileSetTask extends Task {
17+
18+
/**
19+
* The return value.
20+
*
21+
* @var null
22+
*/
23+
protected $return_property = null;
24+
25+
public function setFileList($fileList)
26+
{
27+
$this->fileList = $fileList;
28+
}
29+
30+
public function setRoot($root)
31+
{
32+
$this->root = $root;
33+
}
34+
35+
/**
36+
* Nested adder, adds a set of files (nested fileset attribute).
37+
*
38+
* @param FileSet $fs
39+
* @return void
40+
*/
41+
public function addFileSet(FileSet $fs)
42+
{
43+
$this->filesets[] = $fs;
44+
}
45+
46+
/**
47+
* The Phing property the return code should be assigned to.
48+
*
49+
* @param string $str The Phing property.
50+
*
51+
* @return void
52+
*/
53+
public function setReturnProperty($str)
54+
{
55+
$this->return_property = $str;
56+
}
57+
58+
59+
/**
60+
* The main entry point method.
61+
*
62+
* @throws BuildException
63+
* @return bool $return
64+
*/
65+
public function main() {
66+
67+
if (!isset($this->fileList)) {
68+
throw new BuildException("You must set the file property.");
69+
}
70+
71+
if (!isset($this->root)) {
72+
throw new BuildException("You must set the project root.");
73+
}
74+
75+
if (count($this->filesets) == 0) {
76+
throw new BuildException("You must define a fileset.");
77+
}
78+
79+
$this->fileListFiles = array_map(array($this, 'prependProjectPath'), explode("\n", $this->fileList));
80+
$this->fileSetFiles = $this->getFilesetFiles();
81+
$filteredList = array_intersect($this->fileSetFiles, $this->fileListFiles);
82+
83+
// Return the Behat exit value to a Phing property if specified.
84+
if (!empty($this->return_property)) {
85+
$this->getProject()
86+
->setProperty($this->return_property, implode($filteredList, ','));
87+
}
88+
89+
return (bool) $filteredList;
90+
}
91+
92+
protected function prependProjectPath($relative_path) {
93+
return $this->root. DIRECTORY_SEPARATOR . $relative_path;
94+
}
95+
96+
/**
97+
* Return the list of files to parse
98+
*
99+
* @see PhpCodeSnifferTask
100+
*
101+
* @return string[] list of absolute files to parse
102+
*/
103+
protected function getFilesetFiles()
104+
{
105+
$files = array();
106+
107+
foreach ($this->filesets as $fs) {
108+
$dir = $fs->getDir($this->project)->getAbsolutePath();
109+
foreach ($fs->getDirectoryScanner($this->project)->getIncludedFiles() as $filename) {
110+
$file_path = $dir . DIRECTORY_SEPARATOR . $filename;
111+
$files[] = $file_path;
112+
}
113+
}
114+
115+
return $files;
116+
}
117+
}

template/build/core/phing/tasks/validate.xml

+22-18
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,35 @@
1515
</target>
1616

1717
<!-- Run code sniffer against specific files. -->
18-
<target name="validate:phpcs:file" description="Runs PHP Code Sniffer against a specific file.">
19-
<fail unless="file" message="Missing file parameter."/>
20-
<property name="file.path" value="${repo.root}/${file}"/>
18+
<target name="validate:phpcs:files" description="">
19+
<fail unless="files" message="Missing files parameter."/>
2120
<property name="phpcs.ruleset" value="${repo.root}/vendor/drupal/coder/coder_sniffer/Drupal/ruleset.xml"/>
2221

22+
<filterFileListByFileSet fileList="${files}" returnProperty="filteredFileList" root="${repo.root}">
23+
<fileset refid="files.php.custom.modules"/>
24+
<fileset refid="files.php.custom.themes"/>
25+
<fileset refid="files.php.tests"/>
26+
</filterFileListByFileSet>
27+
2328
<if>
24-
<fileInFileset file="${file.path}">
25-
<fileset refid="files.php.custom.modules"/>
26-
<fileset refid="files.php.custom.themes"/>
27-
<fileset refid="files.php.tests"/>
28-
</fileInFileset>
29+
<istrue value="${filteredFileList}"/>
2930
<then>
30-
<phpcodesniffer
31-
standard="${phpcs.ruleset}"
32-
showSniffs="false"
33-
showWarnings="true"
34-
haltonerror="true"
35-
haltonwarning="true"
36-
verbosity="1"
37-
file="${file.path}">
38-
<formatter type="full" usefile="false"/>
39-
</phpcodesniffer>
31+
<foreach list="${filteredFileList}" target="validate:phpcs:file" param="file"/>
4032
</then>
4133
</if>
34+
</target>
4235

36+
<target name="validate:phpcs:file" description="Runs PHP Code Sniffer against a specific file.">
37+
<fail unless="file" message="Missing file parameter."/>
38+
<property name="phpcs.ruleset" value="${repo.root}/vendor/drupal/coder/coder_sniffer/Drupal/ruleset.xml"/>
39+
<phpcodesniffer
40+
file="${file}"
41+
standard="${phpcs.ruleset}"
42+
showSniffs="false"
43+
showWarnings="true"
44+
haltonerror="true"
45+
haltonwarning="true"
46+
verbosity="1"/>
4347
</target>
4448

4549
<!-- Run code sniffer against all custom code. -->

template/scripts/git-hooks/pre-commit

+3-4
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@ PHPCS_BIN=vendor/bin/phpcs
77
if [ ! -f $PHPCS_BIN ];
88
then
99
echo "PHP Code Sniffer was not found in this project's bin directory. Please run composer install. "
10+
exit 1
1011
else
1112
echo "Sniffing staged files via PHP Code Sniffer."
1213
fi
1314

14-
for FILE in $LIST
15-
do
16-
${ROOT_DIR}bolt.sh validate:phpcs:file -Dfile=$FILE -q
17-
done
15+
16+
${ROOT_DIR}bolt.sh validate:phpcs:files -Dfiles="$LIST" -q
1817
exit 0;

0 commit comments

Comments
 (0)