pi-ext
bugabingas pi extensions
owned by bugabinga
refactor(gitignore): clean up slop — no perf loss
1cbb79ef6aa1e22c83e757afc0cc7e586a2d9663
Oliver Jan Krylow <oliver@bugabinga.net> ·
unsigned
Browse filesCompare with HEAD
Changed files (1)
extensions/llmiterate/gitignore.ts
Modified 1 2 type TestFn = (path
: string)
=> boolean;
5 8 directoryOnly
: boolean;
14 +const CACHE_DIR
= "d:";
15 +const CACHE_FILE
= "f:";
18 +const GLOB_SPECIAL
= new Set<
string>([
"*",
"?",
"[",
"\\"]);
21 +const REGEX_META
= new Set<
string>(
".*+?^${}()|[]\\".
split(
""));
9 35 export class GitIgnoreMatcher {
10 36 private readonly rules
: Rule[]
= [];
11 37 private readonly cache
= new Map<
string,
boolean>();
13 44 add(source
: string)
: this {
14 45 for (
const line
of source.
split(
/\r?\n/)) {
15 46 const rule
= parseRule(line);
21 58 ignores(inputPath: string): boolean {
22 59 const isDir
= inputPath.
endsWith(
"/");
26 62 let slash
= inputPath.
lastIndexOf(
"/", inputPath.
length - 2);
27 63 while (slash
> 0) {
28 - const parent
= inputPath.
slice(
0, slash
+ 1);
29 - if (this.
_testCached(parent,
true))
return true;
64 + if (this.
_testCached(inputPath.
slice(
0, slash
+ 1),
true))
return true;
30 65 slash
= inputPath.
lastIndexOf(
"/", slash
- 1);
36 71 private
_testCached(path: string, isDir: boolean): boolean {
37 - const key
= isDir
? `d:${path}` : `f:${path}`;
72 + const key
= isDir
? CACHE_DIR
+ path
: CACHE_FILE
+ path;
38 73 const cached
= this.cache.
get(key);
39 74 if (cached
!== undefined)
return cached;
40 75 const result
= this.
_test(path, isDir);
57 -function parseRule(line
: string)
: Rule | null {
59 - const trimmed
= line.
trimStart();
60 - if (
!trimmed
|| trimmed.
startsWith(
"#"))
return null;
63 - let pattern
= trimmed;
94 +function parseRule(rawLine
: string)
: Rule | null {
96 + const line
= rawLine.
trimStart();
97 + if (
!line
|| line[
0]
=== "#")
return null;
100 + let pattern
= line;
64 101 let negative
= false;
65 - if (pattern.
startsWith(
"!")) {
102 + if (pattern[
0]
=== "!") {
67 104 pattern
= pattern.
slice(
1);
71 108 if (pattern.
startsWith(
"\\!")) pattern
= "!" + pattern.
slice(
2);
72 109 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);
112 + pattern
= stripTrailingSpaces(pattern);
82 115 const directoryOnly
= pattern.
endsWith(
"/");
83 116 if (directoryOnly) pattern
= pattern.
slice(
0,
-1);
86 119 if (pattern.
endsWith(
"\\"))
return null;
89 120 if (
!pattern
|| pattern
=== "." || pattern
=== "..")
return null;
90 121 if (pattern.
startsWith(
"../")
|| pattern.
startsWith(
"./"))
return null;
93 124 return test
? { negative, directoryOnly, test }
: null;
96 -function compileTest(pattern
: string, directoryOnly
: boolean)
: TestFn | null {
97 - const anchored
= pattern.
startsWith(
"/");
128 +function stripTrailingSpaces(s
: string)
: string {
129 + let end
= s.
length;
131 + if (s[end
- 1]
!== " ")
break;
132 + if (end
> 1 && s[end
- 2]
=== "\\")
break;
135 + return end
=== s.
length ? s
: s.
slice(
0, end);
140 +interface PatternInfo {
146 + isRelative
: boolean;
149 +function analyzePattern(raw
: string)
: { pattern
: string; info
: PatternInfo } {
151 + const anchored
= pattern[
0]
=== "/";
98 152 if (anchored) pattern
= pattern.
slice(
1);
100 154 const hasSlash
= pattern.
includes(
"/");
155 + const isRelative
= hasSlash
&& !anchored
&& !pattern.
startsWith(
"**/");
106 - hasSlash
&& !anchored
&& !pattern.
startsWith(
"**/");
157 + return { pattern, info: { anchored, hasSlash, isRelative } };
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}`);
161 +function basenameOf(p
: string)
: string {
162 + const i
= p.
lastIndexOf(
"/");
163 + return i
>= 0 ? p.
slice(i
+ 1)
: p;
167 +function hasSpecialChars(s
: string)
: boolean {
168 + for (
let i
= 0; i
< s.
length; i
++) {
169 + if (GLOB_SPECIAL.
has(s[i]))
return true;
180 +function compileTest(rawPattern
: string, directoryOnly
: boolean)
: TestFn | null {
181 + const { pattern, info }
= analyzePattern(rawPattern);
182 + const { anchored, isRelative }
= info;
185 + if (
!hasSpecialChars(pattern)) {
186 + return compileLiteral(pattern, anchored, isRelative, directoryOnly);
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))) {
190 + if (
!anchored
&& !info.hasSlash
&& pattern.
startsWith(
"*.")
&& !hasSpecialChars(pattern.
slice(
2))) {
139 191 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);
192 + return (p)
=> basenameOf(p).
endsWith(suffix);
148 - if (
!anchored
&& !hasSlash
&& pattern.
endsWith(
"*")
&& pattern.
indexOf(
"*")
=== pattern.
length - 1 && !hasSpecialChars(pattern.
slice(
0,
-1))) {
196 + if (
!anchored
&& !info.hasSlash
&& pattern.
endsWith(
"*")
&& pattern.
indexOf(
"*")
=== pattern.
length - 1 && !hasSpecialChars(pattern.
slice(
0,
-1))) {
149 197 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);
198 + return (p)
=> basenameOf(p).
startsWith(prefix);
159 - if (isRelative
&& directoryOnly
&& !hasSpecialChars(pattern)) {
160 - return (p)
=> p
=== `${pattern}/` || p.
startsWith(
`${pattern}/`);
164 202 const regex
= buildRegex(pattern, anchored, isRelative);
165 203 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;
207 +function compileLiteral(
210 + isRelative
: boolean,
211 + directoryOnly
: boolean,
213 + if (directoryOnly) {
214 + if (anchored
|| isRelative) {
215 + return (p)
=> p
=== `${pattern}/` || p.
startsWith(
`${pattern}/`);
217 + return (p)
=> p
=== `${pattern}/` || p.
endsWith(
`/${pattern}/`);
220 + if (anchored
|| isRelative) {
222 + const norm
= p.
endsWith(
"/")
? p.
slice(
0,
-1)
: p;
223 + return norm
=== pattern
|| norm.
startsWith(
`${pattern}/`);
228 + const norm
= p.
endsWith(
"/")
? p.
slice(
0,
-1)
: p;
229 + return norm
=== pattern
|| norm.
endsWith(
`/${pattern}`);
176 235 function buildRegex(pattern
: string, anchored
: boolean, isRelative
: boolean)
: RegExp | null {
200 259 if (i
+ 2 === pattern.
length) {
221 279 if (ch
=== "[") {
222 - const end
= pattern.
indexOf(
"]", i
+ 1);
280 + const segment
= parseCharClass(pattern, i);
281 + if (segment
=== null) {
228 - let range
= pattern.
slice(i
+ 1, end);
229 - range
= range.
replace(
/([0-z])-([0-z])/g, (m, a, b)
=> 230 - a.
charCodeAt(
0)
<= b.
charCodeAt(
0)
? m
: "",
233 - if (range.
startsWith(
"!")) {
234 - range
= "^" + range.
slice(
1);
236 - src
+= `[${range}]`;
287 + i
= pattern.
indexOf(
"]", i
+ 1)
+ 1;
256 306 return new RegExp(body);
318 +function parseCharClass(pattern
: string, openIndex
: number)
: string | null {
319 + const close
= pattern.
indexOf(
"]", openIndex
+ 1);
320 + if (close
=== -1)
return null;
322 + let content
= pattern.
slice(openIndex
+ 1, close);
325 + content
= content.
replace(
/([0-z])-([0-z])/g, (m, a, b)
=> 326 + a.
charCodeAt(
0)
<= b.
charCodeAt(
0)
? m
: "",
330 + if (content[
0]
=== "!") {
331 + content
= "^" + content.
slice(
1);
334 + return `[${content}]`;
259 338 function escapeRegex(ch
: string)
: string {
260 - return /[.*+?^${}()|[\]\\]/.
test(ch)
? `\\${ch}` : ch;
339 + return REGEX_META.
has(ch)
? `\\${ch}` : ch;