Skip to content

Commit 10ef1e7

Browse files
Handle new branch scenario
1 parent e1f02e9 commit 10ef1e7

File tree

2 files changed

+20
-27
lines changed

2 files changed

+20
-27
lines changed

resources/config/console.yml

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ services:
7070
- '@GrumPHP\Collection\TestSuiteCollection'
7171
- '@GrumPHP\Runner\TaskRunner'
7272
- '@GrumPHP\Git\GitRepository'
73+
- '@GrumPHP\Locator\RegisteredFiles'
7374
tags:
7475
- { name: 'console.command' }
7576

src/Console/Command/Git/PrePushCommand.php

+19-27
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use GrumPHP\Collection\FilesCollection;
88
use GrumPHP\Collection\TestSuiteCollection;
99
use GrumPHP\Git\GitRepository;
10+
use GrumPHP\Locator\RegisteredFiles;
1011
use GrumPHP\Runner\TaskRunner;
1112
use GrumPHP\Runner\TaskRunnerContext;
1213
use GrumPHP\Task\Context\GitPrePushContext;
@@ -26,9 +27,10 @@ class PrePushCommand extends Command
2627
const SHA1_EMPTY = '0000000000000000000000000000000000000000';
2728

2829
public function __construct(
29-
private TestSuiteCollection $testSuites,
30-
private TaskRunner $taskRunner,
31-
private GitRepository $repository,
30+
private readonly TestSuiteCollection $testSuites,
31+
private readonly TaskRunner $taskRunner,
32+
private readonly GitRepository $repository,
33+
private readonly RegisteredFiles $registeredFilesLocator,
3234
) {
3335
parent::__construct();
3436
}
@@ -52,6 +54,9 @@ protected function configure(): void
5254
public function execute(InputInterface $input, OutputInterface $output): int
5355
{
5456
$files = $this->getChangedFiles();
57+
if ($files === null) {
58+
return self::EXIT_CODE_OK;
59+
}
5560

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

70-
protected function getChangedFiles(): FilesCollection
75+
protected function getChangedFiles(): ?FilesCollection
7176
{
7277
$fileCollection = new FilesCollection([]);
73-
$fullCheck = false;
7478

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

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

84-
// Do a full check if this is a new branch.
8588
if ($remoteSha === self::SHA1_EMPTY) {
86-
$fullCheck = true;
87-
break;
89+
// Do a full check if this is a new branch.
90+
return $this->registeredFilesLocator->locate();
8891
}
8992

90-
$command = (string) $this->repository->run('diff-tree', [
91-
'--no-commit-id',
92-
'--name-only',
93-
'-r',
94-
$localSha,
95-
$remoteSha,
96-
]);
97-
98-
// Filter out empty lines.
99-
$files = array_filter(array_map('trim', explode("\n", $command)));
100-
foreach ($files as $file) {
101-
// Avoid missing files and duplicates.
102-
if (file_exists($file) && !$fileCollection->containsKey($file)) {
93+
$args = ['--no-commit-id', '--name-only', '-r', $localSha, $remoteSha];
94+
$command = (string) $this->repository->run('diff-tree', $args);
95+
96+
foreach (explode("\n", $command) as $file) {
97+
// Avoid empty lines, missing files, and duplicates.
98+
if (!empty($file) && file_exists($file) && !$fileCollection->containsKey($file)) {
10399
$fileCollection->set($file, new \SplFileInfo($file));
104100
}
105101
}
106102
}
107103

108-
if ($fileCollection->isEmpty() && !$fullCheck) {
109-
return $fileCollection;
110-
}
111-
112104
return $fileCollection;
113105
}
114106
}

0 commit comments

Comments
 (0)