forked from phacility/phabricator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhabricatorRepositoryCommitData.php
191 lines (150 loc) · 4.67 KB
/
PhabricatorRepositoryCommitData.php
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
final class PhabricatorRepositoryCommitData extends PhabricatorRepositoryDAO {
protected $commitID;
protected $authorName = '';
protected $commitMessage = '';
protected $commitDetails = array();
private $commitRef;
protected function getConfiguration() {
return array(
self::CONFIG_TIMESTAMPS => false,
self::CONFIG_SERIALIZATION => array(
'commitDetails' => self::SERIALIZATION_JSON,
),
self::CONFIG_COLUMN_SCHEMA => array(
'authorName' => 'text',
'commitMessage' => 'text',
),
self::CONFIG_KEY_SCHEMA => array(
'commitID' => array(
'columns' => array('commitID'),
'unique' => true,
),
),
) + parent::getConfiguration();
}
public function getSummary() {
$message = $this->getCommitMessage();
return self::summarizeCommitMessage($message);
}
public static function summarizeCommitMessage($message) {
$max_bytes = id(new PhabricatorRepositoryCommit())
->getColumnMaximumByteLength('summary');
$summary = phutil_split_lines($message, $retain_endings = false);
$summary = head($summary);
$summary = id(new PhutilUTF8StringTruncator())
->setMaximumBytes($max_bytes)
->setMaximumGlyphs(80)
->truncateString($summary);
return $summary;
}
public function getCommitDetail($key, $default = null) {
return idx($this->commitDetails, $key, $default);
}
public function setCommitDetail($key, $value) {
$this->commitDetails[$key] = $value;
return $this;
}
public function toDictionary() {
return array(
'commitID' => $this->commitID,
'authorName' => $this->authorName,
'commitMessage' => $this->commitMessage,
'commitDetails' => json_encode($this->commitDetails),
);
}
public static function newFromDictionary(array $dict) {
return id(new PhabricatorRepositoryCommitData())
->loadFromArray($dict);
}
public function newPublisherHoldReasons() {
$holds = $this->getCommitDetail('holdReasons');
// Look for the legacy "autocloseReason" if we don't have a modern list
// of hold reasons.
if (!$holds) {
$old_hold = $this->getCommitDetail('autocloseReason');
if ($old_hold) {
$holds = array($old_hold);
}
}
if (!$holds) {
$holds = array();
}
foreach ($holds as $key => $reason) {
$holds[$key] = PhabricatorRepositoryPublisherHoldReason::newForHoldKey(
$reason);
}
return array_values($holds);
}
public function getAuthorString() {
$ref = $this->getCommitRef();
$author = $ref->getAuthor();
if (strlen($author)) {
return $author;
}
$author = phutil_string_cast($this->authorName);
if (strlen($author)) {
return $author;
}
return null;
}
public function getAuthorDisplayName() {
return $this->getCommitRef()->getAuthorName();
}
public function getAuthorEmail() {
return $this->getCommitRef()->getAuthorEmail();
}
public function getAuthorEpoch() {
$epoch = $this->getCommitRef()->getAuthorEpoch();
if ($epoch) {
return (int)$epoch;
}
return null;
}
public function getCommitterString() {
$ref = $this->getCommitRef();
$committer = $ref->getCommitter();
if (strlen($committer)) {
return $committer;
}
return $this->getCommitDetailString('committer');
}
public function getCommitterDisplayName() {
return $this->getCommitRef()->getCommitterName();
}
public function getCommitterEmail() {
return $this->getCommitRef()->getCommitterEmail();
}
private function getCommitDetailString($key) {
$string = $this->getCommitDetail($key);
$string = phutil_string_cast($string);
if (strlen($string)) {
return $string;
}
return null;
}
public function setCommitRef(DiffusionCommitRef $ref) {
$this->setCommitDetail('ref', $ref->newDictionary());
$this->commitRef = null;
return $this;
}
public function getCommitRef() {
if ($this->commitRef === null) {
$map = $this->getCommitDetail('ref', array());
if (!is_array($map)) {
$map = array();
}
$map = $map + array(
'authorName' => $this->getCommitDetailString('authorName'),
'authorEmail' => $this->getCommitDetailString('authorEmail'),
'authorEpoch' => $this->getCommitDetailString('authorEpoch'),
'committerName' => $this->getCommitDetailString('committerName'),
'committerEmail' => $this->getCommitDetailString('committerEmail'),
'message' => $this->getCommitMessage(),
);
$ref = DiffusionCommitRef::newFromDictionary($map);
$this->commitRef = $ref;
}
return $this->commitRef;
}
}