Skip to content

Commit 9ddb2c2

Browse files
committed
When branch configuration has 'Tag' set to 'useBranchName' it will use the branch name to create the tag
1 parent 889b13d commit 9ddb2c2

File tree

8 files changed

+60
-16
lines changed

8 files changed

+60
-16
lines changed

GitVersionCore.Tests/GitVersionContextBuilder.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ public GitVersionContextBuilder WithDevelopBranch()
4343
public GitVersionContextBuilder WithBranch(string branchName)
4444
{
4545
repository = CreateRepository();
46+
return AddBranch(branchName);
47+
}
48+
49+
public GitVersionContextBuilder AddBranch(string branchName)
50+
{
4651
var mockBranch = new MockBranch(branchName)
4752
{
4853
new MockCommit()

GitVersionCore.Tests/TestEffectiveConfiguration.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ public TestEffectiveConfiguration(
99
VersioningMode versioningMode = VersioningMode.ContinuousDelivery,
1010
string gitTagPrefix = "v",
1111
string tag = "",
12-
string nextVersion = null) :
13-
base(assemblyVersioningScheme, versioningMode, gitTagPrefix, tag, nextVersion, IncrementStrategy.Patch)
12+
string nextVersion = null,
13+
string branchPrefixToTrim = "") :
14+
base(assemblyVersioningScheme, versioningMode, gitTagPrefix, tag, nextVersion, IncrementStrategy.Patch, branchPrefixToTrim)
1415
{
1516
}
1617
}

GitVersionCore.Tests/VersionCalculation/NewNextVersionCalculatorTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,27 @@ public void AppliesBranchPreReleaseTag()
5858

5959
version.ToString().ShouldBe("1.0.0-unstable.1");
6060
}
61+
62+
[Test]
63+
public void PreReleaseTagCanUseBranchName()
64+
{
65+
var baseCalculator = new TestBaseVersionCalculator(false, new SemanticVersion(1), new MockCommit());
66+
var semanticVersionBuildMetaData = new SemanticVersionBuildMetaData(1, "develop", "b1a34e", DateTimeOffset.Now);
67+
var sut = new NewNextVersionCalculator(baseCalculator, new TestMetaDataCalculator(semanticVersionBuildMetaData));
68+
var config = new Config();
69+
config.Branches.Add("custom/", new BranchConfig
70+
{
71+
Tag = "useBranchName"
72+
});
73+
var context = new GitVersionContextBuilder()
74+
.WithConfig(config)
75+
.WithDevelopBranch()
76+
.AddBranch("custom/foo")
77+
.Build();
78+
79+
var version = sut.FindVersion(context);
80+
81+
version.ToString().ShouldBe("1.0.0-foo.1");
82+
}
6183
}
6284
}

GitVersionCore/Configuration/BranchConfig.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ public BranchConfig(BranchConfig branchConfiguration)
1818
[YamlAlias("mode")]
1919
public VersioningMode? VersioningMode { get; set; }
2020

21+
/// <summary>
22+
/// Special value 'useBranchName' will extract the tag from the branch name
23+
/// </summary>
2124
[YamlAlias("tag")]
2225
public string Tag { get; set; }
2326

GitVersionCore/Configuration/Config.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public Config()
1717
Branches["feature[/-]"] = new BranchConfig
1818
{
1919
Increment = IncrementStrategy.Inherit,
20+
Tag = "useBranchName"
2021
};
2122
Branches["hotfix[/-]"] = new BranchConfig { Tag = "beta" };
2223
Branches["develop"] = new BranchConfig

GitVersionCore/EffectiveConfiguration.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
/// </summary>
66
public class EffectiveConfiguration
77
{
8-
public EffectiveConfiguration(AssemblyVersioningScheme assemblyVersioningScheme, VersioningMode versioningMode, string gitTagPrefix, string tag, string nextVersion, IncrementStrategy increment)
8+
public EffectiveConfiguration(AssemblyVersioningScheme assemblyVersioningScheme, VersioningMode versioningMode, string gitTagPrefix, string tag, string nextVersion, IncrementStrategy increment, string branchPrefixToTrim)
99
{
1010
AssemblyVersioningScheme = assemblyVersioningScheme;
1111
VersioningMode = versioningMode;
1212
GitTagPrefix = gitTagPrefix;
1313
Tag = tag;
1414
NextVersion = nextVersion;
1515
Increment = increment;
16+
BranchPrefixToTrim = branchPrefixToTrim;
1617
}
1718

1819
public VersioningMode VersioningMode { get; private set; }
@@ -30,6 +31,9 @@ public EffectiveConfiguration(AssemblyVersioningScheme assemblyVersioningScheme,
3031
public string Tag { get; private set; }
3132

3233
public string NextVersion { get; private set; }
34+
3335
public IncrementStrategy Increment { get; private set; }
36+
37+
public string BranchPrefixToTrim { get; private set; }
3438
}
3539
}

