Rev 4229: Lift Branch.set_stacked_on_url up from BzrBranch7. in http://people.ubuntu.com/~robertc/baz2.0/pending/Branch.set_stacked_on_url

Robert Collins robertc at robertcollins.net
Wed Apr 1 05:52:57 BST 2009


At http://people.ubuntu.com/~robertc/baz2.0/pending/Branch.set_stacked_on_url

------------------------------------------------------------
revno: 4229
revision-id: robertc at robertcollins.net-20090401045249-ojc0mpjalj9lrkb5
parent: robertc at robertcollins.net-20090401040529-vwb8tpu42jk2m0w6
committer: Robert Collins <robertc at robertcollins.net>
branch nick: Branch.set_stacked_on_url
timestamp: Wed 2009-04-01 15:52:49 +1100
message:
  Lift Branch.set_stacked_on_url up from BzrBranch7.
=== modified file 'NEWS'
--- a/NEWS	2009-03-31 08:50:05 +0000
+++ b/NEWS	2009-04-01 04:52:49 +0000
@@ -222,6 +222,11 @@
 Internals
 *********
 
+* ``Branch`` now implements ``set_stacked_on_url`` in the base class as
+  the implementation is generic and should impact foreign formats. This
+  helps performance for ``RemoteBranch`` push operations to new stacked
+  branches. (Robert Collins, Andrew Bennetts)
+
 * ``BtreeIndex._spill_mem_keys_to_disk()`` now generates disk index with
   optmizations turned off. This only has effect when processing > 100,000
   keys during something like ``bzr pack``. (John Arbash Meinel)

=== modified file 'bzrlib/branch.py'
--- a/bzrlib/branch.py	2009-03-25 04:20:12 +0000
+++ b/bzrlib/branch.py	2009-04-01 04:52:49 +0000
@@ -99,6 +99,11 @@
     def _open_hook(self):
         """Called by init to allow simpler extension of the base class."""
 
+    def _activate_fallback_location(self, url):
+        """Activate the branch/repository from url as a fallback repository."""
+        self.repository.add_fallback_repository(
+            self._get_fallback_repository(url))
+
     def break_lock(self):
         """Break a lock if one is present from another instance.
 
@@ -113,6 +118,11 @@
         if master is not None:
             master.break_lock()
 
+    def _check_stackable_repo(self):
+        if not self.repository._format.supports_external_lookups:
+            raise errors.UnstackableRepositoryFormat(self.repository._format,
+                self.repository.base)
+
     @staticmethod
     def open(base, _unsupported=False, possible_transports=None):
         """Open the branch rooted at base.
@@ -157,6 +167,13 @@
     def get_config(self):
         return BranchConfig(self)
 
+    def _get_fallback_repository(self, url):
+        """Get the repository we fallback to at url."""
+        url = urlutils.join(self.base, url)
+        a_bzrdir = bzrdir.BzrDir.open(url,
+                                      possible_transports=[self._transport])
+        return a_bzrdir.open_branch().repository
+
     def _get_tags_bytes(self):
         """Get the bytes of a serialised tags dict.
 
@@ -569,7 +586,33 @@
         :raises UnstackableRepositoryFormat: If the repository does not support
             stacking.
         """
-        raise NotImplementedError(self.set_stacked_on_url)
+        self._check_stackable_repo()
+        if not self._format.supports_stacking():
+            raise errors.UnstackableBranchFormat(self._format, self.base)
+        if not url:
+            try:
+                old_url = self.get_stacked_on_url()
+            except (errors.NotStacked, errors.UnstackableBranchFormat,
+                errors.UnstackableRepositoryFormat):
+                return
+            url = ''
+            # repositories don't offer an interface to remove fallback
+            # repositories today; take the conceptually simpler option and just
+            # reopen it.
+            self.repository = self.bzrdir.find_repository()
+            # for every revision reference the branch has, ensure it is pulled
+            # in.
+            source_repository = self._get_fallback_repository(old_url)
+            for revision_id in chain([self.last_revision()],
+                self.tags.get_reverse_tag_dict()):
+                self.repository.fetch(source_repository, revision_id,
+                    find_ghosts=True)
+        else:
+            self._activate_fallback_location(url)
+        # write this out after the repository is stacked to avoid setting a
+        # stacked config that doesn't work.
+        self._set_config_location('stacked_on_location', url)
+
 
     def _set_tags_bytes(self, bytes):
         """Mirror method for _get_tags_bytes.
@@ -2161,9 +2204,6 @@
             self._transport.put_bytes('parent', url + '\n',
                 mode=self.bzrdir._get_file_mode())
 
-    def set_stacked_on_url(self, url):
-        raise errors.UnstackableBranchFormat(self._format, self.base)
-
 
 class BzrBranch5(BzrBranch):
     """A format 5 branch. This supports new features over plain branches.
