Skip to content

Commit 70b79f9

Browse files
committed
Make the result of TaggedSemanticVersionRepository methods distinct.
1 parent 86d5b43 commit 70b79f9

File tree

4 files changed

+51
-52
lines changed

4 files changed

+51
-52
lines changed

src/GitVersion.Core/Core/ITaggedSemanticVersionRepository.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ namespace GitVersion.Core;
44

55
internal interface ITaggedSemanticVersionRepository
66
{
7-
ILookup<ICommit, SemanticVersionWithTag> GetTaggedSemanticVersions(IBranch branch, EffectiveConfiguration configuration);
8-
9-
ILookup<ICommit, SemanticVersionWithTag> GetAllTaggedSemanticVersions(string? tagPrefix, SemanticVersionFormat format);
7+
ILookup<ICommit, SemanticVersionWithTag> GetAllTaggedSemanticVersions(IBranch branch, EffectiveConfiguration configuration);
108

119
ILookup<ICommit, SemanticVersionWithTag> GetTaggedSemanticVersionsOfBranch(
1210
IBranch branch,

src/GitVersion.Core/Core/TaggedSemanticVersionService.cs renamed to src/GitVersion.Core/Core/TaggedSemanticVersionRepository.cs

Lines changed: 43 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public TaggedSemanticVersionRepository(ILog log, Lazy<GitVersionContext> version
2424
this.branchRepository = branchRepository.NotNull();
2525
}
2626

27-
public ILookup<ICommit, SemanticVersionWithTag> GetTaggedSemanticVersions(IBranch branch, EffectiveConfiguration configuration)
27+
public ILookup<ICommit, SemanticVersionWithTag> GetAllTaggedSemanticVersions(IBranch branch, EffectiveConfiguration configuration)
2828
{
2929
configuration.NotNull();
3030

@@ -102,42 +102,7 @@ public ILookup<ICommit, SemanticVersionWithTag> GetTaggedSemanticVersions(IBranc
102102
}
103103
}
104104

105-
return GetElements().ToLookup(element => element.Key, element => element.Value);
106-
}
107-
108-
private readonly ConcurrentDictionary<(string, SemanticVersionFormat), ILookup<ICommit, SemanticVersionWithTag>>
109-
allTaggedSemanticVersionsCache = new();
110-
111-
public ILookup<ICommit, SemanticVersionWithTag> GetAllTaggedSemanticVersions(string? tagPrefix, SemanticVersionFormat format)
112-
{
113-
tagPrefix ??= string.Empty;
114-
115-
IEnumerable<SemanticVersionWithTag> GetElements()
116-
{
117-
this.log.Info($"Getting tagged semantic versions. TagPrefix: {tagPrefix} and Format: {format}");
118-
119-
foreach (var tag in this.gitRepository.Tags)
120-
{
121-
if (SemanticVersion.TryParse(tag.Name.Friendly, tagPrefix, out var semanticVersion, format))
122-
{
123-
yield return new SemanticVersionWithTag(semanticVersion, tag);
124-
}
125-
}
126-
}
127-
128-
bool isCached = true;
129-
var result = allTaggedSemanticVersionsCache.GetOrAdd(new(tagPrefix, format), _ =>
130-
{
131-
isCached = false;
132-
return GetElements().ToLookup(element => element.Tag.Commit, element => element);
133-
});
134-
135-
if (isCached)
136-
{
137-
this.log.Debug($"Returning cached tagged semantic versions. TagPrefix: {tagPrefix} and Format: {format}");
138-
}
139-
140-
return result;
105+
return GetElements().Distinct().ToLookup(element => element.Key, element => element.Value);
141106
}
142107

143108
private readonly ConcurrentDictionary<(IBranch, string, SemanticVersionFormat), ILookup<ICommit, SemanticVersionWithTag>>
@@ -154,8 +119,7 @@ IEnumerable<SemanticVersionWithTag> GetElements()
154119
using (this.log.IndentLog($"Getting tagged semantic versions on branch '{branch.Name.Canonical}'. " +
155120
$"TagPrefix: {tagPrefix} and Format: {format}"))
156121
{
157-
var semanticVersions = GetAllTaggedSemanticVersions(tagPrefix, format);
158-
122+
var semanticVersions = GetTaggedSemanticVersions(tagPrefix, format);
159123
foreach (var commit in branch.Commits)
160124
{
161125
foreach (var semanticVersion in semanticVersions[commit])
@@ -170,8 +134,7 @@ IEnumerable<SemanticVersionWithTag> GetElements()
170134
var result = taggedSemanticVersionsOfBranchCache.GetOrAdd(new(branch, tagPrefix, format), _ =>
171135
{
172136
isCached = false;
173-
var semanticVersions = GetElements();
174-
return semanticVersions.ToLookup(element => element.Tag.Commit, element => element);
137+
return GetElements().Distinct().ToLookup(element => element.Tag.Commit, element => element);
175138
});
176139

177140
if (isCached)
@@ -201,7 +164,7 @@ public ILookup<ICommit, SemanticVersionWithTag> GetTaggedSemanticVersionsOfMerge
201164
{
202165
var shaHashSet = new HashSet<string>(branch.Commits.Select(element => element.Id.Sha));
203166

204-
foreach (var semanticVersion in GetAllTaggedSemanticVersions(tagPrefix, format).SelectMany(_ => _))
167+
foreach (var semanticVersion in GetTaggedSemanticVersions(tagPrefix, format).SelectMany(_ => _))
205168
{
206169
foreach (var commit in semanticVersion.Tag.Commit.Parents.Where(element => shaHashSet.Contains(element.Id.Sha)))
207170
{
@@ -215,7 +178,7 @@ public ILookup<ICommit, SemanticVersionWithTag> GetTaggedSemanticVersionsOfMerge
215178
var result = taggedSemanticVersionsOfMergeTargetCache.GetOrAdd(new(branch, tagPrefix, format), _ =>
216179
{
217180
isCached = false;
218-
return GetElements().ToLookup(element => element.Item1, element => element.Item2);
181+
return GetElements().Distinct().ToLookup(element => element.Item1, element => element.Item2);
219182
});
220183

221184
if (isCached)
@@ -250,7 +213,7 @@ IEnumerable<SemanticVersionWithTag> GetElements()
250213
}
251214
}
252215

253-
return GetElements().ToLookup(element => element.Tag.Commit, element => element);
216+
return GetElements().Distinct().ToLookup(element => element.Tag.Commit, element => element);
254217
}
255218

256219
public ILookup<ICommit, SemanticVersionWithTag> GetTaggedSemanticVersionsOfReleaseBranches(
@@ -274,6 +237,41 @@ IEnumerable<SemanticVersionWithTag> GetElements()
274237
}
275238
}
276239

277-
return GetElements().ToLookup(element => element.Tag.Commit, element => element);
240+
return GetElements().Distinct().ToLookup(element => element.Tag.Commit, element => element);
241+
}
242+
243+
private readonly ConcurrentDictionary<(string, SemanticVersionFormat), ILookup<ICommit, SemanticVersionWithTag>>
244+
taggedSemanticVersionsCache = new();
245+
246+
private ILookup<ICommit, SemanticVersionWithTag> GetTaggedSemanticVersions(string? tagPrefix, SemanticVersionFormat format)
247+
{
248+
tagPrefix ??= string.Empty;
249+
250+
IEnumerable<SemanticVersionWithTag> GetElements()
251+
{
252+
this.log.Info($"Getting tagged semantic versions. TagPrefix: {tagPrefix} and Format: {format}");
253+
254+
foreach (var tag in this.gitRepository.Tags)
255+
{
256+
if (SemanticVersion.TryParse(tag.Name.Friendly, tagPrefix, out var semanticVersion, format))
257+
{
258+
yield return new SemanticVersionWithTag(semanticVersion, tag);
259+
}
260+
}
261+
}
262+
263+
bool isCached = true;
264+
var result = taggedSemanticVersionsCache.GetOrAdd(new(tagPrefix, format), _ =>
265+
{
266+
isCached = false;
267+
return GetElements().ToLookup(element => element.Tag.Commit, element => element);
268+
});
269+
270+
if (isCached)
271+
{
272+
this.log.Debug($"Returning cached tagged semantic versions. TagPrefix: {tagPrefix} and Format: {format}");
273+
}
274+
275+
return result;
278276
}
279277
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@ private IEnumerable<SemanticVersionWithTag> GetTaggedSemanticVersions(EffectiveB
2525
configuration.NotNull();
2626

2727
var label = configuration.Value.GetBranchSpecificLabel(Context.CurrentBranch.Name, null);
28-
foreach (var semanticVersion in taggedSemanticVersionRepository
29-
.GetTaggedSemanticVersions(Context.CurrentBranch, configuration.Value).SelectMany(element => element))
28+
var taggedSemanticVersions = taggedSemanticVersionRepository
29+
.GetAllTaggedSemanticVersions(Context.CurrentBranch, configuration.Value).SelectMany(element => element)
30+
.Distinct().ToArray();
31+
32+
foreach (var semanticVersion in taggedSemanticVersions)
3033
{
3134
if (semanticVersion.Value.IsMatchForBranchSpecificLabel(label))
3235
{

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public override IEnumerable<BaseVersion> GetBaseVersions(EffectiveBranchConfigur
3434

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

37-
var taggedSemanticVersions = TaggedSemanticVersionRepository.GetTaggedSemanticVersions(
37+
var taggedSemanticVersions = TaggedSemanticVersionRepository.GetAllTaggedSemanticVersions(
3838
Context.CurrentBranch, configuration.Value
3939
);
4040

@@ -83,7 +83,7 @@ private bool IterateOverCommitsRecursive(
8383
{
8484
configuration = effectiveConfigurationWasBranchedFrom.Value;
8585
branchName = effectiveConfigurationWasBranchedFrom.Branch.Name;
86-
taggedSemanticVersions = TaggedSemanticVersionRepository.GetTaggedSemanticVersions(
86+
taggedSemanticVersions = TaggedSemanticVersionRepository.GetAllTaggedSemanticVersions(
8787
effectiveConfigurationWasBranchedFrom.Branch, effectiveConfigurationWasBranchedFrom.Value
8888
);
8989
}

0 commit comments

Comments
 (0)