Skip to content

Commit 8e6eef4

Browse files
AlexPykavyarturcic
authored andcommitted
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 b2033f9 commit 8e6eef4

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)
@@ -68,61 +69,44 @@ public virtual NextVersion FindVersion()
6869
}
6970

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

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

91-
// replace calculated version with tagged version only if tagged version greater or equal to calculated version
92-
if (taggedSemanticVersion.CompareTo(semver, false) >= 0)
78+
if (lastPrefixedSemver != null)
9379
{
94-
// set the commit count on the tagged ver
95-
taggedSemanticVersion.BuildMetaData.CommitsSinceVersionSource = semver.BuildMetaData?.CommitsSinceVersionSource;
96-
97-
semver = taggedSemanticVersion;
80+
semver.PreReleaseTag = lastPrefixedSemver.PreReleaseTag;
9881
}
9982
}
10083

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

125-
semanticVersion.PreReleaseTag = new SemanticVersionPreReleaseTag(preReleaseTagName, number);
109+
return new(semver, nextVersion.BaseVersion, new(nextVersion.Branch, nextVersion.Configuration));
126110
}
127111

128112
private static void EnsureHeadIsNotDetached(GitVersionContext context)

0 commit comments

Comments
 (0)