Skip to content

Commit

Permalink
d
Browse files Browse the repository at this point in the history
  • Loading branch information
arturcic committed Sep 24, 2024
1 parent 5956087 commit 19e2907
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 52 deletions.
6 changes: 0 additions & 6 deletions src/GitVersion.Core/Configuration/EffectiveConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,10 @@ public EffectiveConfiguration(IGitVersionConfiguration configuration, IBranchCon
public string? AssemblyVersioningFormat { get; }
public string? AssemblyFileVersioningFormat { get; }

/// <summary>
/// Git tag prefix
/// </summary>
public string? TagPrefix { get; }

public Regex VersionInBranchRegex { get; }

/// <summary>
/// Label to use when calculating SemVer
/// </summary>
public string? Label { get; }

public string? NextVersion { get; }
Expand Down
88 changes: 44 additions & 44 deletions src/GitVersion.Core/Core/RegexPatterns.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace GitVersion.Core;

internal static class RegexPatterns
{
private readonly static RegexOptions RegexOptions = RegexOptions.IgnoreCase | RegexOptions.Compiled;
private const RegexOptions Options = RegexOptions.IgnoreCase | RegexOptions.Compiled;

internal static readonly ConcurrentDictionary<string, Regex> Cache = new();

Expand Down Expand Up @@ -65,14 +65,14 @@ static RegexPatterns()

internal static class Common
{
public static Regex SwitchArgumentRegex { get; } = new(@"/\w+:", RegexOptions);
public static Regex ObscurePasswordRegex { get; } = new("(https?://)(.+)(:.+@)", RegexOptions);
public static Regex SwitchArgumentRegex { get; } = new(@"/\w+:", Options);
public static Regex ObscurePasswordRegex { get; } = new("(https?://)(.+)(:.+@)", Options);

// This regex matches an expression to replace.
// - env:ENV name OR a member name
// - optional fallback value after " ?? "
// - the fallback value should be a quoted string, but simple unquoted text is allowed for back compat
public static Regex ExpandTokensRegex { get; } = new("""{((env:(?<envvar>\w+))|(?<member>\w+))(\s+(\?\?)??\s+((?<fallback>\w+)|"(?<fallback>.*)"))??}""", RegexOptions);
public static Regex ExpandTokensRegex { get; } = new("""{((env:(?<envvar>\w+))|(?<member>\w+))(\s+(\?\?)??\s+((?<fallback>\w+)|"(?<fallback>.*)"))??}""", Options);
}

internal static class Configuration
Expand Down Expand Up @@ -110,39 +110,39 @@ internal static class Configuration
[StringSyntax(StringSyntaxAttribute.Regex)]
public const string UnknownBranchRegexPattern = "(?<BranchName>.+)";

public static Regex DefaultTagPrefixRegex { get; } = new(DefaultTagPrefixPattern, RegexOptions);
public static Regex DefaultVersionInBranchRegex { get; } = new(DefaultVersionInBranchPattern, RegexOptions);
public static Regex DefaultLabelNumberRegex { get; } = new(DefaultLabelNumberPattern, RegexOptions);
public static Regex MainBranchRegex { get; } = new(MainBranchRegexPattern, RegexOptions);
public static Regex DevelopBranchRegex { get; } = new(DevelopBranchRegexPattern, RegexOptions);
public static Regex ReleaseBranchRegex { get; } = new(ReleaseBranchRegexPattern, RegexOptions);
public static Regex FeatureBranchRegex { get; } = new(FeatureBranchRegexPattern, RegexOptions);
public static Regex PullRequestBranchRegex { get; } = new(PullRequestBranchRegexPattern, RegexOptions);
public static Regex HotfixBranchRegex { get; } = new(HotfixBranchRegexPattern, RegexOptions);
public static Regex SupportBranchRegex { get; } = new(SupportBranchRegexPattern, RegexOptions);
public static Regex UnknownBranchRegex { get; } = new(UnknownBranchRegexPattern, RegexOptions);
public static Regex DefaultTagPrefixRegex { get; } = new(DefaultTagPrefixPattern, Options);
public static Regex DefaultVersionInBranchRegex { get; } = new(DefaultVersionInBranchPattern, Options);
public static Regex DefaultLabelNumberRegex { get; } = new(DefaultLabelNumberPattern, Options);
public static Regex MainBranchRegex { get; } = new(MainBranchRegexPattern, Options);
public static Regex DevelopBranchRegex { get; } = new(DevelopBranchRegexPattern, Options);
public static Regex ReleaseBranchRegex { get; } = new(ReleaseBranchRegexPattern, Options);
public static Regex FeatureBranchRegex { get; } = new(FeatureBranchRegexPattern, Options);
public static Regex PullRequestBranchRegex { get; } = new(PullRequestBranchRegexPattern, Options);
public static Regex HotfixBranchRegex { get; } = new(HotfixBranchRegexPattern, Options);
public static Regex SupportBranchRegex { get; } = new(SupportBranchRegexPattern, Options);
public static Regex UnknownBranchRegex { get; } = new(UnknownBranchRegexPattern, Options);
}

internal static class MergeMessage
{
public static Regex DefaultMergeMessageRegex { get; } = new(@"^Merge (branch|tag) '(?<SourceBranch>[^']*)'(?: into (?<TargetBranch>[^\s]*))*", RegexOptions);
public static Regex SmartGitMergeMessageRegex { get; } = new(@"^Finish (?<SourceBranch>[^\s]*)(?: into (?<TargetBranch>[^\s]*))*", RegexOptions);
public static Regex BitBucketPullMergeMessageRegex { get; } = new(@"^Merge pull request #(?<PullRequestNumber>\d+) (from|in) (?<Source>.*) from (?<SourceBranch>[^\s]*) to (?<TargetBranch>[^\s]*)", RegexOptions);
public static Regex BitBucketPullv7MergeMessageRegex { get; } = new(@"^Pull request #(?<PullRequestNumber>\d+).*\r?\n\r?\nMerge in (?<Source>.*) from (?<SourceBranch>[^\s]*) to (?<TargetBranch>[^\s]*)", RegexOptions);
public static Regex BitBucketCloudPullMergeMessageRegex { get; } = new(@"^Merged in (?<SourceBranch>[^\s]*) \(pull request #(?<PullRequestNumber>\d+)\)", RegexOptions);
public static Regex GitHubPullMergeMessageRegex { get; } = new(@"^Merge pull request #(?<PullRequestNumber>\d+) (from|in) (?:[^\s\/]+\/)?(?<SourceBranch>[^\s]*)(?: into (?<TargetBranch>[^\s]*))*", RegexOptions);
public static Regex RemoteTrackingMergeMessageRegex { get; } = new(@"^Merge remote-tracking branch '(?<SourceBranch>[^\s]*)'(?: into (?<TargetBranch>[^\s]*))*", RegexOptions);
public static Regex AzureDevOpsPullMergeMessageRegex { get; } = new(@"^Merge pull request (?<PullRequestNumber>\d+) from (?<SourceBranch>[^\s]*) into (?<TargetBranch>[^\s]*)", RegexOptions);
public static Regex DefaultMergeMessageRegex { get; } = new(@"^Merge (branch|tag) '(?<SourceBranch>[^']*)'(?: into (?<TargetBranch>[^\s]*))*", Options);
public static Regex SmartGitMergeMessageRegex { get; } = new(@"^Finish (?<SourceBranch>[^\s]*)(?: into (?<TargetBranch>[^\s]*))*", Options);
public static Regex BitBucketPullMergeMessageRegex { get; } = new(@"^Merge pull request #(?<PullRequestNumber>\d+) (from|in) (?<Source>.*) from (?<SourceBranch>[^\s]*) to (?<TargetBranch>[^\s]*)", Options);
public static Regex BitBucketPullv7MergeMessageRegex { get; } = new(@"^Pull request #(?<PullRequestNumber>\d+).*\r?\n\r?\nMerge in (?<Source>.*) from (?<SourceBranch>[^\s]*) to (?<TargetBranch>[^\s]*)", Options);
public static Regex BitBucketCloudPullMergeMessageRegex { get; } = new(@"^Merged in (?<SourceBranch>[^\s]*) \(pull request #(?<PullRequestNumber>\d+)\)", Options);
public static Regex GitHubPullMergeMessageRegex { get; } = new(@"^Merge pull request #(?<PullRequestNumber>\d+) (from|in) (?:[^\s\/]+\/)?(?<SourceBranch>[^\s]*)(?: into (?<TargetBranch>[^\s]*))*", Options);
public static Regex RemoteTrackingMergeMessageRegex { get; } = new(@"^Merge remote-tracking branch '(?<SourceBranch>[^\s]*)'(?: into (?<TargetBranch>[^\s]*))*", Options);
public static Regex AzureDevOpsPullMergeMessageRegex { get; } = new(@"^Merge pull request (?<PullRequestNumber>\d+) from (?<SourceBranch>[^\s]*) into (?<TargetBranch>[^\s]*)", Options);
}

internal static class Output
{
public static Regex AssemblyVersionRegex { get; } = new(@"AssemblyVersion(Attribute)?\s*\(.*\)\s*");
public static Regex AssemblyInfoVersionRegex { get; } = new(@"AssemblyInformationalVersion(Attribute)?\s*\(.*\)\s*");
public static Regex AssemblyFileVersionRegex { get; } = new(@"AssemblyFileVersion(Attribute)?\s*\(.*\)\s*");
public static Regex CsharpAssemblyAttributeRegex { get; } = new(@"(\s*\[\s*assembly:\s*(?:.*)\s*\]\s*$(\r?\n)?)", RegexOptions.Multiline);
public static Regex FsharpAssemblyAttributeRegex { get; } = new(@"(\s*\[\s*\<assembly:\s*(?:.*)\>\s*\]\s*$(\r?\n)?)", RegexOptions.Multiline);
public static Regex VisualBasicAssemblyAttributeRegex { get; } = new(@"(\s*\<Assembly:\s*(?:.*)\>\s*$(\r?\n)?)", RegexOptions.Multiline);
public static Regex AssemblyVersionRegex { get; } = new(@"AssemblyVersion(Attribute)?\s*\(.*\)\s*", Options);
public static Regex AssemblyInfoVersionRegex { get; } = new(@"AssemblyInformationalVersion(Attribute)?\s*\(.*\)\s*", Options);
public static Regex AssemblyFileVersionRegex { get; } = new(@"AssemblyFileVersion(Attribute)?\s*\(.*\)\s*", Options);
public static Regex CsharpAssemblyAttributeRegex { get; } = new(@"(\s*\[\s*assembly:\s*(?:.*)\s*\]\s*$(\r?\n)?)", Options | RegexOptions.Multiline);
public static Regex FsharpAssemblyAttributeRegex { get; } = new(@"(\s*\[\s*\<assembly:\s*(?:.*)\>\s*\]\s*$(\r?\n)?)", Options | RegexOptions.Multiline);
public static Regex VisualBasicAssemblyAttributeRegex { get; } = new(@"(\s*\<Assembly:\s*(?:.*)\>\s*$(\r?\n)?)", Options | RegexOptions.Multiline);
}

internal static class VersionCalculation
Expand All @@ -159,33 +159,33 @@ internal static class VersionCalculation
[StringSyntax(StringSyntaxAttribute.Regex)]
public const string DefaultNoBumpPattern = @"\+semver:\s?(none|skip)";

public static Regex DefaultMajorRegex { get; } = new(DefaultMajorPattern, RegexOptions);
public static Regex DefaultMinorRegex { get; } = new(DefaultMinorPattern, RegexOptions);
public static Regex DefaultPatchRegex { get; } = new(DefaultPatchPattern, RegexOptions);
public static Regex DefaultNoBumpRegex { get; } = new(DefaultNoBumpPattern, RegexOptions);
public static Regex DefaultMajorRegex { get; } = new(DefaultMajorPattern, Options);
public static Regex DefaultMinorRegex { get; } = new(DefaultMinorPattern, Options);
public static Regex DefaultPatchRegex { get; } = new(DefaultPatchPattern, Options);
public static Regex DefaultNoBumpRegex { get; } = new(DefaultNoBumpPattern, Options);
}

