Skip to content

Provide a way to translate the PreReleaseLabel (alpha, beta etc) to a numeric value to avoid version collisions #1433

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

4 changes: 4 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ branches:
track-merge-target: false
tracks-release-branches: false
is-release-branch: true
pre-release-weight: 1000
feature:
regex: ^features?[/-]
mode: ContinuousDelivery
Expand Down Expand Up @@ -385,3 +386,6 @@ Indicates this branch config represents a release branch in GitFlow.

### is-mainline
When using Mainline mode, this indicates that this branch is a mainline. By default support/ and master are mainlines.

### pre-release-weight
Provides a way to translate the `PreReleaseLabel` ([variables](/more-info/variables)) to a numeric value in order to avoid version collisions across different branches. For example, a release branch created after "1.2.3-alpha.55" results in "1.2.3-beta.1" and thus e.g. "1.2.3-alpha.4" and "1.2.3-beta.4" would have the same file version: "1.2.3.4". One of the ways to use this value is to set `assembly-file-versioning-format: {Major}.{Minor}.{Patch}.{WeightedPreReleaseNumber}`. If the `pre-release-weight` is set, it would be added to the `PreReleaseNumber` to get a final `AssemblySemFileVer`, otherwise a branch specific default for `pre-release-weight` will be used in the calculation. Related Issues [1145](https://github.com/GitTools/GitVersion/issues/1145), [1366](https://github.com/GitTools/GitVersion/issues/1366)
4 changes: 4 additions & 0 deletions docs/more-info/variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ For the `release/3.0.0` branch of GitVersion it shows:
"PreReleaseTagWithDash":"-beta.1",
"PreReleaseLabel":"beta",
"PreReleaseNumber":1,
"WeightedPreReleaseNumber":1001,
"BuildMetaData":1,
"BuildMetaDataPadded": "0001",
"FullBuildMetaData":"1.Branch.release/3.0.0.Sha.28c853159a46b5a87e6cc9c4f6e940c59d6bc68a",
Expand Down Expand Up @@ -40,3 +41,6 @@ For the `release/3.0.0` branch of GitVersion it shows:

This is a common approach that gives you the ability to roll out hot fixes to your assembly without breaking existing applications that may be referencing it. You are still able to get the full version number if you need to by looking at its file version number.

#### What is WeightedPreReleaseNumber?

It is a summation of branch specific `pre-release-weight` and the `PreReleaseNumber`. It can be used to obtain a monotonically increasing version number across the branches.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"PreReleaseTagWithDash":"-unstable.4",
"PreReleaseLabel":"unstable",
"PreReleaseNumber":4,
"WeightedPreReleaseNumber":4,
"BuildMetaData":5,
"BuildMetaDataPadded":"0005",
"FullBuildMetaData":"5.Branch.feature1.Sha.commitSha",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"PreReleaseTagWithDash":"-unstable.4",
"PreReleaseLabel":"unstable",
"PreReleaseNumber":4,
"WeightedPreReleaseNumber":4,
"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,6 +6,7 @@
"PreReleaseTagWithDash":"-unstable.4",
"PreReleaseLabel":"unstable",
"PreReleaseNumber":4,
"WeightedPreReleaseNumber":4,
"BuildMetaData":5,
"BuildMetaDataPadded":"05",
"FullBuildMetaData":"5.Branch.develop.Sha.commitSha",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"PreReleaseTagWithDash":"",
"PreReleaseLabel":"",
"PreReleaseNumber":"",
"WeightedPreReleaseNumber":"",
"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,6 +6,7 @@
"PreReleaseTagWithDash":"-unstable.5",
"PreReleaseLabel":"unstable",
"PreReleaseNumber":5,
"WeightedPreReleaseNumber":5,
"BuildMetaData":"",
"BuildMetaDataPadded":"",
"FullBuildMetaData":"Branch.develop.Sha.commitSha",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"PreReleaseTagWithDash":"-ci.5",
"PreReleaseLabel":"ci",
"PreReleaseNumber":5,
"WeightedPreReleaseNumber":5,
"BuildMetaData":"",
"BuildMetaDataPadded":"",
"FullBuildMetaData":"Branch.develop.Sha.commitSha",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"PreReleaseTagWithDash":"",
"PreReleaseLabel":"",
"PreReleaseNumber":"",
"WeightedPreReleaseNumber":"",
"BuildMetaData":5,
"BuildMetaDataPadded":"0005",
"FullBuildMetaData":"5.Sha.commitSha",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@
<?define SemVer="1.2.3"?>
<?define Sha="commitSha"?>
<?define ShortSha="commitShortSha"?>
<?define WeightedPreReleaseNumber=""?>
</Include>
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ static class GitVersionInformation
public static string PreReleaseTagWithDash = "-unstable.4";
public static string PreReleaseLabel = "unstable";
public static string PreReleaseNumber = "4";
public static string WeightedPreReleaseNumber = "4";
public static string BuildMetaData = "5";
public static string BuildMetaDataPadded = "0005";
public static string FullBuildMetaData = "5.Branch.feature1.Sha.commitSha";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module GitVersionInformation
let PreReleaseTagWithDash = "-unstable.4"
let PreReleaseLabel = "unstable"
let PreReleaseNumber = "4"
let WeightedPreReleaseNumber = "4"
let BuildMetaData = "5"
let BuildMetaDataPadded = "0005"
let FullBuildMetaData = "5.Branch.feature1.Sha.commitSha"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ NotInheritable Class GitVersionInformation
Public Shared PreReleaseTagWithDash As String = "-unstable.4"
Public Shared PreReleaseLabel As String = "unstable"
Public Shared PreReleaseNumber As String = "4"
Public Shared WeightedPreReleaseNumber As String = "4"
Public Shared BuildMetaData As String = "5"
Public Shared BuildMetaDataPadded As String = "0005"
Public Shared FullBuildMetaData As String = "5.Branch.feature1.Sha.commitSha"
Expand Down
2 changes: 1 addition & 1 deletion src/GitVersionCore.Tests/CommitDateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,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)
"", true, "", "", false, "", "", "", "", CommitMessageIncrementMode.Enabled, 4, 4, 4, Enumerable.Empty<IVersionFilter>(), false, true, format, 0)
);

