Skip to content

Commit d08c2a3

Browse files
authored
perf(fmt): avoid relative resolution in gitignore matching (#201)
1 parent 61f257b commit d08c2a3

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

packages/rstack/src/fmt/discoverPaths.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,14 @@ const findGitRoot = async (cwd: string): Promise<string> => {
7777

7878
class GitIgnoreMatcher {
7979
readonly #rootPath: string;
80+
readonly #rootPrefix: string;
8081
readonly #matchers = new Map<string, ReturnType<typeof ignore>>();
8182
readonly #loads = new Map<string, Promise<void>>();
8283
readonly #ignoredDirectories = new Map<string, boolean>();
8384

8485
private constructor(rootPath: string) {
8586
this.#rootPath = rootPath;
87+
this.#rootPrefix = rootPath.endsWith(path.sep) ? rootPath : `${rootPath}${path.sep}`;
8688
}
8789

8890
static async create(cwd: string): Promise<GitIgnoreMatcher> {
@@ -120,7 +122,12 @@ class GitIgnoreMatcher {
120122
return false;
121123
}
122124

123-
const relativePath = path.relative(this.#rootPath, filePath);
125+
const relativePath =
126+
filePath === this.#rootPath
127+
? ''
128+
: filePath.startsWith(this.#rootPrefix)
129+
? filePath.slice(this.#rootPrefix.length)
130+
: path.relative(this.#rootPath, filePath);
124131
if (relativePath === '' || !isRelativePathInside(relativePath)) {
125132
return false;
126133
}

0 commit comments

Comments
 (0)