@muhkoo/cli 0.14.0-alpha.8 → 0.15.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/lib/ignore.js CHANGED
@@ -9,9 +9,19 @@
9
9
  * open, fixtures measured in megabytes.
10
10
  *
11
11
  * `.gitignore` is still used as a fallback when no `.vcsignore` exists, so a
12
- * project that has not opted in gets sensible behaviour on day one.
12
+ * project that has not opted in gets sensible behaviour on day one — and then
13
+ * the way git reads it: nested `.gitignore` files scoped to their directories,
14
+ * and `.git/info/exclude`. Stopping at the root file uploaded what a monorepo
15
+ * package's own `.gitignore` kept out.
16
+ *
17
+ * The matcher is hand-written rather than generated RegExps. These files travel
18
+ * with the project, so anyone able to write the filesystem can write a pattern
19
+ * here, and a generated `.*a.*a.*a…` backtracked for longer than anyone would
20
+ * wait — synchronously, so not even Ctrl-C got through.
13
21
  */
14
22
 
23
+ import { warn } from "./ui.js";
24
+
15
25
  /** What a Node project almost always wants ignored. */
16
26
  export const NODE_DEFAULTS = `# Dependencies and build output — reinstallable, and large.
17
27
  node_modules/
@@ -41,6 +51,53 @@ yarn-error.log*
41
51
  !.env.example
42
52
  `;
43
53
 
54
+ /**
55
+ * Code points a filesystem may drop when it compares names.
56
+ *
57
+ * HFS+ ignores the zero-width joiners and direction marks outright, so
58
+ * `node\u200c_modules` opens `node_modules`. Only used when matching on a volume
59
+ * that folds names; a written name is never altered.
60
+ */
61
+ export const IGNORABLE = /[\u00ad\u200b-\u200f\u202a-\u202e\u2060-\u206f\ufeff]/g;
62
+
63
+ /**
64
+ * A string as a volume that folds names would compare it.
65
+ *
66
+ * APFS folds case the FULL Unicode way, not one character at a time: `ſ` is
67
+ * `s`, `ß` is `ss`, the `fi` and `st` ligatures are `fi` and `st`. A per-character
68
+ * `toLowerCase` missed all of them, so a remote `config/maſter.key` passed a
69
+ * `config/master.key` rule, was pulled, and the developer's later write of the
70
+ * real key opened the planted entry and uploaded. `toUpperCase` is what expands
71
+ * `ß` to `SS` (lower case first, so capital `ẞ` gets there too); NFKC takes
72
+ * the compatibility forms. This folds further than any
73
+ * one filesystem does, which is safe here: over-folding only ever excludes and
74
+ * refuses more. For comparing only — a written name is never altered.
75
+ */
76
+ export function caseFold(s) {
77
+ return String(s)
78
+ .replace(IGNORABLE, "")
79
+ .normalize("NFKC")
80
+ .toLowerCase()
81
+ .toUpperCase()
82
+ .toLowerCase()
83
+ .normalize("NFKC")
84
+ .replace(IGNORABLE, "");
85
+ }
86
+
87
+ /**
88
+ * Text as this volume compares names: fully folded when it folds case, and
89
+ * NFC regardless — a case-SENSITIVE APFS volume is still insensitive to
90
+ * normalization, so `café` and `cafe\u0301` are one entry there too.
91
+ */
92
+ function fold(s, ignoreCase) {
93
+ return ignoreCase ? caseFold(s) : String(s).normalize("NFC");
94
+ }
95
+
96
+ const GLOBSTAR = "**";
97
+ /** Exactly one segment, whatever it is: a trailing `**` has to consume one. */
98
+ const ANYSEG = "*/";
99
+ const STAR = 1, ANY = 2, LIT = 3, CLASS = 4;
100
+
44
101
  /**
45
102
  * Compile ignore rules into a matcher.
46
103
  *
@@ -48,127 +105,876 @@ yarn-error.log*
48
105
  * relative to the directory the ignore file sits in, with no leading slash.
49
106
  *
50
107
  * Last match wins, which is what makes `!` negation work: `*.log` followed by
51
- * `!keep.log` keeps that one file.
108
+ * `!keep.log` keeps that one file. And, as in git, a path under an excluded
109
+ * directory is excluded whatever a later rule says (see `underExcluded`).
110
+ *
111
+ * Options: `ignoreCase` folds case and Unicode normalization, for a volume
112
+ * that does (git's `core.ignorecase`); `onBadLine(line, reason)` hears about a
113
+ * line that could not be read, which is skipped rather than fatal.
114
+ */
115
+ export function compileIgnores(text, opts = {}) {
116
+ const rules = parseRules(text, opts);
117
+ return (path, isDir = false) => {
118
+ const subj = subject(String(path).split("/").filter(Boolean), opts.ignoreCase, opts.budget ?? Infinity);
119
+ const at = (k, dir) => lastMatch(rules, subj, 0, k, dir) === IGNORED;
120
+ return underExcluded(subj.raw.length, at) || at(subj.raw.length, isDir);
121
+ };
122
+ }
123
+
124
+ /**
125
+ * Is any directory above a path of `n` segments excluded? `at(k, true)` is
126
+ * the verdict for the directory made of the first k segments, on its own.
127
+ *
128
+ * Git never looks inside an excluded directory, so nothing beneath one can be
129
+ * re-included — and, just as much, a negation that matches a directory keeps
130
+ * the DIRECTORY, not everything in it. Testing every rule against every
131
+ * prefix of the path, as the first rewrite did, let `!keys/` override
132
+ * `*.pem` for `keys/server.pem`, and the whitelist idiom (`*`, then `!` for
133
+ * every directory, then `!*.js`) upload every file under a re-included one. Each directory is decided
134
+ * by the rules that match IT; the path by the rules that match it.
52
135
  */
