@@ -137,8 +137,14 @@ public ICommit GetBaseVersionSource(ICommit currentBranchTip)
137137 throw new GitVersionException ( $ "Cannot find commit { currentBranchTip } . Please ensure that the repository is an unshallow clone with `git fetch --unshallow`.", exception ) ;
138138 }
139139 }
140+
140141 public IEnumerable < ICommit > GetMainlineCommitLog ( ICommit ? baseVersionSource , ICommit ? mainlineTip )
141142 {
143+ if ( mainlineTip is null )
144+ {
145+ return Enumerable . Empty < ICommit > ( ) ;
146+ }
147+
142148 var filter = new CommitFilter
143149 {
144150 IncludeReachableFrom = mainlineTip ,
@@ -149,6 +155,7 @@ public IEnumerable<ICommit> GetMainlineCommitLog(ICommit? baseVersionSource, ICo
149155
150156 return this . repository . Commits . QueryBy ( filter ) ;
151157 }
158+
152159 public IEnumerable < ICommit > GetMergeBaseCommits ( ICommit ? mergeCommit , ICommit ? mergedHead , ICommit ? findMergeBase )
153160 {
154161 var filter = new CommitFilter
@@ -289,20 +296,20 @@ static IEnumerable<IBranch> InnerGetBranchesContainingCommit(ICommit commit, IEn
289296 public Dictionary < string , List < IBranch > > GetMainlineBranches ( ICommit commit , IEnumerable < KeyValuePair < string , BranchConfig ? > > ? mainlineBranchConfigs ) =>
290297 this . repository . Branches
291298 . Where ( b => mainlineBranchConfigs ? . Any ( c => c . Value ? . Regex != null && Regex . IsMatch ( b . Name . Friendly , c . Value . Regex ) ) == true )
292- . Select ( b => new
293- {
294- MergeBase = FindMergeBase ( b . Tip ! , commit ) ,
295- Branch = b
296- } )
297- . Where ( a => a . MergeBase != null )
298- . GroupBy ( b => b . MergeBase ! . Sha , b => b . Branch )
299+ . Select ( b => new { Origin = FindBranchOrigin ( b , commit ) , Branch = b } )
300+ . Where ( a => a . Origin != null )
301+ . GroupBy ( b => b . Origin ? . Sha , b => b . Branch )
299302 . ToDictionary ( b => b . Key , b => b . ToList ( ) ) ;
300303
304+ private ICommit FindBranchOrigin ( IBranch branch , ICommit commit ) =>
305+ FindMergeBase ( branch . Tip ! , commit ) ?? FindCommitBranchWasBranchedFrom ( branch , null ) . Commit ;
306+
307+
301308 /// <summary>
302309 /// Find the commit where the given branch was branched from another branch.
303310 /// If there are multiple such commits and branches, tries to guess based on commit histories.
304311 /// </summary>
305- public BranchCommit FindCommitBranchWasBranchedFrom ( IBranch branch , Config configuration , params IBranch [ ] excludedBranches )
312+ public BranchCommit FindCommitBranchWasBranchedFrom ( IBranch branch , Config ? configuration , params IBranch [ ] excludedBranches )
306313 {
307314 if ( branch == null )
308315 {
@@ -456,6 +463,7 @@ private IEnumerable<BranchCommit> GetMergeCommitsForBranch(IBranch branch, Confi
456463 return findMergeBase == null ? BranchCommit . Empty : new BranchCommit ( findMergeBase , otherBranch ) ;
457464
458465 } )
466+ . Where ( b => b . Commit is not null )
459467 . OrderByDescending ( b => b . Commit . When )
460468 . ToList ( ) ;
461469 this . mergeBaseCommitsCache . Add ( branch , branchMergeBases ) ;
0 commit comments