Refactor server capability code into base Handler.
[jelmer/dulwich-libgit2.git] / dulwich / client.py
index 91d6e8fff51f559a9e7c5414294cd21782240a78..3dfa5eda95d3f5e8d102330f7c34afa52d7ae7b2 100644 (file)
@@ -1,5 +1,5 @@
 # client.py -- Implementation of the server side git protocols
-# Copyright (C) 2008 Jelmer Vernooij <jelmer@samba.org>
+# Copyright (C) 2008-2009 Jelmer Vernooij <jelmer@samba.org>
 # Copyright (C) 2008 John Carr
 #
 # This program is free software; you can redistribute it and/or
@@ -26,6 +26,9 @@ import select
 import socket
 import subprocess
 
+from dulwich.errors import (
+    ChecksumMismatch,
+    )
 from dulwich.protocol import (
     Protocol,
     TCP_GIT_PORT,
@@ -37,33 +40,10 @@ from dulwich.pack import (
 
 
 def _fileno_can_read(fileno):
+    """Check if a file descriptor is readable."""
     return len(select.select([fileno], [], [], 0)[0]) > 0
 
 
-class SimpleFetchGraphWalker(object):
-
-    def __init__(self, local_heads, get_parents):
-        self.heads = set(local_heads)
-        self.get_parents = get_parents
-        self.parents = {}
-
-    def ack(self, ref):
-        if ref in self.heads:
-            self.heads.remove(ref)
-        if ref in self.parents:
-            for p in self.parents[ref]:
-                self.ack(p)
-
-    def next(self):
-        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"]
 
 
@@ -104,31 +84,67 @@ class GitClient(object):
             refs[ref] = sha
         return refs, server_capabilities
 
-    def send_pack(self, path, generate_pack_contents):
+    def send_pack(self, path, determine_wants, generate_pack_contents):
         """Upload a pack to a remote repository.
 
         :param path: Repository path
         :param generate_pack_contents: Function that can return the shas of the 
             objects to upload.
         """
-        refs, server_capabilities = self.read_refs()
-        changed_refs = [] # FIXME
-        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
-        self.proto.write_pkt_line("%s %s %s\0%s" % (changed_refs[0][0], changed_refs[0][1], changed_refs[0][2], self.capabilities()))
+            return {}
         want = []
-        have = []
-        for changed_ref in changed_refs[:]:
-            self.proto.write_pkt_line("%s %s %s" % changed_refs)
-            want.append(changed_refs[1])
-            if changed_refs[0] != "0"*40:
-                have.append(changed_refs[0])
+        have = [x for x in old_refs.values() if not x == "0" * 40]
+        sent_capabilities = False
+        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)
-        shas = generate_pack_contents(want, have, None)
-        write_pack_data(self.write, shas, len(shas))
-
-    def fetch_pack(self, path, determine_wants, graph_walker, pack_data, progress):
+        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):
+            raise ChecksumMismatch(sha, client_sha)
+            
+        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.
 
         :param determine_wants: Callback that returns list of commits to fetch
@@ -140,7 +156,8 @@ class GitClient(object):
         wants = determine_wants(refs)
         if not wants:
             self.proto.write_pkt_line(None)
-            return
+            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)
@@ -173,6 +190,7 @@ class GitClient(object):
                 progress(pkt)
             else:
                 raise AssertionError("Invalid sideband channel %d" % channel)
+        return refs
 
 
 class TCPGitClient(GitClient):
@@ -188,13 +206,13 @@ class TCPGitClient(GitClient):
         self.host = host
         super(TCPGitClient, self).__init__(lambda: _fileno_can_read(self._socket.fileno()), self.rfile.read, self.wfile.write, *args, **kwargs)
 
-    def send_pack(self, path):
+    def send_pack(self, path, changed_refs, generate_pack_contents):
         """Send a pack to a remote host.
 
         :param path: Path of the repository on the remote host
         """
         self.proto.send_cmd("git-receive-pack", path, "host=%s" % self.host)
-        super(TCPGitClient, self).send_pack(path)
+        return super(TCPGitClient, self).send_pack(path, changed_refs, generate_pack_contents)
 
     def fetch_pack(self, path, determine_wants, graph_walker, pack_data, progress):
         """Fetch a pack from the remote host.
@@ -207,17 +225,19 @@ class TCPGitClient(GitClient):
         :param progress: Callback for writing progress
         """
         self.proto.send_cmd("git-upload-pack", path, "host=%s" % self.host)
-        super(TCPGitClient, self).fetch_pack(path, determine_wants, graph_walker, pack_data, progress)
+        return super(TCPGitClient, self).fetch_pack(path, determine_wants,
+            graph_walker, pack_data, progress)
 
 
 class SubprocessGitClient(GitClient):
+    """Git client that talks to a server using a subprocess."""
 
     def __init__(self, *args, **kwargs):
         self.proc = None
         self._args = args
         self._kwargs = kwargs
 
-    def _connect(self, service, *args):
+    def _connect(self, service, *args, **kwargs):
         argv = [service] + list(args)
         self.proc = subprocess.Popen(argv, bufsize=0,
                                 stdin=subprocess.PIPE,
@@ -229,14 +249,31 @@ class SubprocessGitClient(GitClient):
             self.proc.stdin.flush()
         return GitClient(lambda: _fileno_can_read(self.proc.stdout.fileno()), read_fn, write_fn, *args, **kwargs)
 
-    def send_pack(self, path):
+    def send_pack(self, path, changed_refs, generate_pack_contents):
+        """Upload a pack to the server.
+
+        :param path: Path to the git repository on the server
+        :param changed_refs: Dictionary with new values for the refs
+        :param generate_pack_contents: Function that returns an iterator over 
+            objects to send
+        """
         client = self._connect("git-receive-pack", path)
-        client.send_pack(path)
+        return client.send_pack(path, changed_refs, generate_pack_contents)
 
     def fetch_pack(self, path, determine_wants, graph_walker, pack_data, 
         progress):
+        """Retrieve a pack from the server
+
+        :param path: Path to the git repository on the server
+        :param determine_wants: Function that receives existing refs 
+            on the server and returns a list of desired shas
+        :param graph_walker: GraphWalker instance
+        :param pack_data: Function that can write pack data
+        :param progress: Function that can write progress texts
+        """
         client = self._connect("git-upload-pack", path)
-        client.fetch_pack(path, determine_wants, graph_walker, pack_data, progress)
+        return client.fetch_pack(path, determine_wants, graph_walker, pack_data,
+                                 progress)
 
 
 class SSHSubprocess(object):
@@ -278,19 +315,22 @@ 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):
-        remote = get_ssh_vendor().connect_ssh(self.host, ["git-receive-pack %s" % path], port=self.port)
+    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, username=self.username)
         client = GitClient(lambda: _fileno_can_read(remote.proc.stdout.fileno()), remote.recv, remote.send, *self._args, **self._kwargs)
-        client.send_pack(path)
+        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)
+    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, username=self.username)
         client = GitClient(lambda: _fileno_can_read(remote.proc.stdout.fileno()), remote.recv, remote.send, *self._args, **self._kwargs)
-        client.fetch_pack(path, determine_wants, graph_walker, pack_data, progress)
+        return client.fetch_pack(path, determine_wants, graph_walker, pack_data,
+                                 progress)