Assert.That(formatValues.CommitDate, Is.EqualTo(expectedOutcome));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ branches:
tracks-release-branches: true
is-release-branch: false
is-mainline: false
pre-release-weight: 0
master:
mode: ContinuousDelivery
tag: ''
Expand All @@ -36,6 +37,7 @@ branches:
tracks-release-branches: false
is-release-branch: false
is-mainline: true
pre-release-weight: 55000
release:
mode: ContinuousDelivery
tag: beta
Expand All @@ -51,6 +53,7 @@ branches:
tracks-release-branches: false
is-release-branch: true
is-mainline: false
pre-release-weight: 30000
feature:
mode: ContinuousDelivery
tag: useBranchName
Expand All @@ -68,6 +71,7 @@ branches:
tracks-release-branches: false
is-release-branch: false
is-mainline: false
pre-release-weight: 30000
pull-request:
mode: ContinuousDelivery
tag: PullRequest
Expand All @@ -86,6 +90,7 @@ branches:
tracks-release-branches: false
is-release-branch: false
is-mainline: false
pre-release-weight: 30000
hotfix:
mode: ContinuousDelivery
tag: beta
Expand All @@ -100,6 +105,7 @@ branches:
tracks-release-branches: false
is-release-branch: false
is-mainline: false
pre-release-weight: 30000
support:
mode: ContinuousDelivery
tag: ''
Expand All @@ -112,6 +118,7 @@ branches:
tracks-release-branches: false
is-release-branch: false
is-mainline: true
pre-release-weight: 55000
ignore:
sha: []
commit-date-format: yyyy-MM-dd
3 changes: 3 additions & 0 deletions src/GitVersionCore.Tests/ExecuteCoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public void CacheFileExistsOnDisk()
PreReleaseTagWithDash: -test.19
PreReleaseLabel: test
PreReleaseNumber: 19
WeightedPreReleaseNumber: 19
BuildMetaData:
BuildMetaDataPadded:
FullBuildMetaData: Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f
Expand Down Expand Up @@ -161,6 +162,7 @@ public void ConfigChangeInvalidatesCache()
PreReleaseTagWithDash: -test.19
PreReleaseLabel: test
PreReleaseNumber: 19
WeightedPreReleaseNumber: 19
BuildMetaData:
BuildMetaDataPadded:
FullBuildMetaData: Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f
Expand Down Expand Up @@ -211,6 +213,7 @@ public void NoCacheBypassesCache()
PreReleaseTagWithDash: -test.19
PreReleaseLabel: test
PreReleaseNumber: 19
WeightedPreReleaseNumber: 19
BuildMetaData:
BuildMetaDataPadded:
FullBuildMetaData: Branch.feature/test.Sha.dd2a29aff0c948e1bdf3dabbe13e1576e70d5f9f
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using GitTools.Testing;
using GitTools.Testing;
using GitVersion;
using GitVersionCore.Tests;
using LibGit2Sharp;
Expand Down Expand Up @@ -551,6 +551,52 @@ public void FeatureFromReleaseBranch_ShouldNotResetCount()
}
}

