Skip to content

Feature/replace the version mode mainline part i.a #3844

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
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
2 changes: 1 addition & 1 deletion src/GitVersion.Core.Tests/Core/RepositoryStoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public void GetBranchesContainingCommitThrowsDirectlyOnNullCommit()
var fixtureRepository = fixture.Repository.ToGitRepository();
var gitRepoMetadataProvider = new RepositoryStore(this.log, fixtureRepository);

Assert.Throws<ArgumentNullException>(() => gitRepoMetadataProvider.GetBranchesContainingCommit(null));
Assert.Throws<ArgumentNullException>(() => gitRepoMetadataProvider.GetBranchesContainingCommit(null!));
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,15 @@ public void CanTakeVersionFromHotfixesBranch()
r.MakeATaggedCommit("2.0.0");
});
// Merge hotfix branch to support
Commands.Checkout(fixture.Repository, MainBranch);
Commands.Checkout(fixture.Repository, fixture.Repository.CreateBranch("support-1.1", (LibGit2Sharp.Commit)fixture.Repository.Tags.Single(t => t.FriendlyName == "1.1.0").Target));
var branch = fixture.Repository.CreateBranch(
"support-1.1", (LibGit2Sharp.Commit)fixture.Repository.Tags.Single(t => t.FriendlyName == "1.1.0").Target
);
Commands.Checkout(fixture.Repository, branch);
fixture.AssertFullSemver("1.1.0");

// create hotfix branch
Commands.Checkout(fixture.Repository, fixture.Repository.CreateBranch("hotfixes/1.1.1"));
fixture.AssertFullSemver("1.1.1+0");
fixture.AssertFullSemver("1.1.1-beta.1+0");
fixture.Repository.MakeACommit();

fixture.AssertFullSemver("1.1.1-beta.1+1");
Expand All @@ -85,7 +87,7 @@ public void PatchOlderReleaseExample()

// create hotfix branch
fixture.BranchTo("hotfix-1.1.1");
fixture.AssertFullSemver("1.1.1+0");
fixture.AssertFullSemver("1.1.1-beta.1+0");
fixture.MakeACommit();

fixture.AssertFullSemver("1.1.1-beta.1+1");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public void RepositoryWithALotOfTags()
var sw = Stopwatch.StartNew();

fixture.AssertFullSemver($"1.0.{maxCommits}-feature.1+1", configuration);
sw.Stop();

sw.ElapsedMilliseconds.ShouldBeLessThan(5000);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void WhenSupportIsBranchedFromMainWithSpecificTag()
using var fixture = new EmptyRepositoryFixture();

fixture.MakeACommit();
fixture.AssertFullSemver("0.0.1-1");
fixture.AssertFullSemver("0.0.1-1", configuration);

fixture.ApplyTag("1.4.0-rc");
fixture.MakeACommit();
Expand Down
1,380 changes: 1,380 additions & 0 deletions src/GitVersion.Core.Tests/VersionCalculation/SemanticVersionTests.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void ConfigNextVersionTest(string nextVersion, string expectedVersion, Se

baseVersion.ShouldNotBeNull();
baseVersion.ShouldIncrement.ShouldBe(false);
baseVersion.SemanticVersion.ToString().ShouldBe(expectedVersion);
baseVersion.GetSemanticVersion().ToString().ShouldBe(expectedVersion);
}

