Skip to content

Commit e0f2051

Browse files
committed
Implement when PreleaseLabel is empty, the PreleaseTag is generated correctly
1 parent 83dbf0e commit e0f2051

File tree

10 files changed

+116
-31
lines changed

10 files changed

+116
-31
lines changed

src/GitVersion.Core.Tests/IntegrationTests/OtherScenarios.cs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -926,12 +926,12 @@ public void IncreaseVersionWithBumpMessageWhenCommitMessageIncrementIsEnabledAnd
926926
fixture.MakeACommit();
927927

928928
// ✅ succeeds as expected
929-
fixture.AssertFullSemver("0.0.0", configuration);
929+
fixture.AssertFullSemver("0.0.0-1", configuration);
930930

931931
fixture.MakeACommit("+semver: minor");
932932

933933
// ✅ succeeds as expected
934-
fixture.AssertFullSemver("0.1.0", configuration);
934+
fixture.AssertFullSemver("0.1.0-2", configuration);
935935

936936
fixture.ApplyTag("1.0.0");
937937

@@ -941,13 +941,13 @@ public void IncreaseVersionWithBumpMessageWhenCommitMessageIncrementIsEnabledAnd
941941
fixture.MakeACommit("+semver: major");
942942

943943
// ✅ succeeds as expected
944-
fixture.AssertFullSemver("2.0.0", configuration);
944+
fixture.AssertFullSemver("2.0.0-1", configuration);
945945

946946
fixture.ApplyTag("2.0.0");
947947
fixture.MakeACommit();
948948

949949
// ✅ succeeds as expected
950-
fixture.AssertFullSemver("2.0.0", configuration);
950+
fixture.AssertFullSemver("2.0.0-1", configuration);
951951
}
952952

953953
[Test]
@@ -1027,4 +1027,24 @@ public void ShouldProvideTheCorrectVersionEvenIfPreReleaseLabelExistsInTheGitTag
10271027
// ✅ succeeds as expected
10281028
fixture.AssertFullSemver("5.0.0-beta.5", configuration);
10291029
}
1030+
1031+
/// <summary>
1032+
/// https://github.com/GitTools/GitVersion/issues/2347
1033+
/// </summary>
1034+
[Test]
1035+
public void EnsureThePreReleaseTagIsCorrectlyGeneratedWhenPreReleaseLabelIsEmpty()
1036+
{
1037+
var configuration = GitFlowConfigurationBuilder.New
1038+
.WithBranch("main", _ => _
1039+
.WithLabel(string.Empty)
1040+
.WithVersioningMode(VersioningMode.ContinuousDeployment)
1041+
).Build();
1042+
1043+
using var fixture = new EmptyRepositoryFixture("main");
1044+
fixture.Repository.MakeCommits(5);
1045+
1046+
var variables = fixture.GetVersion(configuration);
1047+
1048+
fixture.AssertFullSemver("0.0.1-5", configuration);
1049+
}
10301050
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"Major": 1,
3+
"Minor": 2,
4+
"Patch": 3,
5+
"PreReleaseTag": "9",
6+
"PreReleaseTagWithDash": "-9",
7+
"PreReleaseLabel": "",
8+
"PreReleaseLabelWithDash": "",
9+
"PreReleaseNumber": 9,
10+
"WeightedPreReleaseNumber": 55009,
11+
"BuildMetaData": null,
12+
"FullBuildMetaData": "Branch.main.Sha.commitSha",
13+
"MajorMinorPatch": "1.2.3",
14+
"SemVer": "1.2.3-9",
15+
"AssemblySemVer": "1.2.3.0",
16+
"AssemblySemFileVer": "1.2.3.0",
17+
"FullSemVer": "1.2.3-9",
18+
"InformationalVersion": "1.2.3-9+Branch.main.Sha.commitSha",
19+
"BranchName": "main",
20+
"EscapedBranchName": "main",
21+
"Sha": "commitSha",
22+
"ShortSha": "commitShortSha",
23+
"VersionSourceSha": "versionSourceSha",
24+
"CommitsSinceVersionSource": 5,
25+
"CommitDate": "2014-03-06",
26+
"UncommittedChanges": 0
27+
}

