From bb6ab102a397a360bb4ac7255246215175c4912a Mon Sep 17 00:00:00 2001 From: Artur Stolear Date: Tue, 24 Sep 2024 07:09:08 +0200 Subject: [PATCH] move TryGetSemanticVersion out of ReferenceName --- .../GitVersion.Common.csproj | 1 + .../Configuration/ReferenceNameExtensions.cs | 39 ++++++++- src/GitVersion.Core/Git/ReferenceName.cs | 81 +++++++------------ src/GitVersion.Core/MergeMessage.cs | 6 +- src/GitVersion.Core/PublicAPI.Shipped.txt | 3 - src/GitVersion.Core/PublicAPI.Unshipped.txt | 1 + .../VersionInBranchNameVersionStrategy.cs | 7 +- 7 files changed, 72 insertions(+), 66 deletions(-) diff --git a/new-cli/GitVersion.Common/GitVersion.Common.csproj b/new-cli/GitVersion.Common/GitVersion.Common.csproj index 96f23e8568..f224f9e121 100644 --- a/new-cli/GitVersion.Common/GitVersion.Common.csproj +++ b/new-cli/GitVersion.Common/GitVersion.Common.csproj @@ -7,6 +7,7 @@ + diff --git a/src/GitVersion.Core/Configuration/ReferenceNameExtensions.cs b/src/GitVersion.Core/Configuration/ReferenceNameExtensions.cs index dd05cab0f2..6cc8473010 100644 --- a/src/GitVersion.Core/Configuration/ReferenceNameExtensions.cs +++ b/src/GitVersion.Core/Configuration/ReferenceNameExtensions.cs @@ -1,14 +1,45 @@ +using System.Text.RegularExpressions; +using GitVersion.Extensions; using GitVersion.Git; namespace GitVersion.Configuration; public static class ReferenceNameExtensions { - public static bool TryGetSemanticVersion( - this ReferenceName source, out (SemanticVersion Value, string? Name) result, IGitVersionConfiguration configuration) - => source.TryGetSemanticVersion(out result, configuration.VersionInBranchRegex, configuration.TagPrefix, configuration.SemanticVersionFormat); + public static bool TryGetSemanticVersion(this ReferenceName referenceName, out (SemanticVersion Value, string? Name) result, + Regex versionPatternRegex, + string? tagPrefix, + SemanticVersionFormat format) + { + result = default; + + int length = 0; + foreach (var branchPart in referenceName.WithoutOrigin.Split(GetBranchSeparator())) + { + if (string.IsNullOrEmpty(branchPart)) return false; + + var match = versionPatternRegex.NotNull().Match(branchPart); + if (match.Success) + { + var versionPart = match.Groups["version"].Value; + if (SemanticVersion.TryParse(versionPart, tagPrefix, out var semanticVersion, format)) + { + length += versionPart.Length; + var name = referenceName.WithoutOrigin[length..].Trim('-'); + result = new(semanticVersion, name.Length == 0 ? null : name); + return true; + } + } + + length += branchPart.Length + 1; + } + + return false; + + char GetBranchSeparator() => referenceName.WithoutOrigin.Contains('/') || !referenceName.WithoutOrigin.Contains('-') ? '/' : '-'; + } public static bool TryGetSemanticVersion( - this ReferenceName source, out (SemanticVersion Value, string? Name) result, EffectiveConfiguration configuration) + this ReferenceName source, out (SemanticVersion Value, string? Name) result, EffectiveConfiguration configuration) => source.TryGetSemanticVersion(out result, configuration.VersionInBranchRegex, configuration.TagPrefix, configuration.SemanticVersionFormat); } diff --git a/src/GitVersion.Core/Git/ReferenceName.cs b/src/GitVersion.Core/Git/ReferenceName.cs index b256c8e490..5e38c3cc60 100644 --- a/src/GitVersion.Core/Git/ReferenceName.cs +++ b/src/GitVersion.Core/Git/ReferenceName.cs @@ -1,5 +1,4 @@ using System.Diagnostics.CodeAnalysis; -using System.Text.RegularExpressions; using GitVersion.Extensions; using GitVersion.Helpers; @@ -42,39 +41,33 @@ public static ReferenceName Parse(string canonicalName) throw new ArgumentException($"The {nameof(canonicalName)} is not a Canonical name"); } - public static bool TryParse([NotNullWhen(true)] out ReferenceName? value, string canonicalName) - { - value = null; - - if (IsPrefixedBy(canonicalName, LocalBranchPrefix) - || IsPrefixedBy(canonicalName, RemoteTrackingBranchPrefix) - || IsPrefixedBy(canonicalName, TagPrefix) - || IsPrefixedBy(canonicalName, PullRequestPrefixes)) - { - value = new(canonicalName); - } - - return value is not null; - } - public static ReferenceName FromBranchName(string branchName) - { - if (TryParse(out ReferenceName? value, branchName)) return value; - return Parse(LocalBranchPrefix + branchName); - } + => TryParse(out ReferenceName? value, branchName) + ? value + : Parse(LocalBranchPrefix + branchName); public string Canonical { get; } + public string Friendly { get; } + public string WithoutOrigin { get; } + public bool IsLocalBranch { get; } + public bool IsRemoteBranch { get; } + public bool IsTag { get; } + public bool IsPullRequest { get; } public bool Equals(ReferenceName? other) => equalityHelper.Equals(this, other); + public int CompareTo(ReferenceName? other) => comparerHelper.Compare(this, other); + public override bool Equals(object? obj) => Equals(obj as ReferenceName); + public override int GetHashCode() => equalityHelper.GetHashCode(this); + public override string ToString() => Friendly; public static bool operator ==(ReferenceName? left, ReferenceName? right) @@ -86,38 +79,6 @@ public static ReferenceName FromBranchName(string branchName) public static bool operator !=(ReferenceName? left, ReferenceName? right) => !(left == right); - public bool TryGetSemanticVersion(out (SemanticVersion Value, string? Name) result, - Regex versionPatternRegex, - string? tagPrefix, - SemanticVersionFormat format) - { - result = default; - - int length = 0; - foreach (var branchPart in WithoutOrigin.Split(GetBranchSeparator())) - { - if (string.IsNullOrEmpty(branchPart)) return false; - - var match = versionPatternRegex.NotNull().Match(branchPart); - if (match.Success) - { - var versionPart = match.Groups["version"].Value; - if (SemanticVersion.TryParse(versionPart, tagPrefix, out var semanticVersion, format)) - { - length += versionPart.Length; - var name = WithoutOrigin[length..].Trim('-'); - result = new(semanticVersion, name.Length == 0 ? null : name); - return true; - } - } - length += branchPart.Length + 1; - } - - return false; - } - - private char GetBranchSeparator() => WithoutOrigin.Contains('/') || !WithoutOrigin.Contains('-') ? '/' : '-'; - public bool EquivalentTo(string? name) => Canonical.Equals(name, StringComparison.OrdinalIgnoreCase) || Friendly.Equals(name, StringComparison.OrdinalIgnoreCase) @@ -143,9 +104,25 @@ private string RemoveOrigin() { return Friendly[OriginPrefix.Length..]; } + return Friendly; } + private static bool TryParse([NotNullWhen(true)] out ReferenceName? value, string canonicalName) + { + value = null; + + if (IsPrefixedBy(canonicalName, LocalBranchPrefix) + || IsPrefixedBy(canonicalName, RemoteTrackingBranchPrefix) + || IsPrefixedBy(canonicalName, TagPrefix) + || IsPrefixedBy(canonicalName, PullRequestPrefixes)) + { + value = new(canonicalName); + } + + return value is not null; + } + 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)); diff --git a/src/GitVersion.Core/MergeMessage.cs b/src/GitVersion.Core/MergeMessage.cs index 0fbf7b019c..e5c601c3b7 100644 --- a/src/GitVersion.Core/MergeMessage.cs +++ b/src/GitVersion.Core/MergeMessage.cs @@ -70,11 +70,7 @@ public MergeMessage(string mergeMessage, IGitVersionConfiguration configuration) public SemanticVersion? Version { get; } private SemanticVersion? ParseVersion(Regex versionInBranchRegex, string? tagPrefix, SemanticVersionFormat format) - { - if (MergedBranch?.TryGetSemanticVersion(out var result, versionInBranchRegex, tagPrefix, format) == true) - return result.Value; - return null; - } + => MergedBranch?.TryGetSemanticVersion(out var result, versionInBranchRegex, tagPrefix, format) == true ? result.Value : null; private class MergeMessageFormat(string name, Regex pattern) { diff --git a/src/GitVersion.Core/PublicAPI.Shipped.txt b/src/GitVersion.Core/PublicAPI.Shipped.txt index d381ce7ff6..807aaa2d87 100644 --- a/src/GitVersion.Core/PublicAPI.Shipped.txt +++ b/src/GitVersion.Core/PublicAPI.Shipped.txt @@ -287,7 +287,6 @@ GitVersion.Git.ReferenceName.IsPullRequest.get -> bool GitVersion.Git.ReferenceName.IsRemoteBranch.get -> bool GitVersion.Git.ReferenceName.IsTag.get -> bool GitVersion.Git.ReferenceName.ReferenceName(string! canonical) -> void -GitVersion.Git.ReferenceName.TryGetSemanticVersion(out (GitVersion.SemanticVersion! Value, string? Name) result, System.Text.RegularExpressions.Regex! versionPatternRegex, string? tagPrefix, GitVersion.SemanticVersionFormat format) -> bool GitVersion.Git.ReferenceName.WithoutOrigin.get -> string! GitVersion.Git.RefSpecDirection GitVersion.Git.RefSpecDirection.Fetch = 0 -> GitVersion.Git.RefSpecDirection @@ -741,7 +740,6 @@ override GitVersion.VersionCalculation.NextVersion.Equals(object? other) -> bool override GitVersion.VersionCalculation.NextVersion.GetHashCode() -> int override GitVersion.VersionCalculation.NextVersion.ToString() -> string! static GitVersion.Configuration.ReferenceNameExtensions.TryGetSemanticVersion(this GitVersion.Git.ReferenceName! source, out (GitVersion.SemanticVersion! Value, string? Name) result, GitVersion.Configuration.EffectiveConfiguration! configuration) -> bool -static GitVersion.Configuration.ReferenceNameExtensions.TryGetSemanticVersion(this GitVersion.Git.ReferenceName! source, out (GitVersion.SemanticVersion! Value, string? Name) result, GitVersion.Configuration.IGitVersionConfiguration! configuration) -> bool static GitVersion.Extensions.AssemblyVersionsGeneratorExtensions.GetAssemblyFileVersion(this GitVersion.SemanticVersion! sv, GitVersion.Configuration.AssemblyFileVersioningScheme scheme) -> string? static GitVersion.Extensions.AssemblyVersionsGeneratorExtensions.GetAssemblyVersion(this GitVersion.SemanticVersion! sv, GitVersion.Configuration.AssemblyVersioningScheme scheme) -> string? static GitVersion.Extensions.CommonExtensions.NotNull(this T? value, string! name = "") -> T! @@ -770,7 +768,6 @@ static GitVersion.Git.ReferenceName.FromBranchName(string! branchName) -> GitVer static GitVersion.Git.ReferenceName.operator !=(GitVersion.Git.ReferenceName? left, GitVersion.Git.ReferenceName? right) -> bool static GitVersion.Git.ReferenceName.operator ==(GitVersion.Git.ReferenceName? left, GitVersion.Git.ReferenceName? right) -> bool static GitVersion.Git.ReferenceName.Parse(string! canonicalName) -> GitVersion.Git.ReferenceName! -static GitVersion.Git.ReferenceName.TryParse(out GitVersion.Git.ReferenceName? value, string! canonicalName) -> bool static GitVersion.Helpers.Disposable.Create(System.Action! disposer) -> System.IDisposable! static GitVersion.Helpers.Disposable.Create(T value, System.Action! disposer) -> GitVersion.Helpers.IDisposable! static GitVersion.Helpers.EncodingHelper.DetectEncoding(string? filename) -> System.Text.Encoding? diff --git a/src/GitVersion.Core/PublicAPI.Unshipped.txt b/src/GitVersion.Core/PublicAPI.Unshipped.txt index a24b4020b3..00f7a767de 100644 --- a/src/GitVersion.Core/PublicAPI.Unshipped.txt +++ b/src/GitVersion.Core/PublicAPI.Unshipped.txt @@ -1,4 +1,5 @@ #nullable enable +static GitVersion.Configuration.ReferenceNameExtensions.TryGetSemanticVersion(this GitVersion.Git.ReferenceName! referenceName, out (GitVersion.SemanticVersion! Value, string? Name) result, System.Text.RegularExpressions.Regex! versionPatternRegex, string? tagPrefix, GitVersion.SemanticVersionFormat format) -> bool static GitVersion.Extensions.DictionaryExtensions.GetOrAdd(this System.Collections.Concurrent.ConcurrentDictionary! dict, string! pattern) -> System.Text.RegularExpressions.Regex! static GitVersion.Extensions.DictionaryExtensions.GetOrAdd(this System.Collections.Generic.Dictionary! dict, TKey key, System.Func! getValue) -> TValue static GitVersion.Extensions.StringExtensions.RegexReplace(this string! input, string! pattern, string! replace) -> string! diff --git a/src/GitVersion.Core/VersionCalculation/VersionSearchStrategies/VersionInBranchNameVersionStrategy.cs b/src/GitVersion.Core/VersionCalculation/VersionSearchStrategies/VersionInBranchNameVersionStrategy.cs index 8c6af0f781..0585fc7a3e 100644 --- a/src/GitVersion.Core/VersionCalculation/VersionSearchStrategies/VersionInBranchNameVersionStrategy.cs +++ b/src/GitVersion.Core/VersionCalculation/VersionSearchStrategies/VersionInBranchNameVersionStrategy.cs @@ -35,8 +35,11 @@ public bool TryGetBaseVersion(EffectiveBranchConfiguration configuration, [NotNu foreach (var branch in new[] { Context.CurrentBranch, configuration.Branch }) { - if (branch.Name.TryGetSemanticVersion(out var result, configuration.Value.VersionInBranchRegex, - configuration.Value.TagPrefix, configuration.Value.SemanticVersionFormat)) + var versionInBranchRegex = configuration.Value.VersionInBranchRegex; + var tagPrefix = configuration.Value.TagPrefix; + var semanticVersionFormat = configuration.Value.SemanticVersionFormat; + + if (branch.Name.TryGetSemanticVersion(out var result, versionInBranchRegex, tagPrefix, semanticVersionFormat)) { string? branchNameOverride = null; if (!result.Name.IsNullOrEmpty() && (Context.CurrentBranch.Name.Equals(branch.Name)