-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommit.php
More file actions
140 lines (125 loc) · 3.14 KB
/
Commit.php
File metadata and controls
140 lines (125 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
namespace Phug\Split\Git;
use Exception;
class Commit
{
/**
* Hash of the commit.
*
* @var string
*/
protected $hash;
/**
* Author date, name and e-mail address.
*
* @var Act
*/
protected $author;
/**
* Committer date, name and e-mail address.
*
* @var Act
*/
protected $commit;
/**
* Commit message.
*
* @var string
*/
protected $message;
public function __construct(string $hash, Act $author, Act $commit, string $message)
{
$this->hash = $hash;
$this->author = $author;
$this->commit = $commit;
$this->message = $message;
}
/**
* Create a Commit instance from a git log commit string.
*
* @param string $log raw string from git log command.
*
* @throws InvalidGitLogStringException|Exception
*
* @return self
*/
public static function fromGitLogString(string $log): self
{
$log = str_replace(["\r\n", "\r"], "\n", $log);
if (!preg_match('/^'.implode('\n', [
'commit (?<hash>\S+)[\S\s]*',
'Author: (?<authorName>[^<]+)<(?<authorEmail>[^>]+)>(?: .*)?',
'AuthorDate: (?<authorDate>.+)',
'Commit: (?<commitName>[^<]+)<(?<commitEmail>[^>]+)>(?: .*)?',
'CommitDate: (?<commitDate>.+)',
'(?:.+\n)*\n (?<message>[\S\s]+)',
]).'$/', $log, $matches)) {
throw new InvalidGitLogStringException($log);
}
return new self(
$matches['hash'],
new Act(
new Author(trim($matches['authorName']), trim($matches['authorEmail'])),
new Date($matches['authorDate']),
),
new Act(
new Author(trim($matches['commitName']), trim($matches['commitEmail'])),
new Date($matches['commitDate']),
),
(string) preg_replace('/^ {4}/m', '', trim($matches['message'])),
);
}
/**
* Hash of the commit.
*
* @return string
*/
public function getHash(): string
{
return $this->hash;
}
/**
* Get author date, name and e-mail address.
*
* @return Act
*/
public function getAuthor(): Act
{
return $this->author;
}
/**
* Get committer date, name and e-mail address.
*
* @return Act
*/
public function getCommit(): Act
{
return $this->commit;
}
/**
* Get commit message.
*
* @return string
*/
public function getMessage(): string
{
return $this->message;
}
/**
* Return preg_match first captured parenthesis in the commit message by a given regular expression.
*
* @param non-empty-string $regExp
*
* @return string|null
*
* @psalm-return truthy-string|null
*
* @psalm-suppress LessSpecificReturnStatement, MoreSpecificReturnType
*/
public function findInMessage(string $regExp): ?string
{
return preg_match($regExp, $this->message, $matches)
? $matches[1] ?? null
: null;
}
}