[TestCase("0.1", SemanticVersionFormat.Strict)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ private static void AssertMergeMessage(string message, string? expectedVersion,
else
{
baseVersion.ShouldNotBeNull();
baseVersion.SemanticVersion.ToString().ShouldBe(expectedVersion);
baseVersion.GetSemanticVersion().ToString().ShouldBe(expectedVersion);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void CanTakeVersionFromNameOfReleaseBranch(string branchName, string expe
strategy.ShouldNotBeNull();
var baseVersion = strategy.GetBaseVersions(effectiveBranchConfiguration).Single();

baseVersion.SemanticVersion.ToString().ShouldBe(expectedBaseVersion);
baseVersion.GetSemanticVersion().ToString().ShouldBe(expectedBaseVersion);
}

[TestCase("origin/hotfix-2.0.0")]
Expand Down Expand Up @@ -90,7 +90,7 @@ public void CanTakeVersionFromNameOfConfiguredReleaseBranch(string branchName, s
strategy.ShouldNotBeNull();
var baseVersion = strategy.GetBaseVersions(effectiveBranchConfiguration).Single();

baseVersion.SemanticVersion.ToString().ShouldBe(expectedBaseVersion);
baseVersion.GetSemanticVersion().ToString().ShouldBe(expectedBaseVersion);
}

[TestCase("origin", "release-2.0.0", "2.0.0")]
Expand All @@ -114,7 +114,7 @@ public void CanTakeVersionFromNameOfRemoteReleaseBranch(string origin, string br
strategy.ShouldNotBeNull();
var baseVersion = strategy.GetBaseVersions(effectiveBranchConfiguration).Single();

baseVersion.SemanticVersion.ToString().ShouldBe(expectedBaseVersion);
baseVersion.GetSemanticVersion().ToString().ShouldBe(expectedBaseVersion);
}

private static IVersionStrategy GetVersionStrategy(IGitRepository repository,
Expand Down
7 changes: 3 additions & 4 deletions src/GitVersion.Core/Core/Abstractions/IRepositoryStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ public interface IRepositoryStore
IEnumerable<ICommit> GetCommitLog(ICommit? baseVersionSource, ICommit? currentCommit);

IBranch GetTargetBranch(string? targetBranchName);
IBranch? FindBranch(string? branchName);
IBranch? FindBranch(ReferenceName branchName);
IBranch? FindBranch(string branchName);
IBranch? FindMainBranch(IGitVersionConfiguration configuration);
IEnumerable<IBranch> FindMainlineBranches(IGitVersionConfiguration configuration);
IEnumerable<IBranch> GetReleaseBranches(IEnumerable<KeyValuePair<string, IBranchConfiguration>> releaseBranchConfig);
IEnumerable<IBranch> ExcludingBranches(IEnumerable<IBranch> branchesToExclude);
IEnumerable<IBranch> GetBranchesContainingCommit(ICommit? commit, IEnumerable<IBranch>? branches = null, bool onlyTrackedBranches = false);
IEnumerable<IBranch> GetBranchesContainingCommit(ICommit commit, IEnumerable<IBranch>? branches = null, bool onlyTrackedBranches = false);

IDictionary<string, List<IBranch>> GetMainlineBranches(ICommit commit, IGitVersionConfiguration configuration);

Expand All @@ -41,8 +42,6 @@ public interface IRepositoryStore

SemanticVersion? GetCurrentCommitTaggedVersion(ICommit? commit, string? tagPrefix, SemanticVersionFormat format, bool handleDetachedBranch);

IEnumerable<SemanticVersion> GetVersionTagsOnBranch(IBranch branch, string? tagPrefix, SemanticVersionFormat format);

IReadOnlyList<SemanticVersionWithTag> GetTaggedSemanticVersions(string? tagPrefix, SemanticVersionFormat format);

IReadOnlyList<SemanticVersionWithTag> GetTaggedSemanticVersionsOnBranch(IBranch branch, string? tagPrefix, SemanticVersionFormat format);
Expand Down
41 changes: 41 additions & 0 deletions src/GitVersion.Core/Core/BranchRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using GitVersion.Configuration;
using GitVersion.Extensions;

namespace GitVersion.Core;

internal sealed class BranchRepository : IBranchRepository
{
private GitVersionContext VersionContext => this.versionContextLazy.Value;
private readonly Lazy<GitVersionContext> versionContextLazy;

private readonly IGitRepository gitRepository;

public BranchRepository(Lazy<GitVersionContext> versionContext, IGitRepository gitRepository)
{
this.versionContextLazy = versionContext.NotNull();
this.gitRepository = gitRepository.NotNull();
}

public IEnumerable<IBranch> GetMainlineBranches(params IBranch[] excludeBranches)
=> GetBranches(new HashSet<IBranch>(excludeBranches), configuration => configuration.IsMainline == true);

public IEnumerable<IBranch> GetReleaseBranches(params IBranch[] excludeBranches)
=> GetBranches(new HashSet<IBranch>(excludeBranches), configuration => configuration.IsReleaseBranch == true);

private IEnumerable<IBranch> GetBranches(HashSet<IBranch> excludeBranches, Func<IBranchConfiguration, bool> predicate)
{
predicate.NotNull();

foreach (var branch in this.gitRepository.Branches)
{
if (!excludeBranches.Contains(branch))
{
var branchConfiguration = VersionContext.Configuration.GetBranchConfiguration(branch.Name);
if (predicate(branchConfiguration))
{
yield return branch;
}
}
}
}
}
4 changes: 2 additions & 2 deletions src/GitVersion.Core/Core/BranchesContainingCommitFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public BranchesContainingCommitFinder(IGitRepository repository, ILog log)
this.log = log.NotNull();
}

public IEnumerable<IBranch> GetBranchesContainingCommit(ICommit? commit, IEnumerable<IBranch>? branches = null, bool onlyTrackedBranches = false)
public IEnumerable<IBranch> GetBranchesContainingCommit(ICommit commit, IEnumerable<IBranch>? branches = null, bool onlyTrackedBranches = false)
{
commit = commit.NotNull();
commit.NotNull();
branches ??= this.repository.Branches.ToList();

// TODO Should we cache this?
Expand Down
2 changes: 2 additions & 0 deletions src/GitVersion.Core/Core/GitVersionContextFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public GitVersionContext Create(GitVersionOptions gitVersionOptions)
var currentBranch = this.repositoryStore.GetTargetBranch(gitVersionOptions.RepositoryInfo.TargetBranch) ?? throw new InvalidOperationException("Need a branch to operate on");
var currentCommit = this.repositoryStore.GetCurrentCommit(currentBranch, gitVersionOptions.RepositoryInfo.CommitId);

if (currentCommit is null) throw new GitVersionException("No commits found on the current branch.");

var overrideConfiguration = this.options.Value.ConfigurationInfo.OverrideConfiguration;
var configuration = this.configurationProvider.Provide(overrideConfiguration);
if (currentBranch.IsDetachedHead)
Expand Down
8 changes: 8 additions & 0 deletions src/GitVersion.Core/Core/IBranchRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace GitVersion.Core;

internal interface IBranchRepository
{
IEnumerable<IBranch> GetMainlineBranches(params IBranch[] excludeBranches);

IEnumerable<IBranch> GetReleaseBranches(params IBranch[] excludeBranches);
}
30 changes: 30 additions & 0 deletions src/GitVersion.Core/Core/ITaggedSemanticVersionRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using GitVersion.Configuration;

namespace GitVersion.Core;

internal interface ITaggedSemanticVersionRepository
{
ILookup<ICommit, SemanticVersionWithTag> GetTaggedSemanticVersions(IBranch branch, EffectiveConfiguration configuration);

ILookup<ICommit, SemanticVersionWithTag> GetAllTaggedSemanticVersions(string? tagPrefix, SemanticVersionFormat format);

ILookup<ICommit, SemanticVersionWithTag> GetTaggedSemanticVersionsOfBranch(
IBranch branch,
string? tagPrefix,
SemanticVersionFormat format);

ILookup<ICommit, SemanticVersionWithTag> GetTaggedSemanticVersionsOfMergeTarget(
IBranch branch,
string? tagPrefix,
SemanticVersionFormat format);

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

ILookup<ICommit, SemanticVersionWithTag> GetTaggedSemanticVersionsOfReleaseBranches(
string? tagPrefix,
SemanticVersionFormat format,
params IBranch[] excludeBranches);
}
33 changes: 7 additions & 26 deletions src/GitVersion.Core/Core/RepositoryStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ public IBranch GetTargetBranch(string? targetBranchName)
return desiredBranch;
}

public IBranch? FindBranch(string? branchName) => this.repository.Branches.FirstOrDefault(x => x.Name.EquivalentTo(branchName));
public IBranch? FindBranch(ReferenceName branchName) => this.repository.Branches.FirstOrDefault(x => x.Name.Equals(branchName));

public IBranch? FindBranch(string branchName) => this.repository.Branches.FirstOrDefault(x => x.Name.EquivalentTo(branchName));

public IBranch? FindMainBranch(IGitVersionConfiguration configuration)
{
Expand Down Expand Up @@ -143,8 +145,10 @@ private static bool IsReleaseBranch(INamedReference branch, IEnumerable<KeyValue

public IEnumerable<IBranch> ExcludingBranches(IEnumerable<IBranch> branchesToExclude) => this.repository.Branches.ExcludeBranches(branchesToExclude);

public IEnumerable<IBranch> GetBranchesContainingCommit(ICommit? commit, IEnumerable<IBranch>? branches = null, bool onlyTrackedBranches = false)
public IEnumerable<IBranch> GetBranchesContainingCommit(ICommit commit, IEnumerable<IBranch>? branches = null, bool onlyTrackedBranches = false)
{
commit.NotNull();

var branchesContainingCommitFinder = new BranchesContainingCommitFinder(this.repository, this.log);
return branchesContainingCommitFinder.GetBranchesContainingCommit(commit, branches, onlyTrackedBranches);
}
Expand Down Expand Up @@ -253,29 +257,6 @@ public IEnumerable<BranchCommit> FindCommitBranchesWasBranchedFrom(IBranch branc
.SelectMany(tag => GetCurrentCommitSemanticVersions(commit, tagPrefix, tag, format, handleDetachedBranch))
.Max();

public IEnumerable<SemanticVersion> GetVersionTagsOnBranch(IBranch branch, string? tagPrefix, SemanticVersionFormat format)
{
branch = branch.NotNull();

if (this.taggedSemanticVersionsOnBranchCache.TryGetValue(branch, out var onBranch))
{
this.log.Debug($"Cache hit for version tags on branch '{branch.Name.Canonical}");
return onBranch.Select(element => element.Value);
}

using (this.log.IndentLog($"Getting version tags from branch '{branch.Name.Canonical}'."))
{
var semanticVersions = GetTaggedSemanticVersions(tagPrefix, format);
var tagsBySha = semanticVersions.Where(t => t.Tag.TargetSha != null).ToLookup(t => t.Tag.TargetSha, t => t);

var versionTags = (branch.Commits?.SelectMany(c => tagsBySha[c.Sha].Select(t => t))
?? Enumerable.Empty<SemanticVersionWithTag>()).ToList();

this.taggedSemanticVersionsOnBranchCache.Add(branch, versionTags);
return versionTags.Select(element => element.Value);
}
}

public IReadOnlyList<SemanticVersionWithTag> GetTaggedSemanticVersions(string? tagPrefix, SemanticVersionFormat format)
{
if (this.taggedSemanticVersionsCache != null)
Expand Down Expand Up @@ -316,7 +297,7 @@ public IReadOnlyList<SemanticVersionWithTag> GetTaggedSemanticVersionsOnBranch(
var semanticVersions = GetTaggedSemanticVersions(tagPrefix, format);
var tagsBySha = semanticVersions.Where(t => t.Tag.TargetSha != null).ToLookup(t => t.Tag.TargetSha, t => t);

var versionTags = (branch.Commits?.SelectMany(c => tagsBySha[c.Sha].Select(t => t))
var versionTags = (branch.Commits.SelectMany(c => tagsBySha[c.Sha].Select(t => t))
?? Enumerable.Empty<SemanticVersionWithTag>()).ToList();

this.taggedSemanticVersionsOnBranchCache.Add(branch, versionTags);
Expand Down
Loading