Skip to content

Commit 37389dc

Browse files
committed
Add sorting order of commit subjects
- Commit subjects are listed unsorted or in ascending/descending order. - PHPUnit testBuild split to testBuildAscendingCommitOrder and testBuildDescendingCommitOrder.
1 parent 392db51 commit 37389dc

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

src/GitChangeLog.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ class GitChangeLog
148148
* addHashes True includes commit hashes to the listed subjects.
149149
* includeMergeCommits True includes merge commits in the subject lists.
150150
* tagOrderDesc True to sort the tags in descending order.
151+
* commitOrder Set to 'ASC' or 'DESC' to sort the subjects in resp. ascending/descending order.
151152
* </pre>
152153
*/
153154
protected $options = [
@@ -158,6 +159,7 @@ class GitChangeLog
158159
'addHashes' => true,
159160
'includeMergeCommits' => false,
160161
'tagOrderDesc' => true,
162+
'commitOrder' => 'ASC',
161163
];
162164

163165
/**
@@ -271,6 +273,16 @@ public function build(): void
271273
continue;
272274
}
273275

276+
// Sort commit subjects.
277+
switch ($this->options['commitOrder']) {
278+
case 'ASC':
279+
natsort($data['subjects']);
280+
break;
281+
case 'DESC':
282+
natsort($data['subjects']);
283+
$data['subjects'] = array_reverse($data['subjects'], true);
284+
}
285+
274286
// Add commit subjects.
275287
foreach ($data['subjects'] as $subjectKey => &$subject) {
276288
if ($this->options['addHashes']) {

tests/GitChangeLogTest.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,10 @@ public function testSetFromTag()
230230
$changeLog->setFromTag('DoesNotExist');
231231
}
232232

233-
public function testBuild()
233+
public function testBuildAscendingCommitOrder()
234234
{
235235
$changeLog = new GitChangeLog();
236+
$changeLog->setOptions('tagOrderDesc', false);
236237
$testValues =
237238
[
238239
// No tags.
@@ -263,6 +264,40 @@ public function testBuild()
263264
}
264265
}
265266

267+
public function testBuildDescendingCommitOrder()
268+
{
269+
$changeLog = new GitChangeLog();
270+
$changeLog->setOptions('commitOrder', 'DESC');
271+
$testValues =
272+
[
273+
// No tags.
274+
[],
275+
// Head Revision included.
276+
['HEAD' => ['date' => 'B', 'subjects' => ['C', 'D'], 'hashes' => [['E'], ['F']]]],
277+
// Dummy tag, no commits.
278+
['A' => ['date' => 'B', 'subjects' => [], 'hashes' => []]],
279+
// Dummy tag and commits.
280+
['A' => ['date' => 'B', 'subjects' => ['C', 'D'], 'hashes' => [['E', 'F'], ['G']]]],
281+
];
282+
$expectedValues =
283+
[
284+
//No tags
285+
"# Changelog\n\nNo changes.\n",
286+
// Head Revision included.
287+
"# Changelog\n\n## Upcoming changes (Undetermined)\n\n* D (F)\n* C (E)\n",
288+
// Dummy tag, no commits.
289+
"# Changelog\n\n## A (B)\n\n* No changes.\n",
290+
// Dummy tag and commits.
291+
"# Changelog\n\n## A (B)\n\n* D (G)\n* C (E, F)\n",
292+
];
293+
294+
foreach ($testValues as $key => $value) {
295+
$this->setPrivateProperty($changeLog, 'commitData', $value);
296+
$changeLog->build();
297+
$this->assertEquals($expectedValues[$key], $changeLog->get());
298+
}
299+
}
300+
266301
public function testProcessCommitData()
267302
{
268303
$changeLog = new ReflectionClass('DigiLive\GitChangeLog\GitChangeLog');

0 commit comments

Comments
 (0)