mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-05-12 12:54:07 -07:00
fix(gitIgnore): prevent crash/error when processing malformed file paths in GitIgnoreParser (#7553)
Co-authored-by: Srinath Padmanabhan <17151014+srithreepo@users.noreply.github.com>
This commit is contained in:
@@ -181,6 +181,16 @@ src/*.tmp
|
|||||||
expect(() => parser.isIgnored('/node_modules')).not.toThrow();
|
expect(() => parser.isIgnored('/node_modules')).not.toThrow();
|
||||||
expect(parser.isIgnored('/node_modules')).toBe(false);
|
expect(parser.isIgnored('/node_modules')).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should handle backslash-prefixed files without crashing', () => {
|
||||||
|
expect(() => parser.isIgnored('\\backslash-file-test.txt')).not.toThrow();
|
||||||
|
expect(parser.isIgnored('\\backslash-file-test.txt')).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle files with absolute-like names', () => {
|
||||||
|
expect(() => parser.isIgnored('/backslash-file-test.txt')).not.toThrow();
|
||||||
|
expect(parser.isIgnored('/backslash-file-test.txt')).toBe(false);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('getIgnoredPatterns', () => {
|
describe('getIgnoredPatterns', () => {
|
||||||
|
|||||||
@@ -57,16 +57,37 @@ export class GitIgnoreParser implements GitIgnoreFilter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
isIgnored(filePath: string): boolean {
|
isIgnored(filePath: string): boolean {
|
||||||
const resolved = path.resolve(this.projectRoot, filePath);
|
if (!filePath || typeof filePath !== 'string') {
|
||||||
const relativePath = path.relative(this.projectRoot, resolved);
|
|
||||||
|
|
||||||
if (relativePath === '' || relativePath.startsWith('..')) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Even in windows, Ignore expects forward slashes.
|
if (
|
||||||
const normalizedPath = relativePath.replace(/\\/g, '/');
|
filePath.startsWith('\\') ||
|
||||||
return this.ig.ignores(normalizedPath);
|
filePath === '/' ||
|
||||||
|
filePath.includes('\0')
|
||||||
|
) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const resolved = path.resolve(this.projectRoot, filePath);
|
||||||
|
const relativePath = path.relative(this.projectRoot, resolved);
|
||||||
|
|
||||||
|
if (relativePath === '' || relativePath.startsWith('..')) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Even in windows, Ignore expects forward slashes.
|
||||||
|
const normalizedPath = relativePath.replace(/\\/g, '/');
|
||||||
|
|
||||||
|
if (normalizedPath.startsWith('/') || normalizedPath === '') {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.ig.ignores(normalizedPath);
|
||||||
|
} catch (_error) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
getPatterns(): string[] {
|
getPatterns(): string[] {
|
||||||
|
|||||||
Reference in New Issue
Block a user