/
commitutils.php
293 lines (239 loc) · 9.41 KB
/
commitutils.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<?php
/** @file commitutils.php
* Utility functions for formatting commit messages
*
* @authors Copyright @ 2009-2013 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/
includeGuard('commitutils.php');
require_once(DIR_INCLUDES.'/utilities.inc.php');
require_once(DIR_CLASSES.'/url.class.php');
require_once('buildevent.class.php');
function addCommitToGroup(&$groups, $groupName, &$commit)
{
if(!is_array($groups))
throw new Exception('Invalid groups argument, array expected');
$key = array_casekey_exists($groupName, $groups);
if($key === false)
{
$groups[$groupName] = array();
$group = &$groups[$groupName];
}
else
{
$group = &$groups[$key];
}
$group[] = $commit;
}
function groupBuildCommits(&$build, &$groups)
{
if(!$build instanceof BuildEvent)
throw new Exception('Received invalid BuildEvent');
if(!is_array($groups))
throw new Exception('Invalid groups argument, array expected');
foreach($build->commits as &$commit)
{
if(!is_array($commit['tags']) || 0 === count($commit['tags']))
{
addCommitToGroup($groups, 'Miscellaneous', $commit);
continue;
}
$tags = $commit['tags'];
reset($tags);
$firstTag = key($tags);
addCommitToGroup($groups, $firstTag, $commit);
}
}
function make_pretty_hyperlink($matches)
{
$uri = new Url(implode('', array_slice($matches, 1)));
$homeUri = new Url(FrontController::fc()->homeURL());
$isExternal = ($uri->host() !== $homeUri->host());
if(!$isExternal)
{
$uri->setScheme()->setHost();
}
return generateHyperlinkHTML($uri, $isExternal? array('class'=>'link-external') : NULL, 40);
}
function formatCommitHTML($msg)
{
if(strcasecmp(gettype($msg), 'string')) return $msg;
// Process the commit message, replacing web URIs with clickable links.
htmlspecialchars($msg);
// Rules derived from Søren Løvborg's UrlLinker (see: http://www.kwi.dk/projects/php/UrlLinker/)
$rexProtocol = '(https?:\/\/|ftp:\/\/)';
$rexDomain = '(?:[-a-zA-Z0-9]{1,63}\.)+[a-zA-Z][-a-zA-Z0-9]{1,62}';
$rexIp = '(?:[1-9][0-9]{0,2}\.|0\.){3}(?:[1-9][0-9]{0,2}|0)';
$rexPort = '(:[0-9]{1,5})?';
$rexPath = '(/[!$-/0-9:;=@_\':;!a-zA-Z\x7f-\xff]*?)?';
$rexQuery = '(\?[!$-/0-9:;=@_\':;!a-zA-Z\x7f-\xff]+?)?';
$rexFragment = '(#[!$-/0-9:;=@_\':;!a-zA-Z\x7f-\xff]+?)?';
$rexUsername = '[^]\\\\\x00-\x20\"(),:-<>[\x7f-\xff]{1,64}';
$rexPassword = $rexUsername; // allow the same characters as in the username
$rexUrl = "$rexProtocol(?:($rexUsername)(:$rexPassword)?@)?($rexDomain|$rexIp)$rexPort$rexPath$rexQuery$rexFragment";
$msg = preg_replace_callback("&\\b$rexUrl(?=[?.!,;:\"]?(\s|$))&",
"make_pretty_hyperlink", $msg);
return $msg;
}
function formatCommitMessageHTML($msg)
{
if(strcasecmp(gettype($msg), 'string')) return $msg;
$msg = formatCommitHTML($msg);
$msg = nl2br($msg);
return $msg;
}
function outputCommitHTML(&$commit)
{
if(!is_array($commit))
throw new Exception('Invalid commit argument, array expected');
// Format the commit message for HTML output.
$title = formatCommitHTML($commit['title']);
$message = formatCommitMessageHTML($commit['message']);
$haveMessage = (bool)(strlen($message) > 0);
// Compose the supplementary tag list.
$tagList = '<div class="tag_list">';
if(is_array($commit['tags']))
{
$n = (integer)0;
foreach($commit['tags'] as $tag => $value)
{
// Skip the first tag (its used for grouping).
if($n++ === 0) continue;
// Do not output guessed tags (mainly used for grouping).
if(is_array($value) && isset($value['guessed']) && $value['guessed'] !== 0) continue;
$cleanTag = htmlspecialchars($tag);
$tagList .= '<a href="http://source.dengine.net/codex/find_tag.php?tag='. $tag .'" class="tag" title="Find commits tagged \''.$cleanTag.'\' in the Codex">'.$cleanTag.'</a>';
}
}
$tagList .= '</div>';
$repoLinkTitle = 'Show changes in the repository for this commit submitted on '. date(DATE_RFC2822, $commit['submitDate']) .'.';
// Ouput HTML for the commit.
?><span class="metadata"><a href="<?php echo $commit['repositoryUri']; ?>" class="link-external" title="<?php echo htmlspecialchars($repoLinkTitle); ?>"><?php echo htmlspecialchars(date('Y-m-d', $commit['submitDate'])); ?></a></span><?php
?><p class="heading <?php if($haveMessage) echo 'collapsible'; ?>" <?php if($haveMessage) echo 'title="Toggle commit message display"'; ?>><strong><span class="title"><?php echo $title; ?></span></strong> by <em><?php echo htmlspecialchars($commit['author']); ?></em></p><?php echo $tagList;
if($haveMessage)
{
?><div class="commit"><blockquote><?php echo $message; ?></blockquote></div><?php
}
}
function outputCommitJumpList2(&$groups)
{
if(!is_array($groups))
throw new Exception('Invalid groups argument, array expected');
?><ol class="jumplist"><?php
foreach($groups as $groupName => $group)
{
$commitCount = count($group);
$tagLinkTitle = "Jump to commits tagged '$groupName'";
?><li><label title="Number of commits with this tag"><span class="commit-count"><?php echo htmlspecialchars($commitCount); ?></span></label><a href="#<?php echo $groupName; ?>" title="<?php echo htmlspecialchars($tagLinkTitle); ?>"><?php echo htmlspecialchars($groupName); ?></a></li><?php
}
?></ol><?php
}
function outputCommitJumpList(&$groups)
{
if(!is_array($groups))
throw new Exception('Invalid groups argument, array expected');
$groupCount = count($groups);
// If only one list; apply the special 'hnav' class (force horizontal).
?><div class="jumplist_wrapper <?php if($groupCount <= 5) echo 'hnav'; ?>"><?php
// If the list is too long; split it into multiple sublists.
if($groupCount > 5)
{
$numLists = ceil($groupCount / 5);
for($i = (integer)0; $i < $numLists; $i++)
{
$subList = array_slice($groups, $i*5, 5);
outputCommitJumpList2($subList);
}
}
else
{
// Just the one list then.
outputCommitJumpList2($groups);
}
?></div><?php
}
/**
* Process the commit log of the given build event, generating HTML
* content directly to the output stream.
*
* @param build (object) BuildEvent object to process.
*/
function outputCommitLogHTML(&$build)
{
if(!$build instanceof BuildEvent)
throw new Exception('Received invalid BuildEvent');
$commitCount = count($build->commits);
if($commitCount)
{
$groups = array();
groupBuildCommits($build, $groups);
$groupCount = count($groups);
if($groupCount > 1)
{
ksort($groups);
// Generate a jump list?
if($commitCount > 15)
{
outputCommitJumpList($groups);
}
}
// Generate the commit list itself.
?><hr />
<div class="commit_list">
<ul><?php
foreach($groups as $groupName => $group)
{
?><li><?php
if($groupCount > 1)
{
?><strong><a href="http://source.dengine.net/codex/find_tag.php?tag=<?php echo $groupName; ?>" title="<?php echo htmlspecialchars("Find commits with primary tag '$groupName' in the Codex"); ?>" class="tag"><?php echo htmlspecialchars($groupName); ?></a></strong><a name="<?php echo htmlspecialchars($groupName); ?>"></a><a class="jump" href="#commitindex" title="Back to Commits index">index</a><br /><ol><?php
}
foreach($group as &$commit)
{
?><li <?php if(isset($commit['message']) && strlen($commit['message']) > 0) echo 'class="more"'; ?>><?php
outputCommitHTML($commit);
?></li><?php
}
?></ol></li><?php
}
?></ul></div><?php
}
}
/**
* @param build (object) BuildEvent to generate a commit log for.
* @return (boolean) @c true if a commit log was sent to output.
*/
function outputCommitLog(&$build)
{
if(!$build instanceof BuildEvent)
throw new Exception('Received invalid BuildEvent');
if(count($build->commits) <= 0) return FALSE;
$commitsCacheName = 'buildrepository/'.$build->uniqueId().'/commits.html';
try
{
FrontController::contentCache()->import($commitsCacheName);
}
catch(Exception $e)
{
$OutputCache = new OutputCache();
$OutputCache->start();
outputCommitLogHTML($build);
$content = $OutputCache->stop();
FrontController::contentCache()->store($commitsCacheName, $content);
print($content);
}
return TRUE;
}