From 791bbc80a4e9192a59a37d1282ff5032c4e52692 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Thu, 8 Oct 2020 14:26:04 +0200 Subject: [PATCH] Backport some fixes from 2.0, and fix sorting to sort remote branches after local ones, refs #9270 --- .../Package/Version/VersionGuesser.php | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/Composer/Package/Version/VersionGuesser.php b/src/Composer/Package/Version/VersionGuesser.php index 072a53dfa..a71c915ad 100644 --- a/src/Composer/Package/Version/VersionGuesser.php +++ b/src/Composer/Package/Version/VersionGuesser.php @@ -244,12 +244,27 @@ class VersionGuesser $nonFeatureBranches = implode('|', $packageConfig['non-feature-branches']); } + // return directly, if branch is configured to be non-feature branch + if (preg_match('{^(' . $nonFeatureBranches . ')$}', $branch) && in_array($branch, $branches, true)) { + return array('version' => $version, 'pretty_version' => $prettyVersion); + } + + // sort local branches first then remote ones + // and sort numeric branches below named ones, to make sure if the branch has the same distance from main and 1.10 and 1.9 for example, main is picked + // and sort using natural sort so that 1.10 will appear before 1.9 + usort($branches, function ($a, $b) { + $aRemote = 0 === strpos($a, 'remotes/'); + $bRemote = 0 === strpos($b, 'remotes/'); + + if ($aRemote !== $bRemote) { + return $aRemote ? 1 : -1; + } + + return strnatcasecmp($b, $a); + }); + foreach ($branches as $candidate) { $candidateVersion = preg_replace('{^remotes/\S+/}', '', $candidate); - // return directly, if branch is configured to be non-feature branch - if ($candidate === $branch && preg_match('{^(' . $nonFeatureBranches . ')$}', $candidateVersion)) { - break; - } // do not compare against itself or other feature branches if ($candidate === $branch || !preg_match('{^(' . $nonFeatureBranches . '|master|trunk|default|develop|\d+\..+)$}', $candidateVersion, $match)) {