Skip to content

Commit 819df58

Browse files
committed
w
1 parent 3711ff0 commit 819df58

File tree

1 file changed

+125
-60
lines changed

1 file changed

+125
-60
lines changed

src/GitVersion.Core/Core/RegexPatterns.cs

Lines changed: 125 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,22 @@ static RegexPatterns()
6464

6565
internal static class Common
6666
{
67-
public static Regex SwitchArgumentRegex { get; } = new(@"/\w+:", Options);
68-
public static Regex ObscurePasswordRegex { get; } = new("(https?://)(.+)(:.+@)", Options);
67+
[StringSyntax(StringSyntaxAttribute.Regex)]
68+
private const string SwitchArgumentRegexPattern = @"/\w+:";
69+
70+
[StringSyntax(StringSyntaxAttribute.Regex)]
71+
private const string ObscurePasswordRegexPattern = "(https?://)(.+)(:.+@)";
6972

7073
// this regex matches an expression to replace.
7174
// - env:ENV name OR a member name
7275
// - optional fallback value after " ?? "
7376
// - the fallback value should be a quoted string, but simple unquoted text is allowed for back compat
74-
public static Regex ExpandTokensRegex { get; } = new("""{((env:(?<envvar>\w+))|(?<member>\w+))(\s+(\?\?)??\s+((?<fallback>\w+)|"(?<fallback>.*)"))??}""", Options);
77+
[StringSyntax(StringSyntaxAttribute.Regex)]
78+
private const string ExpandTokensRegexPattern = """{((env:(?<envvar>\w+))|(?<member>\w+))(\s+(\?\?)??\s+((?<fallback>\w+)|"(?<fallback>.*)"))??}""";
79+
80+
public static Regex SwitchArgumentRegex { get; } = new(SwitchArgumentRegexPattern, Options);
81+
public static Regex ObscurePasswordRegex { get; } = new(ObscurePasswordRegexPattern, Options);
82+
public static Regex ExpandTokensRegex { get; } = new(ExpandTokensRegexPattern, Options);
7583
}
7684

7785
internal static class Configuration
@@ -120,24 +128,66 @@ internal static class Configuration
120128

