~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/bzrdir.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-04-09 20:23:07 UTC
  • mfrom: (4265.1.4 bbc-merge)
  • Revision ID: pqm@pqm.ubuntu.com-20090409202307-n0depb16qepoe21o
(jam) Change _fetch_uses_deltas = False for CHK repos until we can
        write a better fix.

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
 
38
38
import bzrlib
39
39
from bzrlib import (
40
 
    branch,
41
40
    config,
42
41
    errors,
43
42
    graph,
45
44
    lockdir,
46
45
    osutils,
47
46
    remote,
48
 
    repository,
49
47
    revision as _mod_revision,
50
48
    ui,
51
49
    urlutils,
62
60
from bzrlib.push import (
63
61
    PushResult,
64
62
    )
65
 
from bzrlib.repofmt import pack_repo
66
63
from bzrlib.smart.client import _SmartClient
67
64
from bzrlib.store.versioned import WeaveStore
68
65
from bzrlib.transactions import WriteTransaction
70
67
    do_catching_redirections,
71
68
    get_transport,
72
69
    local,
 
70
    remote as remote_transport,
73
71
    )
74
72
from bzrlib.weave import Weave
75
73
""")
77
75
from bzrlib.trace import (
78
76
    mutter,
79
77
    note,
80
 
    warning,
81
78
    )
82
79
 
83
80
from bzrlib import (
130
127
 
131
128
    def check_conversion_target(self, target_format):
132
129
        target_repo_format = target_format.repository_format
133
 
        self.open_repository()._format.check_conversion_target(target_repo_format)
 
130
        source_repo_format = self._format.repository_format
 
131
        source_repo_format.check_conversion_target(target_repo_format)
134
132
 
135
133
    @staticmethod
136
134
    def _check_supported(format, allow_unsupported,
180
178
                                       preserve_stacking=preserve_stacking)
181
179
 
182
180
    def clone_on_transport(self, transport, revision_id=None,
183
 
        force_new_repo=False, preserve_stacking=False, stacked_on=None,
184
 
        create_prefix=False, use_existing_dir=True):
 
181
                           force_new_repo=False, preserve_stacking=False,
 
182
                           stacked_on=None):
185
183
        """Clone this bzrdir and its contents to transport verbatim.
186
184
 
187
185
        :param transport: The transport for the location to produce the clone
193
191
                               even if one is available.
194
192
        :param preserve_stacking: When cloning a stacked branch, stack the
195
193
            new branch on top of the other branch's stacked-on branch.
196
 
        :param create_prefix: Create any missing directories leading up to
197
 
            to_transport.
198
 
        :param use_existing_dir: Use an existing directory if one exists.
199
194
        """
200
 
        # Overview: put together a broad description of what we want to end up
201
 
        # with; then make as few api calls as possible to do it.
202
 
        
203
 
        # We may want to create a repo/branch/tree, if we do so what format
204
 
        # would we want for each:
 
195
        transport.ensure_base()
205
196
        require_stacking = (stacked_on is not None)
206
197
        format = self.cloning_metadir(require_stacking)
207
 
        
208
 
        # Figure out what objects we want:
 
198
        # Bug: We create a metadir without knowing if it can support stacking,
 
199
        # we should look up the policy needs first.
 
200
        result = format.initialize_on_transport(transport)
 
201
        repository_policy = None
209
202
        try:
210
203
            local_repo = self.find_repository()
211
204
        except errors.NoRepositoryPresent:
225
218
                        errors.UnstackableRepositoryFormat,
226
219
                        errors.NotStacked):
227
220
                    pass
228
 
        # Bug: We create a metadir without knowing if it can support stacking,
229
 
        # we should look up the policy needs first, or just use it as a hint,
230
 
        # or something.
 
221
 
231
222
        if local_repo:
 
223
            # may need to copy content in
 
224
            repository_policy = result.determine_repository_policy(
 
225
                force_new_repo, stacked_on, self.root_transport.base,
 
226
                require_stacking=require_stacking)
232
227
            make_working_trees = local_repo.make_working_trees()
233
 
            want_shared = local_repo.is_shared()
234
 
            repo_format_name = format.repository_format.network_name()
235
 
        else:
236
 
            make_working_trees = False
237
 
            want_shared = False
238
 
            repo_format_name = None
239
 
 
240
 
        result_repo, result, require_stacking, repository_policy = \
241
 
            format.initialize_on_transport_ex(transport,
242
 
            use_existing_dir=use_existing_dir, create_prefix=create_prefix,
243
 
            force_new_repo=force_new_repo, stacked_on=stacked_on,
244
 
            stack_on_pwd=self.root_transport.base,
245
 
            repo_format_name=repo_format_name,
246
 
            make_working_trees=make_working_trees, shared_repo=want_shared)
247
 
        if repo_format_name:
248
 
            try:
249
 
                # If the result repository is in the same place as the
250
 
                # resulting bzr dir, it will have no content, further if the
251
 
                # result is not stacked then we know all content should be
252
 
                # copied, and finally if we are copying up to a specific
253
 
                # revision_id then we can use the pending-ancestry-result which
254
 
                # does not require traversing all of history to describe it.
255
 
                if (result_repo.bzrdir.root_transport.base ==
256
 
                    result.root_transport.base and not require_stacking and
257
 
                    revision_id is not None):
258
 
                    fetch_spec = graph.PendingAncestryResult(
259
 
                        [revision_id], local_repo)
260
 
                    result_repo.fetch(local_repo, fetch_spec=fetch_spec)
261
 
                else:
262
 
                    result_repo.fetch(local_repo, revision_id=revision_id)
263
 
            finally:
264
 
                result_repo.unlock()
265
 
        else:
266
 
            if result_repo is not None:
267
 
                raise AssertionError('result_repo not None(%r)' % result_repo)
 
228
            result_repo, is_new_repo = repository_policy.acquire_repository(
 
229
                make_working_trees, local_repo.is_shared())
 
230
            if not require_stacking and repository_policy._require_stacking:
 
231
                require_stacking = True
 
232
                result._format.require_stacking()
 
233
            if is_new_repo and not require_stacking and revision_id is not None:
 
234
                fetch_spec = graph.PendingAncestryResult(
 
235
                    [revision_id], local_repo)
 
236
                result_repo.fetch(local_repo, fetch_spec=fetch_spec)
 
237
            else:
 
238
                result_repo.fetch(local_repo, revision_id=revision_id)
 
239
        else:
 
240
            result_repo = None
268
241
        # 1 if there is a branch present
269
242
        #   make sure its content is available in the target repository
270
243
        #   clone it.
435
408
            stack_on_pwd = None
436
409
            config = found_bzrdir.get_config()
437
410
            stop = False
438
 
            stack_on = config.get_default_stack_on()
439
 
            if stack_on is not None:
440
 
                stack_on_pwd = found_bzrdir.root_transport.base
441
 
                stop = True
 
411
            if config is not None:
 
412
                stack_on = config.get_default_stack_on()
 
413
                if stack_on is not None:
 
414
                    stack_on_pwd = found_bzrdir.root_transport.base
 
415
                    stop = True
442
416
            # does it have a repository ?
443
417
            try:
444
418
                repository = found_bzrdir.open_repository()
770
744
        raise NotImplementedError(self.get_workingtree_transport)
771
745
 
772
746
    def get_config(self):
773
 
        """Get configuration for this BzrDir."""
774
 
        return config.BzrDirConfig(self)
775
 
 
776
 
    def _get_config(self):
777
 
        """By default, no configuration is available."""
778
 
        return None
 
747
        if getattr(self, '_get_config', None) is None:
 
748
            return None
 
749
        return self._get_config()
779
750
 
780
751
    def __init__(self, _transport, _format):
781
752
        """Initialize a Bzr control dir object.
