~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/bzrdir.py

  • Committer: Andrew Bennetts
  • Date: 2011-02-14 11:57:45 UTC
  • mto: This revision was merged to the branch mainline in revision 5664.
  • Revision ID: andrew.bennetts@canonical.com-20110214115745-gzb0nn2whvs4unp7
Preserve identity of default values in the pretty decorators.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
objects returned.
26
26
"""
27
27
 
 
28
# TODO: Move old formats into a plugin to make this file smaller.
 
29
 
 
30
import os
28
31
import sys
 
32
import warnings
29
33
 
30
34
from bzrlib.lazy_import import lazy_import
31
35
lazy_import(globals(), """
 
36
from stat import S_ISDIR
 
37
 
32
38
import bzrlib
33
39
from bzrlib import (
34
 
    branch as _mod_branch,
35
 
    cleanup,
 
40
    branch,
36
41
    config,
37
42
    controldir,
38
43
    errors,
39
 
    fetch,
40
44
    graph,
41
45
    lockable_files,
42
46
    lockdir,
48
52
    transport as _mod_transport,
49
53
    ui,
50
54
    urlutils,
 
55
    versionedfile,
51
56
    win32utils,
52
 
    workingtree_3,
 
57
    workingtree,
53
58
    workingtree_4,
54
 
    )
55
 
from bzrlib.repofmt import knitpack_repo
 
59
    xml4,
 
60
    xml5,
 
61
    )
 
62
from bzrlib.osutils import (
 
63
    sha_string,
 
64
    )
 
65
from bzrlib.push import (
 
66
    PushResult,
 
67
    )
 
68
from bzrlib.repofmt import pack_repo
 
69
from bzrlib.smart.client import _SmartClient
 
70
from bzrlib.store.versioned import VersionedFileStore
 
71
from bzrlib.transactions import WriteTransaction
56
72
from bzrlib.transport import (
57
73
    do_catching_redirections,
58
74
    local,
59
75
    )
 
76
from bzrlib.weave import (
 
77
    WeaveFile,
 
78
    Weave,
 
79
    )
60
80
""")
61
81
 
62
82
from bzrlib.trace import (
63
83
    mutter,
64
84
    note,
 
85
    warning,
65
86
    )
66
87
 
67
88
from bzrlib import (
68
89
    hooks,
69
90
    registry,
 
91
    symbol_versioning,
70
92
    )
71
93
from bzrlib.symbol_versioning import (
72
94
    deprecated_in,
123
145
            # No repo, no problem.
124
146
            pass
125
147
 
 
148
    @staticmethod
 
149
    def _check_supported(format, allow_unsupported,
 
150
        recommend_upgrade=True,
 
151
        basedir=None):
 
152
        """Give an error or warning on old formats.
 
153
 
 
154
        :param format: may be any kind of format - workingtree, branch,
 
155
        or repository.
 
156
 
 
157
        :param allow_unsupported: If true, allow opening
 
158
        formats that are strongly deprecated, and which may
 
159
        have limited functionality.
 
160
 
 
161
        :param recommend_upgrade: If true (default), warn
 
162
        the user through the ui object that they may wish
 
163
        to upgrade the object.
 
164
        """
 
165
        # TODO: perhaps move this into a base Format class; it's not BzrDir
 
166
        # specific. mbp 20070323
 
167
        if not allow_unsupported and not format.is_supported():
 
168
            # see open_downlevel to open legacy branches.
 
169
            raise errors.UnsupportedFormatError(format=format)
 
170
        if recommend_upgrade \
 
171
            and getattr(format, 'upgrade_recommended', False):
 
172
            ui.ui_factory.recommend_upgrade(
 
173
                format.get_format_description(),
 
174
                basedir)
 
175
 
126
176
    def clone_on_transport(self, transport, revision_id=None,
127
177
        force_new_repo=False, preserve_stacking=False, stacked_on=None,
128
178
        create_prefix=False, use_existing_dir=True, no_tree=False):
140
190
        :param create_prefix: Create any missing directories leading up to
141
191
            to_transport.
142
192
        :param use_existing_dir: Use an existing directory if one exists.
143
 
        :param no_tree: If set to true prevents creation of a working tree.
144
193
        """
145
194
        # Overview: put together a broad description of what we want to end up
146
195
        # with; then make as few api calls as possible to do it.
388
437
        policy = self.determine_repository_policy(force_new_repo)
389
438
        return policy.acquire_repository()[0]
390
439
 
391
 
    def _find_source_repo(self, add_cleanup, source_branch):
392
 
        """Find the source branch and repo for a sprout operation.
393
 
        
394
 
        This is helper intended for use by _sprout.
395
 
 
396
 
        :returns: (source_branch, source_repository).  Either or both may be
397
 
            None.  If not None, they will be read-locked (and their unlock(s)
398
 
            scheduled via the add_cleanup param).
399
 
        """
400
 
        if source_branch is not None:
401
 
            add_cleanup(source_branch.lock_read().unlock)
402
 
            return source_branch, source_branch.repository
403
 
        try:
404
 
            source_branch = self.open_branch()
405
 
            source_repository = source_branch.repository
406
 
        except errors.NotBranchError:
407
 
            source_branch = None
408
 
            try:
409
 
                source_repository = self.open_repository()
410
 
            except errors.NoRepositoryPresent:
411
 
                source_repository = None
412
 
            else:
413
 
                add_cleanup(source_repository.lock_read().unlock)
414
 
        else:
415
 
            add_cleanup(source_branch.lock_read().unlock)
416
 
        return source_branch, source_repository
417
 
 
418
 
    def sprout(self, url, revision_id=None, force_new_repo=False,
419
 
               recurse='down', possible_transports=None,
420
 
               accelerator_tree=None, hardlink=False, stacked=False,
421
 
               source_branch=None, create_tree_if_local=True):
422
 
        """Create a copy of this controldir prepared for use as a new line of
423
 
        development.
424
 
 
425
 
        If url's last component does not exist, it will be created.
426
 
 
427
 
        Attributes related to the identity of the source branch like
428
 
        branch nickname will be cleaned, a working tree is created
429
 
        whether one existed before or not; and a local branch is always
430
 
        created.
431
 
 
432
 
        if revision_id is not None, then the clone operation may tune
433
 
            itself to download less data.
434
 
        :param accelerator_tree: A tree which can be used for retrieving file
435
 
            contents more quickly than the revision tree, i.e. a workingtree.
436
 
            The revision tree will be used for cases where accelerator_tree's
437
 
            content is different.
438
 
        :param hardlink: If true, hard-link files from accelerator_tree,
439
 
            where possible.
440
 
        :param stacked: If true, create a stacked branch referring to the
441
 
            location of this control directory.
442
 
        :param create_tree_if_local: If true, a working-tree will be created
443
 
            when working locally.
444
 
        """
445
 
        operation = cleanup.OperationWithCleanups(self._sprout)
446
 
        return operation.run(url, revision_id=revision_id,
447
 
            force_new_repo=force_new_repo, recurse=recurse,
448
 
            possible_transports=possible_transports,
449
 
            accelerator_tree=accelerator_tree, hardlink=hardlink,
450
 
            stacked=stacked, source_branch=source_branch,
451
 
            create_tree_if_local=create_tree_if_local)
452
 
 
453
 
    def _sprout(self, op, url, revision_id=None, force_new_repo=False,
454
 
               recurse='down', possible_transports=None,
455
 
               accelerator_tree=None, hardlink=False, stacked=False,
456
 
               source_branch=None, create_tree_if_local=True):
457
 
        add_cleanup = op.add_cleanup
458
 
        fetch_spec_factory = fetch.FetchSpecFactory()
459
 
        if revision_id is not None:
460
 
            fetch_spec_factory.add_revision_ids([revision_id])
461
 
            fetch_spec_factory.source_branch_stop_revision_id = revision_id
462
 
        target_transport = _mod_transport.get_transport(url,
463
 
            possible_transports)
464
 
        target_transport.ensure_base()
465
 
        cloning_format = self.cloning_metadir(stacked)
466
 
        # Create/update the result branch
467
 
        result = cloning_format.initialize_on_transport(target_transport)
468
 
        source_branch, source_repository = self._find_source_repo(
469
 
            add_cleanup, source_branch)
470
 
        fetch_spec_factory.source_branch = source_branch
471
 
        # if a stacked branch wasn't requested, we don't create one
472
 
        # even if the origin was stacked
473
 
        if stacked and source_branch is not None:
474
 
            stacked_branch_url = self.root_transport.base
475
 
        else:
476
 
            stacked_branch_url = None
477
 
        repository_policy = result.determine_repository_policy(
478
 
            force_new_repo, stacked_branch_url, require_stacking=stacked)
479
 
        result_repo, is_new_repo = repository_policy.acquire_repository()
480
 
        add_cleanup(result_repo.lock_write().unlock)
481
 
        fetch_spec_factory.source_repo = source_repository
482
 
        fetch_spec_factory.target_repo = result_repo
483
 
        if stacked or (len(result_repo._fallback_repositories) != 0):
484
 
            target_repo_kind = fetch.TargetRepoKinds.STACKED
485
 
        elif is_new_repo:
486
 
            target_repo_kind = fetch.TargetRepoKinds.EMPTY
487
 
        else:
488
 
            target_repo_kind = fetch.TargetRepoKinds.PREEXISTING
489
 
        fetch_spec_factory.target_repo_kind = target_repo_kind
490
 
        if source_repository is not None:
491
 
            fetch_spec = fetch_spec_factory.make_fetch_spec()
492
 
            result_repo.fetch(source_repository, fetch_spec=fetch_spec)
493
 
 
494
 
        if source_branch is None:
495
 
            # this is for sprouting a controldir without a branch; is that
496
 
            # actually useful?
497
 
            # Not especially, but it's part of the contract.
498
 
            result_branch = result.create_branch()
499
 
        else:
500
 
            result_branch = source_branch.sprout(result,
501
 
                revision_id=revision_id, repository_policy=repository_policy,
502
 
                repository=result_repo)
503
 
        mutter("created new branch %r" % (result_branch,))
504
 
 
505
 
        # Create/update the result working tree
506
 
        if (create_tree_if_local and
507
 
            isinstance(target_transport, local.LocalTransport) and
508
 
            (result_repo is None or result_repo.make_working_trees())):
509
 
            wt = result.create_workingtree(accelerator_tree=accelerator_tree,
510
 
                hardlink=hardlink, from_branch=result_branch)
511
 
            wt.lock_write()
512
 
            try:
513
 
                if wt.path2id('') is None:
514
 
                    try:
515
 
                        wt.set_root_id(self.open_workingtree.get_root_id())
516
 
                    except errors.NoWorkingTree:
517
 
                        pass
518
 
            finally:
519
 
                wt.unlock()
520
 
        else:
521
 
            wt = None
522
 
        if recurse == 'down':
523
 
            basis = None
524
 
            if wt is not None:
525
 
                basis = wt.basis_tree()
526
 
            elif result_branch is not None:
527
 
                basis = result_branch.basis_tree()
528
 
            elif source_branch is not None:
529
 
                basis = source_branch.basis_tree()
530
 
            if basis is not None:
531
 
                add_cleanup(basis.lock_read().unlock)
532
 
                subtrees = basis.iter_references()
533
 
            else:
534
 
                subtrees = []
535
 
            for path, file_id in subtrees:
536
 
                target = urlutils.join(url, urlutils.escape(path))
537
 
                sublocation = source_branch.reference_parent(file_id, path)
538
 
                sublocation.bzrdir.sprout(target,
539
 
                    basis.get_reference_revision(file_id, path),
540
 
                    force_new_repo=force_new_repo, recurse=recurse,
541
 
                    stacked=stacked)
542
 
        return result
543
 
 
544
 
 
545
 
 
546
440
    @staticmethod
547
441
    def create_branch_convenience(base, force_new_repo=False,
548
442
                                  force_new_tree=None, format=None,
857
751
        except errors.TooManyRedirections:
858
752
            raise errors.NotBranchError(base)
859
753
 
860
 
        format.check_support_status(_unsupported)
 
754
        BzrDir._check_supported(format, _unsupported)
861
755
        return format.open(transport, _found=True)
862
756
 
863
757
    @staticmethod
1035
929
            format = controldir.ControlDirFormat.get_default_format()
1036
930
        return format.initialize_on_transport(t)
1037
931
 
1038
 
    def get_branch_transport(self, branch_format, name=None):
1039
 
        """Get the transport for use by branch format in this BzrDir.
1040
 
 
1041
 
        Note that bzr dirs that do not support format strings will raise
1042
 
        IncompatibleFormat if the branch format they are given has
1043
 
        a format string, and vice versa.
1044
 
 
1045
 
        If branch_format is None, the transport is returned with no
1046
 
        checking. If it is not None, then the returned transport is
1047
 
        guaranteed to point to an existing directory ready for use.
1048
 
        """
1049
 
        raise NotImplementedError(self.get_branch_transport)
1050
 
 
1051
 
    def get_repository_transport(self, repository_format):
1052
 
        """Get the transport for use by repository format in this BzrDir.
1053
 
 
1054
 
        Note that bzr dirs that do not support format strings will raise
1055
 
        IncompatibleFormat if the repository format they are given has
1056
 
        a format string, and vice versa.
1057
 
 
1058
 
        If repository_format is None, the transport is returned with no
1059
 
        checking. If it is not None, then the returned transport is
1060
 
        guaranteed to point to an existing directory ready for use.
1061
 
        """
1062
 
        raise NotImplementedError(self.get_repository_transport)
1063
 
 
1064
 
    def get_workingtree_transport(self, tree_format):
1065
 
        """Get the transport for use by workingtree format in this BzrDir.
1066
 
 
1067
 
        Note that bzr dirs that do not support format strings will raise
1068
 
        IncompatibleFormat if the workingtree format they are given has a
1069
 
        format string, and vice versa.
1070
 
 
1071
 
        If workingtree_format is None, the transport is returned with no
1072
 
        checking. If it is not None, then the returned transport is
1073
 
        guaranteed to point to an existing directory ready for use.
1074
 
        """
1075
 
        raise NotImplementedError(self.get_workingtree_transport)
1076
 
 
1077
932
 
1078
933
class BzrDirHooks(hooks.Hooks):
1079
934
    """Hooks for BzrDir operations."""
1080
935
 
1081
936
    def __init__(self):
1082
937
        """Create the default hooks."""
1083
 
        hooks.Hooks.__init__(self, "bzrlib.bzrdir", "BzrDir.hooks")
1084
 
        self.add_hook('pre_open',
 
938
        hooks.Hooks.__init__(self)
 
939
        self.create_hook(hooks.HookPoint('pre_open',
1085
940
            "Invoked before attempting to open a BzrDir with the transport "
1086
 
            "that the open will use.", (1, 14))
1087
 
        self.add_hook('post_repo_init',
 
941
            "that the open will use.", (1, 14), None))
 
942
        self.create_hook(hooks.HookPoint('post_repo_init',
1088
943
            "Invoked after a repository has been initialized. "
1089
944
            "post_repo_init is called with a "
1090
945
            "bzrlib.bzrdir.RepoInitHookParams.",
1091
 
            (2, 2))
 
946
            (2, 2), None))
1092
947
 
1093
948
# install the default hooks
1094
949
BzrDir.hooks = BzrDirHooks()
1130
985
                self.bzrdir)
1131
986
 
1132
987
 
 
988
class BzrDirPreSplitOut(BzrDir):
 
989
    """A common class for the all-in-one formats."""
 
990
 
 
991
    def __init__(self, _transport, _format):
 
992
        """See BzrDir.__init__."""
 
993
        super(BzrDirPreSplitOut, self).__init__(_transport, _format)
 
994
        self._control_files = lockable_files.LockableFiles(
 
995
                                            self.get_branch_transport(None),
 
996
                                            self._format._lock_file_name,
 
997
                                            self._format._lock_class)
 
998
 
 
999
    def break_lock(self):
 
1000
        """Pre-splitout bzrdirs do not suffer from stale locks."""
 
1001
        raise NotImplementedError(self.break_lock)
 
1002
 
 
1003
    def cloning_metadir(self, require_stacking=False):
 
1004
        """Produce a metadir suitable for cloning with."""
 
1005
        if require_stacking:
 
1006
            return controldir.format_registry.make_bzrdir('1.6')
 
1007
        return self._format.__class__()
 
1008
 
 
1009
    def clone(self, url, revision_id=None, force_new_repo=False,
 
1010
              preserve_stacking=False):
 
1011
        """See BzrDir.clone().
 
1012
 
 
1013
        force_new_repo has no effect, since this family of formats always
 
1014
        require a new repository.
 
1015
        preserve_stacking has no effect, since no source branch using this
 
1016
        family of formats can be stacked, so there is no stacking to preserve.
 
1017
        """
 
1018
        self._make_tail(url)
 
1019
        result = self._format._initialize_for_clone(url)
 
1020
        self.open_repository().clone(result, revision_id=revision_id)
 
1021
        from_branch = self.open_branch()
 
1022
        from_branch.clone(result, revision_id=revision_id)
 
1023
        try:
 
1024
            tree = self.open_workingtree()
 
1025
        except errors.NotLocalUrl:
 
1026
            # make a new one, this format always has to have one.
 
1027
            result._init_workingtree()
 
1028
        else:
 
1029
            tree.clone(result)
 
1030
        return result
 
1031
 
 
1032
    def create_branch(self, name=None, repository=None):
 
1033
        """See BzrDir.create_branch."""
 
1034
        if repository is not None:
 
1035
            raise NotImplementedError(
 
1036
                "create_branch(repository=<not None>) on %r" % (self,))
 
1037
        return self._format.get_branch_format().initialize(self, name=name)
 
1038
 
 
1039
    def destroy_branch(self, name=None):
 
1040
        """See BzrDir.destroy_branch."""
 
1041
        raise errors.UnsupportedOperation(self.destroy_branch, self)
 
1042
 
 
1043
    def create_repository(self, shared=False):
 
1044
        """See BzrDir.create_repository."""
 
1045
        if shared:
 
1046
            raise errors.IncompatibleFormat('shared repository', self._format)
 
1047
        return self.open_repository()
 
1048
 
 
1049
    def destroy_repository(self):
 
1050
        """See BzrDir.destroy_repository."""
 
1051
        raise errors.UnsupportedOperation(self.destroy_repository, self)
 
1052
 
 
1053
    def create_workingtree(self, revision_id=None, from_branch=None,
 
1054
                           accelerator_tree=None, hardlink=False):
 
1055
        """See BzrDir.create_workingtree."""
 
1056
        # The workingtree is sometimes created when the bzrdir is created,
 
1057
        # but not when cloning.
 
1058
 
 
1059
        # this looks buggy but is not -really-
 
1060
        # because this format creates the workingtree when the bzrdir is
 
1061
        # created
 
1062
        # clone and sprout will have set the revision_id
 
1063
        # and that will have set it for us, its only
 
1064
        # specific uses of create_workingtree in isolation
 
1065
        # that can do wonky stuff here, and that only
 
1066
        # happens for creating checkouts, which cannot be
 
1067
        # done on this format anyway. So - acceptable wart.
 
1068
        if hardlink:
 
1069
            warning("can't support hardlinked working trees in %r"
 
1070
                % (self,))
 
1071
        try:
 
1072
            result = self.open_workingtree(recommend_upgrade=False)
 
1073
        except errors.NoSuchFile:
 
1074
            result = self._init_workingtree()
 
1075
        if revision_id is not None:
 
1076
            if revision_id == _mod_revision.NULL_REVISION:
 
1077
                result.set_parent_ids([])
 
1078
            else:
 
1079
                result.set_parent_ids([revision_id])
 
1080
        return result
 
1081
 
 
1082
    def _init_workingtree(self):
 
1083
        from bzrlib.workingtree import WorkingTreeFormat2
 
1084
        try:
 
1085
            return WorkingTreeFormat2().initialize(self)
 
1086
        except errors.NotLocalUrl:
 
1087
            # Even though we can't access the working tree, we need to
 
1088
            # create its control files.
 
1089
            return WorkingTreeFormat2()._stub_initialize_on_transport(
 
1090
                self.transport, self._control_files._file_mode)
 
1091
 
 
1092
    def destroy_workingtree(self):
 
1093
        """See BzrDir.destroy_workingtree."""
 
1094
        raise errors.UnsupportedOperation(self.destroy_workingtree, self)
 
1095
 
 
1096
    def destroy_workingtree_metadata(self):
 
1097
        """See BzrDir.destroy_workingtree_metadata."""
 
1098
        raise errors.UnsupportedOperation(self.destroy_workingtree_metadata,
 
1099
                                          self)
 
1100
 
 
1101
    def get_branch_transport(self, branch_format, name=None):
 
1102
        """See BzrDir.get_branch_transport()."""
 
1103
        if name is not None:
 
1104
            raise errors.NoColocatedBranchSupport(self)
 
1105
        if branch_format is None:
 
1106
            return self.transport
 
1107
        try:
 
1108
            branch_format.get_format_string()
 
1109
        except NotImplementedError:
 
1110
            return self.transport
 
1111
        raise errors.IncompatibleFormat(branch_format, self._format)
 
1112
 
 
1113
    def get_repository_transport(self, repository_format):
 
1114
        """See BzrDir.get_repository_transport()."""
 
1115
        if repository_format is None:
 
1116
            return self.transport
 
1117
        try:
 
1118
            repository_format.get_format_string()
 
1119
        except NotImplementedError:
 
1120
            return self.transport
 
1121
        raise errors.IncompatibleFormat(repository_format, self._format)
 
1122
 
 
1123
    def get_workingtree_transport(self, workingtree_format):
 
1124
        """See BzrDir.get_workingtree_transport()."""
 
1125
        if workingtree_format is None:
 
1126
            return self.transport
 
1127
        try:
 
1128
            workingtree_format.get_format_string()
 
1129
        except NotImplementedError:
 
1130
            return self.transport
 
1131
        raise errors.IncompatibleFormat(workingtree_format, self._format)
 
1132
 
 
1133
    def needs_format_conversion(self, format=None):
 
1134
        """See BzrDir.needs_format_conversion()."""
 
1135
        # if the format is not the same as the system default,
 
1136
        # an upgrade is needed.
 
1137
        if format is None:
 
1138
            symbol_versioning.warn(symbol_versioning.deprecated_in((1, 13, 0))
 
1139
                % 'needs_format_conversion(format=None)')
 
1140
            format = BzrDirFormat.get_default_format()
 
1141
        return not isinstance(self._format, format.__class__)
 
1142
 
 
1143
    def open_branch(self, name=None, unsupported=False,
 
1144
                    ignore_fallbacks=False):
 
1145
        """See BzrDir.open_branch."""
 
1146
        from bzrlib.branch import BzrBranchFormat4
 
1147
        format = BzrBranchFormat4()
 
1148
        self._check_supported(format, unsupported)
 
1149
        return format.open(self, name, _found=True)
 
1150
 
 
1151
    def sprout(self, url, revision_id=None, force_new_repo=False,
 
1152
               possible_transports=None, accelerator_tree=None,
 
1153
               hardlink=False, stacked=False, create_tree_if_local=True,
 
1154
               source_branch=None):
 
1155
        """See BzrDir.sprout()."""
 
1156
        if source_branch is not None:
 
1157
            my_branch = self.open_branch()
 
1158
            if source_branch.base != my_branch.base:
 
1159
                raise AssertionError(
 
1160
                    "source branch %r is not within %r with branch %r" %
 
1161
                    (source_branch, self, my_branch))
 
1162
        if stacked:
 
1163
            raise errors.UnstackableBranchFormat(
 
1164
                self._format, self.root_transport.base)
 
1165
        if not create_tree_if_local:
 
1166
            raise errors.MustHaveWorkingTree(
 
1167
                self._format, self.root_transport.base)
 
1168
        from bzrlib.workingtree import WorkingTreeFormat2
 
1169
        self._make_tail(url)
 
1170
        result = self._format._initialize_for_clone(url)
 
1171
        try:
 
1172
            self.open_repository().clone(result, revision_id=revision_id)
 
1173
        except errors.NoRepositoryPresent:
 
1174
            pass
 
1175
        try:
 
1176
            self.open_branch().sprout(result, revision_id=revision_id)
 
1177
        except errors.NotBranchError:
 
1178
            pass
 
1179
 
 
1180
        # we always want a working tree
 