121129
internal static class MergeMessage
122130
{
123-
public static Regex DefaultMergeMessageRegex { get; } = new(@"^Merge (branch|tag) '(?<SourceBranch>[^']*)'(?: into (?<TargetBranch>[^\s]*))*", Options);
124-
public static Regex SmartGitMergeMessageRegex { get; } = new(@"^Finish (?<SourceBranch>[^\s]*)(?: into (?<TargetBranch>[^\s]*))*", Options);
125-
public static Regex BitBucketPullMergeMessageRegex { get; } = new(@"^Merge pull request #(?<PullRequestNumber>\d+) (from|in) (?<Source>.*) from (?<SourceBranch>[^\s]*) to (?<TargetBranch>[^\s]*)", Options);
126-
public static Regex BitBucketPullv7MergeMessageRegex { get; } = new(@"^Pull request #(?<PullRequestNumber>\d+).*\r?\n\r?\nMerge in (?<Source>.*) from (?<SourceBranch>[^\s]*) to (?<TargetBranch>[^\s]*)", Options);
127-
public static Regex BitBucketCloudPullMergeMessageRegex { get; } = new(@"^Merged in (?<SourceBranch>[^\s]*) \(pull request #(?<PullRequestNumber>\d+)\)", Options);
128-
public static Regex GitHubPullMergeMessageRegex { get; } = new(@"^Merge pull request #(?<PullRequestNumber>\d+) (from|in) (?:[^\s\/]+\/)?(?<SourceBranch>[^\s]*)(?: into (?<TargetBranch>[^\s]*))*", Options);
129-
public static Regex RemoteTrackingMergeMessageRegex { get; } = new(@"^Merge remote-tracking branch '(?<SourceBranch>[^\s]*)'(?: into (?<TargetBranch>[^\s]*))*", Options);
130-
public static Regex AzureDevOpsPullMergeMessageRegex { get; } = new(@"^Merge pull request (?<PullRequestNumber>\d+) from (?<SourceBranch>[^\s]*) into (?<TargetBranch>[^\s]*)", Options);
131+
[StringSyntax(StringSyntaxAttribute.Regex)]
132+
private const string DefaultMergeMessageRegexPattern = @"^Merge (branch|tag) '(?<SourceBranch>[^']*)'(?: into (?<TargetBranch>[^\s]*))*";
133+
134+
[StringSyntax(StringSyntaxAttribute.Regex)]
135+
private const string SmartGitMergeMessageRegexPattern = @"^Finish (?<SourceBranch>[^\s]*)(?: into (?<TargetBranch>[^\s]*))*";
136+
137+
[StringSyntax(StringSyntaxAttribute.Regex)]
138+
private const string BitBucketPullMergeMessageRegexPattern = @"^Merge pull request #(?<PullRequestNumber>\d+) (from|in) (?<Source>.*) from (?<SourceBranch>[^\s]*) to (?<TargetBranch>[^\s]*)";
139+
140+
[StringSyntax(StringSyntaxAttribute.Regex)]
141+
private const string BitBucketPullv7MergeMessageRegexPattern = @"^Pull request #(?<PullRequestNumber>\d+).*\r?\n\r?\nMerge in (?<Source>.*) from (?<SourceBranch>[^\s]*) to (?<TargetBranch>[^\s]*)";
142+
143+
[StringSyntax(StringSyntaxAttribute.Regex)]
144+
private const string BitBucketCloudPullMergeMessageRegexPattern = @"^Merged in (?<SourceBranch>[^\s]*) \(pull request #(?<PullRequestNumber>\d+)\)";
145+
146+
[StringSyntax(StringSyntaxAttribute.Regex)]
147+
private const string GitHubPullMergeMessageRegexPattern = @"^Merge pull request #(?<PullRequestNumber>\d+) (from|in) (?:[^\s\/]+\/)?(?<SourceBranch>[^\s]*)(?: into (?<TargetBranch>[^\s]*))*";
148+
149+
[StringSyntax(StringSyntaxAttribute.Regex)]
150+
private const string RemoteTrackingMergeMessageRegexPattern = @"^Merge remote-tracking branch '(?<SourceBranch>[^\s]*)'(?: into (?<TargetBranch>[^\s]*))*";
151+
152+
[StringSyntax(StringSyntaxAttribute.Regex)]
153+
private const string AzureDevOpsPullMergeMessageRegexPattern = @"^Merge pull request (?<PullRequestNumber>\d+) from (?<SourceBranch>[^\s]*) into (?<TargetBranch>[^\s]*)";
154+
155+
public static Regex DefaultMergeMessageRegex { get; } = new(DefaultMergeMessageRegexPattern, Options);
156+
public static Regex SmartGitMergeMessageRegex { get; } = new(SmartGitMergeMessageRegexPattern, Options);
157+
public static Regex BitBucketPullMergeMessageRegex { get; } = new(BitBucketPullMergeMessageRegexPattern, Options);
158+
public static Regex BitBucketPullv7MergeMessageRegex { get; } = new(BitBucketPullv7MergeMessageRegexPattern, Options);
159+
public static Regex BitBucketCloudPullMergeMessageRegex { get; } = new(BitBucketCloudPullMergeMessageRegexPattern, Options);
160+
public static Regex GitHubPullMergeMessageRegex { get; } = new(GitHubPullMergeMessageRegexPattern, Options);
161+
public static Regex RemoteTrackingMergeMessageRegex { get; } = new(RemoteTrackingMergeMessageRegexPattern, Options);
162+
public static Regex AzureDevOpsPullMergeMessageRegex { get; } = new(AzureDevOpsPullMergeMessageRegexPattern, Options);
131163
}
132164

133165
internal static class Output
134166
{
135-
public static Regex AssemblyVersionRegex { get; } = new(@"AssemblyVersion(Attribute)?\s*\(.*\)\s*", Options);
136-
public static Regex AssemblyInfoVersionRegex { get; } = new(@"AssemblyInformationalVersion(Attribute)?\s*\(.*\)\s*", Options);
137-
public static Regex AssemblyFileVersionRegex { get; } = new(@"AssemblyFileVersion(Attribute)?\s*\(.*\)\s*", Options);
138-
public static Regex CsharpAssemblyAttributeRegex { get; } = new(@"(\s*\[\s*assembly:\s*(?:.*)\s*\]\s*$(\r?\n)?)", Options | RegexOptions.Multiline);
139-
public static Regex FsharpAssemblyAttributeRegex { get; } = new(@"(\s*\[\s*\<assembly:\s*(?:.*)\>\s*\]\s*$(\r?\n)?)", Options | RegexOptions.Multiline);
140-
public static Regex VisualBasicAssemblyAttributeRegex { get; } = new(@"(\s*\<Assembly:\s*(?:.*)\>\s*$(\r?\n)?)", Options | RegexOptions.Multiline);
167+
[StringSyntax(StringSyntaxAttribute.Regex)]
168+
private const string AssemblyVersionRegexPattern = @"AssemblyVersion(Attribute)?\s*\(.*\)\s*";
169+
170+
[StringSyntax(StringSyntaxAttribute.Regex)]
171+
private const string AssemblyInfoVersionRegexPattern = @"AssemblyInformationalVersion(Attribute)?\s*\(.*\)\s*";
172+
173+
[StringSyntax(StringSyntaxAttribute.Regex)]
174+
private const string AssemblyFileVersionRegexPattern = @"AssemblyFileVersion(Attribute)?\s*\(.*\)\s*";
175+
176+
[StringSyntax(StringSyntaxAttribute.Regex)]
177+
private const string CsharpAssemblyAttributeRegexPattern = @"(\s*\[\s*assembly:\s*(?:.*)\s*\]\s*$(\r?\n)?)";
178+
179+
[StringSyntax(StringSyntaxAttribute.Regex)]
180+
private const string FsharpAssemblyAttributeRegexPattern = @"(\s*\[\s*\<assembly:\s*(?:.*)\>\s*\]\s*$(\r?\n)?)";
181+
182+
[StringSyntax(StringSyntaxAttribute.Regex)]
183+
private const string VisualBasicAssemblyAttributeRegexPattern = @"(\s*\<Assembly:\s*(?:.*)\>\s*$(\r?\n)?)";
184+
185+
public static Regex AssemblyVersionRegex { get; } = new(AssemblyVersionRegexPattern, Options);
186+
public static Regex AssemblyInfoVersionRegex { get; } = new(AssemblyInfoVersionRegexPattern, Options);
187+
public static Regex AssemblyFileVersionRegex { get; } = new(AssemblyFileVersionRegexPattern, Options);
188+
public static Regex CsharpAssemblyAttributeRegex { get; } = new(CsharpAssemblyAttributeRegexPattern, Options | RegexOptions.Multiline);
189+
public static Regex FsharpAssemblyAttributeRegex { get; } = new(FsharpAssemblyAttributeRegexPattern, Options | RegexOptions.Multiline);
190+
public static Regex VisualBasicAssemblyAttributeRegex { get; } = new(VisualBasicAssemblyAttributeRegexPattern, Options | RegexOptions.Multiline);
141191
}
142192

143193
internal static class VersionCalculation
@@ -162,89 +212,104 @@ internal static class VersionCalculation
162212

