Skip to content

Fix/Implement recursive Finder::ignoreVCSIgnored filter #40769

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions src/Symfony/Component/Finder/Finder.php
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,34 @@ public function count()
return iterator_count($this->getIterator());
}

private function searchGitignoreFiles(string $dir): self
{
$finder = self::create()
->in($dir)
->ignoreDotFiles(false)
->name('.gitignore');
if ($this->followLinks) {
$finder->followLinks();
}

return $finder;
}

private function buildGitignoreRegexes(string $dir): array
{
return array_map(function (SplFileInfo $file) {
try {
$data = $file->getContents();
} catch (\RuntimeException $e) {
throw new \RuntimeException(sprintf('Failed to read ".gitignore" "%s" file.', $file->getPathname()), 0, $e);
}

$dataRelativized = Gitignore::relativize($data, $file->getRelativePath());

return Gitignore::toRegex($dataRelativized);
}, iterator_to_array($this->searchGitignoreFiles($dir)));
}

private function searchInDirectory(string $dir): \Iterator
{
$exclude = $this->exclude;
Expand All @@ -721,11 +749,7 @@ private function searchInDirectory(string $dir): \Iterator
}

if (static::IGNORE_VCS_IGNORED_FILES === (static::IGNORE_VCS_IGNORED_FILES & $this->ignore)) {
$gitignoreFilePath = sprintf('%s/.gitignore', $dir);
if (!is_readable($gitignoreFilePath)) {
throw new \RuntimeException(sprintf('The "ignoreVCSIgnored" option cannot be used by the Finder as the "%s" file is not readable.', $gitignoreFilePath));
}
$notPaths = array_merge($notPaths, [Gitignore::toRegex(file_get_contents($gitignoreFilePath))]);
$notPaths = array_merge($notPaths, $this->buildGitignoreRegexes($dir));
}

$minDepth = 0;
Expand Down
30 changes: 30 additions & 0 deletions src/Symfony/Component/Finder/Gitignore.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,36 @@ public static function toRegex(string $gitignoreFileContent): string
return '~^(?:'.$res.')~s';
}

public static function relativize(string $gitignoreFileContent, string $gitignoreFileRelativePath = ''): string
{
if ('/' === substr($gitignoreFileRelativePath, -1)) {
$gitignoreFileRelativePath = substr($gitignoreFileRelativePath, 0, -1);
}

if ('' === $gitignoreFileRelativePath) {
return $gitignoreFileContent;
}

$pathQuoted = str_replace('\\', '/', $gitignoreFileRelativePath);

return preg_replace_callback('~(?<=^|[\r\n])(!?+)((?:[^\r\n# \t]|(?<=\\\\)[# \t]|[ \t]+(?=[^\r\n# \t]))+)~', function ($matches) use ($pathQuoted) {
$gitignoreLine = $matches[2];

$slashPos = strpos($gitignoreLine, '/');
if (false !== $slashPos && \strlen($gitignoreLine) - 1 !== $slashPos) {
if (0 === $slashPos) {
$gitignoreLine = substr($gitignoreLine, 1);
}
$isAbsolute = true;
} else {
$isAbsolute = false;
}

return $matches[1].'/'.$pathQuoted.'/'.$gitignoreLine
.($isAbsolute ? '' : "\n".$matches[1].'/'.$pathQuoted.'/**/'.$gitignoreLine);
}, $gitignoreFileContent);
}

private static function lineToRegex(string $gitignoreLine): string
{
if ('' === $gitignoreLine) {
Expand Down
35 changes: 35 additions & 0 deletions src/Symfony/Component/Finder/Tests/GitignoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,20 @@
*/
class GitignoreTest extends TestCase
{
public function testRelativize()
{
$this->assertSame('a', Gitignore::relativize('a', ''));
$this->assertSame('/u/a', Gitignore::relativize('/a', 'u'));
$this->assertSame('/u/a'."\n".'/u/**/a', Gitignore::relativize('a', 'u'));
$this->assertSame('!/u/a'."\n".'!/u/**/a', Gitignore::relativize('!a', 'u'));
$this->assertSame('/u/a'."\n".'/u/**/a#x', Gitignore::relativize('a#x', 'u/'));
$this->assertSame('/u/a\#x'."\n".'/u/**/a\#x', Gitignore::relativize('a\#x', 'u/'));
}

/**
* @dataProvider provider
* @dataProvider providerExtended
* @dataProvider providerRelativized
*/
public function testToRegex(array $gitignoreLines, array $matchingCases, array $nonMatchingCases)
{
Expand Down Expand Up @@ -336,4 +347,28 @@ public function providerExtended(): array

return $cases;
}

public function providerRelativized(): array
{
$basicCases = $this->provider();

$cases = [];
foreach (['a', 'a/b', str_repeat('super !*? path/', 30)] as $p) {
foreach ($basicCases as $case) {
$cases[] = [
array_map(function ($v) use ($p) {
return Gitignore::relativize($v, $p);
}, $case[0]),
array_map(function ($v) use ($p) {
return rtrim($p, '/').'/'.$v;
}, $case[1]),
array_map(function ($v) use ($p) {
return rtrim($p, '/').'/'.$v;
}, $case[2]),
];
}
}

return $cases;
}
}