-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: switch to
node-ignore
package to better handle excluded files
- Loading branch information
Showing
4 changed files
with
3,182 additions
and
357 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,99 +1,40 @@ | ||
import ignore from "ignore"; | ||
import * as vscode from "vscode"; | ||
|
||
// TODO: make this spec compliant, something like https://github.com/mherrmann/gitignore_parser | ||
// BUGFIX: doesn't match patterns that start with /, like `/.next/*` | ||
|
||
export class GitignoreManager { | ||
private patterns: { pattern: string; isDirectory: boolean }[] = []; | ||
private ig: ReturnType<typeof ignore> | null = null; | ||
private outputChannel: vscode.OutputChannel; | ||
|
||
constructor(outputChannel: vscode.OutputChannel) { | ||
this.outputChannel = outputChannel; | ||
} | ||
|
||
// Static utility methods that can be used by other classes | ||
public static convertGlobToRegex(pattern: string): string { | ||
return pattern | ||
.replace(/[.+^${}()|[\]\\]/g, "\\$&") // escape special regex chars | ||
.replace(/\*\*/g, ".*") // ** matches anything including / | ||
.replace(/\*/g, "[^/]*") // * matches anything except / | ||
.replace(/\?/g, "[^/]"); // ? matches any single non-/ char | ||
} | ||
|
||
public static normalizePath(path: string): string { | ||
return path.replace(/\\/g, "/").replace(/^\/+|\/+$/g, ""); | ||
} | ||
|
||
public static isMatch(pattern: string, filePath: string, isDirectory = false): boolean { | ||
pattern = this.normalizePath(pattern); | ||
filePath = this.normalizePath(filePath); | ||
|
||
// For directory patterns without a slash, match anywhere in the path | ||
if (isDirectory && !pattern.includes("/")) { | ||
const dirName = pattern.replace(/\/$/, ""); | ||
return new RegExp(`(^|/)${this.convertGlobToRegex(dirName)}(/|$)`).test(filePath); | ||
} | ||
|
||
// For file patterns without a slash, match the basename | ||
if (!isDirectory && !pattern.includes("/")) { | ||
const basename = filePath.split("/").pop() || ""; | ||
return new RegExp(`^${this.convertGlobToRegex(pattern)}$`).test(basename); | ||
} | ||
|
||
// For patterns with slashes, match the full path | ||
const regexPattern = this.convertGlobToRegex(pattern); | ||
if (isDirectory || pattern.endsWith("/**")) { | ||
return new RegExp(`^${regexPattern}(/.*)?$`).test(filePath); | ||
public shouldIgnore(filePath: string): boolean { | ||
if (!this.ig) { | ||
return false; | ||
} | ||
return new RegExp(`^${regexPattern}$`).test(filePath); | ||
return this.ig.ignores(filePath); | ||
} | ||
|
||
public async loadGitignore(workspaceFolder: vscode.Uri): Promise<void> { | ||
try { | ||
const gitignorePath = vscode.Uri.joinPath(workspaceFolder, ".gitignore"); | ||
const content = await vscode.workspace.fs.readFile(gitignorePath); | ||
const gitignoreContent = Buffer.from(content).toString("utf8"); | ||
|
||
this.patterns = gitignoreContent | ||
.split(/\r?\n/) | ||
.map((line) => line.trim()) | ||
.filter((line) => line && !line.startsWith("#")) | ||
.map((pattern) => { | ||
pattern = GitignoreManager.normalizePath(pattern); | ||
const isDirectory = !pattern.includes(".") || pattern.endsWith("/"); | ||
return { pattern, isDirectory }; | ||
}); | ||
|
||
const formattedPatterns = this.patterns.map((p) => { | ||
if (p.isDirectory && !p.pattern.includes("/")) { | ||
return `**/${p.pattern}/**`; | ||
} | ||
return p.pattern; | ||
}); | ||
|
||
this.outputChannel.appendLine(`Loaded ${this.patterns.length} patterns from .gitignore`); | ||
this.outputChannel.appendLine(`Patterns: ${formattedPatterns.join(", ")}`); | ||
this.loadFromContent(gitignoreContent); | ||
} catch (error) { | ||
this.outputChannel.appendLine("No .gitignore file found or failed to read it"); | ||
this.patterns = []; | ||
this.ig = null; | ||
} | ||
} | ||
|
||
public shouldIgnore(filePath: string): boolean { | ||
let shouldIgnore = false; | ||
|
||
// process patterns in order, with negations overriding previous matches | ||
for (const { pattern, isDirectory } of this.patterns) { | ||
if (pattern.startsWith("!")) { | ||
// if a negated pattern matches, the file should NOT be ignored | ||
if (GitignoreManager.isMatch(pattern.slice(1), filePath, isDirectory)) { | ||
shouldIgnore = false; | ||
} | ||
} else if (GitignoreManager.isMatch(pattern, filePath, isDirectory)) { | ||
shouldIgnore = true; | ||
} | ||
public loadFromContent(content: string): void { | ||
try { | ||
this.ig = ignore().add(content); | ||
this.outputChannel.appendLine("Successfully loaded gitignore patterns"); | ||
} catch (error) { | ||
this.outputChannel.appendLine("Failed to parse gitignore content"); | ||
this.ig = null; | ||
} | ||
|
||
return shouldIgnore; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.