Skip to content

Commit 37c67fe

Browse files
committed
Implement code review notes and move all readonly field to the beginning of the class definition.
1 parent 70b79f9 commit 37c67fe

File tree

2 files changed

+62
-67
lines changed

2 files changed

+62
-67
lines changed

src/GitVersion.Core/Core/TaggedSemanticVersionRepository.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ namespace GitVersion.Core;
77

88
internal sealed class TaggedSemanticVersionRepository : ITaggedSemanticVersionRepository
99
{
10+
private readonly ConcurrentDictionary<(IBranch, string, SemanticVersionFormat), ILookup<ICommit, SemanticVersionWithTag>>
11+
taggedSemanticVersionsOfBranchCache = new();
12+
private readonly ConcurrentDictionary<(IBranch, string, SemanticVersionFormat), ILookup<ICommit, SemanticVersionWithTag>>
13+
taggedSemanticVersionsOfMergeTargetCache = new();
14+
private readonly ConcurrentDictionary<(string, SemanticVersionFormat), ILookup<ICommit, SemanticVersionWithTag>>
15+
taggedSemanticVersionsCache = new();
1016
private readonly ILog log;
1117

1218
private GitVersionContext VersionContext => this.versionContextLazy.Value;
@@ -105,9 +111,6 @@ public ILookup<ICommit, SemanticVersionWithTag> GetAllTaggedSemanticVersions(IBr
105111
return GetElements().Distinct().ToLookup(element => element.Key, element => element.Value);
106112
}
107113

108-
private readonly ConcurrentDictionary<(IBranch, string, SemanticVersionFormat), ILookup<ICommit, SemanticVersionWithTag>>
109-
taggedSemanticVersionsOfBranchCache = new();
110-
111114
public ILookup<ICommit, SemanticVersionWithTag> GetTaggedSemanticVersionsOfBranch(
112115
IBranch branch, string? tagPrefix, SemanticVersionFormat format)
113116
{
@@ -148,9 +151,6 @@ IEnumerable<SemanticVersionWithTag> GetElements()
148151
return result;
149152
}
150153

151-
private readonly ConcurrentDictionary<(IBranch, string, SemanticVersionFormat), ILookup<ICommit, SemanticVersionWithTag>>
152-
taggedSemanticVersionsOfMergeTargetCache = new();
153-
154154
public ILookup<ICommit, SemanticVersionWithTag> GetTaggedSemanticVersionsOfMergeTarget(
155155
IBranch branch, string? tagPrefix, SemanticVersionFormat format)
156156
{
@@ -240,9 +240,6 @@ IEnumerable<SemanticVersionWithTag> GetElements()
240240
return GetElements().Distinct().ToLookup(element => element.Tag.Commit, element => element);
241241
}
242242

243-
private readonly ConcurrentDictionary<(string, SemanticVersionFormat), ILookup<ICommit, SemanticVersionWithTag>>
244-
taggedSemanticVersionsCache = new();
245-
246243
private ILookup<ICommit, SemanticVersionWithTag> GetTaggedSemanticVersions(string? tagPrefix, SemanticVersionFormat format)
247244
{
248245
tagPrefix ??= string.Empty;

src/GitVersion.Core/VersionCalculation/VersionSearchStrategies/TrunkBasedVersionStrategy.cs

Lines changed: 56 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,61 @@ namespace GitVersion.VersionCalculation;
1111

1212
internal sealed class TrunkBasedVersionStrategy : VersionStrategyBase
1313
{
14-
private volatile int IterationCounter;
14+
private static readonly IReadOnlyCollection<ITrunkBasedContextPreEnricher> TrunkContextPreEnricherCollection = new ITrunkBasedContextPreEnricher[]
15+
{
16+
new EnrichSemanticVersion(),
17+
new EnrichIncrement()
18+
};
19+
private static readonly IReadOnlyCollection<ITrunkBasedContextPostEnricher> TrunkContextPostEnricherCollection = new ITrunkBasedContextPostEnricher[]
20+
{
21+
new RemoveSemanticVersion(),
22+
new RemoveIncrement()
23+
};
24+
private static readonly IReadOnlyCollection<ITrunkBasedIncrementer> TrunkIncrementerCollection = new ITrunkBasedIncrementer[]
25+
{
26+
// Trunk
27+
new CommitOnTrunk(),
1528

16-
private ITaggedSemanticVersionRepository TaggedSemanticVersionRepository { get; }
29+
new CommitOnTrunkWithPreReleaseTag(),
30+
new LastCommitOnTrunkWithPreReleaseTag(),
1731

18-
private IRepositoryStore RepositoryStore { get; }
32+
new CommitOnTrunkWithStableTag(),
33+
new LastCommitOnTrunkWithStableTag(),
1934

20-
private IIncrementStrategyFinder IncrementStrategyFinder { get; }
35+
new MergeCommitOnTrunk(),
36+
new LastMergeCommitOnTrunk(),
37+
38+
new CommitOnTrunkBranchedToTrunk(),
39+
new CommitOnTrunkBranchedToNonTrunk(),
40+
41+
// NonTrunk
42+
new CommitOnNonTrunk(),
43+
new CommitOnNonTrunkWithPreReleaseTag(),
44+
new LastCommitOnNonTrunkWithPreReleaseTag(),
45+
46+
new CommitOnNonTrunkWithStableTag(),
47+
new LastCommitOnNonTrunkWithStableTag(),
48+
49+
new MergeCommitOnNonTrunk(),
50+
new LastMergeCommitOnNonTrunk(),
51+
52+
new CommitOnNonTrunkBranchedToTrunk(),
53+
new CommitOnNonTrunkBranchedToNonTrunk()
54+
};
55+
56+
private volatile int iterationCounter;
57+
58+
private readonly ITaggedSemanticVersionRepository taggedSemanticVersionRepository;
59+
private readonly IRepositoryStore repositoryStore;
60+
private readonly IIncrementStrategyFinder incrementStrategyFinder;
2161

2262
public TrunkBasedVersionStrategy(Lazy<GitVersionContext> context, IRepositoryStore repositoryStore,
2363
ITaggedSemanticVersionRepository taggedSemanticVersionRepository, IIncrementStrategyFinder incrementStrategyFinder
2464
) : base(context)
2565
{
26-
RepositoryStore = repositoryStore.NotNull();
27-
TaggedSemanticVersionRepository = taggedSemanticVersionRepository.NotNull();
28-
IncrementStrategyFinder = incrementStrategyFinder.NotNull();
66+
this.repositoryStore = repositoryStore.NotNull();
67+
this.taggedSemanticVersionRepository = taggedSemanticVersionRepository.NotNull();
68+
this.incrementStrategyFinder = incrementStrategyFinder.NotNull();
2969
}
3070

3171
public override IEnumerable<BaseVersion> GetBaseVersions(EffectiveBranchConfiguration configuration)
@@ -34,7 +74,7 @@ public override IEnumerable<BaseVersion> GetBaseVersions(EffectiveBranchConfigur
3474

3575
var iteration = CreateIteration(branchName: Context.CurrentBranch.Name, configuration: configuration.Value);
3676

37-
var taggedSemanticVersions = TaggedSemanticVersionRepository.GetAllTaggedSemanticVersions(
77+
var taggedSemanticVersions = taggedSemanticVersionRepository.GetAllTaggedSemanticVersions(
3878
Context.CurrentBranch, configuration.Value
3979
);
4080

@@ -52,7 +92,7 @@ public override IEnumerable<BaseVersion> GetBaseVersions(EffectiveBranchConfigur
5292
private TrunkBasedIteration CreateIteration(
5393
ReferenceName branchName, EffectiveConfiguration configuration, TrunkBasedIteration? parent = null)
5494
{
55-
var iterationCount = Interlocked.Increment(ref IterationCounter);
95+
var iterationCount = Interlocked.Increment(ref iterationCounter);
5696
return new TrunkBasedIteration(
5797
id: $"#{iterationCount}",
5898
branchName: branchName,
@@ -83,7 +123,7 @@ private bool IterateOverCommitsRecursive(
83123
{
84124
configuration = effectiveConfigurationWasBranchedFrom.Value;
85125
branchName = effectiveConfigurationWasBranchedFrom.Branch.Name;
86-
taggedSemanticVersions = TaggedSemanticVersionRepository.GetAllTaggedSemanticVersions(
126+
taggedSemanticVersions = taggedSemanticVersionRepository.GetAllTaggedSemanticVersions(
87127
effectiveConfigurationWasBranchedFrom.Branch, effectiveConfigurationWasBranchedFrom.Value
88128
);
89129
}
@@ -103,7 +143,7 @@ private bool IterateOverCommitsRecursive(
103143
if (item.IsMergeCommit)
104144
{
105145
Lazy<IReadOnlyCollection<ICommit>> mergedCommitsInReverseOrderLazy = new(
106-
() => IncrementStrategyFinder.GetMergedCommits(item, 1).Reverse().ToList()
146+
() => incrementStrategyFinder.GetMergedCommits(item, 1).Reverse().ToList()
107147
);
108148

109149
if (configuration.TrackMergeMessage
@@ -125,7 +165,7 @@ private bool IterateOverCommitsRecursive(
125165
{
126166
if (configuration.IsMainline) throw new NotImplementedException();
127167
mergedCommitsInReverseOrderLazy = new(
128-
() => IncrementStrategyFinder.GetMergedCommits(item, 0).Reverse().ToList()
168+
() => incrementStrategyFinder.GetMergedCommits(item, 0).Reverse().ToList()
129169
);
130170
childConfiguration = configuration;
131171
}
@@ -162,10 +202,10 @@ private VersionField GetIncrementForcedByCommit(ICommit commit, EffectiveConfigu
162202

163203
return configuration.CommitMessageIncrementing switch
164204
{
165-
CommitMessageIncrementMode.Enabled => IncrementStrategyFinder.GetIncrementForcedByCommit(commit, configuration),
205+
CommitMessageIncrementMode.Enabled => incrementStrategyFinder.GetIncrementForcedByCommit(commit, configuration),
166206
CommitMessageIncrementMode.Disabled => VersionField.None,
167207
CommitMessageIncrementMode.MergeMessageOnly => commit.IsMergeCommit
168-
? IncrementStrategyFinder.GetIncrementForcedByCommit(commit, configuration) : VersionField.None,
208+
? incrementStrategyFinder.GetIncrementForcedByCommit(commit, configuration) : VersionField.None,
169209
_ => throw new InvalidEnumArgumentException(
170210
nameof(configuration.CommitMessageIncrementing), (int)configuration.CommitMessageIncrementing, typeof(CommitMessageIncrementMode)
171211
)
@@ -176,10 +216,10 @@ private IReadOnlyDictionary<ICommit, EffectiveBranchConfiguration> GetCommitsWas
176216
{
177217
Dictionary<ICommit, EffectiveBranchConfiguration> result = new();
178218

179-
var branch = RepositoryStore.FindBranch(branchName);
219+
var branch = repositoryStore.FindBranch(branchName);
180220
if (branch is null) return result;
181221

182-
var branchCommits = RepositoryStore.FindCommitBranchesWasBranchedFrom(
222+
var branchCommits = repositoryStore.FindCommitBranchesWasBranchedFrom(
183223
branch, Context.Configuration
184224
).ToList();
185225

@@ -290,46 +330,4 @@ private static IEnumerable<BaseVersionV2> GetIncrementSteps(TrunkBasedIteration
290330
}
291331
}
292332
}
293-
294-
private static readonly IReadOnlyCollection<ITrunkBasedContextPreEnricher> TrunkContextPreEnricherCollection = new ITrunkBasedContextPreEnricher[]
295-
{
296-
new EnrichSemanticVersion(),
297-
new EnrichIncrement()
298-
};
299-
private static readonly IReadOnlyCollection<ITrunkBasedContextPostEnricher> TrunkContextPostEnricherCollection = new ITrunkBasedContextPostEnricher[]
300-
{
301-
new RemoveSemanticVersion(),
302-
new RemoveIncrement()
303-
};
304-
private static readonly IReadOnlyCollection<ITrunkBasedIncrementer> TrunkIncrementerCollection = new ITrunkBasedIncrementer[]
305-
{
306-
// Trunk
307-
new CommitOnTrunk(),
308-
309-
new CommitOnTrunkWithPreReleaseTag(),
310-
new LastCommitOnTrunkWithPreReleaseTag(),
311-
312-
new CommitOnTrunkWithStableTag(),
313-
new LastCommitOnTrunkWithStableTag(),
314-
315-
new MergeCommitOnTrunk(),
316-
new LastMergeCommitOnTrunk(),
317-
318-
new CommitOnTrunkBranchedToTrunk(),
319-
new CommitOnTrunkBranchedToNonTrunk(),
320-
321-
// NonTrunk
322-
new CommitOnNonTrunk(),
323-
new CommitOnNonTrunkWithPreReleaseTag(),
324-
new LastCommitOnNonTrunkWithPreReleaseTag(),
325-
326-
new CommitOnNonTrunkWithStableTag(),
327-
new LastCommitOnNonTrunkWithStableTag(),
328-
329-
new MergeCommitOnNonTrunk(),
330-
new LastMergeCommitOnNonTrunk(),
331-
332-
new CommitOnNonTrunkBranchedToTrunk(),
333-
new CommitOnNonTrunkBranchedToNonTrunk()
334-
};
335333
}

0 commit comments

Comments
 (0)