From 95ccf7e85ef234f091efcd26fc9cb198809a35ae Mon Sep 17 00:00:00 2001 From: neverland Date: Mon, 3 Aug 2026 22:38:53 +0800 Subject: [PATCH] perf(fmt): optimize root gitignore matching --- packages/rstack/src/fmt/discoverPaths.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/packages/rstack/src/fmt/discoverPaths.ts b/packages/rstack/src/fmt/discoverPaths.ts index 9f9fe7f..896aba0 100644 --- a/packages/rstack/src/fmt/discoverPaths.ts +++ b/packages/rstack/src/fmt/discoverPaths.ts @@ -164,6 +164,15 @@ class GitIgnoreMatcher { } #matches(relativePath: string, isDirectory: boolean): boolean { + // Most repositories only use a root `.gitignore`. Avoid checking every path + // segment when no nested matcher can override its result. + const rootMatcher = this.#matchers.size === 1 ? this.#matchers.get(this.#rootPath) : undefined; + if (rootMatcher) { + // `ignore` expects POSIX separators and uses a trailing slash to distinguish directories. + const pathFromMatcher = toPosixPath(relativePath); + return rootMatcher.test(isDirectory ? `${pathFromMatcher}/` : pathFromMatcher).ignored; + } + const segments = relativePath.split(path.sep); let directoryPath = this.#rootPath; let pathFromMatcher = segments.join('/');