Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move getting unified git diff to shell #76

Merged
merged 3 commits into from
May 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions PhpcsChanged/Cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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())) {
Expand Down
13 changes: 0 additions & 13 deletions PhpcsChanged/GitWorkflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 2 additions & 0 deletions PhpcsChanged/ShellOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
15 changes: 15 additions & 0 deletions PhpcsChanged/UnixShell.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
14 changes: 0 additions & 14 deletions tests/GitWorkflowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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]);
Expand Down
13 changes: 13 additions & 0 deletions tests/helpers/TestShell.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpcsChanged\Modes;
use PhpcsChanged\ShellOperator;
use PhpcsChanged\ShellException;
use PhpcsChanged\NoChangesException;

class TestShell implements ShellOperator {

Expand Down Expand Up @@ -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;
}
}