|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of CaptainHook. |
|
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\CaptainHook\Git; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class CommitMessage |
|
14
|
|
|
* |
|
15
|
|
|
* @package CaptainHook |
|
16
|
|
|
* @author Sebastian Feldmann <[email protected]> |
|
17
|
|
|
* @link https://github.com/sebastianfeldmann/captainhook |
|
18
|
|
|
* @since Class available since Release 0.9.0 |
|
19
|
|
|
*/ |
|
20
|
|
|
class CommitMessage |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Commit Message content |
|
24
|
|
|
* |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
private $content; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Content split lines |
|
31
|
|
|
* |
|
32
|
|
|
* @var string[] |
|
33
|
|
|
*/ |
|
34
|
|
|
private $lines; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Amount of lines |
|
38
|
|
|
* |
|
39
|
|
|
* @var int |
|
40
|
|
|
*/ |
|
41
|
|
|
private $lineCount; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* CommitMessage constructor. |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $content |
|
47
|
|
|
*/ |
|
48
|
36 |
|
public function __construct($content) |
|
49
|
|
|
{ |
|
50
|
36 |
|
$this->content = $content; |
|
51
|
36 |
|
$this->lines = empty($content) ? [] : preg_split("/\\r\\n|\\r|\\n/", $content); |
|
52
|
36 |
|
$this->lineCount = count($this->lines); |
|
53
|
36 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Is message empty. |
|
57
|
|
|
* |
|
58
|
|
|
* @return bool |
|
59
|
|
|
*/ |
|
60
|
13 |
|
public function isEmpty() |
|
61
|
|
|
{ |
|
62
|
13 |
|
return empty($this->content); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Get complete commit message content. |
|
67
|
|
|
* |
|
68
|
|
|
* @return string |
|
69
|
|
|
*/ |
|
70
|
1 |
|
public function getContent() |
|
71
|
|
|
{ |
|
72
|
1 |
|
return $this->content; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Return all lines. |
|
77
|
|
|
* |
|
78
|
|
|
* @return array |
|
79
|
|
|
*/ |
|
80
|
1 |
|
public function getLines() |
|
81
|
|
|
{ |
|
82
|
1 |
|
return $this->lines; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Return line count. |
|
87
|
|
|
* |
|
88
|
|
|
* @return int |
|
89
|
|
|
*/ |
|
90
|
7 |
|
public function getLineCount() |
|
91
|
|
|
{ |
|
92
|
7 |
|
return $this->lineCount; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Get a specific line. |
|
97
|
|
|
* |
|
98
|
|
|
* @param int $index |
|
99
|
|
|
* @return string |
|
100
|
|
|
*/ |
|
101
|
3 |
|
public function getLine($index) |
|
102
|
|
|
{ |
|
103
|
3 |
|
return isset($this->lines[$index]) ? $this->lines[$index] : ''; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Return first line. |
|
108
|
|
|
* |
|
109
|
|
|
* @return string |
|
110
|
|
|
*/ |
|
111
|
14 |
|
public function getSubject() |
|
112
|
|
|
{ |
|
113
|
14 |
|
return $this->lines[0]; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Return content from line nr. 3 to the last line. |
|
118
|
|
|
* |
|
119
|
|
|
* @return string |
|
120
|
|
|
*/ |
|
121
|
1 |
|
public function getBody() |
|
122
|
|
|
{ |
|
123
|
1 |
|
return implode(PHP_EOL, $this->getBodyLines()); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Return lines from line nr. 3 to the last line. |
|
128
|
|
|
* |
|
129
|
|
|
* @return array |
|
130
|
|
|
*/ |
|
131
|
7 |
|
public function getBodyLines() |
|
132
|
|
|
{ |
|
133
|
7 |
|
return $this->lineCount < 3 ? [] : array_slice($this->lines, 2); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Create CommitMessage from file. |
|
138
|
|
|
* |
|
139
|
|
|
* @param string $path |
|
140
|
|
|
* @return \sebastianfeldmann\CaptainHook\Git\CommitMessage |
|
141
|
|
|
*/ |
|
142
|
3 |
|
public static function createFromFile($path) |
|
143
|
|
|
{ |
|
144
|
3 |
|
if (!file_exists($path)) { |
|
145
|
1 |
|
throw new \RuntimeException('Commit message file not found'); |
|
146
|
|
|
} |
|
147
|
2 |
|
return new CommitMessage(file_get_contents($path)); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|