-
Notifications
You must be signed in to change notification settings - Fork 640
/
NextVersionCalculator.cs
161 lines (137 loc) · 7.26 KB
/
NextVersionCalculator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using GitVersion.Common;
using GitVersion.Configuration;
using GitVersion.Extensions;
using GitVersion.Logging;
using GitVersion.Model.Configuration;
namespace GitVersion.VersionCalculation;
public class NextVersionCalculator : INextVersionCalculator
{
private readonly ILog log;
private readonly IBaseVersionCalculator baseVersionCalculator;
private readonly IMainlineVersionCalculator mainlineVersionCalculator;
private readonly IRepositoryStore repositoryStore;
private readonly IIncrementStrategyFinder incrementStrategyFinder;
private readonly Lazy<GitVersionContext> versionContext;
private GitVersionContext Context => this.versionContext.Value;
public NextVersionCalculator(
ILog log,
IBaseVersionCalculator baseVersionCalculator,
IMainlineVersionCalculator mainlineVersionCalculator,
IRepositoryStore repositoryStore,
IIncrementStrategyFinder incrementStrategyFinder,
Lazy<GitVersionContext> versionContext)
{
this.log = log.NotNull();
this.baseVersionCalculator = baseVersionCalculator.NotNull();
this.mainlineVersionCalculator = mainlineVersionCalculator.NotNull();
this.repositoryStore = repositoryStore.NotNull();
this.incrementStrategyFinder = incrementStrategyFinder.NotNull();
this.versionContext = versionContext.NotNull();
}
public NextVersion FindVersion()
{
this.log.Info($"Running against branch: {Context.CurrentBranch} ({Context.CurrentCommit?.ToString() ?? "-"})");
if (Context.IsCurrentCommitTagged)
{
this.log.Info($"Current commit is tagged with version {Context.CurrentCommitTaggedVersion}, " + "version calculation is for metadata only.");
}
SemanticVersion? taggedSemanticVersion = null;
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;
var semanticVersion = new SemanticVersion(Context.CurrentCommitTaggedVersion) { BuildMetaData = semanticVersionBuildMetaData };
taggedSemanticVersion = semanticVersion;
}
var (baseVersion, configuration) = this.baseVersionCalculator.GetBaseVersion();
baseVersion.SemanticVersion.BuildMetaData = this.mainlineVersionCalculator.CreateVersionBuildMetaData(baseVersion.BaseVersionSource);
SemanticVersion semver;
if (Context.FullConfiguration.VersioningMode == VersioningMode.Mainline)
{
semver = this.mainlineVersionCalculator.FindMainlineModeVersion(baseVersion);
}
else
{
if (taggedSemanticVersion?.BuildMetaData == null || (taggedSemanticVersion.BuildMetaData?.Sha != baseVersion.SemanticVersion.BuildMetaData.Sha))
{
semver = PerformIncrement(baseVersion, configuration.Value);
semver.BuildMetaData = this.mainlineVersionCalculator.CreateVersionBuildMetaData(baseVersion.BaseVersionSource);
}
else
{
semver = baseVersion.SemanticVersion;
}
}
var hasPreReleaseTag = semver.PreReleaseTag?.HasTag() == true;
var tag = configuration.Value.Tag;
var branchConfigHasPreReleaseTagConfigured = !tag.IsNullOrEmpty();
var branchConfigIsMainlineAndHasEmptyPreReleaseTagConfigured = configuration.Value.IsMainline && tag.IsEmpty();
var preReleaseTagDoesNotMatchConfiguration = hasPreReleaseTag
&& (branchConfigHasPreReleaseTagConfigured || branchConfigIsMainlineAndHasEmptyPreReleaseTagConfigured)
&& semver.PreReleaseTag?.Name != tag;
var preReleaseTagOnlyInBranchConfig = !hasPreReleaseTag && branchConfigHasPreReleaseTagConfigured;
if (preReleaseTagOnlyInBranchConfig || preReleaseTagDoesNotMatchConfiguration)
{
UpdatePreReleaseTag(configuration.Value, semver, baseVersion.BranchNameOverride);
}
if (taggedSemanticVersion != null)
{
// replace calculated version with tagged version only if tagged version greater or equal to calculated version
if (semver.CompareTo(taggedSemanticVersion, false) > 0)
{
taggedSemanticVersion = null;
}
else if (taggedSemanticVersion.BuildMetaData != null)
{
// set the commit count on the tagged ver
taggedSemanticVersion.BuildMetaData.CommitsSinceVersionSource = semver.BuildMetaData?.CommitsSinceVersionSource;
// set the updated prerelease tag when it doesn't match with prerelease tag defined in branch configuration
if (preReleaseTagDoesNotMatchConfiguration)
{
taggedSemanticVersion.PreReleaseTag = semver.PreReleaseTag;
}
}
}
var incrementedVersion = taggedSemanticVersion ?? semver;
return new(incrementedVersion, baseVersion, configuration);
}
private SemanticVersion PerformIncrement(BaseVersion baseVersion, EffectiveConfiguration configuration)
{
var semver = baseVersion.SemanticVersion;
var increment = this.incrementStrategyFinder.DetermineIncrementedField(Context, baseVersion, configuration);
semver = semver.IncrementVersion(increment);
return semver;
}
private void UpdatePreReleaseTag(EffectiveConfiguration configuration, SemanticVersion semanticVersion, string? branchNameOverride)
{
var tagToUse = configuration.GetBranchSpecificTag(this.log, Context.CurrentBranch.Name.Friendly, branchNameOverride);
if (configuration.IsMainline && tagToUse.IsEmpty())
{
semanticVersion.PreReleaseTag = new SemanticVersionPreReleaseTag(tagToUse, null);
return;
}
long? number = null;
var lastTag = this.repositoryStore
.GetVersionTagsOnBranch(Context.CurrentBranch, Context.FullConfiguration.TagPrefix)
.FirstOrDefault(v => v.PreReleaseTag?.Name?.IsEquivalentTo(tagToUse) == true);
if (lastTag != null && MajorMinorPatchEqual(lastTag, semanticVersion) && lastTag.PreReleaseTag?.HasTag() == true)
{
number = lastTag.PreReleaseTag.Number + 1;
}
number ??= 1;
semanticVersion.PreReleaseTag = new SemanticVersionPreReleaseTag(tagToUse, number);
}
private static void EnsureHeadIsNotDetached(GitVersionContext context)
{
if (context.CurrentBranch.IsDetachedHead != true)
{
return;
}
var message = string.Format(
"It looks like the branch being examined is a detached Head pointing to commit '{0}'. " + "Without a proper branch name GitVersion cannot determine the build version.",
context.CurrentCommit?.Id.ToString(7));
throw new WarningException(message);
}
private static bool MajorMinorPatchEqual(SemanticVersion lastTag, SemanticVersion baseVersion) => lastTag.Major == baseVersion.Major && lastTag.Minor == baseVersion.Minor && lastTag.Patch == baseVersion.Patch;
}