|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* Changelog keeper |
|
5
|
|
|
* |
|
6
|
|
|
* @link https://github.com/hiqdev/chkipper |
|
7
|
|
|
* @package chkipper |
|
8
|
|
|
* @license BSD-3-Clause |
|
9
|
|
|
* @copyright Copyright (c) 2016, HiQDev (http://hiqdev.com/) |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace hiqdev\chkipper\history; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* History class. |
|
16
|
|
|
* Holds history of commits. |
|
17
|
|
|
* |
|
18
|
|
|
* @author Andrii Vasyliev <[email protected]> |
|
19
|
|
|
*/ |
|
20
|
|
|
class History |
|
21
|
|
|
{ |
|
22
|
|
|
public $lastTag = 'Under development'; |
|
23
|
|
|
|
|
24
|
|
|
public $initTag = 'Development started'; |
|
25
|
|
|
|
|
26
|
|
|
protected $_headers = []; |
|
27
|
|
|
protected $_hashes = []; |
|
28
|
|
|
protected $_links = []; |
|
29
|
|
|
protected $_tags = []; |
|
30
|
|
|
|
|
31
|
2 |
|
public function addHeader($str) |
|
32
|
|
|
{ |
|
33
|
2 |
|
$this->_headers[$str] = $str; |
|
34
|
2 |
|
} |
|
35
|
|
|
|
|
36
|
1 |
|
public function addHeaders(array $headers) |
|
37
|
|
|
{ |
|
38
|
1 |
|
foreach ($headers as $header) { |
|
39
|
1 |
|
$this->addHeader($header); |
|
40
|
1 |
|
} |
|
41
|
1 |
|
} |
|
42
|
|
|
|
|
43
|
1 |
|
public function setHeaders(array $headers) |
|
44
|
|
|
{ |
|
45
|
1 |
|
$this->_headers = []; |
|
46
|
1 |
|
$this->addHeaders($headers); |
|
47
|
1 |
|
} |
|
48
|
|
|
|
|
49
|
1 |
|
public function getHeaders() |
|
50
|
|
|
{ |
|
51
|
1 |
|
return $this->_headers; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
2 |
|
public function addLink($link, $href) |
|
55
|
|
|
{ |
|
56
|
2 |
|
$this->_links[$link] = $href; |
|
57
|
2 |
|
} |
|
58
|
|
|
|
|
59
|
1 |
|
public function setLinks(array $links) |
|
60
|
|
|
{ |
|
61
|
1 |
|
$this->_links = $links; |
|
62
|
1 |
|
} |
|
63
|
|
|
|
|
64
|
1 |
|
public function getLinks() |
|
65
|
|
|
{ |
|
66
|
1 |
|
return $this->_links; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function hasHash($hash) |
|
70
|
|
|
{ |
|
71
|
|
|
return isset($this->_hashes[(string) $hash]); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
2 |
|
public function addHash($hash) |
|
75
|
|
|
{ |
|
76
|
2 |
|
$this->_hashes[(string) $hash] = $hash; |
|
77
|
2 |
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function addHashes(array $hashes) |
|
80
|
|
|
{ |
|
81
|
|
|
foreach ($hashes as $hash) { |
|
82
|
|
|
$this->addHash($hash); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function setHashes(array $hashes) |
|
87
|
|
|
{ |
|
88
|
|
|
$this->_hashes = []; |
|
89
|
|
|
$this->addHashes($hashes); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function getHashes() |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->_hashes; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function getFirstTag() |
|
98
|
|
|
{ |
|
99
|
|
|
return reset($this->_tags); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
1 |
|
public function getTags() |
|
103
|
|
|
{ |
|
104
|
1 |
|
return $this->_tags; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
1 |
|
public function setTags(array $value) |
|
108
|
|
|
{ |
|
109
|
1 |
|
$this->_tags = $value; |
|
110
|
1 |
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Returns tag by name. |
|
114
|
|
|
* Creates if not exists. |
|
115
|
|
|
* Returns first tag when given empty name. |
|
116
|
|
|
* @param string|Tag $tag tag or tag name |
|
117
|
|
|
* @return Tag |
|
118
|
|
|
*/ |
|
119
|
2 |
|
public function findTag($tag, $pre = false) |
|
|
|
|
|
|
120
|
|
|
{ |
|
121
|
2 |
|
if (!$tag) { |
|
122
|
|
|
$tag = reset($this->_tags) ?: $this->lastTag; |
|
123
|
|
|
} |
|
124
|
2 |
|
$name = $tag instanceof Tag ? $tag->getName() : $tag; |
|
125
|
2 |
|
if (!$this->hasTag($name)) { |
|
126
|
2 |
|
$this->_tags[$name] = new Tag($name); |
|
127
|
2 |
|
} |
|
128
|
|
|
|
|
129
|
2 |
|
return $this->_tags[$name]; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
2 |
|
public function hasTag($tag) |
|
133
|
|
|
{ |
|
134
|
2 |
|
return isset($this->_tags[$tag]); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
2 |
|
public function addTag($tag, $date = null) |
|
138
|
|
|
{ |
|
139
|
2 |
|
$this->findTag($tag)->setDate($date); |
|
140
|
2 |
|
} |
|
141
|
|
|
|
|
142
|
2 |
|
public function findNote($tag, $note) |
|
143
|
|
|
{ |
|
144
|
2 |
|
$this->findTag($tag)->findNote($note); |
|
145
|
2 |
|
} |
|
146
|
|
|
|
|
147
|
2 |
|
public function findCommit($tag, $note, $hash, $pre = false) |
|
148
|
|
|
{ |
|
149
|
2 |
|
$this->addHash($hash); |
|
150
|
|
|
return $this->findTag($tag, $pre)->findNote($note, $pre)->findCommit($hash, $pre); |
|
|
|
|
|
|
151
|
2 |
|
} |
|
152
|
|
|
|
|
153
|
|
|
public function addHistory($commit, $front = false) |
|
154
|
|
|
{ |
|
155
|
|
|
$tag = $commit['tag']; |
|
156
|
|
|
$note = $commit['note']; |
|
157
|
|
|
$hash = $commit['hash']; |
|
158
|
|
|
$render = static::renderCommit($commit); |
|
|
|
|
|
|
159
|
|
|
$hashes = &$this->_tags[$tag][$note]; |
|
160
|
|
|
$hashes = (array) $hashes; |
|
161
|
|
|
if ($front) { |
|
162
|
|
|
$hashes = [$hash => [$render]] + $hashes; |
|
163
|
|
|
} else { |
|
164
|
|
|
$hashes[$hash][] = $render; |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
public function addGitLog() |
|
169
|
|
|
{ |
|
170
|
|
|
foreach (array_reverse(static::getVcs()->commits, true) as $hash => $commit) { |
|
|
|
|
|
|
171
|
|
|
if ($this->hasCommit($hash)) { |
|
|
|
|
|
|
172
|
|
|
continue; |
|
173
|
|
|
} |
|
174
|
|
|
$this->addHistory($commit, true); |
|
175
|
|
|
} |
|
176
|
|
|
if (!$this->hasHistory(static::getVcs()->initTag)) { |
|
|
|
|
|
|
177
|
|
|
$this->addHistory(['tag' => static::getVcs()->initTag]); |
|
|
|
|
|
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
public static function skipCommit($commit) |
|
182
|
|
|
{ |
|
183
|
|
|
$comment = $commit['comment']; |
|
184
|
|
|
|
|
185
|
|
|
static $equals = [ |
|
186
|
|
|
'' => 1, |
|
187
|
|
|
'minor' => 1, |
|
188
|
|
|
]; |
|
189
|
|
|
if ($equals[$comment]) { |
|
190
|
|
|
return true; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
static $starts = [ |
|
194
|
|
|
'version bump', |
|
195
|
|
|
'bumped version', |
|
196
|
|
|
"merge branch 'master'", |
|
197
|
|
|
]; |
|
198
|
|
|
foreach ($starts as $start) { |
|
199
|
|
|
if (strtolower(substr($comment, 0, strlen($start))) === $start) { |
|
200
|
|
|
return true; |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
return false; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
} |
|
208
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.