internal static class SemanticVersion
{
// uses the git-semver spec https://github.com/semver/semver/blob/master/semver.md
public static Regex ParseStrictRegex { get; } = new(
@"^(?<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-]+)*))?$",
RegexOptions);
Options);

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

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

public static Regex FormatBuildMetaDataRegex { get; } = new("[^0-9A-Za-z-.]",
RegexOptions);
Options);

public static Regex ParsePreReleaseTagRegex { get; } = new(
@"(?<name>.*?)\.?(?<number>\d+)?$",
RegexOptions);
Options);
}

internal static class AssemblyVersion
Expand All @@ -196,14 +196,14 @@ internal static class CSharp
/\*(.*?)\*/ # Block comments: matches /* ... */
|//(.*?)\r?\n # Line comments: matches // ... followed by a newline
|""((\\[^\n]|[^""\n])*)"" # Strings: matches "" ... "" including escaped quotes",
RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace | RegexOptions);
RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace | Options);

public static Regex AttributeRegex { get; } = new(@"(?x) # IgnorePatternWhitespace
\[\s*assembly\s*:\s* # The [assembly: part
(System\s*\.\s*Reflection\s*\.\s*)? # The System.Reflection. part (optional)
Assembly(File|Informational)?Version # The attribute AssemblyVersion, AssemblyFileVersion, or AssemblyInformationalVersion
\s*\(\s*\)\s*\] # End brackets ()]",
RegexOptions.IgnorePatternWhitespace | RegexOptions);
RegexOptions.IgnorePatternWhitespace | Options);
}

