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.
 
 
66
        """Invoke break_lock on the first object in the bzrdir.
 
 
68
        If there is a tree, the tree is opened and break_lock() called.
 
 
69
        Otherwise, branch is tried, and finally repository.
 
 
72
            thing_to_unlock = self.open_workingtree()
 
 
73
        except (errors.NotLocalUrl, errors.NoWorkingTree):
 
 
75
                thing_to_unlock = self.open_branch()
 
 
76
            except errors.NotBranchError:
 
 
78
                    thing_to_unlock = self.open_repository()
 
 
79
                except errors.NoRepositoryPresent:
 
 
81
        thing_to_unlock.break_lock()
 
 
83
    def can_convert_format(self):
 
 
84
        """Return true if this bzrdir is one whose format we can convert from."""
 
 
88
    def _check_supported(format, allow_unsupported):
 
 
89
        """Check whether format is a supported format.
 
 
91
        If allow_unsupported is True, this is a no-op.
 
 
93
        if not allow_unsupported and not format.is_supported():
 
 
94
            # see open_downlevel to open legacy branches.
 
 
95
            raise errors.UnsupportedFormatError(
 
 
96
                    'sorry, format %s not supported' % format,
 
 
97
                    ['use a different bzr version',
 
 
98
                     'or remove the .bzr directory'
 
 
99
                     ' and "bzr init" again'])
 
 
101
    def clone(self, url, revision_id=None, basis=None, force_new_repo=False):
 
 
102
        """Clone this bzrdir and its contents to url verbatim.
 
 
104
        If urls last component does not exist, it will be created.
 
 
106
        if revision_id is not None, then the clone operation may tune
 
 
107
            itself to download less data.
 
 
108
        :param force_new_repo: Do not use a shared repository for the target 
 
 
109
                               even if one is available.
 
 
112
        basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
 
 
113
        result = self._format.initialize(url)
 
 
115
            local_repo = self.find_repository()
 
 
116
        except errors.NoRepositoryPresent:
 
 
119
            # may need to copy content in
 
 
121
                local_repo.clone(result, revision_id=revision_id, basis=basis_repo)
 
 
124
                    result_repo = result.find_repository()
 
 
125
                    # fetch content this dir needs.
 
 
127
                        # XXX FIXME RBC 20060214 need tests for this when the basis
 
 
129
                        result_repo.fetch(basis_repo, revision_id=revision_id)
 
 
130
                    result_repo.fetch(local_repo, revision_id=revision_id)
 
 
131
                except errors.NoRepositoryPresent:
 
 
132
                    # needed to make one anyway.
 
 
133
                    local_repo.clone(result, revision_id=revision_id, basis=basis_repo)
 
 
134
        # 1 if there is a branch present
 
 
135
        #   make sure its content is available in the target repository
 
 
138
            self.open_branch().clone(result, revision_id=revision_id)
 
 
139
        except errors.NotBranchError:
 
 
142
            self.open_workingtree().clone(result, basis=basis_tree)
 
 
143
        except (errors.NoWorkingTree, errors.NotLocalUrl):
 
 
147
    def _get_basis_components(self, basis):
 
 
148
        """Retrieve the basis components that are available at basis."""
 
 
150
            return None, None, None
 
 
152
            basis_tree = basis.open_workingtree()
 
 
153
            basis_branch = basis_tree.branch
 
 
154
            basis_repo = basis_branch.repository
 
 
155
        except (errors.NoWorkingTree, errors.NotLocalUrl):
 
 
158
                basis_branch = basis.open_branch()
 
 
159
                basis_repo = basis_branch.repository
 
 
160
            except errors.NotBranchError:
 
 
163
                    basis_repo = basis.open_repository()
 
 
164
                except errors.NoRepositoryPresent:
 
 
166
        return basis_repo, basis_branch, basis_tree
 
 
168
    def _make_tail(self, url):
 
 
169
        segments = url.split('/')
 
 
170
        if segments and segments[-1] not in ('', '.'):
 
 
171
            parent = '/'.join(segments[:-1])
 
 
172
            t = bzrlib.transport.get_transport(parent)
 
 
174
                t.mkdir(segments[-1])
 
 
175
            except errors.FileExists:
 
 
179
    def create(cls, base):
 
 
180
        """Create a new BzrDir at the url 'base'.
 
 
182
        This will call the current default formats initialize with base
 
 
183
        as the only parameter.
 
 
185
        If you need a specific format, consider creating an instance
 
 
186
        of that and calling initialize().
 
 
188
        if cls is not BzrDir:
 
 
189
            raise AssertionError("BzrDir.create always creates the default format, "
 
 
190
                    "not one of %r" % cls)
 
 
191
        segments = base.split('/')
 
 
192
        if segments and segments[-1] not in ('', '.'):
 
 
193
            parent = '/'.join(segments[:-1])
 
 
194
            t = bzrlib.transport.get_transport(parent)
 
 
196
                t.mkdir(segments[-1])
 
 
197
            except errors.FileExists:
 
 
199
        return BzrDirFormat.get_default_format().initialize(safe_unicode(base))
 
 
201
    def create_branch(self):
 
 
202
        """Create a branch in this BzrDir.
 
 
204
        The bzrdirs format will control what branch format is created.
 
 
205
        For more control see BranchFormatXX.create(a_bzrdir).
 
 
207
        raise NotImplementedError(self.create_branch)
 
 
210
    def create_branch_and_repo(base, force_new_repo=False):
 
 
211
        """Create a new BzrDir, Branch and Repository at the url 'base'.
 
 
213
        This will use the current default BzrDirFormat, and use whatever 
 
 
214
        repository format that that uses via bzrdir.create_branch and
 
 
215
        create_repository. If a shared repository is available that is used
 
 
218
        The created Branch object is returned.
 
 
220
        :param base: The URL to create the branch at.
 
 
221
        :param force_new_repo: If True a new repository is always created.
 
 
223
        bzrdir = BzrDir.create(base)
 
 
224
        bzrdir._find_or_create_repository(force_new_repo)
 
 
225
        return bzrdir.create_branch()
 
 
227
    def _find_or_create_repository(self, force_new_repo):
 
 
228
        """Create a new repository if needed, returning the repository."""
 
 
230
            return self.create_repository()
 
 
232
            return self.find_repository()
 
 
233
        except errors.NoRepositoryPresent:
 
 
234
            return self.create_repository()
 
 
237
    def create_branch_convenience(base, force_new_repo=False,
 
 
238
                                  force_new_tree=None, format=None):
 
 
239
        """Create a new BzrDir, Branch and Repository at the url 'base'.
 
 
241
        This is a convenience function - it will use an existing repository
 
 
242
        if possible, can be told explicitly whether to create a working tree or
 
 
245
        This will use the current default BzrDirFormat, and use whatever 
 
 
246
        repository format that that uses via bzrdir.create_branch and
 
 
247
        create_repository. If a shared repository is available that is used
 
 
248
        preferentially. Whatever repository is used, its tree creation policy
 
 
251
        The created Branch object is returned.
 
 
252
        If a working tree cannot be made due to base not being a file:// url,
 
 
253
        no error is raised unless force_new_tree is True, in which case no 
 
 
254
        data is created on disk and NotLocalUrl is raised.
 
 
256
        :param base: The URL to create the branch at.
 
 
257
        :param force_new_repo: If True a new repository is always created.
 
 
258
        :param force_new_tree: If True or False force creation of a tree or 
 
 
259
                               prevent such creation respectively.
 
 
260
        :param format: Override for the for the bzrdir format to create
 
 
263
            # check for non local urls
 
 
264
            t = get_transport(safe_unicode(base))
 
 
265
            if not isinstance(t, LocalTransport):
 
 
266
                raise errors.NotLocalUrl(base)
 
 
268
            bzrdir = BzrDir.create(base)
 
 
270
            bzrdir = format.initialize(base)
 
 
271
        repo = bzrdir._find_or_create_repository(force_new_repo)
 
 
272
        result = bzrdir.create_branch()
 
 
273
        if force_new_tree or (repo.make_working_trees() and 
 
 
274
                              force_new_tree is None):
 
 
276
                bzrdir.create_workingtree()
 
 
277
            except errors.NotLocalUrl:
 
 
282
    def create_repository(base, shared=False):
 
 
283
        """Create a new BzrDir and Repository at the url 'base'.
 
 
285
        This will use the current default BzrDirFormat, and use whatever 
 
 
286
        repository format that that uses for bzrdirformat.create_repository.
 
 
288
        ;param shared: Create a shared repository rather than a standalone
 
 
290
        The Repository object is returned.
 
 
292
        This must be overridden as an instance method in child classes, where
 
 
293
        it should take no parameters and construct whatever repository format
 
 
294
        that child class desires.
 
 
