Skip to content

Create TaggedSemanticVersionService class #4003

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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 @@ -61,7 +61,6 @@ public static GitVersionVariables GetVersion(this RepositoryFixtureBase fixture,
{
repository ??= fixture.Repository;
configuration ??= GitFlowConfigurationBuilder.New.Build();
Console.WriteLine("---------");

var overrideConfiguration = new Dictionary<object, object?>();
var options = Options.Create(new GitVersionOptions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,16 @@ public void TakeTheLatestCommitAsBaseVersion(bool mode)
fixture.AssertFullSemver("1.0.0-1+1", configuration);
}

[Test]
public void TakeTheCommitBranchedFromAsBaseVersion()
[TestCase(false, "1.0.0-1+4")]
[TestCase(true, "1.0.0-1+2")]
public void TakeTheCommitBranchedFromAsBaseVersionWhenTracksReleaseBranchesIsTrue(
bool tracksReleaseBranches, string version)
{
var configuration = ConfigurationBuilder
.WithBranch("main", _ => _
.WithIncrement(IncrementStrategy.Major)
.WithTrackMergeTarget(false)
.WithTracksReleaseBranches(true)
.WithTracksReleaseBranches(tracksReleaseBranches)
).Build();

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

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

fixture.Repository.DumpGraph();
}
Expand Down
30 changes: 30 additions & 0 deletions src/GitVersion.Core/Core/EffectiveConfigurationExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using GitVersion.Configuration;
using GitVersion.Extensions;

namespace GitVersion.Core;

internal static class EffectiveConfigurationExtensions
{
public static TaggedSemanticVersions GetTaggedSemanticVersion(this EffectiveConfiguration effectiveConfiguration)
{
effectiveConfiguration.NotNull();

TaggedSemanticVersions taggedSemanticVersion = TaggedSemanticVersions.OfBranch;

if (effectiveConfiguration.TrackMergeTarget)
{
taggedSemanticVersion |= TaggedSemanticVersions.OfMergeTargets;
}

if (effectiveConfiguration.TracksReleaseBranches)
{
taggedSemanticVersion |= TaggedSemanticVersions.OfReleaseBranches;
}

if (!effectiveConfiguration.IsMainBranch && !effectiveConfiguration.IsReleaseBranch)
{
taggedSemanticVersion |= TaggedSemanticVersions.OfMainBranches;
}
return taggedSemanticVersion;
}
}
19 changes: 0 additions & 19 deletions src/GitVersion.Core/Core/ITaggedSemanticVersionRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@ namespace GitVersion.Core;

internal interface ITaggedSemanticVersionRepository
{
ILookup<ICommit, SemanticVersionWithTag> GetAllTaggedSemanticVersions(
IGitVersionConfiguration configuration,
EffectiveConfiguration effectiveConfiguration,
IBranch branch,
string? label,
DateTimeOffset? notOlderThan);

ILookup<ICommit, SemanticVersionWithTag> GetTaggedSemanticVersionsOfBranch(
IBranch branch,
string? tagPrefix,
Expand All @@ -24,18 +17,6 @@ ILookup<ICommit, SemanticVersionWithTag> GetTaggedSemanticVersionsOfMergeTarget(
SemanticVersionFormat format,
IIgnoreConfiguration ignore);

ILookup<ICommit, SemanticVersionWithTag> GetTaggedSemanticVersionsOfMainBranches(
IGitVersionConfiguration configuration,
string? tagPrefix,
SemanticVersionFormat format,
params IBranch[] excludeBranches);

ILookup<ICommit, SemanticVersionWithTag> GetTaggedSemanticVersionsOfReleaseBranches(
IGitVersionConfiguration configuration,
string? tagPrefix,
SemanticVersionFormat format,
params IBranch[] excludeBranches);

ILookup<ICommit, SemanticVersionWithTag> GetTaggedSemanticVersions(
string? tagPrefix, SemanticVersionFormat format, IIgnoreConfiguration ignore);
}
42 changes: 42 additions & 0 deletions src/GitVersion.Core/Core/ITaggedSemanticVersionService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using GitVersion.Configuration;
using GitVersion.Git;

namespace GitVersion.Core;

internal interface ITaggedSemanticVersionService
{
ILookup<ICommit, SemanticVersionWithTag> GetTaggedSemanticVersions(
IBranch branch,
IGitVersionConfiguration configuration,
string? label,
DateTimeOffset? notOlderThan,
TaggedSemanticVersions taggedSemanticVersion);

ILookup<ICommit, SemanticVersionWithTag> GetTaggedSemanticVersionsOfBranch(
IBranch branch,
string? tagPrefix,
SemanticVersionFormat format,
IIgnoreConfiguration ignore,
string? label = null,
DateTimeOffset? notOlderThan = null);

ILookup<ICommit, SemanticVersionWithTag> GetTaggedSemanticVersionsOfMergeTarget(
IBranch branch,
string? tagPrefix,
SemanticVersionFormat format,
IIgnoreConfiguration ignore,
string? label = null,
DateTimeOffset? notOlderThan = null);

ILookup<ICommit, SemanticVersionWithTag> GetTaggedSemanticVersionsOfMainBranches(
IGitVersionConfiguration configuration,
DateTimeOffset? notOlderThan = null,
string? label = null,
params IBranch[] excludeBranches);

ILookup<ICommit, SemanticVersionWithTag> GetTaggedSemanticVersionsOfReleaseBranches(
IGitVersionConfiguration configuration,
DateTimeOffset? notOlderThan = null,
string? label = null,
params IBranch[] excludeBranches);
}
180 changes: 10 additions & 170 deletions src/GitVersion.Core/Core/TaggedSemanticVersionRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,122 +6,17 @@

namespace GitVersion.Core;

internal sealed class TaggedSemanticVersionRepository(
ILog log,
IGitRepository gitRepository,
IBranchRepository branchRepository)
: ITaggedSemanticVersionRepository
internal sealed class TaggedSemanticVersionRepository(ILog log, IGitRepository gitRepository) : ITaggedSemanticVersionRepository
{
private readonly ConcurrentDictionary<(IBranch, string, SemanticVersionFormat), ILookup<ICommit, SemanticVersionWithTag>>
private readonly ConcurrentDictionary<(IBranch, string, SemanticVersionFormat), IReadOnlyList<SemanticVersionWithTag>>
taggedSemanticVersionsOfBranchCache = new();
private readonly ConcurrentDictionary<(IBranch, string, SemanticVersionFormat), ILookup<ICommit, SemanticVersionWithTag>>
private readonly ConcurrentDictionary<(IBranch, string, SemanticVersionFormat), IReadOnlyList<(ICommit Key, SemanticVersionWithTag Value)>>
taggedSemanticVersionsOfMergeTargetCache = new();
private readonly ConcurrentDictionary<(string, SemanticVersionFormat), ILookup<ICommit, SemanticVersionWithTag>>
private readonly ConcurrentDictionary<(string, SemanticVersionFormat), IReadOnlyList<SemanticVersionWithTag>>
taggedSemanticVersionsCache = new();
private readonly ILog log = log.NotNull();

private readonly IGitRepository gitRepository = gitRepository.NotNull();
private readonly IBranchRepository branchRepository = branchRepository.NotNull();

public ILookup<ICommit, SemanticVersionWithTag> GetAllTaggedSemanticVersions(
IGitVersionConfiguration configuration, EffectiveConfiguration effectiveConfiguration,
IBranch branch, string? label, DateTimeOffset? notOlderThan)
{
configuration.NotNull();
effectiveConfiguration.NotNull();
branch.NotNull();

IEnumerable<(ICommit Key, SemanticVersionWithTag Value)> GetElements()
{
var semanticVersionsOfBranch = GetTaggedSemanticVersionsOfBranch(
branch: branch,
tagPrefix: effectiveConfiguration.TagPrefix,
format: effectiveConfiguration.SemanticVersionFormat,
ignore: configuration.Ignore
);
foreach (var grouping in semanticVersionsOfBranch)
{
if (grouping.Key.When > notOlderThan) continue;

foreach (var semanticVersion in grouping)
{
if (semanticVersion.Value.IsMatchForBranchSpecificLabel(label))
{
yield return new(grouping.Key, semanticVersion);
}
}
}

if (effectiveConfiguration.TrackMergeTarget)
{
var semanticVersionsOfMergeTarget = GetTaggedSemanticVersionsOfMergeTarget(
branch: branch,
tagPrefix: effectiveConfiguration.TagPrefix,
format: effectiveConfiguration.SemanticVersionFormat,
ignore: configuration.Ignore
);
foreach (var grouping in semanticVersionsOfMergeTarget)
{
if (grouping.Key.When > notOlderThan) continue;

foreach (var semanticVersion in grouping)
{
if (semanticVersion.Value.IsMatchForBranchSpecificLabel(label))
{
yield return new(grouping.Key, semanticVersion);
}
}
}
}

if (effectiveConfiguration.TracksReleaseBranches)
{
var semanticVersionsOfReleaseBranches = GetTaggedSemanticVersionsOfReleaseBranches(
configuration: configuration,
tagPrefix: effectiveConfiguration.TagPrefix,
format: effectiveConfiguration.SemanticVersionFormat,
excludeBranches: branch
);
foreach (var grouping in semanticVersionsOfReleaseBranches)
{
if (grouping.Key.When > notOlderThan) continue;

foreach (var semanticVersion in grouping)
{
if (semanticVersion.Value.IsMatchForBranchSpecificLabel(label))
{
yield return new(grouping.Key, semanticVersion);
}
}
}
}

if (!effectiveConfiguration.IsMainBranch && !effectiveConfiguration.IsReleaseBranch)
{
var semanticVersionsOfMainlineBranches = GetTaggedSemanticVersionsOfMainBranches(
configuration: configuration,
tagPrefix: effectiveConfiguration.TagPrefix,
format: effectiveConfiguration.SemanticVersionFormat,
excludeBranches: branch
);
foreach (var grouping in semanticVersionsOfMainlineBranches)
{
if (grouping.Key.When > notOlderThan) continue;

foreach (var semanticVersion in grouping)
{
if (semanticVersion.Value.IsMatchForBranchSpecificLabel(label))
{
yield return new(grouping.Key, semanticVersion);
}
}
}
}
}

return GetElements().Distinct().OrderByDescending(element => element.Key.When)
.ToLookup(element => element.Key, element => element.Value);
}

public ILookup<ICommit, SemanticVersionWithTag> GetTaggedSemanticVersionsOfBranch(
IBranch branch, string? tagPrefix, SemanticVersionFormat format, IIgnoreConfiguration ignore)
Expand Down Expand Up @@ -150,8 +45,7 @@ IEnumerable<SemanticVersionWithTag> GetElements()
var result = taggedSemanticVersionsOfBranchCache.GetOrAdd(new(branch, tagPrefix, format), _ =>
{
isCached = false;
return GetElements().Distinct().OrderByDescending(element => element.Tag.Commit.When)
.ToLookup(element => element.Tag.Commit, element => element);
return GetElements().Distinct().OrderByDescending(element => element.Tag.Commit.When).ToList();
});

if (isCached)
Expand All @@ -162,7 +56,7 @@ IEnumerable<SemanticVersionWithTag> GetElements()
);
}

return result;
return result.ToLookup(element => element.Tag.Commit, element => element);
}

public ILookup<ICommit, SemanticVersionWithTag> GetTaggedSemanticVersionsOfMergeTarget(
Expand Down Expand Up @@ -192,8 +86,7 @@ public ILookup<ICommit, SemanticVersionWithTag> GetTaggedSemanticVersionsOfMerge
var result = taggedSemanticVersionsOfMergeTargetCache.GetOrAdd(new(branch, tagPrefix, format), _ =>
{
isCached = false;
return GetElements().Distinct().OrderByDescending(element => element.Key.When)
.ToLookup(element => element.Key, element => element.Value);
return GetElements().Distinct().OrderByDescending(element => element.Key.When).ToList();
});

if (isCached)
Expand All @@ -204,59 +97,7 @@ public ILookup<ICommit, SemanticVersionWithTag> GetTaggedSemanticVersionsOfMerge
);
}