1094
1065
        """
1095
1066
        format, repository = self._cloning_metadir()
1096
1067
        if format._workingtree_format is None:
1097
 
            # No tree in self.
1098
1068
            if repository is None:
1099
 
                # No repository either
1100
1069
                return format
1101
 
            # We have a repository, so set a working tree? (Why? This seems to
1102
 
            # contradict the stated return value in the docstring).
1103
1070
            tree_format = repository._format._matchingbzrdir.workingtree_format
1104
1071
            format.workingtree_format = tree_format.__class__()
1105
1072
        if require_stacking:
1233
1200
        return result
1234
1201
 
1235
1202
    def push_branch(self, source, revision_id=None, overwrite=False, 
1236
 
        remember=False, create_prefix=False):
 
1203
        remember=False):
1237
1204
        """Push the source branch into this BzrDir."""
1238
1205
        br_to = None
1239
1206
        # If we can open a branch, use its direct repository, otherwise see
1384
1351
        # that can do wonky stuff here, and that only
1385
1352
        # happens for creating checkouts, which cannot be
1386
1353
        # done on this format anyway. So - acceptable wart.
1387
 
        if hardlink:
1388
 
            warning("can't support hardlinked working trees in %r"
1389
 
                % (self,))
1390
1354
        try:
1391
1355
            result = self.open_workingtree(recommend_upgrade=False)
1392
1356
        except errors.NoSuchFile:
1633
1597
 
1634
1598
    def get_branch_transport(self, branch_format):
1635
1599
        """See BzrDir.get_branch_transport()."""
1636
 
        # XXX: this shouldn't implicitly create the directory if it's just
1637
 
        # promising to get a transport -- mbp 20090727
1638
1600
        if branch_format is None:
1639
1601
            return self.transport.clone('branch')
1640
1602
        try:
1734
1696
        return format.open(self, _found=True)
1735
1697
 
1736
1698
    def _get_config(self):
1737
 
        return config.TransportConfig(self.transport, 'control.conf')
 
1699
        return config.BzrDirConfig(self.transport)
1738
1700
 
1739
1701
 
1740
1702
class BzrDirFormat(object):
1835
1797
    def initialize(self, url, possible_transports=None):
1836
1798
        """Create a bzr control dir at this url and return an opened copy.
1837
1799
 
1838
 
        While not deprecated, this method is very specific and its use will
1839
 
        lead to many round trips to setup a working environment. See
1840
 
        initialize_on_transport_ex for a [nearly] all-in-one method.
1841
 
 
1842
1800
        Subclasses should typically override initialize_on_transport
1843
1801
        instead of this method.
1844
1802
        """
1863
1821
            self._supply_sub_formats_to(remote_format)
1864
1822
            return remote_format.initialize_on_transport(transport)
1865
1823
 
1866
 
    def initialize_on_transport_ex(self, transport, use_existing_dir=False,
1867
 
        create_prefix=False, force_new_repo=False, stacked_on=None,
1868
 
        stack_on_pwd=None, repo_format_name=None, make_working_trees=None,
1869
 
        shared_repo=False, vfs_only=False):
1870
 
        """Create this format on transport.
