Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mechanism to configure a pre-release-weight for the tagged commits #2222

4 changes: 4 additions & 0 deletions docs/input/docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ The number of characters to pad `CommitsSinceVersionSource` to in the
set to `4`, which will pad the `CommitsSinceVersionSource` value of `1` to
`0001`.

### tag-pre-release-weight

The pre-release weight in case of tagged commits. If the value is not set in the configuration, a default weight of 60000 is used instead. If the `WeightedPreReleaseNumber` [variable](./more-info/variables) is 0 and this parameter is set, its value is used. This helps if your branching model is GitFlow and the last release build, which is often tagged, can utilise this parameter to produce a monotonically increasing build number.

### commit-message-incrementing

Sets whether it should be possible to increment the version with special syntax
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ no-bump-message: '\+semver:\s?(none|skip)'
legacy-semver-padding: 4
build-metadata-padding: 4
commits-since-version-source-padding: 4
tag-pre-release-weight: 60000
commit-message-incrementing: Enabled
branches:
develop:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public TestEffectiveConfiguration(
majorMessage, minorMessage, patchMessage, noBumpMessage,
commitMessageMode, legacySemVerPadding, buildMetaDataPadding, commitsSinceVersionSourcePadding,
versionFilters ?? Enumerable.Empty<IVersionFilter>(),
tracksReleaseBranches, isRelease, commitDateFormat, updateBuildNumber, 0)
tracksReleaseBranches, isRelease, commitDateFormat, updateBuildNumber, 0, 0)
{
}
}
Expand Down
107 changes: 107 additions & 0 deletions src/GitVersionCore.Tests/IntegrationTests/VersionInTagScenarios.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using GitTools.Testing;
using GitVersion.Configuration;
using GitVersion.Model.Configuration;
using GitVersion.VersionCalculation;
using NUnit.Framework;
using Shouldly;

namespace GitVersionCore.Tests.IntegrationTests
{
[TestFixture]
class VersionInTagScenarios
{
[Test]
public void TagPreReleaseWeightIsNotConfigured_HeadIsATaggedCommit_WeightedPreReleaseNumberShouldBeTheDefaultValue()
{
// Arrange
var config = new Config()
{
AssemblyFileVersioningFormat = "{Major}.{Minor}.{Patch}.{WeightedPreReleaseNumber}",
};
config.ApplyDefaults();

// Act
using var fixture = new BaseGitFlowRepositoryFixture("1.0.0");
fixture.MakeATaggedCommit("1.1.0");
var version = fixture.GetVersion(config);

// Assert
version.AssemblySemFileVer.ShouldBe("1.1.0.60000");
}

[Test]
public void TagPreReleaseWeightIsConfigured_HeadIsATaggedCommit_WeightedPreReleaseNumberShouldBeTheSameAsTheTagPreReleaseWeight()
{
// Arrange
var config = new Config()
{
AssemblyFileVersioningFormat = "{Major}.{Minor}.{Patch}.{WeightedPreReleaseNumber}",
TagPreReleaseWeight = 65535
};
config.ApplyDefaults();

// Act
using var fixture = new BaseGitFlowRepositoryFixture("1.0.0");
fixture.MakeATaggedCommit("1.1.0");
var version = fixture.GetVersion(config);

// Assert
version.AssemblySemFileVer.ShouldBe("1.1.0.65535");
}

[Test]
public void TagPreReleaseWeightIsConfigured_GitFlowReleaseIsFinished_WeightedPreReleaseNumberShouldBeTheSameAsTheTagPreReleaseWeight()
{
// Arrange
var config = new Config()
{
AssemblyFileVersioningFormat = "{Major}.{Minor}.{Patch}.{WeightedPreReleaseNumber}",
TagPreReleaseWeight = 65535,
VersioningMode = VersioningMode.ContinuousDeployment
};
config.ApplyDefaults();

// Act
using var fixture = new BaseGitFlowRepositoryFixture("1.0.0");
fixture.Checkout("master");
fixture.MergeNoFF("develop");
fixture.Checkout("develop");
fixture.MakeACommit("Feature commit 1");
fixture.BranchTo("release/1.1.0");
fixture.MakeACommit("Release commit 1");
fixture.AssertFullSemver("1.1.0-beta.1", config);
fixture.ApplyTag("1.1.0");
var version = fixture.GetVersion(config);

// Assert
version.AssemblySemFileVer.ShouldBe("1.1.0.65535");
}

[Test]
public void TagPreReleaseWeightIsNotConfigured_GitFlowReleaseIsFinished_WeightedPreReleaseNumberShouldBeTheDefaultValue()
{
// Arrange
var config = new Config()
{
AssemblyFileVersioningFormat = "{Major}.{Minor}.{Patch}.{WeightedPreReleaseNumber}",
VersioningMode = VersioningMode.ContinuousDeployment
};
config.ApplyDefaults();

// Act
using var fixture = new BaseGitFlowRepositoryFixture("1.0.0");
fixture.Checkout("master");
fixture.MergeNoFF("develop");
fixture.Checkout("develop");
fixture.MakeACommit("Feature commit 1");
fixture.BranchTo("release/1.1.0");
fixture.MakeACommit("Release commit 1");
fixture.AssertFullSemver("1.1.0-beta.1", config);
fixture.ApplyTag("1.1.0");
var version = fixture.GetVersion(config);

// Assert
version.AssemblySemFileVer.ShouldBe("1.1.0.60000");
}
}
}
2 changes: 1 addition & 1 deletion src/GitVersionCore.Tests/Model/CommitDateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void CommitDateFormatTest(string format, string expectedOutcome)
},
new EffectiveConfiguration(
AssemblyVersioningScheme.MajorMinorPatch, AssemblyFileVersioningScheme.MajorMinorPatch, "", "", "", VersioningMode.ContinuousDelivery, "", "", "", IncrementStrategy.Inherit,
"", true, "", "", false, "", "", "", "", CommitMessageIncrementMode.Enabled, 4, 4, 4, Enumerable.Empty<IVersionFilter>(), false, true, format, false, 0)
"", true, "", "", false, "", "", "", "", CommitMessageIncrementMode.Enabled, 4, 4, 4, Enumerable.Empty<IVersionFilter>(), false, true, format, false, 0, 0)
);

