@@ -72,13 +72,22 @@ public static BranchCommit FindCommitBranchWasBranchedFrom([NotNull] this Branch
7272 }
7373 }
7474
75+ private static List < MergeBaseData > cachedMergeBase = new List < MergeBaseData > ( ) ;
76+
7577 /// <summary>
7678 /// Find the merge base of the two branches, i.e. the best common ancestor of the two branches' tips.
7779 /// </summary>
7880 public static Commit FindMergeBase ( this Branch branch , Branch otherBranch , IRepository repository )
7981 {
8082 using ( Logger . IndentLog ( string . Format ( "Finding merge base between '{0}' and '{1}'." , branch . FriendlyName , otherBranch . FriendlyName ) ) )
8183 {
84+ // Check the cache.
85+ var cachedData = cachedMergeBase . FirstOrDefault ( data => IsSameBranch ( branch , data . Branch ) && IsSameBranch ( otherBranch , data . OtherBranch ) && repository == data . Repository ) ;
86+ if ( cachedData != null )
87+ {
88+ return cachedData . MergeBase ;
89+ }
90+
8291 // Otherbranch tip is a forward merge
8392 var commitToFindCommonBase = otherBranch . Tip ;
8493 var commit = branch . Tip ;
@@ -113,6 +122,10 @@ public static Commit FindMergeBase(this Branch branch, Branch otherBranch, IRepo
113122 }
114123 } while ( mergeBaseWasForwardMerge ) ;
115124 }
125+
126+ // Store in cache.
127+ cachedMergeBase . Add ( new MergeBaseData ( branch , otherBranch , repository , findMergeBase ) ) ;
128+
116129 return findMergeBase ;
117130 }
118131 }
@@ -275,5 +288,22 @@ public static void CheckoutFilesIfExist(this IRepository repository, params stri
275288 }
276289 }
277290 }
291+
292+ private class MergeBaseData
293+ {
294+ public Branch Branch { get ; private set ; }
295+ public Branch OtherBranch { get ; private set ; }
296+ public IRepository Repository { get ; private set ; }
297+
298+ public Commit MergeBase { get ; private set ; }
299+
300+ public MergeBaseData ( Branch branch , Branch otherBranch , IRepository repository , Commit mergeBase )
301+ {
302+ Branch = branch ;
303+ OtherBranch = otherBranch ;
304+ Repository = repository ;
305+ MergeBase = mergeBase ;
306+ }
307+ }
278308 }
279309}
0 commit comments