Skip to content

Commit

Permalink
fix: prefix stripping and merged tags only
Browse files Browse the repository at this point in the history
Ref: #47
  • Loading branch information
marcocesarato committed Oct 27, 2022
1 parent 0ab3e32 commit 60274c9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/Changelog.php
Expand Up @@ -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

Expand Down
18 changes: 13 additions & 5 deletions src/Git/Repository.php
Expand Up @@ -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;
}
Expand Down
9 changes: 8 additions & 1 deletion src/Helper/SemanticVersion.php
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
}
}

0 comments on commit 60274c9

Please sign in to comment.