Skip to content

Commit 730dbfb

Browse files
committed
Made FindCommitBranchWasBranchedFrom way more efficient
1 parent dbdbeec commit 730dbfb

File tree

2 files changed

+8
-12
lines changed

2 files changed

+8
-12
lines changed

GitVersionCore.Tests/IntegrationTests/FeatureBranchScenarios.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using System;
2-
using System.Collections.Generic;
31
using GitVersion;
42
using LibGit2Sharp;
53
using NUnit.Framework;
@@ -23,7 +21,7 @@ public void ShouldInheritIncrementCorrectlyWithMultiplePossibleParentsAndWeirdly
2321

2422
//Merge it
2523
fixture.Repository.Checkout("development");
26-
fixture.Repository.Merge(feature123, new Signature("me", "[email protected]", DateTimeOffset.Now));
24+
fixture.Repository.Merge(feature123, SignatureBuilder.SignatureNow());
2725

2826
//Create a second feature branch
2927
fixture.Repository.CreateBranch("feature/JIRA-124");
@@ -53,7 +51,7 @@ public void BranchCreatedAfterFastForwardMergeShouldInheritCorrectly()
5351

5452
//Merge it
5553
fixture.Repository.Checkout("unstable");
56-
fixture.Repository.Merge(feature123, new Signature("me", "[email protected]", DateTimeOffset.Now));
54+
fixture.Repository.Merge(feature123, SignatureBuilder.SignatureNow());
5755

5856
//Create a second feature branch
5957
fixture.Repository.CreateBranch("feature/JIRA-124");

GitVersionCore/LibGitExtensions.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,13 @@ public static Branch FindBranch(this IRepository repository, string branchName)
2626

2727
public static Commit FindCommitBranchWasBranchedFrom(this Branch branch, IRepository repository, bool onlyTrackedBranches, params Branch[] excludedBranches)
2828
{
29-
var currentBranches = branch.Tip.GetBranchesContainingCommit(repository, onlyTrackedBranches).ToList();
30-
var tips = repository.Branches.Except(excludedBranches).Where(b => b != branch && !b.IsRemote).Select(b => b.Tip).ToList();
31-
var branchPoint = branch.Commits.FirstOrDefault(c =>
29+
var otherBranches = repository.Branches.Where(b => !b.IsRemote).Except(excludedBranches).Except(new [] { branch });
30+
var mergeBases = otherBranches.Select(b =>
3231
{
33-
if (tips.Contains(c)) return true;
34-
var branchesContainingCommit = c.GetBranchesContainingCommit(repository, onlyTrackedBranches).ToList();
35-
return branchesContainingCommit.Count > currentBranches.Count;
36-
});
37-
return branchPoint ?? branch.Tip;
32+
var mergeBase = repository.Commits.FindMergeBase(b.Tip, branch.Tip);
33+
return mergeBase;
34+
}).Where(b => b != null);
35+
return mergeBases.OrderByDescending(b => b.Committer.When).FirstOrDefault();
3836
}
3937

4038
public static IEnumerable<Branch> GetBranchesContainingCommit(this Commit commit, IRepository repository, bool onlyTrackedBranches)

0 commit comments

Comments
 (0)