Просмотр исходного кода

Fix python3 support for ignores.

Jelmer Vernooij 7 лет назад
Родитель
Сommit
bcc1ec3930
2 измененных файлов с 12 добавлено и 9 удалено
  1. 4 1
      dulwich/ignore.py
  2. 8 8
      dulwich/tests/test_ignore.py

+ 4 - 1
dulwich/ignore.py

@@ -24,6 +24,7 @@ For details for the matching rules, see https://git-scm.com/docs/gitignore
 
 import os.path
 import re
+import sys
 
 
 def translate(pat):
@@ -145,6 +146,8 @@ class IgnoreFilter(object):
         :return: None if file is not mentioned, True if it is included, False
             if it is explicitly excluded.
         """
+        if not isinstance(path, bytes):
+            path = path.encode(sys.getfilesystemencoding())
         status = None
         for pattern in self._patterns:
             if pattern[0:1] == b'!':
@@ -159,7 +162,7 @@ class IgnoreFilter(object):
 
     @classmethod
     def from_path(cls, path):
-        with open(path, 'r') as f:
+        with open(path, 'rb') as f:
             ret = cls(read_ignore_patterns(f))
             ret._path = path
             return ret

+ 8 - 8
dulwich/tests/test_ignore.py

@@ -165,16 +165,16 @@ class IgnoreFilterManagerTests(TestCase):
         tmp_dir = tempfile.mkdtemp()
         self.addCleanup(shutil.rmtree, tmp_dir)
         repo = Repo.init(tmp_dir)
-        with open(os.path.join(repo.path, '.gitignore'), 'w') as f:
-            f.write('/foo/bar\n')
+        with open(os.path.join(repo.path, '.gitignore'), 'wb') as f:
+            f.write(b'/foo/bar\n')
         os.mkdir(os.path.join(repo.path, 'dir'))
-        with open(os.path.join(repo.path, 'dir', '.gitignore'), 'w') as f:
-            f.write('/blie\n')
-        with open(os.path.join(repo.path, 'dir', 'blie'), 'w') as f:
-            f.write('IGNORED')
+        with open(os.path.join(repo.path, 'dir', '.gitignore'), 'wb') as f:
+            f.write(b'/blie\n')
+        with open(os.path.join(repo.path, 'dir', 'blie'), 'wb') as f:
+            f.write(b'IGNORED')
         p = os.path.join(repo.controldir(), 'info', 'exclude')
-        with open(p, 'w') as f:
-            f.write('/excluded\n')
+        with open(p, 'wb') as f:
+            f.write(b'/excluded\n')
         m = IgnoreFilterManager.from_repo(repo)
         self.assertTrue(m.is_ignored(os.path.join(repo.path, 'dir', 'blie')))
         self.assertIs(None,