1
# Copyright (C) 2005, 2006 Canonical Ltd
 
 
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.
 
 
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.
 
 
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
 
 
17
"""BzrDir logic. The BzrDir is the basic control directory used by bzr.
 
 
19
At format 7 this was split out into Branch, Repository and Checkout control
 
 
23
from copy import deepcopy
 
 
25
from cStringIO import StringIO
 
 
26
from unittest import TestSuite
 
 
29
import bzrlib.errors as errors
 
 
30
from bzrlib.lockable_files import LockableFiles, TransportLock
 
 
31
from bzrlib.lockdir import LockDir
 
 
32
from bzrlib.osutils import safe_unicode
 
 
33
from bzrlib.osutils import (
 
 
40
from bzrlib.store.revision.text import TextRevisionStore
 
 
41
from bzrlib.store.text import TextStore
 
 
42
from bzrlib.store.versioned import WeaveStore
 
 
43
from bzrlib.symbol_versioning import *
 
 
44
from bzrlib.trace import mutter
 
 
45
from bzrlib.transactions import WriteTransaction
 
 
46
from bzrlib.transport import get_transport, urlunescape
 
 
47
from bzrlib.transport.local import LocalTransport
 
 
48
from bzrlib.weave import Weave
 
 
49
from bzrlib.xml4 import serializer_v4
 
 
50
from bzrlib.xml5 import serializer_v5
 
 
54
    """A .bzr control diretory.
 
 
56
    BzrDir instances let you create or open any of the things that can be
 
 
57
    found within .bzr - checkouts, branches and repositories.
 
 
