|
1 | 1 | using GitVersion.Core; |
2 | 2 | using GitVersion.Extensions; |
3 | 3 | using GitVersion.Git; |
| 4 | +using SemanticVersionResult = (GitVersion.SemanticVersion Value, string? Name); |
4 | 5 |
|
5 | 6 | namespace GitVersion.Configuration; |
6 | 7 |
|
7 | 8 | public static class ReferenceNameExtensions |
8 | 9 | { |
9 | | - public static bool TryGetSemanticVersion(this ReferenceName source, EffectiveConfiguration configuration, out (SemanticVersion Value, string? Name) result) |
10 | | - => source.TryGetSemanticVersion(configuration.VersionInBranchPattern, configuration.TagPrefixPattern, configuration.SemanticVersionFormat, out result); |
11 | | - |
12 | | - public static bool TryGetSemanticVersion(this ReferenceName source, IGitVersionConfiguration configuration, out (SemanticVersion Value, string? Name) result) |
13 | | - => source.TryGetSemanticVersion(configuration.VersionInBranchPattern, configuration.TagPrefixPattern, configuration.SemanticVersionFormat, out result); |
14 | | - |
15 | | - private static bool TryGetSemanticVersion(this ReferenceName referenceName, |
16 | | - string? versionPatternPattern, |
17 | | - string? tagPrefix, |
18 | | - SemanticVersionFormat format, out (SemanticVersion Value, string? Name) result) |
| 10 | + extension(ReferenceName source) |
19 | 11 | { |
20 | | - var versionPatternRegex = RegexPatterns.Cache.GetOrAdd(GetVersionInBranchPattern(versionPatternPattern)); |
21 | | - result = default; |
| 12 | + public bool TryGetSemanticVersion(EffectiveConfiguration configuration, out SemanticVersionResult result) |
| 13 | + => source.TryGetSemanticVersion(configuration.VersionInBranchPattern, configuration.TagPrefixPattern, configuration.SemanticVersionFormat, out result); |
| 14 | + |
| 15 | + public bool TryGetSemanticVersion(IGitVersionConfiguration configuration, out SemanticVersionResult result) |
| 16 | + => source.TryGetSemanticVersion(configuration.VersionInBranchPattern, configuration.TagPrefixPattern, configuration.SemanticVersionFormat, out result); |
22 | 17 |
|
23 | | - var length = 0; |
24 | | - foreach (var branchPart in referenceName.WithoutOrigin.Split(GetBranchSeparator())) |
| 18 | + private bool TryGetSemanticVersion(string? versionPatternPattern, |
| 19 | + string? tagPrefix, |
| 20 | + SemanticVersionFormat format, out SemanticVersionResult result) |
25 | 21 | { |
26 | | - if (string.IsNullOrEmpty(branchPart)) return false; |
| 22 | + var versionPatternRegex = RegexPatterns.Cache.GetOrAdd(GetVersionInBranchPattern(versionPatternPattern)); |
| 23 | + result = default; |
27 | 24 |
|
28 | | - var match = versionPatternRegex.Match(branchPart); |
29 | | - if (match.Success) |
| 25 | + var length = 0; |
| 26 | + foreach (var branchPart in source.WithoutOrigin.Split(GetBranchSeparator())) |
30 | 27 | { |
31 | | - var versionPart = match.Groups["version"].Value; |
32 | | - if (SemanticVersion.TryParse(versionPart, tagPrefix, out var semanticVersion, format)) |
| 28 | + if (string.IsNullOrEmpty(branchPart)) return false; |
| 29 | + |
| 30 | + var match = versionPatternRegex.Match(branchPart); |
| 31 | + if (match.Success) |
33 | 32 | { |
34 | | - length += versionPart.Length; |
35 | | - var name = referenceName.WithoutOrigin[length..].Trim('-'); |
36 | | - result = new(semanticVersion, name.Length == 0 ? null : name); |
37 | | - return true; |
| 33 | + var versionPart = match.Groups["version"].Value; |
| 34 | + if (SemanticVersion.TryParse(versionPart, tagPrefix, out var semanticVersion, format)) |
| 35 | + { |
| 36 | + length += versionPart.Length; |
| 37 | + var name = source.WithoutOrigin[length..].Trim('-'); |
| 38 | + result = new(semanticVersion, name.Length == 0 ? null : name); |
| 39 | + return true; |
| 40 | + } |
38 | 41 | } |
39 | | - } |
40 | 42 |
|
41 | | - length += branchPart.Length + 1; |
42 | | - } |
| 43 | + length += branchPart.Length + 1; |
| 44 | + } |
43 | 45 |
|
44 | | - return false; |
| 46 | + return false; |
45 | 47 |
|
46 | | - char GetBranchSeparator() => referenceName.WithoutOrigin.Contains('/') || !referenceName.WithoutOrigin.Contains('-') ? '/' : '-'; |
| 48 | + char GetBranchSeparator() => source.WithoutOrigin.Contains('/') || !source.WithoutOrigin.Contains('-') ? '/' : '-'; |
47 | 49 |
|
48 | | - static string GetVersionInBranchPattern(string? versionInBranchPattern) |
49 | | - { |
50 | | - if (versionInBranchPattern.IsNullOrEmpty()) versionInBranchPattern = RegexPatterns.Configuration.DefaultVersionInBranchRegexPattern; |
51 | | - return $"^{versionInBranchPattern.TrimStart('^')}"; |
| 50 | + static string GetVersionInBranchPattern(string? versionInBranchPattern) |
| 51 | + { |
| 52 | + if (versionInBranchPattern.IsNullOrEmpty()) versionInBranchPattern = RegexPatterns.Configuration.DefaultVersionInBranchRegexPattern; |
| 53 | + return $"^{versionInBranchPattern.TrimStart('^')}"; |
| 54 | + } |
52 | 55 | } |
53 | 56 | } |
54 | 57 | } |
0 commit comments