/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
2
#
1534.4.39 by Robert Collins
Basic BzrDir support.
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
7
#
1534.4.39 by Robert Collins
Basic BzrDir support.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
12
#
1534.4.39 by Robert Collins
Basic BzrDir support.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
"""BzrDir logic. The BzrDir is the basic control directory used by bzr.
18
19
At format 7 this was split out into Branch, Repository and Checkout control
20
directories.
2830.1.1 by Ian Clatworthy
bzrdir.py code clean-ups
21
22
Note: This module has a lot of ``open`` functions/methods that return
23
references to in-memory objects. As a rule, there are no matching ``close``
24
methods. To free any associated resources, simply stop referencing the
25
objects returned.
1534.4.39 by Robert Collins
Basic BzrDir support.
26
"""
27
2830.1.1 by Ian Clatworthy
bzrdir.py code clean-ups
28
# TODO: Move old formats into a plugin to make this file smaller.
1910.7.17 by Andrew Bennetts
Various cosmetic changes.
29
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
30
from cStringIO import StringIO
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
31
import os
3023.1.1 by Alexander Belchenko
Mark .bzr directories as "hidden" on Windows (#71147)
32
import sys
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
33
34
from bzrlib.lazy_import import lazy_import
35
lazy_import(globals(), """
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
36
from stat import S_ISDIR
2711.2.1 by Martin Pool
Deprecate BzrDir.create_repository
37
import textwrap
38
from warnings import warn
1534.4.39 by Robert Collins
Basic BzrDir support.
39
40
import bzrlib
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
41
from bzrlib import (
42
    errors,
2776.1.5 by Robert Collins
Add reasonably comprehensive tests for path last modified and per file graph behaviour.
43
    graph,
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
44
    lockable_files,
45
    lockdir,
2204.4.1 by Aaron Bentley
Add 'formats' help topic
46
    registry,
2018.5.25 by Andrew Bennetts
Make sure RemoteBzrDirFormat is always registered (John Arbash Meinel, Robert Collins, Andrew Bennetts).
47
    remote,
1996.3.12 by John Arbash Meinel
Change how 'revision' is imported to avoid problems later
48
    revision as _mod_revision,
2204.4.12 by Aaron Bentley
Deprecate bzrdir.BzrDirFormat.set_default_format
49
    symbol_versioning,
2323.6.4 by Martin Pool
BzrDir._check_supported now also takes care of recommending upgrades, which
50
    ui,
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
51
    urlutils,
3023.1.2 by Alexander Belchenko
Martin's review.
52
    win32utils,
53
    workingtree,
54
    workingtree_4,
1996.3.6 by John Arbash Meinel
Find a few places that weren't importing their dependencies.
55
    xml4,
56
    xml5,
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
57
    )
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
58
from bzrlib.osutils import (
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
59
    sha_strings,
60
    sha_string,
61
    )
2018.5.159 by Andrew Bennetts
Rename SmartClient to _SmartClient.
62
from bzrlib.smart.client import _SmartClient
2432.3.1 by Andrew Bennetts
Try a version 1 hello probe to determine if we can use RemoteBzrDir on a particular transport, allowing smooth interoperation with older servers.
63
from bzrlib.smart import protocol
1563.2.28 by Robert Collins
Add total_size to the revision_store api.
64
from bzrlib.store.revision.text import TextRevisionStore
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
65
from bzrlib.store.text import TextStore
1563.2.25 by Robert Collins
Merge in upstream.
66
from bzrlib.store.versioned import WeaveStore
1563.2.34 by Robert Collins
Remove the commit and rollback transaction methods as misleading, and implement a WriteTransaction
67
from bzrlib.transactions import WriteTransaction
2164.2.21 by Vincent Ladeuil
Take bundles into account.
68
from bzrlib.transport import (
69
    do_catching_redirections,
70
    get_transport,
71
    )
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
72
from bzrlib.weave import Weave
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
73
""")
74
2164.2.1 by v.ladeuil+lp at free
First rough http branch redirection implementation.
75
from bzrlib.trace import (
76
    mutter,
77
    note,
78
    )
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
79
from bzrlib.transport.local import LocalTransport
2711.2.1 by Martin Pool
Deprecate BzrDir.create_repository
80
from bzrlib.symbol_versioning import (
81
    deprecated_function,
82
    deprecated_method,
83
    zero_ninetyone,
84
    )
1534.4.39 by Robert Collins
Basic BzrDir support.
85
86
87
class BzrDir(object):
88
    """A .bzr control diretory.
89
    
90
    BzrDir instances let you create or open any of the things that can be
91
    found within .bzr - checkouts, branches and repositories.
92
    
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
93
    transport
94
        the transport which this bzr dir is rooted at (i.e. file:///.../.bzr/)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
95
    root_transport
2830.1.1 by Ian Clatworthy
bzrdir.py code clean-ups
96
        a transport connected to the directory this bzr was opened from
97
        (i.e. the parent directory holding the .bzr directory).
1534.4.39 by Robert Collins
Basic BzrDir support.
98
    """
99
1687.1.12 by Robert Collins
Hook in the full break-lock ui.
100
    def break_lock(self):
101
        """Invoke break_lock on the first object in the bzrdir.
102
103
        If there is a tree, the tree is opened and break_lock() called.
104
        Otherwise, branch is tried, and finally repository.
105
        """
1752.2.52 by Andrew Bennetts
Flesh out more Remote* methods needed to open and initialise remote branches/trees/repositories.
106
        # XXX: This seems more like a UI function than something that really
107
        # belongs in this class.
1687.1.12 by Robert Collins
Hook in the full break-lock ui.
108
        try:
109
            thing_to_unlock = self.open_workingtree()
110
        except (errors.NotLocalUrl, errors.NoWorkingTree):
111
            try:
112
                thing_to_unlock = self.open_branch()
113
            except errors.NotBranchError:
114
                try:
115
                    thing_to_unlock = self.open_repository()
116
                except errors.NoRepositoryPresent:
117
                    return
118
        thing_to_unlock.break_lock()
119
1534.5.16 by Robert Collins
Review feedback.
120
    def can_convert_format(self):
121
        """Return true if this bzrdir is one whose format we can convert from."""
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
122
        return True
123
1910.2.12 by Aaron Bentley
Implement knit repo format 2
124
    def check_conversion_target(self, target_format):
125
        target_repo_format = target_format.repository_format
126
        source_repo_format = self._format.repository_format
127
        source_repo_format.check_conversion_target(target_repo_format)
128
1596.2.1 by Robert Collins
Fix BzrDir.open_containing of unsupported branches.
129
    @staticmethod
2323.6.4 by Martin Pool
BzrDir._check_supported now also takes care of recommending upgrades, which
130
    def _check_supported(format, allow_unsupported,
131
        recommend_upgrade=True,
132
        basedir=None):
133
        """Give an error or warning on old formats.
134
135
        :param format: may be any kind of format - workingtree, branch, 
136
        or repository.
137
138
        :param allow_unsupported: If true, allow opening 
139
        formats that are strongly deprecated, and which may 
140
        have limited functionality.
141
142
        :param recommend_upgrade: If true (default), warn
143
        the user through the ui object that they may wish
144
        to upgrade the object.
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
145
        """
2323.5.19 by Martin Pool
No upgrade recommendation on source when cloning
146
        # TODO: perhaps move this into a base Format class; it's not BzrDir
147
        # specific. mbp 20070323
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
148
        if not allow_unsupported and not format.is_supported():
1596.2.1 by Robert Collins
Fix BzrDir.open_containing of unsupported branches.
149
            # see open_downlevel to open legacy branches.
1740.5.6 by Martin Pool
Clean up many exception classes.
150
            raise errors.UnsupportedFormatError(format=format)
2323.6.4 by Martin Pool
BzrDir._check_supported now also takes care of recommending upgrades, which
151
        if recommend_upgrade \
152
            and getattr(format, 'upgrade_recommended', False):
153
            ui.ui_factory.recommend_upgrade(
154
                format.get_format_description(),
155
                basedir)
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
156
2387.1.1 by Robert Collins
Remove the --basis parameter to clone etc. (Robert Collins)
157
    def clone(self, url, revision_id=None, force_new_repo=False):
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
158
        """Clone this bzrdir and its contents to url verbatim.
159
2830.1.1 by Ian Clatworthy
bzrdir.py code clean-ups
160
        If url's last component does not exist, it will be created.
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
161
162
        if revision_id is not None, then the clone operation may tune
163
            itself to download less data.
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
164
        :param force_new_repo: Do not use a shared repository for the target 
165
                               even if one is available.
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
166
        """
2475.3.1 by John Arbash Meinel
Fix bug #75721. Update the BzrDir api to add clone_on_transport()
167
        return self.clone_on_transport(get_transport(url),
168
                                       revision_id=revision_id,
169
                                       force_new_repo=force_new_repo)
170
171
    def clone_on_transport(self, transport, revision_id=None,
172
                           force_new_repo=False):
173
        """Clone this bzrdir and its contents to transport verbatim.
174
175
        If the target directory does not exist, it will be created.
176
177
        if revision_id is not None, then the clone operation may tune
178
            itself to download less data.
179
        :param force_new_repo: Do not use a shared repository for the target 
180
                               even if one is available.
181
        """
2475.3.3 by John Arbash Meinel
Change calls to try/mkdir('.')/except FileExists to ensure_base()
182
        transport.ensure_base()
2475.3.1 by John Arbash Meinel
Fix bug #75721. Update the BzrDir api to add clone_on_transport()
183
        result = self._format.initialize_on_transport(transport)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
184
        try:
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
185
            local_repo = self.find_repository()
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
186
        except errors.NoRepositoryPresent:
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
187
            local_repo = None
188
        if local_repo:
189
            # may need to copy content in
190
            if force_new_repo:
1707.1.1 by Robert Collins
Bugfixes to bzrdir.sprout and clone. Sprout was failing to reset the
191
                result_repo = local_repo.clone(
192
                    result,
2387.1.1 by Robert Collins
Remove the --basis parameter to clone etc. (Robert Collins)
193
                    revision_id=revision_id)
1707.1.1 by Robert Collins
Bugfixes to bzrdir.sprout and clone. Sprout was failing to reset the
194
                result_repo.set_make_working_trees(local_repo.make_working_trees())
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
195
            else:
196
                try:
197
                    result_repo = result.find_repository()
198
                    # fetch content this dir needs.
199
                    result_repo.fetch(local_repo, revision_id=revision_id)
200
                except errors.NoRepositoryPresent:
201
                    # needed to make one anyway.
1707.1.1 by Robert Collins
Bugfixes to bzrdir.sprout and clone. Sprout was failing to reset the
202
                    result_repo = local_repo.clone(
203
                        result,
2387.1.1 by Robert Collins
Remove the --basis parameter to clone etc. (Robert Collins)
204
                        revision_id=revision_id)
1707.1.1 by Robert Collins
Bugfixes to bzrdir.sprout and clone. Sprout was failing to reset the
205
                    result_repo.set_make_working_trees(local_repo.make_working_trees())
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
206
        # 1 if there is a branch present
207
        #   make sure its content is available in the target repository
208
        #   clone it.
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
209
        try:
210
            self.open_branch().clone(result, revision_id=revision_id)
211
        except errors.NotBranchError:
212
            pass
213
        try:
2991.1.2 by Daniel Watkins
Working trees are no longer created by pushing into a local no-trees repo.
214
            result_repo = result.find_repository()
215
        except errors.NoRepositoryPresent:
216
            result_repo = None
217
        if result_repo is None or result_repo.make_working_trees():
218
            try:
219
                self.open_workingtree().clone(result)
220
            except (errors.NoWorkingTree, errors.NotLocalUrl):
221
                pass
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
222
        return result
223
1685.1.61 by Martin Pool
[broken] Change BzrDir._make_tail to use urlutils.split
224
    # TODO: This should be given a Transport, and should chdir up; otherwise
225
    # this will open a new connection.
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
226
    def _make_tail(self, url):
2475.3.3 by John Arbash Meinel
Change calls to try/mkdir('.')/except FileExists to ensure_base()
227
        t = get_transport(url)
228
        t.ensure_base()
2475.3.1 by John Arbash Meinel
Fix bug #75721. Update the BzrDir api to add clone_on_transport()
229
1553.5.71 by Martin Pool
Change branch format 5 to use LockDirs, not transport locks
230
    @classmethod
2476.3.8 by Vincent Ladeuil
Mark transports that need to be instrumented or refactored to check
231
    def create(cls, base, format=None, possible_transports=None):
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
232
        """Create a new BzrDir at the url 'base'.
1534.4.39 by Robert Collins
Basic BzrDir support.
233
        
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
234
        :param format: If supplied, the format of branch to create.  If not
235
            supplied, the default is used.
2476.3.8 by Vincent Ladeuil
Mark transports that need to be instrumented or refactored to check
236
        :param possible_transports: If supplied, a list of transports that 
237
            can be reused to share a remote connection.
1534.4.39 by Robert Collins
Basic BzrDir support.
238
        """
1553.5.71 by Martin Pool
Change branch format 5 to use LockDirs, not transport locks
239
        if cls is not BzrDir:
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
240
            raise AssertionError("BzrDir.create always creates the default"
241
                " format, not one of %r" % cls)
2476.3.8 by Vincent Ladeuil
Mark transports that need to be instrumented or refactored to check
242
        t = get_transport(base, possible_transports)
2475.3.3 by John Arbash Meinel
Change calls to try/mkdir('.')/except FileExists to ensure_base()
243
        t.ensure_base()
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
244
        if format is None:
245
            format = BzrDirFormat.get_default_format()
2830.1.1 by Ian Clatworthy
bzrdir.py code clean-ups
246
        return format.initialize_on_transport(t)
1534.4.39 by Robert Collins
Basic BzrDir support.
247
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
248
    def destroy_repository(self):
249
        """Destroy the repository in this BzrDir"""
250
        raise NotImplementedError(self.destroy_repository)
251
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
252
    def create_branch(self):
253
        """Create a branch in this BzrDir.
254
2830.1.1 by Ian Clatworthy
bzrdir.py code clean-ups
255
        The bzrdir's format will control what branch format is created.
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
256
        For more control see BranchFormatXX.create(a_bzrdir).
257
        """
258
        raise NotImplementedError(self.create_branch)
259
2796.2.6 by Aaron Bentley
Implement destroy_branch
260
    def destroy_branch(self):
2796.2.16 by Aaron Bentley
Documentation updates from review
261
        """Destroy the branch in this BzrDir"""
2796.2.6 by Aaron Bentley
Implement destroy_branch
262
        raise NotImplementedError(self.destroy_branch)
263
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
264
    @staticmethod
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
265
    def create_branch_and_repo(base, force_new_repo=False, format=None):
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
266
        """Create a new BzrDir, Branch and Repository at the url 'base'.
267
2830.1.1 by Ian Clatworthy
bzrdir.py code clean-ups
268
        This will use the current default BzrDirFormat unless one is
269
        specified, and use whatever 
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
270
        repository format that that uses via bzrdir.create_branch and
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
271
        create_repository. If a shared repository is available that is used
272
        preferentially.
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
273
274
        The created Branch object is returned.
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
275
276
        :param base: The URL to create the branch at.
277
        :param force_new_repo: If True a new repository is always created.
2830.1.1 by Ian Clatworthy
bzrdir.py code clean-ups
278
        :param format: If supplied, the format of branch to create.  If not
279
            supplied, the default is used.
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
280
        """
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
281
        bzrdir = BzrDir.create(base, format)
1534.6.11 by Robert Collins
Review feedback.
282
        bzrdir._find_or_create_repository(force_new_repo)
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
283
        return bzrdir.create_branch()
1534.6.11 by Robert Collins
Review feedback.
284
285
    def _find_or_create_repository(self, force_new_repo):
286
        """Create a new repository if needed, returning the repository."""
287
        if force_new_repo:
288
            return self.create_repository()
289
        try:
290
            return self.find_repository()
291
        except errors.NoRepositoryPresent:
292
            return self.create_repository()
2476.3.6 by Vincent Ladeuil
Fix the 'init connects multiple times' in a different way.
293
        
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
294
    @staticmethod
2476.3.6 by Vincent Ladeuil
Fix the 'init connects multiple times' in a different way.
295
    def create_branch_convenience(base, force_new_repo=False,
296
                                  force_new_tree=None, format=None,
2476.3.11 by Vincent Ladeuil
Cosmetic changes.
297
                                  possible_transports=None):
1534.6.10 by Robert Collins
Finish use of repositories support.
298
        """Create a new BzrDir, Branch and Repository at the url 'base'.
299
300
        This is a convenience function - it will use an existing repository
301
        if possible, can be told explicitly whether to create a working tree or
1534.6.12 by Robert Collins
Typo found by John Meinel.
302
        not.
1534.6.10 by Robert Collins
Finish use of repositories support.
303
2830.1.1 by Ian Clatworthy
bzrdir.py code clean-ups
304
        This will use the current default BzrDirFormat unless one is
305
        specified, and use whatever 
1534.6.10 by Robert Collins
Finish use of repositories support.
306
        repository format that that uses via bzrdir.create_branch and
307
        create_repository. If a shared repository is available that is used
308
        preferentially. Whatever repository is used, its tree creation policy
309
        is followed.
310
311
        The created Branch object is returned.
312
        If a working tree cannot be made due to base not being a file:// url,
1563.1.6 by Robert Collins
Add tests for sftp push, and NonLocalTets for BzrDir.create_branch_convenience, before fixing the failure of it to work on non-local urls.
313
        no error is raised unless force_new_tree is True, in which case no 
314
        data is created on disk and NotLocalUrl is raised.
1534.6.10 by Robert Collins
Finish use of repositories support.
315
316
        :param base: The URL to create the branch at.
317
        :param force_new_repo: If True a new repository is always created.
318
        :param force_new_tree: If True or False force creation of a tree or 
319
                               prevent such creation respectively.
2830.1.1 by Ian Clatworthy
bzrdir.py code clean-ups
320
        :param format: Override for the bzrdir format to create.
2476.3.8 by Vincent Ladeuil
Mark transports that need to be instrumented or refactored to check
321
        :param possible_transports: An optional reusable transports list.
1534.6.10 by Robert Collins
Finish use of repositories support.
322
        """
1563.1.6 by Robert Collins
Add tests for sftp push, and NonLocalTets for BzrDir.create_branch_convenience, before fixing the failure of it to work on non-local urls.
323
        if force_new_tree:
324
            # check for non local urls
2485.8.45 by Vincent Ladeuil
Take jam's remarks into account.
325
            t = get_transport(base, possible_transports)
2476.3.6 by Vincent Ladeuil
Fix the 'init connects multiple times' in a different way.
326
            if not isinstance(t, LocalTransport):
327
                raise errors.NotLocalUrl(base)
2476.3.8 by Vincent Ladeuil
Mark transports that need to be instrumented or refactored to check
328
        bzrdir = BzrDir.create(base, format, possible_transports)
1534.6.11 by Robert Collins
Review feedback.
329
        repo = bzrdir._find_or_create_repository(force_new_repo)
1534.6.10 by Robert Collins
Finish use of repositories support.
330
        result = bzrdir.create_branch()
2476.3.4 by Vincent Ladeuil
Add tests.
331
        if force_new_tree or (repo.make_working_trees() and
1534.6.10 by Robert Collins
Finish use of repositories support.
332
                              force_new_tree is None):
1563.1.6 by Robert Collins
Add tests for sftp push, and NonLocalTets for BzrDir.create_branch_convenience, before fixing the failure of it to work on non-local urls.
333
            try:
334
                bzrdir.create_workingtree()
335
            except errors.NotLocalUrl:
336
                pass
1534.6.10 by Robert Collins
Finish use of repositories support.
337
        return result
2476.3.4 by Vincent Ladeuil
Add tests.
338
1551.8.2 by Aaron Bentley
Add create_checkout_convenience
339
    @staticmethod
2711.2.1 by Martin Pool
Deprecate BzrDir.create_repository
340
    @deprecated_function(zero_ninetyone)
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
341
    def create_repository(base, shared=False, format=None):
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
342
        """Create a new BzrDir and Repository at the url 'base'.
343
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
344
        If no format is supplied, this will default to the current default
345
        BzrDirFormat by default, and use whatever repository format that that
346
        uses for bzrdirformat.create_repository.
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
347
1910.7.17 by Andrew Bennetts
Various cosmetic changes.
348
        :param shared: Create a shared repository rather than a standalone
1534.6.1 by Robert Collins
allow API creation of shared repositories
349
                       repository.
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
350
        The Repository object is returned.
351
352
        This must be overridden as an instance method in child classes, where
353
        it should take no parameters and construct whatever repository format
354
        that child class desires.
2711.2.1 by Martin Pool
Deprecate BzrDir.create_repository
355
356
        This method is deprecated, please call create_repository on a bzrdir
357
        instance instead.
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
358
        """
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
359
        bzrdir = BzrDir.create(base, format)
1841.2.1 by Jelmer Vernooij
Fix handling of `shared' parameter in BzrDir.create_repository().
360
        return bzrdir.create_repository(shared)
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
361
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
362
    @staticmethod
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
363
    def create_standalone_workingtree(base, format=None):
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
364
        """Create a new BzrDir, WorkingTree, Branch and Repository at 'base'.
365
366
        'base' must be a local path or a file:// url.
367
2830.1.1 by Ian Clatworthy
bzrdir.py code clean-ups
368
        This will use the current default BzrDirFormat unless one is
369
        specified, and use whatever 
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
370
        repository format that that uses for bzrdirformat.create_workingtree,
371
        create_branch and create_repository.
372
2830.1.1 by Ian Clatworthy
bzrdir.py code clean-ups
373
        :param format: Override for the bzrdir format to create.
1910.7.17 by Andrew Bennetts
Various cosmetic changes.
374
        :return: The WorkingTree object.
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
375
        """
2485.8.45 by Vincent Ladeuil
Take jam's remarks into account.
376
        t = get_transport(base)
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
377
        if not isinstance(t, LocalTransport):
378
            raise errors.NotLocalUrl(base)
2485.8.45 by Vincent Ladeuil
Take jam's remarks into account.
379
        bzrdir = BzrDir.create_branch_and_repo(base,
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
380
                                               force_new_repo=True,
381
                                               format=format).bzrdir
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
382
        return bzrdir.create_workingtree()
383
3123.5.17 by Aaron Bentley
Update docs
384
    def create_workingtree(self, revision_id=None, from_branch=None,
3136.1.3 by Aaron Bentley
Implement hard-link support for branch and checkout
385
        accelerator_tree=None, hardlink=False):
1508.1.21 by Robert Collins
Implement -r limit for checkout command.
386
        """Create a working tree at this BzrDir.
387
        
2955.5.3 by Vincent Ladeuil
Fix second unwanted connection by providing the right branch to create_checkout.
388
        :param revision_id: create it as of this revision id.
389
        :param from_branch: override bzrdir branch (for lightweight checkouts)
3123.5.17 by Aaron Bentley
Update docs
390
        :param accelerator_tree: A tree which can be used for retrieving file
391
            contents more quickly than the revision tree, i.e. a workingtree.
392
            The revision tree will be used for cases where accelerator_tree's
393
            content is different.
1508.1.21 by Robert Collins
Implement -r limit for checkout command.
394
        """
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
395
        raise NotImplementedError(self.create_workingtree)
396
2830.1.2 by Ian Clatworthy
Incorporate feedback from poolie's review
397
    def retire_bzrdir(self, limit=10000):
2255.14.1 by Martin Pool
Add BzrDir.retire_bzrdir and partly fix subsume
398
        """Permanently disable the bzrdir.
399
400
        This is done by renaming it to give the user some ability to recover
401
        if there was a problem.
402
403
        This will have horrible consequences if anyone has anything locked or
404
        in use.
2830.1.2 by Ian Clatworthy
Incorporate feedback from poolie's review
405
        :param limit: number of times to retry
2255.14.1 by Martin Pool
Add BzrDir.retire_bzrdir and partly fix subsume
406
        """
2830.1.2 by Ian Clatworthy
Incorporate feedback from poolie's review
407
        i  = 0
408
        while True:
2255.14.1 by Martin Pool
Add BzrDir.retire_bzrdir and partly fix subsume
409
            try:
410
                to_path = '.bzr.retired.%d' % i
411
                self.root_transport.rename('.bzr', to_path)
412
                note("renamed %s to %s"
413
                    % (self.root_transport.abspath('.bzr'), to_path))
2830.1.1 by Ian Clatworthy
bzrdir.py code clean-ups
414
                return
2255.14.1 by Martin Pool
Add BzrDir.retire_bzrdir and partly fix subsume
415
            except (errors.TransportError, IOError, errors.PathError):
2830.1.2 by Ian Clatworthy
Incorporate feedback from poolie's review
416
                i += 1
417
                if i > limit:
418
                    raise
419
                else:
420
                    pass
2255.14.1 by Martin Pool
Add BzrDir.retire_bzrdir and partly fix subsume
421
1551.8.37 by Aaron Bentley
Cleaner implementation of destroy_working_tree
422
    def destroy_workingtree(self):
1551.8.36 by Aaron Bentley
Introduce BzrDir.destroy_workingtree
423
        """Destroy the working tree at this BzrDir.
424
425
        Formats that do not support this may raise UnsupportedOperation.
426
        """
427
        raise NotImplementedError(self.destroy_workingtree)
428
1551.8.37 by Aaron Bentley
Cleaner implementation of destroy_working_tree
429
    def destroy_workingtree_metadata(self):
430
        """Destroy the control files for the working tree at this BzrDir.
431
432
        The contents of working tree files are not affected.
433
        Formats that do not support this may raise UnsupportedOperation.
434
        """
435
        raise NotImplementedError(self.destroy_workingtree_metadata)
436
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
437
    def find_repository(self):
2830.1.1 by Ian Clatworthy
bzrdir.py code clean-ups
438
        """Find the repository that should be used.
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
439
440
        This does not require a branch as we use it to find the repo for
441
        new branches as well as to hook existing branches up to their
442
        repository.
443
        """
444
        try:
445
            return self.open_repository()
446
        except errors.NoRepositoryPresent:
447
            pass
448
        next_transport = self.root_transport.clone('..')
449
        while True:
1725.2.5 by Robert Collins
Bugfix create_branch_convenience at the root of a file system to not loop
450
            # find the next containing bzrdir
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
451
            try:
1534.6.11 by Robert Collins
Review feedback.
452
                found_bzrdir = BzrDir.open_containing_from_transport(
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
453
                    next_transport)[0]
454
            except errors.NotBranchError:
1725.2.5 by Robert Collins
Bugfix create_branch_convenience at the root of a file system to not loop
455
                # none found
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
456
                raise errors.NoRepositoryPresent(self)
1725.2.5 by Robert Collins
Bugfix create_branch_convenience at the root of a file system to not loop
457
            # does it have a repository ?
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
458
            try:
459
                repository = found_bzrdir.open_repository()
460
            except errors.NoRepositoryPresent:
461
                next_transport = found_bzrdir.root_transport.clone('..')
1725.2.5 by Robert Collins
Bugfix create_branch_convenience at the root of a file system to not loop
462
                if (found_bzrdir.root_transport.base == next_transport.base):
463
                    # top of the file system
464
                    break
465
                else:
466
                    continue
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
467
            if ((found_bzrdir.root_transport.base ==
1534.6.6 by Robert Collins
Move find_repository to bzrdir, its not quite ideal there but its simpler and until someone chooses to vary the search by branch type its completely sufficient.
468
                 self.root_transport.base) or repository.is_shared()):
469
                return repository
470
            else:
471
                raise errors.NoRepositoryPresent(self)
472
        raise errors.NoRepositoryPresent(self)
473
2414.2.1 by Andrew Bennetts
Some miscellaneous new APIs, tests and other changes from the hpss branch.
474
    def get_branch_reference(self):
475
        """Return the referenced URL for the branch in this bzrdir.
476
477
        :raises NotBranchError: If there is no Branch.
478
        :return: The URL the branch in this bzrdir references if it is a
479
            reference branch, or None for regular branches.
480
        """
481
        return None
482
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
483
    def get_branch_transport(self, branch_format):
484
        """Get the transport for use by branch format in this BzrDir.
485
486
        Note that bzr dirs that do not support format strings will raise
487
        IncompatibleFormat if the branch format they are given has
1759.2.1 by Jelmer Vernooij
Fix some types (found using aspell).
488
        a format string, and vice versa.
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
489
490
        If branch_format is None, the transport is returned with no 
2830.1.1 by Ian Clatworthy
bzrdir.py code clean-ups
491
        checking. If it is not None, then the returned transport is
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
492
        guaranteed to point to an existing directory ready for use.
493
        """
494
        raise NotImplementedError(self.get_branch_transport)
495
        
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
496
    def get_repository_transport(self, repository_format):
497
        """Get the transport for use by repository format in this BzrDir.
498
499
        Note that bzr dirs that do not support format strings will raise
500
        IncompatibleFormat if the repository format they are given has
1759.2.1 by Jelmer Vernooij
Fix some types (found using aspell).
501
        a format string, and vice versa.
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
502
503
        If repository_format is None, the transport is returned with no 
2830.1.1 by Ian Clatworthy
bzrdir.py code clean-ups
504
        checking. If it is not None, then the returned transport is
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
505
        guaranteed to point to an existing directory ready for use.
506
        """
507
        raise NotImplementedError(self.get_repository_transport)
508
        
1534.4.53 by Robert Collins
Review feedback from John Meinel.
509
    def get_workingtree_transport(self, tree_format):
1534.4.45 by Robert Collins
Start WorkingTree -> .bzr/checkout transition
510
        """Get the transport for use by workingtree format in this BzrDir.
511
512
        Note that bzr dirs that do not support format strings will raise
2100.3.11 by Aaron Bentley
Add join --reference support
513
        IncompatibleFormat if the workingtree format they are given has a
514
        format string, and vice versa.
1534.4.45 by Robert Collins
Start WorkingTree -> .bzr/checkout transition
515
516
        If workingtree_format is None, the transport is returned with no 
2830.1.1 by Ian Clatworthy
bzrdir.py code clean-ups
517
        checking. If it is not None, then the returned transport is
1534.4.45 by Robert Collins
Start WorkingTree -> .bzr/checkout transition
518
        guaranteed to point to an existing directory ready for use.
519
        """
520
        raise NotImplementedError(self.get_workingtree_transport)
521
        
1534.4.39 by Robert Collins
Basic BzrDir support.
522
    def __init__(self, _transport, _format):
523
        """Initialize a Bzr control dir object.
524
        
525
        Only really common logic should reside here, concrete classes should be
526
        made with varying behaviours.
527
1534.4.53 by Robert Collins
Review feedback from John Meinel.
528
        :param _format: the format that is creating this BzrDir instance.
529
        :param _transport: the transport this dir is based at.
1534.4.39 by Robert Collins
Basic BzrDir support.
530
        """
531
        self._format = _format
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
532
        self.transport = _transport.clone('.bzr')
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
533
        self.root_transport = _transport
1534.4.39 by Robert Collins
Basic BzrDir support.
534
1713.1.9 by Robert Collins
Paired performance tuning of bzr add. (Robert Collins, Martin Pool).
535
    def is_control_filename(self, filename):
536
        """True if filename is the name of a path which is reserved for bzrdir's.
537
        
538
        :param filename: A filename within the root transport of this bzrdir.
539
540
        This is true IF and ONLY IF the filename is part of the namespace reserved
541
        for bzr control dirs. Currently this is the '.bzr' directory in the root
542
        of the root_transport. it is expected that plugins will need to extend
543
        this in the future - for instance to make bzr talk with svn working
544
        trees.
545
        """
546
        # this might be better on the BzrDirFormat class because it refers to 
547
        # all the possible bzrdir disk formats. 
548
        # This method is tested via the workingtree is_control_filename tests- 
2830.1.1 by Ian Clatworthy
bzrdir.py code clean-ups
549
        # it was extracted from WorkingTree.is_control_filename. If the method's
550
        # contract is extended beyond the current trivial implementation, please
1713.1.9 by Robert Collins
Paired performance tuning of bzr add. (Robert Collins, Martin Pool).
551
        # add new tests for it to the appropriate place.
552
        return filename == '.bzr' or filename.startswith('.bzr/')
553
1534.5.16 by Robert Collins
Review feedback.
554
    def needs_format_conversion(self, format=None):
555
        """Return true if this bzrdir needs convert_format run on it.
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
556
        
557
        For instance, if the repository format is out of date but the 
558
        branch and working tree are not, this should return True.
1534.5.13 by Robert Collins
Correct buggy test.
559
560
        :param format: Optional parameter indicating a specific desired
1534.5.16 by Robert Collins
Review feedback.
561
                       format we plan to arrive at.
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
562
        """
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
563
        raise NotImplementedError(self.needs_format_conversion)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
564
1534.4.39 by Robert Collins
Basic BzrDir support.
565
    @staticmethod
566
    def open_unsupported(base):
567
        """Open a branch which is not supported."""
568
        return BzrDir.open(base, _unsupported=True)
569
        
570
    @staticmethod
2806.2.2 by Vincent Ladeuil
Fix #128076 and #131396 by reusing bound branch transport.
571
    def open(base, _unsupported=False, possible_transports=None):
2830.1.1 by Ian Clatworthy
bzrdir.py code clean-ups
572
        """Open an existing bzrdir, rooted at 'base' (url).
1534.4.39 by Robert Collins
Basic BzrDir support.
573
        
2830.1.1 by Ian Clatworthy
bzrdir.py code clean-ups
574
        :param _unsupported: a private parameter to the BzrDir class.
1534.4.39 by Robert Collins
Basic BzrDir support.
575
        """
2806.2.2 by Vincent Ladeuil
Fix #128076 and #131396 by reusing bound branch transport.
576
        t = get_transport(base, possible_transports=possible_transports)
1910.11.1 by Andrew Bennetts
Add BzrDir.open_from_transport, refactored from duplicate code, no explicit tests.
577
        return BzrDir.open_from_transport(t, _unsupported=_unsupported)
578
579
    @staticmethod
2018.5.169 by Andrew Bennetts
Add a _server_formats flag to BzrDir.open_from_transport and BzrDirFormat.find_format, make RemoteBranch.control_files into a property.
580
    def open_from_transport(transport, _unsupported=False,
581
                            _server_formats=True):
1910.11.1 by Andrew Bennetts
Add BzrDir.open_from_transport, refactored from duplicate code, no explicit tests.
582
        """Open a bzrdir within a particular directory.
583
584
        :param transport: Transport containing the bzrdir.
585
        :param _unsupported: private.
586
        """
2164.2.21 by Vincent Ladeuil
Take bundles into account.
587
        base = transport.base
588
589
        def find_format(transport):
2018.5.169 by Andrew Bennetts
Add a _server_formats flag to BzrDir.open_from_transport and BzrDirFormat.find_format, make RemoteBranch.control_files into a property.
590
            return transport, BzrDirFormat.find_format(
591
                transport, _server_formats=_server_formats)
2164.2.21 by Vincent Ladeuil
Take bundles into account.
592
593
        def redirected(transport, e, redirection_notice):
594
            qualified_source = e.get_source_url()
595
            relpath = transport.relpath(qualified_source)
596
            if not e.target.endswith(relpath):
597
                # Not redirected to a branch-format, not a branch
598
                raise errors.NotBranchError(path=e.target)
599
            target = e.target[:-len(relpath)]
600
            note('%s is%s redirected to %s',
601
                 transport.base, e.permanently, target)
602
            # Let's try with a new transport
2164.2.22 by Vincent Ladeuil
Take Aaron's review comments into account.
603
            # FIXME: If 'transport' has a qualifier, this should
2164.2.21 by Vincent Ladeuil
Take bundles into account.
604
            # be applied again to the new transport *iff* the
2830.1.2 by Ian Clatworthy
Incorporate feedback from poolie's review
605
            # schemes used are the same. Uncomment this code
606
            # once the function (and tests) exist.
2164.2.21 by Vincent Ladeuil
Take bundles into account.
607
            # -- vila20070212
2830.1.2 by Ian Clatworthy
Incorporate feedback from poolie's review
608
            #target = urlutils.copy_url_qualifiers(original, target)
2164.2.21 by Vincent Ladeuil
Take bundles into account.
609
            return get_transport(target)
610
2164.2.22 by Vincent Ladeuil
Take Aaron's review comments into account.
611
        try:
2164.2.28 by Vincent Ladeuil
TestingHTTPServer.test_case_server renamed from test_case to avoid confusions.
612
            transport, format = do_catching_redirections(find_format,
613
                                                         transport,
2164.2.22 by Vincent Ladeuil
Take Aaron's review comments into account.
614
                                                         redirected)
615
        except errors.TooManyRedirections:
616
            raise errors.NotBranchError(base)
2164.2.21 by Vincent Ladeuil
Take bundles into account.
617
1596.2.1 by Robert Collins
Fix BzrDir.open_containing of unsupported branches.
618
        BzrDir._check_supported(format, _unsupported)
1910.11.1 by Andrew Bennetts
Add BzrDir.open_from_transport, refactored from duplicate code, no explicit tests.
619
        return format.open(transport, _found=True)
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
620
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
621
    def open_branch(self, unsupported=False):
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
622
        """Open the branch object at this BzrDir if one is present.
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
623
624
        If unsupported is True, then no longer supported branch formats can
625
        still be opened.
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
626
        
627
        TODO: static convenience version of this?
628
        """
629
        raise NotImplementedError(self.open_branch)
1534.4.39 by Robert Collins
Basic BzrDir support.
630
631
    @staticmethod
2485.8.37 by Vincent Ladeuil
Fix merge multiple connections. Test suite *not* passing (sftp
632
    def open_containing(url, possible_transports=None):
1534.4.39 by Robert Collins
Basic BzrDir support.
633
        """Open an existing branch which contains url.
634
        
1534.6.3 by Robert Collins
find_repository sufficiently robust.
635
        :param url: url to search from.
1534.6.11 by Robert Collins
Review feedback.
636
        See open_containing_from_transport for more detail.
1534.6.3 by Robert Collins
find_repository sufficiently robust.
637
        """
2485.8.37 by Vincent Ladeuil
Fix merge multiple connections. Test suite *not* passing (sftp
638
        transport = get_transport(url, possible_transports)
639
        return BzrDir.open_containing_from_transport(transport)
1534.6.3 by Robert Collins
find_repository sufficiently robust.
640
    
641
    @staticmethod
1534.6.11 by Robert Collins
Review feedback.
642
    def open_containing_from_transport(a_transport):
2830.1.1 by Ian Clatworthy
bzrdir.py code clean-ups
643
        """Open an existing branch which contains a_transport.base.
1534.6.3 by Robert Collins
find_repository sufficiently robust.
644
645
        This probes for a branch at a_transport, and searches upwards from there.
1534.4.39 by Robert Collins
Basic BzrDir support.
646
647
        Basically we keep looking up until we find the control directory or
648
        run into the root.  If there isn't one, raises NotBranchError.
649
        If there is one and it is either an unrecognised format or an unsupported 
650
        format, UnknownFormatError or UnsupportedFormatError are raised.
651
        If there is one, it is returned, along with the unused portion of url.
1685.1.27 by John Arbash Meinel
BzrDir works in URLs, but WorkingTree works in unicode paths
652
1685.1.28 by John Arbash Meinel
Changing open_containing to always return a unicode path.
653
        :return: The BzrDir that contains the path, and a Unicode path 
654
                for the rest of the URL.
1534.4.39 by Robert Collins
Basic BzrDir support.
655
        """
656
        # this gets the normalised url back. I.e. '.' -> the full path.
1534.6.3 by Robert Collins
find_repository sufficiently robust.
657
        url = a_transport.base
1534.4.39 by Robert Collins
Basic BzrDir support.
658
        while True:
659
            try:
1910.11.1 by Andrew Bennetts
Add BzrDir.open_from_transport, refactored from duplicate code, no explicit tests.
660
                result = BzrDir.open_from_transport(a_transport)
661
                return result, urlutils.unescape(a_transport.relpath(url))
1534.4.39 by Robert Collins
Basic BzrDir support.
662
            except errors.NotBranchError, e:
1685.1.60 by Martin Pool
[broken] NotBranchError should unescape the url if possible
663
                pass
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
664
            try:
665
                new_t = a_transport.clone('..')
666
            except errors.InvalidURLJoin:
667
                # reached the root, whatever that may be
668
                raise errors.NotBranchError(path=url)
1534.6.3 by Robert Collins
find_repository sufficiently robust.
669
            if new_t.base == a_transport.base:
1534.4.39 by Robert Collins
Basic BzrDir support.
670
                # reached the root, whatever that may be
671
                raise errors.NotBranchError(path=url)
1534.6.3 by Robert Collins
find_repository sufficiently robust.
672
            a_transport = new_t
1534.4.39 by Robert Collins
Basic BzrDir support.
673
3123.5.11 by Aaron Bentley
Accelerate branching from a lightweight checkout
674
    def _get_tree_branch(self):
675
        """Return the branch and tree, if any, for this bzrdir.
676
677
        Return None for tree if not present.
678
        Raise NotBranchError if no branch is present.
679
        :return: (tree, branch)
680
        """
681
        try:
682
            tree = self.open_workingtree()
683
        except (errors.NoWorkingTree, errors.NotLocalUrl):
684
            tree = None
685
            branch = self.open_branch()
686
        else:
687
            branch = tree.branch
688
        return tree, branch
689
690
    @classmethod
691
    def open_tree_or_branch(klass, location):
692
        """Return the branch and working tree at a location.
693
694
        If there is no tree at the location, tree will be None.
695
        If there is no branch at the location, an exception will be
696
        raised
697
        :return: (tree, branch)
698
        """
699
        bzrdir = klass.open(location)
700
        return bzrdir._get_tree_branch()
701
2215.3.2 by Aaron Bentley
Add open_containing_tree_or_branch
702
    @classmethod
703
    def open_containing_tree_or_branch(klass, location):
704
        """Return the branch and working tree contained by a location.
705
706
        Returns (tree, branch, relpath).
707
        If there is no tree at containing the location, tree will be None.
708
        If there is no branch containing the location, an exception will be
709
        raised
710
        relpath is the portion of the path that is contained by the branch.
711
        """
712
        bzrdir, relpath = klass.open_containing(location)
3123.5.11 by Aaron Bentley
Accelerate branching from a lightweight checkout
713
        tree, branch = bzrdir._get_tree_branch()
2215.3.2 by Aaron Bentley
Add open_containing_tree_or_branch
714
        return tree, branch, relpath
715
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
716
    def open_repository(self, _unsupported=False):
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
717
        """Open the repository object at this BzrDir if one is present.
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
718
2830.1.1 by Ian Clatworthy
bzrdir.py code clean-ups
719
        This will not follow the Branch object pointer - it's strictly a direct
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
720
        open facility. Most client code should use open_branch().repository to
721
        get at a repository.
722
2830.1.1 by Ian Clatworthy
bzrdir.py code clean-ups
723
        :param _unsupported: a private parameter, not part of the api.
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
724
        TODO: static convenience version of this?
725
        """
726
        raise NotImplementedError(self.open_repository)
727
2400.2.2 by Robert Collins
Document BzrDir.open_workingtree's new recommend_upgrade parameter.
728
    def open_workingtree(self, _unsupported=False,
2955.5.3 by Vincent Ladeuil
Fix second unwanted connection by providing the right branch to create_checkout.
729
                         recommend_upgrade=True, from_branch=None):
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
730
        """Open the workingtree object at this BzrDir if one is present.
2400.2.2 by Robert Collins
Document BzrDir.open_workingtree's new recommend_upgrade parameter.
731
732
        :param recommend_upgrade: Optional keyword parameter, when True (the
733
            default), emit through the ui module a recommendation that the user
734
            upgrade the working tree when the workingtree being opened is old
735
            (but still fully supported).
2955.5.3 by Vincent Ladeuil
Fix second unwanted connection by providing the right branch to create_checkout.
736
        :param from_branch: override bzrdir branch (for lightweight checkouts)
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
737
        """
738
        raise NotImplementedError(self.open_workingtree)
739
1662.1.19 by Martin Pool
Better error message when initting existing tree
740
    def has_branch(self):
741
        """Tell if this bzrdir contains a branch.
742
        
743
        Note: if you're going to open the branch, you should just go ahead
744
        and try, and not ask permission first.  (This method just opens the 
745
        branch and discards it, and that's somewhat expensive.) 
746
        """
747
        try:
748
            self.open_branch()
749
            return True
750
        except errors.NotBranchError:
751
            return False
752
753
    def has_workingtree(self):
754
        """Tell if this bzrdir contains a working tree.
755
756
        This will still raise an exception if the bzrdir has a workingtree that
757
        is remote & inaccessible.
758
        
759
        Note: if you're going to open the working tree, you should just go ahead
760
        and try, and not ask permission first.  (This method just opens the 
761
        workingtree and discards it, and that's somewhat expensive.) 
762
        """
763
        try:
2323.6.4 by Martin Pool
BzrDir._check_supported now also takes care of recommending upgrades, which
764
            self.open_workingtree(recommend_upgrade=False)
1662.1.19 by Martin Pool
Better error message when initting existing tree
765
            return True
766
        except errors.NoWorkingTree:
767
            return False
768
2018.5.96 by Andrew Bennetts
Merge from bzr.dev, resolving the worst of the semantic conflicts, but there's
769
    def _cloning_metadir(self):
2830.1.1 by Ian Clatworthy
bzrdir.py code clean-ups
770
        """Produce a metadir suitable for cloning with."""
2387.1.1 by Robert Collins
Remove the --basis parameter to clone etc. (Robert Collins)
771
        result_format = self._format.__class__()
772
        try:
1910.2.41 by Aaron Bentley
Clean up clone format creation
773
            try:
2387.1.1 by Robert Collins
Remove the --basis parameter to clone etc. (Robert Collins)
774
                branch = self.open_branch()
775
                source_repository = branch.repository
1910.2.41 by Aaron Bentley
Clean up clone format creation
776
            except errors.NotBranchError:
777
                source_branch = None
2387.1.1 by Robert Collins
Remove the --basis parameter to clone etc. (Robert Collins)
778
                source_repository = self.open_repository()
2305.3.1 by Andrew Bennetts
Tidy up BzrDir.cloning_metadir: bogus try/except, and basis argument isn't actually used.
779
        except errors.NoRepositoryPresent:
2100.3.24 by Aaron Bentley
Get all tests passing again
780
            source_repository = None
2305.3.1 by Andrew Bennetts
Tidy up BzrDir.cloning_metadir: bogus try/except, and basis argument isn't actually used.
781
        else:
2018.5.138 by Robert Collins
Merge bzr.dev.
782
            # XXX TODO: This isinstance is here because we have not implemented
783
            # the fix recommended in bug # 103195 - to delegate this choice the
784
            # repository itself.
2018.5.94 by Andrew Bennetts
Various small changes in aid of making tests pass (including deleting one invalid test).
785
            repo_format = source_repository._format
786
            if not isinstance(repo_format, remote.RemoteRepositoryFormat):
787
                result_format.repository_format = repo_format
2100.3.28 by Aaron Bentley
Make sprout recursive
788
        try:
2323.5.19 by Martin Pool
No upgrade recommendation on source when cloning
789
            # TODO: Couldn't we just probe for the format in these cases,
790
            # rather than opening the whole tree?  It would be a little
791
            # faster. mbp 20070401
792
            tree = self.open_workingtree(recommend_upgrade=False)
2100.3.28 by Aaron Bentley
Make sprout recursive
793
        except (errors.NoWorkingTree, errors.NotLocalUrl):
794
            result_format.workingtree_format = None
795
        else:
796
            result_format.workingtree_format = tree._format.__class__()
2100.3.21 by Aaron Bentley
Work on checking out by-reference trees
797
        return result_format, source_repository
798
2018.5.96 by Andrew Bennetts
Merge from bzr.dev, resolving the worst of the semantic conflicts, but there's
799
    def cloning_metadir(self):
2100.3.32 by Aaron Bentley
fix tree format, basis_tree call, in sprout
800
        """Produce a metadir suitable for cloning or sprouting with.
1910.2.41 by Aaron Bentley
Clean up clone format creation
801
2100.3.32 by Aaron Bentley
fix tree format, basis_tree call, in sprout
802
        These operations may produce workingtrees (yes, even though they're
2830.1.1 by Ian Clatworthy
bzrdir.py code clean-ups
803
        "cloning" something that doesn't have a tree), so a viable workingtree
2100.3.32 by Aaron Bentley
fix tree format, basis_tree call, in sprout
804
        format must be selected.
805
        """
2100.3.21 by Aaron Bentley
Work on checking out by-reference trees
806
        format, repository = self._cloning_metadir()
2100.3.32 by Aaron Bentley
fix tree format, basis_tree call, in sprout
807
        if format._workingtree_format is None:
2100.3.34 by Aaron Bentley
Fix BzrDir.cloning_metadir with no format
808
            if repository is None:
809
                return format
2100.3.21 by Aaron Bentley
Work on checking out by-reference trees
810
            tree_format = repository._format._matchingbzrdir.workingtree_format
2100.3.28 by Aaron Bentley
Make sprout recursive
811
            format.workingtree_format = tree_format.__class__()
2100.3.21 by Aaron Bentley
Work on checking out by-reference trees
812
        return format
813
2100.3.32 by Aaron Bentley
fix tree format, basis_tree call, in sprout
814
    def checkout_metadir(self):
815
        return self.cloning_metadir()
816
2387.1.1 by Robert Collins
Remove the --basis parameter to clone etc. (Robert Collins)
817
    def sprout(self, url, revision_id=None, force_new_repo=False,
3123.5.8 by Aaron Bentley
Work around double-opening lock issue
818
               recurse='down', possible_transports=None,
3136.1.3 by Aaron Bentley
Implement hard-link support for branch and checkout
819
               accelerator_tree=None, hardlink=False):
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
820
        """Create a copy of this bzrdir prepared for use as a new line of
821
        development.
822
2830.1.1 by Ian Clatworthy
bzrdir.py code clean-ups
823
        If url's last component does not exist, it will be created.
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
824
825
        Attributes related to the identity of the source branch like
826
        branch nickname will be cleaned, a working tree is created
827
        whether one existed before or not; and a local branch is always
828
        created.
829
830
        if revision_id is not None, then the clone operation may tune
831
            itself to download less data.
3123.5.17 by Aaron Bentley
Update docs
832
        :param accelerator_tree: A tree which can be used for retrieving file
833
            contents more quickly than the revision tree, i.e. a workingtree.
834
            The revision tree will be used for cases where accelerator_tree's
835
            content is different.
3136.1.3 by Aaron Bentley
Implement hard-link support for branch and checkout
836
        :param hardlink: If true, hard-link files from accelerator_tree,
837
            where possible.
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
838
        """
2485.8.56 by Vincent Ladeuil
Fix bug #112173 and bzr branch multiple connections.
839
        target_transport = get_transport(url, possible_transports)
2475.3.3 by John Arbash Meinel
Change calls to try/mkdir('.')/except FileExists to ensure_base()
840
        target_transport.ensure_base()
2305.3.1 by Andrew Bennetts
Tidy up BzrDir.cloning_metadir: bogus try/except, and basis argument isn't actually used.
841
        cloning_format = self.cloning_metadir()
2475.3.1 by John Arbash Meinel
Fix bug #75721. Update the BzrDir api to add clone_on_transport()
842
        result = cloning_format.initialize_on_transport(target_transport)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
843
        try:
844
            source_branch = self.open_branch()
845
            source_repository = source_branch.repository
846
        except errors.NotBranchError:
847
            source_branch = None
848
            try:
849
                source_repository = self.open_repository()
850
            except errors.NoRepositoryPresent:
2387.1.1 by Robert Collins
Remove the --basis parameter to clone etc. (Robert Collins)
851
                source_repository = None
1534.6.9 by Robert Collins
sprouting into shared repositories
852
        if force_new_repo:
853
            result_repo = None
854
        else:
855
            try:
856
                result_repo = result.find_repository()
857
            except errors.NoRepositoryPresent:
858
                result_repo = None
859
        if source_repository is None and result_repo is not None:
860
            pass
861
        elif source_repository is None and result_repo is None:
862
            # no repo available, make a new one
863
            result.create_repository()
864
        elif source_repository is not None and result_repo is None:
1707.1.1 by Robert Collins
Bugfixes to bzrdir.sprout and clone. Sprout was failing to reset the
865
            # have source, and want to make a new target repo
2485.8.56 by Vincent Ladeuil
Fix bug #112173 and bzr branch multiple connections.
866
            result_repo = source_repository.sprout(result,
867
                                                   revision_id=revision_id)
2440.1.1 by Martin Pool
Add new Repository.sprout,
868
        else:
1534.6.9 by Robert Collins
sprouting into shared repositories
869
            # fetch needed content into target.
1910.4.10 by Andrew Bennetts
Skip various test_sprout* tests when sprouting to non-local bzrdirs that can't have working trees; plus fix a test method naming clash and the bug it revealed in bzrdir.sprout.
870
            if source_repository is not None:
2018.18.20 by Martin Pool
Route branch operations through remote copy_content_into
871
                # would rather do 
2485.8.56 by Vincent Ladeuil
Fix bug #112173 and bzr branch multiple connections.
872
                # source_repository.copy_content_into(result_repo,
873
                #                                     revision_id=revision_id)
2018.18.20 by Martin Pool
Route branch operations through remote copy_content_into
874
                # so we can override the copy method
1910.4.10 by Andrew Bennetts
Skip various test_sprout* tests when sprouting to non-local bzrdirs that can't have working trees; plus fix a test method naming clash and the bug it revealed in bzrdir.sprout.
875
                result_repo.fetch(source_repository, revision_id=revision_id)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
876
        if source_branch is not None:
877
            source_branch.sprout(result, revision_id=revision_id)
878
        else:
879
            result.create_branch()
2485.8.56 by Vincent Ladeuil
Fix bug #112173 and bzr branch multiple connections.
880
        if isinstance(target_transport, LocalTransport) and (
881
            result_repo is None or result_repo.make_working_trees()):
3136.1.3 by Aaron Bentley
Implement hard-link support for branch and checkout
882
            wt = result.create_workingtree(accelerator_tree=accelerator_tree,
883
                hardlink=hardlink)
2255.2.77 by Robert Collins
Tune working inventory generation more: walk the blocks, skipping deleted rows.
884
            wt.lock_write()
885
            try:
886
                if wt.path2id('') is None:
3123.5.10 by Aaron Bentley
Restore old handling of set_root_id
887
                    try:
888
                        wt.set_root_id(self.open_workingtree.get_root_id())
889
                    except errors.NoWorkingTree:
890
                        pass
2255.2.77 by Robert Collins
Tune working inventory generation more: walk the blocks, skipping deleted rows.
891
            finally:
892
                wt.unlock()
2100.3.28 by Aaron Bentley
Make sprout recursive
893
        else:
894
            wt = None
895
        if recurse == 'down':
896
            if wt is not None:
2255.2.226 by Robert Collins
Get merge_nested finally working: change nested tree iterators to take file_ids, and ensure the right branch is connected to in the merge logic. May not be suitable for shared repositories yet.
897
                basis = wt.basis_tree()
898
                basis.lock_read()
899
                subtrees = basis.iter_references()
2100.3.28 by Aaron Bentley
Make sprout recursive
900
                recurse_branch = wt.branch
901
            elif source_branch is not None:
2255.2.226 by Robert Collins
Get merge_nested finally working: change nested tree iterators to take file_ids, and ensure the right branch is connected to in the merge logic. May not be suitable for shared repositories yet.
902
                basis = source_branch.basis_tree()
903
                basis.lock_read()
904
                subtrees = basis.iter_references()
2100.3.28 by Aaron Bentley
Make sprout recursive
905
                recurse_branch = source_branch
906
            else:
2255.2.226 by Robert Collins
Get merge_nested finally working: change nested tree iterators to take file_ids, and ensure the right branch is connected to in the merge logic. May not be suitable for shared repositories yet.
907
                subtrees = []
908
                basis = None
909
            try:
910
                for path, file_id in subtrees:
911
                    target = urlutils.join(url, urlutils.escape(path))
912
                    sublocation = source_branch.reference_parent(file_id, path)
913
                    sublocation.bzrdir.sprout(target,
914
                        basis.get_reference_revision(file_id, path),
915
                        force_new_repo=force_new_repo, recurse=recurse)
916
            finally:
917
                if basis is not None:
918
                    basis.unlock()
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
919
        return result
920
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
921
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
922
class BzrDirPreSplitOut(BzrDir):
923
    """A common class for the all-in-one formats."""
924
1534.5.3 by Robert Collins
Make format 4/5/6 branches share a single LockableFiles instance across wt/branch/repository.
925
    def __init__(self, _transport, _format):
926
        """See BzrDir.__init__."""
927
        super(BzrDirPreSplitOut, self).__init__(_transport, _format)
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
928
        assert self._format._lock_class == lockable_files.TransportLock
1553.5.69 by Martin Pool
BzrDirFormat subclasses can now control what kind of overall lock is used.
929
        assert self._format._lock_file_name == 'branch-lock'
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
930
        self._control_files = lockable_files.LockableFiles(
931
                                            self.get_branch_transport(None),
1553.5.69 by Martin Pool
BzrDirFormat subclasses can now control what kind of overall lock is used.
932
                                            self._format._lock_file_name,
933
                                            self._format._lock_class)
1534.5.3 by Robert Collins
Make format 4/5/6 branches share a single LockableFiles instance across wt/branch/repository.
934
1687.1.12 by Robert Collins
Hook in the full break-lock ui.
935
    def break_lock(self):
936
        """Pre-splitout bzrdirs do not suffer from stale locks."""
937
        raise NotImplementedError(self.break_lock)
938
2387.1.1 by Robert Collins
Remove the --basis parameter to clone etc. (Robert Collins)
939
    def clone(self, url, revision_id=None, force_new_repo=False):
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
940
        """See BzrDir.clone()."""
941
        from bzrlib.workingtree import WorkingTreeFormat2
942
        self._make_tail(url)
1651.1.6 by Martin Pool
Clean up clone-bzrdir code
943
        result = self._format._initialize_for_clone(url)
2387.1.1 by Robert Collins
Remove the --basis parameter to clone etc. (Robert Collins)
944
        self.open_repository().clone(result, revision_id=revision_id)
1692.7.9 by Martin Pool
Don't create broken standalone branches over sftp (Malone #43064)
945
        from_branch = self.open_branch()
946
        from_branch.clone(result, revision_id=revision_id)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
947
        try:
2387.1.1 by Robert Collins
Remove the --basis parameter to clone etc. (Robert Collins)
948
            self.open_workingtree().clone(result)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
949
        except errors.NotLocalUrl:
950
            # make a new one, this format always has to have one.
1563.2.38 by Robert Collins
make push preserve tree formats.
951
            try:
952
                WorkingTreeFormat2().initialize(result)
953
            except errors.NotLocalUrl:
1692.7.9 by Martin Pool
Don't create broken standalone branches over sftp (Malone #43064)
954
                # but we cannot do it for remote trees.
955
                to_branch = result.open_branch()
956
                WorkingTreeFormat2().stub_initialize_remote(to_branch.control_files)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
957
        return result
958
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
959
    def create_branch(self):
960
        """See BzrDir.create_branch."""
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
961
        return self.open_branch()
962
2796.2.6 by Aaron Bentley
Implement destroy_branch
963
    def destroy_branch(self):
2796.2.16 by Aaron Bentley
Documentation updates from review
964
        """See BzrDir.destroy_branch."""
2796.2.6 by Aaron Bentley
Implement destroy_branch
965
        raise errors.UnsupportedOperation(self.destroy_branch, self)
966
1534.6.1 by Robert Collins
allow API creation of shared repositories
967
    def create_repository(self, shared=False):
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
968
        """See BzrDir.create_repository."""
1534.6.1 by Robert Collins
allow API creation of shared repositories
969
        if shared:
970
            raise errors.IncompatibleFormat('shared repository', self._format)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
971
        return self.open_repository()
972
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
973
    def destroy_repository(self):
974
        """See BzrDir.destroy_repository."""
975
        raise errors.UnsupportedOperation(self.destroy_repository, self)
976
3123.5.2 by Aaron Bentley
Allow checkout --files_from
977
    def create_workingtree(self, revision_id=None, from_branch=None,
3136.1.3 by Aaron Bentley
Implement hard-link support for branch and checkout
978
                           accelerator_tree=None, hardlink=False):
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
979
        """See BzrDir.create_workingtree."""
1508.1.21 by Robert Collins
Implement -r limit for checkout command.
980
        # this looks buggy but is not -really-
2323.6.4 by Martin Pool
BzrDir._check_supported now also takes care of recommending upgrades, which
981
        # because this format creates the workingtree when the bzrdir is
982
        # created
1508.1.21 by Robert Collins
Implement -r limit for checkout command.
983
        # clone and sprout will have set the revision_id
984
        # and that will have set it for us, its only
985
        # specific uses of create_workingtree in isolation
986
        # that can do wonky stuff here, and that only
987
        # happens for creating checkouts, which cannot be 
988
        # done on this format anyway. So - acceptable wart.
2323.6.4 by Martin Pool
BzrDir._check_supported now also takes care of recommending upgrades, which
989
        result = self.open_workingtree(recommend_upgrade=False)
1508.1.24 by Robert Collins
Add update command for use with checkouts.
990
        if revision_id is not None:
1996.3.12 by John Arbash Meinel
Change how 'revision' is imported to avoid problems later
991
            if revision_id == _mod_revision.NULL_REVISION:
1551.8.20 by Aaron Bentley
Fix BzrDir.create_workingtree for NULL_REVISION
992
                result.set_parent_ids([])
993
            else:
994
                result.set_parent_ids([revision_id])
1508.1.21 by Robert Collins
Implement -r limit for checkout command.
995
        return result
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
996
1551.8.37 by Aaron Bentley
Cleaner implementation of destroy_working_tree
997
    def destroy_workingtree(self):
1551.8.36 by Aaron Bentley
Introduce BzrDir.destroy_workingtree
998
        """See BzrDir.destroy_workingtree."""
999
        raise errors.UnsupportedOperation(self.destroy_workingtree, self)
1000
1551.8.37 by Aaron Bentley
Cleaner implementation of destroy_working_tree
1001
    def destroy_workingtree_metadata(self):
1002
        """See BzrDir.destroy_workingtree_metadata."""
1003
        raise errors.UnsupportedOperation(self.destroy_workingtree_metadata, 
1004
                                          self)
1005
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1006
    def get_branch_transport(self, branch_format):
1007
        """See BzrDir.get_branch_transport()."""
1008
        if branch_format is None:
1009
            return self.transport
1010
        try:
1011
            branch_format.get_format_string()
1012
        except NotImplementedError:
1013
            return self.transport
1014
        raise errors.IncompatibleFormat(branch_format, self._format)
1015
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
1016
    def get_repository_transport(self, repository_format):
1017
        """See BzrDir.get_repository_transport()."""
1018
        if repository_format is None:
1019
            return self.transport
1020
        try:
1021
            repository_format.get_format_string()
1022
        except NotImplementedError:
1023
            return self.transport
1024
        raise errors.IncompatibleFormat(repository_format, self._format)
1025
1534.4.45 by Robert Collins
Start WorkingTree -> .bzr/checkout transition
1026
    def get_workingtree_transport(self, workingtree_format):
1027
        """See BzrDir.get_workingtree_transport()."""
1028
        if workingtree_format is None:
1029
            return self.transport
1030
        try:
1031
            workingtree_format.get_format_string()
1032
        except NotImplementedError:
1033
            return self.transport
1034
        raise errors.IncompatibleFormat(workingtree_format, self._format)
1035
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
1036
    def needs_format_conversion(self, format=None):
1037
        """See BzrDir.needs_format_conversion()."""
1038
        # if the format is not the same as the system default,
1039
        # an upgrade is needed.
1040
        if format is None:
1041
            format = BzrDirFormat.get_default_format()
1042
        return not isinstance(self._format, format.__class__)
1043
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1044
    def open_branch(self, unsupported=False):
1045
        """See BzrDir.open_branch."""
1046
        from bzrlib.branch import BzrBranchFormat4
1047
        format = BzrBranchFormat4()
1048
        self._check_supported(format, unsupported)
1049
        return format.open(self, _found=True)
1050
2485.8.56 by Vincent Ladeuil
Fix bug #112173 and bzr branch multiple connections.
1051
    def sprout(self, url, revision_id=None, force_new_repo=False,
3136.1.3 by Aaron Bentley
Implement hard-link support for branch and checkout
1052
               possible_transports=None, accelerator_tree=None,
1053
               hardlink=False):
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1054
        """See BzrDir.sprout()."""
1055
        from bzrlib.workingtree import WorkingTreeFormat2
1056
        self._make_tail(url)
1651.1.6 by Martin Pool
Clean up clone-bzrdir code
1057
        result = self._format._initialize_for_clone(url)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1058
        try:
2387.1.1 by Robert Collins
Remove the --basis parameter to clone etc. (Robert Collins)
1059
            self.open_repository().clone(result, revision_id=revision_id)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1060
        except errors.NoRepositoryPresent:
1061
            pass
1062
        try:
1063
            self.open_branch().sprout(result, revision_id=revision_id)
1064
        except errors.NotBranchError:
1065
            pass
1587.1.5 by Robert Collins
Put bzr branch behaviour back to the 0.7 ignore-working-tree state.
1066
        # we always want a working tree
3123.5.8 by Aaron Bentley
Work around double-opening lock issue
1067
        WorkingTreeFormat2().initialize(result,
3136.1.3 by Aaron Bentley
Implement hard-link support for branch and checkout
1068
                                        accelerator_tree=accelerator_tree,
1069
                                        hardlink=hardlink)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1070
        return result
1071
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1072
1073
class BzrDir4(BzrDirPreSplitOut):
1508.1.25 by Robert Collins
Update per review comments.
1074
    """A .bzr version 4 control object.
1075
    
1076
    This is a deprecated format and may be removed after sept 2006.
1077
    """
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1078
1534.6.1 by Robert Collins
allow API creation of shared repositories
1079
    def create_repository(self, shared=False):
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1080
        """See BzrDir.create_repository."""
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
1081
        return self._format.repository_format.initialize(self, shared)
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1082
1534.5.16 by Robert Collins
Review feedback.
1083
    def needs_format_conversion(self, format=None):
1084
        """Format 4 dirs are always in need of conversion."""
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1085
        return True
1086
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1087
    def open_repository(self):
1088
        """See BzrDir.open_repository."""
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
1089
        from bzrlib.repofmt.weaverepo import RepositoryFormat4
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1090
        return RepositoryFormat4().open(self, _found=True)
1091
1092
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1093
class BzrDir5(BzrDirPreSplitOut):
1508.1.25 by Robert Collins
Update per review comments.
1094
    """A .bzr version 5 control object.
1095
1096
    This is a deprecated format and may be removed after sept 2006.
1097
    """
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1098
1099
    def open_repository(self):
1100
        """See BzrDir.open_repository."""
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
1101
        from bzrlib.repofmt.weaverepo import RepositoryFormat5
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1102
        return RepositoryFormat5().open(self, _found=True)
1103
2323.6.4 by Martin Pool
BzrDir._check_supported now also takes care of recommending upgrades, which
1104
    def open_workingtree(self, _unsupported=False,
1105
            recommend_upgrade=True):
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
1106
        """See BzrDir.create_workingtree."""
1107
        from bzrlib.workingtree import WorkingTreeFormat2
2323.6.4 by Martin Pool
BzrDir._check_supported now also takes care of recommending upgrades, which
1108
        wt_format = WorkingTreeFormat2()
1109
        # we don't warn here about upgrades; that ought to be handled for the
1110
        # bzrdir as a whole
1111
        return wt_format.open(self, _found=True)
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
1112
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1113
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1114
class BzrDir6(BzrDirPreSplitOut):
1508.1.25 by Robert Collins
Update per review comments.
1115
    """A .bzr version 6 control object.
1116
1117
    This is a deprecated format and may be removed after sept 2006.
1118
    """
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1119
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1120
    def open_repository(self):
1121
        """See BzrDir.open_repository."""
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
1122
        from bzrlib.repofmt.weaverepo import RepositoryFormat6
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1123
        return RepositoryFormat6().open(self, _found=True)
1124
2323.6.4 by Martin Pool
BzrDir._check_supported now also takes care of recommending upgrades, which
1125
    def open_workingtree(self, _unsupported=False,
1126
        recommend_upgrade=True):
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1127
        """See BzrDir.create_workingtree."""
2323.6.4 by Martin Pool
BzrDir._check_supported now also takes care of recommending upgrades, which
1128
        # we don't warn here about upgrades; that ought to be handled for the
1129
        # bzrdir as a whole
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1130
        from bzrlib.workingtree import WorkingTreeFormat2
1131
        return WorkingTreeFormat2().open(self, _found=True)
1132
1133
1134
class BzrDirMeta1(BzrDir):
1135
    """A .bzr meta version 1 control object.
1136
    
1137
    This is the first control object where the 
1553.5.67 by Martin Pool
doc
1138
    individual aspects are really split out: there are separate repository,
1139
    workingtree and branch subdirectories and any subset of the three can be
1140
    present within a BzrDir.
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1141
    """
1142
1534.5.16 by Robert Collins
Review feedback.
1143
    def can_convert_format(self):
1144
        """See BzrDir.can_convert_format()."""
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
1145
        return True
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1146
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
1147
    def create_branch(self):
1148
        """See BzrDir.create_branch."""
2230.3.55 by Aaron Bentley
Updates from review
1149
        return self._format.get_branch_format().initialize(self)
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
1150
2796.2.6 by Aaron Bentley
Implement destroy_branch
1151
    def destroy_branch(self):
1152
        """See BzrDir.create_branch."""
1153
        self.transport.delete_tree('branch')
1154
1534.6.1 by Robert Collins
allow API creation of shared repositories
1155
    def create_repository(self, shared=False):
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1156
        """See BzrDir.create_repository."""
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
1157
        return self._format.repository_format.initialize(self, shared)
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1158
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
1159
    def destroy_repository(self):
1160
        """See BzrDir.destroy_repository."""
1161
        self.transport.delete_tree('repository')
1162
3123.5.2 by Aaron Bentley
Allow checkout --files_from
1163
    def create_workingtree(self, revision_id=None, from_branch=None,
3136.1.3 by Aaron Bentley
Implement hard-link support for branch and checkout
1164
                           accelerator_tree=None, hardlink=False):
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
1165
        """See BzrDir.create_workingtree."""
2955.5.3 by Vincent Ladeuil
Fix second unwanted connection by providing the right branch to create_checkout.
1166
        return self._format.workingtree_format.initialize(
3123.5.2 by Aaron Bentley
Allow checkout --files_from
1167
            self, revision_id, from_branch=from_branch,
3136.1.3 by Aaron Bentley
Implement hard-link support for branch and checkout
1168
            accelerator_tree=accelerator_tree, hardlink=hardlink)
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
1169
1551.8.37 by Aaron Bentley
Cleaner implementation of destroy_working_tree
1170
    def destroy_workingtree(self):
1551.8.36 by Aaron Bentley
Introduce BzrDir.destroy_workingtree
1171
        """See BzrDir.destroy_workingtree."""
2323.6.4 by Martin Pool
BzrDir._check_supported now also takes care of recommending upgrades, which
1172
        wt = self.open_workingtree(recommend_upgrade=False)
1551.8.37 by Aaron Bentley
Cleaner implementation of destroy_working_tree
1173
        repository = wt.branch.repository
2094.3.5 by John Arbash Meinel
Fix imports to ensure modules are loaded before they are used
1174
        empty = repository.revision_tree(_mod_revision.NULL_REVISION)
2748.3.2 by Aaron Bentley
Fix revert, remove-tree, and various tests to use None for 'no files specified'
1175
        wt.revert(old_tree=empty)
1551.8.37 by Aaron Bentley
Cleaner implementation of destroy_working_tree
1176
        self.destroy_workingtree_metadata()
1177
1178
    def destroy_workingtree_metadata(self):
1179
        self.transport.delete_tree('checkout')
1551.8.36 by Aaron Bentley
Introduce BzrDir.destroy_workingtree
1180
2414.2.1 by Andrew Bennetts
Some miscellaneous new APIs, tests and other changes from the hpss branch.
1181
    def find_branch_format(self):
1182
        """Find the branch 'format' for this bzrdir.
1183
1184
        This might be a synthetic object for e.g. RemoteBranch and SVN.
1185
        """
1186
        from bzrlib.branch import BranchFormat
1187
        return BranchFormat.find_format(self)
1188
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
1189
    def _get_mkdir_mode(self):
1190
        """Figure out the mode to use when creating a bzrdir subdir."""
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
1191
        temp_control = lockable_files.LockableFiles(self.transport, '',
1192
                                     lockable_files.TransportLock)
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
1193
        return temp_control._dir_mode
1194
2414.2.1 by Andrew Bennetts
Some miscellaneous new APIs, tests and other changes from the hpss branch.
1195
    def get_branch_reference(self):
1196
        """See BzrDir.get_branch_reference()."""
1197
        from bzrlib.branch import BranchFormat
1198
        format = BranchFormat.find_format(self)
1199
        return format.get_reference(self)
1200
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1201
    def get_branch_transport(self, branch_format):
1202
        """See BzrDir.get_branch_transport()."""
1203
        if branch_format is None:
1204
            return self.transport.clone('branch')
1205
        try:
1206
            branch_format.get_format_string()
1207
        except NotImplementedError:
1208
            raise errors.IncompatibleFormat(branch_format, self._format)
1209
        try:
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
1210
            self.transport.mkdir('branch', mode=self._get_mkdir_mode())
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1211
        except errors.FileExists:
1212
            pass
1213
        return self.transport.clone('branch')
1214
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
1215
    def get_repository_transport(self, repository_format):
1216
        """See BzrDir.get_repository_transport()."""
1217
        if repository_format is None:
1218
            return self.transport.clone('repository')
1219
        try:
1220
            repository_format.get_format_string()
1221
        except NotImplementedError:
1222
            raise errors.IncompatibleFormat(repository_format, self._format)
1223
        try:
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
1224
            self.transport.mkdir('repository', mode=self._get_mkdir_mode())
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
1225
        except errors.FileExists:
1226
            pass
1227
        return self.transport.clone('repository')
1228
1534.4.45 by Robert Collins
Start WorkingTree -> .bzr/checkout transition
1229
    def get_workingtree_transport(self, workingtree_format):
1230
        """See BzrDir.get_workingtree_transport()."""
1231
        if workingtree_format is None:
1232
            return self.transport.clone('checkout')
1233
        try:
1234
            workingtree_format.get_format_string()
1235
        except NotImplementedError:
1236
            raise errors.IncompatibleFormat(workingtree_format, self._format)
1237
        try:
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
1238
            self.transport.mkdir('checkout', mode=self._get_mkdir_mode())
1534.4.45 by Robert Collins
Start WorkingTree -> .bzr/checkout transition
1239
        except errors.FileExists:
1240
            pass
1241
        return self.transport.clone('checkout')
1242
1534.5.16 by Robert Collins
Review feedback.
1243
    def needs_format_conversion(self, format=None):
1244
        """See BzrDir.needs_format_conversion()."""
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
1245
        if format is None:
1246
            format = BzrDirFormat.get_default_format()
1247
        if not isinstance(self._format, format.__class__):
1248
            # it is not a meta dir format, conversion is needed.
1249
            return True
1250
        # we might want to push this down to the repository?
1251
        try:
1252
            if not isinstance(self.open_repository()._format,
1253
                              format.repository_format.__class__):
1254
                # the repository needs an upgrade.
1255
                return True
1256
        except errors.NoRepositoryPresent:
1257
            pass
2230.3.29 by Aaron Bentley
Implement conversion to branch 6
1258
        try:
1259
            if not isinstance(self.open_branch()._format,
2230.3.55 by Aaron Bentley
Updates from review
1260
                              format.get_branch_format().__class__):
2255.12.1 by Robert Collins
Implement upgrade for working trees.
1261
                # the branch needs an upgrade.
1262
                return True
1263
        except errors.NotBranchError:
1264
            pass
1265
        try:
2323.6.4 by Martin Pool
BzrDir._check_supported now also takes care of recommending upgrades, which
1266
            my_wt = self.open_workingtree(recommend_upgrade=False)
1267
            if not isinstance(my_wt._format,
2255.12.1 by Robert Collins
Implement upgrade for working trees.
1268
                              format.workingtree_format.__class__):
1269
                # the workingtree needs an upgrade.
1270
                return True
2255.2.196 by Robert Collins
Fix test_upgrade defects related to non local or absent working trees.
1271
        except (errors.NoWorkingTree, errors.NotLocalUrl):
2255.12.1 by Robert Collins
Implement upgrade for working trees.
1272
            pass
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1273
        return False
1274
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1275
    def open_branch(self, unsupported=False):
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
1276
        """See BzrDir.open_branch."""
2414.2.1 by Andrew Bennetts
Some miscellaneous new APIs, tests and other changes from the hpss branch.
1277
        format = self.find_branch_format()
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1278
        self._check_supported(format, unsupported)
1279
        return format.open(self, _found=True)
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
1280
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
1281
    def open_repository(self, unsupported=False):
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1282
        """See BzrDir.open_repository."""
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
1283
        from bzrlib.repository import RepositoryFormat
1284
        format = RepositoryFormat.find_format(self)
1285
        self._check_supported(format, unsupported)
1286
        return format.open(self, _found=True)
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1287
2323.6.4 by Martin Pool
BzrDir._check_supported now also takes care of recommending upgrades, which
1288
    def open_workingtree(self, unsupported=False,
1289
            recommend_upgrade=True):
1508.1.21 by Robert Collins
Implement -r limit for checkout command.
1290
        """See BzrDir.open_workingtree."""
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
1291
        from bzrlib.workingtree import WorkingTreeFormat
1292
        format = WorkingTreeFormat.find_format(self)
2323.6.4 by Martin Pool
BzrDir._check_supported now also takes care of recommending upgrades, which
1293
        self._check_supported(format, unsupported,
1294
            recommend_upgrade,
2323.6.5 by Martin Pool
Recommended-upgrade message should give base dir not the control dir url
1295
            basedir=self.root_transport.base)
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
1296
        return format.open(self, _found=True)
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
1297
1534.4.39 by Robert Collins
Basic BzrDir support.
1298
1299
class BzrDirFormat(object):
1300
    """An encapsulation of the initialization and open routines for a format.
1301
1302
    Formats provide three things:
1303
     * An initialization routine,
1304
     * a format string,
1305
     * an open routine.
1306
2830.1.1 by Ian Clatworthy
bzrdir.py code clean-ups
1307
    Formats are placed in a dict by their format string for reference 
1534.4.39 by Robert Collins
Basic BzrDir support.
1308
    during bzrdir opening. These should be subclasses of BzrDirFormat
1309
    for consistency.
1310
1311
    Once a format is deprecated, just deprecate the initialize and open
1312
    methods on the format class. Do not deprecate the object, as the 
1313
    object will be created every system load.
1314
    """
1315
1316
    _default_format = None
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1317
    """The default format used for new .bzr dirs."""
1534.4.39 by Robert Collins
Basic BzrDir support.
1318
1319
    _formats = {}
1320
    """The known formats."""
1321
1733.1.7 by Jelmer Vernooij
Change set of control dir formats to list.
1322
    _control_formats = []
1733.1.6 by Jelmer Vernooij
Fix a couple of minor issues after review by Martin.
1323
    """The registered control formats - .bzr, ....
1324
    
1733.1.7 by Jelmer Vernooij
Change set of control dir formats to list.
1325
    This is a list of BzrDirFormat objects.
1733.1.6 by Jelmer Vernooij
Fix a couple of minor issues after review by Martin.
1326
    """
1733.1.1 by Robert Collins
Support non '.bzr' control directories in bzrdir.
1327
2018.5.169 by Andrew Bennetts
Add a _server_formats flag to BzrDir.open_from_transport and BzrDirFormat.find_format, make RemoteBranch.control_files into a property.
1328
    _control_server_formats = []
1329
    """The registered control server formats, e.g. RemoteBzrDirs.
1330
1331
    This is a list of BzrDirFormat objects.
1332
    """
1333
1553.5.69 by Martin Pool
BzrDirFormat subclasses can now control what kind of overall lock is used.
1334
    _lock_file_name = 'branch-lock'
1335
1336
    # _lock_class must be set in subclasses to the lock type, typ.
1337
    # TransportLock or LockDir
1338
1534.4.39 by Robert Collins
Basic BzrDir support.
1339
    @classmethod
2018.5.169 by Andrew Bennetts
Add a _server_formats flag to BzrDir.open_from_transport and BzrDirFormat.find_format, make RemoteBranch.control_files into a property.
1340
    def find_format(klass, transport, _server_formats=True):
1733.1.1 by Robert Collins
Support non '.bzr' control directories in bzrdir.
1341
        """Return the format present at transport."""
2018.5.169 by Andrew Bennetts
Add a _server_formats flag to BzrDir.open_from_transport and BzrDirFormat.find_format, make RemoteBranch.control_files into a property.
1342
        if _server_formats:
1343
            formats = klass._control_server_formats + klass._control_formats
1344
        else:
1345
            formats = klass._control_formats
1346
        for format in formats:
1733.1.1 by Robert Collins
Support non '.bzr' control directories in bzrdir.
1347
            try:
1348
                return format.probe_transport(transport)
1349
            except errors.NotBranchError:
1350
                # this format does not find a control dir here.
1351
                pass
1352
        raise errors.NotBranchError(path=transport.base)
1353
1354
    @classmethod
1355
    def probe_transport(klass, transport):
1752.2.52 by Andrew Bennetts
Flesh out more Remote* methods needed to open and initialise remote branches/trees/repositories.
1356
        """Return the .bzrdir style format present in a directory."""
1534.4.39 by Robert Collins
Basic BzrDir support.
1357
        try:
2164.2.18 by Vincent Ladeuil
Take Aaron comments into account.
1358
            format_string = transport.get(".bzr/branch-format").read()
1733.2.3 by Michael Ellerman
Don't coallesce try blocks, it can lead to confusing exceptions.
1359
        except errors.NoSuchFile:
1360
            raise errors.NotBranchError(path=transport.base)
1361
1362
        try:
1534.4.39 by Robert Collins
Basic BzrDir support.
1363
            return klass._formats[format_string]
1364
        except KeyError:
1740.5.6 by Martin Pool
Clean up many exception classes.
1365
            raise errors.UnknownFormatError(format=format_string)
1534.4.39 by Robert Collins
Basic BzrDir support.
1366
1367
    @classmethod
1368
    def get_default_format(klass):
1369
        """Return the current default format."""
1370
        return klass._default_format
1371
1372
    def get_format_string(self):
1373
        """Return the ASCII format string that identifies this format."""
1374
        raise NotImplementedError(self.get_format_string)
1375
1624.3.19 by Olaf Conradi
New call get_format_description to give a user-friendly description of a
1376
    def get_format_description(self):
1377
        """Return the short description for this format."""
1378
        raise NotImplementedError(self.get_format_description)
1379
1534.5.16 by Robert Collins
Review feedback.
1380
    def get_converter(self, format=None):
1381
        """Return the converter to use to convert bzrdirs needing converts.
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1382
1383
        This returns a bzrlib.bzrdir.Converter object.
1384
1385
        This should return the best upgrader to step this format towards the
1759.2.1 by Jelmer Vernooij
Fix some types (found using aspell).
1386
        current default format. In the case of plugins we can/should provide
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1387
        some means for them to extend the range of returnable converters.
1534.5.13 by Robert Collins
Correct buggy test.
1388
1759.2.1 by Jelmer Vernooij
Fix some types (found using aspell).
1389
        :param format: Optional format to override the default format of the 
1534.5.13 by Robert Collins
Correct buggy test.
1390
                       library.
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1391
        """
1534.5.16 by Robert Collins
Review feedback.
1392
        raise NotImplementedError(self.get_converter)
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1393
2476.3.8 by Vincent Ladeuil
Mark transports that need to be instrumented or refactored to check
1394
    def initialize(self, url, possible_transports=None):
1608.2.8 by Martin Pool
Separate out BzrDir.initialize_on_transport so it
1395
        """Create a bzr control dir at this url and return an opened copy.
1396
        
1397
        Subclasses should typically override initialize_on_transport
1398
        instead of this method.
1399
        """
2476.3.8 by Vincent Ladeuil
Mark transports that need to be instrumented or refactored to check
1400
        return self.initialize_on_transport(get_transport(url,
1401
                                                          possible_transports))
1608.2.8 by Martin Pool
Separate out BzrDir.initialize_on_transport so it
1402
1651.1.6 by Martin Pool
Clean up clone-bzrdir code
1403
    def initialize_on_transport(self, transport):
1608.2.8 by Martin Pool
Separate out BzrDir.initialize_on_transport so it
1404
        """Initialize a new bzrdir in the base directory of a Transport."""
1759.2.1 by Jelmer Vernooij
Fix some types (found using aspell).
1405
        # Since we don't have a .bzr directory, inherit the
1534.4.39 by Robert Collins
Basic BzrDir support.
1406
        # mode from the root directory
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
1407
        temp_control = lockable_files.LockableFiles(transport,
1408
                            '', lockable_files.TransportLock)
1534.4.39 by Robert Collins
Basic BzrDir support.
1409
        temp_control._transport.mkdir('.bzr',
1759.2.2 by Jelmer Vernooij
Revert some of my spelling fixes and fix some typos after review by Aaron.
1410
                                      # FIXME: RBC 20060121 don't peek under
1534.4.39 by Robert Collins
Basic BzrDir support.
1411
                                      # the covers
1412
                                      mode=temp_control._dir_mode)
3023.1.1 by Alexander Belchenko
Mark .bzr directories as "hidden" on Windows (#71147)
1413
        if sys.platform == 'win32' and isinstance(transport, LocalTransport):
3023.1.2 by Alexander Belchenko
Martin's review.
1414
            win32utils.set_file_attr_hidden(transport._abspath('.bzr'))
1534.4.39 by Robert Collins
Basic BzrDir support.
1415
        file_mode = temp_control._file_mode
1416
        del temp_control
1608.2.8 by Martin Pool
Separate out BzrDir.initialize_on_transport so it
1417
        mutter('created control directory in ' + transport.base)
1418
        control = transport.clone('.bzr')
1534.4.39 by Robert Collins
Basic BzrDir support.
1419
        utf8_files = [('README', 
1420
                       "This is a Bazaar-NG control directory.\n"
1421
                       "Do not change any files in this directory.\n"),
1422
                      ('branch-format', self.get_format_string()),
1423
                      ]
1424
        # NB: no need to escape relative paths that are url safe.
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
1425
        control_files = lockable_files.LockableFiles(control,
1426
                            self._lock_file_name, self._lock_class)
1553.5.60 by Martin Pool
New LockableFiles.create_lock() method
1427
        control_files.create_lock()
1534.4.39 by Robert Collins
Basic BzrDir support.
1428
        control_files.lock_write()
1429
        try:
1430
            for file, content in utf8_files:
1431
                control_files.put_utf8(file, content)
1432
        finally:
1433
            control_files.unlock()
1608.2.8 by Martin Pool
Separate out BzrDir.initialize_on_transport so it
1434
        return self.open(transport, _found=True)
1534.4.39 by Robert Collins
Basic BzrDir support.
1435
1436
    def is_supported(self):
1437
        """Is this format supported?
1438
1439
        Supported formats must be initializable and openable.
1440
        Unsupported formats may not support initialization or committing or 
1441
        some other features depending on the reason for not being supported.
1442
        """
1443
        return True
1444
1910.2.14 by Aaron Bentley
Fail when trying to use interrepository on Knit2 and Knit1
1445
    def same_model(self, target_format):
1446
        return (self.repository_format.rich_root_data == 
1447
            target_format.rich_root_data)
1448
1733.1.3 by Robert Collins
Extend the test suite to run bzrdir conformance tests on non .bzr based control dirs.
1449
    @classmethod
1450
    def known_formats(klass):
1451
        """Return all the known formats.
1452
        
1453
        Concrete formats should override _known_formats.
1454
        """
1733.1.6 by Jelmer Vernooij
Fix a couple of minor issues after review by Martin.
1455
        # There is double indirection here to make sure that control 
1456
        # formats used by more than one dir format will only be probed 
1457
        # once. This can otherwise be quite expensive for remote connections.
1733.1.3 by Robert Collins
Extend the test suite to run bzrdir conformance tests on non .bzr based control dirs.
1458
        result = set()
1459
        for format in klass._control_formats:
1460
            result.update(format._known_formats())
1461
        return result
1462
    
1463
    @classmethod
1464
    def _known_formats(klass):
1465
        """Return the known format instances for this control format."""
1466
        return set(klass._formats.values())
1467
1534.4.39 by Robert Collins
Basic BzrDir support.
1468
    def open(self, transport, _found=False):
1469
        """Return an instance of this format for the dir transport points at.
1470
        
1471
        _found is a private parameter, do not use it.
1472
        """
1473
        if not _found:
2090.2.2 by Martin Pool
Fix an assertion with side effects
1474
            found_format = BzrDirFormat.find_format(transport)
1475
            if not isinstance(found_format, self.__class__):
1476
                raise AssertionError("%s was asked to open %s, but it seems to need "
1477
                        "format %s" 
1478
                        % (self, transport, found_format))
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1479
        return self._open(transport)
1480
1481
    def _open(self, transport):
1482
        """Template method helper for opening BzrDirectories.
1483
1484
        This performs the actual open and any additional logic or parameter
1485
        passing.
1486
        """
1487
        raise NotImplementedError(self._open)
1534.4.39 by Robert Collins
Basic BzrDir support.
1488
1489
    @classmethod
1490
    def register_format(klass, format):
1491
        klass._formats[format.get_format_string()] = format
1492
1493
    @classmethod
1733.1.1 by Robert Collins
Support non '.bzr' control directories in bzrdir.
1494
    def register_control_format(klass, format):
2164.2.13 by v.ladeuil+lp at free
Add tests for redirection. Preserve transport decorations.
1495
        """Register a format that does not use '.bzr' for its control dir.
1733.1.1 by Robert Collins
Support non '.bzr' control directories in bzrdir.
1496
1497
        TODO: This should be pulled up into a 'ControlDirFormat' base class
1498
        which BzrDirFormat can inherit from, and renamed to register_format 
1499
        there. It has been done without that for now for simplicity of
1500
        implementation.
1501
        """
1733.1.7 by Jelmer Vernooij
Change set of control dir formats to list.
1502
        klass._control_formats.append(format)
1733.1.1 by Robert Collins
Support non '.bzr' control directories in bzrdir.
1503
1504
    @classmethod
2018.5.163 by Andrew Bennetts
Deal with various review comments from Robert.
1505
    def register_control_server_format(klass, format):
1506
        """Register a control format for client-server environments.
1507
1508
        These formats will be tried before ones registered with
1509
        register_control_format.  This gives implementations that decide to the
1510
        chance to grab it before anything looks at the contents of the format
1511
        file.
1512
        """
2018.5.169 by Andrew Bennetts
Add a _server_formats flag to BzrDir.open_from_transport and BzrDirFormat.find_format, make RemoteBranch.control_files into a property.
1513
        klass._control_server_formats.append(format)
2018.5.163 by Andrew Bennetts
Deal with various review comments from Robert.
1514
1515
    @classmethod
2204.4.12 by Aaron Bentley
Deprecate bzrdir.BzrDirFormat.set_default_format
1516
    @symbol_versioning.deprecated_method(symbol_versioning.zero_fourteen)
1534.4.39 by Robert Collins
Basic BzrDir support.
1517
    def set_default_format(klass, format):
2204.5.2 by Aaron Bentley
Tweak set_default_format
1518
        klass._set_default_format(format)
1534.4.39 by Robert Collins
Basic BzrDir support.
1519
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
1520
    @classmethod
1521
    def _set_default_format(klass, format):
1522
        """Set default format (for testing behavior of defaults only)"""
1523
        klass._default_format = format
1524
1534.5.1 by Robert Collins
Give info some reasonable output and tests.
1525
    def __str__(self):
2830.1.1 by Ian Clatworthy
bzrdir.py code clean-ups
1526
        # Trim the newline
1527
        return self.get_format_string().rstrip()
1534.5.1 by Robert Collins
Give info some reasonable output and tests.
1528
1534.4.39 by Robert Collins
Basic BzrDir support.
1529
    @classmethod
1530
    def unregister_format(klass, format):
1531
        assert klass._formats[format.get_format_string()] is format
1532
        del klass._formats[format.get_format_string()]
1533
1733.1.1 by Robert Collins
Support non '.bzr' control directories in bzrdir.
1534
    @classmethod
1535
    def unregister_control_format(klass, format):
1536
        klass._control_formats.remove(format)
1537
1538
1534.4.39 by Robert Collins
Basic BzrDir support.
1539
class BzrDirFormat4(BzrDirFormat):
1540
    """Bzr dir format 4.
1541
1542
    This format is a combined format for working tree, branch and repository.
1543
    It has:
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1544
     - Format 1 working trees [always]
1545
     - Format 4 branches [always]
1546
     - Format 4 repositories [always]
1534.4.39 by Robert Collins
Basic BzrDir support.
1547
1548
    This format is deprecated: it indexes texts using a text it which is
1549
    removed in format 5; write support for this format has been removed.
1550
    """
1551
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
1552
    _lock_class = lockable_files.TransportLock
1553.5.69 by Martin Pool
BzrDirFormat subclasses can now control what kind of overall lock is used.
1553
1534.4.39 by Robert Collins
Basic BzrDir support.
1554
    def get_format_string(self):
1555
        """See BzrDirFormat.get_format_string()."""
1556
        return "Bazaar-NG branch, format 0.0.4\n"
1557
1624.3.19 by Olaf Conradi
New call get_format_description to give a user-friendly description of a
1558
    def get_format_description(self):
1559
        """See BzrDirFormat.get_format_description()."""
1560
        return "All-in-one format 4"
1561
1534.5.16 by Robert Collins
Review feedback.
1562
    def get_converter(self, format=None):
1563
        """See BzrDirFormat.get_converter()."""
1534.5.13 by Robert Collins
Correct buggy test.
1564
        # there is one and only one upgrade path here.
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1565
        return ConvertBzrDir4To5()
1566
        
1651.1.6 by Martin Pool
Clean up clone-bzrdir code
1567
    def initialize_on_transport(self, transport):
1534.4.39 by Robert Collins
Basic BzrDir support.
1568
        """Format 4 branches cannot be created."""
1569
        raise errors.UninitializableFormat(self)
1570
1571
    def is_supported(self):
1572
        """Format 4 is not supported.
1573
1574
        It is not supported because the model changed from 4 to 5 and the
1575
        conversion logic is expensive - so doing it on the fly was not 
1576
        feasible.
1577
        """
1578
        return False
1579
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1580
    def _open(self, transport):
1581
        """See BzrDirFormat._open."""
1582
        return BzrDir4(transport, self)
1583
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
1584
    def __return_repository_format(self):
1585
        """Circular import protection."""
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
1586
        from bzrlib.repofmt.weaverepo import RepositoryFormat4
1910.2.12 by Aaron Bentley
Implement knit repo format 2
1587
        return RepositoryFormat4()
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
1588
    repository_format = property(__return_repository_format)
1589
1534.4.39 by Robert Collins
Basic BzrDir support.
1590
1591
class BzrDirFormat5(BzrDirFormat):
1592
    """Bzr control format 5.
1593
1594
    This format is a combined format for working tree, branch and repository.
1595
    It has:
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1596
     - Format 2 working trees [always] 
1597
     - Format 4 branches [always] 
1534.4.53 by Robert Collins
Review feedback from John Meinel.
1598
     - Format 5 repositories [always]
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1599
       Unhashed stores in the repository.
1534.4.39 by Robert Collins
Basic BzrDir support.
1600
    """
1601
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
1602
    _lock_class = lockable_files.TransportLock
1553.5.69 by Martin Pool
BzrDirFormat subclasses can now control what kind of overall lock is used.
1603
1534.4.39 by Robert Collins
Basic BzrDir support.
1604
    def get_format_string(self):
1605
        """See BzrDirFormat.get_format_string()."""
1606
        return "Bazaar-NG branch, format 5\n"
1607
1624.3.19 by Olaf Conradi
New call get_format_description to give a user-friendly description of a
1608
    def get_format_description(self):
1609
        """See BzrDirFormat.get_format_description()."""
1610
        return "All-in-one format 5"
1611
1534.5.16 by Robert Collins
Review feedback.
1612
    def get_converter(self, format=None):
1613
        """See BzrDirFormat.get_converter()."""
1534.5.13 by Robert Collins
Correct buggy test.
1614
        # there is one and only one upgrade path here.
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1615
        return ConvertBzrDir5To6()
1651.1.6 by Martin Pool
Clean up clone-bzrdir code
1616
1617
    def _initialize_for_clone(self, url):
1618
        return self.initialize_on_transport(get_transport(url), _cloning=True)
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1619
        
1608.2.8 by Martin Pool
Separate out BzrDir.initialize_on_transport so it
1620
    def initialize_on_transport(self, transport, _cloning=False):
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1621
        """Format 5 dirs always have working tree, branch and repository.
1622
        
1623
        Except when they are being cloned.
1624
        """
1625
        from bzrlib.branch import BzrBranchFormat4
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
1626
        from bzrlib.repofmt.weaverepo import RepositoryFormat5
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1627
        from bzrlib.workingtree import WorkingTreeFormat2
1651.1.6 by Martin Pool
Clean up clone-bzrdir code
1628
        result = (super(BzrDirFormat5, self).initialize_on_transport(transport))
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1629
        RepositoryFormat5().initialize(result, _internal=True)
1630
        if not _cloning:
1910.5.1 by Andrew Bennetts
Make some old formats create at least a stub working tree rather than incomplete bzrdirs, and change some tests to use the test suite transport rather than hard-coded to local-only.
1631
            branch = BzrBranchFormat4().initialize(result)
1632
            try:
1633
                WorkingTreeFormat2().initialize(result)
1634
            except errors.NotLocalUrl:
1635
                # Even though we can't access the working tree, we need to
1636
                # create its control files.
1637
                WorkingTreeFormat2().stub_initialize_remote(branch.control_files)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1638
        return result
1639
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1640
    def _open(self, transport):
1641
        """See BzrDirFormat._open."""
1642
        return BzrDir5(transport, self)
1643
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
1644
    def __return_repository_format(self):
1645
        """Circular import protection."""
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
1646
        from bzrlib.repofmt.weaverepo import RepositoryFormat5
1910.2.12 by Aaron Bentley
Implement knit repo format 2
1647
        return RepositoryFormat5()
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
1648
    repository_format = property(__return_repository_format)
1649
1534.4.39 by Robert Collins
Basic BzrDir support.
1650
1651
class BzrDirFormat6(BzrDirFormat):
1652
    """Bzr control format 6.
1653
1654
    This format is a combined format for working tree, branch and repository.
1655
    It has:
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1656
     - Format 2 working trees [always] 
1657
     - Format 4 branches [always] 
1658
     - Format 6 repositories [always]
1534.4.39 by Robert Collins
Basic BzrDir support.
1659
    """
1660
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
1661
    _lock_class = lockable_files.TransportLock
1553.5.69 by Martin Pool
BzrDirFormat subclasses can now control what kind of overall lock is used.
1662
1534.4.39 by Robert Collins
Basic BzrDir support.
1663
    def get_format_string(self):
1664
        """See BzrDirFormat.get_format_string()."""
1665
        return "Bazaar-NG branch, format 6\n"
1666
1624.3.19 by Olaf Conradi
New call get_format_description to give a user-friendly description of a
1667
    def get_format_description(self):
1668
        """See BzrDirFormat.get_format_description()."""
1669
        return "All-in-one format 6"
1670
1534.5.16 by Robert Collins
Review feedback.
1671
    def get_converter(self, format=None):
1672
        """See BzrDirFormat.get_converter()."""
1534.5.13 by Robert Collins
Correct buggy test.
1673
        # there is one and only one upgrade path here.
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1674
        return ConvertBzrDir6ToMeta()
1675
        
1651.1.6 by Martin Pool
Clean up clone-bzrdir code
1676
    def _initialize_for_clone(self, url):
1677
        return self.initialize_on_transport(get_transport(url), _cloning=True)
1678
1608.2.8 by Martin Pool
Separate out BzrDir.initialize_on_transport so it
1679
    def initialize_on_transport(self, transport, _cloning=False):
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1680
        """Format 6 dirs always have working tree, branch and repository.
1681
        
1682
        Except when they are being cloned.
1683
        """
1684
        from bzrlib.branch import BzrBranchFormat4
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
1685
        from bzrlib.repofmt.weaverepo import RepositoryFormat6
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1686
        from bzrlib.workingtree import WorkingTreeFormat2
1651.1.6 by Martin Pool
Clean up clone-bzrdir code
1687
        result = super(BzrDirFormat6, self).initialize_on_transport(transport)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1688
        RepositoryFormat6().initialize(result, _internal=True)
1689
        if not _cloning:
1910.5.1 by Andrew Bennetts
Make some old formats create at least a stub working tree rather than incomplete bzrdirs, and change some tests to use the test suite transport rather than hard-coded to local-only.
1690
            branch = BzrBranchFormat4().initialize(result)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1691
            try:
1692
                WorkingTreeFormat2().initialize(result)
1693
            except errors.NotLocalUrl:
1910.5.1 by Andrew Bennetts
Make some old formats create at least a stub working tree rather than incomplete bzrdirs, and change some tests to use the test suite transport rather than hard-coded to local-only.
1694
                # Even though we can't access the working tree, we need to
1695
                # create its control files.
1696
                WorkingTreeFormat2().stub_initialize_remote(branch.control_files)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1697
        return result
1698
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1699
    def _open(self, transport):
1700
        """See BzrDirFormat._open."""
1701
        return BzrDir6(transport, self)
1702
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
1703
    def __return_repository_format(self):
1704
        """Circular import protection."""
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
1705
        from bzrlib.repofmt.weaverepo import RepositoryFormat6
1910.2.12 by Aaron Bentley
Implement knit repo format 2
1706
        return RepositoryFormat6()
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
1707
    repository_format = property(__return_repository_format)
1708
1534.4.39 by Robert Collins
Basic BzrDir support.
1709
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1710
class BzrDirMetaFormat1(BzrDirFormat):
1711
    """Bzr meta control format 1
1712
1713
    This is the first format with split out working tree, branch and repository
1714
    disk storage.
1715
    It has:
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
1716
     - Format 3 working trees [optional]
1717
     - Format 5 branches [optional]
1718
     - Format 7 repositories [optional]
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1719
    """
1720
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
1721
    _lock_class = lockdir.LockDir
1553.5.69 by Martin Pool
BzrDirFormat subclasses can now control what kind of overall lock is used.
1722
2100.3.10 by Aaron Bentley
Ensure added references are serialized properly, beef up Workingtreee3
1723
    def __init__(self):
1724
        self._workingtree_format = None
2230.3.1 by Aaron Bentley
Get branch6 creation working
1725
        self._branch_format = None
2100.3.10 by Aaron Bentley
Ensure added references are serialized properly, beef up Workingtreee3
1726
2100.3.15 by Aaron Bentley
get test suite passing
1727
    def __eq__(self, other):
1728
        if other.__class__ is not self.__class__:
1729
            return False
1730
        if other.repository_format != self.repository_format:
1731
            return False
1732
        if other.workingtree_format != self.workingtree_format:
1733
            return False
1734
        return True
1735
2100.3.35 by Aaron Bentley
equality operations on bzrdir
1736
    def __ne__(self, other):
1737
        return not self == other
1738
2230.3.55 by Aaron Bentley
Updates from review
1739
    def get_branch_format(self):
2230.3.1 by Aaron Bentley
Get branch6 creation working
1740
        if self._branch_format is None:
1741
            from bzrlib.branch import BranchFormat
1742
            self._branch_format = BranchFormat.get_default_format()
1743
        return self._branch_format
1744
2230.3.55 by Aaron Bentley
Updates from review
1745
    def set_branch_format(self, format):
2230.3.1 by Aaron Bentley
Get branch6 creation working
1746
        self._branch_format = format
1747
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
1748
    def get_converter(self, format=None):
1749
        """See BzrDirFormat.get_converter()."""
1750
        if format is None:
1751
            format = BzrDirFormat.get_default_format()
1752
        if not isinstance(self, format.__class__):
1753
            # converting away from metadir is not implemented
1754
            raise NotImplementedError(self.get_converter)
1755
        return ConvertMetaToMeta(format)
1756
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1757
    def get_format_string(self):
1758
        """See BzrDirFormat.get_format_string()."""
1759
        return "Bazaar-NG meta directory, format 1\n"
1760
1624.3.19 by Olaf Conradi
New call get_format_description to give a user-friendly description of a
1761
    def get_format_description(self):
1762
        """See BzrDirFormat.get_format_description()."""
1763
        return "Meta directory format 1"
1764
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1765
    def _open(self, transport):
1766
        """See BzrDirFormat._open."""
2230.3.24 by Aaron Bentley
Remove format-on-open code
1767
        return BzrDirMeta1(transport, self)
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1768
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
1769
    def __return_repository_format(self):
1770
        """Circular import protection."""
1771
        if getattr(self, '_repository_format', None):
1772
            return self._repository_format
1773
        from bzrlib.repository import RepositoryFormat
1774
        return RepositoryFormat.get_default_format()
1775
1776
    def __set_repository_format(self, value):
3015.2.8 by Robert Collins
Typo in __set_repository_format's docstring.
1777
        """Allow changing the repository format for metadir formats."""
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
1778
        self._repository_format = value
1553.5.72 by Martin Pool
Clean up test for Branch5 lockdirs
1779
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
1780
    repository_format = property(__return_repository_format, __set_repository_format)
1781
2100.3.10 by Aaron Bentley
Ensure added references are serialized properly, beef up Workingtreee3
1782
    def __get_workingtree_format(self):
1783
        if self._workingtree_format is None:
1784
            from bzrlib.workingtree import WorkingTreeFormat
1785
            self._workingtree_format = WorkingTreeFormat.get_default_format()
1786
        return self._workingtree_format
1787
1788
    def __set_workingtree_format(self, wt_format):
1789
        self._workingtree_format = wt_format
1790
1791
    workingtree_format = property(__get_workingtree_format,
1792
                                  __set_workingtree_format)
1793
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1794
2164.2.19 by Vincent Ladeuil
Revert BzrDirFormat1 registering.
1795
# Register bzr control format
1796
BzrDirFormat.register_control_format(BzrDirFormat)
2164.2.13 by v.ladeuil+lp at free
Add tests for redirection. Preserve transport decorations.
1797
1798
# Register bzr formats
1534.4.39 by Robert Collins
Basic BzrDir support.
1799
BzrDirFormat.register_format(BzrDirFormat4())
1800
BzrDirFormat.register_format(BzrDirFormat5())
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
1801
BzrDirFormat.register_format(BzrDirFormat6())
1802
__default_format = BzrDirMetaFormat1()
1534.4.39 by Robert Collins
Basic BzrDir support.
1803
BzrDirFormat.register_format(__default_format)
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
1804
BzrDirFormat._default_format = __default_format
1534.4.39 by Robert Collins
Basic BzrDir support.
1805
1806
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1807
class Converter(object):
1808
    """Converts a disk format object from one format to another."""
1809
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1810
    def convert(self, to_convert, pb):
1811
        """Perform the conversion of to_convert, giving feedback via pb.
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1812
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1813
        :param to_convert: The disk object to convert.
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1814
        :param pb: a progress bar to use for progress information.
1815
        """
1816
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
1817
    def step(self, message):
1818
        """Update the pb by a step."""
1819
        self.count +=1
1820
        self.pb.update(message, self.count, self.total)
1821
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1822
1823
class ConvertBzrDir4To5(Converter):
1824
    """Converts format 4 bzr dirs to format 5."""
1825
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1826
    def __init__(self):
1827
        super(ConvertBzrDir4To5, self).__init__()
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1828
        self.converted_revs = set()
1829
        self.absent_revisions = set()
1830
        self.text_count = 0
1831
        self.revisions = {}
1832
        
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1833
    def convert(self, to_convert, pb):
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1834
        """See Converter.convert()."""
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1835
        self.bzrdir = to_convert
1836
        self.pb = pb
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1837
        self.pb.note('starting upgrade from format 4 to 5')
1838
        if isinstance(self.bzrdir.transport, LocalTransport):
1839
            self.bzrdir.get_workingtree_transport(None).delete('stat-cache')
1840
        self._convert_to_weaves()
1841
        return BzrDir.open(self.bzrdir.root_transport.base)
1842
1843
    def _convert_to_weaves(self):
1844
        self.pb.note('note: upgrade may be faster if all store files are ungzipped first')
1845
        try:
1846
            # TODO permissions
1847
            stat = self.bzrdir.transport.stat('weaves')
1848
            if not S_ISDIR(stat.st_mode):
1849
                self.bzrdir.transport.delete('weaves')
1850
                self.bzrdir.transport.mkdir('weaves')
1851
        except errors.NoSuchFile:
1852
            self.bzrdir.transport.mkdir('weaves')
1563.2.10 by Robert Collins
Change weave store to be a versioned store, using WeaveFiles which maintain integrity without needing explicit 'put' operations.
1853
        # deliberately not a WeaveFile as we want to build it up slowly.
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1854
        self.inv_weave = Weave('inventory')
1855
        # holds in-memory weaves for all files
1856
        self.text_weaves = {}
1857
        self.bzrdir.transport.delete('branch-format')
1858
        self.branch = self.bzrdir.open_branch()
1859
        self._convert_working_inv()
1860
        rev_history = self.branch.revision_history()
1861
        # to_read is a stack holding the revisions we still need to process;
1862
        # appending to it adds new highest-priority revisions
1863
        self.known_revisions = set(rev_history)
1864
        self.to_read = rev_history[-1:]
1865
        while self.to_read:
1866
            rev_id = self.to_read.pop()
1867
            if (rev_id not in self.revisions
1868
                and rev_id not in self.absent_revisions):
1869
                self._load_one_rev(rev_id)
1870
        self.pb.clear()
1871
        to_import = self._make_order()
1872
        for i, rev_id in enumerate(to_import):
1873
            self.pb.update('converting revision', i, len(to_import))
1874
            self._convert_one_rev(rev_id)
1875
        self.pb.clear()
1876
        self._write_all_weaves()
1877
        self._write_all_revs()
1878
        self.pb.note('upgraded to weaves:')
1879
        self.pb.note('  %6d revisions and inventories', len(self.revisions))
1880
        self.pb.note('  %6d revisions not present', len(self.absent_revisions))
1881
        self.pb.note('  %6d texts', self.text_count)
1882
        self._cleanup_spare_files_after_format4()
1883
        self.branch.control_files.put_utf8('branch-format', BzrDirFormat5().get_format_string())
1884
1885
    def _cleanup_spare_files_after_format4(self):
1886
        # FIXME working tree upgrade foo.
1887
        for n in 'merged-patches', 'pending-merged-patches':
1888
            try:
1889
                ## assert os.path.getsize(p) == 0
1890
                self.bzrdir.transport.delete(n)
1891
            except errors.NoSuchFile:
1892
                pass
1893
        self.bzrdir.transport.delete_tree('inventory-store')
1894
        self.bzrdir.transport.delete_tree('text-store')
1895
1896
    def _convert_working_inv(self):
1996.3.6 by John Arbash Meinel
Find a few places that weren't importing their dependencies.
1897
        inv = xml4.serializer_v4.read_inventory(
1898
                    self.branch.control_files.get('inventory'))
2817.2.1 by Robert Collins
* Inventory serialisation no longer double-sha's the content.
1899
        new_inv_xml = xml5.serializer_v5.write_inventory_to_string(inv, working=True)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1900
        # FIXME inventory is a working tree change.
1955.3.4 by John Arbash Meinel
Fix 2 calls to 'put()' that were using strings instead of files
1901
        self.branch.control_files.put('inventory', StringIO(new_inv_xml))
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1902
1903
    def _write_all_weaves(self):
1904
        controlweaves = WeaveStore(self.bzrdir.transport, prefixed=False)
1905
        weave_transport = self.bzrdir.transport.clone('weaves')
1906
        weaves = WeaveStore(weave_transport, prefixed=False)
1563.2.34 by Robert Collins
Remove the commit and rollback transaction methods as misleading, and implement a WriteTransaction
1907
        transaction = WriteTransaction()
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1908
1909
        try:
1563.2.10 by Robert Collins
Change weave store to be a versioned store, using WeaveFiles which maintain integrity without needing explicit 'put' operations.
1910
            i = 0
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1911
            for file_id, file_weave in self.text_weaves.items():
1912
                self.pb.update('writing weave', i, len(self.text_weaves))
1563.2.10 by Robert Collins
Change weave store to be a versioned store, using WeaveFiles which maintain integrity without needing explicit 'put' operations.
1913
                weaves._put_weave(file_id, file_weave, transaction)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1914
                i += 1
1563.2.10 by Robert Collins
Change weave store to be a versioned store, using WeaveFiles which maintain integrity without needing explicit 'put' operations.
1915
            self.pb.update('inventory', 0, 1)
1916
            controlweaves._put_weave('inventory', self.inv_weave, transaction)
1917
            self.pb.update('inventory', 1, 1)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1918
        finally:
1919
            self.pb.clear()
1920
1921
    def _write_all_revs(self):
1922
        """Write all revisions out in new form."""
1923
        self.bzrdir.transport.delete_tree('revision-store')
1924
        self.bzrdir.transport.mkdir('revision-store')
1925
        revision_transport = self.bzrdir.transport.clone('revision-store')
1926
        # TODO permissions
1563.2.28 by Robert Collins
Add total_size to the revision_store api.
1927
        _revision_store = TextRevisionStore(TextStore(revision_transport,
1928
                                                      prefixed=False,
1929
                                                      compressed=True))
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1930
        try:
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
1931
            transaction = WriteTransaction()
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1932
            for i, rev_id in enumerate(self.converted_revs):
1933
                self.pb.update('write revision', i, len(self.converted_revs))
1563.2.28 by Robert Collins
Add total_size to the revision_store api.
1934
                _revision_store.add_revision(self.revisions[rev_id], transaction)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1935
        finally:
1936
            self.pb.clear()
1937
            
1938
    def _load_one_rev(self, rev_id):
1939
        """Load a revision object into memory.
1940
1941
        Any parents not either loaded or abandoned get queued to be
1942
        loaded."""
1943
        self.pb.update('loading revision',
1944
                       len(self.revisions),
1945
                       len(self.known_revisions))
1563.2.22 by Robert Collins
Move responsibility for repository.has_revision into RevisionStore
1946
        if not self.branch.repository.has_revision(rev_id):
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1947
            self.pb.clear()
1948
            self.pb.note('revision {%s} not present in branch; '
1949
                         'will be converted as a ghost',
1950
                         rev_id)
1951
            self.absent_revisions.add(rev_id)
1952
        else:
1563.2.28 by Robert Collins
Add total_size to the revision_store api.
1953
            rev = self.branch.repository._revision_store.get_revision(rev_id,
1954
                self.branch.repository.get_transaction())
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1955
            for parent_id in rev.parent_ids:
1956
                self.known_revisions.add(parent_id)
1957
                self.to_read.append(parent_id)
1958
            self.revisions[rev_id] = rev
1959
1960
    def _load_old_inventory(self, rev_id):
1961
        assert rev_id not in self.converted_revs
1962
        old_inv_xml = self.branch.repository.inventory_store.get(rev_id).read()
1996.3.6 by John Arbash Meinel
Find a few places that weren't importing their dependencies.
1963
        inv = xml4.serializer_v4.read_inventory_from_string(old_inv_xml)
1910.2.36 by Aaron Bentley
Get upgrade from format4 under test and fixed for all formats
1964
        inv.revision_id = rev_id
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1965
        rev = self.revisions[rev_id]
1966
        if rev.inventory_sha1:
1967
            assert rev.inventory_sha1 == sha_string(old_inv_xml), \
1968
                'inventory sha mismatch for {%s}' % rev_id
1969
        return inv
1970
1971
    def _load_updated_inventory(self, rev_id):
1972
        assert rev_id in self.converted_revs
1973
        inv_xml = self.inv_weave.get_text(rev_id)
1996.3.6 by John Arbash Meinel
Find a few places that weren't importing their dependencies.
1974
        inv = xml5.serializer_v5.read_inventory_from_string(inv_xml)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1975
        return inv
1976
1977
    def _convert_one_rev(self, rev_id):
1978
        """Convert revision and all referenced objects to new format."""
1979
        rev = self.revisions[rev_id]
1980
        inv = self._load_old_inventory(rev_id)
1981
        present_parents = [p for p in rev.parent_ids
1982
                           if p not in self.absent_revisions]
1983
        self._convert_revision_contents(rev, inv, present_parents)
2817.2.1 by Robert Collins
* Inventory serialisation no longer double-sha's the content.
1984
        self._store_new_inv(rev, inv, present_parents)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1985
        self.converted_revs.add(rev_id)
1986
2817.2.1 by Robert Collins
* Inventory serialisation no longer double-sha's the content.
1987
    def _store_new_inv(self, rev, inv, present_parents):
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1988
        # the XML is now updated with text versions
1989
        if __debug__:
1907.1.8 by Aaron Bentley
Remove is_root
1990
            entries = inv.iter_entries()
1991
            entries.next()
1992
            for path, ie in entries:
1963.2.6 by Robey Pointer
pychecker is on crack; go back to using 'is None'.
1993
                assert getattr(ie, 'revision', None) is not None, \
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1994
                    'no revision on {%s} in {%s}' % \
1995
                    (file_id, rev.revision_id)
1996.3.6 by John Arbash Meinel
Find a few places that weren't importing their dependencies.
1996
        new_inv_xml = xml5.serializer_v5.write_inventory_to_string(inv)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1997
        new_inv_sha1 = sha_string(new_inv_xml)
2817.2.1 by Robert Collins
* Inventory serialisation no longer double-sha's the content.
1998
        self.inv_weave.add_lines(rev.revision_id,
1563.2.28 by Robert Collins
Add total_size to the revision_store api.
1999
                                 present_parents,
2000
                                 new_inv_xml.splitlines(True))
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2001
        rev.inventory_sha1 = new_inv_sha1
2002
2003
    def _convert_revision_contents(self, rev, inv, present_parents):
2004
        """Convert all the files within a revision.
2005
2006
        Also upgrade the inventory to refer to the text revision ids."""
2007
        rev_id = rev.revision_id
2008
        mutter('converting texts of revision {%s}',
2009
               rev_id)
2010
        parent_invs = map(self._load_updated_inventory, present_parents)
1731.1.62 by Aaron Bentley
Changes from review comments
2011
        entries = inv.iter_entries()
2012
        entries.next()
2013
        for path, ie in entries:
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2014
            self._convert_file_version(rev, ie, parent_invs)
2015
2016
    def _convert_file_version(self, rev, ie, parent_invs):
2017
        """Convert one version of one file.
2018
2019
        The file needs to be added into the weave if it is a merge
2020
        of >=2 parents or if it's changed from its parent.
2021
        """
2022
        file_id = ie.file_id
2023
        rev_id = rev.revision_id
2024
        w = self.text_weaves.get(file_id)
2025
        if w is None:
2026
            w = Weave(file_id)
2027
            self.text_weaves[file_id] = w
2028
        text_changed = False
2776.1.5 by Robert Collins
Add reasonably comprehensive tests for path last modified and per file graph behaviour.
2029
        parent_candiate_entries = ie.parent_candidates(parent_invs)
2030
        for old_revision in parent_candiate_entries.keys():
2031
            # if this fails, its a ghost ?
2032
            assert old_revision in self.converted_revs, \
2033
                "Revision {%s} not in converted_revs" % old_revision
2034
        heads = graph.Graph(self).heads(parent_candiate_entries.keys())
2035
        # XXX: Note that this is unordered - and this is tolerable because 
2036
        # the previous code was also unordered.
2037
        previous_entries = dict((head, parent_candiate_entries[head]) for head
2038
            in heads)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2039
        self.snapshot_ie(previous_entries, ie, w, rev_id)
2040
        del ie.text_id
2041
        assert getattr(ie, 'revision', None) is not None
2042
3099.3.7 by John Arbash Meinel
Another parent provider I didn't realize existed.
2043
    @symbol_versioning.deprecated_method(symbol_versioning.one_one)
2776.1.5 by Robert Collins
Add reasonably comprehensive tests for path last modified and per file graph behaviour.
2044
    def get_parents(self, revision_ids):
2045
        for revision_id in revision_ids:
2046
            yield self.revisions[revision_id].parent_ids
2047
3099.3.7 by John Arbash Meinel
Another parent provider I didn't realize existed.
2048
    def get_parent_map(self, revision_ids):
2049
        """See graph._StackedParentsProvider.get_parent_map"""
2050
        return dict((revision_id, self.revisions[revision_id])
2051
                    for revision_id in revision_ids
2052
                     if revision_id in self.revisions)
2053
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2054
    def snapshot_ie(self, previous_revisions, ie, w, rev_id):
2055
        # TODO: convert this logic, which is ~= snapshot to
2056
        # a call to:. This needs the path figured out. rather than a work_tree
2057
        # a v4 revision_tree can be given, or something that looks enough like
2058
        # one to give the file content to the entry if it needs it.
2059
        # and we need something that looks like a weave store for snapshot to 
2060
        # save against.
2061
        #ie.snapshot(rev, PATH, previous_revisions, REVISION_TREE, InMemoryWeaveStore(self.text_weaves))
2062
        if len(previous_revisions) == 1:
2063
            previous_ie = previous_revisions.values()[0]
2064
            if ie._unchanged(previous_ie):
2065
                ie.revision = previous_ie.revision
2066
                return
2067
        if ie.has_text():
2592.3.56 by Robert Collins
Remove legacy text_store attribute from repository objects.
2068
            text = self.branch.repository.weave_store.get(ie.text_id)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2069
            file_lines = text.readlines()
2070
            assert sha_strings(file_lines) == ie.text_sha1
2071
            assert sum(map(len, file_lines)) == ie.text_size
1563.2.18 by Robert Collins
get knit repositories really using knits for text storage.
2072
            w.add_lines(rev_id, previous_revisions, file_lines)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2073
            self.text_count += 1
2074
        else:
1563.2.18 by Robert Collins
get knit repositories really using knits for text storage.
2075
            w.add_lines(rev_id, previous_revisions, [])
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2076
        ie.revision = rev_id
2077
2078
    def _make_order(self):
2079
        """Return a suitable order for importing revisions.
2080
2081
        The order must be such that an revision is imported after all
2082
        its (present) parents.
2083
        """
2084
        todo = set(self.revisions.keys())
2085
        done = self.absent_revisions.copy()
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
2086
        order = []
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2087
        while todo:
2088
            # scan through looking for a revision whose parents
2089
            # are all done
2090
            for rev_id in sorted(list(todo)):
2091
                rev = self.revisions[rev_id]
2092
                parent_ids = set(rev.parent_ids)
2093
                if parent_ids.issubset(done):
2094
                    # can take this one now
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
2095
                    order.append(rev_id)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2096
                    todo.remove(rev_id)
2097
                    done.add(rev_id)
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
2098
        return order
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2099
2100
2101
class ConvertBzrDir5To6(Converter):
2102
    """Converts format 5 bzr dirs to format 6."""
2103
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
2104
    def convert(self, to_convert, pb):
2105
        """See Converter.convert()."""
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2106
        self.bzrdir = to_convert
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
2107
        self.pb = pb
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2108
        self.pb.note('starting upgrade from format 5 to 6')
2109
        self._convert_to_prefixed()
2110
        return BzrDir.open(self.bzrdir.root_transport.base)
2111
2112
    def _convert_to_prefixed(self):
1608.2.1 by Martin Pool
[merge] Storage filename escaping
2113
        from bzrlib.store import TransportStore
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2114
        self.bzrdir.transport.delete('branch-format')
2115
        for store_name in ["weaves", "revision-store"]:
1608.2.1 by Martin Pool
[merge] Storage filename escaping
2116
            self.pb.note("adding prefixes to %s" % store_name)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2117
            store_transport = self.bzrdir.transport.clone(store_name)
1608.2.1 by Martin Pool
[merge] Storage filename escaping
2118
            store = TransportStore(store_transport, prefixed=True)
1608.1.1 by Martin Pool
[patch] LocalTransport.list_dir should return url-quoted strings (ddaa)
2119
            for urlfilename in store_transport.list_dir('.'):
1685.1.45 by John Arbash Meinel
Moved url functions into bzrlib.urlutils
2120
                filename = urlutils.unescape(urlfilename)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2121
                if (filename.endswith(".weave") or
2122
                    filename.endswith(".gz") or
2123
                    filename.endswith(".sig")):
2124
                    file_id = os.path.splitext(filename)[0]
2125
                else:
2126
                    file_id = filename
1608.2.1 by Martin Pool
[merge] Storage filename escaping
2127
                prefix_dir = store.hash_prefix(file_id)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
2128
                # FIXME keep track of the dirs made RBC 20060121
2129
                try:
2130
                    store_transport.move(filename, prefix_dir + '/' + filename)
2131
                except errors.NoSuchFile: # catches missing dirs strangely enough
2132
                    store_transport.mkdir(prefix_dir)
2133
                    store_transport.move(filename, prefix_dir + '/' + filename)
2134
        self.bzrdir._control_files.put_utf8('branch-format', BzrDirFormat6().get_format_string())
2135
2136
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
2137
class ConvertBzrDir6ToMeta(Converter):
2138
    """Converts format 6 bzr dirs to metadirs."""
2139
2140
    def convert(self, to_convert, pb):
2141
        """See Converter.convert()."""
2241.1.11 by Martin Pool
Get rid of RepositoryFormat*_instance objects. Instead the format
2142
        from bzrlib.repofmt.weaverepo import RepositoryFormat7
2094.3.5 by John Arbash Meinel
Fix imports to ensure modules are loaded before they are used
2143
        from bzrlib.branch import BzrBranchFormat5
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
2144
        self.bzrdir = to_convert
2145
        self.pb = pb
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
2146
        self.count = 0
2147
        self.total = 20 # the steps we know about
2148
        self.garbage_inventories = []
2149
1534.5.13 by Robert Collins
Correct buggy test.
2150
        self.pb.note('starting upgrade from format 6 to metadir')
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
2151
        self.bzrdir._control_files.put_utf8('branch-format', "Converting to format 6")
2152
        # its faster to move specific files around than to open and use the apis...
2153
        # first off, nuke ancestry.weave, it was never used.
2154
        try:
2155
            self.step('Removing ancestry.weave')
2156
            self.bzrdir.transport.delete('ancestry.weave')
2157
        except errors.NoSuchFile:
2158
            pass
2159
        # find out whats there
2160
        self.step('Finding branch files')
1666.1.3 by Robert Collins
Fix and test upgrades from bzrdir 6 over SFTP.
2161
        last_revision = self.bzrdir.open_branch().last_revision()
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
2162
        bzrcontents = self.bzrdir.transport.list_dir('.')
2163
        for name in bzrcontents:
2164
            if name.startswith('basis-inventory.'):
2165
                self.garbage_inventories.append(name)
2166
        # create new directories for repository, working tree and branch
1553.5.79 by Martin Pool
upgrade to metadir should create LockDirs not files
2167
        self.dir_mode = self.bzrdir._control_files._dir_mode
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
2168
        self.file_mode = self.bzrdir._control_files._file_mode
2169
        repository_names = [('inventory.weave', True),
2170
                            ('revision-store', True),
2171
                            ('weaves', True)]
2172
        self.step('Upgrading repository  ')
1553.5.79 by Martin Pool
upgrade to metadir should create LockDirs not files
2173
        self.bzrdir.transport.mkdir('repository', mode=self.dir_mode)
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
2174
        self.make_lock('repository')
2175
        # we hard code the formats here because we are converting into
2176
        # the meta format. The meta format upgrader can take this to a 
2177
        # future format within each component.
2241.1.11 by Martin Pool
Get rid of RepositoryFormat*_instance objects. Instead the format
2178
        self.put_format('repository', RepositoryFormat7())
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
2179
        for entry in repository_names:
2180
            self.move_entry('repository', entry)
2181
2182
        self.step('Upgrading branch      ')
1553.5.79 by Martin Pool
upgrade to metadir should create LockDirs not files
2183
        self.bzrdir.transport.mkdir('branch', mode=self.dir_mode)
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
2184
        self.make_lock('branch')
2094.3.5 by John Arbash Meinel
Fix imports to ensure modules are loaded before they are used
2185
        self.put_format('branch', BzrBranchFormat5())
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
2186
        branch_files = [('revision-history', True),
2187
                        ('branch-name', True),
2188
                        ('parent', False)]
2189
        for entry in branch_files:
2190
            self.move_entry('branch', entry)
2191
2192
        checkout_files = [('pending-merges', True),
2193
                          ('inventory', True),
2194
                          ('stat-cache', False)]
1959.3.1 by John Arbash Meinel
David Allouche: bzr upgrade should work if there is no working tree
2195
        # If a mandatory checkout file is not present, the branch does not have
2196
        # a functional checkout. Do not create a checkout in the converted
2197
        # branch.
2198
        for name, mandatory in checkout_files:
2199
            if mandatory and name not in bzrcontents:
2200
                has_checkout = False
2201
                break
2202
        else:
2203
            has_checkout = True
2204
        if not has_checkout:
2205
            self.pb.note('No working tree.')
2206
            # If some checkout files are there, we may as well get rid of them.
2207
            for name, mandatory in checkout_files:
2208
                if name in bzrcontents:
2209
                    self.bzrdir.transport.delete(name)
2210
        else:
2123.2.1 by John Arbash Meinel
Fix bug #70716, make bzrlib.bzrdir directly import bzrlib.workingtree
2211
            from bzrlib.workingtree import WorkingTreeFormat3
1959.3.1 by John Arbash Meinel
David Allouche: bzr upgrade should work if there is no working tree
2212
            self.step('Upgrading working tree')
2213
            self.bzrdir.transport.mkdir('checkout', mode=self.dir_mode)
2214
            self.make_lock('checkout')
2215
            self.put_format(
2123.2.1 by John Arbash Meinel
Fix bug #70716, make bzrlib.bzrdir directly import bzrlib.workingtree
2216
                'checkout', WorkingTreeFormat3())
1959.3.1 by John Arbash Meinel
David Allouche: bzr upgrade should work if there is no working tree
2217
            self.bzrdir.transport.delete_multi(
2218
                self.garbage_inventories, self.pb)
2219
            for entry in checkout_files:
2220
                self.move_entry('checkout', entry)
2221
            if last_revision is not None:
2222
                self.bzrdir._control_files.put_utf8(
2223
                    'checkout/last-revision', last_revision)
2224
        self.bzrdir._control_files.put_utf8(
2225
            'branch-format', BzrDirMetaFormat1().get_format_string())
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
2226
        return BzrDir.open(self.bzrdir.root_transport.base)
2227
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
2228
    def make_lock(self, name):
2229
        """Make a lock for the new control dir name."""
2230
        self.step('Make %s lock' % name)
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
2231
        ld = lockdir.LockDir(self.bzrdir.transport,
2232
                             '%s/lock' % name,
2233
                             file_modebits=self.file_mode,
2234
                             dir_modebits=self.dir_mode)
1553.5.79 by Martin Pool
upgrade to metadir should create LockDirs not files
2235
        ld.create()
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
2236
2237
    def move_entry(self, new_dir, entry):
2238
        """Move then entry name into new_dir."""
2239
        name = entry[0]
2240
        mandatory = entry[1]
2241
        self.step('Moving %s' % name)
2242
        try:
2243
            self.bzrdir.transport.move(name, '%s/%s' % (new_dir, name))
2244
        except errors.NoSuchFile:
2245
            if mandatory:
2246
                raise
2247
2248
    def put_format(self, dirname, format):
2249
        self.bzrdir._control_files.put_utf8('%s/format' % dirname, format.get_format_string())
2250
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
2251
2252
class ConvertMetaToMeta(Converter):
2253
    """Converts the components of metadirs."""
2254
2255
    def __init__(self, target_format):
2256
        """Create a metadir to metadir converter.
2257
2258
        :param target_format: The final metadir format that is desired.
2259
        """
2260
        self.target_format = target_format
2261
2262
    def convert(self, to_convert, pb):
2263
        """See Converter.convert()."""
2264
        self.bzrdir = to_convert
2265
        self.pb = pb
2266
        self.count = 0
2267
        self.total = 1
2268
        self.step('checking repository format')
2269
        try:
2270
            repo = self.bzrdir.open_repository()
2271
        except errors.NoRepositoryPresent:
2272
            pass
2273
        else:
2274
            if not isinstance(repo._format, self.target_format.repository_format.__class__):
2275
                from bzrlib.repository import CopyConverter
2276
                self.pb.note('starting repository conversion')
2277
                converter = CopyConverter(self.target_format.repository_format)
2278
                converter.convert(repo, pb)
2230.3.29 by Aaron Bentley
Implement conversion to branch 6
2279
        try:
2280
            branch = self.bzrdir.open_branch()
2281
        except errors.NotBranchError:
2282
            pass
2283
        else:
2255.12.1 by Robert Collins
Implement upgrade for working trees.
2284
            # TODO: conversions of Branch and Tree should be done by
2285
            # InterXFormat lookups
2230.3.29 by Aaron Bentley
Implement conversion to branch 6
2286
            # Avoid circular imports
2287
            from bzrlib import branch as _mod_branch
2288
            if (branch._format.__class__ is _mod_branch.BzrBranchFormat5 and
2230.3.55 by Aaron Bentley
Updates from review
2289
                self.target_format.get_branch_format().__class__ is
2230.3.29 by Aaron Bentley
Implement conversion to branch 6
2290
                _mod_branch.BzrBranchFormat6):
2291
                branch_converter = _mod_branch.Converter5to6()
2292
                branch_converter.convert(branch)
2255.12.1 by Robert Collins
Implement upgrade for working trees.
2293
        try:
2323.6.4 by Martin Pool
BzrDir._check_supported now also takes care of recommending upgrades, which
2294
            tree = self.bzrdir.open_workingtree(recommend_upgrade=False)
2255.2.196 by Robert Collins
Fix test_upgrade defects related to non local or absent working trees.
2295
        except (errors.NoWorkingTree, errors.NotLocalUrl):
2255.12.1 by Robert Collins
Implement upgrade for working trees.
2296
            pass
2297
        else:
2298
            # TODO: conversions of Branch and Tree should be done by
2299
            # InterXFormat lookups
2300
            if (isinstance(tree, workingtree.WorkingTree3) and
2255.2.212 by Robert Collins
Fix upgrade bug exposed via repository tests, dont replace dirstate format trees during upgrade.
2301
                not isinstance(tree, workingtree_4.WorkingTree4) and
2255.12.1 by Robert Collins
Implement upgrade for working trees.
2302
                isinstance(self.target_format.workingtree_format,
2303
                    workingtree_4.WorkingTreeFormat4)):
2304
                workingtree_4.Converter3to4().convert(tree)
1556.1.4 by Robert Collins
Add a new format for what will become knit, and the surrounding logic to upgrade repositories within metadirs, and tests for the same.
2305
        return to_convert
1731.2.18 by Aaron Bentley
Get extract in repository under test
2306
2307
2018.5.25 by Andrew Bennetts
Make sure RemoteBzrDirFormat is always registered (John Arbash Meinel, Robert Collins, Andrew Bennetts).
2308
# This is not in remote.py because it's small, and needs to be registered.
2309
# Putting it in remote.py creates a circular import problem.
2018.5.28 by Robert Collins
Fix RemoteBzrDirFormat probe api usage.
2310
# we can make it a lazy object if the control formats is turned into something
2311
# like a registry.
2018.5.25 by Andrew Bennetts
Make sure RemoteBzrDirFormat is always registered (John Arbash Meinel, Robert Collins, Andrew Bennetts).
2312
class RemoteBzrDirFormat(BzrDirMetaFormat1):
2313
    """Format representing bzrdirs accessed via a smart server"""
2314
2315
    def get_format_description(self):
2316
        return 'bzr remote bzrdir'
2317
    
2018.5.28 by Robert Collins
Fix RemoteBzrDirFormat probe api usage.
2318
    @classmethod
2319
    def probe_transport(klass, transport):
2320
        """Return a RemoteBzrDirFormat object if it looks possible."""
2018.5.25 by Andrew Bennetts
Make sure RemoteBzrDirFormat is always registered (John Arbash Meinel, Robert Collins, Andrew Bennetts).
2321
        try:
2432.3.1 by Andrew Bennetts
Try a version 1 hello probe to determine if we can use RemoteBzrDir on a particular transport, allowing smooth interoperation with older servers.
2322
            client = transport.get_smart_client()
2018.5.25 by Andrew Bennetts
Make sure RemoteBzrDirFormat is always registered (John Arbash Meinel, Robert Collins, Andrew Bennetts).
2323
        except (NotImplementedError, AttributeError,
2324
                errors.TransportNotPossible):
2018.5.28 by Robert Collins
Fix RemoteBzrDirFormat probe api usage.
2325
            # no smart server, so not a branch for this format type.
2326
            raise errors.NotBranchError(path=transport.base)
2018.5.25 by Andrew Bennetts
Make sure RemoteBzrDirFormat is always registered (John Arbash Meinel, Robert Collins, Andrew Bennetts).
2327
        else:
2432.3.3 by Andrew Bennetts
Update comment according to Martin's suggestion.
2328
            # Send a 'hello' request in protocol version one, and decline to
2329
            # open it if the server doesn't support our required version (2) so
2330
            # that the VFS-based transport will do it.
2432.3.1 by Andrew Bennetts
Try a version 1 hello probe to determine if we can use RemoteBzrDir on a particular transport, allowing smooth interoperation with older servers.
2331
            request = client.get_request()
2332
            smart_protocol = protocol.SmartClientRequestProtocolOne(request)
2432.3.2 by Andrew Bennetts
Add test, and tidy implementation.
2333
            server_version = smart_protocol.query_version()
2334
            if server_version != 2:
2432.3.1 by Andrew Bennetts
Try a version 1 hello probe to determine if we can use RemoteBzrDir on a particular transport, allowing smooth interoperation with older servers.
2335
                raise errors.NotBranchError(path=transport.base)
2018.5.28 by Robert Collins
Fix RemoteBzrDirFormat probe api usage.
2336
            return klass()
2018.5.25 by Andrew Bennetts
Make sure RemoteBzrDirFormat is always registered (John Arbash Meinel, Robert Collins, Andrew Bennetts).
2337
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
2338
    def initialize_on_transport(self, transport):
2018.5.128 by Robert Collins
Have RemoteBzrDirFormat create a local format object when it is asked to initialize something on a non-smart transport - allowing sprout to work cleanly.
2339
        try:
2340
            # hand off the request to the smart server
2485.8.54 by Vincent Ladeuil
Refactor medium uses by making a distinction betweem shared and real medium.
2341
            shared_medium = transport.get_shared_medium()
2018.5.128 by Robert Collins
Have RemoteBzrDirFormat create a local format object when it is asked to initialize something on a non-smart transport - allowing sprout to work cleanly.
2342
        except errors.NoSmartMedium:
2343
            # TODO: lookup the local format from a server hint.
2344
            local_dir_format = BzrDirMetaFormat1()
2345
            return local_dir_format.initialize_on_transport(transport)
2485.8.54 by Vincent Ladeuil
Refactor medium uses by making a distinction betweem shared and real medium.
2346
        client = _SmartClient(shared_medium)
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
2347
        path = client.remote_path_from_transport(transport)
2485.8.54 by Vincent Ladeuil
Refactor medium uses by making a distinction betweem shared and real medium.
2348
        response = _SmartClient(shared_medium).call('BzrDirFormat.initialize',
2349
                                                    path)
2018.5.52 by Wouter van Heyst
Provide more information when encountering unexpected responses from a smart
2350
        assert response[0] in ('ok', ), 'unexpected response code %s' % (response,)
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
2351
        return remote.RemoteBzrDir(transport)
2352
2018.5.25 by Andrew Bennetts
Make sure RemoteBzrDirFormat is always registered (John Arbash Meinel, Robert Collins, Andrew Bennetts).
2353
    def _open(self, transport):
2354
        return remote.RemoteBzrDir(transport)
2355
2356
    def __eq__(self, other):
2357
        if not isinstance(other, RemoteBzrDirFormat):
2358
            return False
2359
        return self.get_format_description() == other.get_format_description()
2360
2361
2018.5.163 by Andrew Bennetts
Deal with various review comments from Robert.
2362
BzrDirFormat.register_control_server_format(RemoteBzrDirFormat)
2018.5.45 by Andrew Bennetts
Merge from bzr.dev
2363
2364
2204.4.4 by Aaron Bentley
Use BzrDirFormatInfo to distinguish native and deprecated formats
2365
class BzrDirFormatInfo(object):
2366
2939.2.2 by Ian Clatworthy
allow bzrdir formats to be registered as experimental
2367
    def __init__(self, native, deprecated, hidden, experimental):
2204.4.4 by Aaron Bentley
Use BzrDirFormatInfo to distinguish native and deprecated formats
2368
        self.deprecated = deprecated
2369
        self.native = native
1551.13.2 by Aaron Bentley
Hide dirstate-with-subtree format
2370
        self.hidden = hidden
2939.2.2 by Ian Clatworthy
allow bzrdir formats to be registered as experimental
2371
        self.experimental = experimental
2204.4.4 by Aaron Bentley
Use BzrDirFormatInfo to distinguish native and deprecated formats
2372
2373
2204.4.1 by Aaron Bentley
Add 'formats' help topic
2374
class BzrDirFormatRegistry(registry.Registry):
2375
    """Registry of user-selectable BzrDir subformats.
2376
    
2377
    Differs from BzrDirFormat._control_formats in that it provides sub-formats,
2378
    e.g. BzrDirMeta1 with weave repository.  Also, it's more user-oriented.
2379
    """
2380
2255.2.158 by Martin Pool
Most of the integration of dirstate and subtree
2381
    def register_metadir(self, key,
2382
             repository_format, help, native=True, deprecated=False,
2383
             branch_format=None,
1551.13.2 by Aaron Bentley
Hide dirstate-with-subtree format
2384
             tree_format=None,
2939.2.2 by Ian Clatworthy
allow bzrdir formats to be registered as experimental
2385
             hidden=False,
2386
             experimental=False):
2204.4.1 by Aaron Bentley
Add 'formats' help topic
2387
        """Register a metadir subformat.
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
2388
2389
        These all use a BzrDirMetaFormat1 bzrdir, but can be parameterized
2390
        by the Repository format.
2391
2255.2.158 by Martin Pool
Most of the integration of dirstate and subtree
2392
        :param repository_format: The fully-qualified repository format class
2393
            name as a string.
2394
        :param branch_format: Fully-qualified branch format class name as
2395
            a string.
2396
        :param tree_format: Fully-qualified tree format class name as
2397
            a string.
2204.4.1 by Aaron Bentley
Add 'formats' help topic
2398
        """
2399
        # This should be expanded to support setting WorkingTree and Branch
2400
        # formats, once BzrDirMetaFormat1 supports that.
2255.2.158 by Martin Pool
Most of the integration of dirstate and subtree
2401
        def _load(full_name):
2402
            mod_name, factory_name = full_name.rsplit('.', 1)
2241.1.21 by Martin Pool
Change register_metadir to take fully-qualified repository class name.
2403
            try:
2404
                mod = __import__(mod_name, globals(), locals(),
2255.2.158 by Martin Pool
Most of the integration of dirstate and subtree
2405
                        [factory_name])
2241.1.21 by Martin Pool
Change register_metadir to take fully-qualified repository class name.
2406
            except ImportError, e:
2255.2.158 by Martin Pool
Most of the integration of dirstate and subtree
2407
                raise ImportError('failed to load %s: %s' % (full_name, e))
2241.1.21 by Martin Pool
Change register_metadir to take fully-qualified repository class name.
2408
            try:
2255.2.158 by Martin Pool
Most of the integration of dirstate and subtree
2409
                factory = getattr(mod, factory_name)
2241.1.4 by Martin Pool
Moved old weave-based repository formats into bzrlib.repofmt.weaverepo.
2410
            except AttributeError:
2255.2.158 by Martin Pool
Most of the integration of dirstate and subtree
2411
                raise AttributeError('no factory %s in module %r'
2412
                    % (full_name, mod))
2413
            return factory()
2414
2415
        def helper():
2204.4.1 by Aaron Bentley
Add 'formats' help topic
2416
            bd = BzrDirMetaFormat1()
2230.3.1 by Aaron Bentley
Get branch6 creation working
2417
            if branch_format is not None:
2255.2.158 by Martin Pool
Most of the integration of dirstate and subtree
2418
                bd.set_branch_format(_load(branch_format))
2419
            if tree_format is not None:
2420
                bd.workingtree_format = _load(tree_format)
2421
            if repository_format is not None:
2422
                bd.repository_format = _load(repository_format)
2204.4.1 by Aaron Bentley
Add 'formats' help topic
2423
            return bd
2939.2.2 by Ian Clatworthy
allow bzrdir formats to be registered as experimental
2424
        self.register(key, helper, help, native, deprecated, hidden,
2425
            experimental)
2204.4.1 by Aaron Bentley
Add 'formats' help topic
2426
1551.13.2 by Aaron Bentley
Hide dirstate-with-subtree format
2427
    def register(self, key, factory, help, native=True, deprecated=False,
2939.2.2 by Ian Clatworthy
allow bzrdir formats to be registered as experimental
2428
                 hidden=False, experimental=False):
2204.4.1 by Aaron Bentley
Add 'formats' help topic
2429
        """Register a BzrDirFormat factory.
2430
        
2431
        The factory must be a callable that takes one parameter: the key.
2432
        It must produce an instance of the BzrDirFormat when called.
2433
2434
        This function mainly exists to prevent the info object from being
2435
        supplied directly.
2436
        """
2204.4.4 by Aaron Bentley
Use BzrDirFormatInfo to distinguish native and deprecated formats
2437
        registry.Registry.register(self, key, factory, help, 
2939.2.2 by Ian Clatworthy
allow bzrdir formats to be registered as experimental
2438
            BzrDirFormatInfo(native, deprecated, hidden, experimental))
2204.4.1 by Aaron Bentley
Add 'formats' help topic
2439
2204.4.7 by Aaron Bentley
restore register_lazy, remove register_factory, other updates
2440
    def register_lazy(self, key, module_name, member_name, help, native=True,
2939.2.2 by Ian Clatworthy
allow bzrdir formats to be registered as experimental
2441
                      deprecated=False, hidden=False, experimental=False):
2204.4.7 by Aaron Bentley
restore register_lazy, remove register_factory, other updates
2442
        registry.Registry.register_lazy(self, key, module_name, member_name, 
2939.2.2 by Ian Clatworthy
allow bzrdir formats to be registered as experimental
2443
            help, BzrDirFormatInfo(native, deprecated, hidden, experimental))
2204.4.1 by Aaron Bentley
Add 'formats' help topic
2444
2445
    def set_default(self, key):
2446
        """Set the 'default' key to be a clone of the supplied key.
2447
        
2448
        This method must be called once and only once.
2449
        """
2204.4.4 by Aaron Bentley
Use BzrDirFormatInfo to distinguish native and deprecated formats
2450
        registry.Registry.register(self, 'default', self.get(key), 
2451
            self.get_help(key), info=self.get_info(key))
2204.4.1 by Aaron Bentley
Add 'formats' help topic
2452
2204.4.11 by Aaron Bentley
deprecate Repository.set_default_format, update upgrade tests
2453
    def set_default_repository(self, key):
2454
        """Set the FormatRegistry default and Repository default.
2455
        
2456
        This is a transitional method while Repository.set_default_format
2457
        is deprecated.
2458
        """
2459
        if 'default' in self:
2460
            self.remove('default')
2461
        self.set_default(key)
2462
        format = self.get('default')()
2463
        assert isinstance(format, BzrDirMetaFormat1)
2464
2204.4.1 by Aaron Bentley
Add 'formats' help topic
2465
    def make_bzrdir(self, key):
2204.4.7 by Aaron Bentley
restore register_lazy, remove register_factory, other updates
2466
        return self.get(key)()
2204.4.1 by Aaron Bentley
Add 'formats' help topic
2467
2468
    def help_topic(self, topic):
2469
        output = textwrap.dedent("""\
2470
            These formats can be used for creating branches, working trees, and
2471
            repositories.
2204.4.2 by Aaron Bentley
Tweak topic appearance
2472
2204.4.1 by Aaron Bentley
Add 'formats' help topic
2473
            """)
2711.2.4 by Martin Pool
Fix unbound variable error in BzrDirFormatRegistry.get_help (test order dependent)
2474
        default_realkey = None
2204.4.1 by Aaron Bentley
Add 'formats' help topic
2475
        default_help = self.get_help('default')
2476
        help_pairs = []
2477
        for key in self.keys():
2478
            if key == 'default':
2479
                continue
2480
            help = self.get_help(key)
2481
            if help == default_help:
2482
                default_realkey = key
2483
            else:
2484
                help_pairs.append((key, help))
2485
2204.4.4 by Aaron Bentley
Use BzrDirFormatInfo to distinguish native and deprecated formats
2486
        def wrapped(key, help, info):
2487
            if info.native:
2488
                help = '(native) ' + help
2666.1.1 by Ian Clatworthy
Bazaar User Reference generated from online help
2489
            return ':%s:\n%s\n\n' % (key, 
2204.4.1 by Aaron Bentley
Add 'formats' help topic
2490
                    textwrap.fill(help, initial_indent='    ', 
2491
                    subsequent_indent='    '))
2711.2.4 by Martin Pool
Fix unbound variable error in BzrDirFormatRegistry.get_help (test order dependent)
2492
        if default_realkey is not None:
2493
            output += wrapped(default_realkey, '(default) %s' % default_help,
2494
                              self.get_info('default'))
2204.4.4 by Aaron Bentley
Use BzrDirFormatInfo to distinguish native and deprecated formats
2495
        deprecated_pairs = []
2939.2.2 by Ian Clatworthy
allow bzrdir formats to be registered as experimental
2496
        experimental_pairs = []
2204.4.1 by Aaron Bentley
Add 'formats' help topic
2497
        for key, help in help_pairs:
2204.4.4 by Aaron Bentley
Use BzrDirFormatInfo to distinguish native and deprecated formats
2498
            info = self.get_info(key)
1551.13.2 by Aaron Bentley
Hide dirstate-with-subtree format
2499
            if info.hidden:
2500
                continue
2501
            elif info.deprecated:
2204.4.4 by Aaron Bentley
Use BzrDirFormatInfo to distinguish native and deprecated formats
2502
                deprecated_pairs.append((key, help))
2939.2.2 by Ian Clatworthy
allow bzrdir formats to be registered as experimental
2503
            elif info.experimental:
2504
                experimental_pairs.append((key, help))
2204.4.4 by Aaron Bentley
Use BzrDirFormatInfo to distinguish native and deprecated formats
2505
            else:
2506
                output += wrapped(key, help, info)
2939.2.2 by Ian Clatworthy
allow bzrdir formats to be registered as experimental
2507
        if len(experimental_pairs) > 0:
2508
            output += "Experimental formats are shown below.\n\n"
2509
            for key, help in experimental_pairs:
2510
                info = self.get_info(key)
2511
                output += wrapped(key, help, info)
2204.4.4 by Aaron Bentley
Use BzrDirFormatInfo to distinguish native and deprecated formats
2512
        if len(deprecated_pairs) > 0:
2666.1.1 by Ian Clatworthy
Bazaar User Reference generated from online help
2513
            output += "Deprecated formats are shown below.\n\n"
2204.4.4 by Aaron Bentley
Use BzrDirFormatInfo to distinguish native and deprecated formats
2514
            for key, help in deprecated_pairs:
2515
                info = self.get_info(key)
2516
                output += wrapped(key, help, info)
2517
2204.4.1 by Aaron Bentley
Add 'formats' help topic
2518
        return output
2519
2520
2521
format_registry = BzrDirFormatRegistry()
2204.4.7 by Aaron Bentley
restore register_lazy, remove register_factory, other updates
2522
format_registry.register('weave', BzrDirFormat6,
2204.4.4 by Aaron Bentley
Use BzrDirFormatInfo to distinguish native and deprecated formats
2523
    'Pre-0.8 format.  Slower than knit and does not'
2241.1.6 by Martin Pool
Move Knit repositories into the submodule bzrlib.repofmt.knitrepo and
2524
    ' support checkouts or shared repositories.',
2525
    deprecated=True)
2526
format_registry.register_metadir('knit',
2241.1.21 by Martin Pool
Change register_metadir to take fully-qualified repository class name.
2527
    'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
2255.12.1 by Robert Collins
Implement upgrade for working trees.
2528
    'Format using knits.  Recommended for interoperation with bzr <= 0.14.',
2529
    branch_format='bzrlib.branch.BzrBranchFormat5',
2255.2.199 by Robert Collins
Fix definition of knit format; typos are not good.
2530
    tree_format='bzrlib.workingtree.WorkingTreeFormat3')
2241.1.21 by Martin Pool
Change register_metadir to take fully-qualified repository class name.
2531
format_registry.register_metadir('metaweave',
2532
    'bzrlib.repofmt.weaverepo.RepositoryFormat7',
2230.3.30 by Aaron Bentley
Fix whitespace issues
2533
    'Transitional format in 0.8.  Slower than knit.',
2255.12.1 by Robert Collins
Implement upgrade for working trees.
2534
    branch_format='bzrlib.branch.BzrBranchFormat5',
2255.2.209 by Robert Collins
Remove circular imports in bzrdir format definitions.
2535
    tree_format='bzrlib.workingtree.WorkingTreeFormat3',
2204.4.4 by Aaron Bentley
Use BzrDirFormatInfo to distinguish native and deprecated formats
2536
    deprecated=True)
2255.12.1 by Robert Collins
Implement upgrade for working trees.
2537
format_registry.register_metadir('dirstate',
2538
    'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
2539
    help='New in 0.15: Fast local operations. Compatible with bzr 0.8 and '
2540
        'above when accessed over the network.',
2541
    branch_format='bzrlib.branch.BzrBranchFormat5',
2255.2.209 by Robert Collins
Remove circular imports in bzrdir format definitions.
2542
    # this uses bzrlib.workingtree.WorkingTreeFormat4 because importing
2543
    # directly from workingtree_4 triggers a circular import.
2544
    tree_format='bzrlib.workingtree.WorkingTreeFormat4',
2255.12.1 by Robert Collins
Implement upgrade for working trees.
2545
    )
1551.13.1 by Aaron Bentley
Introduce dirstate-tags format
2546
format_registry.register_metadir('dirstate-tags',
2547
    'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
2548
    help='New in 0.15: Fast local operations and improved scaling for '
1551.13.2 by Aaron Bentley
Hide dirstate-with-subtree format
2549
        'network operations. Additionally adds support for tags.'
2550
        ' Incompatible with bzr < 0.15.',
1551.13.1 by Aaron Bentley
Introduce dirstate-tags format
2551
    branch_format='bzrlib.branch.BzrBranchFormat6',
2552
    tree_format='bzrlib.workingtree.WorkingTreeFormat4',
2553
    )
2996.2.1 by Aaron Bentley
Add KnitRepositoryFormat4
2554
format_registry.register_metadir('rich-root',
2555
    'bzrlib.repofmt.knitrepo.RepositoryFormatKnit4',
2556
    help='New in 1.0.  Better handling of tree roots.  Incompatible with'
2557
        ' bzr < 1.0',
2558
    branch_format='bzrlib.branch.BzrBranchFormat6',
2559
    tree_format='bzrlib.workingtree.WorkingTreeFormat4',
2560
    hidden=False,
2561
    )
2255.12.1 by Robert Collins
Implement upgrade for working trees.
2562
format_registry.register_metadir('dirstate-with-subtree',
2563
    'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
2564
    help='New in 0.15: Fast local operations and improved scaling for '
2565
        'network operations. Additionally adds support for versioning nested '
2566
        'bzr branches. Incompatible with bzr < 0.15.',
2567
    branch_format='bzrlib.branch.BzrBranchFormat6',
2255.2.209 by Robert Collins
Remove circular imports in bzrdir format definitions.
2568
    tree_format='bzrlib.workingtree.WorkingTreeFormat4',
1551.13.2 by Aaron Bentley
Hide dirstate-with-subtree format
2569
    hidden=True,
2255.12.1 by Robert Collins
Implement upgrade for working trees.
2570
    )
3010.3.2 by Martin Pool
Rename pack0.92 to pack-0.92
2571
format_registry.register_metadir('pack-0.92',
2592.3.224 by Martin Pool
Rename GraphKnitRepository etc to KnitPackRepository
2572
    'bzrlib.repofmt.pack_repo.RepositoryFormatKnitPack1',
2939.2.1 by Ian Clatworthy
use 'knitpack' naming instead of 'experimental' for pack formats
2573
    help='New in 0.92: Pack-based format with data compatible with '
2939.2.6 by Ian Clatworthy
more review feedback from lifeless and poolie
2574
        'dirstate-tags format repositories. Interoperates with '
2575
        'bzr repositories before 0.92 but cannot be read by bzr < 0.92. '
3010.3.1 by Martin Pool
Rename knitpack-experimental format to pack0.92 (not experimental)
2576
        'Previously called knitpack-experimental.  '
2577
        'For more information, see '
2578
        'http://doc.bazaar-vcs.org/latest/developers/packrepo.html.',
2592.3.22 by Robert Collins
Add new experimental repository formats.
2579
    branch_format='bzrlib.branch.BzrBranchFormat6',
2580
    tree_format='bzrlib.workingtree.WorkingTreeFormat4',
2939.2.2 by Ian Clatworthy
allow bzrdir formats to be registered as experimental
2581
    experimental=True,
2592.3.22 by Robert Collins
Add new experimental repository formats.
2582
    )
3010.3.2 by Martin Pool
Rename pack0.92 to pack-0.92
2583
format_registry.register_metadir('pack-0.92-subtree',
2592.3.224 by Martin Pool
Rename GraphKnitRepository etc to KnitPackRepository
2584
    'bzrlib.repofmt.pack_repo.RepositoryFormatKnitPack3',
2939.2.1 by Ian Clatworthy
use 'knitpack' naming instead of 'experimental' for pack formats
2585
    help='New in 0.92: Pack-based format with data compatible with '
2939.2.6 by Ian Clatworthy
more review feedback from lifeless and poolie
2586
        'dirstate-with-subtree format repositories. Interoperates with '
2587
        'bzr repositories before 0.92 but cannot be read by bzr < 0.92. '
3010.3.1 by Martin Pool
Rename knitpack-experimental format to pack0.92 (not experimental)
2588
        'Previously called knitpack-experimental.  '
2589
        'For more information, see '
2590
        'http://doc.bazaar-vcs.org/latest/developers/packrepo.html.',
2592.3.22 by Robert Collins
Add new experimental repository formats.
2591
    branch_format='bzrlib.branch.BzrBranchFormat6',
2592
    tree_format='bzrlib.workingtree.WorkingTreeFormat4',
2593
    hidden=True,
2939.2.5 by Ian Clatworthy
review feedback from lifeless
2594
    experimental=True,
2592.3.22 by Robert Collins
Add new experimental repository formats.
2595
    )
2996.2.11 by Aaron Bentley
Implement rich-root-pack format ( #164639)
2596
format_registry.register_metadir('rich-root-pack',
2597
    'bzrlib.repofmt.pack_repo.RepositoryFormatKnitPack4',
2598
    help='New in 1.0: Pack-based format with data compatible with '
2599
        'rich-root format repositories. Interoperates with '
2600
        'bzr repositories before 0.92 but cannot be read by bzr < 1.0. '
2601
        'NOTE: This format is experimental. Before using it, please read '
3040.1.1 by Matt Nordhoff
(trivial) rich-root-pack help still linked to knitpack.html when it's been renamed.
2602
        'http://doc.bazaar-vcs.org/latest/developers/packrepo.html.',
2996.2.11 by Aaron Bentley
Implement rich-root-pack format ( #164639)
2603
    branch_format='bzrlib.branch.BzrBranchFormat6',
2604
    tree_format='bzrlib.workingtree.WorkingTreeFormat4',
2605
    hidden=False,
2606
    experimental=True,
2607
    )
3044.1.3 by Martin Pool
Set the default format to pack-0.92
2608
format_registry.set_default('pack-0.92')