/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1553.5.48 by Martin Pool
Fix some LockableFiles deprecation warnings
1
# Copyright (C) 2005, 2006 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.
21
"""
22
1773.4.3 by Martin Pool
[merge] bzr.dev
23
# TODO: remove unittest dependency; put that stuff inside the test suite
24
1910.7.17 by Andrew Bennetts
Various cosmetic changes.
25
# TODO: The Format probe_transport seems a bit redundant with just trying to
26
# open the bzrdir. -- mbp
27
#
28
# TODO: Can we move specific formats into separate modules to make this file
29
# smaller?
30
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
31
from cStringIO import StringIO
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
32
import os
2204.4.1 by Aaron Bentley
Add 'formats' help topic
33
import textwrap
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
34
35
from bzrlib.lazy_import import lazy_import
36
lazy_import(globals(), """
37
from copy import deepcopy
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
38
from stat import S_ISDIR
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
39
import unittest
1534.4.39 by Robert Collins
Basic BzrDir support.
40
41
import bzrlib
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
42
from bzrlib import (
43
    errors,
44
    lockable_files,
45
    lockdir,
2204.4.1 by Aaron Bentley
Add 'formats' help topic
46
    registry,
1996.3.12 by John Arbash Meinel
Change how 'revision' is imported to avoid problems later
47
    revision as _mod_revision,
2094.3.5 by John Arbash Meinel
Fix imports to ensure modules are loaded before they are used
48
    repository as _mod_repository,
2204.4.12 by Aaron Bentley
Deprecate bzrdir.BzrDirFormat.set_default_format
49
    symbol_versioning,
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
50
    urlutils,
1996.3.6 by John Arbash Meinel
Find a few places that weren't importing their dependencies.
51
    xml4,
52
    xml5,
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
53
    )
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
54
from bzrlib.osutils import (
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
55
    safe_unicode,
56
    sha_strings,
57
    sha_string,
58
    )
1563.2.28 by Robert Collins
Add total_size to the revision_store api.
59
from bzrlib.store.revision.text import TextRevisionStore
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
60
from bzrlib.store.text import TextStore
1563.2.25 by Robert Collins
Merge in upstream.
61
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
62
from bzrlib.transactions import WriteTransaction
1685.1.45 by John Arbash Meinel
Moved url functions into bzrlib.urlutils
63
from bzrlib.transport import get_transport
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
64
from bzrlib.weave import Weave
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
65
""")
66
67
from bzrlib.trace import mutter
68
from bzrlib.transport.local import LocalTransport
1534.4.39 by Robert Collins
Basic BzrDir support.
69
70
71
class BzrDir(object):
72
    """A .bzr control diretory.
73
    
74
    BzrDir instances let you create or open any of the things that can be
75
    found within .bzr - checkouts, branches and repositories.
76
    
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
77
    transport
78
        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.
79
    root_transport
80
        a transport connected to the directory this bzr was opened from.
1534.4.39 by Robert Collins
Basic BzrDir support.
81
    """
82
1687.1.12 by Robert Collins
Hook in the full break-lock ui.
83
    def break_lock(self):
84
        """Invoke break_lock on the first object in the bzrdir.
85
86
        If there is a tree, the tree is opened and break_lock() called.
87
        Otherwise, branch is tried, and finally repository.
88
        """
89
        try:
90
            thing_to_unlock = self.open_workingtree()
91
        except (errors.NotLocalUrl, errors.NoWorkingTree):
92
            try:
93
                thing_to_unlock = self.open_branch()
94
            except errors.NotBranchError:
95
                try:
96
                    thing_to_unlock = self.open_repository()
97
                except errors.NoRepositoryPresent:
98
                    return
99
        thing_to_unlock.break_lock()
100
1534.5.16 by Robert Collins
Review feedback.
101
    def can_convert_format(self):
102
        """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.
103
        return True
104
1910.2.12 by Aaron Bentley
Implement knit repo format 2
105
    def check_conversion_target(self, target_format):
106
        target_repo_format = target_format.repository_format
107
        source_repo_format = self._format.repository_format
108
        source_repo_format.check_conversion_target(target_repo_format)
109
1596.2.1 by Robert Collins
Fix BzrDir.open_containing of unsupported branches.
110
    @staticmethod
111
    def _check_supported(format, allow_unsupported):
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
112
        """Check whether format is a supported format.
113
114
        If allow_unsupported is True, this is a no-op.
115
        """
116
        if not allow_unsupported and not format.is_supported():
1596.2.1 by Robert Collins
Fix BzrDir.open_containing of unsupported branches.
117
            # see open_downlevel to open legacy branches.
