Rev 4033: Add a BranchFormat.network_name() method as preparation for creating branches via RPC calls. in http://people.ubuntu.com/~robertc/baz2.0/push.roundtrips

Robert Collins robertc at robertcollins.net
Mon Feb 23 05:12:09 GMT 2009


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

------------------------------------------------------------
revno: 4033
revision-id: robertc at robertcollins.net-20090223051205-92ypm6chik138tpy
parent: pqm at pqm.ubuntu.com-20090223012623-0epa5dpnb7sk0tef
committer: Robert Collins <robertc at robertcollins.net>
branch nick: push.roundtrips
timestamp: Mon 2009-02-23 16:12:05 +1100
message:
  Add a BranchFormat.network_name() method as preparation for creating branches via RPC calls.
=== modified file 'bzrlib/branch.py'
--- a/bzrlib/branch.py	2009-02-13 00:52:18 +0000
+++ b/bzrlib/branch.py	2009-02-23 05:12:05 +0000
@@ -45,6 +45,7 @@
 
 from bzrlib.decorators import needs_read_lock, needs_write_lock
 from bzrlib.hooks import Hooks
+from bzrlib import registry
 from bzrlib.symbol_versioning import (
     deprecated_in,
     deprecated_method,
@@ -1258,6 +1259,16 @@
         """
         return True
 
+    def network_name(self):
+        """A simple byte string uniquely identifying this format for RPC calls.
+
+        MetaDir branch formats use their disk format string to identify the
+        repository over the wire. All in one formats such as bzr < 0.8, and
+        foreign formats like svn/git and hg should use some marker which is
+        unique and immutable.
+        """
+        raise NotImplementedError(self.network_name)
+
     def open(self, a_bzrdir, _found=False):
         """Return the branch object for a_bzrdir
 
@@ -1268,7 +1279,10 @@
 
     @classmethod
     def register_format(klass, format):
+        """Register a metadir format."""
         klass._formats[format.get_format_string()] = format
+        # Metadir formats have a network name of their format string.
+        network_format_registry.register(format.get_format_string(), format)
 
     @classmethod
     def set_default_format(klass, format):
@@ -1441,6 +1455,10 @@
         super(BzrBranchFormat4, self).__init__()
         self._matchingbzrdir = bzrdir.BzrDirFormat6()
 
+    def network_name(self):
+        """The network name for this format is the control dirs disk label."""
+        return self._matchingbzrdir.get_format_string()
+
     def open(self, a_bzrdir, _found=False):
         """Return the branch object for a_bzrdir
 
@@ -1466,6 +1484,13 @@
         """What class to instantiate on open calls."""
         raise NotImplementedError(self._branch_class)
 
+    def network_name(self):
+        """A simple byte string uniquely identifying this format for RPC calls.
+
+        Metadir branch formats use their format string.
+        """
+        return self.get_format_string()
+
     def open(self, a_bzrdir, _found=False):
         """Return the branch object for a_bzrdir.
 
@@ -1688,6 +1713,15 @@
         return result
 
 
+network_format_registry = registry.FormatRegistry()
+"""Registry of formats indexed by their network name.
+
+The network name for a repository format is an identifier that can be used when
+referring to formats with smart server operations. See
+BranchFormat.network_name() for more detail.
+"""
+
+
 # formats which have no format string are not discoverable
 # and not independently creatable, so are not registered.
 __format5 = BzrBranchFormat5()
@@ -1699,7 +1733,10 @@
 BranchFormat.register_format(__format7)
 BranchFormat.set_default_format(__format6)
 _legacy_formats = [BzrBranchFormat4(),
-                   ]
+    ]
+network_format_registry.register(
+    _legacy_formats[0].network_name(), _legacy_formats[0])
+
 
 class BzrBranch(Branch):
     """A branch stored in the actual filesystem.

=== modified file 'bzrlib/registry.py'
--- a/bzrlib/registry.py	2009-02-12 13:49:21 +0000
+++ b/bzrlib/registry.py	2009-02-23 05:12:05 +0000
@@ -231,3 +231,30 @@
     default_key = property(_get_default_key, _set_default_key,
                             doc="Current value of the default key."
                                 " Can be set to any existing key.")
+
+
+class FormatRegistry(Registry):
+    """Registry specialised for handling formats."""
+
+    def __init__(self, other_registry=None):
+        Registry.__init__(self)
+        self._other_registry = other_registry
+
+    def register_lazy(self, key, module_name, member_name,
+                      help=None, info=None,
+                      override_existing=False):
+        # Overridden to allow capturing registrations to two seperate
+        # registries in a single call.
+        Registry.register_lazy(self, key, module_name, member_name,
+                help=help, info=info, override_existing=override_existing)
+        if self._other_registry is not None:
+            self._other_registry.register_lazy(key, module_name, member_name,
+                help=help, info=info, override_existing=override_existing)
+
+    def get(self, format_string):
+        r = Registry.get(self, format_string)
+        if callable(r):
+            r = r()
+        return r
+    
+

=== modified file 'bzrlib/remote.py'
--- a/bzrlib/remote.py	2009-02-22 23:58:54 +0000
+++ b/bzrlib/remote.py	2009-02-23 05:12:05 +0000
@@ -1467,6 +1467,9 @@
     def get_format_string(self):
         return 'Remote BZR Branch'
 
+    def network_name(self):
+        return self._network_name
+
     def open(self, a_bzrdir):
         return a_bzrdir.open_branch()
 
@@ -1539,6 +1542,14 @@
         self._repo_lock_token = None
         self._lock_count = 0
         self._leave_lock = False
+        if real_branch is not None:
+            self._format._network_name = \
+                self._real_branch._format.network_name()
+        else:
+            # XXX: Need to get this from BzrDir.open_branch's return value.
+            self._ensure_real()
+            self._format._network_name = \
+                self._real_branch._format.network_name()
         # The base class init is not called, so we duplicate this:
         hooks = branch.Branch.hooks['open']
         for hook in hooks:

=== modified file 'bzrlib/repository.py'
--- a/bzrlib/repository.py	2009-02-22 23:58:54 +0000
+++ b/bzrlib/repository.py	2009-02-23 05:12:05 +0000
@@ -47,10 +47,10 @@
 from bzrlib.testament import Testament
 """)
 
