/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/bzrdir.py

  • Committer: Aaron Bentley
  • Date: 2007-01-02 20:15:07 UTC
  • mto: (1551.9.35 Aaron's mergeable stuff)
  • mto: This revision was merged to the branch mainline in revision 2237.
  • Revision ID: abentley@panoramicfeedback.com-20070102201507-zej9hrf5cxxjkypj
Get ls working on branches

Show diffs side-by-side

added added

removed removed

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