From 244ad64c546efa73e47e41733694cb03449f6fe6 Mon Sep 17 00:00:00 2001 From: Alexander Pykavy Date: Mon, 24 Oct 2022 22:06:40 +0200 Subject: [PATCH] Finally get rid of taggedSemanticVersion approach Change next version calculator to act according to the next algorithm: 1. Calculate next version candidate based on the base one 2. Look for the latest `PreReleaseTag` matching Major, Minor and Patch of the next version candidate. 3. Append the `PreReleaseTag`, if found, to the next version candidate. 4. Compare the next version candidate with `Context.CurrentCommitTaggedVersion` including the `PreReleaseTag` comparison. 5. Increment the `PreReleaseTag` of the next version candidate if versions mismatch. 6. Return the next version candidate. --- .../NextVersionCalculator.cs | 70 +++++++------------ 1 file changed, 27 insertions(+), 43 deletions(-) diff --git a/src/GitVersion.Core/VersionCalculation/NextVersionCalculator.cs b/src/GitVersion.Core/VersionCalculation/NextVersionCalculator.cs index b683c7e8b6..aace6dca39 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) @@ -68,61 +69,44 @@ public virtual NextVersion FindVersion() } semver.BuildMetaData = baseVersionBuildMetaData; - } - - 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)