Skip to content

Commit 5eacf53

Browse files
asbjornuarturcic
authored andcommitted
Refactor recursion protection to internal method
Refactor recursion protection from the public `GetBranchConfiguration` method to an internal `GetBranchConfigurationInternal` method.
1 parent af56fb4 commit 5eacf53

File tree

4 files changed

+14
-10
lines changed

4 files changed

+14
-10
lines changed

src/GitVersion.Core/Configuration/Abstractions/IBranchConfigurationCalculator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ public interface IBranchConfigurationCalculator
77
/// <summary>
88
/// Gets the <see cref="BranchConfig"/> for the current commit.
99
/// </summary>
10-
BranchConfig GetBranchConfiguration(int recursiveProtection, IBranch targetBranch, ICommit? currentCommit, Config configuration, IList<IBranch>? excludedInheritBranches = null);
10+
BranchConfig GetBranchConfiguration(IBranch targetBranch, ICommit? currentCommit, Config configuration, IList<IBranch>? excludedInheritBranches = null);
1111
}

src/GitVersion.Core/Configuration/BranchConfigurationCalculator.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@
33
using GitVersion.Extensions;
44
using GitVersion.Logging;
55
using GitVersion.Model.Configuration;
6+
using GitVersion.Model.Exceptions;
67

78
namespace GitVersion.Configuration;
89

910
public class BranchConfigurationCalculator : IBranchConfigurationCalculator
1011
{
1112
private const string FallbackConfigName = "Fallback";
13+
private const int MaxRecursions = 50;
1214

1315
private readonly ILog log;
1416
private readonly IRepositoryStore repositoryStore;
15-
private readonly int _infiniteLoopProtectionLevel = 50;
1617

1718
public BranchConfigurationCalculator(ILog log, IRepositoryStore repositoryStore)
1819
{
@@ -23,11 +24,14 @@ public BranchConfigurationCalculator(ILog log, IRepositoryStore repositoryStore)
2324
/// <summary>
2425
/// Gets the <see cref="BranchConfig"/> for the current commit.
2526
/// </summary>
26-
public BranchConfig GetBranchConfiguration(int recursiveLevel, IBranch targetBranch, ICommit? currentCommit, Config configuration, IList<IBranch>? excludedInheritBranches = null)
27+
public BranchConfig GetBranchConfiguration(IBranch targetBranch, ICommit? currentCommit, Config configuration, IList<IBranch>? excludedInheritBranches = null) =>
28+
GetBranchConfigurationInternal(0, targetBranch, currentCommit, configuration, excludedInheritBranches);
29+
30+
private BranchConfig GetBranchConfigurationInternal(int recursiveLevel, IBranch targetBranch, ICommit? currentCommit, Config configuration, IList<IBranch>? excludedInheritBranches = null)
2731
{
28-
if (recursiveLevel >= _infiniteLoopProtectionLevel)
32+
if (recursiveLevel >= MaxRecursions)
2933
{
30-
throw new InfiniteLoopProtectionException("Inherited branch configuration caused an infinite loop...breaking.");
34+
throw new InfiniteLoopProtectionException($"Inherited branch configuration caused {recursiveLevel} recursions. Aborting!");
3135
}
3236

3337
var matchingBranches = configuration.GetConfigForBranch(targetBranch.Name.WithoutRemote);
@@ -56,6 +60,7 @@ public BranchConfig GetBranchConfiguration(int recursiveLevel, IBranch targetBra
5660
}
5761

5862
return matchingBranches;
63+
5964
}
6065

6166
// TODO I think we need to take a fresh approach to this.. it's getting really complex with heaps of edge cases
@@ -114,7 +119,7 @@ private BranchConfig InheritBranchConfiguration(int recursiveLevel, IBranch targ
114119

115120
if (possibleParents.Count == 1)
116121
{
117-
var branchConfig = GetBranchConfiguration(recursiveLevel, possibleParents[0], currentCommit, configuration, excludedInheritBranches);
122+
var branchConfig = GetBranchConfigurationInternal(recursiveLevel, possibleParents[0], currentCommit, configuration, excludedInheritBranches);
118123
// If we have resolved a fallback config we should not return that we have got config
119124
if (branchConfig.Name != FallbackConfigName)
120125
{
@@ -162,13 +167,14 @@ private BranchConfig InheritBranchConfiguration(int recursiveLevel, IBranch targ
162167
};
163168
}
164169

165-
var inheritingBranchConfig = GetBranchConfiguration(recursiveLevel, chosenBranch, currentCommit, configuration, excludedInheritBranches)!;
170+
var inheritingBranchConfig = GetBranchConfigurationInternal(recursiveLevel, chosenBranch, currentCommit, configuration, excludedInheritBranches)!;
166171
var configIncrement = inheritingBranchConfig.Increment;
167172
if (inheritingBranchConfig.Name!.IsEquivalentTo(FallbackConfigName) && configIncrement == IncrementStrategy.Inherit)
168173
{
169174
this.log.Warning("Fallback config inherits by default, dropping to patch increment");
170175
configIncrement = IncrementStrategy.Patch;
171176
}
177+
172178
return new BranchConfig(branchConfiguration)
173179
{
174180
Increment = configIncrement,

src/GitVersion.Core/Core/GitVersionContextFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public GitVersionContext Create(GitVersionOptions? gitVersionOptions)
3636
currentBranch = branchForCommit ?? currentBranch;
3737
}
3838

39-
var currentBranchConfig = this.branchConfigurationCalculator.GetBranchConfiguration(0, currentBranch, currentCommit, configuration);
39+
var currentBranchConfig = this.branchConfigurationCalculator.GetBranchConfiguration(currentBranch, currentCommit, configuration);
4040
var effectiveConfiguration = configuration.CalculateEffectiveConfiguration(currentBranchConfig);
4141
var currentCommitTaggedVersion = this.repositoryStore.GetCurrentCommitTaggedVersion(currentCommit, effectiveConfiguration);
4242
var numberOfUncommittedChanges = this.repositoryStore.GetNumberOfUncommittedChanges();

src/GitVersion.Core/Model/Exceptions/InfiniteLoopProtectionException.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using System;
2-
31
namespace GitVersion.Model.Exceptions
42
{
53
public class InfiniteLoopProtectionException : Exception

0 commit comments

Comments
 (0)