bug #33340 [Finder] Adjust regex to correctly match comments in gitignore contents (Jeroeny)

This PR was squashed before being merged into the 4.3 branch (closes #33340).

Discussion
----------

[Finder] Adjust regex to correctly match comments in gitignore contents

| Q             | A
| ------------- | ---
| Branch?       | 4.3
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #32985
| License       | MIT

Description from issue:

When using `ignoreVCSIgnored`  as argument with the Symfony Finder, it will construct a regex equivalent of the gitignore pattern. However it seems that when a comment line (prefixed with `#`) is present in the `.gitignore`, the regex used to remove comment lines matches every line and thus returns `$gitignoreFileContent` as empty.

Commits
-------

e56fc7cb58 [Finder] Adjust regex to correctly match comments in gitignore contents
This commit is contained in:
Fabien Potencier 2019-09-16 13:29:48 +02:00
commit ad6dc2ec06
2 changed files with 26 additions and 2 deletions

View File

@ -25,7 +25,7 @@ class Gitignore
*/
public static function toRegex(string $gitignoreFileContent): string
{
$gitignoreFileContent = preg_replace('/^[^\\\\]*#.*/', '', $gitignoreFileContent);
$gitignoreFileContent = preg_replace('/^[^\\\r\n]*#.*/m', '', $gitignoreFileContent);
$gitignoreLines = preg_split('/\r\n|\r|\n/', $gitignoreFileContent);
$gitignoreLines = array_map('trim', $gitignoreLines);
$gitignoreLines = array_filter($gitignoreLines);

View File

@ -107,7 +107,31 @@ class GitignoreTest extends TestCase
#IamComment
/app/cache/',
['app/cache/file.txt', 'app/cache/subdir/ile.txt'],
['a/app/cache/file.txt'],
['a/app/cache/file.txt', '#IamComment', 'IamComment'],
],
[
'
/app/cache/
#LastLineIsComment',
['app/cache/file.txt', 'app/cache/subdir/ile.txt'],
['a/app/cache/file.txt', '#LastLineIsComment', 'LastLineIsComment'],
],
[
'
/app/cache/
\#file.txt
#LastLineIsComment',
['app/cache/file.txt', 'app/cache/subdir/ile.txt', '#file.txt'],
['a/app/cache/file.txt', '#LastLineIsComment', 'LastLineIsComment'],
],
[
'
/app/cache/
\#file.txt
#IamComment
another_file.txt',
['app/cache/file.txt', 'app/cache/subdir/ile.txt', '#file.txt', 'another_file.txt'],
['a/app/cache/file.txt', 'IamComment', '#IamComment'],
],
];
}