53
- export function compileIgnores(text) {
136
+ function underExcluded(n, at) {
137
+ for (let k = 1; k < n; k++) if (at(k, true)) return true;
138
+ return false;
139
+ }
140
+
141
+ /**
142
+ * Parse every line on its own.
143
+ *
144
+ * One line that cannot be read is skipped with a warning. Compiling the whole
145
+ * file in one go meant a single `[z-a]` threw, and the caller read the throw as
146
+ * "no file here" — every rule in it silently gone, and files it was written to
147
+ * keep home uploaded. Git's own matcher treats a bad range as matching nothing,
148
+ * which is what skipping the line amounts to.
149
+ */
150
+ function parseRules(text, { ignoreCase = false, onBadLine, file } = {}) {
54
151
  const rules = [];
55
- for (const raw of String(text ?? "").split(/\r?\n/)) {
56
- const line = raw.trim();
57
- if (!line || line.startsWith("#")) continue;
58
-
59
- let pattern = line;
60
- let negated = false;
61
- if (pattern.startsWith("!")) {
62
- negated = true;
63
- pattern = pattern.slice(1);
152
+ for (const line of ruleLines(text)) {
153
+ try {
154
+ const rule = parseRule(line, ignoreCase);
155
+ if (rule) rules.push(rule);
156
+ } catch (err) {
157
+ if (onBadLine) onBadLine(line, err.message, file);
158
+ else warn(`ignoring "${line}" in ${file ?? "an ignore file"}: ${err.message}`);
64
159
  }
65
- // An escaped leading '#' or '!' is a literal.
66
- if (pattern.startsWith("\\")) pattern = pattern.slice(1);
160
+ }
161
+ return rules;
162
+ }
163
+
164
+ /**
165
+ * The lines of an ignore file that are rules, read as git reads them: a
166
+ * leading space is part of the pattern, and trailing spaces are dropped
167
+ * unless a backslash escapes them (`foo\ ` is "foo "). Trimming both ends
168
+ * made ` !server.pem` — a literal pattern to git — a negation here.
169
+ *
170
+ * A byte-order mark at the very start is skipped, as git skips it: Windows
171
+ * editors save one, and it became part of the first pattern — which, on a
172
+ * volume that does not fold case, then matched nothing.
173
+ */
174
+ function ruleLines(text) {
175
+ const out = [];
176
+ for (const raw of String(text ?? "").replace(/^\ufeff/, "").split(/\r?\n/)) {
177
+ const line = trimTrailingSpaces(raw);
178
+ if (line && !line.startsWith("#")) out.push(line);
179
+ }
180
+ return out;
181
+ }
67
182
 
68
- let dirOnly = false;
69
- if (pattern.endsWith("/")) {
70
- dirOnly = true;
71
- pattern = pattern.slice(0, -1);
183
+ /**
184
+ * An ignore file's rules and nothing else, one per line — what it MEANS, so
185
+ * two files that differ only in comments, blank lines or line endings come
186
+ * out the same. Read back, it gives exactly the rules the file did.
187
+ */
188
+ export function ruleText(text) {
189
+ return ruleLines(text).join("\n");
190
+ }
191
+
192
+ /** git's trim_trailing_spaces: a backslash protects the character after it. */
193
+ function trimTrailingSpaces(line) {
194
+ let cut = -1;
195
+ for (let i = 0; i < line.length; i++) {
196
+ if (line[i] === " ") {
197
+ if (cut === -1) cut = i;
198
+ } else {
199
+ if (line[i] === "\\") {
200
+ i++;
201
+ if (i >= line.length) return line;
202
+ }
203
+ cut = -1;
72
204
  }
73
- if (!pattern) continue;
205
+ }
206
+ return cut === -1 ? line : line.slice(0, cut);
207
+ }
74
208
 
75
- // A pattern with a slash anywhere but the end is anchored to the root;
76
- // a bare name matches at any depth. This is the rule that makes
77
- // `node_modules/` catch nested copies while `/dist` catches only the
78
- // top-level one.
79
- const anchored = pattern.includes("/");
80
- if (anchored && pattern.startsWith("/")) pattern = pattern.slice(1);
209
+ /**
210
+ * The most wildcards one rule may hold — `*`, `?`, `[...]` and `**` segments,
211
+ * a run of stars counting once. Real rules have a handful: the densest lines
212
+ * in GitHub's templates spell a name case-insensitively a character at a time
213
+ * (`[Tt][Hh][Uu][Mm][Bb][Ss].[Dd][Bb]`, eight), and 32 is four times that.
214
+ * Each one is a place the matcher may have to try more than one way, so a
215
+ * line past it is skipped with a warning, wherever the file came from.
216
+ */
217
+ export const MAX_WILDCARDS = 32;
81
218
 
82
- rules.push({ ...toRegExp(pattern, anchored), negated, dirOnly });
219
+ function parseRule(line, ignoreCase) {
220
+ let pattern = line;
221
+ let negated = false;
222
+ if (pattern.startsWith("!")) {
223
+ negated = true;
224
+ pattern = pattern.slice(1);
83
225
  }
84
226
 
85
- return (path, isDir = false) => {
86
- let ignored = false;
87
- for (const rule of rules) {
88
- // A directory rule matches the directory itself only when the thing
89
- // being tested IS one — but it always matches what lives beneath it.
90
- // `node_modules/` has to catch `node_modules/react/index.js`, which
91
- // is a file, or pruning would ignore the directory and sync its
92
- // entire contents anyway.
93
- const hit = rule.under.test(path) || ((isDir || !rule.dirOnly) && rule.self.test(path));
94
- if (hit) ignored = !rule.negated;
227
+ let dirOnly = false;
228
+ if (pattern.endsWith("/")) {
229
+ dirOnly = true;
230
+ pattern = pattern.slice(0, -1);
231
+ }
232
+ if (!pattern) return null;
233
+
234
+ // A pattern with a slash anywhere but the end is anchored to the root;
235
+ // a bare name matches at any depth. This is the rule that makes
236
+ // `node_modules/` catch nested copies while `/dist` catches only the
237
+ // top-level one.
238
+ const anchored = pattern.includes("/");
239
+
240
+ // `**` is special only as a WHOLE segment (`**/x`, `x/**/y`, `x/**`).
241
+ // Anywhere else consecutive stars are an ordinary `*`, which is what git
242
+ // does — and what keeps `**a**a**a` from meaning "cross every separator".
243
+ // A run of `**` segments is one: the same pattern, and it keeps the work
244
+ // bounded by the PATH however many a planted line repeats.
245
+ //
246
+ // git reads any whole segment of two or more stars as `**`, so `***/x`
247
+ // matches x at every depth, as it does there.
248
+ const segs = [];
249
+ let wildcards = 0;
250
+ for (const part of pattern.split("/").filter(Boolean)) {
251
+ if (!/^\*{2,}$/.test(part)) {
252
+ const tokens = tokenize(part, ignoreCase);
253
+ wildcards += tokens.wildcards;
254
+ segs.push(tokens);
255
+ } else if (segs.at(-1) !== GLOBSTAR) {
256
+ segs.push(GLOBSTAR);
257
+ wildcards++;
95
258
  }
96
- return ignored;
259
+ }
260
+ if (wildcards > MAX_WILDCARDS) throw new Error(`it has ${wildcards} wildcards, and a rule may have at most ${MAX_WILDCARDS}`);
261
+ if (!segs.length) return null;
262
+ // An unanchored pattern may start at any segment boundary, which is how
263
+ // `*.log` matches `src/deep/thing.log`.
264
+ if (!anchored && segs[0] !== GLOBSTAR) segs.unshift(GLOBSTAR);
265
+ // `x/**` is everything INSIDE x, not x: a trailing `**` takes at least one.
266
+ if (segs.at(-1) === GLOBSTAR) segs.splice(-1, 1, ANYSEG, GLOBSTAR);
267
+ // The common case — `*.log`, `node_modules/`, `.env` — is "the last
268
+ // segment matches", and needs no walk over the path at all.
269
+ const base = segs.length === 2 && segs[0] === GLOBSTAR && Array.isArray(segs[1]) ? segs[1] : null;
270
+ return { segs, base, negated, dirOnly };
271
+ }
272
+
273
+ /**
274
+ * One segment of a pattern → tokens. `*`, `?`, `[...]` and `\x` escapes.
275
+ *
276
+ * Syntax is read from the pattern as written; only the LITERAL runs between
277
+ * it are folded, so a fullwidth `*` folds to a literal asterisk rather than
278
+ * becoming a wildcard, and `ß` folds to two literal characters.
279
+ */
280
+ function tokenize(part, ignoreCase) {
281
+ const chars = Array.from(part);
282
+ const out = [];
283
+ let run = "";
284
+ const flush = () => {
285
+ for (const c of Array.from(fold(run, ignoreCase))) out.push({ t: LIT, c });
286
+ run = "";
97
287
  };
288
+ for (let i = 0; i < chars.length; i++) {
289
+ const ch = chars[i];
290
+ if (ch === "\\" && i + 1 < chars.length) {
291
+ run += chars[++i];
292
+ } else if (ch === "[") {
293
+ const cls = parseClass(chars, i, ignoreCase);
294
+ if (!cls) {
295
+ run += ch; // unterminated: a literal bracket
296
+ continue;
297
+ }
298
+ flush();
299
+ out.push(cls.token);
300
+ i = cls.end;
301
+ } else if (ch === "*") {
302
+ flush();
303
+ if (out.at(-1)?.t !== STAR) out.push({ t: STAR }); // a run of stars is one star
304
+ } else if (ch === "?") {
305
+ flush();
306
+ out.push({ t: ANY });
307
+ } else run += ch;
308
+ }
309
+ flush();
310
+ // Shortcuts for the shapes nearly every rule has: all literal (`.env`),
311
+ // or one star and a literal suffix or prefix (`*.log`, `npm-debug.log*`).
312
+ const lits = (a) => (a.every((t) => t.t === LIT) ? a.map((t) => t.c).join("") : undefined);
313
+ out.lit = lits(out);
314
+ if (out.length > 1 && out[0].t === STAR) out.suffix = lits(out.slice(1));
315
+ if (out.length > 1 && out.at(-1).t === STAR) out.prefix = lits(out.slice(0, -1));
316
+ // Everything else, as the runs between stars: the one before the first
317
+ // star must start the name and the one after the last must end it, so
318
+ // only the runs in between are ever searched for.
319
+ const runs = [[]];
320
+ for (const tok of out) {
321
+ if (tok.t === STAR) runs.push([]);
322
+ else runs.at(-1).push(tok);
323
+ }
324
+ out.star = runs.length > 1;
325
+ out.head = runs[0];
326
+ out.tail = out.star ? runs.at(-1) : [];
327
+ out.mids = out.star ? runs.slice(1, -1).filter((run) => run.length) : [];
328
+ out.min = runs.reduce((n, run) => n + run.length, 0);
329
+ out.wildcards = out.filter((tok) => tok.t !== LIT).length;
330
+ // What matching it against one name costs (see `matchSegment`): the
331
+ // shortcuts their length; otherwise the runs compared in place, and the
332
+ // searched runs tried at each place in the name. Each search starts where
333
+ // the last one ended, so between them they try each place about once:
334
+ // (name + runs) tries, none dearer than the dearest run.
335
+ const shortcut = out.lit ?? out.suffix ?? out.prefix;
336
+ out.fixed = shortcut !== undefined ? Math.max(1, shortcut.length) : Math.max(1, runWork(out.head) + runWork(out.tail));
337
+ out.searched = shortcut !== undefined ? 0 : out.mids.reduce((most, run) => Math.max(most, runWork(run)), 0);
338
+ // Worth remembering, within one path, where a `**` makes it be tried
339
+ // more than once at the same place.
340
+ out.heavy = out.searched > 0 || out.fixed > 16;
341
+ out.uid = ++segmentIds;
342
+ return out;
98
343
  }
344
+ let segmentIds = 0;
99
345
 
100
346
  /**
101
- * Glob → RegExp, with gitignore's wildcard rules.
102
- *
103
- * `*` stops at a path separator, `**` crosses them, `?` is a single character
104
- * that is not a separator. An unanchored pattern may start at any segment
105
- * boundary, which is how `*.log` matches `src/deep/thing.log`.
106
- */
107
- function toRegExp(pattern, anchored) {
108
- let out = "";
109
- for (let i = 0; i < pattern.length; i++) {
110
- const ch = pattern[i];
111
- if (ch === "*") {
112
- if (pattern[i + 1] === "*") {
113
- // `**/` may match nothing at all, so `**/foo` also matches `foo`.
114
- if (pattern[i + 2] === "/") { out += "(?:.*/)?"; i += 2; }
115
- else { out += ".*"; i += 1; }
116
- } else out += "[^/]*";
117
- } else if (ch === "?") out += "[^/]";
118
- else if (ch === "[") {
119
- const close = pattern.indexOf("]", i + 1);
120
- if (close === -1) out += "\\[";
121
- else { out += pattern.slice(i, close + 1); i = close; }
122
- } else out += ch.replace(/[.+^${}()|\\]/g, "\\$&");
123
- }
124
- // Two regexes, because "is this the thing" and "is this inside the thing"
125
- // are answered differently for a directory-only rule.
126
- const head = anchored ? "^" : "^(?:.*/)?";
127
- return {
128
- self: new RegExp(`${head}${out}$`),
129
- under: new RegExp(`${head}${out}/.*$`),
347
+ * What one token costs to test against one character: one comparison, or for
348
+ * a class each of its members (a `[:name:]` is a RegExp test, weighed as
349
+ * POSIX_COST) — twice over on a volume that folds case, after upper-casing
350
+ * the character, which is dearer than the tests (FOLD_COST).
351
+ */
352
+ const POSIX_COST = 8;
353
+ const FOLD_COST = 8;
354
+ function tokenWork(tok) {
355
+ if (tok.t !== CLASS) return 1;
356
+ const members = tok.items.reduce((n, item) => n + (item.cls ? POSIX_COST : 1), 0);
357
+ return tok.fold ? 2 * members + FOLD_COST : members + 1;
358
+ }
359
+ const runWork = (run) => run.reduce((n, tok) => n + tokenWork(tok), 0);
360
+
361
+ /**
362
+ * The POSIX classes, in a Map: as an object literal, `[[:constructor:]]` and
363
+ * every other inherited name passed the "is this a class" check, compiled,
364
+ * and then threw on every match — outside the per-line skip, so one planted
365
+ * line failed every later pass.
366
+ */
367
+ const POSIX_CLASSES = new Map(Object.entries({
368
+ alnum: /[\p{L}\p{N}]/u, alpha: /\p{L}/u, blank: /[ \t]/, cntrl: /\p{Cc}/u, digit: /[0-9]/,
369
+ graph: /[^\s\p{Cc}]/u, lower: /\p{Ll}/u, print: /[^\p{Cc}]/u, punct: /[\p{P}\p{S}]/u,
370
+ space: /\s/, upper: /\p{Lu}/u, xdigit: /[0-9a-fA-F]/,
371
+ }));
372
+
373
+ /**
374
+ * The most members one `[...]` may list — a range or a `[:name:]` counting
375
+ * once. Every character a class meets is tested against each of them, so a
376
+ * planted class of 250,000 cost half a second a name while the rule counted
377
+ * as three wildcards. Real classes list a case pair, a digit range or a few
378
+ * separators (`[Tt]`, `[0-9]`, `[._-]`); 64 is far past any of them, and a
379
+ * line over it is skipped with a warning.
380
+ */
381
+ export const MAX_CLASS_ITEMS = 64;
382
+
383
+ /**
384
+ * `[...]` starting at `start`, or null when it never closes. Throws on a bad
385
+ * range. `fold` is whether the subject it meets is case-folded: only then may
386
+ * `[A-Z]` match the folded `a` — on a case-sensitive volume it must not.
387
+ */
388
+ function parseClass(chars, start, fold) {
389
+ let i = start + 1;
390
+ let neg = false;
391
+ if (chars[i] === "!" || chars[i] === "^") {
392
+ neg = true;
393
+ i++;
394
+ }
395
+ const items = [];
396
+ for (let first = true; i < chars.length; i++, first = false) {
397
+ let c = chars[i];
398
+ // `]` straight after the opening is a member, not the close.
399
+ if (c === "]" && !first) return { token: { t: CLASS, neg, items, fold: Boolean(fold) }, end: i };
400
+ if (items.length === MAX_CLASS_ITEMS) throw new Error(`a character class may list at most ${MAX_CLASS_ITEMS} members`);
401
+ if (c === "[" && chars[i + 1] === ":") {
402
+ const close = chars.indexOf(":", i + 2);
403
+ if (close !== -1 && chars[close + 1] === "]") {
404
+ const name = chars.slice(i + 2, close).join("");
405
+ const cls = POSIX_CLASSES.get(name);
406
+ if (!cls) throw new Error(`[:${name}:] is not a character class`);
407
+ items.push({ cls });
408
+ i = close + 1;
409
+ continue;
410
+ }
411
+ }
412
+ if (c === "\\" && i + 1 < chars.length) c = chars[++i];
413
+ if (chars[i + 1] === "-" && i + 2 < chars.length && chars[i + 2] !== "]") {
414
+ let hi = chars[i + 2];
415
+ i += 2;
416
+ if (hi === "\\" && i + 1 < chars.length) hi = chars[++i];
417
+ if (c.codePointAt(0) > hi.codePointAt(0)) throw new Error(`the range ${c}-${hi} is backwards`);
418
+ items.push({ lo: c.codePointAt(0), hi: hi.codePointAt(0) });
419
+ } else {
420
+ items.push({ lo: c.codePointAt(0), hi: c.codePointAt(0) });
421
+ }
422
+ }
423
+ return null;
424
+ }
425
+
426
+ function matchOne(tok, c) {
427
+ if (tok.t === ANY) return true;
428
+ if (tok.t === LIT) return tok.c === c;
429
+ // A folded subject is already lower case, and the class may name either
430
+ // case. Unfolded, a character is exactly itself.
431
+ let hit = inClass(tok.items, c);
432
+ if (!hit && tok.fold) {
433
+ const upper = c.toUpperCase();
434
+ if (upper !== c) hit = inClass(tok.items, upper);
435
+ }
436
+ return tok.neg ? !hit : hit;
437
+ }
438
+
439
+ /** Is `x` a member? A plain loop: this runs for every character a class meets. */
440
+ function inClass(items, x) {
441
+ const cp = x.codePointAt(0);
442
+ for (let i = 0; i < items.length; i++) {
443
+ const item = items[i];
444
+ if (item.cls ? item.cls.test(x) : cp >= item.lo && cp <= item.hi) return true;
445
+ }
446
+ return false;
447
+ }
448
+
449
+ /**
450
+ * One pattern segment against path segment `i` of a prepared subject.
451
+ *
452
+ * The run before the first star has to start the name and the run after the
453
+ * last star has to end it, so both are compared in place; only the runs
454
+ * between two stars are searched for, each at its leftmost place after the
455
+ * one before (which is enough, since a star takes anything). A segment with
456
+ * no star, or one, is therefore compared, never searched: the old greedy walk
457
+ * searched for the tail too, and `*?aaaa…ab3` cost the length of the name
458
+ * times the length of the rule, 10,000 times over, for every path.
459
+ */
460
+ function matchSegment(tokens, subj, i) {
461
+ const str = (subj.strs[i] ??= fold(subj.raw[i], subj.ignoreCase));
462
+ if (tokens.searched === 0) charge(subj, tokens.fixed);
463
+ if (tokens.lit !== undefined) return tokens.lit === str;
464
+ if (tokens.suffix !== undefined) return str.endsWith(tokens.suffix);
465
+ if (tokens.prefix !== undefined) return str.startsWith(tokens.prefix);
466
+ const chars = (subj.chars[i] ??= Array.from(str));
467
+ const n = chars.length;
468
+ if (tokens.searched) charge(subj, tokens.fixed + (n + tokens.mids.length) * tokens.searched);
469
+ if (!tokens.star) return n === tokens.min && matchAt(tokens.head, chars, 0);
470
+ if (n < tokens.min) return false;
471
+ const end = n - tokens.tail.length;
472
+ if (!matchEnd(tokens.tail, chars, end) || !matchAt(tokens.head, chars, 0)) return false;
473
+ let at = tokens.head.length;
474
+ for (const run of tokens.mids) {
475
+ at = find(run, chars, at, end);
476
+ if (at === -1) return false;
477
+ }
478
+ return true;
479
+ }
480
+
481
+ /** `run` at `at`, front to back. */
482
+ function matchAt(run, chars, at) {
483
+ for (let k = 0; k < run.length; k++) if (!matchOne(run[k], chars[at + k])) return false;
484
+ return true;
485
+ }
486
+
487
+ /** `run` at `at`, back to front: names mostly differ at the end. */
488
+ function matchEnd(run, chars, at) {
489
+ for (let k = run.length - 1; k >= 0; k--) if (!matchOne(run[k], chars[at + k])) return false;
490
+ return true;
491
+ }
492
+
493
+ /** The leftmost place for `run` in chars[from, to): the index just past it, or -1. */
494
+ function find(run, chars, from, to) {
495
+ for (let at = from; at + run.length <= to; at++) if (matchAt(run, chars, at)) return at + run.length;
496
+ return -1;
497
+ }
498
+
499
+ /**
500
+ * The most matching one path may cost, in the units `tokenWork` counts —
501
+ * roughly one character comparison each.
502
+ *
503
+ * Counted as the matching happens: every rule considered, every segment
504
+ * compared (the shortcuts by their length, a searched run by the length of
505
+ * the name it is searched in), every step of a `**` walk. A path that would
506
+ * take more is not decided at all — `IgnoreBudgetError` — and the mount
507
+ * leaves it alone, as it does one it cannot read: neither synced nor deleted.
508
+ *
509
+ * This replaces working out the worst case from the rules in advance and
510
+ * refusing files and sets of rules over a limit. That bound was only as good
511
+ * as its model, and each round found a shape it priced wrong — a class of
512
+ * 250,000 members, a path 61 deep, a draft's `web/**` rule at 17 million —
513
+ * while its caps were something the remote party could fill. A count kept
514
+ * while matching needs no worst case worked out in advance, and nothing but
515
+ * the path in hand spends it. Ordinary paths against GitHub's Node,
516
+ * VisualStudio and TeX templates cost 3,000 to 25,000; the budget is what a
517
+ * path 64 deep of 255-character names against a full file of hostile rules
518
+ * spends in about ten milliseconds (M4 Max), so a scan still yields within
519
+ * its slice.
520
+ */
521
+ export const PATH_BUDGET = 4_000_000;
522
+
523
+ export class IgnoreBudgetError extends Error {
524
+ constructor() {
525
+ super("matching this path against the ignore rules would take too long");
526
+ this.name = "IgnoreBudgetError";
527
+ }
528
+ }
529
+ const OVER = new IgnoreBudgetError();
530
+
531
+ /** Spend `n` of the subject's budget, or stop. */
532
+ function charge(subj, n) {
533
+ if ((subj.spent += n) > subj.budget) throw OVER;
534
+ }
535
+
536
+ /**
537
+ * The deepest path, and the longest name, that paths from elsewhere may have
538
+ * before they are matched at all (see `withinMatchBounds`).
539
+ */
540
+ export const NAME_MAX = 255;
541
+ export const DEPTH_MAX = 64;
542
+
543
+ /**
544
+ * Is `rel` inside the bounds a path from elsewhere is held to before it is
545
+ * matched — at most DEPTH_MAX segments, none longer than NAME_MAX characters
546
+ * once folded as this volume compares names? Setting a path up to be matched
547
+ * costs its depth squared before any rule runs, so that part is bounded
548
+ * here rather than by the budget. The filesystem refuses deeper paths and
549
+ * longer names today; this keeps it from depending on that. Folding can
550
+ * lengthen a name (`ß` is two characters, U+FDFA eighteen), so the folded
551
+ * length is what counts.
552
+ */
553
+ export function withinMatchBounds(rel, ignoreCase = false) {
554
+ const segs = String(rel).split("/").filter(Boolean);
555
+ return segs.length <= DEPTH_MAX && segs.every((seg) => Array.from(fold(seg, ignoreCase)).length <= NAME_MAX);
556
+ }
557
+
558
+ /**
559
+ * Pattern segments against path segments [from, to), all of them.
560
+ *
561
+ * The same greedy walk as `matchSegment`, one level up: `**` is the star, and
562
+ * every other pattern segment consumes exactly one path segment. Iterative:
563
+ * the first rewrite recursed once per `**`, and one planted line of 20,000 of
564
+ * them overflowed the stack on every pass after. O(path × pattern) steps in
565
+ * the worst case.
566
+ *
567
+ * After a `**`, a segment may be tried at one place many times — once for
568
+ * every directory above the path that is asked about — so a costly one is
569
+ * remembered for the path (`seenSegment`). Tried afresh, the `report.[0-9]*…`
570
+ * line of GitHub's Node template, drafted under `web/**`, cost 17 million a
571
+ * path 61 deep.
572
+ */
573
+ function matchPath(pat, subj, from, to) {
574
+ let si = 0;
575
+ let pi = from;
576
+ let starSi = -1;
577
+ let starPi = 0;
578
+ let steps = 0;
579
+ while (pi < to) {
580
+ steps++;
581
+ const p = pat[si];
582
+ if (p === GLOBSTAR) {
583
+ starSi = si++;
584
+ starPi = pi;
585
+ } else if (p !== undefined && (p === ANYSEG || (starSi >= 0 && p.heavy ? seenSegment(p, subj, pi) : matchSegment(p, subj, pi)))) {
586
+ si++;
587
+ pi++;
588
+ } else if (starSi >= 0) {
589
+ si = starSi + 1;
590
+ pi = ++starPi;
591
+ } else break;
592
+ }
593
+ charge(subj, steps);
594
+ if (pi < to) return false;
595
+ while (pat[si] === GLOBSTAR) si++;
596
+ return si === pat.length;
597
+ }
598
+
599
+ /** `matchSegment`, remembered for the path in hand. */
600
+ function seenSegment(tokens, subj, i) {
601
+ const seen = (subj.segments ??= new Map());
602
+ const key = tokens.uid * 65536 + i;
603
+ let hit = seen.get(key);
604
+ if (hit === undefined) seen.set(key, (hit = matchSegment(tokens, subj, i)));
605
+ else charge(subj, 4);
606
+ return hit;
607
+ }
608
+
609
+ /**
610
+ * A path, prepared once for every rule and every set of rules asked about
611
+ * it: its segments folded as this volume compares names, their characters
612
+ * split lazily, `budget` for what matching it may cost (`PATH_BUDGET`), and
613
+ * what has been worked out about it so far.
614
+ */
615
+ function subject(raw, ignoreCase, budget) {
616
+ return { raw, ignoreCase, strs: [], chars: [], spent: 0, budget, verdicts: null, segments: null, _prefixes: null };
617
+ }
618
+
619
+ /** The first k segments of the subject's path, as written, for each k. */
620
+ function prefixesOf(subj) {
621
+ if (!subj._prefixes) {
622
+ const p = [""];
623
+ for (let i = 1; i <= subj.raw.length; i++) p[i] = i === 1 ? subj.raw[0] : `${p[i - 1]}/${subj.raw[i - 1]}`;
624
+ subj._prefixes = p;
625
+ }
626
+ return subj._prefixes;
627
+ }
628
+
629
+ /** A file's verdict on a span of a path: no rule matched it, the last one to excluded it, or kept it. */
630
+ const NONE = 0, IGNORED = 1, KEPT = 2;
631
+
632
+ /**
633
+ * The verdict of `rules` on segments [from, to) of a prepared path — that
634
+ * span, as a whole, and nothing above or below it. Last match wins, so they
635
+ * are read from the end, and the first to match decides.
636
+ */
637
+ function lastMatch(rules, subj, from, to, isDir) {
638
+ if (to <= from) return NONE;
639
+ for (let i = rules.length - 1; i >= 0; i--) {
640
+ const rule = rules[i];
641
+ if (rule.dirOnly && !isDir) continue;
642
+ charge(subj, 1);
643
+ const hit = rule.base ? matchSegment(rule.base, subj, to - 1) : matchPath(rule.segs, subj, from, to);
644
+ if (hit) return rule.negated ? KEPT : IGNORED;
645
+ }
646
+ return NONE;
647
+ }
648
+
649
+ const defaultRules = new Map(); // ignoreCase → parsed NODE_DEFAULTS
650
+ function defaultsFor(ignoreCase) {
651
+ const key = Boolean(ignoreCase);
652
+ if (!defaultRules.has(key)) defaultRules.set(key, parseRules(NODE_DEFAULTS, { ignoreCase: key }));
653
+ return defaultRules.get(key);
654
+ }
655
+
656
+ /** The files that govern every path, whatever its directory. */
657
+ const EXCLUDE_FILE = ".git/info/exclude";
658
+
659
+ /**
660
+ * Compiled ignore files, shared.
661
+ *
662
+ * A VERSION is one file as it stood — its path and its rules (`ruleText`) —
663
+ * compiled once, however many sets of rules hold it. A VIEW is one set of
664
+ * rules: which version of each file (`view`). What a version says about a
665
+ * span of a path is worked out once per path, and every view holding that
666
+ * version reads the same answer (`verdictOf`) — so the mount's ignore
667
+ * ratchet, which asks every set of rules a directory has used, pays for a
668
+ * file they share once, not once for each of them.
669
+ *
670
+ * Options: `ignoreCase`, `onBadLine` as for `compileIgnores`, and `budget`
671
+ * (default PATH_BUDGET) for what one path may cost.
672
+ */
673
+ export function ruleBank(opts = {}) {
674
+ const ignoreCase = Boolean(opts.ignoreCase);
675
+ const budget = opts.budget ?? PATH_BUDGET;
676
+ const versions = new Map(); // path + NUL + rules → version
677
+ let ids = 0;
678
+ const make = (file, text, rules) => ({
679
+ id: ++ids,
680
+ file,
681
+ text,
682
+ from: file.endsWith("/.gitignore") ? file.split("/").length - 1 : 0,
683
+ rules,
684
+ // For telling two views apart at a glance (see `view`).
685
+ sig: (Math.random() * 0x40000000) | 0,
686
+ });
687
+ const bank = {
688
+ ignoreCase,
689
+ defaults: make("", NODE_DEFAULTS, defaultsFor(ignoreCase)),
690
+ /** The version of `file` whose rules are `text` (as `ruleText` gives them). */
691
+ version(file, text) {
692
+ const key = `${file}\0${text}`;
693
+ let v = versions.get(key);
694
+ if (!v) versions.set(key, (v = make(file, text, parseRules(text, { ...opts, file }))));
695
+ return v;
696
+ },
697
+ /** `path` prepared for matching (see `subject`). */
698
+ subject(path) {
699
+ return subject(String(path).split("/").filter(Boolean), ignoreCase, budget);
700
+ },
701
+ /** A set of rules over these versions; `files` maps each file to its version. */
702
+ view(files) {
703
+ return view(bank, files);
704
+ },
130
705
  };
706
+ return bank;
707
+ }
708
+
709
+ /**
710
+ * What `v` says about segments [0, k) of the subject's path: NONE, IGNORED
711
+ * or KEPT. Worked out once per path, whichever view asks.
712
+ */
713
+ function verdictOf(v, subj, k, isDir) {
714
+ if (k <= v.from) return NONE; // a nested file speaks only for what is inside its directory
715
+ const key = v.id * 131072 + k * 2 + (isDir ? 1 : 0);
716
+ const known = (subj.verdicts ??= new Map());
717
+ let t = known.get(key);
718
+ if (t === undefined) known.set(key, (t = lastMatch(v.rules, subj, v.from, k, isDir)));
719
+ else charge(subj, 1);
720
+ return t;
131
721
  }
132
722
 
133
723
  /**
134
- * The ignore matcher for a directory.
724
+ * One set of rules for a directory tree: a version of each ignore file there.
725
+ *
726
+ * `.vcsignore` wins outright when present — a project that wrote one meant it
727
+ * — and is read alone on top of the Node defaults. Otherwise this follows git:
728
+ * the defaults, then `.git/info/exclude`, then every `.gitignore` from the
729
+ * root down to the path's own directory, each scoped to the directory it sits
730
+ * in. Last match wins across the lot, which gives git's precedence: a deeper
731
+ * file overrides a shallower one, and any `.gitignore` overrides the exclude
732
+ * file.
135
733
  *
136
- * `.vcsignore` wins outright when present — a project that wrote one meant it.
137
- * Otherwise `.gitignore` stands in, and the Node defaults apply when there is
138
- * neither.
734
+ * A view may change while it is in use — the scan finds nested files on the
735
+ * way down, and registers a directory's before anything in it is matched —
736
+ * and each change drops the directory verdicts it has cached, which every
737
+ * path beneath a directory would otherwise ask for again. `sig` is the XOR
738
+ * of its versions' — equal for equal views, and nearly never otherwise.
139
739
  */
140
- export async function loadIgnores(dir, readFile) {
141
- for (const name of [".vcsignore", ".gitignore"]) {
142
- try {
143
- const text = await readFile(`${dir}/${name}`, "utf8");
144
- // Defaults come first so a project file can negate them with `!`.
145
- return { match: compileIgnores(NODE_DEFAULTS + "\n" + text), source: name };
146
- } catch {
147
- // not there; try the next
740
+ function view(bank, init) {
741
+ const files = new Map(); // file → version
742
+ const nested = new Map(); // directory → the version of its .gitignore
743
+ let vcs = null;
744
+ let exclude = null;
745
+ let root = null;
746
+ let dirs = new Map(); // directory → verdict, while the files are unchanged
747
+ let sig = 0;
748
+ let changes = 0;
749
+
750
+ // The first k segments of a path, on their own.
751
+ const own = (subj, k, isDir) => {
752
+ let v = verdictOf(bank.defaults, subj, k, isDir) === IGNORED;
753
+ if (vcs) {
754
+ const t = verdictOf(vcs, subj, k, isDir);
755
+ return t ? t === IGNORED : v;
756
+ }
757
+ for (const version of [exclude, root]) {
758
+ if (!version) continue;
759
+ const t = verdictOf(version, subj, k, isDir);
760
+ if (t) v = t === IGNORED;
761
+ }
762
+ // Nested files, shallowest first — and only files ABOVE this prefix:
763
+ // `a/.gitignore` speaks for what is inside `a`, not for `a`.
764
+ if (nested.size) {
765
+ const prefixes = prefixesOf(subj);
766
+ charge(subj, k);
767
+ for (let i = 1; i < k; i++) {
768
+ const version = nested.get(prefixes[i]);
769
+ if (!version) continue;
770
+ const t = verdictOf(version, subj, k, isDir);
771
+ if (t) v = t === IGNORED;
772
+ }
148
773
  }
774
+ return v;
775
+ };
776
+
777
+ const self = {
778
+ files,
779
+ get sig() {
780
+ return sig;
781
+ },
782
+ /** How many times the files have changed: for caching what depends on them. */
783
+ get changes() {
784
+ return changes;
785
+ },
786
+ get source() {
787
+ return vcs ? ".vcsignore" : root ? ".gitignore" : "defaults";
788
+ },
789
+ /** Make `version` (null: none) the one `file` holds. */
790
+ put(file, version) {
791
+ const before = files.get(file) ?? null;
792
+ if (before === version) return;
793
+ if (before) sig ^= before.sig;
794
+ if (version) {
795
+ files.set(file, version);
796
+ sig ^= version.sig;
797
+ } else files.delete(file);
798
+ if (file === ".vcsignore") vcs = version;
799
+ else if (file === ".gitignore") root = version;
800
+ else if (file === EXCLUDE_FILE) exclude = version;
801
+ else if (version) nested.set(file.slice(0, -"/.gitignore".length), version);
802
+ else nested.delete(file.slice(0, -"/.gitignore".length));
803
+ dirs = new Map();
804
+ changes++;
805
+ },
806
+ /** `file` as holding `text` (null: absent). */
807
+ set(file, text) {
808
+ self.put(file, text == null ? null : bank.version(file, ruleText(text)));
809
+ },
810
+ delete(file) {
811
+ self.put(file, null);
812
+ },
813
+ /** Is the subject's path ignored? Throws IgnoreBudgetError past its budget. */
814
+ excludes(subj, isDir = false) {
815
+ const n = subj.raw.length;
816
+ if (!n) return false;
817
+ if (n > 1) {
818
+ const prefixes = prefixesOf(subj);
819
+ for (let k = 1; k < n; k++) {
820
+ let v = dirs.get(prefixes[k]);
821
+ if (v === undefined) dirs.set(prefixes[k], (v = own(subj, k, true)));
822
+ if (v) return true;
823
+ }
824
+ }
825
+ return own(subj, n, isDir);
826
+ },
827
+ match(path, isDir = false) {
828
+ return self.excludes(bank.subject(path), isDir);
829
+ },
830
+ /** Does `other` hold exactly these versions? */
831
+ same(other) {
832
+ if (other.sig !== sig || other.files.size !== files.size) return false;
833
+ for (const [file, version] of files) if (other.files.get(file) !== version) return false;
834
+ return true;
835
+ },
836
+ /**
837
+ * Would these rules give `subj`'s path the same verdict as `other`'s,
838
+ * going by the files that govern it alone? True when every one of them
839
+ * is the same version in both.
840
+ */
841
+ alike(other, subj) {
842
+ if (other.files.get(".vcsignore") !== vcs || other.files.get(".gitignore") !== root || other.files.get(EXCLUDE_FILE) !== exclude) return false;
843
+ if (!nested.size && !other.nestedCount) return true;
844
+ const prefixes = prefixesOf(subj);
845
+ charge(subj, subj.raw.length);
846
+ for (let i = 1; i < subj.raw.length; i++) {
847
+ const file = `${prefixes[i]}/.gitignore`;
848
+ if (other.files.get(file) !== files.get(file)) return false;
849
+ }
850
+ return true;
851
+ },
852
+ get nestedCount() {
853
+ return nested.size;
854
+ },
855
+ };
856
+ for (const [file, version] of init ?? []) self.put(file, version);
857
+ return self;
858
+ }
859
+
860
+ /**
861
+ * The rules for one directory tree, from the text of its ignore files (see
862
+ * `view`): `texts` maps each file's path relative to the tree —
863
+ * `.vcsignore`, `.gitignore`, `.git/info/exclude`, `packages/api/.gitignore`
864
+ * — to its text. `set` and `delete` change a file while the rules are in use.
865
+ * `match` throws IgnoreBudgetError for a path past `opts.budget`.
866
+ */
867
+ export function ruleSystem(texts, opts = {}) {
868
+ const rules = ruleBank(opts).view();
869
+ for (const [file, text] of texts instanceof Map ? texts : Object.entries(texts ?? {})) rules.set(file, text);
870
+ return {
871
+ match: rules.match,
872
+ set: rules.set,
873
+ delete: rules.delete,
874
+ get source() {
875
+ return rules.source;
876
+ },
877
+ };
878
+ }
879
+
880
+ /**
881
+ * How large an ignore file may be before it is not read as one — or null
882
+ * when it is fine.
883
+ *
884
+ * These bound what reading and compiling the file costs, and what it takes up
885
+ * in the mount's rules file; what MATCHING against it costs is held to each
886
+ * path's budget instead (`PATH_BUDGET`). The largest real templates are a few
887
+ * hundred lines (GitHub's Unity and VisualStudio files, ~400 lines and ~7KB),
888
+ * so these sit two orders of magnitude above any real file. What happens to a
889
+ * file over them depends on who wrote it (see mount.js).
890
+ */
891
+ export const IGNORE_FILE_MAX_BYTES = 256 * 1024;
892
+ export const IGNORE_FILE_MAX_RULES = 10_000;
893
+
894
+ /** Why `bytes` (or a string) cannot be read as an ignore file, or null. */
895
+ export function ignoreFileProblem(bytes) {
896
+ const text = typeof bytes === "string" ? bytes : Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength).toString("utf8");
897
+ const size = typeof bytes === "string" ? Buffer.byteLength(bytes) : bytes.length;
898
+ if (size > IGNORE_FILE_MAX_BYTES) {
899
+ return `it is ${Math.round(size / 1024)} KB, and an ignore file may be at most ${IGNORE_FILE_MAX_BYTES / 1024} KB`;
900
+ }
901
+ const rules = ruleLines(text).length;
902
+ if (rules > IGNORE_FILE_MAX_RULES) {
903
+ return `it has ${rules} rules, and an ignore file may have at most ${IGNORE_FILE_MAX_RULES}`;
149
904
  }
150
- return { match: compileIgnores(NODE_DEFAULTS), source: "defaults" };
905
+ return null;
151
906
  }
152
907
 
153
908
  /**
154
909
  * Starter `.vcsignore` for a project.
155
910
  *
156
- * Seeded from the Node defaults, then from the project's own `.gitignore` when
157
- * it has one — the patterns a project already maintains are the best statement
158
- * of what is derivable in it, and re-typing them is how the two files drift.
911
+ * Seeded from the Node defaults, then from every file git reads — the
912
+ * patterns a project already maintains are the best statement of what is
913
+ * derivable in it, and re-typing them is how the two files drift. `texts` maps
914
+ * each file (`.gitignore`, `.git/info/exclude`, `packages/api/.gitignore`) to
915
+ * its text; a string is the root `.gitignore` alone.
916
+ *
917
+ * All of them, because a `.vcsignore` wins outright: drafting from the root
918
+ * `.gitignore` alone switched off every nested `.gitignore` and the exclude
919
+ * file the moment it was written, and uploaded what they kept home. They are
920
+ * carried over in git's order of precedence — exclude, root, then nested
921
+ * files shallow to deep — and a nested file's patterns are re-anchored under
922
+ * its directory, so the one flat file means what the several did.
159
923
  */
160
- export function draftIgnores(gitignore) {
161
- const theirs = String(gitignore ?? "")
162
- .split(/\r?\n/)
163
- .map((line) => line.trim())
164
- // Drop what the defaults already cover, so the file reads as "and also
165
- // these" rather than a wall of duplicates.
166
- .filter((line) => line && !line.startsWith("#") && !DEFAULT_LINES.has(line));
924
+ export function draftIgnores(texts) {
925
+ const files = typeof texts === "string" || texts == null
926
+ ? new Map([[".gitignore", texts ?? ""]])
927
+ : texts instanceof Map ? texts : new Map(Object.entries(texts));
928
+ const nested = [...files.keys()]
929
+ .filter((file) => file.endsWith("/.gitignore"))
930
+ .sort((a, b) => a.split("/").length - b.split("/").length || (a < b ? -1 : 1));
931
+
932
+ const sections = [];
933
+ // A line the defaults already have reads as noise — but dropping it is
934
+ // only harmless while no negation has come before it: after `!dist/`, a
935
+ // second `dist/` is what puts dist back out of the sync.
936
+ let negated = false;
937
+ const carry = (label, lines) => {
938
+ const kept = lines.filter((line) => {
939
+ const redundant = !negated && DEFAULT_LINES.has(line);
940
+ if (line.startsWith("!")) negated = true;
941
+ return !redundant;
942
+ });
943
+ if (kept.length) sections.push(`# Carried over from ${label}.\n${kept.join("\n")}`);
944
+ };
945
+ carry(".git/info/exclude", ruleLines(files.get(".git/info/exclude")));
946
+ carry(".gitignore", ruleLines(files.get(".gitignore")));
947
+ for (const file of nested) {
948
+ const dir = file.slice(0, -"/.gitignore".length);
949
+ carry(`${file}, re-anchored under ${dir}/`, ruleLines(files.get(file)).map((line) => reanchor(line, dir)).filter(Boolean));
950
+ }
951
+
952
+ if (!sections.length) return NODE_DEFAULTS;
953
+ return `${NODE_DEFAULTS}\n${sections.join("\n\n")}\n`;
954
+ }
167
955
 
168
- if (!theirs.length) return NODE_DEFAULTS;
169
- return `${NODE_DEFAULTS}\n# Carried over from .gitignore.\n${[...new Set(theirs)].join("\n")}\n`;
956
+ /**
957
+ * One line of `dir/.gitignore` as a line of a root file: an anchored pattern
958
+ * is prefixed with the directory, a bare one may match at any depth beneath
959
+ * it. The directory is escaped, since its name is matched literally.
960
+ */
961
+ function reanchor(line, dir) {
962
+ let pattern = line;
963
+ let neg = "";
964
+ if (pattern.startsWith("!")) {
965
+ neg = "!";
966
+ pattern = pattern.slice(1);
967
+ }
968
+ let slash = "";
969
+ if (pattern.endsWith("/")) {
970
+ slash = "/";
971
+ pattern = pattern.slice(0, -1);
972
+ }
973
+ const anchored = pattern.includes("/");
974
+ pattern = pattern.replace(/^\/+/, "");
975
+ if (!pattern) return null;
976
+ const base = dir.replace(/[\\*?[]/g, "\\$&").replace(/^[#! ]/, "\\$&");
977
+ return `${neg}${base}/${anchored ? "" : "**/"}${pattern}${slash}`;
170
978
  }
171
979
 
172
- const DEFAULT_LINES = new Set(
173
- NODE_DEFAULTS.split(/\r?\n/).map((l) => l.trim()).filter((l) => l && !l.startsWith("#")),
174
- );
980
+ const DEFAULT_LINES = new Set(ruleLines(NODE_DEFAULTS));