Skip to content

Commit 7e43610

Browse files
committed
Merge pull request #766 from gusztavvargadr/feature/support-branch-name-variable-in-tag-configuration
Support branch name variable in tag configuration #664
2 parents 203f1a9 + 16532f8 commit 7e43610

File tree

3 files changed

+59
-3
lines changed

3 files changed

+59
-3
lines changed

src/GitVersionCore.Tests/IntegrationTests/FeatureBranchScenarios.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,30 @@ public void ShouldBePossibleToMergeDevelopForALongRunningBranchWhereDevelopAndMa
170170
fixture.AssertFullSemver("1.2.0-longrunning.2");
171171
}
172172
}
173+
174+
[TestCase("alpha", "JIRA-123", "alpha")]
175+
[TestCase("useBranchName", "JIRA-123", "JIRA-123")]
176+
[TestCase("alpha.{BranchName}", "JIRA-123", "alpha.JIRA-123")]
177+
public void ShouldUseConfiguredTag(string tag, string featureName, string preReleaseTagName)
178+
{
179+
var config = new Config
180+
{
181+
Branches =
182+
{
183+
{ "features?[/-]", new BranchConfig { Tag = tag } }
184+
}
185+
};
186+
187+
using (var fixture = new EmptyRepositoryFixture(config))
188+
{
189+
fixture.Repository.MakeATaggedCommit("1.0.0");
190+
var featureBranchName = string.Format("feature/{0}", featureName);
191+
fixture.Repository.CreateBranch(featureBranchName);
192+
fixture.Repository.Checkout(featureBranchName);
193+
fixture.Repository.MakeCommits(5);
194+
195+
var expectedFullSemVer = string.Format("1.0.1-{0}.1+5", preReleaseTagName);
196+
fixture.AssertFullSemver(expectedFullSemVer);
197+
}
198+
}
173199
}

src/GitVersionCore.Tests/VersionCalculation/NextVersionCalculatorTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,27 @@ public void PreReleaseTagCanUseBranchName()
7272

7373
version.ToString("f").ShouldBe("1.0.0-foo.1+2");
7474
}
75+
76+
[Test]
77+
public void PreReleaseTagCanUseBranchNameVariable()
78+
{
79+
var baseCalculator = new TestBaseVersionCalculator(false, new SemanticVersion(1), new MockCommit());
80+
var semanticVersionBuildMetaData = new SemanticVersionBuildMetaData(2, "develop", "b1a34e", DateTimeOffset.Now);
81+
var sut = new NextVersionCalculator(baseCalculator, new TestMetaDataCalculator(semanticVersionBuildMetaData));
82+
var config = new Config();
83+
config.Branches.Add("custom/", new BranchConfig
84+
{
85+
Tag = "alpha.{BranchName}"
86+
});
87+
var context = new GitVersionContextBuilder()
88+
.WithConfig(config)
89+
.WithDevelopBranch()
90+
.AddBranch("custom/foo")
91+
.Build();
92+
93+
var version = sut.FindVersion(context);
94+
95+
version.ToString("f").ShouldBe("1.0.0-alpha.foo.1+2");
96+
}
7597
}
7698
}

src/GitVersionCore/VersionCalculation/NextVersionCalculator.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,23 @@ void UpdatePreReleaseTag(GitVersionContext context, SemanticVersion semanticVers
7070
{
7171
var tagToUse = context.Configuration.Tag;
7272
if (tagToUse == "useBranchName")
73+
{
74+
tagToUse = "{BranchName}";
75+
}
76+
if (tagToUse.Contains("{BranchName}"))
7377
{
7478
Logger.WriteInfo("Using branch name to calculate version tag");
75-
tagToUse = branchNameOverride ?? context.CurrentBranch.Name;
79+
80+
var branchName = branchNameOverride ?? context.CurrentBranch.Name;
7681
if (!string.IsNullOrWhiteSpace(context.Configuration.BranchPrefixToTrim))
7782
{
78-
tagToUse = tagToUse.RegexReplace(context.Configuration.BranchPrefixToTrim, string.Empty, RegexOptions.IgnoreCase);
83+
branchName = branchName.RegexReplace(context.Configuration.BranchPrefixToTrim, string.Empty, RegexOptions.IgnoreCase);
7984
}
80-
tagToUse = tagToUse.RegexReplace("[^a-zA-Z0-9-]", "-");
85+
branchName = branchName.RegexReplace("[^a-zA-Z0-9-]", "-");
86+
87+
tagToUse = tagToUse.Replace("{BranchName}", branchName);
8188
}
89+
8290
int? number = null;
8391
if (!string.IsNullOrEmpty(context.Configuration.TagNumberPattern))
8492
{

0 commit comments

Comments
 (0)