Skip to content

Commit 120ce11

Browse files
committed
move TryGetSemanticVersion out of ReferenceName
1 parent 4cca352 commit 120ce11

File tree

7 files changed

+72
-66
lines changed

7 files changed

+72
-66
lines changed

new-cli/GitVersion.Common/GitVersion.Common.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<Compile Include="..\..\src\GitVersion.Core\Core\Abstractions\IFileSystem.cs" Link="Infrastructure\%(Filename)%(Extension)" />
88
<Compile Include="..\..\src\GitVersion.Core\Core\Exceptions\WarningException.cs" Link="Exceptions\%(Filename)%(Extension)"/>
99
<Compile Include="..\..\src\GitVersion.Core\Core\RegexPatterns.cs" Link="%(Filename)%(Extension)" />
10+
<Compile Include="..\..\src\GitVersion.Core\Extensions\DictionaryExtensions.cs" Link="%(Filename)%(Extension)" />
1011
<Compile Include="..\..\src\GitVersion.Core\Extensions\StringExtensions.cs" Link="Extensions\StringExtensions.cs" />
1112
<Compile Include="..\..\src\GitVersion.Core\Extensions\CommonExtensions.cs" Link="Extensions\CommonExtensions.cs" />
1213
<Compile Include="..\..\src\GitVersion.Core\Helpers\*.cs" Link="Helpers\%(Filename)%(Extension)" />
Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,45 @@
1+
using System.Text.RegularExpressions;
2+
using GitVersion.Extensions;
13
using GitVersion.Git;
24

35
namespace GitVersion.Configuration;
46

57
public static class ReferenceNameExtensions
68
{
7-
public static bool TryGetSemanticVersion(
8-
this ReferenceName source, out (SemanticVersion Value, string? Name) result, IGitVersionConfiguration configuration)
9-
=> source.TryGetSemanticVersion(out result, configuration.VersionInBranchRegex, configuration.TagPrefix, configuration.SemanticVersionFormat);
9+
public static bool TryGetSemanticVersion(this ReferenceName referenceName, out (SemanticVersion Value, string? Name) result,
10+
Regex versionPatternRegex,
11+
string? tagPrefix,
12+
SemanticVersionFormat format)
13+
{
14+
result = default;
15+
16+
int length = 0;
17+
foreach (var branchPart in referenceName.WithoutOrigin.Split(GetBranchSeparator()))
18+
{
19+
if (string.IsNullOrEmpty(branchPart)) return false;
20+
21+
var match = versionPatternRegex.NotNull().Match(branchPart);
22+
if (match.Success)
23+
{
24+
var versionPart = match.Groups["version"].Value;
25+
if (SemanticVersion.TryParse(versionPart, tagPrefix, out var semanticVersion, format))
26+
{
27+
length += versionPart.Length;
28+
var name = referenceName.WithoutOrigin[length..].Trim('-');
29+
result = new(semanticVersion, name.Length == 0 ? null : name);
30+
return true;
31+
}
32+
}
33+
34+
length += branchPart.Length + 1;
35+
}
36+
37+
return false;
38+
39+
char GetBranchSeparator() => referenceName.WithoutOrigin.Contains('/') || !referenceName.WithoutOrigin.Contains('-') ? '/' : '-';
40+
}
1041

1142
public static bool TryGetSemanticVersion(
12-
this ReferenceName source, out (SemanticVersion Value, string? Name) result, EffectiveConfiguration configuration)
43+
this ReferenceName source, out (SemanticVersion Value, string? Name) result, EffectiveConfiguration configuration)
1344
=> source.TryGetSemanticVersion(out result, configuration.VersionInBranchRegex, configuration.TagPrefix, configuration.SemanticVersionFormat);
1445
}

src/GitVersion.Core/Git/ReferenceName.cs

Lines changed: 29 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Diagnostics.CodeAnalysis;
2-
using System.Text.RegularExpressions;
32
using GitVersion.Extensions;
43
using GitVersion.Helpers;
54

@@ -42,39 +41,33 @@ public static ReferenceName Parse(string canonicalName)
4241
throw new ArgumentException($"The {nameof(canonicalName)} is not a Canonical name");
4342
}
4443

45-
public static bool TryParse([NotNullWhen(true)] out ReferenceName? value, string canonicalName)
46-
{
47-
value = null;
48-
49-
if (IsPrefixedBy(canonicalName, LocalBranchPrefix)
50-
|| IsPrefixedBy(canonicalName, RemoteTrackingBranchPrefix)
51-
|| IsPrefixedBy(canonicalName, TagPrefix)
52-
|| IsPrefixedBy(canonicalName, PullRequestPrefixes))
53-
{
54-
value = new(canonicalName);
55-
}
56-
57-
return value is not null;
58-
}
59-
6044
public static ReferenceName FromBranchName(string branchName)
61-
{
62-
if (TryParse(out ReferenceName? value, branchName)) return value;
63-
return Parse(LocalBranchPrefix + branchName);
64-
}
45+
=> TryParse(out ReferenceName? value, branchName)
46+
? value
47+
: Parse(LocalBranchPrefix + branchName);
6548

