diff --git a/src/GitVersion.BuildAgents.Tests/Agents/BitBucketPipelinesTests.cs b/src/GitVersion.BuildAgents.Tests/Agents/BitBucketPipelinesTests.cs index f8fc58c092..12951340a1 100644 --- a/src/GitVersion.BuildAgents.Tests/Agents/BitBucketPipelinesTests.cs +++ b/src/GitVersion.BuildAgents.Tests/Agents/BitBucketPipelinesTests.cs @@ -139,16 +139,17 @@ private void AssertVariablesAreWrittenToFile(string file) Minor = 2, Patch = 3, PreReleaseTag = "beta1", - BuildMetaData = "5" + BuildMetaData = new SemanticVersionBuildMetaData("5") + { + Sha = "f28807e615e9f06aec8a33c87780374e0c1f6fb8", + CommitDate = new DateTimeOffset(2022, 4, 6, 16, 10, 59, TimeSpan.FromHours(10)) + } }; - semanticVersion.BuildMetaData.CommitDate = new DateTimeOffset(2022, 4, 6, 16, 10, 59, TimeSpan.FromHours(10)); - semanticVersion.BuildMetaData.Sha = "f28807e615e9f06aec8a33c87780374e0c1f6fb8"; - var configuration = new TestEffectiveConfiguration(); var variableProvider = this.sp.GetRequiredService(); - var variables = variableProvider.GetVariablesFor(semanticVersion, configuration, false); + var variables = variableProvider.GetVariablesFor(semanticVersion, configuration, null); this.buildServer.WithPropertyFile(file); diff --git a/src/GitVersion.BuildAgents.Tests/Agents/BuildServerBaseTests.cs b/src/GitVersion.BuildAgents.Tests/Agents/BuildServerBaseTests.cs index b2fbd29ffc..a8cf2d29d8 100644 --- a/src/GitVersion.BuildAgents.Tests/Agents/BuildServerBaseTests.cs +++ b/src/GitVersion.BuildAgents.Tests/Agents/BuildServerBaseTests.cs @@ -29,15 +29,16 @@ public void BuildNumberIsFullSemVer() Minor = 2, Patch = 3, PreReleaseTag = "beta1", - BuildMetaData = "5" + BuildMetaData = new SemanticVersionBuildMetaData("5") + { + Sha = "commitSha", + CommitDate = DateTimeOffset.Parse("2014-03-06 23:59:59Z") + } }; - semanticVersion.BuildMetaData.CommitDate = DateTimeOffset.Parse("2014-03-06 23:59:59Z"); - semanticVersion.BuildMetaData.Sha = "commitSha"; - var configuration = new TestEffectiveConfiguration(); - var variables = this.buildServer.GetVariablesFor(semanticVersion, configuration, false); + var variables = this.buildServer.GetVariablesFor(semanticVersion, configuration, null); var buildAgent = this.sp.GetRequiredService(); buildAgent.WriteIntegration(writes.Add, variables); diff --git a/src/GitVersion.BuildAgents.Tests/Agents/CodeBuildTests.cs b/src/GitVersion.BuildAgents.Tests/Agents/CodeBuildTests.cs index 97ce3eb3e6..e353210a8a 100644 --- a/src/GitVersion.BuildAgents.Tests/Agents/CodeBuildTests.cs +++ b/src/GitVersion.BuildAgents.Tests/Agents/CodeBuildTests.cs @@ -74,17 +74,18 @@ private void AssertVariablesAreWrittenToFile(string file) Minor = 2, Patch = 3, PreReleaseTag = "beta1", - BuildMetaData = "5" + BuildMetaData = new SemanticVersionBuildMetaData("5") + { + Sha = "commitSha", + CommitDate = DateTimeOffset.Parse("2014-03-06 23:59:59Z") + } }; - semanticVersion.BuildMetaData.CommitDate = DateTimeOffset.Parse("2014-03-06 23:59:59Z"); - semanticVersion.BuildMetaData.Sha = "commitSha"; - var configuration = new TestEffectiveConfiguration(); var variableProvider = this.sp.GetRequiredService(); - var variables = variableProvider.GetVariablesFor(semanticVersion, configuration, false); + var variables = variableProvider.GetVariablesFor(semanticVersion, configuration, null); this.buildServer.WithPropertyFile(file); diff --git a/src/GitVersion.BuildAgents.Tests/Agents/GitLabCiTests.cs b/src/GitVersion.BuildAgents.Tests/Agents/GitLabCiTests.cs index 9721aa7907..324d03945e 100644 --- a/src/GitVersion.BuildAgents.Tests/Agents/GitLabCiTests.cs +++ b/src/GitVersion.BuildAgents.Tests/Agents/GitLabCiTests.cs @@ -59,16 +59,17 @@ private void AssertVariablesAreWrittenToFile(string file) Minor = 2, Patch = 3, PreReleaseTag = "beta1", - BuildMetaData = "5" + BuildMetaData = new SemanticVersionBuildMetaData("5") + { + Sha = "commitSha", + CommitDate = DateTimeOffset.Parse("2014-03-06 23:59:59Z") + } }; - semanticVersion.BuildMetaData.CommitDate = DateTimeOffset.Parse("2014-03-06 23:59:59Z"); - semanticVersion.BuildMetaData.Sha = "commitSha"; - var configuration = new TestEffectiveConfiguration(); var variableProvider = this.sp.GetRequiredService(); - var variables = variableProvider.GetVariablesFor(semanticVersion, configuration, false); + var variables = variableProvider.GetVariablesFor(semanticVersion, configuration, null); this.buildServer.WithPropertyFile(file); diff --git a/src/GitVersion.BuildAgents.Tests/Agents/JenkinsTests.cs b/src/GitVersion.BuildAgents.Tests/Agents/JenkinsTests.cs index 89da45896c..8918f4fc50 100644 --- a/src/GitVersion.BuildAgents.Tests/Agents/JenkinsTests.cs +++ b/src/GitVersion.BuildAgents.Tests/Agents/JenkinsTests.cs @@ -130,17 +130,18 @@ private void AssertVariablesAreWrittenToFile(string file) Minor = 2, Patch = 3, PreReleaseTag = "beta1", - BuildMetaData = "5" + BuildMetaData = new SemanticVersionBuildMetaData("5") + { + Sha = "commitSha", + CommitDate = DateTimeOffset.Parse("2014-03-06 23:59:59Z") + } }; - semanticVersion.BuildMetaData.CommitDate = DateTimeOffset.Parse("2014-03-06 23:59:59Z"); - semanticVersion.BuildMetaData.Sha = "commitSha"; - var configuration = new TestEffectiveConfiguration(); var variableProvider = this.sp.GetRequiredService(); - var variables = variableProvider.GetVariablesFor(semanticVersion, configuration, false); + var variables = variableProvider.GetVariablesFor(semanticVersion, configuration, null); this.buildServer.WithPropertyFile(file); diff --git a/src/GitVersion.Core.Tests/Extensions/GitToolsTestingExtensions.cs b/src/GitVersion.Core.Tests/Extensions/GitToolsTestingExtensions.cs index 93d6309302..d527f86398 100644 --- a/src/GitVersion.Core.Tests/Extensions/GitToolsTestingExtensions.cs +++ b/src/GitVersion.Core.Tests/Extensions/GitToolsTestingExtensions.cs @@ -91,7 +91,9 @@ public static VersionVariables GetVersion(this RepositoryFixtureBase fixture, IG var context = contextOptions.Value; var nextVersion = nextVersionCalculator.FindVersion(); - return variableProvider.GetVariablesFor(nextVersion.IncrementedVersion, nextVersion.Configuration, context.IsCurrentCommitTagged); + return variableProvider.GetVariablesFor( + nextVersion.IncrementedVersion, nextVersion.Configuration, context.CurrentCommitTaggedVersion + ); } catch (Exception) { diff --git a/src/GitVersion.Core.Tests/IntegrationTests/OtherBranchScenarios.cs b/src/GitVersion.Core.Tests/IntegrationTests/OtherBranchScenarios.cs index 86763aae65..6344f1828a 100644 --- a/src/GitVersion.Core.Tests/IntegrationTests/OtherBranchScenarios.cs +++ b/src/GitVersion.Core.Tests/IntegrationTests/OtherBranchScenarios.cs @@ -7,6 +7,29 @@ namespace GitVersion.Core.Tests.IntegrationTests; [TestFixture] public class OtherBranchScenarios : TestBase { + /// + /// https://github.com/GitTools/GitVersion/issues/2340 + /// + [Test] + public void ShouldOnlyConsiderTagsMatchingOfCurrentBranch() + { + var configuration = GitFlowConfigurationBuilder.New + .WithBranch("develop", builder => builder.WithLabel("snapshot")) + .WithBranch("release", builder => builder.WithLabel("rc")) + .Build(); + + using var fixture = new EmptyRepositoryFixture(); + + fixture.MakeACommit(); + fixture.BranchTo("develop"); + fixture.MakeACommit(); + fixture.MakeATaggedCommit("0.1.2-snapshot.2"); + fixture.BranchTo("release/0.1.2"); + + // ✅ succeeds as expected + fixture.AssertFullSemver("0.1.2-rc.1+0", configuration); + } + [Test] public void CanTakeVersionFromReleaseBranch() { diff --git a/src/GitVersion.Core.Tests/VersionCalculation/JsonVersionBuilderTests.cs b/src/GitVersion.Core.Tests/VersionCalculation/JsonVersionBuilderTests.cs index eab38dfd5b..2f5a36e73f 100644 --- a/src/GitVersion.Core.Tests/VersionCalculation/JsonVersionBuilderTests.cs +++ b/src/GitVersion.Core.Tests/VersionCalculation/JsonVersionBuilderTests.cs @@ -27,7 +27,7 @@ public void Json() var serviceProvider = ConfigureServices(); var variableProvider = serviceProvider.GetRequiredService(); - var variables = variableProvider.GetVariablesFor(semanticVersion, configuration, false); + var variables = variableProvider.GetVariablesFor(semanticVersion, configuration, null); var json = variables.ToString(); json.ShouldMatchApproved(c => c.SubFolder("Approved")); } diff --git a/src/GitVersion.Core.Tests/VersionCalculation/SemanticVersionTests.cs b/src/GitVersion.Core.Tests/VersionCalculation/SemanticVersionTests.cs index 7bb53cc423..8c6042177c 100644 --- a/src/GitVersion.Core.Tests/VersionCalculation/SemanticVersionTests.cs +++ b/src/GitVersion.Core.Tests/VersionCalculation/SemanticVersionTests.cs @@ -131,16 +131,20 @@ public string ToStringWithFormatITests(int major, int minor, int patch, string p return semVer.ToString("i"); } - private static SemanticVersion BuildSemVer(int major, int minor, int patch, string? preReleaseName, int? preReleaseVersion, int? buildCount, string? branchName = null, string? sha = null, string? otherMetadata = null) + private static SemanticVersion BuildSemVer(int major, int minor, int patch, string? preReleaseName, int? preReleaseVersion, + int? buildCount, string? branchName = null, string? sha = null, string? otherMetadata = null) { - var semVer = new SemanticVersion(major, minor, patch); + + SemanticVersionPreReleaseTag? preReleaseTag = null; if (preReleaseName != null) { - semVer.PreReleaseTag = new SemanticVersionPreReleaseTag(preReleaseName, preReleaseVersion); + preReleaseTag = new SemanticVersionPreReleaseTag(preReleaseName, preReleaseVersion); } + + SemanticVersionBuildMetaData? buildMetaDate = null; if (buildCount.HasValue) { - semVer.BuildMetaData = new SemanticVersionBuildMetaData + buildMetaDate = new SemanticVersionBuildMetaData { CommitsSinceTag = buildCount.Value, Sha = sha, @@ -149,6 +153,10 @@ private static SemanticVersion BuildSemVer(int major, int minor, int patch, stri }; } - return semVer; + return new SemanticVersion(major, minor, patch) + { + PreReleaseTag = preReleaseTag ?? new(), + BuildMetaData = buildMetaDate ?? new() + }; } } diff --git a/src/GitVersion.Core.Tests/VersionCalculation/VariableProviderTests.cs b/src/GitVersion.Core.Tests/VersionCalculation/VariableProviderTests.cs index 487fdfff90..0168c2be9e 100644 --- a/src/GitVersion.Core.Tests/VersionCalculation/VariableProviderTests.cs +++ b/src/GitVersion.Core.Tests/VersionCalculation/VariableProviderTests.cs @@ -36,17 +36,18 @@ public void ProvidesVariablesInContinuousDeliveryModeForPreRelease() Minor = 2, Patch = 3, PreReleaseTag = "unstable.4", - BuildMetaData = "5.Branch.develop" + BuildMetaData = new SemanticVersionBuildMetaData("5.Branch.develop") + { + VersionSourceSha = "versionSourceSha", + Sha = "commitSha", + ShortSha = "commitShortSha", + CommitDate = DateTimeOffset.Parse("2014-03-06 23:59:59Z") + } }; - semVer.BuildMetaData.VersionSourceSha = "versionSourceSha"; - semVer.BuildMetaData.Sha = "commitSha"; - semVer.BuildMetaData.ShortSha = "commitShortSha"; - semVer.BuildMetaData.CommitDate = DateTimeOffset.Parse("2014-03-06 23:59:59Z"); - var configuration = new TestEffectiveConfiguration(); - var vars = this.variableProvider.GetVariablesFor(semVer, configuration, false); + var vars = this.variableProvider.GetVariablesFor(semVer, configuration, null); vars.ToString().ShouldMatchApproved(c => c.SubFolder("Approved")); } @@ -60,17 +61,18 @@ public void ProvidesVariablesInContinuousDeploymentModeForPreRelease() Minor = 2, Patch = 3, PreReleaseTag = "unstable.4", - BuildMetaData = "5.Branch.develop" + BuildMetaData = new SemanticVersionBuildMetaData("5.Branch.develop") + { + VersionSourceSha = "versionSourceSha", + Sha = "commitSha", + ShortSha = "commitShortSha", + CommitDate = DateTimeOffset.Parse("2014-03-06 23:59:59Z") + } }; - semVer.BuildMetaData.VersionSourceSha = "versionSourceSha"; - semVer.BuildMetaData.Sha = "commitSha"; - semVer.BuildMetaData.ShortSha = "commitShortSha"; - semVer.BuildMetaData.CommitDate = DateTimeOffset.Parse("2014-03-06 23:59:59Z"); - var configuration = new TestEffectiveConfiguration(versioningMode: VersioningMode.ContinuousDeployment); - var vars = this.variableProvider.GetVariablesFor(semVer, configuration, false); + var vars = this.variableProvider.GetVariablesFor(semVer, configuration, null); vars.ToString().ShouldMatchApproved(c => c.SubFolder("Approved")); } @@ -83,17 +85,18 @@ public void ProvidesVariablesInContinuousDeliveryModeForStable() Major = 1, Minor = 2, Patch = 3, - BuildMetaData = "5.Branch.develop" + BuildMetaData = new SemanticVersionBuildMetaData("5.Branch.develop") + { + VersionSourceSha = "versionSourceSha", + Sha = "commitSha", + ShortSha = "commitShortSha", + CommitDate = DateTimeOffset.Parse("2014-03-06 23:59:59Z") + } }; - semVer.BuildMetaData.VersionSourceSha = "versionSourceSha"; - semVer.BuildMetaData.Sha = "commitSha"; - semVer.BuildMetaData.ShortSha = "commitShortSha"; - semVer.BuildMetaData.CommitDate = DateTimeOffset.Parse("2014-03-06 23:59:59Z"); - var configuration = new TestEffectiveConfiguration(); - var vars = this.variableProvider.GetVariablesFor(semVer, configuration, false); + var vars = this.variableProvider.GetVariablesFor(semVer, configuration, null); vars.ToString().ShouldMatchApproved(c => c.SubFolder("Approved")); } @@ -106,17 +109,18 @@ public void ProvidesVariablesInContinuousDeploymentModeForStable() Major = 1, Minor = 2, Patch = 3, - BuildMetaData = "5.Branch.develop" + BuildMetaData = new SemanticVersionBuildMetaData("5.Branch.develop") + { + VersionSourceSha = "versionSourceSha", + Sha = "commitSha", + ShortSha = "commitShortSha", + CommitDate = DateTimeOffset.Parse("2014-03-06 23:59:59Z") + } }; - semVer.BuildMetaData.VersionSourceSha = "versionSourceSha"; - semVer.BuildMetaData.Sha = "commitSha"; - semVer.BuildMetaData.ShortSha = "commitShortSha"; - semVer.BuildMetaData.CommitDate = DateTimeOffset.Parse("2014-03-06 23:59:59Z"); - var configuration = new TestEffectiveConfiguration(versioningMode: VersioningMode.ContinuousDeployment, label: "ci"); - var vars = this.variableProvider.GetVariablesFor(semVer, configuration, false); + var vars = this.variableProvider.GetVariablesFor(semVer, configuration, null); vars.ToString().ShouldMatchApproved(c => c.SubFolder("Approved")); } @@ -142,7 +146,7 @@ public void ProvidesVariablesInContinuousDeploymentModeForStableWhenCurrentCommi var configuration = new TestEffectiveConfiguration(versioningMode: VersioningMode.ContinuousDeployment); - var vars = this.variableProvider.GetVariablesFor(semVer, configuration, true); + var vars = this.variableProvider.GetVariablesFor(semVer, configuration, SemanticVersion.Empty); vars.ToString().ShouldMatchApproved(c => c.SubFolder("Approved")); } @@ -156,16 +160,17 @@ public void ProvidesVariablesInContinuousDeploymentModeWithTagNamePattern() Minor = 2, Patch = 3, PreReleaseTag = "PullRequest", - BuildMetaData = "5.Branch.develop" + BuildMetaData = new SemanticVersionBuildMetaData("5.Branch.develop") + { + Branch = "pull/2/merge", + Sha = "commitSha", + ShortSha = "commitShortSha", + CommitDate = DateTimeOffset.Parse("2014-03-06 23:59:59Z") + } }; - semVer.BuildMetaData.Branch = "pull/2/merge"; - semVer.BuildMetaData.Sha = "commitSha"; - semVer.BuildMetaData.ShortSha = "commitShortSha"; - semVer.BuildMetaData.CommitDate = DateTimeOffset.Parse("2014-03-06 23:59:59Z"); - var configuration = new TestEffectiveConfiguration(versioningMode: VersioningMode.ContinuousDeployment, labelNumberPattern: @"[/-](?\d+)[-/]"); - var vars = this.variableProvider.GetVariablesFor(semVer, configuration, false); + var vars = this.variableProvider.GetVariablesFor(semVer, configuration, null); vars.FullSemVer.ShouldBe("1.2.3-PullRequest2.5"); } @@ -178,16 +183,17 @@ public void ProvidesVariablesInContinuousDeploymentModeWithTagSetToUseBranchName Major = 1, Minor = 2, Patch = 3, - BuildMetaData = "5.Branch.develop" + BuildMetaData = new SemanticVersionBuildMetaData("5.Branch.develop") + { + Branch = "feature", + Sha = "commitSha", + ShortSha = "commitShortSha", + CommitDate = DateTimeOffset.Parse("2014-03-06 23:59:59Z") + } }; - semVer.BuildMetaData.Branch = "feature"; - semVer.BuildMetaData.Sha = "commitSha"; - semVer.BuildMetaData.ShortSha = "commitShortSha"; - semVer.BuildMetaData.CommitDate = DateTimeOffset.Parse("2014-03-06 23:59:59Z"); - var configuration = new TestEffectiveConfiguration(versioningMode: VersioningMode.ContinuousDeployment, label: "useBranchName"); - var vars = this.variableProvider.GetVariablesFor(semVer, configuration, false); + var vars = this.variableProvider.GetVariablesFor(semVer, configuration, null); vars.FullSemVer.ShouldBe("1.2.3-feature.5"); } @@ -200,18 +206,19 @@ public void ProvidesVariablesInContinuousDeliveryModeForFeatureBranch() Major = 1, Minor = 2, Patch = 3, - BuildMetaData = "5.Branch.feature/123" + BuildMetaData = new SemanticVersionBuildMetaData("5.Branch.feature/123") + { + Branch = "feature/123", + VersionSourceSha = "versionSourceSha", + Sha = "commitSha", + ShortSha = "commitShortSha", + CommitDate = DateTimeOffset.Parse("2014-03-06 23:59:59Z") + } }; - semVer.BuildMetaData.Branch = "feature/123"; - semVer.BuildMetaData.VersionSourceSha = "versionSourceSha"; - semVer.BuildMetaData.Sha = "commitSha"; - semVer.BuildMetaData.ShortSha = "commitShortSha"; - semVer.BuildMetaData.CommitDate = DateTimeOffset.Parse("2014-03-06 23:59:59Z"); - var configuration = new TestEffectiveConfiguration(); - var vars = this.variableProvider.GetVariablesFor(semVer, configuration, false); + var vars = this.variableProvider.GetVariablesFor(semVer, configuration, null); vars.ToString().ShouldMatchApproved(c => c.SubFolder("Approved")); } @@ -224,18 +231,21 @@ public void ProvidesVariablesInContinuousDeliveryModeForFeatureBranchWithCustomA Major = 1, Minor = 2, Patch = 3, - BuildMetaData = "5.Branch.feature/123" + BuildMetaData = new SemanticVersionBuildMetaData("5.Branch.feature/123") + { + Branch = "feature/123", + VersionSourceSha = "versionSourceSha", + Sha = "commitSha", + ShortSha = "commitShortSha", + CommitDate = DateTimeOffset.Parse("2014-03-06 23:59:59Z") + } }; - semVer.BuildMetaData.Branch = "feature/123"; - semVer.BuildMetaData.VersionSourceSha = "versionSourceSha"; - semVer.BuildMetaData.Sha = "commitSha"; - semVer.BuildMetaData.ShortSha = "commitShortSha"; - semVer.BuildMetaData.CommitDate = DateTimeOffset.Parse("2014-03-06 23:59:59Z"); - - var configuration = new TestEffectiveConfiguration(assemblyInformationalFormat: "{Major}.{Minor}.{Patch}+{CommitsSinceVersionSource}.Branch.{BranchName}.Sha.{ShortSha}"); + var configuration = new TestEffectiveConfiguration( + assemblyInformationalFormat: "{Major}.{Minor}.{Patch}+{CommitsSinceVersionSource}.Branch.{BranchName}.Sha.{ShortSha}" + ); - var vars = this.variableProvider.GetVariablesFor(semVer, configuration, false); + var vars = this.variableProvider.GetVariablesFor(semVer, configuration, null); vars.ToString().ShouldMatchApproved(c => c.SubFolder("Approved")); } diff --git a/src/GitVersion.Core/Core/GitVersionCalculateTool.cs b/src/GitVersion.Core/Core/GitVersionCalculateTool.cs index 425c478d87..013792c032 100644 --- a/src/GitVersion.Core/Core/GitVersionCalculateTool.cs +++ b/src/GitVersion.Core/Core/GitVersionCalculateTool.cs @@ -50,7 +50,9 @@ public VersionVariables CalculateVersionVariables() if (versionVariables != null) return versionVariables; var nextVersion = this.nextVersionCalculator.FindVersion(); - versionVariables = this.variableProvider.GetVariablesFor(nextVersion.IncrementedVersion, nextVersion.Configuration, context.IsCurrentCommitTagged); + versionVariables = this.variableProvider.GetVariablesFor( + nextVersion.IncrementedVersion, nextVersion.Configuration, context.CurrentCommitTaggedVersion + ); if (gitVersionOptions.Settings.NoCache) return versionVariables; try diff --git a/src/GitVersion.Core/PublicAPI.Unshipped.txt b/src/GitVersion.Core/PublicAPI.Unshipped.txt index ff8aaf4dcb..653b071699 100644 --- a/src/GitVersion.Core/PublicAPI.Unshipped.txt +++ b/src/GitVersion.Core/PublicAPI.Unshipped.txt @@ -760,7 +760,8 @@ GitVersion.RepositoryStore.GetVersionTagsOnBranch(GitVersion.IBranch! branch, st GitVersion.RepositoryStore.IsCommitOnBranch(GitVersion.ICommit? baseVersionSource, GitVersion.IBranch! branch, GitVersion.ICommit! firstMatchingCommit) -> bool GitVersion.RepositoryStore.RepositoryStore(GitVersion.Logging.ILog! log, GitVersion.IGitRepository! repository) -> void GitVersion.SemanticVersion -GitVersion.SemanticVersion.BuildMetaData -> GitVersion.SemanticVersionBuildMetaData! +GitVersion.SemanticVersion.BuildMetaData.get -> GitVersion.SemanticVersionBuildMetaData! +GitVersion.SemanticVersion.BuildMetaData.init -> void GitVersion.SemanticVersion.CompareTo(GitVersion.SemanticVersion? value) -> int GitVersion.SemanticVersion.CompareTo(GitVersion.SemanticVersion? value, bool includePrerelease) -> int GitVersion.SemanticVersion.Equals(GitVersion.SemanticVersion? obj) -> bool @@ -768,30 +769,43 @@ GitVersion.SemanticVersion.IncrementVersion(GitVersion.VersionField incrementStr GitVersion.SemanticVersion.IsEmpty() -> bool GitVersion.SemanticVersion.IsLabeledWith(string! value) -> bool GitVersion.SemanticVersion.IsMatchForBranchSpecificLabel(string? value) -> bool -GitVersion.SemanticVersion.Major -> long -GitVersion.SemanticVersion.Minor -> long -GitVersion.SemanticVersion.Patch -> long -GitVersion.SemanticVersion.PreReleaseTag -> GitVersion.SemanticVersionPreReleaseTag! +GitVersion.SemanticVersion.Major.get -> long +GitVersion.SemanticVersion.Major.init -> void +GitVersion.SemanticVersion.Minor.get -> long +GitVersion.SemanticVersion.Minor.init -> void +GitVersion.SemanticVersion.Patch.get -> long +GitVersion.SemanticVersion.Patch.init -> void +GitVersion.SemanticVersion.PreReleaseTag.get -> GitVersion.SemanticVersionPreReleaseTag! +GitVersion.SemanticVersion.PreReleaseTag.init -> void GitVersion.SemanticVersion.SemanticVersion(GitVersion.SemanticVersion! semanticVersion) -> void GitVersion.SemanticVersion.SemanticVersion(long major = 0, long minor = 0, long patch = 0) -> void GitVersion.SemanticVersion.ToString(string! format) -> string! GitVersion.SemanticVersion.ToString(string? format, System.IFormatProvider? formatProvider) -> string! GitVersion.SemanticVersionBuildMetaData -GitVersion.SemanticVersionBuildMetaData.Branch -> string? -GitVersion.SemanticVersionBuildMetaData.CommitDate -> System.DateTimeOffset? -GitVersion.SemanticVersionBuildMetaData.CommitsSinceTag -> long? -GitVersion.SemanticVersionBuildMetaData.CommitsSinceVersionSource -> long? +GitVersion.SemanticVersionBuildMetaData.Branch.get -> string? +GitVersion.SemanticVersionBuildMetaData.Branch.init -> void +GitVersion.SemanticVersionBuildMetaData.CommitDate.get -> System.DateTimeOffset? +GitVersion.SemanticVersionBuildMetaData.CommitDate.init -> void +GitVersion.SemanticVersionBuildMetaData.CommitsSinceTag.get -> long? +GitVersion.SemanticVersionBuildMetaData.CommitsSinceTag.init -> void +GitVersion.SemanticVersionBuildMetaData.CommitsSinceVersionSource.get -> long? +GitVersion.SemanticVersionBuildMetaData.CommitsSinceVersionSource.init -> void GitVersion.SemanticVersionBuildMetaData.Equals(GitVersion.SemanticVersionBuildMetaData? other) -> bool -GitVersion.SemanticVersionBuildMetaData.OtherMetaData -> string? +GitVersion.SemanticVersionBuildMetaData.OtherMetaData.get -> string? +GitVersion.SemanticVersionBuildMetaData.OtherMetaData.init -> void GitVersion.SemanticVersionBuildMetaData.SemanticVersionBuildMetaData() -> void -GitVersion.SemanticVersionBuildMetaData.SemanticVersionBuildMetaData(GitVersion.SemanticVersionBuildMetaData? buildMetaData) -> void +GitVersion.SemanticVersionBuildMetaData.SemanticVersionBuildMetaData(GitVersion.SemanticVersionBuildMetaData! buildMetaData) -> void GitVersion.SemanticVersionBuildMetaData.SemanticVersionBuildMetaData(string? versionSourceSha, int? commitsSinceTag, string? branch, string? commitSha, string? commitShortSha, System.DateTimeOffset? commitDate, int numberOfUnCommittedChanges, string? otherMetadata = null) -> void -GitVersion.SemanticVersionBuildMetaData.Sha -> string? -GitVersion.SemanticVersionBuildMetaData.ShortSha -> string? +GitVersion.SemanticVersionBuildMetaData.Sha.get -> string? +GitVersion.SemanticVersionBuildMetaData.Sha.init -> void +GitVersion.SemanticVersionBuildMetaData.ShortSha.get -> string? +GitVersion.SemanticVersionBuildMetaData.ShortSha.init -> void GitVersion.SemanticVersionBuildMetaData.ToString(string! format) -> string! GitVersion.SemanticVersionBuildMetaData.ToString(string? format, System.IFormatProvider? formatProvider) -> string! -GitVersion.SemanticVersionBuildMetaData.UncommittedChanges -> long -GitVersion.SemanticVersionBuildMetaData.VersionSourceSha -> string? +GitVersion.SemanticVersionBuildMetaData.UncommittedChanges.get -> long +GitVersion.SemanticVersionBuildMetaData.UncommittedChanges.init -> void +GitVersion.SemanticVersionBuildMetaData.VersionSourceSha.get -> string? +GitVersion.SemanticVersionBuildMetaData.VersionSourceSha.init -> void GitVersion.SemanticVersionFormat GitVersion.SemanticVersionFormat.Loose = 1 -> GitVersion.SemanticVersionFormat GitVersion.SemanticVersionFormat.Strict = 0 -> GitVersion.SemanticVersionFormat @@ -827,11 +841,11 @@ GitVersion.SemanticVersionPreReleaseTag.CompareTo(GitVersion.SemanticVersionPreR GitVersion.SemanticVersionPreReleaseTag.Equals(GitVersion.SemanticVersionPreReleaseTag? other) -> bool GitVersion.SemanticVersionPreReleaseTag.HasTag() -> bool GitVersion.SemanticVersionPreReleaseTag.Name.get -> string! -GitVersion.SemanticVersionPreReleaseTag.Name.set -> void +GitVersion.SemanticVersionPreReleaseTag.Name.init -> void GitVersion.SemanticVersionPreReleaseTag.Number.get -> long? -GitVersion.SemanticVersionPreReleaseTag.Number.set -> void +GitVersion.SemanticVersionPreReleaseTag.Number.init -> void GitVersion.SemanticVersionPreReleaseTag.PromotedFromCommits.get -> bool? -GitVersion.SemanticVersionPreReleaseTag.PromotedFromCommits.set -> void +GitVersion.SemanticVersionPreReleaseTag.PromotedFromCommits.init -> void GitVersion.SemanticVersionPreReleaseTag.SemanticVersionPreReleaseTag() -> void GitVersion.SemanticVersionPreReleaseTag.SemanticVersionPreReleaseTag(GitVersion.SemanticVersionPreReleaseTag! preReleaseTag) -> void GitVersion.SemanticVersionPreReleaseTag.SemanticVersionPreReleaseTag(string! name, long? number) -> void @@ -900,7 +914,7 @@ GitVersion.VersionCalculation.IncrementStrategyFinder.GetIncrementForCommits(str GitVersion.VersionCalculation.INextVersionCalculator GitVersion.VersionCalculation.INextVersionCalculator.FindVersion() -> GitVersion.VersionCalculation.NextVersion! GitVersion.VersionCalculation.IVariableProvider -GitVersion.VersionCalculation.IVariableProvider.GetVariablesFor(GitVersion.SemanticVersion! semanticVersion, GitVersion.Configuration.EffectiveConfiguration! configuration, bool isCurrentCommitTagged) -> GitVersion.OutputVariables.VersionVariables! +GitVersion.VersionCalculation.IVariableProvider.GetVariablesFor(GitVersion.SemanticVersion! semanticVersion, GitVersion.Configuration.EffectiveConfiguration! configuration, GitVersion.SemanticVersion? currentCommitTaggedVersion) -> GitVersion.OutputVariables.VersionVariables! GitVersion.VersionCalculation.IVersionFilter GitVersion.VersionCalculation.IVersionFilter.Exclude(GitVersion.VersionCalculation.BaseVersion! version, out string? reason) -> bool GitVersion.VersionCalculation.IVersionStrategy @@ -931,8 +945,8 @@ GitVersion.VersionCalculation.TaggedCommitVersionStrategy.TaggedCommitVersionStr GitVersion.VersionCalculation.TrackReleaseBranchesVersionStrategy GitVersion.VersionCalculation.TrackReleaseBranchesVersionStrategy.TrackReleaseBranchesVersionStrategy(GitVersion.Common.IRepositoryStore! repositoryStore, System.Lazy! versionContext) -> void GitVersion.VersionCalculation.VariableProvider -GitVersion.VersionCalculation.VariableProvider.GetVariablesFor(GitVersion.SemanticVersion! semanticVersion, GitVersion.Configuration.EffectiveConfiguration! configuration, bool isCurrentCommitTagged) -> GitVersion.OutputVariables.VersionVariables! -GitVersion.VersionCalculation.VariableProvider.VariableProvider(GitVersion.IEnvironment! environment, GitVersion.Logging.ILog! log) -> void +GitVersion.VersionCalculation.VariableProvider.GetVariablesFor(GitVersion.SemanticVersion! semanticVersion, GitVersion.Configuration.EffectiveConfiguration! configuration, GitVersion.SemanticVersion? currentCommitTaggedVersion) -> GitVersion.OutputVariables.VersionVariables! +GitVersion.VersionCalculation.VariableProvider.VariableProvider(GitVersion.IEnvironment! environment) -> void GitVersion.VersionCalculation.VersionCalculationModule GitVersion.VersionCalculation.VersionCalculationModule.RegisterTypes(Microsoft.Extensions.DependencyInjection.IServiceCollection! services) -> void GitVersion.VersionCalculation.VersionCalculationModule.VersionCalculationModule() -> void @@ -1209,6 +1223,8 @@ static readonly GitVersion.Helpers.StringComparerUtils.OsDependentComparer -> Sy static readonly GitVersion.Helpers.StringComparerUtils.OsDependentComparison -> System.StringComparison static readonly GitVersion.Logging.Disposable.Empty -> System.IDisposable! static readonly GitVersion.SemanticVersion.Empty -> GitVersion.SemanticVersion! +static readonly GitVersion.SemanticVersionBuildMetaData.Empty -> GitVersion.SemanticVersionBuildMetaData! +static readonly GitVersion.SemanticVersionPreReleaseTag.Empty -> GitVersion.SemanticVersionPreReleaseTag! virtual GitVersion.Agents.BuildAgentBase.CanApplyToCurrentContext() -> bool virtual GitVersion.Agents.BuildAgentBase.GetCurrentBranch(bool usingDynamicRepos) -> string? virtual GitVersion.Agents.BuildAgentBase.IsDefault.get -> bool diff --git a/src/GitVersion.Core/VersionCalculation/Abstractions/IVariableProvider.cs b/src/GitVersion.Core/VersionCalculation/Abstractions/IVariableProvider.cs index 0cfeec8562..58f3e4d2e9 100644 --- a/src/GitVersion.Core/VersionCalculation/Abstractions/IVariableProvider.cs +++ b/src/GitVersion.Core/VersionCalculation/Abstractions/IVariableProvider.cs @@ -5,5 +5,7 @@ namespace GitVersion.VersionCalculation; public interface IVariableProvider { - VersionVariables GetVariablesFor(SemanticVersion semanticVersion, EffectiveConfiguration configuration, bool isCurrentCommitTagged); + VersionVariables GetVariablesFor( + SemanticVersion semanticVersion, EffectiveConfiguration configuration, SemanticVersion? currentCommitTaggedVersion + ); } diff --git a/src/GitVersion.Core/VersionCalculation/BaseVersionCalculators/MergeMessageVersionStrategy.cs b/src/GitVersion.Core/VersionCalculation/BaseVersionCalculators/MergeMessageVersionStrategy.cs index 5c91568e19..31a05ad737 100644 --- a/src/GitVersion.Core/VersionCalculation/BaseVersionCalculators/MergeMessageVersionStrategy.cs +++ b/src/GitVersion.Core/VersionCalculation/BaseVersionCalculators/MergeMessageVersionStrategy.cs @@ -29,21 +29,21 @@ public override IEnumerable GetBaseVersions(EffectiveBranchConfigur var commitsPriorToThan = Context.CurrentBranch.Commits.GetCommitsPriorTo(Context.CurrentCommit.When); var baseVersions = commitsPriorToThan - .SelectMany(c => + .SelectMany(commit => { - if (TryParse(c, Context, out var mergeMessage) && mergeMessage.Version != null + if (TryParse(commit, Context, out var mergeMessage) && mergeMessage.Version != null && Context.Configuration.IsReleaseBranch(mergeMessage.MergedBranch!)) { - this.log.Info($"Found commit [{Context.CurrentCommit}] matching merge message format: {mergeMessage.FormatName}"); + this.log.Info($"Found commit [{commit}] matching merge message format: {mergeMessage.FormatName}"); var shouldIncrement = !configuration.Value.PreventIncrementOfMergedBranchVersion; - var message = c.Message.Trim(); + var message = commit.Message.Trim(); - var baseVersionSource = c; + var baseVersionSource = commit; if (shouldIncrement) { - var parents = c.Parents.ToArray(); + var parents = commit.Parents.ToArray(); if (parents.Length == 2 && message.Contains("Merge branch") && message.Contains("release")) { baseVersionSource = this.repositoryStore.FindMergeBase(parents[0], parents[1]); diff --git a/src/GitVersion.Core/VersionCalculation/NextVersionCalculator.cs b/src/GitVersion.Core/VersionCalculation/NextVersionCalculator.cs index fb5b0d192e..218613c1c4 100644 --- a/src/GitVersion.Core/VersionCalculation/NextVersionCalculator.cs +++ b/src/GitVersion.Core/VersionCalculation/NextVersionCalculator.cs @@ -56,12 +56,6 @@ public virtual NextVersion FindVersion() semanticVersion = FindOtherModeVersion(nextVersion); } - if (semanticVersion.CompareTo(Context.CurrentCommitTaggedVersion) == 0) - { - // Will always be 0, don't bother with the +0 on tags - semanticVersion.BuildMetaData.CommitsSinceTag = null; - } - return new(semanticVersion, nextVersion.BaseVersion, new(nextVersion.Branch, nextVersion.Configuration)); } diff --git a/src/GitVersion.Core/VersionCalculation/SemanticVersioning/SemanticVersion.cs b/src/GitVersion.Core/VersionCalculation/SemanticVersioning/SemanticVersion.cs index c8d6a31201..a68dd49faf 100644 --- a/src/GitVersion.Core/VersionCalculation/SemanticVersioning/SemanticVersion.cs +++ b/src/GitVersion.Core/VersionCalculation/SemanticVersioning/SemanticVersion.cs @@ -18,11 +18,15 @@ public class SemanticVersion : IFormattable, IComparable, IEqua @"^(?(?\d+)(\.(?\d+))?(\.(?\d+))?)(\.(?\d+))?(-(?[^\+]*))?(\+(?.*))?$", RegexOptions.Compiled | RegexOptions.IgnoreCase); - public long Major; - public long Minor; - public long Patch; - public SemanticVersionPreReleaseTag PreReleaseTag; - public SemanticVersionBuildMetaData BuildMetaData; + public long Major { get; init; } + + public long Minor { get; init; } + + public long Patch { get; init; } + + public SemanticVersionPreReleaseTag PreReleaseTag { get; init; } + + public SemanticVersionBuildMetaData BuildMetaData { get; init; } public bool IsLabeledWith(string value) => PreReleaseTag.HasTag() && PreReleaseTag.Name.IsEquivalentTo(value); @@ -198,7 +202,10 @@ private static bool TryParseLoose(string version, [NotNullWhen(true)] out Semant var fourthPart = parsed.Groups["FourthPart"]; if (fourthPart.Success && semanticVersionBuildMetaData.CommitsSinceTag == null) { - semanticVersionBuildMetaData.CommitsSinceTag = int.Parse(fourthPart.Value); + semanticVersionBuildMetaData = new(semanticVersionBuildMetaData) + { + CommitsSinceTag = int.Parse(fourthPart.Value) + }; } semanticVersion = new SemanticVersion @@ -305,41 +312,53 @@ public string ToString(string? format, IFormatProvider? formatProvider) public SemanticVersion IncrementVersion(VersionField incrementStrategy, string? label) { - var incremented = new SemanticVersion(this); + var major = Major; + var minor = Minor; + var patch = Patch; - if (!incremented.PreReleaseTag.HasTag()) + if (!PreReleaseTag.HasTag()) { switch (incrementStrategy) { case VersionField.None: break; case VersionField.Major: - incremented.Major++; - incremented.Minor = 0; - incremented.Patch = 0; + major++; + minor = 0; + patch = 0; break; case VersionField.Minor: - incremented.Minor++; - incremented.Patch = 0; + minor++; + patch = 0; break; case VersionField.Patch: - incremented.Patch++; + patch++; break; default: throw new ArgumentOutOfRangeException(nameof(incrementStrategy)); } } - if (incremented.PreReleaseTag.HasTag() && incremented.PreReleaseTag.Number != null) + var preReleaseTagName = PreReleaseTag.Name; + var preReleaseTagNumber = PreReleaseTag.Number; + + if (PreReleaseTag.HasTag()) { - incremented.PreReleaseTag.Number++; + preReleaseTagNumber++; } - - if (!PreReleaseTag.HasTag() && !label.IsNullOrEmpty()) + else if (!label.IsNullOrEmpty()) { - incremented.PreReleaseTag = new SemanticVersionPreReleaseTag(label, 1); + preReleaseTagNumber = 1; + preReleaseTagName = label; } - return incremented; + return new() + { + Major = major, + Minor = minor, + Patch = patch, + PreReleaseTag = new(preReleaseTagName, preReleaseTagNumber), + BuildMetaData = new(BuildMetaData) + }; } } diff --git a/src/GitVersion.Core/VersionCalculation/SemanticVersioning/SemanticVersionBuildMetaData.cs b/src/GitVersion.Core/VersionCalculation/SemanticVersioning/SemanticVersionBuildMetaData.cs index adf85c660b..b26d31d2fd 100644 --- a/src/GitVersion.Core/VersionCalculation/SemanticVersioning/SemanticVersionBuildMetaData.cs +++ b/src/GitVersion.Core/VersionCalculation/SemanticVersioning/SemanticVersionBuildMetaData.cs @@ -7,6 +7,8 @@ namespace GitVersion; public class SemanticVersionBuildMetaData : IFormattable, IEquatable { + public static readonly SemanticVersionBuildMetaData Empty = new(); + private static readonly Regex ParseRegex = new( @"(?\d+)?(\.?Branch(Name)?\.(?[^\.]+))?(\.?Sha?\.(?[^\.]+))?(?.*)", RegexOptions.Compiled | RegexOptions.IgnoreCase); @@ -14,21 +16,30 @@ public class SemanticVersionBuildMetaData : IFormattable, IEquatable EqualityHelper = new(x => x.CommitsSinceTag, x => x.Branch, x => x.Sha); - public long? CommitsSinceTag; - public string? Branch; - public string? Sha; - public string? ShortSha; - public string? OtherMetaData; - public DateTimeOffset? CommitDate; - public string? VersionSourceSha; - public long? CommitsSinceVersionSource; - public long UncommittedChanges; + public long? CommitsSinceTag { get; init; } + + public string? Branch { get; init; } + + public string? Sha { get; init; } + + public string? ShortSha { get; init; } + + public string? OtherMetaData { get; init; } + + public DateTimeOffset? CommitDate { get; init; } + + public string? VersionSourceSha { get; init; } + + public long? CommitsSinceVersionSource { get; init; } + + public long UncommittedChanges { get; init; } public SemanticVersionBuildMetaData() { } - public SemanticVersionBuildMetaData(string? versionSourceSha, int? commitsSinceTag, string? branch, string? commitSha, string? commitShortSha, DateTimeOffset? commitDate, int numberOfUnCommittedChanges, string? otherMetadata = null) + public SemanticVersionBuildMetaData(string? versionSourceSha, int? commitsSinceTag, string? branch, string? commitSha, + string? commitShortSha, DateTimeOffset? commitDate, int numberOfUnCommittedChanges, string? otherMetadata = null) { this.Sha = commitSha; this.ShortSha = commitShortSha; @@ -41,17 +52,19 @@ public SemanticVersionBuildMetaData(string? versionSourceSha, int? commitsSinceT this.UncommittedChanges = numberOfUnCommittedChanges; } - public SemanticVersionBuildMetaData(SemanticVersionBuildMetaData? buildMetaData) + public SemanticVersionBuildMetaData(SemanticVersionBuildMetaData buildMetaData) { - this.Sha = buildMetaData?.Sha; - this.ShortSha = buildMetaData?.ShortSha; - this.CommitsSinceTag = buildMetaData?.CommitsSinceTag; - this.Branch = buildMetaData?.Branch; - this.CommitDate = buildMetaData?.CommitDate; - this.OtherMetaData = buildMetaData?.OtherMetaData; - this.VersionSourceSha = buildMetaData?.VersionSourceSha; - this.CommitsSinceVersionSource = buildMetaData?.CommitsSinceVersionSource; - this.UncommittedChanges = buildMetaData?.UncommittedChanges ?? 0; + buildMetaData.NotNull(); + + this.Sha = buildMetaData.Sha; + this.ShortSha = buildMetaData.ShortSha; + this.CommitsSinceTag = buildMetaData.CommitsSinceTag; + this.Branch = buildMetaData.Branch; + this.CommitDate = buildMetaData.CommitDate; + this.OtherMetaData = buildMetaData.OtherMetaData; + this.VersionSourceSha = buildMetaData.VersionSourceSha; + this.CommitsSinceVersionSource = buildMetaData.CommitsSinceVersionSource; + this.UncommittedChanges = buildMetaData.UncommittedChanges; } public override bool Equals(object? obj) => Equals(obj as SemanticVersionBuildMetaData); @@ -99,28 +112,39 @@ public string ToString(string? format, IFormatProvider? formatProvider) public static SemanticVersionBuildMetaData Parse(string? buildMetaData) { - var semanticVersionBuildMetaData = new SemanticVersionBuildMetaData(); if (buildMetaData.IsNullOrEmpty()) - return semanticVersionBuildMetaData; + return Empty; var parsed = ParseRegex.Match(buildMetaData); + long? buildMetaDataCommitsSinceTag = null; + long? buildMetaDataCommitsSinceVersionSource = null; if (parsed.Groups["BuildNumber"].Success) { - semanticVersionBuildMetaData.CommitsSinceTag = long.Parse(parsed.Groups["BuildNumber"].Value); - semanticVersionBuildMetaData.CommitsSinceVersionSource = semanticVersionBuildMetaData.CommitsSinceTag ?? 0; + buildMetaDataCommitsSinceTag = long.Parse(parsed.Groups["BuildNumber"].Value); + buildMetaDataCommitsSinceVersionSource = buildMetaDataCommitsSinceTag ?? 0; } + string? buildMetaDataBranch = null; if (parsed.Groups["BranchName"].Success) - semanticVersionBuildMetaData.Branch = parsed.Groups["BranchName"].Value; + buildMetaDataBranch = parsed.Groups["BranchName"].Value; + string? buildMetaDataSha = null; if (parsed.Groups["Sha"].Success) - semanticVersionBuildMetaData.Sha = parsed.Groups["Sha"].Value; + buildMetaDataSha = parsed.Groups["Sha"].Value; + string? buildMetaDataOtherMetaData = null; if (parsed.Groups["Other"].Success && !parsed.Groups["Other"].Value.IsNullOrEmpty()) - semanticVersionBuildMetaData.OtherMetaData = parsed.Groups["Other"].Value.TrimStart('.'); + buildMetaDataOtherMetaData = parsed.Groups["Other"].Value.TrimStart('.'); - return semanticVersionBuildMetaData; + return new() + { + CommitsSinceTag = buildMetaDataCommitsSinceTag, + CommitsSinceVersionSource = buildMetaDataCommitsSinceVersionSource, + Branch = buildMetaDataBranch, + Sha = buildMetaDataSha, + OtherMetaData = buildMetaDataOtherMetaData + }; } private static string FormatMetaDataPart(string value) diff --git a/src/GitVersion.Core/VersionCalculation/SemanticVersioning/SemanticVersionPreReleaseTag.cs b/src/GitVersion.Core/VersionCalculation/SemanticVersioning/SemanticVersionPreReleaseTag.cs index e96e6b09e5..bf88e20489 100644 --- a/src/GitVersion.Core/VersionCalculation/SemanticVersioning/SemanticVersionPreReleaseTag.cs +++ b/src/GitVersion.Core/VersionCalculation/SemanticVersioning/SemanticVersionPreReleaseTag.cs @@ -8,6 +8,8 @@ namespace GitVersion; public sealed class SemanticVersionPreReleaseTag : IFormattable, IComparable, IEquatable { + public static readonly SemanticVersionPreReleaseTag Empty = new(); + private static readonly Regex ParseRegex = new( @"(?.*?)\.?(?\d+)?$", RegexOptions.Compiled | RegexOptions.IgnoreCase); @@ -15,6 +17,12 @@ public sealed class SemanticVersionPreReleaseTag : private static readonly LambdaEqualityHelper EqualityHelper = new(x => x.Name, x => x.Number); + public string Name { get; init; } + + public long? Number { get; init; } + + public bool? PromotedFromCommits { get; init; } + public SemanticVersionPreReleaseTag() => Name = string.Empty; public SemanticVersionPreReleaseTag(string name, long? number) @@ -32,12 +40,6 @@ public SemanticVersionPreReleaseTag(SemanticVersionPreReleaseTag preReleaseTag) PromotedFromCommits = preReleaseTag.PromotedFromCommits; } - public string Name { get; set; } - - public long? Number { get; set; } - - public bool? PromotedFromCommits { get; set; } - public override bool Equals(object? obj) => Equals(obj as SemanticVersionPreReleaseTag); public bool Equals(SemanticVersionPreReleaseTag? other) => EqualityHelper.Equals(this, other); diff --git a/src/GitVersion.Core/VersionCalculation/VariableProvider.cs b/src/GitVersion.Core/VersionCalculation/VariableProvider.cs index 477b920d73..222448520f 100644 --- a/src/GitVersion.Core/VersionCalculation/VariableProvider.cs +++ b/src/GitVersion.Core/VersionCalculation/VariableProvider.cs @@ -2,7 +2,6 @@ using GitVersion.Configuration; using GitVersion.Extensions; using GitVersion.Helpers; -using GitVersion.Logging; using GitVersion.OutputVariables; namespace GitVersion.VersionCalculation; @@ -10,56 +9,67 @@ namespace GitVersion.VersionCalculation; public class VariableProvider : IVariableProvider { private readonly IEnvironment environment; - private readonly ILog log; - public VariableProvider(IEnvironment environment, ILog log) - { - this.environment = environment.NotNull(); - this.log = log.NotNull(); - } + public VariableProvider(IEnvironment environment) => this.environment = environment.NotNull(); - public VersionVariables GetVariablesFor(SemanticVersion semanticVersion, EffectiveConfiguration configuration, bool isCurrentCommitTagged) + public VersionVariables GetVariablesFor( + SemanticVersion semanticVersion, EffectiveConfiguration configuration, SemanticVersion? currentCommitTaggedVersion) { - var isContinuousDeploymentMode = configuration.VersioningMode == VersioningMode.ContinuousDeployment && !isCurrentCommitTagged; + var preReleaseTagName = semanticVersion.PreReleaseTag.Name; + + var isContinuousDeploymentMode = configuration.VersioningMode == VersioningMode.ContinuousDeployment + && currentCommitTaggedVersion is null; if (isContinuousDeploymentMode) { semanticVersion = new SemanticVersion(semanticVersion); // Continuous Deployment always requires a pre-release tag unless the commit is tagged if (!semanticVersion.PreReleaseTag.HasTag()) { - var label = configuration.GetBranchSpecificLabel(semanticVersion.BuildMetaData.Branch, null); - semanticVersion.PreReleaseTag.Name = label ?? string.Empty; - if (semanticVersion.PreReleaseTag.Name.IsNullOrEmpty()) + preReleaseTagName = configuration.GetBranchSpecificLabel(semanticVersion.BuildMetaData.Branch, null); + if (preReleaseTagName.IsNullOrEmpty()) { - // TODO: Why do we manipulating the semantic version here in the VariableProvider? The method name is GET not MANIPULATE. - // What is about the separation of concern and single-responsibility principle? - semanticVersion.PreReleaseTag.Name = configuration.Label ?? string.Empty; + preReleaseTagName = configuration.Label ?? string.Empty; } } } // Evaluate tag number pattern and append to prerelease tag, preserving build metadata var appendTagNumberPattern = !configuration.LabelNumberPattern.IsNullOrEmpty() && semanticVersion.PreReleaseTag.HasTag(); - if (appendTagNumberPattern) + if (appendTagNumberPattern && semanticVersion.BuildMetaData.Branch != null && configuration.LabelNumberPattern != null) { - if (semanticVersion.BuildMetaData.Branch != null && configuration.LabelNumberPattern != null) + var match = Regex.Match(semanticVersion.BuildMetaData.Branch, configuration.LabelNumberPattern); + var numberGroup = match.Groups["number"]; + if (numberGroup.Success) { - var match = Regex.Match(semanticVersion.BuildMetaData.Branch, configuration.LabelNumberPattern); - var numberGroup = match.Groups["number"]; - if (numberGroup.Success) - { - // TODO: Why do we manipulating the semantic version here in the VariableProvider? The method name is GET not MANIPULATE. - // What is about the separation of concern and single-responsibility principle? - semanticVersion.PreReleaseTag.Name += numberGroup.Value; - } + preReleaseTagName += numberGroup.Value; } } if (isContinuousDeploymentMode || appendTagNumberPattern || configuration.VersioningMode == VersioningMode.Mainline) { - // TODO: Why do we manipulating the semantic version here in the VariableProvider? The method name is GET not MANIPULATE. - // What is about the separation of concern and single-responsibility principle? - PromoteNumberOfCommitsToTagNumber(semanticVersion); + semanticVersion = PromoteNumberOfCommitsToTagNumber(semanticVersion, preReleaseTagName); + } + else + { + semanticVersion = new(semanticVersion) + { + PreReleaseTag = new(semanticVersion.PreReleaseTag) + { + Name = preReleaseTagName + } + }; + } + + if (semanticVersion.CompareTo(currentCommitTaggedVersion) == 0) + { + // Will always be 0, don't bother with the +0 on tags + semanticVersion = new(semanticVersion) + { + BuildMetaData = new(semanticVersion.BuildMetaData) + { + CommitsSinceTag = null + } + }; } var semverFormatValues = new SemanticVersionFormatValues(semanticVersion, configuration); @@ -70,7 +80,7 @@ public VersionVariables GetVariablesFor(SemanticVersion semanticVersion, Effecti var assemblySemVer = CheckAndFormatString(configuration.AssemblyVersioningFormat, semverFormatValues, semverFormatValues.AssemblySemVer, "AssemblyVersioningFormat"); - var variables = new VersionVariables( + return new VersionVariables( semverFormatValues.Major, semverFormatValues.Minor, semverFormatValues.Patch, @@ -95,34 +105,53 @@ public VersionVariables GetVariablesFor(SemanticVersion semanticVersion, Effecti semverFormatValues.CommitDate, semverFormatValues.VersionSourceSha, semverFormatValues.CommitsSinceVersionSource, - semverFormatValues.UncommittedChanges); - - return variables; + semverFormatValues.UncommittedChanges + ); } - private static void PromoteNumberOfCommitsToTagNumber(SemanticVersion semanticVersion) + private static SemanticVersion PromoteNumberOfCommitsToTagNumber(SemanticVersion semanticVersion, string preReleaseTagName) { + var preReleaseTagNumber = semanticVersion.PreReleaseTag.Number; + var preReleaseTagPromotedFromCommits = semanticVersion.PreReleaseTag.PromotedFromCommits; + long buildMetaDataCommitsSinceVersionSource; + var buildMetaDataCommitsSinceTag = semanticVersion.BuildMetaData.CommitsSinceTag; + // For continuous deployment the commits since tag gets promoted to the pre-release number if (!semanticVersion.BuildMetaData.CommitsSinceTag.HasValue) { - semanticVersion.PreReleaseTag.Number = null; - semanticVersion.BuildMetaData.CommitsSinceVersionSource = 0; + preReleaseTagNumber = null; + buildMetaDataCommitsSinceVersionSource = 0; } else { // Number of commits since last tag should be added to PreRelease number if given. Remember to deduct automatic version bump. - if (semanticVersion.PreReleaseTag.Number.HasValue) + if (preReleaseTagNumber.HasValue) { - semanticVersion.PreReleaseTag.Number += semanticVersion.BuildMetaData.CommitsSinceTag - 1; + preReleaseTagNumber += semanticVersion.BuildMetaData.CommitsSinceTag - 1; } else { - semanticVersion.PreReleaseTag.Number = semanticVersion.BuildMetaData.CommitsSinceTag; - semanticVersion.PreReleaseTag.PromotedFromCommits = true; + preReleaseTagNumber = semanticVersion.BuildMetaData.CommitsSinceTag; + preReleaseTagPromotedFromCommits = true; } - semanticVersion.BuildMetaData.CommitsSinceVersionSource = semanticVersion.BuildMetaData.CommitsSinceTag.Value; - semanticVersion.BuildMetaData.CommitsSinceTag = null; // why is this set to null ? + buildMetaDataCommitsSinceVersionSource = semanticVersion.BuildMetaData.CommitsSinceTag.Value; + buildMetaDataCommitsSinceTag = null; // why is this set to null ? } + + return new SemanticVersion(semanticVersion) + { + PreReleaseTag = new(semanticVersion.PreReleaseTag) + { + Name = preReleaseTagName, + Number = preReleaseTagNumber, + PromotedFromCommits = preReleaseTagPromotedFromCommits + }, + BuildMetaData = new(semanticVersion.BuildMetaData) + { + CommitsSinceVersionSource = buildMetaDataCommitsSinceVersionSource, + CommitsSinceTag = buildMetaDataCommitsSinceTag + } + }; } private string? CheckAndFormatString(string? formatString, T source, string? defaultValue, string formatVarName) diff --git a/src/GitVersion.Output.Tests/Output/AssemblyInfoFileUpdaterTests.cs b/src/GitVersion.Output.Tests/Output/AssemblyInfoFileUpdaterTests.cs index 2af8d14bb0..d1ecdc9315 100644 --- a/src/GitVersion.Output.Tests/Output/AssemblyInfoFileUpdaterTests.cs +++ b/src/GitVersion.Output.Tests/Output/AssemblyInfoFileUpdaterTests.cs @@ -38,7 +38,7 @@ public void ShouldCreateAssemblyInfoFileWhenNotExistsAndEnsureAssemblyInfo(strin var workingDir = Path.GetTempPath(); var assemblyInfoFile = "VersionAssemblyInfo." + fileExtension; var fullPath = PathHelper.Combine(workingDir, assemblyInfoFile); - var variables = this.variableProvider.GetVariablesFor(SemanticVersion.Parse("1.0.0", ConfigurationConstants.DefaultLabelPrefix), new TestEffectiveConfiguration(), false); + var variables = this.variableProvider.GetVariablesFor(SemanticVersion.Parse("1.0.0", ConfigurationConstants.DefaultLabelPrefix), new TestEffectiveConfiguration(), null); using var assemblyInfoFileUpdater = new AssemblyInfoFileUpdater(this.log, this.fileSystem); assemblyInfoFileUpdater.Execute(variables, new AssemblyInfoContext(workingDir, true, assemblyInfoFile)); @@ -54,7 +54,9 @@ public void ShouldCreateAssemblyInfoFileAtPathWhenNotExistsAndEnsureAssemblyInfo var workingDir = Path.GetTempPath(); var assemblyInfoFile = PathHelper.Combine("src", "Project", "Properties", $"VersionAssemblyInfo.{fileExtension}"); var fullPath = PathHelper.Combine(workingDir, assemblyInfoFile); - var variables = this.variableProvider.GetVariablesFor(SemanticVersion.Parse("1.0.0", ConfigurationConstants.DefaultLabelPrefix), new TestEffectiveConfiguration(), false); + var variables = this.variableProvider.GetVariablesFor( + SemanticVersion.Parse("1.0.0", ConfigurationConstants.DefaultLabelPrefix), new TestEffectiveConfiguration(), null + ); using var assemblyInfoFileUpdater = new AssemblyInfoFileUpdater(this.log, this.fileSystem); assemblyInfoFileUpdater.Execute(variables, new AssemblyInfoContext(workingDir, true, assemblyInfoFile)); @@ -73,7 +75,7 @@ public void ShouldCreateAssemblyInfoFilesAtPathWhenNotExistsAndEnsureAssemblyInf "AssemblyInfo." + fileExtension, PathHelper.Combine("src", "Project", "Properties", "VersionAssemblyInfo." + fileExtension) }; - var variables = this.variableProvider.GetVariablesFor(SemanticVersion.Parse("1.0.0", ConfigurationConstants.DefaultLabelPrefix), new TestEffectiveConfiguration(), false); + var variables = this.variableProvider.GetVariablesFor(SemanticVersion.Parse("1.0.0", ConfigurationConstants.DefaultLabelPrefix), new TestEffectiveConfiguration(), null); using var assemblyInfoFileUpdater = new AssemblyInfoFileUpdater(this.log, this.fileSystem); assemblyInfoFileUpdater.Execute(variables, new AssemblyInfoContext(workingDir, true, assemblyInfoFiles.ToArray())); @@ -93,7 +95,9 @@ public void ShouldNotCreateAssemblyInfoFileWhenNotExistsAndNotEnsureAssemblyInfo var workingDir = Path.GetTempPath(); var assemblyInfoFile = "VersionAssemblyInfo." + fileExtension; var fullPath = PathHelper.Combine(workingDir, assemblyInfoFile); - var variables = this.variableProvider.GetVariablesFor(SemanticVersion.Parse("1.0.0", ConfigurationConstants.DefaultLabelPrefix), new TestEffectiveConfiguration(), false); + var variables = this.variableProvider.GetVariablesFor( + SemanticVersion.Parse("1.0.0", ConfigurationConstants.DefaultLabelPrefix), new TestEffectiveConfiguration(), null + ); using var assemblyInfoFileUpdater = new AssemblyInfoFileUpdater(this.log, this.fileSystem); assemblyInfoFileUpdater.Execute(variables, new AssemblyInfoContext(workingDir, false, assemblyInfoFile)); @@ -108,7 +112,9 @@ public void ShouldNotCreateAssemblyInfoFileForUnknownSourceCodeAndEnsureAssembly var workingDir = Path.GetTempPath(); const string assemblyInfoFile = "VersionAssemblyInfo.js"; var fullPath = PathHelper.Combine(workingDir, assemblyInfoFile); - var variables = this.variableProvider.GetVariablesFor(SemanticVersion.Parse("1.0.0", ConfigurationConstants.DefaultLabelPrefix), new TestEffectiveConfiguration(), false); + var variables = this.variableProvider.GetVariablesFor( + SemanticVersion.Parse("1.0.0", ConfigurationConstants.DefaultLabelPrefix), new TestEffectiveConfiguration(), null + ); using var assemblyInfoFileUpdater = new AssemblyInfoFileUpdater(this.log, this.fileSystem); assemblyInfoFileUpdater.Execute(variables, new AssemblyInfoContext(workingDir, true, assemblyInfoFile)); @@ -122,7 +128,9 @@ public void ShouldStartSearchFromWorkingDirectory() this.fileSystem = Substitute.For(); var workingDir = Path.GetTempPath(); var assemblyInfoFiles = Array.Empty(); - var variables = this.variableProvider.GetVariablesFor(SemanticVersion.Parse("1.0.0", ConfigurationConstants.DefaultLabelPrefix), new TestEffectiveConfiguration(), false); + var variables = this.variableProvider.GetVariablesFor( + SemanticVersion.Parse("1.0.0", ConfigurationConstants.DefaultLabelPrefix), new TestEffectiveConfiguration(), null + ); using var assemblyInfoFileUpdater = new AssemblyInfoFileUpdater(this.log, this.fileSystem); assemblyInfoFileUpdater.Execute(variables, new AssemblyInfoContext(workingDir, false, assemblyInfoFiles.ToArray())); @@ -443,7 +451,7 @@ private void VerifyAssemblyInfoFile( }); var configuration = new TestEffectiveConfiguration(versioningScheme); - var variables = this.variableProvider.GetVariablesFor(version, configuration, false); + var variables = this.variableProvider.GetVariablesFor(version, configuration, null); verify?.Invoke(this.fileSystem, variables); } diff --git a/src/GitVersion.Output.Tests/Output/GitVersionInfoGeneratorTests.cs b/src/GitVersion.Output.Tests/Output/GitVersionInfoGeneratorTests.cs index 75c84c6ca4..b82b839708 100644 --- a/src/GitVersion.Output.Tests/Output/GitVersionInfoGeneratorTests.cs +++ b/src/GitVersion.Output.Tests/Output/GitVersionInfoGeneratorTests.cs @@ -37,7 +37,7 @@ public void ShouldCreateFile(string fileExtension) var fileSystem = sp.GetRequiredService(); var variableProvider = sp.GetRequiredService(); - var variables = variableProvider.GetVariablesFor(semanticVersion, new TestEffectiveConfiguration(), false); + var variables = variableProvider.GetVariablesFor(semanticVersion, new TestEffectiveConfiguration(), null); using var generator = sp.GetRequiredService(); generator.Execute(variables, new GitVersionInfoContext(directory, fileName, fileExtension)); diff --git a/src/GitVersion.Output.Tests/Output/ProjectFileUpdaterTests.cs b/src/GitVersion.Output.Tests/Output/ProjectFileUpdaterTests.cs index bcf228841e..bc664ac7c2 100644 --- a/src/GitVersion.Output.Tests/Output/ProjectFileUpdaterTests.cs +++ b/src/GitVersion.Output.Tests/Output/ProjectFileUpdaterTests.cs @@ -140,7 +140,9 @@ public void CannotUpdateProjectFileWithoutAPropertyGroup(string xml) )] public void UpdateProjectXmlVersionElementWithStandardXmlInsertsElement(string xml) { - var variables = this.variableProvider.GetVariablesFor(SemanticVersion.Parse("2.0.0", ConfigurationConstants.DefaultLabelPrefix), new TestEffectiveConfiguration(), false); + var variables = this.variableProvider.GetVariablesFor( + SemanticVersion.Parse("2.0.0", ConfigurationConstants.DefaultLabelPrefix), new TestEffectiveConfiguration(), null + ); var xmlRoot = XElement.Parse(xml); variables.AssemblySemVer.ShouldNotBeNull(); ProjectFileUpdater.UpdateProjectVersionElement(xmlRoot, ProjectFileUpdater.AssemblyVersionElement, variables.AssemblySemVer); @@ -167,7 +169,7 @@ public void UpdateProjectXmlVersionElementWithStandardXmlInsertsElement(string x )] public void UpdateProjectXmlVersionElementWithStandardXmlModifiesElement(string xml) { - var variables = this.variableProvider.GetVariablesFor(SemanticVersion.Parse("2.0.0", ConfigurationConstants.DefaultLabelPrefix), new TestEffectiveConfiguration(), false); + var variables = this.variableProvider.GetVariablesFor(SemanticVersion.Parse("2.0.0", ConfigurationConstants.DefaultLabelPrefix), new TestEffectiveConfiguration(), null); var xmlRoot = XElement.Parse(xml); variables.AssemblySemVer.ShouldNotBeNull(); ProjectFileUpdater.UpdateProjectVersionElement(xmlRoot, ProjectFileUpdater.AssemblyVersionElement, variables.AssemblySemVer); @@ -197,7 +199,7 @@ public void UpdateProjectXmlVersionElementWithStandardXmlModifiesElement(string )] public void UpdateProjectXmlVersionElementWithDuplicatePropertyGroupsModifiesLastElement(string xml) { - var variables = this.variableProvider.GetVariablesFor(SemanticVersion.Parse("2.0.0", ConfigurationConstants.DefaultLabelPrefix), new TestEffectiveConfiguration(), false); + var variables = this.variableProvider.GetVariablesFor(SemanticVersion.Parse("2.0.0", ConfigurationConstants.DefaultLabelPrefix), new TestEffectiveConfiguration(), null); var xmlRoot = XElement.Parse(xml); variables.AssemblySemVer.ShouldNotBeNull(); ProjectFileUpdater.UpdateProjectVersionElement(xmlRoot, ProjectFileUpdater.AssemblyVersionElement, variables.AssemblySemVer); @@ -228,7 +230,7 @@ public void UpdateProjectXmlVersionElementWithDuplicatePropertyGroupsModifiesLas )] public void UpdateProjectXmlVersionElementWithMultipleVersionElementsLastOneIsModified(string xml) { - var variables = this.variableProvider.GetVariablesFor(SemanticVersion.Parse("2.0.0", ConfigurationConstants.DefaultLabelPrefix), new TestEffectiveConfiguration(), false); + var variables = this.variableProvider.GetVariablesFor(SemanticVersion.Parse("2.0.0", ConfigurationConstants.DefaultLabelPrefix), new TestEffectiveConfiguration(), null); var xmlRoot = XElement.Parse(xml); variables.AssemblySemVer.ShouldNotBeNull(); ProjectFileUpdater.UpdateProjectVersionElement(xmlRoot, ProjectFileUpdater.AssemblyVersionElement, variables.AssemblySemVer); @@ -301,7 +303,7 @@ private void VerifyAssemblyInfoFile( }); var configuration = new TestEffectiveConfiguration(versioningScheme); - var variables = this.variableProvider.GetVariablesFor(version, configuration, false); + var variables = this.variableProvider.GetVariablesFor(version, configuration, null); verify?.Invoke(this.fileSystem, variables); } diff --git a/src/GitVersion.Output.Tests/Output/WixFileTests.cs b/src/GitVersion.Output.Tests/Output/WixFileTests.cs index 24105f4d82..611ce8ac77 100644 --- a/src/GitVersion.Output.Tests/Output/WixFileTests.cs +++ b/src/GitVersion.Output.Tests/Output/WixFileTests.cs @@ -23,14 +23,15 @@ public void UpdateWixVersionFile() Major = 1, Minor = 2, Patch = 3, - BuildMetaData = "5.Branch.develop" + BuildMetaData = new SemanticVersionBuildMetaData("5.Branch.develop") + { + VersionSourceSha = "versionSourceSha", + Sha = "commitSha", + ShortSha = "commitShortSha", + CommitDate = DateTimeOffset.Parse("2019-02-20 23:59:59Z") + } }; - semVer.BuildMetaData.VersionSourceSha = "versionSourceSha"; - semVer.BuildMetaData.Sha = "commitSha"; - semVer.BuildMetaData.ShortSha = "commitShortSha"; - semVer.BuildMetaData.CommitDate = DateTimeOffset.Parse("2019-02-20 23:59:59Z"); - var configuration = new TestEffectiveConfiguration(); var stringBuilder = new StringBuilder(); @@ -43,7 +44,7 @@ public void UpdateWixVersionFile() var fileSystem = sp.GetRequiredService(); var variableProvider = sp.GetRequiredService(); - var versionVariables = variableProvider.GetVariablesFor(semVer, configuration, false); + var versionVariables = variableProvider.GetVariablesFor(semVer, configuration, null); using var wixVersionFileUpdater = sp.GetRequiredService(); @@ -64,14 +65,15 @@ public void UpdateWixVersionFileWhenFileAlreadyExists() Major = 1, Minor = 2, Patch = 3, - BuildMetaData = "5.Branch.develop" + BuildMetaData = new SemanticVersionBuildMetaData("5.Branch.develop") + { + VersionSourceSha = "versionSourceSha", + Sha = "commitSha", + ShortSha = "commitShortSha", + CommitDate = DateTimeOffset.Parse("2019-02-20 23:59:59Z") + } }; - semVer.BuildMetaData.VersionSourceSha = "versionSourceSha"; - semVer.BuildMetaData.Sha = "commitSha"; - semVer.BuildMetaData.ShortSha = "commitShortSha"; - semVer.BuildMetaData.CommitDate = DateTimeOffset.Parse("2019-02-20 23:59:59Z"); - var configuration = new TestEffectiveConfiguration(); var stringBuilder = new StringBuilder(); @@ -84,7 +86,7 @@ public void UpdateWixVersionFileWhenFileAlreadyExists() var fileSystem = sp.GetRequiredService(); var variableProvider = sp.GetRequiredService(); - var versionVariables = variableProvider.GetVariablesFor(semVer, configuration, false); + var versionVariables = variableProvider.GetVariablesFor(semVer, configuration, null); using var wixVersionFileUpdater = sp.GetRequiredService();