forked from silverstripe/silverstripe-installer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreateChangelog.php
302 lines (253 loc) · 8.61 KB
/
CreateChangelog.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
294
295
296
297
298
299
300
301
302
<?php
include_once dirname(__FILE__) . '/SilverStripeBuildTask.php';
/**
* Returns changelogs for the git (or svn) repositories specified in the changelog-definitions file
*
* @author jseide
*
*/
class CreateChangelog extends SilverStripeBuildTask {
protected $definitions = null;
protected $baseDir = null;
protected $sort = 'type';
protected $filter = null;
/**
* Order of the array keys determines order of the lists.
*/
public $types = array(
'API Changes' => array('/^API CHANGE:?/i','/^APICHANGE?:?/i'),
'Features and Enhancements' => array('/^(ENHANCEMENT|ENHNACEMENT):?/i', '/^FEATURE:?/i'),
'Bugfixes' => array('/^(BUGFIX|BUGFUX):?/i','/^BUG FIX:?/i'),
'Minor changes' => array('/^MINOR:?/i'),
'Other' => array('/^[^A-Z][^A-Z][^A-Z]/') // dirty trick: check for uppercase characters
);
public $commitUrls = array(
'.' => 'https://github.com/silverstripe/silverstripe-installer/commit/%s',
'sapphire' => 'https://github.com/silverstripe/sapphire/commit/%s',
'cms' => 'https://github.com/silverstripe/silverstripe-cms/commit/%s',
'themes/blackcandy' => 'https://github.com/silverstripe-themes/silverstripe-blackcandy/commit/%s',
);
public $ignoreRules = array(
'/^Merge/',
'/^Blocked revisions/',
'/^Initialized merge tracking /',
'/^Created (branches|tags)/',
'/^NOTFORMERGE/',
'/^\s*$/'
);
public function setDefinitions($definitions) {
$this->definitions = $definitions;
}
public function setBaseDir($base) {
$this->baseDir = realpath($base);
}
public function setSort($sort) {
$this->sort = $sort;
}
public function setFilter($filter) {
$this->filter = $filter;
}
/**
* Checks is a folder is a version control repository
*/
protected function isRepository($dir_path, $filter) {
$dir = $dir_path;
if (file_exists($dir)) {
// open this directory
if ($handle = opendir($dir)) {
// get each file
while (false !== ($file = readdir($handle))) {
if ($file == $filter && is_dir($file)) {
if ($filter == '.git') { //extra check for git repos
if (file_exists($dir.'/'.$file.'/HEAD')) {
return true; //$dir is a git repository
}
} else { //return true for .svn repos
return true;
}
}
}
echo "Folder '$dir' is not a $filter repository\n";
}
} else {
echo "Folder '$dir' does not exist\n";
}
return false;
}
protected function isGitRepo($dir) {
return $this->isRepository($dir, '.git');
}
protected function isSvnRepo($dir) {
return $this->isRepository($dir, '.svn');
}
protected function gitLog($path, $from = null, $to = null) {
//set the from -> to range, depending on which os these have been set
if ($from && $to) $range = " $from..$to";
elseif ($from) $range = " $from..HEAD";
else $range = "";
$this->log(sprintf('Changing to directory "%s"', $path), Project::MSG_INFO);
chdir("$this->baseDir/$path"); //switch to the module's path
// Internal serialization format, ideally this would be JSON but we can't escape characters in git logs.
$log = $this->exec("git log --pretty=tformat:\"message:%s|||author:%aN|||abbrevhash:%h|||hash:%H|||date:%ad|||timestamp:%at\" --date=short {$range}", true);
chdir($this->baseDir); //switch the working directory back
return $log;
}
/** Sort by the first two letters of the commit string.
* Put any commits without BUGFIX, ENHANCEMENT, etc. at the end of the list
*/
function sortByType($commits) {
$groupedByType = array();
// sort by timestamp
usort($commits, function($a, $b) {
if($a['timestamp'] == $b['timestamp']) return 0;
else return ($a['timestamp'] > $b['timestamp']) ? -1 : 1;
});
foreach($commits as $k => $commit) {
// TODO
// skip ignored revisions
// if(in_array($commit['changeset'], $this->ignorerevisions)) continue;
// Remove email addresses
$commit['message'] = preg_replace('/(<?[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}>?)/mi', '', $commit['message']);
// Condense git-style "From:" messages (remove preceding newline)
if(preg_match('/^From\:/mi', $commit['message'])) {
$commit['message'] = preg_replace('/\n\n^(From\:)/mi', ' $1', $commit['message']);
}
$matched = false;
foreach($this->types as $name => $rules) {
if(!isset($groupedByType[$name])) $groupedByType[$name] = array();
foreach($rules as $rule) {
if(!$matched && preg_match($rule, $commit['message'])) {
// @todo The fallback rule on other can't be replaced, as it doesn't match a full prefix
$commit['message'] = ($name != 'Other') ? trim(preg_replace($rule, '', $commit['message'])) : $commit['message'];
$groupedByType[$name][] = $commit;
$matched = true;
}
}
}
if(!$matched) {
if(!isset($groupedByType['Other'])) $groupedByType['Other'] = array();
$groupedByType['Other'][] = $commit;
}
}
// // remove all categories which should be ignored
// if($this->categoryIgnore) foreach($this->categoryIgnore as $categoryIgnore) {
// if(isset($groupedByType[$categoryIgnore])) unset($groupedByType[$categoryIgnore]);
// }
return $groupedByType;
}
function commitToArray($commit) {
$arr = array();
$parts = explode('|||', $commit);
foreach($parts as $part) {
preg_match('/([^:]*)\:(.*)/', $part, $matches);
$arr[$matches[1]] = $matches[2];
}
return $arr;
}
static function isupper($i) {
return (strtoupper($i) === $i);
}
static function islower($i) {
return (strtolower($i) === $i);
}
public function main() {
error_reporting(E_ALL);
chdir($this->baseDir); //change current working directory
//parse the definitions file
$items = file($this->definitions);
$repos = array(); //git (or svn) repos to scan
foreach ($items as $item) {
$item = trim($item);
if (strpos($item, '#') === 0) {
continue;
}
$bits = preg_split('/\s+/', $item);
if (count($bits) == 1) {
$repos[$bits[0]] = "";
} elseif (count($bits) == 2) {
$repos[$bits[0]] = array($bits[1], null); //sapphire => array(from => HEAD)
} elseif (count($bits) == 3) {
$repos[$bits[0]] = array($bits[1],$bits[2]); //sapphire => array(from => to)
} else {
continue;
}
}
//check all the paths are valid git repos
$gitRepos = array();
$svnRepos = array();
foreach($repos as $path => $range) {
if ($this->isGitRepo($path)) $gitRepos[$path] = $range; //add all git repos to a special array
//TODO: for svn support use the isSvnRepo() method to add repos to the svnRepos array
}
//run git log
$log = array();
foreach($gitRepos as $path => $range) {
$logForPath = array();
if (!empty($range)) {
$from = (isset($range[0])) ? $range[0] : null;
$to = (isset($range[1])) ? $range[1] : null;
$logForPath = explode("\n", $this->gitLog($path, $from, $to));
} else {
$logForPath = explode("\n", $this->gitLog($path));
}
foreach($logForPath as $commit) {
if(!$commit) continue;
$commitArr = $this->commitToArray($commit);
$commitArr['path'] = $path;
// Avoid duplicates by keying on hash
$log[$commitArr['hash']] = $commitArr;
}
}
// Remove ignored commits
foreach($log as $k => $commit) {
$ignore = false;
foreach($this->ignoreRules as $ignoreRule) {
if(preg_match($ignoreRule, $commit['message'])) {
unset($log[$k]);
continue;
}
}
}
//sort the output (based on params), grouping
if ($this->sort == 'type') {
$groupedLog = $this->sortByType($log);
} else {
//leave as sorted by default order
$groupedLog = array('All' => $log);
}
//filter out commits we don't want
// if ($this->filter) {
// foreach($groupedLog as $key => $item) {
// if (preg_match($this->filter, $item)) unset($groupedLog[$key]);
// }
// }
//convert to string
//and generate markdown (add list to beginning of each item)
$output = "\n";
foreach($groupedLog as $groupName => $commits) {
if(!$commits) continue;
$output .= "\n### $groupName\n\n";
foreach($commits as $commit) {
if(isset($this->commitUrls[$commit['path']])) {
$hash = sprintf('[%s](%s)',
$commit['abbrevhash'],
sprintf($this->commitUrls[$commit['path']], $commit['abbrevhash'])
);
} else {
$hash = sprintf('[%s]', $commit['abbrevhash']);
}
$commitStr = sprintf('%s %s %s (%s)',
$commit['date'],
$hash,
// Avoid rendering HTML in markdown
str_replace(array('<', '>'), array('<', '>'), $commit['message']),
$commit['author']
);
// $commitStr = sprintf($this->exec("git log --pretty=tformat:\"%s\" --date=short {$hash}^..{$hash}", true), $this->gitLogFormat);
$output .= " * $commitStr\n";
}
}
$this->project->setProperty('changelogOutput',$output);
}
}
?>