163213
internal static class SemanticVersion
164214
{
215+
[StringSyntax(StringSyntaxAttribute.Regex)]
216+
private const string ParseStrictRegexPattern = @"^(?<major>0|[1-9]\d*)\.(?<minor>0|[1-9]\d*)\.(?<patch>0|[1-9]\d*)(?:-(?<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$";
217+
218+
[StringSyntax(StringSyntaxAttribute.Regex)]
219+
private const string ParseLooseRegexPattern = @"^(?<SemVer>(?<Major>\d+)(\.(?<Minor>\d+))?(\.(?<Patch>\d+))?)(\.(?<FourthPart>\d+))?(-(?<Tag>[^\+]*))?(\+(?<BuildMetaData>.*))?$";
220+
221+
[StringSyntax(StringSyntaxAttribute.Regex)]
222+
private const string ParseBuildMetaDataRegexPattern = @"(?<BuildNumber>\d+)?(\.?Branch(Name)?\.(?<BranchName>[^\.]+))?(\.?Sha?\.(?<Sha>[^\.]+))?(?<Other>.*)";
223+
224+
[StringSyntax(StringSyntaxAttribute.Regex)]
225+
private const string FormatBuildMetaDataRegexPattern = "[^0-9A-Za-z-.]";
226+
227+
[StringSyntax(StringSyntaxAttribute.Regex)]
228+
private const string ParsePreReleaseTagRegexPattern = @"(?<name>.*?)\.?(?<number>\d+)?$";
229+
165230
// uses the git-semver spec https://github.com/semver/semver/blob/master/semver.md
166-
public static Regex ParseStrictRegex { get; } = new(
167-
@"^(?<major>0|[1-9]\d*)\.(?<minor>0|[1-9]\d*)\.(?<patch>0|[1-9]\d*)(?:-(?<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$",
168-
Options);
231+
public static Regex ParseStrictRegex { get; } = new(ParseStrictRegexPattern, Options);
169232

170-
public static Regex ParseLooseRegex { get; } = new(
171-
@"^(?<SemVer>(?<Major>\d+)(\.(?<Minor>\d+))?(\.(?<Patch>\d+))?)(\.(?<FourthPart>\d+))?(-(?<Tag>[^\+]*))?(\+(?<BuildMetaData>.*))?$",
172-
Options);
233+
public static Regex ParseLooseRegex { get; } = new(ParseLooseRegexPattern, Options);
173234

174-
public static Regex ParseBuildMetaDataRegex { get; } = new(
175-
@"(?<BuildNumber>\d+)?(\.?Branch(Name)?\.(?<BranchName>[^\.]+))?(\.?Sha?\.(?<Sha>[^\.]+))?(?<Other>.*)",
176-
Options);
235+
public static Regex ParseBuildMetaDataRegex { get; } = new(ParseBuildMetaDataRegexPattern, Options);
177236

178-
public static Regex FormatBuildMetaDataRegex { get; } = new("[^0-9A-Za-z-.]",
179-
Options);
237+
public static Regex FormatBuildMetaDataRegex { get; } = new(FormatBuildMetaDataRegexPattern, Options);
180238

181-
public static Regex ParsePreReleaseTagRegex { get; } = new(
182-
@"(?<name>.*?)\.?(?<number>\d+)?$",
183-
Options);
239+
public static Regex ParsePreReleaseTagRegex { get; } = new(ParsePreReleaseTagRegexPattern, Options);
184240
}
185241

