Skip to content

Commit 3662148

Browse files
committed
Merge branch 'feature/new-filter-git-staged' of https://github.com/jrfnl/PHP_CodeSniffer
2 parents 01de622 + 926127f commit 3662148

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
111111
<file baseinstalldir="PHP/CodeSniffer" name="ExactMatch.php" role="php" />
112112
<file baseinstalldir="PHP/CodeSniffer" name="Filter.php" role="php" />
113113
<file baseinstalldir="PHP/CodeSniffer" name="GitModified.php" role="php" />
114+
<file baseinstalldir="PHP/CodeSniffer" name="GitStaged.php" role="php" />
114115
</dir>
115116
<dir name="Generators">
116117
<file baseinstalldir="PHP/CodeSniffer" name="Generator.php" role="php" />

src/Filters/GitStaged.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* A filter to only include files that have been staged for commit in a Git repository.
4+
*
5+
* This filter is the ideal companion for your pre-commit git hook.
6+
*
7+
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
8+
* @copyright 2018 Juliette Reinders Folmer. All rights reserved.
9+
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
10+
*/
11+
12+
namespace PHP_CodeSniffer\Filters;
13+
14+
use PHP_CodeSniffer\Util;
15+
16+
class GitStaged extends ExactMatch
17+
{
18+
19+
20+
/**
21+
* Get a list of blacklisted file paths.
22+
*
23+
* @return array
24+
*/
25+
protected function getBlacklist()
26+
{
27+
return [];
28+
29+
}//end getBlacklist()
30+
31+
32+
/**
33+
* Get a list of whitelisted file paths.
34+
*
35+
* @return array
36+
*/
37+
protected function getWhitelist()
38+
{
39+
$modified = [];
40+
41+
$cmd = 'git diff --cached --name-only -- '.escapeshellarg($this->basedir);
42+
$output = [];
43+
exec($cmd, $output);
44+
45+
$basedir = $this->basedir;
46+
if (is_dir($basedir) === false) {
47+
$basedir = dirname($basedir);
48+
}
49+
50+
foreach ($output as $path) {
51+
$path = Util\Common::realpath($path);
52+
if ($path === false) {
53+
// Skip deleted files.
54+
continue;
55+
}
56+
57+
do {
58+
$modified[$path] = true;
59+
$path = dirname($path);
60+
} while ($path !== $basedir);
61+
}
62+
63+
return $modified;
64+
65+
}//end getWhitelist()
66+
67+
68+
}//end class

0 commit comments

Comments
 (0)