From e56fc7cb5840c92aefd7470804fbe5d2bf196154 Mon Sep 17 00:00:00 2001 From: = Date: Tue, 6 Aug 2019 15:08:05 +0200 Subject: [PATCH] [Finder] Adjust regex to correctly match comments in gitignore contents --- src/Symfony/Component/Finder/Gitignore.php | 2 +- .../Component/Finder/Tests/GitignoreTest.php | 26 ++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Finder/Gitignore.php b/src/Symfony/Component/Finder/Gitignore.php index fbeabdbd52..7fe70e1e60 100644 --- a/src/Symfony/Component/Finder/Gitignore.php +++ b/src/Symfony/Component/Finder/Gitignore.php @@ -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); diff --git a/src/Symfony/Component/Finder/Tests/GitignoreTest.php b/src/Symfony/Component/Finder/Tests/GitignoreTest.php index fca846d86e..4c9cbfe528 100644 --- a/src/Symfony/Component/Finder/Tests/GitignoreTest.php +++ b/src/Symfony/Component/Finder/Tests/GitignoreTest.php @@ -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'], ], ]; }