1
0
Fork 0

Backport some fixes from 2.0, and fix sorting to sort remote branches after local ones, refs #9270

pull/9276/head
Jordi Boggiano 2020-10-08 14:26:04 +02:00
parent 6698e0bafa
commit 791bbc80a4
No known key found for this signature in database
GPG Key ID: 7BBD42C429EC80BC
1 changed files with 19 additions and 4 deletions

View File

@ -244,12 +244,27 @@ class VersionGuesser
$nonFeatureBranches = implode('|', $packageConfig['non-feature-branches']); $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) { foreach ($branches as $candidate) {
$candidateVersion = preg_replace('{^remotes/\S+/}', '', $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 // do not compare against itself or other feature branches
if ($candidate === $branch || !preg_match('{^(' . $nonFeatureBranches . '|master|trunk|default|develop|\d+\..+)$}', $candidateVersion, $match)) { if ($candidate === $branch || !preg_match('{^(' . $nonFeatureBranches . '|master|trunk|default|develop|\d+\..+)$}', $candidateVersion, $match)) {