60
        the transport which this bzr dir is rooted at (i.e. file:///.../.bzr/)
 
 
62
        a transport connected to the directory this bzr was opened from.
 
 
65
    def can_convert_format(self):
 
 
66
        """Return true if this bzrdir is one whose format we can convert from."""
 
 
70
    def _check_supported(format, allow_unsupported):
 
 
71
        """Check whether format is a supported format.
 
 
73
        If allow_unsupported is True, this is a no-op.
 
 
75
        if not allow_unsupported and not format.is_supported():
 
 
76
            # see open_downlevel to open legacy branches.
 
 
77
            raise errors.UnsupportedFormatError(
 
 
78
                    'sorry, format %s not supported' % format,
 
 
79
                    ['use a different bzr version',
 
 
80
                     'or remove the .bzr directory'
 
 
81
                     ' and "bzr init" again'])
 
 
83
    def clone(self, url, revision_id=None, basis=None, force_new_repo=False):
 
 
84
        """Clone this bzrdir and its contents to url verbatim.
 
 
86
        If urls last component does not exist, it will be created.
 
 
88
        if revision_id is not None, then the clone operation may tune
 
 
89
            itself to download less data.
 
 
90
        :param force_new_repo: Do not use a shared repository for the target 
 
 
91
                               even if one is available.
 
 
94
        basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
 
 
95
        result = self._format.initialize(url)
 
 
97
            local_repo = self.find_repository()
 
 
98
        except errors.NoRepositoryPresent:
 
 
101
            # may need to copy content in
 
 
103
                local_repo.clone(result, revision_id=revision_id, basis=basis_repo)
 
 
106
                    result_repo = result.find_repository()
 
 
107
                    # fetch content this dir needs.
 
 
109
                        # XXX FIXME RBC 20060214 need tests for this when the basis
 
 
111
                        result_repo.fetch(basis_repo, revision_id=revision_id)
 
 
112
                    result_repo.fetch(local_repo, revision_id=revision_id)
 
 
113
                except errors.NoRepositoryPresent:
 
 
114
                    # needed to make one anyway.
 
 
115
                    local_repo.clone(result, revision_id=revision_id, basis=basis_repo)
 
 
116
        # 1 if there is a branch present
 
 
117
        #   make sure its content is available in the target repository
 
 
120
            self.open_branch().clone(result, revision_id=revision_id)
 
 
121
        except errors.NotBranchError:
 
 
124
            self.open_workingtree().clone(result, basis=basis_tree)
 
 
125
        except (errors.NoWorkingTree, errors.NotLocalUrl):
 
 
129
    def _get_basis_components(self, basis):
 
 
130
        """Retrieve the basis components that are available at basis."""
 
 
132
            return None, None, None
 
 
134
            basis_tree = basis.open_workingtree()
 
 
135
            basis_branch = basis_tree.branch
 
 
136
            basis_repo = basis_branch.repository
 
 
137
        except (errors.NoWorkingTree, errors.NotLocalUrl):
 
 
140
                basis_branch = basis.open_branch()
 
 
141
                basis_repo = basis_branch.repository
 
 
142
            except errors.NotBranchError:
 
 
145
                    basis_repo = basis.open_repository()
 
 
146
                except errors.NoRepositoryPresent:
 
 
148
        return basis_repo, basis_branch, basis_tree
 
 
150
    def _make_tail(self, url):
 
 
151
        segments = url.split('/')
 
 
152
        if segments and segments[-1] not in ('', '.'):
 
 
153
            parent = '/'.join(segments[:-1])
 
 
154
            t = bzrlib.transport.get_transport(parent)
 
 
156
                t.mkdir(segments[-1])
 
 
157
            except errors.FileExists:
 
 
161
    def create(cls, base):
 
 
162
        """Create a new BzrDir at the url 'base'.
 
 
164
        This will call the current default formats initialize with base
 
 
165
        as the only parameter.
 
 
167
        If you need a specific format, consider creating an instance
 
 
168
        of that and calling initialize().
 
 
170
        if cls is not BzrDir:
 
 
171
            raise AssertionError("BzrDir.create always creates the default format, "
 
 
172
                    "not one of %r" % cls)
 
 
173
        segments = base.split('/')
 
 
174
        if segments and segments[-1] not in ('', '.'):
 
 
175
            parent = '/'.join(segments[:-1])
 
 
176
            t = bzrlib.transport.get_transport(parent)
 
 
178
                t.mkdir(segments[-1])
 
 
179
            except errors.FileExists:
 
 
181
        return BzrDirFormat.get_default_format().initialize(safe_unicode(base))
 
 
183
    def create_branch(self):
 
 
184
        """Create a branch in this BzrDir.
 
 
186
        The bzrdirs format will control what branch format is created.
 
 
187
        For more control see BranchFormatXX.create(a_bzrdir).
 
 
189
        raise NotImplementedError(self.create_branch)
 
 
192
    def create_branch_and_repo(base, force_new_repo=False):
 
 
193
        """Create a new BzrDir, Branch and Repository at the url 'base'.
 
 
195
        This will use the current default BzrDirFormat, and use whatever 
 
 
196
        repository format that that uses via bzrdir.create_branch and
 
 
197
        create_repository. If a shared repository is available that is used
 
 
200
        The created Branch object is returned.
 
 
202
        :param base: The URL to create the branch at.
 
 
203
        :param force_new_repo: If True a new repository is always created.
 
 
205
        bzrdir = BzrDir.create(base)
 
 
206
        bzrdir._find_or_create_repository(force_new_repo)
 
 
207
        return bzrdir.create_branch()
 
 
209
    def _find_or_create_repository(self, force_new_repo):
 
 
210
        """Create a new repository if needed, returning the repository."""
 
 
212
            return self.create_repository()
 
 
214
            return self.find_repository()
 
 
215
        except errors.NoRepositoryPresent:
 
 
216
            return self.create_repository()
 
 
219
    def create_branch_convenience(base, force_new_repo=False, force_new_tree=None):
 
 
220
        """Create a new BzrDir, Branch and Repository at the url 'base'.
 
 
222
        This is a convenience function - it will use an existing repository
 
 
223
        if possible, can be told explicitly whether to create a working tree or
 
 
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. Whatever repository is used, its tree creation policy
 
 
232
        The created Branch object is returned.
 
 
233
        If a working tree cannot be made due to base not being a file:// url,
 
 
234
        no error is raised unless force_new_tree is True, in which case no 
 
 
235
        data is created on disk and NotLocalUrl is raised.
 
 
237
        :param base: The URL to create the branch at.
 
 
238
        :param force_new_repo: If True a new repository is always created.
 
 
239
        :param force_new_tree: If True or False force creation of a tree or 
 
 
240
                               prevent such creation respectively.
 
 
243
            # check for non local urls
 
 
244
            t = get_transport(safe_unicode(base))
 
 
245
            if not isinstance(t, LocalTransport):
 
 
246
                raise errors.NotLocalUrl(base)
 
 
247
        bzrdir = BzrDir.create(base)
 
 
248
        repo = bzrdir._find_or_create_repository(force_new_repo)
 
 
249
        result = bzrdir.create_branch()
 
 
250
        if force_new_tree or (repo.make_working_trees() and 
 
 
251
                              force_new_tree is None):
 
 
253
                bzrdir.create_workingtree()
 
 
254
            except errors.NotLocalUrl:
 
 
259
    def create_repository(base, shared=False):
 
 
260
        """Create a new BzrDir and Repository at the url 'base'.
 
 
262
        This will use the current default BzrDirFormat, and use whatever 
 
 
263
        repository format that that uses for bzrdirformat.create_repository.
 
 
265
        ;param shared: Create a shared repository rather than a standalone
 
 
267
        The Repository object is returned.
 
 
269
        This must be overridden as an instance method in child classes, where
 
 
270
        it should take no parameters and construct whatever repository format
 
 
271
        that child class desires.
 
 
273
        bzrdir = BzrDir.create(base)
 
 
274
        return bzrdir.create_repository()
 
 
277
    def create_standalone_workingtree(base):
 
 
278
        """Create a new BzrDir, WorkingTree, Branch and Repository at 'base'.
 
 
280
        'base' must be a local path or a file:// url.
 
 
282
        This will use the current default BzrDirFormat, and use whatever 
 
 
283
        repository format that that uses for bzrdirformat.create_workingtree,
 
 
284
        create_branch and create_repository.
 
 
286
        The WorkingTree object is returned.
 
 
288
        t = get_transport(safe_unicode(base))
 
 
289
        if not isinstance(t, LocalTransport):
 
 
290
            raise errors.NotLocalUrl(base)
 
 
291
        bzrdir = BzrDir.create_branch_and_repo(safe_unicode(base),
 
 
292
                                               force_new_repo=True).bzrdir
 
 
293
        return bzrdir.create_workingtree()
 
 
295
    def create_workingtree(self, revision_id=None):
 
 
296
        """Create a working tree at this BzrDir.
 
 
298
        revision_id: create it as of this revision id.
 
 
300
        raise NotImplementedError(self.create_workingtree)
 
 
302
    def find_repository(self):
 
 
303
        """Find the repository that should be used for a_bzrdir.
 
 
305
        This does not require a branch as we use it to find the repo for
 
 
306
        new branches as well as to hook existing branches up to their
 
 
310
            return self.open_repository()
 
 
311
        except errors.NoRepositoryPresent:
 
 
313
        next_transport = self.root_transport.clone('..')
 
 
316
                found_bzrdir = BzrDir.open_containing_from_transport(
 
 
318
            except errors.NotBranchError:
 
 
319
                raise errors.NoRepositoryPresent(self)
 
 
321
                repository = found_bzrdir.open_repository()
 
 
322
            except errors.NoRepositoryPresent:
 
 
323
                next_transport = found_bzrdir.root_transport.clone('..')
 
 
325
            if ((found_bzrdir.root_transport.base == 
 
 
326
                 self.root_transport.base) or repository.is_shared()):
 
 
329
                raise errors.NoRepositoryPresent(self)
 
 
330
        raise errors.NoRepositoryPresent(self)
 
 
332
    def get_branch_transport(self, branch_format):
 
 
333
        """Get the transport for use by branch format in this BzrDir.
 
 
335
        Note that bzr dirs that do not support format strings will raise
 
 
336
        IncompatibleFormat if the branch format they are given has
 
 
337
        a format string, and vice verca.
 
 
339
        If branch_format is None, the transport is returned with no 
 
 
340
        checking. if it is not None, then the returned transport is
 
 
341
        guaranteed to point to an existing directory ready for use.
 
 
343
        raise NotImplementedError(self.get_branch_transport)
 
 
345
    def get_repository_transport(self, repository_format):
 
 
346
        """Get the transport for use by repository format in this BzrDir.
 
 
348
        Note that bzr dirs that do not support format strings will raise
 
 
349
        IncompatibleFormat if the repository format they are given has
 
 
350
        a format string, and vice verca.
 
 
352
        If repository_format is None, the transport is returned with no 
 
 
353
        checking. if it is not None, then the returned transport is
 
 
354
        guaranteed to point to an existing directory ready for use.
 
 
356
        raise NotImplementedError(self.get_repository_transport)
 
 
358
    def get_workingtree_transport(self, tree_format):
 
 
359
        """Get the transport for use by workingtree format in this BzrDir.
 
 
361
        Note that bzr dirs that do not support format strings will raise
 
 
362
        IncompatibleFormat if the workingtree format they are given has
 
 
363
        a format string, and vice verca.
 
 
365
        If workingtree_format is None, the transport is returned with no 
 
 
366
        checking. if it is not None, then the returned transport is
 
 
367
        guaranteed to point to an existing directory ready for use.
 
 
369
        raise NotImplementedError(self.get_workingtree_transport)
 
 
371
    def __init__(self, _transport, _format):
 
 
372
        """Initialize a Bzr control dir object.
 
 
374
        Only really common logic should reside here, concrete classes should be
 
 
375
        made with varying behaviours.
 
 
377
        :param _format: the format that is creating this BzrDir instance.
 
 
378
        :param _transport: the transport this dir is based at.
 
 
380
        self._format = _format
 
 
381
        self.transport = _transport.clone('.bzr')
 
 
382
        self.root_transport = _transport
 
 
384
    def needs_format_conversion(self, format=None):
 
 
385
        """Return true if this bzrdir needs convert_format run on it.
 
 
387
        For instance, if the repository format is out of date but the 
 
 
388
        branch and working tree are not, this should return True.
 
 
390
        :param format: Optional parameter indicating a specific desired
 
 
391
                       format we plan to arrive at.
 
 
393
        raise NotImplementedError(self.needs_format_conversion)
 
 
396
    def open_unsupported(base):
 
 
397
        """Open a branch which is not supported."""
 
 
398
        return BzrDir.open(base, _unsupported=True)
 
 
401
    def open(base, _unsupported=False):
 
 
402
        """Open an existing bzrdir, rooted at 'base' (url)
 
 
404
        _unsupported is a private parameter to the BzrDir class.
 
 
406
        t = get_transport(base)
 
 
407
        mutter("trying to open %r with transport %r", base, t)
 
 
408
        format = BzrDirFormat.find_format(t)
 
 
409
        BzrDir._check_supported(format, _unsupported)
 
 
410
        return format.open(t, _found=True)
 
 
412
    def open_branch(self, unsupported=False):
 
 
413
        """Open the branch object at this BzrDir if one is present.
 
 
415
        If unsupported is True, then no longer supported branch formats can
 
 
418
        TODO: static convenience version of this?
 
 
420
        raise NotImplementedError(self.open_branch)
 
 
423
    def open_containing(url):
 
 
424
        """Open an existing branch which contains url.
 
 
426
        :param url: url to search from.
 
 
427
        See open_containing_from_transport for more detail.
 
 
429
        return BzrDir.open_containing_from_transport(get_transport(url))
 
 
432
    def open_containing_from_transport(a_transport):
 
 
433
        """Open an existing branch which contains a_transport.base
 
 
435
        This probes for a branch at a_transport, and searches upwards from there.
 
 
437
        Basically we keep looking up until we find the control directory or
 
 
438
        run into the root.  If there isn't one, raises NotBranchError.
 
 
439
        If there is one and it is either an unrecognised format or an unsupported 
 
 
440
        format, UnknownFormatError or UnsupportedFormatError are raised.
 
 
441
        If there is one, it is returned, along with the unused portion of url.
 
 
443
        # this gets the normalised url back. I.e. '.' -> the full path.
 
 
444
        url = a_transport.base
 
 
447
                format = BzrDirFormat.find_format(a_transport)
 
 
448
                BzrDir._check_supported(format, False)
 
 
449
                return format.open(a_transport), a_transport.relpath(url)
 
 
450
            except errors.NotBranchError, e:
 
 
451
                mutter('not a branch in: %r %s', a_transport.base, e)
 
 
452
            new_t = a_transport.clone('..')
 
 
453
            if new_t.base == a_transport.base:
 
 
454
                # reached the root, whatever that may be
 
 
455
                raise errors.NotBranchError(path=url)
 
 
458
    def open_repository(self, _unsupported=False):
 
 
459
        """Open the repository object at this BzrDir if one is present.
 
 
461
        This will not follow the Branch object pointer - its strictly a direct
 
 
462
        open facility. Most client code should use open_branch().repository to
 
 
465
        _unsupported is a private parameter, not part of the api.
 
 
466
        TODO: static convenience version of this?
 
 
468
        raise NotImplementedError(self.open_repository)
 
 
470
    def open_workingtree(self, _unsupported=False):
 
 
471
        """Open the workingtree object at this BzrDir if one is present.
 
 
473
        TODO: static convenience version of this?
 
 
475
        raise NotImplementedError(self.open_workingtree)
 
 
477
    def sprout(self, url, revision_id=None, basis=None, force_new_repo=False):
 
 
478
        """Create a copy of this bzrdir prepared for use as a new line of
 
 
481
        If urls last component does not exist, it will be created.
 
 
483
        Attributes related to the identity of the source branch like
 
 
484
        branch nickname will be cleaned, a working tree is created
 
 
485
        whether one existed before or not; and a local branch is always
 
 
488
        if revision_id is not None, then the clone operation may tune
 
 
489
            itself to download less data.
 
 
492
        result = self._format.initialize(url)
 
 
493
        basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
 
 
495
            source_branch = self.open_branch()
 
 
496
            source_repository = source_branch.repository
 
 
497
        except errors.NotBranchError:
 
 
500
                source_repository = self.open_repository()
 
 
501
            except errors.NoRepositoryPresent:
 
 
502
                # copy the entire basis one if there is one
 
 
503
                # but there is no repository.
 
 
504
                source_repository = basis_repo
 
 
509
                result_repo = result.find_repository()
 
 
510
            except errors.NoRepositoryPresent:
 
 
512
        if source_repository is None and result_repo is not None:
 
 
514
        elif source_repository is None and result_repo is None:
 
 
515
            # no repo available, make a new one
 
 
516
            result.create_repository()
 
 
517
        elif source_repository is not None and result_repo is None:
 
 
518
            # have soure, and want to make a new target repo
 
 
519
            source_repository.clone(result,
 
 
520
                                    revision_id=revision_id,
 
 
523
            # fetch needed content into target.
 
 
525
                # XXX FIXME RBC 20060214 need tests for this when the basis
 
 
527
                result_repo.fetch(basis_repo, revision_id=revision_id)
 
 
528
            result_repo.fetch(source_repository, revision_id=revision_id)
 
 
529
        if source_branch is not None:
 
 
530
            source_branch.sprout(result, revision_id=revision_id)
 
 
532
            result.create_branch()
 
 
533
        result.create_workingtree()
 
 
537
class BzrDirPreSplitOut(BzrDir):
 
 
538
    """A common class for the all-in-one formats."""
 
 
540
    def __init__(self, _transport, _format):
 
 
541
        """See BzrDir.__init__."""
 
 
542
        super(BzrDirPreSplitOut, self).__init__(_transport, _format)
 
 
543
        assert self._format._lock_class == TransportLock
 
 
544
        assert self._format._lock_file_name == 'branch-lock'
 
 
545
        self._control_files = LockableFiles(self.get_branch_transport(None),
 
 
546
                                            self._format._lock_file_name,
 
 
547
                                            self._format._lock_class)
 
 
549
    def clone(self, url, revision_id=None, basis=None, force_new_repo=False):
 
 
550
        """See BzrDir.clone()."""
 
 
551
        from bzrlib.workingtree import WorkingTreeFormat2
 
 
553
        result = self._format.initialize(url, _cloning=True)
 
 
554
        basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
 
 
555
        self.open_repository().clone(result, revision_id=revision_id, basis=basis_repo)
 
 
556
        self.open_branch().clone(result, revision_id=revision_id)
 
 
558
            self.open_workingtree().clone(result, basis=basis_tree)
 
 
559
        except errors.NotLocalUrl:
 
 
560
            # make a new one, this format always has to have one.
 
 
562
                WorkingTreeFormat2().initialize(result)
 
 
563
            except errors.NotLocalUrl:
 
 
564
                # but we canot do it for remote trees.
 
 
568
    def create_branch(self):
 
 
569
        """See BzrDir.create_branch."""
 
 
570
        return self.open_branch()
 
 
572
    def create_repository(self, shared=False):
 
 
573
        """See BzrDir.create_repository."""
 
 
575
            raise errors.IncompatibleFormat('shared repository', self._format)
 
 
576
        return self.open_repository()
 
 
578
    def create_workingtree(self, revision_id=None):
 
 
579
        """See BzrDir.create_workingtree."""
 
 
580
        # this looks buggy but is not -really-
 
 
581
        # clone and sprout will have set the revision_id
 
 
582
        # and that will have set it for us, its only
 
 
583
        # specific uses of create_workingtree in isolation
 
 
584
        # that can do wonky stuff here, and that only
 
 
585
        # happens for creating checkouts, which cannot be 
 
 
586
        # done on this format anyway. So - acceptable wart.
 
 
587
        result = self.open_workingtree()
 
 
588
        if revision_id is not None:
 
 
589
            result.set_last_revision(revision_id)
 
 
592
    def get_branch_transport(self, branch_format):
 
 
593
        """See BzrDir.get_branch_transport()."""
 
 
594
        if branch_format is None:
 
 
595
            return self.transport
 
 
597
            branch_format.get_format_string()
 
 
598
        except NotImplementedError:
 
 
599
            return self.transport
 
 
600
        raise errors.IncompatibleFormat(branch_format, self._format)
 
 
602
    def get_repository_transport(self, repository_format):
 
 
603
        """See BzrDir.get_repository_transport()."""
 
 
604
        if repository_format is None:
 
 
605
            return self.transport
 
 
607
            repository_format.get_format_string()
 
 
608
        except NotImplementedError:
 
 
609
            return self.transport
 
 
610
        raise errors.IncompatibleFormat(repository_format, self._format)
 
 
612
    def get_workingtree_transport(self, workingtree_format):
 
 
613
        """See BzrDir.get_workingtree_transport()."""
 
 
614
        if workingtree_format is None:
 
 
615
            return self.transport
 
 
617
            workingtree_format.get_format_string()
 
 
618
        except NotImplementedError:
 
 
619
            return self.transport
 
 
620
        raise errors.IncompatibleFormat(workingtree_format, self._format)
 
 
622
    def needs_format_conversion(self, format=None):
 
 
623
        """See BzrDir.needs_format_conversion()."""
 
 
624
        # if the format is not the same as the system default,
 
 
625
        # an upgrade is needed.
 
 
627
            format = BzrDirFormat.get_default_format()
 
 
628
        return not isinstance(self._format, format.__class__)
 
 
630
    def open_branch(self, unsupported=False):
 
 
631
        """See BzrDir.open_branch."""
 
 
632
        from bzrlib.branch import BzrBranchFormat4
 
 
633
        format = BzrBranchFormat4()
 
 
634
        self._check_supported(format, unsupported)
 
 
635
        return format.open(self, _found=True)
 
 
637
    def sprout(self, url, revision_id=None, basis=None):
 
 
638
        """See BzrDir.sprout()."""
 
 
639
        from bzrlib.workingtree import WorkingTreeFormat2
 
 
641
        result = self._format.initialize(url, _cloning=True)
 
 
642
        basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
 
 
644
            self.open_repository().clone(result, revision_id=revision_id, basis=basis_repo)
 
 
645
        except errors.NoRepositoryPresent:
 
 
648
            self.open_branch().sprout(result, revision_id=revision_id)
 
 
649
        except errors.NotBranchError:
 
 
651
        # we always want a working tree
 
 
652
        WorkingTreeFormat2().initialize(result)
 
 
656
class BzrDir4(BzrDirPreSplitOut):
 
 
657
    """A .bzr version 4 control object.
 
 
659
    This is a deprecated format and may be removed after sept 2006.
 
 
662
    def create_repository(self, shared=False):
 
 
663
        """See BzrDir.create_repository."""
 
 
664
        return self._format.repository_format.initialize(self, shared)
 
 
666
    def needs_format_conversion(self, format=None):
 
 
667
        """Format 4 dirs are always in need of conversion."""
 
 
670
    def open_repository(self):
 
 
671
        """See BzrDir.open_repository."""
 
 
672
        from bzrlib.repository import RepositoryFormat4
 
 
673
        return RepositoryFormat4().open(self, _found=True)
 
 
676
class BzrDir5(BzrDirPreSplitOut):
 
 
677
    """A .bzr version 5 control object.
 
 
679
    This is a deprecated format and may be removed after sept 2006.
 
 
682
    def open_repository(self):
 
 
683
        """See BzrDir.open_repository."""
 
 
684
        from bzrlib.repository import RepositoryFormat5
 
 
685
        return RepositoryFormat5().open(self, _found=True)
 
 
687
    def open_workingtree(self, _unsupported=False):
 
 
688
        """See BzrDir.create_workingtree."""
 
 
689
        from bzrlib.workingtree import WorkingTreeFormat2
 
 
690
        return WorkingTreeFormat2().open(self, _found=True)
 
 
693
class BzrDir6(BzrDirPreSplitOut):
 
 
694
    """A .bzr version 6 control object.
 
 
696
    This is a deprecated format and may be removed after sept 2006.
 
 
699
    def open_repository(self):
 
 
700
        """See BzrDir.open_repository."""
 
 
701
        from bzrlib.repository import RepositoryFormat6
 
 
702
        return RepositoryFormat6().open(self, _found=True)
 
 
704
    def open_workingtree(self, _unsupported=False):
 
 
705
        """See BzrDir.create_workingtree."""
 
 
706
        from bzrlib.workingtree import WorkingTreeFormat2
 
 
707
        return WorkingTreeFormat2().open(self, _found=True)
 
 
710
class BzrDirMeta1(BzrDir):
 
 
711
    """A .bzr meta version 1 control object.
 
 
713
    This is the first control object where the 
 
 
714
    individual aspects are really split out: there are separate repository,
 
 
715
    workingtree and branch subdirectories and any subset of the three can be
 
 
716
    present within a BzrDir.
 
 
719
    def can_convert_format(self):
 
 
720
        """See BzrDir.can_convert_format()."""
 
 
723
    def create_branch(self):
 
 
724
        """See BzrDir.create_branch."""
 
 
725
        from bzrlib.branch import BranchFormat
 
 
726
        return BranchFormat.get_default_format().initialize(self)
 
 
728
    def create_repository(self, shared=False):
 
 
729
        """See BzrDir.create_repository."""
 
 
730
        return self._format.repository_format.initialize(self, shared)
 
 
732
    def create_workingtree(self, revision_id=None):
 
 
733
        """See BzrDir.create_workingtree."""
 
 
734
        from bzrlib.workingtree import WorkingTreeFormat
 
 
735
        return WorkingTreeFormat.get_default_format().initialize(self, revision_id)
 
 
737
    def get_branch_transport(self, branch_format):
 
 
738
        """See BzrDir.get_branch_transport()."""
 
 
739
        if branch_format is None:
 
 
740
            return self.transport.clone('branch')
 
 
742
            branch_format.get_format_string()
 
 
743
        except NotImplementedError:
 
 
744
            raise errors.IncompatibleFormat(branch_format, self._format)
 
 
746
            self.transport.mkdir('branch')
 
 
747
        except errors.FileExists:
 
 
749
        return self.transport.clone('branch')
 
 
751
    def get_repository_transport(self, repository_format):
 
 
752
        """See BzrDir.get_repository_transport()."""
 
 
753
        if repository_format is None:
 
 
754
            return self.transport.clone('repository')
 
 
756
            repository_format.get_format_string()
 
 
757
        except NotImplementedError:
 
 
758
            raise errors.IncompatibleFormat(repository_format, self._format)
 
 
760
            self.transport.mkdir('repository')
 
 
761
        except errors.FileExists:
 
 
763
        return self.transport.clone('repository')
 
 
765
    def get_workingtree_transport(self, workingtree_format):
 
 
766
        """See BzrDir.get_workingtree_transport()."""
 
 
767
        if workingtree_format is None:
 
 
768
            return self.transport.clone('checkout')
 
 
770
            workingtree_format.get_format_string()
 
 
771
        except NotImplementedError:
 
 
772
            raise errors.IncompatibleFormat(workingtree_format, self._format)
 
 
774
            self.transport.mkdir('checkout')
 
 
775
        except errors.FileExists:
 
 
777
        return self.transport.clone('checkout')
 
 
779
    def needs_format_conversion(self, format=None):
 
 
780
        """See BzrDir.needs_format_conversion()."""
 
 
782
            format = BzrDirFormat.get_default_format()
 
 
783
        if not isinstance(self._format, format.__class__):
 
 
784
            # it is not a meta dir format, conversion is needed.
 
 
786
        # we might want to push this down to the repository?
 
 
788
            if not isinstance(self.open_repository()._format,
 
 
789
                              format.repository_format.__class__):
 
 
790
                # the repository needs an upgrade.
 
 
792
        except errors.NoRepositoryPresent:
 
 
794
        # currently there are no other possible conversions for meta1 formats.
 
 
797
    def open_branch(self, unsupported=False):
 
 
798
        """See BzrDir.open_branch."""
 
 
799
        from bzrlib.branch import BranchFormat
 
 
800
        format = BranchFormat.find_format(self)
 
 
801
        self._check_supported(format, unsupported)
 
 
802
        return format.open(self, _found=True)
 
 
804
    def open_repository(self, unsupported=False):
 
 
805
        """See BzrDir.open_repository."""
 
 
806
        from bzrlib.repository import RepositoryFormat
 
 
807
        format = RepositoryFormat.find_format(self)
 
 
808
        self._check_supported(format, unsupported)
 
 
809
        return format.open(self, _found=True)
 
 
811
    def open_workingtree(self, unsupported=False):
 
 
812
        """See BzrDir.open_workingtree."""
 
 
813
        from bzrlib.workingtree import WorkingTreeFormat
 
 
814
        format = WorkingTreeFormat.find_format(self)
 
 
815
        self._check_supported(format, unsupported)
 
 
816
        return format.open(self, _found=True)
 
 
819
class BzrDirFormat(object):
 
 
820
    """An encapsulation of the initialization and open routines for a format.
 
 
822
    Formats provide three things:
 
 
823
     * An initialization routine,
 
 
827
    Formats are placed in an dict by their format string for reference 
 
 
828
    during bzrdir opening. These should be subclasses of BzrDirFormat
 
 
831
    Once a format is deprecated, just deprecate the initialize and open
 
 
832
    methods on the format class. Do not deprecate the object, as the 
 
 
833
    object will be created every system load.
 
 
836
    _default_format = None
 
 
837
    """The default format used for new .bzr dirs."""
 
 
840
    """The known formats."""
 
 
842
    _lock_file_name = 'branch-lock'
 
 
844
    # _lock_class must be set in subclasses to the lock type, typ.
 
 
845
    # TransportLock or LockDir
 
 
848
    def find_format(klass, transport):
 
 
849
        """Return the format registered for URL."""
 
 
851
            format_string = transport.get(".bzr/branch-format").read()
 
 
852
            return klass._formats[format_string]
 
 
853
        except errors.NoSuchFile:
 
 
854
            raise errors.NotBranchError(path=transport.base)
 
 
856
            raise errors.UnknownFormatError(format_string)
 
 
859
    def get_default_format(klass):
 
 
860
        """Return the current default format."""
 
 
861
        return klass._default_format
 
 
863
    def get_format_string(self):
 
 
864
        """Return the ASCII format string that identifies this format."""
 
 
865
        raise NotImplementedError(self.get_format_string)
 
 
867
    def get_converter(self, format=None):
 
 
868
        """Return the converter to use to convert bzrdirs needing converts.
 
 
870
        This returns a bzrlib.bzrdir.Converter object.
 
 
872
        This should return the best upgrader to step this format towards the
 
 
873
        current default format. In the case of plugins we can/shouold provide
 
 
874
        some means for them to extend the range of returnable converters.
 
 
876
        :param format: Optional format to override the default foramt of the 
 
 
879
        raise NotImplementedError(self.get_converter)
 
 
881
    def initialize(self, url):
 
 
882
        """Create a bzr control dir at this url and return an opened copy."""
 
 
883
        # Since we don't have a .bzr directory, inherit the
 
 
884
        # mode from the root directory
 
 
885
        t = get_transport(url)
 
 
886
        temp_control = LockableFiles(t, '', TransportLock)
 
 
887
        temp_control._transport.mkdir('.bzr',
 
 
888
                                      # FIXME: RBC 20060121 dont peek under
 
 
890
                                      mode=temp_control._dir_mode)
 
 
891
        file_mode = temp_control._file_mode
 
 
893
        mutter('created control directory in ' + t.base)
 
 
894
        control = t.clone('.bzr')
 
 
895
        utf8_files = [('README', 
 
 
896
                       "This is a Bazaar-NG control directory.\n"
 
 
897
                       "Do not change any files in this directory.\n"),
 
 
898
                      ('branch-format', self.get_format_string()),
 
 
900
        # NB: no need to escape relative paths that are url safe.
 
 
901
        control_files = LockableFiles(control, self._lock_file_name, self._lock_class)
 
 
902
        control_files.create_lock()
 
 
903
        control_files.lock_write()
 
 
905
            for file, content in utf8_files:
 
 
906
                control_files.put_utf8(file, content)
 
 
908
            control_files.unlock()
 
 
909
        return self.open(t, _found=True)
 
 
911
    def is_supported(self):
 
 
912
        """Is this format supported?
 
 
914
        Supported formats must be initializable and openable.
 
 
915
        Unsupported formats may not support initialization or committing or 
 
 
916
        some other features depending on the reason for not being supported.
 
 
920
    def open(self, transport, _found=False):
 
 
921
        """Return an instance of this format for the dir transport points at.
 
 
923
        _found is a private parameter, do not use it.
 
 
926
            assert isinstance(BzrDirFormat.find_format(transport),
 
 
928
        return self._open(transport)
 
 
930
    def _open(self, transport):
 
 
931
        """Template method helper for opening BzrDirectories.
 
 
933
        This performs the actual open and any additional logic or parameter
 
 
936
        raise NotImplementedError(self._open)
 
 
939
    def register_format(klass, format):
 
 
940
        klass._formats[format.get_format_string()] = format
 
 
943
    def set_default_format(klass, format):
 
 
944
        klass._default_format = format
 
 
947
        return self.get_format_string()[:-1]
 
 
950
    def unregister_format(klass, format):
 
 
951
        assert klass._formats[format.get_format_string()] is format
 
 
952
        del klass._formats[format.get_format_string()]
 
 
955
class BzrDirFormat4(BzrDirFormat):
 
 
958
    This format is a combined format for working tree, branch and repository.
 
 
960
     - Format 1 working trees [always]
 
 
961
     - Format 4 branches [always]
 
 
962
     - Format 4 repositories [always]
 
 
964
    This format is deprecated: it indexes texts using a text it which is
 
 
965
    removed in format 5; write support for this format has been removed.
 
 
968
    _lock_class = TransportLock
 
 
970
    def get_format_string(self):
 
 
971
        """See BzrDirFormat.get_format_string()."""
 
 
972
        return "Bazaar-NG branch, format 0.0.4\n"
 
 
974
    def get_converter(self, format=None):
 
 
975
        """See BzrDirFormat.get_converter()."""
 
 
976
        # there is one and only one upgrade path here.
 
 
977
        return ConvertBzrDir4To5()
 
 
979
    def initialize(self, url):
 
 
980
        """Format 4 branches cannot be created."""
 
 
981
        raise errors.UninitializableFormat(self)
 
 
983
    def is_supported(self):
 
 
984
        """Format 4 is not supported.
 
 
986
        It is not supported because the model changed from 4 to 5 and the
 
 
987
        conversion logic is expensive - so doing it on the fly was not 
 
 
992
    def _open(self, transport):
 
 
993
        """See BzrDirFormat._open."""
 
 
994
        return BzrDir4(transport, self)
 
 
996
    def __return_repository_format(self):
 
 
997
        """Circular import protection."""
 
 
998
        from bzrlib.repository import RepositoryFormat4
 
 
999
        return RepositoryFormat4(self)
 
 
1000
    repository_format = property(__return_repository_format)
 
 
1003
class BzrDirFormat5(BzrDirFormat):
 
 
1004
    """Bzr control format 5.
 
 
1006
    This format is a combined format for working tree, branch and repository.
 
 
1008
     - Format 2 working trees [always] 
 
 
1009
     - Format 4 branches [always] 
 
 
1010
     - Format 5 repositories [always]
 
 
1011
       Unhashed stores in the repository.
 
 
1014
    _lock_class = TransportLock
 
 
1016
    def get_format_string(self):
 
 
1017
        """See BzrDirFormat.get_format_string()."""
 
 
1018
        return "Bazaar-NG branch, format 5\n"
 
 
1020
    def get_converter(self, format=None):
 
 
1021
        """See BzrDirFormat.get_converter()."""
 
 
1022
        # there is one and only one upgrade path here.
 
 
1023
        return ConvertBzrDir5To6()
 
 
1025
    def initialize(self, url, _cloning=False):
 
 
1026
        """Format 5 dirs always have working tree, branch and repository.
 
 
1028
        Except when they are being cloned.
 
 
1030
        from bzrlib.branch import BzrBranchFormat4
 
 
1031
        from bzrlib.repository import RepositoryFormat5
 
 
1032
        from bzrlib.workingtree import WorkingTreeFormat2
 
 
1033
        result = super(BzrDirFormat5, self).initialize(url)
 
 
1034
        RepositoryFormat5().initialize(result, _internal=True)
 
 
1036
            BzrBranchFormat4().initialize(result)
 
 
1037
            WorkingTreeFormat2().initialize(result)
 
 
1040
    def _open(self, transport):
 
 
1041
        """See BzrDirFormat._open."""
 
 
1042
        return BzrDir5(transport, self)
 
 
1044
    def __return_repository_format(self):
 
 
1045
        """Circular import protection."""
 
 
1046
        from bzrlib.repository import RepositoryFormat5
 
 
1047
        return RepositoryFormat5(self)
 
 
1048
    repository_format = property(__return_repository_format)
 
 
1051
class BzrDirFormat6(BzrDirFormat):
 
 
1052
    """Bzr control format 6.
 
 
1054
    This format is a combined format for working tree, branch and repository.
 
 
1056
     - Format 2 working trees [always] 
 
 
1057
     - Format 4 branches [always] 
 
 
1058
     - Format 6 repositories [always]
 
 
1061
    _lock_class = TransportLock
 
 
1063
    def get_format_string(self):
 
 
1064
        """See BzrDirFormat.get_format_string()."""
 
 
1065
        return "Bazaar-NG branch, format 6\n"
 
 
1067
    def get_converter(self, format=None):
 
 
1068
        """See BzrDirFormat.get_converter()."""
 
 
1069
        # there is one and only one upgrade path here.
 
 
1070
        return ConvertBzrDir6ToMeta()
 
 
1072
    def initialize(self, url, _cloning=False):
 
 
1073
        """Format 6 dirs always have working tree, branch and repository.
 
 
1075
        Except when they are being cloned.
 
 
1077
        from bzrlib.branch import BzrBranchFormat4
 
 
1078
        from bzrlib.repository import RepositoryFormat6
 
 
1079
        from bzrlib.workingtree import WorkingTreeFormat2
 
 
1080
        result = super(BzrDirFormat6, self).initialize(url)
 
 
1081
        RepositoryFormat6().initialize(result, _internal=True)
 
 
1083
            BzrBranchFormat4().initialize(result)
 
 
1085
                WorkingTreeFormat2().initialize(result)
 
 
1086
            except errors.NotLocalUrl:
 
 
1087
                # emulate pre-check behaviour for working tree and silently 
 
 
1092
    def _open(self, transport):
 
 
1093
        """See BzrDirFormat._open."""
 
 
1094
        return BzrDir6(transport, self)
 
 
1096
    def __return_repository_format(self):
 
 
1097
        """Circular import protection."""
 
 
1098
        from bzrlib.repository import RepositoryFormat6
 
 
1099
        return RepositoryFormat6(self)
 
 
1100
    repository_format = property(__return_repository_format)
 
 
1103
class BzrDirMetaFormat1(BzrDirFormat):
 
 
1104
    """Bzr meta control format 1
 
 
1106
    This is the first format with split out working tree, branch and repository
 
 
1109
     - Format 3 working trees [optional]
 
 
1110
     - Format 5 branches [optional]
 
 
1111
     - Format 7 repositories [optional]
 
 
1114
    _lock_class = LockDir
 
 
1116
    def get_converter(self, format=None):
 
 
1117
        """See BzrDirFormat.get_converter()."""
 
 
1119
            format = BzrDirFormat.get_default_format()
 
 
1120
        if not isinstance(self, format.__class__):
 
 
1121
            # converting away from metadir is not implemented
 
 
1122
            raise NotImplementedError(self.get_converter)
 
 
1123
        return ConvertMetaToMeta(format)
 
 
1125
    def get_format_string(self):
 
 
1126
        """See BzrDirFormat.get_format_string()."""
 
 
1127
        return "Bazaar-NG meta directory, format 1\n"
 
 
1129
    def _open(self, transport):
 
 
1130
        """See BzrDirFormat._open."""
 
 
1131
        return BzrDirMeta1(transport, self)
 
 
1133
    def __return_repository_format(self):
 
 
1134
        """Circular import protection."""
 
 
1135
        if getattr(self, '_repository_format', None):
 
 
1136
            return self._repository_format
 
 
1137
        from bzrlib.repository import RepositoryFormat
 
 
1138
        return RepositoryFormat.get_default_format()
 
 
1140
    def __set_repository_format(self, value):
 
 
1141
        """Allow changint the repository format for metadir formats."""
 
 
1142
        self._repository_format = value
 
 
1144
    repository_format = property(__return_repository_format, __set_repository_format)
 
 
1147
BzrDirFormat.register_format(BzrDirFormat4())
 
 
1148
BzrDirFormat.register_format(BzrDirFormat5())
 
 
1149
BzrDirFormat.register_format(BzrDirMetaFormat1())
 
 
1150
__default_format = BzrDirFormat6()
 
 
1151
BzrDirFormat.register_format(__default_format)
 
 
1152
BzrDirFormat.set_default_format(__default_format)
 
 
1155
class BzrDirTestProviderAdapter(object):
 
 
1156
    """A tool to generate a suite testing multiple bzrdir formats at once.
 
 
1158
    This is done by copying the test once for each transport and injecting
 
 
1159
    the transport_server, transport_readonly_server, and bzrdir_format
 
 
1160
    classes into each copy. Each copy is also given a new id() to make it
 
 
1164
    def __init__(self, transport_server, transport_readonly_server, formats):
 
 
1165
        self._transport_server = transport_server
 
 
1166
        self._transport_readonly_server = transport_readonly_server
 
 
1167
        self._formats = formats
 
 
1169
    def adapt(self, test):
 
 
1170
        result = TestSuite()
 
 
1171
        for format in self._formats:
 
 
1172
            new_test = deepcopy(test)
 
 
1173
            new_test.transport_server = self._transport_server
 
 
1174
            new_test.transport_readonly_server = self._transport_readonly_server
 
 
1175
            new_test.bzrdir_format = format
 
 
1176
            def make_new_test_id():
 
 
1177
                new_id = "%s(%s)" % (new_test.id(), format.__class__.__name__)
 
 
1178
                return lambda: new_id
 
 
1179
            new_test.id = make_new_test_id()
 
 
1180
            result.addTest(new_test)
 
 
1184
class ScratchDir(BzrDir6):
 
 
1185
    """Special test class: a bzrdir that cleans up itself..
 
 
1187
    >>> d = ScratchDir()
 
 
1188
    >>> base = d.transport.base
 
 
1191
    >>> b.transport.__del__()
 
 
1196
    def __init__(self, files=[], dirs=[], transport=None):
 
 
1197
        """Make a test branch.
 
 
1199
        This creates a temporary directory and runs init-tree in it.
 
 
1201
        If any files are listed, they are created in the working copy.
 
 
1203
        if transport is None:
 
 
1204
            transport = bzrlib.transport.local.ScratchTransport()
 
 
1205
            # local import for scope restriction
 
 
1206
            BzrDirFormat6().initialize(transport.base)
 
 
1207
            super(ScratchDir, self).__init__(transport, BzrDirFormat6())
 
 
1208
            self.create_repository()
 
 
1209
            self.create_branch()
 
 
1210
            self.create_workingtree()
 
 
1212
            super(ScratchDir, self).__init__(transport, BzrDirFormat6())
 
 
1214
        # BzrBranch creates a clone to .bzr and then forgets about the
 
 
1215
        # original transport. A ScratchTransport() deletes itself and
 
 
1216
        # everything underneath it when it goes away, so we need to
 
 
1217
        # grab a local copy to prevent that from happening
 
 
1218
        self._transport = transport
 
 
1221
            self._transport.mkdir(d)
 
 
1224
            self._transport.put(f, 'content of %s' % f)
 
 
1228
        >>> orig = ScratchDir(files=["file1", "file2"])
 
 
1229
        >>> os.listdir(orig.base)
 
 
1230
        [u'.bzr', u'file1', u'file2']
 
 
1231
        >>> clone = orig.clone()
 
 
1232
        >>> if os.name != 'nt':
 
 
1233
        ...   os.path.samefile(orig.base, clone.base)
 
 
1235
        ...   orig.base == clone.base
 
 
1238
        >>> os.listdir(clone.base)
 
 
1239
        [u'.bzr', u'file1', u'file2']
 
 
1241
        from shutil import copytree
 
 
1242
        from bzrlib.osutils import mkdtemp
 
 
1245
        copytree(self.base, base, symlinks=True)
 
 
1247
            transport=bzrlib.transport.local.ScratchTransport(base))
 
 
1250
class Converter(object):
 
 
1251
    """Converts a disk format object from one format to another."""
 
 
1253
    def convert(self, to_convert, pb):
 
 
1254
        """Perform the conversion of to_convert, giving feedback via pb.
 
 
1256
        :param to_convert: The disk object to convert.
 
 
1257
        :param pb: a progress bar to use for progress information.
 
 
1260
    def step(self, message):
 
 
1261
        """Update the pb by a step."""
 
 
1263
        self.pb.update(message, self.count, self.total)
 
 
1266
class ConvertBzrDir4To5(Converter):
 
 
1267
    """Converts format 4 bzr dirs to format 5."""
 
 
1270
        super(ConvertBzrDir4To5, self).__init__()
 
 
1271
        self.converted_revs = set()
 
 
1272
        self.absent_revisions = set()
 
 
1276
    def convert(self, to_convert, pb):
 
 
1277
        """See Converter.convert()."""
 
 
1278
        self.bzrdir = to_convert
 
 
1280
        self.pb.note('starting upgrade from format 4 to 5')
 
 
1281
        if isinstance(self.bzrdir.transport, LocalTransport):
 
 
1282
            self.bzrdir.get_workingtree_transport(None).delete('stat-cache')
 
 
1283
        self._convert_to_weaves()
 
 
1284
        return BzrDir.open(self.bzrdir.root_transport.base)
 
 
1286
    def _convert_to_weaves(self):
 
 
1287
        self.pb.note('note: upgrade may be faster if all store files are ungzipped first')
 
 
1290
            stat = self.bzrdir.transport.stat('weaves')
 
 
1291
            if not S_ISDIR(stat.st_mode):
 
 
1292
                self.bzrdir.transport.delete('weaves')
 
 
1293
                self.bzrdir.transport.mkdir('weaves')
 
 
1294
        except errors.NoSuchFile:
 
 
1295
            self.bzrdir.transport.mkdir('weaves')
 
 
1296
        # deliberately not a WeaveFile as we want to build it up slowly.
 
 
1297
        self.inv_weave = Weave('inventory')
 
 
1298
        # holds in-memory weaves for all files
 
 
1299
        self.text_weaves = {}
 
 
1300
        self.bzrdir.transport.delete('branch-format')
 
 
1301
        self.branch = self.bzrdir.open_branch()
 
 
1302
        self._convert_working_inv()
 
 
1303
        rev_history = self.branch.revision_history()
 
 
1304
        # to_read is a stack holding the revisions we still need to process;
 
 
1305
        # appending to it adds new highest-priority revisions
 
 
1306
        self.known_revisions = set(rev_history)
 
 
1307
        self.to_read = rev_history[-1:]
 
 
1309
            rev_id = self.to_read.pop()
 
 
1310
            if (rev_id not in self.revisions
 
 
1311
                and rev_id not in self.absent_revisions):
 
 
1312
                self._load_one_rev(rev_id)
 
 
1314
        to_import = self._make_order()
 
 
1315
        for i, rev_id in enumerate(to_import):
 
 
1316
            self.pb.update('converting revision', i, len(to_import))
 
 
1317
            self._convert_one_rev(rev_id)
 
 
1319
        self._write_all_weaves()
 
 
1320
        self._write_all_revs()
 
 
1321
        self.pb.note('upgraded to weaves:')
 
 
1322
        self.pb.note('  %6d revisions and inventories', len(self.revisions))
 
 
1323
        self.pb.note('  %6d revisions not present', len(self.absent_revisions))
 
 
1324
        self.pb.note('  %6d texts', self.text_count)
 
 
1325
        self._cleanup_spare_files_after_format4()
 
 
1326
        self.branch.control_files.put_utf8('branch-format', BzrDirFormat5().get_format_string())
 
 
1328
    def _cleanup_spare_files_after_format4(self):
 
 
1329
        # FIXME working tree upgrade foo.
 
 
1330
        for n in 'merged-patches', 'pending-merged-patches':
 
 
1332
                ## assert os.path.getsize(p) == 0
 
 
1333
                self.bzrdir.transport.delete(n)
 
 
1334
            except errors.NoSuchFile:
 
 
1336
        self.bzrdir.transport.delete_tree('inventory-store')
 
 
1337
        self.bzrdir.transport.delete_tree('text-store')
 
 
1339
    def _convert_working_inv(self):
 
 
1340
        inv = serializer_v4.read_inventory(self.branch.control_files.get('inventory'))
 
 
1341
        new_inv_xml = serializer_v5.write_inventory_to_string(inv)
 
 
1342
        # FIXME inventory is a working tree change.
 
 
1343
        self.branch.control_files.put('inventory', new_inv_xml)
 
 
1345
    def _write_all_weaves(self):
 
 
1346
        controlweaves = WeaveStore(self.bzrdir.transport, prefixed=False)
 
 
1347
        weave_transport = self.bzrdir.transport.clone('weaves')
 
 
1348
        weaves = WeaveStore(weave_transport, prefixed=False)
 
 
1349
        transaction = WriteTransaction()
 
 
1353
            for file_id, file_weave in self.text_weaves.items():
 
 
1354
                self.pb.update('writing weave', i, len(self.text_weaves))
 
 
1355
                weaves._put_weave(file_id, file_weave, transaction)
 
 
1357
            self.pb.update('inventory', 0, 1)
 
 
1358
            controlweaves._put_weave('inventory', self.inv_weave, transaction)
 
 
1359
            self.pb.update('inventory', 1, 1)
 
 
1363
    def _write_all_revs(self):
 
 
1364
        """Write all revisions out in new form."""
 
 
1365
        self.bzrdir.transport.delete_tree('revision-store')
 
 
1366
        self.bzrdir.transport.mkdir('revision-store')
 
 
1367
        revision_transport = self.bzrdir.transport.clone('revision-store')
 
 
1369
        _revision_store = TextRevisionStore(TextStore(revision_transport,
 
 
1373
            transaction = bzrlib.transactions.WriteTransaction()
 
 
1374
            for i, rev_id in enumerate(self.converted_revs):
 
 
1375
                self.pb.update('write revision', i, len(self.converted_revs))
 
 
1376
                _revision_store.add_revision(self.revisions[rev_id], transaction)
 
 
1380
    def _load_one_rev(self, rev_id):
 
 
1381
        """Load a revision object into memory.
 
 
1383
        Any parents not either loaded or abandoned get queued to be
 
 
1385
        self.pb.update('loading revision',
 
 
1386
                       len(self.revisions),
 
 
1387
                       len(self.known_revisions))
 
 
1388
        if not self.branch.repository.has_revision(rev_id):
 
 
1390
            self.pb.note('revision {%s} not present in branch; '
 
 
1391
                         'will be converted as a ghost',
 
 
1393
            self.absent_revisions.add(rev_id)
 
 
1395
            rev = self.branch.repository._revision_store.get_revision(rev_id,
 
 
1396
                self.branch.repository.get_transaction())
 
 
1397
            for parent_id in rev.parent_ids:
 
 
1398
                self.known_revisions.add(parent_id)
 
 
1399
                self.to_read.append(parent_id)
 
 
1400
            self.revisions[rev_id] = rev
 
 
1402
    def _load_old_inventory(self, rev_id):
 
 
1403
        assert rev_id not in self.converted_revs
 
 
1404
        old_inv_xml = self.branch.repository.inventory_store.get(rev_id).read()
 
 
1405
        inv = serializer_v4.read_inventory_from_string(old_inv_xml)
 
 
1406
        rev = self.revisions[rev_id]
 
 
1407
        if rev.inventory_sha1:
 
 
1408
            assert rev.inventory_sha1 == sha_string(old_inv_xml), \
 
 
1409
                'inventory sha mismatch for {%s}' % rev_id
 
 
1412
    def _load_updated_inventory(self, rev_id):
 
 
1413
        assert rev_id in self.converted_revs
 
 
1414
        inv_xml = self.inv_weave.get_text(rev_id)
 
 
1415
        inv = serializer_v5.read_inventory_from_string(inv_xml)
 
 
1418
    def _convert_one_rev(self, rev_id):
 
 
1419
        """Convert revision and all referenced objects to new format."""
 
 
1420
        rev = self.revisions[rev_id]
 
 
1421
        inv = self._load_old_inventory(rev_id)
 
 
1422
        present_parents = [p for p in rev.parent_ids
 
 
1423
                           if p not in self.absent_revisions]
 
 
1424
        self._convert_revision_contents(rev, inv, present_parents)
 
 
1425
        self._store_new_weave(rev, inv, present_parents)
 
 
1426
        self.converted_revs.add(rev_id)
 
 
1428
    def _store_new_weave(self, rev, inv, present_parents):
 
 
1429
        # the XML is now updated with text versions
 
 
1433
                if ie.kind == 'root_directory':
 
 
1435
                assert hasattr(ie, 'revision'), \
 
 
1436
                    'no revision on {%s} in {%s}' % \
 
 
1437
                    (file_id, rev.revision_id)
 
 
1438
        new_inv_xml = serializer_v5.write_inventory_to_string(inv)
 
 
1439
        new_inv_sha1 = sha_string(new_inv_xml)
 
 
1440
        self.inv_weave.add_lines(rev.revision_id, 
 
 
1442
                                 new_inv_xml.splitlines(True))
 
 
1443
        rev.inventory_sha1 = new_inv_sha1
 
 
1445
    def _convert_revision_contents(self, rev, inv, present_parents):
 
 
1446
        """Convert all the files within a revision.
 
 
1448
        Also upgrade the inventory to refer to the text revision ids."""
 
 
1449
        rev_id = rev.revision_id
 
 
1450
        mutter('converting texts of revision {%s}',
 
 
1452
        parent_invs = map(self._load_updated_inventory, present_parents)
 
 
1455
            self._convert_file_version(rev, ie, parent_invs)
 
 
1457
    def _convert_file_version(self, rev, ie, parent_invs):
 
 
1458
        """Convert one version of one file.
 
 
1460
        The file needs to be added into the weave if it is a merge
 
 
1461
        of >=2 parents or if it's changed from its parent.
 
 
1463
        if ie.kind == 'root_directory':
 
 
1465
        file_id = ie.file_id
 
 
1466
        rev_id = rev.revision_id
 
 
1467
        w = self.text_weaves.get(file_id)
 
 
1470
            self.text_weaves[file_id] = w
 
 
1471
        text_changed = False
 
 
1472
        previous_entries = ie.find_previous_heads(parent_invs, w)
 
 
1473
        for old_revision in previous_entries:
 
 
1474
                # if this fails, its a ghost ?
 
 
1475
                assert old_revision in self.converted_revs 
 
 
1476
        self.snapshot_ie(previous_entries, ie, w, rev_id)
 
 
1478
        assert getattr(ie, 'revision', None) is not None
 
 
1480
    def snapshot_ie(self, previous_revisions, ie, w, rev_id):
 
 
1481
        # TODO: convert this logic, which is ~= snapshot to
 
 
1482
        # a call to:. This needs the path figured out. rather than a work_tree
 
 
1483
        # a v4 revision_tree can be given, or something that looks enough like
 
 
1484
        # one to give the file content to the entry if it needs it.
 
 
1485
        # and we need something that looks like a weave store for snapshot to 
 
 
1487
        #ie.snapshot(rev, PATH, previous_revisions, REVISION_TREE, InMemoryWeaveStore(self.text_weaves))
 
 
1488
        if len(previous_revisions) == 1:
 
 
1489
            previous_ie = previous_revisions.values()[0]
 
 
1490
            if ie._unchanged(previous_ie):
 
 
1491
                ie.revision = previous_ie.revision
 
 
1494
            text = self.branch.repository.text_store.get(ie.text_id)
 
 
1495
            file_lines = text.readlines()
 
 
1496
            assert sha_strings(file_lines) == ie.text_sha1
 
 
1497
            assert sum(map(len, file_lines)) == ie.text_size
 
 
1498
            w.add_lines(rev_id, previous_revisions, file_lines)
 
 
1499
            self.text_count += 1
 
 
1501
            w.add_lines(rev_id, previous_revisions, [])
 
 
1502
        ie.revision = rev_id
 
 
1504
    def _make_order(self):
 
 
1505
        """Return a suitable order for importing revisions.
 
 
1507
        The order must be such that an revision is imported after all
 
 
1508
        its (present) parents.
 
 
1510
        todo = set(self.revisions.keys())
 
 
1511
        done = self.absent_revisions.copy()
 
 
1514
            # scan through looking for a revision whose parents
 
 
1516
            for rev_id in sorted(list(todo)):
 
 
1517
                rev = self.revisions[rev_id]
 
 
1518
                parent_ids = set(rev.parent_ids)
 
 
1519
                if parent_ids.issubset(done):
 
 
1520
                    # can take this one now
 
 
1521
                    order.append(rev_id)
 
 
1527
class ConvertBzrDir5To6(Converter):
 
 
1528
    """Converts format 5 bzr dirs to format 6."""
 
 
1530
    def convert(self, to_convert, pb):
 
 
1531
        """See Converter.convert()."""
 
 
1532
        self.bzrdir = to_convert
 
 
1534
        self.pb.note('starting upgrade from format 5 to 6')
 
 
1535
        self._convert_to_prefixed()
 
 
1536
        return BzrDir.open(self.bzrdir.root_transport.base)
 
 
1538
    def _convert_to_prefixed(self):
 
 
1539
        from bzrlib.store import hash_prefix
 
 
1540
        self.bzrdir.transport.delete('branch-format')
 
 
1541
        for store_name in ["weaves", "revision-store"]:
 
 
1542
            self.pb.note("adding prefixes to %s" % store_name) 
 
 
1543
            store_transport = self.bzrdir.transport.clone(store_name)
 
 
1544
            for urlfilename in store_transport.list_dir('.'):
 
 
1545
                filename = urlunescape(urlfilename)
 
 
1546
                if (filename.endswith(".weave") or
 
 
1547
                    filename.endswith(".gz") or
 
 
1548
                    filename.endswith(".sig")):
 
 
1549
                    file_id = os.path.splitext(filename)[0]
 
 
1552
                prefix_dir = hash_prefix(file_id)
 
 
1553
                # FIXME keep track of the dirs made RBC 20060121
 
 
1555
                    store_transport.move(filename, prefix_dir + '/' + filename)
 
 
1556
                except errors.NoSuchFile: # catches missing dirs strangely enough
 
 
1557
                    store_transport.mkdir(prefix_dir)
 
 
1558
                    store_transport.move(filename, prefix_dir + '/' + filename)
 
 
1559
        self.bzrdir._control_files.put_utf8('branch-format', BzrDirFormat6().get_format_string())
 
 
1562
class ConvertBzrDir6ToMeta(Converter):
 
 
1563
    """Converts format 6 bzr dirs to metadirs."""
 
 
1565
    def convert(self, to_convert, pb):
 
 
1566
        """See Converter.convert()."""
 
 
1567
        self.bzrdir = to_convert
 
 
1570
        self.total = 20 # the steps we know about
 
 
1571
        self.garbage_inventories = []
 
 
1573
        self.pb.note('starting upgrade from format 6 to metadir')
 
 
1574
        self.bzrdir._control_files.put_utf8('branch-format', "Converting to format 6")
 
 
1575
        # its faster to move specific files around than to open and use the apis...
 
 
1576
        # first off, nuke ancestry.weave, it was never used.
 
 
1578
            self.step('Removing ancestry.weave')
 
 
1579
            self.bzrdir.transport.delete('ancestry.weave')
 
 
1580
        except errors.NoSuchFile:
 
 
1582
        # find out whats there
 
 
1583
        self.step('Finding branch files')
 
 
1584
        last_revision = self.bzrdir.open_workingtree().last_revision()
 
 
1585
        bzrcontents = self.bzrdir.transport.list_dir('.')
 
 
1586
        for name in bzrcontents:
 
 
1587
            if name.startswith('basis-inventory.'):
 
 
1588
                self.garbage_inventories.append(name)
 
 
1589
        # create new directories for repository, working tree and branch
 
 
1590
        self.dir_mode = self.bzrdir._control_files._dir_mode
 
 
1591
        self.file_mode = self.bzrdir._control_files._file_mode
 
 
1592
        repository_names = [('inventory.weave', True),
 
 
1593
                            ('revision-store', True),
 
 
1595
        self.step('Upgrading repository  ')
 
 
1596
        self.bzrdir.transport.mkdir('repository', mode=self.dir_mode)
 
 
1597
        self.make_lock('repository')
 
 
1598
        # we hard code the formats here because we are converting into
 
 
1599
        # the meta format. The meta format upgrader can take this to a 
 
 
1600
        # future format within each component.
 
 
1601
        self.put_format('repository', bzrlib.repository.RepositoryFormat7())
 
 
1602
        for entry in repository_names:
 
 
1603
            self.move_entry('repository', entry)
 
 
1605
        self.step('Upgrading branch      ')
 
 
1606
        self.bzrdir.transport.mkdir('branch', mode=self.dir_mode)
 
 
1607
        self.make_lock('branch')
 
 
1608
        self.put_format('branch', bzrlib.branch.BzrBranchFormat5())
 
 
1609
        branch_files = [('revision-history', True),
 
 
1610
                        ('branch-name', True),
 
 
1612
        for entry in branch_files:
 
 
1613
            self.move_entry('branch', entry)
 
 
1615
        self.step('Upgrading working tree')
 
 
1616
        self.bzrdir.transport.mkdir('checkout', mode=self.dir_mode)
 
 
1617
        self.make_lock('checkout')
 
 
1618
        self.put_format('checkout', bzrlib.workingtree.WorkingTreeFormat3())
 
 
1619
        self.bzrdir.transport.delete_multi(self.garbage_inventories, self.pb)
 
 
1620
        checkout_files = [('pending-merges', True),
 
 
1621
                          ('inventory', True),
 
 
1622
                          ('stat-cache', False)]
 
 
1623
        for entry in checkout_files:
 
 
1624
            self.move_entry('checkout', entry)
 
 
1625
        if last_revision is not None:
 
 
1626
            self.bzrdir._control_files.put_utf8('checkout/last-revision',
 
 
1628
        self.bzrdir._control_files.put_utf8('branch-format', BzrDirMetaFormat1().get_format_string())
 
 
1629
        return BzrDir.open(self.bzrdir.root_transport.base)
 
 
1631
    def make_lock(self, name):
 
 
1632
        """Make a lock for the new control dir name."""
 
 
1633
        self.step('Make %s lock' % name)
 
 
1634
        ld = LockDir(self.bzrdir.transport, 
 
 
1636
                     file_modebits=self.file_mode,
 
 
1637
                     dir_modebits=self.dir_mode)
 
 
1640
    def move_entry(self, new_dir, entry):
 
 
1641
        """Move then entry name into new_dir."""
 
 
1643
        mandatory = entry[1]
 
 
1644
        self.step('Moving %s' % name)
 
 
1646
            self.bzrdir.transport.move(name, '%s/%s' % (new_dir, name))
 
 
1647
        except errors.NoSuchFile:
 
 
1651
    def put_format(self, dirname, format):
 
 
1652
        self.bzrdir._control_files.put_utf8('%s/format' % dirname, format.get_format_string())
 
 
1655
class ConvertMetaToMeta(Converter):
 
 
1656
    """Converts the components of metadirs."""
 
 
1658
    def __init__(self, target_format):
 
 
1659
        """Create a metadir to metadir converter.
 
 
1661
        :param target_format: The final metadir format that is desired.
 
 
1663
        self.target_format = target_format
 
 
1665
    def convert(self, to_convert, pb):
 
 
1666
        """See Converter.convert()."""
 
 
1667
        self.bzrdir = to_convert
 
 
1671
        self.step('checking repository format')
 
 
1673
            repo = self.bzrdir.open_repository()
 
 
1674
        except errors.NoRepositoryPresent:
 
 
1677
            if not isinstance(repo._format, self.target_format.repository_format.__class__):
 
 
1678
                from bzrlib.repository import CopyConverter
 
 
1679
                self.pb.note('starting repository conversion')
 
 
1680
                converter = CopyConverter(self.target_format.repository_format)
 
 
1681
                converter.convert(repo, pb)