Skip to content

Commit 8bf4cac

Browse files
authored
Merge pull request #4003 from HHobeck/feature/create-tagged-semantic-version-service
Create TaggedSemanticVersionService class
2 parents 6012da7 + 77523fe commit 8bf4cac

13 files changed

+409
-219
lines changed

src/GitVersion.Core.Tests/Extensions/GitToolsTestingExtensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ public static GitVersionVariables GetVersion(this RepositoryFixtureBase fixture,
6161
{
6262
repository ??= fixture.Repository;
6363
configuration ??= GitFlowConfigurationBuilder.New.Build();
64-
Console.WriteLine("---------");
6564

6665
var overrideConfiguration = new Dictionary<object, object?>();
6766
var options = Options.Create(new GitVersionOptions

src/GitVersion.Core.Tests/IntegrationTests/FallbackVersionStrategyScenarios.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,16 @@ public void TakeTheLatestCommitAsBaseVersion(bool mode)
9494
fixture.AssertFullSemver("1.0.0-1+1", configuration);
9595
}
9696

97-
[Test]
98-
public void TakeTheCommitBranchedFromAsBaseVersion()
97+
[TestCase(false, "1.0.0-1+4")]
98+
[TestCase(true, "1.0.0-1+2")]
99+
public void TakeTheCommitBranchedFromAsBaseVersionWhenTracksReleaseBranchesIsTrue(
100+
bool tracksReleaseBranches, string version)
99101
{
100102
var configuration = ConfigurationBuilder
101103
.WithBranch("main", _ => _
102104
.WithIncrement(IncrementStrategy.Major)
103105
.WithTrackMergeTarget(false)
104-
.WithTracksReleaseBranches(true)
106+
.WithTracksReleaseBranches(tracksReleaseBranches)
105107
).Build();
106108

107109
using EmptyRepositoryFixture fixture = new("main");
@@ -118,7 +120,7 @@ public void TakeTheCommitBranchedFromAsBaseVersion()
118120
fixture.MakeACommit("D");
119121

120122
// ✅ succeeds as expected
121-
fixture.AssertFullSemver("1.0.0-1+2", configuration);
123+
fixture.AssertFullSemver(version, configuration);
122124

123125
fixture.Repository.DumpGraph();
124126
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using GitVersion.Configuration;
2+
using GitVersion.Extensions;
3+
4+
namespace GitVersion.Core;
5+
6+
internal static class EffectiveConfigurationExtensions
7+
{
8+
public static TaggedSemanticVersions GetTaggedSemanticVersion(this EffectiveConfiguration effectiveConfiguration)
9+
{
10+
effectiveConfiguration.NotNull();
11+
12+
TaggedSemanticVersions taggedSemanticVersion = TaggedSemanticVersions.OfBranch;
13+
14+
if (effectiveConfiguration.TrackMergeTarget)
15+
{
16+
taggedSemanticVersion |= TaggedSemanticVersions.OfMergeTargets;
17+
}
18+
19+
if (effectiveConfiguration.TracksReleaseBranches)
20+
{
21+
taggedSemanticVersion |= TaggedSemanticVersions.OfReleaseBranches;
22+
}
23+
24+
if (!effectiveConfiguration.IsMainBranch && !effectiveConfiguration.IsReleaseBranch)
25+
{
26+
taggedSemanticVersion |= TaggedSemanticVersions.OfMainBranches;
27+
}
28+
return taggedSemanticVersion;
29+
}
30+
}

src/GitVersion.Core/Core/ITaggedSemanticVersionRepository.cs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,6 @@ namespace GitVersion.Core;
55

66
internal interface ITaggedSemanticVersionRepository
77
{
8-
ILookup<ICommit, SemanticVersionWithTag> GetAllTaggedSemanticVersions(
9-
IGitVersionConfiguration configuration,
10-
EffectiveConfiguration effectiveConfiguration,
11-
IBranch branch,
12-
string? label,
13-
DateTimeOffset? notOlderThan);
14-
158
ILookup<ICommit, SemanticVersionWithTag> GetTaggedSemanticVersionsOfBranch(
169
IBranch branch,
1710
string? tagPrefix,
@@ -24,18 +17,6 @@ ILookup<ICommit, SemanticVersionWithTag> GetTaggedSemanticVersionsOfMergeTarget(
2417
SemanticVersionFormat format,
2518
IIgnoreConfiguration ignore);
2619

27-
ILookup<ICommit, SemanticVersionWithTag> GetTaggedSemanticVersionsOfMainBranches(
28-
IGitVersionConfiguration configuration,
29-
string? tagPrefix,
30-
SemanticVersionFormat format,
31-
params IBranch[] excludeBranches);
32-
33-
ILookup<ICommit, SemanticVersionWithTag> GetTaggedSemanticVersionsOfReleaseBranches(
34-
IGitVersionConfiguration configuration,
35-
string? tagPrefix,
36-
SemanticVersionFormat format,
37-
params IBranch[] excludeBranches);
38-
3920
ILookup<ICommit, SemanticVersionWithTag> GetTaggedSemanticVersions(
4021
string? tagPrefix, SemanticVersionFormat format, IIgnoreConfiguration ignore);
4122
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using GitVersion.Configuration;
2+
using GitVersion.Git;
3+
4+
namespace GitVersion.Core;
5+
6+
internal interface ITaggedSemanticVersionService
7+
{
8+
ILookup<ICommit, SemanticVersionWithTag> GetTaggedSemanticVersions(
9+
IBranch branch,
10+
IGitVersionConfiguration configuration,
11+
string? label,
12+
DateTimeOffset? notOlderThan,
13+
TaggedSemanticVersions taggedSemanticVersion);
14+
15+
ILookup<ICommit, SemanticVersionWithTag> GetTaggedSemanticVersionsOfBranch(
16+
IBranch branch,
17+
string? tagPrefix,
18+
SemanticVersionFormat format,
19+
IIgnoreConfiguration ignore,
20+
string? label = null,
21+
DateTimeOffset? notOlderThan = null);
22+
23+
ILookup<ICommit, SemanticVersionWithTag> GetTaggedSemanticVersionsOfMergeTarget(
24+
IBranch branch,
25+
string? tagPrefix,
26+
SemanticVersionFormat format,
27+
IIgnoreConfiguration ignore,
28+
string? label = null,
29+
DateTimeOffset? notOlderThan = null);
30+
31+
ILookup<ICommit, SemanticVersionWithTag> GetTaggedSemanticVersionsOfMainBranches(
32+
IGitVersionConfiguration configuration,
33+
DateTimeOffset? notOlderThan = null,
34+
string? label = null,
35+
params IBranch[] excludeBranches);
36+
37+
ILookup<ICommit, SemanticVersionWithTag> GetTaggedSemanticVersionsOfReleaseBranches(
38+
IGitVersionConfiguration configuration,
39+
DateTimeOffset? notOlderThan = null,
40+
string? label = null,
41+
params IBranch[] excludeBranches);
42+
}

src/GitVersion.Core/Core/TaggedSemanticVersionRepository.cs

Lines changed: 10 additions & 170 deletions
Original file line numberDiff line numberDiff line change
@@ -6,122 +6,17 @@
66

77
namespace GitVersion.Core;
88

9-
internal sealed class TaggedSemanticVersionRepository(
10-
ILog log,
11-
IGitRepository gitRepository,
12-
IBranchRepository branchRepository)
13-
: ITaggedSemanticVersionRepository
9+
internal sealed class TaggedSemanticVersionRepository(ILog log, IGitRepository gitRepository) : ITaggedSemanticVersionRepository
1410
{
15-
private readonly ConcurrentDictionary<(IBranch, string, SemanticVersionFormat), ILookup<ICommit, SemanticVersionWithTag>>
11+
private readonly ConcurrentDictionary<(IBranch, string, SemanticVersionFormat), IReadOnlyList<SemanticVersionWithTag>>
1612
taggedSemanticVersionsOfBranchCache = new();
17-
private readonly ConcurrentDictionary<(IBranch, string, SemanticVersionFormat), ILookup<ICommit, SemanticVersionWithTag>>
13+
private readonly ConcurrentDictionary<(IBranch, string, SemanticVersionFormat), IReadOnlyList<(ICommit Key, SemanticVersionWithTag Value)>>
1814
taggedSemanticVersionsOfMergeTargetCache = new();
19-
private readonly ConcurrentDictionary<(string, SemanticVersionFormat), ILookup<ICommit, SemanticVersionWithTag>>
15+
private readonly ConcurrentDictionary<(string, SemanticVersionFormat), IReadOnlyList<SemanticVersionWithTag>>
2016
taggedSemanticVersionsCache = new();
2117
private readonly ILog log = log.NotNull();
2218

2319
private readonly IGitRepository gitRepository = gitRepository.NotNull();
24-
private readonly IBranchRepository branchRepository = branchRepository.NotNull();
25-
26-
public ILookup<ICommit, SemanticVersionWithTag> GetAllTaggedSemanticVersions(
27-
IGitVersionConfiguration configuration, EffectiveConfiguration effectiveConfiguration,
28-
IBranch branch, string? label, DateTimeOffset? notOlderThan)
29-
{
30-
configuration.NotNull();
31-
effectiveConfiguration.NotNull();
32-
branch.NotNull();
33-
34-
IEnumerable<(ICommit Key, SemanticVersionWithTag Value)> GetElements()
35-
{
36-
var semanticVersionsOfBranch = GetTaggedSemanticVersionsOfBranch(
37-
branch: branch,
38-
tagPrefix: effectiveConfiguration.TagPrefix,
39-
format: effectiveConfiguration.SemanticVersionFormat,
40-
ignore: configuration.Ignore
41-
);
42-
foreach (var grouping in semanticVersionsOfBranch)
43-
{
44-
if (grouping.Key.When > notOlderThan) continue;
45-
46-
foreach (var semanticVersion in grouping)
47-
{
48-
if (semanticVersion.Value.IsMatchForBranchSpecificLabel(label))
49-
{
50-
yield return new(grouping.Key, semanticVersion);
51-
}
52-
}
53-
}
54-
55-
if (effectiveConfiguration.TrackMergeTarget)
56-
{
57-
var semanticVersionsOfMergeTarget = GetTaggedSemanticVersionsOfMergeTarget(
58-
branch: branch,
59-
tagPrefix: effectiveConfiguration.TagPrefix,
60-
format: effectiveConfiguration.SemanticVersionFormat,
61-
ignore: configuration.Ignore
62-
);
63-
foreach (var grouping in semanticVersionsOfMergeTarget)
64-
{
65-
if (grouping.Key.When > notOlderThan) continue;
66-
67-
foreach (var semanticVersion in grouping)
68-
{
69-
if (semanticVersion.Value.IsMatchForBranchSpecificLabel(label))
70-
{
71-
yield return new(grouping.Key, semanticVersion);
72-
}
73-
}
74-
}
75-
}
76-
77-
if (effectiveConfiguration.TracksReleaseBranches)
78-
{
79-
var semanticVersionsOfReleaseBranches = GetTaggedSemanticVersionsOfReleaseBranches(
80-
configuration: configuration,
81-
tagPrefix: effectiveConfiguration.TagPrefix,
82-
format: effectiveConfiguration.SemanticVersionFormat,
83-
excludeBranches: branch
84-
);
85-
foreach (var grouping in semanticVersionsOfReleaseBranches)
86-
{
87-
if (grouping.Key.When > notOlderThan) continue;
88-
89-
foreach (var semanticVersion in grouping)
90-
{
91-
if (semanticVersion.Value.IsMatchForBranchSpecificLabel(label))
92-
{
93-
yield return new(grouping.Key, semanticVersion);
94-
}
95-
}
96-
}
97-
}
98-
99-
if (!effectiveConfiguration.IsMainBranch && !effectiveConfiguration.IsReleaseBranch)
100-
{
101-
var semanticVersionsOfMainlineBranches = GetTaggedSemanticVersionsOfMainBranches(
102-
configuration: configuration,
103-
tagPrefix: effectiveConfiguration.TagPrefix,
104-
format: effectiveConfiguration.SemanticVersionFormat,
105-
excludeBranches: branch
106-
);
107-
foreach (var grouping in semanticVersionsOfMainlineBranches)
108-
{
109-
if (grouping.Key.When > notOlderThan) continue;
110-
111-
foreach (var semanticVersion in grouping)
112-
{
113-
if (semanticVersion.Value.IsMatchForBranchSpecificLabel(label))
114-
{
115-
yield return new(grouping.Key, semanticVersion);
116-
}
117-
}
118-
}
119-
}
120-
}
121-
122-
return GetElements().Distinct().OrderByDescending(element => element.Key.When)
123-
.ToLookup(element => element.Key, element => element.Value);
124-
}
12520

12621
public ILookup<ICommit, SemanticVersionWithTag> GetTaggedSemanticVersionsOfBranch(
12722
IBranch branch, string? tagPrefix, SemanticVersionFormat format, IIgnoreConfiguration ignore)
@@ -150,8 +45,7 @@ IEnumerable<SemanticVersionWithTag> GetElements()
15045
var result = taggedSemanticVersionsOfBranchCache.GetOrAdd(new(branch, tagPrefix, format), _ =>
15146
{
15247
isCached = false;
153-
return GetElements().Distinct().OrderByDescending(element => element.Tag.Commit.When)
154-
.ToLookup(element => element.Tag.Commit, element => element);
48+
return GetElements().Distinct().OrderByDescending(element => element.Tag.Commit.When).ToList();
15549
});
15650

15751
if (isCached)
@@ -162,7 +56,7 @@ IEnumerable<SemanticVersionWithTag> GetElements()
16256
);
16357
}
16458

165-
return result;
59+
return result.ToLookup(element => element.Tag.Commit, element => element);
16660
}
16761

16862
public ILookup<ICommit, SemanticVersionWithTag> GetTaggedSemanticVersionsOfMergeTarget(
@@ -192,8 +86,7 @@ public ILookup<ICommit, SemanticVersionWithTag> GetTaggedSemanticVersionsOfMerge
19286
var result = taggedSemanticVersionsOfMergeTargetCache.GetOrAdd(new(branch, tagPrefix, format), _ =>
19387
{
19488
isCached = false;
195-
return GetElements().Distinct().OrderByDescending(element => element.Key.When)
196-
.ToLookup(element => element.Key, element => element.Value);
89+
return GetElements().Distinct().OrderByDescending(element => element.Key.When).ToList();
19790
});
19891

19992
if (isCached)
@@ -204,59 +97,7 @@ public ILookup<ICommit, SemanticVersionWithTag> GetTaggedSemanticVersionsOfMerge
20497
);
20598
}
20699

207-
return result;
208-
}
209-
210-
public ILookup<ICommit, SemanticVersionWithTag> GetTaggedSemanticVersionsOfMainBranches(
211-
IGitVersionConfiguration configuration, string? tagPrefix, SemanticVersionFormat format, params IBranch[] excludeBranches)
212-
{
213-
configuration.NotNull();
214-
tagPrefix ??= string.Empty;
215-
excludeBranches.NotNull();
216-
217-
IEnumerable<SemanticVersionWithTag> GetElements()
218-
{
219-
using (this.log.IndentLog($"Getting tagged semantic versions of mainline branches. " +
220-
$"TagPrefix: {tagPrefix} and Format: {format}"))
221-
{
222-
foreach (var mainlinemBranch in branchRepository.GetMainBranches(configuration, excludeBranches))
223-
{
224-
foreach (var semanticVersion in GetTaggedSemanticVersionsOfBranch(mainlinemBranch, tagPrefix, format, configuration.Ignore).SelectMany(_ => _))
225-
{
226-
yield return semanticVersion;
227-
}
228-
}
229-
}
230-
}
231-
232-
return GetElements().Distinct().OrderByDescending(element => element.Tag.Commit.When)
233-
.ToLookup(element => element.Tag.Commit, element => element);
234-
}
235-
236-
public ILookup<ICommit, SemanticVersionWithTag> GetTaggedSemanticVersionsOfReleaseBranches(
237-
IGitVersionConfiguration configuration, string? tagPrefix, SemanticVersionFormat format, params IBranch[] excludeBranches)
238-
{
239-
configuration.NotNull();
240-
tagPrefix ??= string.Empty;
241-
excludeBranches.NotNull();
242-
243-
IEnumerable<SemanticVersionWithTag> GetElements()
244-
{
245-
using (this.log.IndentLog($"Getting tagged semantic versions of release branches. " +
246-
$"TagPrefix: {tagPrefix} and Format: {format}"))
247-
{
248-
foreach (var releaseBranch in branchRepository.GetReleaseBranches(configuration, excludeBranches))
249-
{
250-
foreach (var semanticVersion in GetTaggedSemanticVersionsOfBranch(releaseBranch, tagPrefix, format, configuration.Ignore).SelectMany(_ => _))
251-
{
252-
yield return semanticVersion;
253-
}
254-
}
255-
}
256-
}
257-
258-
return GetElements().Distinct().OrderByDescending(element => element.Tag.Commit.When)
259-
.ToLookup(element => element.Tag.Commit, element => element);
100+
return result.ToLookup(element => element.Key, element => element.Value);
260101
}
261102

262103
public ILookup<ICommit, SemanticVersionWithTag> GetTaggedSemanticVersions(
@@ -281,15 +122,14 @@ IEnumerable<SemanticVersionWithTag> GetElements()
281122
var result = taggedSemanticVersionsCache.GetOrAdd(new(tagPrefix, format), _ =>
282123
{
283124
isCached = false;
284-
return GetElements().OrderByDescending(element => element.Tag.Commit.When)
285-
.ToLookup(element => element.Tag.Commit, element => element);
125+
return GetElements().OrderByDescending(element => element.Tag.Commit.When).ToList();
286126
});
287127

288128
if (isCached)
289129
{
290130
this.log.Debug($"Returning cached tagged semantic versions. TagPrefix: {tagPrefix} and Format: {format}");
291131
}
292132

293-
return result;
133+
return result.ToLookup(element => element.Tag.Commit, element => element);
294134
}
295135
}

0 commit comments

Comments
 (0)