@@ -2295,18 +2335,6 @@
 class BzrBranch7(BzrBranch5):
     """A branch with support for a fallback repository."""
 
-    def _get_fallback_repository(self, url):
-        """Get the repository we fallback to at url."""
-        url = urlutils.join(self.base, url)
-        a_bzrdir = bzrdir.BzrDir.open(url,
-                                      possible_transports=[self._transport])
-        return a_bzrdir.open_branch().repository
-
-    def _activate_fallback_location(self, url):
-        """Activate the branch/repository from url as a fallback repository."""
-        self.repository.add_fallback_repository(
-            self._get_fallback_repository(url))
-
     def _open_hook(self):
         if self._ignore_fallbacks:
             return
@@ -2325,11 +2353,6 @@
                         "None, not a URL." % hook_name)
             self._activate_fallback_location(url)
 
-    def _check_stackable_repo(self):
-        if not self.repository._format.supports_external_lookups:
-            raise errors.UnstackableRepositoryFormat(self.repository._format,
-                self.repository.base)
-
     def __init__(self, *args, **kwargs):
         self._ignore_fallbacks = kwargs.get('ignore_fallbacks', False)
         super(BzrBranch7, self).__init__(*args, **kwargs)
@@ -2510,32 +2533,6 @@
         self.get_config().set_user_option('append_revisions_only', value,
             warn_masked=True)
 
-    def set_stacked_on_url(self, url):
-        self._check_stackable_repo()
-        if not url:
-            try:
-                old_url = self.get_stacked_on_url()
-            except (errors.NotStacked, errors.UnstackableBranchFormat,
-                errors.UnstackableRepositoryFormat):
-                return
-            url = ''
-            # repositories don't offer an interface to remove fallback
-            # repositories today; take the conceptually simpler option and just
-            # reopen it.
-            self.repository = self.bzrdir.find_repository()
-            # for every revision reference the branch has, ensure it is pulled
-            # in.
-            source_repository = self._get_fallback_repository(old_url)
-            for revision_id in chain([self.last_revision()],
-                self.tags.get_reverse_tag_dict()):
-                self.repository.fetch(source_repository, revision_id,
-                    find_ghosts=True)
-        else:
-            self._activate_fallback_location(url)
-        # write this out after the repository is stacked to avoid setting a
-        # stacked config that doesn't work.
-        self._set_config_location('stacked_on_location', url)
-
     def _get_append_revisions_only(self):
         value = self.get_config().get_user_option('append_revisions_only')
         return value == 'True'
@@ -2594,9 +2591,6 @@
     def get_stacked_on_url(self):
         raise errors.UnstackableBranchFormat(self._format, self.base)
 
-    def set_stacked_on_url(self, url):
-        raise errors.UnstackableBranchFormat(self._format, self.base)
-
 
 ######################################################################
 # results of operations

=== modified file 'bzrlib/remote.py'
--- a/bzrlib/remote.py	2009-04-01 03:53:19 +0000
+++ b/bzrlib/remote.py	2009-04-01 04:52:49 +0000
@@ -2288,17 +2288,6 @@
             self._ensure_real()
             return self._real_branch._set_parent_location(url)
 
-    def set_stacked_on_url(self, stacked_location):
-        """Set the URL this branch is stacked against.
-
-        :raises UnstackableBranchFormat: If the branch does not support
-            stacking.
-        :raises UnstackableRepositoryFormat: If the repository does not support
-            stacking.
-        """
-        self._ensure_real()
-        return self._real_branch.set_stacked_on_url(stacked_location)
-
     @needs_write_lock
     def pull(self, source, overwrite=False, stop_revision=None,
              **kwargs):

