Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ public interface IBranchConfigurationCalculator
/// <summary>
/// Gets the <see cref="BranchConfig"/> for the current commit.
/// </summary>
BranchConfig GetBranchConfiguration(IBranch targetBranch, ICommit? currentCommit, Config configuration, IList<IBranch>? excludedInheritBranches = null);
BranchConfig GetBranchConfiguration(int recursiveProtection, IBranch targetBranch, ICommit? currentCommit, Config configuration, IList<IBranch>? excludedInheritBranches = null);
}
21 changes: 16 additions & 5 deletions src/GitVersion.Core/Configuration/BranchConfigurationCalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using GitVersion.Extensions;
using GitVersion.Logging;
using GitVersion.Model.Configuration;
using GitVersion.Model.Exceptions;

namespace GitVersion.Configuration;

Expand All @@ -13,6 +14,8 @@ public class BranchConfigurationCalculator : IBranchConfigurationCalculator
private readonly ILog log;
private readonly IRepositoryStore repositoryStore;

private readonly int _infiniteLoopProtectionLevel = 50;

public BranchConfigurationCalculator(ILog log, IRepositoryStore repositoryStore)
{
this.log = log.NotNull();
Expand All @@ -22,8 +25,14 @@ public BranchConfigurationCalculator(ILog log, IRepositoryStore repositoryStore)
/// <summary>
/// Gets the <see cref="BranchConfig"/> for the current commit.
/// </summary>
public BranchConfig GetBranchConfiguration(IBranch targetBranch, ICommit? currentCommit, Config configuration, IList<IBranch>? excludedInheritBranches = null)
public BranchConfig GetBranchConfiguration(int recursiveLevel, IBranch targetBranch, ICommit? currentCommit, Config configuration, IList<IBranch>? excludedInheritBranches = null)
{

if (recursiveLevel >= _infiniteLoopProtectionLevel)
{
throw new InfiniteLoopProtectionException("Inherited branch configuration caused an infinite loop...breaking.");
}

var matchingBranches = configuration.GetConfigForBranch(targetBranch.Name.WithoutRemote);

if (matchingBranches == null)
Expand All @@ -41,7 +50,7 @@ public BranchConfig GetBranchConfiguration(IBranch targetBranch, ICommit? curren

if (matchingBranches.Increment == IncrementStrategy.Inherit)
{
matchingBranches = InheritBranchConfiguration(targetBranch, matchingBranches, currentCommit, configuration, excludedInheritBranches);
matchingBranches = InheritBranchConfiguration(recursiveLevel, targetBranch, matchingBranches, currentCommit, configuration, excludedInheritBranches);
if (matchingBranches.Name!.IsEquivalentTo(FallbackConfigName) && matchingBranches.Increment == IncrementStrategy.Inherit)
{
// We tried, and failed to inherit, just fall back to patch
Expand All @@ -53,10 +62,12 @@ public BranchConfig GetBranchConfiguration(IBranch targetBranch, ICommit? curren
}

// TODO I think we need to take a fresh approach to this.. it's getting really complex with heaps of edge cases
private BranchConfig InheritBranchConfiguration(IBranch targetBranch, BranchConfig branchConfiguration, ICommit? currentCommit, Config configuration, IList<IBranch>? excludedInheritBranches)
private BranchConfig InheritBranchConfiguration(int recursiveLevel, IBranch targetBranch, BranchConfig branchConfiguration, ICommit? currentCommit, Config configuration, IList<IBranch>? excludedInheritBranches)
{
using (this.log.IndentLog("Attempting to inherit branch configuration from parent branch"))
{
recursiveLevel += 1;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check i tinker that we should take an approach view about the margin right in the issue


var excludedBranches = new[] { targetBranch };
// Check if we are a merge commit. If so likely we are a pull request
var parentCount = currentCommit?.Parents.Count();
Expand Down Expand Up @@ -106,7 +117,7 @@ private BranchConfig InheritBranchConfiguration(IBranch targetBranch, BranchConf

if (possibleParents.Count == 1)
{
var branchConfig = GetBranchConfiguration(possibleParents[0], currentCommit, configuration, excludedInheritBranches);
var branchConfig = GetBranchConfiguration(recursiveLevel, possibleParents[0], currentCommit, configuration, excludedInheritBranches);
// If we have resolved a fallback config we should not return that we have got config
if (branchConfig.Name != FallbackConfigName)
{
Expand Down Expand Up @@ -154,7 +165,7 @@ private BranchConfig InheritBranchConfiguration(IBranch targetBranch, BranchConf
};
}

var inheritingBranchConfig = GetBranchConfiguration(chosenBranch, currentCommit, configuration, excludedInheritBranches)!;
var inheritingBranchConfig = GetBranchConfiguration(recursiveLevel, chosenBranch, currentCommit, configuration, excludedInheritBranches)!;
var configIncrement = inheritingBranchConfig.Increment;
if (inheritingBranchConfig.Name!.IsEquivalentTo(FallbackConfigName) && configIncrement == IncrementStrategy.Inherit)
{
Expand Down
2 changes: 1 addition & 1 deletion src/GitVersion.Core/Core/GitVersionContextFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public GitVersionContext Create(GitVersionOptions? gitVersionOptions)
currentBranch = branchForCommit ?? currentBranch;
}

var currentBranchConfig = this.branchConfigurationCalculator.GetBranchConfiguration(currentBranch, currentCommit, configuration);
var currentBranchConfig = this.branchConfigurationCalculator.GetBranchConfiguration(0, currentBranch, currentCommit, configuration);
var effectiveConfiguration = configuration.CalculateEffectiveConfiguration(currentBranchConfig);
var currentCommitTaggedVersion = this.repositoryStore.GetCurrentCommitTaggedVersion(currentCommit, effectiveConfiguration);
var numberOfUncommittedChanges = this.repositoryStore.GetNumberOfUncommittedChanges();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace GitVersion.Model.Exceptions
{
public class InfiniteLoopProtectionException : Exception
{
public InfiniteLoopProtectionException(string messageFormat)
: base(messageFormat)
{
}
}
}