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 3cd14bb
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 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,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);
}

Expand Down

0 comments on commit 3cd14bb

Please sign in to comment.