-
Notifications
You must be signed in to change notification settings - Fork 653
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract TryGetSemanticVersion method into extensions class
- Loading branch information
Showing
4 changed files
with
51 additions
and
37 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
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
45 changes: 45 additions & 0 deletions
45
src/GitVersion.Core/VersionCalculation/SemanticVersioning/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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Diagnostics.Contracts; | ||
using System.Text.RegularExpressions; | ||
using GitVersion.Extensions; | ||
|
||
namespace GitVersion; | ||
|
||
public static class ReferenceNameExtensions | ||
{ | ||
public static bool TryGetSemanticVersion(this ReferenceName source, | ||
[NotNullWhen(true)] out (SemanticVersion Value, string? Name) result, | ||
Regex versionPatternRegex, string? labelPrefix, SemanticVersionFormat format) | ||
{ | ||
source.NotNull(); | ||
|
||
result = default; | ||
|
||
Contract.Assume(versionPatternRegex.ToString().StartsWith("^")); | ||
|
||
int length = 0; | ||
foreach (var branchPart in source.WithoutOrigin.Split(GetBranchSeparator(source))) | ||
{ | ||
if (branchPart.IsNullOrEmpty()) return false; | ||
|
||
var match = versionPatternRegex.NotNull().Match(branchPart); | ||
if (match.Success) | ||
{ | ||
var versionPart = match.Groups["version"].Value; | ||
if (SemanticVersion.TryParse(versionPart, labelPrefix, out var semanticVersion, format)) | ||
{ | ||
length += versionPart.Length; | ||
var name = source.WithoutOrigin[length..].Trim('-'); | ||
result = new(semanticVersion, name.IsEmpty() ? null : name); | ||
return true; | ||
} | ||
} | ||
length += branchPart.Length + 1; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private static char GetBranchSeparator(ReferenceName source) | ||
=> source.WithoutOrigin.Contains('/') || !source.WithoutOrigin.Contains('-') ? '/' : '-'; | ||
} |