@@ -25,7 +25,64 @@ const WILDCARD_CHARS = /[*?[\\]/
2525const backslashIsSeparator = ( ) => path . sep === '\\'
2626
2727function escapeRegex ( ch : string ) : string {
28- return ch . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ / - ] / g, '\\$&' )
28+ return ch . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ / ] / g, '\\$&' )
29+ }
30+
31+ // Escapes that keep their regex meaning in a bracket expression, like in Python
32+ const CLASS_ESCAPES = new Set ( 'dDsSwWfnrtv' )
33+ const CLASS_CONTROL_ESCAPES : Record < string , string > = { a : '\\x07' , b : '\\x08' }
34+ const CLASS_HEX_ESCAPE_LENGTHS : Record < string , number > = { x : 2 , u : 4 , U : 8 }
35+ // Characters that must stay escaped in a bracket expression with the `u` flag
36+ const CLASS_SYNTAX_CHARS = new Set ( '^$\\.*+?()[]{}|/-' )
37+
38+ // Make a bracket expression body valid with the regex `u` flag
39+ function translateClass ( body : string , backslashIsEscape : boolean ) : string {
40+ let cls = ''
41+ let i = 0
42+ while ( i < body . length ) {
43+ const ch = body [ i ++ ]
44+ if ( ch !== '\\' ) {
45+ cls += ch
46+ } else if ( ! backslashIsEscape || i >= body . length ) {
47+ cls += '\\\\'
48+ } else {
49+ const next = body [ i ++ ]
50+ if ( CLASS_ESCAPES . has ( next ) ) {
51+ cls += '\\' + next
52+ } else if ( next in CLASS_CONTROL_ESCAPES ) {
53+ cls += CLASS_CONTROL_ESCAPES [ next ]
54+ } else if ( next in CLASS_HEX_ESCAPE_LENGTHS ) {
55+ const length = CLASS_HEX_ESCAPE_LENGTHS [ next ]
56+ const hex = body . slice ( i , i + length )
57+ if ( hex . length < length || ! / ^ [ 0 - 9 a - f A - F ] + $ / . test ( hex ) ) {
58+ throw new Error ( `incomplete escape \\${ next } ${ hex } ` )
59+ }
60+ const code = parseInt ( hex , 16 )
61+ if ( code > 0x10ffff ) {
62+ throw new Error ( `bad escape \\${ next } ${ hex } ` )
63+ }
64+ cls += `\\u{${ code . toString ( 16 ) } }`
65+ i += length
66+ } else if ( / [ 0 - 7 ] / . test ( next ) ) {
67+ let octal = next
68+ while ( octal . length < 3 && / [ 0 - 7 ] / . test ( body [ i ] ?? '' ) ) {
69+ octal += body [ i ++ ]
70+ }
71+ const code = parseInt ( octal , 8 )
72+ if ( code > 0o377 ) {
73+ throw new Error (
74+ `octal escape value \\${ octal } outside of range 0-0o377`
75+ )
76+ }
77+ cls += `\\u{${ code . toString ( 16 ) } }`
78+ } else if ( / [ A - Z a - z 0 - 9 ] / . test ( next ) ) {
79+ throw new Error ( `bad escape \\${ next } ` )
80+ } else {
81+ cls += CLASS_SYNTAX_CHARS . has ( next ) ? '\\' + next : next
82+ }
83+ }
84+ }
85+ return cls
2986}
3087
3188// Equivalent of Go's filepath.Clean followed by filepath.ToSlash
@@ -36,12 +93,25 @@ function clean(pattern: string): string {
3693 return path . posix . normalize ( pattern ) . replace ( / ( .) \/ $ / , '$1' )
3794}
3895
39- function compile ( pattern : string ) : RegExp {
96+ type Matcher = ( p : string ) => boolean
97+
98+ enum MatchType {
99+ Exact = 'exact' ,
100+ Prefix = 'prefix' ,
101+ Suffix = 'suffix' ,
102+ Regex = 'regex' ,
103+ }
104+
105+ // Like moby, use plain string checks for patterns without wildcards and only
106+ // fall back to a regex otherwise
107+ function compile ( pattern : string ) : Matcher {
40108 let regex = '^'
109+ let matchType = MatchType . Exact
41110 const n = pattern . length
42111 const backslashIsEscape = ! backslashIsSeparator ( )
43112 let i = 0
44113 while ( i < n ) {
114+ const first = i === 0
45115 const ch = pattern [ i ++ ]
46116 if ( ch === '*' ) {
47117 if ( pattern [ i ] === '*' ) {
@@ -50,14 +120,26 @@ function compile(pattern: string): RegExp {
50120 if ( pattern [ i ] === '/' ) {
51121 i ++
52122 }
53- regex += i >= n ? '.*' : '(.*/)?'
123+ if ( i >= n ) {
124+ regex += '.*'
125+ matchType =
126+ matchType === MatchType . Exact ? MatchType . Prefix : MatchType . Regex
127+ } else {
128+ regex += '(.*/)?'
129+ matchType = MatchType . Regex
130+ }
131+ if ( first ) {
132+ matchType = MatchType . Suffix
133+ }
54134 } else {
55135 regex += '[^/]*'
136+ matchType = MatchType . Regex
56137 }
57138 } else if ( ch === '?' ) {
58139 regex += '[^/]'
140+ matchType = MatchType . Regex
59141 } else if ( ch === '[' ) {
60- // Copy a bracket expression as is , a leading "^" negates it
142+ // Copy a bracket expression, a leading "^" negates it
61143 let j = i
62144 if ( pattern [ j ] === '^' ) {
63145 j ++
@@ -68,28 +150,54 @@ function compile(pattern: string): RegExp {
68150 }
69151 j ++
70152 }
71- regex += pattern . slice ( i - 1 , j + 1 )
153+ regex +=
154+ '[' +
155+ translateClass ( pattern . slice ( i , Math . min ( j , n ) ) , backslashIsEscape ) +
156+ ( j < n ? ']' : '' )
72157 i = j + 1
158+ matchType = MatchType . Regex
159+ } else if ( ch === ']' ) {
160+ regex += '\\]'
161+ matchType = MatchType . Regex
73162 } else if ( LITERAL_REGEX_CHARS . has ( ch ) ) {
74163 regex += '\\' + ch
75164 } else if ( ch === '\\' && backslashIsEscape ) {
76165 // Escape the next character
77166 if ( i < n ) {
78167 regex += escapeRegex ( pattern [ i ++ ] )
168+ matchType = MatchType . Regex
79169 } else {
80170 regex += '\\\\'
81171 }
82172 } else {
83173 regex += ch
84174 }
85175 }
86- return new RegExp ( regex + '$' )
176+
177+ switch ( matchType ) {
178+ case MatchType . Exact :
179+ return ( p ) => p === pattern
180+ case MatchType . Prefix : {
181+ const prefix = pattern . slice ( 0 , - 2 )
182+ return ( p ) => p . startsWith ( prefix )
183+ }
184+ case MatchType . Suffix : {
185+ const suffix = pattern . slice ( 2 )
186+ // "**/foo" also matches "foo"
187+ return ( p ) =>
188+ p . endsWith ( suffix ) || ( suffix [ 0 ] === '/' && p === suffix . slice ( 1 ) )
189+ }
190+ case MatchType . Regex : {
191+ const re = new RegExp ( regex + '$' , 'u' )
192+ return ( p ) => re . test ( p )
193+ }
194+ }
87195}
88196
89197interface Pattern {
90198 exclusion : boolean
91199 dirs : string [ ]
92- regex : RegExp
200+ match : Matcher
93201}
94202
95203/**
@@ -119,43 +227,48 @@ export class PatternMatcher {
119227 if ( pattern . length > 1 && pattern . startsWith ( '/' ) ) {
120228 pattern = pattern . slice ( 1 )
121229 }
122- let regex : RegExp
230+ let match : Matcher
123231 try {
124- regex = compile ( pattern )
232+ match = compile ( pattern )
125233 } catch ( err ) {
126234 throw new TemplateError (
127235 `Invalid ignore pattern '${ original } ': ${ ( err as Error ) . message } `
128236 )
129237 }
130- this . patterns . push ( { exclusion, dirs : pattern . split ( '/' ) , regex } )
238+ this . patterns . push ( { exclusion, dirs : pattern . split ( '/' ) , match } )
131239 }
132240 }
133241
134242 /**
135- * Whether the path or one of its parent directories is excluded.
243+ * Whether the path is excluded. Like BuildKit, the patterns are evaluated
244+ * on each parent directory first, and a pattern that matched a parent
245+ * directory also matches the paths under it.
136246 *
137247 * @param p Slash-separated path relative to the context root
138248 * @returns True if the path is excluded
139249 */
140250 matches ( p : string ) : boolean {
141- const parentPath = path . posix . dirname ( p )
142- const parentDirs = parentPath === '.' ? [ ] : parentPath . split ( '/' )
143-
251+ const segments = p . split ( '/' )
252+ let parentMatched : boolean [ ] = [ ]
144253 let matched = false
145- for ( const pattern of this . patterns ) {
146- // An inclusion can't change an already matched path, and an
147- // exclusion can't change a path that hasn't matched yet
148- if ( pattern . exclusion !== matched ) {
149- continue
150- }
151- const match =
152- pattern . regex . test ( p ) ||
153- parentDirs . some ( ( _ , i ) =>
154- pattern . regex . test ( parentDirs . slice ( 0 , i + 1 ) . join ( '/' ) )
155- )
156- if ( match ) {
157- matched = ! pattern . exclusion
158- }
254+ for ( let depth = 1 ; depth <= segments . length ; depth ++ ) {
255+ const current = segments . slice ( 0 , depth ) . join ( '/' )
256+ matched = false
257+ parentMatched = this . patterns . map ( ( pattern , i ) => {
258+ let match = parentMatched [ i ] ?? false
259+ if ( ! match ) {
260+ // An inclusion can't change an already matched path, and an
261+ // exclusion can't change a path that hasn't matched yet
262+ if ( pattern . exclusion !== matched ) {
263+ return false
264+ }
265+ match = pattern . match ( current )
266+ }
267+ if ( match ) {
268+ matched = ! pattern . exclusion
269+ }
270+ return match
271+ } )
159272 }
160273 return matched
161274 }
0 commit comments