Skip to content

Commit 2771019

Browse files
author
Simon Ejsing
committed
Reuse the tagNumberPattern option for ContinuousDeployment scenario and fix for useBranchName as the tag value
1 parent d61d207 commit 2771019

File tree

3 files changed

+54
-23
lines changed

3 files changed

+54
-23
lines changed

src/GitVersionCore.Tests/VariableProviderTests.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,30 @@ public void ProvidesVariablesInContinuousDeploymentModeWithTagNamePattern()
169169
semVer.BuildMetaData.Sha = "commitSha";
170170
semVer.BuildMetaData.CommitDate = DateTimeOffset.Parse("2014-03-06 23:59:59Z");
171171

172-
var config = new TestEffectiveConfiguration(versioningMode: VersioningMode.ContinuousDeployment, tagNamePattern: @"[/-](?<number>\d+)[-/]");
172+
var config = new TestEffectiveConfiguration(versioningMode: VersioningMode.ContinuousDeployment, tagNumberPattern: @"[/-](?<number>\d+)[-/]");
173173
var vars = VariableProvider.GetVariablesFor(semVer, config, false);
174174

175175
vars.FullSemVer.ShouldBe("1.2.3-PullRequest2.5");
176176
}
177177

178+
[Test]
179+
public void ProvidesVariablesInContinuousDeploymentModeWithTagSetToUseBranchName()
180+
{
181+
var semVer = new SemanticVersion
182+
{
183+
Major = 1,
184+
Minor = 2,
185+
Patch = 3,
186+
BuildMetaData = "5.Branch.develop"
187+
};
188+
189+
semVer.BuildMetaData.Branch = "feature";
190+
semVer.BuildMetaData.Sha = "commitSha";
191+
semVer.BuildMetaData.CommitDate = DateTimeOffset.Parse("2014-03-06 23:59:59Z");
192+
193+
var config = new TestEffectiveConfiguration(versioningMode: VersioningMode.ContinuousDeployment, tag: "useBranchName");
194+
var vars = VariableProvider.GetVariablesFor(semVer, config, false);
195+
196+
vars.FullSemVer.ShouldBe("1.2.3-feature.5");
197+
}
178198
}

src/GitVersionCore/OutputVariables/VariableProvider.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System;
44
using System.ComponentModel;
55
using System.Text.RegularExpressions;
6+
using GitVersion.VersionCalculation;
67

78
public static class VariableProvider
89
{
@@ -14,13 +15,17 @@ public static VersionVariables GetVariablesFor(SemanticVersion semanticVersion,
1415
// Continuous Deployment always requires a pre-release tag unless the commit is tagged
1516
if (!semanticVersion.PreReleaseTag.HasTag())
1617
{
17-
semanticVersion.PreReleaseTag.Name = config.ContinuousDeploymentFallbackTag;
18+
semanticVersion.PreReleaseTag.Name = NextVersionCalculator.GetBranchSpecificTag(config, semanticVersion.BuildMetaData.Branch, null);
19+
if (string.IsNullOrEmpty(semanticVersion.PreReleaseTag.Name))
20+
{
21+
semanticVersion.PreReleaseTag.Name = config.ContinuousDeploymentFallbackTag;
22+
}
1823
}
1924

20-
// Evaluate tag name pattern and append to prerelease tag
21-
if (!string.IsNullOrEmpty(config.TagNamePattern))
25+
// Evaluate tag number pattern and append to prerelease tag, preserving build metadata
26+
if (!string.IsNullOrEmpty(config.TagNumberPattern))
2227
{
23-
var match = Regex.Match(semanticVersion.BuildMetaData.Branch, config.TagNamePattern);
28+
var match = Regex.Match(semanticVersion.BuildMetaData.Branch, config.TagNumberPattern);
2429
var numberGroup = match.Groups["number"];
2530
if (numberGroup.Success)
2631
{

src/GitVersionCore/VersionCalculation/NextVersionCalculator.cs

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -69,24 +69,7 @@ public SemanticVersion FindVersion(GitVersionContext context)
6969

7070
void UpdatePreReleaseTag(GitVersionContext context, SemanticVersion semanticVersion, string branchNameOverride)
7171
{
72-
var tagToUse = context.Configuration.Tag;
73-
if (tagToUse == "useBranchName")
74-
{
75-
tagToUse = "{BranchName}";
76-
}
77-
if (tagToUse.Contains("{BranchName}"))
78-
{
79-
Logger.WriteInfo("Using branch name to calculate version tag");
80-
81-
var branchName = branchNameOverride ?? context.CurrentBranch.FriendlyName;
82-
if (!string.IsNullOrWhiteSpace(context.Configuration.BranchPrefixToTrim))
83-
{
84-
branchName = branchName.RegexReplace(context.Configuration.BranchPrefixToTrim, string.Empty, RegexOptions.IgnoreCase);
85-
}
86-
branchName = branchName.RegexReplace("[^a-zA-Z0-9-]", "-");
87-
88-
tagToUse = tagToUse.Replace("{BranchName}", branchName);
89-
}
72+
var tagToUse = GetBranchSpecificTag(context.Configuration, context.CurrentBranch.FriendlyName, branchNameOverride);
9073

9174
int? number = null;
9275
if (!string.IsNullOrEmpty(context.Configuration.TagNumberPattern))
@@ -119,6 +102,29 @@ void UpdatePreReleaseTag(GitVersionContext context, SemanticVersion semanticVers
119102
semanticVersion.PreReleaseTag = new SemanticVersionPreReleaseTag(tagToUse, number);
120103
}
121104

105+
public static string GetBranchSpecificTag(EffectiveConfiguration configuration, string branchFriendlyName, string branchNameOverride)
106+
{
107+
var tagToUse = configuration.Tag;
108+
if (tagToUse == "useBranchName")
109+
{
110+
tagToUse = "{BranchName}";
111+
}
112+
if (tagToUse.Contains("{BranchName}"))
113+
{
114+
Logger.WriteInfo("Using branch name to calculate version tag");
115+
116+
var branchName = branchNameOverride ?? branchFriendlyName;
117+
if (!string.IsNullOrWhiteSpace(configuration.BranchPrefixToTrim))
118+
{
119+
branchName = branchName.RegexReplace(configuration.BranchPrefixToTrim, string.Empty, RegexOptions.IgnoreCase);
120+
}
121+
branchName = branchName.RegexReplace("[^a-zA-Z0-9-]", "-");
122+
123+
tagToUse = tagToUse.Replace("{BranchName}", branchName);
124+
}
125+
return tagToUse;
126+
}
127+
122128
static bool MajorMinorPatchEqual(SemanticVersion lastTag, SemanticVersion baseVersion)
123129
{
124130
return lastTag.Major == baseVersion.Major &&

0 commit comments

Comments
 (0)