~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/bzrdir.py

  • Committer: Martin Pool
  • Date: 2011-01-20 23:07:25 UTC
  • mfrom: (5626 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5630.
  • Revision ID: mbp@canonical.com-20110120230725-12l7ltnko5x3fgnz
merge news

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,
 
41
    config,
 
42
    controldir,
36
43
    errors,
37
 
    fetch,
38
44
    graph,
39
45
    lockable_files,
40
46
    lockdir,
43
49
    remote,
44
50
    repository,
45
51
    revision as _mod_revision,
46
 
    transport as _mod_transport,
47
52
    ui,
48
53
    urlutils,
 
54
    versionedfile,
49
55
    win32utils,
50
 
    workingtree_3,
 
56
    workingtree,
51
57
    workingtree_4,
52
 
    )
53
 
from bzrlib.repofmt import knitpack_repo
 
58
    xml4,
 
59
    xml5,
 
60
    )
 
61
from bzrlib.osutils import (
 
62
    sha_string,
 
63
    )
 
64
from bzrlib.push import (
 
65
    PushResult,
 
66
    )
 
67
from bzrlib.repofmt import pack_repo
 
68
from bzrlib.smart.client import _SmartClient
 
69
from bzrlib.store.versioned import VersionedFileStore
 
70
from bzrlib.transactions import WriteTransaction
54
71
from bzrlib.transport import (
55
72
    do_catching_redirections,
 
73
    get_transport,
56
74
    local,
57
75
    )
58
 
from bzrlib.i18n import gettext
 
76
from bzrlib.weave import (
 
77
    WeaveFile,
 
78
    Weave,
 
79
    )
59
80
""")
60
81
 
61
82
from bzrlib.trace import (
62
83
    mutter,
63
84
    note,
 
85
    warning,
64
86
    )
65
87
 
66
88
from bzrlib import (
67
 
    config,
68
 
    controldir,
69
89
    hooks,
70
90
    registry,
 
91
    symbol_versioning,
71
92
    )
72
93
from bzrlib.symbol_versioning import (
73
94
    deprecated_in,
124
145
            # No repo, no problem.
125
146
            pass
126
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
 
127
176
    def clone_on_transport(self, transport, revision_id=None,
128
177
        force_new_repo=False, preserve_stacking=False, stacked_on=None,
129
178
        create_prefix=False, use_existing_dir=True, no_tree=False):
141
190
        :param create_prefix: Create any missing directories leading up to
142
191
            to_transport.
143
192
        :param use_existing_dir: Use an existing directory if one exists.
144
 
        :param no_tree: If set to true prevents creation of a working tree.
145
193
        """
146
194
        # Overview: put together a broad description of what we want to end up
147
195
        # with; then make as few api calls as possible to do it.
230
278
    # TODO: This should be given a Transport, and should chdir up; otherwise
231
279
    # this will open a new connection.
232
280
    def _make_tail(self, url):
233
 
        t = _mod_transport.get_transport(url)
 
281
        t = get_transport(url)
234
282
        t.ensure_base()
235
283
 
236
284
    @staticmethod
240
288
        This is intended primarily as a building block for more sophisticated
241
289
        functionality, like finding trees under a directory, or finding
242
290
        branches that use a given repository.
243
 
 
244
291
        :param evaluate: An optional callable that yields recurse, value,
245
292
            where recurse controls whether this bzrdir is recursed into
246
293
            and value is the value to yield.  By default, all bzrdirs
390
437
        policy = self.determine_repository_policy(force_new_repo)
391
438
        return policy.acquire_repository()[0]
392
439
 
393
 
    def _find_source_repo(self, add_cleanup, source_branch):
394
 
        """Find the source branch and repo for a sprout operation.
395
 
        
396
 
        This is helper intended for use by _sprout.
397
 
 
398
 
        :returns: (source_branch, source_repository).  Either or both may be
399
 
            None.  If not None, they will be read-locked (and their unlock(s)
400
 
            scheduled via the add_cleanup param).
401
 
        """
402
 
        if source_branch is not None:
403
 
            add_cleanup(source_branch.lock_read().unlock)
404
 
            return source_branch, source_branch.repository
405
 
        try:
406
 
            source_branch = self.open_branch()
407
 
            source_repository = source_branch.repository
408
 
        except errors.NotBranchError:
409
 
            source_branch = None
410
 
            try:
411
 
                source_repository = self.open_repository()
412
 
            except errors.NoRepositoryPresent:
413
 
                source_repository = None
414
 
            else:
415
 
                add_cleanup(source_repository.lock_read().unlock)
416
 
        else:
417
 
            add_cleanup(source_branch.lock_read().unlock)
418
 
        return source_branch, source_repository
419
 
 
420
 
    def sprout(self, url, revision_id=None, force_new_repo=False,
421
 
               recurse='down', possible_transports=None,
422
 
               accelerator_tree=None, hardlink=False, stacked=False,
423
 
               source_branch=None, create_tree_if_local=True):
424
 
        """Create a copy of this controldir prepared for use as a new line of
425
 
        development.
426
 
 
427
 
        If url's last component does not exist, it will be created.
428
 
 
429
 
        Attributes related to the identity of the source branch like
430
 
        branch nickname will be cleaned, a working tree is created
431
 
        whether one existed before or not; and a local branch is always
432
 
        created.
433
 
 
434
 
        if revision_id is not None, then the clone operation may tune
435
 
            itself to download less data.
436
 
 
437
 
        :param accelerator_tree: A tree which can be used for retrieving file
438
 
            contents more quickly than the revision tree, i.e. a workingtree.
439
 
            The revision tree will be used for cases where accelerator_tree's
440
 
            content is different.
441
 
        :param hardlink: If true, hard-link files from accelerator_tree,
442
 
            where possible.
443
 
        :param stacked: If true, create a stacked branch referring to the
444
 
            location of this control directory.
445
 
        :param create_tree_if_local: If true, a working-tree will be created
446
 
            when working locally.
447
 
        """
448
 
        operation = cleanup.OperationWithCleanups(self._sprout)
449
 
        return operation.run(url, revision_id=revision_id,
450
 
            force_new_repo=force_new_repo, recurse=recurse,
451
 
            possible_transports=possible_transports,
452
 
            accelerator_tree=accelerator_tree, hardlink=hardlink,
453
 
            stacked=stacked, source_branch=source_branch,
454
 
            create_tree_if_local=create_tree_if_local)
455
 
 
456
 
    def _sprout(self, op, url, revision_id=None, force_new_repo=False,
457
 
               recurse='down', possible_transports=None,
458
 
               accelerator_tree=None, hardlink=False, stacked=False,
459
 
               source_branch=None, create_tree_if_local=True):
460
 
        add_cleanup = op.add_cleanup
461
 
        fetch_spec_factory = fetch.FetchSpecFactory()
462
 
        if revision_id is not None:
463
 
            fetch_spec_factory.add_revision_ids([revision_id])
464
 
            fetch_spec_factory.source_branch_stop_revision_id = revision_id
465
 
        target_transport = _mod_transport.get_transport(url,
466
 
            possible_transports)
467
 
        target_transport.ensure_base()
468
 
        cloning_format = self.cloning_metadir(stacked)
469
 
        # Create/update the result branch
470
 
        result = cloning_format.initialize_on_transport(target_transport)
471
 
        source_branch, source_repository = self._find_source_repo(
472
 
            add_cleanup, source_branch)
473
 
        fetch_spec_factory.source_branch = source_branch
474
 
        # if a stacked branch wasn't requested, we don't create one
475
 
        # even if the origin was stacked
476
 
        if stacked and source_branch is not None:
477
 
            stacked_branch_url = self.root_transport.base
478
 
        else:
479
 
            stacked_branch_url = None
480
 
        repository_policy = result.determine_repository_policy(
481
 
            force_new_repo, stacked_branch_url, require_stacking=stacked)
482
 
        result_repo, is_new_repo = repository_policy.acquire_repository()
483
 
        add_cleanup(result_repo.lock_write().unlock)
484
 
        fetch_spec_factory.source_repo = source_repository
485
 
        fetch_spec_factory.target_repo = result_repo
486
 
        if stacked or (len(result_repo._fallback_repositories) != 0):
487
 
            target_repo_kind = fetch.TargetRepoKinds.STACKED
488
 
        elif is_new_repo:
489
 
            target_repo_kind = fetch.TargetRepoKinds.EMPTY
490
 
        else:
491
 
            target_repo_kind = fetch.TargetRepoKinds.PREEXISTING
492
 
        fetch_spec_factory.target_repo_kind = target_repo_kind
493
 
        if source_repository is not None:
494
 
            fetch_spec = fetch_spec_factory.make_fetch_spec()
495
 
            result_repo.fetch(source_repository, fetch_spec=fetch_spec)
496
 
 
497
 
        if source_branch is None:
498
 
            # this is for sprouting a controldir without a branch; is that
499
 
            # actually useful?
500
 
            # Not especially, but it's part of the contract.
501
 
            result_branch = result.create_branch()
502
 
        else:
503
 
            result_branch = source_branch.sprout(result,
504
 
                revision_id=revision_id, repository_policy=repository_policy,
505
 
                repository=result_repo)
506
 
        mutter("created new branch %r" % (result_branch,))
507
 
 
508
 
        # Create/update the result working tree
509
 
        if (create_tree_if_local and
510
 
            isinstance(target_transport, local.LocalTransport) and
511
 
            (result_repo is None or result_repo.make_working_trees())):
512
 
            wt = result.create_workingtree(accelerator_tree=accelerator_tree,
513
 
                hardlink=hardlink, from_branch=result_branch)
514
 
            wt.lock_write()
515
 
            try:
516
 
                if wt.path2id('') is None:
517
 
                    try:
518
 
                        wt.set_root_id(self.open_workingtree.get_root_id())
519
 
                    except errors.NoWorkingTree:
520
 
                        pass
521
 
            finally:
522
 
                wt.unlock()
523
 
        else:
524
 
            wt = None
525
 
        if recurse == 'down':
526
 
            basis = None
527
 
            if wt is not None:
528
 
                basis = wt.basis_tree()
529
 
            elif result_branch is not None:
530
 
                basis = result_branch.basis_tree()
531
 
            elif source_branch is not None:
532
 
                basis = source_branch.basis_tree()
533
 
            if basis is not None:
534
 
                add_cleanup(basis.lock_read().unlock)
535
 
                subtrees = basis.iter_references()
536
 
            else:
537
 
                subtrees = []
538
 
            for path, file_id in subtrees:
539
 
                target = urlutils.join(url, urlutils.escape(path))
540
 
                sublocation = source_branch.reference_parent(file_id, path)
541
 
                sublocation.bzrdir.sprout(target,
542
 
                    basis.get_reference_revision(file_id, path),
543
 
                    force_new_repo=force_new_repo, recurse=recurse,
544
 
                    stacked=stacked)
545
 
        return result
546
 
 
547
440
    @staticmethod
