From ce22d5ddd7b0dcbf70b8c30ae347848067fac3a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Sat, 15 May 2021 17:26:28 +0200 Subject: [PATCH] [Finder] Fix gitignore regex build with "**" --- src/Symfony/Component/Finder/Gitignore.php | 22 +-- .../Component/Finder/Tests/GitignoreTest.php | 154 +++++++++++++++--- 2 files changed, 139 insertions(+), 37 deletions(-) diff --git a/src/Symfony/Component/Finder/Gitignore.php b/src/Symfony/Component/Finder/Gitignore.php index 5bd6829491..304aba9e5a 100644 --- a/src/Symfony/Component/Finder/Gitignore.php +++ b/src/Symfony/Component/Finder/Gitignore.php @@ -68,20 +68,16 @@ class Gitignore $isAbsolute = false; } - $parts = array_map(function (string $v): string { - $v = preg_quote(str_replace('\\', '', $v), '~'); - $v = preg_replace_callback('~\\\\\[([^\[\]]*)\\\\\]~', function (array $matches): string { - return '['.str_replace('\\-', '-', $matches[1]).']'; - }, $v); - $v = preg_replace('~\\\\\*\\\\\*~', '[^/]+(?:/[^/]+)*', $v); - $v = preg_replace('~\\\\\*~', '[^/]*', $v); - $v = preg_replace('~\\\\\?~', '[^/]', $v); - - return $v; - }, explode('/', $gitignoreLine)); + $regex = preg_quote(str_replace('\\', '', $gitignoreLine), '~'); + $regex = preg_replace_callback('~\\\\\[((?:\\\\!)?)([^\[\]]*)\\\\\]~', function (array $matches): string { + return '['.('' !== $matches[1] ? '^' : '').str_replace('\\-', '-', $matches[2]).']'; + }, $regex); + $regex = preg_replace('~(?:(?:\\\\\*){2,}(/?))+~', '(?:(?:(?!//).(?