浏览代码

Support a prefix argument to dulwich.archive.tar_stream.

Jelmer Vernooij 6 年之前
父节点
当前提交
78b4c62510
共有 3 个文件被更改,包括 11 次插入2 次删除
  1. 3 0
      NEWS
  2. 2 2
      dulwich/archive.py
  3. 6 0
      dulwich/tests/test_archive.py

+ 3 - 0
NEWS

@@ -15,6 +15,9 @@
 
   * Add basic `dulwich.stash` module. (Jelmer Vernooij)
 
+  * Support a `prefix` argument to `dulwich.archive.tar_stream`.
+    (Jelmer Vernooij)
+
  BUG FIXES
 
   * Fix handling of encoding for tags. (Jelmer Vernooij, #608)

+ 2 - 2
dulwich/archive.py

@@ -68,7 +68,7 @@ class ChunkedBytesIO(object):
         return b''.join(buf)
 
 
-def tar_stream(store, tree, mtime, format=''):
+def tar_stream(store, tree, mtime, prefix=b'', format=''):
     """Generate a tar stream for the contents of a Git tree.
 
     Returns a generator that lazily assembles a .tar.gz archive, yielding it in
@@ -96,7 +96,7 @@ def tar_stream(store, tree, mtime, format=''):
             buf.write(struct.pack('<L', mtime))
             buf.seek(0, SEEK_END)
 
-        for entry_abspath, entry in _walk_tree(store, tree):
+        for entry_abspath, entry in _walk_tree(store, tree, prefix):
             try:
                 blob = store[entry.sha]
             except KeyError:

+ 6 - 0
dulwich/tests/test_archive.py

@@ -75,6 +75,12 @@ class ArchiveTests(TestCase):
         self.addCleanup(tf.close)
         self.assertEqual(["somename"], tf.getnames())
 
+    def test_prefix(self):
+        stream = self._get_example_tar_stream(mtime=0, prefix=b'blah')
+        tf = tarfile.TarFile(fileobj=stream)
+        self.addCleanup(tf.close)
+        self.assertEqual(["blah/somename"], tf.getnames())
+
     def test_gzip_mtime(self):
         stream = self._get_example_tar_stream(mtime=1234, format='gz')
         expected_mtime = struct.pack('<L', 1234)