296
        bzrdir = BzrDir.create(base)
 
 
297
        return bzrdir.create_repository()
 
 
300
    def create_standalone_workingtree(base):
 
 
301
        """Create a new BzrDir, WorkingTree, Branch and Repository at 'base'.
 
 
303
        'base' must be a local path or a file:// url.
 
 
305
        This will use the current default BzrDirFormat, and use whatever 
 
 
306
        repository format that that uses for bzrdirformat.create_workingtree,
 
 
307
        create_branch and create_repository.
 
 
309
        The WorkingTree object is returned.
 
 
311
        t = get_transport(safe_unicode(base))
 
 
312
        if not isinstance(t, LocalTransport):
 
 
313
            raise errors.NotLocalUrl(base)
 
 
314
        bzrdir = BzrDir.create_branch_and_repo(safe_unicode(base),
 
 
315
                                               force_new_repo=True).bzrdir
 
 
316
        return bzrdir.create_workingtree()
 
 
318
    def create_workingtree(self, revision_id=None):
 
 
319
        """Create a working tree at this BzrDir.
 
 
321
        revision_id: create it as of this revision id.
 
 
323
        raise NotImplementedError(self.create_workingtree)
 
 
325
    def find_repository(self):
 
 
326
        """Find the repository that should be used for a_bzrdir.
 
 
328
        This does not require a branch as we use it to find the repo for
 
 
329
        new branches as well as to hook existing branches up to their
 
 
333
            return self.open_repository()
 
 
334
        except errors.NoRepositoryPresent:
 
 
336
        next_transport = self.root_transport.clone('..')
 
 
339
                found_bzrdir = BzrDir.open_containing_from_transport(
 
 
341
            except errors.NotBranchError:
 
 
342
                raise errors.NoRepositoryPresent(self)
 
 
344
                repository = found_bzrdir.open_repository()
 
 
345
            except errors.NoRepositoryPresent:
 
 
346
                next_transport = found_bzrdir.root_transport.clone('..')
 
 
348
            if ((found_bzrdir.root_transport.base == 
 
 
349
                 self.root_transport.base) or repository.is_shared()):
 
 
352
                raise errors.NoRepositoryPresent(self)
 
 
353
        raise errors.NoRepositoryPresent(self)
 
 
355
    def get_branch_transport(self, branch_format):
 
 
356
        """Get the transport for use by branch format in this BzrDir.
 
 
358
        Note that bzr dirs that do not support format strings will raise
 
 
359
        IncompatibleFormat if the branch format they are given has
 
 
360
        a format string, and vice verca.
 
 
362
        If branch_format is None, the transport is returned with no 
 
 
363
        checking. if it is not None, then the returned transport is
 
 
364
        guaranteed to point to an existing directory ready for use.
 
 
366
        raise NotImplementedError(self.get_branch_transport)
 
 
368
    def get_repository_transport(self, repository_format):
 
 
369
        """Get the transport for use by repository format in this BzrDir.
 
 
371
        Note that bzr dirs that do not support format strings will raise
 
 
372
        IncompatibleFormat if the repository format they are given has
 
 
373
        a format string, and vice verca.
 
 
375
        If repository_format is None, the transport is returned with no 
 
 
376
        checking. if it is not None, then the returned transport is
 
 
377
        guaranteed to point to an existing directory ready for use.
 
 
379
        raise NotImplementedError(self.get_repository_transport)
 
 
381
    def get_workingtree_transport(self, tree_format):
 
 
382
        """Get the transport for use by workingtree format in this BzrDir.
 
 
384
        Note that bzr dirs that do not support format strings will raise
 
 
385
        IncompatibleFormat if the workingtree format they are given has
 
 
386
        a format string, and vice verca.
 
 
388
        If workingtree_format is None, the transport is returned with no 
 
 
389
        checking. if it is not None, then the returned transport is
 
 
390
        guaranteed to point to an existing directory ready for use.
 
 
392
        raise NotImplementedError(self.get_workingtree_transport)
 
 
394
    def __init__(self, _transport, _format):
 
 
395
        """Initialize a Bzr control dir object.
 
 
397
        Only really common logic should reside here, concrete classes should be
 
 
398
        made with varying behaviours.
 
 
400
        :param _format: the format that is creating this BzrDir instance.
 
 
401
        :param _transport: the transport this dir is based at.
 
 
403
        self._format = _format
 
 
404
        self.transport = _transport.clone('.bzr')
 
 
405
        self.root_transport = _transport
 
 
407
    def needs_format_conversion(self, format=None):
 
 
408
        """Return true if this bzrdir needs convert_format run on it.
 
 
410
        For instance, if the repository format is out of date but the 
 
 
411
        branch and working tree are not, this should return True.
 
 
413
        :param format: Optional parameter indicating a specific desired
 
 
414
                       format we plan to arrive at.
 
 
416
        raise NotImplementedError(self.needs_format_conversion)
 
 
419
    def open_unsupported(base):
 
 
420
        """Open a branch which is not supported."""
 
 
421
        return BzrDir.open(base, _unsupported=True)
 
 
424
    def open(base, _unsupported=False):
 
 
425
        """Open an existing bzrdir, rooted at 'base' (url)
 
 
427
        _unsupported is a private parameter to the BzrDir class.
 
 
429
        t = get_transport(base)
 
 
430
        mutter("trying to open %r with transport %r", base, t)
 
 
431
        format = BzrDirFormat.find_format(t)
 
 
432
        BzrDir._check_supported(format, _unsupported)
 
 
433
        return format.open(t, _found=True)
 
 
435
    def open_branch(self, unsupported=False):
 
 
436
        """Open the branch object at this BzrDir if one is present.
 
 
438
        If unsupported is True, then no longer supported branch formats can
 
 
441
        TODO: static convenience version of this?
 
 
443
        raise NotImplementedError(self.open_branch)
 
 
446
    def open_containing(url):
 
 
447
        """Open an existing branch which contains url.
 
 
449
        :param url: url to search from.
 
 
450
        See open_containing_from_transport for more detail.
 
 
452
        return BzrDir.open_containing_from_transport(get_transport(url))
 
 
455
    def open_containing_from_transport(a_transport):
 
 
456
        """Open an existing branch which contains a_transport.base
 
 
458
        This probes for a branch at a_transport, and searches upwards from there.
 
 
460
        Basically we keep looking up until we find the control directory or
 
 
461
        run into the root.  If there isn't one, raises NotBranchError.
 
 
462
        If there is one and it is either an unrecognised format or an unsupported 
 
 
463
        format, UnknownFormatError or UnsupportedFormatError are raised.
 
 
464
        If there is one, it is returned, along with the unused portion of url.
 
 
466
        # this gets the normalised url back. I.e. '.' -> the full path.
 
 
467
        url = a_transport.base
 
 
470
                format = BzrDirFormat.find_format(a_transport)
 
 
471
                BzrDir._check_supported(format, False)
 
 
472
                return format.open(a_transport), a_transport.relpath(url)
 
 
473
            except errors.NotBranchError, e:
 
 
474
                mutter('not a branch in: %r %s', a_transport.base, e)
 
 
475
            new_t = a_transport.clone('..')
 
 
476
            if new_t.base == a_transport.base:
 
 
477
                # reached the root, whatever that may be
 
 
478
                raise errors.NotBranchError(path=url)
 
 
481
    def open_repository(self, _unsupported=False):
 
 
482
        """Open the repository object at this BzrDir if one is present.
 
 
484
        This will not follow the Branch object pointer - its strictly a direct
 
 
485
        open facility. Most client code should use open_branch().repository to
 
 
488
        _unsupported is a private parameter, not part of the api.
 
 
489
        TODO: static convenience version of this?
 
 
491
        raise NotImplementedError(self.open_repository)
 
 
493
    def open_workingtree(self, _unsupported=False):
 
 
494
        """Open the workingtree object at this BzrDir if one is present.
 
 
496
        TODO: static convenience version of this?
 
 
498
        raise NotImplementedError(self.open_workingtree)
 
 
500
    def has_branch(self):
 
 
501
        """Tell if this bzrdir contains a branch.
 
 
503
        Note: if you're going to open the branch, you should just go ahead
 
 
504
        and try, and not ask permission first.  (This method just opens the 
 
 
505
        branch and discards it, and that's somewhat expensive.) 
 
 
510
        except errors.NotBranchError:
 
 
513
    def has_workingtree(self):
 
 
514
        """Tell if this bzrdir contains a working tree.
 
 
516
        This will still raise an exception if the bzrdir has a workingtree that
 
 
517
        is remote & inaccessible.
 
 
519
        Note: if you're going to open the working tree, you should just go ahead
 
 
