Skip to content

Commit

Permalink
Merge branch '4.4' into 5.2
Browse files Browse the repository at this point in the history
* 4.4:
  [Finder] Fix gitignore regex build with "**"
  Fixed deprecation warnings about passing null as parameter
  [Security] Keep Bulgarian wording consistent across all texts.
  Migrate configuration file for PHP CS Fixer 2.19/3.0
  [Form] Replace broken ServerParams mock
  • Loading branch information
fabpot committed May 16, 2021
2 parents eccb8be + a96bc19 commit ccccb9d
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 39 deletions.
4 changes: 2 additions & 2 deletions Comparator/NumberComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ class NumberComparator extends Comparator
*/
public function __construct(?string $test)
{
if (!preg_match('#^\s*(==|!=|[<>]=?)?\s*([0-9\.]+)\s*([kmg]i?)?\s*$#i', $test, $matches)) {
throw new \InvalidArgumentException(sprintf('Don\'t understand "%s" as a number test.', $test));
if (null === $test || !preg_match('#^\s*(==|!=|[<>]=?)?\s*([0-9\.]+)\s*([kmg]i?)?\s*$#i', $test, $matches)) {
throw new \InvalidArgumentException(sprintf('Don\'t understand "%s" as a number test.', $test ?? 'null'));
}

$target = $matches[2];
Expand Down
22 changes: 9 additions & 13 deletions Gitignore.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,16 @@ private static function lineToRegex(string $gitignoreLine): string
$isAbsolute = false;
}

$parts = array_map(function (string $v): string {
$v = preg_quote(str_replace('\\', '', $v), '~');
$v = preg_replace_callback('~\\\\\[([^\[\]]*)\\\\\]~', function (array $matches): string {
return '['.str_replace('\\-', '-', $matches[1]).']';
}, $v);
$v = preg_replace('~\\\\\*\\\\\*~', '[^/]+(?:/[^/]+)*', $v);
$v = preg_replace('~\\\\\*~', '[^/]*', $v);
$v = preg_replace('~\\\\\?~', '[^/]', $v);

return $v;
}, explode('/', $gitignoreLine));
$regex = preg_quote(str_replace('\\', '', $gitignoreLine), '~');
$regex = preg_replace_callback('~\\\\\[((?:\\\\!)?)([^\[\]]*)\\\\\]~', function (array $matches): string {
return '['.('' !== $matches[1] ? '^' : '').str_replace('\\-', '-', $matches[2]).']';
}, $regex);
$regex = preg_replace('~(?:(?:\\\\\*){2,}(/?))+~', '(?:(?:(?!//).(?<!//))+$1)?', $regex);
$regex = preg_replace('~\\\\\*~', '[^/]*', $regex);
$regex = preg_replace('~\\\\\?~', '[^/]', $regex);

return ($isAbsolute ? '' : '(?:[^/]+/)*')
.implode('/', $parts)
.('' !== end($parts) ? '(?:$|/)' : '');
.$regex
.('/' !== substr($gitignoreLine, -1) ? '(?:$|/)' : '');
}
}
154 changes: 130 additions & 24 deletions Tests/GitignoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ public function provider(): array
[],
],
[
['/a', 'm/*'],
['a', 'a/b', 'a/b/c', 'm/'],
['aa', 'm', 'b/m', 'b/m/'],
['/a', 'm/*', 'o/**', 'p/**/', 'x**y'],
['a', 'a/b', 'a/b/c', 'm/', 'o/', 'p/', 'xy', 'xuy', 'x/y', 'x/u/y', 'xu/y', 'x/uy', 'xu/uy'],
['aa', 'm', 'b/m', 'b/m/', 'o', 'b/o', 'b/o/', 'p', 'b/p', 'b/p/'],
],
[
['a', '!x'],
Expand Down Expand Up @@ -173,8 +173,8 @@ public function provider(): array
],
[
['dir1/**/dir2/'],
['dir1/dirA/dir2/', 'dir1/dirA/dirB/dir2/'],
[],
['dir1/dir2/', 'dir1/dirA/dir2/', 'dir1/dirA/dirB/dir2/'],
['dir1dir2/', 'dir1xdir2/', 'dir1/xdir2/', 'dir1x/dir2/'],
],
[
['dir1/*/dir2/'],
Expand Down Expand Up @@ -202,37 +202,22 @@ public function provider(): array
['a/app/cache/file.txt'],
],
[
[
'#IamComment',
'/app/cache/',
],
['#IamComment', '/app/cache/'],
['app/cache/file.txt', 'app/cache/subdir/ile.txt'],
['a/app/cache/file.txt', '#IamComment', 'IamComment'],
],
[
[
'/app/cache/',
'#LastLineIsComment',
],
['/app/cache/', '#LastLineIsComment'],
['app/cache/file.txt', 'app/cache/subdir/ile.txt'],
['a/app/cache/file.txt', '#LastLineIsComment', 'LastLineIsComment'],
],
[
[
'/app/cache/',
'\#file.txt',
'#LastLineIsComment',
],
['/app/cache/', '\#file.txt', '#LastLineIsComment'],
['app/cache/file.txt', 'app/cache/subdir/ile.txt', '#file.txt'],
['a/app/cache/file.txt', '#LastLineIsComment', 'LastLineIsComment'],
],
[
[
'/app/cache/',
'\#file.txt',
'#IamComment',
'another_file.txt',
],
['/app/cache/', '\#file.txt', '#IamComment', 'another_file.txt'],
['app/cache/file.txt', 'app/cache/subdir/ile.txt', '#file.txt', 'another_file.txt'],
['a/app/cache/file.txt', 'IamComment', '#IamComment'],
],
Expand Down Expand Up @@ -280,6 +265,127 @@ public function provider(): array
['example/test', 'example/example.txt2', 'example/packages/foo.yaml'],
['example/example.txt', 'example/packages', 'example/packages/'],
],
// based on https://www.atlassian.com/git/tutorials/saving-changes/gitignore
[
['**/logs'],
['logs/debug.log', 'logs/monday/foo.bar'],
[],
],
[
['**/logs/debug.log'],
['logs/debug.log', 'build/logs/debug.log'],
['logs/build/debug.log'],
],
[
['*.log'],
['debug.log', 'foo.log', '.log', 'logs/debug.log'],
[],
],
[
[
'*.log',
'!important.log',
],
['debug.log', 'trace.log'],
['important.log', 'logs/important.log'],
],
[
[
'*.log',
'!important/*.log',
'trace.*',
],
['debug.log', 'important/trace.log'],
['important/debug.log'],
],
[
['/debug.log'],
['debug.log'],
['logs/debug.log'],
],
[
['debug.log'],
['debug.log', 'logs/debug.log'],
[],
],
[
['debug?.log'],
['debug0.log', 'debugg.log'],
['debug10.log'],
],
[
['debug[0-9].log'],
['debug0.log', 'debug1.log'],
['debug10.log'],
],
[
['debug[01].log'],
['debug0.log', 'debug1.log'],
['debug2.log', 'debug01.log'],
],
[
['debug[!01].log'],
['debug2.log'],
['debug0.log', 'debug1.log', 'debug01.log'],
],
[
['debug[a-z].log'],
['debuga.log', 'debugb.log'],
['debug1.log'],
],
[
['logs'],
['logs', 'logs/debug.log', 'logs/latest/foo.bar', 'build/logs', 'build/logs/debug.log'],
[],
],
[
['logs/'],
['logs/debug.log', 'logs/latest/foo.bar', 'build/logs/foo.bar', 'build/logs/latest/debug.log'],
[],
],
[
[
'logs/',
'!logs/important.log',
],
['logs/debug.log'/* must be pruned on traversal 'logs/important.log'*/],
[],
],
[
['logs/**/debug.log'],
['logs/debug.log', 'logs/monday/debug.log', 'logs/monday/pm/debug.log'],
[],
],
[
['logs/*day/debug.log'],
['logs/monday/debug.log', 'logs/tuesday/debug.log'],
['logs/latest/debug.log'],
],
[
['logs/debug.log'],
['logs/debug.log'],
['debug.log', 'build/logs/debug.log'],
],
[
['*/vendor/*'],
['a/vendor/', 'a/vendor/b', 'a/vendor/b/c'],
['a', 'vendor', 'vendor/', 'a/vendor', 'a/b/vendor', 'a/b/vendor/c'],
],
[
['**/vendor/**'],
['vendor/', 'vendor/a', 'vendor/a/b', 'a/b/vendor/c/d'],
['a', 'vendor', 'a/vendor', 'a/b/vendor'],
],
[
['***/***/vendor/*****/*****'],
['vendor/', 'vendor/a', 'vendor/a/b', 'a/b/vendor/c/d'],
['a', 'vendor', 'a/vendor', 'a/b/vendor'],
],
[
['**vendor**'],
['vendor', 'vendor/', 'vendor/a', 'vendor/a/b', 'a/vendor', 'a/b/vendor', 'a/b/vendor/c/d'],
['a'],
],
];

return $cases;
Expand Down

0 comments on commit ccccb9d

Please sign in to comment.