11using System . Diagnostics . CodeAnalysis ;
2- using System . Text . RegularExpressions ;
32using GitVersion . Extensions ;
43using GitVersion . Helpers ;
54
@@ -42,39 +41,33 @@ public static ReferenceName Parse(string canonicalName)
4241 throw new ArgumentException ( $ "The { nameof ( canonicalName ) } is not a Canonical name") ;
4342 }
4443
45- public static bool TryParse ( [ NotNullWhen ( true ) ] out ReferenceName ? value , string canonicalName )
46- {
47- value = null ;
48-
49- if ( IsPrefixedBy ( canonicalName , LocalBranchPrefix )
50- || IsPrefixedBy ( canonicalName , RemoteTrackingBranchPrefix )
51- || IsPrefixedBy ( canonicalName , TagPrefix )
52- || IsPrefixedBy ( canonicalName , PullRequestPrefixes ) )
53- {
54- value = new ( canonicalName ) ;
55- }
56-
57- return value is not null ;
58- }
59-
6044 public static ReferenceName FromBranchName ( string branchName )
61- {
62- if ( TryParse ( out ReferenceName ? value , branchName ) ) return value ;
63- return Parse ( LocalBranchPrefix + branchName ) ;
64- }
45+ => TryParse ( out ReferenceName ? value , branchName )
46+ ? value
47+ : Parse ( LocalBranchPrefix + branchName ) ;
6548
6649 public string Canonical { get ; }
50+
6751 public string Friendly { get ; }
52+
6853 public string WithoutOrigin { get ; }
54+
6955 public bool IsLocalBranch { get ; }
56+
7057 public bool IsRemoteBranch { get ; }
58+
7159 public bool IsTag { get ; }
60+
7261 public bool IsPullRequest { get ; }
7362
7463 public bool Equals ( ReferenceName ? other ) => equalityHelper . Equals ( this , other ) ;
64+
7565 public int CompareTo ( ReferenceName ? other ) => comparerHelper . Compare ( this , other ) ;
66+
7667 public override bool Equals ( object ? obj ) => Equals ( obj as ReferenceName ) ;
68+
7769 public override int GetHashCode ( ) => equalityHelper . GetHashCode ( this ) ;
70+
7871 public override string ToString ( ) => Friendly ;
7972
8073 public static bool operator == ( ReferenceName ? left , ReferenceName ? right )
@@ -86,38 +79,6 @@ public static ReferenceName FromBranchName(string branchName)
8679
8780 public static bool operator != ( ReferenceName ? left , ReferenceName ? right ) => ! ( left == right ) ;
8881
89- public bool TryGetSemanticVersion ( out ( SemanticVersion Value , string ? Name ) result ,
90- Regex versionPatternRegex ,
91- string ? tagPrefix ,
92- SemanticVersionFormat format )
93- {
94- result = default ;
95-
96- int length = 0 ;
97- foreach ( var branchPart in WithoutOrigin . Split ( GetBranchSeparator ( ) ) )
98- {
99- if ( string . IsNullOrEmpty ( branchPart ) ) return false ;
100-
101- var match = versionPatternRegex . NotNull ( ) . Match ( branchPart ) ;
102- if ( match . Success )
103- {
104- var versionPart = match . Groups [ "version" ] . Value ;
105- if ( SemanticVersion . TryParse ( versionPart , tagPrefix , out var semanticVersion , format ) )
106- {
107- length += versionPart . Length ;
108- var name = WithoutOrigin [ length ..] . Trim ( '-' ) ;
109- result = new ( semanticVersion , name . Length == 0 ? null : name ) ;
110- return true ;
111- }
112- }
113- length += branchPart . Length + 1 ;
114- }
115-
116- return false ;
117- }
118-
119- private char GetBranchSeparator ( ) => WithoutOrigin . Contains ( '/' ) || ! WithoutOrigin . Contains ( '-' ) ? '/' : '-' ;
120-
12182 public bool EquivalentTo ( string ? name ) =>
12283 Canonical . Equals ( name , StringComparison . OrdinalIgnoreCase )
12384 || Friendly . Equals ( name , StringComparison . OrdinalIgnoreCase )
@@ -143,9 +104,25 @@ private string RemoveOrigin()
143104 {
144105 return Friendly [ OriginPrefix . Length ..] ;
145106 }
107+
146108 return Friendly ;
147109 }
148110
111+ private static bool TryParse ( [ NotNullWhen ( true ) ] out ReferenceName ? value , string canonicalName )
112+ {
113+ value = null ;
114+
115+ if ( IsPrefixedBy ( canonicalName , LocalBranchPrefix )
116+ || IsPrefixedBy ( canonicalName , RemoteTrackingBranchPrefix )
117+ || IsPrefixedBy ( canonicalName , TagPrefix )
118+ || IsPrefixedBy ( canonicalName , PullRequestPrefixes ) )
119+ {
120+ value = new ( canonicalName ) ;
121+ }
122+
123+ return value is not null ;
124+ }
125+
149126 private static bool IsPrefixedBy ( string input , string prefix ) => input . StartsWith ( prefix , StringComparison . Ordinal ) ;
150127
151128 private static bool IsPrefixedBy ( string input , string [ ] prefixes ) => prefixes . Any ( prefix => IsPrefixedBy ( input , prefix ) ) ;
0 commit comments