forked from GitTools/GitVersion
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move TryGetSemanticVersion out of ReferenceName
- Loading branch information
Showing
7 changed files
with
72 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 35 additions & 4 deletions
39
src/GitVersion.Core/Configuration/ReferenceNameExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,45 @@ | ||
using System.Text.RegularExpressions; | ||
using GitVersion.Extensions; | ||
using GitVersion.Git; | ||
|
||
namespace GitVersion.Configuration; | ||
|
||
public static class ReferenceNameExtensions | ||
{ | ||
public static bool TryGetSemanticVersion( | ||
this ReferenceName source, out (SemanticVersion Value, string? Name) result, IGitVersionConfiguration configuration) | ||
=> source.TryGetSemanticVersion(out result, configuration.VersionInBranchRegex, configuration.TagPrefix, configuration.SemanticVersionFormat); | ||
public static bool TryGetSemanticVersion(this ReferenceName referenceName, out (SemanticVersion Value, string? Name) result, | ||
Regex versionPatternRegex, | ||
string? tagPrefix, | ||
SemanticVersionFormat format) | ||
{ | ||
result = default; | ||
|
||
int length = 0; | ||
foreach (var branchPart in referenceName.WithoutOrigin.Split(GetBranchSeparator())) | ||
{ | ||
if (string.IsNullOrEmpty(branchPart)) return false; | ||
|
||
var match = versionPatternRegex.NotNull().Match(branchPart); | ||
if (match.Success) | ||
{ | ||
var versionPart = match.Groups["version"].Value; | ||
if (SemanticVersion.TryParse(versionPart, tagPrefix, out var semanticVersion, format)) | ||
{ | ||
length += versionPart.Length; | ||
var name = referenceName.WithoutOrigin[length..].Trim('-'); | ||
result = new(semanticVersion, name.Length == 0 ? null : name); | ||
return true; | ||
} | ||
} | ||
|
||
length += branchPart.Length + 1; | ||
} | ||
|
||
return false; | ||
|
||
char GetBranchSeparator() => referenceName.WithoutOrigin.Contains('/') || !referenceName.WithoutOrigin.Contains('-') ? '/' : '-'; | ||
} | ||
|
||
public static bool TryGetSemanticVersion( | ||
this ReferenceName source, out (SemanticVersion Value, string? Name) result, EffectiveConfiguration configuration) | ||
this ReferenceName source, out (SemanticVersion Value, string? Name) result, EffectiveConfiguration configuration) | ||
=> source.TryGetSemanticVersion(out result, configuration.VersionInBranchRegex, configuration.TagPrefix, configuration.SemanticVersionFormat); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
#nullable enable | ||
static GitVersion.Configuration.ReferenceNameExtensions.TryGetSemanticVersion(this GitVersion.Git.ReferenceName! referenceName, out (GitVersion.SemanticVersion! Value, string? Name) result, System.Text.RegularExpressions.Regex! versionPatternRegex, string? tagPrefix, GitVersion.SemanticVersionFormat format) -> bool | ||
static GitVersion.Extensions.DictionaryExtensions.GetOrAdd(this System.Collections.Concurrent.ConcurrentDictionary<string!, System.Text.RegularExpressions.Regex!>! dict, string! pattern) -> System.Text.RegularExpressions.Regex! | ||
static GitVersion.Extensions.DictionaryExtensions.GetOrAdd<TKey, TValue>(this System.Collections.Generic.Dictionary<TKey, TValue>! dict, TKey key, System.Func<TValue>! getValue) -> TValue | ||
static GitVersion.Extensions.StringExtensions.RegexReplace(this string! input, string! pattern, string! replace) -> string! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters