repositories / pi-ext

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

Added
new file mode 100644
1 +import { bench, describe } from "vitest";
2 +import { GitIgnoreMatcher } from "../gitignore";
4 +const GITIGNORE = `
5 +node_modules/
6 +dist/
7 +build/
8 +coverage/
9 +.next/
10 +.nuxt/
11 +*.log
12 +*.tmp
13 +*.swp
14 +*~
15 +.DS_Store
16 +Thumbs.db
17 +.env
18 +.env.*
19 +!.env.example
20 +.vscode/
21 +.idea/
22 +*.min.js
23 +*.map
24 +package-lock.json
25 +yarn.lock
26 +pnpm-lock.yaml
27 +__pycache__/
28 +*.pyc
29 +*.pyo
30 +.pytest_cache/
31 +target/
32 +*.exe
33 +*.dll
34 +*.so
35 +*.class
36 +*.jar
37 +*.war
38 +*.iml
39 +*.suo
40 +*.ntvs*
41 +*.njsproj
42 +*.sln
43 +*.sw?
44 +*.dmg
45 +*.iso
46 +*.msi
47 +.*.swp
48 +.*.swo
49 +*.bak
50 +*.orig
51 +*.snap
52 +*.spec.js
53 +*.test.js
54 +__snapshots__/
55 +.cache/
56 +temp/
57 +tmp/
58 +`.trim();
60 +function generatePaths(count: number): string[] {
61 + const dirs = [
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",
69 + ];
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}`);
78 + }
79 + return out;
82 +const PATHS = generatePaths(10_000);
83 +const matcher = new GitIgnoreMatcher().add(GITIGNORE);
85 +describe("gitignore scan performance", () => {
86 + bench("scan 10k files", () => {
87 + let ignored = 0;
88 + for (const p of PATHS) {
89 + if (matcher.ignores(p)) ignored++;
90 + }
91 + return ignored;
92 + });
94 + bench("scan 10k dirs", () => {
95 + let ignored = 0;
96 + for (const p of PATHS) {
97 + const dir = p.slice(0, p.lastIndexOf("/"));
98 + if (dir && matcher.ignores(dir + "/")) ignored++;
99 + }
100 + return ignored;
101 + });
102 +});

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);
31 + });
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);
37 + });
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);
43 + });
45 + test("add returns this for chaining", () => {
46 + const matcher = m();
47 + expect(matcher.add("*.log")).toBe(matcher);
48 + });
49 +});
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); // matches component b
57 + });
59 + test("* does not match slash", () => {
60 + // Pattern * alone matches any single path component at any depth
61 + const matcher = m().add("foo*\n");
62 + expect(matcher.ignores("foobar")).toBe(true);
63 + expect(matcher.ignores("foo/bar")).toBe(false);
64 + });
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);
70 + // Basename pattern at any depth: ? matches b in a/b.tmp
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);
80 +});
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);
47 - });
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);
55 - });
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);
62 - });
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); // ** matches zero dirs, b is dir
70 - expect(matcher.ignores("z/a/x/b")).toBe(true);
71 - expect(matcher.ignores("a/c")).toBe(false);
72 - });
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);
95 + });
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);
101 + });
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);
108 + });
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);
114 + });
115 +});
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);
129 + });
131 + test("backslash escapes any character", () => {
132 + const matcher = m().add("\\a\n");
133 + expect(matcher.ignores("a")).toBe(true);
134 + });
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);
140 + // Verify a valid pattern still works in same matcher
141 + const m2 = m().add("foo\\\nbar\n");
142 + expect(m2.ignores("bar")).toBe(true);
143 + });
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);
149 + });
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);
155 + });
156 +});
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);
164 + });
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);
170 + });
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);
176 + });
177 +});
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);
93 - // Without trailing slash in pattern, files named build would also be ignored,
94 - // but with trailing slash only the directory is ignored.
95 - // Our matcher can't distinguish file vs directory without the trailing slash
96 - // in the input path, which is how the watcher uses it.
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);
193 + });
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);
200 + });
201 +});
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);
210 + });
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);
217 + });
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);
225 + // Relative pattern: does NOT match x/abc/foo
226 + expect(matcher.ignores("x/abc/foo")).toBe(false);
227 + });
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);
235 + // Relative pattern: does NOT match z/a/x/b
236 + expect(matcher.ignores("z/a/x/b")).toBe(false);
237 + });
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);
246 + });
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);
252 + });
253 +});
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);
121 - });
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); // matches component b
127 - expect(matcher.ignores("a/b/c")).toBe(true); // matches component c
128 - });
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);
135 - });
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);
141 - });
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);
277 + // Cannot re-include if parent is excluded per gitignore spec
278 + expect(matcher.ignores("build/keep/")).toBe(true);
279 + });
281 + test("negation with directory-only", () => {
282 + const matcher = m().add("logs/\n!logs/important/\n");
283 + expect(matcher.ignores("logs/debug/")).toBe(true);
284 + // Cannot re-include if parent is excluded per gitignore spec
285 + expect(matcher.ignores("logs/important/")).toBe(true);
286 + });
287 +});
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);
295 + });
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);
301 + });
302 +});
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);
310 + });
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);
318 + });
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);
326 + });
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);
335 + });
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);
341 + });
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);
348 + });
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);
355 + });
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);
361 + });
362 +});
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);
368 + });
370 + test("pattern . is invalid", () => {
371 + const matcher = m().add(".\n");
372 + expect(matcher.ignores(".")).toBe(false);
373 + });
375 + test("pattern .. is invalid", () => {
376 + const matcher = m().add("..\n");
377 + expect(matcher.ignores("..")).toBe(false);
378 + });
380 + test("pattern starting with ./ is invalid", () => {
381 + const matcher = m().add("./foo\n");
382 + expect(matcher.ignores("foo")).toBe(false);
383 + });
385 + test("pattern starting with ../ is invalid", () => {
386 + const matcher = m().add("../foo\n");
387 + expect(matcher.ignores("foo")).toBe(false);
388 + });
390 + test("consecutive slashes in pattern", () => {
391 + const matcher = m().add("a//b\n");
392 + expect(matcher.ignores("a//b")).toBe(true);
393 + });
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);
398 + });
400 + test("unicode in pattern", () => {
401 + const matcher = m().add("*.äöü\n");
402 + expect(matcher.ignores("file.äöü")).toBe(true);
403 + });
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);
409 + });
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);
415 + });
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);
422 + });
424 + test("empty string add is no-op", () => {
425 + const matcher = m().add("");
426 + expect(matcher.ignores("a")).toBe(false);
427 + });
429 + test("pattern ending with just / after stripping is invalid", () => {
430 + const matcher = m().add("/\n");
431 + expect(matcher.ignores("/")).toBe(false);
432 + });
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);
437 + });
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);
442 + });
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);
449 + });
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);
455 + });
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);
462 + });
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);
468 + // Does NOT match Makefile.am — different basename
469 + expect(matcher.ignores("src/Makefile.am")).toBe(false);
470 + });
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);
477 + });
479 + test("pattern with all special chars escaped", () => {
480 + const matcher = m().add("\\.\\[\\*\\?\\#\\!\\/\\]\n");
481 + expect(matcher.ignores(".[*?#!/]")).toBe(true);
482 + });
483 +});
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);
490 + });
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);
497 + });
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));
506 + });
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);
514 + });
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);
523 + });
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;
1 3 interface Rule {
2 4 negative: boolean;
3 5 directoryOnly: boolean;
4 - regex: RegExp;
6 + test: TestFn;
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);
23 24 // Check parent directories first.
24 25 // If a parent is ignored, the child is too.
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);
27 + while (slash > 0) {
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);
34 + }
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);
42 + return 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 {
46 58 // Skip blank lines and comments.
47 - const trimmed = line.trim();
59 + const trimmed = line.trimStart();
48 60 if (!trimmed || trimmed.startsWith("#")) return null;
50 62 // Handle negation.
59 71 if (pattern.startsWith("\\!")) pattern = "!" + pattern.slice(2);
60 72 if (pattern.startsWith("\\#")) pattern = "#" + pattern.slice(2);
74 + // Strip unescaped trailing spaces.
75 + let end = pattern.length;
76 + while (end > 0 && pattern[end - 1] === ' ' && (end === 1 || pattern[end - 2] !== '\\')) {
77 + end--;
78 + }
79 + pattern = pattern.slice(0, end);
62 81 // Directory-only flag.
63 82 const directoryOnly = pattern.endsWith("/");
64 83 if (directoryOnly) pattern = pattern.slice(0, -1);
85 + // Invalid: trailing backslash.
86 + if (pattern.endsWith("\\")) return null;
66 88 // Skip patterns that resolve to empty or invalid.
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("/");
102 + // Detect if pattern has a slash that's not at the very start (after **/)
103 + // and not part of a leading **/ prefix. Patterns with middle slashes are
104 + // relative to the .gitignore location, not any depth.
105 + const isRelative =
106 + hasSlash && !anchored && !pattern.startsWith("**/");
108 + // --- Fast path: simple exact-match patterns ---
109 + // e.g. "node_modules/", ".DS_Store", "Makefile"
110 + if (!anchored && !hasSpecialChars(pattern)) {
111 + if (directoryOnly) {
112 + if (isRelative) {
113 + return (p) => p === `${pattern}/` || p.startsWith(`${pattern}/`);
115 + return (p) => p === `${pattern}/` || p.endsWith(`/${pattern}/`);
117 + // File pattern that also matches directories (gitignore: no trailing slash = both)
118 + if (isRelative) {
119 + return (p) => p === pattern || p === `${pattern}/` || p.startsWith(`${pattern}/`);
121 + return (p) => {
122 + const norm = p.endsWith('/') ? p.slice(0, -1) : p;
123 + return norm === pattern || norm.endsWith(`/${pattern}`);
124 + };
127 + // --- Fast path: anchored exact-match ---
128 + // e.g. "/Makefile", "/build/"
129 + if (anchored && !hasSpecialChars(pattern)) {
130 + if (directoryOnly) {
131 + return (p) => p === `${pattern}/`;
133 + return (p) => p === pattern || p.startsWith(`${pattern}/`);
136 + // --- Fast path: suffix patterns (*.ext) ---
137 + // e.g. "*.log", "*.tmp", "*.min.js"
138 + if (!anchored && !hasSlash && pattern.startsWith("*.") && !hasSpecialChars(pattern.slice(2))) {
139 + const suffix = pattern.slice(1); // includes the dot
140 + return (p) => {
141 + const slash = p.lastIndexOf("/");
142 + const basename = slash >= 0 ? p.slice(slash + 1) : p;
143 + return basename.endsWith(suffix);
144 + };
147 + // --- Fast path: prefix patterns (foo*) ---
148 + if (!anchored && !hasSlash && pattern.endsWith("*") && pattern.indexOf("*") === pattern.length - 1 && !hasSpecialChars(pattern.slice(0, -1))) {
149 + const prefix = pattern.slice(0, -1);
150 + return (p) => {
151 + const slash = p.lastIndexOf("/");
152 + const basename = slash >= 0 ? p.slice(slash + 1) : p;
153 + return basename.startsWith(prefix);
154 + };
157 + // --- Fast path: relative directory patterns ---
158 + // e.g. "doc/frotz/" — relative to gitignore dir
159 + if (isRelative && directoryOnly && !hasSpecialChars(pattern)) {
160 + return (p) => p === `${pattern}/` || p.startsWith(`${pattern}/`);
163 + // --- Fallback: regex ---
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++) {
170 + const c = s[i];
171 + if (c === "*" || c === "?" || c === "[" || c === "\\") return true;
173 + return false;
176 +function buildRegex(pattern: string, anchored: boolean, isRelative: boolean): RegExp | null {
80 177 let src = "";
81 178 let i = 0;
82 179 while (i < pattern.length) {
96 193 if (ch === "*") {
97 194 if (pattern[i + 1] === "*") {
98 - // **
99 195 if (pattern[i + 2] === "/") {
100 - // **/
101 196 src += "(?:.*\\/)?";
102 197 i += 3;
103 198 continue;
105 200 if (i + 2 === pattern.length) {
106 - // trailing **
107 - src += ".*";
201 + // trailing ** must match at least one char (everything INSIDE)
202 + src += ".+";
108 203 i += 2;
109 204 continue;
111 - // Double star not followed by slash in the middle — treat as two stars.
112 206 src += "[^/]*";
114 208 continue;
132 226 continue;
134 228 let range = pattern.slice(i + 1, end);
135 - // Sanitize invalid ranges for regex.
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 : "",
232 + // Gitignore uses [!...] for negated classes, regex uses [^...]
233 + if (range.startsWith("!")) {
234 + range = "^" + range.slice(1);
139 236 src += `[${range}]`;
140 237 i = end + 1;
141 238 continue;
148 245 if (!src) return null;
150 247 let body: string;
151 - if (anchored) {
248 + if (anchored || isRelative) {
152 249 body = `^${src}(?=$|\/)`;
153 - } else if (hasSlash) {
154 - // Contains slash → match relative to root, but also at any depth.
250 + } else if (pattern.includes("/")) {
155 251 body = `^(?:.*\/)?${src}(?=$|\/)`;
156 252 } else {
157 - // No slash → match basename at any depth.
158 - body = `^(?:[^/]*\/)*?${src}(?=$|\/)`;
253 + body = `^(?:[^/]*\/)??${src}(?=$|\/)`;
161 256 return new RegExp(body);