-from bzrlib import registry
 from bzrlib.decorators import needs_read_lock, needs_write_lock
 from bzrlib.inter import InterObject
 from bzrlib.inventory import Inventory, InventoryDirectory, ROOT_ID
+from bzrlib import registry
 from bzrlib.symbol_versioning import (
         deprecated_method,
         one_one,
@@ -2207,32 +2207,7 @@
             control_files)
 
 
-class RepositoryFormatRegistry(registry.Registry):
-    """Registry of RepositoryFormats."""
-
-    def __init__(self, other_registry=None):
-        registry.Registry.__init__(self)
-        self._other_registry = other_registry
-
-    def register_lazy(self, key, module_name, member_name,
-                      help=None, info=None,
-                      override_existing=False):
-        # Overridden to allow capturing registrations to two seperate
-        # registries in a single call.
-        registry.Registry.register_lazy(self, key, module_name, member_name,
-                help=help, info=info, override_existing=override_existing)
-        if self._other_registry is not None:
-            self._other_registry.register_lazy(key, module_name, member_name,
-                help=help, info=info, override_existing=override_existing)
-
-    def get(self, format_string):
-        r = registry.Registry.get(self, format_string)
-        if callable(r):
-            r = r()
-        return r
-    
-
-network_format_registry = RepositoryFormatRegistry()
+network_format_registry = registry.FormatRegistry()
 """Registry of formats indexed by their network name.
 
 The network name for a repository format is an identifier that can be used when
@@ -2241,7 +2216,7 @@
 """
 
 
-format_registry = RepositoryFormatRegistry(network_format_registry)
+format_registry = registry.FormatRegistry(network_format_registry)
 """Registry of formats, indexed by their BzrDirMetaFormat format string.
 
 This can contain either format instances themselves, or classes/factories that

=== modified file 'bzrlib/tests/blackbox/test_push.py'
--- a/bzrlib/tests/blackbox/test_push.py	2009-02-22 23:58:54 +0000
+++ b/bzrlib/tests/blackbox/test_push.py	2009-02-23 05:12:05 +0000
@@ -202,7 +202,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.assertEqual(60, rpc_count)
+        self.assertEqual(64, rpc_count)
 
     def test_push_smart_stacked_streaming_acceptance(self):
         self.setup_smart_server_with_call_log()
@@ -219,7 +219,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.assertEqual(85, rpc_count)
+        self.assertEqual(88, rpc_count)
         remote = Branch.open('public')
         self.assertEndsWith(remote.get_stacked_on_url(), '/parent')
 

=== modified file 'bzrlib/tests/branch_implementations/test_branch.py'
--- a/bzrlib/tests/branch_implementations/test_branch.py	2009-02-13 00:52:18 +0000
+++ b/bzrlib/tests/branch_implementations/test_branch.py	2009-02-23 05:12:05 +0000
@@ -493,6 +493,28 @@
         self.assertEquals(br.revision_history(), [])
 
 
+class TestBranchFormat(TestCaseWithBranch):
+
+    def test_branch_format_network_name(self):
+        br = self.make_branch('.')
+        format = br._format
+        network_name = format.network_name()
+        self.assertIsInstance(network_name, str)
+        # We want to test that the network_name matches the actual format on
+        # disk. For local branches that means that using network_name as a key
+        # in the registry gives back the same format. For remote branches we
+        # check that the network_name of the RemoteBranchFormat we have locally
+        # matches the actual format present on disk.
+        if isinstance(format, remote.RemoteBranchFormat):
+            br._ensure_real()
+            real_branch = br._real_branch
+            self.assertEqual(real_branch._format.network_name(), network_name)
+        else:
+            registry = branch.network_format_registry
+            looked_up_format = registry.get(network_name)
+            self.assertEqual(format.__class__, looked_up_format.__class__)
+
+
 class ChrootedTests(TestCaseWithBranch):
     """A support class that provides readonly urls outside the local namespace.
 




More information about the bazaar-commits mailing list