Skip to content

Commit 1dadb20

Browse files
committed
Merge Commit '518a9ca': fix(core): Preserve escaped characters in gitignore patterns (google-gemini#11171)
2 parents b7c2ab6 + 518a9ca commit 1dadb20

2 files changed

Lines changed: 23 additions & 6 deletions

File tree

packages/core/src/utils/gitIgnoreParser.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,4 +234,20 @@ src/*.tmp
234234
);
235235
});
236236
});
237+
describe('Escaped Characters', () => {
238+
beforeEach(async () => {
239+
await setupGitRepo();
240+
});
241+
242+
it('should correctly handle escaped characters in .gitignore', async () => {
243+
await createTestFile('.gitignore', '\\#foo\n\\!bar');
244+
// Create files with special characters in names
245+
await createTestFile('bla/#foo', 'content');
246+
await createTestFile('bla/!bar', 'content');
247+
248+
// These should be ignored based on the escaped patterns
249+
expect(parser.isIgnored('bla/#foo')).toBe(true);
250+
expect(parser.isIgnored('bla/!bar')).toBe(true);
251+
});
252+
});
237253
});

packages/core/src/utils/gitIgnoreParser.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ export class GitIgnoreParser implements GitIgnoreFilter {
3535

3636
const relativeBaseDir = isExcludeFile
3737
? '.'
38-
: path.dirname(path.relative(this.projectRoot, patternsFilePath));
38+
: path
39+
.dirname(path.relative(this.projectRoot, patternsFilePath))
40+
.split(path.sep)
41+
.join(path.posix.sep);
3942

4043
return content
4144
.split('\n')
@@ -68,11 +71,11 @@ export class GitIgnoreParser implements GitIgnoreFilter {
6871
if (!isAnchoredInFile && !p.includes('/')) {
6972
// If no slash and not anchored in file, it matches files in any
7073
// subdirectory.
71-
newPattern = path.join('**', p);
74+
newPattern = path.posix.join('**', p);
7275
}
7376

7477
// Prepend the .gitignore file's directory.
75-
newPattern = path.join(relativeBaseDir, newPattern);
78+
newPattern = path.posix.join(relativeBaseDir, newPattern);
7679

7780
// Anchor the pattern to a nested gitignore directory.
7881
if (!newPattern.startsWith('/')) {
@@ -89,9 +92,6 @@ export class GitIgnoreParser implements GitIgnoreFilter {
8992
newPattern = '!' + newPattern;
9093
}
9194

92-
// Even in windows, Ignore expects forward slashes.
93-
newPattern = newPattern.replace(/\\/g, '/');
94-
9595
return newPattern;
9696
})
9797
.filter((p) => p !== '');
@@ -173,6 +173,7 @@ export class GitIgnoreParser implements GitIgnoreFilter {
173173
const gitignorePath = path.join(dir, '.gitignore');
174174
if (fs.existsSync(gitignorePath)) {
175175
const patterns = this.loadPatternsForFile(gitignorePath);
176+
176177
this.cache.set(dir, patterns);
177178
ig.add(patterns);
178179
} else {

0 commit comments

Comments
 (0)