7
7
use GrumPHP \Collection \FilesCollection ;
8
8
use GrumPHP \Collection \TestSuiteCollection ;
9
9
use GrumPHP \Git \GitRepository ;
10
+ use GrumPHP \Locator \RegisteredFiles ;
10
11
use GrumPHP \Runner \TaskRunner ;
11
12
use GrumPHP \Runner \TaskRunnerContext ;
12
13
use GrumPHP \Task \Context \GitPrePushContext ;
@@ -26,9 +27,10 @@ class PrePushCommand extends Command
26
27
const SHA1_EMPTY = '0000000000000000000000000000000000000000 ' ;
27
28
28
29
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 ,
32
34
) {
33
35
parent ::__construct ();
34
36
}
@@ -52,6 +54,9 @@ protected function configure(): void
52
54
public function execute (InputInterface $ input , OutputInterface $ output ): int
53
55
{
54
56
$ files = $ this ->getChangedFiles ();
57
+ if ($ files === null ) {
58
+ return self ::EXIT_CODE_OK ;
59
+ }
55
60
56
61
$ context = (
57
62
new TaskRunnerContext (
@@ -67,48 +72,35 @@ public function execute(InputInterface $input, OutputInterface $output): int
67
72
return $ results ->isFailed () ? self ::EXIT_CODE_NOK : self ::EXIT_CODE_OK ;
68
73
}
69
74
70
- protected function getChangedFiles (): FilesCollection
75
+ protected function getChangedFiles (): ? FilesCollection
71
76
{
72
77
$ fileCollection = new FilesCollection ([]);
73
- $ fullCheck = false ;
74
78
75
79
// Loop over the commits.
76
80
while ($ commit = trim ((string ) fgets (STDIN ))) {
77
81
[$ localRef , $ localSha , , $ remoteSha ] = explode (' ' , $ commit );
78
82
79
- // Skip if we are deleting a branch or if there is no local branch.
80
83
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 ;
82
86
}
83
87
84
- // Do a full check if this is a new branch.
85
88
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 () ;
88
91
}
89
92
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 )) {
103
99
$ fileCollection ->set ($ file , new \SplFileInfo ($ file ));
104
100
}
105
101
}
106
102
}
107
103
108
- if ($ fileCollection ->isEmpty () && !$ fullCheck ) {
109
- return $ fileCollection ;
110
- }
111
-
112
104
return $ fileCollection ;
113
105
}
114
106
}
0 commit comments