|
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 $lines; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Get the number of lines |
|
71
|
|
|
* |
|
72
|
|
|
* This excludes lines which are comments |
|
73
|
|
|
* |
|
74
|
|
|
* @var int |
|
75
|
|
|
*/ |
|
76
|
|
|
private $lineCount; |
|
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
|
21 |
|
public function __construct(string $content, string $commentCharacter = null) |
|
94
|
|
|
{ |
|
95
|
21 |
|
$this->rawContent = $content; |
|
96
|
21 |
|
$this->rawLines = empty($content) ? [] : preg_split("/\\r\\n|\\r|\\n/", $content); |
|
97
|
21 |
|
$this->rawLineCount = count($this->rawLines); |
|
98
|
|
|
|
|
99
|
21 |
|
$this->commentCharacter = $commentCharacter; |
|
100
|
21 |
|
$this->lines = $this->getContentLines($this->rawLines, $commentCharacter); |
|
101
|
21 |
|
$this->lineCount = count($this->lines); |
|
102
|
21 |
|
$this->content = implode(PHP_EOL, $this->lines); |
|
103
|
21 |
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Is message empty. |
|
107
|
|
|
* |
|
108
|
|
|
* @return bool |
|
109
|
|
|
*/ |
|
110
|
2 |
|
public function isEmpty() : bool |
|
111
|
|
|
{ |
|
112
|
2 |
|
return empty($this->content); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Get complete commit message content. |
|
117
|
|
|
* |
|
118
|
|
|
* This includes lines that are comments |
|
119
|
|
|
* |
|
120
|
|
|
* @return string |
|
121
|
|
|
*/ |
|
122
|
2 |
|
public function getContent() : string |
|
123
|
|
|
{ |
|
124
|
2 |
|
return $this->rawContent; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Return all lines. |
|
129
|
|
|
* |
|
130
|
|
|
* This includes lines that are comments |
|
131
|
|
|
* |
|
132
|
|
|
* @return array |
|
133
|
|
|
*/ |
|
134
|
2 |
|
public function getLines() : array |
|
135
|
|
|
{ |
|
136
|
2 |
|
return $this->rawLines; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Return line count. |
|
141
|
|
|
* |
|
142
|
|
|
* This includes lines that are comments |
|
143
|
|
|
* |
|
144
|
|
|
* @return int |
|
145
|
|
|
*/ |
|
146
|
3 |
|
public function getLineCount() : int |
|
147
|
|
|
{ |
|
148
|
3 |
|
return $this->rawLineCount; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Get a specific line. |
|
153
|
|
|
* |
|
154
|
|
|
* @param int $index |
|
155
|
|
|
* @return string |
|
156
|
|
|
*/ |
|
157
|
2 |
|
public function getLine(int $index) : string |
|
158
|
|
|
{ |
|
159
|
2 |
|
return isset($this->rawLines[$index]) ? $this->rawLines[$index] : ''; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Return first line. |
|
164
|
|
|
* |
|
165
|
|
|
* @return string |
|
166
|
|
|
*/ |
|
167
|
3 |
|
public function getSubject() : string |
|
168
|
|
|
{ |
|
169
|
3 |
|
return $this->lines[0]; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Return content from line nr. 3 to the last line. |
|
174
|
|
|
* |
|
175
|
|
|
* @return string |
|
176
|
|
|
*/ |
|
177
|
3 |
|
public function getBody() : string |
|
178
|
|
|
{ |
|
179
|
3 |
|
return implode(PHP_EOL, $this->getBodyLines()); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Return lines from line nr. 3 to the last line. |
|
184
|
|
|
* |
|
185
|
|
|
* @return array |
|
186
|
|
|
*/ |
|
187
|
5 |
|
public function getBodyLines() : array |
|
188
|
|
|
{ |
|
189
|
5 |
|
return $this->lineCount < 3 ? [] : array_slice($this->lines, 2); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Get the comment character |
|
194
|
|
|
* |
|
195
|
|
|
* Null would indicate the comment character is not set for this commit. A comment character might be null if this |
|
196
|
|
|
* commit came from a message stored in the repository, it would be populated if it came from a commit-msg hook. |
|
197
|
|
|
* |
|
198
|
|
|
* @return null|string |
|
199
|
|
|
*/ |
|
200
|
1 |
|
public function getCommentCharacter() |
|
201
|
|
|
{ |
|
202
|
1 |
|
return $this->commentCharacter; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Get the lines that are not comments |
|
207
|
|
|
* |
|
208
|
|
|
* Null comment character indicates no comment character. |
|
209
|
|
|
* |
|
210
|
|
|
* @param array $rawLines |
|
211
|
|
|
* @param string|null $commentCharacter |
|
212
|
|
|
* @return string[] |
|
213
|
|
|
*/ |
|
214
|
21 |
|
private function getContentLines(array $rawLines, string $commentCharacter = null) : array { |
|
215
|
21 |
|
$lines = []; |
|
216
|
|
|
|
|
217
|
21 |
|
foreach($rawLines as $line) { |
|
218
|
19 |
|
if(!isset($line{0}) || $line{0} !== $commentCharacter) { |
|
219
|
18 |
|
$lines[] = $line; |
|
220
|
|
|
} |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
21 |
|
return $lines; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Create CommitMessage from file. |
|
228
|
|
|
* |
|
229
|
|
|
* @param string $path |
|
230
|
|
|
* @param string|null $commentCharacter |
|
231
|
|
|
* @return \SebastianFeldmann\Git\CommitMessage |
|
232
|
|
|
*/ |
|
233
|
3 |
|
public static function createFromFile(string $path, $commentCharacter = '#') : CommitMessage |
|
234
|
|
|
{ |
|
235
|
3 |
|
if (!file_exists($path)) { |
|
236
|
1 |
|
throw new \RuntimeException('Commit message file not found'); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
2 |
|
return new CommitMessage(file_get_contents($path), $commentCharacter); |
|
240
|
|
|
} |
|
241
|
|
|
} |
|
242
|
|
|
|