From 3871bc23a4c74e28531b48e25383f54acdd5ad04 Mon Sep 17 00:00:00 2001 From: Casper Schmidt Wandahl-Liper Date: Tue, 12 Sep 2023 22:45:15 +0200 Subject: [PATCH 1/7] 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 --- BREAKING_CHANGES.md | 4 +++ schemas/6.0/GitVersion.configuration.json | 4 +-- ...riteOutEffectiveConfiguration.approved.txt | 6 ++-- .../Helpers/TestEffectiveConfiguration.cs | 4 +-- .../FeatureBranchScenarios.cs | 1 - .../IntegrationTests/OtherBranchScenarios.cs | 9 +++--- .../VariableProviderTests.cs | 2 +- .../Configuration/BranchConfiguration.cs | 2 +- .../Configuration/ConfigurationConstants.cs | 4 +-- .../Configuration/ConfigurationExtensions.cs | 30 +++++++++---------- .../Configuration/EffectiveConfiguration.cs | 8 ++--- .../GitFlowConfigurationBuilder.cs | 2 +- .../GitHubFlowConfigurationBuilder.cs | 2 +- .../SupportedWorkflows/GitFlow/v1.yml | 4 +-- .../SupportedWorkflows/GitHubFlow/v1.yml | 4 +-- src/GitVersion.Core/PublicAPI.Unshipped.txt | 4 +-- 16 files changed, 46 insertions(+), 44 deletions(-) diff --git a/BREAKING_CHANGES.md b/BREAKING_CHANGES.md index 70422b9f5f..8b7d869eab 100644 --- a/BREAKING_CHANGES.md +++ b/BREAKING_CHANGES.md @@ -28,6 +28,10 @@ * 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`). * 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. * 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. +* The `useBranchName` magic string has been removed. Instead use `{BranchName}` for `label`. +* The `BranchPrefixToTrim` configuration property has been removed. `RegularExpression` is now used to capture named groups instead. + * Default `RegularExpression` for feature branches is changed from `^features?[/-]` to `^features?[/-](?.+)` to support using `{BranchName}` out-of-the-box + * Default `RegularExpression` for unknown branches is changed from `.*` to `(?.*)` to support using `{BranchName}` out-of-the-box ## v5.0.0 diff --git a/schemas/6.0/GitVersion.configuration.json b/schemas/6.0/GitVersion.configuration.json index 85a8f8d99a..65ee93e74b 100644 --- a/schemas/6.0/GitVersion.configuration.json +++ b/schemas/6.0/GitVersion.configuration.json @@ -265,7 +265,7 @@ } }, "string": { - "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.", + "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).", "type": "string" }, "string1": { @@ -557,4 +557,4 @@ } } } -} \ No newline at end of file +} diff --git a/src/GitVersion.Core.Tests/Configuration/ConfigurationProviderTests.CanWriteOutEffectiveConfiguration.approved.txt b/src/GitVersion.Core.Tests/Configuration/ConfigurationProviderTests.CanWriteOutEffectiveConfiguration.approved.txt index b6527c90b1..1d79bc9575 100644 --- a/src/GitVersion.Core.Tests/Configuration/ConfigurationProviderTests.CanWriteOutEffectiveConfiguration.approved.txt +++ b/src/GitVersion.Core.Tests/Configuration/ConfigurationProviderTests.CanWriteOutEffectiveConfiguration.approved.txt @@ -59,7 +59,7 @@ branches: mode: ContinuousDelivery label: '{BranchName}' increment: Inherit - regex: ^features?[/-] + regex: ^features?[/-](?.+) source-branches: - develop - main @@ -114,7 +114,7 @@ branches: mode: ContinuousDelivery label: '{BranchName}' increment: Inherit - regex: .* + regex: (?.*) source-branches: - main - develop @@ -133,7 +133,7 @@ prevent-increment-of-merged-branch-version: false track-merge-target: false track-merge-message: true commit-message-incrementing: Enabled -regex: '' +regex: (?.+) source-branches: [] is-source-branch-for: [] tracks-release-branches: false diff --git a/src/GitVersion.Core.Tests/Helpers/TestEffectiveConfiguration.cs b/src/GitVersion.Core.Tests/Helpers/TestEffectiveConfiguration.cs index 8044845739..da465ac96c 100644 --- a/src/GitVersion.Core.Tests/Helpers/TestEffectiveConfiguration.cs +++ b/src/GitVersion.Core.Tests/Helpers/TestEffectiveConfiguration.cs @@ -16,7 +16,7 @@ public TestEffectiveConfiguration( string tagPrefix = ConfigurationConstants.DefaultTagPrefix, string label = "ci", string? nextVersion = null, - string branchPrefixToTrim = "", + string regularExpression = "", bool preventIncrementOfMergedBranchVersion = false, string? labelNumberPattern = null, bool trackMergeTarget = false, @@ -41,7 +41,7 @@ public TestEffectiveConfiguration( label, nextVersion, IncrementStrategy.Patch, - branchPrefixToTrim, + regularExpression, preventIncrementOfMergedBranchVersion, labelNumberPattern, trackMergeTarget, diff --git a/src/GitVersion.Core.Tests/IntegrationTests/FeatureBranchScenarios.cs b/src/GitVersion.Core.Tests/IntegrationTests/FeatureBranchScenarios.cs index 59ca1126d6..3139b10438 100644 --- a/src/GitVersion.Core.Tests/IntegrationTests/FeatureBranchScenarios.cs +++ b/src/GitVersion.Core.Tests/IntegrationTests/FeatureBranchScenarios.cs @@ -184,7 +184,6 @@ public void CanUseBranchNameOffAReleaseBranch() } [TestCase("alpha", "JIRA-123", "alpha")] - [TestCase("useBranchName", "JIRA-123", "JIRA-123")] [TestCase($"alpha.{ConfigurationConstants.BranchNamePlaceholder}", "JIRA-123", "alpha.JIRA-123")] public void ShouldUseConfiguredTag(string tag, string featureName, string preReleaseTagName) { diff --git a/src/GitVersion.Core.Tests/IntegrationTests/OtherBranchScenarios.cs b/src/GitVersion.Core.Tests/IntegrationTests/OtherBranchScenarios.cs index d6a6943d40..1fd1446dd7 100644 --- a/src/GitVersion.Core.Tests/IntegrationTests/OtherBranchScenarios.cs +++ b/src/GitVersion.Core.Tests/IntegrationTests/OtherBranchScenarios.cs @@ -34,7 +34,7 @@ public void ShouldOnlyConsiderTagsMatchingOfCurrentBranch() public void CanTakeVersionFromReleaseBranch() { var configuration = GitFlowConfigurationBuilder.New - .WithBranch("release", _ => _.WithLabel("{BranchName}")) + .WithBranch("release", _ => _.WithLabel("{BranchName}").WithRegularExpression("(?.*)")) .Build(); using var fixture = new EmptyRepositoryFixture(); @@ -52,7 +52,7 @@ public void CanTakeVersionFromReleaseBranch() public void CanTakeVersionFromHotfixBranch() { var configuration = GitFlowConfigurationBuilder.New - .WithBranch("hotfix", _ => _.WithLabel("{BranchName}")) + .WithBranch("hotfix", _ => _.WithLabel("{BranchName}").WithRegularExpression("(?.*)")) .Build(); using var fixture = new EmptyRepositoryFixture(); @@ -83,7 +83,7 @@ public void ShouldNotGetVersionFromFeatureBranchIfNotMerged() { // * 1c08923 54 minutes ago (HEAD -> develop) // | * 03dd6d5 56 minutes ago (tag: 1.0.1-feature.1, feature) - // |/ + // |/ // * e2ff13b 58 minutes ago (tag: 1.0.0-unstable.0, main) var configuration = GitFlowConfigurationBuilder.New @@ -106,14 +106,13 @@ public void ShouldNotGetVersionFromFeatureBranchIfNotMerged() } [TestCase("alpha", "JIRA-123", "alpha")] - [TestCase("useBranchName", "JIRA-123", "JIRA-123")] [TestCase($"alpha.{ConfigurationConstants.BranchNamePlaceholder}", "JIRA-123", "alpha.JIRA-123")] public void LabelIsBranchNameForBranchesWithoutPrefixedBranchName(string label, string branchName, string preReleaseTagName) { var configuration = GitFlowConfigurationBuilder.New .WithBranch("other", builder => builder .WithIncrement(IncrementStrategy.Patch) - .WithRegularExpression(".*") + .WithRegularExpression("(?.*)") .WithSourceBranches() .WithLabel(label)) .Build(); diff --git a/src/GitVersion.Core.Tests/VersionCalculation/VariableProviderTests.cs b/src/GitVersion.Core.Tests/VersionCalculation/VariableProviderTests.cs index 325bf51de3..78369450df 100644 --- a/src/GitVersion.Core.Tests/VersionCalculation/VariableProviderTests.cs +++ b/src/GitVersion.Core.Tests/VersionCalculation/VariableProviderTests.cs @@ -182,7 +182,7 @@ public void ProvidesVariablesInContinuousDeploymentModeWithTagNamePattern() } [Test] - public void ProvidesVariablesInContinuousDeploymentModeWithTagSetToUseBranchName() + public void ProvidesVariablesInContinuousDeploymentModeWithTagSetToBranchName() { var semanticVersion = new SemanticVersion { diff --git a/src/GitVersion.Core/Configuration/BranchConfiguration.cs b/src/GitVersion.Core/Configuration/BranchConfiguration.cs index 65953f1a34..451f647cc4 100644 --- a/src/GitVersion.Core/Configuration/BranchConfiguration.cs +++ b/src/GitVersion.Core/Configuration/BranchConfiguration.cs @@ -11,7 +11,7 @@ internal record BranchConfiguration : IBranchConfiguration public VersioningMode? VersioningMode { get; internal set; } [JsonPropertyName("label")] - [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.")] + [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).")] public string? Label { get; internal set; } [JsonPropertyName("increment")] diff --git a/src/GitVersion.Core/Configuration/ConfigurationConstants.cs b/src/GitVersion.Core/Configuration/ConfigurationConstants.cs index 63b505534d..8d238b478d 100644 --- a/src/GitVersion.Core/Configuration/ConfigurationConstants.cs +++ b/src/GitVersion.Core/Configuration/ConfigurationConstants.cs @@ -19,9 +19,9 @@ internal static class ConfigurationConstants public const string MainBranchRegex = "^master$|^main$"; public const string DevelopBranchRegex = "^dev(elop)?(ment)?$"; public const string ReleaseBranchRegex = "^releases?[/-]"; - public const string FeatureBranchRegex = "^features?[/-]"; + public const string FeatureBranchRegex = "^features?[/-](?.+)"; public const string PullRequestBranchRegex = @"^(pull|pull\-requests|pr)[/-]"; public const string HotfixBranchRegex = "^hotfix(es)?[/-]"; public const string SupportBranchRegex = "^support[/-]"; - public const string UnknownBranchRegex = ".*"; + public const string UnknownBranchRegex = "(?.*)"; } diff --git a/src/GitVersion.Core/Configuration/ConfigurationExtensions.cs b/src/GitVersion.Core/Configuration/ConfigurationExtensions.cs index e82deb76eb..fe1db1ee8e 100644 --- a/src/GitVersion.Core/Configuration/ConfigurationExtensions.cs +++ b/src/GitVersion.Core/Configuration/ConfigurationExtensions.cs @@ -68,32 +68,32 @@ public static bool IsReleaseBranch(this IGitVersionConfiguration configuration, configuration.NotNull(); var label = configuration.Label; - if (label == "useBranchName") + if (label is null) { - label = ConfigurationConstants.BranchNamePlaceholder; + return label; } - var value = branchNameOverride ?? branchName; + var effectiveBranchName = branchNameOverride ?? branchName; - if (label?.Contains(ConfigurationConstants.BranchNamePlaceholder) == true) + if (!configuration.RegularExpression.IsNullOrWhiteSpace() && !effectiveBranchName.IsNullOrEmpty()) { - if (!configuration.BranchPrefixToTrim.IsNullOrWhiteSpace()) + effectiveBranchName = effectiveBranchName.RegexReplace("[^a-zA-Z0-9-]", "-"); + var pattern = new Regex(configuration.RegularExpression, RegexOptions.IgnoreCase); + var match = pattern.Match(effectiveBranchName); + if (match.Success) { - var branchNameTrimmed = value?.RegexReplace( - configuration.BranchPrefixToTrim, string.Empty, RegexOptions.IgnoreCase - ); - value = branchNameTrimmed.IsNullOrEmpty() ? value : branchNameTrimmed; + // ReSharper disable once LoopCanBeConvertedToQuery + foreach (var groupName in pattern.GetGroupNames()) + { + label = label.Replace("{" + groupName + "}", match.Groups[groupName].Value); + } } - - value = value?.RegexReplace("[^a-zA-Z0-9-]", "-"); - - label = label.Replace(ConfigurationConstants.BranchNamePlaceholder, value); } // Evaluate tag number pattern and append to prerelease tag, preserving build metadata - if (!configuration.LabelNumberPattern.IsNullOrEmpty() && !value.IsNullOrEmpty() && label is not null) + if (!configuration.LabelNumberPattern.IsNullOrEmpty() && !effectiveBranchName.IsNullOrEmpty()) { - var match = Regex.Match(value, configuration.LabelNumberPattern); + var match = Regex.Match(effectiveBranchName, configuration.LabelNumberPattern); var numberGroup = match.Groups["number"]; if (numberGroup.Success) { diff --git a/src/GitVersion.Core/Configuration/EffectiveConfiguration.cs b/src/GitVersion.Core/Configuration/EffectiveConfiguration.cs index 1b43340e22..acf18d36dd 100644 --- a/src/GitVersion.Core/Configuration/EffectiveConfiguration.cs +++ b/src/GitVersion.Core/Configuration/EffectiveConfiguration.cs @@ -44,7 +44,7 @@ public EffectiveConfiguration(IGitVersionConfiguration configuration, IBranchCon Label = branchConfiguration.Label; NextVersion = configuration.NextVersion; Increment = branchConfiguration.Increment; - BranchPrefixToTrim = branchConfiguration.RegularExpression; + RegularExpression = branchConfiguration.RegularExpression; PreventIncrementOfMergedBranchVersion = branchConfiguration.PreventIncrementOfMergedBranchVersion ?? false; LabelNumberPattern = branchConfiguration.LabelNumberPattern; TrackMergeTarget = branchConfiguration.TrackMergeTarget ?? false; @@ -75,7 +75,7 @@ protected EffectiveConfiguration(AssemblyVersioningScheme assemblyVersioningSche string label, string? nextVersion, IncrementStrategy increment, - string? branchPrefixToTrim, + string? regularExpression, bool preventIncrementOfMergedBranchVersion, string? labelNumberPattern, bool trackMergeTarget, @@ -104,7 +104,7 @@ protected EffectiveConfiguration(AssemblyVersioningScheme assemblyVersioningSche Label = label; NextVersion = nextVersion; Increment = increment; - BranchPrefixToTrim = branchPrefixToTrim; + RegularExpression = regularExpression; PreventIncrementOfMergedBranchVersion = preventIncrementOfMergedBranchVersion; LabelNumberPattern = labelNumberPattern; TrackMergeTarget = trackMergeTarget; @@ -150,7 +150,7 @@ protected EffectiveConfiguration(AssemblyVersioningScheme assemblyVersioningSche public IncrementStrategy Increment { get; } - public string? BranchPrefixToTrim { get; } + public string? RegularExpression { get; } public bool PreventIncrementOfMergedBranchVersion { get; } diff --git a/src/GitVersion.Core/Configuration/GitFlowConfigurationBuilder.cs b/src/GitVersion.Core/Configuration/GitFlowConfigurationBuilder.cs index 5338e45ec1..4797e9329b 100644 --- a/src/GitVersion.Core/Configuration/GitFlowConfigurationBuilder.cs +++ b/src/GitVersion.Core/Configuration/GitFlowConfigurationBuilder.cs @@ -24,7 +24,7 @@ private GitFlowConfigurationBuilder() TagPreReleaseWeight = 60000, UpdateBuildNumber = true, VersioningMode = VersioningMode.ContinuousDeployment, - RegularExpression = string.Empty, + RegularExpression = "(?.+)", Label = ConfigurationConstants.BranchNamePlaceholder, Increment = IncrementStrategy.Inherit, CommitMessageIncrementing = CommitMessageIncrementMode.Enabled, diff --git a/src/GitVersion.Core/Configuration/GitHubFlowConfigurationBuilder.cs b/src/GitVersion.Core/Configuration/GitHubFlowConfigurationBuilder.cs index a56a638e5c..faa4cdc569 100644 --- a/src/GitVersion.Core/Configuration/GitHubFlowConfigurationBuilder.cs +++ b/src/GitVersion.Core/Configuration/GitHubFlowConfigurationBuilder.cs @@ -24,7 +24,7 @@ private GitHubFlowConfigurationBuilder() TagPreReleaseWeight = 60000, UpdateBuildNumber = true, VersioningMode = VersioningMode.ContinuousDeployment, - RegularExpression = string.Empty, + RegularExpression = "(?.+)", Label = ConfigurationConstants.BranchNamePlaceholder, Increment = IncrementStrategy.Inherit, CommitMessageIncrementing = CommitMessageIncrementMode.Enabled, diff --git a/src/GitVersion.Core/Configuration/SupportedWorkflows/GitFlow/v1.yml b/src/GitVersion.Core/Configuration/SupportedWorkflows/GitFlow/v1.yml index 4d417a05d6..f918b5ef13 100644 --- a/src/GitVersion.Core/Configuration/SupportedWorkflows/GitFlow/v1.yml +++ b/src/GitVersion.Core/Configuration/SupportedWorkflows/GitFlow/v1.yml @@ -55,7 +55,7 @@ branches: mode: ContinuousDelivery label: '{BranchName}' increment: Inherit - regex: ^features?[/-] + regex: ^features?[/-](?.+) source-branches: - develop - main @@ -105,7 +105,7 @@ branches: mode: ContinuousDelivery label: '{BranchName}' increment: Inherit - regex: .* + regex: (?.*) source-branches: - main - develop diff --git a/src/GitVersion.Core/Configuration/SupportedWorkflows/GitHubFlow/v1.yml b/src/GitVersion.Core/Configuration/SupportedWorkflows/GitHubFlow/v1.yml index f5c7e83848..81d62b7037 100644 --- a/src/GitVersion.Core/Configuration/SupportedWorkflows/GitHubFlow/v1.yml +++ b/src/GitVersion.Core/Configuration/SupportedWorkflows/GitHubFlow/v1.yml @@ -40,7 +40,7 @@ branches: mode: ContinuousDelivery label: '{BranchName}' increment: Inherit - regex: ^features?[/-] + regex: ^features?[/-](?.+) source-branches: - main - release @@ -61,7 +61,7 @@ branches: mode: ContinuousDelivery label: '{BranchName}' increment: Inherit - regex: .* + regex: (?.*) source-branches: - main - release diff --git a/src/GitVersion.Core/PublicAPI.Unshipped.txt b/src/GitVersion.Core/PublicAPI.Unshipped.txt index 7c0d531d89..77b6fb53dd 100644 --- a/src/GitVersion.Core/PublicAPI.Unshipped.txt +++ b/src/GitVersion.Core/PublicAPI.Unshipped.txt @@ -108,11 +108,11 @@ GitVersion.Configuration.EffectiveConfiguration.AssemblyFileVersioningScheme.get GitVersion.Configuration.EffectiveConfiguration.AssemblyInformationalFormat.get -> string? GitVersion.Configuration.EffectiveConfiguration.AssemblyVersioningFormat.get -> string? GitVersion.Configuration.EffectiveConfiguration.AssemblyVersioningScheme.get -> GitVersion.Extensions.AssemblyVersioningScheme -GitVersion.Configuration.EffectiveConfiguration.BranchPrefixToTrim.get -> string? +GitVersion.Configuration.EffectiveConfiguration.RegularExpression.get -> string? GitVersion.Configuration.EffectiveConfiguration.CommitDateFormat.get -> string? GitVersion.Configuration.EffectiveConfiguration.CommitMessageIncrementing.get -> GitVersion.VersionCalculation.CommitMessageIncrementMode GitVersion.Configuration.EffectiveConfiguration.EffectiveConfiguration(GitVersion.Configuration.IGitVersionConfiguration! configuration, GitVersion.Configuration.IBranchConfiguration! branchConfiguration) -> void -GitVersion.Configuration.EffectiveConfiguration.EffectiveConfiguration(GitVersion.Extensions.AssemblyVersioningScheme assemblyVersioningScheme, GitVersion.Extensions.AssemblyFileVersioningScheme assemblyFileVersioningScheme, string? assemblyInformationalFormat, string? assemblyVersioningFormat, string? assemblyFileVersioningFormat, GitVersion.VersionCalculation.VersioningMode versioningMode, string? tagPrefix, string! label, string? nextVersion, GitVersion.IncrementStrategy increment, string? branchPrefixToTrim, bool preventIncrementOfMergedBranchVersion, string? labelNumberPattern, bool trackMergeTarget, string? majorVersionBumpMessage, string? minorVersionBumpMessage, string? patchVersionBumpMessage, string? noBumpMessage, GitVersion.VersionCalculation.CommitMessageIncrementMode commitMessageIncrementing, System.Collections.Generic.IEnumerable! versionFilters, bool tracksReleaseBranches, bool isReleaseBranch, bool isMainline, string? commitDateFormat, bool updateBuildNumber, GitVersion.SemanticVersionFormat semanticVersionFormat, int preReleaseWeight, int tagPreReleaseWeight) -> void +GitVersion.Configuration.EffectiveConfiguration.EffectiveConfiguration(GitVersion.Extensions.AssemblyVersioningScheme assemblyVersioningScheme, GitVersion.Extensions.AssemblyFileVersioningScheme assemblyFileVersioningScheme, string? assemblyInformationalFormat, string? assemblyVersioningFormat, string? assemblyFileVersioningFormat, GitVersion.VersionCalculation.VersioningMode versioningMode, string? tagPrefix, string! label, string? nextVersion, GitVersion.IncrementStrategy increment, string? regularExpression, bool preventIncrementOfMergedBranchVersion, string? labelNumberPattern, bool trackMergeTarget, string? majorVersionBumpMessage, string? minorVersionBumpMessage, string? patchVersionBumpMessage, string? noBumpMessage, GitVersion.VersionCalculation.CommitMessageIncrementMode commitMessageIncrementing, System.Collections.Generic.IEnumerable! versionFilters, bool tracksReleaseBranches, bool isReleaseBranch, bool isMainline, string? commitDateFormat, bool updateBuildNumber, GitVersion.SemanticVersionFormat semanticVersionFormat, int preReleaseWeight, int tagPreReleaseWeight) -> void GitVersion.Configuration.EffectiveConfiguration.Increment.get -> GitVersion.IncrementStrategy GitVersion.Configuration.EffectiveConfiguration.IsMainline.get -> bool GitVersion.Configuration.EffectiveConfiguration.IsReleaseBranch.get -> bool From ace31c11762cb5719eb365edbea04d0d26507773 Mon Sep 17 00:00:00 2001 From: Casper Schmidt Wandahl-Liper Date: Thu, 21 Sep 2023 08:52:33 +0200 Subject: [PATCH 2/7] Revert default regular expression for GitHub and GitFlow configuration builders --- ...ProviderTests.CanWriteOutEffectiveConfiguration.approved.txt | 2 +- .../Configuration/GitFlowConfigurationBuilder.cs | 2 +- .../Configuration/GitHubFlowConfigurationBuilder.cs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/GitVersion.Core.Tests/Configuration/ConfigurationProviderTests.CanWriteOutEffectiveConfiguration.approved.txt b/src/GitVersion.Core.Tests/Configuration/ConfigurationProviderTests.CanWriteOutEffectiveConfiguration.approved.txt index 1d79bc9575..45a075aa8b 100644 --- a/src/GitVersion.Core.Tests/Configuration/ConfigurationProviderTests.CanWriteOutEffectiveConfiguration.approved.txt +++ b/src/GitVersion.Core.Tests/Configuration/ConfigurationProviderTests.CanWriteOutEffectiveConfiguration.approved.txt @@ -133,7 +133,7 @@ prevent-increment-of-merged-branch-version: false track-merge-target: false track-merge-message: true commit-message-incrementing: Enabled -regex: (?.+) +regex: '' source-branches: [] is-source-branch-for: [] tracks-release-branches: false diff --git a/src/GitVersion.Core/Configuration/GitFlowConfigurationBuilder.cs b/src/GitVersion.Core/Configuration/GitFlowConfigurationBuilder.cs index 4797e9329b..5338e45ec1 100644 --- a/src/GitVersion.Core/Configuration/GitFlowConfigurationBuilder.cs +++ b/src/GitVersion.Core/Configuration/GitFlowConfigurationBuilder.cs @@ -24,7 +24,7 @@ private GitFlowConfigurationBuilder() TagPreReleaseWeight = 60000, UpdateBuildNumber = true, VersioningMode = VersioningMode.ContinuousDeployment, - RegularExpression = "(?.+)", + RegularExpression = string.Empty, Label = ConfigurationConstants.BranchNamePlaceholder, Increment = IncrementStrategy.Inherit, CommitMessageIncrementing = CommitMessageIncrementMode.Enabled, diff --git a/src/GitVersion.Core/Configuration/GitHubFlowConfigurationBuilder.cs b/src/GitVersion.Core/Configuration/GitHubFlowConfigurationBuilder.cs index faa4cdc569..a56a638e5c 100644 --- a/src/GitVersion.Core/Configuration/GitHubFlowConfigurationBuilder.cs +++ b/src/GitVersion.Core/Configuration/GitHubFlowConfigurationBuilder.cs @@ -24,7 +24,7 @@ private GitHubFlowConfigurationBuilder() TagPreReleaseWeight = 60000, UpdateBuildNumber = true, VersioningMode = VersioningMode.ContinuousDeployment, - RegularExpression = "(?.+)", + RegularExpression = string.Empty, Label = ConfigurationConstants.BranchNamePlaceholder, Increment = IncrementStrategy.Inherit, CommitMessageIncrementing = CommitMessageIncrementMode.Enabled, From 8cc191daaac47039c4df85c1fc3ee1dc19057015 Mon Sep 17 00:00:00 2001 From: Casper Schmidt Wandahl-Liper Date: Thu, 21 Sep 2023 08:52:55 +0200 Subject: [PATCH 3/7] Add test case for labelling using named group --- .../ConfigurationExtensionsTests.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/GitVersion.Core.Tests/Configuration/ConfigurationExtensionsTests.cs b/src/GitVersion.Core.Tests/Configuration/ConfigurationExtensionsTests.cs index 817753962f..07351e0978 100644 --- a/src/GitVersion.Core.Tests/Configuration/ConfigurationExtensionsTests.cs +++ b/src/GitVersion.Core.Tests/Configuration/ConfigurationExtensionsTests.cs @@ -57,4 +57,20 @@ public void EnsureIsReleaseBranchWithReferenceNameWorksAsExpected(string branchN actual.IsTag.ShouldBe(expectedIsTag); isReleaseBranch.ShouldBe(expectedIsReleaseBranch); } + + [TestCase("feature/sc-1000/Description", "^features?[/-](?.+)", "{BranchName}", "sc-1000-Description")] + [TestCase("feature/sc-1000/Description", "^features?[/-](?sc-\\d+)[-/].+", "{StoryNo}", "sc-1000")] + public void EnsureGetBranchSpecificLabelWorksAsExpected(string branchName, string regularExpression, string label, string expectedLabel) + { + var configuration = GitFlowConfigurationBuilder.New + .WithoutBranches() + .WithBranch(branchName, builder => builder + .WithLabel(label) + .WithRegularExpression(regularExpression)) + .Build(); + + var effectiveConfiguration = configuration.GetEffectiveConfiguration(ReferenceName.FromBranchName(branchName)); + var actual = effectiveConfiguration.GetBranchSpecificLabel(ReferenceName.FromBranchName(branchName), null); + actual.ShouldBe(expectedLabel); + } } From a1b45fa90be1aed8efd294893e0afaa5b58d0576 Mon Sep 17 00:00:00 2001 From: Casper Schmidt Wandahl-Liper Date: Thu, 21 Sep 2023 08:58:39 +0200 Subject: [PATCH 4/7] Fix regular expression for unknown branches --- src/GitVersion.Core/Configuration/ConfigurationConstants.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/GitVersion.Core/Configuration/ConfigurationConstants.cs b/src/GitVersion.Core/Configuration/ConfigurationConstants.cs index 8d238b478d..80593a9b74 100644 --- a/src/GitVersion.Core/Configuration/ConfigurationConstants.cs +++ b/src/GitVersion.Core/Configuration/ConfigurationConstants.cs @@ -23,5 +23,5 @@ internal static class ConfigurationConstants public const string PullRequestBranchRegex = @"^(pull|pull\-requests|pr)[/-]"; public const string HotfixBranchRegex = "^hotfix(es)?[/-]"; public const string SupportBranchRegex = "^support[/-]"; - public const string UnknownBranchRegex = "(?.*)"; + public const string UnknownBranchRegex = "(?.+)"; } From 1df77ed442b6d43b3bf68038d85113ecb3f14766 Mon Sep 17 00:00:00 2001 From: Casper Schmidt Wandahl-Liper Date: Thu, 21 Sep 2023 14:54:36 +0200 Subject: [PATCH 5/7] Update docs --- docs/input/docs/reference/configuration.md | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/docs/input/docs/reference/configuration.md b/docs/input/docs/reference/configuration.md index 67aac71547..1ee8f81eb6 100644 --- a/docs/input/docs/reference/configuration.md +++ b/docs/input/docs/reference/configuration.md @@ -484,7 +484,9 @@ values, but here they are if you need to: ### regex This is the regex which is used to match the current branch to the correct -branch configuration. +branch configuration. + +[Named groups](https://learn.microsoft.com/en-us/dotnet/standard/base-types/grouping-constructs-in-regular-expressions#named-matched-subexpressions) can be used to dynamically label pre-releases based on the branch name, or parts of it. See [Label](#label) for more details and examples. ### source-branches @@ -572,11 +574,13 @@ Same as for the [global configuration, explained above](#mode). ### label -The pre release label to use for this branch. Use the value `useBranchName` to use -the branch name instead. For example `feature/foo` would become a pre-release -label of `foo` with this value. Use the value `{BranchName}` as a placeholder to -insert the branch name. For example `feature/foo` would become a pre-release label -of `alpha.foo` with the value of `alpha.{BranchName}`. +The pre-release label to use for this branch. Use the value `{BranchName}` as a placeholder to +insert the value of the named group `BranchName` from the [regular expression](#regex). + +For example: branch `feature/foo` would become a pre-release label +of `alpha.foo` with `label: 'alpha.{BranchName}'` and `regex: '^features?[/-](?.+)'`. + +Another example: branch `features/sc-12345/some-description` would become a pre-release label of `sc-12345` with `label: '{StoryNo}'` and `regex: '^features?[/-](?sc-\d+)[-/].+'`. **Note:** To clear a default use an empty string: `label: ''` From 22f90558ae2980d0b45df2b89137a87828268581 Mon Sep 17 00:00:00 2001 From: Casper Schmidt Wandahl-Liper Date: Thu, 21 Sep 2023 20:22:03 +0200 Subject: [PATCH 6/7] Fixes failing unit test after updating Regex --- ...ProviderTests.CanWriteOutEffectiveConfiguration.approved.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/GitVersion.Core.Tests/Configuration/ConfigurationProviderTests.CanWriteOutEffectiveConfiguration.approved.txt b/src/GitVersion.Core.Tests/Configuration/ConfigurationProviderTests.CanWriteOutEffectiveConfiguration.approved.txt index 45a075aa8b..9e2281f79f 100644 --- a/src/GitVersion.Core.Tests/Configuration/ConfigurationProviderTests.CanWriteOutEffectiveConfiguration.approved.txt +++ b/src/GitVersion.Core.Tests/Configuration/ConfigurationProviderTests.CanWriteOutEffectiveConfiguration.approved.txt @@ -114,7 +114,7 @@ branches: mode: ContinuousDelivery label: '{BranchName}' increment: Inherit - regex: (?.*) + regex: (?.+) source-branches: - main - develop From 9ede03450c328878e635c79e711e7e874c283e9d Mon Sep 17 00:00:00 2001 From: Casper Schmidt Wandahl-Liper Date: Thu, 21 Sep 2023 21:19:23 +0200 Subject: [PATCH 7/7] Add integration test and add underscore as a valid character in effective branch name --- .../IntegrationTests/FeatureBranchScenarios.cs | 13 ++++++++----- .../Configuration/ConfigurationExtensions.cs | 2 +- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/GitVersion.Core.Tests/IntegrationTests/FeatureBranchScenarios.cs b/src/GitVersion.Core.Tests/IntegrationTests/FeatureBranchScenarios.cs index 3139b10438..a21e22a721 100644 --- a/src/GitVersion.Core.Tests/IntegrationTests/FeatureBranchScenarios.cs +++ b/src/GitVersion.Core.Tests/IntegrationTests/FeatureBranchScenarios.cs @@ -183,12 +183,15 @@ public void CanUseBranchNameOffAReleaseBranch() fixture.AssertFullSemver("0.3.0-PROJ-1.1+3", configuration); } - [TestCase("alpha", "JIRA-123", "alpha")] - [TestCase($"alpha.{ConfigurationConstants.BranchNamePlaceholder}", "JIRA-123", "alpha.JIRA-123")] - public void ShouldUseConfiguredTag(string tag, string featureName, string preReleaseTagName) + [TestCase("alpha", "JIRA-123", "^features?[/-](?.+)", "alpha")] + [TestCase($"alpha.{ConfigurationConstants.BranchNamePlaceholder}", "JIRA-123", "^features?[/-](?.+)", "alpha.JIRA-123")] + [TestCase("{BranchName}-of-task-number-{TaskNumber}", "4711_this-is-a-feature", "^features?[/-](?\\d+)_(?.+)", "this-is-a-feature-of-task-number-4711")] + public void ShouldUseConfiguredLabel(string label, string featureName, string regularExpression, string preReleaseLabelName) { var configuration = GitFlowConfigurationBuilder.New - .WithBranch("feature", builder => builder.WithLabel(tag)) + .WithBranch("feature", builder => builder + .WithLabel(label) + .WithRegularExpression(regularExpression)) .Build(); using var fixture = new EmptyRepositoryFixture(); @@ -197,7 +200,7 @@ public void ShouldUseConfiguredTag(string tag, string featureName, string preRel fixture.BranchTo(featureBranchName); fixture.Repository.MakeCommits(5); - var expectedFullSemVer = $"1.0.1-{preReleaseTagName}.1+5"; + var expectedFullSemVer = $"1.0.1-{preReleaseLabelName}.1+5"; fixture.AssertFullSemver(expectedFullSemVer, configuration); } diff --git a/src/GitVersion.Core/Configuration/ConfigurationExtensions.cs b/src/GitVersion.Core/Configuration/ConfigurationExtensions.cs index fe1db1ee8e..78c2e823bf 100644 --- a/src/GitVersion.Core/Configuration/ConfigurationExtensions.cs +++ b/src/GitVersion.Core/Configuration/ConfigurationExtensions.cs @@ -77,7 +77,7 @@ public static bool IsReleaseBranch(this IGitVersionConfiguration configuration, if (!configuration.RegularExpression.IsNullOrWhiteSpace() && !effectiveBranchName.IsNullOrEmpty()) { - effectiveBranchName = effectiveBranchName.RegexReplace("[^a-zA-Z0-9-]", "-"); + effectiveBranchName = effectiveBranchName.RegexReplace("[^a-zA-Z0-9-_]", "-"); var pattern = new Regex(configuration.RegularExpression, RegexOptions.IgnoreCase); var match = pattern.Match(effectiveBranchName); if (match.Success)