From 3cd14bb3d63f30dbc6b8fb0c4650f81aa694f12a Mon Sep 17 00:00:00 2001 From: core23 Date: Wed, 5 Jun 2024 18:08:53 +0200 Subject: [PATCH] Add support for versioned branches --- src/Version/Persister/TagPersister.php | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/Version/Persister/TagPersister.php b/src/Version/Persister/TagPersister.php index 57bd00c..f13ea73 100644 --- a/src/Version/Persister/TagPersister.php +++ b/src/Version/Persister/TagPersister.php @@ -22,10 +22,13 @@ final class TagPersister implements Persister private readonly string $tagPrefix; - public function __construct(?string $tagPattern = null, ?string $tagPrefix = null) + private readonly bool $versionedBranch; + + public function __construct(?string $tagPattern = null, ?string $tagPrefix = null, bool $versionedBranch = true) { - $this->tagPrefix = $tagPrefix ?? ''; - $this->versionRegex = $tagPattern; + $this->tagPrefix = $tagPrefix ?? ''; + $this->versionRegex = $tagPattern; + $this->versionedBranch = $versionedBranch; } public function getCurrentVersion(Context $context): string @@ -51,6 +54,19 @@ public function getCurrentVersion(Context $context): string usort($versions, [$context->versionGenerator, 'compareVersions']); + if ($this->versionedBranch) { + $currentBranch = $context->getVersionControl()->getCurrentBranch(); + $branchPrefix = preg_replace('/^(\d+(?:\.\d)?(?:\.\d)?\.?).*/', '$1', $currentBranch); + + $branchVersions = array_filter($versions, static function ($version) use ($branchPrefix) { + return str_starts_with($version, $branchPrefix); + }); + + if ([] !== $branchVersions) { + $versions = $branchVersions; + } + } + return array_pop($versions); }