~bzr-pqm/bzr/bzr.dev

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