520
        and try, and not ask permission first.  (This method just opens the 
 
 
521
        workingtree and discards it, and that's somewhat expensive.) 
 
 
524
            self.open_workingtree()
 
 
526
        except errors.NoWorkingTree:
 
 
529
    def sprout(self, url, revision_id=None, basis=None, force_new_repo=False):
 
 
530
        """Create a copy of this bzrdir prepared for use as a new line of
 
 
533
        If urls last component does not exist, it will be created.
 
 
535
        Attributes related to the identity of the source branch like
 
 
536
        branch nickname will be cleaned, a working tree is created
 
 
537
        whether one existed before or not; and a local branch is always
 
 
540
        if revision_id is not None, then the clone operation may tune
 
 
541
            itself to download less data.
 
 
544
        result = self._format.initialize(url)
 
 
545
        basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
 
 
547
            source_branch = self.open_branch()
 
 
548
            source_repository = source_branch.repository
 
 
549
        except errors.NotBranchError:
 
 
552
                source_repository = self.open_repository()
 
 
553
            except errors.NoRepositoryPresent:
 
 
554
                # copy the entire basis one if there is one
 
 
555
                # but there is no repository.
 
 
556
                source_repository = basis_repo
 
 
561
                result_repo = result.find_repository()
 
 
562
            except errors.NoRepositoryPresent:
 
 
564
        if source_repository is None and result_repo is not None:
 
 
566
        elif source_repository is None and result_repo is None:
 
 
567
            # no repo available, make a new one
 
 
568
            result.create_repository()
 
 
569
        elif source_repository is not None and result_repo is None:
 
 
570
            # have soure, and want to make a new target repo
 
 
571
            source_repository.clone(result,
 
 
572
                                    revision_id=revision_id,
 
 
575
            # fetch needed content into target.
 
 
577
                # XXX FIXME RBC 20060214 need tests for this when the basis
 
 
579
                result_repo.fetch(basis_repo, revision_id=revision_id)
 
 
580
            result_repo.fetch(source_repository, revision_id=revision_id)
 
 
581
        if source_branch is not None:
 
 
582
            source_branch.sprout(result, revision_id=revision_id)
 
 
584
            result.create_branch()
 
 
585
        if result_repo is None or result_repo.make_working_trees():
 
 
586
            result.create_workingtree()
 
 
590
class BzrDirPreSplitOut(BzrDir):
 
 
591
    """A common class for the all-in-one formats."""
 
 
593
    def __init__(self, _transport, _format):
 
 
594
        """See BzrDir.__init__."""
 
 
595
        super(BzrDirPreSplitOut, self).__init__(_transport, _format)
 
 
596
        assert self._format._lock_class == TransportLock
 
 
597
        assert self._format._lock_file_name == 'branch-lock'
 
 
598
        self._control_files = LockableFiles(self.get_branch_transport(None),
 
 
599
                                            self._format._lock_file_name,
 
 
600
                                            self._format._lock_class)
 
 
602
    def break_lock(self):
 
 
603
        """Pre-splitout bzrdirs do not suffer from stale locks."""
 
 
604
        raise NotImplementedError(self.break_lock)
 
 
606
    def clone(self, url, revision_id=None, basis=None, force_new_repo=False):
 
 
607
        """See BzrDir.clone()."""
 
 
608
        from bzrlib.workingtree import WorkingTreeFormat2
 
 
610
        result = self._format._initialize_for_clone(url)
 
 
611
        basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
 
 
612
        self.open_repository().clone(result, revision_id=revision_id, basis=basis_repo)
 
 
613
        from_branch = self.open_branch()
 
 
614
        from_branch.clone(result, revision_id=revision_id)
 
 
616
            self.open_workingtree().clone(result, basis=basis_tree)
 
 
617
        except errors.NotLocalUrl:
 
 
618
            # make a new one, this format always has to have one.
 
 
620
                WorkingTreeFormat2().initialize(result)
 
 
621
            except errors.NotLocalUrl:
 
 
622
                # but we cannot do it for remote trees.
 
 
623
                to_branch = result.open_branch()
 
 
624
                WorkingTreeFormat2().stub_initialize_remote(to_branch.control_files)
 
 
627
    def create_branch(self):
 
 
628
        """See BzrDir.create_branch."""
 
 
629
        return self.open_branch()
 
 
631
    def create_repository(self, shared=False):
 
 
632
        """See BzrDir.create_repository."""
 
 
634
            raise errors.IncompatibleFormat('shared repository', self._format)
 
 
635
        return self.open_repository()
 
 
637
    def create_workingtree(self, revision_id=None):
 
 
638
        """See BzrDir.create_workingtree."""
 
 
639
        # this looks buggy but is not -really-
 
 
640
        # clone and sprout will have set the revision_id
 
 
641
        # and that will have set it for us, its only
 
 
642
        # specific uses of create_workingtree in isolation
 
 
643
        # that can do wonky stuff here, and that only
 
 
644
        # happens for creating checkouts, which cannot be 
 
 
645
        # done on this format anyway. So - acceptable wart.
 
 
646
        result = self.open_workingtree()
 
 
647
        if revision_id is not None:
 
 
648
            result.set_last_revision(revision_id)
 
 
651
    def get_branch_transport(self, branch_format):
 
 
652
        """See BzrDir.get_branch_transport()."""
 
 
653
        if branch_format is None:
 
 
654
            return self.transport
 
 
656
            branch_format.get_format_string()
 
 
657
        except NotImplementedError:
 
 
658
            return self.transport
 
 
659
        raise errors.IncompatibleFormat(branch_format, self._format)
 
 
661
    def get_repository_transport(self, repository_format):
 
 
662
        """See BzrDir.get_repository_transport()."""
 
 
663
        if repository_format is None:
 
 
664
            return self.transport
 
 
666
            repository_format.get_format_string()
 
 
667
        except NotImplementedError:
 
 
668
            return self.transport
 
 
669
        raise errors.IncompatibleFormat(repository_format, self._format)
 
 
671
    def get_workingtree_transport(self, workingtree_format):
 
 
672
        """See BzrDir.get_workingtree_transport()."""
 
 
673
        if workingtree_format is None:
 
 
674
            return self.transport
 
 
676
            workingtree_format.get_format_string()
 
 
677
        except NotImplementedError:
 
 
678
            return self.transport
 
 
679
        raise errors.IncompatibleFormat(workingtree_format, self._format)
 
 
681
    def needs_format_conversion(self, format=None):
 
 
682
        """See BzrDir.needs_format_conversion()."""
 
 
683
        # if the format is not the same as the system default,
 
 
684
        # an upgrade is needed.
 
 
686
            format = BzrDirFormat.get_default_format()
 
 
687
        return not isinstance(self._format, format.__class__)
 
 
689
    def open_branch(self, unsupported=False):
 
 
690
        """See BzrDir.open_branch."""
 
 
691
        from bzrlib.branch import BzrBranchFormat4
 
 
692
        format = BzrBranchFormat4()
 
 
693
        self._check_supported(format, unsupported)
 
 
694
        return format.open(self, _found=True)
 
 
696
    def sprout(self, url, revision_id=None, basis=None):
 
 
697
        """See BzrDir.sprout()."""
 
 
698
        from bzrlib.workingtree import WorkingTreeFormat2
 
 
700
        result = self._format._initialize_for_clone(url)
 
 
701
        basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
 
 
703
            self.open_repository().clone(result, revision_id=revision_id, basis=basis_repo)
 
 
704
        except errors.NoRepositoryPresent:
 
 
707
            self.open_branch().sprout(result, revision_id=revision_id)
 
 
708
        except errors.NotBranchError:
 
 
710
        # we always want a working tree
 
 
711
        WorkingTreeFormat2().initialize(result)
 
 
715
class BzrDir4(BzrDirPreSplitOut):
 
 
716
    """A .bzr version 4 control object.
 
 
718
    This is a deprecated format and may be removed after sept 2006.
 
 
721
    def create_repository(self, shared=False):
 
 
722
        """See BzrDir.create_repository."""
 
 
723
        return self._format.repository_format.initialize(self, shared)
 
 
725
    def needs_format_conversion(self, format=None):
 
 
726
        """Format 4 dirs are always in need of conversion."""
 
 
729
    def open_repository(self):
 
 
730
        """See BzrDir.open_repository."""
 
 
731
        from bzrlib.repository import RepositoryFormat4
 
 
732
        return RepositoryFormat4().open(self, _found=True)
 
 
735
class BzrDir5(BzrDirPreSplitOut):
 
 
736
    """A .bzr version 5 control object.
 
 
738
    This is a deprecated format and may be removed after sept 2006.
 
 
741
    def open_repository(self):
 
 
742
        """See BzrDir.open_repository."""
 
 
743
        from bzrlib.repository import RepositoryFormat5
 
 
744
        return RepositoryFormat5().open(self, _found=True)
 
 
746
    def open_workingtree(self, _unsupported=False):
 
 
747
        """See BzrDir.create_workingtree."""
 
 
748
        from bzrlib.workingtree import WorkingTreeFormat2
 
 
749
        return WorkingTreeFormat2().open(self, _found=True)
 
 
752
class BzrDir6(BzrDirPreSplitOut):
 
 
753
    """A .bzr version 6 control object.
 
 
755
    This is a deprecated format and may be removed after sept 2006.
 
 
758
    def open_repository(self):
 
 
759
        """See BzrDir.open_repository."""
 
 
760
        from bzrlib.repository import RepositoryFormat6
 
 
761
        return RepositoryFormat6().open(self, _found=True)
 
 
763
    def open_workingtree(self, _unsupported=False):
 
 
764
        """See BzrDir.create_workingtree."""
 
 
765
        from bzrlib.workingtree import WorkingTreeFormat2
 
 
766
        return WorkingTreeFormat2().open(self, _found=True)
 
 
769
class BzrDirMeta1(BzrDir):
 
 
770
    """A .bzr meta version 1 control object.
 
 
772
    This is the first control object where the 
 
 
773
    individual aspects are really split out: there are separate repository,
 
 
774
    workingtree and branch subdirectories and any subset of the three can be
 
 
775
    present within a BzrDir.
 
 
778
    def can_convert_format(self):
 
 
779
        """See BzrDir.can_convert_format()."""
 
 
782
    def create_branch(self):
 
 
783
        """See BzrDir.create_branch."""
 
 
784
        from bzrlib.branch import BranchFormat
 
 
785
        return BranchFormat.get_default_format().initialize(self)
 
 
787
    def create_repository(self, shared=False):
 
 
788
        """See BzrDir.create_repository."""
 
 
789
        return self._format.repository_format.initialize(self, shared)
 
 
791
    def create_workingtree(self, revision_id=None):
 
 
792
        """See BzrDir.create_workingtree."""
 
 
793
        from bzrlib.workingtree import WorkingTreeFormat
 
 
794
        return WorkingTreeFormat.get_default_format().initialize(self, revision_id)
 
 
796
    def _get_mkdir_mode(self):
 
 
797
        """Figure out the mode to use when creating a bzrdir subdir."""
 
 
798
        temp_control = LockableFiles(self.transport, '', TransportLock)
 
 
799
        return temp_control._dir_mode
 
 
801
    def get_branch_transport(self, branch_format):
 
 
802
        """See BzrDir.get_branch_transport()."""
 
 
803
        if branch_format is None:
 
 
804
            return self.transport.clone('branch')
 
 
806
            branch_format.get_format_string()
 
 
807
        except NotImplementedError:
 
 
808
            raise errors.IncompatibleFormat(branch_format, self._format)
 
 
810
            self.transport.mkdir('branch', mode=self._get_mkdir_mode())
 
 
811
        except errors.FileExists:
 
 
813
        return self.transport.clone('branch')
 
 
815
    def get_repository_transport(self, repository_format):
 
 
816
        """See BzrDir.get_repository_transport()."""
 
 
817
        if repository_format is None:
 
 
818
            return self.transport.clone('repository')
 
 
820
            repository_format.get_format_string()
 
 
821
        except NotImplementedError:
 
 
822
            raise errors.IncompatibleFormat(repository_format, self._format)
 
 
824
            self.transport.mkdir('repository', mode=self._get_mkdir_mode())
 
 
825
        except errors.FileExists:
 
 
827
        return self.transport.clone('repository')
 
 
829
    def get_workingtree_transport(self, workingtree_format):
 
 
830
        """See BzrDir.get_workingtree_transport()."""
 
 
831
        if workingtree_format is None:
 
 
832
            return self.transport.clone('checkout')
 
 
834
            workingtree_format.get_format_string()
 
 
835
        except NotImplementedError:
 
 
836
            raise errors.IncompatibleFormat(workingtree_format, self._format)
 
 
838
            self.transport.mkdir('checkout', mode=self._get_mkdir_mode())
 
 
839
        except errors.FileExists:
 
 
841
        return self.transport.clone('checkout')
 
 
843
    def needs_format_conversion(self, format=None):
 
 
844
        """See BzrDir.needs_format_conversion()."""
 
 
846
            format = BzrDirFormat.get_default_format()
 
 
847
        if not isinstance(self._format, format.__class__):
 
 
848
            # it is not a meta dir format, conversion is needed.
 
 
850
        # we might want to push this down to the repository?
 
 
852
            if not isinstance(self.open_repository()._format,
 
 
853
                              format.repository_format.__class__):
 
 
854
                # the repository needs an upgrade.
 
 
856
        except errors.NoRepositoryPresent:
 
 
858
        # currently there are no other possible conversions for meta1 formats.
 
 
861
    def open_branch(self, unsupported=False):
 
 
862
        """See BzrDir.open_branch."""
 
 
863
        from bzrlib.branch import BranchFormat
 
 
864
        format = BranchFormat.find_format(self)
 
 
865
        self._check_supported(format, unsupported)
 
 
866
        return format.open(self, _found=True)
 
 
868
    def open_repository(self, unsupported=False):
 
 
869
        """See BzrDir.open_repository."""
 
 
870
        from bzrlib.repository import RepositoryFormat
 
 
871
        format = RepositoryFormat.find_format(self)
 
 
872
        self._check_supported(format, unsupported)
 
 
873
        return format.open(self, _found=True)
 
 
875
    def open_workingtree(self, unsupported=False):
 
 
876
        """See BzrDir.open_workingtree."""
 
 
877
        from bzrlib.workingtree import WorkingTreeFormat
 
 
878
        format = WorkingTreeFormat.find_format(self)
 
 
879
        self._check_supported(format, unsupported)
 
 
880
        return format.open(self, _found=True)
 
 
883
class BzrDirFormat(object):
 
 
884
    """An encapsulation of the initialization and open routines for a format.
 
 
886
    Formats provide three things:
 
 
887
     * An initialization routine,
 
 
891
    Formats are placed in an dict by their format string for reference 
 
 
892
    during bzrdir opening. These should be subclasses of BzrDirFormat
 
 
895
    Once a format is deprecated, just deprecate the initialize and open
 
 
896
    methods on the format class. Do not deprecate the object, as the 
 
 
897
    object will be created every system load.
 
 
900
    _default_format = None
 
 
901
    """The default format used for new .bzr dirs."""
 
 
904
    """The known formats."""
 
 
906
    _lock_file_name = 'branch-lock'
 
 
908
    # _lock_class must be set in subclasses to the lock type, typ.
 
 
909
    # TransportLock or LockDir
 
 
912
    def find_format(klass, transport):
 
 
913
        """Return the format registered for URL."""
 
 
915
            format_string = transport.get(".bzr/branch-format").read()
 
 
916
            return klass._formats[format_string]
 
 
917
        except errors.NoSuchFile:
 
 
918
            raise errors.NotBranchError(path=transport.base)
 
 
920
            raise errors.UnknownFormatError(format_string)
 
 
923
    def get_default_format(klass):
 
 
924
        """Return the current default format."""
 
 
925
        return klass._default_format
 
 
927
    def get_format_string(self):
 
 
928
        """Return the ASCII format string that identifies this format."""
 
 
929
        raise NotImplementedError(self.get_format_string)
 
 
931
    def get_format_description(self):
 
 
932
        """Return the short description for this format."""
 
 
933
        raise NotImplementedError(self.get_format_description)
 
 
935
    def get_converter(self, format=None):
 
 
936
        """Return the converter to use to convert bzrdirs needing converts.
 
 
938
        This returns a bzrlib.bzrdir.Converter object.
 
 
940
        This should return the best upgrader to step this format towards the
 
 
941
        current default format. In the case of plugins we can/shouold provide
 
 
942
        some means for them to extend the range of returnable converters.
 
 
944
        :param format: Optional format to override the default foramt of the 
 
 
947
        raise NotImplementedError(self.get_converter)
 
 
949
    def initialize(self, url):
 
 
950
        """Create a bzr control dir at this url and return an opened copy.
 
 
952
        Subclasses should typically override initialize_on_transport
 
 
953
        instead of this method.
 
 
955
        return self.initialize_on_transport(get_transport(url))
 
 
957
    def initialize_on_transport(self, transport):
 
 
958
        """Initialize a new bzrdir in the base directory of a Transport."""
 
 
959
        # Since we don'transport have a .bzr directory, inherit the
 
 
960
        # mode from the root directory
 
 
961
        temp_control = LockableFiles(transport, '', TransportLock)
 
 
962
        temp_control._transport.mkdir('.bzr',
 
 
963
                                      # FIXME: RBC 20060121 dont peek under
 
 
965
                                      mode=temp_control._dir_mode)
 
 
966
        file_mode = temp_control._file_mode
 
 
968
        mutter('created control directory in ' + transport.base)
 
 
969
        control = transport.clone('.bzr')
 
 
970
        utf8_files = [('README', 
 
 
971
                       "This is a Bazaar-NG control directory.\n"
 
 
972
                       "Do not change any files in this directory.\n"),
 
 
973
                      ('branch-format', self.get_format_string()),
 
 
975
        # NB: no need to escape relative paths that are url safe.
 
 
976
        control_files = LockableFiles(control, self._lock_file_name, 
 
 
978
        control_files.create_lock()
 
 
979
        control_files.lock_write()
 
 
981
            for file, content in utf8_files:
 
 
982
                control_files.put_utf8(file, content)
 
 
984
            control_files.unlock()
 
 
985
        return self.open(transport, _found=True)
 
 
987
    def is_supported(self):
 
 
988
        """Is this format supported?
 
 
990
        Supported formats must be initializable and openable.
 
 
991
        Unsupported formats may not support initialization or committing or 
 
 
992
        some other features depending on the reason for not being supported.
 
 
996
    def open(self, transport, _found=False):
 
 
997
        """Return an instance of this format for the dir transport points at.
 
 
999
        _found is a private parameter, do not use it.
 
 
1002
            assert isinstance(BzrDirFormat.find_format(transport),
 
 
1004
        return self._open(transport)
 
 
1006
    def _open(self, transport):
 
 
1007
        """Template method helper for opening BzrDirectories.
 
 
1009
        This performs the actual open and any additional logic or parameter
 
 
1012
        raise NotImplementedError(self._open)
 
 
1015
    def register_format(klass, format):
 
 
1016
        klass._formats[format.get_format_string()] = format
 
 
1019
    def set_default_format(klass, format):
 
 
1020
        klass._default_format = format
 
 
1023
        return self.get_format_string()[:-1]
 
 
1026
    def unregister_format(klass, format):
 
 
1027
        assert klass._formats[format.get_format_string()] is format
 
 
1028
        del klass._formats[format.get_format_string()]
 
 
1031
class BzrDirFormat4(BzrDirFormat):
 
 
1032
    """Bzr dir format 4.
 
 
1034
    This format is a combined format for working tree, branch and repository.
 
 
1036
     - Format 1 working trees [always]
 
 
1037
     - Format 4 branches [always]
 
 
1038
     - Format 4 repositories [always]
 
 
1040
    This format is deprecated: it indexes texts using a text it which is
 
 
1041
    removed in format 5; write support for this format has been removed.
 
 
1044
    _lock_class = TransportLock
 
 
1046
    def get_format_string(self):
 
 
1047
        """See BzrDirFormat.get_format_string()."""
 
 
1048
        return "Bazaar-NG branch, format 0.0.4\n"
 
 
1050
    def get_format_description(self):
 
 
1051
        """See BzrDirFormat.get_format_description()."""
 
 
1052
        return "All-in-one format 4"
 
 
1054
    def get_converter(self, format=None):
 
 
1055
        """See BzrDirFormat.get_converter()."""
 
 
1056
        # there is one and only one upgrade path here.
 
 
1057
        return ConvertBzrDir4To5()
 
 
1059
    def initialize_on_transport(self, transport):
 
 
1060
        """Format 4 branches cannot be created."""
 
 
1061
        raise errors.UninitializableFormat(self)
 
 
1063
    def is_supported(self):
 
 
1064
        """Format 4 is not supported.
 
 
1066
        It is not supported because the model changed from 4 to 5 and the
 
 
1067
        conversion logic is expensive - so doing it on the fly was not 
 
 
1072
    def _open(self, transport):
 
 
1073
        """See BzrDirFormat._open."""
 
 
1074
        return BzrDir4(transport, self)
 
 
1076
    def __return_repository_format(self):
 
 
1077
        """Circular import protection."""
 
 
1078
        from bzrlib.repository import RepositoryFormat4
 
 
1079
        return RepositoryFormat4(self)
 
 
1080
    repository_format = property(__return_repository_format)
 
 
1083
class BzrDirFormat5(BzrDirFormat):
 
 
1084
    """Bzr control format 5.
 
 
1086
    This format is a combined format for working tree, branch and repository.
 
 
1088
     - Format 2 working trees [always] 
 
 
1089
     - Format 4 branches [always] 
 
 
1090
     - Format 5 repositories [always]
 
 
1091
       Unhashed stores in the repository.
 
 
1094
    _lock_class = TransportLock
 
 
1096
    def get_format_string(self):
 
 
1097
        """See BzrDirFormat.get_format_string()."""
 
 
1098
        return "Bazaar-NG branch, format 5\n"
 
 
1100
    def get_format_description(self):
 
 
1101
        """See BzrDirFormat.get_format_description()."""
 
 
1102
        return "All-in-one format 5"
 
 
1104
    def get_converter(self, format=None):
 
 
1105
        """See BzrDirFormat.get_converter()."""
 
 
1106
        # there is one and only one upgrade path here.
 
 
1107
        return ConvertBzrDir5To6()
 
 
1109
    def _initialize_for_clone(self, url):
 
 
1110
        return self.initialize_on_transport(get_transport(url), _cloning=True)
 
 
1112
    def initialize_on_transport(self, transport, _cloning=False):
 
 
1113
        """Format 5 dirs always have working tree, branch and repository.
 
 
1115
        Except when they are being cloned.
 
 
1117
        from bzrlib.branch import BzrBranchFormat4
 
 
1118
        from bzrlib.repository import RepositoryFormat5
 
 
1119
        from bzrlib.workingtree import WorkingTreeFormat2
 
 
1120
        result = (super(BzrDirFormat5, self).initialize_on_transport(transport))
 
 
1121
        RepositoryFormat5().initialize(result, _internal=True)
 
 
1123
            BzrBranchFormat4().initialize(result)
 
 
1124
            WorkingTreeFormat2().initialize(result)
 
 
1127
    def _open(self, transport):
 
 
1128
        """See BzrDirFormat._open."""
 
 
1129
        return BzrDir5(transport, self)
 
 
1131
    def __return_repository_format(self):
 
 
1132
        """Circular import protection."""
 
 
1133
        from bzrlib.repository import RepositoryFormat5
 
 
1134
        return RepositoryFormat5(self)
 
 
1135
    repository_format = property(__return_repository_format)
 
 
1138
class BzrDirFormat6(BzrDirFormat):
 
 
1139
    """Bzr control format 6.
 
 
1141
    This format is a combined format for working tree, branch and repository.
 
 
1143
     - Format 2 working trees [always] 
 
 
1144
     - Format 4 branches [always] 
 
 
1145
     - Format 6 repositories [always]
 
 
1148
    _lock_class = TransportLock
 
 
1150
    def get_format_string(self):
 
 
1151
        """See BzrDirFormat.get_format_string()."""
 
 
1152
        return "Bazaar-NG branch, format 6\n"
 
 
1154
    def get_format_description(self):
 
 
1155
        """See BzrDirFormat.get_format_description()."""
 
 
1156
        return "All-in-one format 6"
 
 
1158
    def get_converter(self, format=None):
 
 
1159
        """See BzrDirFormat.get_converter()."""
 
 
1160
        # there is one and only one upgrade path here.
 
 
1161
        return ConvertBzrDir6ToMeta()
 
 
1163
    def _initialize_for_clone(self, url):
 
 
1164
        return self.initialize_on_transport(get_transport(url), _cloning=True)
 
 
1166
    def initialize_on_transport(self, transport, _cloning=False):
 
 
1167
        """Format 6 dirs always have working tree, branch and repository.
 
 
1169
        Except when they are being cloned.
 
 
1171
        from bzrlib.branch import BzrBranchFormat4
 
 
1172
        from bzrlib.repository import RepositoryFormat6
 
 
1173
        from bzrlib.workingtree import WorkingTreeFormat2
 
 
1174
        result = super(BzrDirFormat6, self).initialize_on_transport(transport)
 
 
1175
        RepositoryFormat6().initialize(result, _internal=True)
 
 
1177
            BzrBranchFormat4().initialize(result)
 
 
1179
                WorkingTreeFormat2().initialize(result)
 
 
1180
            except errors.NotLocalUrl:
 
 
1181
                # emulate pre-check behaviour for working tree and silently 
 
 
1186
    def _open(self, transport):
 
 
1187
        """See BzrDirFormat._open."""
 
 
1188
        return BzrDir6(transport, self)
 
 
1190
    def __return_repository_format(self):
 
 
1191
        """Circular import protection."""
 
 
1192
        from bzrlib.repository import RepositoryFormat6
 
 
1193
        return RepositoryFormat6(self)
 
 
1194
    repository_format = property(__return_repository_format)
 
 
1197
class BzrDirMetaFormat1(BzrDirFormat):
 
 
1198
    """Bzr meta control format 1
 
 
1200
    This is the first format with split out working tree, branch and repository
 
 
1203
     - Format 3 working trees [optional]
 
 
1204
     - Format 5 branches [optional]
 
 
1205
     - Format 7 repositories [optional]
 
 
1208
    _lock_class = LockDir
 
 
1210
    def get_converter(self, format=None):
 
 
1211
        """See BzrDirFormat.get_converter()."""
 
 
1213
            format = BzrDirFormat.get_default_format()
 
 
1214
        if not isinstance(self, format.__class__):
 
 
1215
            # converting away from metadir is not implemented
 
 
1216
            raise NotImplementedError(self.get_converter)
 
 
1217
        return ConvertMetaToMeta(format)
 
 
1219
    def get_format_string(self):
 
 
1220
        """See BzrDirFormat.get_format_string()."""
 
 
1221
        return "Bazaar-NG meta directory, format 1\n"
 
 
1223
    def get_format_description(self):
 
 
1224
        """See BzrDirFormat.get_format_description()."""
 
 
1225
        return "Meta directory format 1"
 
 
1227
    def _open(self, transport):
 
 
1228
        """See BzrDirFormat._open."""
 
 
1229
        return BzrDirMeta1(transport, self)
 
 
1231
    def __return_repository_format(self):
 
 
1232
        """Circular import protection."""
 
 
1233
        if getattr(self, '_repository_format', None):
 
 
1234
            return self._repository_format
 
 
1235
        from bzrlib.repository import RepositoryFormat
 
 
1236
        return RepositoryFormat.get_default_format()
 
 
1238
    def __set_repository_format(self, value):
 
 
1239
        """Allow changint the repository format for metadir formats."""
 
 
1240
        self._repository_format = value
 
 
1242
    repository_format = property(__return_repository_format, __set_repository_format)
 
 
1245
BzrDirFormat.register_format(BzrDirFormat4())
 
 
1246
BzrDirFormat.register_format(BzrDirFormat5())
 
 
1247
BzrDirFormat.register_format(BzrDirFormat6())
 
 
1248
__default_format = BzrDirMetaFormat1()
 
 
1249
BzrDirFormat.register_format(__default_format)
 
 
1250
BzrDirFormat.set_default_format(__default_format)
 
 
1253
class BzrDirTestProviderAdapter(object):
 
 
1254
    """A tool to generate a suite testing multiple bzrdir formats at once.
 
 
1256
    This is done by copying the test once for each transport and injecting
 
 
1257
    the transport_server, transport_readonly_server, and bzrdir_format
 
 
1258
    classes into each copy. Each copy is also given a new id() to make it
 
 
1262
    def __init__(self, transport_server, transport_readonly_server, formats):
 
 
1263
        self._transport_server = transport_server
 
 
1264
        self._transport_readonly_server = transport_readonly_server
 
 
1265
        self._formats = formats
 
 
1267
    def adapt(self, test):
 
 
1268
        result = TestSuite()
 
 
1269
        for format in self._formats:
 
 
1270
            new_test = deepcopy(test)
 
 
1271
            new_test.transport_server = self._transport_server
 
 
1272
            new_test.transport_readonly_server = self._transport_readonly_server
 
 
1273
            new_test.bzrdir_format = format
 
 
1274
            def make_new_test_id():
 
 
1275
                new_id = "%s(%s)" % (new_test.id(), format.__class__.__name__)
 
 
1276
                return lambda: new_id
 
 
1277
            new_test.id = make_new_test_id()
 
 
1278
            result.addTest(new_test)
 
 
1282
class ScratchDir(BzrDir6):
 
 
1283
    """Special test class: a bzrdir that cleans up itself..
 
 
1285
    >>> d = ScratchDir()
 
 
1286
    >>> base = d.transport.base
 
 
1289
    >>> b.transport.__del__()
 
 
1294
    def __init__(self, files=[], dirs=[], transport=None):
 
 
1295
        """Make a test branch.
 
 
1297
        This creates a temporary directory and runs init-tree in it.
 
 
1299
        If any files are listed, they are created in the working copy.
 
 
1301
        if transport is None:
 
 
1302
            transport = bzrlib.transport.local.ScratchTransport()
 
 
1303
            # local import for scope restriction
 
 
1304
            BzrDirFormat6().initialize(transport.base)
 
 
1305
            super(ScratchDir, self).__init__(transport, BzrDirFormat6())
 
 
1306
            self.create_repository()
 
 
1307
            self.create_branch()
 
 
1308
            self.create_workingtree()
 
 
1310
            super(ScratchDir, self).__init__(transport, BzrDirFormat6())
 
 
1312
        # BzrBranch creates a clone to .bzr and then forgets about the
 
 
1313
        # original transport. A ScratchTransport() deletes itself and
 
 
1314
        # everything underneath it when it goes away, so we need to
 
 
1315
        # grab a local copy to prevent that from happening
 
 
1316
        self._transport = transport
 
 
1319
            self._transport.mkdir(d)
 
 
1322
            self._transport.put(f, 'content of %s' % f)
 
 
1326
        >>> orig = ScratchDir(files=["file1", "file2"])
 
 
1327
        >>> os.listdir(orig.base)
 
 
1328
        [u'.bzr', u'file1', u'file2']
 
 
1329
        >>> clone = orig.clone()
 
 
1330
        >>> if os.name != 'nt':
 
 
1331
        ...   os.path.samefile(orig.base, clone.base)
 
 
1333
        ...   orig.base == clone.base
 
 
1336
        >>> os.listdir(clone.base)
 
 
1337
        [u'.bzr', u'file1', u'file2']
 
 
1339
        from shutil import copytree
 
 
1340
        from bzrlib.osutils import mkdtemp
 
 
1343
        copytree(self.base, base, symlinks=True)
 
 
1345
            transport=bzrlib.transport.local.ScratchTransport(base))
 
 
1348
class Converter(object):
 
 
1349
    """Converts a disk format object from one format to another."""
 
 
1351
    def convert(self, to_convert, pb):
 
 
1352
        """Perform the conversion of to_convert, giving feedback via pb.
 
 
1354
        :param to_convert: The disk object to convert.
 
 
1355
        :param pb: a progress bar to use for progress information.
 
 
1358
    def step(self, message):
 
 
1359
        """Update the pb by a step."""
 
 
1361
        self.pb.update(message, self.count, self.total)
 
 
1364
class ConvertBzrDir4To5(Converter):
 
 
1365
    """Converts format 4 bzr dirs to format 5."""
 
 
1368
        super(ConvertBzrDir4To5, self).__init__()
 
 
1369
        self.converted_revs = set()
 
 
1370
        self.absent_revisions = set()
 
 
1374
    def convert(self, to_convert, pb):
 
 
1375
        """See Converter.convert()."""
 
 
1376
        self.bzrdir = to_convert
 
 
1378
        self.pb.note('starting upgrade from format 4 to 5')
 
 
1379
        if isinstance(self.bzrdir.transport, LocalTransport):
 
 
1380
            self.bzrdir.get_workingtree_transport(None).delete('stat-cache')
 
 
1381
        self._convert_to_weaves()
 
 
1382
        return BzrDir.open(self.bzrdir.root_transport.base)
 
 
1384
    def _convert_to_weaves(self):
 
 
1385
        self.pb.note('note: upgrade may be faster if all store files are ungzipped first')
 
 
1388
            stat = self.bzrdir.transport.stat('weaves')
 
 
1389
            if not S_ISDIR(stat.st_mode):
 
 
1390
                self.bzrdir.transport.delete('weaves')
 
 
1391
                self.bzrdir.transport.mkdir('weaves')
 
 
1392
        except errors.NoSuchFile:
 
 
1393
            self.bzrdir.transport.mkdir('weaves')
 
 
1394
        # deliberately not a WeaveFile as we want to build it up slowly.
 
 
1395
        self.inv_weave = Weave('inventory')
 
 
1396
        # holds in-memory weaves for all files
 
 
1397
        self.text_weaves = {}
 
 
1398
        self.bzrdir.transport.delete('branch-format')
 
 
1399
        self.branch = self.bzrdir.open_branch()
 
 
1400
        self._convert_working_inv()
 
 
1401
        rev_history = self.branch.revision_history()
 
 
1402
        # to_read is a stack holding the revisions we still need to process;
 
 
1403
        # appending to it adds new highest-priority revisions
 
 
1404
        self.known_revisions = set(rev_history)
 
 
1405
        self.to_read = rev_history[-1:]
 
 
1407
            rev_id = self.to_read.pop()
 
 
1408
            if (rev_id not in self.revisions
 
 
1409
                and rev_id not in self.absent_revisions):
 
 
1410
                self._load_one_rev(rev_id)
 
 
1412
        to_import = self._make_order()
 
 
1413
        for i, rev_id in enumerate(to_import):
 
 
1414
            self.pb.update('converting revision', i, len(to_import))
 
 
1415
            self._convert_one_rev(rev_id)
 
 
1417
        self._write_all_weaves()
 
 
1418
        self._write_all_revs()
 
 
1419
        self.pb.note('upgraded to weaves:')
 
 
1420
        self.pb.note('  %6d revisions and inventories', len(self.revisions))
 
 
1421
        self.pb.note('  %6d revisions not present', len(self.absent_revisions))
 
 
1422
        self.pb.note('  %6d texts', self.text_count)
 
 
1423
        self._cleanup_spare_files_after_format4()
 
 
1424
        self.branch.control_files.put_utf8('branch-format', BzrDirFormat5().get_format_string())
 
 
1426
    def _cleanup_spare_files_after_format4(self):
 
 
1427
        # FIXME working tree upgrade foo.
 
 
1428
        for n in 'merged-patches', 'pending-merged-patches':
 
 
1430
                ## assert os.path.getsize(p) == 0
 
 
1431
                self.bzrdir.transport.delete(n)
 
 
1432
            except errors.NoSuchFile:
 
 
1434
        self.bzrdir.transport.delete_tree('inventory-store')
 
 
1435
        self.bzrdir.transport.delete_tree('text-store')
 
 
1437
    def _convert_working_inv(self):
 
 
1438
        inv = serializer_v4.read_inventory(self.branch.control_files.get('inventory'))
 
 
1439
        new_inv_xml = serializer_v5.write_inventory_to_string(inv)
 
 
1440
        # FIXME inventory is a working tree change.
 
 
1441
        self.branch.control_files.put('inventory', new_inv_xml)
 
 
1443
    def _write_all_weaves(self):
 
 
1444
        controlweaves = WeaveStore(self.bzrdir.transport, prefixed=False)
 
 
1445
        weave_transport = self.bzrdir.transport.clone('weaves')
 
 
1446
        weaves = WeaveStore(weave_transport, prefixed=False)
 
 
1447
        transaction = WriteTransaction()
 
 
1451
            for file_id, file_weave in self.text_weaves.items():
 
 
1452
                self.pb.update('writing weave', i, len(self.text_weaves))
 
 
1453
                weaves._put_weave(file_id, file_weave, transaction)
 
 
1455
            self.pb.update('inventory', 0, 1)
 
 
1456
            controlweaves._put_weave('inventory', self.inv_weave, transaction)
 
 
1457
            self.pb.update('inventory', 1, 1)
 
 
1461
    def _write_all_revs(self):
 
 
1462
        """Write all revisions out in new form."""
 
 
1463
        self.bzrdir.transport.delete_tree('revision-store')
 
 
1464
        self.bzrdir.transport.mkdir('revision-store')
 
 
1465
        revision_transport = self.bzrdir.transport.clone('revision-store')
 
 
1467
        _revision_store = TextRevisionStore(TextStore(revision_transport,
 
 
1471
            transaction = bzrlib.transactions.WriteTransaction()
 
 
1472
            for i, rev_id in enumerate(self.converted_revs):
 
 
1473
                self.pb.update('write revision', i, len(self.converted_revs))
 
 
1474
                _revision_store.add_revision(self.revisions[rev_id], transaction)
 
 
1478
    def _load_one_rev(self, rev_id):
 
 
1479
        """Load a revision object into memory.
 
 
1481
        Any parents not either loaded or abandoned get queued to be
 
 
1483
        self.pb.update('loading revision',
 
 
1484
                       len(self.revisions),
 
 
1485
                       len(self.known_revisions))
 
 
1486
        if not self.branch.repository.has_revision(rev_id):
 
 
1488
            self.pb.note('revision {%s} not present in branch; '
 
 
1489
                         'will be converted as a ghost',
 
 
1491
            self.absent_revisions.add(rev_id)
 
 
1493
            rev = self.branch.repository._revision_store.get_revision(rev_id,
 
 
1494
                self.branch.repository.get_transaction())
 
 
1495
            for parent_id in rev.parent_ids:
 
 
1496
                self.known_revisions.add(parent_id)
 
 
1497
                self.to_read.append(parent_id)
 
 
1498
            self.revisions[rev_id] = rev
 
 
1500
    def _load_old_inventory(self, rev_id):
 
 
1501
        assert rev_id not in self.converted_revs
 
 
1502
        old_inv_xml = self.branch.repository.inventory_store.get(rev_id).read()
 
 
1503
        inv = serializer_v4.read_inventory_from_string(old_inv_xml)
 
 
1504
        rev = self.revisions[rev_id]
 
 
1505
        if rev.inventory_sha1:
 
 
1506
            assert rev.inventory_sha1 == sha_string(old_inv_xml), \
 
 
1507
                'inventory sha mismatch for {%s}' % rev_id
 
 
1510
    def _load_updated_inventory(self, rev_id):
 
 
1511
        assert rev_id in self.converted_revs
 
 
1512
        inv_xml = self.inv_weave.get_text(rev_id)
 
 
1513
        inv = serializer_v5.read_inventory_from_string(inv_xml)
 
 
1516
    def _convert_one_rev(self, rev_id):
 
 
1517
        """Convert revision and all referenced objects to new format."""
 
 
1518
        rev = self.revisions[rev_id]
 
 
1519
        inv = self._load_old_inventory(rev_id)
 
 
1520
        present_parents = [p for p in rev.parent_ids
 
 
1521
                           if p not in self.absent_revisions]
 
 
1522
        self._convert_revision_contents(rev, inv, present_parents)
 
 
1523
        self._store_new_weave(rev, inv, present_parents)
 
 
1524
        self.converted_revs.add(rev_id)
 
 
1526
    def _store_new_weave(self, rev, inv, present_parents):
 
 
1527
        # the XML is now updated with text versions
 
 
1531
                if ie.kind == 'root_directory':
 
 
1533
                assert hasattr(ie, 'revision'), \
 
 
1534
                    'no revision on {%s} in {%s}' % \
 
 
1535
                    (file_id, rev.revision_id)
 
 
1536
        new_inv_xml = serializer_v5.write_inventory_to_string(inv)
 
 
1537
        new_inv_sha1 = sha_string(new_inv_xml)
 
 
1538
        self.inv_weave.add_lines(rev.revision_id, 
 
 
1540
                                 new_inv_xml.splitlines(True))
 
 
1541
        rev.inventory_sha1 = new_inv_sha1
 
 
1543
    def _convert_revision_contents(self, rev, inv, present_parents):
 
 
1544
        """Convert all the files within a revision.
 
 
1546
        Also upgrade the inventory to refer to the text revision ids."""
 
 
1547
        rev_id = rev.revision_id
 
 
1548
        mutter('converting texts of revision {%s}',
 
 
1550
        parent_invs = map(self._load_updated_inventory, present_parents)
 
 
1553
            self._convert_file_version(rev, ie, parent_invs)
 
 
1555
    def _convert_file_version(self, rev, ie, parent_invs):
 
 
1556
        """Convert one version of one file.
 
 
1558
        The file needs to be added into the weave if it is a merge
 
 
1559
        of >=2 parents or if it's changed from its parent.
 
 
1561
        if ie.kind == 'root_directory':
 
 
1563
        file_id = ie.file_id
 
 
1564
        rev_id = rev.revision_id
 
 
1565
        w = self.text_weaves.get(file_id)
 
 
1568
            self.text_weaves[file_id] = w
 
 
1569
        text_changed = False
 
 
1570
        previous_entries = ie.find_previous_heads(parent_invs,
 
 
1574
        for old_revision in previous_entries:
 
 
1575
                # if this fails, its a ghost ?
 
 
1576
                assert old_revision in self.converted_revs 
 
 
1577
        self.snapshot_ie(previous_entries, ie, w, rev_id)
 
 
1579
        assert getattr(ie, 'revision', None) is not None
 
 
1581
    def snapshot_ie(self, previous_revisions, ie, w, rev_id):
 
 
1582
        # TODO: convert this logic, which is ~= snapshot to
 
 
1583
        # a call to:. This needs the path figured out. rather than a work_tree
 
 
1584
        # a v4 revision_tree can be given, or something that looks enough like
 
 
1585
        # one to give the file content to the entry if it needs it.
 
 
1586
        # and we need something that looks like a weave store for snapshot to 
 
 
1588
        #ie.snapshot(rev, PATH, previous_revisions, REVISION_TREE, InMemoryWeaveStore(self.text_weaves))
 
 
1589
        if len(previous_revisions) == 1:
 
 
1590
            previous_ie = previous_revisions.values()[0]
 
 
1591
            if ie._unchanged(previous_ie):
 
 
1592
                ie.revision = previous_ie.revision
 
 
1595
            text = self.branch.repository.text_store.get(ie.text_id)
 
 
1596
            file_lines = text.readlines()
 
 
1597
            assert sha_strings(file_lines) == ie.text_sha1
 
 
1598
            assert sum(map(len, file_lines)) == ie.text_size
 
 
1599
            w.add_lines(rev_id, previous_revisions, file_lines)
 
 
1600
            self.text_count += 1
 
 
1602
            w.add_lines(rev_id, previous_revisions, [])
 
 
1603
        ie.revision = rev_id
 
 
1605
    def _make_order(self):
 
 
1606
        """Return a suitable order for importing revisions.
 
 
1608
        The order must be such that an revision is imported after all
 
 
1609
        its (present) parents.
 
 
1611
        todo = set(self.revisions.keys())
 
 
1612
        done = self.absent_revisions.copy()
 
 
1615
            # scan through looking for a revision whose parents
 
 
1617
            for rev_id in sorted(list(todo)):
 
 
1618
                rev = self.revisions[rev_id]
 
 
1619
                parent_ids = set(rev.parent_ids)
 
 
1620
                if parent_ids.issubset(done):
 
 
1621
                    # can take this one now
 
 
1622
                    order.append(rev_id)
 
 
1628
class ConvertBzrDir5To6(Converter):
 
 
1629
    """Converts format 5 bzr dirs to format 6."""
 
 
1631
    def convert(self, to_convert, pb):
 
 
1632
        """See Converter.convert()."""
 
 
1633
        self.bzrdir = to_convert
 
 
1635
        self.pb.note('starting upgrade from format 5 to 6')
 
 
1636
        self._convert_to_prefixed()
 
 
1637
        return BzrDir.open(self.bzrdir.root_transport.base)
 
 
1639
    def _convert_to_prefixed(self):
 
 
1640
        from bzrlib.store import TransportStore
 
 
1641
        self.bzrdir.transport.delete('branch-format')
 
 
1642
        for store_name in ["weaves", "revision-store"]:
 
 
1643
            self.pb.note("adding prefixes to %s" % store_name)
 
 
1644
            store_transport = self.bzrdir.transport.clone(store_name)
 
 
1645
            store = TransportStore(store_transport, prefixed=True)
 
 
1646
            for urlfilename in store_transport.list_dir('.'):
 
 
1647
                filename = urlunescape(urlfilename)
 
 
1648
                if (filename.endswith(".weave") or
 
 
1649
                    filename.endswith(".gz") or
 
 
1650
                    filename.endswith(".sig")):
 
 
1651
                    file_id = os.path.splitext(filename)[0]
 
 
1654
                prefix_dir = store.hash_prefix(file_id)
 
 
1655
                # FIXME keep track of the dirs made RBC 20060121
 
 
1657
                    store_transport.move(filename, prefix_dir + '/' + filename)
 
 
1658
                except errors.NoSuchFile: # catches missing dirs strangely enough
 
 
1659
                    store_transport.mkdir(prefix_dir)
 
 
1660
                    store_transport.move(filename, prefix_dir + '/' + filename)
 
 
1661
        self.bzrdir._control_files.put_utf8('branch-format', BzrDirFormat6().get_format_string())
 
 
1664
class ConvertBzrDir6ToMeta(Converter):
 
 
1665
    """Converts format 6 bzr dirs to metadirs."""
 
 
1667
    def convert(self, to_convert, pb):
 
 
1668
        """See Converter.convert()."""
 
 
1669
        self.bzrdir = to_convert
 
 
1672
        self.total = 20 # the steps we know about
 
 
1673
        self.garbage_inventories = []
 
 
1675
        self.pb.note('starting upgrade from format 6 to metadir')
 
 
1676
        self.bzrdir._control_files.put_utf8('branch-format', "Converting to format 6")
 
 
1677
        # its faster to move specific files around than to open and use the apis...
 
 
1678
        # first off, nuke ancestry.weave, it was never used.
 
 
1680
            self.step('Removing ancestry.weave')
 
 
1681
            self.bzrdir.transport.delete('ancestry.weave')
 
 
1682
        except errors.NoSuchFile:
 
 
1684
        # find out whats there
 
 
1685
        self.step('Finding branch files')
 
 
1686
        last_revision = self.bzrdir.open_branch().last_revision()
 
 
1687
        bzrcontents = self.bzrdir.transport.list_dir('.')
 
 
1688
        for name in bzrcontents:
 
 
1689
            if name.startswith('basis-inventory.'):
 
 
1690
                self.garbage_inventories.append(name)
 
 
1691
        # create new directories for repository, working tree and branch
 
 
1692
        self.dir_mode = self.bzrdir._control_files._dir_mode
 
 
1693
        self.file_mode = self.bzrdir._control_files._file_mode
 
 
1694
        repository_names = [('inventory.weave', True),
 
 
1695
                            ('revision-store', True),
 
 
1697
        self.step('Upgrading repository  ')
 
 
1698
        self.bzrdir.transport.mkdir('repository', mode=self.dir_mode)
 
 
1699
        self.make_lock('repository')
 
 
1700
        # we hard code the formats here because we are converting into
 
 
1701
        # the meta format. The meta format upgrader can take this to a 
 
 
1702
        # future format within each component.
 
 
1703
        self.put_format('repository', bzrlib.repository.RepositoryFormat7())
 
 
1704
        for entry in repository_names:
 
 
1705
            self.move_entry('repository', entry)
 
 
1707
        self.step('Upgrading branch      ')
 
 
1708
        self.bzrdir.transport.mkdir('branch', mode=self.dir_mode)
 
 
1709
        self.make_lock('branch')
 
 
1710
        self.put_format('branch', bzrlib.branch.BzrBranchFormat5())
 
 
1711
        branch_files = [('revision-history', True),
 
 
1712
                        ('branch-name', True),
 
 
1714
        for entry in branch_files:
 
 
1715
            self.move_entry('branch', entry)
 
 
1717
        self.step('Upgrading working tree')
 
 
1718
        self.bzrdir.transport.mkdir('checkout', mode=self.dir_mode)
 
 
1719
        self.make_lock('checkout')
 
 
1720
        self.put_format('checkout', bzrlib.workingtree.WorkingTreeFormat3())
 
 
1721
        self.bzrdir.transport.delete_multi(self.garbage_inventories, self.pb)
 
 
1722
        checkout_files = [('pending-merges', True),
 
 
1723
                          ('inventory', True),
 
 
1724
                          ('stat-cache', False)]
 
 
1725
        for entry in checkout_files:
 
 
1726
            self.move_entry('checkout', entry)
 
 
1727
        if last_revision is not None:
 
 
1728
            self.bzrdir._control_files.put_utf8('checkout/last-revision',
 
 
1730
        self.bzrdir._control_files.put_utf8('branch-format', BzrDirMetaFormat1().get_format_string())
 
 
1731
        return BzrDir.open(self.bzrdir.root_transport.base)
 
 
1733
    def make_lock(self, name):
 
 
1734
        """Make a lock for the new control dir name."""
 
 
1735
        self.step('Make %s lock' % name)
 
 
1736
        ld = LockDir(self.bzrdir.transport, 
 
 
1738
                     file_modebits=self.file_mode,
 
 
1739
                     dir_modebits=self.dir_mode)
 
 
1742
    def move_entry(self, new_dir, entry):
 
 
1743
        """Move then entry name into new_dir."""
 
 
1745
        mandatory = entry[1]
 
 
1746
        self.step('Moving %s' % name)
 
 
1748
            self.bzrdir.transport.move(name, '%s/%s' % (new_dir, name))
 
 
1749
        except errors.NoSuchFile:
 
 
1753
    def put_format(self, dirname, format):
 
 
1754
        self.bzrdir._control_files.put_utf8('%s/format' % dirname, format.get_format_string())
 
 
1757
class ConvertMetaToMeta(Converter):
 
 
1758
    """Converts the components of metadirs."""
 
 
1760
    def __init__(self, target_format):
 
 
1761
        """Create a metadir to metadir converter.
 
 
1763
        :param target_format: The final metadir format that is desired.
 
 
1765
        self.target_format = target_format
 
 
1767
    def convert(self, to_convert, pb):
 
 
1768
        """See Converter.convert()."""
 
 
1769
        self.bzrdir = to_convert
 
 
1773
        self.step('checking repository format')
 
 
1775
            repo = self.bzrdir.open_repository()
 
 
1776
        except errors.NoRepositoryPresent:
 
 
1779
            if not isinstance(repo._format, self.target_format.repository_format.__class__):
 
 
1780
                from bzrlib.repository import CopyConverter
 
 
1781
                self.pb.note('starting repository conversion')
 
 
1782
                converter = CopyConverter(self.target_format.repository_format)
 
 
1783
                converter.convert(repo, pb)