548
441
    def create_branch_convenience(base, force_new_repo=False,
549
442
                                  force_new_tree=None, format=None,
575
468
        """
576
469
        if force_new_tree:
577
470
            # check for non local urls
578
 
            t = _mod_transport.get_transport(base, possible_transports)
 
471
            t = get_transport(base, possible_transports)
579
472
            if not isinstance(t, local.LocalTransport):
580
473
                raise errors.NotLocalUrl(base)
581
474
        bzrdir = BzrDir.create(base, format, possible_transports)
603
496
        :param format: Override for the bzrdir format to create.
604
497
        :return: The WorkingTree object.
605
498
        """
606
 
        t = _mod_transport.get_transport(base)
 
499
        t = get_transport(base)
607
500
        if not isinstance(t, local.LocalTransport):
608
501
            raise errors.NotLocalUrl(base)
609
502
        bzrdir = BzrDir.create_branch_and_repo(base,
633
526
            old_path = self.root_transport.abspath('.bzr')
634
527
            backup_dir = self._available_backup_name('backup.bzr')
635
528
            new_path = self.root_transport.abspath(backup_dir)
636
 
            ui.ui_factory.note(gettext('making backup of {0}\n  to {1}').format(
637
 
                urlutils.unescape_for_display(old_path, 'utf-8'),
638
 
                urlutils.unescape_for_display(new_path, 'utf-8')))
 
529
            ui.ui_factory.note('making backup of %s\n  to %s'
 
530
                               % (old_path, new_path,))
639
531
            self.root_transport.copy_tree('.bzr', backup_dir)
640
532
            return (old_path, new_path)
641
533
        finally:
656
548
            try:
657
549
                to_path = '.bzr.retired.%d' % i
658
550
                self.root_transport.rename('.bzr', to_path)
659
 
                note(gettext("renamed {0} to {1}").format(
660
 
                    self.root_transport.abspath('.bzr'), to_path))
 
551
                note("renamed %s to %s"
 
552
                    % (self.root_transport.abspath('.bzr'), to_path))
661
553
                return
662
554
            except (errors.TransportError, IOError, errors.PathError):
663
555
                i += 1
824
716
 
825
717
        :param _unsupported: a private parameter to the BzrDir class.
826
718
        """
827
 
        t = _mod_transport.get_transport(base, possible_transports)
 
719
        t = get_transport(base, possible_transports=possible_transports)
828
720
        return BzrDir.open_from_transport(t, _unsupported=_unsupported)
829
721
 
830
722
    @staticmethod
848
740
            redirected_transport = transport._redirected_to(e.source, e.target)
849
741
            if redirected_transport is None:
850
742
                raise errors.NotBranchError(base)
851
 
            note(gettext('{0} is{1} redirected to {2}').format(
852
 
                 transport.base, e.permanently, redirected_transport.base))
 
743
            note('%s is%s redirected to %s',
 
744
                 transport.base, e.permanently, redirected_transport.base)
853
745
            return redirected_transport
854
746
 
855
747
        try:
859
751
        except errors.TooManyRedirections:
860
752
            raise errors.NotBranchError(base)
861
753
 
862
 
        format.check_support_status(_unsupported)
 
754
        BzrDir._check_supported(format, _unsupported)
863
755
        return format.open(transport, _found=True)
864
756
 
865
757
    @staticmethod
867
759
        """Open an existing branch which contains url.
868
760
 
869
761
        :param url: url to search from.
870
 
 
871
762
        See open_containing_from_transport for more detail.
872
763
        """
873
 
        transport = _mod_transport.get_transport(url, possible_transports)
 
764
        transport = get_transport(url, possible_transports)
874
765
        return BzrDir.open_containing_from_transport(transport)
875
766
 
876
767
    @staticmethod
1032
923
        if cls is not BzrDir:
1033
924
            raise AssertionError("BzrDir.create always creates the"
1034
925
                "default format, not one of %r" % cls)
1035
 
        t = _mod_transport.get_transport(base, possible_transports)
 
926
        t = get_transport(base, possible_transports)
1036
927
        t.ensure_base()
1037
928
        if format is None:
1038
929
            format = controldir.ControlDirFormat.get_default_format()
1039
930
        return format.initialize_on_transport(t)
1040
931
 
1041
 
    def get_branch_transport(self, branch_format, name=None):
1042
 
        """Get the transport for use by branch format in this BzrDir.
1043
 
 
1044
 
        Note that bzr dirs that do not support format strings will raise
1045
 
        IncompatibleFormat if the branch format they are given has
1046
 
        a format string, and vice versa.
1047
 
 
1048
 
        If branch_format is None, the transport is returned with no
1049
 
        checking. If it is not None, then the returned transport is
1050
 
        guaranteed to point to an existing directory ready for use.
1051
 
        """
1052
 
        raise NotImplementedError(self.get_branch_transport)
1053
 
 
1054
 
    def get_repository_transport(self, repository_format):
1055
 
        """Get the transport for use by repository format in this BzrDir.
1056
 
 
1057
 
        Note that bzr dirs that do not support format strings will raise
1058
 
        IncompatibleFormat if the repository format they are given has
1059
 
        a format string, and vice versa.
1060
 
 
1061
 
        If repository_format is None, the transport is returned with no
1062
 
        checking. If it is not None, then the returned transport is
1063
 
        guaranteed to point to an existing directory ready for use.
1064
 
        """
1065
 
        raise NotImplementedError(self.get_repository_transport)
1066
 
 
1067
 
    def get_workingtree_transport(self, tree_format):
1068
 
        """Get the transport for use by workingtree format in this BzrDir.
1069
 
 
1070
 
        Note that bzr dirs that do not support format strings will raise
1071
 
        IncompatibleFormat if the workingtree format they are given has a
1072
 
        format string, and vice versa.
1073
 
 
1074
 
        If workingtree_format is None, the transport is returned with no
1075
 
        checking. If it is not None, then the returned transport is
1076
 
        guaranteed to point to an existing directory ready for use.
1077
 
        """
1078
 
        raise NotImplementedError(self.get_workingtree_transport)
1079
 
 
1080
932
 
1081
933
class BzrDirHooks(hooks.Hooks):
1082
934
    """Hooks for BzrDir operations."""
1083
935
 
1084
936
    def __init__(self):
1085
937
        """Create the default hooks."""
1086
 
        hooks.Hooks.__init__(self, "bzrlib.bzrdir", "BzrDir.hooks")
1087
 
        self.add_hook('pre_open',
 
938
        hooks.Hooks.__init__(self)
 
939
        self.create_hook(hooks.HookPoint('pre_open',
1088
940
            "Invoked before attempting to open a BzrDir with the transport "
1089
 
            "that the open will use.", (1, 14))
1090
 
        self.add_hook('post_repo_init',
 
941
            "that the open will use.", (1, 14), None))
 
942
        self.create_hook(hooks.HookPoint('post_repo_init',
1091
943
            "Invoked after a repository has been initialized. "
1092
944
            "post_repo_init is called with a "
1093
945
            "bzrlib.bzrdir.RepoInitHookParams.",
1094
 
            (2, 2))
 
946
            (2, 2), None))
1095
947
 
1096
948
# install the default hooks
1097
949
BzrDir.hooks = BzrDirHooks()
1098
950
 
1099
951
 
1100
952
class RepoInitHookParams(object):
1101
 
    """Object holding parameters passed to `*_repo_init` hooks.
 
953
    """Object holding parameters passed to *_repo_init hooks.
1102
954
 
1103
955
    There are 4 fields that hooks may wish to access:
1104
956
 
1133
985
                self.bzrdir)
1134
986
 
1135
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
 
1136
1259
class BzrDirMeta1(BzrDir):
1137
1260
    """A .bzr meta version 1 control object.
1138
1261
 
1146
1269
        """See BzrDir.can_convert_format()."""
1147
1270
        return True
1148
1271
 
1149
 
    def create_branch(self, name=None, repository=None,
1150
 
            append_revisions_only=None):
 
1272
    def create_branch(self, name=None, repository=None):
1151
1273
        """See BzrDir.create_branch."""
1152
1274
        return self._format.get_branch_format().initialize(self, name=name,
1153
 
                repository=repository,
1154
 
                append_revisions_only=append_revisions_only)
 
1275
                repository=repository)
1155
1276
 
1156
1277
    def destroy_branch(self, name=None):
1157
1278
        """See BzrDir.create_branch."""
1257
1378
    def has_workingtree(self):
1258
1379
        """Tell if this bzrdir contains a working tree.
1259
1380
 
 
1381
        This will still raise an exception if the bzrdir has a workingtree that
 
1382
        is remote & inaccessible.
 
1383
 
1260
1384
        Note: if you're going to open the working tree, you should just go
1261
1385
        ahead and try, and not ask permission first.
1262
1386
        """
1263
1387
        from bzrlib.workingtree import WorkingTreeFormat
1264
1388
        try:
1265
 
            WorkingTreeFormat.find_format_string(self)
 
1389
            WorkingTreeFormat.find_format(self)
1266
1390
        except errors.NoWorkingTree:
1267
1391
            return False
1268
1392
        return True
1269
1393
 
1270
 
    def needs_format_conversion(self, format):
 
1394
    def needs_format_conversion(self, format=None):
1271
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()
1272
1401
        if not isinstance(self._format, format.__class__):
1273
1402
            # it is not a meta dir format, conversion is needed.
1274
1403
            return True
1299
1428
                    ignore_fallbacks=False):
1300
1429
        """See BzrDir.open_branch."""
1301
1430
        format = self.find_branch_format(name=name)
1302
 
        format.check_support_status(unsupported)
 
1431
        self._check_supported(format, unsupported)
1303
1432
        return format.open(self, name=name,
1304
1433
            _found=True, ignore_fallbacks=ignore_fallbacks)
1305
1434
 
1307
1436
        """See BzrDir.open_repository."""
1308
1437
        from bzrlib.repository import RepositoryFormat
1309
1438
        format = RepositoryFormat.find_format(self)
1310
 
        format.check_support_status(unsupported)
 
1439
        self._check_supported(format, unsupported)
1311
1440
        return format.open(self, _found=True)
1312
1441
 
