pi-ext
bugabingas pi extensions
owned by bugabinga
refactor(llmiterate): inline gitignore matcher, remove ignore dependency
9614c2e36dcb1824204e8bc39635e7848c501789
Oliver Jan Krylow <oliver@bugabinga.net> ·
unsigned
Browse filesCompare with HEAD
Changed files (4)
extensions/llmiterate/__tests__/gitignore.test.ts
Addednew file mode 100644 1 +import { describe, expect, test }
from "bun:test";
2 +import { GitIgnoreMatcher }
from "../gitignore";
4 +function m()
: GitIgnoreMatcher {
5 + return new GitIgnoreMatcher();
8 +describe(
"GitIgnoreMatcher", ()
=> {
9 + test(
"empty matcher ignores nothing", ()
=> {
10 + const matcher
= m();
11 + expect(matcher.
ignores(
"a.ts")).
toBe(
false);
12 + expect(matcher.
ignores(
"src/a.ts")).
toBe(
false);
15 + test(
"comments and blank lines are skipped", ()
=> {
16 + const matcher
= m().
add(
"# comment\n\n \n*.log\n");
17 + 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);
29 + test(
"*.tmp ignores files at any depth", ()
=> {
30 + const matcher
= m().
add(
"*.tmp\n");
31 + expect(matcher.
ignores(
"a.tmp")).
toBe(
true);
32 + expect(matcher.
ignores(
"src/b.tmp")).
toBe(
true);
33 + expect(matcher.
ignores(
"a.ts")).
toBe(
false);
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);
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", ()
=> {
75 + const matcher
= m().
add(
"*.[to]mp\n");
76 + expect(matcher.
ignores(
"a.tmp")).
toBe(
true);
77 + expect(matcher.
ignores(
"a.omp")).
toBe(
true);
78 + expect(matcher.
ignores(
"a.xmp")).
toBe(
false);
81 + test(
"escaped specials are treated as literals", ()
=> {
82 + const matcher
= m().
add(
"\\#file\n\\!important\nstar\\*.txt\n");
83 + expect(matcher.
ignores(
"#file")).
toBe(
true);
84 + expect(matcher.
ignores(
"!important")).
toBe(
true);
85 + expect(matcher.
ignores(
"star*.txt")).
toBe(
true);
86 + expect(matcher.
ignores(
"#other")).
toBe(
false);
89 + test(
"trailing-slash semantics: build/ ignores only directories", ()
=> {
90 + const matcher
= m().
add(
"build/\n");
91 + expect(matcher.
ignores(
"build/")).
toBe(
true);
92 + expect(matcher.
ignores(
"build/output.js")).
toBe(
true);
97 + expect(matcher.
ignores(
"build")).
toBe(
false);
100 + test(
"pattern without trailing slash ignores files and directories", ()
=> {
101 + const matcher
= m().
add(
"build\n");
102 + expect(matcher.
ignores(
"build")).
toBe(
true);
103 + expect(matcher.
ignores(
"build/")).
toBe(
true);
104 + expect(matcher.
ignores(
"src/build")).
toBe(
true);
107 + test(
"multiple negations in sequence", ()
=> {
108 + const matcher
= m().
add(
"*.log\n!a.log\n!b.log\n");
109 + expect(matcher.
ignores(
"c.log")).
toBe(
true);
110 + expect(matcher.
ignores(
"a.log")).
toBe(
false);
111 + 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", ()
=> {
144 + const matcher
= m().
add(
"dist/\n!dist/keep.txt\n");
145 + 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);
extensions/llmiterate/gitignore.ts
Addednew file mode 100644 3 + directoryOnly
: boolean;
7 +export class GitIgnoreMatcher {
8 + private readonly rules
: Rule[]
= [];
10 + add(source
: string)
: this {
11 + for (
const line
of source.
split(
/\r?\n/)) {
12 + const rule
= parseRule(line);
13 + if (rule) this.
rules.
push(rule);
18 + ignores(inputPath
: string)
: boolean {
19 + const path
= inputPath.
replace(
/\/+$/,
"");
20 + 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;
30 + return this.
_test(inputPath, isDir);
33 + private _test(path
: string, isDir
: boolean)
: boolean {
34 + let ignored
= false;
35 + for (
const rule
of this.
rules) {
36 + if (rule.directoryOnly
&& !isDir)
continue;
37 + if (rule.regex.
test(path)) {
38 + ignored
= !rule.negative;
45 +function parseRule(line
: string)
: Rule | null {
47 + const trimmed
= line.
trim();
48 + if (
!trimmed
|| trimmed.
startsWith(
"#"))
return null;
51 + let pattern
= trimmed;
52 + let negative
= false;
53 + if (pattern.
startsWith(
"!")) {
55 + pattern
= pattern.
slice(
1);
59 + if (pattern.
startsWith(
"\\!")) pattern
= "!" + pattern.
slice(
2);
60 + if (pattern.
startsWith(
"\\#")) pattern
= "#" + pattern.
slice(
2);
63 + const directoryOnly
= pattern.
endsWith(
"/");
64 + if (directoryOnly) pattern
= pattern.
slice(
0,
-1);
67 + if (
!pattern
|| pattern
=== "." || pattern
=== "..")
return null;
68 + if (pattern.
startsWith(
"../")
|| pattern.
startsWith(
"./"))
return null;
70 + const regex
= buildRegex(pattern);
71 + return regex
? { negative, directoryOnly, regex }
: null;
74 +function buildRegex(pattern
: string)
: RegExp | null {
75 + const anchored
= pattern.
startsWith(
"/");
76 + if (anchored) pattern
= pattern.
slice(
1);
78 + const hasSlash
= pattern.
includes(
"/");
82 + while (i
< pattern.
length) {
83 + const ch
= pattern[i];
86 + const next
= pattern[i
+ 1];
88 + src
+= escapeRegex(next);
97 + if (pattern[i
+ 1]
=== "*") {
99 + if (pattern[i
+ 2]
=== "/") {
101 + src
+= "(?:.*\\/)?";
105 + if (i
+ 2 === pattern.
length) {
128 + const end
= pattern.
indexOf(
"]", i
+ 1);
134 + let range
= pattern.
slice(i
+ 1, end);
136 + range
= range.
replace(
/([0-z])-([0-z])/g, (m, a, b)
=> 137 + a.
charCodeAt(
0)
<= b.
charCodeAt(
0)
? m
: "",
139 + src
+= `[${range}]`;
144 + src
+= escapeRegex(ch);
148 + if (
!src)
return null;
152 + body
= `^${src}(?=$|\/)`;
153 + }
else if (hasSlash) {
155 + body
= `^(?:.*\/)?${src}(?=$|\/)`;
158 + body
= `^(?:[^/]*\/)*?${src}(?=$|\/)`;
161 + return new RegExp(body);
164 +function escapeRegex(ch
: string)
: string {
165 + return /[.*+?^${}()|[\]\\]/.
test(ch)
? `\\${ch}` : ch;
extensions/llmiterate/package.json
Modified 6 6 "test":
"bun test ./__tests__/*.test.ts",
7 7 "test:harness":
"bun test ./__tests__/harness.test.ts" 12 10 "peerDependencies": {
13 11 "@earendil-works/pi-coding-agent":
"*",
14 12 "@earendil-works/pi-tui":
"*" extensions/llmiterate/watcher.ts
Modified 1 1 import * as fs
from "node:fs";
2 2 import * as path
from "node:path";
3 -import ignore, { type Ignore }
from "ignore";
4 3 import type { LlmiterateConfig }
from "./config";
4 +import { GitIgnoreMatcher }
from "./gitignore";
6 6 type CompiledPathFilter,
7 7 shouldIncludeCompiledPath,
23 23 interface IgnoreContext {
24 + matcher
: GitIgnoreMatcher;
333 333 if (parent
!== absDir
&& isDescendant(this.root, absDir))
334 334 this.
loadIgnoreContext(parent);
336 - const matcher
= ignore();
336 + const matcher
= new GitIgnoreMatcher();
337 337 for (
const name
of IGNORE_FILE_NAMES) {
339 339 matcher.
add(fs.
readFileSync(path.
join(absDir, name),
"utf-8"));