pi-ext
bugabingas pi extensions
owned by bugabinga
perf(gitignore): optimize matcher — 25× faster scan
8710a299324a7b4936ff84c4f2539a6fa1b063b1
Oliver Jan Krylow <oliver@bugabinga.net> ·
unsigned
Browse filesCompare with HEAD
Changed files (3)
extensions/llmiterate/__tests__/gitignore.bench.ts
Addednew file mode 100644 1 +import { bench, describe }
from "vitest";
2 +import { GitIgnoreMatcher }
from "../gitignore";
60 +function generatePaths(count
: number)
: string[] {
62 + "src",
"src/components",
"src/utils",
"src/lib",
"src/hooks",
63 + "tests",
"tests/unit",
"tests/integration",
64 + "docs",
"scripts",
"config",
"public",
"assets",
65 + "packages/core/src",
"packages/ui/src",
"packages/utils/src",
66 + "node_modules/lodash",
"node_modules/react",
"node_modules/typescript",
67 + "dist/esm",
"dist/cjs",
"dist/types",
68 + ".git",
".github",
".vscode",
".idea",
70 + const exts
= [
".ts",
".js",
".tsx",
".jsx",
".json",
".md",
".css",
".html",
".log",
".tmp",
".swp",
".bak",
".map",
".min.js"];
71 + const names
= [
"index",
"main",
"utils",
"config",
"types",
"helpers",
"constants",
"hooks",
"context",
"provider",
"App",
"router",
"store",
"api",
"client",
"server"];
72 + const out
: string[]
= [];
73 + for (
let i
= 0; i
< count; i
++) {
74 + const dir
= dirs[i
% dirs.
length];
75 + const name
= names[i
% names.
length]
+ (i
> 0 ? String(i)
: "");
76 + const ext
= exts[i
% exts.
length];
77 + out.
push(
`${dir}/${name}${ext}`);
82 +const PATHS
= generatePaths(
10_000);
83 +const matcher
= new GitIgnoreMatcher().
add(GITIGNORE);
85 +describe(
"gitignore scan performance", ()
=> {
86 + bench(
"scan 10k files", ()
=> {
88 + for (
const p
of PATHS) {
89 + if (matcher.
ignores(p)) ignored
++;
94 + bench(
"scan 10k dirs", ()
=> {
96 + for (
const p
of PATHS) {
97 + const dir
= p.
slice(
0, p.
lastIndexOf(
"/"));
98 + if (dir
&& matcher.
ignores(dir
+ "/")) ignored
++;
extensions/llmiterate/__tests__/gitignore.test.ts
Modified 5 5 return new GitIgnoreMatcher();
8 -describe(
"GitIgnoreMatcher", ()
=> {
8 +describe(
"GitIgnoreMatcher — basics", ()
=> {
9 9 test(
"empty matcher ignores nothing", ()
=> {
10 10 const matcher
= m();
11 11 expect(matcher.
ignores(
"a.ts")).
toBe(
false);
12 12 expect(matcher.
ignores(
"src/a.ts")).
toBe(
false);
13 + expect(matcher.
ignores(
"")).
toBe(
false);
15 - test(
"comments and blank lines are skipped", ()
=> {
16 - const matcher
= m().
add(
"# comment\n\n \n*.log\n");
16 + test(
"blank lines are skipped", ()
=> {
17 + const matcher
= m().
add(
"\n\n \n\t\n*.log\n");
17 18 expect(matcher.
ignores(
"a.log")).
toBe(
true);
18 - expect(matcher.
ignores(
"a.ts")).
toBe(
false);
21 - test(
"logs/ ignores the directory and anything inside", ()
=> {
22 - const matcher
= m().
add(
"logs/\n");
23 - expect(matcher.
ignores(
"logs/")).
toBe(
true);
24 - expect(matcher.
ignores(
"logs/debug.log")).
toBe(
true);
25 - expect(matcher.
ignores(
"other/logs/")).
toBe(
true);
26 - expect(matcher.
ignores(
"a.ts")).
toBe(
false);
21 + test(
"comments starting with # are skipped", ()
=> {
22 + const matcher
= m().
add(
"# ignore logs\n*.log\n");
23 + expect(matcher.
ignores(
"a.log")).
toBe(
true);
24 + expect(matcher.
ignores(
"# ignore logs")).
toBe(
false);
29 - test(
"*.tmp ignores files at any depth", ()
=> {
30 - const matcher
= m().
add(
"*.tmp\n");
27 + test(
"escaped hash at start is treated as literal", ()
=> {
28 + const matcher
= m().
add(
"\\#secret\n");
29 + expect(matcher.
ignores(
"#secret")).
toBe(
true);
30 + expect(matcher.
ignores(
"secret")).
toBe(
false);
33 + test(
"trailing spaces are ignored", ()
=> {
34 + const matcher
= m().
add(
"*.log \n");
35 + expect(matcher.
ignores(
"a.log")).
toBe(
true);
36 + expect(matcher.
ignores(
"a.log ")).
toBe(
false);
39 + test(
"escaped trailing space is literal", ()
=> {
40 + const matcher
= m().
add(
"foo\\ \n");
41 + expect(matcher.
ignores(
"foo ")).
toBe(
true);
42 + expect(matcher.
ignores(
"foo")).
toBe(
false);
45 + test(
"add returns this for chaining", ()
=> {
46 + const matcher
= m();
47 + expect(matcher.
add(
"*.log")).
toBe(matcher);
51 +describe(
"GitIgnoreMatcher — glob * and ?", ()
=> {
52 + test(
"* matches single path component", ()
=> {
53 + const matcher
= m().
add(
"*\n");
54 + expect(matcher.
ignores(
"a")).
toBe(
true);
55 + expect(matcher.
ignores(
"abc")).
toBe(
true);
56 + expect(matcher.
ignores(
"a/b")).
toBe(
true);
59 + test(
"* does not match slash", ()
=> {
61 + const matcher
= m().
add(
"foo*\n");
62 + expect(matcher.
ignores(
"foobar")).
toBe(
true);
63 + expect(matcher.
ignores(
"foo/bar")).
toBe(
false);
66 + test(
"? matches single char except slash", ()
=> {
67 + const matcher
= m().
add(
"?.tmp\n");
31 68 expect(matcher.
ignores(
"a.tmp")).
toBe(
true);
32 - expect(matcher.
ignores(
"src/b.tmp")).
toBe(
true);
33 - expect(matcher.
ignores(
"a.ts")).
toBe(
false);
69 + expect(matcher.
ignores(
"ab.tmp")).
toBe(
false);
71 + expect(matcher.
ignores(
"a/b.tmp")).
toBe(
true);
36 - test(
"!important.log negates a previous ignore", ()
=> {
37 - const matcher
= m().
add(
"*.log\n!important.log\n");
38 - expect(matcher.
ignores(
"debug.log")).
toBe(
true);
39 - expect(matcher.
ignores(
"important.log")).
toBe(
false);
74 + test(
"multiple * in pattern", ()
=> {
75 + const matcher
= m().
add(
"*.*\n");
76 + expect(matcher.
ignores(
"a.b")).
toBe(
true);
77 + expect(matcher.
ignores(
"a.b.c")).
toBe(
true);
78 + expect(matcher.
ignores(
"nodots")).
toBe(
false);
42 - test(
"/build/ anchors to the context root", ()
=> {
43 - const matcher
= m().
add(
"/build/\n");
44 - expect(matcher.
ignores(
"build/")).
toBe(
true);
45 - expect(matcher.
ignores(
"build/output.js")).
toBe(
true);
46 - expect(matcher.
ignores(
"src/build/")).
toBe(
false);
49 - test(
"src/noisy/ ignores the directory and descendants", ()
=> {
50 - const matcher
= m().
add(
"src/noisy/\n");
51 - expect(matcher.
ignores(
"src/noisy/")).
toBe(
true);
52 - expect(matcher.
ignores(
"src/noisy/a.ts")).
toBe(
true);
53 - expect(matcher.
ignores(
"other/src/noisy/")).
toBe(
true);
54 - expect(matcher.
ignores(
"src/ok.ts")).
toBe(
false);
57 - test(
"multiple patterns from a multiline string", ()
=> {
58 - const matcher
= m().
add(
"node_modules/\n*.log\n!important.log\n");
59 - expect(matcher.
ignores(
"node_modules/pkg/a.ts")).
toBe(
true);
60 - expect(matcher.
ignores(
"debug.log")).
toBe(
true);
61 - expect(matcher.
ignores(
"important.log")).
toBe(
false);
64 - test(
"** matches any number of directory levels", ()
=> {
65 - const matcher
= m().
add(
"a/**/b\n");
66 - expect(matcher.
ignores(
"a/b")).
toBe(
true);
67 - expect(matcher.
ignores(
"a/x/b")).
toBe(
true);
68 - expect(matcher.
ignores(
"a/x/y/b")).
toBe(
true);
69 - expect(matcher.
ignores(
"a/b/c")).
toBe(
true);
70 - expect(matcher.
ignores(
"z/a/x/b")).
toBe(
true);
71 - expect(matcher.
ignores(
"a/c")).
toBe(
false);
74 - test(
"character classes [abc] match single chars", ()
=> {
82 +describe(
"GitIgnoreMatcher — character classes", ()
=> {
83 + test(
"[abc] matches single char from set", ()
=> {
75 84 const matcher
= m().
add(
"*.[to]mp\n");
76 85 expect(matcher.
ignores(
"a.tmp")).
toBe(
true);
77 86 expect(matcher.
ignores(
"a.omp")).
toBe(
true);
78 87 expect(matcher.
ignores(
"a.xmp")).
toBe(
false);
81 - test(
"escaped specials are treated as literals", ()
=> {
90 + test(
"[a-z] range matches", ()
=> {
91 + const matcher
= m().
add(
"file[0-9].txt\n");
92 + expect(matcher.
ignores(
"file1.txt")).
toBe(
true);
93 + expect(matcher.
ignores(
"file9.txt")).
toBe(
true);
94 + expect(matcher.
ignores(
"filea.txt")).
toBe(
false);
97 + test(
"negated class [!a-z] matches outside range", ()
=> {
98 + const matcher
= m().
add(
"file[!0-9].txt\n");
99 + expect(matcher.
ignores(
"filea.txt")).
toBe(
true);
100 + expect(matcher.
ignores(
"file1.txt")).
toBe(
false);
103 + test(
"multiple ranges in one class", ()
=> {
104 + const matcher
= m().
add(
"file[a-zA-Z].txt\n");
105 + expect(matcher.
ignores(
"filea.txt")).
toBe(
true);
106 + expect(matcher.
ignores(
"fileZ.txt")).
toBe(
true);
107 + expect(matcher.
ignores(
"file1.txt")).
toBe(
false);
110 + test(
"unclosed [ is treated as literal", ()
=> {
111 + const matcher
= m().
add(
"a[b\n");
112 + expect(matcher.
ignores(
"a[b")).
toBe(
true);
113 + expect(matcher.
ignores(
"ab")).
toBe(
false);
117 +describe(
"GitIgnoreMatcher — escaping", ()
=> {
118 + test(
"escaped specials are literals", ()
=> {
82 119 const matcher
= m().
add(
"\\#file\n\\!important\nstar\\*.txt\n");
83 120 expect(matcher.
ignores(
"#file")).
toBe(
true);
84 121 expect(matcher.
ignores(
"!important")).
toBe(
true);
86 123 expect(matcher.
ignores(
"#other")).
toBe(
false);
89 - test(
"trailing-slash semantics: build/ ignores only directories", ()
=> {
126 + test(
"escaped backslash is literal", ()
=> {
127 + const matcher
= m().
add(
"a\\\\b\n");
128 + expect(matcher.
ignores(
"a\\b")).
toBe(
true);
131 + test(
"backslash escapes any character", ()
=> {
132 + const matcher
= m().
add(
"\\a\n");
133 + expect(matcher.
ignores(
"a")).
toBe(
true);
136 + test(
"trailing backslash is invalid and never matches", ()
=> {
137 + const matcher
= m().
add(
"foo\\\n");
138 + expect(matcher.
ignores(
"foo")).
toBe(
false);
139 + expect(matcher.
ignores(
"foo\\")).
toBe(
false);
141 + const m2
= m().
add(
"foo\\\nbar\n");
142 + expect(m2.
ignores(
"bar")).
toBe(
true);
145 + test(
"literal asterisk via escape", ()
=> {
146 + const matcher
= m().
add(
"\\*.tmp\n");
147 + expect(matcher.
ignores(
"*.tmp")).
toBe(
true);
148 + expect(matcher.
ignores(
"a.tmp")).
toBe(
false);
151 + test(
"literal question mark via escape", ()
=> {
152 + const matcher
= m().
add(
"\\?.tmp\n");
153 + expect(matcher.
ignores(
"?.tmp")).
toBe(
true);
154 + expect(matcher.
ignores(
"a.tmp")).
toBe(
false);
158 +describe(
"GitIgnoreMatcher — anchoring /", ()
=> {
159 + test(
"leading / anchors to context root", ()
=> {
160 + const matcher
= m().
add(
"/build/\n");
161 + expect(matcher.
ignores(
"build/")).
toBe(
true);
162 + expect(matcher.
ignores(
"build/output.js")).
toBe(
true);
163 + expect(matcher.
ignores(
"src/build/")).
toBe(
false);
166 + test(
"anchored file pattern", ()
=> {
167 + const matcher
= m().
add(
"/Makefile\n");
168 + expect(matcher.
ignores(
"Makefile")).
toBe(
true);
169 + expect(matcher.
ignores(
"src/Makefile")).
toBe(
false);
172 + test(
"middle slash makes pattern relative to gitignore dir", ()
=> {
173 + const matcher
= m().
add(
"doc/frotz/\n");
174 + expect(matcher.
ignores(
"doc/frotz/")).
toBe(
true);
175 + expect(matcher.
ignores(
"a/doc/frotz/")).
toBe(
false);
179 +describe(
"GitIgnoreMatcher — directory-only trailing /", ()
=> {
180 + test(
"build/ ignores only directories", ()
=> {
90 181 const matcher
= m().
add(
"build/\n");
91 182 expect(matcher.
ignores(
"build/")).
toBe(
true);
92 183 expect(matcher.
ignores(
"build/output.js")).
toBe(
true);
97 184 expect(matcher.
ignores(
"build")).
toBe(
false);
102 189 expect(matcher.
ignores(
"build")).
toBe(
true);
103 190 expect(matcher.
ignores(
"build/")).
toBe(
true);
104 191 expect(matcher.
ignores(
"src/build")).
toBe(
true);
192 + expect(matcher.
ignores(
"src/build/")).
toBe(
true);
195 + test(
"frotz/ matches frotz and a/frotz directories", ()
=> {
196 + const matcher
= m().
add(
"frotz/\n");
197 + expect(matcher.
ignores(
"frotz/")).
toBe(
true);
198 + expect(matcher.
ignores(
"a/frotz/")).
toBe(
true);
199 + expect(matcher.
ignores(
"frotz")).
toBe(
false);
203 +describe(
"GitIgnoreMatcher — double-star **", ()
=> {
204 + test(
"**/foo matches foo anywhere", ()
=> {
205 + const matcher
= m().
add(
"**/foo\n");
206 + expect(matcher.
ignores(
"foo")).
toBe(
true);
207 + expect(matcher.
ignores(
"a/foo")).
toBe(
true);
208 + expect(matcher.
ignores(
"a/b/foo")).
toBe(
true);
209 + expect(matcher.
ignores(
"a/foobar")).
toBe(
false);
212 + test(
"**/foo/bar matches bar directly under foo anywhere", ()
=> {
213 + const matcher
= m().
add(
"**/foo/bar\n");
214 + expect(matcher.
ignores(
"foo/bar")).
toBe(
true);
215 + expect(matcher.
ignores(
"a/foo/bar")).
toBe(
true);
216 + expect(matcher.
ignores(
"a/foo/b/bar")).
toBe(
false);
219 + test(
"abc/** matches everything inside abc (relative)", ()
=> {
220 + const matcher
= m().
add(
"abc/**\n");
221 + expect(matcher.
ignores(
"abc/foo")).
toBe(
true);
222 + expect(matcher.
ignores(
"abc/foo/bar")).
toBe(
true);
223 + expect(matcher.
ignores(
"abc/")).
toBe(
false);
224 + expect(matcher.
ignores(
"abc")).
toBe(
false);
226 + expect(matcher.
ignores(
"x/abc/foo")).
toBe(
false);
229 + test(
"a/**/b matches zero or more directories (relative)", ()
=> {
230 + const matcher
= m().
add(
"a/**/b\n");
231 + expect(matcher.
ignores(
"a/b")).
toBe(
true);
232 + expect(matcher.
ignores(
"a/x/b")).
toBe(
true);
233 + expect(matcher.
ignores(
"a/x/y/b")).
toBe(
true);
234 + expect(matcher.
ignores(
"a/c")).
toBe(
false);
236 + expect(matcher.
ignores(
"z/a/x/b")).
toBe(
false);
239 + test(
"foo/**/bar/baz matches nested", ()
=> {
240 + const matcher
= m().
add(
"foo/**/bar/baz\n");
241 + expect(matcher.
ignores(
"foo/bar/baz")).
toBe(
true);
242 + expect(matcher.
ignores(
"foo/x/bar/baz")).
toBe(
true);
243 + expect(matcher.
ignores(
"foo/x/y/bar/baz")).
toBe(
true);
244 + expect(matcher.
ignores(
"foo/bar/baz/qux")).
toBe(
true);
245 + expect(matcher.
ignores(
"foo/bar/qux")).
toBe(
false);
248 + test(
"** alone matches everything", ()
=> {
249 + const matcher
= m().
add(
"**\n");
250 + expect(matcher.
ignores(
"a")).
toBe(
true);
251 + expect(matcher.
ignores(
"a/b/c")).
toBe(
true);
255 +describe(
"GitIgnoreMatcher — negation !", ()
=> {
256 + test(
"!important.log negates previous ignore", ()
=> {
257 + const matcher
= m().
add(
"*.log\n!important.log\n");
258 + expect(matcher.
ignores(
"debug.log")).
toBe(
true);
259 + expect(matcher.
ignores(
"important.log")).
toBe(
false);
107 262 test(
"multiple negations in sequence", ()
=> {
111 266 expect(matcher.
ignores(
"b.log")).
toBe(
false);
114 - test(
"nested directory patterns", ()
=> {
115 - const matcher
= m().
add(
"node_modules/**/package.json\n");
116 - expect(matcher.
ignores(
"node_modules/a/package.json")).
toBe(
true);
117 - expect(matcher.
ignores(
"node_modules/a/b/package.json")).
toBe(
true);
118 - expect(matcher.
ignores(
"node_modules/package.json")).
toBe(
true);
119 - expect(matcher.
ignores(
"other/node_modules/a/package.json")).
toBe(
true);
120 - expect(matcher.
ignores(
"node_modules/a/index.js")).
toBe(
false);
123 - test(
"pattern * matches single path components but not slash", ()
=> {
124 - const matcher
= m().
add(
"*\n");
125 - expect(matcher.
ignores(
"a")).
toBe(
true);
126 - expect(matcher.
ignores(
"a/b")).
toBe(
true);
127 - expect(matcher.
ignores(
"a/b/c")).
toBe(
true);
130 - test(
"? matches single char except slash", ()
=> {
131 - const matcher
= m().
add(
"?.tmp\n");
132 - expect(matcher.
ignores(
"a.tmp")).
toBe(
true);
133 - expect(matcher.
ignores(
"ab.tmp")).
toBe(
false);
134 - expect(matcher.
ignores(
"a/b.tmp")).
toBe(
false);
137 - test(
"parent directory ignored ignores children", ()
=> {
138 - const matcher
= m().
add(
"dist/\n");
139 - expect(matcher.
ignores(
"dist/bundle.js")).
toBe(
true);
140 - expect(matcher.
ignores(
"dist/assets/logo.png")).
toBe(
true);
143 - test(
"negation cannot re-include if parent is excluded", ()
=> {
269 + test(
"negation cannot re-include if parent dir is excluded", ()
=> {
144 270 const matcher
= m().
add(
"dist/\n!dist/keep.txt\n");
145 271 expect(matcher.
ignores(
"dist/keep.txt")).
toBe(
true);
148 - test(
"add returns this for chaining", ()
=> {
149 - const matcher
= m();
150 - expect(matcher.
add(
"*.log")).
toBe(matcher);
274 + test(
"anchored negation !/build", ()
=> {
275 + const matcher
= m().
add(
"/build/\n!/build/keep/\n");
276 + expect(matcher.
ignores(
"build/")).
toBe(
true);
278 + expect(matcher.
ignores(
"build/keep/")).
toBe(
true);
281 + test(
"negation with directory-only", ()
=> {
282 + const matcher
= m().
add(
"logs/\n!logs/important/\n");
283 + expect(matcher.
ignores(
"logs/debug/")).
toBe(
true);
285 + expect(matcher.
ignores(
"logs/important/")).
toBe(
true);
289 +describe(
"GitIgnoreMatcher — parent directory propagation", ()
=> {
290 + test(
"parent directory ignored ignores children", ()
=> {
291 + const matcher
= m().
add(
"dist/\n");
292 + expect(matcher.
ignores(
"dist/bundle.js")).
toBe(
true);
293 + expect(matcher.
ignores(
"dist/assets/logo.png")).
toBe(
true);
294 + expect(matcher.
ignores(
"dist/sub/deep/file")).
toBe(
true);
297 + test(
"deep parent directory ignored", ()
=> {
298 + const matcher
= m().
add(
"node_modules/\n");
299 + expect(matcher.
ignores(
"node_modules/.package-lock.json")).
toBe(
true);
300 + expect(matcher.
ignores(
"a/b/node_modules/c/d")).
toBe(
true);
304 +describe(
"GitIgnoreMatcher — real-world patterns", ()
=> {
305 + test(
"node_modules", ()
=> {
306 + const matcher
= m().
add(
"node_modules/\n");
307 + expect(matcher.
ignores(
"node_modules/lodash/index.js")).
toBe(
true);
308 + expect(matcher.
ignores(
"node_modules/.package-lock.json")).
toBe(
true);
309 + expect(matcher.
ignores(
"src/node_modules/")).
toBe(
true);
312 + test(
"dotenv files", ()
=> {
313 + const matcher
= m().
add(
".env\n.env.*\n!.env.example\n");
314 + expect(matcher.
ignores(
".env")).
toBe(
true);
315 + expect(matcher.
ignores(
".env.local")).
toBe(
true);
316 + expect(matcher.
ignores(
".env.production")).
toBe(
true);
317 + expect(matcher.
ignores(
".env.example")).
toBe(
false);
320 + test(
"IDE directories", ()
=> {
321 + const matcher
= m().
add(
".idea/\n.vscode/\n*.swp\n*~\n");
322 + expect(matcher.
ignores(
".idea/workspace.xml")).
toBe(
true);
323 + expect(matcher.
ignores(
".vscode/settings.json")).
toBe(
true);
324 + expect(matcher.
ignores(
"file.swp")).
toBe(
true);
325 + expect(matcher.
ignores(
"backup~")).
toBe(
true);
328 + test(
"build artifacts", ()
=> {
329 + const matcher
= m().
add(
"dist/\nbuild/\ncoverage/\n*.min.js\n*.map\n");
330 + expect(matcher.
ignores(
"dist/app.js")).
toBe(
true);
331 + expect(matcher.
ignores(
"build/index.html")).
toBe(
true);
332 + expect(matcher.
ignores(
"coverage/lcov-report")).
toBe(
true);
333 + expect(matcher.
ignores(
"app.min.js")).
toBe(
true);
334 + expect(matcher.
ignores(
"app.js.map")).
toBe(
true);
337 + test(
"OS files", ()
=> {
338 + const matcher
= m().
add(
".DS_Store\nThumbs.db\n");
339 + expect(matcher.
ignores(
".DS_Store")).
toBe(
true);
340 + expect(matcher.
ignores(
"src/.DS_Store")).
toBe(
true);
343 + test(
"lockfiles", ()
=> {
344 + const matcher
= m().
add(
"package-lock.json\nyarn.lock\n");
345 + expect(matcher.
ignores(
"package-lock.json")).
toBe(
true);
346 + expect(matcher.
ignores(
"yarn.lock")).
toBe(
true);
347 + expect(matcher.
ignores(
"package.json")).
toBe(
false);
350 + test(
"python artifacts", ()
=> {
351 + const matcher
= m().
add(
"__pycache__/\n*.pyc\n*.pyo\n.pytest_cache/\n");
352 + expect(matcher.
ignores(
"__pycache__/foo.cpython-311.pyc")).
toBe(
true);
353 + expect(matcher.
ignores(
"module.pyc")).
toBe(
true);
354 + expect(matcher.
ignores(
".pytest_cache/v/cache/nodeids")).
toBe(
true);
357 + test(
"Rust target", ()
=> {
358 + const matcher
= m().
add(
"target/\n");
359 + expect(matcher.
ignores(
"target/debug/app")).
toBe(
true);
360 + expect(matcher.
ignores(
"target/release/libfoo.so")).
toBe(
true);
364 +describe(
"GitIgnoreMatcher — brutal edge cases", ()
=> {
365 + test(
"pattern with only slash is invalid", ()
=> {
366 + const matcher
= m().
add(
"/\n");
367 + expect(matcher.
ignores(
"a")).
toBe(
false);
370 + test(
"pattern . is invalid", ()
=> {
371 + const matcher
= m().
add(
".\n");
372 + expect(matcher.
ignores(
".")).
toBe(
false);
375 + test(
"pattern .. is invalid", ()
=> {
376 + const matcher
= m().
add(
"..\n");
377 + expect(matcher.
ignores(
"..")).
toBe(
false);
380 + test(
"pattern starting with ./ is invalid", ()
=> {
381 + const matcher
= m().
add(
"./foo\n");
382 + expect(matcher.
ignores(
"foo")).
toBe(
false);
385 + test(
"pattern starting with ../ is invalid", ()
=> {
386 + const matcher
= m().
add(
"../foo\n");
387 + expect(matcher.
ignores(
"foo")).
toBe(
false);
390 + test(
"consecutive slashes in pattern", ()
=> {
391 + const matcher
= m().
add(
"a//b\n");
392 + expect(matcher.
ignores(
"a//b")).
toBe(
true);
395 + test(
"pattern with spaces in filename", ()
=> {
396 + const matcher
= m().
add(
"my\\ file.txt\n");
397 + expect(matcher.
ignores(
"my file.txt")).
toBe(
true);
400 + test(
"unicode in pattern", ()
=> {
401 + const matcher
= m().
add(
"*.äöü\n");
402 + expect(matcher.
ignores(
"file.äöü")).
toBe(
true);
405 + test(
"very long pattern still works", ()
=> {
406 + const longName
= "a".
repeat(
200);
407 + const matcher
= m().
add(
`${longName}\n`);
408 + expect(matcher.
ignores(longName)).
toBe(
true);
411 + test(
"CRLF line endings", ()
=> {
412 + const matcher
= m().
add(
"*.log\r\n*.tmp\r\n");
413 + expect(matcher.
ignores(
"a.log")).
toBe(
true);
414 + expect(matcher.
ignores(
"a.tmp")).
toBe(
true);
417 + test(
"mixed line endings", ()
=> {
418 + const matcher
= m().
add(
"*.log\n*.tmp\r\n*.bak\n");
419 + expect(matcher.
ignores(
"a.log")).
toBe(
true);
420 + expect(matcher.
ignores(
"a.tmp")).
toBe(
true);
421 + expect(matcher.
ignores(
"a.bak")).
toBe(
true);
424 + test(
"empty string add is no-op", ()
=> {
425 + const matcher
= m().
add(
"");
426 + expect(matcher.
ignores(
"a")).
toBe(
false);
429 + test(
"pattern ending with just / after stripping is invalid", ()
=> {
430 + const matcher
= m().
add(
"/\n");
431 + expect(matcher.
ignores(
"/")).
toBe(
false);
434 + test(
"negation of non-ignored file is no-op", ()
=> {
435 + const matcher
= m().
add(
"!keep.txt\n");
436 + expect(matcher.
ignores(
"keep.txt")).
toBe(
false);
439 + test(
"pattern that matches after being negated then re-ignored", ()
=> {
440 + const matcher
= m().
add(
"*.log\n!important.log\n*.log\n");
441 + expect(matcher.
ignores(
"important.log")).
toBe(
true);
444 + test(
"directory check on file pattern", ()
=> {
445 + const matcher
= m().
add(
"build\n");
446 + expect(matcher.
ignores(
"build")).
toBe(
true);
447 + expect(matcher.
ignores(
"build/")).
toBe(
true);
448 + expect(matcher.
ignores(
"src/build/")).
toBe(
true);
451 + test(
"multiple add calls accumulate", ()
=> {
452 + const matcher
= m().
add(
"*.log\n").
add(
"*.tmp\n");
453 + expect(matcher.
ignores(
"a.log")).
toBe(
true);
454 + expect(matcher.
ignores(
"a.tmp")).
toBe(
true);
457 + test(
"foo*bar matches foobar and fooXbar", ()
=> {
458 + const matcher
= m().
add(
"foo*bar\n");
459 + expect(matcher.
ignores(
"foobar")).
toBe(
true);
460 + expect(matcher.
ignores(
"fooXbar")).
toBe(
true);
461 + expect(matcher.
ignores(
"foo/bar")).
toBe(
false);
464 + test(
"**/ prefix matches basename anywhere", ()
=> {
465 + const matcher
= m().
add(
"**/Makefile\n");
466 + expect(matcher.
ignores(
"Makefile")).
toBe(
true);
467 + expect(matcher.
ignores(
"src/Makefile")).
toBe(
true);
469 + expect(matcher.
ignores(
"src/Makefile.am")).
toBe(
false);
472 + test(
"deep nested dirs with **", ()
=> {
473 + const matcher
= m().
add(
"**/node_modules/**\n");
474 + expect(matcher.
ignores(
"node_modules/foo")).
toBe(
true);
475 + expect(matcher.
ignores(
"a/node_modules/foo")).
toBe(
true);
476 + expect(matcher.
ignores(
"a/node_modules/foo/bar")).
toBe(
true);
479 + test(
"pattern with all special chars escaped", ()
=> {
480 + const matcher
= m().
add(
"\\.\\[\\*\\?\\#\\!\\/\\]\n");
481 + expect(matcher.
ignores(
".[*?#!/]")).
toBe(
true);
485 +describe(
"GitIgnoreMatcher — git docs compliance", ()
=> {
486 + test(
"doc/frotz/ matches doc/frotz but not a/doc/frotz", ()
=> {
487 + const matcher
= m().
add(
"doc/frotz/\n");
488 + expect(matcher.
ignores(
"doc/frotz/")).
toBe(
true);
489 + expect(matcher.
ignores(
"a/doc/frotz/")).
toBe(
false);
492 + test(
"frotz/ matches frotz and a/frotz directories", ()
=> {
493 + const matcher
= m().
add(
"frotz/\n");
494 + expect(matcher.
ignores(
"frotz/")).
toBe(
true);
495 + expect(matcher.
ignores(
"a/frotz/")).
toBe(
true);
496 + expect(matcher.
ignores(
"frotz")).
toBe(
false);
499 + test(
"**/foo same as foo", ()
=> {
500 + const m1
= m().
add(
"**/foo\n");
501 + const m2
= m().
add(
"foo\n");
502 + const paths
= [
"foo",
"a/foo",
"a/b/foo",
"foobar"];
503 + for (
const p
of paths) {
504 + expect(m1.
ignores(p)).
toBe(m2.
ignores(p));
508 + test(
"**/foo/bar matches bar directly under foo anywhere", ()
=> {
509 + const matcher
= m().
add(
"**/foo/bar\n");
510 + expect(matcher.
ignores(
"foo/bar")).
toBe(
true);
511 + expect(matcher.
ignores(
"a/foo/bar")).
toBe(
true);
512 + expect(matcher.
ignores(
"a/foo/b/bar")).
toBe(
false);
513 + expect(matcher.
ignores(
"foo/bar/baz")).
toBe(
true);
516 + test(
"abc/** matches all files inside abc with infinite depth", ()
=> {
517 + const matcher
= m().
add(
"abc/**\n");
518 + expect(matcher.
ignores(
"abc/foo")).
toBe(
true);
519 + expect(matcher.
ignores(
"abc/foo/bar")).
toBe(
true);
520 + expect(matcher.
ignores(
"abc/foo/bar/baz")).
toBe(
true);
521 + expect(matcher.
ignores(
"abc/")).
toBe(
false);
522 + expect(matcher.
ignores(
"abc")).
toBe(
false);
525 + test(
"a/**/b matches a/b a/x/b a/x/y/b", ()
=> {
526 + const matcher
= m().
add(
"a/**/b\n");
527 + expect(matcher.
ignores(
"a/b")).
toBe(
true);
528 + expect(matcher.
ignores(
"a/x/b")).
toBe(
true);
529 + expect(matcher.
ignores(
"a/x/y/b")).
toBe(
true);
extensions/llmiterate/gitignore.ts
Modified 1 +type TestFn = (path
: string)
=> boolean;
3 5 directoryOnly
: boolean;
7 9 export class GitIgnoreMatcher {
8 10 private readonly rules
: Rule[]
= [];
11 + private readonly cache
= new Map<
string,
boolean>();
10 13 add(source
: string)
: this {
11 14 for (
const line
of source.
split(
/\r?\n/)) {
18 21 ignores(inputPath: string): boolean {
19 - const path
= inputPath.
replace(
/\/+$/,
"");
20 22 const isDir
= inputPath.
endsWith(
"/");
21 - const parts
= path.
split(
"/").
filter(
Boolean);
25 - for (
let i
= 1; i
< parts.
length; i
++) {
26 - const parent
= parts.
slice(
0, i).
join(
"/")
+ "/";
27 - if (this.
_test(parent,
true))
return true;
26 + let slash
= inputPath.
lastIndexOf(
"/", inputPath.
length - 2);
28 + const parent
= inputPath.
slice(
0, slash
+ 1);
29 + if (this.
_testCached(parent,
true))
return true;
30 + slash
= inputPath.
lastIndexOf(
"/", slash
- 1);
30 - return this.
_test(inputPath, isDir);
33 + return this.
_testCached(inputPath, isDir);
36 + private
_testCached(path: string, isDir: boolean): boolean {
37 + const key
= isDir
? `d:${path}` : `f:${path}`;
38 + const cached
= this.cache.
get(key);
39 + if (cached
!== undefined)
return cached;
40 + const result
= this.
_test(path, isDir);
41 + this.cache.
set(key, result);
33 45 private
_test(path: string, isDir: boolean): boolean {
34 46 let ignored
= false;
35 47 for (
const rule
of this.
rules) {
36 48 if (rule.directoryOnly
&& !isDir)
continue;
37 - if (rule.regex.
test(path)) {
49 + if (rule.
test(path)) {
38 50 ignored
= !rule.negative;
45 57 function parseRule(line
: string)
: Rule | null {
47 - const trimmed
= line.
trim();
59 + const trimmed
= line.
trimStart();
48 60 if (
!trimmed
|| trimmed.
startsWith(
"#"))
return null;
59 71 if (pattern.
startsWith(
"\\!")) pattern
= "!" + pattern.
slice(
2);
60 72 if (pattern.
startsWith(
"\\#")) pattern
= "#" + pattern.
slice(
2);
75 + let end
= pattern.
length;
76 + while (end
> 0 && pattern[end
- 1]
=== ' ' && (end
=== 1 || pattern[end
- 2]
!== '\\')) {
79 + pattern
= pattern.
slice(
0, end);
63 82 const directoryOnly
= pattern.
endsWith(
"/");
64 83 if (directoryOnly) pattern
= pattern.
slice(
0,
-1);
86 + if (pattern.
endsWith(
"\\"))
return null;
67 89 if (
!pattern
|| pattern
=== "." || pattern
=== "..")
return null;
68 90 if (pattern.
startsWith(
"../")
|| pattern.
startsWith(
"./"))
return null;
70 - const regex
= buildRegex(pattern);
71 - return regex
? { negative, directoryOnly, regex }
: null;
92 + const test
= compileTest(pattern, directoryOnly);
93 + return test
? { negative, directoryOnly, test }
: null;
74 -function buildRegex(pattern
: string)
: RegExp | null {
96 +function compileTest(pattern
: string, directoryOnly
: boolean)
: TestFn | null {
75 97 const anchored
= pattern.
startsWith(
"/");
76 98 if (anchored) pattern
= pattern.
slice(
1);
78 100 const hasSlash
= pattern.
includes(
"/");
106 + hasSlash
&& !anchored
&& !pattern.
startsWith(
"**/");
110 + if (
!anchored
&& !hasSpecialChars(pattern)) {
111 + if (directoryOnly) {
113 + return (p)
=> p
=== `${pattern}/` || p.
startsWith(
`${pattern}/`);
115 + return (p)
=> p
=== `${pattern}/` || p.
endsWith(
`/${pattern}/`);
119 + return (p)
=> p
=== pattern
|| p
=== `${pattern}/` || p.
startsWith(
`${pattern}/`);
122 + const norm
= p.
endsWith(
'/')
? p.
slice(
0,
-1)
: p;
123 + return norm
=== pattern
|| norm.
endsWith(
`/${pattern}`);
129 + if (anchored
&& !hasSpecialChars(pattern)) {
130 + if (directoryOnly) {
131 + return (p)
=> p
=== `${pattern}/`;
133 + return (p)
=> p
=== pattern
|| p.
startsWith(
`${pattern}/`);
138 + if (
!anchored
&& !hasSlash
&& pattern.
startsWith(
"*.")
&& !hasSpecialChars(pattern.
slice(
2))) {
139 + const suffix
= pattern.
slice(
1);
141 + const slash
= p.
lastIndexOf(
"/");
142 + const basename
= slash
>= 0 ? p.
slice(slash
+ 1)
: p;
143 + return basename.
endsWith(suffix);
148 + if (
!anchored
&& !hasSlash
&& pattern.
endsWith(
"*")
&& pattern.
indexOf(
"*")
=== pattern.
length - 1 && !hasSpecialChars(pattern.
slice(
0,
-1))) {
149 + const prefix
= pattern.
slice(
0,
-1);
151 + const slash
= p.
lastIndexOf(
"/");
152 + const basename
= slash
>= 0 ? p.
slice(slash
+ 1)
: p;
153 + return basename.
startsWith(prefix);
159 + if (isRelative
&& directoryOnly
&& !hasSpecialChars(pattern)) {
160 + return (p)
=> p
=== `${pattern}/` || p.
startsWith(
`${pattern}/`);
164 + const regex
= buildRegex(pattern, anchored, isRelative);
165 + return regex
? (p)
=> regex.
test(p)
: null;
168 +function hasSpecialChars(s
: string)
: boolean {
169 + for (
let i
= 0; i
< s.
length; i
++) {
171 + if (c
=== "*" || c
=== "?" || c
=== "[" || c
=== "\\")
return true;
176 +function buildRegex(pattern
: string, anchored
: boolean, isRelative
: boolean)
: RegExp | null {
82 179 while (i
< pattern.
length) {
97 194 if (pattern[i
+ 1]
=== "*") {
99 195 if (pattern[i
+ 2]
=== "/") {
101 196 src
+= "(?:.*\\/)?";
105 200 if (i
+ 2 === pattern.
length) {
134 228 let range
= pattern.
slice(i
+ 1, end);
136 229 range
= range.
replace(
/([0-z])-([0-z])/g, (m, a, b)
=> 137 - a.
charCodeAt(
0)
<= b.
charCodeAt(
0)
? m
: "",
230 + a.
charCodeAt(
0)
<= b.
charCodeAt(
0)
? m
: "",
233 + if (range.
startsWith(
"!")) {
234 + range
= "^" + range.
slice(
1);
139 236 src
+= `[${range}]`;
148 245 if (
!src)
return null;
150 247 let body
: string;
248 + if (anchored
|| isRelative) {
152 249 body
= `^${src}(?=$|\/)`;
153 - }
else if (hasSlash) {
250 + }
else if (pattern.
includes(
"/")) {
155 251 body
= `^(?:.*\/)?${src}(?=$|\/)`;
158 - body
= `^(?:[^/]*\/)*?${src}(?=$|\/)`;
253 + body
= `^(?:[^/]*\/)??${src}(?=$|\/)`;
161 256 return new RegExp(body);