8000 Implement recursive Finder::ignoreVCSIgnored filter · symfony/symfony@f1862d0 · GitHub
[go: up one dir, main page]

Skip to content

Commit f1862d0

Browse files
committed
Implement recursive Finder::ignoreVCSIgnored filter
1 parent 5dbd951 commit f1862d0

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

src/Symfony/Component/Finder/Finder.php

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,34 @@ public function count()
707707
return iterator_count($this->getIterator());
708708
}
709709

710+
private function searchGitignoreFiles(string $dir): self
711+
{
712+
$finder = self::create()
713+
->in($dir)
714+
->ignoreDotFiles(false)
715+
->name('.gitignore');
716+
if ($this->followLinks) {
717+
$finder->followLinks();
718+
}
719+
720+
return $finder;
721+
}
722+
723+
private function buildGitignoreRegexes(string $dir): array
724+
{
725+
return array_map(function (SplFileInfo $file) {
726+
try {
727+
$data = $file->getContents();
728+
} catch (\RuntimeException $e) {
729+
throw new \RuntimeException(sprintf('Failed to read ".gitignore" "%s" file.', $file->getPathname()), 0, $e);
730+
}
731+
732+
$dataRelativized = Gitignore::relativize($data, $file->getRelativePath());
733+
734+
return Gitignore::toRegex($dataRelativized);
735+
}, iterator_to_array($this->searchGitignoreFiles($dir)));
736+
}
737+
710738
private function searchInDirectory(string $dir): \Iterator
711739
{
712740
$exclude = $this->exclude;
@@ -721,11 +749,7 @@ private function searchInDirectory(string $dir): \Iterator
721749
}
722750

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

731755
$minDepth = 0;

0 commit comments

Comments
 (0)
0