Skip to content

Commit

Permalink
Merge pull request #20 from martinssipenko/extra-patters
Browse files Browse the repository at this point in the history
Added support for consecutive asterisks at the beginning or end
  • Loading branch information
timoschinkel authored Jun 29, 2022
2 parents 80e0fb2 + 588eaba commit 3d745ef
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/PatternMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ private function isMatch(Pattern $pattern, string $filename): bool
'*' => '[^\/]+',
'?' => '[^\/]',
'**' => '.*',
'/**' => '\/.*',
'**/' => '.*\/',
'/**/' => '\/([^\/]+\/)*',
];

Expand Down
16 changes: 16 additions & 0 deletions tests/Fixtures/CODEOWNERS.example
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ apps/ @octocat
# directory in the root of your repository.
/docs/ @doctocat

# A leading "**" followed by a slash means match in all directories.
# For example, "**/foo" matches file or directory "foo" anywhere,
# the same as pattern "foo". "**/foo/bar" matches file or directory
# "bar" anywhere that is directly under directory "foo".
**/foo @doctocat

# A trailing "/**" matches everything inside.
# For example, "abc/**" matches all files inside directory "abc"
# with infinite depth.
abc/** @doctocat

# A slash followed by two consecutive asterisks then a slash ma
# zero or more directories. For example, "a/**/b" matches "a/b",
# "a/x/b", "a/x/y/b" and so on.
a/**/b @doctocat

# In this example nobody owns the files
# This line should be ignored by the parser.
/src
6 changes: 6 additions & 0 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public function testParsingResultsInPatterns()
new Pattern('docs/*', ['[email protected]']),
new Pattern('apps/', ['@octocat']),
new Pattern('/docs/', ['@doctocat']),
new Pattern('**/foo', ['@doctocat']),
new Pattern('abc/**', ['@doctocat']),
new Pattern('a/**/b', ['@doctocat']),
], $patterns);
}

Expand All @@ -66,6 +69,9 @@ public function testParsingStringResultsInPatterns()
new Pattern('docs/*', ['[email protected]']),
new Pattern('apps/', ['@octocat']),
new Pattern('/docs/', ['@doctocat']),
new Pattern('**/foo', ['@doctocat']),
new Pattern('abc/**', ['@doctocat']),
new Pattern('a/**/b', ['@doctocat']),
], $patterns);
}
}
10 changes: 10 additions & 0 deletions tests/PatternMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ public function provideCorrectMatchIsReturnedForFilename(): array
// does NOT match "foo/bar/file.ext"

// **
[new Pattern('**/c', ['@owner']), 'a/c'],
[new Pattern('**/c', ['@owner']), 'b/c'],
[new Pattern('**/c', ['@owner']), 'a/b/c'],
[new Pattern('a/**', ['@owner']), 'a/b'],
[new Pattern('a/**', ['@owner']), 'a/b/c'],
[new Pattern('a/**/b', ['@owner']), 'a/b'],
[new Pattern('a/**/b', ['@owner']), 'a/x/b'],
[new Pattern('a/**/b', ['@owner']), 'a/x/y/b'],
Expand Down Expand Up @@ -126,6 +131,11 @@ public function provideNoMatchFoundExceptionIsThrownForFilename(): array
[new Pattern('*.ext', ['@owner']), 'fooext'],
[new Pattern('*.ext', ['@owner']), 'foo/ext'],
[new Pattern('foo/*', ['@owner']), 'foo/bar/file.ext'],

// **
[new Pattern('**/foo', ['@owner']), 'foo.ext'],
[new Pattern('**/foo', ['@owner']), 'bar-foo/file.ext'],
[new Pattern('foo/**', ['@owner']), 'foo.ext'],
];
}
}

0 comments on commit 3d745ef

Please sign in to comment.