internal static class FSharp
Expand All @@ -212,29 +212,29 @@ internal static class FSharp
/\*(.*?)\*/ # Block comments: matches /* ... */
|//(.*?)\r?\n # Line comments: matches // ... followed by a newline
|""((\\[^\n]|[^""\n])*)"" # Strings: matches "" ... "" including escaped quotes",
RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace | RegexOptions);
RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace | Options);

public static Regex AttributeRegex { get; } = new(@"(?x) # IgnorePatternWhitespace
\[\s*<\s*assembly\s*:\s* # The [<assembly: part
(System\s*\.\s*Reflection\s*\.\s*)? # The System.Reflection. part (optional)
Assembly(File|Informational)?Version # The attribute AssemblyVersion, AssemblyFileVersion, or AssemblyInformationalVersion
\s*\(\s*\)\s*>\s*\] # End brackets ()>]",
RegexOptions.IgnorePatternWhitespace | RegexOptions);
RegexOptions.IgnorePatternWhitespace | Options);
}

internal static class VisualBasic
{
public static Regex TriviaRegex { get; } = new(@"
'(.*?)\r?\n # Line comments: matches // ... followed by a newline
|""((\\[^\n]|[^""\n])*)"" # Strings: matches "" ... "" including escaped quotes",
RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace | RegexOptions);
RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace | Options);

