Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev updates #15

Merged
merged 5 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Changelog/Formatter/AddTopFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,6 @@ public function updateExistingLines(

public function getLastVersionRegex(): string
{
return '#.*#';
return '#(\d+\.\d+\.\d+.*)#';
}
}
2 changes: 1 addition & 1 deletion src/Changelog/Formatter/PrefixGroupFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function updateExistingLines(

public function getLastVersionRegex(): string
{
return '#.*#';
return '/\[(\d+\.\d+\.\d+.*)\]/';
}

private function getFormattedDate(): string
Expand Down
4 changes: 0 additions & 4 deletions src/Command/ReleaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Interaction/InteractionCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
45 changes: 39 additions & 6 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 @@ -45,11 +48,15 @@ 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');
if ($this->versionedBranch) {
$branchVersions = $this->getPrefixedVersions($context, $versions);

if ([] !== $branchVersions) {
$versions = $branchVersions;
}
}

usort($versions, [$context->versionGenerator, 'compareVersions']);
\assert([] !== $versions);

return array_pop($versions);
}
Expand Down Expand Up @@ -91,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;
}

Expand Down Expand Up @@ -135,4 +149,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);
});
}
}
Loading