Assert.That(formatValues.CommitDate, Is.EqualTo(expectedOutcome));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"PreReleaseTagWithDash":"",
"PreReleaseLabel":"",
"PreReleaseNumber":"",
"WeightedPreReleaseNumber":"",
"WeightedPreReleaseNumber":0,
"BuildMetaData":5,
"BuildMetaDataPadded":"0005",
"FullBuildMetaData":"5.Branch.feature-123.Sha.commitSha",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"PreReleaseTagWithDash":"",
"PreReleaseLabel":"",
"PreReleaseNumber":"",
"WeightedPreReleaseNumber":"",
"WeightedPreReleaseNumber":0,
"BuildMetaData":5,
"BuildMetaDataPadded":"0005",
"FullBuildMetaData":"5.Branch.feature-123.Sha.commitSha",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"PreReleaseTagWithDash":"",
"PreReleaseLabel":"",
"PreReleaseNumber":"",
"WeightedPreReleaseNumber":"",
"WeightedPreReleaseNumber":0,
"BuildMetaData":5,
"BuildMetaDataPadded":"0005",
"FullBuildMetaData":"5.Branch.develop.Sha.commitSha",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"PreReleaseTagWithDash":"",
"PreReleaseLabel":"",
"PreReleaseNumber":"",
"WeightedPreReleaseNumber":"",
"WeightedPreReleaseNumber":0,
"BuildMetaData":5,
"BuildMetaDataPadded":"0005",
"FullBuildMetaData":"5.Sha.commitSha",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@
<?define Sha="commitSha"?>
<?define ShortSha="commitShortSha"?>
<?define VersionSourceSha="versionSourceSha"?>
<?define WeightedPreReleaseNumber=""?>
<?define WeightedPreReleaseNumber="0"?>
</Include>
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@
<?define Sha="commitSha"?>
<?define ShortSha="commitShortSha"?>
<?define VersionSourceSha="versionSourceSha"?>
<?define WeightedPreReleaseNumber=""?>
<?define WeightedPreReleaseNumber="0"?>
</Include>
8 changes: 6 additions & 2 deletions src/GitVersionCore/Configuration/ConfigExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public static void Reset(this Config config)
config.CommitsSinceVersionSourcePadding ??= 4;
config.CommitDateFormat ??= "yyyy-MM-dd";
config.UpdateBuildNumber ??= true;
config.TagPreReleaseWeight ??= DefaultTagPreReleaseWeight;

var configBranches = config.Branches.ToList();

Expand Down Expand Up @@ -128,6 +129,7 @@ public static void Reset(this Config config)
{ Config.MasterBranchRegex, 55000 }
};
private const IncrementStrategy DefaultIncrementStrategy = IncrementStrategy.Inherit;
private const int DefaultTagPreReleaseWeight = 60000;

public static void ApplyBranchDefaults(this Config config,
BranchConfig branchConfig,
Expand Down Expand Up @@ -182,6 +184,7 @@ public static void ApplyOverridesTo(this Config config, Config overrideConfig)
config.VersioningMode = overrideConfig.VersioningMode;
config.AssemblyFileVersioningFormat = overrideConfig.AssemblyFileVersioningFormat;
config.TagPrefix = string.IsNullOrWhiteSpace(overrideConfig.TagPrefix) ? config.TagPrefix : overrideConfig.TagPrefix;
config.TagPreReleaseWeight = overrideConfig.TagPreReleaseWeight;
}

