Skip to content

Commit 6dc2bee

Browse files
committed
Optimize commitData processing
Library: - Subjects without duplicates where processed as a duplicate. - First duplicate subject was altered and then unset. - Conversion of hashes elements to an array was inefficient. PHPUnit tests: - First instance of duplicates is now kept instead of last. Updated Expected result for relevant tests.
1 parent 31d33af commit 6dc2bee

File tree

2 files changed

+27
-29
lines changed

2 files changed

+27
-29
lines changed

src/GitChangeLog.php

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -361,12 +361,12 @@ public function fetchCommitData($force = false): array
361361
$commitData[$previousTag]['subjects'] =
362362
explode(
363363
"\n",
364-
shell_exec("git $gitPath log $tagRange $includeMergeCommits --pretty=format:%s --reverse")
364+
shell_exec("git $gitPath log $tagRange $includeMergeCommits --pretty=format:%s")
365365
);
366366
$commitData[$previousTag]['hashes'] =
367367
explode(
368368
"\n",
369-
shell_exec("git $gitPath log $tagRange $includeMergeCommits --pretty=format:%h --reverse")
369+
shell_exec("git $gitPath log $tagRange $includeMergeCommits --pretty=format:%h")
370370
);
371371
$previousTag = $tag;
372372
}
@@ -393,28 +393,20 @@ public function fetchCommitData($force = false): array
393393
private function processCommitData(): void
394394
{
395395
foreach ($this->commitData as $tag => &$data) {
396-
// Remove duplicates per tag.
397-
foreach ($data['subjects'] as $subjectKey => $subject) {
398-
// Get indexes of current subject.
396+
// Merge duplicate subjects per tag.
397+
foreach ($data['subjects'] as $subjectKey => &$subject) {
398+
// Convert hash element into an array.
399+
$data['hashes'][$subjectKey] = [$data['hashes'][$subjectKey]];
400+
401+
// Get indexes of all other elements with the same subject as the current one.
399402
$duplicates = array_keys($data['subjects'], $subject);
400-
$hashes = [];
403+
array_shift($duplicates);
401404

405+
// Add hashes of duplicate subjects to the current subject and remove this duplicates.
402406
// Subjects and hashes which belong to each other, have the same array key.
403407
foreach ($duplicates as $key => $index) {
404-
// Collect hashes.
405-
if (!is_array($data['hashes'][$index])) {
406-
$hashes[] = $data['hashes'][$index];
407-
} else {
408-
$hashes = $data['hashes'][$index];
409-
}
410-
411-
$data['hashes'][$index] = $hashes;
412-
413-
// Remove all but last duplicate.
414-
end($duplicates);
415-
if ($key !== key($duplicates)) {
416-
unset($data['subjects'][$index], $data['hashes'][$index]);
417-
}
408+
$data['hashes'][$subjectKey][] = $data['hashes'][$index];
409+
unset($data['subjects'][$index], $data['hashes'][$index]);
418410
}
419411

420412
// Remove subjects and hashes without specified labels.

tests/GitChangeLogTest.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ public function testSetFromTag()
232232

233233
public function testBuildAscendingCommitOrder()
234234
{
235-
$changeLog = new GitChangeLog();
235+
$changeLog = new GitChangeLog();
236236
$changeLog->setOptions('tagOrderDesc', false);
237237
$testValues =
238238
[
@@ -266,7 +266,7 @@ public function testBuildAscendingCommitOrder()
266266

267267
public function testBuildDescendingCommitOrder()
268268
{
269-
$changeLog = new GitChangeLog();
269+
$changeLog = new GitChangeLog();
270270
$changeLog->setOptions('commitOrder', 'DESC');
271271
$testValues =
272272
[
@@ -305,20 +305,26 @@ public function testProcessCommitData()
305305
$method->setAccessible(true);
306306

307307
$changeLog = new GitChangeLog();
308-
$changeLog->setLabels('C', 'D');
308+
$changeLog->setLabels('C', 'D', 'F');
309309

310-
$value = ['A' => ['date' => 'B', 'subjects' => ['C', 'D', 'C', 'E'], 'hashes' => ['F', 'G', 'H', 'I']]];
311-
$this->setPrivateProperty($changeLog, 'commitData', $value);
312-
$value = [
310+
$commitData = [
311+
'A' => [
312+
'date' => 'B',
313+
'subjects' => [0 => 'C', 1 => 'D', 2 => 'C', 3 => 'E', 4 => 'F'],
314+
'hashes' => [0 => 'G', 1 => 'H', 2 => 'I', 3 => 'J', 4 => 'K'],
315+
],
316+
];
317+
$this->setPrivateProperty($changeLog, 'commitData', $commitData);
318+
$commitData = [
313319
'A' => [
314320
'date' => 'B',
315-
'subjects' => [1 => 'D', 2 => 'C'],
316-
'hashes' => [1 => ['G'], 2 => ['F', 'H']],
321+
'subjects' => [0 => 'C', 1 => 'D', 4 => 'F'],
322+
'hashes' => [0 => ['G', 'I'], 1 => ['H'], 4 => ['K']],
317323
],
318324
];
319325
$method->invokeArgs($changeLog, []);
320326

321-
$this->assertEquals($value, $this->getPrivateProperty($changeLog, 'commitData'));
327+
$this->assertEquals($commitData, $this->getPrivateProperty($changeLog, 'commitData'));
322328
}
323329

324330
public function testAddLabel()

0 commit comments

Comments
 (0)