@@ -154,64 +154,8 @@ public IEnumerable<IBranch> GetReleaseBranches(IEnumerable<KeyValuePair<string,
154
154
155
155
public IEnumerable < IBranch > ExcludingBranches ( IEnumerable < IBranch > branchesToExclude ) => this . repository . Branches . ExcludeBranches ( branchesToExclude ) ;
156
156
157
- // TODO Should we cache this?
158
157
public IEnumerable < IBranch > GetBranchesContainingCommit ( ICommit ? commit , IEnumerable < IBranch > ? branches = null , bool onlyTrackedBranches = false )
159
- {
160
- commit = commit . NotNull ( ) ;
161
-
162
- return InnerGetBranchesContainingCommit ( commit , branches , onlyTrackedBranches , this . repository , this . log ) ;
163
-
164
- static bool IncludeTrackedBranches ( IBranch branch , bool includeOnlyTracked )
165
- => includeOnlyTracked && branch . IsTracking || ! includeOnlyTracked ;
166
-
167
- // Yielding part is split from the main part of the method to avoid having the exception check performed lazily.
168
- // Details at https://github.com/GitTools/GitVersion/issues/2755
169
- static IEnumerable < IBranch > InnerGetBranchesContainingCommit ( IGitObject commit , IEnumerable < IBranch > ? branches , bool onlyTrackedBranches , IGitRepository repository , ILog log )
170
- {
171
- branches ??= repository . Branches . ToList ( ) ;
172
-
173
- using ( log . IndentLog ( $ "Getting branches containing the commit '{ commit . Id } '.") )
174
- {
175
- var directBranchHasBeenFound = false ;
176
- log . Info ( "Trying to find direct branches." ) ;
177
- // TODO: It looks wasteful looping through the branches twice. Can't these loops be merged somehow? @asbjornu
178
- List < IBranch > branchList = branches . ToList ( ) ;
179
- foreach ( IBranch branch in branchList )
180
- {
181
- if ( branch . Tip != null && branch . Tip . Sha != commit . Sha || IncludeTrackedBranches ( branch , onlyTrackedBranches ) )
182
- {
183
- continue ;
184
- }
185
-
186
- directBranchHasBeenFound = true ;
187
- log . Info ( $ "Direct branch found: '{ branch } '.") ;
188
- yield return branch ;
189
- }
190
-
191
- if ( directBranchHasBeenFound )
192
- {
193
- yield break ;
194
- }
195
-
196
- log . Info ( $ "No direct branches found, searching through { ( onlyTrackedBranches ? "tracked" : "all" ) } branches.") ;
197
- foreach ( IBranch branch in branchList . Where ( b => IncludeTrackedBranches ( b , onlyTrackedBranches ) ) )
198
- {
199
- log . Info ( $ "Searching for commits reachable from '{ branch } '.") ;
200
-
201
- var commits = GetCommitsReacheableFrom ( repository , commit , branch ) ;
202
-
203
- if ( ! commits . Any ( ) )
204
- {
205
- log . Info ( $ "The branch '{ branch } ' has no matching commits.") ;
206
- continue ;
207
- }
208
-
209
- log . Info ( $ "The branch '{ branch } ' has a matching commit.") ;
210
- yield return branch ;
211
- }
212
- }
213
- }
214
- }
158
+ => new BranchesContainingCommitFinder ( this . repository , this . log ) . GetBranchesContainingCommit ( commit , branches , onlyTrackedBranches ) ;
215
159
216
160
public Dictionary < string , List < IBranch > > GetMainlineBranches ( ICommit commit , Config configuration , IEnumerable < KeyValuePair < string , BranchConfig > > ? mainlineBranchConfigs )
217
161
{
@@ -389,12 +333,4 @@ private bool BranchMatchesMainlineConfig(INamedReference branch, KeyValuePair<st
389
333
this . log . Info ( $ "Found no merge base or parent commit for '{ branchName } '.") ;
390
334
return null ;
391
335
}
392
-
393
- private static IEnumerable < ICommit > GetCommitsReacheableFrom ( IGitRepository repository , IGitObject commit , IBranch branch )
394
- {
395
- var filter = new CommitFilter { IncludeReachableFrom = branch } ;
396
- var commitCollection = repository . Commits . QueryBy ( filter ) ;
397
-
398
- return commitCollection . Where ( c => c . Sha == commit . Sha ) ;
399
- }
400
336
}
0 commit comments