/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

Merge from bzr.dev

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