Skip to content

Commit

Permalink
Merge branch 'feature/new-filter-git-staged' of https://github.com/jr…
Browse files Browse the repository at this point in the history
  • Loading branch information
gsherwood committed Apr 15, 2019
2 parents 01de622 + 926127f commit 3662148
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
<file baseinstalldir="PHP/CodeSniffer" name="ExactMatch.php" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="Filter.php" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="GitModified.php" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="GitStaged.php" role="php" />
</dir>
<dir name="Generators">
<file baseinstalldir="PHP/CodeSniffer" name="Generator.php" role="php" />
Expand Down
68 changes: 68 additions & 0 deletions src/Filters/GitStaged.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* A filter to only include files that have been staged for commit in a Git repository.
*
* This filter is the ideal companion for your pre-commit git hook.
*
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
* @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

0 comments on commit 3662148

Please sign in to comment.