return result;
}

public ILookup<ICommit, SemanticVersionWithTag> GetTaggedSemanticVersionsOfMainBranches(
IGitVersionConfiguration configuration, string? tagPrefix, SemanticVersionFormat format, params IBranch[] excludeBranches)
{
configuration.NotNull();
tagPrefix ??= string.Empty;
excludeBranches.NotNull();

IEnumerable<SemanticVersionWithTag> GetElements()
{
using (this.log.IndentLog($"Getting tagged semantic versions of mainline branches. " +
$"TagPrefix: {tagPrefix} and Format: {format}"))
{
foreach (var mainlinemBranch in branchRepository.GetMainBranches(configuration, excludeBranches))
{
foreach (var semanticVersion in GetTaggedSemanticVersionsOfBranch(mainlinemBranch, tagPrefix, format, configuration.Ignore).SelectMany(_ => _))
{
yield return semanticVersion;
}
}
}
}

return GetElements().Distinct().OrderByDescending(element => element.Tag.Commit.When)
.ToLookup(element => element.Tag.Commit, element => element);
}

public ILookup<ICommit, SemanticVersionWithTag> GetTaggedSemanticVersionsOfReleaseBranches(
IGitVersionConfiguration configuration, string? tagPrefix, SemanticVersionFormat format, params IBranch[] excludeBranches)
{
configuration.NotNull();
tagPrefix ??= string.Empty;
excludeBranches.NotNull();

IEnumerable<SemanticVersionWithTag> GetElements()
{
using (this.log.IndentLog($"Getting tagged semantic versions of release branches. " +
$"TagPrefix: {tagPrefix} and Format: {format}"))
{
foreach (var releaseBranch in branchRepository.GetReleaseBranches(configuration, excludeBranches))
{
foreach (var semanticVersion in GetTaggedSemanticVersionsOfBranch(releaseBranch, tagPrefix, format, configuration.Ignore).SelectMany(_ => _))
{
yield return semanticVersion;
}
}
}
}

return GetElements().Distinct().OrderByDescending(element => element.Tag.Commit.When)
.ToLookup(element => element.Tag.Commit, element => element);
return result.ToLookup(element => element.Key, element => element.Value);
}

public ILookup<ICommit, SemanticVersionWithTag> GetTaggedSemanticVersions(
Expand All @@ -281,15 +122,14 @@ IEnumerable<SemanticVersionWithTag> GetElements()
var result = taggedSemanticVersionsCache.GetOrAdd(new(tagPrefix, format), _ =>
{
isCached = false;
return GetElements().OrderByDescending(element => element.Tag.Commit.When)
.ToLookup(element => element.Tag.Commit, element => element);
return GetElements().OrderByDescending(element => element.Tag.Commit.When).ToList();
});

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

return result;
return result.ToLookup(element => element.Tag.Commit, element => element);
}
}
Loading