1871
 
 
1872
 
        The directory to initialize will be created.
1873
 
 
1874
 
        :param force_new_repo: Do not use a shared repository for the target,
1875
 
                               even if one is available.
1876
 
        :param create_prefix: Create any missing directories leading up to
1877
 
            to_transport.
1878
 
        :param use_existing_dir: Use an existing directory if one exists.
1879
 
        :param stacked_on: A url to stack any created branch on, None to follow
1880
 
            any target stacking policy.
1881
 
        :param stack_on_pwd: If stack_on is relative, the location it is
1882
 
            relative to.
1883
 
        :param repo_format_name: If non-None, a repository will be
1884
 
            made-or-found. Should none be found, or if force_new_repo is True
1885
 
            the repo_format_name is used to select the format of repository to
1886
 
            create.
1887
 
        :param make_working_trees: Control the setting of make_working_trees
1888
 
            for a new shared repository when one is made. None to use whatever
1889
 
            default the format has.
1890
 
        :param shared_repo: Control whether made repositories are shared or
1891
 
            not.
1892
 
        :param vfs_only: If True do not attempt to use a smart server
1893
 
        :return: repo, bzrdir, require_stacking, repository_policy. repo is
1894
 
            None if none was created or found, bzrdir is always valid.
1895
 
            require_stacking is the result of examining the stacked_on
1896
 
            parameter and any stacking policy found for the target.
1897
 
        """
1898
 
        if not vfs_only:
1899
 
            # Try to hand off to a smart server 
1900
 
            try:
1901
 
                client_medium = transport.get_smart_medium()
1902
 
            except errors.NoSmartMedium:
1903
 
                pass
1904
 
            else:
1905
 
                # TODO: lookup the local format from a server hint.
1906
 
                remote_dir_format = RemoteBzrDirFormat()
1907
 
                remote_dir_format._network_name = self.network_name()
1908
 
                self._supply_sub_formats_to(remote_dir_format)
1909
 
                return remote_dir_format.initialize_on_transport_ex(transport,
1910
 
                    use_existing_dir=use_existing_dir, create_prefix=create_prefix,
1911
 
                    force_new_repo=force_new_repo, stacked_on=stacked_on,
1912
 
                    stack_on_pwd=stack_on_pwd, repo_format_name=repo_format_name,
1913
 
                    make_working_trees=make_working_trees, shared_repo=shared_repo)
1914
 
        # XXX: Refactor the create_prefix/no_create_prefix code into a
1915
 
        #      common helper function
1916
 
        # The destination may not exist - if so make it according to policy.
1917
 
        def make_directory(transport):
1918
 
            transport.mkdir('.')
1919
 
            return transport
1920
 
        def redirected(transport, e, redirection_notice):
1921
 
            note(redirection_notice)
1922
 
            return transport._redirected_to(e.source, e.target)
1923
 
        try:
1924
 
            transport = do_catching_redirections(make_directory, transport,
1925
 
                redirected)
1926
 
        except errors.FileExists:
1927
 
            if not use_existing_dir:
1928
 
                raise
1929
 
        except errors.NoSuchFile:
1930
 
            if not create_prefix:
1931
 
                raise
1932
 
            transport.create_prefix()
1933
 
 
1934
 
        require_stacking = (stacked_on is not None)
1935
 
        # Now the target directory exists, but doesn't have a .bzr
1936
 
        # directory. So we need to create it, along with any work to create
1937
 
        # all of the dependent branches, etc.
1938
 
 
1939
 
        result = self.initialize_on_transport(transport)
1940
 
        if repo_format_name:
1941
 
            try:
1942
 
                # use a custom format
1943
 
                result._format.repository_format = \
1944
 
                    repository.network_format_registry.get(repo_format_name)
1945
 
            except AttributeError:
1946
 
                # The format didn't permit it to be set.
1947
 
                pass
1948
 
            # A repository is desired, either in-place or shared.
1949
 
            repository_policy = result.determine_repository_policy(
1950
 
                force_new_repo, stacked_on, stack_on_pwd,
1951
 
                require_stacking=require_stacking)
1952
 
            result_repo, is_new_repo = repository_policy.acquire_repository(
1953
 
                make_working_trees, shared_repo)
1954
 
            if not require_stacking and repository_policy._require_stacking:
1955
 
                require_stacking = True
1956
 
                result._format.require_stacking()
1957
 
            result_repo.lock_write()
1958
 
        else:
1959
 
            result_repo = None
1960
 
            repository_policy = None
1961
 
        return result_repo, result, require_stacking, repository_policy
1962
 
 
1963
1824
    def _initialize_on_transport_vfs(self, transport):
1964
1825
        """Initialize a new bzrdir using VFS calls.
1965
1826
 
2177
2038
    repository_format = property(__return_repository_format)
2178
2039
 
2179
2040
 
2180
 
class BzrDirFormatAllInOne(BzrDirFormat):
2181
 
    """Common class for formats before meta-dirs."""
2182
 
 
2183
 
    def initialize_on_transport_ex(self, transport, use_existing_dir=False,
2184
 
        create_prefix=False, force_new_repo=False, stacked_on=None,
2185
 
        stack_on_pwd=None, repo_format_name=None, make_working_trees=None,
2186
 
        shared_repo=False):
