From ce87323a52445a5602f5e52e74dfc4fe738c5b6f Mon Sep 17 00:00:00 2001 From: Yannis Guedel Date: Thu, 23 Apr 2015 17:18:52 +0200 Subject: [PATCH 1/3] Fix bug on PR when target has two possibilities This PR fixes a bug on pull-request builds when there are two possibilities which branch the pull-reuqest targets. --- .../IntegrationTests/PullRequestScenarios.cs | 18 ++++++++++++++++++ .../BranchConfigurationCalculator.cs | 10 +++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/GitVersionCore.Tests/IntegrationTests/PullRequestScenarios.cs b/GitVersionCore.Tests/IntegrationTests/PullRequestScenarios.cs index 87af5373be..bc9a817074 100644 --- a/GitVersionCore.Tests/IntegrationTests/PullRequestScenarios.cs +++ b/GitVersionCore.Tests/IntegrationTests/PullRequestScenarios.cs @@ -73,6 +73,24 @@ public void CanCalculatePullRequestChangesInheritingConfigFromRemoteRepo() } } + [Test] + public void CanCalculatePullRequestChangesWhenThereAreMultipleMergeCandidates() + { + using (var fixture = new EmptyRepositoryFixture(new Config())) + { + fixture.Repository.MakeATaggedCommit("0.1.0"); + fixture.Repository.CreateBranch("develop").Checkout(); + fixture.Repository.MakeACommit(); + fixture.Repository.CreateBranch("copyOfDevelop").Checkout(); + fixture.Repository.CreateBranch("feature/Foo").Checkout(); + fixture.Repository.MakeACommit(); + + fixture.Repository.CreatePullRequest("feature/Foo", "develop"); + + fixture.AssertFullSemver("0.2.0-PullRequest.2+3"); + } + } + [Test] public void CalculatesCorrectVersionAfterReleaseBranchMergedToMaster() { diff --git a/GitVersionCore/BranchConfigurationCalculator.cs b/GitVersionCore/BranchConfigurationCalculator.cs index 7955cece50..e414e96181 100644 --- a/GitVersionCore/BranchConfigurationCalculator.cs +++ b/GitVersionCore/BranchConfigurationCalculator.cs @@ -57,7 +57,15 @@ static KeyValuePair InheritBranchConfiguration( } else { - currentBranch = repository.Branches.SingleOrDefault(b => !b.IsRemote && b.Tip == parents[0]) ?? currentBranch; + var possibleTargetBranches = repository.Branches.Where(b => !b.IsRemote && b.Tip == parents[0]).ToList(); + if (possibleTargetBranches.Count() > 1) + { + currentBranch = possibleTargetBranches.FirstOrDefault(b => b.Name == "master") ?? possibleTargetBranches.First(); + } + else + { + currentBranch = possibleTargetBranches.FirstOrDefault() ?? currentBranch; + } } Logger.WriteInfo("HEAD is merge commit, this is likely a pull request using " + currentBranch.Name + " as base"); From 7af92d5c74d053b32aeecbadc3cf161947681aa0 Mon Sep 17 00:00:00 2001 From: Yannis Guedel Date: Thu, 23 Apr 2015 16:26:06 +0200 Subject: [PATCH 2/3] added test to reproduce #411 --- .../IntegrationTests/FeatureBranchScenarios.cs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/GitVersionCore.Tests/IntegrationTests/FeatureBranchScenarios.cs b/GitVersionCore.Tests/IntegrationTests/FeatureBranchScenarios.cs index b8be93ffdd..cce6be8230 100644 --- a/GitVersionCore.Tests/IntegrationTests/FeatureBranchScenarios.cs +++ b/GitVersionCore.Tests/IntegrationTests/FeatureBranchScenarios.cs @@ -48,4 +48,22 @@ public void TestFeatureBranch() fixture.AssertFullSemver("1.0.1-test.1+5"); } } + + [Test] + public void WhenTwoFeatureBranchPointToTheSameCommit() + { + using (var fixture = new EmptyRepositoryFixture(new Config())) + { + fixture.Repository.MakeACommit(); + fixture.Repository.CreateBranch("develop"); + fixture.Repository.Checkout("develop"); + fixture.Repository.CreateBranch("feature/feature1"); + fixture.Repository.Checkout("feature/feature1"); + fixture.Repository.MakeACommit(); + fixture.Repository.CreateBranch("feature/feature2"); + fixture.Repository.Checkout("feature/feature2"); + + fixture.AssertFullSemver("1.0.1-test.1+5"); + } + } } \ No newline at end of file From 0c2ce26811681852ac56e0e39179bf0c2799607d Mon Sep 17 00:00:00 2001 From: Yannis Guedel Date: Thu, 23 Apr 2015 16:37:54 +0200 Subject: [PATCH 3/3] implemented possible fix for #411 --- .../FeatureBranchScenarios.cs | 2 +- .../BranchConfigurationCalculator.cs | 24 ++++++++++--------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/GitVersionCore.Tests/IntegrationTests/FeatureBranchScenarios.cs b/GitVersionCore.Tests/IntegrationTests/FeatureBranchScenarios.cs index cce6be8230..ba061dcf0e 100644 --- a/GitVersionCore.Tests/IntegrationTests/FeatureBranchScenarios.cs +++ b/GitVersionCore.Tests/IntegrationTests/FeatureBranchScenarios.cs @@ -63,7 +63,7 @@ public void WhenTwoFeatureBranchPointToTheSameCommit() fixture.Repository.CreateBranch("feature/feature2"); fixture.Repository.Checkout("feature/feature2"); - fixture.AssertFullSemver("1.0.1-test.1+5"); + fixture.AssertFullSemver("0.1.0-feature2.1+1"); } } } \ No newline at end of file diff --git a/GitVersionCore/BranchConfigurationCalculator.cs b/GitVersionCore/BranchConfigurationCalculator.cs index e414e96181..35e411c452 100644 --- a/GitVersionCore/BranchConfigurationCalculator.cs +++ b/GitVersionCore/BranchConfigurationCalculator.cs @@ -8,7 +8,7 @@ namespace GitVersion public class BranchConfigurationCalculator { - public static KeyValuePair GetBranchConfiguration(Commit currentCommit, IRepository repository, bool onlyEvaluateTrackedBranches, Config config, Branch currentBranch) + public static KeyValuePair GetBranchConfiguration(Commit currentCommit, IRepository repository, bool onlyEvaluateTrackedBranches, Config config, Branch currentBranch, IList excludedInheritBranches = null) { var matchingBranches = config.Branches.Where(b => Regex.IsMatch(currentBranch.Name, "^" + b.Key, RegexOptions.IgnoreCase)).ToArray(); @@ -23,7 +23,7 @@ public static KeyValuePair GetBranchConfiguration(Commit c if (branchConfiguration.Increment == IncrementStrategy.Inherit) { - return InheritBranchConfiguration(onlyEvaluateTrackedBranches, repository, currentCommit, currentBranch, keyValuePair, branchConfiguration, config); + return InheritBranchConfiguration(onlyEvaluateTrackedBranches, repository, currentCommit, currentBranch, keyValuePair, branchConfiguration, config, excludedInheritBranches); } return keyValuePair; @@ -33,10 +33,7 @@ public static KeyValuePair GetBranchConfiguration(Commit c throw new Exception(string.Format(format, currentBranch.Name, string.Join(", ", matchingBranches.Select(b => b.Key)))); } - static KeyValuePair InheritBranchConfiguration( - bool onlyEvaluateTrackedBranches, IRepository repository, Commit currentCommit, - Branch currentBranch, KeyValuePair keyValuePair, - BranchConfig branchConfiguration, Config config) + static KeyValuePair InheritBranchConfiguration(bool onlyEvaluateTrackedBranches, IRepository repository, Commit currentCommit, Branch currentBranch, KeyValuePair keyValuePair, BranchConfig branchConfiguration, Config config, IList excludedInheritBranches) { Logger.WriteInfo("Attempting to inherit branch configuration from parent branch"); var excludedBranches = new [] { currentBranch }; @@ -70,18 +67,23 @@ static KeyValuePair InheritBranchConfiguration( Logger.WriteInfo("HEAD is merge commit, this is likely a pull request using " + currentBranch.Name + " as base"); } + if (excludedInheritBranches == null) + { + excludedInheritBranches = new List(); + } + excludedBranches.ToList().ForEach(excludedInheritBranches.Add); - var branchPoint = currentBranch.FindCommitBranchWasBranchedFrom(repository, onlyEvaluateTrackedBranches, excludedBranches); + var branchPoint = currentBranch.FindCommitBranchWasBranchedFrom(repository, onlyEvaluateTrackedBranches, excludedInheritBranches.ToArray()); List possibleParents; if (branchPoint.Sha == currentCommit.Sha) { - possibleParents = currentCommit.GetBranchesContainingCommit(repository, true).Except(excludedBranches).ToList(); + possibleParents = currentCommit.GetBranchesContainingCommit(repository, true).Except(excludedInheritBranches).ToList(); } else { - var branches = branchPoint.GetBranchesContainingCommit(repository, true).Except(excludedBranches).ToList(); - var currentTipBranches = currentCommit.GetBranchesContainingCommit(repository, true).Except(excludedBranches).ToList(); + var branches = branchPoint.GetBranchesContainingCommit(repository, true).Except(excludedInheritBranches).ToList(); + var currentTipBranches = currentCommit.GetBranchesContainingCommit(repository, true).Except(excludedInheritBranches).ToList(); possibleParents = branches .Except(currentTipBranches) .ToList(); @@ -97,7 +99,7 @@ static KeyValuePair InheritBranchConfiguration( if (possibleParents.Count == 1) { - var branchConfig = GetBranchConfiguration(currentCommit, repository, onlyEvaluateTrackedBranches, config, possibleParents[0]).Value; + var branchConfig = GetBranchConfiguration(currentCommit, repository, onlyEvaluateTrackedBranches, config, possibleParents[0], excludedInheritBranches).Value; return new KeyValuePair( keyValuePair.Key, new BranchConfig(branchConfiguration)