1181
        WorkingTreeFormat2().initialize(result,
 
1182
                                        accelerator_tree=accelerator_tree,
 
1183
                                        hardlink=hardlink)
 
1184
        return result
 
1185
 
 
1186
 
 
1187
class BzrDir4(BzrDirPreSplitOut):
 
1188
    """A .bzr version 4 control object.
 
1189
 
 
1190
    This is a deprecated format and may be removed after sept 2006.
 
1191
    """
 
1192
 
 
1193
    def create_repository(self, shared=False):
 
1194
        """See BzrDir.create_repository."""
 
1195
        return self._format.repository_format.initialize(self, shared)
 
1196
 
 
1197
    def needs_format_conversion(self, format=None):
 
1198
        """Format 4 dirs are always in need of conversion."""
 
1199
        if format is None:
 
1200
            symbol_versioning.warn(symbol_versioning.deprecated_in((1, 13, 0))
 
1201
                % 'needs_format_conversion(format=None)')
 
1202
        return True
 
1203
 
 
1204
    def open_repository(self):
 
1205
        """See BzrDir.open_repository."""
 
1206
        from bzrlib.repofmt.weaverepo import RepositoryFormat4
 
1207
        return RepositoryFormat4().open(self, _found=True)
 
1208
 
 
1209
 
 
1210
class BzrDir5(BzrDirPreSplitOut):
 
1211
    """A .bzr version 5 control object.
 
1212
 
 
1213
    This is a deprecated format and may be removed after sept 2006.
 
1214
    """
 
1215
 
 
1216
    def has_workingtree(self):
 
1217
        """See BzrDir.has_workingtree."""
 
1218
        return True
 
1219
    
 
1220
    def open_repository(self):
 
1221
        """See BzrDir.open_repository."""
 
1222
        from bzrlib.repofmt.weaverepo import RepositoryFormat5
 
1223
        return RepositoryFormat5().open(self, _found=True)
 
1224
 
 
1225
    def open_workingtree(self, _unsupported=False,
 
1226
            recommend_upgrade=True):
 
1227
        """See BzrDir.create_workingtree."""
 
1228
        from bzrlib.workingtree import WorkingTreeFormat2
 
1229
        wt_format = WorkingTreeFormat2()
 
1230
        # we don't warn here about upgrades; that ought to be handled for the
 
1231
        # bzrdir as a whole
 
1232
        return wt_format.open(self, _found=True)
 
1233
 
 
1234
 
 
1235
class BzrDir6(BzrDirPreSplitOut):
 
1236
    """A .bzr version 6 control object.
 
1237
 
 
1238
    This is a deprecated format and may be removed after sept 2006.
 
1239
    """
 
1240
 
 
1241
    def has_workingtree(self):
 
1242
        """See BzrDir.has_workingtree."""
 
1243
        return True
 
1244
    
 
1245
    def open_repository(self):
 
1246
        """See BzrDir.open_repository."""
 
1247
        from bzrlib.repofmt.weaverepo import RepositoryFormat6
 
1248
        return RepositoryFormat6().open(self, _found=True)
 
1249
 
 
1250
    def open_workingtree(self, _unsupported=False,
 
1251
        recommend_upgrade=True):
 
1252
        """See BzrDir.create_workingtree."""
 
1253
        # we don't warn here about upgrades; that ought to be handled for the
 
1254
        # bzrdir as a whole
 
1255
        from bzrlib.workingtree import WorkingTreeFormat2
 
1256
        return WorkingTreeFormat2().open(self, _found=True)
 
1257
 
 
1258
 
1133
1259
class BzrDirMeta1(BzrDir):
1134
1260
    """A .bzr meta version 1 control object.
1135
1261
 
1252
1378
    def has_workingtree(self):
1253
1379
        """Tell if this bzrdir contains a working tree.
1254
1380
 
 
1381
        This will still raise an exception if the bzrdir has a workingtree that
 
1382
        is remote & inaccessible.
 
1383
 
1255
1384
        Note: if you're going to open the working tree, you should just go
1256
1385
        ahead and try, and not ask permission first.
1257
1386
        """
1258
1387
        from bzrlib.workingtree import WorkingTreeFormat
1259
1388
        try:
1260
 
            WorkingTreeFormat.find_format_string(self)
 
1389
            WorkingTreeFormat.find_format(self)
1261
1390
        except errors.NoWorkingTree:
1262
1391
            return False
1263
1392
        return True
1264
1393
 
1265
 
    def needs_format_conversion(self, format):
 
1394
    def needs_format_conversion(self, format=None):
1266
1395
        """See BzrDir.needs_format_conversion()."""
 
1396
        if format is None:
 
1397
            symbol_versioning.warn(symbol_versioning.deprecated_in((1, 13, 0))
 
1398
                % 'needs_format_conversion(format=None)')
 
1399
        if format is None:
 
1400
            format = BzrDirFormat.get_default_format()
1267
1401
        if not isinstance(self._format, format.__class__):
1268
1402
            # it is not a meta dir format, conversion is needed.
1269
1403
            return True
1294
1428
                    ignore_fallbacks=False):
1295
1429
        """See BzrDir.open_branch."""
1296
1430
        format = self.find_branch_format(name=name)
1297
 
        format.check_support_status(unsupported)
 
1431
        self._check_supported(format, unsupported)
1298
1432
        return format.open(self, name=name,
1299
1433
            _found=True, ignore_fallbacks=ignore_fallbacks)
1300
1434
 
1302
1436
        """See BzrDir.open_repository."""
1303
1437
        from bzrlib.repository import RepositoryFormat
1304
1438
        format = RepositoryFormat.find_format(self)
1305
 
        format.check_support_status(unsupported)
 
1439
        self._check_supported(format, unsupported)
1306
1440
        return format.open(self, _found=True)
1307
1441
 
1308
1442
    def open_workingtree(self, unsupported=False,
1310
1444
        """See BzrDir.open_workingtree."""
1311
1445
        from bzrlib.workingtree import WorkingTreeFormat
1312
1446
        format = WorkingTreeFormat.find_format(self)
1313
 
        format.check_support_status(unsupported, recommend_upgrade,
 
1447
        self._check_supported(format, unsupported,
 
1448
            recommend_upgrade,
1314
1449
            basedir=self.root_transport.base)
1315
1450
        return format.open(self, _found=True)
1316
1451
 
1321
1456
class BzrProber(controldir.Prober):
1322
1457
    """Prober for formats that use a .bzr/ control directory."""
1323
1458
 
1324
 
    formats = registry.FormatRegistry(controldir.network_format_registry)
 
1459
    _formats = {}
1325
1460
    """The known .bzr formats."""
1326
1461
 
1327
1462
    @classmethod
1328
 
    @deprecated_method(deprecated_in((2, 4, 0)))
1329
1463
    def register_bzrdir_format(klass, format):
1330
 
        klass.formats.register(format.get_format_string(), format)
 
1464
        klass._formats[format.get_format_string()] = format
1331
1465
 
1332
1466
    @classmethod
1333
 
    @deprecated_method(deprecated_in((2, 4, 0)))
1334
1467
    def unregister_bzrdir_format(klass, format):
1335
 
        klass.formats.remove(format.get_format_string())
 
1468
        del klass._formats[format.get_format_string()]
1336
1469
 
1337
1470
    @classmethod
1338
1471
    def probe_transport(klass, transport):
1342
1475
        except errors.NoSuchFile:
1343
1476
            raise errors.NotBranchError(path=transport.base)
1344
1477
        try:
1345
 
            return klass.formats.get(format_string)
 
1478
            return klass._formats[format_string]
1346
1479
        except KeyError:
1347
1480
            raise errors.UnknownFormatError(format=format_string, kind='bzrdir')
1348
1481
 
1349
 
    @classmethod
1350
 
    def known_formats(cls):
1351
 
        result = set()
1352
 
        for name, format in cls.formats.iteritems():
1353
 
            if callable(format):
1354
 
                format = format()
1355
 
            result.add(format)
1356
 
        return result
1357
 
 
1358
1482
 
1359
1483
controldir.ControlDirFormat.register_prober(BzrProber)
1360
1484
 
1384
1508
                    raise errors.NotBranchError(path=transport.base)
1385
1509
                if server_version != '2':
1386
1510
                    raise errors.NotBranchError(path=transport.base)
1387
 
            from bzrlib.remote import RemoteBzrDirFormat
1388
1511
            return RemoteBzrDirFormat()
1389
1512
 
1390
 
    @classmethod
1391
 
    def known_formats(cls):
1392
 
        from bzrlib.remote import RemoteBzrDirFormat
1393
 
        return set([RemoteBzrDirFormat()])
1394
 
 
1395
1513
 
1396
1514
class BzrDirFormat(controldir.ControlDirFormat):
1397
1515
    """ControlDirFormat base class for .bzr/ directories.
1410
1528
    # _lock_class must be set in subclasses to the lock type, typ.
1411
1529
    # TransportLock or LockDir
1412
1530
 
1413
 
    @classmethod
1414
 
    def get_format_string(cls):
 
1531
    def get_format_string(self):
1415
1532
        """Return the ASCII format string that identifies this format."""
1416
1533
        raise NotImplementedError(self.get_format_string)
1417
1534
 
1429
1546
            # metadir1
1430
1547
            if type(self) != BzrDirMetaFormat1:
1431
1548
                return self._initialize_on_transport_vfs(transport)
1432
 
            from bzrlib.remote import RemoteBzrDirFormat
1433
1549
            remote_format = RemoteBzrDirFormat()
1434
1550
            self._supply_sub_formats_to(remote_format)
1435
1551
            return remote_format.initialize_on_transport(transport)
1473
1589
            except errors.NoSmartMedium:
1474
1590
                pass
1475
1591
            else:
1476
 
                from bzrlib.remote import RemoteBzrDirFormat
1477
1592
                # TODO: lookup the local format from a server hint.
1478
1593
                remote_dir_format = RemoteBzrDirFormat()
1479
1594
                remote_dir_format._network_name = self.network_name()
1594
1709
        """
1595
1710
        raise NotImplementedError(self._open)
1596
1711
 
 
1712
    @classmethod
 
1713
    def register_format(klass, format):
 
1714
        BzrProber.register_bzrdir_format(format)
 
1715
        # bzr native formats have a network name of their format string.
 
1716
        controldir.network_format_registry.register(format.get_format_string(), format.__class__)
 
1717
        controldir.ControlDirFormat.register_format(format)
 
1718
 
1597
1719
    def _supply_sub_formats_to(self, other_format):
1598
1720
        """Give other_format the same values for sub formats as this has.
1599
1721
 
1606
1728
        :return: None.
1607
1729
        """
1608
1730
 
 
1731
    @classmethod
 
1732
    def unregister_format(klass, format):
 
1733
        BzrProber.unregister_bzrdir_format(format)
 
1734
        controldir.ControlDirFormat.unregister_format(format)
 
1735
        controldir.network_format_registry.remove(format.get_format_string())
 
1736
 
 
1737
 
 
1738
class BzrDirFormat4(BzrDirFormat):
 
1739
    """Bzr dir format 4.
 
1740
 
 
1741
    This format is a combined format for working tree, branch and repository.
 
1742
    It has:
 
1743
     - Format 1 working trees [always]
 
1744
     - Format 4 branches [always]
 
1745
     - Format 4 repositories [always]
 
1746
 
 
1747
    This format is deprecated: it indexes texts using a text it which is
 
1748
    removed in format 5; write support for this format has been removed.
 
1749
    """
 
1750
 
 
1751
    _lock_class = lockable_files.TransportLock
 
1752
 
 
1753
    def get_format_string(self):
 
1754
        """See BzrDirFormat.get_format_string()."""
 
1755
        return "Bazaar-NG branch, format 0.0.4\n"
 
1756
 
 
1757
    def get_format_description(self):
 
1758
        """See BzrDirFormat.get_format_description()."""
 
1759
        return "All-in-one format 4"
 
1760
 
 
1761
    def get_converter(self, format=None):
 
1762
        """See BzrDirFormat.get_converter()."""
 
1763
        # there is one and only one upgrade path here.
 
1764
        return ConvertBzrDir4To5()
 
1765
 
 
1766
    def initialize_on_transport(self, transport):
 
1767
        """Format 4 branches cannot be created."""
 
1768
        raise errors.UninitializableFormat(self)
 
1769
 
 
1770
    def is_supported(self):
 
1771
        """Format 4 is not supported.
 