2187
 
        """See BzrDirFormat.initialize_on_transport_ex."""
2188
 
        require_stacking = (stacked_on is not None)
2189
 
        # Format 5 cannot stack, but we've been asked to - actually init
2190
 
        # a Meta1Dir
2191
 
        if require_stacking:
2192
 
            format = BzrDirMetaFormat1()
2193
 
            return format.initialize_on_transport_ex(transport,
2194
 
                use_existing_dir=use_existing_dir, create_prefix=create_prefix,
2195
 
                force_new_repo=force_new_repo, stacked_on=stacked_on,
2196
 
                stack_on_pwd=stack_on_pwd, repo_format_name=repo_format_name,
2197
 
                make_working_trees=make_working_trees, shared_repo=shared_repo)
2198
 
        return BzrDirFormat.initialize_on_transport_ex(self, transport,
2199
 
            use_existing_dir=use_existing_dir, create_prefix=create_prefix,
2200
 
            force_new_repo=force_new_repo, stacked_on=stacked_on,
2201
 
            stack_on_pwd=stack_on_pwd, repo_format_name=repo_format_name,
2202
 
            make_working_trees=make_working_trees, shared_repo=shared_repo)
2203
 
 
2204
 
 
2205
 
class BzrDirFormat5(BzrDirFormatAllInOne):
 
2041
class BzrDirFormat5(BzrDirFormat):
2206
2042
    """Bzr control format 5.
2207
2043
 
2208
2044
    This format is a combined format for working tree, branch and repository.
2263
2099
    repository_format = property(__return_repository_format)
2264
2100
 
2265
2101
 
2266
 
class BzrDirFormat6(BzrDirFormatAllInOne):
 
2102
class BzrDirFormat6(BzrDirFormat):
2267
2103
    """Bzr control format 6.
2268
2104
 
2269
2105
    This format is a combined format for working tree, branch and repository.
2362
2198
    def set_branch_format(self, format):
2363
2199
        self._branch_format = format
2364
2200
 
2365
 
    def require_stacking(self, stack_on=None, possible_transports=None,
2366
 
            _skip_repo=False):
2367
 
        """We have a request to stack, try to ensure the formats support it.
2368
 
 
2369
 
        :param stack_on: If supplied, it is the URL to a branch that we want to
2370
 
            stack on. Check to see if that format supports stacking before
2371
 
            forcing an upgrade.
2372
 
        """
2373
 
        # Stacking is desired. requested by the target, but does the place it
2374
 
        # points at support stacking? If it doesn't then we should
2375
 
        # not implicitly upgrade. We check this here.
2376
 
        new_repo_format = None
2377
 
        new_branch_format = None
2378
 
 
2379
 
        # a bit of state for get_target_branch so that we don't try to open it
2380
 
        # 2 times, for both repo *and* branch
2381
 
        target = [None, False, None] # target_branch, checked, upgrade anyway
2382
 
        def get_target_branch():
2383
 
            if target[1]:
2384
 
                # We've checked, don't check again
2385
 
                return target
2386
 
            if stack_on is None:
2387
 
                # No target format, that means we want to force upgrading
2388
 
                target[:] = [None, True, True]
2389
 
                return target
2390
 
            try:
2391
 
                target_dir = BzrDir.open(stack_on,
2392
 
                    possible_transports=possible_transports)
2393
 
            except errors.NotBranchError:
2394
 
                # Nothing there, don't change formats
2395
 
                target[:] = [None, True, False]
2396
 
                return target
2397
 
            except errors.JailBreak:
2398
 
                # JailBreak, JFDI and upgrade anyway
2399
 
                target[:] = [None, True, True]
2400
 
                return target
2401
 
            try:
2402
 
                target_branch = target_dir.open_branch()
2403
 
            except errors.NotBranchError:
2404
 
                # No branch, don't upgrade formats
2405
 
                target[:] = [None, True, False]
2406
 
                return target
2407
 
            target[:] = [target_branch, True, False]
2408
 
            return target
2409
 
 
2410
 
        if (not _skip_repo and
2411
 
                 not self.repository_format.supports_external_lookups):
2412
 
            # We need to upgrade the Repository.
2413
 
            target_branch, _, do_upgrade = get_target_branch()
2414
 
            if target_branch is None:
2415
 
                # We don't have a target branch, should we upgrade anyway?
2416
 
                if do_upgrade:
2417
 
                    # stack_on is inaccessible, JFDI.
2418
 
                    # TODO: bad monkey, hard-coded formats...
2419
 
                    if self.repository_format.rich_root_data:
2420
 
                        new_repo_format = pack_repo.RepositoryFormatKnitPack5RichRoot()
2421
 
                    else:
2422
 
                        new_repo_format = pack_repo.RepositoryFormatKnitPack5()
2423
 
            else:
2424
 
                # If the target already supports stacking, then we know the
2425
 
                # project is already able to use stacking, so auto-upgrade
2426
 
                # for them
2427
 
                new_repo_format = target_branch.repository._format
2428
 
                if not new_repo_format.supports_external_lookups:
2429
 
                    # target doesn't, source doesn't, so don't auto upgrade
2430
 
                    # repo
2431
 
                    new_repo_format = None
2432
 
            if new_repo_format is not None:
2433
 
                self.repository_format = new_repo_format
2434
 
                note('Source repository format does not support stacking,'
2435
 
                     ' using format:\n  %s',
2436
 
                     new_repo_format.get_format_description())