1313
1442
    def open_workingtree(self, unsupported=False,
1315
1444
        """See BzrDir.open_workingtree."""
1316
1445
        from bzrlib.workingtree import WorkingTreeFormat
1317
1446
        format = WorkingTreeFormat.find_format(self)
1318
 
        format.check_support_status(unsupported, recommend_upgrade,
 
1447
        self._check_supported(format, unsupported,
 
1448
            recommend_upgrade,
1319
1449
            basedir=self.root_transport.base)
1320
1450
        return format.open(self, _found=True)
1321
1451
 
1326
1456
class BzrProber(controldir.Prober):
1327
1457
    """Prober for formats that use a .bzr/ control directory."""
1328
1458
 
1329
 
    formats = registry.FormatRegistry(controldir.network_format_registry)
 
1459
    _formats = {}
1330
1460
    """The known .bzr formats."""
1331
1461
 
1332
1462
    @classmethod
1333
 
    @deprecated_method(deprecated_in((2, 4, 0)))
1334
1463
    def register_bzrdir_format(klass, format):
1335
 
        klass.formats.register(format.get_format_string(), format)
 
1464
        klass._formats[format.get_format_string()] = format
1336
1465
 
1337
1466
    @classmethod
1338
 
    @deprecated_method(deprecated_in((2, 4, 0)))
1339
1467
    def unregister_bzrdir_format(klass, format):
1340
 
        klass.formats.remove(format.get_format_string())
 
1468
        del klass._formats[format.get_format_string()]
1341
1469
 
1342
1470
    @classmethod
1343
1471
    def probe_transport(klass, transport):
1347
1475
        except errors.NoSuchFile:
1348
1476
            raise errors.NotBranchError(path=transport.base)
1349
1477
        try:
1350
 
            return klass.formats.get(format_string)
 
1478
            return klass._formats[format_string]
1351
1479
        except KeyError:
1352
1480
            raise errors.UnknownFormatError(format=format_string, kind='bzrdir')
1353
1481
 
1354
 
    @classmethod
1355
 
    def known_formats(cls):
1356
 
        result = set()
1357
 
        for name, format in cls.formats.iteritems():
1358
 
            if callable(format):
1359
 
                format = format()
1360
 
            result.add(format)
1361
 
        return result
1362
 
 
1363
1482
 
1364
1483
controldir.ControlDirFormat.register_prober(BzrProber)
1365
1484
 
1389
1508
                    raise errors.NotBranchError(path=transport.base)
1390
1509
                if server_version != '2':
1391
1510
                    raise errors.NotBranchError(path=transport.base)
1392
 
            from bzrlib.remote import RemoteBzrDirFormat
1393
1511
            return RemoteBzrDirFormat()
1394
1512
 
1395
 
    @classmethod
1396
 
    def known_formats(cls):
1397
 
        from bzrlib.remote import RemoteBzrDirFormat
1398
 
        return set([RemoteBzrDirFormat()])
1399
 
 
1400
1513
 
1401
1514
class BzrDirFormat(controldir.ControlDirFormat):
1402
1515
    """ControlDirFormat base class for .bzr/ directories.
1415
1528
    # _lock_class must be set in subclasses to the lock type, typ.
1416
1529
    # TransportLock or LockDir
1417
1530
 
1418
 
    @classmethod
1419
 
    def get_format_string(cls):
 
1531
    def get_format_string(self):
1420
1532
        """Return the ASCII format string that identifies this format."""
1421
 
        raise NotImplementedError(cls.get_format_string)
 
1533
        raise NotImplementedError(self.get_format_string)
1422
1534
 
1423
1535
    def initialize_on_transport(self, transport):
1424
1536
        """Initialize a new bzrdir in the base directory of a Transport."""
1434
1546
            # metadir1
1435
1547
            if type(self) != BzrDirMetaFormat1:
1436
1548
                return self._initialize_on_transport_vfs(transport)
1437
 
            from bzrlib.remote import RemoteBzrDirFormat
1438
1549
            remote_format = RemoteBzrDirFormat()
1439
1550
            self._supply_sub_formats_to(remote_format)
1440
1551
            return remote_format.initialize_on_transport(transport)
1478
1589
            except errors.NoSmartMedium:
1479
1590
                pass
1480
1591
            else:
1481
 
                from bzrlib.remote import RemoteBzrDirFormat
1482
1592
                # TODO: lookup the local format from a server hint.
1483
1593
                remote_dir_format = RemoteBzrDirFormat()
1484
1594
                remote_dir_format._network_name = self.network_name()
1599
1709
        """
1600
1710
        raise NotImplementedError(self._open)
1601
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
 
1602
1719
    def _supply_sub_formats_to(self, other_format):
1603
1720
        """Give other_format the same values for sub formats as this has.
1604
1721
 
1611
1728
        :return: None.
1612
1729
        """
1613
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(get_transport(url), _cloning=True)
 
1850
 
 
1851
    def initialize_on_transport(self, transport, _cloning=False):
 
1852
        """Format 5 dirs always have working tree, branch and repository.
 
1853
 
 
1854
        Except when they are being cloned.
 
1855
        """
 
1856
        from bzrlib.branch import BzrBranchFormat4
 
1857
        from bzrlib.repofmt.weaverepo import RepositoryFormat5
 
1858
        result = (super(BzrDirFormat5, self).initialize_on_transport(transport))
 
1859
        RepositoryFormat5().initialize(result, _internal=True)
 
1860
        if not _cloning:
 
1861
            branch = BzrBranchFormat4().initialize(result)
 
1862
            result._init_workingtree()
 
1863
        return result
 
1864
 
 
1865
    def network_name(self):
 
1866
        return self.get_format_string()
 
1867
 
 
1868
    def _open(self, transport):
 
1869
        """See BzrDirFormat._open."""
 
1870
        return BzrDir5(transport, self)
 
1871
 
 
1872
    def __return_repository_format(self):
 
1873
        """Circular import protection."""
 
1874
        from bzrlib.repofmt.weaverepo import RepositoryFormat5
 
1875
        return RepositoryFormat5()
 
1876
    repository_format = property(__return_repository_format)
 
1877
 
 
1878
 
 
1879
class BzrDirFormat6(BzrDirFormatAllInOne):
 
1880
    """Bzr control format 6.
 
1881
 
 
1882
    This format is a combined format for working tree, branch and repository.
 
1883
    It has:
 
1884
     - Format 2 working trees [always]
 
1885
     - Format 4 branches [always]
 
1886
     - Format 6 repositories [always]
 
1887
    """
 
1888
 
 
1889
    _lock_class = lockable_files.TransportLock
 
1890
 
 
1891
    def get_format_string(self):
 
1892
        """See BzrDirFormat.get_format_string()."""
 
1893
        return "Bazaar-NG branch, format 6\n"
 
1894
 
 
1895
    def get_format_description(self):
 
1896
        """See BzrDirFormat.get_format_description()."""
 
1897
        return "All-in-one format 6"
 
1898
 
 
1899
    def get_branch_format(self):
 
1900
        from bzrlib import branch
 
1901
        return branch.BzrBranchFormat4()
 
1902
 
 
1903
    def get_converter(self, format=None):
 
1904
        """See BzrDirFormat.get_converter()."""
 
1905
        # there is one and only one upgrade path here.
 
1906
        return ConvertBzrDir6ToMeta()
 
1907
 
 
1908
    def _initialize_for_clone(self, url):
 
1909
        return self.initialize_on_transport(get_transport(url), _cloning=True)
 
1910
 
 
1911
    def initialize_on_transport(self, transport, _cloning=False):
 
1912
        """Format 6 dirs always have working tree, branch and repository.
 
1913
 
 
1914
        Except when they are being cloned.
 
1915
        """
 
1916
        from bzrlib.branch import BzrBranchFormat4
 
1917
        from bzrlib.repofmt.weaverepo import RepositoryFormat6
 
1918
        result = super(BzrDirFormat6, self).initialize_on_transport(transport)
 
1919
        RepositoryFormat6().initialize(result, _internal=True)
 
1920
        if not _cloning:
 
1921
            branch = BzrBranchFormat4().initialize(result)
 
1922
            result._init_workingtree()
 
1923
        return result
 
1924
 
 
1925
    def network_name(self):
 
1926
        return self.get_format_string()
 
1927
 
 
1928
    def _open(self, transport):
 
1929
        """See BzrDirFormat._open."""
 
1930
        return BzrDir6(transport, self)
 
1931
 
 
1932
    def __return_repository_format(self):
 
1933
        """Circular import protection."""
 
1934
        from bzrlib.repofmt.weaverepo import RepositoryFormat6
 
1935
        return RepositoryFormat6()
 
1936
    repository_format = property(__return_repository_format)
 
1937
 
1614
1938
 
1615
1939
class BzrDirMetaFormat1(BzrDirFormat):
1616
1940
    """Bzr meta control format 1
1617
1941
 
1618
1942
    This is the first format with split out working tree, branch and repository
1619
1943
    disk storage.
1620
 
 
1621
1944
    It has:
1622
 
 
1623
 
    - Format 3 working trees [optional]
1624
 
    - Format 5 branches [optional]
1625
 
    - Format 7 repositories [optional]
 
1945
     - Format 3 working trees [optional]
 
1946
     - Format 5 branches [optional]
 
1947
     - Format 7 repositories [optional]
1626
1948
    """
1627
1949
 
1628
1950
    _lock_class = lockdir.LockDir
1629
1951
 
1630
 
    fixed_components = False
1631
 
 
1632
1952
    def __init__(self):
1633
1953
        self._workingtree_format = None
1634
1954
        self._branch_format = None
1648
1968
 
1649
1969
    def get_branch_format(self):
1650
1970
        if self._branch_format is None:
1651
 
            from bzrlib.branch import format_registry as branch_format_registry
1652
 
            self._branch_format = branch_format_registry.get_default()
 
1971
            from bzrlib.branch import BranchFormat
 
1972
            self._branch_format = BranchFormat.get_default_format()
1653
1973
        return self._branch_format
1654
1974
 
1655
1975
    def set_branch_format(self, format):
1710
2030
                    # stack_on is inaccessible, JFDI.
1711
2031
                    # TODO: bad monkey, hard-coded formats...
1712
2032
                    if self.repository_format.rich_root_data:
1713
 
                        new_repo_format = knitpack_repo.RepositoryFormatKnitPack5RichRoot()
 
2033
                        new_repo_format = pack_repo.RepositoryFormatKnitPack5RichRoot()
1714
2034
                    else:
1715
 
                        new_repo_format = knitpack_repo.RepositoryFormatKnitPack5()
 
2035
                        new_repo_format = pack_repo.RepositoryFormatKnitPack5()
1716
2036
            else:
1717
2037
                # If the target already supports stacking, then we know the
1718
2038
                # project is already able to use stacking, so auto-upgrade
1724
2044
                    new_repo_format = None
1725
2045
            if new_repo_format is not None:
1726
2046
                self.repository_format = new_repo_format
1727
 
                note(gettext('Source repository format does not support stacking,'
1728
 
                     ' using format:\n  %s'),
 
2047
                note('Source repository format does not support stacking,'
 
2048
                     ' using format:\n  %s',
1729
2049
                     new_repo_format.get_format_description())
1730
2050
 
1731
2051
        if not self.get_branch_format().supports_stacking():
1735
2055
            if target_branch is None:
1736
2056
                if do_upgrade:
1737
2057
                    # TODO: bad monkey, hard-coded formats...
1738
 
                    from bzrlib.branch import BzrBranchFormat7
1739
 
                    new_branch_format = BzrBranchFormat7()
 
2058
                    new_branch_format = branch.BzrBranchFormat7()
1740
2059
            else:
1741
2060
                new_branch_format = target_branch._format
1742
2061
                if not new_branch_format.supports_stacking():
1744
2063
            if new_branch_format is not None:
1745
2064
                # Does support stacking, use its format.
1746
2065
                self.set_branch_format(new_branch_format)
1747
 
                note(gettext('Source branch format does not support stacking,'
1748
 
                     ' using format:\n  %s'),
 
2066
                note('Source branch format does not support stacking,'
 
2067
                     ' using format:\n  %s',
1749
2068
                     new_branch_format.get_format_description())
1750
2069
 
1751
2070
    def get_converter(self, format=None):
1757
2076
            raise NotImplementedError(self.get_converter)
1758
2077
        return ConvertMetaToMeta(format)
1759
2078
 
1760
 
    @classmethod
1761
 
    def get_format_string(cls):
 
2079
    def get_format_string(self):
1762
2080
        """See BzrDirFormat.get_format_string()."""
1763
2081
        return "Bazaar-NG meta directory, format 1\n"
1764
2082
 
1782
2100
        """Circular import protection."""
1783
2101
        if self._repository_format:
1784
2102
            return self._repository_format
1785
 
        from bzrlib.repository import format_registry
1786
 
        return format_registry.get_default()
 
2103
        from bzrlib.repository import RepositoryFormat
 
2104
        return RepositoryFormat.get_default_format()
1787
2105
 
1788
2106
    def _set_repository_format(self, value):
1789
2107
        """Allow changing the repository format for metadir formats."""
1812
2130
 
1813
2131
    def __get_workingtree_format(self):
1814
2132
        if self._workingtree_format is None:
1815
 
            from bzrlib.workingtree import (
1816
 
                format_registry as wt_format_registry,
1817
 
                )
1818
 
            self._workingtree_format = wt_format_registry.get_default()
 
2133
            from bzrlib.workingtree import WorkingTreeFormat
 
2134
            self._workingtree_format = WorkingTreeFormat.get_default_format()
1819
2135
        return self._workingtree_format
1820
2136
 
1821
2137
    def __set_workingtree_format(self, wt_format):
1826
2142
 
1827
2143
 
1828
2144
# Register bzr formats
1829
 
BzrProber.formats.register(BzrDirMetaFormat1.get_format_string(),
1830
 
    BzrDirMetaFormat1)
1831
 
controldir.ControlDirFormat._default_format = BzrDirMetaFormat1()
1832
 
 
1833
 
 
1834
 
class ConvertMetaToMeta(controldir.Converter):
 
2145
BzrDirFormat.register_format(BzrDirFormat4())
 
2146
BzrDirFormat.register_format(BzrDirFormat5())
 
2147
BzrDirFormat.register_format(BzrDirFormat6())
 
2148
__default_format = BzrDirMetaFormat1()
 
2149
BzrDirFormat.register_format(__default_format)
 
2150
controldir.ControlDirFormat._default_format = __default_format
 
2151
 
 
2152
 
 
2153
class Converter(object):
 
2154
    """Converts a disk format object from one format to another."""
 
2155
 
 
2156
    def convert(self, to_convert, pb):
 
2157
        """Perform the conversion of to_convert, giving feedback via pb.
 
2158
 
 
2159
        :param to_convert: The disk object to convert.
 
2160
        :param pb: a progress bar to use for progress information.
 
2161
        """
 
2162
 
 
2163
    def step(self, message):
 
2164
        """Update the pb by a step."""
 
2165
        self.count +=1
 
2166
        self.pb.update(message, self.count, self.total)
 
2167
 
 
2168
 
 
2169
class ConvertBzrDir4To5(Converter):
 
2170
    """Converts format 4 bzr dirs to format 5."""
 
2171
 
 
2172
    def __init__(self):
 
2173
        super(ConvertBzrDir4To5, self).__init__()
 
2174
        self.converted_revs = set()
 
2175
        self.absent_revisions = set()
 
2176
        self.text_count = 0
 
2177
        self.revisions = {}
 
2178
 
 
2179
    def convert(self, to_convert, pb):
 
2180
        """See Converter.convert()."""
 
2181
        self.bzrdir = to_convert
 
2182
        if pb is not None:
 
2183
            warnings.warn("pb parameter to convert() is deprecated")
 
2184
        self.pb = ui.ui_factory.nested_progress_bar()
 
2185
        try:
 
2186
            ui.ui_factory.note('starting upgrade from format 4 to 5')
 
2187
            if isinstance(self.bzrdir.transport, local.LocalTransport):
 
2188
                self.bzrdir.get_workingtree_transport(None).delete('stat-cache')
 
2189
            self._convert_to_weaves()
 
2190
            return BzrDir.open(self.bzrdir.user_url)
 
2191
        finally:
 
2192
            self.pb.finished()
 
2193
 
 
2194
    def _convert_to_weaves(self):
 
2195
        ui.ui_factory.note('note: upgrade may be faster if all store files are ungzipped first')
 
2196
        try:
 
2197
            # TODO permissions
 
2198
            stat = self.bzrdir.transport.stat('weaves')
 
2199
            if not S_ISDIR(stat.st_mode):
 
2200
                self.bzrdir.transport.delete('weaves')
 
2201
                self.bzrdir.transport.mkdir('weaves')
 
2202
        except errors.NoSuchFile:
 
2203
            self.bzrdir.transport.mkdir('weaves')
 
2204
        # deliberately not a WeaveFile as we want to build it up slowly.
 
2205
        self.inv_weave = Weave('inventory')
 
2206
        # holds in-memory weaves for all files
 
2207
        self.text_weaves = {}
 
2208
        self.bzrdir.transport.delete('branch-format')
 
2209
        self.branch = self.bzrdir.open_branch()
 
2210
        self._convert_working_inv()
 
2211
        rev_history = self.branch.revision_history()
 
2212
        # to_read is a stack holding the revisions we still need to process;
 
2213
        # appending to it adds new highest-priority revisions
 
2214
        self.known_revisions = set(rev_history)
 
2215
        self.to_read = rev_history[-1:]
 
2216
        while self.to_read:
 
2217
            rev_id = self.to_read.pop()
 
2218
            if (rev_id not in self.revisions
 
2219
                and rev_id not in self.absent_revisions):
 
2220
                self._load_one_rev(rev_id)
 
2221
        self.pb.clear()
 
2222
        to_import = self._make_order()
 
2223
        for i, rev_id in enumerate(to_import):
 
2224
            self.pb.update('converting revision', i, len(to_import))
 
2225
            self._convert_one_rev(rev_id)
 
2226
        self.pb.clear()
 
2227
        self._write_all_weaves()
 
2228
        self._write_all_revs()
 
2229
        ui.ui_factory.note('upgraded to weaves:')
 
2230
        ui.ui_factory.note('  %6d revisions and inventories' % len(self.revisions))
 
2231
        ui.ui_factory.note('  %6d revisions not present' % len(self.absent_revisions))
 
2232
        ui.ui_factory.note('  %6d texts' % self.text_count)
 
2233
        self._cleanup_spare_files_after_format4()
 
2234
        self.branch._transport.put_bytes(
 
2235
            'branch-format',
 
2236
            BzrDirFormat5().get_format_string(),
 
2237
            mode=self.bzrdir._get_file_mode())
 
2238
 
 
2239
    def _cleanup_spare_files_after_format4(self):
 
2240
        # FIXME working tree upgrade foo.
 
2241
        for n in 'merged-patches', 'pending-merged-patches':
 
2242
            try:
 
2243
                ## assert os.path.getsize(p) == 0
 
2244
                self.bzrdir.transport.delete(n)
 
2245
            except errors.NoSuchFile:
 
2246
                pass
 
2247
        self.bzrdir.transport.delete_tree('inventory-store')
 
2248
        self.bzrdir.transport.delete_tree('text-store')
 
2249
 
 
2250
    def _convert_working_inv(self):
 
2251
        inv = xml4.serializer_v4.read_inventory(
 
2252
                self.branch._transport.get('inventory'))
 
2253
        new_inv_xml = xml5.serializer_v5.write_inventory_to_string(inv, working=True)
 
2254
        self.branch._transport.put_bytes('inventory', new_inv_xml,
 
2255
            mode=self.bzrdir._get_file_mode())
 
2256
 
 
2257
    def _write_all_weaves(self):
 
2258
        controlweaves = VersionedFileStore(self.bzrdir.transport, prefixed=False,
 
2259
                                           versionedfile_class=WeaveFile)
 
2260
        weave_transport = self.bzrdir.transport.clone('weaves')
 
2261
        weaves = VersionedFileStore(weave_transport, prefixed=False,
 
2262
                            versionedfile_class=WeaveFile)
 
2263
        transaction = WriteTransaction()
 
2264
 
 
2265
        try:
 
2266
            i = 0
 
2267
            for file_id, file_weave in self.text_weaves.items():
 
2268
                self.pb.update('writing weave', i, len(self.text_weaves))
 
2269
                weaves._put_weave(file_id, file_weave, transaction)
 
2270
                i += 1
 
2271
            self.pb.update('inventory', 0, 1)
 
2272
            controlweaves._put_weave('inventory', self.inv_weave, transaction)
 
2273
            self.pb.update('inventory', 1, 1)
 
2274
        finally:
 
2275
            self.pb.clear()
 
2276
 
 
2277
    def _write_all_revs(self):
 
2278
        """Write all revisions out in new form."""
 
2279
        self.bzrdir.transport.delete_tree('revision-store')
 
2280
        self.bzrdir.transport.mkdir('revision-store')
 
2281
        revision_transport = self.bzrdir.transport.clone('revision-store')
 
2282
        # TODO permissions
 
2283
        from bzrlib.xml5 import serializer_v5
 
2284
        from bzrlib.repofmt.weaverepo import RevisionTextStore
 
2285
        revision_store = RevisionTextStore(revision_transport,
 
2286
            serializer_v5, False, versionedfile.PrefixMapper(),
 
2287
            lambda:True, lambda:True)
 
2288
        try:
 
2289
            for i, rev_id in enumerate(self.converted_revs):
 
2290
                self.pb.update('write revision', i, len(self.converted_revs))
 
2291
                text = serializer_v5.write_revision_to_string(
 
2292
                    self.revisions[rev_id])
 
2293
                key = (rev_id,)
 
2294
                revision_store.add_lines(key, None, osutils.split_lines(text))
 
2295
        finally:
 
2296
            self.pb.clear()
 
2297
 
 
2298
    def _load_one_rev(self, rev_id):
 
2299
        """Load a revision object into memory.
 
2300
 
 
2301
        Any parents not either loaded or abandoned get queued to be
 
2302
        loaded."""
 
2303
        self.pb.update('loading revision',
 
2304
                       len(self.revisions),
 
2305
                       len(self.known_revisions))
 
2306
        if not self.branch.repository.has_revision(rev_id):
 
2307
            self.pb.clear()
 
2308
            ui.ui_factory.note('revision {%s} not present in branch; '
 
2309
                         'will be converted as a ghost' %
 
2310
                         rev_id)
 
2311
            self.absent_revisions.add(rev_id)
 
2312
        else:
 
2313
            rev = self.branch.repository.get_revision(rev_id)
 
2314
            for parent_id in rev.parent_ids:
 
2315
                self.known_revisions.add(parent_id)
 
2316
                self.to_read.append(parent_id)
 
2317
            self.revisions[rev_id] = rev
 
2318
 
 
2319
    def _load_old_inventory(self, rev_id):
 
2320
        f = self.branch.repository.inventory_store.get(rev_id)
 
2321
        try:
 
2322
            old_inv_xml = f.read()
 
2323
        finally:
 
2324
            f.close()
 
2325
        inv = xml4.serializer_v4.read_inventory_from_string(old_inv_xml)
 
2326
        inv.revision_id = rev_id
 
2327
        rev = self.revisions[rev_id]
 
2328
        return inv
 
2329
 
 
2330
    def _load_updated_inventory(self, rev_id):
 
2331
        inv_xml = self.inv_weave.get_text(rev_id)
 
2332
        inv = xml5.serializer_v5.read_inventory_from_string(inv_xml, rev_id)
 
2333
        return inv
 
2334
 
 
2335
    def _convert_one_rev(self, rev_id):
 
2336
        """Convert revision and all referenced objects to new format."""
 
2337
        rev = self.revisions[rev_id]
 
2338
        inv = self._load_old_inventory(rev_id)
 
2339
        present_parents = [p for p in rev.parent_ids
 
2340
                           if p not in self.absent_revisions]
 
2341
        self._convert_revision_contents(rev, inv, present_parents)
 
2342
        self._store_new_inv(rev, inv, present_parents)
 
2343
        self.converted_revs.add(rev_id)
 
2344
 
 
2345
    def _store_new_inv(self, rev, inv, present_parents):
 
2346
        new_inv_xml = xml5.serializer_v5.write_inventory_to_string(inv)
 
2347
        new_inv_sha1 = sha_string(new_inv_xml)
 
2348
        self.inv_weave.add_lines(rev.revision_id,
 
2349
                                 present_parents,
 
2350
                                 new_inv_xml.splitlines(True))
 
2351
        rev.inventory_sha1 = new_inv_sha1
 
2352
 
 
2353
    def _convert_revision_contents(self, rev, inv, present_parents):
 
2354
        """Convert all the files within a revision.
 
2355
 
 
2356
        Also upgrade the inventory to refer to the text revision ids."""
 
2357
        rev_id = rev.revision_id
 
2358
        mutter('converting texts of revision {%s}',
 
2359
               rev_id)
 
2360
        parent_invs = map(self._load_updated_inventory, present_parents)
 
2361
        entries = inv.iter_entries()
 
2362
        entries.next()
 
2363
        for path, ie in entries:
 
2364
            self._convert_file_version(rev, ie, parent_invs)
 
2365
 
 
2366
    def _convert_file_version(self, rev, ie, parent_invs):
 
2367
        """Convert one version of one file.
 
2368
 
 
2369
        The file needs to be added into the weave if it is a merge
 
2370
        of >=2 parents or if it's changed from its parent.
 
2371
        """
 
2372
        file_id = ie.file_id
 
2373
        rev_id = rev.revision_id
 
2374
        w = self.text_weaves.get(file_id)
 
2375
        if w is None:
 
2376
            w = Weave(file_id)
 
2377
            self.text_weaves[file_id] = w
 
2378
        text_changed = False
 
2379
        parent_candiate_entries = ie.parent_candidates(parent_invs)
 
2380
        heads = graph.Graph(self).heads(parent_candiate_entries.keys())
 
2381
        # XXX: Note that this is unordered - and this is tolerable because
 
2382
        # the previous code was also unordered.
 
2383
        previous_entries = dict((head, parent_candiate_entries[head]) for head
 
2384
            in heads)
 
2385
        self.snapshot_ie(previous_entries, ie, w, rev_id)
 
2386
 
 
2387
    def get_parent_map(self, revision_ids):
 
2388
        """See graph.StackedParentsProvider.get_parent_map"""
 
2389
        return dict((revision_id, self.revisions[revision_id])
 
2390
                    for revision_id in revision_ids
 
2391
                     if revision_id in self.revisions)
 
2392
 
 
2393
    def snapshot_ie(self, previous_revisions, ie, w, rev_id):
 
2394
        # TODO: convert this logic, which is ~= snapshot to
 
2395
        # a call to:. This needs the path figured out. rather than a work_tree
 
2396
        # a v4 revision_tree can be given, or something that looks enough like
 
2397
        # one to give the file content to the entry if it needs it.
 
2398
        # and we need something that looks like a weave store for snapshot to
 
2399
        # save against.
 
2400
        #ie.snapshot(rev, PATH, previous_revisions, REVISION_TREE, InMemoryWeaveStore(self.text_weaves))
 
2401
        if len(previous_revisions) == 1:
 
2402
            previous_ie = previous_revisions.values()[0]
 
2403
            if ie._unchanged(previous_ie):
 
2404
                ie.revision = previous_ie.revision
 
2405
                return
 
2406
        if ie.has_text():
 
2407
            f = self.branch.repository._text_store.get(ie.text_id)
 
2408
            try:
 
2409
                file_lines = f.readlines()
 
2410
            finally:
 
2411
                f.close()
 
2412
            w.add_lines(rev_id, previous_revisions, file_lines)
 
2413
            self.text_count += 1
 
2414
        else:
 
2415
            w.add_lines(rev_id, previous_revisions, [])
 
2416
        ie.revision = rev_id
 
2417
 
 
2418
    def _make_order(self):
 
2419
        """Return a suitable order for importing revisions.
 
2420
 
 
2421
        The order must be such that an revision is imported after all
 
2422
        its (present) parents.
 
2423
        """
 
2424
        todo = set(self.revisions.keys())
 
2425
        done = self.absent_revisions.copy()
 
2426
        order = []
 
2427
        while todo:
 
2428
            # scan through looking for a revision whose parents
 
2429
            # are all done
 
2430
            for rev_id in sorted(list(todo)):
 
2431
                rev = self.revisions[rev_id]
 
2432
                parent_ids = set(rev.parent_ids)
 
2433
                if parent_ids.issubset(done):
 
2434
                    # can take this one now
 
2435
                    order.append(rev_id)
 
2436
                    todo.remove(rev_id)
 
2437
                    done.add(rev_id)
 
2438
        return order
 
2439
 
 
2440
 
 
2441
class ConvertBzrDir5To6(Converter):
 
2442
    """Converts format 5 bzr dirs to format 6."""
 
2443
 
 
2444
    def convert(self, to_convert, pb):
 
2445
        """See Converter.convert()."""
 
2446
        self.bzrdir = to_convert
 
2447
        pb = ui.ui_factory.nested_progress_bar()
 
2448
        try:
 
2449
            ui.ui_factory.note('starting upgrade from format 5 to 6')
 
2450
            self._convert_to_prefixed()
 
2451
            return BzrDir.open(self.bzrdir.user_url)
 
2452
        finally:
 
2453
            pb.finished()
 
2454
 
 
2455
    def _convert_to_prefixed(self):
 
2456
        from bzrlib.store import TransportStore
 
2457
        self.bzrdir.transport.delete('branch-format')
 
2458
        for store_name in ["weaves", "revision-store"]:
 
2459
            ui.ui_factory.note("adding prefixes to %s" % store_name)
 
2460
            store_transport = self.bzrdir.transport.clone(store_name)
 
2461
            store = TransportStore(store_transport, prefixed=True)
 
2462
            for urlfilename in store_transport.list_dir('.'):
 
2463
                filename = urlutils.unescape(urlfilename)
 
2464
                if (filename.endswith(".weave") or
 
2465
                    filename.endswith(".gz") or
 
2466
                    filename.endswith(".sig")):
 
2467
                    file_id, suffix = os.path.splitext(filename)
 
2468
                else:
 
2469
                    file_id = filename
 
2470
                    suffix = ''
 
2471
                new_name = store._mapper.map((file_id,)) + suffix
 
2472
                # FIXME keep track of the dirs made RBC 20060121
 
2473
                try:
 
2474
                    store_transport.move(filename, new_name)
 
2475
                except errors.NoSuchFile: # catches missing dirs strangely enough
 
2476
                    store_transport.mkdir(osutils.dirname(new_name))
 
2477
                    store_transport.move(filename, new_name)
 
2478
        self.bzrdir.transport.put_bytes(
 
2479
            'branch-format',
 
2480
            BzrDirFormat6().get_format_string(),
 
2481
            mode=self.bzrdir._get_file_mode())
 
2482
 
 
2483
 
 
2484
class ConvertBzrDir6ToMeta(Converter):
 
2485
    """Converts format 6 bzr dirs to metadirs."""
 
2486
 
 
2487
    def convert(self, to_convert, pb):
 
2488
        """See Converter.convert()."""
 
2489
        from bzrlib.repofmt.weaverepo import RepositoryFormat7
 
2490
        from bzrlib.branch import BzrBranchFormat5
 
2491
        self.bzrdir = to_convert
 
2492
        self.pb = ui.ui_factory.nested_progress_bar()
 
2493
        self.count = 0
 
2494
        self.total = 20 # the steps we know about
 
2495
        self.garbage_inventories = []
 
2496
        self.dir_mode = self.bzrdir._get_dir_mode()
 
2497
        self.file_mode = self.bzrdir._get_file_mode()
 
2498
 
 
2499
        ui.ui_factory.note('starting upgrade from format 6 to metadir')
 
2500
        self.bzrdir.transport.put_bytes(
 
2501
                'branch-format',
 
2502
                "Converting to format 6",
 
2503
                mode=self.file_mode)
 
2504
        # its faster to move specific files around than to open and use the apis...
 
2505
        # first off, nuke ancestry.weave, it was never used.
 
2506
        try:
 
2507
            self.step('Removing ancestry.weave')
 
2508
            self.bzrdir.transport.delete('ancestry.weave')
 
2509
        except errors.NoSuchFile:
 
2510
            pass
 
2511
        # find out whats there
 
2512
        self.step('Finding branch files')
 
2513
        last_revision = self.bzrdir.open_branch().last_revision()
 
2514
        bzrcontents = self.bzrdir.transport.list_dir('.')
 
2515
        for name in bzrcontents:
 
2516
            if name.startswith('basis-inventory.'):
 
2517
                self.garbage_inventories.append(name)
 
2518
        # create new directories for repository, working tree and branch
 
2519
        repository_names = [('inventory.weave', True),
 
2520
                            ('revision-store', True),
 
2521
                            ('weaves', True)]
 
2522
        self.step('Upgrading repository  ')
 
2523
        self.bzrdir.transport.mkdir('repository', mode=self.dir_mode)
 
2524
        self.make_lock('repository')
 
2525
        # we hard code the formats here because we are converting into
 
2526
        # the meta format. The meta format upgrader can take this to a
 
2527
        # future format within each component.
 
2528
        self.put_format('repository', RepositoryFormat7())
 
2529
        for entry in repository_names:
 
2530
            self.move_entry('repository', entry)
 
2531
 
 
2532
        self.step('Upgrading branch      ')
 
2533
        self.bzrdir.transport.mkdir('branch', mode=self.dir_mode)
 
2534
        self.make_lock('branch')
 
2535
        self.put_format('branch', BzrBranchFormat5())
 
2536
        branch_files = [('revision-history', True),
 
2537
                        ('branch-name', True),
 
2538
                        ('parent', False)]
 
2539
        for entry in branch_files:
 
2540
            self.move_entry('branch', entry)
 
2541
 
 
2542
        checkout_files = [('pending-merges', True),
 
2543
                          ('inventory', True),
 
2544
                          ('stat-cache', False)]
 
2545
        # If a mandatory checkout file is not present, the branch does not have
 
2546
        # a functional checkout. Do not create a checkout in the converted
 
2547
        # branch.
 
2548
        for name, mandatory in checkout_files:
 
2549
            if mandatory and name not in bzrcontents:
 
2550
                has_checkout = False
 
2551
                break
 
2552
        else:
 
2553
            has_checkout = True
 
2554
        if not has_checkout:
 
2555
            ui.ui_factory.note('No working tree.')
 
2556
            # If some checkout files are there, we may as well get rid of them.
 
2557
            for name, mandatory in checkout_files:
 
2558
                if name in bzrcontents:
 
2559
                    self.bzrdir.transport.delete(name)
 
2560
        else:
 
2561
            from bzrlib.workingtree import WorkingTreeFormat3
 
2562
            self.step('Upgrading working tree')
 
2563
            self.bzrdir.transport.mkdir('checkout', mode=self.dir_mode)
 
2564
            self.make_lock('checkout')
 
2565
            self.put_format(
 
2566
                'checkout', WorkingTreeFormat3())
 
2567
            self.bzrdir.transport.delete_multi(
 
2568
                self.garbage_inventories, self.pb)
 
2569
            for entry in checkout_files:
 
2570
                self.move_entry('checkout', entry)
 
2571
            if last_revision is not None:
 
2572
                self.bzrdir.transport.put_bytes(
 
2573
                    'checkout/last-revision', last_revision)
 
2574
        self.bzrdir.transport.put_bytes(
 
2575
            'branch-format',
 
2576
            BzrDirMetaFormat1().get_format_string(),
 
2577
            mode=self.file_mode)
 
2578
        self.pb.finished()
 
2579
        return BzrDir.open(self.bzrdir.user_url)
 
2580
 
 
2581
    def make_lock(self, name):
 
2582
        """Make a lock for the new control dir name."""
 
2583
        self.step('Make %s lock' % name)
 
2584
        ld = lockdir.LockDir(self.bzrdir.transport,
 
2585
                             '%s/lock' % name,
 
2586
                             file_modebits=self.file_mode,
 
2587
                             dir_modebits=self.dir_mode)
 
2588
        ld.create()
 
2589
 
 
2590
    def move_entry(self, new_dir, entry):
 
2591
        """Move then entry name into new_dir."""
 
2592
        name = entry[0]
 
2593
        mandatory = entry[1]
 
2594
        self.step('Moving %s' % name)
 
2595
        try:
 
2596
            self.bzrdir.transport.move(name, '%s/%s' % (new_dir, name))
 
2597
        except errors.NoSuchFile:
 
2598
            if mandatory:
 
2599
                raise
 
2600
 
 
2601
    def put_format(self, dirname, format):
 
2602
        self.bzrdir.transport.put_bytes('%s/format' % dirname,
 
2603
            format.get_format_string(),
 
2604
            self.file_mode)
 
2605
 
 
2606
 
 
2607
class ConvertMetaToMeta(Converter):
1835
2608
    """Converts the components of metadirs."""
1836
2609
 
1837
2610
    def __init__(self, target_format):
1855
2628
        else:
1856
2629
            if not isinstance(repo._format, self.target_format.repository_format.__class__):
1857
2630
                from bzrlib.repository import CopyConverter
1858
 
                ui.ui_factory.note(gettext('starting repository conversion'))
 
2631
                ui.ui_factory.note('starting repository conversion')
1859
2632
                converter = CopyConverter(self.target_format.repository_format)
1860
2633
                converter.convert(repo, pb)
1861
2634
        for branch in self.bzrdir.list_branches():
1862
2635
            # TODO: conversions of Branch and Tree should be done by
1863
2636
            # InterXFormat lookups/some sort of registry.
1864
2637
            # Avoid circular imports
 
2638
            from bzrlib import branch as _mod_branch
1865
2639
            old = branch._format.__class__
1866
2640
            new = self.target_format.get_branch_format().__class__
1867
2641
            while old != new:
1890
2664
        else:
1891
2665
            # TODO: conversions of Branch and Tree should be done by
1892
2666
            # InterXFormat lookups
1893
 
            if (isinstance(tree, workingtree_3.WorkingTree3) and
 
2667
            if (isinstance(tree, workingtree.WorkingTree3) and
1894
2668
                not isinstance(tree, workingtree_4.DirStateWorkingTree) and
1895
2669
                isinstance(self.target_format.workingtree_format,
1896
2670
                    workingtree_4.DirStateWorkingTreeFormat)):
1909
2683
        return to_convert
1910
2684
 
1911
2685
 
 
2686
# This is not in remote.py because it's relatively small, and needs to be
 
2687
# registered. Putting it in remote.py creates a circular import problem.
 
2688
# we can make it a lazy object if the control formats is turned into something
 
2689
# like a registry.
 
2690
class RemoteBzrDirFormat(BzrDirMetaFormat1):
 
2691
    """Format representing bzrdirs accessed via a smart server"""
 
2692
 
 
2693
    supports_workingtrees = False
 
2694
 
 
2695
    def __init__(self):
 
2696
        BzrDirMetaFormat1.__init__(self)
 
2697
        # XXX: It's a bit ugly that the network name is here, because we'd
 
2698
        # like to believe that format objects are stateless or at least
 
2699
        # immutable,  However, we do at least avoid mutating the name after
 
2700
        # it's returned.  See <https://bugs.launchpad.net/bzr/+bug/504102>
 
2701
        self._network_name = None
 
2702
 
 
2703
    def __repr__(self):
 
2704
        return "%s(_network_name=%r)" % (self.__class__.__name__,
 
2705
            self._network_name)
 
2706
 
 
2707
    def get_format_description(self):
 
2708
        if self._network_name:
 
2709
            real_format = controldir.network_format_registry.get(self._network_name)
 
2710
            return 'Remote: ' + real_format.get_format_description()
 
2711
        return 'bzr remote bzrdir'
 
2712
 
 
2713
    def get_format_string(self):
 
2714
        raise NotImplementedError(self.get_format_string)
 
2715
 
 
2716
    def network_name(self):
 
2717
        if self._network_name:
 
2718
            return self._network_name
 
2719
        else:
 
2720
            raise AssertionError("No network name set.")
 
2721
 
 
2722
    def initialize_on_transport(self, transport):
 
2723
        try:
 
2724
            # hand off the request to the smart server
 
2725
            client_medium = transport.get_smart_medium()
 
2726
        except errors.NoSmartMedium:
 
2727
            # TODO: lookup the local format from a server hint.
 
2728
            local_dir_format = BzrDirMetaFormat1()
 
2729
            return local_dir_format.initialize_on_transport(transport)
 
2730
        client = _SmartClient(client_medium)
 
2731
        path = client.remote_path_from_transport(transport)
 
2732
        try:
 
2733
            response = client.call('BzrDirFormat.initialize', path)
 
2734
        except errors.ErrorFromSmartServer, err:
 
2735
            remote._translate_error(err, path=path)
 
2736
        if response[0] != 'ok':
 
2737
            raise errors.SmartProtocolError('unexpected response code %s' % (response,))
 
2738
        format = RemoteBzrDirFormat()
 
2739
        self._supply_sub_formats_to(format)
 
2740
        return remote.RemoteBzrDir(transport, format)
 
2741
 
 
2742
    def parse_NoneTrueFalse(self, arg):
 
2743
        if not arg:
 
2744
            return None
 
2745
        if arg == 'False':
 
2746
            return False
 
2747
        if arg == 'True':
 
2748
            return True
 
2749
        raise AssertionError("invalid arg %r" % arg)
 
2750
 
 
2751
    def _serialize_NoneTrueFalse(self, arg):
 
2752
        if arg is False:
 
2753
            return 'False'
 
2754
        if arg:
 
2755
            return 'True'
 
2756
        return ''
 
2757
 
 
2758
    def _serialize_NoneString(self, arg):
 
2759
        return arg or ''
 
2760
 
 
2761
    def initialize_on_transport_ex(self, transport, use_existing_dir=False,
 
2762
        create_prefix=False, force_new_repo=False, stacked_on=None,
 
2763
        stack_on_pwd=None, repo_format_name=None, make_working_trees=None,
 
2764
        shared_repo=False):
 
2765
        try:
 
2766
            # hand off the request to the smart server
 
2767
            client_medium = transport.get_smart_medium()
 
2768
        except errors.NoSmartMedium:
 
2769
            do_vfs = True
 
2770
        else:
 
2771
            # Decline to open it if the server doesn't support our required
 
2772
            # version (3) so that the VFS-based transport will do it.
 
2773
            if client_medium.should_probe():
 
2774
                try:
 
2775
                    server_version = client_medium.protocol_version()
 
2776
                    if server_version != '2':
 
2777
                        do_vfs = True
 
2778
                    else:
 
2779
                        do_vfs = False
 
2780
                except errors.SmartProtocolError:
 
2781
                    # Apparently there's no usable smart server there, even though
 
2782
                    # the medium supports the smart protocol.
 
2783
                    do_vfs = True
 
2784
            else:
 
2785
                do_vfs = False
 
2786
        if not do_vfs:
 
2787
            client = _SmartClient(client_medium)
 
2788
            path = client.remote_path_from_transport(transport)
 
2789
            if client_medium._is_remote_before((1, 16)):
 
2790
                do_vfs = True
 
2791
        if do_vfs:
 
2792
            # TODO: lookup the local format from a server hint.
 
2793
            local_dir_format = BzrDirMetaFormat1()
 
2794
            self._supply_sub_formats_to(local_dir_format)
 
2795
            return local_dir_format.initialize_on_transport_ex(transport,
 
2796
                use_existing_dir=use_existing_dir, create_prefix=create_prefix,
 
2797
                force_new_repo=force_new_repo, stacked_on=stacked_on,
 
2798
                stack_on_pwd=stack_on_pwd, repo_format_name=repo_format_name,
 
2799
                make_working_trees=make_working_trees, shared_repo=shared_repo,
 
2800
                vfs_only=True)
 
2801
        return self._initialize_on_transport_ex_rpc(client, path, transport,
 
2802
            use_existing_dir, create_prefix, force_new_repo, stacked_on,
 
2803
            stack_on_pwd, repo_format_name, make_working_trees, shared_repo)
 
2804
 
 
2805
    def _initialize_on_transport_ex_rpc(self, client, path, transport,
 
2806
        use_existing_dir, create_prefix, force_new_repo, stacked_on,
 
2807
        stack_on_pwd, repo_format_name, make_working_trees, shared_repo):
 
2808
        args = []
 
2809
        args.append(self._serialize_NoneTrueFalse(use_existing_dir))
 
2810
        args.append(self._serialize_NoneTrueFalse(create_prefix))
 
2811
        args.append(self._serialize_NoneTrueFalse(force_new_repo))
 
2812
        args.append(self._serialize_NoneString(stacked_on))
 
2813
        # stack_on_pwd is often/usually our transport
 
2814
        if stack_on_pwd:
 
2815
            try:
 
2816
                stack_on_pwd = transport.relpath(stack_on_pwd)
 
2817
                if not stack_on_pwd:
 
2818
                    stack_on_pwd = '.'
 
2819
            except errors.PathNotChild:
 
2820
                pass
 
2821
        args.append(self._serialize_NoneString(stack_on_pwd))
 
2822
        args.append(self._serialize_NoneString(repo_format_name))
 
2823
        args.append(self._serialize_NoneTrueFalse(make_working_trees))
 
2824
        args.append(self._serialize_NoneTrueFalse(shared_repo))
 
2825
        request_network_name = self._network_name or \
 
2826
            BzrDirFormat.get_default_format().network_name()
 
2827
        try:
 
2828
            response = client.call('BzrDirFormat.initialize_ex_1.16',
 
2829
                request_network_name, path, *args)
 
2830
        except errors.UnknownSmartMethod:
 
2831
            client._medium._remember_remote_is_before((1,16))
 
2832
            local_dir_format = BzrDirMetaFormat1()
 
2833
            self._supply_sub_formats_to(local_dir_format)
 
2834
            return local_dir_format.initialize_on_transport_ex(transport,
 
2835
                use_existing_dir=use_existing_dir, create_prefix=create_prefix,
 
2836
                force_new_repo=force_new_repo, stacked_on=stacked_on,
 
2837
                stack_on_pwd=stack_on_pwd, repo_format_name=repo_format_name,
 
2838
                make_working_trees=make_working_trees, shared_repo=shared_repo,
 
2839
                vfs_only=True)
 
2840
        except errors.ErrorFromSmartServer, err:
 
2841
            remote._translate_error(err, path=path)
 
2842
        repo_path = response[0]
 
2843
        bzrdir_name = response[6]
 
2844
        require_stacking = response[7]
 
2845
        require_stacking = self.parse_NoneTrueFalse(require_stacking)
 
2846
        format = RemoteBzrDirFormat()
 
2847
        format._network_name = bzrdir_name
 
2848
        self._supply_sub_formats_to(format)
 
2849
        bzrdir = remote.RemoteBzrDir(transport, format, _client=client)
 
2850
        if repo_path:
 
2851
            repo_format = remote.response_tuple_to_repo_format(response[1:])
 
2852
            if repo_path == '.':
 
2853
                repo_path = ''
 
2854
            if repo_path:
 
2855
                repo_bzrdir_format = RemoteBzrDirFormat()
 
2856
                repo_bzrdir_format._network_name = response[5]
 
2857
                repo_bzr = remote.RemoteBzrDir(transport.clone(repo_path),
 
2858
                    repo_bzrdir_format)
 
2859
            else:
 
2860
                repo_bzr = bzrdir
 
2861
            final_stack = response[8] or None
 
2862
            final_stack_pwd = response[9] or None
 
2863
            if final_stack_pwd:
 
2864
                final_stack_pwd = urlutils.join(
 
2865
                    transport.base, final_stack_pwd)
 
2866
            remote_repo = remote.RemoteRepository(repo_bzr, repo_format)
 
2867
            if len(response) > 10:
 
2868
                # Updated server verb that locks remotely.
 
2869
                repo_lock_token = response[10] or None
 
2870
                remote_repo.lock_write(repo_lock_token, _skip_rpc=True)
 
2871
                if repo_lock_token:
 
2872
                    remote_repo.dont_leave_lock_in_place()
 
2873
            else:
 
2874
                remote_repo.lock_write()
 
2875
            policy = UseExistingRepository(remote_repo, final_stack,
 
2876
                final_stack_pwd, require_stacking)
 
2877
            policy.acquire_repository()
 
2878
        else:
 
2879
            remote_repo = None
 
2880
            policy = None
 
2881
        bzrdir._format.set_branch_format(self.get_branch_format())
 
2882
        if require_stacking:
 
2883
            # The repo has already been created, but we need to make sure that
 
2884
            # we'll make a stackable branch.
 
2885
            bzrdir._format.require_stacking(_skip_repo=True)
 
2886
        return remote_repo, bzrdir, require_stacking, policy
 
2887
 
 
2888
    def _open(self, transport):
 
2889
        return remote.RemoteBzrDir(transport, self)
 
2890
 
 
2891
    def __eq__(self, other):
 
2892
        if not isinstance(other, RemoteBzrDirFormat):
 
2893
            return False
 
2894
        return self.get_format_description() == other.get_format_description()
 
2895
 
 
2896
    def __return_repository_format(self):
 
2897
        # Always return a RemoteRepositoryFormat object, but if a specific bzr
 
2898
        # repository format has been asked for, tell the RemoteRepositoryFormat
 
2899
        # that it should use that for init() etc.
 
2900
        result = remote.RemoteRepositoryFormat()
 
2901
        custom_format = getattr(self, '_repository_format', None)
 
2902
        if custom_format:
 
2903
            if isinstance(custom_format, remote.RemoteRepositoryFormat):
 
2904
                return custom_format
 
2905
            else:
 
2906
                # We will use the custom format to create repositories over the
 
2907
                # wire; expose its details like rich_root_data for code to
 
2908
                # query
 
2909
                result._custom_format = custom_format
 
2910
        return result
 
2911
 
 
2912
    def get_branch_format(self):
 
2913
        result = BzrDirMetaFormat1.get_branch_format(self)
 
2914
        if not isinstance(result, remote.RemoteBranchFormat):
 
2915
            new_result = remote.RemoteBranchFormat()
 
2916
            new_result._custom_format = result
 
2917
            # cache the result
 
2918
            self.set_branch_format(new_result)
 
2919
            result = new_result
 
2920
        return result
 
2921
 
 
2922
    repository_format = property(__return_repository_format,
 
2923
        BzrDirMetaFormat1._set_repository_format) #.im_func)
 
2924
 
 
2925
 
1912
2926
controldir.ControlDirFormat.register_server_prober(RemoteBzrProber)
1913
2927
 
1914
2928
 
1996
3010
 
1997
3011
        Implementations may create a new repository or use a pre-exising
1998
3012
        repository.
1999
 
 
2000
3013
        :param make_working_trees: If creating a repository, set
2001
3014
            make_working_trees to this value (if non-None)
2002
3015
        :param shared: If creating a repository, make it shared if True
2011
3024
 
2012
3025
    def __init__(self, bzrdir, stack_on=None, stack_on_pwd=None,
2013
3026
                 require_stacking=False):
2014
 
        """Constructor.
2015
 
 
 
3027
        """
 
3028
        Constructor.
2016
3029
        :param bzrdir: The bzrdir to create the repository on.
2017
3030
        :param stack_on: A location to stack on
2018
3031
        :param stack_on_pwd: If stack_on is relative, the location it is
2034
3047
                                    possible_transports=[self._bzrdir.root_transport])
2035
3048
            if not self._require_stacking:
2036
3049
                # We have picked up automatic stacking somewhere.
2037
 
                note(gettext('Using default stacking branch {0} at {1}').format(
2038
 
                    self._stack_on, self._stack_on_pwd))
 
3050
                note('Using default stacking branch %s at %s', self._stack_on,
 
3051
                    self._stack_on_pwd)
2039
3052
        repository = self._bzrdir.create_repository(shared=shared)
2040
3053
        self._add_fallback(repository,
2041
3054
                           possible_transports=[self._bzrdir.transport])
2114
3127
    registry.register(key, helper, help, native, deprecated, hidden,
2115
3128
        experimental, alias)
2116
3129
 
 
3130
# The pre-0.8 formats have their repository format network name registered in
 
3131
# repository.py. MetaDir formats have their repository format network name
 
3132
# inferred from their disk format string.
 
3133
controldir.format_registry.register('weave', BzrDirFormat6,
 
3134
    'Pre-0.8 format.  Slower than knit and does not'
 
3135
    ' support checkouts or shared repositories.',
 
3136
    hidden=True,
 
3137
    deprecated=True)
 
3138
register_metadir(controldir.format_registry, 'metaweave',
 
3139
    'bzrlib.repofmt.weaverepo.RepositoryFormat7',
 
3140
    'Transitional format in 0.8.  Slower than knit.',
 
3141
    branch_format='bzrlib.branch.BzrBranchFormat5',
 
3142
    tree_format='bzrlib.workingtree.WorkingTreeFormat3',
 
3143
    hidden=True,
 
3144
    deprecated=True)
2117
3145
register_metadir(controldir.format_registry, 'knit',
2118
3146
    'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
2119
3147
    'Format using knits.  Recommended for interoperation with bzr <= 0.14.',
2120
3148
    branch_format='bzrlib.branch.BzrBranchFormat5',
2121
 
    tree_format='bzrlib.workingtree_3.WorkingTreeFormat3',
 
3149
    tree_format='bzrlib.workingtree.WorkingTreeFormat3',
2122
3150
    hidden=True,
2123
3151
    deprecated=True)
2124
3152
register_metadir(controldir.format_registry, 'dirstate',
2126
3154
    help='New in 0.15: Fast local operations. Compatible with bzr 0.8 and '
2127
3155
        'above when accessed over the network.',
2128
3156
    branch_format='bzrlib.branch.BzrBranchFormat5',
2129
 
    tree_format='bzrlib.workingtree_4.WorkingTreeFormat4',
 
3157
    # this uses bzrlib.workingtree.WorkingTreeFormat4 because importing
 
3158
    # directly from workingtree_4 triggers a circular import.
 
3159
    tree_format='bzrlib.workingtree.WorkingTreeFormat4',
2130
3160
    hidden=True,
2131
3161
    deprecated=True)
2132
3162
register_metadir(controldir.format_registry, 'dirstate-tags',
2135
3165
        'network operations. Additionally adds support for tags.'
2136
3166
        ' Incompatible with bzr < 0.15.',
2137
3167
    branch_format='bzrlib.branch.BzrBranchFormat6',
2138
 
    tree_format='bzrlib.workingtree_4.WorkingTreeFormat4',
 
3168
    tree_format='bzrlib.workingtree.WorkingTreeFormat4',
2139
3169
    hidden=True,
2140
3170
    deprecated=True)
2141
3171
register_metadir(controldir.format_registry, 'rich-root',
2143
3173
    help='New in 1.0.  Better handling of tree roots.  Incompatible with'
2144
3174
        ' bzr < 1.0.',
2145
3175
    branch_format='bzrlib.branch.BzrBranchFormat6',
2146
 
    tree_format='bzrlib.workingtree_4.WorkingTreeFormat4',
 
3176
    tree_format='bzrlib.workingtree.WorkingTreeFormat4',
2147
3177
    hidden=True,
2148
3178
    deprecated=True)
2149
3179
register_metadir(controldir.format_registry, 'dirstate-with-subtree',
2152
3182
        'network operations. Additionally adds support for versioning nested '
2153
3183
        'bzr branches. Incompatible with bzr < 0.15.',
2154
3184
    branch_format='bzrlib.branch.BzrBranchFormat6',
2155
 
    tree_format='bzrlib.workingtree_4.WorkingTreeFormat4',
 
3185
    tree_format='bzrlib.workingtree.WorkingTreeFormat4',
2156
3186
    experimental=True,
2157
3187
    hidden=True,
2158
3188
    )
2159
3189
register_metadir(controldir.format_registry, 'pack-0.92',
2160
 
    'bzrlib.repofmt.knitpack_repo.RepositoryFormatKnitPack1',
 
3190
    'bzrlib.repofmt.pack_repo.RepositoryFormatKnitPack1',
2161
3191
    help='New in 0.92: Pack-based format with data compatible with '
2162
3192
        'dirstate-tags format repositories. Interoperates with '
2163
3193
        'bzr repositories before 0.92 but cannot be read by bzr < 0.92. '
2164
3194
        ,
2165
3195
    branch_format='bzrlib.branch.BzrBranchFormat6',
2166
 
    tree_format='bzrlib.workingtree_4.WorkingTreeFormat4',
 
3196
    tree_format='bzrlib.workingtree.WorkingTreeFormat4',
2167
3197
    )
2168
3198
register_metadir(controldir.format_registry, 'pack-0.92-subtree',
2169
 
    'bzrlib.repofmt.knitpack_repo.RepositoryFormatKnitPack3',
 
3199
    'bzrlib.repofmt.pack_repo.RepositoryFormatKnitPack3',
2170
3200
    help='New in 0.92: Pack-based format with data compatible with '
2171
3201
        'dirstate-with-subtree format repositories. Interoperates with '
2172
3202
        'bzr repositories before 0.92 but cannot be read by bzr < 0.92. '
2173
3203
        ,
2174
3204
    branch_format='bzrlib.branch.BzrBranchFormat6',
2175
 
    tree_format='bzrlib.workingtree_4.WorkingTreeFormat4',
 
3205
    tree_format='bzrlib.workingtree.WorkingTreeFormat4',
2176
3206
    hidden=True,
2177
3207
    experimental=True,
2178
3208
    )
2179
3209
register_metadir(controldir.format_registry, 'rich-root-pack',
2180
 
    'bzrlib.repofmt.knitpack_repo.RepositoryFormatKnitPack4',
 
3210
    'bzrlib.repofmt.pack_repo.RepositoryFormatKnitPack4',
2181
3211
    help='New in 1.0: A variant of pack-0.92 that supports rich-root data '
2182
3212
         '(needed for bzr-svn and bzr-git).',
2183
3213
    branch_format='bzrlib.branch.BzrBranchFormat6',
2184
 
    tree_format='bzrlib.workingtree_4.WorkingTreeFormat4',
 
3214
    tree_format='bzrlib.workingtree.WorkingTreeFormat4',
2185
3215
    hidden=True,
2186
3216
    )
2187
3217
register_metadir(controldir.format_registry, '1.6',
2188
 
    'bzrlib.repofmt.knitpack_repo.RepositoryFormatKnitPack5',
 
3218
    'bzrlib.repofmt.pack_repo.RepositoryFormatKnitPack5',
2189
3219
    help='A format that allows a branch to indicate that there is another '
2190
3220
         '(stacked) repository that should be used to access data that is '
2191
3221
         'not present locally.',
2192
3222
    branch_format='bzrlib.branch.BzrBranchFormat7',
2193
 
    tree_format='bzrlib.workingtree_4.WorkingTreeFormat4',
 
3223
    tree_format='bzrlib.workingtree.WorkingTreeFormat4',
2194
3224
    hidden=True,
2195
3225
    )
2196
3226
register_metadir(controldir.format_registry, '1.6.1-rich-root',
2197
 
    'bzrlib.repofmt.knitpack_repo.RepositoryFormatKnitPack5RichRoot',
 
3227
    'bzrlib.repofmt.pack_repo.RepositoryFormatKnitPack5RichRoot',
2198
3228
    help='A variant of 1.6 that supports rich-root data '
2199
3229
         '(needed for bzr-svn and bzr-git).',
2200
3230
    branch_format='bzrlib.branch.BzrBranchFormat7',
2201
 
    tree_format='bzrlib.workingtree_4.WorkingTreeFormat4',
 
3231
    tree_format='bzrlib.workingtree.WorkingTreeFormat4',
2202
3232
    hidden=True,
2203
3233
    )
2204
3234
register_metadir(controldir.format_registry, '1.9',
2205
 
    'bzrlib.repofmt.knitpack_repo.RepositoryFormatKnitPack6',
 
3235
    'bzrlib.repofmt.pack_repo.RepositoryFormatKnitPack6',
2206
3236
    help='A repository format using B+tree indexes. These indexes '
2207
3237
         'are smaller in size, have smarter caching and provide faster '
2208
3238
         'performance for most operations.',
2209
3239
    branch_format='bzrlib.branch.BzrBranchFormat7',
2210
 
    tree_format='bzrlib.workingtree_4.WorkingTreeFormat4',
 
3240
    tree_format='bzrlib.workingtree.WorkingTreeFormat4',
2211
3241
    hidden=True,
2212
3242
    )
2213
3243
register_metadir(controldir.format_registry, '1.9-rich-root',
2214
 
    'bzrlib.repofmt.knitpack_repo.RepositoryFormatKnitPack6RichRoot',
 
3244
    'bzrlib.repofmt.pack_repo.RepositoryFormatKnitPack6RichRoot',
2215
3245
    help='A variant of 1.9 that supports rich-root data '
2216
3246
         '(needed for bzr-svn and bzr-git).',
2217
3247
    branch_format='bzrlib.branch.BzrBranchFormat7',
2218
 
    tree_format='bzrlib.workingtree_4.WorkingTreeFormat4',
 
3248
    tree_format='bzrlib.workingtree.WorkingTreeFormat4',
2219
3249
    hidden=True,
2220
3250
    )
2221
3251
register_metadir(controldir.format_registry, '1.14',
2222
 
    'bzrlib.repofmt.knitpack_repo.RepositoryFormatKnitPack6',
 
3252
    'bzrlib.repofmt.pack_repo.RepositoryFormatKnitPack6',
2223
3253
    help='A working-tree format that supports content filtering.',
2224
3254
    branch_format='bzrlib.branch.BzrBranchFormat7',
2225
 
    tree_format='bzrlib.workingtree_4.WorkingTreeFormat5',
 
3255
    tree_format='bzrlib.workingtree.WorkingTreeFormat5',
2226
3256
    )
2227
3257
register_metadir(controldir.format_registry, '1.14-rich-root',
2228
 
    'bzrlib.repofmt.knitpack_repo.RepositoryFormatKnitPack6RichRoot',
 
3258
    'bzrlib.repofmt.pack_repo.RepositoryFormatKnitPack6RichRoot',
2229
3259
    help='A variant of 1.14 that supports rich-root data '
2230
3260
         '(needed for bzr-svn and bzr-git).',
2231
3261
    branch_format='bzrlib.branch.BzrBranchFormat7',
2232
 
    tree_format='bzrlib.workingtree_4.WorkingTreeFormat5',
 
3262
    tree_format='bzrlib.workingtree.WorkingTreeFormat5',
2233
3263
    )
2234
3264
# The following un-numbered 'development' formats should always just be aliases.
2235
3265
register_metadir(controldir.format_registry, 'development-subtree',
2241
3271
        'http://doc.bazaar.canonical.com/latest/developers/development-repo.html '
2242
3272
        'before use.',
2243
3273
    branch_format='bzrlib.branch.BzrBranchFormat7',
2244
 
    tree_format='bzrlib.workingtree_4.WorkingTreeFormat6',
 
3274
    tree_format='bzrlib.workingtree.WorkingTreeFormat6',
2245
3275
    experimental=True,
2246
3276
    hidden=True,
2247
3277
    alias=False, # Restore to being an alias when an actual development subtree format is added
2249
3279
                 # chk based subtree format.
2250
3280
    )
2251
3281
register_metadir(controldir.format_registry, 'development5-subtree',
2252
 
    'bzrlib.repofmt.knitpack_repo.RepositoryFormatPackDevelopment2Subtree',
 
3282
    'bzrlib.repofmt.pack_repo.RepositoryFormatPackDevelopment2Subtree',
2253
3283
    help='Development format, subtree variant. Can convert data to and '
2254
3284
        'from pack-0.92-subtree (and anything compatible with '
2255
3285
        'pack-0.92-subtree) format repositories. Repositories and branches in '
2257
3287
        'http://doc.bazaar.canonical.com/latest/developers/development-repo.html '
2258
3288
        'before use.',
2259
3289
    branch_format='bzrlib.branch.BzrBranchFormat7',
2260
 
    tree_format='bzrlib.workingtree_4.WorkingTreeFormat6',
 
3290
    tree_format='bzrlib.workingtree.WorkingTreeFormat6',
2261
3291
    experimental=True,
2262
3292
    hidden=True,
2263
3293
    alias=False,
2274
3304
        # 'storage in packs, 255-way hashed CHK inventory, bencode revision, group compress, '
2275
3305
        # 'rich roots. Supported by bzr 1.16 and later.',
2276
3306
    branch_format='bzrlib.branch.BzrBranchFormat7',
2277
 
    tree_format='bzrlib.workingtree_4.WorkingTreeFormat6',
 
3307
    tree_format='bzrlib.workingtree.WorkingTreeFormat6',
2278
3308
    experimental=False,
2279
3309
    )
2280
3310
 
2283
3313
register_metadir(controldir.format_registry, 'default-rich-root',
2284
3314
    'bzrlib.repofmt.groupcompress_repo.RepositoryFormat2a',
2285
3315
    branch_format='bzrlib.branch.BzrBranchFormat7',
2286
 
    tree_format='bzrlib.workingtree_4.WorkingTreeFormat6',
 
3316
    tree_format='bzrlib.workingtree.WorkingTreeFormat6',
2287
3317
    alias=True,
2288
3318
    hidden=True,
2289
3319
    help='Same as 2a.')
2290
3320
 
2291
3321
# The current format that is made on 'bzr init'.
2292
 
format_name = config.GlobalStack().get('default_format')
2293
 
controldir.format_registry.set_default(format_name)
 
3322
format_name = config.GlobalConfig().get_user_option('default_format')
 
3323
if format_name is None:
 
3324
    controldir.format_registry.set_default('2a')
 
3325
else:
 
3326
    controldir.format_registry.set_default(format_name)
2294
3327
 
2295
3328
# XXX 2010-08-20 JRV: There is still a lot of code relying on
2296
3329
# bzrlib.bzrdir.format_registry existing. When BzrDir.create/BzrDir.open/etc