Skip to content

Commit 5939345

Browse files
committed
Cache the data in FindCommitBranchWasBranchedFrom().
Clear the caches for each new run of GitVersion.
1 parent cd0ef09 commit 5939345

File tree

2 files changed

+35
-14
lines changed

2 files changed

+35
-14
lines changed

src/GitVersionCore/GitVersionContext.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ public GitVersionContext(IRepository repository, Config configuration, bool isFo
1616

1717
public GitVersionContext(IRepository repository, Branch currentBranch, Config configuration, bool onlyEvaluateTrackedBranches = true, string commitId = null)
1818
{
19+
// With a new context, clear any in-memory caches.
20+
LibGitExtensions.ClearInMemoryCache();
21+
1922
Repository = repository;
2023
FullConfiguration = configuration;
2124
OnlyEvaluateTrackedBranches = onlyEvaluateTrackedBranches;

src/GitVersionCore/LibGitExtensions.cs

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace GitVersion
44
using System.Collections.Generic;
55
using System.IO;
66
using System.Linq;
7+
using System.Runtime.CompilerServices;
78
using JetBrains.Annotations;
89

910
using LibGit2Sharp;
@@ -32,6 +33,8 @@ public static IEnumerable<SemanticVersion> GetVersionTagsOnBranch(this Branch br
3233
}));
3334
}
3435

36+
private static List<BranchCommit> cacheMergeBaseCommits;
37+
3538
/// <summary>
3639
/// Find the commit where the given branch was branched from another branch.
3740
/// If there are multiple such commits and branches, returns the newest commit.
@@ -53,22 +56,22 @@ public static BranchCommit FindCommitBranchWasBranchedFrom([NotNull] this Branch
5356
return BranchCommit.Empty;
5457
}
5558

56-
var otherBranches = repository.Branches
57-
.ExcludingBranches(excludedBranches)
58-
.Where(b => !IsSameBranch(branch, b));
59-
var mergeBases = otherBranches.Select(otherBranch =>
59+
if (cacheMergeBaseCommits == null)
6060
{
61-
if (otherBranch.Tip == null)
61+
cacheMergeBaseCommits = repository.Branches.Select(otherBranch =>
6262
{
63-
Logger.WriteWarning(string.Format(missingTipFormat, otherBranch.FriendlyName));
64-
return BranchCommit.Empty;
65-
}
63+
if (otherBranch.Tip == null)
64+
{
65+
Logger.WriteWarning(string.Format(missingTipFormat, otherBranch.FriendlyName));
66+
return BranchCommit.Empty;
67+
}
6668

67-
var findMergeBase = FindMergeBase(branch, otherBranch, repository);
68-
return new BranchCommit(findMergeBase,otherBranch);
69-
}).Where(b => b.Commit != null).OrderByDescending(b => b.Commit.Committer.When);
69+
var findMergeBase = FindMergeBase(branch, otherBranch, repository);
70+
return new BranchCommit(findMergeBase, otherBranch);
71+
}).Where(b => b.Commit != null).OrderByDescending(b => b.Commit.Committer.When).ToList();
72+
}
7073

71-
return mergeBases.FirstOrDefault();
74+
return cacheMergeBaseCommits.ExcludingBranches(excludedBranches).FirstOrDefault(b => !IsSameBranch(branch, b.Branch));
7275
}
7376
}
7477

@@ -146,6 +149,14 @@ public static bool IsSameBranch(Branch branch, Branch otherBranch)
146149
return otherBranchFriendlyName == branchFriendlyName;
147150
}
148151

152+
/// <summary>
153+
/// Exclude the given branches (by value equality according to friendly name).
154+
/// </summary>
155+
public static IEnumerable<BranchCommit> ExcludingBranches([NotNull] this IEnumerable<BranchCommit> branches, [NotNull] IEnumerable<Branch> branchesToExclude)
156+
{
157+
return branches.Where(b => branchesToExclude.All(bte => !IsSameBranch(b.Branch, bte)));
158+
}
159+
149160
/// <summary>
150161
/// Exclude the given branches (by value equality according to friendly name).
151162
/// </summary>
@@ -210,7 +221,7 @@ public static IEnumerable<Branch> GetBranchesContainingCommit([NotNull] this Com
210221
public static GitObject PeeledTarget(this Tag tag)
211222
{
212223
GitObject cachedTarget;
213-
if(_cachedPeeledTarget.TryGetValue(tag.Target.Sha, out cachedTarget))
224+
if (_cachedPeeledTarget.TryGetValue(tag.Target.Sha, out cachedTarget))
214225
{
215226
return cachedTarget;
216227
}
@@ -274,7 +285,7 @@ public static void CheckoutFilesIfExist(this IRepository repository, params stri
274285
}
275286

276287
var fullPath = Path.Combine(repository.GetRepositoryDirectory(), fileName);
277-
using (var stream = ((Blob) treeEntry.Target).GetContentStream())
288+
using (var stream = ((Blob)treeEntry.Target).GetContentStream())
278289
{
279290
using (var streamReader = new BinaryReader(stream))
280291
{
@@ -289,6 +300,13 @@ public static void CheckoutFilesIfExist(this IRepository repository, params stri
289300
}
290301
}
291302

303+
internal static void ClearInMemoryCache()
304+
{
305+
cacheMergeBaseCommits = null;
306+
cachedMergeBase.Clear();
307+
_cachedPeeledTarget.Clear();
308+
}
309+
292310
private class MergeBaseData
293311
{
294312
public Branch Branch { get; private set; }

0 commit comments

Comments
 (0)