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
# TODO: remove unittest dependency; put that stuff inside the test suite
25
from copy import deepcopy
26
from cStringIO import StringIO
28
from stat import S_ISDIR
29
from unittest import TestSuite
32
import bzrlib.errors as errors
33
from bzrlib.lockable_files import LockableFiles, TransportLock
34
from bzrlib.lockdir import LockDir
35
from bzrlib.osutils import (
42
import bzrlib.revision
43
from bzrlib.store.revision.text import TextRevisionStore
44
from bzrlib.store.text import TextStore
45
from bzrlib.store.versioned import WeaveStore
46
from bzrlib.trace import mutter
47
from bzrlib.transactions import WriteTransaction
48
from bzrlib.transport import get_transport
49
from bzrlib.transport.local import LocalTransport
50
import bzrlib.urlutils as urlutils
51
from bzrlib.weave import Weave
52
from bzrlib.xml4 import serializer_v4
57
"""A .bzr control diretory.
59
BzrDir instances let you create or open any of the things that can be
60
found within .bzr - checkouts, branches and repositories.
63
the transport which this bzr dir is rooted at (i.e. file:///.../.bzr/)
65
a transport connected to the directory this bzr was opened from.
69
"""Invoke break_lock on the first object in the bzrdir.
71
If there is a tree, the tree is opened and break_lock() called.
72
Otherwise, branch is tried, and finally repository.
75
thing_to_unlock = self.open_workingtree()
76
except (errors.NotLocalUrl, errors.NoWorkingTree):
78
thing_to_unlock = self.open_branch()
79
except errors.NotBranchError:
81
thing_to_unlock = self.open_repository()
82
except errors.NoRepositoryPresent:
84
thing_to_unlock.break_lock()
86
def can_convert_format(self):
87
"""Return true if this bzrdir is one whose format we can convert from."""
91
def _check_supported(format, allow_unsupported):
92
"""Check whether format is a supported format.
94
If allow_unsupported is True, this is a no-op.
96
if not allow_unsupported and not format.is_supported():
97
# see open_downlevel to open legacy branches.
98
raise errors.UnsupportedFormatError(format=format)
100
def clone(self, url, revision_id=None, basis=None, force_new_repo=False):
101
"""Clone this bzrdir and its contents to url verbatim.
103
If urls last component does not exist, it will be created.
105
if revision_id is not None, then the clone operation may tune
106
itself to download less data.
107
:param force_new_repo: Do not use a shared repository for the target
108
even if one is available.
111
basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
112
result = self._format.initialize(url)
114
local_repo = self.find_repository()
115
except errors.NoRepositoryPresent:
118
# may need to copy content in
120
result_repo = local_repo.clone(
122
revision_id=revision_id,
124
result_repo.set_make_working_trees(local_repo.make_working_trees())
127
result_repo = result.find_repository()
128
# fetch content this dir needs.
130
# XXX FIXME RBC 20060214 need tests for this when the basis
132
result_repo.fetch(basis_repo, revision_id=revision_id)
133
result_repo.fetch(local_repo, revision_id=revision_id)
134
except errors.NoRepositoryPresent:
135
# needed to make one anyway.
136
result_repo = local_repo.clone(
138
revision_id=revision_id,
140
result_repo.set_make_working_trees(local_repo.make_working_trees())
141
# 1 if there is a branch present
142
# make sure its content is available in the target repository
145
self.open_branch().clone(result, revision_id=revision_id)
146
except errors.NotBranchError:
149
self.open_workingtree().clone(result, basis=basis_tree)
150
except (errors.NoWorkingTree, errors.NotLocalUrl):
154
def _get_basis_components(self, basis):
155
"""Retrieve the basis components that are available at basis."""
157
return None, None, None
159
basis_tree = basis.open_workingtree()
160
basis_branch = basis_tree.branch
161
basis_repo = basis_branch.repository
162
except (errors.NoWorkingTree, errors.NotLocalUrl):
165
basis_branch = basis.open_branch()
166
basis_repo = basis_branch.repository
167
except errors.NotBranchError:
170
basis_repo = basis.open_repository()
171
except errors.NoRepositoryPresent:
173
return basis_repo, basis_branch, basis_tree
175
# TODO: This should be given a Transport, and should chdir up; otherwise
176
# this will open a new connection.
177
def _make_tail(self, url):
178
head, tail = urlutils.split(url)
179
if tail and tail != '.':
180
t = bzrlib.transport.get_transport(head)
183
except errors.FileExists:
186
# TODO: Should take a Transport
188
def create(cls, base):
189
"""Create a new BzrDir at the url 'base'.
191
This will call the current default formats initialize with base
192
as the only parameter.
194
If you need a specific format, consider creating an instance
195
of that and calling initialize().
197
if cls is not BzrDir:
198
raise AssertionError("BzrDir.create always creates the default format, "
199
"not one of %r" % cls)
200
head, tail = urlutils.split(base)
201
if tail and tail != '.':
202
t = bzrlib.transport.get_transport(head)
205
except errors.FileExists:
207
return BzrDirFormat.get_default_format().initialize(safe_unicode(base))
209
def create_branch(self):
210
"""Create a branch in this BzrDir.
212
The bzrdirs format will control what branch format is created.
213
For more control see BranchFormatXX.create(a_bzrdir).
215
raise NotImplementedError(self.create_branch)
218
def create_branch_and_repo(base, force_new_repo=False):
219
"""Create a new BzrDir, Branch and Repository at the url 'base'.
221
This will use the current default BzrDirFormat, and use whatever
222
repository format that that uses via bzrdir.create_branch and
223
create_repository. If a shared repository is available that is used
226
The created Branch object is returned.
228
:param base: The URL to create the branch at.
229
:param force_new_repo: If True a new repository is always created.
231
bzrdir = BzrDir.create(base)
232
bzrdir._find_or_create_repository(force_new_repo)
233
return bzrdir.create_branch()
235
def _find_or_create_repository(self, force_new_repo):
236
"""Create a new repository if needed, returning the repository."""
238
return self.create_repository()
240
return self.find_repository()
241
except errors.NoRepositoryPresent:
242
return self.create_repository()
245
def create_branch_convenience(base, force_new_repo=False,
246
force_new_tree=None, format=None):
247
"""Create a new BzrDir, Branch and Repository at the url 'base'.
249
This is a convenience function - it will use an existing repository
250
if possible, can be told explicitly whether to create a working tree or
253
This will use the current default BzrDirFormat, and use whatever
254
repository format that that uses via bzrdir.create_branch and
255
create_repository. If a shared repository is available that is used
256
preferentially. Whatever repository is used, its tree creation policy
259
The created Branch object is returned.
260
If a working tree cannot be made due to base not being a file:// url,
261
no error is raised unless force_new_tree is True, in which case no
262
data is created on disk and NotLocalUrl is raised.
264
:param base: The URL to create the branch at.
265
:param force_new_repo: If True a new repository is always created.
266
:param force_new_tree: If True or False force creation of a tree or
267
prevent such creation respectively.
268
:param format: Override for the for the bzrdir format to create
271
# check for non local urls
272
t = get_transport(safe_unicode(base))
273
if not isinstance(t, LocalTransport):
274
raise errors.NotLocalUrl(base)
276
bzrdir = BzrDir.create(base)
278
bzrdir = format.initialize(base)
279
repo = bzrdir._find_or_create_repository(force_new_repo)
280
result = bzrdir.create_branch()
281
if force_new_tree or (repo.make_working_trees() and
282
force_new_tree is None):
284
bzrdir.create_workingtree()
285
except errors.NotLocalUrl:
290
def create_repository(base, shared=False):
291
"""Create a new BzrDir and Repository at the url 'base'.
293
This will use the current default BzrDirFormat, and use whatever
294
repository format that that uses for bzrdirformat.create_repository.
296
;param shared: Create a shared repository rather than a standalone
298
The Repository object is returned.
300
This must be overridden as an instance method in child classes, where
301
it should take no parameters and construct whatever repository format
302
that child class desires.
304
bzrdir = BzrDir.create(base)
305
return bzrdir.create_repository(shared)
308
def create_standalone_workingtree(base):
309
"""Create a new BzrDir, WorkingTree, Branch and Repository at 'base'.
311
'base' must be a local path or a file:// url.
313
This will use the current default BzrDirFormat, and use whatever
314
repository format that that uses for bzrdirformat.create_workingtree,
315
create_branch and create_repository.
317
The WorkingTree object is returned.
319
t = get_transport(safe_unicode(base))
320
if not isinstance(t, LocalTransport):
321
raise errors.NotLocalUrl(base)
322
bzrdir = BzrDir.create_branch_and_repo(safe_unicode(base),
323
force_new_repo=True).bzrdir
324
return bzrdir.create_workingtree()
326
def create_workingtree(self, revision_id=None):
327
"""Create a working tree at this BzrDir.
329
revision_id: create it as of this revision id.
331
raise NotImplementedError(self.create_workingtree)
333
def find_repository(self):
334
"""Find the repository that should be used for a_bzrdir.
336
This does not require a branch as we use it to find the repo for
337
new branches as well as to hook existing branches up to their
341
return self.open_repository()
342
except errors.NoRepositoryPresent:
344
next_transport = self.root_transport.clone('..')
346
# find the next containing bzrdir
348
found_bzrdir = BzrDir.open_containing_from_transport(
350
except errors.NotBranchError:
352
raise errors.NoRepositoryPresent(self)
353
# does it have a repository ?
355
repository = found_bzrdir.open_repository()
356
except errors.NoRepositoryPresent:
357
next_transport = found_bzrdir.root_transport.clone('..')
358
if (found_bzrdir.root_transport.base == next_transport.base):
359
# top of the file system
363
if ((found_bzrdir.root_transport.base ==
364
self.root_transport.base) or repository.is_shared()):
367
raise errors.NoRepositoryPresent(self)
368
raise errors.NoRepositoryPresent(self)
370
def get_branch_transport(self, branch_format):
371
"""Get the transport for use by branch format in this BzrDir.
373
Note that bzr dirs that do not support format strings will raise
374
IncompatibleFormat if the branch format they are given has
375
a format string, and vice versa.
377
If branch_format is None, the transport is returned with no
378
checking. if it is not None, then the returned transport is
379
guaranteed to point to an existing directory ready for use.
381
raise NotImplementedError(self.get_branch_transport)
383
def get_repository_transport(self, repository_format):
384
"""Get the transport for use by repository format in this BzrDir.
386
Note that bzr dirs that do not support format strings will raise
387
IncompatibleFormat if the repository format they are given has
388
a format string, and vice versa.
390
If repository_format is None, the transport is returned with no
391
checking. if it is not None, then the returned transport is
392
guaranteed to point to an existing directory ready for use.
394
raise NotImplementedError(self.get_repository_transport)
396
def get_workingtree_transport(self, tree_format):
397
"""Get the transport for use by workingtree format in this BzrDir.
399
Note that bzr dirs that do not support format strings will raise
400
IncompatibleFormat if the workingtree format they are given has
401
a format string, and vice versa.
403
If workingtree_format is None, the transport is returned with no
404
checking. if it is not None, then the returned transport is
405
guaranteed to point to an existing directory ready for use.
407
raise NotImplementedError(self.get_workingtree_transport)
409
def __init__(self, _transport, _format):
410
"""Initialize a Bzr control dir object.
412
Only really common logic should reside here, concrete classes should be
413
made with varying behaviours.
415
:param _format: the format that is creating this BzrDir instance.
416
:param _transport: the transport this dir is based at.
418
self._format = _format
419
self.transport = _transport.clone('.bzr')
420
self.root_transport = _transport
422
def is_control_filename(self, filename):
423
"""True if filename is the name of a path which is reserved for bzrdir's.
425
:param filename: A filename within the root transport of this bzrdir.
427
This is true IF and ONLY IF the filename is part of the namespace reserved
428
for bzr control dirs. Currently this is the '.bzr' directory in the root
429
of the root_transport. it is expected that plugins will need to extend
430
this in the future - for instance to make bzr talk with svn working
433
# this might be better on the BzrDirFormat class because it refers to
434
# all the possible bzrdir disk formats.
435
# This method is tested via the workingtree is_control_filename tests-
436
# it was extracted from WorkingTree.is_control_filename. If the methods
437
# contract is extended beyond the current trivial implementation please
438
# add new tests for it to the appropriate place.
439
return filename == '.bzr' or filename.startswith('.bzr/')
441
def needs_format_conversion(self, format=None):
442
"""Return true if this bzrdir needs convert_format run on it.
444
For instance, if the repository format is out of date but the
445
branch and working tree are not, this should return True.
447
:param format: Optional parameter indicating a specific desired
448
format we plan to arrive at.
450
raise NotImplementedError(self.needs_format_conversion)
453
def open_unsupported(base):
454
"""Open a branch which is not supported."""
455
return BzrDir.open(base, _unsupported=True)
458
def open(base, _unsupported=False):
459
"""Open an existing bzrdir, rooted at 'base' (url)
461
_unsupported is a private parameter to the BzrDir class.
463
t = get_transport(base)
464
# mutter("trying to open %r with transport %r", base, t)
465
format = BzrDirFormat.find_format(t)
466
BzrDir._check_supported(format, _unsupported)
467
return format.open(t, _found=True)
469
def open_branch(self, unsupported=False):
470
"""Open the branch object at this BzrDir if one is present.
472
If unsupported is True, then no longer supported branch formats can
475
TODO: static convenience version of this?
477
raise NotImplementedError(self.open_branch)
480
def open_containing(url):
481
"""Open an existing branch which contains url.
483
:param url: url to search from.
484
See open_containing_from_transport for more detail.
486
return BzrDir.open_containing_from_transport(get_transport(url))
489
def open_containing_from_transport(a_transport):
490
"""Open an existing branch which contains a_transport.base
492
This probes for a branch at a_transport, and searches upwards from there.
494
Basically we keep looking up until we find the control directory or
495
run into the root. If there isn't one, raises NotBranchError.
496
If there is one and it is either an unrecognised format or an unsupported
497
format, UnknownFormatError or UnsupportedFormatError are raised.
498
If there is one, it is returned, along with the unused portion of url.
500
:return: The BzrDir that contains the path, and a Unicode path
501
for the rest of the URL.
503
# this gets the normalised url back. I.e. '.' -> the full path.
504
url = a_transport.base
507
format = BzrDirFormat.find_format(a_transport)
508
BzrDir._check_supported(format, False)
509
return format.open(a_transport), urlutils.unescape(a_transport.relpath(url))
510
except errors.NotBranchError, e:
511
## mutter('not a branch in: %r %s', a_transport.base, e)
513
new_t = a_transport.clone('..')
514
if new_t.base == a_transport.base:
515
# reached the root, whatever that may be
516
raise errors.NotBranchError(path=url)
519
def open_repository(self, _unsupported=False):
520
"""Open the repository object at this BzrDir if one is present.
522
This will not follow the Branch object pointer - its strictly a direct
523
open facility. Most client code should use open_branch().repository to
526
_unsupported is a private parameter, not part of the api.
527
TODO: static convenience version of this?
529
raise NotImplementedError(self.open_repository)
531
def open_workingtree(self, _unsupported=False):
532
"""Open the workingtree object at this BzrDir if one is present.
534
TODO: static convenience version of this?
536
raise NotImplementedError(self.open_workingtree)
538
def has_branch(self):
539
"""Tell if this bzrdir contains a branch.
541
Note: if you're going to open the branch, you should just go ahead
542
and try, and not ask permission first. (This method just opens the
543
branch and discards it, and that's somewhat expensive.)
548
except errors.NotBranchError:
551
def has_workingtree(self):
552
"""Tell if this bzrdir contains a working tree.
554
This will still raise an exception if the bzrdir has a workingtree that
555
is remote & inaccessible.
557
Note: if you're going to open the working tree, you should just go ahead
558
and try, and not ask permission first. (This method just opens the
559
workingtree and discards it, and that's somewhat expensive.)
562
self.open_workingtree()
564
except errors.NoWorkingTree:
567
def sprout(self, url, revision_id=None, basis=None, force_new_repo=False):
568
"""Create a copy of this bzrdir prepared for use as a new line of
571
If urls last component does not exist, it will be created.
573
Attributes related to the identity of the source branch like
574
branch nickname will be cleaned, a working tree is created
575
whether one existed before or not; and a local branch is always
578
if revision_id is not None, then the clone operation may tune
579
itself to download less data.
582
result = self._format.initialize(url)
583
basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
585
source_branch = self.open_branch()
586
source_repository = source_branch.repository
587
except errors.NotBranchError:
590
source_repository = self.open_repository()
591
except errors.NoRepositoryPresent:
592
# copy the entire basis one if there is one
593
# but there is no repository.
594
source_repository = basis_repo
599
result_repo = result.find_repository()
600
except errors.NoRepositoryPresent:
602
if source_repository is None and result_repo is not None:
604
elif source_repository is None and result_repo is None:
605
# no repo available, make a new one
606
result.create_repository()
607
elif source_repository is not None and result_repo is None:
608
# have source, and want to make a new target repo
609
# we don't clone the repo because that preserves attributes
610
# like is_shared(), and we have not yet implemented a
611
# repository sprout().
612
result_repo = result.create_repository()
613
if result_repo is not None:
614
# fetch needed content into target.
616
# XXX FIXME RBC 20060214 need tests for this when the basis
618
result_repo.fetch(basis_repo, revision_id=revision_id)
619
if source_repository is not None:
620
result_repo.fetch(source_repository, revision_id=revision_id)
621
if source_branch is not None:
622
source_branch.sprout(result, revision_id=revision_id)
624
result.create_branch()
625
# TODO: jam 20060426 we probably need a test in here in the
626
# case that the newly sprouted branch is a remote one
627
if result_repo is None or result_repo.make_working_trees():
628
result.create_workingtree()
632
class BzrDirPreSplitOut(BzrDir):
633
"""A common class for the all-in-one formats."""
635
def __init__(self, _transport, _format):
636
"""See BzrDir.__init__."""
637
super(BzrDirPreSplitOut, self).__init__(_transport, _format)
638
assert self._format._lock_class == TransportLock
639
assert self._format._lock_file_name == 'branch-lock'
640
self._control_files = LockableFiles(self.get_branch_transport(None),
641
self._format._lock_file_name,
642
self._format._lock_class)
644
def break_lock(self):
645
"""Pre-splitout bzrdirs do not suffer from stale locks."""
646
raise NotImplementedError(self.break_lock)
648
def clone(self, url, revision_id=None, basis=None, force_new_repo=False):
649
"""See BzrDir.clone()."""
650
from bzrlib.workingtree import WorkingTreeFormat2
652
result = self._format._initialize_for_clone(url)
653
basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
654
self.open_repository().clone(result, revision_id=revision_id, basis=basis_repo)
655
from_branch = self.open_branch()
656
from_branch.clone(result, revision_id=revision_id)
658
self.open_workingtree().clone(result, basis=basis_tree)
659
except errors.NotLocalUrl:
660
# make a new one, this format always has to have one.
662
WorkingTreeFormat2().initialize(result)
663
except errors.NotLocalUrl:
664
# but we cannot do it for remote trees.
665
to_branch = result.open_branch()
666
WorkingTreeFormat2().stub_initialize_remote(to_branch.control_files)
669
def create_branch(self):
670
"""See BzrDir.create_branch."""
671
return self.open_branch()
673
def create_repository(self, shared=False):
674
"""See BzrDir.create_repository."""
676
raise errors.IncompatibleFormat('shared repository', self._format)
677
return self.open_repository()
679
def create_workingtree(self, revision_id=None):
680
"""See BzrDir.create_workingtree."""
681
# this looks buggy but is not -really-
682
# clone and sprout will have set the revision_id
683
# and that will have set it for us, its only
684
# specific uses of create_workingtree in isolation
685
# that can do wonky stuff here, and that only
686
# happens for creating checkouts, which cannot be
687
# done on this format anyway. So - acceptable wart.
688
result = self.open_workingtree()
689
if revision_id is not None:
690
if revision_id == bzrlib.revision.NULL_REVISION:
691
result.set_parent_ids([])
693
result.set_parent_ids([revision_id])
696
def get_branch_transport(self, branch_format):
697
"""See BzrDir.get_branch_transport()."""
698
if branch_format is None:
699
return self.transport
701
branch_format.get_format_string()
702
except NotImplementedError:
703
return self.transport
704
raise errors.IncompatibleFormat(branch_format, self._format)
706
def get_repository_transport(self, repository_format):
707
"""See BzrDir.get_repository_transport()."""
708
if repository_format is None:
709
return self.transport
711
repository_format.get_format_string()
712
except NotImplementedError:
713
return self.transport
714
raise errors.IncompatibleFormat(repository_format, self._format)
716
def get_workingtree_transport(self, workingtree_format):
717
"""See BzrDir.get_workingtree_transport()."""
718
if workingtree_format is None:
719
return self.transport
721
workingtree_format.get_format_string()
722
except NotImplementedError:
723
return self.transport
724
raise errors.IncompatibleFormat(workingtree_format, self._format)
726
def needs_format_conversion(self, format=None):
727
"""See BzrDir.needs_format_conversion()."""
728
# if the format is not the same as the system default,
729
# an upgrade is needed.
731
format = BzrDirFormat.get_default_format()
732
return not isinstance(self._format, format.__class__)
734
def open_branch(self, unsupported=False):
735
"""See BzrDir.open_branch."""
736
from bzrlib.branch import BzrBranchFormat4
737
format = BzrBranchFormat4()
738
self._check_supported(format, unsupported)
739
return format.open(self, _found=True)
741
def sprout(self, url, revision_id=None, basis=None, force_new_repo=False):
742
"""See BzrDir.sprout()."""
743
from bzrlib.workingtree import WorkingTreeFormat2
745
result = self._format._initialize_for_clone(url)
746
basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
748
self.open_repository().clone(result, revision_id=revision_id, basis=basis_repo)
749
except errors.NoRepositoryPresent:
752
self.open_branch().sprout(result, revision_id=revision_id)
753
except errors.NotBranchError:
755
# we always want a working tree
756
WorkingTreeFormat2().initialize(result)
760
class BzrDir4(BzrDirPreSplitOut):
761
"""A .bzr version 4 control object.
763
This is a deprecated format and may be removed after sept 2006.
766
def create_repository(self, shared=False):
767
"""See BzrDir.create_repository."""
768
return self._format.repository_format.initialize(self, shared)
770
def needs_format_conversion(self, format=None):
771
"""Format 4 dirs are always in need of conversion."""
774
def open_repository(self):
775
"""See BzrDir.open_repository."""
776
from bzrlib.repository import RepositoryFormat4
777
return RepositoryFormat4().open(self, _found=True)
780
class BzrDir5(BzrDirPreSplitOut):
781
"""A .bzr version 5 control object.
783
This is a deprecated format and may be removed after sept 2006.
786
def open_repository(self):
787
"""See BzrDir.open_repository."""
788
from bzrlib.repository import RepositoryFormat5
789
return RepositoryFormat5().open(self, _found=True)
791
def open_workingtree(self, _unsupported=False):
792
"""See BzrDir.create_workingtree."""
793
from bzrlib.workingtree import WorkingTreeFormat2
794
return WorkingTreeFormat2().open(self, _found=True)
797
class BzrDir6(BzrDirPreSplitOut):
798
"""A .bzr version 6 control object.
800
This is a deprecated format and may be removed after sept 2006.
803
def open_repository(self):
804
"""See BzrDir.open_repository."""
805
from bzrlib.repository import RepositoryFormat6
806
return RepositoryFormat6().open(self, _found=True)
808
def open_workingtree(self, _unsupported=False):
809
"""See BzrDir.create_workingtree."""
810
from bzrlib.workingtree import WorkingTreeFormat2
811
return WorkingTreeFormat2().open(self, _found=True)
814
class BzrDirMeta1(BzrDir):
815
"""A .bzr meta version 1 control object.
817
This is the first control object where the
818
individual aspects are really split out: there are separate repository,
819
workingtree and branch subdirectories and any subset of the three can be
820
present within a BzrDir.
823
def can_convert_format(self):
824
"""See BzrDir.can_convert_format()."""
827
def create_branch(self):
828
"""See BzrDir.create_branch."""
829
from bzrlib.branch import BranchFormat
830
return BranchFormat.get_default_format().initialize(self)
832
def create_repository(self, shared=False):
833
"""See BzrDir.create_repository."""
834
return self._format.repository_format.initialize(self, shared)
836
def create_workingtree(self, revision_id=None):
837
"""See BzrDir.create_workingtree."""
838
from bzrlib.workingtree import WorkingTreeFormat
839
return WorkingTreeFormat.get_default_format().initialize(self, revision_id)
841
def _get_mkdir_mode(self):
842
"""Figure out the mode to use when creating a bzrdir subdir."""
843
temp_control = LockableFiles(self.transport, '', TransportLock)
844
return temp_control._dir_mode
846
def get_branch_transport(self, branch_format):
847
"""See BzrDir.get_branch_transport()."""
848
if branch_format is None:
849
return self.transport.clone('branch')
851
branch_format.get_format_string()
852
except NotImplementedError:
853
raise errors.IncompatibleFormat(branch_format, self._format)
855
self.transport.mkdir('branch', mode=self._get_mkdir_mode())
856
except errors.FileExists:
858
return self.transport.clone('branch')
860
def get_repository_transport(self, repository_format):
861
"""See BzrDir.get_repository_transport()."""
862
if repository_format is None:
863
return self.transport.clone('repository')
865
repository_format.get_format_string()
866
except NotImplementedError:
867
raise errors.IncompatibleFormat(repository_format, self._format)
869
self.transport.mkdir('repository', mode=self._get_mkdir_mode())
870
except errors.FileExists:
872
return self.transport.clone('repository')
874
def get_workingtree_transport(self, workingtree_format):
875
"""See BzrDir.get_workingtree_transport()."""
876
if workingtree_format is None:
877
return self.transport.clone('checkout')
879
workingtree_format.get_format_string()
880
except NotImplementedError:
881
raise errors.IncompatibleFormat(workingtree_format, self._format)
883
self.transport.mkdir('checkout', mode=self._get_mkdir_mode())
884
except errors.FileExists:
886
return self.transport.clone('checkout')
888
def needs_format_conversion(self, format=None):
889
"""See BzrDir.needs_format_conversion()."""
891
format = BzrDirFormat.get_default_format()
892
if not isinstance(self._format, format.__class__):
893
# it is not a meta dir format, conversion is needed.
895
# we might want to push this down to the repository?
897
if not isinstance(self.open_repository()._format,
898
format.repository_format.__class__):
899
# the repository needs an upgrade.
901
except errors.NoRepositoryPresent:
903
# currently there are no other possible conversions for meta1 formats.
906
def open_branch(self, unsupported=False):
907
"""See BzrDir.open_branch."""
908
from bzrlib.branch import BranchFormat
909
format = BranchFormat.find_format(self)
910
self._check_supported(format, unsupported)
911
return format.open(self, _found=True)
913
def open_repository(self, unsupported=False):
914
"""See BzrDir.open_repository."""
915
from bzrlib.repository import RepositoryFormat
916
format = RepositoryFormat.find_format(self)
917
self._check_supported(format, unsupported)
918
return format.open(self, _found=True)
920
def open_workingtree(self, unsupported=False):
921
"""See BzrDir.open_workingtree."""
922
from bzrlib.workingtree import WorkingTreeFormat
923
format = WorkingTreeFormat.find_format(self)
924
self._check_supported(format, unsupported)
925
return format.open(self, _found=True)
928
class BzrDirFormat(object):
929
"""An encapsulation of the initialization and open routines for a format.
931
Formats provide three things:
932
* An initialization routine,
936
Formats are placed in an dict by their format string for reference
937
during bzrdir opening. These should be subclasses of BzrDirFormat
940
Once a format is deprecated, just deprecate the initialize and open
941
methods on the format class. Do not deprecate the object, as the
942
object will be created every system load.
945
_default_format = None
946
"""The default format used for new .bzr dirs."""
949
"""The known formats."""
951
_control_formats = []
952
"""The registered control formats - .bzr, ....
954
This is a list of BzrDirFormat objects.
957
_lock_file_name = 'branch-lock'
959
# _lock_class must be set in subclasses to the lock type, typ.
960
# TransportLock or LockDir
963
def find_format(klass, transport):
964
"""Return the format present at transport."""
965
for format in klass._control_formats:
967
return format.probe_transport(transport)
968
except errors.NotBranchError:
969
# this format does not find a control dir here.
971
raise errors.NotBranchError(path=transport.base)
974
def probe_transport(klass, transport):
975
"""Return the .bzrdir style transport present at URL."""
977
format_string = transport.get(".bzr/branch-format").read()
978
except errors.NoSuchFile:
979
raise errors.NotBranchError(path=transport.base)
982
return klass._formats[format_string]
984
raise errors.UnknownFormatError(format=format_string)
987
def get_default_format(klass):
988
"""Return the current default format."""
989
return klass._default_format
991
def get_format_string(self):
992
"""Return the ASCII format string that identifies this format."""
993
raise NotImplementedError(self.get_format_string)
995
def get_format_description(self):
996
"""Return the short description for this format."""
997
raise NotImplementedError(self.get_format_description)
999
def get_converter(self, format=None):
1000
"""Return the converter to use to convert bzrdirs needing converts.
1002
This returns a bzrlib.bzrdir.Converter object.
1004
This should return the best upgrader to step this format towards the
1005
current default format. In the case of plugins we can/should provide
1006
some means for them to extend the range of returnable converters.
1008
:param format: Optional format to override the default format of the
1011
raise NotImplementedError(self.get_converter)
1013
def initialize(self, url):
1014
"""Create a bzr control dir at this url and return an opened copy.
1016
Subclasses should typically override initialize_on_transport
1017
instead of this method.
1019
return self.initialize_on_transport(get_transport(url))
1021
def initialize_on_transport(self, transport):
1022
"""Initialize a new bzrdir in the base directory of a Transport."""
1023
# Since we don't have a .bzr directory, inherit the
1024
# mode from the root directory
1025
temp_control = LockableFiles(transport, '', TransportLock)
1026
temp_control._transport.mkdir('.bzr',
1027
# FIXME: RBC 20060121 don't peek under
1029
mode=temp_control._dir_mode)
1030
file_mode = temp_control._file_mode
1032
mutter('created control directory in ' + transport.base)
1033
control = transport.clone('.bzr')
1034
utf8_files = [('README',
1035
"This is a Bazaar-NG control directory.\n"
1036
"Do not change any files in this directory.\n"),
1037
('branch-format', self.get_format_string()),
1039
# NB: no need to escape relative paths that are url safe.
1040
control_files = LockableFiles(control, self._lock_file_name,
1042
control_files.create_lock()
1043
control_files.lock_write()
1045
for file, content in utf8_files:
1046
control_files.put_utf8(file, content)
1048
control_files.unlock()
1049
return self.open(transport, _found=True)
1051
def is_supported(self):
1052
"""Is this format supported?
1054
Supported formats must be initializable and openable.
1055
Unsupported formats may not support initialization or committing or
1056
some other features depending on the reason for not being supported.
1061
def known_formats(klass):
1062
"""Return all the known formats.
1064
Concrete formats should override _known_formats.
1066
# There is double indirection here to make sure that control
1067
# formats used by more than one dir format will only be probed
1068
# once. This can otherwise be quite expensive for remote connections.
1070
for format in klass._control_formats:
1071
result.update(format._known_formats())
1075
def _known_formats(klass):
1076
"""Return the known format instances for this control format."""
1077
return set(klass._formats.values())
1079
def open(self, transport, _found=False):
1080
"""Return an instance of this format for the dir transport points at.
1082
_found is a private parameter, do not use it.
1085
assert isinstance(BzrDirFormat.find_format(transport),
1087
return self._open(transport)
1089
def _open(self, transport):
1090
"""Template method helper for opening BzrDirectories.
1092
This performs the actual open and any additional logic or parameter
1095
raise NotImplementedError(self._open)
1098
def register_format(klass, format):
1099
klass._formats[format.get_format_string()] = format
1102
def register_control_format(klass, format):
1103
"""Register a format that does not use '.bzrdir' for its control dir.
1105
TODO: This should be pulled up into a 'ControlDirFormat' base class
1106
which BzrDirFormat can inherit from, and renamed to register_format
1107
there. It has been done without that for now for simplicity of
1110
klass._control_formats.append(format)
1113
def set_default_format(klass, format):
1114
klass._default_format = format
1117
return self.get_format_string()[:-1]
1120
def unregister_format(klass, format):
1121
assert klass._formats[format.get_format_string()] is format
1122
del klass._formats[format.get_format_string()]
1125
def unregister_control_format(klass, format):
1126
klass._control_formats.remove(format)
1129
# register BzrDirFormat as a control format
1130
BzrDirFormat.register_control_format(BzrDirFormat)
1133
class BzrDirFormat4(BzrDirFormat):
1134
"""Bzr dir format 4.
1136
This format is a combined format for working tree, branch and repository.
1138
- Format 1 working trees [always]
1139
- Format 4 branches [always]
1140
- Format 4 repositories [always]
1142
This format is deprecated: it indexes texts using a text it which is
1143
removed in format 5; write support for this format has been removed.
1146
_lock_class = TransportLock
1148
def get_format_string(self):
1149
"""See BzrDirFormat.get_format_string()."""
1150
return "Bazaar-NG branch, format 0.0.4\n"
1152
def get_format_description(self):
1153
"""See BzrDirFormat.get_format_description()."""
1154
return "All-in-one format 4"
1156
def get_converter(self, format=None):
1157
"""See BzrDirFormat.get_converter()."""
1158
# there is one and only one upgrade path here.
1159
return ConvertBzrDir4To5()
1161
def initialize_on_transport(self, transport):
1162
"""Format 4 branches cannot be created."""
1163
raise errors.UninitializableFormat(self)
1165
def is_supported(self):
1166
"""Format 4 is not supported.
1168
It is not supported because the model changed from 4 to 5 and the
1169
conversion logic is expensive - so doing it on the fly was not
1174
def _open(self, transport):
1175
"""See BzrDirFormat._open."""
1176
return BzrDir4(transport, self)
1178
def __return_repository_format(self):
1179
"""Circular import protection."""
1180
from bzrlib.repository import RepositoryFormat4
1181
return RepositoryFormat4(self)
1182
repository_format = property(__return_repository_format)
1185
class BzrDirFormat5(BzrDirFormat):
1186
"""Bzr control format 5.
1188
This format is a combined format for working tree, branch and repository.
1190
- Format 2 working trees [always]
1191
- Format 4 branches [always]
1192
- Format 5 repositories [always]
1193
Unhashed stores in the repository.
1196
_lock_class = TransportLock
1198
def get_format_string(self):
1199
"""See BzrDirFormat.get_format_string()."""
1200
return "Bazaar-NG branch, format 5\n"
1202
def get_format_description(self):
1203
"""See BzrDirFormat.get_format_description()."""
1204
return "All-in-one format 5"
1206
def get_converter(self, format=None):
1207
"""See BzrDirFormat.get_converter()."""
1208
# there is one and only one upgrade path here.
1209
return ConvertBzrDir5To6()
1211
def _initialize_for_clone(self, url):
1212
return self.initialize_on_transport(get_transport(url), _cloning=True)
1214
def initialize_on_transport(self, transport, _cloning=False):
1215
"""Format 5 dirs always have working tree, branch and repository.
1217
Except when they are being cloned.
1219
from bzrlib.branch import BzrBranchFormat4
1220
from bzrlib.repository import RepositoryFormat5
1221
from bzrlib.workingtree import WorkingTreeFormat2
1222
result = (super(BzrDirFormat5, self).initialize_on_transport(transport))
1223
RepositoryFormat5().initialize(result, _internal=True)
1225
branch = BzrBranchFormat4().initialize(result)
1227
WorkingTreeFormat2().initialize(result)
1228
except errors.NotLocalUrl:
1229
# Even though we can't access the working tree, we need to
1230
# create its control files.
1231
WorkingTreeFormat2().stub_initialize_remote(branch.control_files)
1234
def _open(self, transport):
1235
"""See BzrDirFormat._open."""
1236
return BzrDir5(transport, self)
1238
def __return_repository_format(self):
1239
"""Circular import protection."""
1240
from bzrlib.repository import RepositoryFormat5
1241
return RepositoryFormat5(self)
1242
repository_format = property(__return_repository_format)
1245
class BzrDirFormat6(BzrDirFormat):
1246
"""Bzr control format 6.
1248
This format is a combined format for working tree, branch and repository.
1250
- Format 2 working trees [always]
1251
- Format 4 branches [always]
1252
- Format 6 repositories [always]
1255
_lock_class = TransportLock
1257
def get_format_string(self):
1258
"""See BzrDirFormat.get_format_string()."""
1259
return "Bazaar-NG branch, format 6\n"
1261
def get_format_description(self):
1262
"""See BzrDirFormat.get_format_description()."""
1263
return "All-in-one format 6"
1265
def get_converter(self, format=None):
1266
"""See BzrDirFormat.get_converter()."""
1267
# there is one and only one upgrade path here.
1268
return ConvertBzrDir6ToMeta()
1270
def _initialize_for_clone(self, url):
1271
return self.initialize_on_transport(get_transport(url), _cloning=True)
1273
def initialize_on_transport(self, transport, _cloning=False):
1274
"""Format 6 dirs always have working tree, branch and repository.
1276
Except when they are being cloned.
1278
from bzrlib.branch import BzrBranchFormat4
1279
from bzrlib.repository import RepositoryFormat6
1280
from bzrlib.workingtree import WorkingTreeFormat2
1281
result = super(BzrDirFormat6, self).initialize_on_transport(transport)
1282
RepositoryFormat6().initialize(result, _internal=True)
1284
branch = BzrBranchFormat4().initialize(result)
1286
WorkingTreeFormat2().initialize(result)
1287
except errors.NotLocalUrl:
1288
# Even though we can't access the working tree, we need to
1289
# create its control files.
1290
WorkingTreeFormat2().stub_initialize_remote(branch.control_files)
1293
def _open(self, transport):
1294
"""See BzrDirFormat._open."""
1295
return BzrDir6(transport, self)
1297
def __return_repository_format(self):
1298
"""Circular import protection."""
1299
from bzrlib.repository import RepositoryFormat6
1300
return RepositoryFormat6(self)
1301
repository_format = property(__return_repository_format)
1304
class BzrDirMetaFormat1(BzrDirFormat):
1305
"""Bzr meta control format 1
1307
This is the first format with split out working tree, branch and repository
1310
- Format 3 working trees [optional]
1311
- Format 5 branches [optional]
1312
- Format 7 repositories [optional]
1315
_lock_class = LockDir
1317
def get_converter(self, format=None):
1318
"""See BzrDirFormat.get_converter()."""
1320
format = BzrDirFormat.get_default_format()
1321
if not isinstance(self, format.__class__):
1322
# converting away from metadir is not implemented
1323
raise NotImplementedError(self.get_converter)
1324
return ConvertMetaToMeta(format)
1326
def get_format_string(self):
1327
"""See BzrDirFormat.get_format_string()."""
1328
return "Bazaar-NG meta directory, format 1\n"
1330
def get_format_description(self):
1331
"""See BzrDirFormat.get_format_description()."""
1332
return "Meta directory format 1"
1334
def _open(self, transport):
1335
"""See BzrDirFormat._open."""
1336
return BzrDirMeta1(transport, self)
1338
def __return_repository_format(self):
1339
"""Circular import protection."""
1340
if getattr(self, '_repository_format', None):
1341
return self._repository_format
1342
from bzrlib.repository import RepositoryFormat
1343
return RepositoryFormat.get_default_format()
1345
def __set_repository_format(self, value):
1346
"""Allow changint the repository format for metadir formats."""
1347
self._repository_format = value
1349
repository_format = property(__return_repository_format, __set_repository_format)
1352
BzrDirFormat.register_format(BzrDirFormat4())
1353
BzrDirFormat.register_format(BzrDirFormat5())
1354
BzrDirFormat.register_format(BzrDirFormat6())
1355
__default_format = BzrDirMetaFormat1()
1356
BzrDirFormat.register_format(__default_format)
1357
BzrDirFormat.set_default_format(__default_format)
1360
class BzrDirTestProviderAdapter(object):
1361
"""A tool to generate a suite testing multiple bzrdir formats at once.
1363
This is done by copying the test once for each transport and injecting
1364
the transport_server, transport_readonly_server, and bzrdir_format
1365
classes into each copy. Each copy is also given a new id() to make it
1369
def __init__(self, transport_server, transport_readonly_server, formats):
1370
self._transport_server = transport_server
1371
self._transport_readonly_server = transport_readonly_server
1372
self._formats = formats
1374
def adapt(self, test):
1375
result = TestSuite()
1376
for format in self._formats:
1377
new_test = deepcopy(test)
1378
new_test.transport_server = self._transport_server
1379
new_test.transport_readonly_server = self._transport_readonly_server
1380
new_test.bzrdir_format = format
1381
def make_new_test_id():
1382
new_id = "%s(%s)" % (new_test.id(), format.__class__.__name__)
1383
return lambda: new_id
1384
new_test.id = make_new_test_id()
1385
result.addTest(new_test)
1389
class Converter(object):
1390
"""Converts a disk format object from one format to another."""
1392
def convert(self, to_convert, pb):
1393
"""Perform the conversion of to_convert, giving feedback via pb.
1395
:param to_convert: The disk object to convert.
1396
:param pb: a progress bar to use for progress information.
1399
def step(self, message):
1400
"""Update the pb by a step."""
1402
self.pb.update(message, self.count, self.total)
1405
class ConvertBzrDir4To5(Converter):
1406
"""Converts format 4 bzr dirs to format 5."""
1409
super(ConvertBzrDir4To5, self).__init__()
1410
self.converted_revs = set()
1411
self.absent_revisions = set()
1415
def convert(self, to_convert, pb):
1416
"""See Converter.convert()."""
1417
self.bzrdir = to_convert
1419
self.pb.note('starting upgrade from format 4 to 5')
1420
if isinstance(self.bzrdir.transport, LocalTransport):
1421
self.bzrdir.get_workingtree_transport(None).delete('stat-cache')
1422
self._convert_to_weaves()
1423
return BzrDir.open(self.bzrdir.root_transport.base)
1425
def _convert_to_weaves(self):
1426
self.pb.note('note: upgrade may be faster if all store files are ungzipped first')
1429
stat = self.bzrdir.transport.stat('weaves')
1430
if not S_ISDIR(stat.st_mode):
1431
self.bzrdir.transport.delete('weaves')
1432
self.bzrdir.transport.mkdir('weaves')
1433
except errors.NoSuchFile:
1434
self.bzrdir.transport.mkdir('weaves')
1435
# deliberately not a WeaveFile as we want to build it up slowly.
1436
self.inv_weave = Weave('inventory')
1437
# holds in-memory weaves for all files
1438
self.text_weaves = {}
1439
self.bzrdir.transport.delete('branch-format')
1440
self.branch = self.bzrdir.open_branch()
1441
self._convert_working_inv()
1442
rev_history = self.branch.revision_history()
1443
# to_read is a stack holding the revisions we still need to process;
1444
# appending to it adds new highest-priority revisions
1445
self.known_revisions = set(rev_history)
1446
self.to_read = rev_history[-1:]
1448
rev_id = self.to_read.pop()
1449
if (rev_id not in self.revisions
1450
and rev_id not in self.absent_revisions):
1451
self._load_one_rev(rev_id)
1453
to_import = self._make_order()
1454
for i, rev_id in enumerate(to_import):
1455
self.pb.update('converting revision', i, len(to_import))
1456
self._convert_one_rev(rev_id)
1458
self._write_all_weaves()
1459
self._write_all_revs()
1460
self.pb.note('upgraded to weaves:')
1461
self.pb.note(' %6d revisions and inventories', len(self.revisions))
1462
self.pb.note(' %6d revisions not present', len(self.absent_revisions))
1463
self.pb.note(' %6d texts', self.text_count)
1464
self._cleanup_spare_files_after_format4()
1465
self.branch.control_files.put_utf8('branch-format', BzrDirFormat5().get_format_string())
1467
def _cleanup_spare_files_after_format4(self):
1468
# FIXME working tree upgrade foo.
1469
for n in 'merged-patches', 'pending-merged-patches':
1471
## assert os.path.getsize(p) == 0
1472
self.bzrdir.transport.delete(n)
1473
except errors.NoSuchFile:
1475
self.bzrdir.transport.delete_tree('inventory-store')
1476
self.bzrdir.transport.delete_tree('text-store')
1478
def _convert_working_inv(self):
1479
inv = serializer_v4.read_inventory(self.branch.control_files.get('inventory'))
1480
new_inv_xml = bzrlib.xml5.serializer_v5.write_inventory_to_string(inv)
1481
# FIXME inventory is a working tree change.
1482
self.branch.control_files.put('inventory', new_inv_xml)
1484
def _write_all_weaves(self):
1485
controlweaves = WeaveStore(self.bzrdir.transport, prefixed=False)
1486
weave_transport = self.bzrdir.transport.clone('weaves')
1487
weaves = WeaveStore(weave_transport, prefixed=False)
1488
transaction = WriteTransaction()
1492
for file_id, file_weave in self.text_weaves.items():
1493
self.pb.update('writing weave', i, len(self.text_weaves))
1494
weaves._put_weave(file_id, file_weave, transaction)
1496
self.pb.update('inventory', 0, 1)
1497
controlweaves._put_weave('inventory', self.inv_weave, transaction)
1498
self.pb.update('inventory', 1, 1)
1502
def _write_all_revs(self):
1503
"""Write all revisions out in new form."""
1504
self.bzrdir.transport.delete_tree('revision-store')
1505
self.bzrdir.transport.mkdir('revision-store')
1506
revision_transport = self.bzrdir.transport.clone('revision-store')
1508
_revision_store = TextRevisionStore(TextStore(revision_transport,
1512
transaction = bzrlib.transactions.WriteTransaction()
1513
for i, rev_id in enumerate(self.converted_revs):
1514
self.pb.update('write revision', i, len(self.converted_revs))
1515
_revision_store.add_revision(self.revisions[rev_id], transaction)
1519
def _load_one_rev(self, rev_id):
1520
"""Load a revision object into memory.
1522
Any parents not either loaded or abandoned get queued to be
1524
self.pb.update('loading revision',
1525
len(self.revisions),
1526
len(self.known_revisions))
1527
if not self.branch.repository.has_revision(rev_id):
1529
self.pb.note('revision {%s} not present in branch; '
1530
'will be converted as a ghost',
1532
self.absent_revisions.add(rev_id)
1534
rev = self.branch.repository._revision_store.get_revision(rev_id,
1535
self.branch.repository.get_transaction())
1536
for parent_id in rev.parent_ids:
1537
self.known_revisions.add(parent_id)
1538
self.to_read.append(parent_id)
1539
self.revisions[rev_id] = rev
1541
def _load_old_inventory(self, rev_id):
1542
assert rev_id not in self.converted_revs
1543
old_inv_xml = self.branch.repository.inventory_store.get(rev_id).read()
1544
inv = serializer_v4.read_inventory_from_string(old_inv_xml)
1545
rev = self.revisions[rev_id]
1546
if rev.inventory_sha1:
1547
assert rev.inventory_sha1 == sha_string(old_inv_xml), \
1548
'inventory sha mismatch for {%s}' % rev_id
1551
def _load_updated_inventory(self, rev_id):
1552
assert rev_id in self.converted_revs
1553
inv_xml = self.inv_weave.get_text(rev_id)
1554
inv = bzrlib.xml5.serializer_v5.read_inventory_from_string(inv_xml)
1557
def _convert_one_rev(self, rev_id):
1558
"""Convert revision and all referenced objects to new format."""
1559
rev = self.revisions[rev_id]
1560
inv = self._load_old_inventory(rev_id)
1561
present_parents = [p for p in rev.parent_ids
1562
if p not in self.absent_revisions]
1563
self._convert_revision_contents(rev, inv, present_parents)
1564
self._store_new_weave(rev, inv, present_parents)
1565
self.converted_revs.add(rev_id)
1567
def _store_new_weave(self, rev, inv, present_parents):
1568
# the XML is now updated with text versions
1570
entries = inv.iter_entries()
1572
for path, ie in entries:
1573
assert hasattr(ie, 'revision'), \
1574
'no revision on {%s} in {%s}' % \
1575
(file_id, rev.revision_id)
1576
new_inv_xml = bzrlib.xml5.serializer_v5.write_inventory_to_string(inv)
1577
new_inv_sha1 = sha_string(new_inv_xml)
1578
self.inv_weave.add_lines(rev.revision_id,
1580
new_inv_xml.splitlines(True))
1581
rev.inventory_sha1 = new_inv_sha1
1583
def _convert_revision_contents(self, rev, inv, present_parents):
1584
"""Convert all the files within a revision.
1586
Also upgrade the inventory to refer to the text revision ids."""
1587
rev_id = rev.revision_id
1588
mutter('converting texts of revision {%s}',
1590
parent_invs = map(self._load_updated_inventory, present_parents)
1591
entries = inv.iter_entries()
1593
for path, ie in entries:
1594
self._convert_file_version(rev, ie, parent_invs)
1596
def _convert_file_version(self, rev, ie, parent_invs):
1597
"""Convert one version of one file.
1599
The file needs to be added into the weave if it is a merge
1600
of >=2 parents or if it's changed from its parent.
1602
file_id = ie.file_id
1603
rev_id = rev.revision_id
1604
w = self.text_weaves.get(file_id)
1607
self.text_weaves[file_id] = w
1608
text_changed = False
1609
previous_entries = ie.find_previous_heads(parent_invs,
1613
for old_revision in previous_entries:
1614
# if this fails, its a ghost ?
1615
assert old_revision in self.converted_revs
1616
self.snapshot_ie(previous_entries, ie, w, rev_id)
1618
assert getattr(ie, 'revision', None) is not None
1620
def snapshot_ie(self, previous_revisions, ie, w, rev_id):
1621
# TODO: convert this logic, which is ~= snapshot to
1622
# a call to:. This needs the path figured out. rather than a work_tree
1623
# a v4 revision_tree can be given, or something that looks enough like
1624
# one to give the file content to the entry if it needs it.
1625
# and we need something that looks like a weave store for snapshot to
1627
#ie.snapshot(rev, PATH, previous_revisions, REVISION_TREE, InMemoryWeaveStore(self.text_weaves))
1628
if len(previous_revisions) == 1:
1629
previous_ie = previous_revisions.values()[0]
1630
if ie._unchanged(previous_ie):
1631
ie.revision = previous_ie.revision
1634
text = self.branch.repository.text_store.get(ie.text_id)
1635
file_lines = text.readlines()
1636
assert sha_strings(file_lines) == ie.text_sha1
1637
assert sum(map(len, file_lines)) == ie.text_size
1638
w.add_lines(rev_id, previous_revisions, file_lines)
1639
self.text_count += 1
1641
w.add_lines(rev_id, previous_revisions, [])
1642
ie.revision = rev_id
1644
def _make_order(self):
1645
"""Return a suitable order for importing revisions.
1647
The order must be such that an revision is imported after all
1648
its (present) parents.
1650
todo = set(self.revisions.keys())
1651
done = self.absent_revisions.copy()
1654
# scan through looking for a revision whose parents
1656
for rev_id in sorted(list(todo)):
1657
rev = self.revisions[rev_id]
1658
parent_ids = set(rev.parent_ids)
1659
if parent_ids.issubset(done):
1660
# can take this one now
1661
order.append(rev_id)
1667
class ConvertBzrDir5To6(Converter):
1668
"""Converts format 5 bzr dirs to format 6."""
1670
def convert(self, to_convert, pb):
1671
"""See Converter.convert()."""
1672
self.bzrdir = to_convert
1674
self.pb.note('starting upgrade from format 5 to 6')
1675
self._convert_to_prefixed()
1676
return BzrDir.open(self.bzrdir.root_transport.base)
1678
def _convert_to_prefixed(self):
1679
from bzrlib.store import TransportStore
1680
self.bzrdir.transport.delete('branch-format')
1681
for store_name in ["weaves", "revision-store"]:
1682
self.pb.note("adding prefixes to %s" % store_name)
1683
store_transport = self.bzrdir.transport.clone(store_name)
1684
store = TransportStore(store_transport, prefixed=True)
1685
for urlfilename in store_transport.list_dir('.'):
1686
filename = urlutils.unescape(urlfilename)
1687
if (filename.endswith(".weave") or
1688
filename.endswith(".gz") or
1689
filename.endswith(".sig")):
1690
file_id = os.path.splitext(filename)[0]
1693
prefix_dir = store.hash_prefix(file_id)
1694
# FIXME keep track of the dirs made RBC 20060121
1696
store_transport.move(filename, prefix_dir + '/' + filename)
1697
except errors.NoSuchFile: # catches missing dirs strangely enough
1698
store_transport.mkdir(prefix_dir)
1699
store_transport.move(filename, prefix_dir + '/' + filename)
1700
self.bzrdir._control_files.put_utf8('branch-format', BzrDirFormat6().get_format_string())
1703
class ConvertBzrDir6ToMeta(Converter):
1704
"""Converts format 6 bzr dirs to metadirs."""
1706
def convert(self, to_convert, pb):
1707
"""See Converter.convert()."""
1708
self.bzrdir = to_convert
1711
self.total = 20 # the steps we know about
1712
self.garbage_inventories = []
1714
self.pb.note('starting upgrade from format 6 to metadir')
1715
self.bzrdir._control_files.put_utf8('branch-format', "Converting to format 6")
1716
# its faster to move specific files around than to open and use the apis...
1717
# first off, nuke ancestry.weave, it was never used.
1719
self.step('Removing ancestry.weave')
1720
self.bzrdir.transport.delete('ancestry.weave')
1721
except errors.NoSuchFile:
1723
# find out whats there
1724
self.step('Finding branch files')
1725
last_revision = self.bzrdir.open_branch().last_revision()
1726
bzrcontents = self.bzrdir.transport.list_dir('.')
1727
for name in bzrcontents:
1728
if name.startswith('basis-inventory.'):
1729
self.garbage_inventories.append(name)
1730
# create new directories for repository, working tree and branch
1731
self.dir_mode = self.bzrdir._control_files._dir_mode
1732
self.file_mode = self.bzrdir._control_files._file_mode
1733
repository_names = [('inventory.weave', True),
1734
('revision-store', True),
1736
self.step('Upgrading repository ')
1737
self.bzrdir.transport.mkdir('repository', mode=self.dir_mode)
1738
self.make_lock('repository')
1739
# we hard code the formats here because we are converting into
1740
# the meta format. The meta format upgrader can take this to a
1741
# future format within each component.
1742
self.put_format('repository', bzrlib.repository.RepositoryFormat7())
1743
for entry in repository_names:
1744
self.move_entry('repository', entry)
1746
self.step('Upgrading branch ')
1747
self.bzrdir.transport.mkdir('branch', mode=self.dir_mode)
1748
self.make_lock('branch')
1749
self.put_format('branch', bzrlib.branch.BzrBranchFormat5())
1750
branch_files = [('revision-history', True),
1751
('branch-name', True),
1753
for entry in branch_files:
1754
self.move_entry('branch', entry)
1756
checkout_files = [('pending-merges', True),
1757
('inventory', True),
1758
('stat-cache', False)]
1759
# If a mandatory checkout file is not present, the branch does not have
1760
# a functional checkout. Do not create a checkout in the converted
1762
for name, mandatory in checkout_files:
1763
if mandatory and name not in bzrcontents:
1764
has_checkout = False
1768
if not has_checkout:
1769
self.pb.note('No working tree.')
1770
# If some checkout files are there, we may as well get rid of them.
1771
for name, mandatory in checkout_files:
1772
if name in bzrcontents:
1773
self.bzrdir.transport.delete(name)
1775
self.step('Upgrading working tree')
1776
self.bzrdir.transport.mkdir('checkout', mode=self.dir_mode)
1777
self.make_lock('checkout')
1779
'checkout', bzrlib.workingtree.WorkingTreeFormat3())
1780
self.bzrdir.transport.delete_multi(
1781
self.garbage_inventories, self.pb)
1782
for entry in checkout_files:
1783
self.move_entry('checkout', entry)
1784
if last_revision is not None:
1785
self.bzrdir._control_files.put_utf8(
1786
'checkout/last-revision', last_revision)
1787
self.bzrdir._control_files.put_utf8(
1788
'branch-format', BzrDirMetaFormat1().get_format_string())
1789
return BzrDir.open(self.bzrdir.root_transport.base)
1791
def make_lock(self, name):
1792
"""Make a lock for the new control dir name."""
1793
self.step('Make %s lock' % name)
1794
ld = LockDir(self.bzrdir.transport,
1796
file_modebits=self.file_mode,
1797
dir_modebits=self.dir_mode)
1800
def move_entry(self, new_dir, entry):
1801
"""Move then entry name into new_dir."""
1803
mandatory = entry[1]
1804
self.step('Moving %s' % name)
1806
self.bzrdir.transport.move(name, '%s/%s' % (new_dir, name))
1807
except errors.NoSuchFile:
1811
def put_format(self, dirname, format):
1812
self.bzrdir._control_files.put_utf8('%s/format' % dirname, format.get_format_string())
1815
class ConvertMetaToMeta(Converter):
1816
"""Converts the components of metadirs."""
1818
def __init__(self, target_format):
1819
"""Create a metadir to metadir converter.
1821
:param target_format: The final metadir format that is desired.
1823
self.target_format = target_format
1825
def convert(self, to_convert, pb):
1826
"""See Converter.convert()."""
1827
self.bzrdir = to_convert
1831
self.step('checking repository format')
1833
repo = self.bzrdir.open_repository()
1834
except errors.NoRepositoryPresent:
1837
if not isinstance(repo._format, self.target_format.repository_format.__class__):
1838
from bzrlib.repository import CopyConverter
1839
self.pb.note('starting repository conversion')
1840
converter = CopyConverter(self.target_format.repository_format)
1841
converter.convert(repo, pb)