2437
 
 
 
2201
    def require_stacking(self):
2438
2202
        if not self.get_branch_format().supports_stacking():
2439
 
            # We just checked the repo, now lets check if we need to
2440
 
            # upgrade the branch format
2441
 
            target_branch, _, do_upgrade = get_target_branch()
2442
 
            if target_branch is None:
2443
 
                if do_upgrade:
2444
 
                    # TODO: bad monkey, hard-coded formats...
2445
 
                    new_branch_format = branch.BzrBranchFormat7()
 
2203
            # We need to make a stacked branch, but the default format for the
 
2204
            # target doesn't support stacking.  So force a branch that *can*
 
2205
            # support stacking.
 
2206
            from bzrlib.branch import BzrBranchFormat7
 
2207
            branch_format = BzrBranchFormat7()
 
2208
            self.set_branch_format(branch_format)
 
2209
            mutter("using %r for stacking" % (branch_format,))
 
2210
            from bzrlib.repofmt import pack_repo
 
2211
            if self.repository_format.rich_root_data:
 
2212
                bzrdir_format_name = '1.6.1-rich-root'
 
2213
                repo_format = pack_repo.RepositoryFormatKnitPack5RichRoot()
2446
2214
            else:
2447
 
                new_branch_format = target_branch._format
2448
 
                if not new_branch_format.supports_stacking():
2449
 
                    new_branch_format = None
2450
 
            if new_branch_format is not None:
2451
 
                # Does support stacking, use its format.
2452
 
                self.set_branch_format(new_branch_format)
2453
 
                note('Source branch format does not support stacking,'
2454
 
                     ' using format:\n  %s',
2455
 
                     new_branch_format.get_format_description())
 
2215
                bzrdir_format_name = '1.6'
 
2216
                repo_format = pack_repo.RepositoryFormatKnitPack5()
 
2217
            note('Source format does not support stacking, using format:'
 
2218
                 ' \'%s\'\n  %s\n',
 
2219
                 bzrdir_format_name, repo_format.get_format_description())
 
2220
            self.repository_format = repo_format
2456
2221
 
2457
2222
    def get_converter(self, format=None):
2458
2223
        """See BzrDirFormat.get_converter()."""
2476
2241
 
2477
2242
    def _open(self, transport):
2478
2243
        """See BzrDirFormat._open."""
2479
 
        # Create a new format instance because otherwise initialisation of new
2480
 
        # metadirs share the global default format object leading to alias
2481
 
        # problems.
2482
 
        format = BzrDirMetaFormat1()
2483
 
        self._supply_sub_formats_to(format)
2484
 
        return BzrDirMeta1(transport, format)
 
2244
        return BzrDirMeta1(transport, self)
2485
2245
 
2486
2246
    def __return_repository_format(self):
2487
2247
        """Circular import protection."""
2774
2534
        del ie.text_id
2775
2535
 
2776
2536
    def get_parent_map(self, revision_ids):
2777
 
        """See graph.StackedParentsProvider.get_parent_map"""
 
2537
        """See graph._StackedParentsProvider.get_parent_map"""
2778
2538
        return dict((revision_id, self.revisions[revision_id])
2779
2539
                    for revision_id in revision_ids
2780
2540
                     if revision_id in self.revisions)
3027
2787
            while old != new:
3028
2788
                if (old == _mod_branch.BzrBranchFormat5 and
3029
2789
                    new in (_mod_branch.BzrBranchFormat6,
3030
 
                        _mod_branch.BzrBranchFormat7,
3031
 
                        _mod_branch.BzrBranchFormat8)):
 
2790
                        _mod_branch.BzrBranchFormat7)):
3032
2791
                    branch_converter = _mod_branch.Converter5to6()
3033
2792
                elif (old == _mod_branch.BzrBranchFormat6 and
3034
 
                    new in (_mod_branch.BzrBranchFormat7,
3035
 
                            _mod_branch.BzrBranchFormat8)):
 
2793
                    new == _mod_branch.BzrBranchFormat7):
3036
2794
                    branch_converter = _mod_branch.Converter6to7()
3037
 
                elif (old == _mod_branch.BzrBranchFormat7 and
3038
 
                      new is _mod_branch.BzrBranchFormat8):
3039
 
                    branch_converter = _mod_branch.Converter7to8()
3040
2795
                else:
3041
 
                    raise errors.BadConversionTarget("No converter", new,
3042
 
                        branch._format)
 
2796
                    raise errors.BadConversionTarget("No converter", new)
3043
2797
                branch_converter.convert(branch)
3044
2798
                branch = self.bzrdir.open_branch()
3045
2799
                old = branch._format.__class__
3068
2822
        return to_convert
3069
2823
 
3070
2824
 
3071
 
# This is not in remote.py because it's relatively small, and needs to be
3072
 
# registered. Putting it in remote.py creates a circular import problem.
 
2825
# This is not in remote.py because it's small, and needs to be registered.
 
2826
# Putting it in remote.py creates a circular import problem.
3073
2827
# we can make it a lazy object if the control formats is turned into something
3074
2828
# like a registry.
3075
2829
class RemoteBzrDirFormat(BzrDirMetaFormat1):
3125
2879
            return local_dir_format.initialize_on_transport(transport)
3126
2880
        client = _SmartClient(client_medium)
3127
2881
        path = client.remote_path_from_transport(transport)
3128
 
        try:
