Rev 4045: Create Branch.create_clone_on_transport helper method to combine bzr and branch creation for push. in http://people.ubuntu.com/~robertc/baz2.0/push.roundtrips

Robert Collins robertc at robertcollins.net
Wed Feb 25 03:22:15 GMT 2009


At http://people.ubuntu.com/~robertc/baz2.0/push.roundtrips

------------------------------------------------------------
revno: 4045
revision-id: robertc at robertcollins.net-20090225032212-odesl979bcnjrk3f
parent: pqm at pqm.ubuntu.com-20090225011157-xn0w5zux5mrges6j
committer: Robert Collins <robertc at robertcollins.net>
branch nick: push.roundtrips
timestamp: Wed 2009-02-25 14:22:12 +1100
message:
  Create Branch.create_clone_on_transport helper method to combine bzr and branch creation for push.
=== modified file 'NEWS'
--- a/NEWS	2009-02-24 10:31:49 +0000
+++ b/NEWS	2009-02-25 03:22:12 +0000
@@ -99,6 +99,9 @@
     * ``bzrlib.tests.run_suite`` accepts a runner_class parameter
       supporting the use of different runners. (Robert Collins)
 
+    * New branch method ``create_clone_on_transport`` that returns a
+      branch object. (Robert Collins)
+
     * New hook Commands['extend_command'] to allow plugins to access a
       command object before the command is run (or help generated from
       it), without overriding the command. (Robert Collins)

=== modified file 'bzrlib/branch.py'
--- a/bzrlib/branch.py	2009-02-24 08:47:58 +0000
+++ b/bzrlib/branch.py	2009-02-25 03:22:12 +0000
@@ -916,6 +916,9 @@
     def clone(self, to_bzrdir, revision_id=None):
         """Clone this branch into to_bzrdir preserving all semantic values.
 
+        Most API users will want 'create_clone_on_transport', which creates a
+        new bzrdir and branch on the fly.
+
         revision_id: if not None, the revision history in the new branch will
                      be truncated to end with revision_id.
         """
@@ -1036,6 +1039,19 @@
             format.set_branch_format(self._format)
         return format
 
+    def create_clone_on_transport(self, to_transport, revision_id=None,
+        stacked_on=None):
+        """Create a clone of this branch and its bzrdir.
+
+        :param to_transport: The transport to clone onto.
+        :param revision_id: The revision id to use as tip in the new branch.
+            If None the tip is obtained from this branch.
+        :param stacked_on: An optional URL to stack the clone on.
+        """
+        dir_to = self.bzrdir.clone_on_transport(to_transport,
+            revision_id=revision_id, stacked_on=stacked_on)
+        return dir_to.open_branch()
+
     def create_checkout(self, to_location, revision_id=None,
                         lightweight=False, accelerator_tree=None,
                         hardlink=False):

=== modified file 'bzrlib/push.py'
--- a/bzrlib/push.py	2009-02-24 08:09:17 +0000
+++ b/bzrlib/push.py	2009-02-25 03:22:12 +0000
@@ -101,11 +101,8 @@
         # Now the target directory exists, but doesn't have a .bzr
         # directory. So we need to create it, along with any work to create
         # all of the dependent branches, etc.
-        dir_to = br_from.bzrdir.clone_on_transport(to_transport,
+        br_to = br_from.create_clone_on_transport(to_transport,
             revision_id=revision_id, stacked_on=stacked_on)
-        # XXX: Fix this API to allow getting the branch back from the clone
-        # call. Or something. 20090224 RBC/spiv.
-        br_to = dir_to.open_branch()
         # TODO: Some more useful message about what was copied
         try:
             finally_stacked_on = br_to.get_stacked_on_url()

=== modified file 'bzrlib/tests/branch_implementations/__init__.py'
--- a/bzrlib/tests/branch_implementations/__init__.py	2009-02-23 15:29:35 +0000
+++ b/bzrlib/tests/branch_implementations/__init__.py	2009-02-25 03:22:12 +0000
@@ -156,6 +156,7 @@
         'bzrlib.tests.branch_implementations.test_break_lock',
         'bzrlib.tests.branch_implementations.test_check',
         'bzrlib.tests.branch_implementations.test_create_checkout',
+        'bzrlib.tests.branch_implementations.test_create_clone',
         'bzrlib.tests.branch_implementations.test_commit',
         'bzrlib.tests.branch_implementations.test_dotted_revno_to_revision_id',
         'bzrlib.tests.branch_implementations.test_get_revision_id_to_revno_map',

=== added file 'bzrlib/tests/branch_implementations/test_create_clone.py'
--- a/bzrlib/tests/branch_implementations/test_create_clone.py	1970-01-01 00:00:00 +0000
+++ b/bzrlib/tests/branch_implementations/test_create_clone.py	2009-02-25 03:22:12 +0000
@@ -0,0 +1,56 @@
+# Copyright (C) 2009 Canonical Ltd
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+"""Tests for branch.create_clone behaviour."""
+
+from bzrlib.tests.branch_implementations.test_branch import TestCaseWithBranch
+
+
+class TestCreateClone(TestCaseWithBranch):
+
+    def test_create_clone_on_transport_no_revision_id(self):
+        tree = self.make_branch_and_tree('source')
+        tree.commit('a commit')
+        source = tree.branch
+        target_transport = self.get_transport('target')
+        result = tree.branch.create_clone_on_transport(target_transport)
+        self.assertEqual(source.last_revision(), result.last_revision())
+
+    def test_create_clone_on_transport_revision_id(self):
+        tree = self.make_branch_and_tree('source')
+        old_revid = tree.commit('a commit')
+        source_tip = tree.commit('a second commit')
+        source = tree.branch
+        target_transport = self.get_transport('target')
+        result = tree.branch.create_clone_on_transport(target_transport,
+            revision_id=old_revid)
+        self.assertEqual(old_revid, result.last_revision())
+        result.lock_read()
+        self.addCleanup(result.unlock)
+        self.assertFalse(result.repository.has_revision(source_tip))
+
+    def test_create_clone_on_transport_stacked(self):
+        tree = self.make_branch_and_tree('source')
+        tree.commit('a commit')
+        trunk = tree.branch.create_clone_on_transport(
+            self.get_transport('trunk'))
+        revid = tree.commit('a second commit')
+        source = tree.branch
+        target_transport = self.get_transport('target')
+        result = tree.branch.create_clone_on_transport(target_transport,
+            stacked_on=trunk.base)
+        self.assertEqual(revid, result.last_revision())
+        self.assertEqual(trunk.base, result.get_stacked_on_url())




More information about the bazaar-commits mailing list