[Test]
public void AssemblySemFileVerShouldBeWeightedByPreReleaseWeight()
{
var config = new Config
{
AssemblyFileVersioningFormat = "{Major}.{Minor}.{Patch}.{WeightedPreReleaseNumber}",
Branches =
{
{ "release", new BranchConfig
{
PreReleaseWeight = 1000
}
}
}
};
using (var fixture = new EmptyRepositoryFixture())
{
fixture.Repository.MakeATaggedCommit("1.0.3");
fixture.Repository.MakeCommits(5);
fixture.Repository.CreateBranch("release-2.0.0");
fixture.Checkout("release-2.0.0");
ConfigurationProvider.ApplyDefaultsTo(config);
var variables = fixture.GetVersion(config);
Assert.AreEqual(variables.AssemblySemFileVer, "2.0.0.1001");
}
}

[Test]
public void AssemblySemFileVerShouldBeWeightedByDefaultPreReleaseWeight()
{
var config = new Config
{
AssemblyFileVersioningFormat = "{Major}.{Minor}.{Patch}.{WeightedPreReleaseNumber}",
};
using (var fixture = new EmptyRepositoryFixture())
{
fixture.Repository.MakeATaggedCommit("1.0.3");
fixture.Repository.MakeCommits(5);
fixture.Repository.CreateBranch("release-2.0.0");
fixture.Checkout("release-2.0.0");
ConfigurationProvider.ApplyDefaultsTo(config);
var variables = fixture.GetVersion(config);
Assert.AreEqual(variables.AssemblySemFileVer, "2.0.0.30001");
}
}

/// <summary>
/// Create a feature branch from a release branch, and merge back, then delete it
/// </summary>
Expand Down Expand Up @@ -623,4 +669,4 @@ public void FeatureOnRelease_FeatureBranchNotDeleted()
fixture.AssertFullSemver(config, "4.5.0-beta.2");
}
}
}
}
2 changes: 1 addition & 1 deletion src/GitVersionCore.Tests/TestEffectiveConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public TestEffectiveConfiguration(
majorMessage, minorMessage, patchMessage, noBumpMessage,
commitMessageMode, legacySemVerPadding, buildMetaDataPadding, commitsSinceVersionSourcePadding,
versionFilters ?? Enumerable.Empty<IVersionFilter>(),
tracksReleaseBranches, isRelease, commitDateFormat)
tracksReleaseBranches, isRelease, commitDateFormat, 0)
{
}
}
Expand Down
17 changes: 16 additions & 1 deletion src/GitVersionCore.Tests/TestableVersionVariables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,22 @@