1740.5.6 by Martin Pool
Clean up many exception classes.
118
            raise errors.UnsupportedFormatError(format=format)
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
119
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.
120
    def clone(self, url, revision_id=None, basis=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.
121
        """Clone this bzrdir and its contents to url verbatim.
122
123
        If urls last component does not exist, it will be created.
124
125
        if revision_id is not None, then the clone operation may tune
126
            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.
127
        :param force_new_repo: Do not use a shared repository for the target 
128
                               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.
129
        """
130
        self._make_tail(url)
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.
131
        basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
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.
132
        result = self._format.initialize(url)
133
        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.
134
            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.
135
        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.
136
            local_repo = None
137
        if local_repo:
138
            # may need to copy content in
139
            if force_new_repo:
1707.1.1 by Robert Collins
Bugfixes to bzrdir.sprout and clone. Sprout was failing to reset the
140
                result_repo = local_repo.clone(
141
                    result,
142
                    revision_id=revision_id,
143
                    basis=basis_repo)
144
                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.
145
            else:
146
                try:
147
                    result_repo = result.find_repository()
148
                    # fetch content this dir needs.
149
                    if basis_repo:
150
                        # XXX FIXME RBC 20060214 need tests for this when the basis
151
                        # is incomplete
152
                        result_repo.fetch(basis_repo, revision_id=revision_id)
153
                    result_repo.fetch(local_repo, revision_id=revision_id)
154
                except errors.NoRepositoryPresent:
155
                    # needed to make one anyway.
1707.1.1 by Robert Collins
Bugfixes to bzrdir.sprout and clone. Sprout was failing to reset the
156
                    result_repo = local_repo.clone(
157
                        result,
158
                        revision_id=revision_id,
159
                        basis=basis_repo)
160
                    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.
161
        # 1 if there is a branch present
162
        #   make sure its content is available in the target repository
163
        #   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.
164
        try:
165
            self.open_branch().clone(result, revision_id=revision_id)
166
        except errors.NotBranchError:
167
            pass
168
        try:
169
            self.open_workingtree().clone(result, basis=basis_tree)
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
170
        except (errors.NoWorkingTree, errors.NotLocalUrl):
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.
171
            pass
172
        return result
173
174
    def _get_basis_components(self, basis):
175
        """Retrieve the basis components that are available at basis."""
176
        if basis is None:
177
            return None, None, None
178
        try:
179
            basis_tree = basis.open_workingtree()
180
            basis_branch = basis_tree.branch
181
            basis_repo = basis_branch.repository
182
        except (errors.NoWorkingTree, errors.NotLocalUrl):
183
            basis_tree = None
184
            try:
185
                basis_branch = basis.open_branch()
186
                basis_repo = basis_branch.repository
187
            except errors.NotBranchError:
188
                basis_branch = None
189
                try:
190
                    basis_repo = basis.open_repository()
191
                except errors.NoRepositoryPresent:
192
                    basis_repo = None
193
        return basis_repo, basis_branch, basis_tree
194
1685.1.61 by Martin Pool
[broken] Change BzrDir._make_tail to use urlutils.split
195
    # TODO: This should be given a Transport, and should chdir up; otherwise
196
    # 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.
197
    def _make_tail(self, url):
1685.1.61 by Martin Pool
[broken] Change BzrDir._make_tail to use urlutils.split
198
        head, tail = urlutils.split(url)
199
        if tail and tail != '.':
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
200
            t = get_transport(head)
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.
201
            try:
1685.1.61 by Martin Pool
[broken] Change BzrDir._make_tail to use urlutils.split
202
                t.mkdir(tail)
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.
203
            except errors.FileExists:
204
                pass
205
1685.1.63 by Martin Pool
Small Transport fixups
206
    # TODO: Should take a Transport
1553.5.71 by Martin Pool
Change branch format 5 to use LockDirs, not transport locks
207
    @classmethod
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
208
    def create(cls, base, format=None):
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
209
        """Create a new BzrDir at the url 'base'.
1534.4.39 by Robert Collins
Basic BzrDir support.
210
        
211
        This will call the current default formats initialize with base
212
        as the only parameter.
213
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
214
        :param format: If supplied, the format of branch to create.  If not
215
            supplied, the default is used.
1534.4.39 by Robert Collins
Basic BzrDir support.
216
        """
1553.5.71 by Martin Pool
Change branch format 5 to use LockDirs, not transport locks
217
        if cls is not BzrDir:
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
218
            raise AssertionError("BzrDir.create always creates the default"
219
                " format, not one of %r" % cls)
1685.1.62 by Martin Pool
[broken] Change BzrDir.create to use urlutils.split
220
        head, tail = urlutils.split(base)
221
        if tail and tail != '.':
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
222
            t = get_transport(head)
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
223
            try:
1685.1.62 by Martin Pool
[broken] Change BzrDir.create to use urlutils.split
224
                t.mkdir(tail)
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
225
            except errors.FileExists:
226
                pass
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
227
        if format is None:
228
            format = BzrDirFormat.get_default_format()
229
        return format.initialize(safe_unicode(base))
1534.4.39 by Robert Collins
Basic BzrDir support.
230
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
231
    def create_branch(self):
232
        """Create a branch in this BzrDir.
233
234
        The bzrdirs format will control what branch format is created.
235
        For more control see BranchFormatXX.create(a_bzrdir).
236
        """
237
        raise NotImplementedError(self.create_branch)
238
239
    @staticmethod
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
240
    def create_branch_and_repo(base, force_new_repo=False, format=None):
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
241
        """Create a new BzrDir, Branch and Repository at the url 'base'.
242
243
        This will use the current default BzrDirFormat, and use whatever 
244
        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.
245
        create_repository. If a shared repository is available that is used
246
        preferentially.
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
247
248
        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.
249
250
        :param base: The URL to create the branch at.
251
        :param force_new_repo: If True a new repository is always created.
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
252
        """
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
253
        bzrdir = BzrDir.create(base, format)
1534.6.11 by Robert Collins
Review feedback.
254
        bzrdir._find_or_create_repository(force_new_repo)
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
255
        return bzrdir.create_branch()
1534.6.11 by Robert Collins
Review feedback.
256
257
    def _find_or_create_repository(self, force_new_repo):
258
        """Create a new repository if needed, returning the repository."""
259
        if force_new_repo:
260
            return self.create_repository()
261
        try:
262
            return self.find_repository()
263
        except errors.NoRepositoryPresent:
264
            return self.create_repository()
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
265
        
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
266
    @staticmethod
1558.5.3 by Aaron Bentley
Init command can produce sharing branches
267
    def create_branch_convenience(base, force_new_repo=False,
268
                                  force_new_tree=None, format=None):
1534.6.10 by Robert Collins
Finish use of repositories support.
269
        """Create a new BzrDir, Branch and Repository at the url 'base'.
270
271
        This is a convenience function - it will use an existing repository
272
        if possible, can be told explicitly whether to create a working tree or
1534.6.12 by Robert Collins
Typo found by John Meinel.
273
        not.
1534.6.10 by Robert Collins
Finish use of repositories support.
274
275
        This will use the current default BzrDirFormat, and use whatever 
276
        repository format that that uses via bzrdir.create_branch and
277
        create_repository. If a shared repository is available that is used
278
        preferentially. Whatever repository is used, its tree creation policy
279
        is followed.
280
281
        The created Branch object is returned.
282
        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.
283
        no error is raised unless force_new_tree is True, in which case no 
284
        data is created on disk and NotLocalUrl is raised.
1534.6.10 by Robert Collins
Finish use of repositories support.
285
286
        :param base: The URL to create the branch at.
287
        :param force_new_repo: If True a new repository is always created.
288
        :param force_new_tree: If True or False force creation of a tree or 
289
                               prevent such creation respectively.
1558.5.3 by Aaron Bentley
Init command can produce sharing branches
290
        :param format: Override for the for the bzrdir format to create
1534.6.10 by Robert Collins
Finish use of repositories support.
291
        """
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.
292
        if force_new_tree:
293
            # check for non local urls
294
            t = get_transport(safe_unicode(base))
295
            if not isinstance(t, LocalTransport):
296
                raise errors.NotLocalUrl(base)
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
297
        bzrdir = BzrDir.create(base, format)
1534.6.11 by Robert Collins
Review feedback.
298
        repo = bzrdir._find_or_create_repository(force_new_repo)
1534.6.10 by Robert Collins
Finish use of repositories support.
299
        result = bzrdir.create_branch()
300
        if force_new_tree or (repo.make_working_trees() and 
301
                              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.
302
            try:
303
                bzrdir.create_workingtree()
304
            except errors.NotLocalUrl:
305
                pass
1534.6.10 by Robert Collins
Finish use of repositories support.
306
        return result
1551.8.4 by Aaron Bentley
Tweak import style
307
        
1551.8.2 by Aaron Bentley
Add create_checkout_convenience
308
    @staticmethod
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
309
    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.
310
        """Create a new BzrDir and Repository at the url 'base'.
311
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
312
        If no format is supplied, this will default to the current default
313
        BzrDirFormat by default, and use whatever repository format that that
314
        uses for bzrdirformat.create_repository.
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
315
1910.7.17 by Andrew Bennetts
Various cosmetic changes.
316
        :param shared: Create a shared repository rather than a standalone
1534.6.1 by Robert Collins
allow API creation of shared repositories
317
                       repository.
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
318
        The Repository object is returned.
319
320
        This must be overridden as an instance method in child classes, where
321
        it should take no parameters and construct whatever repository format
322
        that child class desires.
323
        """
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
324
        bzrdir = BzrDir.create(base, format)
1841.2.1 by Jelmer Vernooij
Fix handling of `shared' parameter in BzrDir.create_repository().
325
        return bzrdir.create_repository(shared)
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
326
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
327
    @staticmethod
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
328
    def create_standalone_workingtree(base, format=None):
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
329
        """Create a new BzrDir, WorkingTree, Branch and Repository at 'base'.
330
331
        'base' must be a local path or a file:// url.
332
333
        This will use the current default BzrDirFormat, and use whatever 
334
        repository format that that uses for bzrdirformat.create_workingtree,
335
        create_branch and create_repository.
336
1910.7.17 by Andrew Bennetts
Various cosmetic changes.
337
        :return: The WorkingTree object.
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
338
        """
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
339
        t = get_transport(safe_unicode(base))
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
340
        if not isinstance(t, LocalTransport):
341
            raise errors.NotLocalUrl(base)
1534.6.10 by Robert Collins
Finish use of repositories support.
342
        bzrdir = BzrDir.create_branch_and_repo(safe_unicode(base),
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
343
                                               force_new_repo=True,
344
                                               format=format).bzrdir
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
345
        return bzrdir.create_workingtree()
346
1508.1.21 by Robert Collins
Implement -r limit for checkout command.
347
    def create_workingtree(self, revision_id=None):
348
        """Create a working tree at this BzrDir.
349
        
350
        revision_id: create it as of this revision id.
351
        """
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
352
        raise NotImplementedError(self.create_workingtree)
353
1551.8.37 by Aaron Bentley
Cleaner implementation of destroy_working_tree
354
    def destroy_workingtree(self):
1551.8.36 by Aaron Bentley
Introduce BzrDir.destroy_workingtree
355
        """Destroy the working tree at this BzrDir.
356
357
        Formats that do not support this may raise UnsupportedOperation.
358
        """
359
        raise NotImplementedError(self.destroy_workingtree)
360
1551.8.37 by Aaron Bentley
Cleaner implementation of destroy_working_tree
361
    def destroy_workingtree_metadata(self):
362
        """Destroy the control files for the working tree at this BzrDir.
363
364
        The contents of working tree files are not affected.
365
        Formats that do not support this may raise UnsupportedOperation.
366
        """
367
        raise NotImplementedError(self.destroy_workingtree_metadata)
368
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.
369
    def find_repository(self):
370
        """Find the repository that should be used for a_bzrdir.
371
372
        This does not require a branch as we use it to find the repo for
373
        new branches as well as to hook existing branches up to their
374
        repository.
375
        """
376
        try:
377
            return self.open_repository()
378
        except errors.NoRepositoryPresent:
379
            pass
380
        next_transport = self.root_transport.clone('..')
381
        while True:
1725.2.5 by Robert Collins
Bugfix create_branch_convenience at the root of a file system to not loop
382
            # 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.
383
            try:
1534.6.11 by Robert Collins
Review feedback.
384
                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.
385
                    next_transport)[0]
386
            except errors.NotBranchError:
1725.2.5 by Robert Collins
Bugfix create_branch_convenience at the root of a file system to not loop
387
                # 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.
388
                raise errors.NoRepositoryPresent(self)
1725.2.5 by Robert Collins
Bugfix create_branch_convenience at the root of a file system to not loop
389
            # 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.
390
            try:
391
                repository = found_bzrdir.open_repository()
392
            except errors.NoRepositoryPresent:
393
                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
394
                if (found_bzrdir.root_transport.base == next_transport.base):
395
                    # top of the file system
396
                    break
397
                else:
398
                    continue
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.
399
            if ((found_bzrdir.root_transport.base == 
400
                 self.root_transport.base) or repository.is_shared()):
401
                return repository
402
            else:
403
                raise errors.NoRepositoryPresent(self)
404
        raise errors.NoRepositoryPresent(self)
405
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
406
    def get_branch_transport(self, branch_format):
407
        """Get the transport for use by branch format in this BzrDir.
408
409
        Note that bzr dirs that do not support format strings will raise
410
        IncompatibleFormat if the branch format they are given has
1759.2.1 by Jelmer Vernooij
Fix some types (found using aspell).
411
        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.
412
413
        If branch_format is None, the transport is returned with no 
414
        checking. if it is not None, then the returned transport is
415
        guaranteed to point to an existing directory ready for use.
416
        """
417
        raise NotImplementedError(self.get_branch_transport)
418
        
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
419
    def get_repository_transport(self, repository_format):
420
        """Get the transport for use by repository format in this BzrDir.
421
422
        Note that bzr dirs that do not support format strings will raise
423
        IncompatibleFormat if the repository format they are given has
1759.2.1 by Jelmer Vernooij
Fix some types (found using aspell).
424
        a format string, and vice versa.
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
425
426
        If repository_format is None, the transport is returned with no 
427
        checking. if it is not None, then the returned transport is
428
        guaranteed to point to an existing directory ready for use.
429
        """
430
        raise NotImplementedError(self.get_repository_transport)
431
        
1534.4.53 by Robert Collins
Review feedback from John Meinel.
432
    def get_workingtree_transport(self, tree_format):
1534.4.45 by Robert Collins
Start WorkingTree -> .bzr/checkout transition
433
        """Get the transport for use by workingtree format in this BzrDir.
434
435
        Note that bzr dirs that do not support format strings will raise
436
        IncompatibleFormat if the workingtree format they are given has
1759.2.1 by Jelmer Vernooij
Fix some types (found using aspell).
437
        a format string, and vice versa.
1534.4.45 by Robert Collins
Start WorkingTree -> .bzr/checkout transition
438
439
        If workingtree_format is None, the transport is returned with no 
440
        checking. if it is not None, then the returned transport is
441
        guaranteed to point to an existing directory ready for use.
442
        """
443
        raise NotImplementedError(self.get_workingtree_transport)
444
        
1534.4.39 by Robert Collins
Basic BzrDir support.
445
    def __init__(self, _transport, _format):
446
        """Initialize a Bzr control dir object.
447
        
448
        Only really common logic should reside here, concrete classes should be
449
        made with varying behaviours.
450
1534.4.53 by Robert Collins
Review feedback from John Meinel.
451
        :param _format: the format that is creating this BzrDir instance.
452
        :param _transport: the transport this dir is based at.
1534.4.39 by Robert Collins
Basic BzrDir support.
453
        """
454
        self._format = _format
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
455
        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.
456
        self.root_transport = _transport
1534.4.39 by Robert Collins
Basic BzrDir support.
457
1713.1.9 by Robert Collins
Paired performance tuning of bzr add. (Robert Collins, Martin Pool).
458
    def is_control_filename(self, filename):
459
        """True if filename is the name of a path which is reserved for bzrdir's.
460
        
461
        :param filename: A filename within the root transport of this bzrdir.
462
463
        This is true IF and ONLY IF the filename is part of the namespace reserved
464
        for bzr control dirs. Currently this is the '.bzr' directory in the root
465
        of the root_transport. it is expected that plugins will need to extend
466
        this in the future - for instance to make bzr talk with svn working
467
        trees.
468
        """
469
        # this might be better on the BzrDirFormat class because it refers to 
470
        # all the possible bzrdir disk formats. 
471
        # This method is tested via the workingtree is_control_filename tests- 
1759.2.1 by Jelmer Vernooij
Fix some types (found using aspell).
472
        # it was extracted from WorkingTree.is_control_filename. If the methods
1713.1.9 by Robert Collins
Paired performance tuning of bzr add. (Robert Collins, Martin Pool).
473
        # contract is extended beyond the current trivial  implementation please
474
        # add new tests for it to the appropriate place.
475
        return filename == '.bzr' or filename.startswith('.bzr/')
476
1534.5.16 by Robert Collins
Review feedback.
477
    def needs_format_conversion(self, format=None):
478
        """Return true if this bzrdir needs convert_format run on it.
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
479
        
480
        For instance, if the repository format is out of date but the 
481
        branch and working tree are not, this should return True.
1534.5.13 by Robert Collins
Correct buggy test.
482
483
        :param format: Optional parameter indicating a specific desired
1534.5.16 by Robert Collins
Review feedback.
484
                       format we plan to arrive at.
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
485
        """
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.
486
        raise NotImplementedError(self.needs_format_conversion)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
487
1534.4.39 by Robert Collins
Basic BzrDir support.
488
    @staticmethod
489
    def open_unsupported(base):
490
        """Open a branch which is not supported."""
491
        return BzrDir.open(base, _unsupported=True)
492
        
493
    @staticmethod
494
    def open(base, _unsupported=False):
1534.4.53 by Robert Collins
Review feedback from John Meinel.
495
        """Open an existing bzrdir, rooted at 'base' (url)
1534.4.39 by Robert Collins
Basic BzrDir support.
496
        
497
        _unsupported is a private parameter to the BzrDir class.
498
        """
499
        t = get_transport(base)
1910.11.1 by Andrew Bennetts
Add BzrDir.open_from_transport, refactored from duplicate code, no explicit tests.
500
        return BzrDir.open_from_transport(t, _unsupported=_unsupported)
501
502
    @staticmethod
503
    def open_from_transport(transport, _unsupported=False):
504
        """Open a bzrdir within a particular directory.
505
506
        :param transport: Transport containing the bzrdir.
507
        :param _unsupported: private.
508
        """
509
        format = BzrDirFormat.find_format(transport)
1596.2.1 by Robert Collins
Fix BzrDir.open_containing of unsupported branches.
510
        BzrDir._check_supported(format, _unsupported)
1910.11.1 by Andrew Bennetts
Add BzrDir.open_from_transport, refactored from duplicate code, no explicit tests.
511
        return format.open(transport, _found=True)
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
512
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
513
    def open_branch(self, unsupported=False):
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
514
        """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.
515
516
        If unsupported is True, then no longer supported branch formats can
517
        still be opened.
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
518
        
519
        TODO: static convenience version of this?
520
        """
521
        raise NotImplementedError(self.open_branch)
1534.4.39 by Robert Collins
Basic BzrDir support.
522
523
    @staticmethod
524
    def open_containing(url):
525
        """Open an existing branch which contains url.
526
        
1534.6.3 by Robert Collins
find_repository sufficiently robust.
527
        :param url: url to search from.
1534.6.11 by Robert Collins
Review feedback.
528
        See open_containing_from_transport for more detail.
1534.6.3 by Robert Collins
find_repository sufficiently robust.
529
        """
1534.6.11 by Robert Collins
Review feedback.
530
        return BzrDir.open_containing_from_transport(get_transport(url))
1534.6.3 by Robert Collins
find_repository sufficiently robust.
531
    
532
    @staticmethod
1534.6.11 by Robert Collins
Review feedback.
533
    def open_containing_from_transport(a_transport):
1534.6.3 by Robert Collins
find_repository sufficiently robust.
534
        """Open an existing branch which contains a_transport.base
535
536
        This probes for a branch at a_transport, and searches upwards from there.
1534.4.39 by Robert Collins
Basic BzrDir support.
537
538
        Basically we keep looking up until we find the control directory or
539
        run into the root.  If there isn't one, raises NotBranchError.
540
        If there is one and it is either an unrecognised format or an unsupported 
541
        format, UnknownFormatError or UnsupportedFormatError are raised.
542
        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
543
1685.1.28 by John Arbash Meinel
Changing open_containing to always return a unicode path.
544
        :return: The BzrDir that contains the path, and a Unicode path 
545
                for the rest of the URL.
1534.4.39 by Robert Collins
Basic BzrDir support.
546
        """
547
        # this gets the normalised url back. I.e. '.' -> the full path.
1534.6.3 by Robert Collins
find_repository sufficiently robust.
548
        url = a_transport.base
1534.4.39 by Robert Collins
Basic BzrDir support.
549
        while True:
550
            try:
1910.11.1 by Andrew Bennetts
Add BzrDir.open_from_transport, refactored from duplicate code, no explicit tests.
551
                result = BzrDir.open_from_transport(a_transport)
552
                return result, urlutils.unescape(a_transport.relpath(url))
1534.4.39 by Robert Collins
Basic BzrDir support.
553
            except errors.NotBranchError, e:
1685.1.60 by Martin Pool
[broken] NotBranchError should unescape the url if possible
554
                pass
1534.6.3 by Robert Collins
find_repository sufficiently robust.
555
            new_t = a_transport.clone('..')
556
            if new_t.base == a_transport.base:
1534.4.39 by Robert Collins
Basic BzrDir support.
557
                # reached the root, whatever that may be
558
                raise errors.NotBranchError(path=url)
1534.6.3 by Robert Collins
find_repository sufficiently robust.
559
            a_transport = new_t
1534.4.39 by Robert Collins
Basic BzrDir support.
560
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
561
    def open_repository(self, _unsupported=False):
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
562
        """Open the repository object at this BzrDir if one is present.
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
563
564
        This will not follow the Branch object pointer - its strictly a direct
565
        open facility. Most client code should use open_branch().repository to
566
        get at a repository.
567
568
        _unsupported is 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.
569
        TODO: static convenience version of this?
570
        """
571
        raise NotImplementedError(self.open_repository)
572
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
573
    def open_workingtree(self, _unsupported=False):
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
574
        """Open the workingtree object at this BzrDir if one is present.
575
        
576
        TODO: static convenience version of this?
577
        """
578
        raise NotImplementedError(self.open_workingtree)
579
1662.1.19 by Martin Pool
Better error message when initting existing tree
580
    def has_branch(self):
581
        """Tell if this bzrdir contains a branch.
582
        
583
        Note: if you're going to open the branch, you should just go ahead
584
        and try, and not ask permission first.  (This method just opens the 
585
        branch and discards it, and that's somewhat expensive.) 
586
        """
587
        try:
588
            self.open_branch()
589
            return True
590
        except errors.NotBranchError:
591
            return False
592
593
    def has_workingtree(self):
594
        """Tell if this bzrdir contains a working tree.
595
596
        This will still raise an exception if the bzrdir has a workingtree that
597
        is remote & inaccessible.
598
        
599
        Note: if you're going to open the working tree, you should just go ahead
600
        and try, and not ask permission first.  (This method just opens the 
601
        workingtree and discards it, and that's somewhat expensive.) 
602
        """
603
        try:
604
            self.open_workingtree()
605
            return True
606
        except errors.NoWorkingTree:
607
            return False
608
1910.2.41 by Aaron Bentley
Clean up clone format creation
609
    def cloning_metadir(self, basis=None):
610
        """Produce a metadir suitable for cloning with"""
611
        def related_repository(bzrdir):
612
            try:
613
                branch = bzrdir.open_branch()
614
                return branch.repository
615
            except errors.NotBranchError:
616
                source_branch = None
617
                return bzrdir.open_repository()
618
        result_format = self._format.__class__()
619
        try:
620
            try:
621
                source_repository = related_repository(self)
622
            except errors.NoRepositoryPresent:
623
                if basis is None:
624
                    raise
625
                source_repository = related_repository(self)
626
            result_format.repository_format = source_repository._format
627
        except errors.NoRepositoryPresent:
628
            pass
629
        return result_format
630
1534.6.9 by Robert Collins
sprouting into shared repositories
631
    def sprout(self, url, revision_id=None, basis=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.
632
        """Create a copy of this bzrdir prepared for use as a new line of
633
        development.
634
635
        If urls last component does not exist, it will be created.
636
637
        Attributes related to the identity of the source branch like
638
        branch nickname will be cleaned, a working tree is created
639
        whether one existed before or not; and a local branch is always
640
        created.
641
642
        if revision_id is not None, then the clone operation may tune
643
            itself to download less data.
644
        """
645
        self._make_tail(url)
1910.2.41 by Aaron Bentley
Clean up clone format creation
646
        cloning_format = self.cloning_metadir(basis)
647
        result = cloning_format.initialize(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.
648
        basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
649
        try:
650
            source_branch = self.open_branch()
651
            source_repository = source_branch.repository
652
        except errors.NotBranchError:
653
            source_branch = None
654
            try:
655
                source_repository = self.open_repository()
656
            except errors.NoRepositoryPresent:
1534.6.9 by Robert Collins
sprouting into shared repositories
657
                # copy the entire basis one if there is one
658
                # but there is no 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.
659
                source_repository = basis_repo
1534.6.9 by Robert Collins
sprouting into shared repositories
660
        if force_new_repo:
661
            result_repo = None
662
        else:
663
            try:
664
                result_repo = result.find_repository()
665
            except errors.NoRepositoryPresent:
666
                result_repo = None
667
        if source_repository is None and result_repo is not None:
668
            pass
669
        elif source_repository is None and result_repo is None:
670
            # no repo available, make a new one
671
            result.create_repository()
672
        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
673
            # have source, and want to make a new target repo
1759.2.2 by Jelmer Vernooij
Revert some of my spelling fixes and fix some typos after review by Aaron.
674
            # we don't clone the repo because that preserves attributes
1707.1.1 by Robert Collins
Bugfixes to bzrdir.sprout and clone. Sprout was failing to reset the
675
            # like is_shared(), and we have not yet implemented a 
676
            # repository sprout().
677
            result_repo = result.create_repository()
678
        if result_repo is not None:
1534.6.9 by Robert Collins
sprouting into shared repositories
679
            # fetch needed content into target.
680
            if basis_repo:
681
                # XXX FIXME RBC 20060214 need tests for this when the basis
682
                # is incomplete
683
                result_repo.fetch(basis_repo, revision_id=revision_id)
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.
684
            if source_repository is not None:
685
                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.
686
        if source_branch is not None:
687
            source_branch.sprout(result, revision_id=revision_id)
688
        else:
689
            result.create_branch()
1685.1.20 by John Arbash Meinel
More changes to get 'bzr branch' and 'bzr pull' to work
690
        # TODO: jam 20060426 we probably need a test in here in the
691
        #       case that the newly sprouted branch is a remote one
1558.5.8 by Aaron Bentley
Bugfix when result_repo is None
692
        if result_repo is None or result_repo.make_working_trees():
1731.1.33 by Aaron Bentley
Revert no-special-root changes
693
            wt = result.create_workingtree()
694
            if wt.inventory.root is None:
695
                try:
696
                    wt.set_root_id(self.open_workingtree.get_root_id())
697
                except errors.NoWorkingTree:
698
                    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.
699
        return result
700
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
701
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
702
class BzrDirPreSplitOut(BzrDir):
703
    """A common class for the all-in-one formats."""
704
1534.5.3 by Robert Collins
Make format 4/5/6 branches share a single LockableFiles instance across wt/branch/repository.
705
    def __init__(self, _transport, _format):
706
        """See BzrDir.__init__."""
707
        super(BzrDirPreSplitOut, self).__init__(_transport, _format)
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
708
        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.
709
        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
710
        self._control_files = lockable_files.LockableFiles(
711
                                            self.get_branch_transport(None),
1553.5.69 by Martin Pool
BzrDirFormat subclasses can now control what kind of overall lock is used.
712
                                            self._format._lock_file_name,
713
                                            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.
714
1687.1.12 by Robert Collins
Hook in the full break-lock ui.
715
    def break_lock(self):
716
        """Pre-splitout bzrdirs do not suffer from stale locks."""
717
        raise NotImplementedError(self.break_lock)
718
1534.6.8 by Robert Collins
Test the use of clone on empty bzrdir with force_new_repo.
719
    def clone(self, url, revision_id=None, basis=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.
720
        """See BzrDir.clone()."""
721
        from bzrlib.workingtree import WorkingTreeFormat2
722
        self._make_tail(url)
1651.1.6 by Martin Pool
Clean up clone-bzrdir code
723
        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.
724
        basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
725
        self.open_repository().clone(result, revision_id=revision_id, basis=basis_repo)
1692.7.9 by Martin Pool
Don't create broken standalone branches over sftp (Malone #43064)
726
        from_branch = self.open_branch()
727
        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.
728
        try:
729
            self.open_workingtree().clone(result, basis=basis_tree)
730
        except errors.NotLocalUrl:
731
            # make a new one, this format always has to have one.
1563.2.38 by Robert Collins
make push preserve tree formats.
732
            try:
733
                WorkingTreeFormat2().initialize(result)
734
            except errors.NotLocalUrl:
1692.7.9 by Martin Pool
Don't create broken standalone branches over sftp (Malone #43064)
735
                # but we cannot do it for remote trees.
736
                to_branch = result.open_branch()
737
                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.
738
        return result
739
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
740
    def create_branch(self):
741
        """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.
742
        return self.open_branch()
743
1534.6.1 by Robert Collins
allow API creation of shared repositories
744
    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.
745
        """See BzrDir.create_repository."""
1534.6.1 by Robert Collins
allow API creation of shared repositories
746
        if shared:
747
            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.
748
        return self.open_repository()
749
1508.1.21 by Robert Collins
Implement -r limit for checkout command.
750
    def create_workingtree(self, revision_id=None):
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.
751
        """See BzrDir.create_workingtree."""
1508.1.21 by Robert Collins
Implement -r limit for checkout command.
752
        # this looks buggy but is not -really-
753
        # clone and sprout will have set the revision_id
754
        # and that will have set it for us, its only
755
        # specific uses of create_workingtree in isolation
756
        # that can do wonky stuff here, and that only
757
        # happens for creating checkouts, which cannot be 
758
        # done on this format anyway. So - acceptable wart.
759
        result = self.open_workingtree()
1508.1.24 by Robert Collins
Add update command for use with checkouts.
760
        if revision_id is not None:
1996.3.12 by John Arbash Meinel
Change how 'revision' is imported to avoid problems later
761
            if revision_id == _mod_revision.NULL_REVISION:
1551.8.20 by Aaron Bentley
Fix BzrDir.create_workingtree for NULL_REVISION
762
                result.set_parent_ids([])
763
            else:
764
                result.set_parent_ids([revision_id])
1508.1.21 by Robert Collins
Implement -r limit for checkout command.
765
        return result
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
766
1551.8.37 by Aaron Bentley
Cleaner implementation of destroy_working_tree
767
    def destroy_workingtree(self):
1551.8.36 by Aaron Bentley
Introduce BzrDir.destroy_workingtree
768
        """See BzrDir.destroy_workingtree."""
769
        raise errors.UnsupportedOperation(self.destroy_workingtree, self)
770
1551.8.37 by Aaron Bentley
Cleaner implementation of destroy_working_tree
771
    def destroy_workingtree_metadata(self):
772
        """See BzrDir.destroy_workingtree_metadata."""
773
        raise errors.UnsupportedOperation(self.destroy_workingtree_metadata, 
774
                                          self)
775
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
776
    def get_branch_transport(self, branch_format):
777
        """See BzrDir.get_branch_transport()."""
778
        if branch_format is None:
779
            return self.transport
780
        try:
781
            branch_format.get_format_string()
782
        except NotImplementedError:
783
            return self.transport
784
        raise errors.IncompatibleFormat(branch_format, self._format)
785
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
786
    def get_repository_transport(self, repository_format):
787
        """See BzrDir.get_repository_transport()."""
788
        if repository_format is None:
789
            return self.transport
790
        try:
791
            repository_format.get_format_string()
792
        except NotImplementedError:
793
            return self.transport
794
        raise errors.IncompatibleFormat(repository_format, self._format)
795
1534.4.45 by Robert Collins
Start WorkingTree -> .bzr/checkout transition
796
    def get_workingtree_transport(self, workingtree_format):
797
        """See BzrDir.get_workingtree_transport()."""
798
        if workingtree_format is None:
799
            return self.transport
800
        try:
801
            workingtree_format.get_format_string()
802
        except NotImplementedError:
803
            return self.transport
804
        raise errors.IncompatibleFormat(workingtree_format, self._format)
805
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.
806
    def needs_format_conversion(self, format=None):
807
        """See BzrDir.needs_format_conversion()."""
808
        # if the format is not the same as the system default,
809
        # an upgrade is needed.
810
        if format is None:
811
            format = BzrDirFormat.get_default_format()
812
        return not isinstance(self._format, format.__class__)
813
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
814
    def open_branch(self, unsupported=False):
815
        """See BzrDir.open_branch."""
816
        from bzrlib.branch import BzrBranchFormat4
817
        format = BzrBranchFormat4()
818
        self._check_supported(format, unsupported)
819
        return format.open(self, _found=True)
820
1910.4.15 by Robert Collins
Fix a minor bug in BzrDirPreSplitOut.sprout - it did not define the force_new_repo parameter - and we were not testing that it did or didn't until the test cleanup.
821
    def sprout(self, url, revision_id=None, basis=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.
822
        """See BzrDir.sprout()."""
823
        from bzrlib.workingtree import WorkingTreeFormat2
824
        self._make_tail(url)
1651.1.6 by Martin Pool
Clean up clone-bzrdir code
825
        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.
826
        basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
827
        try:
828
            self.open_repository().clone(result, revision_id=revision_id, basis=basis_repo)
829
        except errors.NoRepositoryPresent:
830
            pass
831
        try:
832
            self.open_branch().sprout(result, revision_id=revision_id)
833
        except errors.NotBranchError:
834
            pass
1587.1.5 by Robert Collins
Put bzr branch behaviour back to the 0.7 ignore-working-tree state.
835
        # we always want a working tree
836
        WorkingTreeFormat2().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.
837
        return result
838
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
839
840
class BzrDir4(BzrDirPreSplitOut):
1508.1.25 by Robert Collins
Update per review comments.
841
    """A .bzr version 4 control object.
842
    
843
    This is a deprecated format and may be removed after sept 2006.
844
    """
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
845
1534.6.1 by Robert Collins
allow API creation of shared repositories
846
    def create_repository(self, shared=False):
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
847
        """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.
848
        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.
849
1534.5.16 by Robert Collins
Review feedback.
850
    def needs_format_conversion(self, format=None):
851
        """Format 4 dirs are always in need of conversion."""
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
852
        return True
853
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
854
    def open_repository(self):
855
        """See BzrDir.open_repository."""
856
        from bzrlib.repository import RepositoryFormat4
857
        return RepositoryFormat4().open(self, _found=True)
858
859
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
860
class BzrDir5(BzrDirPreSplitOut):
1508.1.25 by Robert Collins
Update per review comments.
861
    """A .bzr version 5 control object.
862
863
    This is a deprecated format and may be removed after sept 2006.
864
    """
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
865
866
    def open_repository(self):
867
        """See BzrDir.open_repository."""
868
        from bzrlib.repository import RepositoryFormat5
869
        return RepositoryFormat5().open(self, _found=True)
870
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
871
    def open_workingtree(self, _unsupported=False):
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
872
        """See BzrDir.create_workingtree."""
873
        from bzrlib.workingtree import WorkingTreeFormat2
874
        return WorkingTreeFormat2().open(self, _found=True)
875
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
876
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
877
class BzrDir6(BzrDirPreSplitOut):
1508.1.25 by Robert Collins
Update per review comments.
878
    """A .bzr version 6 control object.
879
880
    This is a deprecated format and may be removed after sept 2006.
881
    """
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
882
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
883
    def open_repository(self):
884
        """See BzrDir.open_repository."""
885
        from bzrlib.repository import RepositoryFormat6
886
        return RepositoryFormat6().open(self, _found=True)
887
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
888
    def open_workingtree(self, _unsupported=False):
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
889
        """See BzrDir.create_workingtree."""
890
        from bzrlib.workingtree import WorkingTreeFormat2
891
        return WorkingTreeFormat2().open(self, _found=True)
892
893
894
class BzrDirMeta1(BzrDir):
895
    """A .bzr meta version 1 control object.
896
    
897
    This is the first control object where the 
1553.5.67 by Martin Pool
doc
898
    individual aspects are really split out: there are separate repository,
899
    workingtree and branch subdirectories and any subset of the three can be
900
    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.
901
    """
902
1534.5.16 by Robert Collins
Review feedback.
903
    def can_convert_format(self):
904
        """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.
905
        return True
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
906
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
907
    def create_branch(self):
908
        """See BzrDir.create_branch."""
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
909
        from bzrlib.branch import BranchFormat
910
        return BranchFormat.get_default_format().initialize(self)
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
911
1534.6.1 by Robert Collins
allow API creation of shared repositories
912
    def create_repository(self, shared=False):
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
913
        """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.
914
        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.
915
1508.1.21 by Robert Collins
Implement -r limit for checkout command.
916
    def create_workingtree(self, revision_id=None):
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
917
        """See BzrDir.create_workingtree."""
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
918
        from bzrlib.workingtree import WorkingTreeFormat
1508.1.21 by Robert Collins
Implement -r limit for checkout command.
919
        return WorkingTreeFormat.get_default_format().initialize(self, revision_id)
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
920
1551.8.37 by Aaron Bentley
Cleaner implementation of destroy_working_tree
921
    def destroy_workingtree(self):
1551.8.36 by Aaron Bentley
Introduce BzrDir.destroy_workingtree
922
        """See BzrDir.destroy_workingtree."""
923
        wt = self.open_workingtree()
1551.8.37 by Aaron Bentley
Cleaner implementation of destroy_working_tree
924
        repository = wt.branch.repository
2094.3.5 by John Arbash Meinel
Fix imports to ensure modules are loaded before they are used
925
        empty = repository.revision_tree(_mod_revision.NULL_REVISION)
1551.8.37 by Aaron Bentley
Cleaner implementation of destroy_working_tree
926
        wt.revert([], old_tree=empty)
927
        self.destroy_workingtree_metadata()
928
929
    def destroy_workingtree_metadata(self):
930
        self.transport.delete_tree('checkout')
1551.8.36 by Aaron Bentley
Introduce BzrDir.destroy_workingtree
931
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
932
    def _get_mkdir_mode(self):
933
        """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
934
        temp_control = lockable_files.LockableFiles(self.transport, '',
935
                                     lockable_files.TransportLock)
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
936
        return temp_control._dir_mode
937
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
938
    def get_branch_transport(self, branch_format):
939
        """See BzrDir.get_branch_transport()."""
940
        if branch_format is None:
941
            return self.transport.clone('branch')
942
        try:
943
            branch_format.get_format_string()
944
        except NotImplementedError:
945
            raise errors.IncompatibleFormat(branch_format, self._format)
946
        try:
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
947
            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.
948
        except errors.FileExists:
949
            pass
950
        return self.transport.clone('branch')
951
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
952
    def get_repository_transport(self, repository_format):
953
        """See BzrDir.get_repository_transport()."""
954
        if repository_format is None:
955
            return self.transport.clone('repository')
956
        try:
957
            repository_format.get_format_string()
958
        except NotImplementedError:
959
            raise errors.IncompatibleFormat(repository_format, self._format)
960
        try:
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
961
            self.transport.mkdir('repository', mode=self._get_mkdir_mode())
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
962
        except errors.FileExists:
963
            pass
964
        return self.transport.clone('repository')
965
1534.4.45 by Robert Collins
Start WorkingTree -> .bzr/checkout transition
966
    def get_workingtree_transport(self, workingtree_format):
967
        """See BzrDir.get_workingtree_transport()."""
968
        if workingtree_format is None:
969
            return self.transport.clone('checkout')
970
        try:
971
            workingtree_format.get_format_string()
972
        except NotImplementedError:
973
            raise errors.IncompatibleFormat(workingtree_format, self._format)
974
        try:
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
975
            self.transport.mkdir('checkout', mode=self._get_mkdir_mode())
1534.4.45 by Robert Collins
Start WorkingTree -> .bzr/checkout transition
976
        except errors.FileExists:
977
            pass
978
        return self.transport.clone('checkout')
979
1534.5.16 by Robert Collins
Review feedback.
980
    def needs_format_conversion(self, format=None):
981
        """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.
982
        if format is None:
983
            format = BzrDirFormat.get_default_format()
984
        if not isinstance(self._format, format.__class__):
985
            # it is not a meta dir format, conversion is needed.
986
            return True
987
        # we might want to push this down to the repository?
988
        try:
989
            if not isinstance(self.open_repository()._format,
990
                              format.repository_format.__class__):
991
                # the repository needs an upgrade.
992
                return True
993
        except errors.NoRepositoryPresent:
994
            pass
995
        # currently there are no other possible conversions for meta1 formats.
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
996
        return False
997
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
998
    def open_branch(self, unsupported=False):
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
999
        """See BzrDir.open_branch."""
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1000
        from bzrlib.branch import BranchFormat
1001
        format = BranchFormat.find_format(self)
1002
        self._check_supported(format, unsupported)
1003
        return format.open(self, _found=True)
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
1004
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
1005
    def open_repository(self, unsupported=False):
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1006
        """See BzrDir.open_repository."""
1534.4.47 by Robert Collins
Split out repository into .bzr/repository
1007
        from bzrlib.repository import RepositoryFormat
1008
        format = RepositoryFormat.find_format(self)
1009
        self._check_supported(format, unsupported)
1010
        return format.open(self, _found=True)
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1011
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
1012
    def open_workingtree(self, unsupported=False):
1508.1.21 by Robert Collins
Implement -r limit for checkout command.
1013
        """See BzrDir.open_workingtree."""
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
1014
        from bzrlib.workingtree import WorkingTreeFormat
1015
        format = WorkingTreeFormat.find_format(self)
1016
        self._check_supported(format, unsupported)
1017
        return format.open(self, _found=True)
1534.4.42 by Robert Collins
add working tree to the BzrDir facilities.
1018
1534.4.39 by Robert Collins
Basic BzrDir support.
1019
1020
class BzrDirFormat(object):
1021
    """An encapsulation of the initialization and open routines for a format.
1022
1023
    Formats provide three things:
1024
     * An initialization routine,
1025
     * a format string,
1026
     * an open routine.
1027
1028
    Formats are placed in an dict by their format string for reference 
1029
    during bzrdir opening. These should be subclasses of BzrDirFormat
1030
    for consistency.
1031
1032
    Once a format is deprecated, just deprecate the initialize and open
1033
    methods on the format class. Do not deprecate the object, as the 
1034
    object will be created every system load.
1035
    """
1036
1037
    _default_format = None
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1038
    """The default format used for new .bzr dirs."""
1534.4.39 by Robert Collins
Basic BzrDir support.
1039
1040
    _formats = {}
1041
    """The known formats."""
1042
1733.1.7 by Jelmer Vernooij
Change set of control dir formats to list.
1043
    _control_formats = []
1733.1.6 by Jelmer Vernooij
Fix a couple of minor issues after review by Martin.
1044
    """The registered control formats - .bzr, ....
1045
    
1733.1.7 by Jelmer Vernooij
Change set of control dir formats to list.
1046
    This is a list of BzrDirFormat objects.
1733.1.6 by Jelmer Vernooij
Fix a couple of minor issues after review by Martin.
1047
    """
1733.1.1 by Robert Collins
Support non '.bzr' control directories in bzrdir.
1048
1553.5.69 by Martin Pool
BzrDirFormat subclasses can now control what kind of overall lock is used.
1049
    _lock_file_name = 'branch-lock'
1050
1051
    # _lock_class must be set in subclasses to the lock type, typ.
1052
    # TransportLock or LockDir
1053
1534.4.39 by Robert Collins
Basic BzrDir support.
1054
    @classmethod
1055
    def find_format(klass, transport):
1733.1.1 by Robert Collins
Support non '.bzr' control directories in bzrdir.
1056
        """Return the format present at transport."""
1057
        for format in klass._control_formats:
1058
            try:
1059
                return format.probe_transport(transport)
1060
            except errors.NotBranchError:
1061
                # this format does not find a control dir here.
1062
                pass
1063
        raise errors.NotBranchError(path=transport.base)
1064
1065
    @classmethod
1066
    def probe_transport(klass, transport):
1067
        """Return the .bzrdir style transport present at URL."""
1534.4.39 by Robert Collins
Basic BzrDir support.
1068
        try:
1069
            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.
1070
        except errors.NoSuchFile:
1071
            raise errors.NotBranchError(path=transport.base)
1072
1073
        try:
1534.4.39 by Robert Collins
Basic BzrDir support.
1074
            return klass._formats[format_string]
1075
        except KeyError:
1740.5.6 by Martin Pool
Clean up many exception classes.
1076
            raise errors.UnknownFormatError(format=format_string)
1534.4.39 by Robert Collins
Basic BzrDir support.
1077
1078
    @classmethod
1079
    def get_default_format(klass):
1080
        """Return the current default format."""
1081
        return klass._default_format
1082
1083
    def get_format_string(self):
1084
        """Return the ASCII format string that identifies this format."""
1085
        raise NotImplementedError(self.get_format_string)
1086
1624.3.19 by Olaf Conradi
New call get_format_description to give a user-friendly description of a
1087
    def get_format_description(self):
1088
        """Return the short description for this format."""
1089
        raise NotImplementedError(self.get_format_description)
1090
1534.5.16 by Robert Collins
Review feedback.
1091
    def get_converter(self, format=None):
1092
        """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.
1093
1094
        This returns a bzrlib.bzrdir.Converter object.
1095
1096
        This should return the best upgrader to step this format towards the
1759.2.1 by Jelmer Vernooij
Fix some types (found using aspell).
1097
        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.
1098
        some means for them to extend the range of returnable converters.
1534.5.13 by Robert Collins
Correct buggy test.
1099
1759.2.1 by Jelmer Vernooij
Fix some types (found using aspell).
1100
        :param format: Optional format to override the default format of the 
1534.5.13 by Robert Collins
Correct buggy test.
1101
                       library.
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1102
        """
1534.5.16 by Robert Collins
Review feedback.
1103
        raise NotImplementedError(self.get_converter)
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1104
1651.1.6 by Martin Pool
Clean up clone-bzrdir code
1105
    def initialize(self, url):
1608.2.8 by Martin Pool
Separate out BzrDir.initialize_on_transport so it
1106
        """Create a bzr control dir at this url and return an opened copy.
1107
        
1108
        Subclasses should typically override initialize_on_transport
1109
        instead of this method.
1110
        """
1651.1.6 by Martin Pool
Clean up clone-bzrdir code
1111
        return self.initialize_on_transport(get_transport(url))
1608.2.8 by Martin Pool
Separate out BzrDir.initialize_on_transport so it
1112
1651.1.6 by Martin Pool
Clean up clone-bzrdir code
1113
    def initialize_on_transport(self, transport):
1608.2.8 by Martin Pool
Separate out BzrDir.initialize_on_transport so it
1114
        """Initialize a new bzrdir in the base directory of a Transport."""
1759.2.1 by Jelmer Vernooij
Fix some types (found using aspell).
1115
        # Since we don't have a .bzr directory, inherit the
1534.4.39 by Robert Collins
Basic BzrDir support.
1116
        # mode from the root directory
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
1117
        temp_control = lockable_files.LockableFiles(transport,
1118
                            '', lockable_files.TransportLock)
1534.4.39 by Robert Collins
Basic BzrDir support.
1119
        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.
1120
                                      # FIXME: RBC 20060121 don't peek under
1534.4.39 by Robert Collins
Basic BzrDir support.
1121
                                      # the covers
1122
                                      mode=temp_control._dir_mode)
1123
        file_mode = temp_control._file_mode
1124
        del temp_control
1608.2.8 by Martin Pool
Separate out BzrDir.initialize_on_transport so it
1125
        mutter('created control directory in ' + transport.base)
1126
        control = transport.clone('.bzr')
1534.4.39 by Robert Collins
Basic BzrDir support.
1127
        utf8_files = [('README', 
1128
                       "This is a Bazaar-NG control directory.\n"
1129
                       "Do not change any files in this directory.\n"),
1130
                      ('branch-format', self.get_format_string()),
1131
                      ]
1132
        # 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
1133
        control_files = lockable_files.LockableFiles(control,
1134
                            self._lock_file_name, self._lock_class)
1553.5.60 by Martin Pool
New LockableFiles.create_lock() method
1135
        control_files.create_lock()
1534.4.39 by Robert Collins
Basic BzrDir support.
1136
        control_files.lock_write()
1137
        try:
1138
            for file, content in utf8_files:
1139
                control_files.put_utf8(file, content)
1140
        finally:
1141
            control_files.unlock()
1608.2.8 by Martin Pool
Separate out BzrDir.initialize_on_transport so it
1142
        return self.open(transport, _found=True)
1534.4.39 by Robert Collins
Basic BzrDir support.
1143
1144
    def is_supported(self):
1145
        """Is this format supported?
1146
1147
        Supported formats must be initializable and openable.
1148
        Unsupported formats may not support initialization or committing or 
1149
        some other features depending on the reason for not being supported.
1150
        """
1151
        return True
1152
1910.2.14 by Aaron Bentley
Fail when trying to use interrepository on Knit2 and Knit1
1153
    def same_model(self, target_format):
1154
        return (self.repository_format.rich_root_data == 
1155
            target_format.rich_root_data)
1156
1733.1.3 by Robert Collins
Extend the test suite to run bzrdir conformance tests on non .bzr based control dirs.
1157
    @classmethod
1158
    def known_formats(klass):
1159
        """Return all the known formats.
1160
        
1161
        Concrete formats should override _known_formats.
1162
        """
1733.1.6 by Jelmer Vernooij
Fix a couple of minor issues after review by Martin.
1163
        # There is double indirection here to make sure that control 
1164
        # formats used by more than one dir format will only be probed 
1165
        # 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.
1166
        result = set()
1167
        for format in klass._control_formats:
1168
            result.update(format._known_formats())
1169
        return result
1170
    
1171
    @classmethod
1172
    def _known_formats(klass):
1173
        """Return the known format instances for this control format."""
1174
        return set(klass._formats.values())
1175
1534.4.39 by Robert Collins
Basic BzrDir support.
1176
    def open(self, transport, _found=False):
1177
        """Return an instance of this format for the dir transport points at.
1178
        
1179
        _found is a private parameter, do not use it.
1180
        """
1181
        if not _found:
2090.2.2 by Martin Pool
Fix an assertion with side effects
1182
            found_format = BzrDirFormat.find_format(transport)
1183
            if not isinstance(found_format, self.__class__):
1184
                raise AssertionError("%s was asked to open %s, but it seems to need "
1185
                        "format %s" 
1186
                        % (self, transport, found_format))
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1187
        return self._open(transport)
1188
1189
    def _open(self, transport):
1190
        """Template method helper for opening BzrDirectories.
1191
1192
        This performs the actual open and any additional logic or parameter
1193
        passing.
1194
        """
1195
        raise NotImplementedError(self._open)
1534.4.39 by Robert Collins
Basic BzrDir support.
1196
1197
    @classmethod
1198
    def register_format(klass, format):
1199
        klass._formats[format.get_format_string()] = format
1200
1201
    @classmethod
1733.1.1 by Robert Collins
Support non '.bzr' control directories in bzrdir.
1202
    def register_control_format(klass, format):
1203
        """Register a format that does not use '.bzrdir' for its control dir.
1204
1205
        TODO: This should be pulled up into a 'ControlDirFormat' base class
1206
        which BzrDirFormat can inherit from, and renamed to register_format 
1207
        there. It has been done without that for now for simplicity of
1208
        implementation.
1209
        """
1733.1.7 by Jelmer Vernooij
Change set of control dir formats to list.
1210
        klass._control_formats.append(format)
1733.1.1 by Robert Collins
Support non '.bzr' control directories in bzrdir.
1211
1212
    @classmethod
2204.4.12 by Aaron Bentley
Deprecate bzrdir.BzrDirFormat.set_default_format
1213
    @symbol_versioning.deprecated_method(symbol_versioning.zero_fourteen)
1534.4.39 by Robert Collins
Basic BzrDir support.
1214
    def set_default_format(klass, format):
2204.5.2 by Aaron Bentley
Tweak set_default_format
1215
        klass._set_default_format(format)
1534.4.39 by Robert Collins
Basic BzrDir support.
1216
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
1217
    @classmethod
1218
    def _set_default_format(klass, format):
1219
        """Set default format (for testing behavior of defaults only)"""
1220
        klass._default_format = format
1221
1534.5.1 by Robert Collins
Give info some reasonable output and tests.
1222
    def __str__(self):
1223
        return self.get_format_string()[:-1]
1224
1534.4.39 by Robert Collins
Basic BzrDir support.
1225
    @classmethod
1226
    def unregister_format(klass, format):
1227
        assert klass._formats[format.get_format_string()] is format
1228
        del klass._formats[format.get_format_string()]
1229
1733.1.1 by Robert Collins
Support non '.bzr' control directories in bzrdir.
1230
    @classmethod
1231
    def unregister_control_format(klass, format):
1232
        klass._control_formats.remove(format)
1233
1234
1235
# register BzrDirFormat as a control format
1236
BzrDirFormat.register_control_format(BzrDirFormat)
1237
1534.4.39 by Robert Collins
Basic BzrDir support.
1238
1239
class BzrDirFormat4(BzrDirFormat):
1240
    """Bzr dir format 4.
1241
1242
    This format is a combined format for working tree, branch and repository.
1243
    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.
1244
     - Format 1 working trees [always]
1245
     - Format 4 branches [always]
1246
     - Format 4 repositories [always]
1534.4.39 by Robert Collins
Basic BzrDir support.
1247
1248
    This format is deprecated: it indexes texts using a text it which is
1249
    removed in format 5; write support for this format has been removed.
1250
    """
1251
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
1252
    _lock_class = lockable_files.TransportLock
1553.5.69 by Martin Pool
BzrDirFormat subclasses can now control what kind of overall lock is used.
1253
1534.4.39 by Robert Collins
Basic BzrDir support.
1254
    def get_format_string(self):
1255
        """See BzrDirFormat.get_format_string()."""
1256
        return "Bazaar-NG branch, format 0.0.4\n"
1257
1624.3.19 by Olaf Conradi
New call get_format_description to give a user-friendly description of a
1258
    def get_format_description(self):
1259
        """See BzrDirFormat.get_format_description()."""
1260
        return "All-in-one format 4"
1261
1534.5.16 by Robert Collins
Review feedback.
1262
    def get_converter(self, format=None):
1263
        """See BzrDirFormat.get_converter()."""
1534.5.13 by Robert Collins
Correct buggy test.
1264
        # 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.
1265
        return ConvertBzrDir4To5()
1266
        
1651.1.6 by Martin Pool
Clean up clone-bzrdir code
1267
    def initialize_on_transport(self, transport):
1534.4.39 by Robert Collins
Basic BzrDir support.
1268
        """Format 4 branches cannot be created."""
1269
        raise errors.UninitializableFormat(self)
1270
1271
    def is_supported(self):
1272
        """Format 4 is not supported.
1273
1274
        It is not supported because the model changed from 4 to 5 and the
1275
        conversion logic is expensive - so doing it on the fly was not 
1276
        feasible.
1277
        """
1278
        return False
1279
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1280
    def _open(self, transport):
1281
        """See BzrDirFormat._open."""
1282
        return BzrDir4(transport, self)
1283
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.
1284
    def __return_repository_format(self):
1285
        """Circular import protection."""
1286
        from bzrlib.repository import RepositoryFormat4
1910.2.12 by Aaron Bentley
Implement knit repo format 2
1287
        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.
1288
    repository_format = property(__return_repository_format)
1289
1534.4.39 by Robert Collins
Basic BzrDir support.
1290
1291
class BzrDirFormat5(BzrDirFormat):
1292
    """Bzr control format 5.
1293
1294
    This format is a combined format for working tree, branch and repository.
1295
    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.
1296
     - Format 2 working trees [always] 
1297
     - Format 4 branches [always] 
1534.4.53 by Robert Collins
Review feedback from John Meinel.
1298
     - 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.
1299
       Unhashed stores in the repository.
1534.4.39 by Robert Collins
Basic BzrDir support.
1300
    """
1301
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
1302
    _lock_class = lockable_files.TransportLock
1553.5.69 by Martin Pool
BzrDirFormat subclasses can now control what kind of overall lock is used.
1303
1534.4.39 by Robert Collins
Basic BzrDir support.
1304
    def get_format_string(self):
1305
        """See BzrDirFormat.get_format_string()."""
1306
        return "Bazaar-NG branch, format 5\n"
1307
1624.3.19 by Olaf Conradi
New call get_format_description to give a user-friendly description of a
1308
    def get_format_description(self):
1309
        """See BzrDirFormat.get_format_description()."""
1310
        return "All-in-one format 5"
1311
1534.5.16 by Robert Collins
Review feedback.
1312
    def get_converter(self, format=None):
1313
        """See BzrDirFormat.get_converter()."""
1534.5.13 by Robert Collins
Correct buggy test.
1314
        # 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.
1315
        return ConvertBzrDir5To6()
1651.1.6 by Martin Pool
Clean up clone-bzrdir code
1316
1317
    def _initialize_for_clone(self, url):
1318
        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.
1319
        
1608.2.8 by Martin Pool
Separate out BzrDir.initialize_on_transport so it
1320
    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.
1321
        """Format 5 dirs always have working tree, branch and repository.
1322
        
1323
        Except when they are being cloned.
1324
        """
1325
        from bzrlib.branch import BzrBranchFormat4
1326
        from bzrlib.repository import RepositoryFormat5
1327
        from bzrlib.workingtree import WorkingTreeFormat2
1651.1.6 by Martin Pool
Clean up clone-bzrdir code
1328
        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.
1329
        RepositoryFormat5().initialize(result, _internal=True)
1330
        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.
1331
            branch = BzrBranchFormat4().initialize(result)
1332
            try:
1333
                WorkingTreeFormat2().initialize(result)
1334
            except errors.NotLocalUrl:
1335
                # Even though we can't access the working tree, we need to
1336
                # create its control files.
1337
                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.
1338
        return result
1339
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1340
    def _open(self, transport):
1341
        """See BzrDirFormat._open."""
1342
        return BzrDir5(transport, self)
1343
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.
1344
    def __return_repository_format(self):
1345
        """Circular import protection."""
1346
        from bzrlib.repository import RepositoryFormat5
1910.2.12 by Aaron Bentley
Implement knit repo format 2
1347
        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.
1348
    repository_format = property(__return_repository_format)
1349
1534.4.39 by Robert Collins
Basic BzrDir support.
1350
1351
class BzrDirFormat6(BzrDirFormat):
1352
    """Bzr control format 6.
1353
1354
    This format is a combined format for working tree, branch and repository.
1355
    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.
1356
     - Format 2 working trees [always] 
1357
     - Format 4 branches [always] 
1358
     - Format 6 repositories [always]
1534.4.39 by Robert Collins
Basic BzrDir support.
1359
    """
1360
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
1361
    _lock_class = lockable_files.TransportLock
1553.5.69 by Martin Pool
BzrDirFormat subclasses can now control what kind of overall lock is used.
1362
1534.4.39 by Robert Collins
Basic BzrDir support.
1363
    def get_format_string(self):
1364
        """See BzrDirFormat.get_format_string()."""
1365
        return "Bazaar-NG branch, format 6\n"
1366
1624.3.19 by Olaf Conradi
New call get_format_description to give a user-friendly description of a
1367
    def get_format_description(self):
1368
        """See BzrDirFormat.get_format_description()."""
1369
        return "All-in-one format 6"
1370
1534.5.16 by Robert Collins
Review feedback.
1371
    def get_converter(self, format=None):
1372
        """See BzrDirFormat.get_converter()."""
1534.5.13 by Robert Collins
Correct buggy test.
1373
        # 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.
1374
        return ConvertBzrDir6ToMeta()
1375
        
1651.1.6 by Martin Pool
Clean up clone-bzrdir code
1376
    def _initialize_for_clone(self, url):
1377
        return self.initialize_on_transport(get_transport(url), _cloning=True)
1378
1608.2.8 by Martin Pool
Separate out BzrDir.initialize_on_transport so it
1379
    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.
1380
        """Format 6 dirs always have working tree, branch and repository.
1381
        
1382
        Except when they are being cloned.
1383
        """
1384
        from bzrlib.branch import BzrBranchFormat4
1385
        from bzrlib.repository import RepositoryFormat6
1386
        from bzrlib.workingtree import WorkingTreeFormat2
1651.1.6 by Martin Pool
Clean up clone-bzrdir code
1387
        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.
1388
        RepositoryFormat6().initialize(result, _internal=True)
1389
        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.
1390
            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.
1391
            try:
1392
                WorkingTreeFormat2().initialize(result)
1393
            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.
1394
                # Even though we can't access the working tree, we need to
1395
                # create its control files.
1396
                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.
1397
        return result
1398
1534.4.40 by Robert Collins
Add RepositoryFormats and allow bzrdir.open or create _repository to be used.
1399
    def _open(self, transport):
1400
        """See BzrDirFormat._open."""
1401
        return BzrDir6(transport, self)
1402
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.
1403
    def __return_repository_format(self):
1404
        """Circular import protection."""
1405
        from bzrlib.repository import RepositoryFormat6
1910.2.12 by Aaron Bentley
Implement knit repo format 2
1406
        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.
1407
    repository_format = property(__return_repository_format)
1408
1534.4.39 by Robert Collins
Basic BzrDir support.
1409
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1410
class BzrDirMetaFormat1(BzrDirFormat):
1411
    """Bzr meta control format 1
1412
1413
    This is the first format with split out working tree, branch and repository
1414
    disk storage.
1415
    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.
1416
     - Format 3 working trees [optional]
1417
     - Format 5 branches [optional]
1418
     - 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.
1419
    """
1420
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
1421
    _lock_class = lockdir.LockDir
1553.5.69 by Martin Pool
BzrDirFormat subclasses can now control what kind of overall lock is used.
1422
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.
1423
    def get_converter(self, format=None):
1424
        """See BzrDirFormat.get_converter()."""
1425
        if format is None:
1426
            format = BzrDirFormat.get_default_format()
1427
        if not isinstance(self, format.__class__):
1428
            # converting away from metadir is not implemented
1429
            raise NotImplementedError(self.get_converter)
1430
        return ConvertMetaToMeta(format)
1431
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1432
    def get_format_string(self):
1433
        """See BzrDirFormat.get_format_string()."""
1434
        return "Bazaar-NG meta directory, format 1\n"
1435
1624.3.19 by Olaf Conradi
New call get_format_description to give a user-friendly description of a
1436
    def get_format_description(self):
1437
        """See BzrDirFormat.get_format_description()."""
1438
        return "Meta directory format 1"
1439
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1440
    def _open(self, transport):
1441
        """See BzrDirFormat._open."""
1442
        return BzrDirMeta1(transport, self)
1443
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.
1444
    def __return_repository_format(self):
1445
        """Circular import protection."""
1446
        if getattr(self, '_repository_format', None):
1447
            return self._repository_format
1448
        from bzrlib.repository import RepositoryFormat
1449
        return RepositoryFormat.get_default_format()
1450
1451
    def __set_repository_format(self, value):
1452
        """Allow changint the repository format for metadir formats."""
1453
        self._repository_format = value
1553.5.72 by Martin Pool
Clean up test for Branch5 lockdirs
1454
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.
1455
    repository_format = property(__return_repository_format, __set_repository_format)
1456
1534.4.44 by Robert Collins
Make a new BzrDir format that uses a versioned branch format in a branch/ subdirectory.
1457
1534.4.39 by Robert Collins
Basic BzrDir support.
1458
BzrDirFormat.register_format(BzrDirFormat4())
1459
BzrDirFormat.register_format(BzrDirFormat5())
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
1460
BzrDirFormat.register_format(BzrDirFormat6())
1461
__default_format = BzrDirMetaFormat1()
1534.4.39 by Robert Collins
Basic BzrDir support.
1462
BzrDirFormat.register_format(__default_format)
2204.4.13 by Aaron Bentley
Update all test cases to avoid set_default_format
1463
BzrDirFormat._default_format = __default_format
1534.4.39 by Robert Collins
Basic BzrDir support.
1464
1465
1466
class BzrDirTestProviderAdapter(object):
1467
    """A tool to generate a suite testing multiple bzrdir formats at once.
1468
1469
    This is done by copying the test once for each transport and injecting
1470
    the transport_server, transport_readonly_server, and bzrdir_format
1471
    classes into each copy. Each copy is also given a new id() to make it
1472
    easy to identify.
1473
    """
1474
1475
    def __init__(self, transport_server, transport_readonly_server, formats):
1476
        self._transport_server = transport_server
1477
        self._transport_readonly_server = transport_readonly_server
1478
        self._formats = formats
1479
    
1480
    def adapt(self, test):
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
1481
        result = unittest.TestSuite()
1534.4.39 by Robert Collins
Basic BzrDir support.
1482
        for format in self._formats:
1483
            new_test = deepcopy(test)
1484
            new_test.transport_server = self._transport_server
1485
            new_test.transport_readonly_server = self._transport_readonly_server
1486
            new_test.bzrdir_format = format
1487
            def make_new_test_id():
1488
                new_id = "%s(%s)" % (new_test.id(), format.__class__.__name__)
1489
                return lambda: new_id
1490
            new_test.id = make_new_test_id()
1491
            result.addTest(new_test)
1492
        return result
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
1493
1494
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1495
class Converter(object):
1496
    """Converts a disk format object from one format to another."""
1497
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1498
    def convert(self, to_convert, pb):
1499
        """Perform the conversion of to_convert, giving feedback via pb.
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1500
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1501
        :param to_convert: The disk object to convert.
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1502
        :param pb: a progress bar to use for progress information.
1503
        """
1504
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.
1505
    def step(self, message):
1506
        """Update the pb by a step."""
1507
        self.count +=1
1508
        self.pb.update(message, self.count, self.total)
1509
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1510
1511
class ConvertBzrDir4To5(Converter):
1512
    """Converts format 4 bzr dirs to format 5."""
1513
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1514
    def __init__(self):
1515
        super(ConvertBzrDir4To5, self).__init__()
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1516
        self.converted_revs = set()
1517
        self.absent_revisions = set()
1518
        self.text_count = 0
1519
        self.revisions = {}
1520
        
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1521
    def convert(self, to_convert, pb):
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1522
        """See Converter.convert()."""
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1523
        self.bzrdir = to_convert
1524
        self.pb = pb
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1525
        self.pb.note('starting upgrade from format 4 to 5')
1526
        if isinstance(self.bzrdir.transport, LocalTransport):
1527
            self.bzrdir.get_workingtree_transport(None).delete('stat-cache')
1528
        self._convert_to_weaves()
1529
        return BzrDir.open(self.bzrdir.root_transport.base)
1530
1531
    def _convert_to_weaves(self):
1532
        self.pb.note('note: upgrade may be faster if all store files are ungzipped first')
1533
        try:
1534
            # TODO permissions
1535
            stat = self.bzrdir.transport.stat('weaves')
1536
            if not S_ISDIR(stat.st_mode):
1537
                self.bzrdir.transport.delete('weaves')
1538
                self.bzrdir.transport.mkdir('weaves')
1539
        except errors.NoSuchFile:
1540
            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.
1541
        # 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.
1542
        self.inv_weave = Weave('inventory')
1543
        # holds in-memory weaves for all files
1544
        self.text_weaves = {}
1545
        self.bzrdir.transport.delete('branch-format')
1546
        self.branch = self.bzrdir.open_branch()
1547
        self._convert_working_inv()
1548
        rev_history = self.branch.revision_history()
1549
        # to_read is a stack holding the revisions we still need to process;
1550
        # appending to it adds new highest-priority revisions
1551
        self.known_revisions = set(rev_history)
1552
        self.to_read = rev_history[-1:]
1553
        while self.to_read:
1554
            rev_id = self.to_read.pop()
1555
            if (rev_id not in self.revisions
1556
                and rev_id not in self.absent_revisions):
1557
                self._load_one_rev(rev_id)
1558
        self.pb.clear()
1559
        to_import = self._make_order()
1560
        for i, rev_id in enumerate(to_import):
1561
            self.pb.update('converting revision', i, len(to_import))
1562
            self._convert_one_rev(rev_id)
1563
        self.pb.clear()
1564
        self._write_all_weaves()
1565
        self._write_all_revs()
1566
        self.pb.note('upgraded to weaves:')
1567
        self.pb.note('  %6d revisions and inventories', len(self.revisions))
1568
        self.pb.note('  %6d revisions not present', len(self.absent_revisions))
1569
        self.pb.note('  %6d texts', self.text_count)
1570
        self._cleanup_spare_files_after_format4()
1571
        self.branch.control_files.put_utf8('branch-format', BzrDirFormat5().get_format_string())
1572
1573
    def _cleanup_spare_files_after_format4(self):
1574
        # FIXME working tree upgrade foo.
1575
        for n in 'merged-patches', 'pending-merged-patches':
1576
            try:
1577
                ## assert os.path.getsize(p) == 0
1578
                self.bzrdir.transport.delete(n)
1579
            except errors.NoSuchFile:
1580
                pass
1581
        self.bzrdir.transport.delete_tree('inventory-store')
1582
        self.bzrdir.transport.delete_tree('text-store')
1583
1584
    def _convert_working_inv(self):
1996.3.6 by John Arbash Meinel
Find a few places that weren't importing their dependencies.
1585
        inv = xml4.serializer_v4.read_inventory(
1586
                    self.branch.control_files.get('inventory'))
1587
        new_inv_xml = xml5.serializer_v5.write_inventory_to_string(inv)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1588
        # 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
1589
        self.branch.control_files.put('inventory', StringIO(new_inv_xml))
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1590
1591
    def _write_all_weaves(self):
1592
        controlweaves = WeaveStore(self.bzrdir.transport, prefixed=False)
1593
        weave_transport = self.bzrdir.transport.clone('weaves')
1594
        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
1595
        transaction = WriteTransaction()
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1596
1597
        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.
1598
            i = 0
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1599
            for file_id, file_weave in self.text_weaves.items():
1600
                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.
1601
                weaves._put_weave(file_id, file_weave, transaction)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1602
                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.
1603
            self.pb.update('inventory', 0, 1)
1604
            controlweaves._put_weave('inventory', self.inv_weave, transaction)
1605
            self.pb.update('inventory', 1, 1)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1606
        finally:
1607
            self.pb.clear()
1608
1609
    def _write_all_revs(self):
1610
        """Write all revisions out in new form."""
1611
        self.bzrdir.transport.delete_tree('revision-store')
1612
        self.bzrdir.transport.mkdir('revision-store')
1613
        revision_transport = self.bzrdir.transport.clone('revision-store')
1614
        # TODO permissions
1563.2.28 by Robert Collins
Add total_size to the revision_store api.
1615
        _revision_store = TextRevisionStore(TextStore(revision_transport,
1616
                                                      prefixed=False,
1617
                                                      compressed=True))
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1618
        try:
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
1619
            transaction = WriteTransaction()
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1620
            for i, rev_id in enumerate(self.converted_revs):
1621
                self.pb.update('write revision', i, len(self.converted_revs))
1563.2.28 by Robert Collins
Add total_size to the revision_store api.
1622
                _revision_store.add_revision(self.revisions[rev_id], transaction)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1623
        finally:
1624
            self.pb.clear()
1625
            
1626
    def _load_one_rev(self, rev_id):
1627
        """Load a revision object into memory.
1628
1629
        Any parents not either loaded or abandoned get queued to be
1630
        loaded."""
1631
        self.pb.update('loading revision',
1632
                       len(self.revisions),
1633
                       len(self.known_revisions))
1563.2.22 by Robert Collins
Move responsibility for repository.has_revision into RevisionStore
1634
        if not self.branch.repository.has_revision(rev_id):
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1635
            self.pb.clear()
1636
            self.pb.note('revision {%s} not present in branch; '
1637
                         'will be converted as a ghost',
1638
                         rev_id)
1639
            self.absent_revisions.add(rev_id)
1640
        else:
1563.2.28 by Robert Collins
Add total_size to the revision_store api.
1641
            rev = self.branch.repository._revision_store.get_revision(rev_id,
1642
                self.branch.repository.get_transaction())
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1643
            for parent_id in rev.parent_ids:
1644
                self.known_revisions.add(parent_id)
1645
                self.to_read.append(parent_id)
1646
            self.revisions[rev_id] = rev
1647
1648
    def _load_old_inventory(self, rev_id):
1649
        assert rev_id not in self.converted_revs
1650
        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.
1651
        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
1652
        inv.revision_id = rev_id
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1653
        rev = self.revisions[rev_id]
1654
        if rev.inventory_sha1:
1655
            assert rev.inventory_sha1 == sha_string(old_inv_xml), \
1656
                'inventory sha mismatch for {%s}' % rev_id
1657
        return inv
1658
1659
    def _load_updated_inventory(self, rev_id):
1660
        assert rev_id in self.converted_revs
1661
        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.
1662
        inv = xml5.serializer_v5.read_inventory_from_string(inv_xml)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1663
        return inv
1664
1665
    def _convert_one_rev(self, rev_id):
1666
        """Convert revision and all referenced objects to new format."""
1667
        rev = self.revisions[rev_id]
1668
        inv = self._load_old_inventory(rev_id)
1669
        present_parents = [p for p in rev.parent_ids
1670
                           if p not in self.absent_revisions]
1671
        self._convert_revision_contents(rev, inv, present_parents)
1672
        self._store_new_weave(rev, inv, present_parents)
1673
        self.converted_revs.add(rev_id)
1674
1675
    def _store_new_weave(self, rev, inv, present_parents):
1676
        # the XML is now updated with text versions
1677
        if __debug__:
1907.1.8 by Aaron Bentley
Remove is_root
1678
            entries = inv.iter_entries()
1679
            entries.next()
1680
            for path, ie in entries:
1963.2.6 by Robey Pointer
pychecker is on crack; go back to using 'is None'.
1681
                assert getattr(ie, 'revision', None) is not None, \
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1682
                    'no revision on {%s} in {%s}' % \
1683
                    (file_id, rev.revision_id)
1996.3.6 by John Arbash Meinel
Find a few places that weren't importing their dependencies.
1684
        new_inv_xml = xml5.serializer_v5.write_inventory_to_string(inv)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1685
        new_inv_sha1 = sha_string(new_inv_xml)
1563.2.28 by Robert Collins
Add total_size to the revision_store api.
1686
        self.inv_weave.add_lines(rev.revision_id, 
1687
                                 present_parents,
1688
                                 new_inv_xml.splitlines(True))
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1689
        rev.inventory_sha1 = new_inv_sha1
1690
1691
    def _convert_revision_contents(self, rev, inv, present_parents):
1692
        """Convert all the files within a revision.
1693
1694
        Also upgrade the inventory to refer to the text revision ids."""
1695
        rev_id = rev.revision_id
1696
        mutter('converting texts of revision {%s}',
1697
               rev_id)
1698
        parent_invs = map(self._load_updated_inventory, present_parents)
1731.1.62 by Aaron Bentley
Changes from review comments
1699
        entries = inv.iter_entries()
1700
        entries.next()
1701
        for path, ie in entries:
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1702
            self._convert_file_version(rev, ie, parent_invs)
1703
1704
    def _convert_file_version(self, rev, ie, parent_invs):
1705
        """Convert one version of one file.
1706
1707
        The file needs to be added into the weave if it is a merge
1708
        of >=2 parents or if it's changed from its parent.
1709
        """
1710
        file_id = ie.file_id
1711
        rev_id = rev.revision_id
1712
        w = self.text_weaves.get(file_id)
1713
        if w is None:
1714
            w = Weave(file_id)
1715
            self.text_weaves[file_id] = w
1716
        text_changed = False
1596.2.20 by Robert Collins
optimise commit to only access weaves for merged, or altered files during commit.
1717
        previous_entries = ie.find_previous_heads(parent_invs,
1718
                                                  None,
1719
                                                  None,
1720
                                                  entry_vf=w)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1721
        for old_revision in previous_entries:
1722
                # if this fails, its a ghost ?
1731.1.33 by Aaron Bentley
Revert no-special-root changes
1723
                assert old_revision in self.converted_revs, \
1724
                    "Revision {%s} not in converted_revs" % old_revision
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1725
        self.snapshot_ie(previous_entries, ie, w, rev_id)
1726
        del ie.text_id
1727
        assert getattr(ie, 'revision', None) is not None
1728
1729
    def snapshot_ie(self, previous_revisions, ie, w, rev_id):
1730
        # TODO: convert this logic, which is ~= snapshot to
1731
        # a call to:. This needs the path figured out. rather than a work_tree
1732
        # a v4 revision_tree can be given, or something that looks enough like
1733
        # one to give the file content to the entry if it needs it.
1734
        # and we need something that looks like a weave store for snapshot to 
1735
        # save against.
1736
        #ie.snapshot(rev, PATH, previous_revisions, REVISION_TREE, InMemoryWeaveStore(self.text_weaves))
1737
        if len(previous_revisions) == 1:
1738
            previous_ie = previous_revisions.values()[0]
1739
            if ie._unchanged(previous_ie):
1740
                ie.revision = previous_ie.revision
1741
                return
1742
        if ie.has_text():
1743
            text = self.branch.repository.text_store.get(ie.text_id)
1744
            file_lines = text.readlines()
1745
            assert sha_strings(file_lines) == ie.text_sha1
1746
            assert sum(map(len, file_lines)) == ie.text_size
1563.2.18 by Robert Collins
get knit repositories really using knits for text storage.
1747
            w.add_lines(rev_id, previous_revisions, file_lines)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1748
            self.text_count += 1
1749
        else:
1563.2.18 by Robert Collins
get knit repositories really using knits for text storage.
1750
            w.add_lines(rev_id, previous_revisions, [])
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1751
        ie.revision = rev_id
1752
1753
    def _make_order(self):
1754
        """Return a suitable order for importing revisions.
1755
1756
        The order must be such that an revision is imported after all
1757
        its (present) parents.
1758
        """
1759
        todo = set(self.revisions.keys())
1760
        done = self.absent_revisions.copy()
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
1761
        order = []
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1762
        while todo:
1763
            # scan through looking for a revision whose parents
1764
            # are all done
1765
            for rev_id in sorted(list(todo)):
1766
                rev = self.revisions[rev_id]
1767
                parent_ids = set(rev.parent_ids)
1768
                if parent_ids.issubset(done):
1769
                    # can take this one now
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
1770
                    order.append(rev_id)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1771
                    todo.remove(rev_id)
1772
                    done.add(rev_id)
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
1773
        return order
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1774
1775
1776
class ConvertBzrDir5To6(Converter):
1777
    """Converts format 5 bzr dirs to format 6."""
1778
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1779
    def convert(self, to_convert, pb):
1780
        """See Converter.convert()."""
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1781
        self.bzrdir = to_convert
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1782
        self.pb = pb
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1783
        self.pb.note('starting upgrade from format 5 to 6')
1784
        self._convert_to_prefixed()
1785
        return BzrDir.open(self.bzrdir.root_transport.base)
1786
1787
    def _convert_to_prefixed(self):
1608.2.1 by Martin Pool
[merge] Storage filename escaping
1788
        from bzrlib.store import TransportStore
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1789
        self.bzrdir.transport.delete('branch-format')
1790
        for store_name in ["weaves", "revision-store"]:
1608.2.1 by Martin Pool
[merge] Storage filename escaping
1791
            self.pb.note("adding prefixes to %s" % store_name)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1792
            store_transport = self.bzrdir.transport.clone(store_name)
1608.2.1 by Martin Pool
[merge] Storage filename escaping
1793
            store = TransportStore(store_transport, prefixed=True)
1608.1.1 by Martin Pool
[patch] LocalTransport.list_dir should return url-quoted strings (ddaa)
1794
            for urlfilename in store_transport.list_dir('.'):
1685.1.45 by John Arbash Meinel
Moved url functions into bzrlib.urlutils
1795
                filename = urlutils.unescape(urlfilename)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1796
                if (filename.endswith(".weave") or
1797
                    filename.endswith(".gz") or
1798
                    filename.endswith(".sig")):
1799
                    file_id = os.path.splitext(filename)[0]
1800
                else:
1801
                    file_id = filename
1608.2.1 by Martin Pool
[merge] Storage filename escaping
1802
                prefix_dir = store.hash_prefix(file_id)
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1803
                # FIXME keep track of the dirs made RBC 20060121
1804
                try:
1805
                    store_transport.move(filename, prefix_dir + '/' + filename)
1806
                except errors.NoSuchFile: # catches missing dirs strangely enough
1807
                    store_transport.mkdir(prefix_dir)
1808
                    store_transport.move(filename, prefix_dir + '/' + filename)
1809
        self.bzrdir._control_files.put_utf8('branch-format', BzrDirFormat6().get_format_string())
1810
1811
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1812
class ConvertBzrDir6ToMeta(Converter):
1813
    """Converts format 6 bzr dirs to metadirs."""
1814
1815
    def convert(self, to_convert, pb):
1816
        """See Converter.convert()."""
2094.3.5 by John Arbash Meinel
Fix imports to ensure modules are loaded before they are used
1817
        from bzrlib.branch import BzrBranchFormat5
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1818
        self.bzrdir = to_convert
1819
        self.pb = pb
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
1820
        self.count = 0
1821
        self.total = 20 # the steps we know about
1822
        self.garbage_inventories = []
1823
1534.5.13 by Robert Collins
Correct buggy test.
1824
        self.pb.note('starting upgrade from format 6 to metadir')
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
1825
        self.bzrdir._control_files.put_utf8('branch-format', "Converting to format 6")
1826
        # its faster to move specific files around than to open and use the apis...
1827
        # first off, nuke ancestry.weave, it was never used.
1828
        try:
1829
            self.step('Removing ancestry.weave')
1830
            self.bzrdir.transport.delete('ancestry.weave')
1831
        except errors.NoSuchFile:
1832
            pass
1833
        # find out whats there
1834
        self.step('Finding branch files')
1666.1.3 by Robert Collins
Fix and test upgrades from bzrdir 6 over SFTP.
1835
        last_revision = self.bzrdir.open_branch().last_revision()
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
1836
        bzrcontents = self.bzrdir.transport.list_dir('.')
1837
        for name in bzrcontents:
1838
            if name.startswith('basis-inventory.'):
1839
                self.garbage_inventories.append(name)
1840
        # create new directories for repository, working tree and branch
1553.5.79 by Martin Pool
upgrade to metadir should create LockDirs not files
1841
        self.dir_mode = self.bzrdir._control_files._dir_mode
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
1842
        self.file_mode = self.bzrdir._control_files._file_mode
1843
        repository_names = [('inventory.weave', True),
1844
                            ('revision-store', True),
1845
                            ('weaves', True)]
1846
        self.step('Upgrading repository  ')
1553.5.79 by Martin Pool
upgrade to metadir should create LockDirs not files
1847
        self.bzrdir.transport.mkdir('repository', mode=self.dir_mode)
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
1848
        self.make_lock('repository')
1849
        # we hard code the formats here because we are converting into
1850
        # the meta format. The meta format upgrader can take this to a 
1851
        # future format within each component.
2094.3.5 by John Arbash Meinel
Fix imports to ensure modules are loaded before they are used
1852
        self.put_format('repository', _mod_repository.RepositoryFormat7())
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
1853
        for entry in repository_names:
1854
            self.move_entry('repository', entry)
1855
1856
        self.step('Upgrading branch      ')
1553.5.79 by Martin Pool
upgrade to metadir should create LockDirs not files
1857
        self.bzrdir.transport.mkdir('branch', mode=self.dir_mode)
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
1858
        self.make_lock('branch')
2094.3.5 by John Arbash Meinel
Fix imports to ensure modules are loaded before they are used
1859
        self.put_format('branch', BzrBranchFormat5())
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
1860
        branch_files = [('revision-history', True),
1861
                        ('branch-name', True),
1862
                        ('parent', False)]
1863
        for entry in branch_files:
1864
            self.move_entry('branch', entry)
1865
1866
        checkout_files = [('pending-merges', True),
1867
                          ('inventory', True),
1868
                          ('stat-cache', False)]
1959.3.1 by John Arbash Meinel
David Allouche: bzr upgrade should work if there is no working tree
1869
        # If a mandatory checkout file is not present, the branch does not have
1870
        # a functional checkout. Do not create a checkout in the converted
1871
        # branch.
1872
        for name, mandatory in checkout_files:
1873
            if mandatory and name not in bzrcontents:
1874
                has_checkout = False
1875
                break
1876
        else:
1877
            has_checkout = True
1878
        if not has_checkout:
1879
            self.pb.note('No working tree.')
1880
            # If some checkout files are there, we may as well get rid of them.
1881
            for name, mandatory in checkout_files:
1882
                if name in bzrcontents:
1883
                    self.bzrdir.transport.delete(name)
1884
        else:
2123.2.1 by John Arbash Meinel
Fix bug #70716, make bzrlib.bzrdir directly import bzrlib.workingtree
1885
            from bzrlib.workingtree import WorkingTreeFormat3
1959.3.1 by John Arbash Meinel
David Allouche: bzr upgrade should work if there is no working tree
1886
            self.step('Upgrading working tree')
1887
            self.bzrdir.transport.mkdir('checkout', mode=self.dir_mode)
1888
            self.make_lock('checkout')
1889
            self.put_format(
2123.2.1 by John Arbash Meinel
Fix bug #70716, make bzrlib.bzrdir directly import bzrlib.workingtree
1890
                'checkout', WorkingTreeFormat3())
1959.3.1 by John Arbash Meinel
David Allouche: bzr upgrade should work if there is no working tree
1891
            self.bzrdir.transport.delete_multi(
1892
                self.garbage_inventories, self.pb)
1893
            for entry in checkout_files:
1894
                self.move_entry('checkout', entry)
1895
            if last_revision is not None:
1896
                self.bzrdir._control_files.put_utf8(
1897
                    'checkout/last-revision', last_revision)
1898
        self.bzrdir._control_files.put_utf8(
1899
            'branch-format', BzrDirMetaFormat1().get_format_string())
1534.5.10 by Robert Collins
Make upgrade driver unaware of the specific formats in play.
1900
        return BzrDir.open(self.bzrdir.root_transport.base)
1901
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
1902
    def make_lock(self, name):
1903
        """Make a lock for the new control dir name."""
1904
        self.step('Make %s lock' % name)
1996.3.3 by John Arbash Meinel
Shave off another 40ms by demand loading branch and bzrdir
1905
        ld = lockdir.LockDir(self.bzrdir.transport,
1906
                             '%s/lock' % name,
1907
                             file_modebits=self.file_mode,
1908
                             dir_modebits=self.dir_mode)
1553.5.79 by Martin Pool
upgrade to metadir should create LockDirs not files
1909
        ld.create()
1534.5.11 by Robert Collins
Implement upgrades to Metaformat trees.
1910
1911
    def move_entry(self, new_dir, entry):
1912
        """Move then entry name into new_dir."""
1913
        name = entry[0]
1914
        mandatory = entry[1]
1915
        self.step('Moving %s' % name)
1916
        try:
1917
            self.bzrdir.transport.move(name, '%s/%s' % (new_dir, name))
1918
        except errors.NoSuchFile:
1919
            if mandatory:
1920
                raise
1921
1922
    def put_format(self, dirname, format):
1923
        self.bzrdir._control_files.put_utf8('%s/format' % dirname, format.get_format_string())
1924
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.
1925
1926
class ConvertMetaToMeta(Converter):
1927
    """Converts the components of metadirs."""
1928
1929
    def __init__(self, target_format):
1930
        """Create a metadir to metadir converter.
1931
1932
        :param target_format: The final metadir format that is desired.
1933
        """
1934
        self.target_format = target_format
1935
1936
    def convert(self, to_convert, pb):
1937
        """See Converter.convert()."""
1938
        self.bzrdir = to_convert
1939
        self.pb = pb
1940
        self.count = 0
1941
        self.total = 1
1942
        self.step('checking repository format')
1943
        try:
1944
            repo = self.bzrdir.open_repository()
1945
        except errors.NoRepositoryPresent:
1946
            pass
1947
        else:
1948
            if not isinstance(repo._format, self.target_format.repository_format.__class__):
1949
                from bzrlib.repository import CopyConverter
1950
                self.pb.note('starting repository conversion')
1951
                converter = CopyConverter(self.target_format.repository_format)
1952
                converter.convert(repo, pb)
1953
        return to_convert
2204.4.1 by Aaron Bentley
Add 'formats' help topic
1954
1955
2204.4.4 by Aaron Bentley
Use BzrDirFormatInfo to distinguish native and deprecated formats
1956
class BzrDirFormatInfo(object):
1957
1958
    def __init__(self, native, deprecated):
1959
        self.deprecated = deprecated
1960
        self.native = native
1961
1962
2204.4.1 by Aaron Bentley
Add 'formats' help topic
1963
class BzrDirFormatRegistry(registry.Registry):
1964
    """Registry of user-selectable BzrDir subformats.
1965
    
1966
    Differs from BzrDirFormat._control_formats in that it provides sub-formats,
1967
    e.g. BzrDirMeta1 with weave repository.  Also, it's more user-oriented.
1968
    """
1969
2204.4.4 by Aaron Bentley
Use BzrDirFormatInfo to distinguish native and deprecated formats
1970
    def register_metadir(self, key, repo, help, native=True, deprecated=False):
2204.4.1 by Aaron Bentley
Add 'formats' help topic
1971
        """Register a metadir subformat.
1972
        
1973
        repo is the repository format name as a string.
1974
        """
1975
        # This should be expanded to support setting WorkingTree and Branch
1976
        # formats, once BzrDirMetaFormat1 supports that.
2204.4.7 by Aaron Bentley
restore register_lazy, remove register_factory, other updates
1977
        def helper():
2204.4.1 by Aaron Bentley
Add 'formats' help topic
1978
            import bzrlib.repository
1979
            repo_format = getattr(bzrlib.repository, repo)
1980
            bd = BzrDirMetaFormat1()
1981
            bd.repository_format = repo_format()
1982
            return bd
2204.4.4 by Aaron Bentley
Use BzrDirFormatInfo to distinguish native and deprecated formats
1983
        self.register(key, helper, help, native, deprecated)
2204.4.1 by Aaron Bentley
Add 'formats' help topic
1984
2204.4.4 by Aaron Bentley
Use BzrDirFormatInfo to distinguish native and deprecated formats
1985
    def register(self, key, factory, help, native=True, deprecated=False):
2204.4.1 by Aaron Bentley
Add 'formats' help topic
1986
        """Register a BzrDirFormat factory.
1987
        
1988
        The factory must be a callable that takes one parameter: the key.
1989
        It must produce an instance of the BzrDirFormat when called.
1990
1991
        This function mainly exists to prevent the info object from being
1992
        supplied directly.
1993
        """
2204.4.4 by Aaron Bentley
Use BzrDirFormatInfo to distinguish native and deprecated formats
1994
        registry.Registry.register(self, key, factory, help, 
1995
            BzrDirFormatInfo(native, deprecated))
2204.4.1 by Aaron Bentley
Add 'formats' help topic
1996
2204.4.7 by Aaron Bentley
restore register_lazy, remove register_factory, other updates
1997
    def register_lazy(self, key, module_name, member_name, help, native=True,
1998
                      deprecated=False):
1999
        registry.Registry.register_lazy(self, key, module_name, member_name, 
2000
            help, BzrDirFormatInfo(native, deprecated))
2204.4.1 by Aaron Bentley
Add 'formats' help topic
2001
2002
    def set_default(self, key):
2003
        """Set the 'default' key to be a clone of the supplied key.
2004
        
2005
        This method must be called once and only once.
2006
        """
2204.4.4 by Aaron Bentley
Use BzrDirFormatInfo to distinguish native and deprecated formats
2007
        registry.Registry.register(self, 'default', self.get(key), 
2008
            self.get_help(key), info=self.get_info(key))
2204.4.1 by Aaron Bentley
Add 'formats' help topic
2009
2204.4.11 by Aaron Bentley
deprecate Repository.set_default_format, update upgrade tests
2010
    def set_default_repository(self, key):
2011
        """Set the FormatRegistry default and Repository default.
2012
        
2013
        This is a transitional method while Repository.set_default_format
2014
        is deprecated.
2015
        """
2016
        if 'default' in self:
2017
            self.remove('default')
2018
        self.set_default(key)
2019
        format = self.get('default')()
2020
        assert isinstance(format, BzrDirMetaFormat1)
2021
        from bzrlib import repository
2022
        repository.RepositoryFormat._set_default_format(
2023
            format.repository_format)
2024
2204.4.1 by Aaron Bentley
Add 'formats' help topic
2025
    def make_bzrdir(self, key):
2204.4.7 by Aaron Bentley
restore register_lazy, remove register_factory, other updates
2026
        return self.get(key)()
2204.4.1 by Aaron Bentley
Add 'formats' help topic
2027
2028
    def help_topic(self, topic):
2029
        output = textwrap.dedent("""\
2030
            Bazaar directory formats
2031
            ------------------------
2032
2033
            These formats can be used for creating branches, working trees, and
2034
            repositories.
2204.4.2 by Aaron Bentley
Tweak topic appearance
2035
2204.4.1 by Aaron Bentley
Add 'formats' help topic
2036
            """)
2037
        default_help = self.get_help('default')
2038
        help_pairs = []
2039
        for key in self.keys():
2040
            if key == 'default':
2041
                continue
2042
            help = self.get_help(key)
2043
            if help == default_help:
2044
                default_realkey = key
2045
            else:
2046
                help_pairs.append((key, help))
2047
2204.4.4 by Aaron Bentley
Use BzrDirFormatInfo to distinguish native and deprecated formats
2048
        def wrapped(key, help, info):
2049
            if info.native:
2050
                help = '(native) ' + help
2204.4.2 by Aaron Bentley
Tweak topic appearance
2051
            return '  %s:\n%s\n\n' % (key, 
2204.4.1 by Aaron Bentley
Add 'formats' help topic
2052
                    textwrap.fill(help, initial_indent='    ', 
2053
                    subsequent_indent='    '))
2204.4.4 by Aaron Bentley
Use BzrDirFormatInfo to distinguish native and deprecated formats
2054
        output += wrapped('%s/default' % default_realkey, default_help,
2055
                          self.get_info('default'))
2056
        deprecated_pairs = []
2204.4.1 by Aaron Bentley
Add 'formats' help topic
2057
        for key, help in help_pairs:
2204.4.4 by Aaron Bentley
Use BzrDirFormatInfo to distinguish native and deprecated formats
2058
            info = self.get_info(key)
2059
            if info.deprecated:
2060
                deprecated_pairs.append((key, help))
2061
            else:
2062
                output += wrapped(key, help, info)
2063
        if len(deprecated_pairs) > 0:
2064
            output += "Deprecated formats\n------------------\n\n"
2065
            for key, help in deprecated_pairs:
2066
                info = self.get_info(key)
2067
                output += wrapped(key, help, info)
2068
2204.4.1 by Aaron Bentley
Add 'formats' help topic
2069
        return output
2070
2071
2072
format_registry = BzrDirFormatRegistry()
2204.4.7 by Aaron Bentley
restore register_lazy, remove register_factory, other updates
2073
format_registry.register('weave', BzrDirFormat6,
2204.4.4 by Aaron Bentley
Use BzrDirFormatInfo to distinguish native and deprecated formats
2074
    'Pre-0.8 format.  Slower than knit and does not'
2204.4.5 by Aaron Bentley
Punctuation
2075
    ' support checkouts or shared repositories.', deprecated=True)
2204.4.1 by Aaron Bentley
Add 'formats' help topic
2076
format_registry.register_metadir('knit', 'RepositoryFormatKnit1',
2204.4.4 by Aaron Bentley
Use BzrDirFormatInfo to distinguish native and deprecated formats
2077
    'Format using knits.  Recommended.')
2204.4.1 by Aaron Bentley
Add 'formats' help topic
2078
format_registry.set_default('knit')
2079
format_registry.register_metadir('metaweave', 'RepositoryFormat7',
2204.4.4 by Aaron Bentley
Use BzrDirFormatInfo to distinguish native and deprecated formats
2080
    'Transitional format in 0.8.  Slower than knit.',
2081
    deprecated=True)
2204.4.1 by Aaron Bentley
Add 'formats' help topic
2082
format_registry.register_metadir('experimental-knit2', 'RepositoryFormatKnit2',
2204.4.4 by Aaron Bentley
Use BzrDirFormatInfo to distinguish native and deprecated formats
2083
    'Experimental successor to knit.  Use at your own risk.')