@@ -92,6 +92,7 @@ export interface CacheableAutolinkReference extends AutolinkReference {
9292 messageHtmlRegex ?: RegExp ;
9393 messageMarkdownRegex ?: RegExp ;
9494 messageRegex ?: RegExp ;
95+ branchNameRegex ?: RegExp ;
9596}
9697
9798export interface DynamicAutolinkReference {
@@ -155,6 +156,11 @@ export class Autolinks implements Disposable {
155156 }
156157 }
157158
159+ async getAutolinks (
160+ branchName : string ,
161+ remote ?: GitRemote ,
162+ options ?: { isBranchName : true } ,
163+ ) : Promise < Map < string , Autolink > > ;
158164 async getAutolinks ( message : string , remote ?: GitRemote ) : Promise < Map < string , Autolink > > ;
159165 async getAutolinks (
160166 message : string ,
@@ -169,9 +175,9 @@ export class Autolinks implements Disposable {
169175 } ,
170176 } )
171177 async getAutolinks (
172- message : string ,
178+ messageOrBranchName : string ,
173179 remote ?: GitRemote ,
174- options ?: { excludeCustom ?: boolean } ,
180+ options ?: { excludeCustom ?: boolean ; isBranchName ?: boolean } ,
175181 ) : Promise < Map < string , Autolink > > {
176182 const refsets : [
177183 ProviderReference | undefined ,
@@ -212,26 +218,52 @@ export class Autolinks implements Disposable {
212218
213219 const autolinks = new Map < string , Autolink > ( ) ;
214220
221+ const matchRef = ( ref : RequireSome < CacheableAutolinkReference , 'messageRegex' | 'branchNameRegex' > ) =>
222+ options ?. isBranchName
223+ ? ref . branchNameRegex . exec ( messageOrBranchName )
224+ : ref . messageRegex . exec ( messageOrBranchName ) ;
225+
215226 let match ;
216227 let num ;
217228 for ( const [ provider , refs ] of refsets ) {
218229 for ( const ref of refs ) {
219230 if ( ! isCacheable ( ref ) ) {
220231 if ( isDynamic ( ref ) ) {
221- ref . parse ( message , autolinks ) ;
232+ ref . parse ( messageOrBranchName , autolinks ) ;
222233 }
223234 continue ;
224235 }
225236
237+ if ( ref . referenceType ) {
238+ if ( ref . referenceType !== 'branchName' && options ?. isBranchName ) {
239+ continue ;
240+ }
241+ if ( ref . referenceType !== 'message' && ! options ?. isBranchName ) {
242+ continue ;
243+ }
244+ }
245+
226246 ensureCachedRegex ( ref , 'plaintext' ) ;
227247
228248 do {
229- match = ref . messageRegex . exec ( message ) ;
230- if ( match == null ) break ;
231-
232- [ , , , num ] = match ;
249+ match = matchRef ( ref ) ;
250+ if ( ! match ?. groups ) break ;
251+
252+ num = match . groups . issueKeyNumber ;
253+ let key = num ;
254+ if ( autolinks . has ( key ) ) {
255+ const prevAutolink = autolinks . get ( key ) ! ;
256+ if ( ! ref . prefix ) {
257+ continue ;
258+ } else if ( ! prevAutolink . prefix && ref . prefix ) {
259+ /** override */
260+ } else {
261+ // add more autolinks
262+ key = ref . prefix + num ;
263+ }
264+ }
233265
234- autolinks . set ( num , {
266+ autolinks . set ( key , {
235267 provider : provider ,
236268 id : num ,
237269 prefix : ref . prefix ,
@@ -615,27 +647,30 @@ function ensureCachedRegex(
615647function ensureCachedRegex (
616648 ref : CacheableAutolinkReference ,
617649 outputFormat : 'plaintext' ,
618- ) : asserts ref is RequireSome < CacheableAutolinkReference , 'messageRegex' > ;
650+ ) : asserts ref is RequireSome < CacheableAutolinkReference , 'messageRegex' | 'branchNameRegex' > ;
619651function ensureCachedRegex ( ref : CacheableAutolinkReference , outputFormat : 'html' | 'markdown' | 'plaintext' ) {
620652 // Regexes matches the ref prefix followed by a token (e.g. #1234)
621653 if ( outputFormat === 'markdown' && ref . messageMarkdownRegex == null ) {
622654 // Extra `\\\\` in `\\\\\\[` is because the markdown is escaped
623655 ref . messageMarkdownRegex = new RegExp (
624- `(^|\\s|\\(|\\[|\\{)(${ escapeRegex ( encodeHtmlWeak ( escapeMarkdown ( ref . prefix ) ) ) } ( ${
625- ref . alphanumeric ? '\\w' : '\\d'
626- } +))\\b`,
656+ `(^|\\s|\\(|\\[|\\{)(?<issueKeyNumber> ${ escapeRegex (
657+ encodeHtmlWeak ( escapeMarkdown ( ref . prefix ) ) ,
658+ ) } (?<issueKeyNumber> ${ ref . alphanumeric ? '\\w' : '\\d' } +))\\b`,
627659 ref . ignoreCase ? 'gi' : 'g' ,
628660 ) ;
629661 } else if ( outputFormat === 'html' && ref . messageHtmlRegex == null ) {
630662 ref . messageHtmlRegex = new RegExp (
631- `(^|\\s|\\(|\\[|\\{)(${ escapeRegex ( encodeHtmlWeak ( ref . prefix ) ) } (${ ref . alphanumeric ? '\\w' : '\\d' } +))\\b` ,
663+ `(^|\\s|\\(|\\[|\\{)(${ escapeRegex ( encodeHtmlWeak ( ref . prefix ) ) } (?<issueKeyNumber>${
664+ ref . alphanumeric ? '\\w' : '\\d'
665+ } +))\\b`,
632666 ref . ignoreCase ? 'gi' : 'g' ,
633667 ) ;
634668 } else if ( ref . messageRegex == null ) {
635669 ref . messageRegex = new RegExp (
636- `(^|\\s|\\(|\\[|\\{)(${ escapeRegex ( ref . prefix ) } (${ ref . alphanumeric ? '\\w' : '\\d' } +))\\b` ,
670+ `(^|\\s|\\(|\\[|\\{)(${ escapeRegex ( ref . prefix ) } (?<issueKeyNumber> ${ ref . alphanumeric ? '\\w' : '\\d' } +))\\b` ,
637671 ref . ignoreCase ? 'gi' : 'g' ,
638672 ) ;
673+ ref . branchNameRegex = new RegExp ( `(^|\\-|_)(?<prefix>${ ref . prefix } )(?<issueKeyNumber>\\d+)` , 'gi' ) ;
639674 }
640675
641676 return true ;
0 commit comments