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