Skip to content

Commit 74a67c4

Browse files
srozefabpot
authored andcommitted
[Finder] Glob wildcard while using double-star without ending slash
1 parent 851a876 commit 74a67c4

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

Glob.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,19 @@ public static function toRegex($glob, $strictLeadingDot = true, $strictWildcardS
6060

6161
$firstByte = '/' === $car;
6262

63-
if ($firstByte && $strictWildcardSlash && isset($glob[$i + 3]) && '**/' === $glob[$i + 1].$glob[$i + 2].$glob[$i + 3]) {
64-
$car = $strictLeadingDot ? '/(?:(?=[^\.])[^/]++/)*' : '/(?:[^/]++/)*';
65-
$i += 3;
63+
if ($firstByte && $strictWildcardSlash && isset($glob[$i + 2]) && '**' === $glob[$i + 1].$glob[$i + 2] && (!isset($glob[$i + 3]) || '/' === $glob[$i + 3])) {
64+
$car = '[^/]++/';
65+
if (!isset($glob[$i + 3])) {
66+
$car .= '?';
67+
}
68+
69+
if ($strictLeadingDot) {
70+
$car = '(?=[^\.])'.$car;
71+
}
72+
73+
$car = '/(?:'.$car.')*';
74+
$i += 2 + isset($glob[$i + 3]);
75+
6676
if ('/' === $delimiter) {
6777
$car = str_replace('/', '\\/', $car);
6878
}

Tests/Fixtures/one/.dot

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.dot

Tests/GlobTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,38 @@ public function testGlobToRegexDoubleStarNonStrictDots()
5858

5959
$this->assertSame(array('.dot/b/c.neon', '.dot/b/d.neon', 'one/b/c.neon', 'one/b/d.neon'), $match);
6060
}
61+
62+
public function testGlobToRegexDoubleStarWithoutLeadingSlash()
63+
{
64+
$finder = new Finder();
65+
$finder->ignoreDotFiles(false);
66+
$regex = Glob::toRegex('/Fixtures/one/**');
67+
68+
foreach ($finder->in(__DIR__) as $k => $v) {
69+
$k = str_replace(DIRECTORY_SEPARATOR, '/', $k);
70+
if (preg_match($regex, substr($k, strlen(__DIR__)))) {
71+
$match[] = substr($k, 10 + strlen(__DIR__));
72+
}
73+
}
74+
sort($match);
75+
76+
$this->assertSame(array('one/a', 'one/b', 'one/b/c.neon', 'one/b/d.neon'), $match);
77+
}
78+
79+
public function testGlobToRegexDoubleStarWithoutLeadingSlashNotStrictLeadingDot()
80+
{
81+
$finder = new Finder();
82+
$finder->ignoreDotFiles(false);
83+
$regex = Glob::toRegex('/Fixtures/one/**', false);
84+
85+
foreach ($finder->in(__DIR__) as $k => $v) {
86+
$k = str_replace(DIRECTORY_SEPARATOR, '/', $k);
87+
if (preg_match($regex, substr($k, strlen(__DIR__)))) {
88+
$match[] = substr($k, 10 + strlen(__DIR__));
89+
}
90+
}
91+
sort($match);
92+
93+
$this->assertSame(array('one/.dot', 'one/a', 'one/b', 'one/b/c.neon', 'one/b/d.neon'), $match);
94+
}
6195
}

0 commit comments

Comments
 (0)