Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions GitVersionCore.Tests/IntegrationTests/FeatureBranchScenarios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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("0.1.0-feature2.1+1");
}
}
}
18 changes: 18 additions & 0 deletions GitVersionCore.Tests/IntegrationTests/PullRequestScenarios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
34 changes: 22 additions & 12 deletions GitVersionCore/BranchConfigurationCalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace GitVersion

public class BranchConfigurationCalculator
{
public static KeyValuePair<string, BranchConfig> GetBranchConfiguration(Commit currentCommit, IRepository repository, bool onlyEvaluateTrackedBranches, Config config, Branch currentBranch)
public static KeyValuePair<string, BranchConfig> GetBranchConfiguration(Commit currentCommit, IRepository repository, bool onlyEvaluateTrackedBranches, Config config, Branch currentBranch, IList<Branch> excludedInheritBranches = null)
{
var matchingBranches = config.Branches.Where(b => Regex.IsMatch(currentBranch.Name, "^" + b.Key, RegexOptions.IgnoreCase)).ToArray();

Expand All @@ -23,7 +23,7 @@ public static KeyValuePair<string, BranchConfig> 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;
Expand All @@ -33,10 +33,7 @@ public static KeyValuePair<string, BranchConfig> GetBranchConfiguration(Commit c
throw new Exception(string.Format(format, currentBranch.Name, string.Join(", ", matchingBranches.Select(b => b.Key))));
}

static KeyValuePair<string, BranchConfig> InheritBranchConfiguration(
bool onlyEvaluateTrackedBranches, IRepository repository, Commit currentCommit,
Branch currentBranch, KeyValuePair<string, BranchConfig> keyValuePair,
BranchConfig branchConfiguration, Config config)
static KeyValuePair<string, BranchConfig> InheritBranchConfiguration(bool onlyEvaluateTrackedBranches, IRepository repository, Commit currentCommit, Branch currentBranch, KeyValuePair<string, BranchConfig> keyValuePair, BranchConfig branchConfiguration, Config config, IList<Branch> excludedInheritBranches)
{
Logger.WriteInfo("Attempting to inherit branch configuration from parent branch");
var excludedBranches = new [] { currentBranch };
Expand All @@ -57,23 +54,36 @@ static KeyValuePair<string, BranchConfig> 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");
}
if (excludedInheritBranches == null)
{
excludedInheritBranches = new List<Branch>();
}
excludedBranches.ToList().ForEach(excludedInheritBranches.Add);

var branchPoint = currentBranch.FindCommitBranchWasBranchedFrom(repository, onlyEvaluateTrackedBranches, excludedBranches);
var branchPoint = currentBranch.FindCommitBranchWasBranchedFrom(repository, onlyEvaluateTrackedBranches, excludedInheritBranches.ToArray());

List<Branch> 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();
Expand All @@ -89,7 +99,7 @@ static KeyValuePair<string, BranchConfig> 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<string, BranchConfig>(
keyValuePair.Key,
new BranchConfig(branchConfiguration)
Expand Down