Skip to content

Commit ec14032

Browse files
committed
Extract TryGetSemanticVersion method into extensions class
1 parent 1ed02ce commit ec14032

File tree

4 files changed

+51
-37
lines changed

4 files changed

+51
-37
lines changed

src/GitVersion.Core.Tests/VersionCalculation/Strategies/VersionInBranchNameBaseVersionStrategyTests.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,8 @@ public void CanTakeVersionFromNameOfReleaseBranch(string branchName, string expe
3333
baseVersion.SemanticVersion.ToString().ShouldBe(expectedBaseVersion);
3434
}
3535

36-
//[TestCase("hotfix-2.0.0")]
3736
[TestCase("origin/hotfix-2.0.0")]
3837
[TestCase("remotes/origin/hotfix-2.0.0")]
39-
//[TestCase("hotfix/2.0.0")]
4038
[TestCase("origin/hotfix/2.0.0")]
4139
[TestCase("remotes/origin/hotfix/2.0.0")]
4240
[TestCase("custom/JIRA-123")]
@@ -70,6 +68,10 @@ public void ShouldNotTakeVersionFromNameOfNonReleaseBranch(string branchName)
7068
[TestCase("release/3.0.0", "3.0.0")]
7169
[TestCase("support/2.0.0-lts", "2.0.0")]
7270
[TestCase("support-3.0.0-lts", "3.0.0")]
71+
[TestCase("hotfix/2.0.0", "2.0.0")]
72+
[TestCase("hotfix-3.0.0", "3.0.0")]
73+
[TestCase("hotfix/2.0.0-lts", "2.0.0")]
74+
[TestCase("hotfix-3.0.0-lts", "3.0.0")]
7375
public void CanTakeVersionFromNameOfConfiguredReleaseBranch(string branchName, string expectedBaseVersion)
7476
{
7577
using var fixture = new EmptyRepositoryFixture();

src/GitVersion.Core/Git/ReferenceName.cs

-34
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using System.Diagnostics.CodeAnalysis;
2-
using System.Diagnostics.Contracts;
3-
using System.Text.RegularExpressions;
42
using GitVersion.Extensions;
53
using GitVersion.Helpers;
64

@@ -106,39 +104,7 @@ private string RemoveOrigin()
106104
return Friendly;
107105
}
108106

109-
private char GetBranchSeparator() => WithoutOrigin.Contains('/') || !WithoutOrigin.Contains('-') ? '/' : '-';
110-
111107
private static bool IsPrefixedBy(string input, string prefix) => input.StartsWith(prefix, StringComparison.Ordinal);
112108

113109
private static bool IsPrefixedBy(string input, string[] prefixes) => prefixes.Any(prefix => IsPrefixedBy(input, prefix));
114-
115-
public bool TryGetSemanticVersion([NotNullWhen(true)] out (SemanticVersion Value, string? Name) result,
116-
Regex versionPatternRegex, string? labelPrefix, SemanticVersionFormat format)
117-
{
118-
result = default;
119-
120-
Contract.Assume(versionPatternRegex.ToString().StartsWith("^"));
121-
122-
int length = 0;
123-
foreach (var branchPart in WithoutOrigin.Split(GetBranchSeparator()))
124-
{
125-
if (branchPart.IsNullOrEmpty()) return false;
126-
127-
var match = versionPatternRegex.NotNull().Match(branchPart);
128-
if (match.Success)
129-
{
130-
var versionPart = match.Groups["version"].Value;
131-
if (SemanticVersion.TryParse(versionPart, labelPrefix, out var semanticVersion, format))
132-
{
133-
length += versionPart.Length;
134-
var name = WithoutOrigin[length..].Trim('-');
135-
result = new(semanticVersion, name.IsEmpty() ? null : name);
136-
return true;
137-
}
138-
}
139-
length += branchPart.Length + 1;
140-
}
141-
142-
return false;
143-
}
144110
}

