Quellcode durchsuchen

Fix handling of escaped characters in ignore files. Fixes #930

Jelmer Vernooij vor 3 Jahren
Ursprung
Commit
e7818a9482
2 geänderte Dateien mit 15 neuen und 1 gelöschten Zeilen
  1. 3 0
      dulwich/ignore.py
  2. 12 1
      dulwich/tests/test_ignore.py

+ 3 - 0
dulwich/ignore.py

@@ -52,6 +52,9 @@ def _translate_segment(segment: bytes) -> bytes:
             res += b"[^/]*"
         elif c == b"?":
             res += b"[^/]"
+        elif c == b"\\":
+            res += re.escape(segment[i : i + 1])
+            i += 1
         elif c == b"[":
             j = i
             if j < n and segment[j : j + 1] == b"!":

+ 12 - 1
dulwich/tests/test_ignore.py

@@ -82,6 +82,11 @@ TRANSLATE_TESTS = [
     (b"**/bla.c", b"(?ms)(.*/)?bla\\.c/?\\Z"),
     (b"foo/**/bar", b"(?ms)foo(/.*)?\\/bar/?\\Z"),
     (b"foo/bar/*", b"(?ms)foo\\/bar\\/[^/]+/?\\Z"),
+    (b"/foo\[bar\]", b"(?ms)foo\\[bar\\]/?\\Z"),
+    (b"/foo\[bar\]", b"(?ms)foo\\[bar\\]/?\\Z"),
+    (b"/foo\[bar\]", b"(?ms)foo\\[bar\\]/?\\Z"),
+    (b"/foo[bar]", b"(?ms)foo[bar]/?\\Z"),
+    (b"/foo[0-9]", b"(?ms)foo[0-9]/?\\Z"),
 ]
 
 
@@ -183,6 +188,12 @@ class IgnoreFilterTests(TestCase):
         self.assertFalse(filter.is_ignored(b"foo/bar/"))
         self.assertFalse(filter.is_ignored(b"foo/bar/bloe"))
 
+    def test_regex_special(self):
+        # See https://github.com/dulwich/dulwich/issues/930#issuecomment-1026166429
+        filter = IgnoreFilter([b"/foo\\[bar\\]", b"/foo"])
+        self.assertTrue(filter.is_ignored("foo"))
+        self.assertTrue(filter.is_ignored("foo[bar]"))
+
 
 class IgnoreFilterStackTests(TestCase):
     def test_stack_first(self):
@@ -239,7 +250,7 @@ class IgnoreFilterManagerTests(TestCase):
 
         with open(os.path.join(repo.path, 'foo', 'bar'), 'wb') as f:
             f.write(b'IGNORED')
-        
+
         m = IgnoreFilterManager.from_repo(repo)
         self.assertTrue(m.is_ignored('foo/bar'))