Переглянути джерело

Support ** in most patterns.

Jelmer Vernooij 7 роки тому
батько
коміт
bcce8a409a
2 змінених файлів з 18 додано та 13 видалено
  1. 8 4
      dulwich/ignore.py
  2. 10 9
      dulwich/tests/test_ignore.py

+ 8 - 4
dulwich/ignore.py

@@ -40,14 +40,20 @@ def translate(pat):
     if not '/' in pat:
         res = '(.*\/)?'
 
-    pat = pat.lstrip('/')
+    if pat.startswith(b'/'):
+        pat = pat[1:]
+
     i, n = 0, len(pat)
 
     while i < n:
         c = pat[i]
         i = i+1
         if c == '*':
-            res = res + '.*'
+            if i < n and pat[i] == '*':
+                res = res + '.*?'
+                i = i+1
+            else:
+                res = res + '[^\/]+'
         elif c == '?':
             res = res + '.'
         elif c == '[':
@@ -107,8 +113,6 @@ def match_pattern(path, pattern):
     :return: bool indicating whether the pattern matched
     """
     re_pattern = translate(pattern)
-    if b'/' not in pattern:
-        return re.match(re_pattern, posixpath.basename(path))
     return re.match(re_pattern, path)
 
 

+ 10 - 9
dulwich/tests/test_ignore.py

@@ -42,6 +42,7 @@ POSITIVE_MATCH_TESTS = [
     ("foo/bar/bla.c", "foo/**"),
     ("foo/bar/bla/blie.c", "foo/**/blie.c"),
     ("foo/bar/bla.c", "**/bla.c"),
+    ("bla.c", "**/bla.c"),
 ]
 
 NEGATIVE_MATCH_TESTS = [
@@ -52,15 +53,15 @@ NEGATIVE_MATCH_TESTS = [
 
 
 TRANSLATE_TESTS = [
-    ("*.c", '[^\\/]+\\.c$(?ms)'),
-    ("foo.c", 'foo\\.c$(?ms)'),
-    ("/*.c", '\\/[^\\/]+\\.c$(?ms)'),
-    ("/foo.c", '\\/foo\\.c$(?ms)'),
-    ("foo.c", 'foo\\.c$(?ms)'),
-    ("foo.[ch]",  'foo\\.[ch]$(?ms)'),
-    ("foo/**", 'foo\\/.*?$(?ms)'),
-    ("foo/**/blie.c", 'foo\\/.*?\\/blie\\.c$(?ms)'),
-    ("**/bla.c", '.*?\\/bla\\.c$(?ms)'),
+    ("*.c", '(.*\\/)?[^\\/]+\\.c\\Z(?ms)'),
+    ("foo.c", '(.*\\/)?foo\\.c\\Z(?ms)'),
+    ("/*.c", '[^\\/]+\\.c\\Z(?ms)'),
+    ("/foo.c", 'foo\\.c\\Z(?ms)'),
+    ("foo.c", '(.*\\/)?foo\\.c\\Z(?ms)'),
+    ("foo.[ch]", '(.*\\/)?foo\\.[ch]\\Z(?ms)'),
+    ("foo/**", 'foo\\/.*?\\Z(?ms)'),
+    ("foo/**/blie.c", 'foo\\/.*?\\/blie\\.c\\Z(?ms)'),
+    ("**/bla.c", '.*?\\/bla\\.c\\Z(?ms)'),
 ]