5
5
use App \Contracts \PathsRepository ;
6
6
use App \Factories \ConfigurationFactory ;
7
7
use Illuminate \Support \Collection ;
8
- use Illuminate \Support \Str ;
9
8
use Symfony \Component \Process \Process ;
10
9
11
10
class GitPathsRepository implements PathsRepository
@@ -32,17 +31,15 @@ public function __construct($path)
32
31
*/
33
32
public function dirty ()
34
33
{
35
- $ process = tap (new Process (['git ' , 'status ' , '--short ' , '-- ' , '**.php ' ]))->run ();
34
+ $ process = tap (new Process (['git ' , 'diff ' , '--name-only ' , ' HEAD ' , '-- ' , '**.php ' ]))->run ();
36
35
37
36
if (! $ process ->isSuccessful ()) {
38
37
abort (1 , 'The [--dirty] option is only available when using Git. ' );
39
38
}
40
39
41
40
$ dirtyFiles = collect (preg_split ('/\R+/ ' , $ process ->getOutput (), flags: PREG_SPLIT_NO_EMPTY ))
42
- ->mapWithKeys (fn ($ file ) => [substr ($ file , 3 ) => trim (substr ($ file , 0 , 3 ))])
43
- ->reject (fn ($ status ) => $ status === 'D ' )
44
- ->map (fn ($ status , $ file ) => $ status === 'R ' ? Str::after ($ file , ' -> ' ) : $ file )
45
- ->values ();
41
+ ->values ()
42
+ ->map (fn ($ s ) => (string ) $ s );
46
43
47
44
return $ this ->processFileNames ($ dirtyFiles );
48
45
}
@@ -61,16 +58,16 @@ public function diff($branch)
61
58
62
59
/** @var Collection<int, string> $files */
63
60
$ files = collect ($ files )
64
- ->each (fn ($ process ) => abort_if (
61
+ ->each (fn ($ process ) => abort_if (
65
62
boolean: ! $ process ->isSuccessful (),
66
63
code: 1 ,
67
64
message: 'The [--diff] option is only available when using Git. ' ,
68
65
))
69
- ->map (fn ($ process ) => preg_split ('/\R+/ ' , $ process ->getOutput (), flags: PREG_SPLIT_NO_EMPTY ))
66
+ ->map (fn ($ process ) => preg_split ('/\R+/ ' , $ process ->getOutput (), flags: PREG_SPLIT_NO_EMPTY ))
70
67
->flatten ()
71
68
->unique ()
72
69
->values ()
73
- ->map (fn ($ s ) => (string ) $ s );
70
+ ->map (fn ($ s ) => (string ) $ s );
74
71
75
72
return $ this ->processFileNames ($ files );
76
73
}
@@ -83,21 +80,26 @@ public function diff($branch)
83
80
*/
84
81
protected function processFileNames (Collection $ fileNames )
85
82
{
83
+ $ gitRoot = trim (tap (new Process (['git ' , 'rev-parse ' , '--show-toplevel ' ]))->run ()->getOutput ());
84
+
86
85
$ processedFileNames = $ fileNames
87
- ->map (function ($ file ) {
86
+ ->map (function ($ file ) use ($ gitRoot ) {
87
+ $ absolutePath = $ gitRoot . DIRECTORY_SEPARATOR . $ file ;
88
+
88
89
if (PHP_OS_FAMILY === 'Windows ' ) {
89
- $ file = str_replace ('/ ' , DIRECTORY_SEPARATOR , $ file );
90
+ $ absolutePath = str_replace ('/ ' , DIRECTORY_SEPARATOR , $ absolutePath );
90
91
}
91
92
92
- return $ this -> path . DIRECTORY_SEPARATOR . $ file ;
93
+ return $ absolutePath ;
93
94
})
94
95
->all ();
95
96
96
97
$ files = array_values (array_map (function ($ splFile ) {
97
98
return $ splFile ->getPathname ();
98
- }, iterator_to_array (ConfigurationFactory::finder ()
99
- ->in ($ this ->path )
100
- ->files ()
99
+ }, iterator_to_array (
100
+ ConfigurationFactory::finder ()
101
+ ->in ($ this ->path )
102
+ ->files ()
101
103
)));
102
104
103
105
return array_values (array_intersect ($ files , $ processedFileNames ));
0 commit comments