Browse Source

Properly check for directory paths with trailing slash in IgnoreFilterManager.

Jelmer Vernooij 7 years ago
parent
commit
e687adb8a8
2 changed files with 10 additions and 1 deletions
  1. 6 1
      dulwich/ignore.py
  2. 4 0
      dulwich/tests/test_ignore.py

+ 6 - 1
dulwich/ignore.py

@@ -116,11 +116,12 @@ def read_ignore_patterns(f):
         yield l
 
 
-def match_pattern(path, pattern, ignorecase):
+def match_pattern(path, pattern, ignorecase=False):
     """Match a gitignore-style pattern against a path.
 
     :param path: Path to match
     :param pattern: Pattern to match
+    :param ignorecase: Whether to do case-sensitive matching
     :return: bool indicating whether the pattern matched
     """
     return Pattern(pattern, ignorecase).match(path)
@@ -304,6 +305,10 @@ class IgnoreFilterManager(object):
             dirname = '/'.join(parts[:i])
             for s, f in filters:
                 relpath = '/'.join(parts[s:i])
+                if i < len(parts):
+                    # Paths leading up to the final part are all directories,
+                    # so need a trailing slash.
+                    relpath += b'/'
                 matches = list(f.find_matching(relpath))
                 if matches:
                     return iter(matches)

+ 4 - 0
dulwich/tests/test_ignore.py

@@ -191,6 +191,7 @@ class IgnoreFilterManagerTests(TestCase):
         with open(os.path.join(repo.path, '.gitignore'), 'wb') as f:
             f.write(b'/foo/bar\n')
             f.write(b'/dir2\n')
+            f.write(b'/dir3/\n')
         os.mkdir(os.path.join(repo.path, 'dir'))
         with open(os.path.join(repo.path, 'dir', '.gitignore'), 'wb') as f:
             f.write(b'/blie\n')
@@ -208,6 +209,9 @@ class IgnoreFilterManagerTests(TestCase):
         self.assertTrue(m.is_ignored(os.path.join(repo.path, 'excluded')))
         self.assertTrue(m.is_ignored(os.path.join(
             repo.path, 'dir2', 'fileinignoreddir')))
+        self.assertFalse(m.is_ignored(b'dir3'))
+        self.assertTrue(m.is_ignored(b'dir3/'))
+        self.assertTrue(m.is_ignored(b'dir3/bla'))
 
     def test_load_ignore_ignorecase(self):
         tmp_dir = tempfile.mkdtemp()