Skip to content

Commit 5205799

Browse files
committed
Refactor BranchConfiguration for improved encapsulation and re-usability
This commit modifies EffectiveBranchConfiguration and NextVersion to encapsulate branch and configuration data. The changes focus on transforming some classes to records, thus creating better data structures. The countless changes in method calls from various strategies and calculators accommodate the revised data encapsulation. The intention behind this refactor is to enhance readability, reduce redundancy, and provide better design of the data structure. It is important to note that these modifications are not expected to change the existing behavior.
1 parent a003da2 commit 5205799

15 files changed

+38
-63
lines changed

src/GitVersion.Core.Tests/Helpers/TestEffectiveConfiguration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace GitVersion.Core.Tests.Helpers;
66

7-
public class TestEffectiveConfiguration : EffectiveConfiguration
7+
public record TestEffectiveConfiguration : EffectiveConfiguration
88
{
99
public TestEffectiveConfiguration(
1010
AssemblyVersioningScheme assemblyVersioningScheme = AssemblyVersioningScheme.MajorMinorPatch,

src/GitVersion.Core.Tests/VersionCalculation/Strategies/ConfigNextVersionBaseVersionStrategyTests.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,8 @@ public void ConfigNextVersionTestShouldFail(string nextVersion, SemanticVersionF
5757
var strategy = contextBuilder.ServicesProvider.GetServiceForType<IVersionStrategy, ConfigNextVersionVersionStrategy>();
5858
var context = contextBuilder.ServicesProvider.GetRequiredService<Lazy<GitVersionContext>>().Value;
5959
var branchMock = GitToolsTestingExtensions.CreateMockBranch("main", GitToolsTestingExtensions.CreateMockCommit());
60-
var branchConfiguration = context.Configuration.GetBranchConfiguration(branchMock);
61-
var effectiveConfiguration = new EffectiveConfiguration(context.Configuration, branchConfiguration);
6260

6361
strategy.ShouldNotBeNull();
64-
return strategy.GetBaseVersions(new(branchMock, effectiveConfiguration)).SingleOrDefault();
62+
return strategy.GetBaseVersions(context.Configuration.GetEffectiveBranchConfiguration(branchMock)).SingleOrDefault();
6563
}
6664
}

src/GitVersion.Core.Tests/VersionCalculation/Strategies/MergeMessageBaseVersionStrategyTests.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,9 @@ public void ShouldNotAllowIncrementOfVersion()
3535
contextBuilder.ServicesProvider.ShouldNotBeNull();
3636
var strategy = contextBuilder.ServicesProvider.GetServiceForType<IVersionStrategy, MergeMessageVersionStrategy>();
3737
var context = contextBuilder.ServicesProvider.GetRequiredService<Lazy<GitVersionContext>>().Value;
38-
var branchConfiguration = context.Configuration.GetBranchConfiguration(mockBranch);
39-
var effectiveConfiguration = new EffectiveConfiguration(context.Configuration, branchConfiguration);
4038

4139
strategy.ShouldNotBeNull();
42-
var baseVersion = strategy.GetBaseVersions(new(mockBranch, effectiveConfiguration)).Single();
40+
var baseVersion = strategy.GetBaseVersions(context.Configuration.GetEffectiveBranchConfiguration(mockBranch)).Single();
4341

4442
baseVersion.ShouldIncrement.ShouldBe(false);
4543
}
@@ -168,11 +166,9 @@ private static void AssertMergeMessage(string message, string? expectedVersion,
168166
contextBuilder.ServicesProvider.ShouldNotBeNull();
169167
var strategy = contextBuilder.ServicesProvider.GetServiceForType<IVersionStrategy, MergeMessageVersionStrategy>();
170168
var context = contextBuilder.ServicesProvider.GetRequiredService<Lazy<GitVersionContext>>().Value;
171-
var branchConfiguration = context.Configuration.GetBranchConfiguration(mockBranch);
172-
var effectiveConfiguration = new EffectiveConfiguration(context.Configuration, branchConfiguration);
173169

174170
strategy.ShouldNotBeNull();
175-
var baseVersion = strategy.GetBaseVersions(new(mockBranch, effectiveConfiguration)).SingleOrDefault();
171+
var baseVersion = strategy.GetBaseVersions(context.Configuration.GetEffectiveBranchConfiguration(mockBranch)).SingleOrDefault();
176172

177173
if (expectedVersion == null)
178174
{

src/GitVersion.Core.Tests/VersionCalculation/Strategies/VersionInBranchNameBaseVersionStrategyTests.cs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ public void CanTakeVersionFromNameOfReleaseBranch(string branchName, string expe
2323
var branch = repository.FindBranch(branchName);
2424

2525
var configuration = GitFlowConfigurationBuilder.New.Build();
26-
var branchConfiguration = configuration.GetBranchConfiguration(branch);
27-
var effectiveConfiguration = new EffectiveConfiguration(configuration, branchConfiguration);
28-
var effectiveBranchConfiguration = new EffectiveBranchConfiguration(branch, effectiveConfiguration);
26+
var effectiveBranchConfiguration = configuration.GetEffectiveBranchConfiguration(branch);
2927

3028
strategy.ShouldNotBeNull();
3129
var baseVersion = strategy.GetBaseVersions(effectiveBranchConfiguration).Single();
@@ -54,9 +52,7 @@ public void ShouldNotTakeVersionFromNameOfNonReleaseBranch(string branchName)
5452
var branch = repository.FindBranch(branchName);
5553

5654
var configuration = GitFlowConfigurationBuilder.New.Build();
57-
var branchConfiguration = configuration.GetBranchConfiguration(branch);
58-
var effectiveConfiguration = new EffectiveConfiguration(configuration, branchConfiguration);
59-
var effectiveBranchConfiguration = new EffectiveBranchConfiguration(branch, effectiveConfiguration);
55+
var effectiveBranchConfiguration = configuration.GetEffectiveBranchConfiguration(branch);
6056

6157
strategy.ShouldNotBeNull();
6258
var baseVersions = strategy.GetBaseVersions(effectiveBranchConfiguration);
@@ -89,9 +85,7 @@ public void CanTakeVersionFromNameOfConfiguredReleaseBranch(string branchName, s
8985
var strategy = GetVersionStrategy(repository, null, configurationHelper.Dictionary);
9086
var branch = repository.FindBranch(branchName);
9187

92-
var branchConfiguration = configuration.GetBranchConfiguration(branch);
93-
var effectiveConfiguration = new EffectiveConfiguration(configuration, branchConfiguration);
94-
var effectiveBranchConfiguration = new EffectiveBranchConfiguration(branch, effectiveConfiguration);
88+
var effectiveBranchConfiguration = configuration.GetEffectiveBranchConfiguration(branch);
9589

9690
strategy.ShouldNotBeNull();
9791
var baseVersion = strategy.GetBaseVersions(effectiveBranchConfiguration).Single();
@@ -115,9 +109,7 @@ public void CanTakeVersionFromNameOfRemoteReleaseBranch(string origin, string br
115109
var branch = localRepository.FindBranch(branchName);
116110

117111
var configuration = GitFlowConfigurationBuilder.New.Build();
118-
var branchConfiguration = configuration.GetBranchConfiguration(branch);
119-
var effectiveConfiguration = new EffectiveConfiguration(configuration, branchConfiguration);
120-
var effectiveBranchConfiguration = new EffectiveBranchConfiguration(branch, effectiveConfiguration);
112+
var effectiveBranchConfiguration = configuration.GetEffectiveBranchConfiguration(branch);
121113

122114
strategy.ShouldNotBeNull();
123115
var baseVersion = strategy.GetBaseVersions(effectiveBranchConfiguration).Single();

src/GitVersion.Core/Configuration/ConfigurationExtensions.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@ namespace GitVersion.Configuration;
55

66
public static class ConfigurationExtensions
77
{
8-
public static EffectiveConfiguration GetEffectiveConfiguration(this IGitVersionConfiguration configuration, IBranch branch)
9-
=> GetEffectiveConfiguration(configuration, branch.NotNull().Name);
8+
public static EffectiveBranchConfiguration GetEffectiveBranchConfiguration(this IGitVersionConfiguration configuration, IBranch branch)
9+
{
10+
var effectiveConfiguration = GetEffectiveConfiguration(configuration, branch.Name);
11+
return new(effectiveConfiguration, branch);
12+
}
1013

1114
public static EffectiveConfiguration GetEffectiveConfiguration(this IGitVersionConfiguration configuration, ReferenceName branchName)
1215
{
13-
IBranchConfiguration branchConfiguration = configuration.GetBranchConfiguration(branchName);
14-
return new EffectiveConfiguration(configuration, branchConfiguration);
16+
var branchConfiguration = configuration.GetBranchConfiguration(branchName);
17+
return new(configuration, branchConfiguration);
1518
}
1619

17-
public static IBranchConfiguration GetBranchConfiguration(this IGitVersionConfiguration configuration, IBranch branch)
18-
=> GetBranchConfiguration(configuration, branch.NotNull().Name);
19-
2020
public static IBranchConfiguration GetBranchConfiguration(this IGitVersionConfiguration configuration, ReferenceName branchName)
2121
{
2222
var branchConfiguration = GetBranchConfigurations(configuration, branchName.WithoutOrigin).FirstOrDefault();

src/GitVersion.Core/Configuration/EffectiveBranchConfiguration.cs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,17 @@
33

44
namespace GitVersion.Configuration;
55

6-
public class EffectiveBranchConfiguration
6+
public record EffectiveBranchConfiguration(EffectiveConfiguration Value, IBranch Branch)
77
{
8-
public IBranch Branch { get; }
8+
public IBranch Branch { get; } = Branch.NotNull();
99

10-
public EffectiveConfiguration Value { get; }
11-
12-
public EffectiveBranchConfiguration(IBranch branch, EffectiveConfiguration value)
13-
{
14-
Branch = branch.NotNull();
15-
Value = value.NotNull();
16-
}
10+
public EffectiveConfiguration Value { get; } = Value.NotNull();
1711

1812
public NextVersion CreateNextVersion(BaseVersion baseVersion, SemanticVersion incrementedVersion)
1913
{
2014
incrementedVersion.NotNull();
2115
baseVersion.NotNull();
2216

23-
return new NextVersion(incrementedVersion, baseVersion, new EffectiveBranchConfiguration(Branch, Value));
17+
return new(incrementedVersion, baseVersion, this);
2418
}
2519
}

src/GitVersion.Core/Configuration/EffectiveConfiguration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace GitVersion.Configuration;
88
/// Configuration can be applied to different things, effective configuration is the result after applying the
99
/// appropriate configuration
1010
/// </summary>
11-
public class EffectiveConfiguration
11+
public record EffectiveConfiguration
1212
{
1313
public EffectiveConfiguration(IGitVersionConfiguration configuration, IBranchConfiguration branchConfiguration)
1414
{

src/GitVersion.Core/Core/RepositoryStore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public IEnumerable<IBranch> FindMainlineBranches(IGitVersionConfiguration config
126126

127127
foreach (var branch in this.repository.Branches)
128128
{
129-
var branchConfiguration = configuration.GetBranchConfiguration(branch);
129+
var branchConfiguration = configuration.GetBranchConfiguration(branch.Name);
130130
if (branchConfiguration.IsMainline == true)
131131
{
132132
yield return branch;

src/GitVersion.Core/PublicAPI.Unshipped.txt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ GitVersion.Configuration.ConfigurationModule.RegisterTypes(Microsoft.Extensions.
100100
GitVersion.Configuration.EffectiveBranchConfiguration
101101
GitVersion.Configuration.EffectiveBranchConfiguration.Branch.get -> GitVersion.IBranch!
102102
GitVersion.Configuration.EffectiveBranchConfiguration.CreateNextVersion(GitVersion.VersionCalculation.BaseVersion! baseVersion, GitVersion.SemanticVersion! incrementedVersion) -> GitVersion.VersionCalculation.NextVersion!
103-
GitVersion.Configuration.EffectiveBranchConfiguration.EffectiveBranchConfiguration(GitVersion.IBranch! branch, GitVersion.Configuration.EffectiveConfiguration! value) -> void
103+
GitVersion.Configuration.EffectiveBranchConfiguration.EffectiveBranchConfiguration(GitVersion.Configuration.EffectiveConfiguration! Value, GitVersion.IBranch! Branch) -> void
104104
GitVersion.Configuration.EffectiveBranchConfiguration.Value.get -> GitVersion.Configuration.EffectiveConfiguration!
105105
GitVersion.Configuration.EffectiveConfiguration
106106
GitVersion.Configuration.EffectiveConfiguration.AssemblyFileVersioningFormat.get -> string?
@@ -735,13 +735,12 @@ GitVersion.VersionCalculation.IVersionStrategy
735735
GitVersion.VersionCalculation.IVersionStrategy.GetBaseVersions(GitVersion.Configuration.EffectiveBranchConfiguration! configuration) -> System.Collections.Generic.IEnumerable<GitVersion.VersionCalculation.BaseVersion!>!
736736
GitVersion.VersionCalculation.NextVersion
737737
GitVersion.VersionCalculation.NextVersion.BaseVersion.get -> GitVersion.VersionCalculation.BaseVersion!
738-
GitVersion.VersionCalculation.NextVersion.Branch.get -> GitVersion.IBranch!
738+
GitVersion.VersionCalculation.NextVersion.BranchConfiguration.get -> GitVersion.Configuration.EffectiveBranchConfiguration!
739739
GitVersion.VersionCalculation.NextVersion.CompareTo(GitVersion.VersionCalculation.NextVersion? other) -> int
740740
GitVersion.VersionCalculation.NextVersion.Configuration.get -> GitVersion.Configuration.EffectiveConfiguration!
741741
GitVersion.VersionCalculation.NextVersion.Equals(GitVersion.VersionCalculation.NextVersion? other) -> bool
742742
GitVersion.VersionCalculation.NextVersion.IncrementedVersion.get -> GitVersion.SemanticVersion!
743743
GitVersion.VersionCalculation.NextVersion.NextVersion(GitVersion.SemanticVersion! incrementedVersion, GitVersion.VersionCalculation.BaseVersion! baseVersion, GitVersion.Configuration.EffectiveBranchConfiguration! configuration) -> void
744-
GitVersion.VersionCalculation.NextVersion.NextVersion(GitVersion.SemanticVersion! incrementedVersion, GitVersion.VersionCalculation.BaseVersion! baseVersion, GitVersion.IBranch! branch, GitVersion.Configuration.EffectiveConfiguration! configuration) -> void
745744
GitVersion.VersionCalculation.VersionCalculationModule
746745
GitVersion.VersionCalculation.VersionCalculationModule.RegisterTypes(Microsoft.Extensions.DependencyInjection.IServiceCollection! services) -> void
747746
GitVersion.VersionCalculation.VersionCalculationModule.VersionCalculationModule() -> void
@@ -808,11 +807,10 @@ readonly GitVersion.Agents.BuildAgentBase.Log -> GitVersion.Logging.ILog!
808807
static GitVersion.BranchCommit.operator !=(GitVersion.BranchCommit left, GitVersion.BranchCommit right) -> bool
809808
static GitVersion.BranchCommit.operator ==(GitVersion.BranchCommit left, GitVersion.BranchCommit right) -> bool
810809
static GitVersion.Configuration.ConfigurationExtensions.FindGitDir(this string! path) -> (string! GitDirectory, string! WorkingTreeDirectory)?
811-
static GitVersion.Configuration.ConfigurationExtensions.GetBranchConfiguration(this GitVersion.Configuration.IGitVersionConfiguration! configuration, GitVersion.IBranch! branch) -> GitVersion.Configuration.IBranchConfiguration!
812810
static GitVersion.Configuration.ConfigurationExtensions.GetBranchConfiguration(this GitVersion.Configuration.IGitVersionConfiguration! configuration, GitVersion.ReferenceName! branchName) -> GitVersion.Configuration.IBranchConfiguration!
813811
static GitVersion.Configuration.ConfigurationExtensions.GetBranchSpecificLabel(this GitVersion.Configuration.EffectiveConfiguration! configuration, GitVersion.ReferenceName! branchName, string? branchNameOverride) -> string?
814812
static GitVersion.Configuration.ConfigurationExtensions.GetBranchSpecificLabel(this GitVersion.Configuration.EffectiveConfiguration! configuration, string? branchName, string? branchNameOverride) -> string?
815-
static GitVersion.Configuration.ConfigurationExtensions.GetEffectiveConfiguration(this GitVersion.Configuration.IGitVersionConfiguration! configuration, GitVersion.IBranch! branch) -> GitVersion.Configuration.EffectiveConfiguration!
813+
static GitVersion.Configuration.ConfigurationExtensions.GetEffectiveBranchConfiguration(this GitVersion.Configuration.IGitVersionConfiguration! configuration, GitVersion.IBranch! branch) -> GitVersion.Configuration.EffectiveBranchConfiguration!
816814
static GitVersion.Configuration.ConfigurationExtensions.GetEffectiveConfiguration(this GitVersion.Configuration.IGitVersionConfiguration! configuration, GitVersion.ReferenceName! branchName) -> GitVersion.Configuration.EffectiveConfiguration!
817815
static GitVersion.Configuration.ConfigurationExtensions.GetFallbackBranchConfiguration(this GitVersion.Configuration.IGitVersionConfiguration! configuration) -> GitVersion.Configuration.IBranchConfiguration!
818816
static GitVersion.Configuration.ConfigurationExtensions.GetReleaseBranchConfiguration(this GitVersion.Configuration.IGitVersionConfiguration! configuration) -> System.Collections.Generic.List<System.Collections.Generic.KeyValuePair<string!, GitVersion.Configuration.IBranchConfiguration!>>!

src/GitVersion.Core/VersionCalculation/EffectiveBranchConfigurationFinder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ private IEnumerable<EffectiveBranchConfiguration> GetEffectiveConfigurationsRecu
2929
{
3030
if (!traversedBranches.Add(branch)) yield break; // This should never happen!! But it is good to have a circuit breaker.
3131

32-
var branchConfiguration = configuration.GetBranchConfiguration(branch);
32+
var branchConfiguration = configuration.GetBranchConfiguration(branch.Name);
3333
if (childBranchConfiguration != null)
3434
{
3535
branchConfiguration = childBranchConfiguration.Inherit(branchConfiguration);
@@ -66,7 +66,7 @@ in GetEffectiveConfigurationsRecursive(sourceBranch, configuration, branchConfig
6666
}
6767
else
6868
{
69-
yield return new(branch, new EffectiveConfiguration(configuration, branchConfiguration));
69+
yield return new(new(configuration, branchConfiguration), branch);
7070
}
7171
}
7272
}

0 commit comments

Comments
 (0)