From ec14032df01c238ea97acda7886495ef3f16b1b3 Mon Sep 17 00:00:00 2001 From: Hardy Hobeck Date: Sat, 1 Apr 2023 10:17:43 +0200 Subject: [PATCH] Extract TryGetSemanticVersion method into extensions class --- ...ionInBranchNameBaseVersionStrategyTests.cs | 6 ++- src/GitVersion.Core/Git/ReferenceName.cs | 34 -------------- src/GitVersion.Core/PublicAPI.Unshipped.txt | 3 +- .../ReferenceNameExtensions.cs | 45 +++++++++++++++++++ 4 files changed, 51 insertions(+), 37 deletions(-) create mode 100644 src/GitVersion.Core/VersionCalculation/SemanticVersioning/ReferenceNameExtensions.cs diff --git a/src/GitVersion.Core.Tests/VersionCalculation/Strategies/VersionInBranchNameBaseVersionStrategyTests.cs b/src/GitVersion.Core.Tests/VersionCalculation/Strategies/VersionInBranchNameBaseVersionStrategyTests.cs index 4f28c60463..57dbaf78f7 100644 --- a/src/GitVersion.Core.Tests/VersionCalculation/Strategies/VersionInBranchNameBaseVersionStrategyTests.cs +++ b/src/GitVersion.Core.Tests/VersionCalculation/Strategies/VersionInBranchNameBaseVersionStrategyTests.cs @@ -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")] @@ -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(); diff --git a/src/GitVersion.Core/Git/ReferenceName.cs b/src/GitVersion.Core/Git/ReferenceName.cs index fa975c2d53..3f600f6b43 100644 --- a/src/GitVersion.Core/Git/ReferenceName.cs +++ b/src/GitVersion.Core/Git/ReferenceName.cs @@ -1,6 +1,4 @@ using System.Diagnostics.CodeAnalysis; -using System.Diagnostics.Contracts; -using System.Text.RegularExpressions; using GitVersion.Extensions; using GitVersion.Helpers; @@ -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; - } } diff --git a/src/GitVersion.Core/PublicAPI.Unshipped.txt b/src/GitVersion.Core/PublicAPI.Unshipped.txt index 1286de50e6..56c7c56a78 100644 --- a/src/GitVersion.Core/PublicAPI.Unshipped.txt +++ b/src/GitVersion.Core/PublicAPI.Unshipped.txt @@ -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 @@ -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 diff --git a/src/GitVersion.Core/VersionCalculation/SemanticVersioning/ReferenceNameExtensions.cs b/src/GitVersion.Core/VersionCalculation/SemanticVersioning/ReferenceNameExtensions.cs new file mode 100644 index 0000000000..09664f5290 --- /dev/null +++ b/src/GitVersion.Core/VersionCalculation/SemanticVersioning/ReferenceNameExtensions.cs @@ -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('-') ? '/' : '-'; +}