Skip to content

[Finder] Add .gitignore nested negated patterns support #43837

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

Merged
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
14 changes: 12 additions & 2 deletions src/Symfony/Component/Finder/Gitignore.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,22 @@ class Gitignore
* Format specification: https://git-scm.com/docs/gitignore#_pattern_format
*/
public static function toRegex(string $gitignoreFileContent): string
{
return self::buildRegex($gitignoreFileContent, false);
}

public static function toRegexMatchingNegatedPatterns(string $gitignoreFileContent): string
{
return self::buildRegex($gitignoreFileContent, true);
}

private static function buildRegex(string $gitignoreFileContent, bool $inverted): string
{
$gitignoreFileContent = preg_replace('~(?<!\\\\)#[^\n\r]*~', '', $gitignoreFileContent);
$gitignoreLines = preg_split('~\r\n?|\n~', $gitignoreFileContent);

$res = self::lineToRegex('');
foreach ($gitignoreLines as $i => $line) {
foreach ($gitignoreLines as $line) {
$line = preg_replace('~(?<!\\\\)[ \t]+$~', '', $line);

if ('!' === substr($line, 0, 1)) {
Expand All @@ -41,7 +51,7 @@ public static function toRegex(string $gitignoreFileContent): string
}

if ('' !== $line) {
if ($isNegative) {
if ($isNegative xor $inverted) {
$res = '(?!'.self::lineToRegex($line).'$)'.$res;
} else {
$res = '(?:'.$res.'|'.self::lineToRegex($line).')';
Expand Down
58 changes: 48 additions & 10 deletions src/Symfony/Component/Finder/Iterator/VcsIgnoredFilterIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,15 @@ final class VcsIgnoredFilterIterator extends \FilterIterator
private $baseDir;

/**
* @var array<string, string|null>
* @var array<string, array{0: string, 1: string}|null>
*/
private $gitignoreFilesCache = [];

/**
* @var array<string, bool>
*/
private $ignoredPathsCache = [];

public function __construct(\Iterator $iterator, string $baseDir)
{
$this->baseDir = $this->normalizePath($baseDir);
Expand All @@ -37,25 +42,50 @@ public function accept(): bool
$file = $this->current();

$fileRealPath = $this->normalizePath($file->getRealPath());
if ($file->isDir() && !str_ends_with($fileRealPath, '/')) {

return !$this->isIgnored($fileRealPath);
}

private function isIgnored(string $fileRealPath): bool
{
if (is_dir($fileRealPath) && !str_ends_with($fileRealPath, '/')) {
$fileRealPath .= '/';
}

if (isset($this->ignoredPathsCache[$fileRealPath])) {
return $this->ignoredPathsCache[$fileRealPath];
}

$ignored = false;

foreach ($this->parentsDirectoryDownward($fileRealPath) as $parentDirectory) {
if ($this->isIgnored($parentDirectory)) {
$ignored = true;

// rules in ignored directories are ignored, no need to check further.
break;
}

$fileRelativePath = substr($fileRealPath, \strlen($parentDirectory) + 1);

$regex = $this->readGitignoreFile("{$parentDirectory}/.gitignore");
if (null === $regexps = $this->readGitignoreFile("{$parentDirectory}/.gitignore")) {
continue;
}

[$exclusionRegex, $inclusionRegex] = $regexps;

if (preg_match($exclusionRegex, $fileRelativePath)) {
$ignored = true;

if (null !== $regex && preg_match($regex, $fileRelativePath)) {
return false;
continue;
}

if (0 !== strpos($parentDirectory, $this->baseDir)) {
break;
if (preg_match($inclusionRegex, $fileRelativePath)) {
$ignored = false;
}
}

return true;
return $this->ignoredPathsCache[$fileRealPath] = $ignored;
}

/**
Expand Down Expand Up @@ -87,7 +117,10 @@ private function parentsDirectoryDownward(string $fileRealPath): array
return array_reverse($parentDirectories);
}

private function readGitignoreFile(string $path): ?string
/**
* @return array{0: string, 1: string}|null
*/
private function readGitignoreFile(string $path): ?array
{
if (\array_key_exists($path, $this->gitignoreFilesCache)) {
return $this->gitignoreFilesCache[$path];
Expand All @@ -101,7 +134,12 @@ private function readGitignoreFile(string $path): ?string
throw new \RuntimeException("The \"ignoreVCSIgnored\" option cannot be used by the Finder as the \"{$path}\" file is not readable.");
}

return $this->gitignoreFilesCache[$path] = Gitignore::toRegex(file_get_contents($path));
$gitignoreFileContent = file_get_contents($path);

return $this->gitignoreFilesCache[$path] = [
Gitignore::toRegex($gitignoreFileContent),
Gitignore::toRegexMatchingNegatedPatterns($gitignoreFileContent),
];
}

private function normalizePath(string $path): string
Expand Down
Loading