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

Move creation of Regex to a central location #4246

Merged
merged 4 commits into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions new-cli/GitVersion.Common/GitVersion.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<Compile Include="..\..\src\GitVersion.Core\Core\Abstractions\IFileSystem.cs" Link="Infrastructure\%(Filename)%(Extension)" />
<Compile Include="..\..\src\GitVersion.Core\Core\Exceptions\WarningException.cs" Link="Exceptions\%(Filename)%(Extension)"/>
<Compile Include="..\..\src\GitVersion.Core\Core\RegexPatterns.cs" Link="%(Filename)%(Extension)" />
<Compile Include="..\..\src\GitVersion.Core\Extensions\DictionaryExtensions.cs" Link="%(Filename)%(Extension)" />
<Compile Include="..\..\src\GitVersion.Core\Extensions\StringExtensions.cs" Link="Extensions\StringExtensions.cs" />
<Compile Include="..\..\src\GitVersion.Core\Extensions\CommonExtensions.cs" Link="Extensions\CommonExtensions.cs" />
<Compile Include="..\..\src\GitVersion.Core\Helpers\*.cs" Link="Helpers\%(Filename)%(Extension)" />
Expand Down
8 changes: 4 additions & 4 deletions src/GitVersion.App.Tests/ArgumentParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ private static IEnumerable<TestCaseData> OverrideConfigWithSingleOptionTestData(
"tag-prefix=sample",
new GitVersionConfiguration
{
TagPrefix = "sample"
TagPrefixPattern = "sample"
}
);
yield return new TestCaseData(
Expand Down Expand Up @@ -546,23 +546,23 @@ private static IEnumerable<TestCaseData> OverrideConfigWithMultipleOptionsTestDa
"/overrideconfig tag-prefix=sample /overrideconfig assembly-versioning-scheme=MajorMinor",
new GitVersionConfiguration
{
TagPrefix = "sample",
TagPrefixPattern = "sample",
AssemblyVersioningScheme = AssemblyVersioningScheme.MajorMinor
}
);
yield return new TestCaseData(
"/overrideconfig tag-prefix=sample /overrideconfig assembly-versioning-format=\"{Major}.{Minor}.{Patch}.{env:CI_JOB_ID ?? 0}\"",
new GitVersionConfiguration
{
TagPrefix = "sample",
TagPrefixPattern = "sample",
AssemblyVersioningFormat = "{Major}.{Minor}.{Patch}.{env:CI_JOB_ID ?? 0}"
}
);
yield return new TestCaseData(
"/overrideconfig tag-prefix=sample /overrideconfig assembly-versioning-format=\"{Major}.{Minor}.{Patch}.{env:CI_JOB_ID ?? 0}\" /overrideconfig update-build-number=true /overrideconfig assembly-versioning-scheme=MajorMinorPatchTag /overrideconfig mode=ContinuousDelivery /overrideconfig tag-pre-release-weight=4",
new GitVersionConfiguration
{
TagPrefix = "sample",
TagPrefixPattern = "sample",
AssemblyVersioningFormat = "{Major}.{Minor}.{Patch}.{env:CI_JOB_ID ?? 0}",
UpdateBuildNumber = true,
AssemblyVersioningScheme = AssemblyVersioningScheme.MajorMinorPatchTag,
Expand Down
3 changes: 1 addition & 2 deletions src/GitVersion.BuildAgents/Agents/AzurePipelines.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Text.RegularExpressions;
using GitVersion.Extensions;
using GitVersion.Logging;
using GitVersion.OutputVariables;
Expand Down Expand Up @@ -52,7 +51,7 @@ private static string ReplaceVariables(string buildNumberEnv, KeyValuePair<strin
return replacement switch
{
null => buildNumberEnv,
_ => buildNumberEnv.RegexReplace(pattern, replacement, RegexOptions.IgnoreCase)
_ => buildNumberEnv.RegexReplace(pattern, replacement)
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ public void CanReadDefaultDocument()
configuration.AssemblyInformationalFormat.ShouldBe(null);
configuration.Branches["develop"].Label.ShouldBe("alpha");
configuration.Branches["release"].Label.ShouldBe("beta");
configuration.TagPrefix.ShouldBe(ConfigurationConstants.DefaultTagPrefix);
configuration.TagPrefixPattern.ShouldBe(RegexPatterns.Configuration.DefaultTagPrefixPattern);
configuration.NextVersion.ShouldBe(null);
}

Expand Down Expand Up @@ -361,7 +361,7 @@ public void ShouldNotOverrideAnythingWhenOverrideConfigIsEmpty()

var expectedConfig = GitFlowConfigurationBuilder.New
.WithNextVersion("1.2.3")
.WithTagPrefix("custom-tag-prefix-from-yml")
.WithTagPrefixPattern("custom-tag-prefix-from-yml")
.Build();
var configuration = this.configurationProvider.ProvideForDirectory(this.repoPath);

Expand All @@ -370,7 +370,7 @@ public void ShouldNotOverrideAnythingWhenOverrideConfigIsEmpty()
configuration.AssemblyInformationalFormat.ShouldBe(expectedConfig.AssemblyInformationalFormat);
configuration.AssemblyVersioningFormat.ShouldBe(expectedConfig.AssemblyVersioningFormat);
configuration.AssemblyFileVersioningFormat.ShouldBe(expectedConfig.AssemblyFileVersioningFormat);
configuration.TagPrefix.ShouldBe(expectedConfig.TagPrefix);
configuration.TagPrefixPattern.ShouldBe(expectedConfig.TagPrefixPattern);
configuration.NextVersion.ShouldBe(expectedConfig.NextVersion);
configuration.MajorVersionBumpMessage.ShouldBe(expectedConfig.MajorVersionBumpMessage);
configuration.MinorVersionBumpMessage.ShouldBe(expectedConfig.MinorVersionBumpMessage);
Expand Down Expand Up @@ -398,7 +398,7 @@ public void ShouldUseDefaultTagPrefixWhenNotSetInConfigFile()
using var _ = this.fileSystem.SetupConfigFile(path: this.repoPath, text: text);
var configuration = this.configurationProvider.ProvideForDirectory(this.repoPath);

configuration.TagPrefix.ShouldBe(ConfigurationConstants.DefaultTagPrefix);
configuration.TagPrefixPattern.ShouldBe(RegexPatterns.Configuration.DefaultTagPrefixPattern);
}

[Test]
Expand All @@ -408,7 +408,7 @@ public void ShouldUseTagPrefixFromConfigFileWhenProvided()
using var _ = this.fileSystem.SetupConfigFile(path: this.repoPath, text: text);
var configuration = this.configurationProvider.ProvideForDirectory(this.repoPath);

configuration.TagPrefix.ShouldBe("custom-tag-prefix-from-yml");
configuration.TagPrefixPattern.ShouldBe("custom-tag-prefix-from-yml");
}

[Test]
Expand All @@ -422,7 +422,7 @@ public void ShouldOverrideTagPrefixWithOverrideConfigValue([Values] bool tagPref
};
var configuration = this.configurationProvider.ProvideForDirectory(this.repoPath, overrideConfiguration);

configuration.TagPrefix.ShouldBe("tag-prefix-from-override-configuration");
configuration.TagPrefixPattern.ShouldBe("tag-prefix-from-override-configuration");
}

