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

This commit is contained in:
= 2019-08-06 15:08:05 +02:00 committed by Fabien Potencier
parent 36466a3ab5
commit e56fc7cb58
2 changed files with 26 additions and 2 deletions

View File

@ -27,7 +27,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

@ -111,7 +111,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'],
],
];
}