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:
commit
ad6dc2ec06
@ -25,7 +25,7 @@ class Gitignore
|
|||||||
*/
|
*/
|
||||||
public static function toRegex(string $gitignoreFileContent): string
|
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 = preg_split('/\r\n|\r|\n/', $gitignoreFileContent);
|
||||||
$gitignoreLines = array_map('trim', $gitignoreLines);
|
$gitignoreLines = array_map('trim', $gitignoreLines);
|
||||||
$gitignoreLines = array_filter($gitignoreLines);
|
$gitignoreLines = array_filter($gitignoreLines);
|
||||||
|
@ -107,7 +107,31 @@ class GitignoreTest extends TestCase
|
|||||||
#IamComment
|
#IamComment
|
||||||
/app/cache/',
|
/app/cache/',
|
||||||
['app/cache/file.txt', 'app/cache/subdir/ile.txt'],
|
['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'],
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user