Skip to content

Commit c95b521

Browse files
committed
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.
1 parent efa9e78 commit c95b521

File tree

1 file changed

+27
-43
lines changed

1 file changed

+27
-43
lines changed

src/GitVersion.Core/VersionCalculation/NextVersionCalculator.cs

Lines changed: 27 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public virtual NextVersion FindVersion()
4848
}
4949

5050
var nextVersion = Calculate(Context.CurrentBranch, Context.Configuration);
51+
var preReleaseTagName = nextVersion.Configuration.GetBranchSpecificTag(this.log, Context.CurrentBranch.Name.Friendly, nextVersion.BaseVersion.BranchNameOverride);
5152

5253
SemanticVersion semver;
5354
if (Context.Configuration.VersioningMode == VersioningMode.Mainline)
@@ -67,61 +68,44 @@ public virtual NextVersion FindVersion()
6768
{
6869
semver = nextVersion.BaseVersion.SemanticVersion;
6970
}
70-
}
71-
72-
var tag = nextVersion.Configuration.Tag;
73-
if (!tag.IsNullOrEmpty() && semver.PreReleaseTag?.Name != tag)
74-
{
75-
UpdatePreReleaseTag(new(nextVersion.Branch, nextVersion.Configuration), semver, nextVersion.BaseVersion.BranchNameOverride);
76-
}
77-
78-
// 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
79-
// is the same or make it configurable but each run should be deterministic.Even if the development process goes on the tagged commit
80-
// should always calculating the same result. Otherwise something is wrong with the configuration or someone messed up the branching history.
81-
82-
if (Context.IsCurrentCommitTagged)
83-
{
84-
// Will always be 0, don't bother with the +0 on tags
85-
var semanticVersionBuildMetaData = this.mainlineVersionCalculator.CreateVersionBuildMetaData(Context.CurrentCommit!);
86-
semanticVersionBuildMetaData.CommitsSinceTag = null;
8771

88-
SemanticVersion taggedSemanticVersion = new SemanticVersion(Context.CurrentCommitTaggedVersion) { BuildMetaData = semanticVersionBuildMetaData };
72+
var lastPrefixedSemver = this.repositoryStore
73+
.GetVersionTagsOnBranch(Context.CurrentBranch, Context.Configuration.TagPrefix)
74+
.Where(v => MajorMinorPatchEqual(v, semver) && v.PreReleaseTag?.HasTag() == true)
75+
.FirstOrDefault(v => v.PreReleaseTag?.Name?.IsEquivalentTo(preReleaseTagName) == true);
8976

90-
// replace calculated version with tagged version only if tagged version greater or equal to calculated version
91-
if (taggedSemanticVersion.CompareTo(semver, false) >= 0)
77+
if (lastPrefixedSemver != null)
9278
{
93-
// set the commit count on the tagged ver
94-
taggedSemanticVersion.BuildMetaData.CommitsSinceVersionSource = semver.BuildMetaData?.CommitsSinceVersionSource;
95-
96-
semver = taggedSemanticVersion;
79+
semver.PreReleaseTag = lastPrefixedSemver.PreReleaseTag;
9780
}
9881
}
9982

100-
return new(semver, nextVersion.BaseVersion, new(nextVersion.Branch, nextVersion.Configuration));
101-
}
102-
103-
private void UpdatePreReleaseTag(EffectiveBranchConfiguration configuration, SemanticVersion semanticVersion, string? branchNameOverride)
104-
{
105-
var preReleaseTagName = configuration.Value.GetBranchSpecificTag(this.log, Context.CurrentBranch.Name.Friendly, branchNameOverride);
106-
107-
// TODO: Please update the pre release-tag in the IVersionStrategy implementation.
108-
var lastPrefixedSemver = this.repositoryStore
109-
.GetVersionTagsOnBranch(Context.CurrentBranch, Context.Configuration.TagPrefix)
110-
.Where(v => MajorMinorPatchEqual(v, semanticVersion) && v.HasPreReleaseTagWithLabel)
111-
.FirstOrDefault(v => v.PreReleaseTag?.Name?.IsEquivalentTo(preReleaseTagName) == true);
112-
113-
long? number = null;
114-
115-
if (lastPrefixedSemver != null)
83+
if (semver.CompareTo(Context.CurrentCommitTaggedVersion) == 0)
11684
{
117-
number = lastPrefixedSemver.PreReleaseTag!.Number + 1;
85+
// Will always be 0, don't bother with the +0 on tags
86+
semver.BuildMetaData.CommitsSinceTag = null;
87+
}
88+
else if (string.IsNullOrEmpty(preReleaseTagName))
89+
{
90+
semver.PreReleaseTag = new SemanticVersionPreReleaseTag();
11891
}
11992
else
12093
{
121-
number = 1;
94+
long? number;
95+
96+
if (semver.PreReleaseTag.Name == preReleaseTagName)
97+
{
98+
number = semver.PreReleaseTag.Number + 1;
99+
}
100+
else
101+
{
102+
number = 1;
103+
}
104+
105+
semver.PreReleaseTag = new SemanticVersionPreReleaseTag(preReleaseTagName, number);
122106
}
123107

124-
semanticVersion.PreReleaseTag = new SemanticVersionPreReleaseTag(preReleaseTagName, number);
108+
return new(semver, nextVersion.BaseVersion, new(nextVersion.Branch, nextVersion.Configuration));
125109
}
126110

127111
private static void EnsureHeadIsNotDetached(GitVersionContext context)

0 commit comments

Comments
 (0)