1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of SebastianFeldmann\Git. |
4
|
|
|
* |
5
|
|
|
* (c) Sebastian Feldmann <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace SebastianFeldmann\Git; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class CommitMessage |
14
|
|
|
* |
15
|
|
|
* @package SebastianFeldmann\Git |
16
|
|
|
* @author Sebastian Feldmann <[email protected]> |
17
|
|
|
* @link https://github.com/sebastianfeldmann/git |
18
|
|
|
* @since Class available since Release 0.9.0 |
19
|
|
|
*/ |
20
|
|
|
class CommitMessage |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Commit Message content |
24
|
|
|
* |
25
|
|
|
* This includes lines that are comments. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $rawContent; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Content split lines |
33
|
|
|
* |
34
|
|
|
* This includes lines that are comments. |
35
|
|
|
* |
36
|
|
|
* @var string[] |
37
|
|
|
*/ |
38
|
|
|
private $rawLines; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Amount of lines |
42
|
|
|
* |
43
|
|
|
* This includes lines that are comments |
44
|
|
|
* |
45
|
|
|
* @var int |
46
|
|
|
*/ |
47
|
|
|
private $rawLineCount; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* The comment character |
51
|
|
|
* |
52
|
|
|
* Null would indicate the comment character is not set for this commit. A comment character might be null if this |
53
|
|
|
* commit came from a message stored in the repository, it would be populated if it came from a commit-msg hook, |
54
|
|
|
* where the commit message can still contain comments. |
55
|
|
|
* |
56
|
|
|
* @var string|null |
57
|
|
|
*/ |
58
|
|
|
private $commentCharacter; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get the lines |
62
|
|
|
* |
63
|
|
|
* This excludes the lines which are comments. |
64
|
|
|
* |
65
|
|
|
* @var string[] |
66
|
|
|
*/ |
67
|
|
|
private $contentLines; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get the number of lines |
71
|
|
|
* |
72
|
|
|
* This excludes lines which are comments. |
73
|
|
|
* |
74
|
|
|
* @var int |
75
|
|
|
*/ |
76
|
|
|
private $contentLineCount; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Commit Message content |
80
|
|
|
* |
81
|
|
|
* This excludes lines that are comments. |
82
|
|
|
* |
83
|
|
|
* @var string |
84
|
|
|
*/ |
85
|
|
|
private $content; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* CommitMessage constructor |
89
|
|
|
* |
90
|
|
|
* @param string $content |
91
|
|
|
* @param string|null $commentCharacter |
92
|
|
|
*/ |
93
|
22 |
|
public function __construct(string $content, string $commentCharacter = null) |
94
|
|
|
{ |
95
|
22 |
|
$this->rawContent = $content; |
96
|
22 |
|
$this->rawLines = empty($content) ? [] : preg_split("/\\r\\n|\\r|\\n/", $content); |
97
|
22 |
|
$this->rawLineCount = count($this->rawLines); |
98
|
22 |
|
$this->commentCharacter = $commentCharacter; |
99
|
22 |
|
$this->contentLines = $this->getContentLines($this->rawLines, $commentCharacter); |
100
|
22 |
|
$this->contentLineCount = count($this->contentLines); |
101
|
22 |
|
$this->content = implode(PHP_EOL, $this->contentLines); |
102
|
22 |
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Is message empty. |
106
|
|
|
* |
107
|
|
|
* @return bool |
108
|
|
|
*/ |
109
|
2 |
|
public function isEmpty() : bool |
110
|
|
|
{ |
111
|
2 |
|
return empty($this->content); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get commit message content |
117
|
|
|
* |
118
|
|
|
* This excludes lines that are comments. |
119
|
|
|
* |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
2 |
|
public function getContent() : string |
123
|
|
|
{ |
124
|
2 |
|
return $this->content; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Get complete commit message content |
129
|
|
|
* |
130
|
|
|
* This includes lines that are comments. |
131
|
|
|
* |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
1 |
|
public function getRawContent() : string |
135
|
|
|
{ |
136
|
1 |
|
return $this->rawContent; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Return all lines |
141
|
|
|
* |
142
|
|
|
* This includes lines that are comments. |
143
|
|
|
* |
144
|
|
|
* @return array |
145
|
|
|
*/ |
146
|
2 |
|
public function getLines() : array |
147
|
|
|
{ |
148
|
2 |
|
return $this->rawLines; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Return line count |
153
|
|
|
* |
154
|
|
|
* This includes lines that are comments. |
155
|
|
|
* |
156
|
|
|
* @return int |
157
|
|
|
*/ |
158
|
3 |
|
public function getLineCount() : int |
159
|
|
|
{ |
160
|
3 |
|
return $this->rawLineCount; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Get a specific line |
165
|
|
|
* |
166
|
|
|
* @param int $index |
167
|
|
|
* @return string |
168
|
|
|
*/ |
169
|
2 |
|
public function getLine(int $index) : string |
170
|
|
|
{ |
171
|
2 |
|
return isset($this->rawLines[$index]) ? $this->rawLines[$index] : ''; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Return first line |
176
|
|
|
* |
177
|
|
|
* @return string |
178
|
|
|
*/ |
179
|
3 |
|
public function getSubject() : string |
180
|
|
|
{ |
181
|
3 |
|
return $this->contentLines[0]; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Return content from line nr. 3 to the last line |
186
|
|
|
* |
187
|
|
|
* @return string |
188
|
|
|
*/ |
189
|
3 |
|
public function getBody() : string |
190
|
|
|
{ |
191
|
3 |
|
return implode(PHP_EOL, $this->getBodyLines()); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Return lines from line nr. 3 to the last line |
196
|
|
|
* |
197
|
|
|
* @return array |
198
|
|
|
*/ |
199
|
5 |
|
public function getBodyLines() : array |
200
|
|
|
{ |
201
|
5 |
|
return $this->contentLineCount < 3 ? [] : array_slice($this->contentLines, 2); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Get the comment character |
206
|
|
|
* |
207
|
|
|
* Null would indicate the comment character is not set for this commit. A comment character might be null if this |
208
|
|
|
* commit came from a message stored in the repository, it would be populated if it came from a commit-msg hook. |
209
|
|
|
* |
210
|
|
|
* @return null|string |
211
|
|
|
*/ |
212
|
1 |
|
public function getCommentCharacter() |
213
|
|
|
{ |
214
|
1 |
|
return $this->commentCharacter; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Get the lines that are not comments |
219
|
|
|
* |
220
|
|
|
* Null comment character indicates no comment character. |
221
|
|
|
* |
222
|
|
|
* @param array $rawLines |
223
|
|
|
* @param string|null $commentCharacter |
224
|
|
|
* @return string[] |
225
|
|
|
*/ |
226
|
22 |
|
private function getContentLines(array $rawLines, string $commentCharacter = null) : array |
227
|
|
|
{ |
228
|
22 |
|
$lines = []; |
229
|
|
|
|
230
|
22 |
|
foreach($rawLines as $line) { |
231
|
20 |
|
if(!isset($line{0}) || $line{0} !== $commentCharacter) { |
232
|
19 |
|
$lines[] = $line; |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
236
|
22 |
|
return $lines; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Create CommitMessage from file |
241
|
|
|
* |
242
|
|
|
* @param string $path |
243
|
|
|
* @param string|null $commentCharacter |
244
|
|
|
* @return \SebastianFeldmann\Git\CommitMessage |
245
|
|
|
*/ |
246
|
3 |
|
public static function createFromFile(string $path, $commentCharacter = '#') : CommitMessage |
247
|
|
|
{ |
248
|
3 |
|
if (!file_exists($path)) { |
249
|
1 |
|
throw new \RuntimeException('Commit message file not found'); |
250
|
|
|
} |
251
|
|
|
|
252
|
2 |
|
return new CommitMessage(file_get_contents($path), $commentCharacter); |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|