3129
 
            response = client.call('BzrDirFormat.initialize', path)
3130
 
        except errors.ErrorFromSmartServer, err:
3131
 
            remote._translate_error(err, path=path)
 
2882
        response = client.call('BzrDirFormat.initialize', path)
3132
2883
        if response[0] != 'ok':
3133
2884
            raise errors.SmartProtocolError('unexpected response code %s' % (response,))
3134
2885
        format = RemoteBzrDirFormat()
3135
2886
        self._supply_sub_formats_to(format)
3136
2887
        return remote.RemoteBzrDir(transport, format)
3137
2888
 
3138
 
    def parse_NoneTrueFalse(self, arg):
3139
 
        if not arg:
3140
 
            return None
3141
 
        if arg == 'False':
3142
 
            return False
3143
 
        if arg == 'True':
3144
 
            return True
3145
 
        raise AssertionError("invalid arg %r" % arg)
3146
 
 
3147
 
    def _serialize_NoneTrueFalse(self, arg):
3148
 
        if arg is False:
3149
 
            return 'False'
3150
 
        if arg:
3151
 
            return 'True'
3152
 
        return ''
3153
 
 
3154
 
    def _serialize_NoneString(self, arg):
3155
 
        return arg or ''
3156
 
 
3157
 
    def initialize_on_transport_ex(self, transport, use_existing_dir=False,
3158
 
        create_prefix=False, force_new_repo=False, stacked_on=None,
3159
 
        stack_on_pwd=None, repo_format_name=None, make_working_trees=None,
3160
 
        shared_repo=False):
3161
 
        try:
3162
 
            # hand off the request to the smart server
3163
 
            client_medium = transport.get_smart_medium()
3164
 
        except errors.NoSmartMedium:
3165
 
            do_vfs = True
3166
 
        else:
3167
 
            # Decline to open it if the server doesn't support our required
3168
 
            # version (3) so that the VFS-based transport will do it.
3169
 
            if client_medium.should_probe():
3170
 
                try:
3171
 
                    server_version = client_medium.protocol_version()
3172
 
                    if server_version != '2':
3173
 
                        do_vfs = True
3174
 
                    else:
3175
 
                        do_vfs = False
3176
 
                except errors.SmartProtocolError:
3177
 
                    # Apparently there's no usable smart server there, even though
3178
 
                    # the medium supports the smart protocol.
3179
 
                    do_vfs = True
3180
 
            else:
3181
 
                do_vfs = False
3182
 
        if not do_vfs:
3183
 
            client = _SmartClient(client_medium)
3184
 
            path = client.remote_path_from_transport(transport)
3185
 
            if client_medium._is_remote_before((1, 16)):
3186
 
                do_vfs = True
3187
 
        if do_vfs:
3188
 
            # TODO: lookup the local format from a server hint.
3189
 
            local_dir_format = BzrDirMetaFormat1()
3190
 
            self._supply_sub_formats_to(local_dir_format)
3191
 
            return local_dir_format.initialize_on_transport_ex(transport,
3192
 
                use_existing_dir=use_existing_dir, create_prefix=create_prefix,
3193
 
                force_new_repo=force_new_repo, stacked_on=stacked_on,
3194
 
                stack_on_pwd=stack_on_pwd, repo_format_name=repo_format_name,
3195
 
                make_working_trees=make_working_trees, shared_repo=shared_repo,
3196
 
                vfs_only=True)
3197
 
        return self._initialize_on_transport_ex_rpc(client, path, transport,
3198
 
            use_existing_dir, create_prefix, force_new_repo, stacked_on,
3199
 
            stack_on_pwd, repo_format_name, make_working_trees, shared_repo)
3200
 
 
3201
 
    def _initialize_on_transport_ex_rpc(self, client, path, transport,
3202
 
        use_existing_dir, create_prefix, force_new_repo, stacked_on,
3203
 
        stack_on_pwd, repo_format_name, make_working_trees, shared_repo):
3204
 
        args = []
3205
 
        args.append(self._serialize_NoneTrueFalse(use_existing_dir))
3206
 
        args.append(self._serialize_NoneTrueFalse(create_prefix))
3207
 
        args.append(self._serialize_NoneTrueFalse(force_new_repo))
3208
 
        args.append(self._serialize_NoneString(stacked_on))
3209
 
        # stack_on_pwd is often/usually our transport
3210
 
        if stack_on_pwd:
3211
 
            try:
3212
 
                stack_on_pwd = transport.relpath(stack_on_pwd)
3213
 
                if not stack_on_pwd:
3214
 
                    stack_on_pwd = '.'
3215
 
            except errors.PathNotChild:
3216
 
                pass
3217
 
        args.append(self._serialize_NoneString(stack_on_pwd))
3218
 
        args.append(self._serialize_NoneString(repo_format_name))
3219
 
        args.append(self._serialize_NoneTrueFalse(make_working_trees))
3220
 
        args.append(self._serialize_NoneTrueFalse(shared_repo))
3221
 
        if self._network_name is None:
3222
 
            self._network_name = \
3223
 
            BzrDirFormat.get_default_format().network_name()
3224
 
        try:
3225
 
            response = client.call('BzrDirFormat.initialize_ex_1.16',
3226
 
                self.network_name(), path, *args)
3227
 
        except errors.UnknownSmartMethod:
3228
 
            client._medium._remember_remote_is_before((1,16))
3229
 
            local_dir_format = BzrDirMetaFormat1()
