blob: eeee9f488ef31b83abd98f2689cd38cc6d161234 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import * as fs from 'fs/promises';
import * as path from 'path';
import ignore, { type Ignore } from 'ignore';
import { isGitRepository } from './gitUtils.js';
export interface GitIgnoreFilter {
isIgnored(filePath: string): boolean;
}
export class GitIgnoreParser implements GitIgnoreFilter {
private projectRoot: string;
private isGitRepo: boolean = false;
private ig: Ignore = ignore();
private patterns: string[] = [];
constructor(projectRoot: string) {
this.projectRoot = path.resolve(projectRoot);
}
async initialize(patternsFileName?: string): Promise<void> {
const patternFiles = [];
if (patternsFileName && patternsFileName !== '') {
patternFiles.push(patternsFileName);
}
this.isGitRepo = isGitRepository(this.projectRoot);
if (this.isGitRepo) {
patternFiles.push('.gitignore');
patternFiles.push(path.join('.git', 'info', 'exclude'));
// Always ignore .git directory regardless of .gitignore content
this.addPatterns(['.git']);
}
for (const pf of patternFiles) {
try {
await this.loadPatterns(pf);
} catch (_error) {
// File doesn't exist or can't be read, continue silently
}
}
}
async loadPatterns(patternsFileName: string): Promise<void> {
const content = await fs.readFile(
path.join(this.projectRoot, patternsFileName),
'utf-8',
);
const patterns = content
.split('\n')
.map((p) => p.trim())
.filter((p) => p !== '' && !p.startsWith('#'));
this.addPatterns(patterns);
}
private addPatterns(patterns: string[]) {
this.ig.add(patterns);
this.patterns.push(...patterns);
}
isIgnored(filePath: string): boolean {
const relativePath = path.isAbsolute(filePath)
? path.relative(this.projectRoot, filePath)
: filePath;
if (relativePath === '' || relativePath.startsWith('..')) {
return false;
}
let normalizedPath = relativePath.replace(/\\/g, '/');
if (normalizedPath.startsWith('./')) {
normalizedPath = normalizedPath.substring(2);
}
return this.ig.ignores(normalizedPath);
}
getPatterns(): string[] {
return this.patterns;
}
}
|