@@ -12,6 +12,7 @@ public class BranchConfigurationCalculator : IBranchConfigurationCalculator
12
12
13
13
private readonly ILog log ;
14
14
private readonly IRepositoryStore repositoryStore ;
15
+ private readonly int _infiniteLoopProtectionLevel = 50 ;
15
16
16
17
public BranchConfigurationCalculator ( ILog log , IRepositoryStore repositoryStore )
17
18
{
@@ -22,8 +23,13 @@ public BranchConfigurationCalculator(ILog log, IRepositoryStore repositoryStore)
22
23
/// <summary>
23
24
/// Gets the <see cref="BranchConfig"/> for the current commit.
24
25
/// </summary>
25
- public BranchConfig GetBranchConfiguration ( IBranch targetBranch , ICommit ? currentCommit , Config configuration , IList < IBranch > ? excludedInheritBranches = null )
26
+ public BranchConfig GetBranchConfiguration ( int recursiveLevel , IBranch targetBranch , ICommit ? currentCommit , Config configuration , IList < IBranch > ? excludedInheritBranches = null )
26
27
{
28
+ if ( recursiveLevel >= _infiniteLoopProtectionLevel )
29
+ {
30
+ throw new InfiniteLoopProtectionException ( "Inherited branch configuration caused an infinite loop...breaking." ) ;
31
+ }
32
+
27
33
var matchingBranches = configuration . GetConfigForBranch ( targetBranch . Name . WithoutRemote ) ;
28
34
29
35
if ( matchingBranches == null )
@@ -41,7 +47,7 @@ public BranchConfig GetBranchConfiguration(IBranch targetBranch, ICommit? curren
41
47
42
48
if ( matchingBranches . Increment == IncrementStrategy . Inherit )
43
49
{
44
- matchingBranches = InheritBranchConfiguration ( targetBranch , matchingBranches , currentCommit , configuration , excludedInheritBranches ) ;
50
+ matchingBranches = InheritBranchConfiguration ( recursiveLevel , targetBranch , matchingBranches , currentCommit , configuration , excludedInheritBranches ) ;
45
51
if ( matchingBranches . Name ! . IsEquivalentTo ( FallbackConfigName ) && matchingBranches . Increment == IncrementStrategy . Inherit )
46
52
{
47
53
// We tried, and failed to inherit, just fall back to patch
@@ -53,10 +59,12 @@ public BranchConfig GetBranchConfiguration(IBranch targetBranch, ICommit? curren
53
59
}
54
60
55
61
// TODO I think we need to take a fresh approach to this.. it's getting really complex with heaps of edge cases
56
- private BranchConfig InheritBranchConfiguration ( IBranch targetBranch , BranchConfig branchConfiguration , ICommit ? currentCommit , Config configuration , IList < IBranch > ? excludedInheritBranches )
62
+ private BranchConfig InheritBranchConfiguration ( int recursiveLevel , IBranch targetBranch , BranchConfig branchConfiguration , ICommit ? currentCommit , Config configuration , IList < IBranch > ? excludedInheritBranches )
57
63
{
58
64
using ( this . log . IndentLog ( "Attempting to inherit branch configuration from parent branch" ) )
59
65
{
66
+ recursiveLevel += 1 ;
67
+
60
68
var excludedBranches = new [ ] { targetBranch } ;
61
69
// Check if we are a merge commit. If so likely we are a pull request
62
70
var parentCount = currentCommit ? . Parents . Count ( ) ;
@@ -106,7 +114,7 @@ private BranchConfig InheritBranchConfiguration(IBranch targetBranch, BranchConf
106
114
107
115
if ( possibleParents . Count == 1 )
108
116
{
109
- var branchConfig = GetBranchConfiguration ( possibleParents [ 0 ] , currentCommit , configuration , excludedInheritBranches ) ;
117
+ var branchConfig = GetBranchConfiguration ( recursiveLevel , possibleParents [ 0 ] , currentCommit , configuration , excludedInheritBranches ) ;
110
118
// If we have resolved a fallback config we should not return that we have got config
111
119
if ( branchConfig . Name != FallbackConfigName )
112
120
{
@@ -154,7 +162,7 @@ private BranchConfig InheritBranchConfiguration(IBranch targetBranch, BranchConf
154
162
} ;
155
163
}
156
164
157
- var inheritingBranchConfig = GetBranchConfiguration ( chosenBranch , currentCommit , configuration , excludedInheritBranches ) ! ;
165
+ var inheritingBranchConfig = GetBranchConfiguration ( recursiveLevel , chosenBranch , currentCommit , configuration , excludedInheritBranches ) ! ;
158
166
var configIncrement = inheritingBranchConfig . Increment ;
159
167
if ( inheritingBranchConfig . Name ! . IsEquivalentTo ( FallbackConfigName ) && configIncrement == IncrementStrategy . Inherit )
160
168
{
0 commit comments