Skip to content

Commit

Permalink
Extract TryGetSemanticVersion method into extensions class
Browse files Browse the repository at this point in the history
  • Loading branch information
HHobeck committed Apr 1, 2023
1 parent 1ed02ce commit ec14032
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,8 @@ public void CanTakeVersionFromNameOfReleaseBranch(string branchName, string expe
baseVersion.SemanticVersion.ToString().ShouldBe(expectedBaseVersion);
}

//[TestCase("hotfix-2.0.0")]
[TestCase("origin/hotfix-2.0.0")]
[TestCase("remotes/origin/hotfix-2.0.0")]
//[TestCase("hotfix/2.0.0")]
[TestCase("origin/hotfix/2.0.0")]
[TestCase("remotes/origin/hotfix/2.0.0")]
[TestCase("custom/JIRA-123")]
Expand Down Expand Up @@ -70,6 +68,10 @@ public void ShouldNotTakeVersionFromNameOfNonReleaseBranch(string branchName)
[TestCase("release/3.0.0", "3.0.0")]
[TestCase("support/2.0.0-lts", "2.0.0")]
[TestCase("support-3.0.0-lts", "3.0.0")]
[TestCase("hotfix/2.0.0", "2.0.0")]
[TestCase("hotfix-3.0.0", "3.0.0")]
[TestCase("hotfix/2.0.0-lts", "2.0.0")]
[TestCase("hotfix-3.0.0-lts", "3.0.0")]
public void CanTakeVersionFromNameOfConfiguredReleaseBranch(string branchName, string expectedBaseVersion)
{
using var fixture = new EmptyRepositoryFixture();
Expand Down
34 changes: 0 additions & 34 deletions src/GitVersion.Core/Git/ReferenceName.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
using System.Text.RegularExpressions;
using GitVersion.Extensions;
using GitVersion.Helpers;

Expand Down Expand Up @@ -106,39 +104,7 @@ private string RemoveOrigin()
return Friendly;
}

private char GetBranchSeparator() => WithoutOrigin.Contains('/') || !WithoutOrigin.Contains('-') ? '/' : '-';

private static bool IsPrefixedBy(string input, string prefix) => input.StartsWith(prefix, StringComparison.Ordinal);

private static bool IsPrefixedBy(string input, string[] prefixes) => prefixes.Any(prefix => IsPrefixedBy(input, prefix));

public bool TryGetSemanticVersion([NotNullWhen(true)] out (SemanticVersion Value, string? Name) result,
Regex versionPatternRegex, string? labelPrefix, SemanticVersionFormat format)
{
result = default;

Contract.Assume(versionPatternRegex.ToString().StartsWith("^"));

int length = 0;
foreach (var branchPart in WithoutOrigin.Split(GetBranchSeparator()))
{
if (branchPart.IsNullOrEmpty()) return false;

var match = versionPatternRegex.NotNull().Match(branchPart);
if (match.Success)
{
var versionPart = match.Groups["version"].Value;
if (SemanticVersion.TryParse(versionPart, labelPrefix, out var semanticVersion, format))
{
length += versionPart.Length;
var name = WithoutOrigin[length..].Trim('-');
result = new(semanticVersion, name.IsEmpty() ? null : name);
return true;
}
}
length += branchPart.Length + 1;
}

return false;
}
}
3 changes: 2 additions & 1 deletion src/GitVersion.Core/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ GitVersion.OutputVariables.VersionVariablesJsonModel.WeightedPreReleaseNumber.ge
GitVersion.OutputVariables.VersionVariablesJsonModel.WeightedPreReleaseNumber.set -> void
GitVersion.OutputVariables.VersionVariablesJsonStringConverter
GitVersion.OutputVariables.VersionVariablesJsonStringConverter.VersionVariablesJsonStringConverter() -> void
GitVersion.ReferenceName.TryGetSemanticVersion(out (GitVersion.SemanticVersion! Value, string? Name) result, System.Text.RegularExpressions.Regex! versionPatternRegex, string? labelPrefix, GitVersion.SemanticVersionFormat format) -> bool
GitVersion.ReferenceNameExtensions
GitVersion.RefSpecDirection
GitVersion.RefSpecDirection.Fetch = 0 -> GitVersion.RefSpecDirection
GitVersion.RefSpecDirection.Push = 1 -> GitVersion.RefSpecDirection
Expand Down Expand Up @@ -887,6 +887,7 @@ static GitVersion.OutputVariables.VersionVariablesHelper.ToJsonString(this GitVe
static GitVersion.ReferenceName.FromBranchName(string! branchName) -> GitVersion.ReferenceName!
static GitVersion.ReferenceName.Parse(string! canonicalName) -> GitVersion.ReferenceName!
static GitVersion.ReferenceName.TryParse(out GitVersion.ReferenceName? value, string! canonicalName) -> bool
static GitVersion.ReferenceNameExtensions.TryGetSemanticVersion(this GitVersion.ReferenceName! source, out (GitVersion.SemanticVersion! Value, string? Name) result, System.Text.RegularExpressions.Regex! versionPatternRegex, string? labelPrefix, GitVersion.SemanticVersionFormat format) -> bool
static GitVersion.SemanticVersion.Parse(string! version, string? tagPrefixRegex, GitVersion.SemanticVersionFormat versionFormat = GitVersion.SemanticVersionFormat.Strict) -> GitVersion.SemanticVersion!
static GitVersion.SemanticVersion.TryParse(string! version, string? tagPrefixRegex, out GitVersion.SemanticVersion? semanticVersion, GitVersion.SemanticVersionFormat format = GitVersion.SemanticVersionFormat.Strict) -> bool
static GitVersion.SemanticVersion.operator !=(GitVersion.SemanticVersion? v1, GitVersion.SemanticVersion? v2) -> bool
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
using System.Text.RegularExpressions;
using GitVersion.Extensions;

namespace GitVersion;

public static class ReferenceNameExtensions
{
public static bool TryGetSemanticVersion(this ReferenceName source,
[NotNullWhen(true)] out (SemanticVersion Value, string? Name) result,
Regex versionPatternRegex, string? labelPrefix, SemanticVersionFormat format)
{
source.NotNull();

result = default;

Contract.Assume(versionPatternRegex.ToString().StartsWith("^"));

int length = 0;
foreach (var branchPart in source.WithoutOrigin.Split(GetBranchSeparator(source)))
{
if (branchPart.IsNullOrEmpty()) return false;

var match = versionPatternRegex.NotNull().Match(branchPart);
if (match.Success)
{
var versionPart = match.Groups["version"].Value;
if (SemanticVersion.TryParse(versionPart, labelPrefix, out var semanticVersion, format))
{
length += versionPart.Length;
var name = source.WithoutOrigin[length..].Trim('-');
result = new(semanticVersion, name.IsEmpty() ? null : name);
return true;
}
}
length += branchPart.Length + 1;
}

return false;
}

private static char GetBranchSeparator(ReferenceName source)
=> source.WithoutOrigin.Contains('/') || !source.WithoutOrigin.Contains('-') ? '/' : '-';
}

0 comments on commit ec14032

Please sign in to comment.