@@ -10,98 +10,25 @@ namespace GitVersion;
10
10
11
11
public class RepositoryStore : IRepositoryStore
12
12
{
13
- internal const string MissingTipFormat = "{0} has no tip. Please see https://example.com/docs for information on how to fix this." ;
14
13
private readonly IIncrementStrategyFinder incrementStrategyFinder ;
15
14
private readonly ILog log ;
16
- private readonly Dictionary < Tuple < IBranch , IBranch ? > , ICommit ? > mergeBaseCache = new ( ) ;
17
15
private readonly IGitRepository repository ;
18
16
private readonly Dictionary < IBranch , List < SemanticVersion > > semanticVersionTagsOnBranchCache = new ( ) ;
17
+ private readonly MergeBaseFinder mergeBaseFinder ;
19
18
20
19
public RepositoryStore ( ILog log , IGitRepository repository , IIncrementStrategyFinder incrementStrategyFinder )
21
20
{
22
21
this . log = log . NotNull ( ) ;
23
22
this . repository = repository . NotNull ( ) ;
24
23
this . incrementStrategyFinder = incrementStrategyFinder . NotNull ( ) ;
24
+ this . mergeBaseFinder = new MergeBaseFinder ( this , repository , log ) ;
25
25
}
26
26
27
27
/// <summary>
28
28
/// Find the merge base of the two branches, i.e. the best common ancestor of the two branches' tips.
29
29
/// </summary>
30
30
public ICommit ? FindMergeBase ( IBranch ? branch , IBranch ? otherBranch )
31
- {
32
- branch = branch . NotNull ( ) ;
33
-
34
- var key = Tuple . Create ( branch , otherBranch ) ;
35
-
36
- if ( this . mergeBaseCache . ContainsKey ( key ) )
37
- {
38
- this . log . Debug ( $ "Cache hit for merge base between '{ branch } ' and '{ otherBranch } '.") ;
39
- return this . mergeBaseCache [ key ] ;
40
- }
41
-
42
- using ( this . log . IndentLog ( $ "Finding merge base between '{ branch } ' and '{ otherBranch } '.") )
43
- {
44
- // Other branch tip is a forward merge
45
- var commitToFindCommonBase = otherBranch ? . Tip ;
46
- var commit = branch . Tip ;
47
-
48
- if ( commit == null )
49
- return null ;
50
-
51
- if ( commitToFindCommonBase ? . Parents . Contains ( commit ) == true )
52
- {
53
- commitToFindCommonBase = commitToFindCommonBase . Parents . First ( ) ;
54
- }
55
-
56
- if ( commitToFindCommonBase == null )
57
- return null ;
58
-
59
- var findMergeBase = FindMergeBase ( commit , commitToFindCommonBase ) ;
60
- if ( findMergeBase != null )
61
- {
62
- this . log . Info ( $ "Found merge base of { findMergeBase } ") ;
63
- // We do not want to include merge base commits which got forward merged into the other branch
64
- ICommit ? forwardMerge ;
65
- do
66
- {
67
- // Now make sure that the merge base is not a forward merge
68
- forwardMerge = GetForwardMerge ( commitToFindCommonBase , findMergeBase ) ;
69
-
70
- if ( forwardMerge == null )
71
- continue ;
72
-
73
- // TODO Fix the logging up in this section
74
- var second = forwardMerge . Parents . First ( ) ;
75
- this . log . Debug ( $ "Second { second } ") ;
76
- var mergeBase = FindMergeBase ( commit , second ) ;
77
- if ( mergeBase == null )
78
- {
79
- this . log . Warning ( "Could not find merge base for " + commit ) ;
80
- }
81
- else
82
- {
83
- this . log . Debug ( $ "New Merge base { mergeBase } ") ;
84
- }
85
-
86
- if ( Equals ( mergeBase , findMergeBase ) )
87
- {
88
- this . log . Debug ( "Breaking" ) ;
89
- break ;
90
- }
91
-
92
- findMergeBase = mergeBase ;
93
- commitToFindCommonBase = second ;
94
- this . log . Info ( $ "Merge base was due to a forward merge, next merge base is { findMergeBase } ") ;
95
- } while ( forwardMerge != null ) ;
96
- }
97
-
98
- // Store in cache.
99
- this . mergeBaseCache . Add ( key , findMergeBase ) ;
100
-
101
- this . log . Info ( $ "Merge base of { branch } ' and '{ otherBranch } is { findMergeBase } ") ;
102
- return findMergeBase ;
103
- }
104
- }
31
+ => this . mergeBaseFinder . FindMergeBaseOf ( branch , otherBranch ) ;
105
32
106
33
public ICommit ? GetCurrentCommit ( IBranch currentBranch , string ? commitId )
107
34
{
@@ -312,7 +239,7 @@ public BranchCommit FindCommitBranchWasBranchedFrom(IBranch? branch, Config conf
312
239
{
313
240
if ( branch . Tip == null )
314
241
{
315
- this . log . Warning ( string . Format ( MissingTipFormat , branch ) ) ;
242
+ this . log . Warning ( $ " { branch } has no tip." ) ;
316
243
return BranchCommit . Empty ;
317
244
}
318
245
@@ -463,20 +390,11 @@ private bool BranchMatchesMainlineConfig(INamedReference branch, KeyValuePair<st
463
390
return null ;
464
391
}
465
392
466
-
467
393
private static IEnumerable < ICommit > GetCommitsReacheableFrom ( IGitRepository repository , IGitObject commit , IBranch branch )
468
394
{
469
395
var filter = new CommitFilter { IncludeReachableFrom = branch } ;
470
396
var commitCollection = repository . Commits . QueryBy ( filter ) ;
471
397
472
398
return commitCollection . Where ( c => c . Sha == commit . Sha ) ;
473
399
}
474
-
475
- private ICommit ? GetForwardMerge ( ICommit ? commitToFindCommonBase , ICommit ? findMergeBase )
476
- {
477
- var filter = new CommitFilter { IncludeReachableFrom = commitToFindCommonBase , ExcludeReachableFrom = findMergeBase } ;
478
- var commitCollection = this . repository . Commits . QueryBy ( filter ) ;
479
-
480
- return commitCollection . FirstOrDefault ( c => c . Parents . Contains ( findMergeBase ) ) ;
481
- }
482
400
}
0 commit comments