=== modified file 'bzrlib/smart/request.py'
--- a/bzrlib/smart/request.py	2009-03-26 06:59:15 +0000
+++ b/bzrlib/smart/request.py	2009-04-01 04:52:49 +0000
@@ -443,15 +443,20 @@
     'Branch.get_tags_bytes', 'bzrlib.smart.branch',
     'SmartServerBranchGetTagsBytes')
 request_handlers.register_lazy(
-    'Branch.get_stacked_on_url', 'bzrlib.smart.branch', 'SmartServerBranchRequestGetStackedOnURL')
-request_handlers.register_lazy(
-    'Branch.last_revision_info', 'bzrlib.smart.branch', 'SmartServerBranchRequestLastRevisionInfo')
-request_handlers.register_lazy(
-    'Branch.lock_write', 'bzrlib.smart.branch', 'SmartServerBranchRequestLockWrite')
-request_handlers.register_lazy(
-    'Branch.revision_history', 'bzrlib.smart.branch', 'SmartServerRequestRevisionHistory')
-request_handlers.register_lazy(
-    'Branch.set_last_revision', 'bzrlib.smart.branch', 'SmartServerBranchRequestSetLastRevision')
+    'Branch.get_stacked_on_url', 'bzrlib.smart.branch',
+    'SmartServerBranchRequestGetStackedOnURL')
+request_handlers.register_lazy(
+    'Branch.last_revision_info', 'bzrlib.smart.branch',
+    'SmartServerBranchRequestLastRevisionInfo')
+request_handlers.register_lazy(
+    'Branch.lock_write', 'bzrlib.smart.branch',
+    'SmartServerBranchRequestLockWrite')
+request_handlers.register_lazy(
+    'Branch.revision_history', 'bzrlib.smart.branch',
+    'SmartServerRequestRevisionHistory')
+request_handlers.register_lazy(
+    'Branch.set_last_revision', 'bzrlib.smart.branch',
+    'SmartServerBranchRequestSetLastRevision')
 request_handlers.register_lazy(
     'Branch.set_last_revision_info', 'bzrlib.smart.branch',
     'SmartServerBranchRequestSetLastRevisionInfo')
@@ -459,6 +464,9 @@
     'Branch.set_last_revision_ex', 'bzrlib.smart.branch',
     'SmartServerBranchRequestSetLastRevisionEx')
 request_handlers.register_lazy(
+    'Branch.set_stacked_on_url', 'bzrlib.smart.branch',
+    'SmartServerBranchRequestSetStackedOnURL')
+request_handlers.register_lazy(
     'Branch.unlock', 'bzrlib.smart.branch', 'SmartServerBranchRequestUnlock')
 request_handlers.register_lazy(
     'BzrDir.cloning_metadir', 'bzrlib.smart.bzrdir',

=== modified file 'bzrlib/tests/blackbox/test_push.py'
--- a/bzrlib/tests/blackbox/test_push.py	2009-04-01 03:53:19 +0000
+++ b/bzrlib/tests/blackbox/test_push.py	2009-04-01 04:52:49 +0000
@@ -217,7 +217,7 @@
         # being too low. If rpc_count increases, more network roundtrips have
         # become necessary for this use case. Please do not adjust this number
         # upwards without agreement from bzr's network support maintainers.
-        self.assertLength(52, self.hpss_calls)
+        self.assertLength(47, self.hpss_calls)
         remote = Branch.open('public')
         self.assertEndsWith(remote.get_stacked_on_url(), '/parent')
 

=== modified file 'bzrlib/tests/test_smart.py'
--- a/bzrlib/tests/test_smart.py	2009-03-24 23:19:12 +0000
+++ b/bzrlib/tests/test_smart.py	2009-04-01 04:52:49 +0000
@@ -1365,6 +1365,9 @@
             smart.request.request_handlers.get('Branch.get_parent'),
             smart.branch.SmartServerBranchGetParent)
         self.assertEqual(
+            smart.request.request_handlers.get('Branch.get_stacked_on_url'),
+            smart.branch.SmartServerBranchRequestGetStackedOnURL)
+        self.assertEqual(
             smart.request.request_handlers.get('Branch.get_tags_bytes'),
             smart.branch.SmartServerBranchGetTagsBytes)
         self.assertEqual(




More information about the bazaar-commits mailing list