@@ -11,36 +11,37 @@ public class ReferenceName : IEquatable<ReferenceName?>, IComparable<ReferenceNa
11
11
private const string LocalBranchPrefix = "refs/heads/" ;
12
12
private const string RemoteTrackingBranchPrefix = "refs/remotes/" ;
13
13
private const string TagPrefix = "refs/tags/" ;
14
- private const string PullRequestPrefix1 = "refs/pull/" ;
15
- private const string PullRequestPrefix2 = "refs/pull-requests/" ;
16
- private const string RemotePullRequestPrefix1 = "refs/remotes/pull/" ;
17
- private const string RemotePullRequestPrefix2 = "refs/remotes/pull-requests/" ;
14
+ private static readonly string [ ] PullRequestPrefixes =
15
+ {
16
+ "refs/pull/" ,
17
+ "refs/pull-requests/" ,
18
+ "refs/remotes/pull/" ,
19
+ "refs/remotes/pull-requests/"
20
+ } ;
18
21
19
22
public ReferenceName ( string canonical )
20
23
{
21
24
Canonical = canonical . NotNull ( ) ;
22
- Friendly = Shorten ( ) ;
23
- WithoutRemote = RemoveRemote ( ) ;
24
25
25
26
IsBranch = IsPrefixedBy ( Canonical , LocalBranchPrefix ) ;
26
27
IsRemoteBranch = IsPrefixedBy ( Canonical , RemoteTrackingBranchPrefix ) ;
27
28
IsTag = IsPrefixedBy ( Canonical , TagPrefix ) ;
28
- IsPullRequest = IsPrefixedBy ( Canonical , PullRequestPrefix1 )
29
- || IsPrefixedBy ( Canonical , PullRequestPrefix2 )
30
- || IsPrefixedBy ( Canonical , RemotePullRequestPrefix1 )
31
- || IsPrefixedBy ( Canonical , RemotePullRequestPrefix2 ) ;
29
+ IsPullRequest = IsPrefixedBy ( Canonical , PullRequestPrefixes ) ;
30
+
31
+ Friendly = Shorten ( ) ;
32
+ WithoutRemote = RemoveRemote ( ) ;
32
33
}
33
34
34
35
public static ReferenceName Parse ( string canonicalName )
35
36
{
36
37
if ( IsPrefixedBy ( canonicalName , LocalBranchPrefix )
37
38
|| IsPrefixedBy ( canonicalName , RemoteTrackingBranchPrefix )
38
39
|| IsPrefixedBy ( canonicalName , TagPrefix )
39
- || IsPrefixedBy ( canonicalName , PullRequestPrefix1 )
40
- || IsPrefixedBy ( canonicalName , PullRequestPrefix2 ) )
40
+ || IsPrefixedBy ( canonicalName , PullRequestPrefixes ) )
41
41
{
42
42
return new ReferenceName ( canonicalName ) ;
43
43
}
44
+
44
45
throw new ArgumentException ( $ "The { nameof ( canonicalName ) } is not a Canonical name") ;
45
46
}
46
47
@@ -67,30 +68,30 @@ public bool EquivalentTo(string? name) =>
67
68
68
69
private string Shorten ( )
69
70
{
70
- if ( IsPrefixedBy ( Canonical , LocalBranchPrefix ) )
71
+ if ( IsBranch )
71
72
return Canonical . Substring ( LocalBranchPrefix . Length ) ;
72
- if ( IsPrefixedBy ( Canonical , RemoteTrackingBranchPrefix ) )
73
+
74
+ if ( IsRemoteBranch )
73
75
return Canonical . Substring ( RemoteTrackingBranchPrefix . Length ) ;
74
- if ( IsPrefixedBy ( Canonical , TagPrefix ) )
76
+
77
+ if ( IsTag )
75
78
return Canonical . Substring ( TagPrefix . Length ) ;
79
+
76
80
return Canonical ;
77
81
}
78
82
79
83
private string RemoveRemote ( )
80
84
{
81
- var isRemote = IsPrefixedBy ( Canonical , RemoteTrackingBranchPrefix ) ;
82
-
83
- if ( isRemote )
85
+ if ( IsRemoteBranch )
84
86
{
85
- var isPullRequest = IsPrefixedBy ( Canonical , RemotePullRequestPrefix1 )
86
- || IsPrefixedBy ( Canonical , RemotePullRequestPrefix2 ) ;
87
-
88
- if ( ! isPullRequest )
87
+ if ( ! IsPullRequest )
89
88
return Friendly . Substring ( Friendly . IndexOf ( "/" , StringComparison . Ordinal ) + 1 ) ;
90
89
}
91
90
92
91
return Friendly ;
93
92
}
94
93
95
94
private static bool IsPrefixedBy ( string input , string prefix ) => input . StartsWith ( prefix , StringComparison . Ordinal ) ;
95
+
96
+ private static bool IsPrefixedBy ( string input , string [ ] prefixes ) => prefixes . Any ( prefix => IsPrefixedBy ( input , prefix ) ) ;
96
97
}
0 commit comments