src/GitVersion.Core.Tests/VersionCalculation/VariableProviderTests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using GitVersion.Configuration;
12
using GitVersion.Core.Tests.Helpers;
23
using GitVersion.Logging;
34
using GitVersion.OutputVariables;
@@ -250,4 +251,35 @@ public void ProvidesVariablesInContinuousDeliveryModeForFeatureBranchWithCustomA
250251

251252
vars.ToJsonString().ShouldMatchApproved(c => c.SubFolder("Approved"));
252253
}
254+
255+
[Test]
256+
public void ProvidesVariablesInContinuousDeploymentModeForMainBranchWithEmptyLabel()
257+
{
258+
var semanticVersion = new SemanticVersion
259+
{
260+
Major = 1,
261+
Minor = 2,
262+
Patch = 3,
263+
PreReleaseTag = new(string.Empty, 5),
264+
BuildMetaData = new("5.Branch.main")
265+
{
266+
Branch = "main",
267+
VersionSourceSha = "versionSourceSha",
268+
Sha = "commitSha",
269+
ShortSha = "commitShortSha",
270+
CommitDate = DateTimeOffset.Parse("2014-03-06 23:59:59Z")
271+
}
272+
};
273+
274+
var configuration = GitFlowConfigurationBuilder.New
275+
.WithBranch("main", _ => _
276+
.WithLabel(string.Empty)
277+
.WithVersioningMode(VersioningMode.ContinuousDeployment)
278+
).Build().GetEffectiveConfiguration(ReferenceName.FromBranchName("main"));
279+
280+
281+
var variables = this.variableProvider.GetVariablesFor(semanticVersion, configuration, null);
282+
283+
variables.ToString().ShouldMatchApproved(_ => _.SubFolder("Approved"));
284+
}
253285
}

src/GitVersion.Core/Configuration/ConfigurationExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public static class ConfigurationExtensions
88
public static EffectiveConfiguration GetEffectiveConfiguration(this IGitVersionConfiguration configuration, IBranch branch)
99
=> GetEffectiveConfiguration(configuration, branch.NotNull().Name);
1010

11-
private static EffectiveConfiguration GetEffectiveConfiguration(this IGitVersionConfiguration configuration, ReferenceName branchName)
11+
public static EffectiveConfiguration GetEffectiveConfiguration(this IGitVersionConfiguration configuration, ReferenceName branchName)
1212
{
1313
IBranchConfiguration branchConfiguration = configuration.GetBranchConfiguration(branchName);
1414
return new EffectiveConfiguration(configuration, branchConfiguration);

src/GitVersion.Core/Extensions/StringExtensions.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,8 @@ public static bool IsEquivalentTo(this string self, string? other) =>
104104
public static bool IsNullOrWhiteSpace([NotNullWhen(false)] this string? value) => string.IsNullOrWhiteSpace(value);
105105

106106
public static bool IsEmpty([NotNullWhen(false)] this string? value) => string.Empty.Equals(value);
107+
108+
public static string WithPrefixIfNotNullOrEmpty(this string value, string prefix)
109+
=> string.IsNullOrEmpty(value) ? value : prefix + value;
110+
107111
}

