1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Vasoft\VersionIncrement\Commits; |
6
|
|
|
|
7
|
|
|
use Vasoft\VersionIncrement\Config; |
8
|
|
|
use Vasoft\VersionIncrement\Contract\CommitParserInterface; |
9
|
|
|
use Vasoft\VersionIncrement\Exceptions\ChangesNotFoundException; |
10
|
|
|
use Vasoft\VersionIncrement\Exceptions\ConfigNotSetException; |
11
|
|
|
use Vasoft\VersionIncrement\Exceptions\GitCommandException; |
12
|
|
|
|
13
|
|
|
final class ShortParser implements CommitParserInterface |
14
|
|
|
{ |
15
|
|
|
public const REG_EXP = "/^[\t *-]*((?<key>[a-z]+)(?:\\((?<scope>[^)]+)\\))?(?<breaking>!)?:\\s+(?<message>.+))/"; |
16
|
|
|
private ?Config $config = null; |
17
|
|
|
|
18
|
29 |
|
public function setConfig(Config $config): void |
19
|
|
|
{ |
20
|
29 |
|
$this->config = $config; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @throws ChangesNotFoundException |
25
|
|
|
* @throws ConfigNotSetException |
26
|
|
|
* @throws GitCommandException |
27
|
|
|
*/ |
28
|
29 |
|
public function process( |
29
|
|
|
?string $tagsFrom, |
30
|
|
|
string $tagsTo = '', |
31
|
|
|
): CommitCollection { |
32
|
29 |
|
if (null === $this->config) { |
33
|
1 |
|
throw new ConfigNotSetException(); |
34
|
|
|
} |
35
|
28 |
|
$vcs = $this->config->getVcsExecutor(); |
36
|
28 |
|
$commits = $vcs->getCommitsSinceLastTag($tagsFrom); |
37
|
28 |
|
if (empty($commits)) { |
38
|
1 |
|
throw new ChangesNotFoundException(); |
39
|
|
|
} |
40
|
27 |
|
$commitCollection = $this->config->getCommitCollection(); |
41
|
27 |
|
$aggregateKey = $this->config->getAggregateSection(); |
42
|
27 |
|
$shouldProcessDefaultSquashedCommit = $this->config->shouldProcessDefaultSquashedCommit(); |
43
|
27 |
|
$squashedCommitMessage = $this->config->getSquashedCommitMessage(); |
44
|
27 |
|
foreach ($commits as $commit) { |
45
|
27 |
|
if (preg_match( |
46
|
27 |
|
'/^(?<hash>[^ ]+) (?<commit>.+)/', |
47
|
27 |
|
$commit, |
48
|
27 |
|
$matches, |
49
|
27 |
|
)) { |
50
|
27 |
|
$hash = $matches['hash']; |
51
|
27 |
|
$commit = $matches['commit']; |
52
|
|
|
if ( |
53
|
27 |
|
$shouldProcessDefaultSquashedCommit |
54
|
27 |
|
&& str_ends_with($commit, $squashedCommitMessage) |
55
|
|
|
) { |
56
|
2 |
|
$this->processAggregated($hash, $commitCollection); |
57
|
|
|
|
58
|
2 |
|
continue; |
59
|
|
|
} |
60
|
27 |
|
if (!$this->parseCommit($commit, $commitCollection, $aggregateKey, $hash)) { |
61
|
1 |
|
$commitCollection->addRawMessage($commit); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
27 |
|
return $commitCollection; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @throws GitCommandException |
71
|
|
|
*/ |
72
|
27 |
|
private function parseCommit( |
73
|
|
|
string $line, |
74
|
|
|
CommitCollection $commitCollection, |
75
|
|
|
string $aggregateKey, |
76
|
|
|
string $hash, |
77
|
|
|
): bool { |
78
|
27 |
|
$matches = []; |
79
|
27 |
|
if (preg_match(self::REG_EXP, $line, $matches)) { |
80
|
26 |
|
$key = trim($matches['key']); |
81
|
26 |
|
if ('' !== $hash && '' !== $aggregateKey && $aggregateKey === $key) { |
82
|
1 |
|
$commitCollection->setMajorMarker('!' === $matches['breaking']); |
83
|
1 |
|
$this->processAggregated($hash, $commitCollection); |
84
|
|
|
} else { |
85
|
26 |
|
$commitCollection->add( |
86
|
26 |
|
new Commit( |
87
|
26 |
|
$line, |
88
|
26 |
|
$key, |
89
|
26 |
|
$matches['message'], |
90
|
26 |
|
'!' === $matches['breaking'], |
91
|
26 |
|
$matches['scope'], |
92
|
26 |
|
[$matches['breaking']], |
93
|
26 |
|
), |
94
|
26 |
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
26 |
|
return true; |
98
|
|
|
} |
99
|
|
|
|
100
|
4 |
|
return false; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @throws GitCommandException |
105
|
|
|
*/ |
106
|
3 |
|
private function processAggregated(string $hash, CommitCollection $commitCollection): void |
107
|
|
|
{ |
108
|
|
|
/** @scrutinizer ignore-call */ |
109
|
3 |
|
$description = $this->config->getVcsExecutor()->getCommitDescription($hash); |
110
|
3 |
|
foreach ($description as $line) { |
111
|
3 |
|
$this->parseCommit($line, $commitCollection, '', ''); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|