6649
public string Canonical { get; }
50+
6751
public string Friendly { get; }
52+
6853
public string WithoutOrigin { get; }
54+
6955
public bool IsLocalBranch { get; }
56+
7057
public bool IsRemoteBranch { get; }
58+
7159
public bool IsTag { get; }
60+
7261
public bool IsPullRequest { get; }
7362

7463
public bool Equals(ReferenceName? other) => equalityHelper.Equals(this, other);
64+
7565
public int CompareTo(ReferenceName? other) => comparerHelper.Compare(this, other);
66+
7667
public override bool Equals(object? obj) => Equals(obj as ReferenceName);
68+
7769
public override int GetHashCode() => equalityHelper.GetHashCode(this);
70+
7871
public override string ToString() => Friendly;
7972

8073
public static bool operator ==(ReferenceName? left, ReferenceName? right)
@@ -86,38 +79,6 @@ public static ReferenceName FromBranchName(string branchName)
8679

8780
public static bool operator !=(ReferenceName? left, ReferenceName? right) => !(left == right);
8881

89-
public bool TryGetSemanticVersion(out (SemanticVersion Value, string? Name) result,
90-
Regex versionPatternRegex,
91-
string? tagPrefix,
92-
SemanticVersionFormat format)
93-
{
94-
result = default;
95-
96-
int length = 0;
97-
foreach (var branchPart in WithoutOrigin.Split(GetBranchSeparator()))
98-
{
99-
if (string.IsNullOrEmpty(branchPart)) return false;
100-
101-
var match = versionPatternRegex.NotNull().Match(branchPart);
102-
if (match.Success)
103-
{
104-
var versionPart = match.Groups["version"].Value;
105-
if (SemanticVersion.TryParse(versionPart, tagPrefix, out var semanticVersion, format))
106-
{
107-
length += versionPart.Length;
108-
var name = WithoutOrigin[length..].Trim('-');
109-
result = new(semanticVersion, name.Length == 0 ? null : name);
110-
return true;
111-
}
112-
}
113-
length += branchPart.Length + 1;
114-
}
115-
116-
return false;
117-
}
118-
119-
private char GetBranchSeparator() => WithoutOrigin.Contains('/') || !WithoutOrigin.Contains('-') ? '/' : '-';
120-
12182
public bool EquivalentTo(string? name) =>
12283
Canonical.Equals(name, StringComparison.OrdinalIgnoreCase)
12384
|| Friendly.Equals(name, StringComparison.OrdinalIgnoreCase)
@@ -143,9 +104,25 @@ private string RemoveOrigin()
143104
{
144105
return Friendly[OriginPrefix.Length..];
145106
}
107+
146108
return Friendly;
147109
}
148110

111+
private static bool TryParse([NotNullWhen(true)] out ReferenceName? value, string canonicalName)
112+
{
113+
value = null;
114+
115+
if (IsPrefixedBy(canonicalName, LocalBranchPrefix)
116+
|| IsPrefixedBy(canonicalName, RemoteTrackingBranchPrefix)
117+
|| IsPrefixedBy(canonicalName, TagPrefix)
118+
|| IsPrefixedBy(canonicalName, PullRequestPrefixes))
119+
{
120+
value = new(canonicalName);
121+
}
122+
123+
return value is not null;
124+
}
125+
149126
private static bool IsPrefixedBy(string input, string prefix) => input.StartsWith(prefix, StringComparison.Ordinal);
150127

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

src/GitVersion.Core/MergeMessage.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,7 @@ public MergeMessage(string mergeMessage, IGitVersionConfiguration configuration)
7070
public SemanticVersion? Version { get; }
7171

7272
private SemanticVersion? ParseVersion(Regex versionInBranchRegex, string? tagPrefix, SemanticVersionFormat format)
73-
{
74-
if (MergedBranch?.TryGetSemanticVersion(out var result, versionInBranchRegex, tagPrefix, format) == true)
75-
return result.Value;
76-
return null;
77-
}
73+
=> MergedBranch?.TryGetSemanticVersion(out var result, versionInBranchRegex, tagPrefix, format) == true ? result.Value : null;
7874

