diff --git a/package.xml b/package.xml index c4e04e1372..e50ac7209f 100644 --- a/package.xml +++ b/package.xml @@ -111,6 +111,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> + diff --git a/src/Filters/GitStaged.php b/src/Filters/GitStaged.php new file mode 100644 index 0000000000..fcb92c3d97 --- /dev/null +++ b/src/Filters/GitStaged.php @@ -0,0 +1,68 @@ + + * @copyright 2018 Juliette Reinders Folmer. All rights reserved. + * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence + */ + +namespace PHP_CodeSniffer\Filters; + +use PHP_CodeSniffer\Util; + +class GitStaged extends ExactMatch +{ + + + /** + * Get a list of blacklisted file paths. + * + * @return array + */ + protected function getBlacklist() + { + return []; + + }//end getBlacklist() + + + /** + * Get a list of whitelisted file paths. + * + * @return array + */ + protected function getWhitelist() + { + $modified = []; + + $cmd = 'git diff --cached --name-only -- '.escapeshellarg($this->basedir); + $output = []; + exec($cmd, $output); + + $basedir = $this->basedir; + if (is_dir($basedir) === false) { + $basedir = dirname($basedir); + } + + foreach ($output as $path) { + $path = Util\Common::realpath($path); + if ($path === false) { + // Skip deleted files. + continue; + } + + do { + $modified[$path] = true; + $path = dirname($path); + } while ($path !== $basedir); + } + + return $modified; + + }//end getWhitelist() + + +}//end class