Explorar o código

Share the code to work out which objects to send

John Carr %!s(int64=16) %!d(string=hai) anos
pai
achega
9c3b8f0866
Modificáronse 2 ficheiros con 46 adicións e 37 borrados
  1. 44 0
      dulwich/pack.py
  2. 2 37
      dulwich/server.py

+ 44 - 0
dulwich/pack.py

@@ -790,3 +790,47 @@ def load_packs(path):
     for name in os.listdir(path):
         if name.startswith("pack-") and name.endswith(".pack"):
             yield Pack(os.path.join(path, name[:-len(".pack")]))
+
+
+def generate_pack_contents(want, have, get_object):
+    """
+    Given a list of desired commits and a list of restraining commits,
+    work out what objects to send
+
+    :param want: list of sha's we want to send
+    :param have: list of sha's recepient has (or when to stop for a shallow pack)
+    :param get_object: callback to get a commit or tree object
+    :return: a list of commits
+    """    
+    sha_queue = []
+    commits_to_send = want[:]
+    for sha in commits_to_send:
+        if sha in sha_queue:
+            continue
+
+        sha_queue.append(sha)
+
+        c = get_object(sha)
+        for p in c.parents:
+            if not p in commits_to_send:
+                commits_to_send.append(p)
+
+        def parse_tree(tree, sha_queue):
+            for mode, name, x in tree.entries():
+                if not x in sha_queue:
+                    sha_queue.append(x)
+                    t = get_object(x)
+                    if t._type == 'tree':
+                        parse_tree(t, sha_queue)
+
+        treesha = c.tree
+        if treesha not in sha_queue:
+            sha_queue.append(treesha)
+            t = get_object(treesha)
+            parse_tree(t, sha_queue)
+
+        #progress("counting objects: %d\r" % len(sha_queue))
+
+    #progress("counting objects: %d, done.\n" % len(sha_queue))
+
+    return sha_queue

+ 2 - 37
dulwich/server.py

@@ -19,7 +19,7 @@
 import SocketServer
 from dulwich.protocol import Protocol, ProtocolFile, TCP_GIT_PORT, extract_capabilities
 from dulwich.repo import Repo
-from dulwich.pack import PackData, Pack, write_pack_data
+from dulwich.pack import PackData, Pack, write_pack_data, generate_pack_contents
 import os, sha, tempfile
 
 class Backend(object):
@@ -105,43 +105,8 @@ class GitBackend(Backend):
 
     def generate_pack(self, want, have, write, progress):
         progress("dul-daemon says what\n")
-
-        sha_queue = []
-
-        commits_to_send = want[:]
-        for sha in commits_to_send:
-            if sha in sha_queue:
-                continue
-
-            sha_queue.append(sha)
-
-            c = self.repo.commit(sha)
-            for p in c.parents:
-                if not p in commits_to_send:
-                    commits_to_send.append(p)
-
-            def parse_tree(tree, sha_queue):
-                for mode, name, x in tree.entries():
-                    if not x in sha_queue:
-                        try:
-                            t = self.repo.tree(x)
-                            sha_queue.append(x)
-                            parse_tree(t, sha_queue)
-                        except:
-                            sha_queue.append(x)
-
-            treesha = c.tree
-            if treesha not in sha_queue:
-                sha_queue.append(treesha)
-                t = self.repo.tree(treesha)
-                parse_tree(t, sha_queue)
-
-            progress("counting objects: %d\r" % len(sha_queue))
-
-        progress("counting objects: %d, done.\n" % len(sha_queue))
-
+        sha_queue = generate_pack_contents(want, have, self.repo.get_object) 
         write_pack_data(ProtocolFile(None, write), (self.repo.get_object(sha) for sha in sha_queue), len(sha_queue))
-
         progress("how was that, then?\n")