Skip to content

Commit 71d8fe5

Browse files
committed
minor cleanup
1 parent 4cc84d2 commit 71d8fe5

File tree

4 files changed

+35
-40
lines changed

4 files changed

+35
-40
lines changed

src/GitVersionCore/Configuration/ConfigExtensions.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using System.Text.RegularExpressions;
55
using GitVersion.Extensions;
6+
using GitVersion.Logging;
67
using GitVersion.VersionCalculation;
78

89
namespace GitVersion.Configuration
@@ -283,6 +284,29 @@ public static EffectiveConfiguration CalculateEffectiveConfiguration(this Config
283284
preReleaseWeight);
284285
}
285286

287+
public static string GetBranchSpecificTag(this EffectiveConfiguration configuration, ILog log, string branchFriendlyName, string branchNameOverride)
288+
{
289+
var tagToUse = configuration.Tag;
290+
if (tagToUse == "useBranchName")
291+
{
292+
tagToUse = "{BranchName}";
293+
}
294+
if (tagToUse.Contains("{BranchName}"))
295+
{
296+
log.Info("Using branch name to calculate version tag");
297+
298+
var branchName = branchNameOverride ?? branchFriendlyName;
299+
if (!string.IsNullOrWhiteSpace(configuration.BranchPrefixToTrim))
300+
{
301+
branchName = branchName.RegexReplace(configuration.BranchPrefixToTrim, string.Empty, RegexOptions.IgnoreCase);
302+
}
303+
branchName = branchName.RegexReplace("[^a-zA-Z0-9-]", "-");
304+
305+
tagToUse = tagToUse.Replace("{BranchName}", branchName);
306+
}
307+
return tagToUse;
308+
}
309+
286310
private static BranchConfig GetOrCreateBranchDefaults(this Config config, string branchKey)
287311
{
288312
if (!config.Branches.ContainsKey(branchKey))
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
using GitVersion.Configuration;
2-
31
namespace GitVersion.VersionCalculation
42
{
53
public interface INextVersionCalculator
64
{
75
SemanticVersion FindVersion(GitVersionContext context);
8-
string GetBranchSpecificTag(EffectiveConfiguration configuration, string branchFriendlyName, string branchNameOverride);
96
}
107
}

src/GitVersionCore/VersionCalculation/NextVersionCalculator.cs

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.IO;
33
using System.Linq;
4-
using System.Text.RegularExpressions;
54
using GitVersion.Common;
65
using GitVersion.Configuration;
76
using GitVersion.Exceptions;
@@ -47,20 +46,6 @@ public SemanticVersion FindVersion(GitVersionContext context)
4746
return FindVersionInternal(context);
4847
}
4948

50-
private static void EnsureHeadIsNotDetached(GitVersionContext context)
51-
{
52-
if (!context.CurrentBranch.IsDetachedHead())
53-
{
54-
return;
55-
}
56-
57-
var message = string.Format(
58-
"It looks like the branch being examined is a detached Head pointing to commit '{0}'. " +
59-
"Without a proper branch name GitVersion cannot determine the build version.",
60-
context.CurrentCommit.Id.ToString(7));
61-
throw new WarningException(message);
62-
}
63-
6449
public SemanticVersion FindVersionInternal(GitVersionContext context)
6550
{
6651
SemanticVersion taggedSemanticVersion = null;
@@ -129,7 +114,7 @@ private SemanticVersion PerformIncrement(GitVersionContext context, BaseVersion
129114

130115
private void UpdatePreReleaseTag(GitVersionContext context, SemanticVersion semanticVersion, string branchNameOverride)
131116
{
132-
var tagToUse = GetBranchSpecificTag(context.Configuration, context.CurrentBranch.FriendlyName, branchNameOverride);
117+
var tagToUse = context.Configuration.GetBranchSpecificTag(log, context.CurrentBranch.FriendlyName, branchNameOverride);
133118

134119
int? number = null;
135120

@@ -152,27 +137,18 @@ private void UpdatePreReleaseTag(GitVersionContext context, SemanticVersion sema
152137
semanticVersion.PreReleaseTag = new SemanticVersionPreReleaseTag(tagToUse, number);
153138
}
154139

155-
public string GetBranchSpecificTag(EffectiveConfiguration configuration, string branchFriendlyName, string branchNameOverride)
140+
private static void EnsureHeadIsNotDetached(GitVersionContext context)
156141
{
157-
var tagToUse = configuration.Tag;
158-
if (tagToUse == "useBranchName")
142+
if (!context.CurrentBranch.IsDetachedHead())
159143
{
160-
tagToUse = "{BranchName}";
144+
return;
161145
}
162-
if (tagToUse.Contains("{BranchName}"))
163-
{
164-
log.Info("Using branch name to calculate version tag");
165-
166-
var branchName = branchNameOverride ?? branchFriendlyName;
167-
if (!string.IsNullOrWhiteSpace(configuration.BranchPrefixToTrim))
168-
{
169-
branchName = branchName.RegexReplace(configuration.BranchPrefixToTrim, string.Empty, RegexOptions.IgnoreCase);
170-
}
171-
branchName = branchName.RegexReplace("[^a-zA-Z0-9-]", "-");
172146

173-
tagToUse = tagToUse.Replace("{BranchName}", branchName);
174-
}
175-
return tagToUse;
147+
var message = string.Format(
148+
"It looks like the branch being examined is a detached Head pointing to commit '{0}'. " +
149+
"Without a proper branch name GitVersion cannot determine the build version.",
150+
context.CurrentCommit.Id.ToString(7));
151+
throw new WarningException(message);
176152
}
177153

178154
private static bool MajorMinorPatchEqual(SemanticVersion lastTag, SemanticVersion baseVersion)

src/GitVersionCore/VersionCalculation/VariableProvider.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,11 @@ namespace GitVersion.OutputVariables
1111
{
1212
public class VariableProvider : IVariableProvider
1313
{
14-
private readonly INextVersionCalculator nextVersionCalculator;
1514
private readonly IEnvironment environment;
1615
private readonly ILog log;
1716

18-
public VariableProvider(INextVersionCalculator nextVersionCalculator, IEnvironment environment, ILog log)
17+
public VariableProvider(IEnvironment environment, ILog log)
1918
{
20-
this.nextVersionCalculator = nextVersionCalculator ?? throw new ArgumentNullException(nameof(nextVersionCalculator));
2119
this.environment = environment ?? throw new ArgumentNullException(nameof(environment));
2220
this.log = log ?? throw new ArgumentNullException(nameof(log));
2321
}
@@ -31,7 +29,7 @@ public VersionVariables GetVariablesFor(SemanticVersion semanticVersion, Effecti
3129
// Continuous Deployment always requires a pre-release tag unless the commit is tagged
3230
if (!semanticVersion.PreReleaseTag.HasTag())
3331
{
34-
semanticVersion.PreReleaseTag.Name = nextVersionCalculator.GetBranchSpecificTag(config, semanticVersion.BuildMetaData.Branch, null);
32+
semanticVersion.PreReleaseTag.Name = config.GetBranchSpecificTag(log, semanticVersion.BuildMetaData.Branch, null);
3533
if (string.IsNullOrEmpty(semanticVersion.PreReleaseTag.Name))
3634
{
3735
semanticVersion.PreReleaseTag.Name = config.ContinuousDeploymentFallbackTag;

0 commit comments

Comments
 (0)