From 02fd9580c67c713e85a3cd79eba8d5cca8b1e7a3 Mon Sep 17 00:00:00 2001 From: Vladimir Chebotarev Date: Fri, 26 Aug 2022 07:25:23 +0300 Subject: [PATCH 1/2] Added test ignoring all subdirectories. --- tests.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests.py b/tests.py index 4f5faad..5863579 100644 --- a/tests.py +++ b/tests.py @@ -111,6 +111,13 @@ def test_single_asterisk(self): self.assertTrue(matches('/home/michael/directory')) self.assertTrue(matches('/home/michael/directory-trailing/')) + def test_ignore_all_subdirectories(self): + matches = _parse_gitignore_string('**/', fake_base_dir='/home/michael') + self.assertFalse(matches('/home/michael/file.txt')) + self.assertTrue(matches('/home/michael/directory/')) + self.assertTrue(matches('/home/michael/directory/file.txt')) + self.assertTrue(matches('/home/michael/directory/subdirectory/')) + def _parse_gitignore_string(data: str, fake_base_dir: str = None): with patch('builtins.open', mock_open(read_data=data)): From b38c5ebffd92e831b08574edc398328da525c8f0 Mon Sep 17 00:00:00 2001 From: Vladimir Chebotarev Date: Fri, 26 Aug 2022 10:36:39 +0300 Subject: [PATCH 2/2] Avoid error. --- gitignore_parser.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/gitignore_parser.py b/gitignore_parser.py index fa6cc85..b3ab236 100644 --- a/gitignore_parser.py +++ b/gitignore_parser.py @@ -78,17 +78,17 @@ def rule_from_pattern(pattern, base_path=None, source=None): # A slash is a sign that we're tied to the base_path of our rule # set. anchored = '/' in pattern[:-1] - if pattern[0] == '/': + if pattern[:1] == '/': pattern = pattern[1:] - if pattern[0] == '*' and len(pattern) >= 2 and pattern[1] == '*': + if pattern[:2] == '**': pattern = pattern[2:] anchored = False - if pattern[0] == '/': + if pattern[:1] == '/': pattern = pattern[1:] - if pattern[-1] == '/': + if pattern[-1:] == '/': pattern = pattern[:-1] # patterns with leading hashes are escaped with a backslash in front, unescape it - if pattern[0] == '\\' and pattern[1] == '#': + if pattern[:2] == '\\#': pattern = pattern[1:] # trailing spaces are ignored unless they are escaped with a backslash i = len(pattern)-1