Refactor server capability code into base Handler.
[jelmer/dulwich-libgit2.git] / dulwich / client.py
index 7109f1c3630b89b5c5740a5dd4d1efe37e52244a..3dfa5eda95d3f5e8d102330f7c34afa52d7ae7b2 100644 (file)
@@ -102,14 +102,17 @@ class GitClient(object):
         for refname in set(new_refs.keys() + old_refs.keys()):
             old_sha1 = old_refs.get(refname, "0" * 40)
             new_sha1 = new_refs.get(refname, "0" * 40)
-            if sent_capabilities:
-                self.proto.write_pkt_line("%s %s %s" % (old_sha1, new_sha1, refname))
-            else:
-                self.proto.write_pkt_line("%s %s %s\0%s" % (old_sha1, new_sha1, refname, self.capabilities()))
-                sent_capabilities = True
+            if old_sha1 != new_sha1:
+                if sent_capabilities:
+                    self.proto.write_pkt_line("%s %s %s" % (old_sha1, new_sha1, refname))
+                else:
+                    self.proto.write_pkt_line("%s %s %s\0%s" % (old_sha1, new_sha1, refname, self.capabilities()))
+                    sent_capabilities = True
             if not new_sha1 in (have, "0" * 40):
                 want.append(new_sha1)
         self.proto.write_pkt_line(None)
+        if not want:
+            return new_refs
         objects = generate_pack_contents(have, want)
         (entries, sha) = write_pack_data(self.proto.write_file(), objects, 
                                          len(objects))
@@ -121,6 +124,25 @@ class GitClient(object):
             
         return new_refs
 
+    def fetch(self, path, target, determine_wants=None, progress=None):
+        """Fetch into a target repository.
+
+        :param path: Path to fetch from
+        :param target: Target repository to fetch into
+        :param determine_wants: Optional function to determine what refs 
+            to fetch
+        :param progress: Optional progress function
+        :return: remote refs
+        """
+        if determine_wants is None:
+            determine_wants = target.object_store.determine_wants_all
+        f, commit = target.object_store.add_pack()
+        try:
+            return self.fetch_pack(path, determine_wants, target.graph_walker, 
+                                   f.write, progress)
+        finally:
+            commit()
+
     def fetch_pack(self, path, determine_wants, graph_walker, pack_data,
                    progress):
         """Retrieve a pack from a git smart server.