1772
 
 
1773
        It is not supported because the model changed from 4 to 5 and the
 
1774
        conversion logic is expensive - so doing it on the fly was not
 
1775
        feasible.
 
1776
        """
 
1777
        return False
 
1778
 
 
1779
    def network_name(self):
 
1780
        return self.get_format_string()
 
1781
 
 
1782
    def _open(self, transport):
 
1783
        """See BzrDirFormat._open."""
 
1784
        return BzrDir4(transport, self)
 
1785
 
 
1786
    def __return_repository_format(self):
 
1787
        """Circular import protection."""
 
1788
        from bzrlib.repofmt.weaverepo import RepositoryFormat4
 
1789
        return RepositoryFormat4()
 
1790
    repository_format = property(__return_repository_format)
 
1791
 
 
1792
 
 
1793
class BzrDirFormatAllInOne(BzrDirFormat):
 
1794
    """Common class for formats before meta-dirs."""
 
1795
 
 
1796
    def initialize_on_transport_ex(self, transport, use_existing_dir=False,
 
1797
        create_prefix=False, force_new_repo=False, stacked_on=None,
 
1798
        stack_on_pwd=None, repo_format_name=None, make_working_trees=None,
 
1799
        shared_repo=False):
 
1800
        """See BzrDirFormat.initialize_on_transport_ex."""
 
1801
        require_stacking = (stacked_on is not None)
 
1802
        # Format 5 cannot stack, but we've been asked to - actually init
 
1803
        # a Meta1Dir
 
1804
        if require_stacking:
 
1805
            format = BzrDirMetaFormat1()
 
1806
            return format.initialize_on_transport_ex(transport,
 
1807
                use_existing_dir=use_existing_dir, create_prefix=create_prefix,
 
1808
                force_new_repo=force_new_repo, stacked_on=stacked_on,
 
1809
                stack_on_pwd=stack_on_pwd, repo_format_name=repo_format_name,
 
1810
                make_working_trees=make_working_trees, shared_repo=shared_repo)
 
1811
        return BzrDirFormat.initialize_on_transport_ex(self, transport,
 
1812
            use_existing_dir=use_existing_dir, create_prefix=create_prefix,
 
1813
            force_new_repo=force_new_repo, stacked_on=stacked_on,
 
1814
            stack_on_pwd=stack_on_pwd, repo_format_name=repo_format_name,
 
1815
            make_working_trees=make_working_trees, shared_repo=shared_repo)
 
1816
 
 
1817
 
 
1818
class BzrDirFormat5(BzrDirFormatAllInOne):
 
1819
    """Bzr control format 5.
 
1820
 
 
1821
    This format is a combined format for working tree, branch and repository.
 
1822
    It has:
 
1823
     - Format 2 working trees [always]
 
1824
     - Format 4 branches [always]
 
1825
     - Format 5 repositories [always]
 
1826
       Unhashed stores in the repository.
 
1827
    """
 
1828
 
 
1829
    _lock_class = lockable_files.TransportLock
 
1830
 
 
1831
    def get_format_string(self):
 
1832
        """See BzrDirFormat.get_format_string()."""
 
1833
        return "Bazaar-NG branch, format 5\n"
 
1834
 
 
1835
    def get_branch_format(self):
 
1836
        from bzrlib import branch
 
1837
        return branch.BzrBranchFormat4()
 
1838
 
 
1839
    def get_format_description(self):
 
1840
        """See BzrDirFormat.get_format_description()."""
 
1841
        return "All-in-one format 5"
 
1842
 
 
1843
    def get_converter(self, format=None):
 
1844
        """See BzrDirFormat.get_converter()."""
 
1845
        # there is one and only one upgrade path here.
 
1846
        return ConvertBzrDir5To6()
 
1847
 
 
1848
    def _initialize_for_clone(self, url):
 
1849
        return self.initialize_on_transport(
 
1850
            _mod_transport.get_transport(url), _cloning=True)
 
1851
 
 
1852
    def initialize_on_transport(self, transport, _cloning=False):
 
1853
        """Format 5 dirs always have working tree, branch and repository.
 
1854
 
 
1855
        Except when they are being cloned.
 
1856
        """
 
1857
        from bzrlib.branch import BzrBranchFormat4
 
1858
        from bzrlib.repofmt.weaverepo import RepositoryFormat5
 
1859
        result = (super(BzrDirFormat5, self).initialize_on_transport(transport))
 
1860
        RepositoryFormat5().initialize(result, _internal=True)
 
1861
        if not _cloning:
 
1862
            branch = BzrBranchFormat4().initialize(result)
 
1863
            result._init_workingtree()
 
1864
        return result
 
1865
 
 
1866
    def network_name(self):
 
1867
        return self.get_format_string()
 
1868
 
 
1869
    def _open(self, transport):
 
1870
        """See BzrDirFormat._open."""
 
1871
        return BzrDir5(transport, self)
 
1872
 
 
1873
    def __return_repository_format(self):
 
1874
        """Circular import protection."""
 
1875
        from bzrlib.repofmt.weaverepo import RepositoryFormat5
 
1876
        return RepositoryFormat5()
 
1877
    repository_format = property(__return_repository_format)
 
1878
 
 
1879
 
 
1880
class BzrDirFormat6(BzrDirFormatAllInOne):
 
1881
    """Bzr control format 6.
 
1882
 
 
1883
    This format is a combined format for working tree, branch and repository.
 
1884
    It has:
 
1885
     - Format 2 working trees [always]
 
1886
     - Format 4 branches [always]
 
1887
     - Format 6 repositories [always]
 
1888
    """
 
1889
 
 
1890
    _lock_class = lockable_files.TransportLock
 
1891
 
 
1892
    def get_format_string(self):
 
1893
        """See BzrDirFormat.get_format_string()."""
 
1894
        return "Bazaar-NG branch, format 6\n"
 
1895
 
 
1896
    def get_format_description(self):
 
1897
        """See BzrDirFormat.get_format_description()."""
 
1898
        return "All-in-one format 6"
 
1899
 
 
1900
    def get_branch_format(self):
 
1901
        from bzrlib import branch
 
1902
        return branch.BzrBranchFormat4()
 
1903
 
 
1904
    def get_converter(self, format=None):
 
1905
        """See BzrDirFormat.get_converter()."""
 
1906
        # there is one and only one upgrade path here.
 
1907
        return ConvertBzrDir6ToMeta()
 
1908
 
 
1909
    def _initialize_for_clone(self, url):
 
1910
        return self.initialize_on_transport(
 
1911
            _mod_transport.get_transport(url), _cloning=True)
 
1912
 
 
1913
    def initialize_on_transport(self, transport, _cloning=False):
 
1914
        """Format 6 dirs always have working tree, branch and repository.
 
1915
 
 
1916
        Except when they are being cloned.
 
1917
        """
 
1918
        from bzrlib.branch import BzrBranchFormat4
 
1919
        from bzrlib.repofmt.weaverepo import RepositoryFormat6
 
1920
        result = super(BzrDirFormat6, self).initialize_on_transport(transport)
 
1921
        RepositoryFormat6().initialize(result, _internal=True)
 
1922
        if not _cloning:
 
1923
            branch = BzrBranchFormat4().initialize(result)
 
1924
            result._init_workingtree()
 
1925
        return result
 
1926
 
 
1927
    def network_name(self):
 
1928
        return self.get_format_string()
 
1929
 
 
1930
    def _open(self, transport):
 
1931
        """See BzrDirFormat._open."""
 
1932
        return BzrDir6(transport, self)
 
1933
 
 
1934
    def __return_repository_format(self):
 
1935
        """Circular import protection."""
 
1936
        from bzrlib.repofmt.weaverepo import RepositoryFormat6
 
1937
        return RepositoryFormat6()
 
1938
    repository_format = property(__return_repository_format)
 
1939
 
1609
1940
 
1610
1941
class BzrDirMetaFormat1(BzrDirFormat):
1611
1942
    """Bzr meta control format 1
1620
1951
 
1621
1952
    _lock_class = lockdir.LockDir
1622
1953
 
1623
 
    fixed_components = False
1624
 
 
1625
1954
    def __init__(self):
1626
1955
        self._workingtree_format = None
1627
1956
        self._branch_format = None
1641
1970
 
1642
1971
    def get_branch_format(self):
1643
1972
        if self._branch_format is None:
1644
 
            from bzrlib.branch import format_registry as branch_format_registry
1645
 
            self._branch_format = branch_format_registry.get_default()
 
1973
            from bzrlib.branch import BranchFormat
 
1974
            self._branch_format = BranchFormat.get_default_format()
1646
1975
        return self._branch_format
1647
1976
 
1648
1977
    def set_branch_format(self, format):
1703
2032
                    # stack_on is inaccessible, JFDI.
1704
2033
                    # TODO: bad monkey, hard-coded formats...
1705
2034
                    if self.repository_format.rich_root_data:
1706
 
                        new_repo_format = knitpack_repo.RepositoryFormatKnitPack5RichRoot()
 
2035
                        new_repo_format = pack_repo.RepositoryFormatKnitPack5RichRoot()
1707
2036
                    else:
1708
 
                        new_repo_format = knitpack_repo.RepositoryFormatKnitPack5()
 
2037
                        new_repo_format = pack_repo.RepositoryFormatKnitPack5()
1709
2038
            else:
1710
2039
                # If the target already supports stacking, then we know the
1711
2040
                # project is already able to use stacking, so auto-upgrade
1728
2057
            if target_branch is None:
1729
2058
                if do_upgrade:
1730
2059
                    # TODO: bad monkey, hard-coded formats...
1731
 
                    from bzrlib.branch import BzrBranchFormat7
1732
 
                    new_branch_format = BzrBranchFormat7()
 
2060
                    new_branch_format = branch.BzrBranchFormat7()
1733
2061
            else:
1734
2062
                new_branch_format = target_branch._format
1735
2063
                if not new_branch_format.supports_stacking():
1750
2078
            raise NotImplementedError(self.get_converter)
1751
2079
        return ConvertMetaToMeta(format)
1752
2080
 
1753
 
    @classmethod
1754
 
    def get_format_string(cls):
 
2081
    def get_format_string(self):
1755
2082
        """See BzrDirFormat.get_format_string()."""
1756
2083
        return "Bazaar-NG meta directory, format 1\n"
1757
2084
 
1805
2132
 
1806
2133
    def __get_workingtree_format(self):
1807
2134
        if self._workingtree_format is None:
1808
 
            from bzrlib.workingtree import (
1809
 
                format_registry as wt_format_registry,
1810
 
                )
1811
 
            self._workingtree_format = wt_format_registry.get_default()
 
2135
            from bzrlib.workingtree import WorkingTreeFormat
 
2136
            self._workingtree_format = WorkingTreeFormat.get_default_format()
1812
2137
        return self._workingtree_format
1813
2138
 
1814
2139
    def __set_workingtree_format(self, wt_format):
1819
2144
 
1820
2145
 
1821
2146
# Register bzr formats
1822
 
BzrProber.formats.register(BzrDirMetaFormat1.get_format_string(),
1823
 
    BzrDirMetaFormat1)
1824
 
controldir.ControlDirFormat._default_format = BzrDirMetaFormat1()
1825
 
 
1826
 
 
1827
 
class ConvertMetaToMeta(controldir.Converter):
 
2147
BzrDirFormat.register_format(BzrDirFormat4())
 
2148
BzrDirFormat.register_format(BzrDirFormat5())
 
2149
BzrDirFormat.register_format(BzrDirFormat6())
 
2150
__default_format = BzrDirMetaFormat1()
 
2151
BzrDirFormat.register_format(__default_format)
 
2152
controldir.ControlDirFormat._default_format = __default_format
 
2153
 
 
2154
 
 
2155
class Converter(object):
 
2156
    """Converts a disk format object from one format to another."""
 
2157
 
 
2158
    def convert(self, to_convert, pb):
 
2159
        """Perform the conversion of to_convert, giving feedback via pb.
 
2160
 
 
2161
        :param to_convert: The disk object to convert.
 
2162
        :param pb: a progress bar to use for progress information.
 
2163
        """
 
2164
 
 
2165
    def step(self, message):
 
2166
        """Update the pb by a step."""
 
2167
        self.count +=1
 
