diff --git a/src/GitVersionCore.Tests/IntegrationTests/BranchWithoutCommitScenario.cs b/src/GitVersionCore.Tests/IntegrationTests/BranchWithoutCommitScenario.cs deleted file mode 100644 index 00b477535d..0000000000 --- a/src/GitVersionCore.Tests/IntegrationTests/BranchWithoutCommitScenario.cs +++ /dev/null @@ -1,22 +0,0 @@ -using GitTools.Testing; -using LibGit2Sharp; -using NUnit.Framework; - -namespace GitVersionCore.Tests.IntegrationTests -{ - [TestFixture] - public class BranchWithoutCommitScenario : TestBase - { - [Test] - public void CanTakeVersionFromReleaseBranch() - { - using var fixture = new EmptyRepositoryFixture(); - fixture.Repository.MakeATaggedCommit("1.0.3"); - var commit = fixture.Repository.MakeACommit(); - fixture.Repository.CreateBranch("release-4.0.123"); - fixture.Checkout(commit.Sha); - - fixture.AssertFullSemver("4.0.123-beta.1+0", fixture.Repository, commit.Sha, false, "release-4.0.123"); - } - } -} \ No newline at end of file diff --git a/src/GitVersionCore.Tests/IntegrationTests/BranchWithoutCommitScenarios.cs b/src/GitVersionCore.Tests/IntegrationTests/BranchWithoutCommitScenarios.cs new file mode 100644 index 0000000000..9b314c802c --- /dev/null +++ b/src/GitVersionCore.Tests/IntegrationTests/BranchWithoutCommitScenarios.cs @@ -0,0 +1,39 @@ +using GitTools.Testing; +using LibGit2Sharp; +using NUnit.Framework; + +namespace GitVersionCore.Tests.IntegrationTests +{ + [TestFixture] + public class BranchWithoutCommitScenarios : TestBase + { + [Test] + public void CanTakeVersionFromReleaseBranch() + { + using var fixture = new EmptyRepositoryFixture(); + fixture.Repository.MakeATaggedCommit("1.0.3"); + var commit = fixture.Repository.MakeACommit(); + fixture.Repository.CreateBranch("release-4.0.123"); + fixture.Checkout(commit.Sha); + + fixture.AssertFullSemver("4.0.123-beta.1+0", fixture.Repository, commit.Sha, false, "release-4.0.123"); + } + + [Test] + public void BranchVersionHavePrecedenceOverTagVersionIfVersionGreaterThanTag() + { + using var fixture = new EmptyRepositoryFixture(); + + fixture.Repository.MakeACommit(); + + fixture.Repository.CreateBranch("develop"); + fixture.Checkout("develop"); + fixture.MakeATaggedCommit("0.1.0-alpha.1"); // simulate merge from feature branch + + fixture.Repository.CreateBranch("release/1.0"); + fixture.Checkout("release/1.0"); + + fixture.AssertFullSemver("1.0.0-beta.1+0"); + } + } +} diff --git a/src/GitVersionCore/SemanticVersioning/SemanticVersion.cs b/src/GitVersionCore/SemanticVersioning/SemanticVersion.cs index 13c31be697..6597fff425 100644 --- a/src/GitVersionCore/SemanticVersioning/SemanticVersion.cs +++ b/src/GitVersionCore/SemanticVersioning/SemanticVersion.cs @@ -184,6 +184,11 @@ public static bool TryParse(string version, string tagPrefixRegex, out SemanticV } public int CompareTo(SemanticVersion value) + { + return CompareTo(value, true); + } + + public int CompareTo(SemanticVersion value, bool includePrerelease) { if (value == null) { @@ -213,7 +218,7 @@ public int CompareTo(SemanticVersion value) } return -1; } - if (PreReleaseTag != value.PreReleaseTag) + if (includePrerelease && PreReleaseTag != value.PreReleaseTag) { if (PreReleaseTag > value.PreReleaseTag) { diff --git a/src/GitVersionCore/VersionCalculation/NextVersionCalculator.cs b/src/GitVersionCore/VersionCalculation/NextVersionCalculator.cs index 835642d6cb..a57f206e1e 100644 --- a/src/GitVersionCore/VersionCalculation/NextVersionCalculator.cs +++ b/src/GitVersionCore/VersionCalculation/NextVersionCalculator.cs @@ -1,6 +1,7 @@ using System; using System.Linq; using System.Text.RegularExpressions; + using GitVersion.VersionCalculation.BaseVersionCalculators; using GitVersion.VersioningModes; using GitVersion.Configuration; @@ -28,7 +29,7 @@ public NextVersionCalculator(ILog log, IMetaDataCalculator metaDataCalculator, I public SemanticVersion FindVersion(GitVersionContext context) { SemanticVersion taggedSemanticVersion = null; - // If current commit is tagged, don't do anything except add build metadata + if (context.IsCurrentCommitTagged) { // Will always be 0, don't bother with the +0 on tags @@ -62,11 +63,18 @@ public SemanticVersion FindVersion(GitVersionContext context) UpdatePreReleaseTag(context, semver, baseVersion.BranchNameOverride); } - if (taggedSemanticVersion != null) { - // set the commit count on the tagged ver - taggedSemanticVersion.BuildMetaData.CommitsSinceVersionSource = semver.BuildMetaData.CommitsSinceVersionSource; + // replace calculated version with tagged version only if tagged version greater or equal to calculated version + if (semver.CompareTo(taggedSemanticVersion, false) > 0) + { + taggedSemanticVersion = null; + } + else + { + // set the commit count on the tagged ver + taggedSemanticVersion.BuildMetaData.CommitsSinceVersionSource = semver.BuildMetaData.CommitsSinceVersionSource; + } } return taggedSemanticVersion ?? semver;