8000 [Finder] Also consider .git inside the basedir of in() directory · symfony/symfony@fdf5dcc · GitHub
[go: up one dir, main page]

Skip to content

Commit fdf5dcc

Browse files
committed
[Finder] Also consider .git inside the basedir of in() directory
Signed-off-by: Joas Schilling <coding@schilljs.com>
1 parent 6b3407d commit fdf5dcc

File tree

2 files changed

+59
-5
lines changed

2 files changed

+59
-5
lines changed

src/Symfony/Component/Finder/Iterator/VcsIgnoredFilterIterator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ public function __construct(\Iterator $iterator, string $baseDir)
3737
{
3838
$this->baseDir = $this->normalizePath($baseDir);
3939

40-
foreach ($this->parentDirectoriesUpwards($this->baseDir) as $parentDirectory) {
41-
if (@is_dir("{$parentDirectory}/.git")) {
42-
$this->baseDir = $parentDirectory;
40+
foreach ([$this->baseDir, ...$this->parentDirectoriesUpwards($this->baseDir)] as $directory) {
41+
if (@is_dir("{$directory}/.git")) {
42+
$this->baseDir = $directory;
4343
break;
4444
}
4545
}

src/Symfony/Component/Finder/Tests/Iterator/VcsIgnoredFilterIteratorTest.php

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ protected function tearDown(): void
3434
*
3535
* @dataProvider getAcceptData
3636
*/
37-
public function testAccept(array $gitIgnoreFiles, array $otherFileNames, array $expectedResult)
37+
public function testAccept(array $gitIgnoreFiles, array $otherFileNames, array $expectedResult, string $baseDir = '')
3838
{
3939
$otherFileNames = $this->toAbsolute($otherFileNames);
4040
foreach ($otherFileNames as $path) {
@@ -51,7 +51,12 @@ public function testAccept(array $gitIgnoreFiles, array $otherFileNames, array $
5151

5252
$inner = new InnerNameIterator($otherFileNames);
5353

54-
$iterator = new VcsIgnoredFilterIterator($inner, $this->tmpDir);
54+
if ($baseDir !== '') {
55+
$baseDir = $this->tmpDir . '/' . $baseDir;
56+
} else {
57+
$baseDir = $this->tmpDir;
58+
}
59+
$iterator = new VcsIgnoredFilterIterator($inner, $baseDir);
5560

5661
$this->assertIterator($this->toAbsolute($expectedResult), $iterator);
5762
}
@@ -74,6 +79,55 @@ public static function getAcceptData(): iterable
7479
],
7580
];
7681

82+
yield 'simple file - .gitignore and in() from repository root' => [
83+
[
84+
'.gitignore' => 'a.txt',
85+
],
86+
[
87+
'.git',
88+
'a.txt',
89+
'b.txt',
90+
'dir/',
91+
'dir/a.txt',
92+
],
93+
[
94+
'.git',
95+
'b.txt',
96+
'dir',
97+
],
98+
];
99+
100+
yield 'nested git repositories only consider .gitignore files of the most inner repository' => [
101+
[
102+
'.gitignore' => "nested/*\na.txt",
103+
'nested/.gitignore' => 'c.txt',
104+
'nested/dir/.gitignore' => 'f.txt',
105+
],
106+
[
107+
'.git',
108+
'a.txt',
109+
'b.txt',
110+
'nested/',
111+
'nested/.git',
112+
'nested/c.txt',
113+
'nested/d.txt',
114+
'nested/dir/',
115+
'nested/dir/e.txt',
116+
'nested/dir/f.txt',
117+
],
118+
[
119+
'.git',
120+
'a.txt',
121+
'b.txt',
122+
'nested',
123+
'nested/.git',
124+
'nested/d.txt',
125+
'nested/dir',
126+
'nested/dir/e.txt',
127+
],
128+
'nested',
129+
];
130+
77131
yield 'simple file at root' => [
78132
[
79133
'.gitignore' => '/a.txt',

0 commit comments

Comments
 (0)
0