diff --git a/PhpcsChanged/Cli.php b/PhpcsChanged/Cli.php index e7043d7..a422482 100644 --- a/PhpcsChanged/Cli.php +++ b/PhpcsChanged/Cli.php @@ -16,7 +16,7 @@ use PhpcsChanged\CacheManager; use function PhpcsChanged\{getNewPhpcsMessages, getNewPhpcsMessagesFromFiles, getVersion}; use function PhpcsChanged\SvnWorkflow\{getSvnUnifiedDiff, getSvnFileInfo, isNewSvnFile, getSvnUnmodifiedPhpcsOutput, getSvnModifiedPhpcsOutput, getSvnRevisionId}; -use function PhpcsChanged\GitWorkflow\{getGitMergeBase, getGitUnifiedDiff}; +use function PhpcsChanged\GitWorkflow\getGitMergeBase; function getDebug(bool $debugEnabled): callable { return @@ -344,8 +344,6 @@ function runGitWorkflow(CliOptions $options, ShellOperator $shell, CacheManager } function runGitWorkflowForFile(string $gitFile, CliOptions $options, ShellOperator $shell, CacheManager $cache, callable $debug): PhpcsMessages { - $git = getenv('GIT') ?: 'git'; - $phpcsStandard = $options->phpcsStandard; $warningSeverity = $options->warningSeverity; $errorSeverity = $options->errorSeverity; @@ -385,7 +383,7 @@ function runGitWorkflowForFile(string $gitFile, CliOptions $options, ShellOperat } if (! $isNewFile) { $debug('Checking the unmodified file with PHPCS since the file is not new and contains some messages.'); - $unifiedDiff = getGitUnifiedDiff($gitFile, $git, [$shell, 'executeCommand'], $options->toArray(), $debug); + $unifiedDiff = $shell->getGitUnifiedDiff($gitFile); $unmodifiedFilePhpcsOutput = null; $unmodifiedFileHash = ''; if (isCachingEnabled($options->toArray())) { diff --git a/PhpcsChanged/GitWorkflow.php b/PhpcsChanged/GitWorkflow.php index b19b440..06c0039 100644 --- a/PhpcsChanged/GitWorkflow.php +++ b/PhpcsChanged/GitWorkflow.php @@ -23,16 +23,3 @@ function getGitMergeBase(string $git, callable $executeCommand, array $options, $debug('merge-base command output:', $mergeBase); return trim($mergeBase); } - -function getGitUnifiedDiff(string $gitFile, string $git, callable $executeCommand, array $options, callable $debug): string { - $objectOption = isset($options['git-base']) && ! empty($options['git-base']) ? ' ' . escapeshellarg($options['git-base']) . '...' : ''; - $stagedOption = empty( $objectOption ) && ! isset($options['git-unstaged']) ? ' --staged' : ''; - $unifiedDiffCommand = "{$git} diff{$stagedOption}{$objectOption} --no-prefix " . escapeshellarg($gitFile); - $debug('running diff command:', $unifiedDiffCommand); - $unifiedDiff = $executeCommand($unifiedDiffCommand); - if (! $unifiedDiff) { - throw new NoChangesException("Cannot get git diff for file '{$gitFile}'; skipping"); - } - $debug('diff command output:', $unifiedDiff); - return $unifiedDiff; -} diff --git a/PhpcsChanged/ShellOperator.php b/PhpcsChanged/ShellOperator.php index 09275ea..219e3bd 100644 --- a/PhpcsChanged/ShellOperator.php +++ b/PhpcsChanged/ShellOperator.php @@ -33,4 +33,6 @@ public function getGitHashOfUnmodifiedFile(string $fileName): string; public function getPhpcsOutputOfModifiedGitFile(string $fileName): string; public function getPhpcsOutputOfUnmodifiedGitFile(string $fileName): string; + + public function getGitUnifiedDiff(string $fileName): string; } diff --git a/PhpcsChanged/UnixShell.php b/PhpcsChanged/UnixShell.php index 80ecbe9..fb27fb3 100644 --- a/PhpcsChanged/UnixShell.php +++ b/PhpcsChanged/UnixShell.php @@ -204,6 +204,21 @@ public function getPhpcsOutputOfUnmodifiedGitFile(string $fileName): string { return $unmodifiedFilePhpcsOutput; } + public function getGitUnifiedDiff(string $fileName): string { + $debug = getDebug($this->options->debug); + $git = getenv('GIT') ?: 'git'; + $objectOption = $this->options->mode === Modes::GIT_BASE ? ' ' . escapeshellarg($this->options->gitBase) . '...' : ''; + $stagedOption = empty($objectOption) && $this->options->mode !== Modes::GIT_UNSTAGED ? ' --staged' : ''; + $unifiedDiffCommand = "{$git} diff{$stagedOption}{$objectOption} --no-prefix " . escapeshellarg($fileName); + $debug('running diff command:', $unifiedDiffCommand); + $unifiedDiff = $this->executeCommand($unifiedDiffCommand); + if (! $unifiedDiff) { + throw new NoChangesException("Cannot get git diff for file '{$fileName}'; skipping"); + } + $debug('diff command output:', $unifiedDiff); + return $unifiedDiff; + } + public function isReadable(string $fileName): bool { return is_readable($fileName); } diff --git a/tests/GitWorkflowTest.php b/tests/GitWorkflowTest.php index 50aadb1..c7a7575 100644 --- a/tests/GitWorkflowTest.php +++ b/tests/GitWorkflowTest.php @@ -14,7 +14,6 @@ use PhpcsChangedTests\PhpcsFixture; use PhpcsChangedTests\TestCache; use function PhpcsChanged\Cli\runGitWorkflow; -use function PhpcsChanged\GitWorkflow\getGitUnifiedDiff; final class GitWorkflowTest extends TestCase { public $fixture; @@ -26,19 +25,6 @@ public function setUp(): void { $this->phpcs = new PhpcsFixture(); } - public function testGetGitUnifiedDiff() { - $gitFile = 'foobar.php'; - $git = 'git'; - $diff = $this->fixture->getAddedLineDiff('foobar.php', 'use Foobar;'); - $executeCommand = function($command) use ($diff) { - if (! $command || false === strpos($command, "git diff --staged --no-prefix 'foobar.php'")) { - return ''; - } - return $diff; - }; - $this->assertEquals($diff, getGitUnifiedDiff($gitFile, $git, $executeCommand, [], '\PhpcsChangedTests\Debug')); - } - public function testFullGitWorkflowForOneFileStaged() { $gitFile = 'foobar.php'; $shell = new TestShell([$gitFile]); diff --git a/tests/helpers/TestShell.php b/tests/helpers/TestShell.php index 7db0204..862ddc5 100644 --- a/tests/helpers/TestShell.php +++ b/tests/helpers/TestShell.php @@ -7,6 +7,7 @@ use PhpcsChanged\Modes; use PhpcsChanged\ShellOperator; use PhpcsChanged\ShellException; +use PhpcsChanged\NoChangesException; class TestShell implements ShellOperator { @@ -222,4 +223,16 @@ public function doesUnmodifiedFileExistInGit(string $fileName): bool { } return $this->isFileStagedForAdding($fileName); } + + public function getGitUnifiedDiff(string $fileName): string { + $git = getenv('GIT') ?: 'git'; + $objectOption = $this->options->mode === Modes::GIT_BASE ? ' ' . escapeshellarg($this->options->gitBase) . '...' : ''; + $stagedOption = empty($objectOption) && $this->options->mode !== Modes::GIT_UNSTAGED ? ' --staged' : ''; + $unifiedDiffCommand = "{$git} diff{$stagedOption}{$objectOption} --no-prefix " . escapeshellarg($fileName); + $unifiedDiff = $this->executeCommand($unifiedDiffCommand); + if (! $unifiedDiff) { + throw new NoChangesException("Cannot get git diff for file '{$fileName}'; skipping"); + } + return $unifiedDiff; + } }