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