Skip to content

Commit

Permalink
Add support for versioned branches
Browse files Browse the repository at this point in the history
  • Loading branch information
core23 committed Jun 5, 2024
1 parent 24d63ee commit 99f7186
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions src/Version/Persister/TagPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}

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

0 comments on commit 99f7186

Please sign in to comment.