Skip to content

Commit

Permalink
[Finder] Also consider .git inside the basedir of in() directory
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <coding@schilljs.com>
  • Loading branch information
nickvergessen committed Apr 23, 2024
1 parent 3d93aad commit 511c489
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
6 changes: 3 additions & 3 deletions Iterator/VcsIgnoredFilterIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ public function __construct(\Iterator $iterator, string $baseDir)
{
$this->baseDir = $this->normalizePath($baseDir);

foreach ($this->parentDirectoriesUpwards($this->baseDir) as $parentDirectory) {
if (@is_dir("{$parentDirectory}/.git")) {
$this->baseDir = $parentDirectory;
foreach ([$this->baseDir, ...$this->parentDirectoriesUpwards($this->baseDir)] as $directory) {
if (@is_dir("{$directory}/.git")) {
$this->baseDir = $directory;
break;
}
}
Expand Down
54 changes: 52 additions & 2 deletions Tests/Iterator/VcsIgnoredFilterIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected function tearDown(): void
*
* @dataProvider getAcceptData
*/
public function testAccept(array $gitIgnoreFiles, array $otherFileNames, array $expectedResult)
public function testAccept(array $gitIgnoreFiles, array $otherFileNames, array $expectedResult, string $baseDir = '')
{
$otherFileNames = $this->toAbsolute($otherFileNames);
foreach ($otherFileNames as $path) {
Expand All @@ -51,7 +51,8 @@ public function testAccept(array $gitIgnoreFiles, array $otherFileNames, array $

$inner = new InnerNameIterator($otherFileNames);

$iterator = new VcsIgnoredFilterIterator($inner, $this->tmpDir);
$baseDir = $this->tmpDir.('' !== $baseDir ? '/'.$baseDir : '');
$iterator = new VcsIgnoredFilterIterator($inner, $baseDir);

$this->assertIterator($this->toAbsolute($expectedResult), $iterator);
}
Expand All @@ -74,6 +75,55 @@ public static function getAcceptData(): iterable
],
];

yield 'simple file - .gitignore and in() from repository root' => [
[
'.gitignore' => 'a.txt',
],
[
'.git',
'a.txt',
'b.txt',
'dir/',
'dir/a.txt',
],
[
'.git',
'b.txt',
'dir',
],
];

yield 'nested git repositories only consider .gitignore files of the most inner repository' => [
[
'.gitignore' => "nested/*\na.txt",
'nested/.gitignore' => 'c.txt',
'nested/dir/.gitignore' => 'f.txt',
],
[
'.git',
'a.txt',
'b.txt',
'nested/',
'nested/.git',
'nested/c.txt',
'nested/d.txt',
'nested/dir/',
'nested/dir/e.txt',
'nested/dir/f.txt',
],
[
'.git',
'a.txt',
'b.txt',
'nested',
'nested/.git',
'nested/d.txt',
'nested/dir',
'nested/dir/e.txt',
],
'nested',
];

yield 'simple file at root' => [
[
'.gitignore' => '/a.txt',
Expand Down

0 comments on commit 511c489

Please sign in to comment.