1 | <?php |
||
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 | * Commit Message content |
||
82 | * |
||
83 | * This excludes lines that are comments |
||
84 | * |
||
85 | * @var string |
||
86 | */ |
||
87 | private $content; |
||
88 | |||
89 | /** |
||
90 | * CommitMessage constructor. |
||
91 | * |
||
92 | * @param string $content |
||
93 | * @param string|null $commentCharacter |
||
94 | */ |
||
95 | 21 | public function __construct(string $content, string $commentCharacter = null) |
|
106 | |||
107 | /** |
||
108 | * Is message empty. |
||
109 | * |
||
110 | * @return bool |
||
111 | */ |
||
112 | 2 | public function isEmpty() : bool |
|
116 | |||
117 | /** |
||
118 | * Get complete commit message content. |
||
119 | * |
||
120 | * This includes lines that are comments |
||
121 | * |
||
122 | * @return string |
||
123 | */ |
||
124 | 2 | public function getContent() : string |
|
128 | |||
129 | /** |
||
130 | * Return all lines. |
||
131 | * |
||
132 | * This includes lines that are comments |
||
133 | * |
||
134 | * @return array |
||
135 | */ |
||
136 | 2 | public function getLines() : array |
|
140 | |||
141 | /** |
||
142 | * Return line count. |
||
143 | * |
||
144 | * This includes lines that are comments |
||
145 | * |
||
146 | * @return int |
||
147 | */ |
||
148 | 3 | public function getLineCount() : int |
|
152 | |||
153 | /** |
||
154 | * Get a specific line. |
||
155 | * |
||
156 | * @param int $index |
||
157 | * @return string |
||
158 | */ |
||
159 | 2 | public function getLine(int $index) : string |
|
163 | |||
164 | /** |
||
165 | * Return first line. |
||
166 | * |
||
167 | * @return string |
||
168 | */ |
||
169 | 3 | public function getSubject() : string |
|
173 | |||
174 | /** |
||
175 | * Return content from line nr. 3 to the last line. |
||
176 | * |
||
177 | * @return string |
||
178 | */ |
||
179 | 3 | public function getBody() : string |
|
183 | |||
184 | /** |
||
185 | * Return lines from line nr. 3 to the last line. |
||
186 | * |
||
187 | * @return array |
||
188 | */ |
||
189 | 5 | public function getBodyLines() : array |
|
193 | |||
194 | /** |
||
195 | * Get the comment character |
||
196 | * |
||
197 | * Null would indicate the comment character is not set for this commit. A comment character might be null if this |
||
198 | * commit came from a message stored in the repository, it would be populated if it came from a commit-msg hook. |
||
199 | * |
||
200 | * @return null|string |
||
201 | */ |
||
202 | 1 | public function getCommentCharacter() |
|
206 | |||
207 | /** |
||
208 | * Get the lines that are not comments |
||
209 | * |
||
210 | * Null comment character indicates no comment character. |
||
211 | * |
||
212 | * @param array $rawLines |
||
213 | * @param string|null $commentCharacter |
||
214 | * @return string[] |
||
215 | */ |
||
216 | 21 | private function getContentLines(array $rawLines, string $commentCharacter = null) : array { |
|
227 | |||
228 | /** |
||
229 | * Create CommitMessage from file. |
||
230 | * |
||
231 | * @param string $path |
||
232 | * @param string|null $commentCharacter |
||
233 | * @return \SebastianFeldmann\Git\CommitMessage |
||
234 | */ |
||
235 | 3 | public static function createFromFile(string $path, $commentCharacter = '#') : CommitMessage |
|
243 | } |
||
244 |