Skip to content

Commit

Permalink
Handle new branch scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiu-cristea committed May 26, 2024
1 parent e1f02e9 commit 10ef1e7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 27 deletions.
1 change: 1 addition & 0 deletions resources/config/console.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ services:
- '@GrumPHP\Collection\TestSuiteCollection'
- '@GrumPHP\Runner\TaskRunner'
- '@GrumPHP\Git\GitRepository'
- '@GrumPHP\Locator\RegisteredFiles'
tags:
- { name: 'console.command' }

Expand Down
46 changes: 19 additions & 27 deletions src/Console/Command/Git/PrePushCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use GrumPHP\Collection\FilesCollection;
use GrumPHP\Collection\TestSuiteCollection;
use GrumPHP\Git\GitRepository;
use GrumPHP\Locator\RegisteredFiles;
use GrumPHP\Runner\TaskRunner;
use GrumPHP\Runner\TaskRunnerContext;
use GrumPHP\Task\Context\GitPrePushContext;
Expand All @@ -26,9 +27,10 @@ class PrePushCommand extends Command
const SHA1_EMPTY = '0000000000000000000000000000000000000000';

public function __construct(
private TestSuiteCollection $testSuites,
private TaskRunner $taskRunner,
private GitRepository $repository,
private readonly TestSuiteCollection $testSuites,
private readonly TaskRunner $taskRunner,
private readonly GitRepository $repository,
private readonly RegisteredFiles $registeredFilesLocator,
) {
parent::__construct();
}
Expand All @@ -52,6 +54,9 @@ protected function configure(): void
public function execute(InputInterface $input, OutputInterface $output): int
{
$files = $this->getChangedFiles();
if ($files === null) {
return self::EXIT_CODE_OK;
}

$context = (
new TaskRunnerContext(
Expand All @@ -67,48 +72,35 @@ public function execute(InputInterface $input, OutputInterface $output): int
return $results->isFailed() ? self::EXIT_CODE_NOK : self::EXIT_CODE_OK;
}

protected function getChangedFiles(): FilesCollection
protected function getChangedFiles(): ?FilesCollection
{
$fileCollection = new FilesCollection([]);
$fullCheck = false;

// Loop over the commits.
while ($commit = trim((string) fgets(STDIN))) {
[$localRef, $localSha, , $remoteSha] = explode(' ', $commit);

// Skip if we are deleting a branch or if there is no local branch.
if ($localRef === '(delete)' || $localSha === self::SHA1_EMPTY) {
return $fileCollection;
// A branch has been deleted or there's no local branch.
return null;
}

// Do a full check if this is a new branch.
if ($remoteSha === self::SHA1_EMPTY) {
$fullCheck = true;
break;
// Do a full check if this is a new branch.
return $this->registeredFilesLocator->locate();
}

$command = (string) $this->repository->run('diff-tree', [
'--no-commit-id',
'--name-only',
'-r',
$localSha,
$remoteSha,
]);

// Filter out empty lines.
$files = array_filter(array_map('trim', explode("\n", $command)));
foreach ($files as $file) {
// Avoid missing files and duplicates.
if (file_exists($file) && !$fileCollection->containsKey($file)) {
$args = ['--no-commit-id', '--name-only', '-r', $localSha, $remoteSha];
$command = (string) $this->repository->run('diff-tree', $args);

foreach (explode("\n", $command) as $file) {
// Avoid empty lines, missing files, and duplicates.
if (!empty($file) && file_exists($file) && !$fileCollection->containsKey($file)) {
$fileCollection->set($file, new \SplFileInfo($file));
}
}
}

if ($fileCollection->isEmpty() && !$fullCheck) {
return $fileCollection;
}

return $fileCollection;
}
}

0 comments on commit 10ef1e7

Please sign in to comment.