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