Browse Source

Merge pull request #1041 from jelmer/tar-unicode

Support non-ascii characters in filenames in tar streams
Jelmer Vernooij 2 năm trước cách đây
mục cha
commit
e697785c7f
2 tập tin đã thay đổi với 13 bổ sung1 xóa
  1. 1 1
      dulwich/archive.py
  2. 12 0
      dulwich/tests/test_archive.py

+ 1 - 1
dulwich/archive.py

@@ -110,7 +110,7 @@ def tar_stream(store, tree, mtime, prefix=b"", format=""):
 
             info = tarfile.TarInfo()
             # tarfile only works with ascii.
-            info.name = entry_abspath.decode("ascii")
+            info.name = entry_abspath.decode('utf-8', 'surrogateescape')
             info.size = blob.raw_length()
             info.mode = entry.mode
             info.mtime = mtime

+ 12 - 0
dulwich/tests/test_archive.py

@@ -73,6 +73,18 @@ class ArchiveTests(TestCase):
         self.addCleanup(tf.close)
         self.assertEqual(["somename"], tf.getnames())
 
+    def test_unicode(self):
+        store = MemoryObjectStore()
+        b1 = Blob.from_string(b"somedata")
+        store.add_object(b1)
+        t1 = Tree()
+        t1.add("ő".encode('utf-8'), 0o100644, b1.id)
+        store.add_object(t1)
+        stream = b"".join(tar_stream(store, t1, mtime=0))
+        tf = tarfile.TarFile(fileobj=BytesIO(stream))
+        self.addCleanup(tf.close)
+        self.assertEqual(["ő"], tf.getnames())
+
     def test_prefix(self):
         stream = self._get_example_tar_stream(mtime=0, prefix=b"blah")
         tf = tarfile.TarFile(fileobj=stream)