Skip to content

Commit 3871bc2

Browse files
Casper Schmidt Wandahl-Liperarturcic
authored andcommitted
Implements regular expression for determining version label
- Remove magic string 'useBranchName' - Merge branch configuration property BranchPrefixToTrim with Regex/RegularExpression to support dynamic named groups in the regular expression
1 parent 6ffb6e1 commit 3871bc2

16 files changed

+46
-44
lines changed

BREAKING_CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
* A new configuration property with name `version-in-branch-pattern` has been introduced. This setting only applies on branches where the option `is-release-branch` is set to `true`. Please notice that the branch name needs to be defined after the version number by default (instead of `support/lts-2.0.0` please name the branch like `support/2.0.0-lts`).
2929
* The `is-release-branch` property of the `hotfix` branch setting has been changed from `false` to `true`. If present the hotfix number will be considered now by default.
3030
* In the GitHub and the Git Flow workflows the `label` property is by default set to an empty string on the `main` branch. This yields to a pre-release version on `main` with an empty tag. Instead of for instance `1.0.1+46` GitVersion generates the full semantic version `1.0.1-46` instead. This behavior can be changed to generate only stable versions (no pre-release version) with setting the label to `null` (Please keep in mind that the `label` property on root needs to be set to `null` as well, otherwise the fallback applies). This change is caused by issue #2347.
31+
* The `useBranchName` magic string has been removed. Instead use `{BranchName}` for `label`.
32+
* The `BranchPrefixToTrim` configuration property has been removed. `RegularExpression` is now used to capture named groups instead.
33+
* Default `RegularExpression` for feature branches is changed from `^features?[/-]` to `^features?[/-](?<BranchName>.+)` to support using `{BranchName}` out-of-the-box
34+
* Default `RegularExpression` for unknown branches is changed from `.*` to `(?<BranchName>.*)` to support using `{BranchName}` out-of-the-box
3135
3236
## v5.0.0
3337

schemas/6.0/GitVersion.configuration.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@
265265
}
266266
},
267267
"string": {
268-
"description": "The label to use for this branch. Can be \u0027useBranchName\u0027 to extract the label from the branch name. Use the value {BranchName} as a placeholder to insert the branch name.",
268+
"description": "The label to use for this branch. Use the value {BranchName} or similar as a placeholder to insert a named capture group from RegularExpression (fx. the branch name).",
269269
"type": "string"
270270
},
271271
"string1": {
@@ -557,4 +557,4 @@
557557
}
558558
}
559559
}
560-
}
560+
}

src/GitVersion.Core.Tests/Configuration/ConfigurationProviderTests.CanWriteOutEffectiveConfiguration.approved.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ branches:
5959
mode: ContinuousDelivery
6060
label: '{BranchName}'
6161
increment: Inherit
62-
regex: ^features?[/-]
62+
regex: ^features?[/-](?<BranchName>.+)
6363
source-branches:
6464
- develop
6565
- main
@@ -114,7 +114,7 @@ branches:
114114
mode: ContinuousDelivery
115115
label: '{BranchName}'
116116
increment: Inherit
117-
regex: .*
117+
regex: (?<BranchName>.*)
118118
source-branches:
119119
- main
120120
- develop
@@ -133,7 +133,7 @@ prevent-increment-of-merged-branch-version: false
133133
track-merge-target: false
134134
track-merge-message: true
135135
commit-message-incrementing: Enabled
136-
regex: ''
136+
regex: (?<BranchName>.+)
137137
source-branches: []
138138
is-source-branch-for: []
139139
tracks-release-branches: false

