浏览代码

Use write_pack_data.

Jelmer Vernooij 7 年之前
父节点
当前提交
4349a6e7e8
共有 2 个文件被更改,包括 27 次插入20 次删除
  1. 13 16
      dulwich/client.py
  2. 14 4
      dulwich/pack.py

+ 13 - 16
dulwich/client.py

@@ -100,6 +100,8 @@ from dulwich.protocol import (
     parse_capability,
     )
 from dulwich.pack import (
+    pack_objects_to_data,
+    write_pack_data,
     write_pack_objects,
     )
 from dulwich.refs import (
@@ -309,7 +311,7 @@ class GitClient(object):
         raise NotImplementedError(cls.from_parsedurl)
 
     def send_pack(self, path, update_refs, generate_pack_contents,
-                  progress=None, write_pack=None):
+                  progress=None):
         """Upload a pack to a remote repository.
 
         :param path: Repository path (as bytestring)
@@ -319,8 +321,6 @@ class GitClient(object):
         :param generate_pack_contents: Function that can return a sequence of
             the shas of the objects to upload.
         :param progress: Optional progress function
-        :param write_pack: Function called with (file, iterable of objects) to
-            write the objects returned by generate_pack_contents to the server.
 
         :raises SendPackError: if server rejects the pack data
         :raises UpdateRefsError: if the server supports report-status
@@ -636,7 +636,7 @@ class TraditionalGitClient(GitClient):
         raise NotImplementedError()
 
     def send_pack(self, path, update_refs, generate_pack_contents,
-                  progress=None, write_pack=write_pack_objects):
+                  progress=None):
         """Upload a pack to a remote repository.
 
         :param path: Repository path (as bytestring)
@@ -646,8 +646,6 @@ class TraditionalGitClient(GitClient):
         :param generate_pack_contents: Function that can return a sequence of
             the shas of the objects to upload.
         :param progress: Optional callback called with progress updates
-        :param write_pack: Function called with (file, iterable of objects) to
-            write the objects returned by generate_pack_contents to the server.
 
         :raises SendPackError: if server rejects the pack data
         :raises UpdateRefsError: if the server supports report-status
@@ -700,12 +698,14 @@ class TraditionalGitClient(GitClient):
                 return new_refs
             objects = generate_pack_contents(have, want)
 
-            dowrite = bool(objects)
+            pack_data_count, pack_data = pack_objects_to_data(objects)
+
+            dowrite = bool(pack_data_count)
             dowrite = dowrite or any(old_refs.get(ref) != sha
                                      for (ref, sha) in new_refs.items()
                                      if sha != ZERO_SHA)
             if dowrite:
-                write_pack(proto.write_file(), objects)
+                write_pack_data(proto.write_file(), pack_data_count, pack_data)
 
             self._handle_receive_pack_tail(
                 proto, negotiated_capabilities, progress)
@@ -949,7 +949,7 @@ class LocalGitClient(GitClient):
         return closing(Repo(path))
 
     def send_pack(self, path, update_refs, generate_pack_contents,
-                  progress=None, write_pack=write_pack_objects):
+                  progress=None):
         """Upload a pack to a remote repository.
 
         :param path: Repository path (as bytestring)
@@ -959,8 +959,6 @@ class LocalGitClient(GitClient):
         :param generate_pack_contents: Function that can return a sequence of
             the shas of the objects to upload.
         :param progress: Optional progress function
-        :param write_pack: Function called with (file, iterable of objects) to
-            write the objects returned by generate_pack_contents to the server.
 
         :raises SendPackError: if server rejects the pack data
         :raises UpdateRefsError: if the server supports report-status
@@ -1335,7 +1333,7 @@ class HttpGitClient(GitClient):
         return resp, read
 
     def send_pack(self, path, update_refs, generate_pack_contents,
-                  progress=None, write_pack=write_pack_objects):
+                  progress=None):
         """Upload a pack to a remote repository.
 
         :param path: Repository path (as bytestring)
@@ -1345,8 +1343,6 @@ class HttpGitClient(GitClient):
         :param generate_pack_contents: Function that can return a sequence of
             the shas of the objects to upload.
         :param progress: Optional progress function
-        :param write_pack: Function called with (file, iterable of objects) to
-            write the objects returned by generate_pack_contents to the server.
 
         :raises SendPackError: if server rejects the pack data
         :raises UpdateRefsError: if the server supports report-status
@@ -1377,8 +1373,9 @@ class HttpGitClient(GitClient):
         if not want and set(new_refs.items()).issubset(set(old_refs.items())):
             return new_refs
         objects = generate_pack_contents(have, want)
-        if bool(objects):
-            write_pack(req_proto.write_file(), objects)
+        pack_data_count, pack_data = pack_objects_to_data(objects)
+        if bool(pack_data_count):
+            write_pack_data(req_proto.write_file(), pack_data_count, pack_data)
         resp, read = self._smart_request("git-receive-pack", url,
                                          data=req_data.getvalue())
         try:

+ 14 - 4
dulwich/pack.py

@@ -1578,6 +1578,17 @@ def deltify_pack_objects(objects, window_size=None):
         while len(possible_bases) > window_size:
             possible_bases.pop()
 
+def pack_objects_to_data(objects):
+    """Create pack data from objects
+
+    :param objects: Pack objects
+    :return: Tuples with (type_num, hexdigest, delta base, object chunks)
+    """
+    count = len(objects)
+    return (count,
+            ((o.type_num, o.sha().digest(), None, o.as_raw_string())
+              for (o, path) in objects))
+
 
 def write_pack_objects(f, objects, delta_window_size=None, deltify=None):
     """Write a new pack data file.
@@ -1596,12 +1607,11 @@ def write_pack_objects(f, objects, delta_window_size=None, deltify=None):
         deltify = False
     if deltify:
         pack_contents = deltify_pack_objects(objects, delta_window_size)
+        pack_contents_count = len(objects)
     else:
-        pack_contents = (
-            (o.type_num, o.sha().digest(), None, o.as_raw_string())
-            for (o, path) in objects)
+        pack_contents_count, pack_contents = pack_objects_to_data(objects)
 
-    return write_pack_data(f, len(objects), pack_contents)
+    return write_pack_data(f, pack_contents_count, pack_contents)
 
 
 def write_pack_data(f, num_records, records):