-
-
Notifications
You must be signed in to change notification settings - Fork 238
/
GitPatcher.php
57 lines (49 loc) · 1.64 KB
/
GitPatcher.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
<?php
namespace cweagans\Composer\Patcher;
use cweagans\Composer\Patch;
use Composer\IO\IOInterface;
class GitPatcher extends PatcherBase
{
protected string $tool = 'git';
public function apply(Patch $patch, string $path): bool
{
// If the path isn't a git repo, don't even try.
// @see https://stackoverflow.com/a/27283285
if (!is_dir($path . '/.git')) {
$this->io->write("$path is not a git repo. Skipping Git patcher.", true, IOInterface::VERBOSE);
return false;
}
// Dry run first.
$status = $this->executeCommand(
'%s -C %s apply --check --verbose -p%s %s',
$this->patchTool(),
$path,
$patch->depth,
$patch->localPath
);
if (str_starts_with($this->executor->getErrorOutput(), 'Skipped')) {
// Git will indicate success but silently skip patches in some scenarios.
//
// @see https://github.com/cweagans/composer-patches/pull/165
$status = false;
}
// If the check failed, then don't proceed with actually applying the patch.
if (!$status) {
return false;
}
// Otherwise, we can try to apply the patch.
return $this->executeCommand(
'%s -C %s apply -p%s --verbose %s',
$this->patchTool(),
$path,
$patch->depth,
$patch->localPath
);
}
public function canUse(): bool
{
$output = "";
$result = $this->executor->execute($this->patchTool() . ' --version', $output);
return ($result === 0);
}
}