186242
internal static class AssemblyVersion
187243
{
188244
internal static class CSharp
189245
{
190-
public static Regex TriviaRegex { get; } = new(
191-
"""
192-
/\*(.*?)\*/ # Block comments: matches /* ... */
193-
|//(.*?)\r?\n # Line comments: matches // ... followed by a newline
194-
|"((\\[^\n]|[^"\n])*)" # Strings: matches " ... " including escaped quotes
195-
""",
196-
RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace | Options);
197-
198-
public static Regex AttributeRegex { get; } = new(
199-
"""
200-
(?x) # IgnorePatternWhitespace
201-
\[\s*assembly\s*:\s* # The [assembly: part
202-
(System\s*\.\s*Reflection\s*\.\s*)? # The System.Reflection. part (optional)
203-
Assembly(File|Informational)?Version # The attribute AssemblyVersion, AssemblyFileVersion, or AssemblyInformationalVersion
204-
\s*\(\s*\)\s*\] # End brackets ()]
205-
""",
206-
RegexOptions.IgnorePatternWhitespace | Options);
246+
[StringSyntax(StringSyntaxAttribute.Regex)]
247+
private const string TriviaRegexPattern =
248+
"""
249+
/\*(.*?)\*/ # Block comments: matches /* ... */
250+
|//(.*?)\r?\n # Line comments: matches // ... followed by a newline
251+
|"((\\[^\n]|[^"\n])*)" # Strings: matches " ... " including escaped quotes
252+
""";
253+
254+
[StringSyntax(StringSyntaxAttribute.Regex)]
255+
private const string AttributeRegexPattern =
256+
"""
257+
(?x) # IgnorePatternWhitespace
258+
\[\s*assembly\s*:\s* # The [assembly: part
259+
(System\s*\.\s*Reflection\s*\.\s*)? # The System.Reflection. part (optional)
260+
Assembly(File|Informational)?Version # The attribute AssemblyVersion, AssemblyFileVersion, or AssemblyInformationalVersion
261+
\s*\(\s*\)\s*\] # End brackets ()]
262+
""";
263+
264+
public static Regex TriviaRegex { get; } = new(TriviaRegexPattern, RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace | Options);
265+
public static Regex AttributeRegex { get; } = new(AttributeRegexPattern, RegexOptions.IgnorePatternWhitespace | Options);
207266
}
208267

209268
internal static class FSharp
210269
{
211-
public static Regex TriviaRegex { get; } = new(
270+
[StringSyntax(StringSyntaxAttribute.Regex)]
271+
private const string TriviaRegexPattern =
212272
"""
213273
/\*(.*?)\*/ # Block comments: matches /* ... */
214274
|//(.*?)\r?\n # Line comments: matches // ... followed by a newline
215275
|"((\\[^\n]|[^"\n])*)" # Strings: matches " ... " including escaped quotes
216-
""",
217-
RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace | Options);
276+
""";
218277

219-
public static Regex AttributeRegex { get; } = new(
278+
[StringSyntax(StringSyntaxAttribute.Regex)]
279+
private const string AttributeRegexPattern =
220280
"""
221281
(?x) # IgnorePatternWhitespace
222282
\[\s*<\s*assembly\s*:\s* # The [<assembly: part
223283
(System\s*\.\s*Reflection\s*\.\s*)? # The System.Reflection. part (optional)
224284
Assembly(File|Informational)?Version # The attribute AssemblyVersion, AssemblyFileVersion, or AssemblyInformationalVersion
225285
\s*\(\s*\)\s*>\s*\] # End brackets ()>]
226-
""",
227-
RegexOptions.IgnorePatternWhitespace | Options);
286+
""";
287+
288+
public static Regex TriviaRegex { get; } = new(TriviaRegexPattern, RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace | Options);
289+
public static Regex AttributeRegex { get; } = new(AttributeRegexPattern, RegexOptions.IgnorePatternWhitespace | Options);
228290
}
229291

230292
internal static class VisualBasic
231293
{
232-
public static Regex TriviaRegex { get; } = new(
294+
[StringSyntax(StringSyntaxAttribute.Regex)]
295+
private const string TriviaRegexPattern =
233296
"""
234297
'(.*?)\r?\n # Line comments: matches // ... followed by a newline
235298
|"((\\[^\n]|[^"\n])*)" # Strings: matches " ... " including escaped quotes
236-
""",
237-
RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace | Options);
299+
""";
238300

239-
public static Regex AttributeRegex { get; } = new(
301+
[StringSyntax(StringSyntaxAttribute.Regex)]
302+
private const string AttributeRegexPattern =
240303
"""
241304
(?x) # IgnorePatternWhitespace
242305
\<\s*Assembly\s*:\s* # The <Assembly: part
243306
(System\s*\.\s*Reflection\s*\.\s*)? # The System.Reflection. part (optional)
244307
Assembly(File|Informational)?Version # The attribute AssemblyVersion, AssemblyFileVersion, or AssemblyInformationalVersion
245308
\s*\(\s*\)\s*\> # End brackets ()>
246-
""",
247-
RegexOptions.IgnorePatternWhitespace | Options);
309+
""";
310+
311+
public static Regex TriviaRegex { get; } = new(TriviaRegexPattern, RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace | Options);
312+
public static Regex AttributeRegex { get; } = new(AttributeRegexPattern, RegexOptions.IgnorePatternWhitespace | Options);
248313
}
249314
}
250315
}

0 commit comments

Comments
 (0)