Skip to content

Commit

Permalink
Finally get rid of taggedSemanticVersion approach
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
AlexPykavy authored and arturcic committed Nov 29, 2022
1 parent b2033f9 commit 8e6eef4
Showing 1 changed file with 27 additions and 43 deletions.
70 changes: 27 additions & 43 deletions src/GitVersion.Core/VersionCalculation/NextVersionCalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 8e6eef4

Please sign in to comment.