Use dictionary in send_pack.
[jelmer/dulwich-libgit2.git] / dulwich / client.py
index 98d0075e24bfdf984f791189a5d99021b185321c..eff18b33b78d58843e20a88f429511c14a106be5 100644 (file)
@@ -1,5 +1,5 @@
-# server.py -- Implementation of the server side git protocols
-# Copyright (C) 2008 Jelmer Vernooij <jelmer@samba.org>
+# client.py -- Implementation of the server side git protocols
+# 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
@@ -72,8 +72,19 @@ class GitClient(object):
 
     """
 
-    def __init__(self, can_read, read, write, thin_packs=True):
-        self.proto = Protocol(read, write)
+    def __init__(self, can_read, read, write, thin_packs=True, 
+        report_activity=None):
+        """Create a new GitClient instance.
+
+        :param can_read: Function that returns True if there is data available
+            to be read.
+        :param read: Callback for reading data, takes number of bytes to read
+        :param write: Callback for writing data
+        :param thin_packs: Whether or not thin packs should be retrieved
+        :param report_activity: Optional callback for reporting transport
+            activity.
+        """
+        self.proto = Protocol(read, write, report_activity)
         self._can_read = can_read
         self._capabilities = list(CAPABILITIES)
         if thin_packs:
@@ -93,7 +104,7 @@ class GitClient(object):
             refs[ref] = sha
         return refs, server_capabilities
 
-    def send_pack(self, path, generate_pack_contents):
+    def send_pack(self, path, get_changed_refs, generate_pack_contents):
         """Upload a pack to a remote repository.
 
         :param path: Repository path
@@ -101,23 +112,39 @@ class GitClient(object):
             objects to upload.
         """
         refs, server_capabilities = self.read_refs()
-        changed_refs = [] # FIXME
+        changed_refs = get_changed_refs(refs)
         if not changed_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])
+        sent_capabilities = False
+        for changed_ref, (old_sha1, new_sha1) in changed_refs.iteritems():
+            if old_sha1 is None:
+                old_sha1 = "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)
         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):
+        shas = generate_pack_contents(want, have)
+            
+        (entries, sha) = write_pack_data(self.proto, shas, len(shas))
+        self.proto.write(sha)
+        
+        # read the final confirmation sha
+        sha = self.proto.read(20)
+        if sha:
+            pass # FIXME: Check that this sha is valid
+            
+        return changed_refs
+
+    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
@@ -162,26 +189,29 @@ class GitClient(object):
                 progress(pkt)
             else:
                 raise AssertionError("Invalid sideband channel %d" % channel)
+        return refs
 
 
 class TCPGitClient(GitClient):
     """A Git Client that works over TCP directly (i.e. git://)."""
 
-    def __init__(self, host, port=TCP_GIT_PORT, *args, **kwargs):
+    def __init__(self, host, port=None, *args, **kwargs):
         self._socket = socket.socket(type=socket.SOCK_STREAM)
+        if port is None:
+            port = TCP_GIT_PORT
         self._socket.connect((host, port))
         self.rfile = self._socket.makefile('rb', -1)
         self.wfile = self._socket.makefile('wb', 0)
         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.
@@ -194,7 +224,8 @@ 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):
@@ -216,14 +247,15 @@ 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):
         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):
         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):
@@ -265,18 +297,21 @@ get_ssh_vendor = SSHVendor
 
 class SSHGitClient(GitClient):
 
-    def __init__(self, host, port=None, thin_packs=True):
+    def __init__(self, host, port=None, *args, **kwargs):
         self.host = host
         self.port = port
-        self._thin_packs = thin_packs
+        self._args = args
+        self._kwargs = kwargs
 
-    def send_pack(self, path):
+    def send_pack(self, path, get_changed_refs, generate_pack_contents):
         remote = get_ssh_vendor().connect_ssh(self.host, ["git-receive-pack %s" % path], port=self.port)
-        client = GitClient(lambda: _fileno_can_read(remote.proc.stdout.fileno()), remote.recv, remote.send, thin_packs=self._thin_packs)
-        client.send_pack(path)
+        client = GitClient(lambda: _fileno_can_read(remote.proc.stdout.fileno()), remote.recv, remote.send, *self._args, **self._kwargs)
+        return client.send_pack(path, get_changed_refs, generate_pack_contents)
 
-    def fetch_pack(self, path, determine_wants, graph_walker, pack_data, progress):
+    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)
-        client = GitClient(lambda: _fileno_can_read(remote.proc.stdout.fileno()), remote.recv, remote.send, thin_packs=self._thin_packs)
-        client.fetch_pack(path, determine_wants, graph_walker, pack_data, progress)
+        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)