public static Regex AttributeRegex { get; } = new(@"(?x) # IgnorePatternWhitespace
\<\s*Assembly\s*:\s* # The <Assembly: part
(System\s*\.\s*Reflection\s*\.\s*)? # The System.Reflection. part (optional)
Assembly(File|Informational)?Version # The attribute AssemblyVersion, AssemblyFileVersion, or AssemblyInformationalVersion
\s*\(\s*\)\s*\> # End brackets ()>",
RegexOptions.IgnorePatternWhitespace | RegexOptions);
RegexOptions.IgnorePatternWhitespace | Options);
}
}
}
5 changes: 3 additions & 2 deletions src/GitVersion.Core/Core/SourceBranchFinder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Text.RegularExpressions;
using GitVersion.Configuration;
using GitVersion.Core;
using GitVersion.Extensions;
using GitVersion.Git;

Expand Down Expand Up @@ -35,7 +36,7 @@ private static IEnumerable<Regex> GetSourceBranchRegexes(INamedReference branch,
var currentBranchConfig = configuration.GetBranchConfiguration(branch.Name);
if (currentBranchConfig.SourceBranches == null)
{
yield return new(".*");
yield return RegexPatterns.Cache.GetOrAdd(".*");
}
else
{
Expand All @@ -44,7 +45,7 @@ private static IEnumerable<Regex> GetSourceBranchRegexes(INamedReference branch,
{
var regex = branches[sourceBranch].RegularExpression;
if (regex != null)
yield return new(regex);
yield return RegexPatterns.Cache.GetOrAdd(regex);
}
}
}
Expand Down

0 comments on commit 19e2907

Please sign in to comment.