repositories / pi-ext

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

Added
new file mode 100644
1 +import { describe, expect, test } from "bun:test";
2 +import { GitIgnoreMatcher } from "../gitignore";
4 +function m(): GitIgnoreMatcher {
5 + return new GitIgnoreMatcher();
6 +}
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);
13 + });
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);
19 + });
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);
27 + });
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);
34 + });
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);
40 + });
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", () => {
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);
79 + });
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);
87 + });
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);
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 + expect(matcher.ignores("build")).toBe(false);
98 + });
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);
105 + });
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);
112 + });
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", () => {
144 + const matcher = m().add("dist/\n!dist/keep.txt\n");
145 + expect(matcher.ignores("dist/keep.txt")).toBe(true);
146 + });
148 + test("add returns this for chaining", () => {
149 + const matcher = m();
150 + expect(matcher.add("*.log")).toBe(matcher);
151 + });
152 +});

extensions/llmiterate/gitignore.ts

Added
new file mode 100644
1 +interface Rule {
2 + negative: boolean;
3 + directoryOnly: boolean;
4 + regex: RegExp;
5 +}
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);
14 + }
15 + return this;
16 + }
18 + ignores(inputPath: string): boolean {
19 + const path = inputPath.replace(/\/+$/, "");
20 + const isDir = inputPath.endsWith("/");
21 + const parts = path.split("/").filter(Boolean);
23 + // Check parent directories first.
24 + // 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;
28 + }
30 + return this._test(inputPath, isDir);
31 + }
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;
39 + }
40 + }
41 + return ignored;
42 + }
45 +function parseRule(line: string): Rule | null {
46 + // Skip blank lines and comments.
47 + const trimmed = line.trim();
48 + if (!trimmed || trimmed.startsWith("#")) return null;
50 + // Handle negation.
51 + let pattern = trimmed;
52 + let negative = false;
53 + if (pattern.startsWith("!")) {
54 + negative = true;
55 + pattern = pattern.slice(1);
56 + }
58 + // Handle leading escaped `!` or `#`.
59 + if (pattern.startsWith("\\!")) pattern = "!" + pattern.slice(2);
60 + if (pattern.startsWith("\\#")) pattern = "#" + pattern.slice(2);
62 + // Directory-only flag.
63 + const directoryOnly = pattern.endsWith("/");
64 + if (directoryOnly) pattern = pattern.slice(0, -1);
66 + // Skip patterns that resolve to empty or invalid.
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("/");
80 + let src = "";
81 + let i = 0;
82 + while (i < pattern.length) {
83 + const ch = pattern[i];
85 + if (ch === "\\") {
86 + const next = pattern[i + 1];
87 + if (next) {
88 + src += escapeRegex(next);
89 + i += 2;
90 + continue;
91 + }
92 + i++;
93 + continue;
94 + }
96 + if (ch === "*") {
97 + if (pattern[i + 1] === "*") {
98 + // **
99 + if (pattern[i + 2] === "/") {
100 + // **/
101 + src += "(?:.*\\/)?";
102 + i += 3;
103 + continue;
105 + if (i + 2 === pattern.length) {
106 + // trailing **
107 + src += ".*";
108 + i += 2;
109 + continue;
111 + // Double star not followed by slash in the middle — treat as two stars.
112 + src += "[^/]*";
113 + i++;
114 + continue;
116 + src += "[^/]*";
117 + i++;
118 + continue;
121 + if (ch === "?") {
122 + src += "[^/]";
123 + i++;
124 + continue;
127 + if (ch === "[") {
128 + const end = pattern.indexOf("]", i + 1);
129 + if (end === -1) {
130 + src += "\\[";
131 + i++;
132 + continue;
134 + let range = pattern.slice(i + 1, end);
135 + // Sanitize invalid ranges for regex.
136 + range = range.replace(/([0-z])-([0-z])/g, (m, a, b) =>
137 + a.charCodeAt(0) <= b.charCodeAt(0) ? m : "",
138 + );
139 + src += `[${range}]`;
140 + i = end + 1;
141 + continue;
144 + src += escapeRegex(ch);
145 + i++;
148 + if (!src) return null;
150 + let body: string;
151 + if (anchored) {
152 + body = `^${src}(?=$|\/)`;
153 + } else if (hasSlash) {
154 + // Contains slash → match relative to root, but also at any depth.
155 + body = `^(?:.*\/)?${src}(?=$|\/)`;
156 + } else {
157 + // No slash → match basename at any depth.
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"
9 - "dependencies": {
10 - "ignore": "^7.0.5"
11 - },
9 + "dependencies": {},
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";
5 5 import {
6 6 type CompiledPathFilter,
7 7 shouldIncludeCompiledPath,
23 23 interface IgnoreContext {
24 - matcher: Ignore;
24 + matcher: GitIgnoreMatcher;
25 25 stamp: string;
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"));