public static BranchConfig GetConfigForBranch(this Config config, string branchName)
Expand Down Expand Up @@ -265,9 +268,9 @@ public static EffectiveConfiguration CalculateEffectiveConfiguration(this Config
var noBumpMessage = configuration.NoBumpMessage;
var commitDateFormat = configuration.CommitDateFormat;
var updateBuildNumber = configuration.UpdateBuildNumber ?? true;
var tagPreReleaseWeight = configuration.TagPreReleaseWeight ?? DefaultTagPreReleaseWeight;

var commitMessageVersionBump = currentBranchConfig.CommitMessageIncrementing ?? configuration.CommitMessageIncrementing.Value;

return new EffectiveConfiguration(
assemblyVersioningScheme, assemblyFileVersioningScheme, assemblyInformationalFormat, assemblyVersioningFormat, assemblyFileVersioningFormat, versioningMode, gitTagPrefix,
tag, nextVersion, incrementStrategy,
Expand All @@ -285,7 +288,8 @@ public static EffectiveConfiguration CalculateEffectiveConfiguration(this Config
currentBranchConfig.IsReleaseBranch.Value,
commitDateFormat,
updateBuildNumber,
preReleaseWeight);
preReleaseWeight,
tagPreReleaseWeight);
}

public static string GetBranchSpecificTag(this EffectiveConfiguration configuration, ILog log, string branchFriendlyName, string branchNameOverride)
Expand Down
3 changes: 3 additions & 0 deletions src/GitVersionCore/Model/Configuration/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ public string NextVersion
[YamlMember(Alias = "commits-since-version-source-padding")]
public int? CommitsSinceVersionSourcePadding { get; set; }

[YamlMember(Alias = "tag-pre-release-weight")]
public int? TagPreReleaseWeight { get; set; }

[YamlMember(Alias = "commit-message-incrementing")]
public CommitMessageIncrementMode? CommitMessageIncrementing { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public EffectiveConfiguration(
bool isCurrentBranchRelease,
string commitDateFormat,
bool updateBuildNumber,
int preReleaseWeight)
int preReleaseWeight,
int tagPreReleaseWeight)
{
AssemblyVersioningScheme = assemblyVersioningScheme;
AssemblyFileVersioningScheme = assemblyFileVersioningScheme;
Expand Down Expand Up @@ -66,6 +67,7 @@ public EffectiveConfiguration(
CommitDateFormat = commitDateFormat;
UpdateBuildNumber = updateBuildNumber;
PreReleaseWeight = preReleaseWeight;
TagPreReleaseWeight = tagPreReleaseWeight;
}

public bool TracksReleaseBranches { get; private set; }
Expand Down Expand Up @@ -124,5 +126,7 @@ public EffectiveConfiguration(
public bool UpdateBuildNumber { get; private set; }

public int PreReleaseWeight { get; private set; }

public int TagPreReleaseWeight { get; private set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public SemanticVersionFormatValues(SemanticVersion semver, EffectiveConfiguratio

public string PreReleaseNumber => semver.PreReleaseTag.HasTag() ? semver.PreReleaseTag.Number.ToString() : null;

public string WeightedPreReleaseNumber => semver.PreReleaseTag.HasTag() ? (semver.PreReleaseTag.Number + config.PreReleaseWeight).ToString() : null;
public string WeightedPreReleaseNumber => GetWeightedPreReleaseNumber();

public string BuildMetaData => semver.BuildMetaData;

Expand Down Expand Up @@ -82,5 +82,17 @@ public SemanticVersionFormatValues(SemanticVersion semver, EffectiveConfiguratio
public string CommitsSinceVersionSource => semver.BuildMetaData.CommitsSinceVersionSource.ToString(CultureInfo.InvariantCulture);

public string CommitsSinceVersionSourcePadded => semver.BuildMetaData.CommitsSinceVersionSource.ToString(CultureInfo.InvariantCulture).PadLeft(config.CommitsSinceVersionSourcePadding, '0');

private String GetWeightedPreReleaseNumber()
{
var weightedPreReleaseNumber =
semver.PreReleaseTag.HasTag() ? (semver.PreReleaseTag.Number + config.PreReleaseWeight).ToString() : null;
if (string.IsNullOrEmpty(weightedPreReleaseNumber))
{
weightedPreReleaseNumber = $"{config.TagPreReleaseWeight}";
}

return weightedPreReleaseNumber;
}
}
}