Skip to content

Commit af56fb4

Browse files
scphantmarturcic
authored andcommitted
Added Infinite loop protection
1 parent e267b29 commit af56fb4

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
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(IBranch targetBranch, ICommit? currentCommit, Config configuration, IList<IBranch>? excludedInheritBranches = null);
10+
BranchConfig GetBranchConfiguration(int recursiveProtection, IBranch targetBranch, ICommit? currentCommit, Config configuration, IList<IBranch>? excludedInheritBranches = null);
1111
}

src/GitVersion.Core/Configuration/BranchConfigurationCalculator.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class BranchConfigurationCalculator : IBranchConfigurationCalculator
1212

1313
private readonly ILog log;
1414
private readonly IRepositoryStore repositoryStore;
15+
private readonly int _infiniteLoopProtectionLevel = 50;
1516

1617
public BranchConfigurationCalculator(ILog log, IRepositoryStore repositoryStore)
1718
{
@@ -22,8 +23,13 @@ public BranchConfigurationCalculator(ILog log, IRepositoryStore repositoryStore)
2223
/// <summary>
2324
/// Gets the <see cref="BranchConfig"/> for the current commit.
2425
/// </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)
2627
{
28+
if (recursiveLevel >= _infiniteLoopProtectionLevel)
29+
{
30+
throw new InfiniteLoopProtectionException("Inherited branch configuration caused an infinite loop...breaking.");
31+
}
32+
2733
var matchingBranches = configuration.GetConfigForBranch(targetBranch.Name.WithoutRemote);
2834

2935
if (matchingBranches == null)
@@ -41,7 +47,7 @@ public BranchConfig GetBranchConfiguration(IBranch targetBranch, ICommit? curren
4147

4248
if (matchingBranches.Increment == IncrementStrategy.Inherit)
4349
{
44-
matchingBranches = InheritBranchConfiguration(targetBranch, matchingBranches, currentCommit, configuration, excludedInheritBranches);
50+
matchingBranches = InheritBranchConfiguration(recursiveLevel, targetBranch, matchingBranches, currentCommit, configuration, excludedInheritBranches);
4551
if (matchingBranches.Name!.IsEquivalentTo(FallbackConfigName) && matchingBranches.Increment == IncrementStrategy.Inherit)
4652
{
4753
// We tried, and failed to inherit, just fall back to patch
@@ -53,10 +59,12 @@ public BranchConfig GetBranchConfiguration(IBranch targetBranch, ICommit? curren
5359
}
5460

5561
// 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)
5763
{
5864
using (this.log.IndentLog("Attempting to inherit branch configuration from parent branch"))
5965
{
66+
recursiveLevel += 1;
67+
6068
var excludedBranches = new[] { targetBranch };
6169
// Check if we are a merge commit. If so likely we are a pull request
6270
var parentCount = currentCommit?.Parents.Count();
@@ -106,7 +114,7 @@ private BranchConfig InheritBranchConfiguration(IBranch targetBranch, BranchConf
106114

107115
if (possibleParents.Count == 1)
108116
{
109-
var branchConfig = GetBranchConfiguration(possibleParents[0], currentCommit, configuration, excludedInheritBranches);
117+
var branchConfig = GetBranchConfiguration(recursiveLevel, possibleParents[0], currentCommit, configuration, excludedInheritBranches);
110118
// If we have resolved a fallback config we should not return that we have got config
111119
if (branchConfig.Name != FallbackConfigName)
112120
{
@@ -154,7 +162,7 @@ private BranchConfig InheritBranchConfiguration(IBranch targetBranch, BranchConf
154162
};
155163
}
156164

157-
var inheritingBranchConfig = GetBranchConfiguration(chosenBranch, currentCommit, configuration, excludedInheritBranches)!;
165+
var inheritingBranchConfig = GetBranchConfiguration(recursiveLevel, chosenBranch, currentCommit, configuration, excludedInheritBranches)!;
158166
var configIncrement = inheritingBranchConfig.Increment;
159167
if (inheritingBranchConfig.Name!.IsEquivalentTo(FallbackConfigName) && configIncrement == IncrementStrategy.Inherit)
160168
{

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(currentBranch, currentCommit, configuration);
39+
var currentBranchConfig = this.branchConfigurationCalculator.GetBranchConfiguration(0, currentBranch, currentCommit, configuration);
4040
var effectiveConfiguration = configuration.CalculateEffectiveConfiguration(currentBranchConfig);
4141
var currentCommitTaggedVersion = this.repositoryStore.GetCurrentCommitTaggedVersion(currentCommit, effectiveConfiguration);
4242
var numberOfUncommittedChanges = this.repositoryStore.GetNumberOfUncommittedChanges();
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
3+
namespace GitVersion.Model.Exceptions
4+
{
5+
public class InfiniteLoopProtectionException : Exception
6+
{
7+
public InfiniteLoopProtectionException(string messageFormat)
8+
: base(messageFormat)
9+
{
10+
}
11+
}
12+
}

0 commit comments

Comments
 (0)