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