src/GitVersion.Core/PublicAPI.Unshipped.txt

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,7 @@ GitVersion.SemanticVersionFormatValues
813813
GitVersion.SemanticVersionFormatValues.AssemblyFileSemVer.get -> string?
814814
GitVersion.SemanticVersionFormatValues.AssemblySemVer.get -> string?
815815
GitVersion.SemanticVersionFormatValues.BranchName.get -> string?
816-
GitVersion.SemanticVersionFormatValues.BuildMetaData.get -> string?
816+
GitVersion.SemanticVersionFormatValues.BuildMetaData.get -> string!
817817
GitVersion.SemanticVersionFormatValues.CommitDate.get -> string?
818818
GitVersion.SemanticVersionFormatValues.CommitsSinceVersionSource.get -> string?
819819
GitVersion.SemanticVersionFormatValues.EscapedBranchName.get -> string?
@@ -824,13 +824,13 @@ GitVersion.SemanticVersionFormatValues.Major.get -> string!
824824
GitVersion.SemanticVersionFormatValues.MajorMinorPatch.get -> string!
825825
GitVersion.SemanticVersionFormatValues.Minor.get -> string!
826826
GitVersion.SemanticVersionFormatValues.Patch.get -> string!
827-
GitVersion.SemanticVersionFormatValues.PreReleaseLabel.get -> string?
828-
GitVersion.SemanticVersionFormatValues.PreReleaseLabelWithDash.get -> string?
829-
GitVersion.SemanticVersionFormatValues.PreReleaseNumber.get -> string?
830-
GitVersion.SemanticVersionFormatValues.PreReleaseTag.get -> string?
831-
GitVersion.SemanticVersionFormatValues.PreReleaseTagWithDash.get -> string?
832-
GitVersion.SemanticVersionFormatValues.SemVer.get -> string!
827+
GitVersion.SemanticVersionFormatValues.PreReleaseLabel.get -> string!
828+
GitVersion.SemanticVersionFormatValues.PreReleaseLabelWithDash.get -> string!
829+
GitVersion.SemanticVersionFormatValues.PreReleaseNumber.get -> string!
830+
GitVersion.SemanticVersionFormatValues.PreReleaseTag.get -> string!
831+
GitVersion.SemanticVersionFormatValues.PreReleaseTagWithDash.get -> string!
833832
GitVersion.SemanticVersionFormatValues.SemanticVersionFormatValues(GitVersion.SemanticVersion! semver, GitVersion.Configuration.EffectiveConfiguration! configuration) -> void
833+
GitVersion.SemanticVersionFormatValues.SemVer.get -> string!
834834
GitVersion.SemanticVersionFormatValues.Sha.get -> string?
835835
GitVersion.SemanticVersionFormatValues.ShortSha.get -> string?
836836
GitVersion.SemanticVersionFormatValues.UncommittedChanges.get -> string!
@@ -844,11 +844,11 @@ GitVersion.SemanticVersionPreReleaseTag.Name.get -> string!
844844
GitVersion.SemanticVersionPreReleaseTag.Name.init -> void
845845
GitVersion.SemanticVersionPreReleaseTag.Number.get -> long?
846846
GitVersion.SemanticVersionPreReleaseTag.Number.init -> void
847-
GitVersion.SemanticVersionPreReleaseTag.PromotedFromCommits.get -> bool?
848-
GitVersion.SemanticVersionPreReleaseTag.PromotedFromCommits.init -> void
847+
GitVersion.SemanticVersionPreReleaseTag.PromoteTagEvenIfNameIsEmpty.get -> bool
848+
GitVersion.SemanticVersionPreReleaseTag.PromoteTagEvenIfNameIsEmpty.init -> void
849849
GitVersion.SemanticVersionPreReleaseTag.SemanticVersionPreReleaseTag() -> void
850850
GitVersion.SemanticVersionPreReleaseTag.SemanticVersionPreReleaseTag(GitVersion.SemanticVersionPreReleaseTag! preReleaseTag) -> void
851-
GitVersion.SemanticVersionPreReleaseTag.SemanticVersionPreReleaseTag(string! name, long? number) -> void
851+
GitVersion.SemanticVersionPreReleaseTag.SemanticVersionPreReleaseTag(string! name, long? number, bool promoteTagEvenIfNameIsEmpty = true) -> void
852852
GitVersion.SemanticVersionPreReleaseTag.ToString(string! format) -> string!
853853
GitVersion.SemanticVersionPreReleaseTag.ToString(string? format, System.IFormatProvider? formatProvider) -> string!
854854
GitVersion.SemanticVersionWithTag
@@ -1102,6 +1102,7 @@ static GitVersion.Configuration.ConfigurationExtensions.GetBranchConfiguration(t
11021102
static GitVersion.Configuration.ConfigurationExtensions.GetBranchSpecificLabel(this GitVersion.Configuration.EffectiveConfiguration! configuration, GitVersion.ReferenceName! branchName, string? branchNameOverride) -> string?
11031103
static GitVersion.Configuration.ConfigurationExtensions.GetBranchSpecificLabel(this GitVersion.Configuration.EffectiveConfiguration! configuration, string? branchName, string? branchNameOverride) -> string?
11041104
static GitVersion.Configuration.ConfigurationExtensions.GetEffectiveConfiguration(this GitVersion.Configuration.IGitVersionConfiguration! configuration, GitVersion.IBranch! branch) -> GitVersion.Configuration.EffectiveConfiguration!
1105+
static GitVersion.Configuration.ConfigurationExtensions.GetEffectiveConfiguration(this GitVersion.Configuration.IGitVersionConfiguration! configuration, GitVersion.ReferenceName! branchName) -> GitVersion.Configuration.EffectiveConfiguration!
11051106
static GitVersion.Configuration.ConfigurationExtensions.GetFallbackBranchConfiguration(this GitVersion.Configuration.IGitVersionConfiguration! configuration) -> GitVersion.Configuration.IBranchConfiguration!
11061107
static GitVersion.Configuration.ConfigurationExtensions.GetReleaseBranchConfiguration(this GitVersion.Configuration.IGitVersionConfiguration! configuration) -> System.Collections.Generic.List<System.Collections.Generic.KeyValuePair<string!, GitVersion.Configuration.IBranchConfiguration!>>!
11071108
static GitVersion.Configuration.ConfigurationExtensions.IsReleaseBranch(this GitVersion.Configuration.IGitVersionConfiguration! configuration, GitVersion.IBranch! branch) -> bool
@@ -1144,6 +1145,7 @@ static GitVersion.Extensions.StringExtensions.IsSwitchArgument(this string? valu
11441145
static GitVersion.Extensions.StringExtensions.IsTrue(this string? value) -> bool
11451146
static GitVersion.Extensions.StringExtensions.IsValidPath(this string? path) -> bool
11461147
static GitVersion.Extensions.StringExtensions.RegexReplace(this string! input, string! pattern, string! replace, System.Text.RegularExpressions.RegexOptions options = System.Text.RegularExpressions.RegexOptions.None) -> string!
1148+
static GitVersion.Extensions.StringExtensions.WithPrefixIfNotNullOrEmpty(this string! value, string! prefix) -> string!
11471149
static GitVersion.GitVersionModule.FindAllDerivedTypes<T>(System.Reflection.Assembly? assembly) -> System.Collections.Generic.IEnumerable<System.Type!>!
11481150
static GitVersion.Helpers.EncodingHelper.DetectEncoding(System.Collections.Generic.IList<byte>! bytes) -> System.Text.Encoding?
11491151
static GitVersion.Helpers.EncodingHelper.DetectEncoding(string? filename) -> System.Text.Encoding?

src/GitVersion.Core/VersionCalculation/MainlineVersionCalculator.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,10 @@ public SemanticVersion FindMainlineModeVersion(NextVersion nextVersion)
8383

8484
return new SemanticVersion(mainlineVersion)
8585
{
86-
PreReleaseTag = new SemanticVersionPreReleaseTag(nextVersion.IncrementedVersion.PreReleaseTag),
86+
PreReleaseTag = new SemanticVersionPreReleaseTag(nextVersion.IncrementedVersion.PreReleaseTag)
87+
{
88+
PromoteTagEvenIfNameIsEmpty = false
89+
},
8790
BuildMetaData = baseVersionBuildMetaData
8891
};
8992
}

src/GitVersion.Core/VersionCalculation/SemanticVersioning/SemanticVersionFormatValues.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,19 @@ public SemanticVersionFormatValues(SemanticVersion semver, EffectiveConfiguratio
2121

2222
public string Patch => this.semver.Patch.ToString();
2323

24-
public string? PreReleaseTag => this.semver.PreReleaseTag;
24+
public string PreReleaseTag => this.semver.PreReleaseTag.ToString();
2525

26-
public string? PreReleaseTagWithDash => this.semver.PreReleaseTag.HasTag() ? "-" + this.semver.PreReleaseTag : null;
26+
public string PreReleaseTagWithDash => this.PreReleaseTag.WithPrefixIfNotNullOrEmpty("-");
2727

28-
public string? PreReleaseLabel => this.semver.PreReleaseTag.HasTag() ? this.semver.PreReleaseTag.Name : null;
28+
public string PreReleaseLabel => this.semver.PreReleaseTag.Name;
2929

30-
public string? PreReleaseLabelWithDash => this.semver.PreReleaseTag.HasTag() ? "-" + this.semver.PreReleaseTag.Name : null;
30+
public string PreReleaseLabelWithDash => this.PreReleaseLabel.WithPrefixIfNotNullOrEmpty("-");
3131

32-
public string? PreReleaseNumber => this.semver.PreReleaseTag.HasTag() ? this.semver.PreReleaseTag.Number.ToString() : null;
32+
public string PreReleaseNumber => this.semver.PreReleaseTag.Number?.ToString() ?? string.Empty;
3333

3434
public string WeightedPreReleaseNumber => GetWeightedPreReleaseNumber();
3535

36-
public string? BuildMetaData => this.semver.BuildMetaData;
36+
public string BuildMetaData => this.semver.BuildMetaData.ToString();
3737

3838
public string FullBuildMetaData => this.semver.BuildMetaData.ToString("f");
3939

src/GitVersion.Core/VersionCalculation/SemanticVersioning/SemanticVersionPreReleaseTag.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@ public sealed class SemanticVersionPreReleaseTag :
2121

2222
public long? Number { get; init; }
2323

24-
public bool? PromotedFromCommits { get; init; }
24+
public bool PromoteTagEvenIfNameIsEmpty { get; init; }
2525

2626
public SemanticVersionPreReleaseTag() => Name = string.Empty;
2727

28-
public SemanticVersionPreReleaseTag(string name, long? number)
28+
public SemanticVersionPreReleaseTag(string name, long? number, bool promoteTagEvenIfNameIsEmpty = true)
2929
{
3030
Name = name.NotNull();
3131
Number = number;
32+
PromoteTagEvenIfNameIsEmpty = promoteTagEvenIfNameIsEmpty;
3233
}
3334

3435
public SemanticVersionPreReleaseTag(SemanticVersionPreReleaseTag preReleaseTag)
@@ -37,7 +38,7 @@ public SemanticVersionPreReleaseTag(SemanticVersionPreReleaseTag preReleaseTag)
3738

3839
Name = preReleaseTag.Name;
3940
Number = preReleaseTag.Number;
40-
PromotedFromCommits = preReleaseTag.PromotedFromCommits;
41+
PromoteTagEvenIfNameIsEmpty = preReleaseTag.PromoteTagEvenIfNameIsEmpty;
4142
}
4243

4344
public override bool Equals(object? obj) => Equals(obj as SemanticVersionPreReleaseTag);
@@ -130,6 +131,5 @@ public string ToString(string? format, IFormatProvider? formatProvider)
130131
};
131132
}
132133

