Skip to content

Commit

Permalink
Deployer::matchMask() improved matching
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jan 21, 2015
1 parent 2b60276 commit eeffa88
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 37 deletions.
16 changes: 9 additions & 7 deletions Deployment/libs/Deployer.php
Original file line number Diff line number Diff line change
Expand Up @@ -406,16 +406,18 @@ public static function matchMask($path, array $patterns, $isDir = FALSE)
if ($neg = substr($pattern, 0, 1) === '!') {
$pattern = substr($pattern, 1);
}
if (substr($pattern, -1) === '/') { // trailing slash means directory
if (!$isDir) {
continue;
}
$pattern = substr($pattern, 0, -1);
}
if (strpos($pattern, '/') === FALSE) { // no slash means file name

if (strpos($pattern, '/') === FALSE) { // no slash means base name
if (fnmatch($pattern, end($path), FNM_CASEFOLD)) {
$res = !$neg;
}
continue;

} elseif (substr($pattern, -1) === '/') { // trailing slash means directory
$pattern = trim($pattern, '/');
if (!$isDir && count($path) <= count(explode('/', $pattern))) {
continue;
}
}

$parts = explode('/', ltrim($pattern, '/'));
Expand Down
128 changes: 98 additions & 30 deletions tests/Deployer.matchMask.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require __DIR__ . '/bootstrap.php';


Assert::false( Deployer::matchMask('/deployment.ini', ['*.i[xy]i']) );
Assert::false( Deployer::matchMask('/deployment.ini', ['*.i[!n]i']) );
Assert::true( Deployer::matchMask('/deployment.ini', ['*.ini']) );
Assert::false( Deployer::matchMask('deployment.ini', ['*.ini/']) );
Assert::true( Deployer::matchMask('deployment.ini', ['/*.ini']) );
Expand All @@ -16,35 +17,102 @@ Assert::false( Deployer::matchMask('.git', ['.g*/']) );
Assert::false( Deployer::matchMask('deployment.ini', ['*.ini', '!dep*']) );
Assert::true( Deployer::matchMask('deployment.ini', ['*.ini', '!dep*', '*ment*']) );

Assert::true( Deployer::matchMask('/dir', ['dir']) );
Assert::false( Deployer::matchMask('/dir', ['dir/']) );
Assert::true( Deployer::matchMask('/dir', ['dir/'], TRUE) );
Assert::false( Deployer::matchMask('/dir', ['dir/*']) );

Assert::true( Deployer::matchMask('dir/file', ['file']) );
Assert::false( Deployer::matchMask('dir/file', ['/file']) );
Assert::true( Deployer::matchMask('dir/file', ['dir/file']) );
Assert::true( Deployer::matchMask('dir/file', ['*/file']) );
Assert::true( Deployer::matchMask('dir/file', ['**/file']) );
Assert::false( Deployer::matchMask('dir/subdir/file', ['*/file']) );
Assert::true( Deployer::matchMask('dir/subdir/file', ['*/*/file']) );

// a/* disallowes everything to the right, i.e. a/*/*/*
// !a/*/c allowes directories to the left, i.e. a, a/*
Assert::false( Deployer::matchMask('a', ['/a', '!/*']) );
Assert::false( Deployer::matchMask('a', ['/a', '!/*'], TRUE) );
Assert::true( Deployer::matchMask('a', ['a', '!*/b']) );
Assert::false( Deployer::matchMask('a', ['a', '!*/b'], TRUE) );
Assert::true( Deployer::matchMask('a', ['a', '!a/b', 'a/b/c']) );
Assert::false( Deployer::matchMask('a', ['a', '!a/b', 'a/b/c'], TRUE) );
Assert::false( Deployer::matchMask('a', ['a/*', '!a/b', 'a/b/c']) );
Assert::false( Deployer::matchMask('b', ['a', '!a/b', 'a/b/c']) );
Assert::false( Deployer::matchMask('a/b', ['a', '!a/b', 'a/b/c']) );
Assert::false( Deployer::matchMask('a/b', ['a/*', '!a/b', 'a/b/c']) );
Assert::true( Deployer::matchMask('a/c', ['c', '!a/b', 'a/b/c']) );
Assert::true( Deployer::matchMask('a/c', ['a', '!a/b', 'a/b/c']) );
Assert::true( Deployer::matchMask('a/c', ['a/*', '!a/b', 'a/b/c']) );
Assert::true( Deployer::matchMask('a/b/c', ['a', '!a/b', 'a/b/c']) );
Assert::true( Deployer::matchMask('a/b/c', ['a/*', '!*/b', 'a/b/c']) );
Assert::false( Deployer::matchMask('a/b/d', ['a', '!a/b', 'a/b/c']) );
Assert::true( Deployer::matchMask('a/b/c/d', ['a', '!a/b', 'a/b/c']) );
Assert::true( Deployer::matchMask('a', ['a'] ) );
Assert::true( Deployer::matchMask('a', ['a'], TRUE) );
Assert::false( Deployer::matchMask('a/b', ['a'] ) );
Assert::false( Deployer::matchMask('a/b', ['a'], TRUE) );
Assert::true( Deployer::matchMask('a/b', ['b'] ) );
Assert::true( Deployer::matchMask('a/b', ['b'], TRUE) );
Assert::false( Deployer::matchMask('a/b', ['c'] ) );
Assert::false( Deployer::matchMask('a/b', ['c'], TRUE) );
Assert::false( Deployer::matchMask('a', ['*', '!a'] ) );
Assert::false( Deployer::matchMask('a', ['*', '!a'], TRUE) );
Assert::true( Deployer::matchMask('a/b', ['*', '!a'] ) );
Assert::true( Deployer::matchMask('a/b', ['*', '!a'], TRUE) );
Assert::false( Deployer::matchMask('a/b', ['*', '!b'] ) );
Assert::false( Deployer::matchMask('a/b', ['*', '!b'], TRUE) );
Assert::true( Deployer::matchMask('a/b', ['*', '!c'] ) );
Assert::true( Deployer::matchMask('a/b', ['*', '!c'], TRUE) );

Assert::true( Deployer::matchMask('a', ['/a'] ) );
Assert::true( Deployer::matchMask('a', ['/a'], TRUE) );
Assert::true( Deployer::matchMask('a/b', ['/a'] ) );
Assert::true( Deployer::matchMask('a/b', ['/a'], TRUE) );
Assert::true( Deployer::matchMask('a/b/b', ['/a'] ) );
Assert::true( Deployer::matchMask('a/b/b', ['/a'], TRUE) );
Assert::false( Deployer::matchMask('a', ['*', '!/a'] ) );
Assert::false( Deployer::matchMask('a', ['*', '!/a'], TRUE) );
Assert::false( Deployer::matchMask('a/b', ['*', '!/a'] ) );
Assert::false( Deployer::matchMask('a/b', ['*', '!/a'], TRUE) );
Assert::false( Deployer::matchMask('a/b/b', ['*', '!/a'] ) );
Assert::false( Deployer::matchMask('a/b/b', ['*', '!/a'], TRUE) );
Assert::false( Deployer::matchMask('a', ['a/'] ) );
Assert::true( Deployer::matchMask('a', ['a/'], TRUE) );
Assert::true( Deployer::matchMask('a/b', ['a/'] ) );
Assert::true( Deployer::matchMask('a/b', ['a/'], TRUE) );
Assert::true( Deployer::matchMask('a/b/b', ['a/'] ) );
Assert::true( Deployer::matchMask('a/b/b', ['a/'], TRUE) );
Assert::true( Deployer::matchMask('a', ['*', '!a/'] ) );
Assert::false( Deployer::matchMask('a', ['*', '!a/'], TRUE) );
Assert::false( Deployer::matchMask('a/b', ['*', '!a/'] ) );
Assert::false( Deployer::matchMask('a/b', ['*', '!a/'], TRUE) );
Assert::false( Deployer::matchMask('a/b/b', ['*', '!a/'] ) );
Assert::false( Deployer::matchMask('a/b/b', ['*', '!a/'], TRUE) );
Assert::false( Deployer::matchMask('a', ['a/*'] ) );
Assert::false( Deployer::matchMask('a', ['a/*'], TRUE) );
Assert::true( Deployer::matchMask('a/b', ['a/*'] ) );
Assert::true( Deployer::matchMask('a/b', ['a/*'], TRUE) );
Assert::true( Deployer::matchMask('a/b/b', ['a/*'] ) );
Assert::true( Deployer::matchMask('a/b/b', ['a/*'], TRUE) );
Assert::true( Deployer::matchMask('a', ['*', '!a/*'] ) );
Assert::false( Deployer::matchMask('a', ['*', '!a/*'], TRUE) );
Assert::false( Deployer::matchMask('a/b', ['*', '!a/*'] ) );
Assert::false( Deployer::matchMask('a/b', ['*', '!a/*'], TRUE) );
Assert::false( Deployer::matchMask('a/b/b', ['*', '!a/*'] ) );
Assert::false( Deployer::matchMask('a/b/b', ['*', '!a/*'], TRUE) );
Assert::false( Deployer::matchMask('a', ['a/*/'] ) );
Assert::false( Deployer::matchMask('a', ['a/*/'], TRUE) );
Assert::false( Deployer::matchMask('a/b', ['a/*/'] ) );
Assert::true( Deployer::matchMask('a/b', ['a/*/'], TRUE) );
Assert::true( Deployer::matchMask('a/b/b', ['a/*/'] ) );
Assert::true( Deployer::matchMask('a/b/b', ['a/*/'], TRUE) );
Assert::true( Deployer::matchMask('a', ['*', '!a/*/'] ) );
Assert::false( Deployer::matchMask('a', ['*', '!a/*/'], TRUE) );
Assert::true( Deployer::matchMask('a/b', ['*', '!a/*/'] ) );
Assert::false( Deployer::matchMask('a/b', ['*', '!a/*/'], TRUE) );
Assert::false( Deployer::matchMask('a/b/b', ['*', '!a/*/'] ) );
Assert::false( Deployer::matchMask('a/b/b', ['*', '!a/*/'], TRUE) );
Assert::false( Deployer::matchMask('a', ['a/*/b'] ) );
Assert::false( Deployer::matchMask('a', ['a/*/b'], TRUE) );
Assert::false( Deployer::matchMask('a/b', ['a/*/b'] ) );
Assert::false( Deployer::matchMask('a/b', ['a/*/b'], TRUE) );
Assert::true( Deployer::matchMask('a/b/b', ['a/*/b'] ) );
Assert::true( Deployer::matchMask('a/b/b', ['a/*/b'], TRUE) );
Assert::false( Deployer::matchMask('a/b/c', ['a/*/b'] ) );
Assert::false( Deployer::matchMask('a/b/c', ['a/*/b'], TRUE) );
Assert::true( Deployer::matchMask('a', ['*', '!a/*/b'] ) );
Assert::false( Deployer::matchMask('a', ['*', '!a/*/b'], TRUE) );
Assert::true( Deployer::matchMask('a/b', ['*', '!a/*/b'] ) );
Assert::false( Deployer::matchMask('a/b', ['*', '!a/*/b'], TRUE) );
Assert::false( Deployer::matchMask('a/b/b', ['*', '!a/*/b'] ) );
Assert::false( Deployer::matchMask('a/b/b', ['*', '!a/*/b'], TRUE) );
Assert::true( Deployer::matchMask('a/b/c', ['*', '!a/*/b'] ) );
Assert::true( Deployer::matchMask('a/b/c', ['*', '!a/*/b'], TRUE) );
Assert::false( Deployer::matchMask('a', ['a/*/b/'] ) );
Assert::false( Deployer::matchMask('a', ['a/*/b/'], TRUE) );
Assert::false( Deployer::matchMask('a/b', ['a/*/b/'] ) );
Assert::false( Deployer::matchMask('a/b', ['a/*/b/'], TRUE) );
Assert::false( Deployer::matchMask('a/b/b', ['a/*/b/'] ) );
Assert::true( Deployer::matchMask('a/b/b', ['a/*/b/'], TRUE) );
Assert::false( Deployer::matchMask('a/b/c', ['a/*/b/'] ) );
Assert::false( Deployer::matchMask('a/b/c', ['a/*/b/'], TRUE) );
Assert::true( Deployer::matchMask('a', ['*', '!a/*/b/'] ) );
Assert::false( Deployer::matchMask('a', ['*', '!a/*/b/'], TRUE) );
Assert::true( Deployer::matchMask('a/b', ['*', '!a/*/b/'] ) );
Assert::false( Deployer::matchMask('a/b', ['*', '!a/*/b/'], TRUE) );
Assert::true( Deployer::matchMask('a/b/b', ['*', '!a/*/b/'] ) );
Assert::false( Deployer::matchMask('a/b/b', ['*', '!a/*/b/'], TRUE) );
Assert::true( Deployer::matchMask('a/b/c', ['*', '!a/*/b/'] ) );
Assert::true( Deployer::matchMask('a/b/c', ['*', '!a/*/b/'], TRUE) );

0 comments on commit eeffa88

Please sign in to comment.