Skip to content

Commit adb914f

Browse files
committed
Happened across a few more Regexes to move in to the cache.
1 parent fffb957 commit adb914f

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

src/GitVersion.Core/Core/RegexPatterns.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public static Regex GetOrAdd([StringSyntax(StringSyntaxAttribute.Regex)] string
3030
[Common.ObscurePasswordRegexPattern] = Common.ObscurePasswordRegex,
3131
[Common.ExpandTokensRegexPattern] = Common.ExpandTokensRegex,
3232
[Common.SanitizeEnvVarNameRegexPattern] = Common.SanitizeEnvVarNameRegex,
33+
[Common.SanitizeMemberNameRegexPattern] = Common.SanitizeMemberNameRegex,
34+
[Common.SanitizeGitNameRegexPattern] = Common.SanitizeGitNameRegex,
3335
[Configuration.DefaultTagPrefixRegexPattern] = Configuration.DefaultTagPrefixRegex,
3436
[Configuration.DefaultVersionInBranchRegexPattern] = Configuration.DefaultVersionInBranchRegex,
3537
[Configuration.MainBranchRegexPattern] = Configuration.MainBranchRegex,
@@ -51,6 +53,7 @@ public static Regex GetOrAdd([StringSyntax(StringSyntaxAttribute.Regex)] string
5153
[Output.AssemblyVersionRegexPattern] = Output.AssemblyVersionRegex,
5254
[Output.AssemblyInfoVersionRegexPattern] = Output.AssemblyInfoVersionRegex,
5355
[Output.AssemblyFileVersionRegexPattern] = Output.AssemblyFileVersionRegex,
56+
[Output.SanitizeAssemblyInfoRegexPattern] = Output.SanitizeAssemblyInfoRegex,
5457
[Output.CsharpAssemblyAttributeRegexPattern] = Output.CsharpAssemblyAttributeRegex,
5558
[Output.FsharpAssemblyAttributeRegexPattern] = Output.FsharpAssemblyAttributeRegex,
5659
[Output.VisualBasicAssemblyAttributeRegexPattern] = Output.VisualBasicAssemblyAttributeRegex,
@@ -107,15 +110,18 @@ internal static partial class Common
107110
/// <summary>
108111
/// Allow alphanumeric, underscore, colon (for custom format specification), hyphen, and dot
109112
/// </summary>
110-
[StringSyntax(StringSyntaxAttribute.Regex)]
113+
[StringSyntax(StringSyntaxAttribute.Regex, Options)]
111114
internal const string SanitizeEnvVarNameRegexPattern = @"^[A-Za-z0-9_:\-\.]+$";
112115

113116
/// <summary>
114117
/// Allow alphanumeric, underscore, and dot for property/field access
115118
/// </summary>
116-
[StringSyntax(StringSyntaxAttribute.Regex)]
119+
[StringSyntax(StringSyntaxAttribute.Regex, Options)]
117120
internal const string SanitizeMemberNameRegexPattern = @"^[A-Za-z0-9_\.]+$";
118121

122+
[StringSyntax(StringSyntaxAttribute.Regex, Options)]
123+
internal const string SanitizeGitNameRegexPattern = "[^a-zA-Z0-9-]";
124+
119125
[GeneratedRegex(SwitchArgumentRegexPattern, Options)]
120126
public static partial Regex SwitchArgumentRegex();
121127

@@ -130,6 +136,9 @@ internal static partial class Common
130136

131137
[GeneratedRegex(SanitizeMemberNameRegexPattern, Options)]
132138
public static partial Regex SanitizeMemberNameRegex();
139+
140+
[GeneratedRegex(SanitizeGitNameRegexPattern, Options)]
141+
public static partial Regex SanitizeGitNameRegex();
133142
}
134143

135144
internal static partial class Configuration
@@ -269,6 +278,9 @@ internal static partial class Output
269278
[StringSyntax(StringSyntaxAttribute.Regex)]
270279
internal const string SanitizeParticipantRegexPattern = "[^a-zA-Z0-9]";
271280

281+
[StringSyntax(StringSyntaxAttribute.Regex)]
282+
internal const string SanitizeAssemblyInfoRegexPattern = "[^0-9A-Za-z-.+]";
283+
272284
[GeneratedRegex(AssemblyVersionRegexPattern, Options)]
273285
public static partial Regex AssemblyVersionRegex();
274286

@@ -289,6 +301,9 @@ internal static partial class Output
289301

290302
[GeneratedRegex(SanitizeParticipantRegexPattern, Options)]
291303
public static partial Regex SanitizeParticipantRegex();
304+
305+
[GeneratedRegex(SanitizeAssemblyInfoRegexPattern, RegexOptions.IgnorePatternWhitespace | Options)]
306+
public static partial Regex SanitizeAssemblyInfoRegex();
292307
}
293308

294309
internal static partial class VersionCalculation
@@ -434,7 +449,7 @@ internal static partial class VisualBasic
434449
[GeneratedRegex(TriviaRegexPattern, RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace | Options)]
435450
public static partial Regex TriviaRegex();
436451

437-
[GeneratedRegex(AttributeRegexPattern, RegexOptions.IgnorePatternWhitespace | Options)]
452+
[GeneratedRegex(AttributeRegexPattern, Options)]
438453
public static partial Regex AttributeRegex();
439454
}
440455
}

src/GitVersion.Core/Extensions/ConfigurationExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public static bool IsReleaseBranch(this IGitVersionConfiguration configuration,
109109
return label;
110110
}
111111

112-
private static string EscapeInvalidCharacters(string groupValue) => groupValue.RegexReplace("[^a-zA-Z0-9-]", "-");
112+
private static string EscapeInvalidCharacters(string groupValue) => groupValue.RegexReplace(RegexPatterns.Common.SanitizeGitNameRegexPattern, "-");
113113

114114
public static (string GitDirectory, string WorkingTreeDirectory)? FindGitDir(this IFileSystem fileSystem, string? path)
115115
{

src/GitVersion.Core/VersionCalculation/SemanticVersioning/SemanticVersionFormatValues.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Globalization;
22
using GitVersion.Configuration;
3+
using GitVersion.Core;
34
using GitVersion.Extensions;
45

56
namespace GitVersion;
@@ -41,7 +42,7 @@ public class SemanticVersionFormatValues(SemanticVersion semver, IGitVersionConf
4142

4243
public string? BranchName => semver.BuildMetaData.Branch;
4344

44-
public string? EscapedBranchName => semver.BuildMetaData.Branch?.RegexReplace("[^a-zA-Z0-9-]", "-");
45+
public string? EscapedBranchName => semver.BuildMetaData.Branch?.RegexReplace(RegexPatterns.Common.SanitizeGitNameRegexPattern, "-");
4546

4647
public string? Sha => semver.BuildMetaData.Sha;
4748

src/GitVersion.Core/VersionCalculation/VariableProvider.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using GitVersion.Configuration;
2+
using GitVersion.Core;
23
using GitVersion.Extensions;
34
using GitVersion.Helpers;
45
using GitVersion.OutputVariables;
@@ -79,7 +80,8 @@ public GitVersionVariables GetVariablesFor(
7980
{
8081
try
8182
{
82-
formattedString = formatString.FormatWith(source, this.environment).RegexReplace("[^0-9A-Za-z-.+]", "-");
83+
formattedString = formatString.FormatWith(source, this.environment)
84+
.RegexReplace(RegexPatterns.Output.SanitizeAssemblyInfoRegexPattern, "-");
8385
}
8486
catch (ArgumentException exception)
8587
{

0 commit comments

Comments
 (0)