Refactor server capability code into base Handler.
[jelmer/dulwich-libgit2.git] / dulwich / client.py
index 1b92ad00a9c9cef16ccb97b5a050bc6f3186c008..3dfa5eda95d3f5e8d102330f7c34afa52d7ae7b2 100644 (file)
@@ -44,38 +44,6 @@ def _fileno_can_read(fileno):
     return len(select.select([fileno], [], [], 0)[0]) > 0
 
 
-class SimpleFetchGraphWalker(object):
-    """Graph walker that finds out what commits are missing."""
-
-    def __init__(self, local_heads, get_parents):
-        """Create a new SimpleFetchGraphWalker instance.
-
-        :param local_heads: SHA1s that should be retrieved
-        :param get_parents: Function for finding the parents of a SHA1.
-        """
-        self.heads = set(local_heads)
-        self.get_parents = get_parents
-        self.parents = {}
-
-    def ack(self, sha):
-        """Ack that a particular revision and its ancestors are present in the target."""
-        if sha in self.heads:
-            self.heads.remove(sha)
-        if sha in self.parents:
-            for p in self.parents[sha]:
-                self.ack(p)
-
-    def next(self):
-        """Iterate over revisions that might be missing in the target."""
-        if self.heads:
-            ret = self.heads.pop()
-            ps = self.get_parents(ret)
-            self.parents[ret] = ps
-            self.heads.update(ps)
-            return ret
-        return None
-
-
 CAPABILITIES = ["multi_ack", "side-band-64k", "ofs-delta"]
 
 
@@ -123,35 +91,57 @@ class GitClient(object):
         :param generate_pack_contents: Function that can return the shas of the 
             objects to upload.
         """
-        refs, server_capabilities = self.read_refs()
-        changed_refs = determine_wants(refs)
-        if not changed_refs:
+        old_refs, server_capabilities = self.read_refs()
+        new_refs = determine_wants(old_refs)
+        if not new_refs:
             self.proto.write_pkt_line(None)
             return {}
         want = []
-        have = []
+        have = [x for x in old_refs.values() if not x == "0" * 40]
         sent_capabilities = False
-        for changed_ref, new_sha1 in changed_refs.iteritems():
-            old_sha1 = refs.get(changed_ref, "0" * 40)
-            if sent_capabilities:
-                self.proto.write_pkt_line("%s %s %s" % (old_sha1, new_sha1, changed_ref))
-            else:
-                self.proto.write_pkt_line("%s %s %s\0%s" % (old_sha1, new_sha1, changed_ref, self.capabilities()))
-                sent_capabilities = True
-            want.append(new_sha1)
-            if old_sha1 != "0"*40:
-                have.append(old_sha1)
+        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 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)
-        objects = generate_pack_contents(want, have)
-        (entries, sha) = write_pack_data(self.proto.write_file(), objects, len(objects))
-        self.proto.write(sha)
+        if not want:
+            return new_refs
+        objects = generate_pack_contents(have, want)
+        (entries, sha) = write_pack_data(self.proto.write_file(), objects, 
+                                         len(objects))
         
         # read the final confirmation sha
         client_sha = self.proto.read(20)
-        if not client_sha in (None, sha)
+        if not client_sha in (None, "", sha):
             raise ChecksumMismatch(sha, client_sha)
             
-        return changed_refs
+        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):
@@ -167,6 +157,7 @@ class GitClient(object):
         if not wants:
             self.proto.write_pkt_line(None)
             return refs
+        assert isinstance(wants, list) and type(wants[0]) == str
         self.proto.write_pkt_line("want %s %s\n" % (wants[0], self.capabilities()))
         for want in wants[1:]:
             self.proto.write_pkt_line("want %s\n" % want)
@@ -324,20 +315,21 @@ get_ssh_vendor = SSHVendor
 
 class SSHGitClient(GitClient):
 
-    def __init__(self, host, port=None, *args, **kwargs):
+    def __init__(self, host, port=None, username=None, *args, **kwargs):
         self.host = host
         self.port = port
+        self.username = username
         self._args = args
         self._kwargs = kwargs
 
     def send_pack(self, path, determine_wants, generate_pack_contents):
-        remote = get_ssh_vendor().connect_ssh(self.host, ["git-receive-pack %s" % path], port=self.port)
+        remote = get_ssh_vendor().connect_ssh(self.host, ["git-receive-pack '%s'" % path], port=self.port, username=self.username)
         client = GitClient(lambda: _fileno_can_read(remote.proc.stdout.fileno()), remote.recv, remote.send, *self._args, **self._kwargs)
         return client.send_pack(path, determine_wants, generate_pack_contents)
 
     def fetch_pack(self, path, determine_wants, graph_walker, pack_data,
         progress):
-        remote = get_ssh_vendor().connect_ssh(self.host, ["git-upload-pack %s" % path], port=self.port)
+        remote = get_ssh_vendor().connect_ssh(self.host, ["git-upload-pack '%s'" % path], port=self.port, username=self.username)
         client = GitClient(lambda: _fileno_can_read(remote.proc.stdout.fileno()), remote.recv, remote.send, *self._args, **self._kwargs)
         return client.fetch_pack(path, determine_wants, graph_walker, pack_data,
                                  progress)