3230
 
            self._supply_sub_formats_to(local_dir_format)
3231
 
            return local_dir_format.initialize_on_transport_ex(transport,
3232
 
                use_existing_dir=use_existing_dir, create_prefix=create_prefix,
3233
 
                force_new_repo=force_new_repo, stacked_on=stacked_on,
3234
 
                stack_on_pwd=stack_on_pwd, repo_format_name=repo_format_name,
3235
 
                make_working_trees=make_working_trees, shared_repo=shared_repo,
3236
 
                vfs_only=True)
3237
 
        except errors.ErrorFromSmartServer, err:
3238
 
            remote._translate_error(err, path=path)
3239
 
        repo_path = response[0]
3240
 
        bzrdir_name = response[6]
3241
 
        require_stacking = response[7]
3242
 
        require_stacking = self.parse_NoneTrueFalse(require_stacking)
3243
 
        format = RemoteBzrDirFormat()
3244
 
        format._network_name = bzrdir_name
3245
 
        self._supply_sub_formats_to(format)
3246
 
        bzrdir = remote.RemoteBzrDir(transport, format, _client=client)
3247
 
        if repo_path:
3248
 
            repo_format = remote.response_tuple_to_repo_format(response[1:])
3249
 
            if repo_path == '.':
3250
 
                repo_path = ''
3251
 
            if repo_path:
3252
 
                repo_bzrdir_format = RemoteBzrDirFormat()
3253
 
                repo_bzrdir_format._network_name = response[5]
3254
 
                repo_bzr = remote.RemoteBzrDir(transport.clone(repo_path),
3255
 
                    repo_bzrdir_format)
3256
 
            else:
3257
 
                repo_bzr = bzrdir
3258
 
            final_stack = response[8] or None
3259
 
            final_stack_pwd = response[9] or None
3260
 
            if final_stack_pwd:
3261
 
                final_stack_pwd = urlutils.join(
3262
 
                    transport.base, final_stack_pwd)
3263
 
            remote_repo = remote.RemoteRepository(repo_bzr, repo_format)
3264
 
            if len(response) > 10:
3265
 
                # Updated server verb that locks remotely.
3266
 
                repo_lock_token = response[10] or None
3267
 
                remote_repo.lock_write(repo_lock_token, _skip_rpc=True)
3268
 
                if repo_lock_token:
3269
 
                    remote_repo.dont_leave_lock_in_place()
3270
 
            else:
3271
 
                remote_repo.lock_write()
3272
 
            policy = UseExistingRepository(remote_repo, final_stack,
3273
 
                final_stack_pwd, require_stacking)
3274
 
            policy.acquire_repository()
3275
 
        else:
3276
 
            remote_repo = None
3277
 
            policy = None
3278
 
        bzrdir._format.set_branch_format(self.get_branch_format())
3279
 
        if require_stacking:
3280
 
            # The repo has already been created, but we need to make sure that
3281
 
            # we'll make a stackable branch.
3282
 
            bzrdir._format.require_stacking(_skip_repo=True)
3283
 
        return remote_repo, bzrdir, require_stacking, policy
3284
 
 
3285
2889
    def _open(self, transport):
3286
2890
        return remote.RemoteBzrDir(transport, self)
3287
2891
 
3461
3065
            if info.native:
3462
3066
                help = '(native) ' + help
3463
3067
            return ':%s:\n%s\n\n' % (key,
3464
 
                textwrap.fill(help, initial_indent='    ',
3465
 
                    subsequent_indent='    ',
3466
 
                    break_long_words=False))
 
3068
                    textwrap.fill(help, initial_indent='    ',
 
3069
                    subsequent_indent='    '))
3467
3070
        if default_realkey is not None:
3468
3071
            output += wrapped(default_realkey, '(default) %s' % default_help,
3469
3072
                              self.get_info('default'))
3548
3151
            if self._require_stacking:
3549
3152
                raise
3550
3153
 
3551
 
    def requires_stacking(self):
3552
 
        """Return True if this policy requires stacking."""
3553
 
        return self._stack_on is not None and self._require_stacking
3554
 
 
3555
3154
    def _get_full_stack_on(self):
3556
3155
        """Get a fully-qualified URL for the stack_on location."""
3557
3156
        if self._stack_on is None:
3566
3165
        stack_on = self._get_full_stack_on()
3567
3166
        if stack_on is None:
3568
3167
            return
3569
 
        try:
3570
 
            stacked_dir = BzrDir.open(stack_on,
3571
 
                                      possible_transports=possible_transports)
3572
 
        except errors.JailBreak:
3573
 
            # We keep the stacking details, but we are in the server code so
3574
 
            # actually stacking is not needed.
3575
 
            return
 
3168
        stacked_dir = BzrDir.open(stack_on,
 
3169
                                  possible_transports=possible_transports)
3576
3170
        try:
3577
3171
            stacked_repo = stacked_dir.open_branch().repository
3578
3172
        except errors.NotBranchError:
