Skip to content

Commit

Permalink
Sort branches according to their branch-alias if available, and sort …
Browse files Browse the repository at this point in the history
…default branch on top only if non-numeric, fixes #1096
  • Loading branch information
Seldaek committed Jul 2, 2020
1 parent 433e26f commit d313cb8
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/Packagist/WebBundle/Entity/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -947,19 +947,28 @@ public function setReplacementPackage($replacementPackage)

public static function sortVersions($a, $b)
{
// sort default branch first
if ($a->isDefaultBranch()) {
return -1;
$aVersion = $a->getNormalizedVersion();
$bVersion = $b->getNormalizedVersion();

// use branch alias for sorting if one is provided
if (isset($a->getExtra()['branch-alias'][$aVersion])) {
$aVersion = preg_replace('{(.x)?-dev$}', '.9999999-dev', $a->getExtra()['branch-alias'][$aVersion]);
}
if ($b->isDefaultBranch()) {
return 1;
if (isset($b->getExtra()['branch-alias'][$bVersion])) {
$bVersion = preg_replace('{(.x)?-dev$}', '.9999999-dev', $b->getExtra()['branch-alias'][$bVersion]);
}

$aVersion = $a->getNormalizedVersion();
$bVersion = $b->getNormalizedVersion();
$aVersion = preg_replace('{^dev-.*}', '0.0.0-alpha', $aVersion);
$bVersion = preg_replace('{^dev-.*}', '0.0.0-alpha', $bVersion);

// sort default branch first if it is non numeric
if ($aVersion === '0.0.0-alpha' && $a->isDefaultBranch()) {
return -1;
}
if ($bVersion === '0.0.0-alpha' && $b->isDefaultBranch()) {
return 1;
}

// equal versions are sorted by date
if ($aVersion === $bVersion) {
// make sure sort is stable
Expand Down

0 comments on commit d313cb8

Please sign in to comment.