[Test]
Expand All @@ -437,7 +437,7 @@ public void ShouldNotOverrideDefaultTagPrefixWhenNotSetInOverrideConfig()

var configuration = this.configurationProvider.ProvideForDirectory(this.repoPath, overrideConfiguration);

configuration.TagPrefix.ShouldBe(ConfigurationConstants.DefaultTagPrefix);
configuration.TagPrefixPattern.ShouldBe(RegexPatterns.Configuration.DefaultTagPrefixPattern);
}

[Test]
Expand All @@ -451,7 +451,7 @@ public void ShouldNotOverrideTagPrefixFromConfigFileWhenNotSetInOverrideConfig()
};
var configuration = this.configurationProvider.ProvideForDirectory(this.repoPath, overrideConfiguration);

configuration.TagPrefix.ShouldBe("custom-tag-prefix-from-yml");
configuration.TagPrefixPattern.ShouldBe("custom-tag-prefix-from-yml");
}

[Test]
Expand All @@ -465,6 +465,6 @@ public void ShouldOverrideTagPrefixFromConfigFileWhenSetInOverrideConfig()
};
var configuration = this.configurationProvider.ProvideForDirectory(this.repoPath, overrideConfiguration);

configuration.TagPrefix.ShouldBe("custom-tag-prefix-from-console");
configuration.TagPrefixPattern.ShouldBe("custom-tag-prefix-from-console");
}
}
5 changes: 3 additions & 2 deletions src/GitVersion.Configuration/BranchConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using GitVersion.Configuration.Attributes;
using GitVersion.Core;
using GitVersion.Extensions;
using GitVersion.VersionCalculation;

