@@ -23,7 +23,12 @@ public class ReferenceName : IEquatable<ReferenceName?>, IComparable<ReferenceNa
2323 "refs/remotes/pull-requests/"
2424 ] ;
2525
26- private const string GitLabMergeRequestRefPrefix = "refs/merge-requests/" ;
26+ /// <summary>
27+ /// The sole <see cref="PullRequestPrefixes" /> entry for <c>refs/merge-requests/<id>/head|merge</c>.
28+ /// Adding another prefix that also contains <c>/merge-requests/</c> will fail at type initialization.
29+ /// </summary>
30+ private static readonly string mergeRequestsRefPrefix = PullRequestPrefixes . Single (
31+ p => p . Contains ( "/merge-requests/" , StringComparison . Ordinal ) ) ;
2732
2833 public ReferenceName ( string canonical )
2934 {
@@ -88,23 +93,27 @@ public bool EquivalentTo(string? name) =>
8893 || WithoutOrigin . Equals ( name , StringComparison . OrdinalIgnoreCase ) ;
8994
9095 /// <summary>
91- /// Detects GitLab MR ref ( refs/merge-requests/<iid >/head or /merge) and extracts IID for pull-requests config .
96+ /// Parses canonical refs under refs/merge-requests/<id >/head or /merge (convention used by some Git hosts ) and extracts the merge-request id .
9297 /// </summary>
93- public static bool TryParseGitLabMergeRequestRef ( string ? canonicalRef , out int iid )
98+ public static bool TryParseMergeRequestsRef ( string ? canonicalRef , out int mergeRequestId )
9499 {
95- iid = 0 ;
96- if ( string . IsNullOrEmpty ( canonicalRef ) || ! canonicalRef . StartsWith ( GitLabMergeRequestRefPrefix , StringComparison . Ordinal ) )
100+ mergeRequestId = 0 ;
101+ if ( string . IsNullOrEmpty ( canonicalRef ) || ! canonicalRef . StartsWith ( mergeRequestsRefPrefix , StringComparison . Ordinal ) )
97102 return false ;
98- var after = canonicalRef . Substring ( GitLabMergeRequestRefPrefix . Length ) ;
103+ var after = canonicalRef . Substring ( mergeRequestsRefPrefix . Length ) ;
99104 var slash = after . IndexOf ( '/' ) ;
100105 if ( slash <= 0 || slash >= after . Length - 1 ) return false ;
101106 var suffix = after [ ( slash + 1 ) ..] ;
102107 if ( ! suffix . Equals ( "head" , StringComparison . OrdinalIgnoreCase ) && ! suffix . Equals ( "merge" , StringComparison . OrdinalIgnoreCase ) )
103108 return false ;
104- return int . TryParse ( after . Substring ( 0 , slash ) , System . Globalization . NumberStyles . None , System . Globalization . CultureInfo . InvariantCulture , out iid )
105- && iid > 0 ;
109+ return int . TryParse ( after . Substring ( 0 , slash ) , System . Globalization . NumberStyles . None , System . Globalization . CultureInfo . InvariantCulture , out mergeRequestId ) ;
106110 }
107111
112+ /// <summary>
113+ /// Returns the branch-style name pull-requests/<id> for default pull-request configuration matching.
114+ /// </summary>
115+ public static string MergeRequestsRefFriendlyName ( int mergeRequestId ) => $ "pull-requests/{ mergeRequestId } ";
116+
108117 private string Shorten ( )
109118 {
110119 if ( IsLocalBranch )
@@ -116,8 +125,8 @@ private string Shorten()
116125 if ( IsTag )
117126 return Canonical [ TagPrefix . Length ..] ;
118127
119- if ( TryParseGitLabMergeRequestRef ( Canonical , out var iid ) )
120- return $ "pull-requests/ { iid } " ;
128+ if ( TryParseMergeRequestsRef ( Canonical , out var mergeRequestId ) )
129+ return MergeRequestsRefFriendlyName ( mergeRequestId ) ;
121130
122131 return Canonical ;
123132 }
0 commit comments