Skip to content

Commit 1ac133c

Browse files
fix: append git root path to diff and dirty files
1 parent 6f113c1 commit 1ac133c

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

app/Repositories/GitPathsRepository.php

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use App\Contracts\PathsRepository;
66
use App\Factories\ConfigurationFactory;
77
use Illuminate\Support\Collection;
8-
use Illuminate\Support\Str;
98
use Symfony\Component\Process\Process;
109

1110
class GitPathsRepository implements PathsRepository
@@ -32,17 +31,15 @@ public function __construct($path)
3231
*/
3332
public function dirty()
3433
{
35-
$process = tap(new Process(['git', 'status', '--short', '--', '**.php']))->run();
34+
$process = tap(new Process(['git', 'diff', '--name-only', 'HEAD', '--', '**.php']))->run();
3635

3736
if (! $process->isSuccessful()) {
3837
abort(1, 'The [--dirty] option is only available when using Git.');
3938
}
4039

4140
$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);
4643

4744
return $this->processFileNames($dirtyFiles);
4845
}
@@ -61,16 +58,16 @@ public function diff($branch)
6158

6259
/** @var Collection<int, string> $files */
6360
$files = collect($files)
64-
->each(fn ($process) => abort_if(
61+
->each(fn($process) => abort_if(
6562
boolean: ! $process->isSuccessful(),
6663
code: 1,
6764
message: 'The [--diff] option is only available when using Git.',
6865
))
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))
7067
->flatten()
7168
->unique()
7269
->values()
73-
->map(fn ($s) => (string) $s);
70+
->map(fn($s) => (string) $s);
7471

7572
return $this->processFileNames($files);
7673
}
@@ -83,21 +80,26 @@ public function diff($branch)
8380
*/
8481
protected function processFileNames(Collection $fileNames)
8582
{
83+
$gitRoot = trim(tap(new Process(['git', 'rev-parse', '--show-toplevel']))->run()->getOutput());
84+
8685
$processedFileNames = $fileNames
87-
->map(function ($file) {
86+
->map(function ($file) use ($gitRoot) {
87+
$absolutePath = $gitRoot . DIRECTORY_SEPARATOR . $file;
88+
8889
if (PHP_OS_FAMILY === 'Windows') {
89-
$file = str_replace('/', DIRECTORY_SEPARATOR, $file);
90+
$absolutePath = str_replace('/', DIRECTORY_SEPARATOR, $absolutePath);
9091
}
9192

92-
return $this->path.DIRECTORY_SEPARATOR.$file;
93+
return $absolutePath;
9394
})
9495
->all();
9596

9697
$files = array_values(array_map(function ($splFile) {
9798
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()
101103
)));
102104

103105
return array_values(array_intersect($files, $processedFileNames));

0 commit comments

Comments
 (0)