Skip to content

More Regexes for cache #4607

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

Merged
merged 1 commit into from
Jun 29, 2025
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
14 changes: 14 additions & 0 deletions src/GitVersion.Core/Core/RegexPatterns.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public static Regex GetOrAdd([StringSyntax(StringSyntaxAttribute.Regex)] string
[Common.SwitchArgumentRegexPattern] = Common.SwitchArgumentRegex,
[Common.ObscurePasswordRegexPattern] = Common.ObscurePasswordRegex,
[Common.ExpandTokensRegexPattern] = Common.ExpandTokensRegex,
[Common.SanitizeNameRegexPattern] = Common.SanitizeNameRegex,
[Configuration.DefaultTagPrefixRegexPattern] = Configuration.DefaultTagPrefixRegex,
[Configuration.DefaultVersionInBranchRegexPattern] = Configuration.DefaultVersionInBranchRegex,
[Configuration.MainBranchRegexPattern] = Configuration.MainBranchRegex,
Expand All @@ -50,6 +51,7 @@ public static Regex GetOrAdd([StringSyntax(StringSyntaxAttribute.Regex)] string
[Output.AssemblyVersionRegexPattern] = Output.AssemblyVersionRegex,
[Output.AssemblyInfoVersionRegexPattern] = Output.AssemblyInfoVersionRegex,
[Output.AssemblyFileVersionRegexPattern] = Output.AssemblyFileVersionRegex,
[Output.SanitizeAssemblyInfoRegexPattern] = Output.SanitizeAssemblyInfoRegex,
[Output.CsharpAssemblyAttributeRegexPattern] = Output.CsharpAssemblyAttributeRegex,
[Output.FsharpAssemblyAttributeRegexPattern] = Output.FsharpAssemblyAttributeRegex,
[Output.VisualBasicAssemblyAttributeRegexPattern] = Output.VisualBasicAssemblyAttributeRegex,
Expand Down Expand Up @@ -84,6 +86,9 @@ internal static partial class Common
[StringSyntax(StringSyntaxAttribute.Regex)]
internal const string ExpandTokensRegexPattern = """{((env:(?<envvar>\w+))|(?<member>\w+))(\s+(\?\?)??\s+((?<fallback>\w+)|"(?<fallback>.*)"))??}""";

[StringSyntax(StringSyntaxAttribute.Regex, Options)]
internal const string SanitizeNameRegexPattern = "[^a-zA-Z0-9-]";

[GeneratedRegex(SwitchArgumentRegexPattern, Options)]
public static partial Regex SwitchArgumentRegex();

Expand All @@ -92,6 +97,9 @@ internal static partial class Common

[GeneratedRegex(ExpandTokensRegexPattern, Options)]
public static partial Regex ExpandTokensRegex();

[GeneratedRegex(SanitizeNameRegexPattern, Options)]
public static partial Regex SanitizeNameRegex();
}

internal static partial class Configuration
Expand Down Expand Up @@ -231,6 +239,9 @@ internal static partial class Output
[StringSyntax(StringSyntaxAttribute.Regex)]
internal const string SanitizeParticipantRegexPattern = "[^a-zA-Z0-9]";

[StringSyntax(StringSyntaxAttribute.Regex)]
internal const string SanitizeAssemblyInfoRegexPattern = "[^0-9A-Za-z-.+]";

[GeneratedRegex(AssemblyVersionRegexPattern, Options)]
public static partial Regex AssemblyVersionRegex();

Expand All @@ -251,6 +262,9 @@ internal static partial class Output

[GeneratedRegex(SanitizeParticipantRegexPattern, Options)]
public static partial Regex SanitizeParticipantRegex();

[GeneratedRegex(SanitizeAssemblyInfoRegexPattern, RegexOptions.IgnorePatternWhitespace | Options)]
public static partial Regex SanitizeAssemblyInfoRegex();
}

internal static partial class VersionCalculation
Expand Down
2 changes: 1 addition & 1 deletion src/GitVersion.Core/Extensions/ConfigurationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public static bool IsReleaseBranch(this IGitVersionConfiguration configuration,
return label;
}

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

public static (string GitDirectory, string WorkingTreeDirectory)? FindGitDir(this IFileSystem fileSystem, string? path)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Globalization;
using GitVersion.Configuration;
using GitVersion.Core;
using GitVersion.Extensions;

namespace GitVersion;
Expand Down Expand Up @@ -41,7 +42,7 @@ public class SemanticVersionFormatValues(SemanticVersion semver, IGitVersionConf

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

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

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

Expand Down
4 changes: 3 additions & 1 deletion src/GitVersion.Core/VersionCalculation/VariableProvider.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using GitVersion.Configuration;
using GitVersion.Core;
using GitVersion.Extensions;
using GitVersion.Helpers;
using GitVersion.OutputVariables;
Expand Down Expand Up @@ -79,7 +80,8 @@ public GitVersionVariables GetVariablesFor(
{
try
{
formattedString = formatString.FormatWith(source, this.environment).RegexReplace("[^0-9A-Za-z-.+]", "-");
formattedString = formatString.FormatWith(source, this.environment)
.RegexReplace(RegexPatterns.Output.SanitizeAssemblyInfoRegexPattern, "-");
}
catch (ArgumentException exception)
{
Expand Down