diff --git a/src/GitVersion.Core/VersionCalculation/NextVersionCalculator.cs b/src/GitVersion.Core/VersionCalculation/NextVersionCalculator.cs index f2a4fb929c..dd81986cff 100644 --- a/src/GitVersion.Core/VersionCalculation/NextVersionCalculator.cs +++ b/src/GitVersion.Core/VersionCalculation/NextVersionCalculator.cs @@ -48,6 +48,7 @@ public virtual NextVersion FindVersion() } var nextVersion = Calculate(Context.CurrentBranch, Context.Configuration); + var preReleaseTagName = nextVersion.Configuration.GetBranchSpecificTag(this.log, Context.CurrentBranch.Name.Friendly, nextVersion.BaseVersion.BranchNameOverride); SemanticVersion semver; if (Context.Configuration.VersioningMode == VersioningMode.Mainline) @@ -67,61 +68,44 @@ public virtual NextVersion FindVersion() { semver = nextVersion.BaseVersion.SemanticVersion; } - } - - var tag = nextVersion.Configuration.Tag; - if (!tag.IsNullOrEmpty() && semver.PreReleaseTag?.Name != tag) - { - UpdatePreReleaseTag(new(nextVersion.Branch, nextVersion.Configuration), semver, nextVersion.BaseVersion.BranchNameOverride); - } - - // TODO: It is totally unimportant that the current commit has been tagged or not IMO. We can make a double check actually if the result - // is the same or make it configurable but each run should be deterministic.Even if the development process goes on the tagged commit - // should always calculating the same result. Otherwise something is wrong with the configuration or someone messed up the branching history. - - if (Context.IsCurrentCommitTagged) - { - // Will always be 0, don't bother with the +0 on tags - var semanticVersionBuildMetaData = this.mainlineVersionCalculator.CreateVersionBuildMetaData(Context.CurrentCommit!); - semanticVersionBuildMetaData.CommitsSinceTag = null; - SemanticVersion taggedSemanticVersion = new SemanticVersion(Context.CurrentCommitTaggedVersion) { BuildMetaData = semanticVersionBuildMetaData }; + var lastPrefixedSemver = this.repositoryStore + .GetVersionTagsOnBranch(Context.CurrentBranch, Context.Configuration.TagPrefix) + .Where(v => MajorMinorPatchEqual(v, semver) && v.PreReleaseTag?.HasTag() == true) + .FirstOrDefault(v => v.PreReleaseTag?.Name?.IsEquivalentTo(preReleaseTagName) == true); - // replace calculated version with tagged version only if tagged version greater or equal to calculated version - if (taggedSemanticVersion.CompareTo(semver, false) >= 0) + if (lastPrefixedSemver != null) { - // set the commit count on the tagged ver - taggedSemanticVersion.BuildMetaData.CommitsSinceVersionSource = semver.BuildMetaData?.CommitsSinceVersionSource; - - semver = taggedSemanticVersion; + semver.PreReleaseTag = lastPrefixedSemver.PreReleaseTag; } } - return new(semver, nextVersion.BaseVersion, new(nextVersion.Branch, nextVersion.Configuration)); - } - - private void UpdatePreReleaseTag(EffectiveBranchConfiguration configuration, SemanticVersion semanticVersion, string? branchNameOverride) - { - var preReleaseTagName = configuration.Value.GetBranchSpecificTag(this.log, Context.CurrentBranch.Name.Friendly, branchNameOverride); - - // TODO: Please update the pre release-tag in the IVersionStrategy implementation. - var lastPrefixedSemver = this.repositoryStore - .GetVersionTagsOnBranch(Context.CurrentBranch, Context.Configuration.TagPrefix) - .Where(v => MajorMinorPatchEqual(v, semanticVersion) && v.HasPreReleaseTagWithLabel) - .FirstOrDefault(v => v.PreReleaseTag?.Name?.IsEquivalentTo(preReleaseTagName) == true); - - long? number = null; - - if (lastPrefixedSemver != null) + if (semver.CompareTo(Context.CurrentCommitTaggedVersion) == 0) { - number = lastPrefixedSemver.PreReleaseTag!.Number + 1; + // Will always be 0, don't bother with the +0 on tags + semver.BuildMetaData.CommitsSinceTag = null; + } + else if (string.IsNullOrEmpty(preReleaseTagName)) + { + semver.PreReleaseTag = new SemanticVersionPreReleaseTag(); } else { - number = 1; + long? number; + + if (semver.PreReleaseTag.Name == preReleaseTagName) + { + number = semver.PreReleaseTag.Number + 1; + } + else + { + number = 1; + } + + semver.PreReleaseTag = new SemanticVersionPreReleaseTag(preReleaseTagName, number); } - semanticVersion.PreReleaseTag = new SemanticVersionPreReleaseTag(preReleaseTagName, number); + return new(semver, nextVersion.BaseVersion, new(nextVersion.Branch, nextVersion.Configuration)); } private static void EnsureHeadIsNotDetached(GitVersionContext context)