/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

Update test to use changes_from

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