From a5d4e3cdb21e540e55ac920857f026d28b7afc0c Mon Sep 17 00:00:00 2001
From: core23 <mail@core23.de>
Date: Wed, 5 Jun 2024 17:48:42 +0200
Subject: [PATCH 1/5] Fix version regex in formatter

---
 src/Changelog/Formatter/AddTopFormatter.php      | 2 +-
 src/Changelog/Formatter/PrefixGroupFormatter.php | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/Changelog/Formatter/AddTopFormatter.php b/src/Changelog/Formatter/AddTopFormatter.php
index 7aab5cc..c74114e 100644
--- a/src/Changelog/Formatter/AddTopFormatter.php
+++ b/src/Changelog/Formatter/AddTopFormatter.php
@@ -69,6 +69,6 @@ public function updateExistingLines(
 
     public function getLastVersionRegex(): string
     {
-        return '#.*#';
+        return '#(\d+\.\d+\.\d+.*)#';
     }
 }
diff --git a/src/Changelog/Formatter/PrefixGroupFormatter.php b/src/Changelog/Formatter/PrefixGroupFormatter.php
index 3af142b..f458c44 100644
--- a/src/Changelog/Formatter/PrefixGroupFormatter.php
+++ b/src/Changelog/Formatter/PrefixGroupFormatter.php
@@ -102,7 +102,7 @@ public function updateExistingLines(
 
     public function getLastVersionRegex(): string
     {
-        return '#.*#';
+        return '/\[(\d+\.\d+\.\d+.*)\]/';
     }
 
     private function getFormattedDate(): string

From 24d63ee497a548e57168384d3871a42f4c7fb68f Mon Sep 17 00:00:00 2001
From: core23 <mail@core23.de>
Date: Wed, 5 Jun 2024 17:49:40 +0200
Subject: [PATCH 2/5] Prevent registering same interaction request

---
 src/Interaction/InteractionCollector.php | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/Interaction/InteractionCollector.php b/src/Interaction/InteractionCollector.php
index f971bd4..2671784 100644
--- a/src/Interaction/InteractionCollector.php
+++ b/src/Interaction/InteractionCollector.php
@@ -28,7 +28,7 @@ public function registerRequest(InteractionRequest $request): void
         $name = $request->getName();
 
         if ($this->hasRequest($name)) {
-            throw new RelazyException(sprintf('Request [%s] already registered', $name));
+            return;
         }
 
         $this->requests[$name] = $request;

From 99f7186d8b5dafe67707483bbbb5975a102373fa Mon Sep 17 00:00:00 2001
From: core23 <mail@core23.de>
Date: Wed, 5 Jun 2024 18:08:53 +0200
Subject: [PATCH 3/5] Add support for versioned branches

---
 src/Version/Persister/TagPersister.php | 36 +++++++++++++++++++++++---
 1 file changed, 33 insertions(+), 3 deletions(-)

diff --git a/src/Version/Persister/TagPersister.php b/src/Version/Persister/TagPersister.php
index 57bd00c..17353db 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,14 @@ public function getCurrentVersion(Context $context): string
 
         usort($versions, [$context->versionGenerator, 'compareVersions']);
 
+        if ($this->versionedBranch) {
+            $branchVersions = $this->getPrefixedVersions($context, $versions);
+
+            if ([] !== $branchVersions) {
+                $versions = $branchVersions;
+            }
+        }
+
         return array_pop($versions);
     }
 
@@ -135,4 +146,23 @@ private function generatePrefix(string $userTag, Context $context): string
 
         return $userTag;
     }
+
+    /**
+     * @param string[] $versions
+     *
+     * @return string[]
+     */
+    private function getPrefixedVersions(Context $context, array $versions): array
+    {
+        $currentBranch = $context->getVersionControl()->getCurrentBranch();
+        $branchPrefix  = preg_replace('/^(\d+(?:\.\d)?(?:\.\d)?\.?).*/', '$1', $currentBranch) ?? '';
+
+        if ('' === $currentBranch) {
+            return [];
+        }
+
+        return array_filter($versions, static function ($version) use ($branchPrefix) {
+            return str_starts_with($version, $branchPrefix);
+        });
+    }
 }

From ab93da8bd7a4a584852b23ebb6073709fc3f9d6d Mon Sep 17 00:00:00 2001
From: core23 <mail@core23.de>
Date: Wed, 5 Jun 2024 18:12:18 +0200
Subject: [PATCH 4/5] Fix releasing first version

---
 src/Command/ReleaseCommand.php | 4 ----
 1 file changed, 4 deletions(-)

diff --git a/src/Command/ReleaseCommand.php b/src/Command/ReleaseCommand.php
index da6c588..7f4132c 100644
--- a/src/Command/ReleaseCommand.php
+++ b/src/Command/ReleaseCommand.php
@@ -124,10 +124,6 @@ protected function getCurrentVersion(Context $context): string
         try {
             $currentVersion = $context->versionPersister->getCurrentVersion($context);
         } catch (NoReleaseFoundException $e) {
-            if (false === $this->informationCollector->getValue(self::CONFIRM_FIRST)) {
-                throw $e;
-            }
-
             $currentVersion = $context->getInitialVersion();
         }
 

From 9f893cc5e297c4f1a12dfebcb64fce3dacffc408 Mon Sep 17 00:00:00 2001
From: core23 <mail@core23.de>
Date: Wed, 5 Jun 2024 18:18:09 +0200
Subject: [PATCH 5/5] Internal refactoring

---
 src/Version/Persister/TagPersister.php | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/src/Version/Persister/TagPersister.php b/src/Version/Persister/TagPersister.php
index 17353db..983e8ce 100644
--- a/src/Version/Persister/TagPersister.php
+++ b/src/Version/Persister/TagPersister.php
@@ -48,12 +48,6 @@ public function getCurrentVersion(Context $context): string
         // Extract versions from tags and sort them
         $versions = $this->getVersionFromTags($tags, $context);
 
-        if ([] === $versions) {
-            throw new NoReleaseFoundException('No versions found in tag list');
-        }
-
-        usort($versions, [$context->versionGenerator, 'compareVersions']);
-
         if ($this->versionedBranch) {
             $branchVersions = $this->getPrefixedVersions($context, $versions);
 
@@ -62,6 +56,8 @@ public function getCurrentVersion(Context $context): string
             }
         }
 
+        \assert([] !== $versions);
+
         return array_pop($versions);
     }
 
@@ -102,10 +98,17 @@ private function getVersionFromTag(string $tagName, Context $context): string
     private function getVersionFromTags(array $tags, Context $context): array
     {
         $versions = [];
+
         foreach ($tags as $tag) {
             $versions[] = $this->getVersionFromTag($tag, $context);
         }
 
+        if ([] === $versions) {
+            throw new NoReleaseFoundException('No versions found in tag list');
+        }
+
+        usort($versions, [$context->versionGenerator, 'compareVersions']);
+
         return $versions;
     }