From 60274c9bc220d0285ad535b14dfc630548a701cf Mon Sep 17 00:00:00 2001 From: Marco Cesarato <36447518+marcocesarato@users.noreply.github.com> Date: Thu, 27 Oct 2022 19:42:05 +0000 Subject: [PATCH] fix: prefix stripping and merged tags only Ref: #47 --- src/Changelog.php | 1 + src/Git/Repository.php | 18 +++++++++++++----- src/Helper/SemanticVersion.php | 9 ++++++++- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/Changelog.php b/src/Changelog.php index 95a2e23..a00a5b5 100644 --- a/src/Changelog.php +++ b/src/Changelog.php @@ -173,6 +173,7 @@ public function generate(string $root, InputInterface $input, SymfonyStyle $outp $autoBump = false; } $newVersion = preg_replace('#^v#i', '', $newVersion); + $newVersion = preg_replace('/^' . preg_quote($tagPrefix, '/') . '/', '', $newVersion); $options = []; // Git retrieve options per version diff --git a/src/Git/Repository.php b/src/Git/Repository.php index 84dc65a..32d20b2 100644 --- a/src/Git/Repository.php +++ b/src/Git/Repository.php @@ -57,18 +57,26 @@ public static function getLastCommit(): string */ public static function getLastTag($prefix = '', $merged = false): string { - $tags = self::run("git for-each-ref --sort=-v:refname --format='%(refname:strip=2)" . self::$delimiter . "' {$merged}"); + $mergedArg = ($merged) ? '--merged' : ''; + $tags = self::run("git for-each-ref --sort=-v:refname --format='%(refname:strip=2)" . self::$delimiter . "' {$mergedArg}"); $tagsArray = explode(self::$delimiter . "\n", $tags); $prefixQuote = preg_quote($prefix); $tagsFound = preg_grep('/^' . $prefixQuote . '[^-]*$/', $tagsArray); - $lastTag = $prefix . '0.0.0'; + $lastTag = '0.0.0'; + foreach ($tagsFound as $value) { + $value = preg_replace('/^' . preg_quote($prefix, '/') . '/', '', $value); + if (SemanticVersion::validate($value)) { + $lastTag = $value; + break; + } + } + if (count($tagsFound) > 0) { foreach ($tagsFound as $found) { - $foundStrip = str_replace($prefix, '', $found); - $semver = new SemanticVersion($foundStrip); - if ($semver->getVersionCode() !== '0.0.0') { + $found = preg_replace('/^' . preg_quote($prefix, '/') . '/', '', $value); + if (SemanticVersion::validate($found)) { $lastTag = $found; break; } diff --git a/src/Helper/SemanticVersion.php b/src/Helper/SemanticVersion.php index 9ce0284..8425014 100644 --- a/src/Helper/SemanticVersion.php +++ b/src/Helper/SemanticVersion.php @@ -70,7 +70,7 @@ public function bump(string $release): string $newVersion = [0, 0, 0]; - if (!preg_match('/^' . self::PATTERN . '$/', $version)) { + if (!self::validate($version)) { return $version; } @@ -136,4 +136,11 @@ public function setVersion(string $version): SemanticVersion return $this; } + + public static function validate($version): bool + { + $version = preg_replace('#^v#i', '', $version); + + return preg_match('/^' . self::PATTERN . '$/', $version); + } }