2168
        self.pb.update(message, self.count, self.total)
 
2169
 
 
2170
 
 
2171
class ConvertBzrDir4To5(Converter):
 
2172
    """Converts format 4 bzr dirs to format 5."""
 
2173
 
 
2174
    def __init__(self):
 
2175
        super(ConvertBzrDir4To5, self).__init__()
 
2176
        self.converted_revs = set()
 
2177
        self.absent_revisions = set()
 
2178
        self.text_count = 0
 
2179
        self.revisions = {}
 
2180
 
 
2181
    def convert(self, to_convert, pb):
 
2182
        """See Converter.convert()."""
 
2183
        self.bzrdir = to_convert
 
2184
        if pb is not None:
 
2185
            warnings.warn("pb parameter to convert() is deprecated")
 
2186
        self.pb = ui.ui_factory.nested_progress_bar()
 
2187
        try:
 
2188
            ui.ui_factory.note('starting upgrade from format 4 to 5')
 
2189
            if isinstance(self.bzrdir.transport, local.LocalTransport):
 
2190
                self.bzrdir.get_workingtree_transport(None).delete('stat-cache')
 
2191
            self._convert_to_weaves()
 
2192
            return BzrDir.open(self.bzrdir.user_url)
 
2193
        finally:
 
2194
            self.pb.finished()
 
2195
 
 
2196
    def _convert_to_weaves(self):
 
2197
        ui.ui_factory.note('note: upgrade may be faster if all store files are ungzipped first')
 
2198
        try:
 
2199
            # TODO permissions
 
2200
            stat = self.bzrdir.transport.stat('weaves')
 
2201
            if not S_ISDIR(stat.st_mode):
 
2202
                self.bzrdir.transport.delete('weaves')
 
2203
                self.bzrdir.transport.mkdir('weaves')
 
2204
        except errors.NoSuchFile:
 
2205
            self.bzrdir.transport.mkdir('weaves')
 
2206
        # deliberately not a WeaveFile as we want to build it up slowly.
 
2207
        self.inv_weave = Weave('inventory')
 
2208
        # holds in-memory weaves for all files
 
2209
        self.text_weaves = {}
 
2210
        self.bzrdir.transport.delete('branch-format')
 
2211
        self.branch = self.bzrdir.open_branch()
 
2212
        self._convert_working_inv()
 
2213
        rev_history = self.branch.revision_history()
 
2214
        # to_read is a stack holding the revisions we still need to process;
 
2215
        # appending to it adds new highest-priority revisions
 
2216
        self.known_revisions = set(rev_history)
 
2217
        self.to_read = rev_history[-1:]
 
2218
        while self.to_read:
 
2219
            rev_id = self.to_read.pop()
 
2220
            if (rev_id not in self.revisions
 
2221
                and rev_id not in self.absent_revisions):
 
2222
                self._load_one_rev(rev_id)
 
2223
        self.pb.clear()
 
2224
        to_import = self._make_order()
 
2225
        for i, rev_id in enumerate(to_import):
 
2226
            self.pb.update('converting revision', i, len(to_import))
 
2227
            self._convert_one_rev(rev_id)
 
2228
        self.pb.clear()
 
2229
        self._write_all_weaves()
 
2230
        self._write_all_revs()
 
2231
        ui.ui_factory.note('upgraded to weaves:')
 
2232
        ui.ui_factory.note('  %6d revisions and inventories' % len(self.revisions))
 
2233
        ui.ui_factory.note('  %6d revisions not present' % len(self.absent_revisions))
 
2234
        ui.ui_factory.note('  %6d texts' % self.text_count)
 
2235
        self._cleanup_spare_files_after_format4()
 
2236
        self.branch._transport.put_bytes(
 
2237
            'branch-format',
 
2238
            BzrDirFormat5().get_format_string(),
 
2239
            mode=self.bzrdir._get_file_mode())
 
2240
 
 
2241
    def _cleanup_spare_files_after_format4(self):
 
2242
        # FIXME working tree upgrade foo.
 
2243
        for n in 'merged-patches', 'pending-merged-patches':
 
2244
            try:
 
2245
                ## assert os.path.getsize(p) == 0
 
2246
                self.bzrdir.transport.delete(n)
 
2247
            except errors.NoSuchFile:
 
2248
                pass
 
2249
        self.bzrdir.transport.delete_tree('inventory-store')
 
2250
        self.bzrdir.transport.delete_tree('text-store')
 
2251
 
 
2252
    def _convert_working_inv(self):
 
2253
        inv = xml4.serializer_v4.read_inventory(
 
2254
                self.branch._transport.get('inventory'))
 
2255
        new_inv_xml = xml5.serializer_v5.write_inventory_to_string(inv, working=True)
 
2256
        self.branch._transport.put_bytes('inventory', new_inv_xml,
 
2257
            mode=self.bzrdir._get_file_mode())
 
2258
 
 
2259
    def _write_all_weaves(self):
 
2260
        controlweaves = VersionedFileStore(self.bzrdir.transport, prefixed=False,
 
2261
                                           versionedfile_class=WeaveFile)
 
2262
        weave_transport = self.bzrdir.transport.clone('weaves')
 
2263
        weaves = VersionedFileStore(weave_transport, prefixed=False,
 
2264
                            versionedfile_class=WeaveFile)
 
2265
        transaction = WriteTransaction()
 
2266
 
 
2267
        try:
 
2268
            i = 0
 
2269
            for file_id, file_weave in self.text_weaves.items():
 
2270
                self.pb.update('writing weave', i, len(self.text_weaves))
 
2271
                weaves._put_weave(file_id, file_weave, transaction)
 
2272
                i += 1
 
2273
            self.pb.update('inventory', 0, 1)
 
2274
            controlweaves._put_weave('inventory', self.inv_weave, transaction)
 
2275
            self.pb.update('inventory', 1, 1)
 
2276
        finally:
 
2277
            self.pb.clear()
 
2278
 
 
2279
    def _write_all_revs(self):
 
2280
        """Write all revisions out in new form."""
 
2281
        self.bzrdir.transport.delete_tree('revision-store')
 
2282
        self.bzrdir.transport.mkdir('revision-store')
 
2283
        revision_transport = self.bzrdir.transport.clone('revision-store')
 
2284
        # TODO permissions
 
2285
        from bzrlib.xml5 import serializer_v5
 
2286
        from bzrlib.repofmt.weaverepo import RevisionTextStore
 
2287
        revision_store = RevisionTextStore(revision_transport,
 
2288
            serializer_v5, False, versionedfile.PrefixMapper(),
 
2289
            lambda:True, lambda:True)
 
2290
        try:
 
2291
            for i, rev_id in enumerate(self.converted_revs):
 
2292
                self.pb.update('write revision', i, len(self.converted_revs))
 
2293
                text = serializer_v5.write_revision_to_string(
 
2294
                    self.revisions[rev_id])
 
2295
                key = (rev_id,)
 
2296
                revision_store.add_lines(key, None, osutils.split_lines(text))
 
2297
        finally:
 
2298
            self.pb.clear()
 
2299
 
 
2300
    def _load_one_rev(self, rev_id):
 
2301
        """Load a revision object into memory.
 
2302
 
 
2303
        Any parents not either loaded or abandoned get queued to be
 
2304
        loaded."""
 
2305
        self.pb.update('loading revision',
 
2306
                       len(self.revisions),
 
2307
                       len(self.known_revisions))
 
2308
        if not self.branch.repository.has_revision(rev_id):
 
2309
            self.pb.clear()
 
2310
            ui.ui_factory.note('revision {%s} not present in branch; '
 
2311
                         'will be converted as a ghost' %
 
2312
                         rev_id)
 
2313
            self.absent_revisions.add(rev_id)
 
2314
        else:
 
2315
            rev = self.branch.repository.get_revision(rev_id)
 
2316
            for parent_id in rev.parent_ids:
 
2317
                self.known_revisions.add(parent_id)
 
2318
                self.to_read.append(parent_id)
 
2319
            self.revisions[rev_id] = rev
 
2320
 
 
2321
    def _load_old_inventory(self, rev_id):
 
2322
        f = self.branch.repository.inventory_store.get(rev_id)
 
2323
        try:
 
2324
            old_inv_xml = f.read()
 
2325
        finally:
 
2326
            f.close()
 
2327
        inv = xml4.serializer_v4.read_inventory_from_string(old_inv_xml)
 
2328
        inv.revision_id = rev_id
 
2329
        rev = self.revisions[rev_id]
 
2330
        return inv
 
2331
 
 
2332
    def _load_updated_inventory(self, rev_id):
 
2333
        inv_xml = self.inv_weave.get_text(rev_id)
 
2334
        inv = xml5.serializer_v5.read_inventory_from_string(inv_xml, rev_id)
 
2335
        return inv
 
2336
 
 
2337
    def _convert_one_rev(self, rev_id):
 
2338
        """Convert revision and all referenced objects to new format."""
 
2339
        rev = self.revisions[rev_id]
 
2340
        inv = self._load_old_inventory(rev_id)
 
2341
        present_parents = [p for p in rev.parent_ids
 
2342
                           if p not in self.absent_revisions]
 
2343
        self._convert_revision_contents(rev, inv, present_parents)
 
2344
        self._store_new_inv(rev, inv, present_parents)
 
2345
        self.converted_revs.add(rev_id)
 
2346
 
 
2347
    def _store_new_inv(self, rev, inv, present_parents):
 
2348
        new_inv_xml = xml5.serializer_v5.write_inventory_to_string(inv)
 
2349
        new_inv_sha1 = sha_string(new_inv_xml)
 
2350
        self.inv_weave.add_lines(rev.revision_id,
 
2351
                                 present_parents,
 
2352
                                 new_inv_xml.splitlines(True))
 
2353
        rev.inventory_sha1 = new_inv_sha1
 
2354
 
 
2355
    def _convert_revision_contents(self, rev, inv, present_parents):
 
2356
        """Convert all the files within a revision.
 
2357
 
 
2358
        Also upgrade the inventory to refer to the text revision ids."""
 
2359
        rev_id = rev.revision_id
 
2360
        mutter('converting texts of revision {%s}',
 
2361
               rev_id)
 
2362
        parent_invs = map(self._load_updated_inventory, present_parents)
 
2363
        entries = inv.iter_entries()
 
2364
        entries.next()
 
2365
        for path, ie in entries:
 
2366
            self._convert_file_version(rev, ie, parent_invs)
 
2367
 
 
2368
    def _convert_file_version(self, rev, ie, parent_invs):
 
2369
        """Convert one version of one file.
 
2370
 
 
2371
        The file needs to be added into the weave if it is a merge
 
2372
        of >=2 parents or if it's changed from its parent.
 
2373
        """
 
2374
        file_id = ie.file_id
 
2375
        rev_id = rev.revision_id
 
2376
        w = self.text_weaves.get(file_id)
 
2377
        if w is None:
 
2378
            w = Weave(file_id)
 
2379
            self.text_weaves[file_id] = w
 
2380
        text_changed = False
 
2381
        parent_candiate_entries = ie.parent_candidates(parent_invs)
 
2382
        heads = graph.Graph(self).heads(parent_candiate_entries.keys())
 
2383
        # XXX: Note that this is unordered - and this is tolerable because
 
2384
        # the previous code was also unordered.
 
2385
        previous_entries = dict((head, parent_candiate_entries[head]) for head
 
2386
            in heads)
 
2387
        self.snapshot_ie(previous_entries, ie, w, rev_id)
 
2388
 
 
2389
    def get_parent_map(self, revision_ids):
 
2390
        """See graph.StackedParentsProvider.get_parent_map"""
 
2391
        return dict((revision_id, self.revisions[revision_id])
 
2392
                    for revision_id in revision_ids
 
2393
                     if revision_id in self.revisions)
 
2394
 
 
2395
    def snapshot_ie(self, previous_revisions, ie, w, rev_id):
 
2396
        # TODO: convert this logic, which is ~= snapshot to
 
2397
        # a call to:. This needs the path figured out. rather than a work_tree
 
2398
        # a v4 revision_tree can be given, or something that looks enough like
 
2399
        # one to give the file content to the entry if it needs it.
 
2400
        # and we need something that looks like a weave store for snapshot to
 
2401
        # save against.
 
2402
        #ie.snapshot(rev, PATH, previous_revisions, REVISION_TREE, InMemoryWeaveStore(self.text_weaves))
 