133-
public bool HasTag() =>
134-
!Name.IsNullOrEmpty() || (Number.HasValue && PromotedFromCommits != true);
134+
public bool HasTag() => !Name.IsNullOrEmpty() || Number.HasValue && PromoteTagEvenIfNameIsEmpty;
135135
}

src/GitVersion.Core/VersionCalculation/VariableProvider.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ public GitVersionVariables GetVariablesFor(
109109
private static SemanticVersion PromoteNumberOfCommitsToTagNumber(SemanticVersion semanticVersion, string preReleaseTagName)
110110
{
111111
var preReleaseTagNumber = semanticVersion.PreReleaseTag.Number;
112-
var preReleaseTagPromotedFromCommits = semanticVersion.PreReleaseTag.PromotedFromCommits;
113112
long buildMetaDataCommitsSinceVersionSource;
114113
var buildMetaDataCommitsSinceTag = semanticVersion.BuildMetaData.CommitsSinceTag;
115114

@@ -129,7 +128,6 @@ private static SemanticVersion PromoteNumberOfCommitsToTagNumber(SemanticVersion
129128
else
130129
{
131130
preReleaseTagNumber = semanticVersion.BuildMetaData.CommitsSinceTag;
132-
preReleaseTagPromotedFromCommits = true;
133131
}
134132
buildMetaDataCommitsSinceVersionSource = semanticVersion.BuildMetaData.CommitsSinceTag.Value;
135133
buildMetaDataCommitsSinceTag = null; // why is this set to null ?
@@ -140,8 +138,7 @@ private static SemanticVersion PromoteNumberOfCommitsToTagNumber(SemanticVersion
140138
PreReleaseTag = new(semanticVersion.PreReleaseTag)
141139
{
142140
Name = preReleaseTagName,
143-
Number = preReleaseTagNumber,
144-
PromotedFromCommits = preReleaseTagPromotedFromCommits
141+
Number = preReleaseTagNumber
145142
},
146143
BuildMetaData = new(semanticVersion.BuildMetaData)
147144
{

0 commit comments

Comments
 (0)