/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/bzrdir.py

  • Committer: Jelmer Vernooij
  • Date: 2011-03-11 15:36:12 UTC
  • mfrom: (5712.4.8 bzrdir-weave)
  • mto: This revision was merged to the branch mainline in revision 5718.
  • Revision ID: jelmer@samba.org-20110311153612-7xniucwst6rrjshk
merge bzrdir-weave.

Show diffs side-by-side

added added

removed removed

Lines of Context:
55
55
    workingtree_4,
56
56
    )
57
57
from bzrlib.repofmt import pack_repo
58
 
from bzrlib.smart.client import _SmartClient
59
58
from bzrlib.transport import (
60
59
    do_catching_redirections,
61
60
    local,
1229
1228
        except KeyError:
1230
1229
            raise errors.UnknownFormatError(format=format_string, kind='bzrdir')
1231
1230
 
 
1231
    @classmethod
 
1232
    def known_formats(cls):
 
1233
        result = set()
 
1234
        for name, format in cls.formats.iteritems():
 
1235
            result.add(format)
 
1236
        return result
 
1237
 
1232
1238
 
1233
1239
controldir.ControlDirFormat.register_prober(BzrProber)
1234
1240
 
1258
1264
                    raise errors.NotBranchError(path=transport.base)
1259
1265
                if server_version != '2':
1260
1266
                    raise errors.NotBranchError(path=transport.base)
 
1267
            from bzrlib.remote import RemoteBzrDirFormat
1261
1268
            return RemoteBzrDirFormat()
1262
1269
 
 
1270
    @classmethod
 
1271
    def known_formats(cls):
 
1272
        from bzrlib.remote import RemoteBzrDirFormat
 
1273
        return set([RemoteBzrDirFormat()])
 
1274
 
1263
1275
 
1264
1276
class BzrDirFormat(controldir.ControlDirFormat):
1265
1277
    """ControlDirFormat base class for .bzr/ directories.
1278
1290
    # _lock_class must be set in subclasses to the lock type, typ.
1279
1291
    # TransportLock or LockDir
1280
1292
 
1281
 
    def get_format_string(self):
 
1293
    @classmethod
 
1294
    def get_format_string(cls):
1282
1295
        """Return the ASCII format string that identifies this format."""
1283
1296
        raise NotImplementedError(self.get_format_string)
1284
1297
 
1296
1309
            # metadir1
1297
1310
            if type(self) != BzrDirMetaFormat1:
1298
1311
                return self._initialize_on_transport_vfs(transport)
 
1312
            from bzrlib.remote import RemoteBzrDirFormat
1299
1313
            remote_format = RemoteBzrDirFormat()
1300
1314
            self._supply_sub_formats_to(remote_format)
1301
1315
            return remote_format.initialize_on_transport(transport)
1339
1353
            except errors.NoSmartMedium:
1340
1354
                pass
1341
1355
            else:
 
1356
                from bzrlib.remote import RemoteBzrDirFormat
1342
1357
                # TODO: lookup the local format from a server hint.
1343
1358
                remote_dir_format = RemoteBzrDirFormat()
1344
1359
                remote_dir_format._network_name = self.network_name()
1459
1474
        """
1460
1475
        raise NotImplementedError(self._open)
1461
1476
 
1462
 
    @classmethod
1463
 
    def register_format(klass, format):
1464
 
        """Register a new BzrDir format.
1465
 
        """
1466
 
        BzrProber.formats.register(format.get_format_string(), format)
1467
 
        controldir.ControlDirFormat.register_format(format)
1468
 
 
1469
 
    @classmethod
1470
 
    def register_lazy_format(klass, format_string, module_name, member_name):
1471
 
        """Lazily register a new BzrDir format.
1472
 
 
1473
 
        :param format_string: Format string
1474
 
        :param module_name: Name of module with the BzrDirFormat subclass
1475
 
        :param member_name: Class name of the BzrDirFormat
1476
 
        """
1477
 
        BzrProber.formats.register_lazy(format_string, module_name,
1478
 
            member_name)
1479
 
        controldir.ControlDirFormat.register_lazy_format(
1480
 
            module_name, member_name)
1481
 
 
1482
1477
    def _supply_sub_formats_to(self, other_format):
1483
1478
        """Give other_format the same values for sub formats as this has.
1484
1479
 
1491
1486
        :return: None.
1492
1487
        """
1493
1488
 
1494
 
    @classmethod
1495
 
    def unregister_format(klass, format):
1496
 
        BzrProber.formats.remove(format.get_format_string())
1497
 
        controldir.ControlDirFormat.unregister_format(format)
1498
 
 
1499
 
    @classmethod
1500
 
    def unregister_lazy_format(klass, format_string, module_name, member_name):
1501
 
        BzrProber.formats.remove(format_string)
1502
 
        controldir.ControlDirFormat.unregister_lazy_format(
1503
 
            module_name, member_name)
1504
 
 
1505
1489
 
1506
1490
class BzrDirMetaFormat1(BzrDirFormat):
1507
1491
    """Bzr meta control format 1
1646
1630
            raise NotImplementedError(self.get_converter)
1647
1631
        return ConvertMetaToMeta(format)
1648
1632
 
1649
 
    def get_format_string(self):
 
1633
    @classmethod
 
1634
    def get_format_string(cls):
1650
1635
        """See BzrDirFormat.get_format_string()."""
1651
1636
        return "Bazaar-NG meta directory, format 1\n"
1652
1637
 
1715
1700
 
1716
1701
# Register bzr formats
1717
1702
__default_format = BzrDirMetaFormat1()
1718
 
BzrDirFormat.register_format(__default_format)
 
1703
BzrProber.formats.register(__default_format.get_format_string(), __default_format)
1719
1704
controldir.ControlDirFormat._default_format = __default_format
1720
1705
 
1721
1706
 
1798
1783
        return to_convert
1799
1784
 
1800
1785
 
1801
 
# This is not in remote.py because it's relatively small, and needs to be
1802
 
# registered. Putting it in remote.py creates a circular import problem.
1803
 
# we can make it a lazy object if the control formats is turned into something
1804
 
# like a registry.
1805
 
class RemoteBzrDirFormat(BzrDirMetaFormat1):
1806
 
    """Format representing bzrdirs accessed via a smart server"""
1807
 
 
1808
 
    supports_workingtrees = False
1809
 
 
1810
 
    def __init__(self):
1811
 
        BzrDirMetaFormat1.__init__(self)
1812
 
        # XXX: It's a bit ugly that the network name is here, because we'd
1813
 
        # like to believe that format objects are stateless or at least
1814
 
        # immutable,  However, we do at least avoid mutating the name after
1815
 
        # it's returned.  See <https://bugs.launchpad.net/bzr/+bug/504102>
1816
 
        self._network_name = None
1817
 
 
1818
 
    def __repr__(self):
1819
 
        return "%s(_network_name=%r)" % (self.__class__.__name__,
1820
 
            self._network_name)
1821
 
 
1822
 
    def get_format_description(self):
1823
 
        if self._network_name:
1824
 
            real_format = controldir.network_format_registry.get(self._network_name)
1825
 
            return 'Remote: ' + real_format.get_format_description()
1826
 
        return 'bzr remote bzrdir'
1827
 
 
1828
 
    def get_format_string(self):
1829
 
        raise NotImplementedError(self.get_format_string)
1830
 
 
1831
 
    def network_name(self):
1832
 
        if self._network_name:
1833
 
            return self._network_name
1834
 
        else:
1835
 
            raise AssertionError("No network name set.")
1836
 
 
1837
 
    def initialize_on_transport(self, transport):
1838
 
        try:
1839
 
            # hand off the request to the smart server
1840
 
            client_medium = transport.get_smart_medium()
1841
 
        except errors.NoSmartMedium:
1842
 
            # TODO: lookup the local format from a server hint.
1843
 
            local_dir_format = BzrDirMetaFormat1()
1844
 
            return local_dir_format.initialize_on_transport(transport)
1845
 
        client = _SmartClient(client_medium)
1846
 
        path = client.remote_path_from_transport(transport)
1847
 
        try:
1848
 
            response = client.call('BzrDirFormat.initialize', path)
1849
 
        except errors.ErrorFromSmartServer, err:
1850
 
            remote._translate_error(err, path=path)
1851
 
        if response[0] != 'ok':
1852
 
            raise errors.SmartProtocolError('unexpected response code %s' % (response,))
1853
 
        format = RemoteBzrDirFormat()
1854
 
        self._supply_sub_formats_to(format)
1855
 
        return remote.RemoteBzrDir(transport, format)
1856
 
 
1857
 
    def parse_NoneTrueFalse(self, arg):
1858
 
        if not arg:
1859
 
            return None
1860
 
        if arg == 'False':
1861
 
            return False
1862
 
        if arg == 'True':
1863
 
            return True
1864
 
        raise AssertionError("invalid arg %r" % arg)
1865
 
 
1866
 
    def _serialize_NoneTrueFalse(self, arg):
1867
 
        if arg is False:
1868
 
            return 'False'
1869
 
        if arg:
1870
 
            return 'True'
1871
 
        return ''
1872
 
 
1873
 
    def _serialize_NoneString(self, arg):
1874
 
        return arg or ''
1875
 
 
1876
 
    def initialize_on_transport_ex(self, transport, use_existing_dir=False,
1877
 
        create_prefix=False, force_new_repo=False, stacked_on=None,
1878
 
        stack_on_pwd=None, repo_format_name=None, make_working_trees=None,
1879
 
        shared_repo=False):
1880
 
        try:
1881
 
            # hand off the request to the smart server
1882
 
            client_medium = transport.get_smart_medium()
1883
 
        except errors.NoSmartMedium:
1884
 
            do_vfs = True
1885
 
        else:
1886
 
            # Decline to open it if the server doesn't support our required
1887
 
            # version (3) so that the VFS-based transport will do it.
1888
 
            if client_medium.should_probe():
1889
 
                try:
1890
 
                    server_version = client_medium.protocol_version()
1891
 
                    if server_version != '2':
1892
 
                        do_vfs = True
1893
 
                    else:
1894
 
                        do_vfs = False
1895
 
                except errors.SmartProtocolError:
1896
 
                    # Apparently there's no usable smart server there, even though
1897
 
                    # the medium supports the smart protocol.
1898
 
                    do_vfs = True
1899
 
            else:
1900
 
                do_vfs = False
1901
 
        if not do_vfs:
1902
 
            client = _SmartClient(client_medium)
1903
 
            path = client.remote_path_from_transport(transport)
1904
 
            if client_medium._is_remote_before((1, 16)):
1905
 
                do_vfs = True
1906
 
        if do_vfs:
1907
 
            # TODO: lookup the local format from a server hint.
1908
 
            local_dir_format = BzrDirMetaFormat1()
1909
 
            self._supply_sub_formats_to(local_dir_format)
1910
 
            return local_dir_format.initialize_on_transport_ex(transport,
1911
 
                use_existing_dir=use_existing_dir, create_prefix=create_prefix,
1912
 
                force_new_repo=force_new_repo, stacked_on=stacked_on,
1913
 
                stack_on_pwd=stack_on_pwd, repo_format_name=repo_format_name,
1914
 
                make_working_trees=make_working_trees, shared_repo=shared_repo,
1915
 
                vfs_only=True)
1916
 
        return self._initialize_on_transport_ex_rpc(client, path, transport,
1917
 
            use_existing_dir, create_prefix, force_new_repo, stacked_on,
1918
 
            stack_on_pwd, repo_format_name, make_working_trees, shared_repo)
1919
 
 
1920
 
    def _initialize_on_transport_ex_rpc(self, client, path, transport,
1921
 
        use_existing_dir, create_prefix, force_new_repo, stacked_on,
1922
 
        stack_on_pwd, repo_format_name, make_working_trees, shared_repo):
1923
 
        args = []
1924
 
        args.append(self._serialize_NoneTrueFalse(use_existing_dir))
1925
 
        args.append(self._serialize_NoneTrueFalse(create_prefix))
1926
 
        args.append(self._serialize_NoneTrueFalse(force_new_repo))
1927
 
        args.append(self._serialize_NoneString(stacked_on))
1928
 
        # stack_on_pwd is often/usually our transport
1929
 
        if stack_on_pwd:
1930
 
            try:
1931
 
                stack_on_pwd = transport.relpath(stack_on_pwd)
1932
 
                if not stack_on_pwd:
1933
 
                    stack_on_pwd = '.'
1934
 
            except errors.PathNotChild:
1935
 
                pass
1936
 
        args.append(self._serialize_NoneString(stack_on_pwd))
1937
 
        args.append(self._serialize_NoneString(repo_format_name))
1938
 
        args.append(self._serialize_NoneTrueFalse(make_working_trees))
1939
 
        args.append(self._serialize_NoneTrueFalse(shared_repo))
1940
 
        request_network_name = self._network_name or \
1941
 
            BzrDirFormat.get_default_format().network_name()
1942
 
        try:
1943
 
            response = client.call('BzrDirFormat.initialize_ex_1.16',
1944
 
                request_network_name, path, *args)
1945
 
        except errors.UnknownSmartMethod:
1946
 
            client._medium._remember_remote_is_before((1,16))
1947
 
            local_dir_format = BzrDirMetaFormat1()
1948
 
            self._supply_sub_formats_to(local_dir_format)
1949
 
            return local_dir_format.initialize_on_transport_ex(transport,
1950
 
                use_existing_dir=use_existing_dir, create_prefix=create_prefix,
1951
 
                force_new_repo=force_new_repo, stacked_on=stacked_on,
1952
 
                stack_on_pwd=stack_on_pwd, repo_format_name=repo_format_name,
1953
 
                make_working_trees=make_working_trees, shared_repo=shared_repo,
1954
 
                vfs_only=True)
1955
 
        except errors.ErrorFromSmartServer, err:
1956
 
            remote._translate_error(err, path=path)
1957
 
        repo_path = response[0]
1958
 
        bzrdir_name = response[6]
1959
 
        require_stacking = response[7]
1960
 
        require_stacking = self.parse_NoneTrueFalse(require_stacking)
1961
 
        format = RemoteBzrDirFormat()
1962
 
        format._network_name = bzrdir_name
1963
 
        self._supply_sub_formats_to(format)
1964
 
        bzrdir = remote.RemoteBzrDir(transport, format, _client=client)
1965
 
        if repo_path:
1966
 
            repo_format = remote.response_tuple_to_repo_format(response[1:])
1967
 
            if repo_path == '.':
1968
 
                repo_path = ''
1969
 
            if repo_path:
1970
 
                repo_bzrdir_format = RemoteBzrDirFormat()
1971
 
                repo_bzrdir_format._network_name = response[5]
1972
 
                repo_bzr = remote.RemoteBzrDir(transport.clone(repo_path),
1973
 
                    repo_bzrdir_format)
1974
 
            else:
1975
 
                repo_bzr = bzrdir
1976
 
            final_stack = response[8] or None
1977
 
            final_stack_pwd = response[9] or None
1978
 
            if final_stack_pwd:
1979
 
                final_stack_pwd = urlutils.join(
1980
 
                    transport.base, final_stack_pwd)
1981
 
            remote_repo = remote.RemoteRepository(repo_bzr, repo_format)
1982
 
            if len(response) > 10:
1983
 
                # Updated server verb that locks remotely.
1984
 
                repo_lock_token = response[10] or None
1985
 
                remote_repo.lock_write(repo_lock_token, _skip_rpc=True)
1986
 
                if repo_lock_token:
1987
 
                    remote_repo.dont_leave_lock_in_place()
1988
 
            else:
1989
 
                remote_repo.lock_write()
1990
 
            policy = UseExistingRepository(remote_repo, final_stack,
1991
 
                final_stack_pwd, require_stacking)
1992
 
            policy.acquire_repository()
1993
 
        else:
1994
 
            remote_repo = None
1995
 
            policy = None
1996
 
        bzrdir._format.set_branch_format(self.get_branch_format())
1997
 
        if require_stacking:
1998
 
            # The repo has already been created, but we need to make sure that
1999
 
            # we'll make a stackable branch.
2000
 
            bzrdir._format.require_stacking(_skip_repo=True)
2001
 
        return remote_repo, bzrdir, require_stacking, policy
2002
 
 
2003
 
    def _open(self, transport):
2004
 
        return remote.RemoteBzrDir(transport, self)
2005
 
 
2006
 
    def __eq__(self, other):
2007
 
        if not isinstance(other, RemoteBzrDirFormat):
2008
 
            return False
2009
 
        return self.get_format_description() == other.get_format_description()
2010
 
 
2011
 
    def __return_repository_format(self):
2012
 
        # Always return a RemoteRepositoryFormat object, but if a specific bzr
2013
 
        # repository format has been asked for, tell the RemoteRepositoryFormat
2014
 
        # that it should use that for init() etc.
2015
 
        result = remote.RemoteRepositoryFormat()
2016
 
        custom_format = getattr(self, '_repository_format', None)
2017
 
        if custom_format:
2018
 
            if isinstance(custom_format, remote.RemoteRepositoryFormat):
2019
 
                return custom_format
2020
 
            else:
2021
 
                # We will use the custom format to create repositories over the
2022
 
                # wire; expose its details like rich_root_data for code to
2023
 
                # query
2024
 
                result._custom_format = custom_format
2025
 
        return result
2026
 
 
2027
 
    def get_branch_format(self):
2028
 
        result = BzrDirMetaFormat1.get_branch_format(self)
2029
 
        if not isinstance(result, remote.RemoteBranchFormat):
2030
 
            new_result = remote.RemoteBranchFormat()
2031
 
            new_result._custom_format = result
2032
 
            # cache the result
2033
 
            self.set_branch_format(new_result)
2034
 
            result = new_result
2035
 
        return result
2036
 
 
2037
 
    repository_format = property(__return_repository_format,
2038
 
        BzrDirMetaFormat1._set_repository_format) #.im_func)
2039
 
 
2040
 
 
2041
1786
controldir.ControlDirFormat.register_server_prober(RemoteBzrProber)
2042
1787
 
2043
1788