Skip to content

Commit

Permalink
move TryGetSemanticVersion out of ReferenceName
Browse files Browse the repository at this point in the history
  • Loading branch information
arturcic committed Sep 24, 2024
1 parent 7730850 commit bb6ab10
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 66 deletions.
1 change: 1 addition & 0 deletions new-cli/GitVersion.Common/GitVersion.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<Compile Include="..\..\src\GitVersion.Core\Core\Abstractions\IFileSystem.cs" Link="Infrastructure\%(Filename)%(Extension)" />
<Compile Include="..\..\src\GitVersion.Core\Core\Exceptions\WarningException.cs" Link="Exceptions\%(Filename)%(Extension)"/>
<Compile Include="..\..\src\GitVersion.Core\Core\RegexPatterns.cs" Link="%(Filename)%(Extension)" />
<Compile Include="..\..\src\GitVersion.Core\Extensions\DictionaryExtensions.cs" Link="%(Filename)%(Extension)" />
<Compile Include="..\..\src\GitVersion.Core\Extensions\StringExtensions.cs" Link="Extensions\StringExtensions.cs" />
<Compile Include="..\..\src\GitVersion.Core\Extensions\CommonExtensions.cs" Link="Extensions\CommonExtensions.cs" />
<Compile Include="..\..\src\GitVersion.Core\Helpers\*.cs" Link="Helpers\%(Filename)%(Extension)" />
Expand Down
39 changes: 35 additions & 4 deletions src/GitVersion.Core/Configuration/ReferenceNameExtensions.cs
Original file line number Diff line number Diff line change
@@ -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);
}
81 changes: 29 additions & 52 deletions src/GitVersion.Core/Git/ReferenceName.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Diagnostics.CodeAnalysis;
using System.Text.RegularExpressions;
using GitVersion.Extensions;
using GitVersion.Helpers;

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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));
Expand Down
6 changes: 1 addition & 5 deletions src/GitVersion.Core/MergeMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
3 changes: 0 additions & 3 deletions src/GitVersion.Core/PublicAPI.Shipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<T>(this T? value, string! name = "") -> T!
Expand Down Expand Up @@ -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>(T value, System.Action! disposer) -> GitVersion.Helpers.IDisposable<T>!
static GitVersion.Helpers.EncodingHelper.DetectEncoding(string? filename) -> System.Text.Encoding?
Expand Down
1 change: 1 addition & 0 deletions src/GitVersion.Core/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -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<string!, System.Text.RegularExpressions.Regex!>! dict, string! pattern) -> System.Text.RegularExpressions.Regex!
static GitVersion.Extensions.DictionaryExtensions.GetOrAdd<TKey, TValue>(this System.Collections.Generic.Dictionary<TKey, TValue>! dict, TKey key, System.Func<TValue>! getValue) -> TValue
static GitVersion.Extensions.StringExtensions.RegexReplace(this string! input, string! pattern, string! replace) -> string!
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit bb6ab10

Please sign in to comment.