GitVersionCore/GitVersionContext.cs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,25 +83,27 @@ void CalculateEffectiveConfiguration()
8383
{
8484
var currentBranchConfig = GetBranchConfiguration(CurrentBranch);
8585

86-
var versioningMode = currentBranchConfig.VersioningMode ?? configuration.VersioningMode ?? VersioningMode.ContinuousDelivery;
87-
var tag = currentBranchConfig.Tag;
86+
var versioningMode = currentBranchConfig.Value.VersioningMode ?? configuration.VersioningMode ?? VersioningMode.ContinuousDelivery;
87+
var tag = currentBranchConfig.Value.Tag;
8888
var nextVersion = configuration.NextVersion;
89-
var incrementStrategy = currentBranchConfig.Increment ?? IncrementStrategy.Patch;
90-
91-
Configuration = new EffectiveConfiguration(configuration.AssemblyVersioningScheme, versioningMode, configuration.TagPrefix, tag, nextVersion, incrementStrategy);
89+
var incrementStrategy = currentBranchConfig.Value.Increment ?? IncrementStrategy.Patch;
90+
var assemblyVersioningScheme = configuration.AssemblyVersioningScheme;
91+
var gitTagPrefix = configuration.TagPrefix;
92+
Configuration = new EffectiveConfiguration(assemblyVersioningScheme, versioningMode, gitTagPrefix, tag, nextVersion, incrementStrategy, currentBranchConfig.Key);
9293
}
9394

94-
BranchConfig GetBranchConfiguration(Branch currentBranch)
95+
KeyValuePair<string, BranchConfig> GetBranchConfiguration(Branch currentBranch)
9596
{
9697
KeyValuePair<string, BranchConfig>[] matchingBranches = configuration.Branches.Where(b => Regex.IsMatch("^" + currentBranch.Name, b.Key)).ToArray();
9798

9899
if (matchingBranches.Length == 0)
99100
{
100-
return new BranchConfig();
101+
return new KeyValuePair<string, BranchConfig>(string.Empty, new BranchConfig());
101102
}
102103
if (matchingBranches.Length == 1)
103104
{
104-
var branchConfiguration = matchingBranches[0].Value;
105+
var keyValuePair = matchingBranches[0];
106+
var branchConfiguration = keyValuePair.Value;
105107

106108
if (branchConfiguration.Increment == IncrementStrategy.Inherit)
107109
{
@@ -121,16 +123,18 @@ BranchConfig GetBranchConfiguration(Branch currentBranch)
121123

122124
if (possibleParents.Length == 1)
123125
{
124-
return new BranchConfig(branchConfiguration)
126+
return new KeyValuePair<string, BranchConfig>(
127+
keyValuePair.Key,
128+
new BranchConfig(branchConfiguration)
125129
{
126-
Increment = GetBranchConfiguration(possibleParents[0]).Increment
127-
};
130+
Increment = GetBranchConfiguration(possibleParents[0]).Value.Increment
131+
});
128132
}
129133

130134
throw new Exception("Failed to inherit Increment branch configuration");
131135
}
132136

133-
return branchConfiguration;
137+
return keyValuePair;
134138
}
135139

136140
const string format = "Multiple branch configurations match the current branch branchName of '{0}'. Matching configurations: '{1}'";

GitVersionCore/VersionCalculation/NewNextVersionCalculator.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace GitVersion.VersionCalculation
22
{
33
using System;
4+
using System.Text.RegularExpressions;
45
using BaseVersionCalculators;
56

67
public class NewNextVersionCalculator
@@ -27,7 +28,10 @@ public SemanticVersion FindVersion(GitVersionContext context)
2728

2829
if (!baseVersion.SemanticVersion.PreReleaseTag.HasTag() && !string.IsNullOrEmpty(context.Configuration.Tag))
2930
{
30-
baseVersion.SemanticVersion.PreReleaseTag = new SemanticVersionPreReleaseTag(context.Configuration.Tag, 1);
31+
var tagToUse = context.Configuration.Tag;
32+
if (tagToUse == "useBranchName")
33+
tagToUse = context.CurrentBranch.Name.RegexReplace(context.Configuration.BranchPrefixToTrim, string.Empty, RegexOptions.IgnoreCase);
34+
baseVersion.SemanticVersion.PreReleaseTag = new SemanticVersionPreReleaseTag(tagToUse, 1);
3135
}
3236

3337
baseVersion.SemanticVersion.BuildMetaData = metaDataCalculator.Create(baseVersion.BaseVersionSource, context);

0 commit comments

Comments
 (0)