@@ -1002,23 +1002,27 @@ const UPSTREAM_FORMAT =
10021002const byteOrder = ( left : string , right : string ) =>
10031003 Buffer . compare ( Buffer . from ( left ) , Buffer . from ( right ) ) ;
10041004
1005- /** Every ref below `refs/heads` or `refs/remotes`, loose and packed, that resolves to an object. */
1006- async function listBranchRefs ( repo : Repository , namespace : string ) : Promise < ReadonlyArray < string > > {
1005+ /**
1006+ * Every ref whose name starts with `prefix`, loose and packed, that resolves to
1007+ * an object: the refs git iterates for a pattern without wildcards.
1008+ */
1009+ async function listBranchRefs ( repo : Repository , prefix : string ) : Promise < ReadonlyArray < string > > {
10071010 const packed = await readPackedRefs ( repo . commonDir ) ;
10081011 const refs = new Set < string > ( ) ;
1009- for ( const ref of packed . keys ( ) ) if ( ref . startsWith ( ` ${ namespace } /` ) ) refs . add ( ref ) ;
1012+ for ( const ref of packed . keys ( ) ) if ( ref . startsWith ( prefix ) ) refs . add ( ref ) ;
10101013
1011- const walk = async ( ref : string ) : Promise < void > => {
1012- const dir = NodePath . join ( repo . commonDir , ...ref . split ( "/" ) ) ;
1014+ const visit = async ( parent : string , keep : ( name : string ) => boolean ) : Promise < void > => {
1015+ const dir = NodePath . join ( repo . commonDir , ...parent . split ( "/" ) ) ;
10131016 await assertNoSymlinks ( dir ) ;
10141017 const entries = await NodeFSP . readdir ( dir , { withFileTypes : true } ) . catch (
10151018 ( error : NodeJS . ErrnoException ) =>
10161019 error . code === "ENOENT" ? [ ] : unsure ( "unreadable refs directory" ) ,
10171020 ) ;
10181021 for ( const entry of entries ) {
1019- const child = `${ ref } /${ entry . name } ` ;
1022+ if ( ! keep ( entry . name ) ) continue ;
1023+ const child = `${ parent } /${ entry . name } ` ;
10201024 if ( entry . isDirectory ( ) ) {
1021- await walk ( child ) ;
1025+ await visit ( child , ( ) => true ) ;
10221026 continue ;
10231027 }
10241028 // Lock files, odd names and non-files are git's to judge.
@@ -1039,21 +1043,22 @@ async function listBranchRefs(repo: Repository, namespace: string): Promise<Read
10391043 refs . add ( child ) ;
10401044 }
10411045 } ;
1042- await walk ( namespace ) ;
1046+ const slash = prefix . lastIndexOf ( "/" ) ;
1047+ const base = prefix . slice ( slash + 1 ) ;
1048+ await visit ( prefix . slice ( 0 , slash ) , ( name ) => name . startsWith ( base ) ) ;
10431049 return [ ...refs ] ;
10441050}
10451051
1046- /** git's pattern rule: the whole ref, a leading directory of it, or a glob where `*` stays within one level. */
1052+ /**
1053+ * git's pattern rule for a pattern without wildcards: the whole ref or a leading
1054+ * directory of it. Wildcards make git walk the whole namespace, and so does a
1055+ * pattern naming no single remote; those are git's to answer.
1056+ */
10471057function refPatternMatcher ( pattern : string ) : ( ref : string ) => boolean {
1048- if ( ! isSafeRefName ( pattern . replaceAll ( "*" , "x" ) ) || pattern . includes ( "**" ) ) unsure ( "ref pattern" ) ;
1049- if ( ! pattern . includes ( "*" ) ) return ( ref ) => ref === pattern || ref . startsWith ( `${ pattern } /` ) ;
1050- const glob = new RegExp (
1051- `^${ pattern
1052- . split ( "*" )
1053- . map ( ( part ) => part . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, "\\$&" ) )
1054- . join ( "[^/]*" ) } $`,
1055- ) ;
1056- return ( ref ) => glob . test ( ref ) ;
1058+ if ( ! isSafeRefName ( pattern ) || pattern . includes ( "*" ) ) unsure ( "ref pattern" ) ;
1059+ if ( pattern . startsWith ( "refs/remotes" ) && pattern . split ( "/" ) . length < 4 )
1060+ unsure ( "every remote-tracking ref" ) ;
1061+ return ( ref ) => ref === pattern || ref . startsWith ( `${ pattern } /` ) ;
10571062}
10581063
10591064/** Tracking ref for `branch.<name>.merge` under the remote's fetch refspecs, or `null` when none maps it. */
@@ -1168,17 +1173,12 @@ async function answerForEachRef(repo: Repository, args: ReadonlyArray<string>) {
11681173 if ( ( format !== REFNAME_FORMAT && format !== UPSTREAM_FORMAT ) || patterns . length === 0 )
11691174 unsure ( "for-each-ref arguments" ) ;
11701175
1171- const namespaces = new Set < string > ( ) ;
11721176 for ( const pattern of patterns ) {
1173- const namespace = / ^ ( r e f s \/ (?: h e a d s | r e m o t e s ) ) (?: \/ | $ ) / . exec ( pattern ) ?. [ 1 ] ;
1174- namespaces . add ( namespace ?? unsure ( "for-each-ref outside branches" ) ) ;
1177+ if ( ! / ^ r e f s \/ (?: h e a d s | r e m o t e s ) (?: \/ | $ ) / . test ( pattern ) ) unsure ( "for-each-ref outside branches" ) ;
11751178 }
11761179 const matchers = patterns . map ( refPatternMatcher ) ;
1177- const listed = await Promise . all (
1178- [ ...namespaces ] . map ( ( namespace ) => listBranchRefs ( repo , namespace ) ) ,
1179- ) ;
1180- const refs = listed
1181- . flat ( )
1180+ const listed = await Promise . all ( patterns . map ( ( pattern ) => listBranchRefs ( repo , pattern ) ) ) ;
1181+ const refs = [ ...new Set ( listed . flat ( ) ) ]
11821182 . filter ( ( ref ) => matchers . some ( ( matches ) => matches ( ref ) ) )
11831183 . toSorted ( byteOrder )
11841184 . slice ( 0 , count ) ;
0 commit comments