src/GitVersion.Core/PublicAPI.Unshipped.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ GitVersion.OutputVariables.VersionVariablesJsonModel.WeightedPreReleaseNumber.ge
557557
GitVersion.OutputVariables.VersionVariablesJsonModel.WeightedPreReleaseNumber.set -> void
558558
GitVersion.OutputVariables.VersionVariablesJsonStringConverter
559559
GitVersion.OutputVariables.VersionVariablesJsonStringConverter.VersionVariablesJsonStringConverter() -> void
560-
GitVersion.ReferenceName.TryGetSemanticVersion(out (GitVersion.SemanticVersion! Value, string? Name) result, System.Text.RegularExpressions.Regex! versionPatternRegex, string? labelPrefix, GitVersion.SemanticVersionFormat format) -> bool
560+
GitVersion.ReferenceNameExtensions
561561
GitVersion.RefSpecDirection
562562
GitVersion.RefSpecDirection.Fetch = 0 -> GitVersion.RefSpecDirection
563563
GitVersion.RefSpecDirection.Push = 1 -> GitVersion.RefSpecDirection
@@ -887,6 +887,7 @@ static GitVersion.OutputVariables.VersionVariablesHelper.ToJsonString(this GitVe
887887
static GitVersion.ReferenceName.FromBranchName(string! branchName) -> GitVersion.ReferenceName!
888888
static GitVersion.ReferenceName.Parse(string! canonicalName) -> GitVersion.ReferenceName!
889889
static GitVersion.ReferenceName.TryParse(out GitVersion.ReferenceName? value, string! canonicalName) -> bool
890+
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
890891
static GitVersion.SemanticVersion.Parse(string! version, string? tagPrefixRegex, GitVersion.SemanticVersionFormat versionFormat = GitVersion.SemanticVersionFormat.Strict) -> GitVersion.SemanticVersion!
891892
static GitVersion.SemanticVersion.TryParse(string! version, string? tagPrefixRegex, out GitVersion.SemanticVersion? semanticVersion, GitVersion.SemanticVersionFormat format = GitVersion.SemanticVersionFormat.Strict) -> bool
892893
static GitVersion.SemanticVersion.operator !=(GitVersion.SemanticVersion? v1, GitVersion.SemanticVersion? v2) -> bool
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System.Diagnostics.CodeAnalysis;
2+
using System.Diagnostics.Contracts;
3+
using System.Text.RegularExpressions;
4+
using GitVersion.Extensions;
5+
6+
namespace GitVersion;
7+
8+
public static class ReferenceNameExtensions
9+
{
10+
public static bool TryGetSemanticVersion(this ReferenceName source,
11+
[NotNullWhen(true)] out (SemanticVersion Value, string? Name) result,
12+
Regex versionPatternRegex, string? labelPrefix, SemanticVersionFormat format)
13+
{
14+
source.NotNull();
15+
16+
result = default;
17+
18+
Contract.Assume(versionPatternRegex.ToString().StartsWith("^"));
19+
20+
int length = 0;
21+
foreach (var branchPart in source.WithoutOrigin.Split(GetBranchSeparator(source)))
22+
{
23+
if (branchPart.IsNullOrEmpty()) return false;
24+
25+
var match = versionPatternRegex.NotNull().Match(branchPart);
26+
if (match.Success)
27+
{
28+
var versionPart = match.Groups["version"].Value;
29+
if (SemanticVersion.TryParse(versionPart, labelPrefix, out var semanticVersion, format))
30+
{
31+
length += versionPart.Length;
32+
var name = source.WithoutOrigin[length..].Trim('-');
33+
result = new(semanticVersion, name.IsEmpty() ? null : name);
34+
return true;
35+
}
36+
}
37+
length += branchPart.Length + 1;
38+
}
39+
40+
return false;
41+
}
42+
43+
private static char GetBranchSeparator(ReferenceName source)
44+
=> source.WithoutOrigin.Contains('/') || !source.WithoutOrigin.Contains('-') ? '/' : '-';
45+
}

0 commit comments

Comments
 (0)