@@ -4,6 +4,7 @@ namespace GitVersion
4
4
using System . Collections . Generic ;
5
5
using System . IO ;
6
6
using System . Linq ;
7
+ using System . Runtime . CompilerServices ;
7
8
using JetBrains . Annotations ;
8
9
9
10
using LibGit2Sharp ;
@@ -32,6 +33,8 @@ public static IEnumerable<SemanticVersion> GetVersionTagsOnBranch(this Branch br
32
33
} ) ) ;
33
34
}
34
35
36
+ private static List < BranchCommit > cacheMergeBaseCommits ;
37
+
35
38
/// <summary>
36
39
/// Find the commit where the given branch was branched from another branch.
37
40
/// If there are multiple such commits and branches, returns the newest commit.
@@ -53,22 +56,22 @@ public static BranchCommit FindCommitBranchWasBranchedFrom([NotNull] this Branch
53
56
return BranchCommit . Empty ;
54
57
}
55
58
56
- var otherBranches = repository . Branches
57
- . ExcludingBranches ( excludedBranches )
58
- . Where ( b => ! IsSameBranch ( branch , b ) ) ;
59
- var mergeBases = otherBranches . Select ( otherBranch =>
59
+ if ( cacheMergeBaseCommits == null )
60
60
{
61
- if ( otherBranch . Tip == null )
61
+ cacheMergeBaseCommits = repository . Branches . Select ( otherBranch =>
62
62
{
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
+ }
66
68
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
+ }
70
73
71
- return mergeBases . FirstOrDefault ( ) ;
74
+ return cacheMergeBaseCommits . ExcludingBranches ( excludedBranches ) . FirstOrDefault ( b => ! IsSameBranch ( branch , b . Branch ) ) ;
72
75
}
73
76
}
74
77
@@ -146,6 +149,14 @@ public static bool IsSameBranch(Branch branch, Branch otherBranch)
146
149
return otherBranchFriendlyName == branchFriendlyName ;
147
150
}
148
151
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
+
149
160
/// <summary>
150
161
/// Exclude the given branches (by value equality according to friendly name).
151
162
/// </summary>
@@ -210,7 +221,7 @@ public static IEnumerable<Branch> GetBranchesContainingCommit([NotNull] this Com
210
221
public static GitObject PeeledTarget ( this Tag tag )
211
222
{
212
223
GitObject cachedTarget ;
213
- if ( _cachedPeeledTarget . TryGetValue ( tag . Target . Sha , out cachedTarget ) )
224
+ if ( _cachedPeeledTarget . TryGetValue ( tag . Target . Sha , out cachedTarget ) )
214
225
{
215
226
return cachedTarget ;
216
227
}
@@ -274,7 +285,7 @@ public static void CheckoutFilesIfExist(this IRepository repository, params stri
274
285
}
275
286
276
287
var fullPath = Path . Combine ( repository . GetRepositoryDirectory ( ) , fileName ) ;
277
- using ( var stream = ( ( Blob ) treeEntry . Target ) . GetContentStream ( ) )
288
+ using ( var stream = ( ( Blob ) treeEntry . Target ) . GetContentStream ( ) )
278
289
{
279
290
using ( var streamReader = new BinaryReader ( stream ) )
280
291
{
@@ -289,6 +300,13 @@ public static void CheckoutFilesIfExist(this IRepository repository, params stri
289
300
}
290
301
}
291
302
303
+ internal static void ClearInMemoryCache ( )
304
+ {
305
+ cacheMergeBaseCommits = null ;
306
+ cachedMergeBase . Clear ( ) ;
307
+ _cachedPeeledTarget . Clear ( ) ;
308
+ }
309
+
292
310
private class MergeBaseData
293
311
{
294
312
public Branch Branch { get ; private set ; }
0 commit comments