mirror of
https://github.com/QwenLM/qwen-code.git
synced 2025-12-19 09:33:53 +00:00
reuse GitIgnoreParser for loading .geminiignore (#1025)
This commit is contained in:
@@ -8,14 +8,13 @@ import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest';
|
||||
import { GitIgnoreParser } from './gitIgnoreParser.js';
|
||||
import * as fs from 'fs/promises';
|
||||
import * as path from 'path';
|
||||
import { isGitRepository } from './gitUtils.js';
|
||||
|
||||
// Mock fs module
|
||||
vi.mock('fs/promises');
|
||||
|
||||
// Mock gitUtils module
|
||||
vi.mock('./gitUtils.js', () => ({
|
||||
isGitRepository: vi.fn(() => true),
|
||||
}));
|
||||
vi.mock('./gitUtils.js');
|
||||
|
||||
describe('GitIgnoreParser', () => {
|
||||
let parser: GitIgnoreParser;
|
||||
@@ -26,6 +25,7 @@ describe('GitIgnoreParser', () => {
|
||||
// Reset mocks before each test
|
||||
vi.mocked(fs.readFile).mockClear();
|
||||
vi.mocked(fs.readFile).mockRejectedValue(new Error('ENOENT')); // Default to no file
|
||||
vi.mocked(isGitRepository).mockReturnValue(true);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
@@ -51,6 +51,13 @@ node_modules/
|
||||
|
||||
await parser.initialize();
|
||||
|
||||
expect(parser.getPatterns()).toEqual([
|
||||
'.git',
|
||||
'node_modules/',
|
||||
'*.log',
|
||||
'/dist',
|
||||
'.env',
|
||||
]);
|
||||
expect(parser.isIgnored('node_modules/some-lib')).toBe(true);
|
||||
expect(parser.isIgnored('src/app.log')).toBe(true);
|
||||
expect(parser.isIgnored('dist/index.js')).toBe(true);
|
||||
@@ -68,7 +75,22 @@ node_modules/
|
||||
});
|
||||
|
||||
await parser.initialize();
|
||||
expect(parser.getPatterns()).toEqual(['.git', 'temp/', '*.tmp']);
|
||||
expect(parser.isIgnored('temp/file.txt')).toBe(true);
|
||||
expect(parser.isIgnored('src/file.tmp')).toBe(true);
|
||||
});
|
||||
|
||||
it('should handle custom patterns file name', async () => {
|
||||
vi.mocked(isGitRepository).mockReturnValue(false);
|
||||
vi.mocked(fs.readFile).mockImplementation(async (filePath) => {
|
||||
if (filePath === path.join(mockProjectRoot, '.geminiignore')) {
|
||||
return 'temp/\n*.tmp';
|
||||
}
|
||||
throw new Error('ENOENT');
|
||||
});
|
||||
|
||||
await parser.initialize('.geminiignore');
|
||||
expect(parser.getPatterns()).toEqual(['temp/', '*.tmp']);
|
||||
expect(parser.isIgnored('temp/file.txt')).toBe(true);
|
||||
expect(parser.isIgnored('src/file.tmp')).toBe(true);
|
||||
});
|
||||
|
||||
@@ -17,43 +17,53 @@ 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(): Promise<void> {
|
||||
async initialize(patternsFileName?: string): Promise<void> {
|
||||
const patternFiles = [];
|
||||
if (patternsFileName && patternsFileName !== '') {
|
||||
patternFiles.push(patternsFileName);
|
||||
}
|
||||
|
||||
this.isGitRepo = isGitRepository(this.projectRoot);
|
||||
if (this.isGitRepo) {
|
||||
const gitIgnoreFiles = [
|
||||
path.join(this.projectRoot, '.gitignore'),
|
||||
path.join(this.projectRoot, '.git', 'info', 'exclude'),
|
||||
];
|
||||
patternFiles.push('.gitignore');
|
||||
patternFiles.push(path.join('.git', 'info', 'exclude'));
|
||||
|
||||
// Always ignore .git directory regardless of .gitignore content
|
||||
this.addPatterns(['.git']);
|
||||
|
||||
for (const gitIgnoreFile of gitIgnoreFiles) {
|
||||
try {
|
||||
const content = await fs.readFile(gitIgnoreFile, 'utf-8');
|
||||
const patterns = content.split('\n').map((p) => p.trim());
|
||||
this.addPatterns(patterns);
|
||||
} catch (_error) {
|
||||
// File doesn't exist or can't be read, continue silently
|
||||
}
|
||||
}
|
||||
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 {
|
||||
if (!this.isGitRepo) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const relativePath = path.isAbsolute(filePath)
|
||||
? path.relative(this.projectRoot, filePath)
|
||||
: filePath;
|
||||
@@ -67,11 +77,10 @@ export class GitIgnoreParser implements GitIgnoreFilter {
|
||||
normalizedPath = normalizedPath.substring(2);
|
||||
}
|
||||
|
||||
const ignored = this.ig.ignores(normalizedPath);
|
||||
return ignored;
|
||||
return this.ig.ignores(normalizedPath);
|
||||
}
|
||||
|
||||
getGitRepoRoot(): string {
|
||||
return this.projectRoot;
|
||||
getPatterns(): string[] {
|
||||
return this.patterns;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user