Expand Down Expand Up @@ -26,8 +27,8 @@ internal record BranchConfiguration : IBranchConfiguration
public PreventIncrementConfiguration PreventIncrement { get; internal set; } = new();

[JsonPropertyName("label-number-pattern")]
[JsonPropertyDescription($"The regular expression pattern to use to extract the number from the branch name. Defaults to '{ConfigurationConstants.DefaultLabelNumberPattern}'.")]
[JsonPropertyDefault(ConfigurationConstants.DefaultLabelNumberPattern)]
[JsonPropertyDescription($"The regular expression pattern to use to extract the number from the branch name. Defaults to '{RegexPatterns.Configuration.DefaultLabelNumberPattern}'.")]
[JsonPropertyDefault(RegexPatterns.Configuration.DefaultLabelNumberPattern)]
[JsonPropertyFormat(Format.Regex)]
public string? LabelNumberPattern { get; internal set; }

Expand Down
23 changes: 12 additions & 11 deletions src/GitVersion.Configuration/Builders/ConfigurationBuilderBase.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using GitVersion.Core;
using GitVersion.Extensions;
using GitVersion.Helpers;
using GitVersion.VersionCalculation;
Expand Down Expand Up @@ -47,49 +48,49 @@ internal abstract class ConfigurationBuilderBase<TConfigurationBuilder> : IConfi
protected readonly BranchMetaData MainBranch = new()
{
Name = ConfigurationConstants.MainBranchKey,
RegexPattern = ConfigurationConstants.MainBranchRegex
RegexPattern = RegexPatterns.Configuration.MainBranchRegexPattern
};

protected readonly BranchMetaData DevelopBranch = new()
{
Name = ConfigurationConstants.DevelopBranchKey,
RegexPattern = ConfigurationConstants.DevelopBranchRegex
RegexPattern = RegexPatterns.Configuration.DevelopBranchRegexPattern
};

protected readonly BranchMetaData ReleaseBranch = new()
{
Name = ConfigurationConstants.ReleaseBranchKey,
RegexPattern = ConfigurationConstants.ReleaseBranchRegex
RegexPattern = RegexPatterns.Configuration.ReleaseBranchRegexPattern
};

protected readonly BranchMetaData FeatureBranch = new()
{
Name = ConfigurationConstants.FeatureBranchKey,
RegexPattern = ConfigurationConstants.FeatureBranchRegex
RegexPattern = RegexPatterns.Configuration.FeatureBranchRegexPattern
};

protected readonly BranchMetaData PullRequestBranch = new()
{
Name = ConfigurationConstants.PullRequestBranchKey,
RegexPattern = ConfigurationConstants.PullRequestBranchRegex
RegexPattern = RegexPatterns.Configuration.PullRequestBranchRegexPattern
};

protected readonly BranchMetaData HotfixBranch = new()
{
Name = ConfigurationConstants.HotfixBranchKey,
RegexPattern = ConfigurationConstants.HotfixBranchRegex
RegexPattern = RegexPatterns.Configuration.HotfixBranchRegexPattern
};

protected readonly BranchMetaData SupportBranch = new()
{
Name = ConfigurationConstants.SupportBranchKey,
RegexPattern = ConfigurationConstants.SupportBranchRegex
RegexPattern = RegexPatterns.Configuration.SupportBranchRegexPattern
};

protected readonly BranchMetaData UnknownBranch = new()
{
Name = ConfigurationConstants.UnknownBranchKey,
RegexPattern = ConfigurationConstants.UnknownBranchRegex
RegexPattern = RegexPatterns.Configuration.UnknownBranchRegexPattern
};

protected ConfigurationBuilderBase()
Expand Down Expand Up @@ -130,7 +131,7 @@ public virtual TConfigurationBuilder WithAssemblyFileVersioningFormat(string? va
return (TConfigurationBuilder)this;
}

