diff --git a/BREAKING_CHANGES.md b/BREAKING_CHANGES.md index 27863697a4..51c4d6a1e5 100644 --- a/BREAKING_CHANGES.md +++ b/BREAKING_CHANGES.md @@ -55,6 +55,7 @@ * TrunkBased * The initialization wizard has been removed. * On the `develop`, `release` and `hotfix` branch the introduced branch related property `prevent-increment.when-current-commit-tagged` has been set to `false` to get the incremented instead of the tagged semantic version. +* When setting the "ignore commits before" parameter to a future value, an exception will occur if no commits are found on the current branch. This behavior mimics that of an empty repository. ## v5.0.0 diff --git a/docs/input/docs/reference/configuration.md b/docs/input/docs/reference/configuration.md index 45cbecc3d0..da84070111 100644 --- a/docs/input/docs/reference/configuration.md +++ b/docs/input/docs/reference/configuration.md @@ -384,8 +384,8 @@ branches: regex: (?.+) source-branches: - main - - release - feature + - hotfix - pull-request ignore: sha: [] diff --git a/src/GitVersion.Configuration/SupportedWorkflows/TrunkBased/v1.yml b/src/GitVersion.Configuration/SupportedWorkflows/TrunkBased/v1.yml index f2da9a9ddd..bb225073de 100644 --- a/src/GitVersion.Configuration/SupportedWorkflows/TrunkBased/v1.yml +++ b/src/GitVersion.Configuration/SupportedWorkflows/TrunkBased/v1.yml @@ -59,8 +59,8 @@ branches: regex: (?.+) source-branches: - main - - release - feature + - hotfix - pull-request ignore: sha: [] diff --git a/src/GitVersion.Configuration/TrunkBasedConfigurationBuilder.cs b/src/GitVersion.Configuration/TrunkBasedConfigurationBuilder.cs new file mode 100644 index 0000000000..a522d694a7 --- /dev/null +++ b/src/GitVersion.Configuration/TrunkBasedConfigurationBuilder.cs @@ -0,0 +1,122 @@ +using GitVersion.VersionCalculation; + +namespace GitVersion.Configuration; + +internal sealed class TrunkBasedConfigurationBuilder : ConfigurationBuilderBase +{ + public static TrunkBasedConfigurationBuilder New => new(); + + private TrunkBasedConfigurationBuilder() + { + WithConfiguration(new GitVersionConfiguration() + { + AssemblyFileVersioningScheme = ConfigurationConstants.DefaultAssemblyFileVersioningScheme, + AssemblyVersioningScheme = ConfigurationConstants.DefaultAssemblyVersioningScheme, + CommitDateFormat = ConfigurationConstants.DefaultCommitDateFormat, + MajorVersionBumpMessage = IncrementStrategyFinder.DefaultMajorPattern, + MinorVersionBumpMessage = IncrementStrategyFinder.DefaultMinorPattern, + NoBumpMessage = IncrementStrategyFinder.DefaultNoBumpPattern, + PatchVersionBumpMessage = IncrementStrategyFinder.DefaultPatchPattern, + SemanticVersionFormat = ConfigurationConstants.DefaultSemanticVersionFormat, + VersionStrategies = [ + VersionStrategies.ConfiguredNextVersion, + VersionStrategies.TrunkBased + ], + TagPrefix = ConfigurationConstants.DefaultTagPrefix, + VersionInBranchPattern = ConfigurationConstants.DefaultVersionInBranchPattern, + TagPreReleaseWeight = ConfigurationConstants.DefaultTagPreReleaseWeight, + UpdateBuildNumber = ConfigurationConstants.DefaultUpdateBuildNumber, + DeploymentMode = DeploymentMode.ManualDeployment, + RegularExpression = string.Empty, + Label = ConfigurationConstants.BranchNamePlaceholder, + Increment = IncrementStrategy.Inherit, + CommitMessageIncrementing = CommitMessageIncrementMode.Enabled, + PreventIncrement = new PreventIncrementConfiguration() + { + OfMergedBranch = false, + WhenBranchMerged = false, + WhenCurrentCommitTagged = true + }, + TrackMergeTarget = false, + TrackMergeMessage = true, + TracksReleaseBranches = false, + IsReleaseBranch = false, + IsMainBranch = false + }); + + WithBranch(MainBranch.Name).WithConfiguration(new BranchConfiguration() + { + Increment = IncrementStrategy.Patch, + RegularExpression = MainBranch.RegexPattern, + DeploymentMode = DeploymentMode.ContinuousDeployment, + SourceBranches = [], + Label = string.Empty, + PreventIncrement = new PreventIncrementConfiguration() + { + OfMergedBranch = true + }, + TrackMergeTarget = false, + TracksReleaseBranches = false, + IsMainBranch = true, + IsReleaseBranch = false, + PreReleaseWeight = 55000 + }); + + WithBranch(FeatureBranch.Name).WithConfiguration(new BranchConfiguration() + { + Increment = IncrementStrategy.Minor, + RegularExpression = FeatureBranch.RegexPattern, + SourceBranches = + [ + this.MainBranch.Name + ], + PreventIncrement = new PreventIncrementConfiguration() + { + WhenCurrentCommitTagged = false + }, + PreReleaseWeight = 30000 + }); + + WithBranch(HotfixBranch.Name).WithConfiguration(new BranchConfiguration() + { + Increment = IncrementStrategy.Patch, + RegularExpression = HotfixBranch.RegexPattern, + SourceBranches = + [ + this.MainBranch.Name + ], + PreventIncrement = new PreventIncrementConfiguration() + { + WhenCurrentCommitTagged = false + }, + PreReleaseWeight = 30000 + }); + + WithBranch(PullRequestBranch.Name).WithConfiguration(new BranchConfiguration + { + Increment = IncrementStrategy.Inherit, + RegularExpression = PullRequestBranch.RegexPattern, + DeploymentMode = DeploymentMode.ManualDeployment, + SourceBranches = + [ + this.MainBranch.Name + ], + Label = "PullRequest", + LabelNumberPattern = ConfigurationConstants.DefaultLabelNumberPattern, + PreReleaseWeight = 30000 + }); + + WithBranch(UnknownBranch.Name).WithConfiguration(new BranchConfiguration + { + RegularExpression = UnknownBranch.RegexPattern, + DeploymentMode = DeploymentMode.ManualDeployment, + Increment = IncrementStrategy.Inherit, + SourceBranches = + [ + this.MainBranch.Name, + this.FeatureBranch.Name, + this.PullRequestBranch.Name + ] + }); + } +} diff --git a/src/GitVersion.Core.Tests/IntegrationTests/IgnoreBeforeScenarios.cs b/src/GitVersion.Core.Tests/IntegrationTests/IgnoreBeforeScenarios.cs deleted file mode 100644 index 0150b215a4..0000000000 --- a/src/GitVersion.Core.Tests/IntegrationTests/IgnoreBeforeScenarios.cs +++ /dev/null @@ -1,40 +0,0 @@ -using GitVersion.Configuration; -using GitVersion.Core.Tests.Helpers; - -namespace GitVersion.Core.Tests.IntegrationTests; - -[TestFixture] -public class IgnoreBeforeScenarios : TestBase -{ - [TestCase(null, "0.0.1-0")] - [TestCase("0.0.1", "0.0.1-0")] - [TestCase("0.1.0", "0.1.0-0")] - [TestCase("1.0.0", "1.0.0-0")] - public void ShouldFallbackToBaseVersionWhenAllCommitsAreIgnored(string? nextVersion, string expectedFullSemVer) - { - using var fixture = new EmptyRepositoryFixture(); - var dateTimeNow = DateTimeOffset.Now; - fixture.MakeACommit(); - - var configuration = GitFlowConfigurationBuilder.New.WithNextVersion(nextVersion) - .WithIgnoreConfiguration(new IgnoreConfiguration { Before = dateTimeNow.AddDays(1) }).Build(); - - fixture.AssertFullSemver(expectedFullSemVer, configuration); - } - - [TestCase(null, "0.0.1-1")] - [TestCase("0.0.1", "0.0.1-1")] - [TestCase("0.1.0", "0.1.0-1")] - [TestCase("1.0.0", "1.0.0-1")] - public void ShouldNotFallbackToBaseVersionWhenAllCommitsAreNotIgnored(string? nextVersion, string expectedFullSemVer) - { - using var fixture = new EmptyRepositoryFixture(); - var dateTimeNow = DateTimeOffset.Now; - fixture.MakeACommit(); - - var configuration = GitFlowConfigurationBuilder.New.WithNextVersion(nextVersion) - .WithIgnoreConfiguration(new IgnoreConfiguration { Before = dateTimeNow.AddDays(-1) }).Build(); - - fixture.AssertFullSemver(expectedFullSemVer, configuration); - } -} diff --git a/src/GitVersion.Core.Tests/IntegrationTests/IgnoreCommitScenarios.cs b/src/GitVersion.Core.Tests/IntegrationTests/IgnoreCommitScenarios.cs new file mode 100644 index 0000000000..186e233f74 --- /dev/null +++ b/src/GitVersion.Core.Tests/IntegrationTests/IgnoreCommitScenarios.cs @@ -0,0 +1,334 @@ +using GitVersion.Configuration; +using GitVersion.Core.Tests.Helpers; + +namespace GitVersion.Core.Tests.IntegrationTests; + +[TestFixture] +public class IgnoreCommitScenarios : TestBase +{ + [Test] + public void ShouldThrowGitVersionExceptionWhenAllCommitsAreIgnored() + { + using var fixture = new EmptyRepositoryFixture(); + var dateTimeNow = DateTimeOffset.Now; + fixture.MakeACommit(); + + var configuration = GitFlowConfigurationBuilder.New + .WithIgnoreConfiguration(new IgnoreConfiguration { Before = dateTimeNow.AddYears(1) }).Build(); + + Should.Throw(() => fixture.GetVersion(configuration)) + .Message.ShouldBe("No commits found on the current branch."); + } + + [TestCase(null, "0.0.1-1")] + [TestCase("0.0.1", "0.0.1-1")] + [TestCase("0.1.0", "0.1.0-1")] + [TestCase("1.0.0", "1.0.0-1")] + public void ShouldNotFallbackToBaseVersionWhenAllCommitsAreNotIgnored(string? nextVersion, string expectedFullSemVer) + { + using var fixture = new EmptyRepositoryFixture(); + var dateTimeNow = DateTimeOffset.Now; + fixture.MakeACommit(); + + var configuration = GitFlowConfigurationBuilder.New.WithNextVersion(nextVersion) + .WithIgnoreConfiguration(new IgnoreConfiguration { Before = dateTimeNow.AddYears(-1) }).Build(); + + fixture.AssertFullSemver(expectedFullSemVer, configuration); + } + + [Test] + public void GivenTrunkBasedWorkflowWithCommitParameterThenVersionShouldBeCorrect() + { + using var fixture = new EmptyRepositoryFixture("main"); + + fixture.MakeACommit("A"); + var commitB = fixture.Repository.MakeACommit("B"); + fixture.MakeACommit("C"); + fixture.MakeACommit("D"); + + var configuration = TrunkBasedConfigurationBuilder.New.Build(); + + // ✅ succeeds as expected + fixture.AssertFullSemver("0.0.2", configuration, commitId: commitB.Sha); + } + + [Test] + public void GivenTrunkBasedWorkflowWithIgnoreConfigurationForCommitThenVersionShouldBeCorrect() + { + using var fixture = new EmptyRepositoryFixture("main"); + + fixture.MakeACommit("A"); + var commitB = fixture.Repository.MakeACommit("B"); + fixture.MakeACommit("C"); + fixture.MakeACommit("D"); + + var configuration = TrunkBasedConfigurationBuilder.New + .WithIgnoreConfiguration(new IgnoreConfiguration() { Shas = [commitB.Sha] }) + .Build(); + + // ✅ succeeds as expected + fixture.AssertFullSemver("0.0.3", configuration); + } + + [Test] + public void GivenTrunkBasedWorkflowWithIgnoreConfigurationForCommitBAndCommitParameterAThenVersionShouldBeCorrect() + { + using var fixture = new EmptyRepositoryFixture("main"); + + fixture.MakeACommit("A"); + var commitB = fixture.Repository.MakeACommit("B"); + var commitC = fixture.Repository.MakeACommit("C"); + fixture.MakeACommit("D"); + + var configuration = TrunkBasedConfigurationBuilder.New + .WithIgnoreConfiguration(new IgnoreConfiguration() { Shas = [commitB.Sha] }) + .Build(); + + // ✅ succeeds as expected + fixture.AssertFullSemver("0.0.2", configuration, commitId: commitC.Sha); + } + + [Test] + public void GivenTrunkBasedWorkflowWithIgnoreConfigurationForCommitCAndCommitParameterCThenCommitBShouldBeUsed() + { + using var fixture = new EmptyRepositoryFixture("main"); + + fixture.MakeACommit("A"); + fixture.MakeACommit("B"); + var commitC = fixture.Repository.MakeACommit("C"); + + var configuration = TrunkBasedConfigurationBuilder.New + .WithIgnoreConfiguration(new IgnoreConfiguration() { Shas = [commitC.Sha] }) + .Build(); + + // ✅ succeeds as expected + fixture.AssertFullSemver("0.0.2", configuration, commitId: commitC.Sha); + } + + [Test] + public void GivenTrunkBasedWorkflowWithIgnoreConfigurationForTaggedCommitThenTagShouldBeIgnored() + { + using var fixture = new EmptyRepositoryFixture("main"); + + fixture.MakeACommit("A"); + fixture.MakeACommit("B"); + var commitC = fixture.Repository.MakeACommit("C"); + fixture.ApplyTag("1.0.0"); + fixture.MakeACommit("D"); + + var configuration = TrunkBasedConfigurationBuilder.New + .WithIgnoreConfiguration(new IgnoreConfiguration() { Shas = [commitC.Sha] }) + .Build(); + + // ✅ succeeds as expected + fixture.AssertFullSemver("0.0.3", configuration); + } + + [Test] + public void GivenTrunkBasedWorkflowWithIgnoreConfigurationBeforeCommitWithTagThenTagShouldBeIgnored() + { + using var fixture = new EmptyRepositoryFixture("main"); + + fixture.MakeACommit("A"); + fixture.MakeACommit("B"); + var commitC = fixture.Repository.MakeACommit("C"); + fixture.ApplyTag("1.0.0"); + fixture.MakeACommit("D"); + + var configuration = TrunkBasedConfigurationBuilder.New + .WithIgnoreConfiguration(new IgnoreConfiguration() { Before = commitC.Committer.When }) + .Build(); + + // ✅ succeeds as expected + fixture.AssertFullSemver("0.0.1", configuration); + } + + [TestCase(false, "1.0.1-0")] + [TestCase(true, "1.0.0")] + public void GivenTrunkBasedWorkflowWithIgnoreConfigurationOfCommitBThenTagShouldBeConsidered( + bool preventIncrementWhenCurrentCommitTagged, string semanticVersion) + { + using var fixture = new EmptyRepositoryFixture("main"); + + fixture.MakeACommit("A"); + fixture.ApplyTag("1.0.0"); + var commitB = fixture.Repository.MakeACommit("B"); + + var configuration = TrunkBasedConfigurationBuilder.New + .WithIgnoreConfiguration(new IgnoreConfiguration() { Shas = [commitB.Sha] }) + .WithBranch("main", _ => _.WithIncrement(IncrementStrategy.Patch) + .WithPreventIncrementWhenCurrentCommitTagged(preventIncrementWhenCurrentCommitTagged) + .WithDeploymentMode(GitVersion.VersionCalculation.DeploymentMode.ContinuousDelivery) + ).Build(); + + // ✅ succeeds as expected + fixture.AssertFullSemver(semanticVersion, configuration); + } + + [TestCase(false, "1.0.1-0")] + [TestCase(true, "1.0.0")] + public void GivenTrunkBasedWorkflowWithCommitParameterBThenTagShouldBeConsidered( + bool preventIncrementWhenCurrentCommitTagged, string semanticVersion) + { + using var fixture = new EmptyRepositoryFixture("main"); + + var commitA = fixture.Repository.MakeACommit("A"); + fixture.ApplyTag("1.0.0"); + fixture.MakeACommit("B"); + + var configuration = TrunkBasedConfigurationBuilder.New + .WithBranch("main", _ => _.WithIncrement(IncrementStrategy.Patch) + .WithPreventIncrementWhenCurrentCommitTagged(preventIncrementWhenCurrentCommitTagged) + .WithDeploymentMode(GitVersion.VersionCalculation.DeploymentMode.ContinuousDelivery) + ).Build(); + + // ✅ succeeds as expected + fixture.AssertFullSemver(semanticVersion, configuration, commitId: commitA.Sha); + } + + [Test] + public void GivenGitHubFlowBasedWorkflowWithCommitParameterThenVersionShouldBeCorrect() + { + using var fixture = new EmptyRepositoryFixture("main"); + + fixture.MakeACommit("A"); + var commitB = fixture.Repository.MakeACommit("B"); + fixture.MakeACommit("C"); + fixture.MakeACommit("D"); + + var configuration = GitHubFlowConfigurationBuilder.New.Build(); + + // ✅ succeeds as expected + fixture.AssertFullSemver("0.0.1-2", configuration, commitId: commitB.Sha); + } + + [Test] + public void GivenGitHubFlowWorkflowWithIgnoreConfigurationForCommitThenVersionShouldBeCorrect() + { + using var fixture = new EmptyRepositoryFixture("main"); + + fixture.MakeACommit("A"); + var commitB = fixture.Repository.MakeACommit("B"); + fixture.MakeACommit("C"); + fixture.MakeACommit("D"); + + var configuration = GitHubFlowConfigurationBuilder.New + .WithIgnoreConfiguration(new IgnoreConfiguration() { Shas = [commitB.Sha] }) + .Build(); + + // ✅ succeeds as expected + fixture.AssertFullSemver("0.0.1-3", configuration); + } + + [Test] + public void GivenGitHubFlowWorkflowWithIgnoreConfigurationForCommitBAndCommitParameterAThenVersionShouldBeCorrect() + { + using var fixture = new EmptyRepositoryFixture("main"); + + fixture.MakeACommit("A"); + var commitB = fixture.Repository.MakeACommit("B"); + var commitC = fixture.Repository.MakeACommit("C"); + fixture.MakeACommit("D"); + + var configuration = GitHubFlowConfigurationBuilder.New + .WithIgnoreConfiguration(new IgnoreConfiguration() { Shas = [commitB.Sha] }) + .Build(); + + // ✅ succeeds as expected + fixture.AssertFullSemver("0.0.1-2", configuration, commitId: commitC.Sha); + } + + [Test] + public void GivenGitHubFlowWorkflowWithIgnoreConfigurationForCommitCAndCommitParameterCThenCommitBShouldBeUsed() + { + using var fixture = new EmptyRepositoryFixture("main"); + + fixture.MakeACommit("A"); + fixture.MakeACommit("B"); + var commitC = fixture.Repository.MakeACommit("C"); + + var configuration = GitHubFlowConfigurationBuilder.New + .WithIgnoreConfiguration(new IgnoreConfiguration() { Shas = [commitC.Sha] }) + .Build(); + + // ✅ succeeds as expected + fixture.AssertFullSemver("0.0.1-2", configuration, commitId: commitC.Sha); + } + + [Test] + public void GivenGitHubFlowWorkflowWithIgnoreConfigurationForTaggedCommitThenTagShouldBeIgnored() + { + using var fixture = new EmptyRepositoryFixture("main"); + + fixture.MakeACommit("A"); + fixture.MakeACommit("B"); + var commitC = fixture.Repository.MakeACommit("C"); + fixture.ApplyTag("1.0.0"); + fixture.MakeACommit("D"); + + var configuration = GitHubFlowConfigurationBuilder.New + .WithIgnoreConfiguration(new IgnoreConfiguration() { Shas = [commitC.Sha] }) + .Build(); + + // ✅ succeeds as expected + fixture.AssertFullSemver("0.0.1-3", configuration); + } + + [Test] + public void GivenGitHubFlowWorkflowWithIgnoreConfigurationBeforeCommitWithTagThenTagShouldBeIgnored() + { + using var fixture = new EmptyRepositoryFixture("main"); + + fixture.MakeACommit("A"); + fixture.MakeACommit("B"); + var commitC = fixture.Repository.MakeACommit("C"); + fixture.ApplyTag("1.0.0"); + fixture.MakeACommit("D"); + + var configuration = GitHubFlowConfigurationBuilder.New + .WithIgnoreConfiguration(new IgnoreConfiguration() { Before = commitC.Committer.When }) + .Build(); + + // ✅ succeeds as expected + fixture.AssertFullSemver("0.0.1-1", configuration); + } + + [TestCase(false, "1.0.1-0")] + [TestCase(true, "1.0.0")] + public void GivenGitHubFlowWorkflowWithIgnoreConfigurationOfCommitBThenTagShouldBeConsidered( + bool preventIncrementWhenCurrentCommitTagged, string semanticVersion) + { + using var fixture = new EmptyRepositoryFixture("main"); + + fixture.MakeACommit("A"); + fixture.ApplyTag("1.0.0"); + var commitB = fixture.Repository.MakeACommit("B"); + + var configuration = GitHubFlowConfigurationBuilder.New + .WithIgnoreConfiguration(new IgnoreConfiguration() { Shas = [commitB.Sha] }) + .WithBranch("main", _ => _.WithPreventIncrementWhenCurrentCommitTagged(preventIncrementWhenCurrentCommitTagged)) + .Build(); + + // ✅ succeeds as expected + fixture.AssertFullSemver(semanticVersion, configuration); + } + + [TestCase(false, "1.0.1-0")] + [TestCase(true, "1.0.0")] + public void GivenGitHubFlowWorkflowWithCommitParameterBThenTagShouldBeConsidered( + bool preventIncrementWhenCurrentCommitTagged, string semanticVersion) + { + using var fixture = new EmptyRepositoryFixture("main"); + + var commitA = fixture.Repository.MakeACommit("A"); + fixture.ApplyTag("1.0.0"); + fixture.MakeACommit("B"); + + var configuration = GitHubFlowConfigurationBuilder.New + .WithBranch("main", _ => _.WithPreventIncrementWhenCurrentCommitTagged(preventIncrementWhenCurrentCommitTagged)) + .Build(); + + // ✅ succeeds as expected + fixture.AssertFullSemver(semanticVersion, configuration, commitId: commitA.Sha); + } +} diff --git a/src/GitVersion.Core/Configuration/EffectiveConfiguration.cs b/src/GitVersion.Core/Configuration/EffectiveConfiguration.cs index d414e499fb..9505fb4383 100644 --- a/src/GitVersion.Core/Configuration/EffectiveConfiguration.cs +++ b/src/GitVersion.Core/Configuration/EffectiveConfiguration.cs @@ -60,6 +60,7 @@ public EffectiveConfiguration(IGitVersionConfiguration configuration, IBranchCon NoBumpMessage = configuration.NoBumpMessage; CommitMessageIncrementing = branchConfiguration.CommitMessageIncrementing.Value; VersionFilters = configuration.Ignore.ToFilters(); + Ignore = configuration.Ignore; TracksReleaseBranches = branchConfiguration.TracksReleaseBranches ?? false; IsReleaseBranch = branchConfiguration.IsReleaseBranch ?? false; IsMainBranch = branchConfiguration.IsMainBranch ?? false; @@ -123,6 +124,8 @@ public EffectiveConfiguration(IGitVersionConfiguration configuration, IBranchCon public IEnumerable VersionFilters { get; } + public IIgnoreConfiguration Ignore { get; } + public string? CommitDateFormat { get; } public bool UpdateBuildNumber { get; } diff --git a/src/GitVersion.Core/Configuration/IgnoreConfigurationExtensions.cs b/src/GitVersion.Core/Configuration/IgnoreConfigurationExtensions.cs new file mode 100644 index 0000000000..4308b39254 --- /dev/null +++ b/src/GitVersion.Core/Configuration/IgnoreConfigurationExtensions.cs @@ -0,0 +1,33 @@ +using GitVersion.Extensions; + +namespace GitVersion.Configuration; + +public static class IgnoreConfigurationExtensions +{ + public static IEnumerable Filter(this IIgnoreConfiguration ignore, IEnumerable source) + { + ignore.NotNull(); + source.NotNull(); + + if (!ignore.IsEmpty) + { + return source.Where(element => ShouldBeIgnored(element.Commit, ignore)); + } + return source; + } + + public static IEnumerable Filter(this IIgnoreConfiguration ignore, IEnumerable source) + { + ignore.NotNull(); + source.NotNull(); + + if (!ignore.IsEmpty) + { + return source.Where(element => ShouldBeIgnored(element, ignore)); + } + return source; + } + + private static bool ShouldBeIgnored(ICommit commit, IIgnoreConfiguration ignore) + => !(commit.When <= ignore.Before) && !ignore.Shas.Contains(commit.Sha); +} diff --git a/src/GitVersion.Core/Core/Abstractions/IRepositoryStore.cs b/src/GitVersion.Core/Core/Abstractions/IRepositoryStore.cs index 178309b953..c077ee994e 100644 --- a/src/GitVersion.Core/Core/Abstractions/IRepositoryStore.cs +++ b/src/GitVersion.Core/Core/Abstractions/IRepositoryStore.cs @@ -10,8 +10,10 @@ public interface IRepositoryStore ICommit? FindMergeBase(IBranch? branch, IBranch? otherBranch); ICommit? FindMergeBase(ICommit commit, ICommit mainlineTip); - ICommit? GetCurrentCommit(IBranch currentBranch, string? commitId); - IEnumerable GetCommitLog(ICommit? baseVersionSource, ICommit? currentCommit); + + ICommit? GetCurrentCommit(IBranch currentBranch, string? commitId, IIgnoreConfiguration ignore); + + IReadOnlyList GetCommitLog(ICommit? baseVersionSource, ICommit currentCommit, IIgnoreConfiguration ignore); IBranch GetTargetBranch(string? targetBranchName); IBranch? FindBranch(ReferenceName branchName); diff --git a/src/GitVersion.Core/Core/GitPreparer.cs b/src/GitVersion.Core/Core/GitPreparer.cs index fc511e7054..ec583dcbf3 100644 --- a/src/GitVersion.Core/Core/GitPreparer.cs +++ b/src/GitVersion.Core/Core/GitPreparer.cs @@ -278,6 +278,7 @@ private void ChooseLocalBranchToAttach(string? headSha, IReadOnlyCollection GetAllTaggedSemanticVersions( ILookup GetTaggedSemanticVersionsOfBranch( IBranch branch, string? tagPrefix, - SemanticVersionFormat format); + SemanticVersionFormat format, + IIgnoreConfiguration ignore); ILookup GetTaggedSemanticVersionsOfMergeTarget( IBranch branch, string? tagPrefix, - SemanticVersionFormat format); + SemanticVersionFormat format, + IIgnoreConfiguration ignore); ILookup GetTaggedSemanticVersionsOfMainBranches( IGitVersionConfiguration configuration, @@ -33,5 +35,6 @@ ILookup GetTaggedSemanticVersionsOfReleaseBranc SemanticVersionFormat format, params IBranch[] excludeBranches); - ILookup GetTaggedSemanticVersions(string? tagPrefix, SemanticVersionFormat format); + ILookup GetTaggedSemanticVersions( + string? tagPrefix, SemanticVersionFormat format, IIgnoreConfiguration ignore); } diff --git a/src/GitVersion.Core/Core/RepositoryStore.cs b/src/GitVersion.Core/Core/RepositoryStore.cs index 4ad673e6fa..a520de3ffe 100644 --- a/src/GitVersion.Core/Core/RepositoryStore.cs +++ b/src/GitVersion.Core/Core/RepositoryStore.cs @@ -27,8 +27,11 @@ public RepositoryStore(ILog log, IGitRepository repository) public ICommit? FindMergeBase(IBranch? branch, IBranch? otherBranch) => this.mergeBaseFinder.FindMergeBaseOf(branch, otherBranch); - public ICommit? GetCurrentCommit(IBranch currentBranch, string? commitId) + public ICommit? GetCurrentCommit(IBranch currentBranch, string? commitId, IIgnoreConfiguration ignore) { + currentBranch.NotNull(); + ignore.NotNull(); + ICommit? currentCommit = null; if (!commitId.IsNullOrWhiteSpace()) { @@ -45,11 +48,18 @@ public RepositoryStore(ILog log, IGitRepository repository) } } + IEnumerable commits = currentBranch.Commits; if (currentCommit != null) - return currentCommit; + { + commits = currentBranch.Commits.GetCommitsPriorTo(currentCommit.When); + } + else + { + this.log.Info("Using latest commit on specified branch"); + } - this.log.Info("Using latest commit on specified branch"); - return currentBranch.Tip; + commits = ignore.Filter(commits); + return commits.FirstOrDefault(); } public IBranch GetTargetBranch(string? targetBranchName) @@ -191,11 +201,21 @@ public IEnumerable FindCommitBranchesWasBranchedFrom(IBranch branc } } - public IEnumerable GetCommitLog(ICommit? baseVersionSource, ICommit? currentCommit) + public IReadOnlyList GetCommitLog(ICommit? baseVersionSource, ICommit currentCommit, IIgnoreConfiguration ignore) { - var filter = new CommitFilter { IncludeReachableFrom = currentCommit, ExcludeReachableFrom = baseVersionSource, SortBy = CommitSortStrategies.Topological | CommitSortStrategies.Time }; + currentCommit.NotNull(); + ignore.NotNull(); + + var filter = new CommitFilter + { + IncludeReachableFrom = currentCommit, + ExcludeReachableFrom = baseVersionSource, + SortBy = CommitSortStrategies.Topological | CommitSortStrategies.Time + }; + + var commits = this.repository.Commits.QueryBy(filter).ToArray(); - return this.repository.Commits.QueryBy(filter); + return ignore.Filter(commits).ToList(); } public bool IsCommitOnBranch(ICommit? baseVersionSource, IBranch branch, ICommit firstMatchingCommit) diff --git a/src/GitVersion.Core/Core/TaggedSemanticVersionRepository.cs b/src/GitVersion.Core/Core/TaggedSemanticVersionRepository.cs index 5af9503836..138c562edc 100644 --- a/src/GitVersion.Core/Core/TaggedSemanticVersionRepository.cs +++ b/src/GitVersion.Core/Core/TaggedSemanticVersionRepository.cs @@ -35,7 +35,8 @@ public ILookup GetAllTaggedSemanticVersions( var semanticVersionsOfBranch = GetTaggedSemanticVersionsOfBranch( branch: branch, tagPrefix: effectiveConfiguration.TagPrefix, - format: effectiveConfiguration.SemanticVersionFormat + format: effectiveConfiguration.SemanticVersionFormat, + ignore: configuration.Ignore ); foreach (var grouping in semanticVersionsOfBranch) { @@ -55,7 +56,8 @@ public ILookup GetAllTaggedSemanticVersions( var semanticVersionsOfMergeTarget = GetTaggedSemanticVersionsOfMergeTarget( branch: branch, tagPrefix: effectiveConfiguration.TagPrefix, - format: effectiveConfiguration.SemanticVersionFormat + format: effectiveConfiguration.SemanticVersionFormat, + ignore: configuration.Ignore ); foreach (var grouping in semanticVersionsOfMergeTarget) { @@ -120,7 +122,7 @@ public ILookup GetAllTaggedSemanticVersions( } public ILookup GetTaggedSemanticVersionsOfBranch( - IBranch branch, string? tagPrefix, SemanticVersionFormat format) + IBranch branch, string? tagPrefix, SemanticVersionFormat format, IIgnoreConfiguration ignore) { branch.NotNull(); tagPrefix ??= string.Empty; @@ -130,8 +132,9 @@ IEnumerable GetElements() using (this.log.IndentLog($"Getting tagged semantic versions on branch '{branch.Name.Canonical}'. " + $"TagPrefix: {tagPrefix} and Format: {format}")) { - var semanticVersions = GetTaggedSemanticVersions(tagPrefix, format); - foreach (var commit in branch.Commits) + var semanticVersions = GetTaggedSemanticVersions(tagPrefix, format, ignore); + + foreach (var commit in ignore.Filter(branch.Commits)) { foreach (var semanticVersion in semanticVersions[commit]) { @@ -160,7 +163,7 @@ IEnumerable GetElements() } public ILookup GetTaggedSemanticVersionsOfMergeTarget( - IBranch branch, string? tagPrefix, SemanticVersionFormat format) + IBranch branch, string? tagPrefix, SemanticVersionFormat format, IIgnoreConfiguration ignore) { branch.NotNull(); tagPrefix ??= string.Empty; @@ -170,9 +173,9 @@ public ILookup GetTaggedSemanticVersionsOfMerge using (this.log.IndentLog($"Getting tagged semantic versions by track merge target '{branch.Name.Canonical}'. " + $"TagPrefix: {tagPrefix} and Format: {format}")) { - var shaHashSet = new HashSet(branch.Commits.Select(element => element.Id.Sha)); + var shaHashSet = new HashSet(ignore.Filter(branch.Commits).Select(element => element.Id.Sha)); - foreach (var semanticVersion in GetTaggedSemanticVersions(tagPrefix, format).SelectMany(_ => _)) + foreach (var semanticVersion in GetTaggedSemanticVersions(tagPrefix, format, ignore).SelectMany(_ => _)) { foreach (var commit in semanticVersion.Tag.Commit.Parents.Where(element => shaHashSet.Contains(element.Id.Sha))) { @@ -214,7 +217,7 @@ IEnumerable GetElements() { foreach (var mainlinemBranch in branchRepository.GetMainBranches(configuration, excludeBranches)) { - foreach (var semanticVersion in GetTaggedSemanticVersionsOfBranch(mainlinemBranch, tagPrefix, format).SelectMany(_ => _)) + foreach (var semanticVersion in GetTaggedSemanticVersionsOfBranch(mainlinemBranch, tagPrefix, format, configuration.Ignore).SelectMany(_ => _)) { yield return semanticVersion; } @@ -239,7 +242,7 @@ IEnumerable GetElements() { foreach (var releaseBranch in branchRepository.GetReleaseBranches(configuration, excludeBranches)) { - foreach (var semanticVersion in GetTaggedSemanticVersionsOfBranch(releaseBranch, tagPrefix, format).SelectMany(_ => _)) + foreach (var semanticVersion in GetTaggedSemanticVersionsOfBranch(releaseBranch, tagPrefix, format, configuration.Ignore).SelectMany(_ => _)) { yield return semanticVersion; } @@ -250,7 +253,8 @@ IEnumerable GetElements() return GetElements().Distinct().ToLookup(element => element.Tag.Commit, element => element); } - public ILookup GetTaggedSemanticVersions(string? tagPrefix, SemanticVersionFormat format) + public ILookup GetTaggedSemanticVersions( + string? tagPrefix, SemanticVersionFormat format, IIgnoreConfiguration ignore) { tagPrefix ??= string.Empty; @@ -258,7 +262,7 @@ IEnumerable GetElements() { this.log.Info($"Getting tagged semantic versions. TagPrefix: {tagPrefix} and Format: {format}"); - foreach (var tag in this.gitRepository.Tags) + foreach (var tag in ignore.Filter(gitRepository.Tags)) { if (SemanticVersion.TryParse(tag.Name.Friendly, tagPrefix, out var semanticVersion, format)) { diff --git a/src/GitVersion.Core/GitVersionContext.cs b/src/GitVersion.Core/GitVersionContext.cs index 41e7cfe9a6..fd654d5dc1 100644 --- a/src/GitVersion.Core/GitVersionContext.cs +++ b/src/GitVersion.Core/GitVersionContext.cs @@ -20,6 +20,8 @@ public class GitVersionContext( public IBranch CurrentBranch { get; } = currentBranch.NotNull(); + public IEnumerable CurrentBranchCommits => CurrentBranch.Commits.GetCommitsPriorTo(CurrentCommit.When); + public ICommit CurrentCommit { get; } = currentCommit.NotNull(); public bool IsCurrentCommitTagged { get; } = isCurrentCommitTagged; diff --git a/src/GitVersion.Core/PublicAPI.Unshipped.txt b/src/GitVersion.Core/PublicAPI.Unshipped.txt index 9a66723e78..7902498b8a 100644 --- a/src/GitVersion.Core/PublicAPI.Unshipped.txt +++ b/src/GitVersion.Core/PublicAPI.Unshipped.txt @@ -94,8 +94,8 @@ GitVersion.Common.IRepositoryStore.FindCommitBranchesWasBranchedFrom(GitVersion. GitVersion.Common.IRepositoryStore.FindMergeBase(GitVersion.IBranch? branch, GitVersion.IBranch? otherBranch) -> GitVersion.ICommit? GitVersion.Common.IRepositoryStore.FindMergeBase(GitVersion.ICommit! commit, GitVersion.ICommit! mainlineTip) -> GitVersion.ICommit? GitVersion.Common.IRepositoryStore.GetBranchesContainingCommit(GitVersion.ICommit! commit, System.Collections.Generic.IEnumerable? branches = null, bool onlyTrackedBranches = false) -> System.Collections.Generic.IEnumerable! -GitVersion.Common.IRepositoryStore.GetCommitLog(GitVersion.ICommit? baseVersionSource, GitVersion.ICommit? currentCommit) -> System.Collections.Generic.IEnumerable! -GitVersion.Common.IRepositoryStore.GetCurrentCommit(GitVersion.IBranch! currentBranch, string? commitId) -> GitVersion.ICommit? +GitVersion.Common.IRepositoryStore.GetCommitLog(GitVersion.ICommit? baseVersionSource, GitVersion.ICommit! currentCommit, GitVersion.Configuration.IIgnoreConfiguration! ignore) -> System.Collections.Generic.IReadOnlyList! +GitVersion.Common.IRepositoryStore.GetCurrentCommit(GitVersion.IBranch! currentBranch, string? commitId, GitVersion.Configuration.IIgnoreConfiguration! ignore) -> GitVersion.ICommit? GitVersion.Common.IRepositoryStore.GetNumberOfUncommittedChanges() -> int GitVersion.Common.IRepositoryStore.GetReleaseBranches(System.Collections.Generic.IEnumerable>! releaseBranchConfig) -> System.Collections.Generic.IEnumerable! GitVersion.Common.IRepositoryStore.GetSourceBranches(GitVersion.IBranch! branch, GitVersion.Configuration.IGitVersionConfiguration! configuration, System.Collections.Generic.IEnumerable! excludedBranches) -> System.Collections.Generic.IEnumerable! @@ -115,6 +115,7 @@ GitVersion.Configuration.EffectiveConfiguration.AssemblyVersioningFormat.get -> GitVersion.Configuration.EffectiveConfiguration.AssemblyVersioningScheme.get -> GitVersion.Extensions.AssemblyVersioningScheme GitVersion.Configuration.EffectiveConfiguration.DeploymentMode.get -> GitVersion.VersionCalculation.DeploymentMode GitVersion.Configuration.EffectiveConfiguration.EffectiveConfiguration(GitVersion.Configuration.IGitVersionConfiguration! configuration, GitVersion.Configuration.IBranchConfiguration! branchConfiguration) -> void +GitVersion.Configuration.EffectiveConfiguration.Ignore.get -> GitVersion.Configuration.IIgnoreConfiguration! GitVersion.Configuration.EffectiveConfiguration.PreventIncrementOfMergedBranch.get -> bool GitVersion.Configuration.EffectiveConfiguration.PreventIncrementWhenBranchMerged.get -> bool GitVersion.Configuration.EffectiveConfiguration.PreventIncrementWhenCurrentCommitTagged.get -> bool @@ -192,6 +193,7 @@ GitVersion.Configuration.IGitVersionConfiguration.VersionInBranchPattern.get -> GitVersion.Configuration.IGitVersionConfiguration.VersionInBranchRegex.get -> System.Text.RegularExpressions.Regex! GitVersion.Configuration.IGitVersionConfiguration.VersionStrategy.get -> GitVersion.VersionCalculation.VersionStrategies GitVersion.Configuration.IGitVersionConfiguration.Workflow.get -> string? +GitVersion.Configuration.IgnoreConfigurationExtensions GitVersion.Configuration.IIgnoreConfiguration GitVersion.Configuration.IIgnoreConfiguration.Before.get -> System.DateTimeOffset? GitVersion.Configuration.IIgnoreConfiguration.IsEmpty.get -> bool @@ -241,6 +243,7 @@ GitVersion.GitVersionCommonModule.RegisterTypes(Microsoft.Extensions.DependencyI GitVersion.GitVersionContext GitVersion.GitVersionContext.Configuration.get -> GitVersion.Configuration.IGitVersionConfiguration! GitVersion.GitVersionContext.CurrentBranch.get -> GitVersion.IBranch! +GitVersion.GitVersionContext.CurrentBranchCommits.get -> System.Collections.Generic.IEnumerable! GitVersion.GitVersionContext.CurrentCommit.get -> GitVersion.ICommit! GitVersion.GitVersionContext.GitVersionContext(GitVersion.IBranch! currentBranch, GitVersion.ICommit! currentCommit, GitVersion.Configuration.IGitVersionConfiguration! configuration, bool isCurrentCommitTagged, int numberOfUncommittedChanges) -> void GitVersion.GitVersionContext.IsCurrentCommitTagged.get -> bool @@ -694,7 +697,7 @@ GitVersion.VersionCalculation.IIncrementStrategyFinder GitVersion.VersionCalculation.IIncrementStrategyFinder.DetermineIncrementedField(GitVersion.ICommit! currentCommit, GitVersion.VersionCalculation.BaseVersion! baseVersion, GitVersion.Configuration.EffectiveConfiguration! configuration, string? label) -> GitVersion.VersionField GitVersion.VersionCalculation.IIncrementStrategyFinder.GetIncrementForcedByCommit(GitVersion.ICommit! commit, GitVersion.Configuration.EffectiveConfiguration! configuration) -> GitVersion.VersionField GitVersion.VersionCalculation.IIncrementStrategyFinder.GetIncrementForCommits(string? majorVersionBumpMessage, string? minorVersionBumpMessage, string? patchVersionBumpMessage, string? noBumpMessage, GitVersion.ICommit![]! commits) -> GitVersion.VersionField? -GitVersion.VersionCalculation.IIncrementStrategyFinder.GetMergedCommits(GitVersion.ICommit! mergeCommit, int index) -> System.Collections.Generic.IEnumerable! +GitVersion.VersionCalculation.IIncrementStrategyFinder.GetMergedCommits(GitVersion.ICommit! mergeCommit, int index, GitVersion.Configuration.IIgnoreConfiguration! ignore) -> System.Collections.Generic.IEnumerable! GitVersion.VersionCalculation.INextVersionCalculator GitVersion.VersionCalculation.INextVersionCalculator.FindVersion() -> GitVersion.SemanticVersion! GitVersion.VersionCalculation.IVariableProvider @@ -788,6 +791,8 @@ static GitVersion.Configuration.ConfigurationExtensions.GetReleaseBranchConfigur static GitVersion.Configuration.ConfigurationExtensions.IsReleaseBranch(this GitVersion.Configuration.IGitVersionConfiguration! configuration, GitVersion.IBranch! branch) -> bool static GitVersion.Configuration.ConfigurationExtensions.IsReleaseBranch(this GitVersion.Configuration.IGitVersionConfiguration! configuration, GitVersion.ReferenceName! branchName) -> bool static GitVersion.Configuration.ConfigurationExtensions.ToFilters(this GitVersion.Configuration.IIgnoreConfiguration! source) -> System.Collections.Generic.IEnumerable! +static GitVersion.Configuration.IgnoreConfigurationExtensions.Filter(this GitVersion.Configuration.IIgnoreConfiguration! ignore, System.Collections.Generic.IEnumerable! source) -> System.Collections.Generic.IEnumerable! +static GitVersion.Configuration.IgnoreConfigurationExtensions.Filter(this GitVersion.Configuration.IIgnoreConfiguration! ignore, System.Collections.Generic.IEnumerable! source) -> System.Collections.Generic.IEnumerable! static GitVersion.Extensions.AssemblyVersionsGeneratorExtensions.GetAssemblyFileVersion(this GitVersion.SemanticVersion! sv, GitVersion.Extensions.AssemblyFileVersioningScheme scheme) -> string? static GitVersion.Extensions.AssemblyVersionsGeneratorExtensions.GetAssemblyVersion(this GitVersion.SemanticVersion! sv, GitVersion.Extensions.AssemblyVersioningScheme scheme) -> string? static GitVersion.Extensions.CommonExtensions.NotNull(this T? value, string! name = "") -> T! diff --git a/src/GitVersion.Core/VersionCalculation/Abstractions/IIncrementStrategyFinder.cs b/src/GitVersion.Core/VersionCalculation/Abstractions/IIncrementStrategyFinder.cs index fb8817767f..ff3b29a999 100644 --- a/src/GitVersion.Core/VersionCalculation/Abstractions/IIncrementStrategyFinder.cs +++ b/src/GitVersion.Core/VersionCalculation/Abstractions/IIncrementStrategyFinder.cs @@ -12,7 +12,7 @@ VersionField DetermineIncrementedField( ICommit[] commits ); - IEnumerable GetMergedCommits(ICommit mergeCommit, int index); + IEnumerable GetMergedCommits(ICommit mergeCommit, int index, IIgnoreConfiguration ignore); VersionField GetIncrementForcedByCommit(ICommit commit, EffectiveConfiguration configuration); } diff --git a/src/GitVersion.Core/VersionCalculation/IncrementStrategyFinder.cs b/src/GitVersion.Core/VersionCalculation/IncrementStrategyFinder.cs index 93c04fa163..21b090c30a 100644 --- a/src/GitVersion.Core/VersionCalculation/IncrementStrategyFinder.cs +++ b/src/GitVersion.Core/VersionCalculation/IncrementStrategyFinder.cs @@ -87,7 +87,8 @@ public VersionField DetermineIncrementedField( semanticVersionFormat: configuration.SemanticVersionFormat, baseVersionSource: baseVersionSource, currentCommit: currentCommit, - label: label + label: label, + ignore: configuration.Ignore ); if (configuration.CommitMessageIncrementing == CommitMessageIncrementMode.MergeMessageOnly) @@ -109,16 +110,19 @@ private static Regex TryGetRegexOrDefault(string? messageRegex, Regex defaultReg ? defaultRegex : CompiledRegexCache.GetOrAdd(messageRegex, pattern => new(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase)); - private IReadOnlyCollection GetCommitHistory( - string? tagPrefix, SemanticVersionFormat semanticVersionFormat, ICommit? baseVersionSource, ICommit currentCommit, string? label) + private IReadOnlyCollection GetCommitHistory(string? tagPrefix, SemanticVersionFormat semanticVersionFormat, + ICommit? baseVersionSource, ICommit currentCommit, string? label, IIgnoreConfiguration ignore) { var targetShas = new Lazy>(() => - this.taggedSemanticVersionRepository.GetTaggedSemanticVersions(tagPrefix, semanticVersionFormat) - .SelectMany(_ => _).Where(_ => _.Value.IsMatchForBranchSpecificLabel(label)).Select(_ => _.Tag.TargetSha).ToHashSet() + taggedSemanticVersionRepository + .GetTaggedSemanticVersions(tagPrefix, semanticVersionFormat, ignore) + .SelectMany(_ => _) + .Where(_ => _.Value.IsMatchForBranchSpecificLabel(label)) + .Select(_ => _.Tag.TargetSha) + .ToHashSet() ); - var intermediateCommits = GetIntermediateCommits(baseVersionSource, currentCommit).ToArray(); - + var intermediateCommits = GetIntermediateCommits(baseVersionSource, currentCommit, ignore); var commitLog = intermediateCommits.ToDictionary(element => element.Id.Sha); foreach (var intermediateCommit in intermediateCommits.Reverse()) @@ -148,9 +152,9 @@ private IReadOnlyCollection GetCommitHistory( /// Get the sequence of commits in a repository between a (exclusive) /// and a particular (inclusive) /// - private IEnumerable GetIntermediateCommits(ICommit? baseCommit, ICommit? headCommit) + private IEnumerable GetIntermediateCommits(ICommit? baseCommit, ICommit headCommit, IIgnoreConfiguration ignore) { - var map = GetHeadCommitsMap(headCommit); + var map = GetHeadCommitsMap(headCommit, ignore); var commitAfterBaseIndex = 0; if (baseCommit != null) @@ -159,7 +163,7 @@ private IEnumerable GetIntermediateCommits(ICommit? baseCommit, ICommit commitAfterBaseIndex = baseIndex + 1; } - var headCommits = GetHeadCommits(headCommit); + var headCommits = GetHeadCommits(headCommit, ignore); return new ArraySegment(headCommits, commitAfterBaseIndex, headCommits.Length - commitAfterBaseIndex); } @@ -167,9 +171,9 @@ private IEnumerable GetIntermediateCommits(ICommit? baseCommit, ICommit /// Get a mapping of commit shas to their zero-based position in the sequence of commits from the beginning of a /// repository to a particular /// - private Dictionary GetHeadCommitsMap(ICommit? headCommit) => + private Dictionary GetHeadCommitsMap(ICommit? headCommit, IIgnoreConfiguration ignore) => this.headCommitsMapCache.GetOrAdd(headCommit?.Sha ?? "NULL", () => - GetHeadCommits(headCommit) + GetHeadCommits(headCommit, ignore) .Select((commit, index) => (commit.Sha, Index: index)) .ToDictionary(t => t.Sha, t => t.Index)); @@ -177,9 +181,9 @@ private Dictionary GetHeadCommitsMap(ICommit? headCommit) => /// Get the sequence of commits from the beginning of a repository to a particular /// (inclusive) /// - private ICommit[] GetHeadCommits(ICommit? headCommit) => + private ICommit[] GetHeadCommits(ICommit? headCommit, IIgnoreConfiguration ignore) => this.headCommitsCache.GetOrAdd(headCommit?.Sha ?? "NULL", () => - GetCommitsReacheableFromHead(headCommit).ToArray()); + GetCommitsReacheableFromHead(headCommit, ignore).ToArray()); private VersionField? GetIncrementFromCommit(ICommit commit, Regex majorRegex, Regex minorRegex, Regex patchRegex, Regex none) => this.commitIncrementCache.GetOrAdd(commit.Sha, () => @@ -194,7 +198,7 @@ private ICommit[] GetHeadCommits(ICommit? headCommit) => return null; } - private IEnumerable GetCommitsReacheableFromHead(ICommit? headCommit) + private IEnumerable GetCommitsReacheableFromHead(ICommit? headCommit, IIgnoreConfiguration ignore) { var filter = new CommitFilter { @@ -202,10 +206,11 @@ private IEnumerable GetCommitsReacheableFromHead(ICommit? headCommit) SortBy = CommitSortStrategies.Topological | CommitSortStrategies.Reverse }; - return repository.Commits.QueryBy(filter); + var commits = repository.Commits.QueryBy(filter); + return ignore.Filter(commits); } - public IEnumerable GetMergedCommits(ICommit mergeCommit, int index) + public IEnumerable GetMergedCommits(ICommit mergeCommit, int index, IIgnoreConfiguration ignore) { mergeCommit.NotNull(); @@ -221,7 +226,7 @@ public IEnumerable GetMergedCommits(ICommit mergeCommit, int index) ICommit findMergeBase = this.repository.FindMergeBase(baseCommit, mergedCommit) ?? throw new InvalidOperationException("Cannot find the base commit of merged branch."); - return GetIntermediateCommits(findMergeBase, mergedCommit); + return GetIntermediateCommits(findMergeBase, mergedCommit, ignore); } private static ICommit GetMergedHead(ICommit mergeCommit) diff --git a/src/GitVersion.Core/VersionCalculation/TrunkBased/Trunk/CommitOnTrunkWithStableTagBase.cs b/src/GitVersion.Core/VersionCalculation/TrunkBased/Trunk/CommitOnTrunkWithStableTagBase.cs index 558554206c..d880036c63 100644 --- a/src/GitVersion.Core/VersionCalculation/TrunkBased/Trunk/CommitOnTrunkWithStableTagBase.cs +++ b/src/GitVersion.Core/VersionCalculation/TrunkBased/Trunk/CommitOnTrunkWithStableTagBase.cs @@ -19,7 +19,6 @@ public virtual IEnumerable GetIncrements(TrunkBasedIteration iter semanticVersion: context.SemanticVersion.NotNull() ); - context.Increment = VersionField.None; context.Label = commit.Configuration.GetBranchSpecificLabel(commit.BranchName, null); } } diff --git a/src/GitVersion.Core/VersionCalculation/TrunkBased/Trunk/LastCommitOnTrunkWithStableTag.cs b/src/GitVersion.Core/VersionCalculation/TrunkBased/Trunk/LastCommitOnTrunkWithStableTag.cs index b7647771f8..96e75b57a5 100644 --- a/src/GitVersion.Core/VersionCalculation/TrunkBased/Trunk/LastCommitOnTrunkWithStableTag.cs +++ b/src/GitVersion.Core/VersionCalculation/TrunkBased/Trunk/LastCommitOnTrunkWithStableTag.cs @@ -26,9 +26,6 @@ public override IEnumerable GetIncrements(TrunkBasedIteration ite label: context.Label, forceIncrement: context.ForceIncrement ); - - context.Increment = VersionField.None; - context.Label = null; } } } diff --git a/src/GitVersion.Core/VersionCalculation/VersionCalculators/ContinuousDeliveryVersionCalculator.cs b/src/GitVersion.Core/VersionCalculation/VersionCalculators/ContinuousDeliveryVersionCalculator.cs index 8e90c0e432..8cd7bd2044 100644 --- a/src/GitVersion.Core/VersionCalculation/VersionCalculators/ContinuousDeliveryVersionCalculator.cs +++ b/src/GitVersion.Core/VersionCalculation/VersionCalculators/ContinuousDeliveryVersionCalculator.cs @@ -3,7 +3,8 @@ namespace GitVersion.VersionCalculation; -internal sealed class ContinuousDeliveryVersionCalculator(ILog log, IRepositoryStore repositoryStore, Lazy versionContext) +internal sealed class ContinuousDeliveryVersionCalculator( + ILog log, IRepositoryStore repositoryStore, Lazy versionContext) : VersionCalculatorBase(log, repositoryStore, versionContext), IDeploymentModeCalculator { public SemanticVersion Calculate(SemanticVersion semanticVersion, ICommit? baseVersionSource) diff --git a/src/GitVersion.Core/VersionCalculation/VersionCalculators/ContinuousDeploymentVersionCalculator.cs b/src/GitVersion.Core/VersionCalculation/VersionCalculators/ContinuousDeploymentVersionCalculator.cs index 7ed5b72042..0808340627 100644 --- a/src/GitVersion.Core/VersionCalculation/VersionCalculators/ContinuousDeploymentVersionCalculator.cs +++ b/src/GitVersion.Core/VersionCalculation/VersionCalculators/ContinuousDeploymentVersionCalculator.cs @@ -3,7 +3,8 @@ namespace GitVersion.VersionCalculation; -internal sealed class ContinuousDeploymentVersionCalculator(ILog log, IRepositoryStore repositoryStore, Lazy versionContext) +internal sealed class ContinuousDeploymentVersionCalculator( + ILog log, IRepositoryStore repositoryStore, Lazy versionContext) : VersionCalculatorBase(log, repositoryStore, versionContext), IDeploymentModeCalculator { public SemanticVersion Calculate(SemanticVersion semanticVersion, ICommit? baseVersionSource) diff --git a/src/GitVersion.Core/VersionCalculation/VersionCalculators/ManualDeploymentVersionCalculator.cs b/src/GitVersion.Core/VersionCalculation/VersionCalculators/ManualDeploymentVersionCalculator.cs index 842bc6ad74..41364db63d 100644 --- a/src/GitVersion.Core/VersionCalculation/VersionCalculators/ManualDeploymentVersionCalculator.cs +++ b/src/GitVersion.Core/VersionCalculation/VersionCalculators/ManualDeploymentVersionCalculator.cs @@ -3,7 +3,8 @@ namespace GitVersion.VersionCalculation; -internal sealed class ManualDeploymentVersionCalculator(ILog log, IRepositoryStore repositoryStore, Lazy versionContext) +internal sealed class ManualDeploymentVersionCalculator( + ILog log, IRepositoryStore repositoryStore, Lazy versionContext) : VersionCalculatorBase(log, repositoryStore, versionContext), IDeploymentModeCalculator { public SemanticVersion Calculate(SemanticVersion semanticVersion, ICommit? baseVersionSource) diff --git a/src/GitVersion.Core/VersionCalculation/VersionCalculators/NextVersionCalculator.cs b/src/GitVersion.Core/VersionCalculation/VersionCalculators/NextVersionCalculator.cs index b2f2a8130e..2f30762a9c 100644 --- a/src/GitVersion.Core/VersionCalculation/VersionCalculators/NextVersionCalculator.cs +++ b/src/GitVersion.Core/VersionCalculation/VersionCalculators/NextVersionCalculator.cs @@ -50,7 +50,8 @@ public virtual SemanticVersion FindVersion() NextVersion nextVersion = CalculateNextVersion(Context.CurrentBranch, Context.Configuration); - if (Context.IsCurrentCommitTagged && someBranchRelatedPropertiesMightBeNotKnown && nextVersion.Configuration.PreventIncrementWhenCurrentCommitTagged) + if (Context.IsCurrentCommitTagged && someBranchRelatedPropertiesMightBeNotKnown + && nextVersion.Configuration.PreventIncrementWhenCurrentCommitTagged) { var allTaggedSemanticVersions = taggedSemanticVersionRepository.GetAllTaggedSemanticVersions( Context.Configuration, nextVersion.Configuration, Context.CurrentBranch, null, Context.CurrentCommit.When @@ -72,7 +73,10 @@ public virtual SemanticVersion FindVersion() var ignore = Context.Configuration.Ignore; var alternativeSemanticVersion = taggedSemanticVersionRepository.GetTaggedSemanticVersionsOfBranch( - nextVersion.BranchConfiguration.Branch, Context.Configuration.TagPrefix, Context.Configuration.SemanticVersionFormat + branch: nextVersion.BranchConfiguration.Branch, + tagPrefix: Context.Configuration.TagPrefix, + format: Context.Configuration.SemanticVersionFormat, + ignore: Context.Configuration.Ignore ).Where(element => element.Key.When <= Context.CurrentCommit.When && !(element.Key.When <= ignore.Before) && !ignore.Shas.Contains(element.Key.Sha) ).SelectMany(element => element).Max()?.Value; diff --git a/src/GitVersion.Core/VersionCalculation/VersionCalculators/VersionCalculatorBase.cs b/src/GitVersion.Core/VersionCalculation/VersionCalculators/VersionCalculatorBase.cs index b6115a2cd6..3c939332dc 100644 --- a/src/GitVersion.Core/VersionCalculation/VersionCalculators/VersionCalculatorBase.cs +++ b/src/GitVersion.Core/VersionCalculation/VersionCalculators/VersionCalculatorBase.cs @@ -4,7 +4,8 @@ namespace GitVersion.VersionCalculation; -internal abstract class VersionCalculatorBase(ILog log, IRepositoryStore repositoryStore, Lazy versionContext) +internal abstract class VersionCalculatorBase( + ILog log, IRepositoryStore repositoryStore, Lazy versionContext) { protected readonly ILog log = log.NotNull(); protected readonly IRepositoryStore repositoryStore = repositoryStore.NotNull(); @@ -14,16 +15,13 @@ internal abstract class VersionCalculatorBase(ILog log, IRepositoryStore reposit protected SemanticVersionBuildMetaData CreateVersionBuildMetaData(ICommit? baseVersionSource) { - var commitLogs = this.repositoryStore.GetCommitLog(baseVersionSource, Context.CurrentCommit); - - var ignore = Context.Configuration.Ignore; - if (!ignore.IsEmpty) - { - commitLogs = commitLogs - .Where(c => ignore.Before is null || (c.When > ignore.Before && !ignore.Shas.Contains(c.Sha))); - } - var commitsSinceTag = commitLogs.Count(); + var commitLogs = this.repositoryStore.GetCommitLog( + baseVersionSource: baseVersionSource, + currentCommit: Context.CurrentCommit, + ignore: Context.Configuration.Ignore + ); + int commitsSinceTag = commitLogs.Count; this.log.Info($"{commitsSinceTag} commits found between {baseVersionSource} and {Context.CurrentCommit}"); var shortSha = Context.CurrentCommit.Id.ToString(7); diff --git a/src/GitVersion.Core/VersionCalculation/VersionSearchStrategies/MergeMessageVersionStrategy.cs b/src/GitVersion.Core/VersionCalculation/VersionSearchStrategies/MergeMessageVersionStrategy.cs index 8cc479a5b2..9ea12dded1 100644 --- a/src/GitVersion.Core/VersionCalculation/VersionSearchStrategies/MergeMessageVersionStrategy.cs +++ b/src/GitVersion.Core/VersionCalculation/VersionSearchStrategies/MergeMessageVersionStrategy.cs @@ -22,7 +22,7 @@ public override IEnumerable GetBaseVersions(EffectiveBranchConfigur if (!Context.Configuration.VersionStrategy.HasFlag(VersionStrategies.MergeMessage) || !configuration.Value.TrackMergeMessage) return []; - var commitsPriorToThan = Context.CurrentBranch.Commits.GetCommitsPriorTo(Context.CurrentCommit.When); + var commitsPriorToThan = configuration.Value.Ignore.Filter(Context.CurrentBranchCommits); var baseVersions = commitsPriorToThan .SelectMany(commit => { diff --git a/src/GitVersion.Core/VersionCalculation/VersionSearchStrategies/TrunkBasedVersionStrategy.cs b/src/GitVersion.Core/VersionCalculation/VersionSearchStrategies/TrunkBasedVersionStrategy.cs index 5b6bbbcc02..e37c9ec087 100644 --- a/src/GitVersion.Core/VersionCalculation/VersionSearchStrategies/TrunkBasedVersionStrategy.cs +++ b/src/GitVersion.Core/VersionCalculation/VersionSearchStrategies/TrunkBasedVersionStrategy.cs @@ -71,13 +71,15 @@ public override IEnumerable GetBaseVersions(EffectiveBranchConfigur var iteration = CreateIteration(branchName: Context.CurrentBranch.Name, configuration: configuration.Value); + var commitsInReverseOrder = configuration.Value.Ignore.Filter(Context.CurrentBranchCommits); + var taggedSemanticVersions = taggedSemanticVersionRepository.GetAllTaggedSemanticVersions( Context.Configuration, configuration.Value, Context.CurrentBranch, null, Context.CurrentCommit.When ); var targetLabel = configuration.Value.GetBranchSpecificLabel(Context.CurrentBranch.Name, null); IterateOverCommitsRecursive( - commitsInReverseOrder: Context.CurrentBranch.Commits, + commitsInReverseOrder: commitsInReverseOrder, iteration: iteration, targetLabel: targetLabel, taggedSemanticVersions: taggedSemanticVersions @@ -140,7 +142,7 @@ private bool IterateOverCommitsRecursive( if (item.IsMergeCommit) { Lazy> mergedCommitsInReverseOrderLazy = new( - () => incrementStrategyFinder.GetMergedCommits(item, 1).Reverse().ToList() + () => incrementStrategyFinder.GetMergedCommits(item, 1, configuration.Ignore).Reverse().ToList() ); if (configuration.TrackMergeMessage @@ -162,7 +164,7 @@ private bool IterateOverCommitsRecursive( { if (configuration.IsMainBranch) throw new NotImplementedException(); mergedCommitsInReverseOrderLazy = new( - () => incrementStrategyFinder.GetMergedCommits(item, 0).Reverse().ToList() + () => incrementStrategyFinder.GetMergedCommits(item, 0, configuration.Ignore).Reverse().ToList() ); childConfiguration = configuration; }