From 881596b997bd7c22ee70a8b8d03a96f95aa21533 Mon Sep 17 00:00:00 2001 From: Austen Dicken Date: Fri, 21 Oct 2016 14:26:00 -0700 Subject: [PATCH 1/3] Use the first matched branch configuration This change causes GitVersion to allow multiple branch configurations to match the current branch, and instead of throwing an exception it will use the first matching configuration that was found. --- src/GitVersionCore/BranchConfigurationCalculator.cs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/GitVersionCore/BranchConfigurationCalculator.cs b/src/GitVersionCore/BranchConfigurationCalculator.cs index 5409bebd8f..6ed2a5b3a1 100644 --- a/src/GitVersionCore/BranchConfigurationCalculator.cs +++ b/src/GitVersionCore/BranchConfigurationCalculator.cs @@ -16,14 +16,8 @@ public static BranchConfig GetBranchConfiguration(Commit currentCommit, IReposit { var matchingBranches = LookupBranchConfiguration(config, currentBranch).ToArray(); - if (matchingBranches.Length > 1) - { - const string format = "Multiple branch configurations match the current branch branchName of '{0}'. Matching configurations: '{1}'"; - throw new Exception(string.Format(format, currentBranch.FriendlyName, string.Join(", ", matchingBranches.Select(b => b.Name)))); - } - BranchConfig branchConfiguration; - if (matchingBranches.Length == 1) + if (matchingBranches.Length > 0) { branchConfiguration = matchingBranches[0]; } From 9c64e5954d3d456a83dee913de112ae33a28d01f Mon Sep 17 00:00:00 2001 From: Austen Dicken Date: Thu, 27 Oct 2016 21:25:32 -0700 Subject: [PATCH 2/3] Add test for multiple branch config matches This adds a test for validating that the first matched branch configuration will be chosen when multiple are present that match the current branch. --- .../GitVersionContextTests.cs | 32 +++++++++++++++++++ .../BranchConfigurationCalculator.cs | 8 +++++ 2 files changed, 40 insertions(+) diff --git a/src/GitVersionCore.Tests/GitVersionContextTests.cs b/src/GitVersionCore.Tests/GitVersionContextTests.cs index d91910af75..5261f4a416 100644 --- a/src/GitVersionCore.Tests/GitVersionContextTests.cs +++ b/src/GitVersionCore.Tests/GitVersionContextTests.cs @@ -90,6 +90,38 @@ public void UsesBranchSpecificConfigOverTopLevelDefaults() context.Configuration.Tag.ShouldBe("alpha"); } + [Test] + public void UsesFirstBranchConfigWhenMultipleMatch() + { + var config = new Config + { + VersioningMode = VersioningMode.ContinuousDelivery, + Branches = + { + { "release/latest", new BranchConfig { Increment = IncrementStrategy.None, Regex = "release/latest" } }, + { "release", new BranchConfig { Increment = IncrementStrategy.Patch, Regex = "releases?[/-]" } } + } + }.ApplyDefaults(); + + var releaseLatestBranch = new MockBranch("release/latest") { new MockCommit { CommitterEx = Generate.SignatureNow() } }; + var releaseVersionBranch = new MockBranch("release/1.0.0") { new MockCommit { CommitterEx = Generate.SignatureNow() } }; + + var mockRepository = new MockRepository + { + Branches = new MockBranchCollection + { + releaseLatestBranch, + releaseVersionBranch + } + }; + + var latestContext = new GitVersionContext(mockRepository, releaseLatestBranch, config); + latestContext.Configuration.Increment.ShouldBe(IncrementStrategy.None); + + var versionContext = new GitVersionContext(mockRepository, releaseVersionBranch, config); + versionContext.Configuration.Increment.ShouldBe(IncrementStrategy.Patch); + } + [Test] public void CanFindParentBranchForInheritingIncrementStrategy() { diff --git a/src/GitVersionCore/BranchConfigurationCalculator.cs b/src/GitVersionCore/BranchConfigurationCalculator.cs index 6ed2a5b3a1..f436d66449 100644 --- a/src/GitVersionCore/BranchConfigurationCalculator.cs +++ b/src/GitVersionCore/BranchConfigurationCalculator.cs @@ -20,6 +20,14 @@ public static BranchConfig GetBranchConfiguration(Commit currentCommit, IReposit if (matchingBranches.Length > 0) { branchConfiguration = matchingBranches[0]; + + if (matchingBranches.Length > 1) { + Logger.WriteInfo(string.Format( + "Multiple branch configurations match the current branch branchName of '{0}'. Using the first matching configuration, '{1}'. Matching configurations include: '{2}'", + currentBranch.FriendlyName, + branchConfiguration.Name, + string.Join("', '", matchingBranches.Select(b => b.Name)))); + } } else { From 832356c767067c7cc3eeb3fde20a7dd11aa3a237 Mon Sep 17 00:00:00 2001 From: Austen Dicken Date: Sat, 26 Nov 2016 12:39:28 -0600 Subject: [PATCH 3/3] Use a warning message when multiple branches are matched. --- src/GitVersionCore/BranchConfigurationCalculator.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/GitVersionCore/BranchConfigurationCalculator.cs b/src/GitVersionCore/BranchConfigurationCalculator.cs index f436d66449..14f314cf94 100644 --- a/src/GitVersionCore/BranchConfigurationCalculator.cs +++ b/src/GitVersionCore/BranchConfigurationCalculator.cs @@ -21,8 +21,9 @@ public static BranchConfig GetBranchConfiguration(Commit currentCommit, IReposit { branchConfiguration = matchingBranches[0]; - if (matchingBranches.Length > 1) { - Logger.WriteInfo(string.Format( + if (matchingBranches.Length > 1) + { + Logger.WriteWarning(string.Format( "Multiple branch configurations match the current branch branchName of '{0}'. Using the first matching configuration, '{1}'. Matching configurations include: '{2}'", currentBranch.FriendlyName, branchConfiguration.Name,