/brz/remove-bazaar

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

« back to all changes in this revision

Viewing changes to bzrlib/bzrdir.py

  • Committer: Andrew Bennetts
  • Date: 2006-09-13 04:48:33 UTC
  • mto: (1752.2.82 remote bzrdir)
  • mto: This revision was merged to the branch mainline in revision 2015.
  • Revision ID: andrew.bennetts@canonical.com-20060913044833-6d54576583f4d7bd
Add a doc on "Running a Bazaar Server" (Robert Collins, Andrew Bennetts)

Show diffs side-by-side

added added

removed removed

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