Skip to content

Commit 691b295

Browse files
authored
Merge pull request #1070 from cvpcs/feature/MatchFirstBranchConfig
Use the first matched branch configuration
2 parents fe9a964 + 832356c commit 691b295

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

src/GitVersionCore.Tests/GitVersionContextTests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,38 @@ public void UsesBranchSpecificConfigOverTopLevelDefaults()
9090
context.Configuration.Tag.ShouldBe("alpha");
9191
}
9292

93+
[Test]
94+
public void UsesFirstBranchConfigWhenMultipleMatch()
95+
{
96+
var config = new Config
97+
{
98+
VersioningMode = VersioningMode.ContinuousDelivery,
99+
Branches =
100+
{
101+
{ "release/latest", new BranchConfig { Increment = IncrementStrategy.None, Regex = "release/latest" } },
102+
{ "release", new BranchConfig { Increment = IncrementStrategy.Patch, Regex = "releases?[/-]" } }
103+
}
104+
}.ApplyDefaults();
105+
106+
var releaseLatestBranch = new MockBranch("release/latest") { new MockCommit { CommitterEx = Generate.SignatureNow() } };
107+
var releaseVersionBranch = new MockBranch("release/1.0.0") { new MockCommit { CommitterEx = Generate.SignatureNow() } };
108+
109+
var mockRepository = new MockRepository
110+
{
111+
Branches = new MockBranchCollection
112+
{
113+
releaseLatestBranch,
114+
releaseVersionBranch
115+
}
116+
};
117+
118+
var latestContext = new GitVersionContext(mockRepository, releaseLatestBranch, config);
119+
latestContext.Configuration.Increment.ShouldBe(IncrementStrategy.None);
120+
121+
var versionContext = new GitVersionContext(mockRepository, releaseVersionBranch, config);
122+
versionContext.Configuration.Increment.ShouldBe(IncrementStrategy.Patch);
123+
}
124+
93125
[Test]
94126
public void CanFindParentBranchForInheritingIncrementStrategy()
95127
{

src/GitVersionCore/BranchConfigurationCalculator.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,19 @@ public static BranchConfig GetBranchConfiguration(Commit currentCommit, IReposit
1616
{
1717
var matchingBranches = LookupBranchConfiguration(config, currentBranch).ToArray();
1818

19-
if (matchingBranches.Length > 1)
20-
{
21-
const string format = "Multiple branch configurations match the current branch branchName of '{0}'. Matching configurations: '{1}'";
22-
throw new Exception(string.Format(format, currentBranch.FriendlyName, string.Join(", ", matchingBranches.Select(b => b.Name))));
23-
}
24-
2519
BranchConfig branchConfiguration;
26-
if (matchingBranches.Length == 1)
20+
if (matchingBranches.Length > 0)
2721
{
2822
branchConfiguration = matchingBranches[0];
23+
24+
if (matchingBranches.Length > 1)
25+
{
26+
Logger.WriteWarning(string.Format(
27+
"Multiple branch configurations match the current branch branchName of '{0}'. Using the first matching configuration, '{1}'. Matching configurations include: '{2}'",
28+
currentBranch.FriendlyName,
29+
branchConfiguration.Name,
30+
string.Join("', '", matchingBranches.Select(b => b.Name))));
31+
}
2932
}
3033
else
3134
{

0 commit comments

Comments
 (0)