class TestableVersionVariables : VersionVariables
{
public TestableVersionVariables(string major = "", string minor = "", string patch = "", string buildMetaData = "", string buildMetaDataPadded = "", string fullBuildMetaData = "", string branchName = "", string sha = "", string shortSha = "", string majorMinorPatch = "", string semVer = "", string legacySemVer = "", string legacySemVerPadded = "", string fullSemVer = "", string assemblySemVer = "", string assemblySemFileVer = "", string preReleaseTag = "", string preReleaseTagWithDash = "", string preReleaseLabel = "", string preReleaseNumber = "", string informationalVersion = "", string commitDate = "", string nugetVersion = "", string nugetVersionV2 = "", string nugetPreReleaseTag = "", string nugetPreReleaseTagV2 = "", string commitsSinceVersionSource = "", string commitsSinceVersionSourcePadded = "") : base(major, minor, patch, buildMetaData, buildMetaDataPadded, fullBuildMetaData, branchName, sha, shortSha, majorMinorPatch, semVer, legacySemVer, legacySemVerPadded, fullSemVer, assemblySemVer, assemblySemFileVer, preReleaseTag, preReleaseTagWithDash, preReleaseLabel, preReleaseNumber, informationalVersion, commitDate, nugetVersion, nugetVersionV2, nugetPreReleaseTag, nugetPreReleaseTagV2, commitsSinceVersionSource, commitsSinceVersionSourcePadded)
public TestableVersionVariables(
string major = "", string minor = "", string patch = "", string buildMetaData = "",
string buildMetaDataPadded = "", string fullBuildMetaData = "", string branchName = "",
string sha = "", string shortSha = "", string majorMinorPatch = "", string semVer = "",
string legacySemVer = "", string legacySemVerPadded = "", string fullSemVer = "",
string assemblySemVer = "", string assemblySemFileVer = "", string preReleaseTag = "",
string preReleaseTagWithDash = "", string preReleaseLabel = "", string preReleaseNumber = "",
string weightedPreReleaseNumber = "", string informationalVersion = "", string commitDate = "",
string nugetVersion = "", string nugetVersionV2 = "", string nugetPreReleaseTag = "",
string nugetPreReleaseTagV2 = "", string commitsSinceVersionSource = "",
string commitsSinceVersionSourcePadded = "") : base(
major, minor, patch, buildMetaData, buildMetaDataPadded, fullBuildMetaData, branchName,
sha, shortSha, majorMinorPatch, semVer, legacySemVer, legacySemVerPadded, fullSemVer,
assemblySemVer, assemblySemFileVer, preReleaseTag, weightedPreReleaseNumber, preReleaseTagWithDash,
preReleaseLabel, preReleaseNumber, informationalVersion, commitDate, nugetVersion, nugetVersionV2,
nugetPreReleaseTag, nugetPreReleaseTagV2, commitsSinceVersionSource, commitsSinceVersionSourcePadded)
{
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/GitVersionCore/Configuration/BranchConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public BranchConfig(BranchConfig branchConfiguration)
Name = branchConfiguration.Name;
SourceBranches = branchConfiguration.SourceBranches;
IsSourceBranchFor = branchConfiguration.IsSourceBranchFor;
PreReleaseWeight = branchConfiguration.PreReleaseWeight;
}

[YamlMember(Alias = "mode")]
Expand Down Expand Up @@ -72,6 +73,9 @@ public BranchConfig(BranchConfig branchConfiguration)
[YamlMember(Alias = "is-mainline")]
public bool? IsMainline { get; set; }

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

/// <summary>
/// The name given to this configuration in the config file.
/// </summary>
Expand Down
14 changes: 14 additions & 0 deletions src/GitVersionCore/Configuration/ConfigurationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ public class ConfigurationProvider
public const string HotfixBranchKey = "hotfix";
public const string SupportBranchKey = "support";
public const string DevelopBranchKey = "develop";
public static Dictionary<string, int> DefaultPreReleaseWeight =
new Dictionary<string, int>
{
{ DevelopBranchRegex, 0 },
{ HotfixBranchRegex, 30000 },
{ ReleaseBranchRegex, 30000 },
{ FeatureBranchRegex, 30000 },
{ PullRequestRegex, 30000 },
{ SupportBranchRegex, 55000 },
{ MasterBranchRegex, 55000 }
};

private const IncrementStrategy DefaultIncrementStrategy = IncrementStrategy.Inherit;

Expand Down Expand Up @@ -231,6 +242,9 @@ public static void ApplyBranchDefaults(Config config,
branchConfig.TracksReleaseBranches = branchConfig.TracksReleaseBranches ?? tracksReleaseBranches;
branchConfig.IsReleaseBranch = branchConfig.IsReleaseBranch ?? isReleaseBranch;
branchConfig.IsMainline = branchConfig.IsMainline ?? isMainline;
int defaultPreReleaseNumber = 0;
DefaultPreReleaseWeight.TryGetValue(branchRegex, out defaultPreReleaseNumber);
branchConfig.PreReleaseWeight = branchConfig.PreReleaseWeight ?? defaultPreReleaseNumber;
}

static Config ReadConfig(string workingDirectory, IFileSystem fileSystem)
Expand Down
6 changes: 5 additions & 1 deletion src/GitVersionCore/EffectiveConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public EffectiveConfiguration(
IEnumerable<IVersionFilter> versionFilters,
bool tracksReleaseBranches,
bool isCurrentBranchRelease,
string commitDateFormat)
string commitDateFormat,
int preReleaseWeight)
{
AssemblyVersioningScheme = assemblyVersioningScheme;
AssemblyFileVersioningScheme = assemblyFileVersioningScheme;
Expand Down Expand Up @@ -61,6 +62,7 @@ public EffectiveConfiguration(
TracksReleaseBranches = tracksReleaseBranches;
IsCurrentBranchRelease = isCurrentBranchRelease;
CommitDateFormat = commitDateFormat;
PreReleaseWeight = preReleaseWeight;
}

public bool TracksReleaseBranches { get; private set; }
Expand Down Expand Up @@ -115,5 +117,7 @@ public EffectiveConfiguration(
public IEnumerable<IVersionFilter> VersionFilters { get; private set; }

public string CommitDateFormat { get; private set; }

public int PreReleaseWeight { get; private set; }
}
}
4 changes: 3 additions & 1 deletion src/GitVersionCore/GitVersionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ void CalculateEffectiveConfiguration()
var incrementStrategy = currentBranchConfig.Increment.Value;
var preventIncrementForMergedBranchVersion = currentBranchConfig.PreventIncrementOfMergedBranchVersion.Value;
var trackMergeTarget = currentBranchConfig.TrackMergeTarget.Value;
var preReleaseWeight = currentBranchConfig.PreReleaseWeight ?? 0;

var nextVersion = FullConfiguration.NextVersion;
var assemblyVersioningScheme = FullConfiguration.AssemblyVersioningScheme.Value;
Expand Down Expand Up @@ -148,7 +149,8 @@ void CalculateEffectiveConfiguration()
FullConfiguration.Ignore.ToFilters(),
currentBranchConfig.TracksReleaseBranches.Value,
currentBranchConfig.IsReleaseBranch.Value,
commitDateFormat);
commitDateFormat,
preReleaseWeight);
}

private static Branch GetTargetBranch(IRepository repository, string targetBranch)
Expand Down
Loading