|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Vasoft\VersionIncrement; |
|
6
|
|
|
|
|
7
|
|
|
use Vasoft\VersionIncrement\Contract\VcsExecutorInterface; |
|
8
|
|
|
use Vasoft\VersionIncrement\Exceptions\GitCommandException; |
|
9
|
|
|
|
|
10
|
|
|
class GitExecutor implements VcsExecutorInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @codeCoverageIgnore |
|
14
|
|
|
*/ |
|
15
|
|
|
public function setConfig(Config $config): void |
|
16
|
|
|
{ |
|
17
|
|
|
// Do nothing |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
1 |
|
public function status(): array |
|
21
|
|
|
{ |
|
22
|
1 |
|
return $this->runCommand('status --porcelain'); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
1 |
|
public function getCommitDescription(string $commitId): array |
|
26
|
|
|
{ |
|
27
|
1 |
|
return $this->runCommand('log -1 --pretty=format:%b ' . $commitId); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
1 |
|
public function addFile(string $file): void |
|
31
|
|
|
{ |
|
32
|
1 |
|
$this->runCommand('add ' . $file); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
1 |
|
public function setVersionTag(string $version): void |
|
36
|
|
|
{ |
|
37
|
1 |
|
$this->runCommand('tag v' . $version); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
1 |
|
public function commit(string $message): void |
|
41
|
|
|
{ |
|
42
|
1 |
|
$this->runCommand("commit -am '" . $message . "'"); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
2 |
|
public function getCurrentBranch(): string |
|
46
|
|
|
{ |
|
47
|
2 |
|
$branch = $this->runCommand('rev-parse --abbrev-ref HEAD'); |
|
48
|
|
|
|
|
49
|
1 |
|
return trim($branch[0] ?? ''); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
2 |
|
public function getLastTag(): ?string |
|
53
|
|
|
{ |
|
54
|
2 |
|
$tags = $this->runCommand('tag --sort=-creatordate'); |
|
55
|
|
|
|
|
56
|
2 |
|
return $tags[0] ?? null; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
2 |
|
public function getCommitsSinceLastTag(?string $lastTag): array |
|
60
|
|
|
{ |
|
61
|
2 |
|
$command = $lastTag ? "log {$lastTag}..HEAD --pretty=format:\"%H %s\"" : 'log --pretty=format:"%H %s"'; |
|
62
|
|
|
|
|
63
|
2 |
|
return $this->runCommand($command); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @throws GitCommandException |
|
68
|
|
|
*/ |
|
69
|
11 |
|
private function runCommand(string $command): array |
|
70
|
|
|
{ |
|
71
|
11 |
|
exec("git {$command} 2>&1", $output, $returnCode); |
|
72
|
11 |
|
if (0 !== $returnCode) { |
|
73
|
1 |
|
throw new GitCommandException($command, $output); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
10 |
|
return $output; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|