forked from phacility/phabricator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDifferentialLocalCommitsView.php
151 lines (126 loc) · 3.57 KB
/
DifferentialLocalCommitsView.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
<?php
final class DifferentialLocalCommitsView extends AphrontView {
private $localCommits;
private $commitsForLinks = array();
public function setLocalCommits($local_commits) {
$this->localCommits = $local_commits;
return $this;
}
public function setCommitsForLinks(array $commits) {
assert_instances_of($commits, 'PhabricatorRepositoryCommit');
$this->commitsForLinks = $commits;
return $this;
}
public function render() {
$viewer = $this->getViewer();
$local = $this->localCommits;
if (!$local) {
return null;
}
$has_tree = false;
$has_local = false;
foreach ($local as $commit) {
if (idx($commit, 'tree')) {
$has_tree = true;
}
if (idx($commit, 'local')) {
$has_local = true;
}
}
$rows = array();
foreach ($local as $commit) {
$row = array();
if (idx($commit, 'commit')) {
$commit_link = $this->buildCommitLink($commit['commit']);
} else if (isset($commit['rev'])) {
$commit_link = $this->buildCommitLink($commit['rev']);
} else {
$commit_link = null;
}
$row[] = $commit_link;
if ($has_tree) {
$row[] = $this->buildCommitLink($commit['tree']);
}
if ($has_local) {
$row[] = $this->buildCommitLink($commit['local']);
}
$parents = idx($commit, 'parents', array());
foreach ($parents as $k => $parent) {
if (is_array($parent)) {
$parent = idx($parent, 'rev');
}
$parents[$k] = $this->buildCommitLink($parent);
}
$parents = phutil_implode_html(phutil_tag('br'), $parents);
$row[] = $parents;
$author = nonempty(
idx($commit, 'user'),
idx($commit, 'author'));
$row[] = $author;
$message = idx($commit, 'message');
$summary = idx($commit, 'summary');
$summary = id(new PhutilUTF8StringTruncator())
->setMaximumGlyphs(80)
->truncateString($summary);
$view = new AphrontMoreView();
$view->setSome($summary);
if ($message && (trim($summary) != trim($message))) {
$view->setMore(phutil_escape_html_newlines($message));
}
$row[] = $view->render();
$date = nonempty(
idx($commit, 'date'),
idx($commit, 'time'));
if ($date) {
$date = phabricator_datetime($date, $viewer);
}
$row[] = $date;
$rows[] = $row;
}
$column_classes = array('');
if ($has_tree) {
$column_classes[] = '';
}
if ($has_local) {
$column_classes[] = '';
}
$column_classes[] = '';
$column_classes[] = '';
$column_classes[] = 'wide';
$column_classes[] = 'date';
$table = id(new AphrontTableView($rows))
->setColumnClasses($column_classes);
$headers = array();
$headers[] = pht('Commit');
if ($has_tree) {
$headers[] = pht('Tree');
}
if ($has_local) {
$headers[] = pht('Local');
}
$headers[] = pht('Parents');
$headers[] = pht('Author');
$headers[] = pht('Summary');
$headers[] = pht('Date');
$table->setHeaders($headers);
return $table;
}
private static function formatCommit($commit) {
return substr($commit, 0, 12);
}
private function buildCommitLink($hash) {
$commit_for_link = idx($this->commitsForLinks, $hash);
$commit_hash = self::formatCommit($hash);
if ($commit_for_link) {
$link = phutil_tag(
'a',
array(
'href' => $commit_for_link->getURI(),
),
$commit_hash);
} else {
$link = $commit_hash;
}
return $link;
}
}