|
| 1 | +/** |
| 2 | + * Matching of `.dockerignore` patterns, following the semantics Docker uses to |
| 3 | + * filter the build context. |
| 4 | + * |
| 5 | + * Port of the pattern matcher from moby/patternmatcher (Apache-2.0): |
| 6 | + * https://github.com/moby/patternmatcher |
| 7 | + */ |
| 8 | +import path from 'node:path' |
| 9 | +import { TemplateError } from '../errors' |
| 10 | + |
| 11 | +// Characters that have a meaning in a regex but not in a Docker pattern |
| 12 | +const LITERAL_REGEX_CHARS = new Set([ |
| 13 | + '.', |
| 14 | + '+', |
| 15 | + '(', |
| 16 | + ')', |
| 17 | + '|', |
| 18 | + '{', |
| 19 | + '}', |
| 20 | + '$', |
| 21 | + '^', |
| 22 | +]) |
| 23 | +const WILDCARD_CHARS = /[*?[\\]/ |
| 24 | +// Evaluated lazily, as `node:path` is not available when loaded in the browser |
| 25 | +const backslashIsSeparator = () => path.sep === '\\' |
| 26 | + |
| 27 | +function escapeRegex(ch: string): string { |
| 28 | + return ch.replace(/[.*+?^${}()|[\]\\/-]/g, '\\$&') |
| 29 | +} |
| 30 | + |
| 31 | +// Equivalent of Go's filepath.Clean followed by filepath.ToSlash |
| 32 | +function clean(pattern: string): string { |
| 33 | + if (backslashIsSeparator()) { |
| 34 | + pattern = pattern.replace(/\\/g, '/') |
| 35 | + } |
| 36 | + return path.posix.normalize(pattern).replace(/(.)\/$/, '$1') |
| 37 | +} |
| 38 | + |
| 39 | +function compile(pattern: string): RegExp { |
| 40 | + let regex = '^' |
| 41 | + const n = pattern.length |
| 42 | + const backslashIsEscape = !backslashIsSeparator() |
| 43 | + let i = 0 |
| 44 | + while (i < n) { |
| 45 | + const ch = pattern[i++] |
| 46 | + if (ch === '*') { |
| 47 | + if (pattern[i] === '*') { |
| 48 | + i++ |
| 49 | + // Treat "**/" as "**" |
| 50 | + if (pattern[i] === '/') { |
| 51 | + i++ |
| 52 | + } |
| 53 | + regex += i >= n ? '.*' : '(.*/)?' |
| 54 | + } else { |
| 55 | + regex += '[^/]*' |
| 56 | + } |
| 57 | + } else if (ch === '?') { |
| 58 | + regex += '[^/]' |
| 59 | + } else if (ch === '[') { |
| 60 | + // Copy a bracket expression as is, a leading "^" negates it |
| 61 | + let j = i |
| 62 | + if (pattern[j] === '^') { |
| 63 | + j++ |
| 64 | + } |
| 65 | + while (j < n && pattern[j] !== ']') { |
| 66 | + if (pattern[j] === '\\' && backslashIsEscape) { |
| 67 | + j++ |
| 68 | + } |
| 69 | + j++ |
| 70 | + } |
| 71 | + regex += pattern.slice(i - 1, j + 1) |
| 72 | + i = j + 1 |
| 73 | + } else if (LITERAL_REGEX_CHARS.has(ch)) { |
| 74 | + regex += '\\' + ch |
| 75 | + } else if (ch === '\\' && backslashIsEscape) { |
| 76 | + // Escape the next character |
| 77 | + if (i < n) { |
| 78 | + regex += escapeRegex(pattern[i++]) |
| 79 | + } else { |
| 80 | + regex += '\\\\' |
| 81 | + } |
| 82 | + } else { |
| 83 | + regex += ch |
| 84 | + } |
| 85 | + } |
| 86 | + return new RegExp(regex + '$') |
| 87 | +} |
| 88 | + |
| 89 | +interface Pattern { |
| 90 | + exclusion: boolean |
| 91 | + dirs: string[] |
| 92 | + regex: RegExp |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * Match paths relative to the context root against `.dockerignore` patterns. |
| 97 | + * |
| 98 | + * A pattern that matches a directory excludes everything under it, a leading |
| 99 | + * `/` is ignored, and `!` patterns re-include paths (the last matching |
| 100 | + * pattern wins). |
| 101 | + */ |
| 102 | +export class PatternMatcher { |
| 103 | + private readonly patterns: Pattern[] = [] |
| 104 | + |
| 105 | + constructor(patterns: string[]) { |
| 106 | + for (const original of patterns) { |
| 107 | + let pattern = original.trim() |
| 108 | + if (!pattern || pattern.startsWith('#')) { |
| 109 | + continue |
| 110 | + } |
| 111 | + const exclusion = pattern.startsWith('!') |
| 112 | + if (exclusion) { |
| 113 | + pattern = pattern.slice(1).trim() |
| 114 | + if (!pattern) { |
| 115 | + continue |
| 116 | + } |
| 117 | + } |
| 118 | + pattern = clean(pattern) |
| 119 | + if (pattern.length > 1 && pattern.startsWith('/')) { |
| 120 | + pattern = pattern.slice(1) |
| 121 | + } |
| 122 | + let regex: RegExp |
| 123 | + try { |
| 124 | + regex = compile(pattern) |
| 125 | + } catch (err) { |
| 126 | + throw new TemplateError( |
| 127 | + `Invalid ignore pattern '${original}': ${(err as Error).message}` |
| 128 | + ) |
| 129 | + } |
| 130 | + this.patterns.push({ exclusion, dirs: pattern.split('/'), regex }) |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Whether the path or one of its parent directories is excluded. |
| 136 | + * |
| 137 | + * @param p Slash-separated path relative to the context root |
| 138 | + * @returns True if the path is excluded |
| 139 | + */ |
| 140 | + matches(p: string): boolean { |
| 141 | + const parentPath = path.posix.dirname(p) |
| 142 | + const parentDirs = parentPath === '.' ? [] : parentPath.split('/') |
| 143 | + |
| 144 | + 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 | + } |
| 159 | + } |
| 160 | + return matched |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Whether a `!` pattern could re-include a path under the directory, |
| 165 | + * in which case an excluded directory must still be walked. |
| 166 | + * |
| 167 | + * @param dirPath Slash-separated directory path relative to the context root |
| 168 | + * @returns True if a path under the directory could be re-included |
| 169 | + */ |
| 170 | + mayMatchUnder(dirPath: string): boolean { |
| 171 | + const dirSegments = dirPath.split('/') |
| 172 | + return this.patterns.some( |
| 173 | + (pattern) => |
| 174 | + pattern.exclusion && patternMayMatchUnder(pattern, dirSegments) |
| 175 | + ) |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +function patternMayMatchUnder(pattern: Pattern, dirSegments: string[]) { |
| 180 | + for (let i = 0; i < dirSegments.length; i++) { |
| 181 | + if (i >= pattern.dirs.length) { |
| 182 | + return false |
| 183 | + } |
| 184 | + const segment = pattern.dirs[i] |
| 185 | + if (segment.includes('**')) { |
| 186 | + return true |
| 187 | + } |
| 188 | + if (!WILDCARD_CHARS.test(segment) && segment !== dirSegments[i]) { |
| 189 | + return false |
| 190 | + } |
| 191 | + } |
| 192 | + return pattern.dirs.length > dirSegments.length |
| 193 | +} |
0 commit comments