public virtual TConfigurationBuilder WithTagPrefix(string? value)
public virtual TConfigurationBuilder WithTagPrefixPattern(string? value)
{
this.tagPrefix = value;
return (TConfigurationBuilder)this;
Expand Down Expand Up @@ -338,7 +339,7 @@ public virtual TConfigurationBuilder WithConfiguration(IGitVersionConfiguration
WithAssemblyInformationalFormat(value.AssemblyInformationalFormat);
WithAssemblyVersioningFormat(value.AssemblyVersioningFormat);
WithAssemblyFileVersioningFormat(value.AssemblyFileVersioningFormat);
WithTagPrefix(value.TagPrefix);
WithTagPrefixPattern(value.TagPrefixPattern);
WithVersionInBranchPattern(value.VersionInBranchPattern);
WithNextVersion(value.NextVersion);
WithMajorVersionBumpMessage(value.MajorVersionBumpMessage);
Expand Down Expand Up @@ -397,7 +398,7 @@ public virtual IGitVersionConfiguration Build()
AssemblyInformationalFormat = this.assemblyInformationalFormat,
AssemblyVersioningFormat = this.assemblyVersioningFormat,
AssemblyFileVersioningFormat = this.assemblyFileVersioningFormat,
TagPrefix = this.tagPrefix,
TagPrefixPattern = this.tagPrefix,
VersionInBranchPattern = this.versionInBranchPattern,
NextVersion = this.nextVersion,
MajorVersionBumpMessage = this.majorVersionBumpMessage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ private GitFlowConfigurationBuilder()
PatchVersionBumpMessage = RegexPatterns.VersionCalculation.DefaultPatchPattern,
SemanticVersionFormat = ConfigurationConstants.DefaultSemanticVersionFormat,
VersionStrategies = ConfigurationConstants.DefaultVersionStrategies,
TagPrefix = ConfigurationConstants.DefaultTagPrefix,
VersionInBranchPattern = ConfigurationConstants.DefaultVersionInBranchPattern,
TagPrefixPattern = RegexPatterns.Configuration.DefaultTagPrefixPattern,
VersionInBranchPattern = RegexPatterns.Configuration.DefaultVersionInBranchPattern,
TagPreReleaseWeight = ConfigurationConstants.DefaultTagPreReleaseWeight,
UpdateBuildNumber = ConfigurationConstants.DefaultUpdateBuildNumber,
DeploymentMode = DeploymentMode.ContinuousDelivery,
Expand Down Expand Up @@ -145,7 +145,7 @@ private GitFlowConfigurationBuilder()
OfMergedBranch = true,
WhenCurrentCommitTagged = false
},
LabelNumberPattern = ConfigurationConstants.DefaultLabelNumberPattern,
LabelNumberPattern = RegexPatterns.Configuration.DefaultLabelNumberPattern,
TrackMergeMessage = true,
PreReleaseWeight = 30000
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ private GitHubFlowConfigurationBuilder()
PatchVersionBumpMessage = RegexPatterns.VersionCalculation.DefaultPatchPattern,
SemanticVersionFormat = ConfigurationConstants.DefaultSemanticVersionFormat,
VersionStrategies = ConfigurationConstants.DefaultVersionStrategies,
TagPrefix = ConfigurationConstants.DefaultTagPrefix,
VersionInBranchPattern = ConfigurationConstants.DefaultVersionInBranchPattern,
TagPrefixPattern = RegexPatterns.Configuration.DefaultTagPrefixPattern,
VersionInBranchPattern = RegexPatterns.Configuration.DefaultVersionInBranchPattern,
TagPreReleaseWeight = ConfigurationConstants.DefaultTagPreReleaseWeight,
UpdateBuildNumber = ConfigurationConstants.DefaultUpdateBuildNumber,
DeploymentMode = DeploymentMode.ContinuousDelivery,
Expand Down Expand Up @@ -114,7 +114,7 @@ private GitHubFlowConfigurationBuilder()
OfMergedBranch = true,
WhenCurrentCommitTagged = false
},
LabelNumberPattern = ConfigurationConstants.DefaultLabelNumberPattern,
LabelNumberPattern = RegexPatterns.Configuration.DefaultLabelNumberPattern,
RegularExpression = PullRequestBranch.RegexPattern,
SourceBranches =
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ private TrunkBasedConfigurationBuilder()
VersionStrategies.ConfiguredNextVersion,
VersionStrategies.Mainline
],
TagPrefix = ConfigurationConstants.DefaultTagPrefix,
VersionInBranchPattern = ConfigurationConstants.DefaultVersionInBranchPattern,
TagPrefixPattern = RegexPatterns.Configuration.DefaultTagPrefixPattern,
VersionInBranchPattern = RegexPatterns.Configuration.DefaultVersionInBranchPattern,
TagPreReleaseWeight = ConfigurationConstants.DefaultTagPreReleaseWeight,
UpdateBuildNumber = ConfigurationConstants.DefaultUpdateBuildNumber,
DeploymentMode = DeploymentMode.ContinuousDelivery,
Expand Down Expand Up @@ -112,7 +112,7 @@ private TrunkBasedConfigurationBuilder()
OfMergedBranch = true,
WhenCurrentCommitTagged = false
},
LabelNumberPattern = ConfigurationConstants.DefaultLabelNumberPattern,
LabelNumberPattern = RegexPatterns.Configuration.DefaultLabelNumberPattern,
RegularExpression = PullRequestBranch.RegexPattern,
SourceBranches =
[
Expand Down
27 changes: 6 additions & 21 deletions src/GitVersion.Configuration/GitVersionConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System.Globalization;
using System.Text.RegularExpressions;
using GitVersion.Configuration.Attributes;
using GitVersion.Core;
using GitVersion.Extensions;
using GitVersion.VersionCalculation;
using static GitVersion.Configuration.ConfigurationConstants;

Expand Down Expand Up @@ -38,28 +36,17 @@ internal sealed record GitVersionConfiguration : BranchConfiguration, IGitVersio
public string? AssemblyFileVersioningFormat { get; internal set; }

[JsonPropertyName("tag-prefix")]
[JsonPropertyDescription($"A regular expression which is used to trim Git tags before processing. Defaults to '{DefaultTagPrefix}'")]
[JsonPropertyDefault(DefaultTagPrefix)]
[JsonPropertyDescription($"A regular expression which is used to trim Git tags before processing. Defaults to '{RegexPatterns.Configuration.DefaultTagPrefixPattern}'")]
[JsonPropertyDefault(RegexPatterns.Configuration.DefaultTagPrefixPattern)]
[JsonPropertyFormat(Format.Regex)]
public string? TagPrefix { get; internal set; }
public string? TagPrefixPattern { get; internal set; }

[JsonPropertyName("version-in-branch-pattern")]
[JsonPropertyDescription($"A regular expression which is used to determine the version number in the branch name or commit message (e.g., v1.0.0-LTS). Defaults to '{DefaultVersionInBranchPattern}'.")]
[JsonPropertyDefault(DefaultVersionInBranchPattern)]
[JsonPropertyDescription($"A regular expression which is used to determine the version number in the branch name or commit message (e.g., v1.0.0-LTS). Defaults to '{RegexPatterns.Configuration.DefaultVersionInBranchPattern}'.")]
[JsonPropertyDefault(RegexPatterns.Configuration.DefaultVersionInBranchPattern)]
[JsonPropertyFormat(Format.Regex)]
public string? VersionInBranchPattern { get; internal set; }

[JsonIgnore]
public Regex VersionInBranchRegex => versionInBranchRegex ??= new(GetVersionInBranchPattern(), RegexOptions.Compiled);
private Regex? versionInBranchRegex;

private string GetVersionInBranchPattern()
{
var versionInBranchPattern = VersionInBranchPattern;
if (versionInBranchPattern.IsNullOrEmpty()) versionInBranchPattern = DefaultVersionInBranchPattern;
return $"^{versionInBranchPattern.TrimStart('^')}";
}

[JsonPropertyName("next-version")]
[JsonPropertyDescription("Allows you to bump the next version explicitly. Useful for bumping main or a feature branch with breaking changes")]
public string? NextVersion
Expand Down Expand Up @@ -103,9 +90,7 @@ public string? NextVersion
[JsonPropertyName("commit-date-format")]
[JsonPropertyDescription($"The format to use when calculating the commit date. Defaults to '{DefaultCommitDateFormat}'. See [Standard Date and Time Format Strings](https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-date-and-time-format-strings) and [Custom Date and Time Format Strings](https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-date-and-time-format-strings).")]
[JsonPropertyDefault(DefaultCommitDateFormat)]
#if NET7_0_OR_GREATER
[System.Diagnostics.CodeAnalysis.StringSyntax("DateTimeFormat")] // See https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.codeanalysis.stringsyntaxattribute, https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.codeanalysis.stringsyntaxattribute.datetimeformat?view=net-7.0#system-diagnostics-codeanalysis-stringsyntaxattribute-datetimeformat
#endif
[System.Diagnostics.CodeAnalysis.StringSyntax("DateTimeFormat")]
public string? CommitDateFormat { get; internal set; }

[JsonPropertyName("merge-message-formats")]
Expand Down
Loading