1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ReliqArts\Docweaver\Service; |
6
|
|
|
|
7
|
|
|
use ReliqArts\Docweaver\Contract\VCSCommandRunner; |
8
|
|
|
use Symfony\Component\Process\Exception\ProcessFailedException; |
9
|
|
|
use Symfony\Component\Process\Process; |
10
|
|
|
|
11
|
|
|
final class GitCommandRunner implements VCSCommandRunner |
12
|
|
|
{ |
13
|
|
|
private const DEFAULT_REMOTE_NAME = 'origin'; |
14
|
|
|
|
15
|
|
|
public function clone(string $source, string $branch, string $workingDirectory): void |
16
|
|
|
{ |
17
|
|
|
$command = ['git', 'clone', '--branch', $branch, sprintf('%s', $source), $branch]; |
18
|
|
|
|
19
|
|
|
$clone = new Process($command, $workingDirectory); |
20
|
|
|
$clone->mustRun(); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @throws ProcessFailedException |
25
|
|
|
*/ |
26
|
|
|
public function listTags(string $workingDirectory): array |
27
|
|
|
{ |
28
|
|
|
$this->fetch($workingDirectory); |
29
|
|
|
|
30
|
|
|
$listTags = new Process(['git', 'tag', '-l'], $workingDirectory); |
31
|
|
|
$listTags->mustRun(); |
32
|
|
|
|
33
|
|
|
if ($splitTags = preg_split("/[\n\r]/", $listTags->getOutput())) { |
34
|
|
|
return array_filter(array_map('trim', $splitTags)); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return []; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @throws ProcessFailedException |
42
|
|
|
*/ |
43
|
|
|
public function pull(string $workingDirectory): void |
44
|
|
|
{ |
45
|
|
|
$pull = new Process(['git', 'pull'], $workingDirectory); |
46
|
|
|
$pull->mustRun(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @throws ProcessFailedException |
51
|
|
|
*/ |
52
|
|
|
public function getRemoteUrl(string $workingDirectory, ?string $remoteName = null): string |
53
|
|
|
{ |
54
|
|
|
$remoteName ??= self::DEFAULT_REMOTE_NAME; |
55
|
|
|
$command = ['git', 'config', '--get', sprintf('remote.%s.url', $remoteName)]; |
56
|
|
|
|
57
|
|
|
$getUrl = new Process($command, $workingDirectory); |
58
|
|
|
$getUrl->mustRun(); |
59
|
|
|
|
60
|
|
|
return trim($getUrl->getOutput()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
private function fetch(string $workingDirectory): void |
64
|
|
|
{ |
65
|
|
|
$pull = new Process(['git', 'fetch'], $workingDirectory); |
66
|
|
|
$pull->mustRun(); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|