-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy pathGitPathsRepository.php
106 lines (91 loc) · 3.22 KB
/
GitPathsRepository.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
namespace App\Repositories;
use App\Contracts\PathsRepository;
use App\Factories\ConfigurationFactory;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Symfony\Component\Process\Process;
class GitPathsRepository implements PathsRepository
{
/**
* The project path.
*
* @var string
*/
protected $path;
/**
* Creates a new Paths Repository instance.
*
* @param string $path
*/
public function __construct($path)
{
$this->path = $path;
}
/**
* {@inheritDoc}
*/
public function dirty()
{
$process = tap(new Process(['git', 'status', '--short', '--', '**.php']))->run();
if (! $process->isSuccessful()) {
abort(1, 'The [--dirty] option is only available when using Git.');
}
$dirtyFiles = collect(preg_split('/\R+/', $process->getOutput(), flags: PREG_SPLIT_NO_EMPTY))
->mapWithKeys(fn ($file) => [substr($file, 3) => trim(substr($file, 0, 3))])
->reject(fn ($status) => $status === 'D')
->map(fn ($status, $file) => $status === 'R' ? Str::after($file, ' -> ') : $file)
->values();
return $this->processFileNames($dirtyFiles);
}
/**
* {@inheritDoc}
*/
public function diff($branch)
{
$files = [
'committed' => tap(new Process(['git', 'diff', '--name-only', '--diff-filter=AM', "{$branch}...HEAD", '--', '**.php']))->run(),
'staged' => tap(new Process(['git', 'diff', '--name-only', '--diff-filter=AM', '--cached', '--', '**.php']))->run(),
'unstaged' => tap(new Process(['git', 'diff', '--name-only', '--diff-filter=AM', '--', '**.php']))->run(),
'untracked' => tap(new Process(['git', 'ls-files', '--others', '--exclude-standard', '--', '**.php']))->run(),
];
$files = collect($files)
->each(fn ($process) => abort_if(
boolean: ! $process->isSuccessful(),
code: 1,
message: 'The [--diff] option is only available when using Git.',
))
->map(fn ($process) => $process->getOutput())
->map(fn ($output) => explode(PHP_EOL, $output))
->flatten()
->filter()
->unique()
->values()
->map(fn ($s) => (string) $s);
return $this->processFileNames($files);
}
/**
* Process the files.
*
* @param \Illuminate\Support\Collection<int, string> $fileNames
* @return array<int, string>
*/
protected function processFileNames(Collection $fileNames)
{
$processedFileNames = $fileNames
->map(function ($file) {
if (PHP_OS_FAMILY === 'Windows') {
$file = str_replace('/', DIRECTORY_SEPARATOR, $file);
}
return $this->path.DIRECTORY_SEPARATOR.$file;
})
->all();
$files = array_values(array_map(function ($splFile) {
return $splFile->getPathname();
}, iterator_to_array(ConfigurationFactory::finder()
->in($this->path)
->files()
)));
return array_values(array_intersect($files, $processedFileNames));
}
}