Skip to content

Commit

Permalink
Fix error with subdirectories
Browse files Browse the repository at this point in the history
Thank you @excitoon for the PR!
  • Loading branch information
excitoon authored Aug 29, 2022
1 parent f243daa commit da9d8e9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
10 changes: 5 additions & 5 deletions gitignore_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)):
Expand Down

0 comments on commit da9d8e9

Please sign in to comment.