2403
        if len(previous_revisions) == 1:
 
2404
            previous_ie = previous_revisions.values()[0]
 
2405
            if ie._unchanged(previous_ie):
 
2406
                ie.revision = previous_ie.revision
 
2407
                return
 
2408
        if ie.has_text():
 
2409
            f = self.branch.repository._text_store.get(ie.text_id)
 
2410
            try:
 
2411
                file_lines = f.readlines()
 
2412
            finally:
 
2413
                f.close()
 
2414
            w.add_lines(rev_id, previous_revisions, file_lines)
 
2415
            self.text_count += 1
 
2416
        else:
 
2417
            w.add_lines(rev_id, previous_revisions, [])
 
2418
        ie.revision = rev_id
 
2419
 
 
2420
    def _make_order(self):
 
2421
        """Return a suitable order for importing revisions.
 
2422
 
 
2423
        The order must be such that an revision is imported after all
 
2424
        its (present) parents.
 
2425
        """
 
2426
        todo = set(self.revisions.keys())
 
2427
        done = self.absent_revisions.copy()
 
2428
        order = []
 
2429
        while todo:
 
2430
            # scan through looking for a revision whose parents
 
2431
            # are all done
 
2432
            for rev_id in sorted(list(todo)):
 
2433
                rev = self.revisions[rev_id]
 
2434
                parent_ids = set(rev.parent_ids)
 
2435
                if parent_ids.issubset(done):
 
2436
                    # can take this one now
 
2437
                    order.append(rev_id)
 
2438
                    todo.remove(rev_id)
 
2439
                    done.add(rev_id)
 
2440
        return order
 
2441
 
 
2442
 
 
2443
class ConvertBzrDir5To6(Converter):
 
2444
    """Converts format 5 bzr dirs to format 6."""
 
2445
 
 
2446
    def convert(self, to_convert, pb):
 
2447
        """See Converter.convert()."""
 
2448
        self.bzrdir = to_convert
 
2449
        pb = ui.ui_factory.nested_progress_bar()
 
2450
        try:
 
2451
            ui.ui_factory.note('starting upgrade from format 5 to 6')
 
2452
            self._convert_to_prefixed()
 
2453
            return BzrDir.open(self.bzrdir.user_url)
 
2454
        finally:
 
2455
            pb.finished()
 
2456
 
 
2457
    def _convert_to_prefixed(self):
 
2458
        from bzrlib.store import TransportStore
 
2459
        self.bzrdir.transport.delete('branch-format')
 
2460
        for store_name in ["weaves", "revision-store"]:
 
2461
            ui.ui_factory.note("adding prefixes to %s" % store_name)
 
2462
            store_transport = self.bzrdir.transport.clone(store_name)
 
2463
            store = TransportStore(store_transport, prefixed=True)
 
2464
            for urlfilename in store_transport.list_dir('.'):
 
2465
                filename = urlutils.unescape(urlfilename)
 
2466
                if (filename.endswith(".weave") or
 
2467
                    filename.endswith(".gz") or
 
2468
                    filename.endswith(".sig")):
 
2469
                    file_id, suffix = os.path.splitext(filename)
 
2470
                else:
 
2471
                    file_id = filename
 
2472
                    suffix = ''
 
2473
                new_name = store._mapper.map((file_id,)) + suffix
 
2474
                # FIXME keep track of the dirs made RBC 20060121
 
2475
                try:
 
2476
                    store_transport.move(filename, new_name)
 
2477
                except errors.NoSuchFile: # catches missing dirs strangely enough
 
2478
                    store_transport.mkdir(osutils.dirname(new_name))
 
2479
                    store_transport.move(filename, new_name)
 
2480
        self.bzrdir.transport.put_bytes(
 
2481
            'branch-format',
 
2482
            BzrDirFormat6().get_format_string(),
 
2483
            mode=self.bzrdir._get_file_mode())
 
2484
 
 
2485
 
 
2486
class ConvertBzrDir6ToMeta(Converter):
 
2487
    """Converts format 6 bzr dirs to metadirs."""
 
2488
 
 
2489
    def convert(self, to_convert, pb):
 
2490
        """See Converter.convert()."""
 
2491
        from bzrlib.repofmt.weaverepo import RepositoryFormat7
 
2492
        from bzrlib.branch import BzrBranchFormat5
 
2493
        self.bzrdir = to_convert
 
2494
        self.pb = ui.ui_factory.nested_progress_bar()
 
2495
        self.count = 0
 
2496
        self.total = 20 # the steps we know about
 
2497
        self.garbage_inventories = []
 
2498
        self.dir_mode = self.bzrdir._get_dir_mode()
 
2499
        self.file_mode = self.bzrdir._get_file_mode()
 
2500
 
 
2501
        ui.ui_factory.note('starting upgrade from format 6 to metadir')
 
2502
        self.bzrdir.transport.put_bytes(
 
2503
                'branch-format',
 
2504
                "Converting to format 6",
 
2505
                mode=self.file_mode)
 
2506
        # its faster to move specific files around than to open and use the apis...
 
2507
        # first off, nuke ancestry.weave, it was never used.
 
2508
        try:
 
2509
            self.step('Removing ancestry.weave')
 
2510
            self.bzrdir.transport.delete('ancestry.weave')
 
2511
        except errors.NoSuchFile:
 
2512
            pass
 
2513
        # find out whats there
 
2514
        self.step('Finding branch files')
 
2515
        last_revision = self.bzrdir.open_branch().last_revision()
 
2516
        bzrcontents = self.bzrdir.transport.list_dir('.')
 
2517
        for name in bzrcontents:
 
2518
            if name.startswith('basis-inventory.'):
 
2519
                self.garbage_inventories.append(name)
 
2520
        # create new directories for repository, working tree and branch
 
2521
        repository_names = [('inventory.weave', True),
 
2522
                            ('revision-store', True),
 
2523
                            ('weaves', True)]
 
2524
        self.step('Upgrading repository  ')
 
2525
        self.bzrdir.transport.mkdir('repository', mode=self.dir_mode)
 
2526
        self.make_lock('repository')
 
2527
        # we hard code the formats here because we are converting into
 
2528
        # the meta format. The meta format upgrader can take this to a
 
2529
        # future format within each component.
 
2530
        self.put_format('repository', RepositoryFormat7())
 
2531
        for entry in repository_names:
 
2532
            self.move_entry('repository', entry)
 
2533
 
 
2534
        self.step('Upgrading branch      ')
 
2535
        self.bzrdir.transport.mkdir('branch', mode=self.dir_mode)
 
2536
        self.make_lock('branch')
 
2537
        self.put_format('branch', BzrBranchFormat5())
 
2538
        branch_files = [('revision-history', True),
 
2539
                        ('branch-name', True),
 
2540
                        ('parent', False)]
 
2541
        for entry in branch_files:
 
2542
            self.move_entry('branch', entry)
 
2543
 
 
2544
        checkout_files = [('pending-merges', True),
 
2545
                          ('inventory', True),
 
2546
                          ('stat-cache', False)]
 
2547
        # If a mandatory checkout file is not present, the branch does not have
 
2548
        # a functional checkout. Do not create a checkout in the converted
 
2549
        # branch.
 
2550
        for name, mandatory in checkout_files:
 
2551
            if mandatory and name not in bzrcontents:
 
2552
                has_checkout = False
 
2553
                break
 
2554
        else:
 
2555
            has_checkout = True
 
2556
        if not has_checkout:
 
2557
            ui.ui_factory.note('No working tree.')
 
2558
            # If some checkout files are there, we may as well get rid of them.
 
2559
            for name, mandatory in checkout_files:
 
2560
                if name in bzrcontents:
 
2561
                    self.bzrdir.transport.delete(name)
 
2562
        else:
 
2563
            from bzrlib.workingtree import WorkingTreeFormat3
 
2564
            self.step('Upgrading working tree')
 
2565
            self.bzrdir.transport.mkdir('checkout', mode=self.dir_mode)
 
2566
            self.make_lock('checkout')
 
2567
            self.put_format(
 
2568
                'checkout', WorkingTreeFormat3())
 
2569
            self.bzrdir.transport.delete_multi(
 
2570
                self.garbage_inventories, self.pb)
 
2571
            for entry in checkout_files:
 
2572
                self.move_entry('checkout', entry)
 
2573
            if last_revision is not None:
 
2574
                self.bzrdir.transport.put_bytes(
 
2575
                    'checkout/last-revision', last_revision)
 
2576
        self.bzrdir.transport.put_bytes(
 
2577
            'branch-format',
 
2578
            BzrDirMetaFormat1().get_format_string(),
 
2579
            mode=self.file_mode)
 
2580
        self.pb.finished()
 
2581
        return BzrDir.open(self.bzrdir.user_url)
 
2582
 
 
2583
    def make_lock(self, name):
 
2584
        """Make a lock for the new control dir name."""
 
2585
        self.step('Make %s lock' % name)
 
2586
        ld = lockdir.LockDir(self.bzrdir.transport,
 
2587
                             '%s/lock' % name,
 
2588
                             file_modebits=self.file_mode,
 
2589
                             dir_modebits=self.dir_mode)
 
2590
        ld.create()
 
2591
 
 
2592
    def move_entry(self, new_dir, entry):
 
2593
        """Move then entry name into new_dir."""
 
2594
        name = entry[0]
 
2595
        mandatory = entry[1]
 
2596
        self.step('Moving %s' % name)
 
2597
        try:
 
2598
            self.bzrdir.transport.move(name, '%s/%s' % (new_dir, name))
 
2599
        except errors.NoSuchFile:
 
2600
            if mandatory:
 
2601
                raise
 
2602
 
 
2603
    def put_format(self, dirname, format):
 
2604
        self.bzrdir.transport.put_bytes('%s/format' % dirname,
 
2605
            format.get_format_string(),
 
2606
            self.file_mode)
 
2607
 
 
2608
 
 
2609
class ConvertMetaToMeta(Converter):
1828
2610
    """Converts the components of metadirs."""
1829
2611
 
1830
2612
    def __init__(self, target_format):
1855
2637
            # TODO: conversions of Branch and Tree should be done by
1856
2638
            # InterXFormat lookups/some sort of registry.
1857
2639
            # Avoid circular imports
 
2640
            from bzrlib import branch as _mod_branch
1858
2641
            old = branch._format.__class__
1859
2642
            new = self.target_format.get_branch_format().__class__
1860
2643
            while old != new:
1883
2666
        else:
1884
2667
            # TODO: conversions of Branch and Tree should be done by
1885
2668
            # InterXFormat lookups