src/GitVersion.Core.Tests/Helpers/TestEffectiveConfiguration.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public TestEffectiveConfiguration(
1616
string tagPrefix = ConfigurationConstants.DefaultTagPrefix,
1717
string label = "ci",
1818
string? nextVersion = null,
19-
string branchPrefixToTrim = "",
19+
string regularExpression = "",
2020
bool preventIncrementOfMergedBranchVersion = false,
2121
string? labelNumberPattern = null,
2222
bool trackMergeTarget = false,
@@ -41,7 +41,7 @@ public TestEffectiveConfiguration(
4141
label,
4242
nextVersion,
4343
IncrementStrategy.Patch,
44-
branchPrefixToTrim,
44+
regularExpression,
4545
preventIncrementOfMergedBranchVersion,
4646
labelNumberPattern,
4747
trackMergeTarget,

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ public void CanUseBranchNameOffAReleaseBranch()
184184
}
185185

186186
[TestCase("alpha", "JIRA-123", "alpha")]
187-
[TestCase("useBranchName", "JIRA-123", "JIRA-123")]
188187
[TestCase($"alpha.{ConfigurationConstants.BranchNamePlaceholder}", "JIRA-123", "alpha.JIRA-123")]
189188
public void ShouldUseConfiguredTag(string tag, string featureName, string preReleaseTagName)
190189
{

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public void ShouldOnlyConsiderTagsMatchingOfCurrentBranch()
3434
public void CanTakeVersionFromReleaseBranch()
3535
{
3636
var configuration = GitFlowConfigurationBuilder.New
37-
.WithBranch("release", _ => _.WithLabel("{BranchName}"))
37+
.WithBranch("release", _ => _.WithLabel("{BranchName}").WithRegularExpression("(?<BranchName>.*)"))
3838
.Build();
3939

4040
using var fixture = new EmptyRepositoryFixture();
@@ -52,7 +52,7 @@ public void CanTakeVersionFromReleaseBranch()
5252
public void CanTakeVersionFromHotfixBranch()
5353
{
5454
var configuration = GitFlowConfigurationBuilder.New
55-
.WithBranch("hotfix", _ => _.WithLabel("{BranchName}"))
55+
.WithBranch("hotfix", _ => _.WithLabel("{BranchName}").WithRegularExpression("(?<BranchName>.*)"))
5656
.Build();
5757

5858
using var fixture = new EmptyRepositoryFixture();
@@ -83,7 +83,7 @@ public void ShouldNotGetVersionFromFeatureBranchIfNotMerged()
8383
{
8484
// * 1c08923 54 minutes ago (HEAD -> develop)
8585
// | * 03dd6d5 56 minutes ago (tag: 1.0.1-feature.1, feature)
86-
// |/
86+
// |/
8787
// * e2ff13b 58 minutes ago (tag: 1.0.0-unstable.0, main)
8888

8989
var configuration = GitFlowConfigurationBuilder.New
@@ -106,14 +106,13 @@ public void ShouldNotGetVersionFromFeatureBranchIfNotMerged()
106106
}
107107

108108
[TestCase("alpha", "JIRA-123", "alpha")]
109-
[TestCase("useBranchName", "JIRA-123", "JIRA-123")]
110109
[TestCase($"alpha.{ConfigurationConstants.BranchNamePlaceholder}", "JIRA-123", "alpha.JIRA-123")]
111110
public void LabelIsBranchNameForBranchesWithoutPrefixedBranchName(string label, string branchName, string preReleaseTagName)
112111
{
113112
var configuration = GitFlowConfigurationBuilder.New
114113
.WithBranch("other", builder => builder
115114
.WithIncrement(IncrementStrategy.Patch)
116-
.WithRegularExpression(".*")
115+
.WithRegularExpression("(?<BranchName>.*)")
117116
.WithSourceBranches()
118117
.WithLabel(label))
119118
.Build();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public void ProvidesVariablesInContinuousDeploymentModeWithTagNamePattern()
182182
}
183183

184184
[Test]
185-
public void ProvidesVariablesInContinuousDeploymentModeWithTagSetToUseBranchName()
185+
public void ProvidesVariablesInContinuousDeploymentModeWithTagSetToBranchName()
186186
{
187187
var semanticVersion = new SemanticVersion
188188
{

src/GitVersion.Core/Configuration/BranchConfiguration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ internal record BranchConfiguration : IBranchConfiguration
1111
public VersioningMode? VersioningMode { get; internal set; }
1212

1313
[JsonPropertyName("label")]
14-
[JsonPropertyDescription("The label to use for this branch. Can be 'useBranchName' to extract the label from the branch name. Use the value {BranchName} as a placeholder to insert the branch name.")]
14+
[JsonPropertyDescription("The label to use for this branch. Use the value {BranchName} or similar as a placeholder to insert a named capture group from RegularExpression (fx. the branch name).")]
1515
public string? Label { get; internal set; }
1616

1717
[JsonPropertyName("increment")]

src/GitVersion.Core/Configuration/ConfigurationConstants.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ internal static class ConfigurationConstants
1919
public const string MainBranchRegex = "^master$|^main$";
2020
public const string DevelopBranchRegex = "^dev(elop)?(ment)?$";
2121
public const string ReleaseBranchRegex = "^releases?[/-]";
22-
public const string FeatureBranchRegex = "^features?[/-]";
22+
public const string FeatureBranchRegex = "^features?[/-](?<BranchName>.+)";
2323
public const string PullRequestBranchRegex = @"^(pull|pull\-requests|pr)[/-]";
2424
public const string HotfixBranchRegex = "^hotfix(es)?[/-]";
2525
public const string SupportBranchRegex = "^support[/-]";
26-
public const string UnknownBranchRegex = ".*";
26+
public const string UnknownBranchRegex = "(?<BranchName>.*)";
2727
}

src/GitVersion.Core/Configuration/ConfigurationExtensions.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -68,32 +68,32 @@ public static bool IsReleaseBranch(this IGitVersionConfiguration configuration,
6868
configuration.NotNull();
6969

7070
var label = configuration.Label;
71-
if (label == "useBranchName")
71+
if (label is null)
7272
{
73-
label = ConfigurationConstants.BranchNamePlaceholder;
73+
return label;
7474
}
7575

76-
var value = branchNameOverride ?? branchName;
76+
var effectiveBranchName = branchNameOverride ?? branchName;
7777

78-
if (label?.Contains(ConfigurationConstants.BranchNamePlaceholder) == true)
78+
if (!configuration.RegularExpression.IsNullOrWhiteSpace() && !effectiveBranchName.IsNullOrEmpty())
7979
{
80-
if (!configuration.BranchPrefixToTrim.IsNullOrWhiteSpace())
80+
effectiveBranchName = effectiveBranchName.RegexReplace("[^a-zA-Z0-9-]", "-");
81+
var pattern = new Regex(configuration.RegularExpression, RegexOptions.IgnoreCase);
82+
var match = pattern.Match(effectiveBranchName);
83+
if (match.Success)
8184
{
82-
var branchNameTrimmed = value?.RegexReplace(
83-
configuration.BranchPrefixToTrim, string.Empty, RegexOptions.IgnoreCase
84-
);
85-
value = branchNameTrimmed.IsNullOrEmpty() ? value : branchNameTrimmed;
85+
// ReSharper disable once LoopCanBeConvertedToQuery
86+
foreach (var groupName in pattern.GetGroupNames())
87+
{
88+
label = label.Replace("{" + groupName + "}", match.Groups[groupName].Value);
89+
}
8690
}
87-
88-
value = value?.RegexReplace("[^a-zA-Z0-9-]", "-");
89-
90-
label = label.Replace(ConfigurationConstants.BranchNamePlaceholder, value);
9191
}
9292

9393
// Evaluate tag number pattern and append to prerelease tag, preserving build metadata
94-
if (!configuration.LabelNumberPattern.IsNullOrEmpty() && !value.IsNullOrEmpty() && label is not null)
94+
if (!configuration.LabelNumberPattern.IsNullOrEmpty() && !effectiveBranchName.IsNullOrEmpty())
9595
{
96-
var match = Regex.Match(value, configuration.LabelNumberPattern);
96+
var match = Regex.Match(effectiveBranchName, configuration.LabelNumberPattern);
9797
var numberGroup = match.Groups["number"];
9898
if (numberGroup.Success)
9999
{

0 commit comments

Comments
 (0)