3622
3216
        """
3623
3217
        stack_on = self._get_full_stack_on()
3624
3218
        if stack_on:
 
3219
            # Stacking is desired. requested by the target, but does the place it
 
3220
            # points at support stacking? If it doesn't then we should
 
3221
            # not implicitly upgrade. We check this here.
3625
3222
            format = self._bzrdir._format
3626
 
            format.require_stacking(stack_on=stack_on,
3627
 
                                    possible_transports=[self._bzrdir.root_transport])
 
3223
            if not (format.repository_format.supports_external_lookups
 
3224
                and format.get_branch_format().supports_stacking()):
 
3225
                # May need to upgrade - but only do if the target also
 
3226
                # supports stacking. Note that this currently wastes
 
3227
                # network round trips to check - but we only do this
 
3228
                # when the source can't stack so it will fade away
 
3229
                # as people do upgrade.
 
3230
                try:
 
3231
                    target_dir = BzrDir.open(stack_on,
 
3232
                        possible_transports=[self._bzrdir.root_transport])
 
3233
                except errors.NotBranchError:
 
3234
                    # Nothing there, don't change formats
 
3235
                    pass
 
3236
                else:
 
3237
                    try:
 
3238
                        target_branch = target_dir.open_branch()
 
3239
                    except errors.NotBranchError:
 
3240
                        # No branch, don't change formats
 
3241
                        pass
 
3242
                    else:
 
3243
                        branch_format = target_branch._format
 
3244
                        repo_format = target_branch.repository._format
 
3245
                        if not (branch_format.supports_stacking()
 
3246
                            and repo_format.supports_external_lookups):
 
3247
                            # Doesn't stack itself, don't force an upgrade
 
3248
                            pass
 
3249
                        else:
 
3250
                            # Does support stacking, use its format.
 
3251
                            format.repository_format = repo_format
 
3252
                            format.set_branch_format(branch_format)
 
3253
                            note('Source format does not support stacking, '
 
3254
                                'using format: \'%s\'\n  %s\n',
 
3255
                                branch_format.get_format_description(),
 
3256
                                repo_format.get_format_description())
3628
3257
            if not self._require_stacking:
3629
3258
                # We have picked up automatic stacking somewhere.
3630
3259
                note('Using default stacking branch %s at %s', self._stack_on,
3797
3426
# The following un-numbered 'development' formats should always just be aliases.
3798
3427
format_registry.register_metadir('development-rich-root',
3799
3428
    'bzrlib.repofmt.groupcompress_repo.RepositoryFormatCHK1',
3800
 
    help='Current development format. Supports rich roots. Can convert data '
3801
 
        'to and from rich-root-pack (and anything compatible with '
3802
 
        'rich-root-pack) format repositories. Repositories and branches in '
3803
 
        'this format can only be read by bzr.dev. Please read '
 
3429
    help='Current development format. Can convert data to and from pack-0.92 '
 
3430
        '(and anything compatible with pack-0.92) format repositories. '
 
3431
        'Repositories and branches in this format can only be read by bzr.dev. '
 
3432
        'Please read '
3804
3433
        'http://doc.bazaar-vcs.org/latest/developers/development-repo.html '
3805
3434
        'before use.',
3806
3435
    branch_format='bzrlib.branch.BzrBranchFormat7',
3837
3466
    experimental=True,
3838
3467
    )
3839
3468
 
3840
 
format_registry.register_metadir('development7-rich-root',
3841
 
    'bzrlib.repofmt.groupcompress_repo.RepositoryFormatCHK2',
3842
 
    help='pack-1.9 with 255-way hashed CHK inv, bencode revision, group compress, '
3843
 
        'rich roots. Please read '
3844
 
        'http://doc.bazaar-vcs.org/latest/developers/development-repo.html '
3845
 
        'before use.',
3846
 
    branch_format='bzrlib.branch.BzrBranchFormat7',
3847
 
    tree_format='bzrlib.workingtree.WorkingTreeFormat6',
3848
 
    hidden=True,
3849
 
    experimental=True,
3850
 
    )
3851
 
 
3852
 
format_registry.register_metadir('2a',
3853
 
    'bzrlib.repofmt.groupcompress_repo.RepositoryFormat2a',
3854
 
    help='First format for bzr 2.0 series.\n'
3855
 
        'Uses group-compress storage.\n'
3856
 
        'Provides rich roots which are a one-way transition.\n',
3857
 
        # 'storage in packs, 255-way hashed CHK inventory, bencode revision, group compress, '
3858
 
        # 'rich roots. Supported by bzr 1.16 and later.',
3859
 
    branch_format='bzrlib.branch.BzrBranchFormat7',
3860
 
    tree_format='bzrlib.workingtree.WorkingTreeFormat6',
3861
 
    experimental=True,
3862
 
    )
3863
 
 
3864
3469
# The following format should be an alias for the rich root equivalent 
3865
3470
# of the default format
3866
3471
format_registry.register_metadir('default-rich-root',
3867
 
    'bzrlib.repofmt.groupcompress_repo.RepositoryFormat2a',
3868
 
    branch_format='bzrlib.branch.BzrBranchFormat7',
3869
 
    tree_format='bzrlib.workingtree.WorkingTreeFormat6',
 
3472
    'bzrlib.repofmt.pack_repo.RepositoryFormatKnitPack4',
 
3473
    help='Default format, rich root variant. (needed for bzr-svn and bzr-git).',
 
3474
    branch_format='bzrlib.branch.BzrBranchFormat6',
 
3475
    tree_format='bzrlib.workingtree.WorkingTreeFormat4',
3870
3476
    alias=True,
3871
 
    help='Same as 2a.')
3872
 
 
 
3477
    )
3873
3478
# The current format that is made on 'bzr init'.
3874
 
format_registry.set_default('2a')
 
3479
format_registry.set_default('pack-0.92')