7975
private class MergeMessageFormat(string name, Regex pattern)
8076
{

src/GitVersion.Core/PublicAPI.Shipped.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,6 @@ GitVersion.Git.ReferenceName.IsPullRequest.get -> bool
287287
GitVersion.Git.ReferenceName.IsRemoteBranch.get -> bool
288288
GitVersion.Git.ReferenceName.IsTag.get -> bool
289289
GitVersion.Git.ReferenceName.ReferenceName(string! canonical) -> void
290-
GitVersion.Git.ReferenceName.TryGetSemanticVersion(out (GitVersion.SemanticVersion! Value, string? Name) result, System.Text.RegularExpressions.Regex! versionPatternRegex, string? tagPrefix, GitVersion.SemanticVersionFormat format) -> bool
291290
GitVersion.Git.ReferenceName.WithoutOrigin.get -> string!
292291
GitVersion.Git.RefSpecDirection
293292
GitVersion.Git.RefSpecDirection.Fetch = 0 -> GitVersion.Git.RefSpecDirection
@@ -741,7 +740,6 @@ override GitVersion.VersionCalculation.NextVersion.Equals(object? other) -> bool
741740
override GitVersion.VersionCalculation.NextVersion.GetHashCode() -> int
742741
override GitVersion.VersionCalculation.NextVersion.ToString() -> string!
743742
static GitVersion.Configuration.ReferenceNameExtensions.TryGetSemanticVersion(this GitVersion.Git.ReferenceName! source, out (GitVersion.SemanticVersion! Value, string? Name) result, GitVersion.Configuration.EffectiveConfiguration! configuration) -> bool
744-
static GitVersion.Configuration.ReferenceNameExtensions.TryGetSemanticVersion(this GitVersion.Git.ReferenceName! source, out (GitVersion.SemanticVersion! Value, string? Name) result, GitVersion.Configuration.IGitVersionConfiguration! configuration) -> bool
745743
static GitVersion.Extensions.AssemblyVersionsGeneratorExtensions.GetAssemblyFileVersion(this GitVersion.SemanticVersion! sv, GitVersion.Configuration.AssemblyFileVersioningScheme scheme) -> string?
746744
static GitVersion.Extensions.AssemblyVersionsGeneratorExtensions.GetAssemblyVersion(this GitVersion.SemanticVersion! sv, GitVersion.Configuration.AssemblyVersioningScheme scheme) -> string?
747745
static GitVersion.Extensions.CommonExtensions.NotNull<T>(this T? value, string! name = "") -> T!
@@ -770,7 +768,6 @@ static GitVersion.Git.ReferenceName.FromBranchName(string! branchName) -> GitVer
770768
static GitVersion.Git.ReferenceName.operator !=(GitVersion.Git.ReferenceName? left, GitVersion.Git.ReferenceName? right) -> bool
771769
static GitVersion.Git.ReferenceName.operator ==(GitVersion.Git.ReferenceName? left, GitVersion.Git.ReferenceName? right) -> bool
772770
static GitVersion.Git.ReferenceName.Parse(string! canonicalName) -> GitVersion.Git.ReferenceName!
773-
static GitVersion.Git.ReferenceName.TryParse(out GitVersion.Git.ReferenceName? value, string! canonicalName) -> bool
774771
static GitVersion.Helpers.Disposable.Create(System.Action! disposer) -> System.IDisposable!
775772
static GitVersion.Helpers.Disposable.Create<T>(T value, System.Action! disposer) -> GitVersion.Helpers.IDisposable<T>!
776773
static GitVersion.Helpers.EncodingHelper.DetectEncoding(string? filename) -> System.Text.Encoding?
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#nullable enable
2+
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
23
static GitVersion.Extensions.DictionaryExtensions.GetOrAdd(this System.Collections.Concurrent.ConcurrentDictionary<string!, System.Text.RegularExpressions.Regex!>! dict, string! pattern) -> System.Text.RegularExpressions.Regex!
34
static GitVersion.Extensions.DictionaryExtensions.GetOrAdd<TKey, TValue>(this System.Collections.Generic.Dictionary<TKey, TValue>! dict, TKey key, System.Func<TValue>! getValue) -> TValue
45
static GitVersion.Extensions.StringExtensions.RegexReplace(this string! input, string! pattern, string! replace) -> string!

src/GitVersion.Core/VersionCalculation/VersionSearchStrategies/VersionInBranchNameVersionStrategy.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,11 @@ public bool TryGetBaseVersion(EffectiveBranchConfiguration configuration, [NotNu
3535

3636
foreach (var branch in new[] { Context.CurrentBranch, configuration.Branch })
3737
{
38-
if (branch.Name.TryGetSemanticVersion(out var result, configuration.Value.VersionInBranchRegex,
39-
configuration.Value.TagPrefix, configuration.Value.SemanticVersionFormat))
38+
var versionInBranchRegex = configuration.Value.VersionInBranchRegex;
39+
var tagPrefix = configuration.Value.TagPrefix;
40+
var semanticVersionFormat = configuration.Value.SemanticVersionFormat;
41+
42+
if (branch.Name.TryGetSemanticVersion(out var result, versionInBranchRegex, tagPrefix, semanticVersionFormat))
4043
{
4144
string? branchNameOverride = null;
4245
if (!result.Name.IsNullOrEmpty() && (Context.CurrentBranch.Name.Equals(branch.Name)

0 commit comments

Comments
 (0)