1886
 
            if (isinstance(tree, workingtree_3.WorkingTree3) and
 
2669
            if (isinstance(tree, workingtree.WorkingTree3) and
1887
2670
                not isinstance(tree, workingtree_4.DirStateWorkingTree) and
1888
2671
                isinstance(self.target_format.workingtree_format,
1889
2672
                    workingtree_4.DirStateWorkingTreeFormat)):
1902
2685
        return to_convert
1903
2686
 
1904
2687
 
 
2688
# This is not in remote.py because it's relatively small, and needs to be
 
2689
# registered. Putting it in remote.py creates a circular import problem.
 
2690
# we can make it a lazy object if the control formats is turned into something
 
2691
# like a registry.
 
2692
class RemoteBzrDirFormat(BzrDirMetaFormat1):
 
2693
    """Format representing bzrdirs accessed via a smart server"""
 
2694
 
 
2695
    supports_workingtrees = False
 
2696
 
 
2697
    def __init__(self):
 
2698
        BzrDirMetaFormat1.__init__(self)
 
2699
        # XXX: It's a bit ugly that the network name is here, because we'd
 
2700
        # like to believe that format objects are stateless or at least
 
2701
        # immutable,  However, we do at least avoid mutating the name after
 
2702
        # it's returned.  See <https://bugs.launchpad.net/bzr/+bug/504102>
 
2703
        self._network_name = None
 
2704
 
 
2705
    def __repr__(self):
 
2706
        return "%s(_network_name=%r)" % (self.__class__.__name__,
 
2707
            self._network_name)
 
2708
 
 
2709
    def get_format_description(self):
 
2710
        if self._network_name:
 
2711
            real_format = controldir.network_format_registry.get(self._network_name)
 
2712
            return 'Remote: ' + real_format.get_format_description()
 
2713
        return 'bzr remote bzrdir'
 
2714
 
 
2715
    def get_format_string(self):
 
2716
        raise NotImplementedError(self.get_format_string)
 
2717
 
 
2718
    def network_name(self):
 
2719
        if self._network_name:
 
2720
            return self._network_name
 
2721
        else:
 
2722
            raise AssertionError("No network name set.")
 
2723
 
 
2724
    def initialize_on_transport(self, transport):
 
2725
        try:
 
2726
            # hand off the request to the smart server
 
2727
            client_medium = transport.get_smart_medium()
 
2728
        except errors.NoSmartMedium:
 
2729
            # TODO: lookup the local format from a server hint.
 
2730
            local_dir_format = BzrDirMetaFormat1()
 
2731
            return local_dir_format.initialize_on_transport(transport)
 
2732
        client = _SmartClient(client_medium)
 
2733
        path = client.remote_path_from_transport(transport)
 
2734
        try:
 
2735
            response = client.call('BzrDirFormat.initialize', path)
 
2736
        except errors.ErrorFromSmartServer, err:
 
2737
            remote._translate_error(err, path=path)
 
2738
        if response[0] != 'ok':
 
2739
            raise errors.SmartProtocolError('unexpected response code %s' % (response,))
 
2740
        format = RemoteBzrDirFormat()
 
2741
        self._supply_sub_formats_to(format)
 
2742
        return remote.RemoteBzrDir(transport, format)
 
2743
 
 
2744
    def parse_NoneTrueFalse(self, arg):
 
2745
        if not arg:
 
2746
            return None
 
2747
        if arg == 'False':
 
2748
            return False
 
2749
        if arg == 'True':
 
2750
            return True
 
2751
        raise AssertionError("invalid arg %r" % arg)
 
2752
 
 
2753
    def _serialize_NoneTrueFalse(self, arg):
 
2754
        if arg is False:
 
2755
            return 'False'
 
2756
        if arg:
 
2757
            return 'True'
 
2758
        return ''
 
2759
 
 
2760
    def _serialize_NoneString(self, arg):
 
2761
        return arg or ''
 
2762
 
 
2763
    def initialize_on_transport_ex(self, transport, use_existing_dir=False,
 
2764
        create_prefix=False, force_new_repo=False, stacked_on=None,
 
2765
        stack_on_pwd=None, repo_format_name=None, make_working_trees=None,
 
2766
        shared_repo=False):
 
2767
        try:
 
2768
            # hand off the request to the smart server
 
2769
            client_medium = transport.get_smart_medium()
 
2770
        except errors.NoSmartMedium:
 
2771
            do_vfs = True
 
2772
        else:
 
2773
            # Decline to open it if the server doesn't support our required
 
2774
            # version (3) so that the VFS-based transport will do it.
 
2775
            if client_medium.should_probe():
 
2776
                try:
 
2777
                    server_version = client_medium.protocol_version()
 
2778
                    if server_version != '2':
 
2779
                        do_vfs = True
 
2780
                    else:
 
2781
                        do_vfs = False
 
2782
                except errors.SmartProtocolError:
 
2783
                    # Apparently there's no usable smart server there, even though
 
2784
                    # the medium supports the smart protocol.
 
2785
                    do_vfs = True
 
2786
            else:
 
2787
                do_vfs = False
 
2788
        if not do_vfs:
 
2789
            client = _SmartClient(client_medium)
 
2790
            path = client.remote_path_from_transport(transport)
 
2791
            if client_medium._is_remote_before((1, 16)):
 
2792
                do_vfs = True
 
2793
        if do_vfs:
 
2794
            # TODO: lookup the local format from a server hint.
 
2795
            local_dir_format = BzrDirMetaFormat1()
 
2796
            self._supply_sub_formats_to(local_dir_format)
 
2797
            return local_dir_format.initialize_on_transport_ex(transport,
 
2798
                use_existing_dir=use_existing_dir, create_prefix=create_prefix,
 
2799
                force_new_repo=force_new_repo, stacked_on=stacked_on,
 
2800
                stack_on_pwd=stack_on_pwd, repo_format_name=repo_format_name,
 
2801
                make_working_trees=make_working_trees, shared_repo=shared_repo,
 
2802
                vfs_only=True)
 
2803
        return self._initialize_on_transport_ex_rpc(client, path, transport,
 
2804
            use_existing_dir, create_prefix, force_new_repo, stacked_on,
 
2805
            stack_on_pwd, repo_format_name, make_working_trees, shared_repo)
 
2806
 
 
2807
    def _initialize_on_transport_ex_rpc(self, client, path, transport,
 
2808
        use_existing_dir, create_prefix, force_new_repo, stacked_on,
 
2809
        stack_on_pwd, repo_format_name, make_working_trees, shared_repo):
 
2810
        args = []
 
2811
        args.append(self._serialize_NoneTrueFalse(use_existing_dir))
 
2812
        args.append(self._serialize_NoneTrueFalse(create_prefix))
 
2813
        args.append(self._serialize_NoneTrueFalse(force_new_repo))
 
2814
        args.append(self._serialize_NoneString(stacked_on))
 
2815
        # stack_on_pwd is often/usually our transport
 
2816
        if stack_on_pwd:
 
2817
            try:
 
2818
                stack_on_pwd = transport.relpath(stack_on_pwd)
 
2819
                if not stack_on_pwd:
 
2820
                    stack_on_pwd = '.'
 
2821
            except errors.PathNotChild:
 
2822
                pass
 
2823
        args.append(self._serialize_NoneString(stack_on_pwd))
 
2824
        args.append(self._serialize_NoneString(repo_format_name))
 
2825
        args.append(self._serialize_NoneTrueFalse(make_working_trees))
 
2826
        args.append(self._serialize_NoneTrueFalse(shared_repo))
 
2827
        request_network_name = self._network_name or \
 
2828
            BzrDirFormat.get_default_format().network_name()
 
2829
        try:
 
2830
            response = client.call('BzrDirFormat.initialize_ex_1.16',
 
2831
                request_network_name, path, *args)
 
2832
        except errors.UnknownSmartMethod:
 
2833
            client._medium._remember_remote_is_before((1,16))
 
2834
            local_dir_format = BzrDirMetaFormat1()
 
2835
            self._supply_sub_formats_to(local_dir_format)
 
2836
            return local_dir_format.initialize_on_transport_ex(transport,
 
2837
                use_existing_dir=use_existing_dir, create_prefix=create_prefix,
 
2838
                force_new_repo=force_new_repo, stacked_on=stacked_on,
 
2839
                stack_on_pwd=stack_on_pwd, repo_format_name=repo_format_name,
 
2840
                make_working_trees=make_working_trees, shared_repo=shared_repo,
 
2841
                vfs_only=True)
 
2842
        except errors.ErrorFromSmartServer, err:
 
2843
            remote._translate_error(err, path=path)
 
2844
        repo_path = response[0]
 
2845
        bzrdir_name = response[6]
 
2846
        require_stacking = response[7]
 
2847
        require_stacking = self.parse_NoneTrueFalse(require_stacking)
 
2848
        format = RemoteBzrDirFormat()
 
2849
        format._network_name = bzrdir_name
 
2850
        self._supply_sub_formats_to(format)
 
2851
        bzrdir = remote.RemoteBzrDir(transport, format, _client=client)
 
2852
        if repo_path:
 
2853
            repo_format = remote.response_tuple_to_repo_format(response[1:])
 
2854
            if repo_path == '.':
 
2855
                repo_path = ''
 
2856
            if repo_path:
 
2857
                repo_bzrdir_format = RemoteBzrDirFormat()
 
2858
                repo_bzrdir_format._network_name = response[5]
 
2859
                repo_bzr = remote.RemoteBzrDir(transport.clone(repo_path),
 
2860
                    repo_bzrdir_format)
 
2861
            else:
 
2862
                repo_bzr = bzrdir
 
2863
            final_stack = response[8] or None
 
2864
            final_stack_pwd = response[9] or None
 
2865
            if final_stack_pwd:
 
2866
                final_stack_pwd = urlutils.join(
 
2867
                    transport.base, final_stack_pwd)
 
2868
            remote_repo = remote.RemoteRepository(repo_bzr, repo_format)
 
2869
            if len(response) > 10:
 
2870
                # Updated server verb that locks remotely.
 
2871
                repo_lock_token = response[10] or None
 
2872
                remote_repo.lock_write(repo_lock_token, _skip_rpc=True)
 
2873
                if repo_lock_token:
 
2874
                    remote_repo.dont_leave_lock_in_place()
 
2875
            else:
 
2876
                remote_repo.lock_write()
 
2877
            policy = UseExistingRepository(remote_repo, final_stack,
 
2878
                final_stack_pwd, require_stacking)
 
2879
            policy.acquire_repository()
 
2880
        else:
 
2881
            remote_repo = None
 
2882
            policy = None
 
2883
        bzrdir._format.set_branch_format(self.get_branch_format())
 
2884
        if require_stacking:
 
2885
            # The repo has already been created, but we need to make sure that
 
2886
            # we'll make a stackable branch.
 
2887
            bzrdir._format.require_stacking(_skip_repo=True)
 
2888
        return remote_repo, bzrdir, require_stacking, policy
 
2889
 
 
2890
    def _open(self, transport):
 
2891
        return remote.RemoteBzrDir(transport, self)
 
2892
 
 
2893
    def __eq__(self, other):
 
2894
        if not isinstance(other, RemoteBzrDirFormat):
 
2895
            return False
 
2896
        return self.get_format_description() == other.get_format_description()
 
2897
 
 
2898
    def __return_repository_format(self):
 
2899
        # Always return a RemoteRepositoryFormat object, but if a specific bzr
 
2900
        # repository format has been asked for, tell the RemoteRepositoryFormat
 
2901
        # that it should use that for init() etc.
 
2902
        result = remote.RemoteRepositoryFormat()
 
2903
        custom_format = getattr(self, '_repository_format', None)
 
2904
        if custom_format:
 
2905
            if isinstance(custom_format, remote.RemoteRepositoryFormat):
 
2906
                return custom_format
 
2907
            else:
 
2908
                # We will use the custom format to create repositories over the
 
2909
                # wire; expose its details like rich_root_data for code to
 
2910
                # query
 
2911
                result._custom_format = custom_format
 
2912
        return result
 
2913
 
 
2914
    def get_branch_format(self):
 
2915
        result = BzrDirMetaFormat1.get_branch_format(self)
 
2916
        if not isinstance(result, remote.RemoteBranchFormat):
 
2917
            new_result = remote.RemoteBranchFormat()
 
2918
            new_result._custom_format = result
 
2919
            # cache the result
 
2920
            self.set_branch_format(new_result)
 
2921
            result = new_result
 
2922
        return result
 
2923
 
 
2924
    repository_format = property(__return_repository_format,
 
2925
        BzrDirMetaFormat1._set_repository_format) #.im_func)
 
2926
 
 
2927
 
1905
2928
controldir.ControlDirFormat.register_server_prober(RemoteBzrProber)
1906
2929
 
1907
2930
 
2106
3129
    registry.register(key, helper, help, native, deprecated, hidden,
2107
3130
        experimental, alias)
2108
3131
 
 
3132
# The pre-0.8 formats have their repository format network name registered in
 
3133
# repository.py. MetaDir formats have their repository format network name
 
3134
# inferred from their disk format string.
 
3135
controldir.format_registry.register('weave', BzrDirFormat6,
 
3136
    'Pre-0.8 format.  Slower than knit and does not'
 
3137
    ' support checkouts or shared repositories.',
 
3138
    hidden=True,
 
3139
    deprecated=True)
 
3140
register_metadir(controldir.format_registry, 'metaweave',
 
3141
    'bzrlib.repofmt.weaverepo.RepositoryFormat7',
 
3142
    'Transitional format in 0.8.  Slower than knit.',
 
3143
    branch_format='bzrlib.branch.BzrBranchFormat5',
 
3144
    tree_format='bzrlib.workingtree.WorkingTreeFormat3',
 
3145
    hidden=True,
 
3146
    deprecated=True)
2109
3147
register_metadir(controldir.format_registry, 'knit',
2110
3148
    'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
2111
3149
    'Format using knits.  Recommended for interoperation with bzr <= 0.14.',
2112
3150
    branch_format='bzrlib.branch.BzrBranchFormat5',
2113
 
    tree_format='bzrlib.workingtree_3.WorkingTreeFormat3',
 
3151
    tree_format='bzrlib.workingtree.WorkingTreeFormat3',
2114
3152
    hidden=True,
2115
3153
    deprecated=True)
2116
3154
register_metadir(controldir.format_registry, 'dirstate',
2118
3156
    help='New in 0.15: Fast local operations. Compatible with bzr 0.8 and '
2119
3157
        'above when accessed over the network.',
2120
3158
    branch_format='bzrlib.branch.BzrBranchFormat5',
2121
 
    tree_format='bzrlib.workingtree_4.WorkingTreeFormat4',
 
3159
    # this uses bzrlib.workingtree.WorkingTreeFormat4 because importing
 
3160
    # directly from workingtree_4 triggers a circular import.
 
3161
    tree_format='bzrlib.workingtree.WorkingTreeFormat4',
2122
3162
    hidden=True,
2123
3163
    deprecated=True)
2124
3164
register_metadir(controldir.format_registry, 'dirstate-tags',
2127
3167
        'network operations. Additionally adds support for tags.'
2128
3168
        ' Incompatible with bzr < 0.15.',
2129
3169
    branch_format='bzrlib.branch.BzrBranchFormat6',
2130
 
    tree_format='bzrlib.workingtree_4.WorkingTreeFormat4',
 
3170
    tree_format='bzrlib.workingtree.WorkingTreeFormat4',
2131
3171
    hidden=True,
2132
3172
    deprecated=True)
2133
3173
register_metadir(controldir.format_registry, 'rich-root',
2135
3175
    help='New in 1.0.  Better handling of tree roots.  Incompatible with'
2136
3176
        ' bzr < 1.0.',
2137
3177
    branch_format='bzrlib.branch.BzrBranchFormat6',
2138
 
    tree_format='bzrlib.workingtree_4.WorkingTreeFormat4',
 
3178
    tree_format='bzrlib.workingtree.WorkingTreeFormat4',
2139
3179
    hidden=True,
2140
3180
    deprecated=True)
2141
3181
register_metadir(controldir.format_registry, 'dirstate-with-subtree',
2144
3184
        'network operations. Additionally adds support for versioning nested '
2145
3185
        'bzr branches. Incompatible with bzr < 0.15.',
2146
3186
    branch_format='bzrlib.branch.BzrBranchFormat6',
2147
 
    tree_format='bzrlib.workingtree_4.WorkingTreeFormat4',
 
3187
    tree_format='bzrlib.workingtree.WorkingTreeFormat4',
2148
3188
    experimental=True,
2149
3189
    hidden=True,
2150
3190
    )
2151
3191
register_metadir(controldir.format_registry, 'pack-0.92',
2152
 
    'bzrlib.repofmt.knitpack_repo.RepositoryFormatKnitPack1',
 
3192
    'bzrlib.repofmt.pack_repo.RepositoryFormatKnitPack1',
2153
3193
    help='New in 0.92: Pack-based format with data compatible with '
2154
3194
        'dirstate-tags format repositories. Interoperates with '
2155
3195
        'bzr repositories before 0.92 but cannot be read by bzr < 0.92. '
2156
3196
        ,
2157
3197
    branch_format='bzrlib.branch.BzrBranchFormat6',
2158
 
    tree_format='bzrlib.workingtree_4.WorkingTreeFormat4',
 
3198
    tree_format='bzrlib.workingtree.WorkingTreeFormat4',
2159
3199
    )
2160
3200
register_metadir(controldir.format_registry, 'pack-0.92-subtree',
2161
 
    'bzrlib.repofmt.knitpack_repo.RepositoryFormatKnitPack3',
 
3201
    'bzrlib.repofmt.pack_repo.RepositoryFormatKnitPack3',
2162
3202
    help='New in 0.92: Pack-based format with data compatible with '
2163
3203
        'dirstate-with-subtree format repositories. Interoperates with '
2164
3204
        'bzr repositories before 0.92 but cannot be read by bzr < 0.92. '
2165
3205
        ,
2166
3206
    branch_format='bzrlib.branch.BzrBranchFormat6',
2167
 
    tree_format='bzrlib.workingtree_4.WorkingTreeFormat4',
 
3207
    tree_format='bzrlib.workingtree.WorkingTreeFormat4',
2168
3208
    hidden=True,
2169
3209
    experimental=True,
2170
3210
    )
2171
3211
register_metadir(controldir.format_registry, 'rich-root-pack',
2172
 
    'bzrlib.repofmt.knitpack_repo.RepositoryFormatKnitPack4',
 
3212
    'bzrlib.repofmt.pack_repo.RepositoryFormatKnitPack4',
2173
3213
    help='New in 1.0: A variant of pack-0.92 that supports rich-root data '
2174
3214
         '(needed for bzr-svn and bzr-git).',
2175
3215
    branch_format='bzrlib.branch.BzrBranchFormat6',
2176
 
    tree_format='bzrlib.workingtree_4.WorkingTreeFormat4',
 
3216
    tree_format='bzrlib.workingtree.WorkingTreeFormat4',
2177
3217
    hidden=True,
2178
3218
    )
2179
3219
register_metadir(controldir.format_registry, '1.6',
2180
 
    'bzrlib.repofmt.knitpack_repo.RepositoryFormatKnitPack5',
 
3220
    'bzrlib.repofmt.pack_repo.RepositoryFormatKnitPack5',
2181
3221
    help='A format that allows a branch to indicate that there is another '
2182
3222
         '(stacked) repository that should be used to access data that is '
2183
3223
         'not present locally.',
2184
3224
    branch_format='bzrlib.branch.BzrBranchFormat7',
2185
 
    tree_format='bzrlib.workingtree_4.WorkingTreeFormat4',
 
3225
    tree_format='bzrlib.workingtree.WorkingTreeFormat4',
2186
3226
    hidden=True,
2187
3227
    )
2188
3228
register_metadir(controldir.format_registry, '1.6.1-rich-root',
2189
 
    'bzrlib.repofmt.knitpack_repo.RepositoryFormatKnitPack5RichRoot',
 
3229
    'bzrlib.repofmt.pack_repo.RepositoryFormatKnitPack5RichRoot',
2190
3230
    help='A variant of 1.6 that supports rich-root data '
2191
3231
         '(needed for bzr-svn and bzr-git).',
2192
3232
    branch_format='bzrlib.branch.BzrBranchFormat7',
2193
 
    tree_format='bzrlib.workingtree_4.WorkingTreeFormat4',
 
3233
    tree_format='bzrlib.workingtree.WorkingTreeFormat4',
2194
3234
    hidden=True,
2195
3235
    )
2196
3236
register_metadir(controldir.format_registry, '1.9',
2197
 
    'bzrlib.repofmt.knitpack_repo.RepositoryFormatKnitPack6',
 
3237
    'bzrlib.repofmt.pack_repo.RepositoryFormatKnitPack6',
2198
3238
    help='A repository format using B+tree indexes. These indexes '
2199
3239
         'are smaller in size, have smarter caching and provide faster '
2200
3240
         'performance for most operations.',
2201
3241
    branch_format='bzrlib.branch.BzrBranchFormat7',
2202
 
    tree_format='bzrlib.workingtree_4.WorkingTreeFormat4',
 
3242
    tree_format='bzrlib.workingtree.WorkingTreeFormat4',
2203
3243
    hidden=True,
2204
3244
    )
2205
3245
register_metadir(controldir.format_registry, '1.9-rich-root',
2206
 
    'bzrlib.repofmt.knitpack_repo.RepositoryFormatKnitPack6RichRoot',
 
3246
    'bzrlib.repofmt.pack_repo.RepositoryFormatKnitPack6RichRoot',
2207
3247
    help='A variant of 1.9 that supports rich-root data '
2208
3248
         '(needed for bzr-svn and bzr-git).',
2209
3249
    branch_format='bzrlib.branch.BzrBranchFormat7',
2210
 
    tree_format='bzrlib.workingtree_4.WorkingTreeFormat4',
 
3250
    tree_format='bzrlib.workingtree.WorkingTreeFormat4',
2211
3251
    hidden=True,
2212
3252
    )
2213
3253
register_metadir(controldir.format_registry, '1.14',
2214
 
    'bzrlib.repofmt.knitpack_repo.RepositoryFormatKnitPack6',
 
3254
    'bzrlib.repofmt.pack_repo.RepositoryFormatKnitPack6',
2215
3255
    help='A working-tree format that supports content filtering.',
2216
3256
    branch_format='bzrlib.branch.BzrBranchFormat7',
2217
 
    tree_format='bzrlib.workingtree_4.WorkingTreeFormat5',
 
3257
    tree_format='bzrlib.workingtree.WorkingTreeFormat5',
2218
3258
    )
2219
3259
register_metadir(controldir.format_registry, '1.14-rich-root',
2220
 
    'bzrlib.repofmt.knitpack_repo.RepositoryFormatKnitPack6RichRoot',
 
3260
    'bzrlib.repofmt.pack_repo.RepositoryFormatKnitPack6RichRoot',
2221
3261
    help='A variant of 1.14 that supports rich-root data '
2222
3262
         '(needed for bzr-svn and bzr-git).',
2223
3263
    branch_format='bzrlib.branch.BzrBranchFormat7',
2224
 
    tree_format='bzrlib.workingtree_4.WorkingTreeFormat5',
 
3264
    tree_format='bzrlib.workingtree.WorkingTreeFormat5',
2225
3265
    )
2226
3266
# The following un-numbered 'development' formats should always just be aliases.
2227
3267
register_metadir(controldir.format_registry, 'development-subtree',
2233
3273
        'http://doc.bazaar.canonical.com/latest/developers/development-repo.html '
2234
3274
        'before use.',
2235
3275
    branch_format='bzrlib.branch.BzrBranchFormat7',
2236
 
    tree_format='bzrlib.workingtree_4.WorkingTreeFormat6',
 
3276
    tree_format='bzrlib.workingtree.WorkingTreeFormat6',
2237
3277
    experimental=True,
2238
3278
    hidden=True,
2239
3279
    alias=False, # Restore to being an alias when an actual development subtree format is added
2241
3281
                 # chk based subtree format.
2242
3282
    )
2243
3283
register_metadir(controldir.format_registry, 'development5-subtree',
2244
 
    'bzrlib.repofmt.knitpack_repo.RepositoryFormatPackDevelopment2Subtree',
 
3284
    'bzrlib.repofmt.pack_repo.RepositoryFormatPackDevelopment2Subtree',
2245
3285
    help='Development format, subtree variant. Can convert data to and '
2246
3286
        'from pack-0.92-subtree (and anything compatible with '
2247
3287
        'pack-0.92-subtree) format repositories. Repositories and branches in '
2249
3289
        'http://doc.bazaar.canonical.com/latest/developers/development-repo.html '
2250
3290
        'before use.',
2251
3291
    branch_format='bzrlib.branch.BzrBranchFormat7',
2252
 
    tree_format='bzrlib.workingtree_4.WorkingTreeFormat6',
 
3292
    tree_format='bzrlib.workingtree.WorkingTreeFormat6',
2253
3293
    experimental=True,
2254
3294
    hidden=True,
2255
3295
    alias=False,
2266
3306
        # 'storage in packs, 255-way hashed CHK inventory, bencode revision, group compress, '
2267
3307
        # 'rich roots. Supported by bzr 1.16 and later.',
2268
3308
    branch_format='bzrlib.branch.BzrBranchFormat7',
2269
 
    tree_format='bzrlib.workingtree_4.WorkingTreeFormat6',
 
3309
    tree_format='bzrlib.workingtree.WorkingTreeFormat6',
2270
3310
    experimental=False,
2271
3311
    )
2272
3312
 
2275
3315
register_metadir(controldir.format_registry, 'default-rich-root',
2276
3316
    'bzrlib.repofmt.groupcompress_repo.RepositoryFormat2a',
2277
3317
    branch_format='bzrlib.branch.BzrBranchFormat7',
2278
 
    tree_format='bzrlib.workingtree_4.WorkingTreeFormat6',
 
3318
    tree_format='bzrlib.workingtree.WorkingTreeFormat6',
2279
3319
    alias=True,
2280
3320
    hidden=True,
2281
3321
    help='Same as 2a.')