|
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
|
|
|
protected $_headers = []; |
|
23
|
|
|
protected $_hashes = []; |
|
24
|
|
|
protected $_links = []; |
|
25
|
|
|
protected $_tags = []; |
|
26
|
|
|
|
|
27
|
2 |
|
public function addHeader($str) |
|
28
|
|
|
{ |
|
29
|
2 |
|
$this->_headers[$str] = $str; |
|
30
|
2 |
|
} |
|
31
|
|
|
|
|
32
|
1 |
|
public function addHeaders(array $value) |
|
33
|
|
|
{ |
|
34
|
1 |
|
foreach ($value as $header) { |
|
35
|
1 |
|
$this->addHeader($header); |
|
36
|
1 |
|
} |
|
37
|
1 |
|
} |
|
38
|
|
|
|
|
39
|
1 |
|
public function setHeaders(array $value) |
|
40
|
|
|
{ |
|
41
|
1 |
|
$this->_headers = []; |
|
42
|
1 |
|
$this->addHeaders($value); |
|
43
|
1 |
|
} |
|
44
|
|
|
|
|
45
|
1 |
|
public function getHeaders() |
|
46
|
|
|
{ |
|
47
|
1 |
|
return $this->_headers; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
2 |
|
public function addLink($link, $href) |
|
51
|
|
|
{ |
|
52
|
2 |
|
$this->_links[$link] = $href; |
|
53
|
2 |
|
} |
|
54
|
|
|
|
|
55
|
1 |
|
public function setLinks($value) |
|
56
|
|
|
{ |
|
57
|
1 |
|
$this->_links = $value; |
|
58
|
1 |
|
} |
|
59
|
|
|
|
|
60
|
1 |
|
public function getLinks() |
|
61
|
|
|
{ |
|
62
|
1 |
|
return $this->_links; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
1 |
|
public function getTags() |
|
66
|
|
|
{ |
|
67
|
1 |
|
return $this->_tags; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
1 |
|
public function setTags(array $value) |
|
71
|
|
|
{ |
|
72
|
1 |
|
$this->_tags = $value; |
|
73
|
1 |
|
} |
|
74
|
|
|
|
|
75
|
2 |
|
public function findTag($tag) |
|
76
|
|
|
{ |
|
77
|
2 |
|
if (!isset($this->_tags[$tag])) { |
|
78
|
2 |
|
$this->_tags[$tag] = new Tag($tag); |
|
79
|
2 |
|
} |
|
80
|
|
|
|
|
81
|
2 |
|
return $this->_tags[$tag]; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function hasTag($tag) |
|
85
|
|
|
{ |
|
86
|
|
|
return array_key_exists($tag, $this->_tags); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
2 |
|
public function addTag($tag, $date = null) |
|
90
|
|
|
{ |
|
91
|
2 |
|
$this->findTag($tag)->setDate($date); |
|
92
|
2 |
|
} |
|
93
|
|
|
|
|
94
|
2 |
|
public function addNote($tag, $note) |
|
95
|
|
|
{ |
|
96
|
2 |
|
$this->findTag($tag)->findNote($note); |
|
97
|
2 |
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function hasHash($hash) |
|
100
|
|
|
{ |
|
101
|
|
|
return array_key_exists((string) $hash, $this->_hashes); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
2 |
|
public function addHash($hash, $label) |
|
105
|
|
|
{ |
|
106
|
2 |
|
$this->_hashes[(string) $hash] = $label; |
|
107
|
2 |
|
} |
|
108
|
|
|
|
|
109
|
2 |
|
public function addCommit($tag, $note, $hash, $label = null) |
|
110
|
|
|
{ |
|
111
|
2 |
|
$this->addHash($hash, $label); |
|
112
|
2 |
|
$this->findTag($tag)->findNote($note)->findCommit($hash)->setLabel($label); |
|
113
|
2 |
|
} |
|
114
|
|
|
|
|
115
|
2 |
|
public function addComment($tag, $note, $hash, $text = null) |
|
116
|
|
|
{ |
|
117
|
2 |
|
$this->findTag($tag)->findNote($note)->findCommit($hash)->addComment($text); |
|
118
|
2 |
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function addHistory($commit, $front = false) |
|
121
|
|
|
{ |
|
122
|
|
|
$tag = $commit['tag']; |
|
123
|
|
|
$note = $commit['note']; |
|
124
|
|
|
$hash = $commit['hash']; |
|
125
|
|
|
$render = static::renderCommit($commit); |
|
126
|
|
|
$hashes = &$this->_tags[$tag][$note]; |
|
127
|
|
|
$hashes = (array) $hashes; |
|
128
|
|
|
if ($front) { |
|
129
|
|
|
$hashes = [$hash => [$render]] + $hashes; |
|
130
|
|
|
} else { |
|
131
|
|
|
$hashes[$hash][] = $render; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function addGitLog() |
|
136
|
|
|
{ |
|
137
|
|
|
foreach (array_reverse(static::getVcs()->commits, true) as $hash => $commit) { |
|
|
|
|
|
|
138
|
|
|
if ($this->hasCommit($hash)) { |
|
|
|
|
|
|
139
|
|
|
continue; |
|
140
|
|
|
} |
|
141
|
|
|
$this->addHistory($commit, true); |
|
142
|
|
|
} |
|
143
|
|
|
if (!$this->hasHistory(static::getVcs()->initTag)) { |
|
|
|
|
|
|
144
|
|
|
$this->addHistory(['tag' => static::getVcs()->initTag]); |
|
|
|
|
|
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
public function render($data) |
|
|
|
|
|
|
149
|
|
|
{ |
|
150
|
|
|
$res = static::renderHeader('commits history'); |
|
151
|
|
|
|
|
152
|
|
|
foreach ($this->_tags as $tag => $notes) { |
|
153
|
|
|
$prev = $res; |
|
154
|
|
|
$tag = static::arrayPop($notes, 'tag') ?: $tag; |
|
155
|
|
|
$new = static::arrayPop($notes, '') ?: []; |
|
156
|
|
|
$res .= static::renderTag($tag); |
|
157
|
|
|
$save = $res; |
|
158
|
|
|
foreach ($new as $hash => $lines) { |
|
159
|
|
|
$res .= static::renderLines($lines); |
|
160
|
|
|
} |
|
161
|
|
|
foreach ($notes as $note => $cs) { |
|
162
|
|
|
$note = static::arrayPop($cs, 'note'); |
|
163
|
|
|
$res .= static::renderNote($note); |
|
164
|
|
|
foreach ($cs as $hash => $lines) { |
|
165
|
|
|
$res .= static::renderLines($lines); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
if ($save === $res && stripos($tag, static::getVcs()->lastTag) !== false) { |
|
|
|
|
|
|
169
|
|
|
$res = $prev; |
|
170
|
|
|
unset($this->_tags[$tag]); |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
return $res; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
public static function arrayPop(&$array, $key) |
|
178
|
|
|
{ |
|
179
|
|
|
$res = $array[$key]; |
|
180
|
|
|
unset($array[$key]); |
|
181
|
|
|
|
|
182
|
|
|
return $res; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
public static function renderNote($note) |
|
186
|
|
|
{ |
|
187
|
|
|
return "- $note\n"; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
public static function renderLines(array $lines) |
|
191
|
|
|
{ |
|
192
|
|
|
$res = implode("\n", array_filter($lines)); |
|
193
|
|
|
|
|
194
|
|
|
return $res ? $res . "\n" : ''; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
public static function renderCommit($commit) |
|
198
|
|
|
{ |
|
199
|
|
|
return static::skipCommit($commit) ? '' : " - $commit[hash] $commit[date] $commit[comment] ($commit[email])"; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
public static function skipCommit($commit) |
|
203
|
|
|
{ |
|
204
|
|
|
$comment = $commit['comment']; |
|
205
|
|
|
|
|
206
|
|
|
static $equals = [ |
|
207
|
|
|
'' => 1, |
|
208
|
|
|
'minor' => 1, |
|
209
|
|
|
]; |
|
210
|
|
|
if ($equals[$comment]) { |
|
211
|
|
|
return true; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
static $starts = [ |
|
215
|
|
|
'version bump', |
|
216
|
|
|
'bumped version', |
|
217
|
|
|
"merge branch 'master'", |
|
218
|
|
|
]; |
|
219
|
|
|
foreach ($starts as $start) { |
|
220
|
|
|
if (strtolower(substr($comment, 0, strlen($start))) === $start) { |
|
221
|
|
|
return true; |
|
222
|
|
|
} |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
return false; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
public static function renderTag($tag) |
|
229
|
|
|
{ |
|
230
|
|
|
if (strpos($tag, ' ') === false || $tag === static::getVcs()->initTag) { |
|
|
|
|
|
|
231
|
|
|
$tag .= static::renderDate(static::getVcs()->tags[$tag]); |
|
|
|
|
|
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
return "\n## $tag\n\n"; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
public static function renderDate($date) |
|
238
|
|
|
{ |
|
239
|
|
|
return $date ? date(' Y-m-d', strtotime($date)) : ''; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
public static function renderHeader($string) |
|
|
|
|
|
|
243
|
|
|
{ |
|
244
|
|
|
$res = ''; |
|
245
|
|
|
foreach ($this->_headers as $str) { |
|
|
|
|
|
|
246
|
|
|
$res .= $str . "\n"; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
return $res; |
|
250
|
|
|
} |
|
251
|
|
|
} |
|
252
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.