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
from bzrlib.store.revision.text import TextRevisionStore
43
from bzrlib.store.text import TextStore
44
from bzrlib.store.versioned import WeaveStore
45
from bzrlib.trace import mutter
46
from bzrlib.transactions import WriteTransaction
47
from bzrlib.transport import get_transport
48
from bzrlib.transport.local import LocalTransport
49
import bzrlib.urlutils as urlutils
50
from bzrlib.weave import Weave
51
from bzrlib.xml4 import serializer_v4
56
"""A .bzr control diretory.
58
BzrDir instances let you create or open any of the things that can be
59
found within .bzr - checkouts, branches and repositories.
62
the transport which this bzr dir is rooted at (i.e. file:///.../.bzr/)
64
a transport connected to the directory this bzr was opened from.
68
"""Invoke break_lock on the first object in the bzrdir.
70
If there is a tree, the tree is opened and break_lock() called.
71
Otherwise, branch is tried, and finally repository.
74
thing_to_unlock = self.open_workingtree()
75
except (errors.NotLocalUrl, errors.NoWorkingTree):
77
thing_to_unlock = self.open_branch()
78
except errors.NotBranchError:
80
thing_to_unlock = self.open_repository()
81
except errors.NoRepositoryPresent:
83
thing_to_unlock.break_lock()
85
def can_convert_format(self):
86
"""Return true if this bzrdir is one whose format we can convert from."""
90
def _check_supported(format, allow_unsupported):
91
"""Check whether format is a supported format.
93
If allow_unsupported is True, this is a no-op.
95
if not allow_unsupported and not format.is_supported():
96
# see open_downlevel to open legacy branches.
97
raise errors.UnsupportedFormatError(format=format)
99
def clone(self, url, revision_id=None, basis=None, force_new_repo=False):
100
"""Clone this bzrdir and its contents to url verbatim.
102
If urls last component does not exist, it will be created.
104
if revision_id is not None, then the clone operation may tune
105
itself to download less data.
106
:param force_new_repo: Do not use a shared repository for the target
107
even if one is available.
110
basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
111
result = self._format.initialize(url)
113
local_repo = self.find_repository()
114
except errors.NoRepositoryPresent:
117
# may need to copy content in
119
result_repo = local_repo.clone(
121
revision_id=revision_id,
123
result_repo.set_make_working_trees(local_repo.make_working_trees())
126
result_repo = result.find_repository()
127
# fetch content this dir needs.
129
# XXX FIXME RBC 20060214 need tests for this when the basis
131
result_repo.fetch(basis_repo, revision_id=revision_id)
132
result_repo.fetch(local_repo, revision_id=revision_id)
133
except errors.NoRepositoryPresent:
134
# needed to make one anyway.
135
result_repo = local_repo.clone(
137
revision_id=revision_id,
139
result_repo.set_make_working_trees(local_repo.make_working_trees())
140
# 1 if there is a branch present
141
# make sure its content is available in the target repository
144
self.open_branch().clone(result, revision_id=revision_id)
145
except errors.NotBranchError:
148
self.open_workingtree().clone(result, basis=basis_tree)
149
except (errors.NoWorkingTree, errors.NotLocalUrl):
153
def _get_basis_components(self, basis):
154
"""Retrieve the basis components that are available at basis."""
156
return None, None, None
158
basis_tree = basis.open_workingtree()
159
basis_branch = basis_tree.branch
160
basis_repo = basis_branch.repository
161
except (errors.NoWorkingTree, errors.NotLocalUrl):
164
basis_branch = basis.open_branch()
165
basis_repo = basis_branch.repository
166
except errors.NotBranchError:
169
basis_repo = basis.open_repository()
170
except errors.NoRepositoryPresent:
172
return basis_repo, basis_branch, basis_tree
174
# TODO: This should be given a Transport, and should chdir up; otherwise
175
# this will open a new connection.
176
def _make_tail(self, url):
177
head, tail = urlutils.split(url)
178
if tail and tail != '.':
179
t = bzrlib.transport.get_transport(head)
182
except errors.FileExists:
185
# TODO: Should take a Transport
187
def create(cls, base):
188
"""Create a new BzrDir at the url 'base'.
190
This will call the current default formats initialize with base
191
as the only parameter.
193
If you need a specific format, consider creating an instance
194
of that and calling initialize().
196
if cls is not BzrDir:
197
raise AssertionError("BzrDir.create always creates the default format, "
198
"not one of %r" % cls)
199
head, tail = urlutils.split(base)
200
if tail and tail != '.':
201
t = bzrlib.transport.get_transport(head)
204
except errors.FileExists:
206
return BzrDirFormat.get_default_format().initialize(safe_unicode(base))
208
def create_branch(self):
209
"""Create a branch in this BzrDir.
211
The bzrdirs format will control what branch format is created.
212
For more control see BranchFormatXX.create(a_bzrdir).
214
raise NotImplementedError(self.create_branch)
217
def create_branch_and_repo(base, force_new_repo=False):
218
"""Create a new BzrDir, Branch and Repository at the url 'base'.
220
This will use the current default BzrDirFormat, and use whatever
221
repository format that that uses via bzrdir.create_branch and
222
create_repository. If a shared repository is available that is used
225
The created Branch object is returned.
227
:param base: The URL to create the branch at.
228
:param force_new_repo: If True a new repository is always created.
230
bzrdir = BzrDir.create(base)
231
bzrdir._find_or_create_repository(force_new_repo)
232
return bzrdir.create_branch()
234
def _find_or_create_repository(self, force_new_repo):
235
"""Create a new repository if needed, returning the repository."""
237
return self.create_repository()
239
return self.find_repository()
240
except errors.NoRepositoryPresent:
241
return self.create_repository()
244
def create_branch_convenience(base, force_new_repo=False,
245
force_new_tree=None, format=None):
246
"""Create a new BzrDir, Branch and Repository at the url 'base'.
248
This is a convenience function - it will use an existing repository
249
if possible, can be told explicitly whether to create a working tree or
252
This will use the current default BzrDirFormat, and use whatever
253
repository format that that uses via bzrdir.create_branch and
254
create_repository. If a shared repository is available that is used
255
preferentially. Whatever repository is used, its tree creation policy
258
The created Branch object is returned.
259
If a working tree cannot be made due to base not being a file:// url,
260
no error is raised unless force_new_tree is True, in which case no
261
data is created on disk and NotLocalUrl is raised.
263
:param base: The URL to create the branch at.
264
:param force_new_repo: If True a new repository is always created.
265
:param force_new_tree: If True or False force creation of a tree or
266
prevent such creation respectively.
267
:param format: Override for the for the bzrdir format to create
270
# check for non local urls
271
t = get_transport(safe_unicode(base))
272
if not isinstance(t, LocalTransport):
273
raise errors.NotLocalUrl(base)
275
bzrdir = BzrDir.create(base)
277
bzrdir = format.initialize(base)
278
repo = bzrdir._find_or_create_repository(force_new_repo)
279
result = bzrdir.create_branch()
280
if force_new_tree or (repo.make_working_trees() and
281
force_new_tree is None):
283
bzrdir.create_workingtree()
284
except errors.NotLocalUrl:
289
def create_checkout_convenience(to_location, source, revision_id=None,
291
"""Create a checkout of a branch.
293
:param to_location: The url to produce the checkout at
294
:param source: The branch to produce the checkout from
295
:param revision_id: The revision to check out
296
:param lighweight: If True, produce a lightweight checkout, othewise
297
produce a bound branch (heavyweight checkout)
298
:return: The tree of the created checkout
301
checkout = BzrDirMetaFormat1().initialize(to_location)
302
bzrlib.branch.BranchReferenceFormat().initialize(checkout, source)
304
checkout_branch = BzrDir.create_branch_convenience(
305
to_location, force_new_tree=False)
306
checkout = checkout_branch.bzrdir
307
checkout_branch.bind(source)
308
if revision_id is not None:
309
rh = checkout_branch.revision_history()
310
checkout_branch.set_revision_history(rh[:rh.index(revision_id) + 1])
311
return checkout.create_workingtree(revision_id)
315
def create_repository(base, shared=False):
316
"""Create a new BzrDir and Repository at the url 'base'.
318
This will use the current default BzrDirFormat, and use whatever
319
repository format that that uses for bzrdirformat.create_repository.
321
;param shared: Create a shared repository rather than a standalone
323
The Repository object is returned.
325
This must be overridden as an instance method in child classes, where
326
it should take no parameters and construct whatever repository format
327
that child class desires.
329
bzrdir = BzrDir.create(base)
330
return bzrdir.create_repository(shared)
333
def create_standalone_workingtree(base):
334
"""Create a new BzrDir, WorkingTree, Branch and Repository at 'base'.
336
'base' must be a local path or a file:// url.
338
This will use the current default BzrDirFormat, and use whatever
339
repository format that that uses for bzrdirformat.create_workingtree,
340
create_branch and create_repository.
342
The WorkingTree object is returned.
344
t = get_transport(safe_unicode(base))
345
if not isinstance(t, LocalTransport):
346
raise errors.NotLocalUrl(base)
347
bzrdir = BzrDir.create_branch_and_repo(safe_unicode(base),
348
force_new_repo=True).bzrdir
349
return bzrdir.create_workingtree()
351
def create_workingtree(self, revision_id=None):
352
"""Create a working tree at this BzrDir.
354
revision_id: create it as of this revision id.
356
raise NotImplementedError(self.create_workingtree)
358
def find_repository(self):
359
"""Find the repository that should be used for a_bzrdir.
361
This does not require a branch as we use it to find the repo for
362
new branches as well as to hook existing branches up to their
366
return self.open_repository()
367
except errors.NoRepositoryPresent:
369
next_transport = self.root_transport.clone('..')
371
# find the next containing bzrdir
373
found_bzrdir = BzrDir.open_containing_from_transport(
375
except errors.NotBranchError:
377
raise errors.NoRepositoryPresent(self)
378
# does it have a repository ?
380
repository = found_bzrdir.open_repository()
381
except errors.NoRepositoryPresent:
382
next_transport = found_bzrdir.root_transport.clone('..')
383
if (found_bzrdir.root_transport.base == next_transport.base):
384
# top of the file system
388
if ((found_bzrdir.root_transport.base ==
389
self.root_transport.base) or repository.is_shared()):
392
raise errors.NoRepositoryPresent(self)
393
raise errors.NoRepositoryPresent(self)
395
def get_branch_transport(self, branch_format):
396
"""Get the transport for use by branch format in this BzrDir.
398
Note that bzr dirs that do not support format strings will raise
399
IncompatibleFormat if the branch format they are given has
400
a format string, and vice versa.
402
If branch_format is None, the transport is returned with no
403
checking. if it is not None, then the returned transport is
404
guaranteed to point to an existing directory ready for use.
406
raise NotImplementedError(self.get_branch_transport)
408
def get_repository_transport(self, repository_format):
409
"""Get the transport for use by repository format in this BzrDir.
411
Note that bzr dirs that do not support format strings will raise
412
IncompatibleFormat if the repository format they are given has
413
a format string, and vice versa.
415
If repository_format is None, the transport is returned with no
416
checking. if it is not None, then the returned transport is
417
guaranteed to point to an existing directory ready for use.
419
raise NotImplementedError(self.get_repository_transport)
421
def get_workingtree_transport(self, tree_format):
422
"""Get the transport for use by workingtree format in this BzrDir.
424
Note that bzr dirs that do not support format strings will raise
425
IncompatibleFormat if the workingtree format they are given has
426
a format string, and vice versa.
428
If workingtree_format is None, the transport is returned with no
429
checking. if it is not None, then the returned transport is
430
guaranteed to point to an existing directory ready for use.
432
raise NotImplementedError(self.get_workingtree_transport)
434
def __init__(self, _transport, _format):
435
"""Initialize a Bzr control dir object.
437
Only really common logic should reside here, concrete classes should be
438
made with varying behaviours.
440
:param _format: the format that is creating this BzrDir instance.
441
:param _transport: the transport this dir is based at.
443
self._format = _format
444
self.transport = _transport.clone('.bzr')
445
self.root_transport = _transport
447
def is_control_filename(self, filename):
448
"""True if filename is the name of a path which is reserved for bzrdir's.
450
:param filename: A filename within the root transport of this bzrdir.
452
This is true IF and ONLY IF the filename is part of the namespace reserved
453
for bzr control dirs. Currently this is the '.bzr' directory in the root
454
of the root_transport. it is expected that plugins will need to extend
455
this in the future - for instance to make bzr talk with svn working
458
# this might be better on the BzrDirFormat class because it refers to
459
# all the possible bzrdir disk formats.
460
# This method is tested via the workingtree is_control_filename tests-
461
# it was extracted from WorkingTree.is_control_filename. If the methods
462
# contract is extended beyond the current trivial implementation please
463
# add new tests for it to the appropriate place.
464
return filename == '.bzr' or filename.startswith('.bzr/')
466
def needs_format_conversion(self, format=None):
467
"""Return true if this bzrdir needs convert_format run on it.
469
For instance, if the repository format is out of date but the
470
branch and working tree are not, this should return True.
472
:param format: Optional parameter indicating a specific desired
473
format we plan to arrive at.
475
raise NotImplementedError(self.needs_format_conversion)
478
def open_unsupported(base):
479
"""Open a branch which is not supported."""
480
return BzrDir.open(base, _unsupported=True)
483
def open(base, _unsupported=False):
484
"""Open an existing bzrdir, rooted at 'base' (url)
486
_unsupported is a private parameter to the BzrDir class.
488
t = get_transport(base)
489
mutter("trying to open %r with transport %r", base, t)
490
format = BzrDirFormat.find_format(t)
491
BzrDir._check_supported(format, _unsupported)
492
return format.open(t, _found=True)
494
def open_branch(self, unsupported=False):
495
"""Open the branch object at this BzrDir if one is present.
497
If unsupported is True, then no longer supported branch formats can
500
TODO: static convenience version of this?
502
raise NotImplementedError(self.open_branch)
505
def open_containing(url):
506
"""Open an existing branch which contains url.
508
:param url: url to search from.
509
See open_containing_from_transport for more detail.
511
return BzrDir.open_containing_from_transport(get_transport(url))
514
def open_containing_from_transport(a_transport):
515
"""Open an existing branch which contains a_transport.base
517
This probes for a branch at a_transport, and searches upwards from there.
519
Basically we keep looking up until we find the control directory or
520
run into the root. If there isn't one, raises NotBranchError.
521
If there is one and it is either an unrecognised format or an unsupported
522
format, UnknownFormatError or UnsupportedFormatError are raised.
523
If there is one, it is returned, along with the unused portion of url.
525
:return: The BzrDir that contains the path, and a Unicode path
526
for the rest of the URL.
528
# this gets the normalised url back. I.e. '.' -> the full path.
529
url = a_transport.base
532
format = BzrDirFormat.find_format(a_transport)
533
BzrDir._check_supported(format, False)
534
return format.open(a_transport), urlutils.unescape(a_transport.relpath(url))
535
except errors.NotBranchError, e:
536
## mutter('not a branch in: %r %s', a_transport.base, e)
538
new_t = a_transport.clone('..')
539
if new_t.base == a_transport.base:
540
# reached the root, whatever that may be
541
raise errors.NotBranchError(path=url)
544
def open_repository(self, _unsupported=False):
545
"""Open the repository object at this BzrDir if one is present.
547
This will not follow the Branch object pointer - its strictly a direct
548
open facility. Most client code should use open_branch().repository to
551
_unsupported is a private parameter, not part of the api.
552
TODO: static convenience version of this?
554
raise NotImplementedError(self.open_repository)
556
def open_workingtree(self, _unsupported=False):
557
"""Open the workingtree object at this BzrDir if one is present.
559
TODO: static convenience version of this?
561
raise NotImplementedError(self.open_workingtree)
563
def has_branch(self):
564
"""Tell if this bzrdir contains a branch.
566
Note: if you're going to open the branch, you should just go ahead
567
and try, and not ask permission first. (This method just opens the
568
branch and discards it, and that's somewhat expensive.)
573
except errors.NotBranchError:
576
def has_workingtree(self):
577
"""Tell if this bzrdir contains a working tree.
579
This will still raise an exception if the bzrdir has a workingtree that
580
is remote & inaccessible.
582
Note: if you're going to open the working tree, you should just go ahead
583
and try, and not ask permission first. (This method just opens the
584
workingtree and discards it, and that's somewhat expensive.)
587
self.open_workingtree()
589
except errors.NoWorkingTree:
592
def sprout(self, url, revision_id=None, basis=None, force_new_repo=False):
593
"""Create a copy of this bzrdir prepared for use as a new line of
596
If urls last component does not exist, it will be created.
598
Attributes related to the identity of the source branch like
599
branch nickname will be cleaned, a working tree is created
600
whether one existed before or not; and a local branch is always
603
if revision_id is not None, then the clone operation may tune
604
itself to download less data.
607
result = self._format.initialize(url)
608
basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
610
source_branch = self.open_branch()
611
source_repository = source_branch.repository
612
except errors.NotBranchError:
615
source_repository = self.open_repository()
616
except errors.NoRepositoryPresent:
617
# copy the entire basis one if there is one
618
# but there is no repository.
619
source_repository = basis_repo
624
result_repo = result.find_repository()
625
except errors.NoRepositoryPresent:
627
if source_repository is None and result_repo is not None:
629
elif source_repository is None and result_repo is None:
630
# no repo available, make a new one
631
result.create_repository()
632
elif source_repository is not None and result_repo is None:
633
# have source, and want to make a new target repo
634
# we don't clone the repo because that preserves attributes
635
# like is_shared(), and we have not yet implemented a
636
# repository sprout().
637
result_repo = result.create_repository()
638
if result_repo is not None:
639
# fetch needed content into target.
641
# XXX FIXME RBC 20060214 need tests for this when the basis
643
result_repo.fetch(basis_repo, revision_id=revision_id)
644
result_repo.fetch(source_repository, revision_id=revision_id)
645
if source_branch is not None:
646
source_branch.sprout(result, revision_id=revision_id)
648
result.create_branch()
649
# TODO: jam 20060426 we probably need a test in here in the
650
# case that the newly sprouted branch is a remote one
651
if result_repo is None or result_repo.make_working_trees():
652
wt = result.create_workingtree()
653
if wt.inventory.root is None:
655
wt.set_root_id(self.open_workingtree.get_root_id())
656
except errors.NoWorkingTree:
661
class BzrDirPreSplitOut(BzrDir):
662
"""A common class for the all-in-one formats."""
664
def __init__(self, _transport, _format):
665
"""See BzrDir.__init__."""
666
super(BzrDirPreSplitOut, self).__init__(_transport, _format)
667
assert self._format._lock_class == TransportLock
668
assert self._format._lock_file_name == 'branch-lock'
669
self._control_files = LockableFiles(self.get_branch_transport(None),
670
self._format._lock_file_name,
671
self._format._lock_class)
673
def break_lock(self):
674
"""Pre-splitout bzrdirs do not suffer from stale locks."""
675
raise NotImplementedError(self.break_lock)
677
def clone(self, url, revision_id=None, basis=None, force_new_repo=False):
678
"""See BzrDir.clone()."""
679
from bzrlib.workingtree import WorkingTreeFormat2
681
result = self._format._initialize_for_clone(url)
682
basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
683
self.open_repository().clone(result, revision_id=revision_id, basis=basis_repo)
684
from_branch = self.open_branch()
685
from_branch.clone(result, revision_id=revision_id)
687
self.open_workingtree().clone(result, basis=basis_tree)
688
except errors.NotLocalUrl:
689
# make a new one, this format always has to have one.
691
WorkingTreeFormat2().initialize(result)
692
except errors.NotLocalUrl:
693
# but we cannot do it for remote trees.
694
to_branch = result.open_branch()
695
WorkingTreeFormat2().stub_initialize_remote(to_branch.control_files)
698
def create_branch(self):
699
"""See BzrDir.create_branch."""
700
return self.open_branch()
702
def create_repository(self, shared=False):
703
"""See BzrDir.create_repository."""
705
raise errors.IncompatibleFormat('shared repository', self._format)
706
return self.open_repository()
708
def create_workingtree(self, revision_id=None):
709
"""See BzrDir.create_workingtree."""
710
# this looks buggy but is not -really-
711
# clone and sprout will have set the revision_id
712
# and that will have set it for us, its only
713
# specific uses of create_workingtree in isolation
714
# that can do wonky stuff here, and that only
715
# happens for creating checkouts, which cannot be
716
# done on this format anyway. So - acceptable wart.
717
result = self.open_workingtree()
718
if revision_id is not None:
719
result.set_last_revision(revision_id)
722
def get_branch_transport(self, branch_format):
723
"""See BzrDir.get_branch_transport()."""
724
if branch_format is None:
725
return self.transport
727
branch_format.get_format_string()
728
except NotImplementedError:
729
return self.transport
730
raise errors.IncompatibleFormat(branch_format, self._format)
732
def get_repository_transport(self, repository_format):
733
"""See BzrDir.get_repository_transport()."""
734
if repository_format is None:
735
return self.transport
737
repository_format.get_format_string()
738
except NotImplementedError:
739
return self.transport
740
raise errors.IncompatibleFormat(repository_format, self._format)
742
def get_workingtree_transport(self, workingtree_format):
743
"""See BzrDir.get_workingtree_transport()."""
744
if workingtree_format is None:
745
return self.transport
747
workingtree_format.get_format_string()
748
except NotImplementedError:
749
return self.transport
750
raise errors.IncompatibleFormat(workingtree_format, self._format)
752
def needs_format_conversion(self, format=None):
753
"""See BzrDir.needs_format_conversion()."""
754
# if the format is not the same as the system default,
755
# an upgrade is needed.
757
format = BzrDirFormat.get_default_format()
758
return not isinstance(self._format, format.__class__)
760
def open_branch(self, unsupported=False):
761
"""See BzrDir.open_branch."""
762
from bzrlib.branch import BzrBranchFormat4
763
format = BzrBranchFormat4()
764
self._check_supported(format, unsupported)
765
return format.open(self, _found=True)
767
def sprout(self, url, revision_id=None, basis=None):
768
"""See BzrDir.sprout()."""
769
from bzrlib.workingtree import WorkingTreeFormat2
771
result = self._format._initialize_for_clone(url)
772
basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
774
self.open_repository().clone(result, revision_id=revision_id, basis=basis_repo)
775
except errors.NoRepositoryPresent:
778
self.open_branch().sprout(result, revision_id=revision_id)
779
except errors.NotBranchError:
781
# we always want a working tree
782
WorkingTreeFormat2().initialize(result)
786
class BzrDir4(BzrDirPreSplitOut):
787
"""A .bzr version 4 control object.
789
This is a deprecated format and may be removed after sept 2006.
792
def create_repository(self, shared=False):
793
"""See BzrDir.create_repository."""
794
return self._format.repository_format.initialize(self, shared)
796
def needs_format_conversion(self, format=None):
797
"""Format 4 dirs are always in need of conversion."""
800
def open_repository(self):
801
"""See BzrDir.open_repository."""
802
from bzrlib.repository import RepositoryFormat4
803
return RepositoryFormat4().open(self, _found=True)
806
class BzrDir5(BzrDirPreSplitOut):
807
"""A .bzr version 5 control object.
809
This is a deprecated format and may be removed after sept 2006.
812
def open_repository(self):
813
"""See BzrDir.open_repository."""
814
from bzrlib.repository import RepositoryFormat5
815
return RepositoryFormat5().open(self, _found=True)
817
def open_workingtree(self, _unsupported=False):
818
"""See BzrDir.create_workingtree."""
819
from bzrlib.workingtree import WorkingTreeFormat2
820
return WorkingTreeFormat2().open(self, _found=True)
823
class BzrDir6(BzrDirPreSplitOut):
824
"""A .bzr version 6 control object.
826
This is a deprecated format and may be removed after sept 2006.
829
def open_repository(self):
830
"""See BzrDir.open_repository."""
831
from bzrlib.repository import RepositoryFormat6
832
return RepositoryFormat6().open(self, _found=True)
834
def open_workingtree(self, _unsupported=False):
835
"""See BzrDir.create_workingtree."""
836
from bzrlib.workingtree import WorkingTreeFormat2
837
return WorkingTreeFormat2().open(self, _found=True)
840
class BzrDirMeta1(BzrDir):
841
"""A .bzr meta version 1 control object.
843
This is the first control object where the
844
individual aspects are really split out: there are separate repository,
845
workingtree and branch subdirectories and any subset of the three can be
846
present within a BzrDir.
849
def can_convert_format(self):
850
"""See BzrDir.can_convert_format()."""
853
def create_branch(self):
854
"""See BzrDir.create_branch."""
855
from bzrlib.branch import BranchFormat
856
return BranchFormat.get_default_format().initialize(self)
858
def create_repository(self, shared=False):
859
"""See BzrDir.create_repository."""
860
return self._format.repository_format.initialize(self, shared)
862
def create_workingtree(self, revision_id=None):
863
"""See BzrDir.create_workingtree."""
864
from bzrlib.workingtree import WorkingTreeFormat
865
return WorkingTreeFormat.get_default_format().initialize(self, revision_id)
867
def _get_mkdir_mode(self):
868
"""Figure out the mode to use when creating a bzrdir subdir."""
869
temp_control = LockableFiles(self.transport, '', TransportLock)
870
return temp_control._dir_mode
872
def get_branch_transport(self, branch_format):
873
"""See BzrDir.get_branch_transport()."""
874
if branch_format is None:
875
return self.transport.clone('branch')
877
branch_format.get_format_string()
878
except NotImplementedError:
879
raise errors.IncompatibleFormat(branch_format, self._format)
881
self.transport.mkdir('branch', mode=self._get_mkdir_mode())
882
except errors.FileExists:
884
return self.transport.clone('branch')
886
def get_repository_transport(self, repository_format):
887
"""See BzrDir.get_repository_transport()."""
888
if repository_format is None:
889
return self.transport.clone('repository')
891
repository_format.get_format_string()
892
except NotImplementedError:
893
raise errors.IncompatibleFormat(repository_format, self._format)
895
self.transport.mkdir('repository', mode=self._get_mkdir_mode())
896
except errors.FileExists:
898
return self.transport.clone('repository')
900
def get_workingtree_transport(self, workingtree_format):
901
"""See BzrDir.get_workingtree_transport()."""
902
if workingtree_format is None:
903
return self.transport.clone('checkout')
905
workingtree_format.get_format_string()
906
except NotImplementedError:
907
raise errors.IncompatibleFormat(workingtree_format, self._format)
909
self.transport.mkdir('checkout', mode=self._get_mkdir_mode())
910
except errors.FileExists:
912
return self.transport.clone('checkout')
914
def needs_format_conversion(self, format=None):
915
"""See BzrDir.needs_format_conversion()."""
917
format = BzrDirFormat.get_default_format()
918
if not isinstance(self._format, format.__class__):
919
# it is not a meta dir format, conversion is needed.
921
# we might want to push this down to the repository?
923
if not isinstance(self.open_repository()._format,
924
format.repository_format.__class__):
925
# the repository needs an upgrade.
927
except errors.NoRepositoryPresent:
929
# currently there are no other possible conversions for meta1 formats.
932
def open_branch(self, unsupported=False):
933
"""See BzrDir.open_branch."""
934
from bzrlib.branch import BranchFormat
935
format = BranchFormat.find_format(self)
936
self._check_supported(format, unsupported)
937
return format.open(self, _found=True)
939
def open_repository(self, unsupported=False):
940
"""See BzrDir.open_repository."""
941
from bzrlib.repository import RepositoryFormat
942
format = RepositoryFormat.find_format(self)
943
self._check_supported(format, unsupported)
944
return format.open(self, _found=True)
946
def open_workingtree(self, unsupported=False):
947
"""See BzrDir.open_workingtree."""
948
from bzrlib.workingtree import WorkingTreeFormat
949
format = WorkingTreeFormat.find_format(self)
950
self._check_supported(format, unsupported)
951
return format.open(self, _found=True)
954
class BzrDirFormat(object):
955
"""An encapsulation of the initialization and open routines for a format.
957
Formats provide three things:
958
* An initialization routine,
962
Formats are placed in an dict by their format string for reference
963
during bzrdir opening. These should be subclasses of BzrDirFormat
966
Once a format is deprecated, just deprecate the initialize and open
967
methods on the format class. Do not deprecate the object, as the
968
object will be created every system load.
971
_default_format = None
972
"""The default format used for new .bzr dirs."""
975
"""The known formats."""
977
_control_formats = []
978
"""The registered control formats - .bzr, ....
980
This is a list of BzrDirFormat objects.
983
_lock_file_name = 'branch-lock'
985
# _lock_class must be set in subclasses to the lock type, typ.
986
# TransportLock or LockDir
989
def find_format(klass, transport):
990
"""Return the format present at transport."""
991
for format in klass._control_formats:
993
return format.probe_transport(transport)
994
except errors.NotBranchError:
995
# this format does not find a control dir here.
997
raise errors.NotBranchError(path=transport.base)
1000
def probe_transport(klass, transport):
1001
"""Return the .bzrdir style transport present at URL."""
1003
format_string = transport.get(".bzr/branch-format").read()
1004
except errors.NoSuchFile:
1005
raise errors.NotBranchError(path=transport.base)
1008
return klass._formats[format_string]
1010
raise errors.UnknownFormatError(format=format_string)
1013
def get_default_format(klass):
1014
"""Return the current default format."""
1015
return klass._default_format
1017
def get_format_string(self):
1018
"""Return the ASCII format string that identifies this format."""
1019
raise NotImplementedError(self.get_format_string)
1021
def get_format_description(self):
1022
"""Return the short description for this format."""
1023
raise NotImplementedError(self.get_format_description)
1025
def get_converter(self, format=None):
1026
"""Return the converter to use to convert bzrdirs needing converts.
1028
This returns a bzrlib.bzrdir.Converter object.
1030
This should return the best upgrader to step this format towards the
1031
current default format. In the case of plugins we can/should provide
1032
some means for them to extend the range of returnable converters.
1034
:param format: Optional format to override the default format of the
1037
raise NotImplementedError(self.get_converter)
1039
def initialize(self, url):
1040
"""Create a bzr control dir at this url and return an opened copy.
1042
Subclasses should typically override initialize_on_transport
1043
instead of this method.
1045
return self.initialize_on_transport(get_transport(url))
1047
def initialize_on_transport(self, transport):
1048
"""Initialize a new bzrdir in the base directory of a Transport."""
1049
# Since we don't have a .bzr directory, inherit the
1050
# mode from the root directory
1051
temp_control = LockableFiles(transport, '', TransportLock)
1052
temp_control._transport.mkdir('.bzr',
1053
# FIXME: RBC 20060121 don't peek under
1055
mode=temp_control._dir_mode)
1056
file_mode = temp_control._file_mode
1058
mutter('created control directory in ' + transport.base)
1059
control = transport.clone('.bzr')
1060
utf8_files = [('README',
1061
"This is a Bazaar-NG control directory.\n"
1062
"Do not change any files in this directory.\n"),
1063
('branch-format', self.get_format_string()),
1065
# NB: no need to escape relative paths that are url safe.
1066
control_files = LockableFiles(control, self._lock_file_name,
1068
control_files.create_lock()
1069
control_files.lock_write()
1071
for file, content in utf8_files:
1072
control_files.put_utf8(file, content)
1074
control_files.unlock()
1075
return self.open(transport, _found=True)
1077
def is_supported(self):
1078
"""Is this format supported?
1080
Supported formats must be initializable and openable.
1081
Unsupported formats may not support initialization or committing or
1082
some other features depending on the reason for not being supported.
1087
def known_formats(klass):
1088
"""Return all the known formats.
1090
Concrete formats should override _known_formats.
1092
# There is double indirection here to make sure that control
1093
# formats used by more than one dir format will only be probed
1094
# once. This can otherwise be quite expensive for remote connections.
1096
for format in klass._control_formats:
1097
result.update(format._known_formats())
1101
def _known_formats(klass):
1102
"""Return the known format instances for this control format."""
1103
return set(klass._formats.values())
1105
def open(self, transport, _found=False):
1106
"""Return an instance of this format for the dir transport points at.
1108
_found is a private parameter, do not use it.
1111
assert isinstance(BzrDirFormat.find_format(transport),
1113
return self._open(transport)
1115
def _open(self, transport):
1116
"""Template method helper for opening BzrDirectories.
1118
This performs the actual open and any additional logic or parameter
1121
raise NotImplementedError(self._open)
1124
def register_format(klass, format):
1125
klass._formats[format.get_format_string()] = format
1128
def register_control_format(klass, format):
1129
"""Register a format that does not use '.bzrdir' for its control dir.
1131
TODO: This should be pulled up into a 'ControlDirFormat' base class
1132
which BzrDirFormat can inherit from, and renamed to register_format
1133
there. It has been done without that for now for simplicity of
1136
klass._control_formats.append(format)
1139
def set_default_format(klass, format):
1140
klass._default_format = format
1143
return self.get_format_string()[:-1]
1146
def unregister_format(klass, format):
1147
assert klass._formats[format.get_format_string()] is format
1148
del klass._formats[format.get_format_string()]
1151
def unregister_control_format(klass, format):
1152
klass._control_formats.remove(format)
1155
# register BzrDirFormat as a control format
1156
BzrDirFormat.register_control_format(BzrDirFormat)
1159
class BzrDirFormat4(BzrDirFormat):
1160
"""Bzr dir format 4.
1162
This format is a combined format for working tree, branch and repository.
1164
- Format 1 working trees [always]
1165
- Format 4 branches [always]
1166
- Format 4 repositories [always]
1168
This format is deprecated: it indexes texts using a text it which is
1169
removed in format 5; write support for this format has been removed.
1172
_lock_class = TransportLock
1174
def get_format_string(self):
1175
"""See BzrDirFormat.get_format_string()."""
1176
return "Bazaar-NG branch, format 0.0.4\n"
1178
def get_format_description(self):
1179
"""See BzrDirFormat.get_format_description()."""
1180
return "All-in-one format 4"
1182
def get_converter(self, format=None):
1183
"""See BzrDirFormat.get_converter()."""
1184
# there is one and only one upgrade path here.
1185
return ConvertBzrDir4To5()
1187
def initialize_on_transport(self, transport):
1188
"""Format 4 branches cannot be created."""
1189
raise errors.UninitializableFormat(self)
1191
def is_supported(self):
1192
"""Format 4 is not supported.
1194
It is not supported because the model changed from 4 to 5 and the
1195
conversion logic is expensive - so doing it on the fly was not
1200
def _open(self, transport):
1201
"""See BzrDirFormat._open."""
1202
return BzrDir4(transport, self)
1204
def __return_repository_format(self):
1205
"""Circular import protection."""
1206
from bzrlib.repository import RepositoryFormat4
1207
return RepositoryFormat4(self)
1208
repository_format = property(__return_repository_format)
1211
class BzrDirFormat5(BzrDirFormat):
1212
"""Bzr control format 5.
1214
This format is a combined format for working tree, branch and repository.
1216
- Format 2 working trees [always]
1217
- Format 4 branches [always]
1218
- Format 5 repositories [always]
1219
Unhashed stores in the repository.
1222
_lock_class = TransportLock
1224
def get_format_string(self):
1225
"""See BzrDirFormat.get_format_string()."""
1226
return "Bazaar-NG branch, format 5\n"
1228
def get_format_description(self):
1229
"""See BzrDirFormat.get_format_description()."""
1230
return "All-in-one format 5"
1232
def get_converter(self, format=None):
1233
"""See BzrDirFormat.get_converter()."""
1234
# there is one and only one upgrade path here.
1235
return ConvertBzrDir5To6()
1237
def _initialize_for_clone(self, url):
1238
return self.initialize_on_transport(get_transport(url), _cloning=True)
1240
def initialize_on_transport(self, transport, _cloning=False):
1241
"""Format 5 dirs always have working tree, branch and repository.
1243
Except when they are being cloned.
1245
from bzrlib.branch import BzrBranchFormat4
1246
from bzrlib.repository import RepositoryFormat5
1247
from bzrlib.workingtree import WorkingTreeFormat2
1248
result = (super(BzrDirFormat5, self).initialize_on_transport(transport))
1249
RepositoryFormat5().initialize(result, _internal=True)
1251
BzrBranchFormat4().initialize(result)
1252
WorkingTreeFormat2().initialize(result)
1255
def _open(self, transport):
1256
"""See BzrDirFormat._open."""
1257
return BzrDir5(transport, self)
1259
def __return_repository_format(self):
1260
"""Circular import protection."""
1261
from bzrlib.repository import RepositoryFormat5
1262
return RepositoryFormat5(self)
1263
repository_format = property(__return_repository_format)
1266
class BzrDirFormat6(BzrDirFormat):
1267
"""Bzr control format 6.
1269
This format is a combined format for working tree, branch and repository.
1271
- Format 2 working trees [always]
1272
- Format 4 branches [always]
1273
- Format 6 repositories [always]
1276
_lock_class = TransportLock
1278
def get_format_string(self):
1279
"""See BzrDirFormat.get_format_string()."""
1280
return "Bazaar-NG branch, format 6\n"
1282
def get_format_description(self):
1283
"""See BzrDirFormat.get_format_description()."""
1284
return "All-in-one format 6"
1286
def get_converter(self, format=None):
1287
"""See BzrDirFormat.get_converter()."""
1288
# there is one and only one upgrade path here.
1289
return ConvertBzrDir6ToMeta()
1291
def _initialize_for_clone(self, url):
1292
return self.initialize_on_transport(get_transport(url), _cloning=True)
1294
def initialize_on_transport(self, transport, _cloning=False):
1295
"""Format 6 dirs always have working tree, branch and repository.
1297
Except when they are being cloned.
1299
from bzrlib.branch import BzrBranchFormat4
1300
from bzrlib.repository import RepositoryFormat6
1301
from bzrlib.workingtree import WorkingTreeFormat2
1302
result = super(BzrDirFormat6, self).initialize_on_transport(transport)
1303
RepositoryFormat6().initialize(result, _internal=True)
1305
BzrBranchFormat4().initialize(result)
1307
WorkingTreeFormat2().initialize(result)
1308
except errors.NotLocalUrl:
1309
# emulate pre-check behaviour for working tree and silently
1314
def _open(self, transport):
1315
"""See BzrDirFormat._open."""
1316
return BzrDir6(transport, self)
1318
def __return_repository_format(self):
1319
"""Circular import protection."""
1320
from bzrlib.repository import RepositoryFormat6
1321
return RepositoryFormat6(self)
1322
repository_format = property(__return_repository_format)
1325
class BzrDirMetaFormat1(BzrDirFormat):
1326
"""Bzr meta control format 1
1328
This is the first format with split out working tree, branch and repository
1331
- Format 3 working trees [optional]
1332
- Format 5 branches [optional]
1333
- Format 7 repositories [optional]
1336
_lock_class = LockDir
1338
def get_converter(self, format=None):
1339
"""See BzrDirFormat.get_converter()."""
1341
format = BzrDirFormat.get_default_format()
1342
if not isinstance(self, format.__class__):
1343
# converting away from metadir is not implemented
1344
raise NotImplementedError(self.get_converter)
1345
return ConvertMetaToMeta(format)
1347
def get_format_string(self):
1348
"""See BzrDirFormat.get_format_string()."""
1349
return "Bazaar-NG meta directory, format 1\n"
1351
def get_format_description(self):
1352
"""See BzrDirFormat.get_format_description()."""
1353
return "Meta directory format 1"
1355
def _open(self, transport):
1356
"""See BzrDirFormat._open."""
1357
return BzrDirMeta1(transport, self)
1359
def __return_repository_format(self):
1360
"""Circular import protection."""
1361
if getattr(self, '_repository_format', None):
1362
return self._repository_format
1363
from bzrlib.repository import RepositoryFormat
1364
return RepositoryFormat.get_default_format()
1366
def __set_repository_format(self, value):
1367
"""Allow changint the repository format for metadir formats."""
1368
self._repository_format = value
1370
repository_format = property(__return_repository_format, __set_repository_format)
1373
BzrDirFormat.register_format(BzrDirFormat4())
1374
BzrDirFormat.register_format(BzrDirFormat5())
1375
BzrDirFormat.register_format(BzrDirFormat6())
1376
__default_format = BzrDirMetaFormat1()
1377
BzrDirFormat.register_format(__default_format)
1378
BzrDirFormat.set_default_format(__default_format)
1381
class BzrDirTestProviderAdapter(object):
1382
"""A tool to generate a suite testing multiple bzrdir formats at once.
1384
This is done by copying the test once for each transport and injecting
1385
the transport_server, transport_readonly_server, and bzrdir_format
1386
classes into each copy. Each copy is also given a new id() to make it
1390
def __init__(self, transport_server, transport_readonly_server, formats):
1391
self._transport_server = transport_server
1392
self._transport_readonly_server = transport_readonly_server
1393
self._formats = formats
1395
def adapt(self, test):
1396
result = TestSuite()
1397
for format in self._formats:
1398
new_test = deepcopy(test)
1399
new_test.transport_server = self._transport_server
1400
new_test.transport_readonly_server = self._transport_readonly_server
1401
new_test.bzrdir_format = format
1402
def make_new_test_id():
1403
new_id = "%s(%s)" % (new_test.id(), format.__class__.__name__)
1404
return lambda: new_id
1405
new_test.id = make_new_test_id()
1406
result.addTest(new_test)
1410
class Converter(object):
1411
"""Converts a disk format object from one format to another."""
1413
def convert(self, to_convert, pb):
1414
"""Perform the conversion of to_convert, giving feedback via pb.
1416
:param to_convert: The disk object to convert.
1417
:param pb: a progress bar to use for progress information.
1420
def step(self, message):
1421
"""Update the pb by a step."""
1423
self.pb.update(message, self.count, self.total)
1426
class ConvertBzrDir4To5(Converter):
1427
"""Converts format 4 bzr dirs to format 5."""
1430
super(ConvertBzrDir4To5, self).__init__()
1431
self.converted_revs = set()
1432
self.absent_revisions = set()
1436
def convert(self, to_convert, pb):
1437
"""See Converter.convert()."""
1438
self.bzrdir = to_convert
1440
self.pb.note('starting upgrade from format 4 to 5')
1441
if isinstance(self.bzrdir.transport, LocalTransport):
1442
self.bzrdir.get_workingtree_transport(None).delete('stat-cache')
1443
self._convert_to_weaves()
1444
return BzrDir.open(self.bzrdir.root_transport.base)
1446
def _convert_to_weaves(self):
1447
self.pb.note('note: upgrade may be faster if all store files are ungzipped first')
1450
stat = self.bzrdir.transport.stat('weaves')
1451
if not S_ISDIR(stat.st_mode):
1452
self.bzrdir.transport.delete('weaves')
1453
self.bzrdir.transport.mkdir('weaves')
1454
except errors.NoSuchFile:
1455
self.bzrdir.transport.mkdir('weaves')
1456
# deliberately not a WeaveFile as we want to build it up slowly.
1457
self.inv_weave = Weave('inventory')
1458
# holds in-memory weaves for all files
1459
self.text_weaves = {}
1460
self.bzrdir.transport.delete('branch-format')
1461
self.branch = self.bzrdir.open_branch()
1462
self._convert_working_inv()
1463
rev_history = self.branch.revision_history()
1464
# to_read is a stack holding the revisions we still need to process;
1465
# appending to it adds new highest-priority revisions
1466
self.known_revisions = set(rev_history)
1467
self.to_read = rev_history[-1:]
1469
rev_id = self.to_read.pop()
1470
if (rev_id not in self.revisions
1471
and rev_id not in self.absent_revisions):
1472
self._load_one_rev(rev_id)
1474
to_import = self._make_order()
1475
for i, rev_id in enumerate(to_import):
1476
self.pb.update('converting revision', i, len(to_import))
1477
self._convert_one_rev(rev_id)
1479
self._write_all_weaves()
1480
self._write_all_revs()
1481
self.pb.note('upgraded to weaves:')
1482
self.pb.note(' %6d revisions and inventories', len(self.revisions))
1483
self.pb.note(' %6d revisions not present', len(self.absent_revisions))
1484
self.pb.note(' %6d texts', self.text_count)
1485
self._cleanup_spare_files_after_format4()
1486
self.branch.control_files.put_utf8('branch-format', BzrDirFormat5().get_format_string())
1488
def _cleanup_spare_files_after_format4(self):
1489
# FIXME working tree upgrade foo.
1490
for n in 'merged-patches', 'pending-merged-patches':
1492
## assert os.path.getsize(p) == 0
1493
self.bzrdir.transport.delete(n)
1494
except errors.NoSuchFile:
1496
self.bzrdir.transport.delete_tree('inventory-store')
1497
self.bzrdir.transport.delete_tree('text-store')
1499
def _convert_working_inv(self):
1500
inv = serializer_v4.read_inventory(self.branch.control_files.get('inventory'))
1501
new_inv_xml = bzrlib.xml5.serializer_v5.write_inventory_to_string(inv)
1502
# FIXME inventory is a working tree change.
1503
self.branch.control_files.put('inventory', new_inv_xml)
1505
def _write_all_weaves(self):
1506
controlweaves = WeaveStore(self.bzrdir.transport, prefixed=False)
1507
weave_transport = self.bzrdir.transport.clone('weaves')
1508
weaves = WeaveStore(weave_transport, prefixed=False)
1509
transaction = WriteTransaction()
1513
for file_id, file_weave in self.text_weaves.items():
1514
self.pb.update('writing weave', i, len(self.text_weaves))
1515
weaves._put_weave(file_id, file_weave, transaction)
1517
self.pb.update('inventory', 0, 1)
1518
controlweaves._put_weave('inventory', self.inv_weave, transaction)
1519
self.pb.update('inventory', 1, 1)
1523
def _write_all_revs(self):
1524
"""Write all revisions out in new form."""
1525
self.bzrdir.transport.delete_tree('revision-store')
1526
self.bzrdir.transport.mkdir('revision-store')
1527
revision_transport = self.bzrdir.transport.clone('revision-store')
1529
_revision_store = TextRevisionStore(TextStore(revision_transport,
1533
transaction = bzrlib.transactions.WriteTransaction()
1534
for i, rev_id in enumerate(self.converted_revs):
1535
self.pb.update('write revision', i, len(self.converted_revs))
1536
_revision_store.add_revision(self.revisions[rev_id], transaction)
1540
def _load_one_rev(self, rev_id):
1541
"""Load a revision object into memory.
1543
Any parents not either loaded or abandoned get queued to be
1545
self.pb.update('loading revision',
1546
len(self.revisions),
1547
len(self.known_revisions))
1548
if not self.branch.repository.has_revision(rev_id):
1550
self.pb.note('revision {%s} not present in branch; '
1551
'will be converted as a ghost',
1553
self.absent_revisions.add(rev_id)
1555
rev = self.branch.repository._revision_store.get_revision(rev_id,
1556
self.branch.repository.get_transaction())
1557
for parent_id in rev.parent_ids:
1558
self.known_revisions.add(parent_id)
1559
self.to_read.append(parent_id)
1560
self.revisions[rev_id] = rev
1562
def _load_old_inventory(self, rev_id):
1563
assert rev_id not in self.converted_revs
1564
old_inv_xml = self.branch.repository.inventory_store.get(rev_id).read()
1565
inv = serializer_v4.read_inventory_from_string(old_inv_xml)
1566
rev = self.revisions[rev_id]
1567
if rev.inventory_sha1:
1568
assert rev.inventory_sha1 == sha_string(old_inv_xml), \
1569
'inventory sha mismatch for {%s}' % rev_id
1572
def _load_updated_inventory(self, rev_id):
1573
assert rev_id in self.converted_revs
1574
inv_xml = self.inv_weave.get_text(rev_id)
1575
inv = bzrlib.xml5.serializer_v5.read_inventory_from_string(inv_xml)
1578
def _convert_one_rev(self, rev_id):
1579
"""Convert revision and all referenced objects to new format."""
1580
rev = self.revisions[rev_id]
1581
inv = self._load_old_inventory(rev_id)
1582
present_parents = [p for p in rev.parent_ids
1583
if p not in self.absent_revisions]
1584
self._convert_revision_contents(rev, inv, present_parents)
1585
self._store_new_weave(rev, inv, present_parents)
1586
self.converted_revs.add(rev_id)
1588
def _store_new_weave(self, rev, inv, present_parents):
1589
# the XML is now updated with text versions
1592
if inv.is_root(file_id):
1595
assert hasattr(ie, 'revision'), \
1596
'no revision on {%s} in {%s}' % \
1597
(file_id, rev.revision_id)
1598
new_inv_xml = bzrlib.xml5.serializer_v5.write_inventory_to_string(inv)
1599
new_inv_sha1 = sha_string(new_inv_xml)
1600
self.inv_weave.add_lines(rev.revision_id,
1602
new_inv_xml.splitlines(True))
1603
rev.inventory_sha1 = new_inv_sha1
1605
def _convert_revision_contents(self, rev, inv, present_parents):
1606
"""Convert all the files within a revision.
1608
Also upgrade the inventory to refer to the text revision ids."""
1609
rev_id = rev.revision_id
1610
mutter('converting texts of revision {%s}',
1612
parent_invs = map(self._load_updated_inventory, present_parents)
1614
if inv.is_root(file_id):
1617
self._convert_file_version(rev, ie, parent_invs)
1619
def _convert_file_version(self, rev, ie, parent_invs):
1620
"""Convert one version of one file.
1622
The file needs to be added into the weave if it is a merge
1623
of >=2 parents or if it's changed from its parent.
1625
file_id = ie.file_id
1626
rev_id = rev.revision_id
1627
w = self.text_weaves.get(file_id)
1630
self.text_weaves[file_id] = w
1631
text_changed = False
1632
previous_entries = ie.find_previous_heads(parent_invs,
1636
for old_revision in previous_entries:
1637
# if this fails, its a ghost ?
1638
assert old_revision in self.converted_revs, \
1639
"Revision {%s} not in converted_revs" % old_revision
1640
self.snapshot_ie(previous_entries, ie, w, rev_id)
1642
assert getattr(ie, 'revision', None) is not None
1644
def snapshot_ie(self, previous_revisions, ie, w, rev_id):
1645
# TODO: convert this logic, which is ~= snapshot to
1646
# a call to:. This needs the path figured out. rather than a work_tree
1647
# a v4 revision_tree can be given, or something that looks enough like
1648
# one to give the file content to the entry if it needs it.
1649
# and we need something that looks like a weave store for snapshot to
1651
#ie.snapshot(rev, PATH, previous_revisions, REVISION_TREE, InMemoryWeaveStore(self.text_weaves))
1652
if len(previous_revisions) == 1:
1653
previous_ie = previous_revisions.values()[0]
1654
if ie._unchanged(previous_ie):
1655
ie.revision = previous_ie.revision
1658
text = self.branch.repository.text_store.get(ie.text_id)
1659
file_lines = text.readlines()
1660
assert sha_strings(file_lines) == ie.text_sha1
1661
assert sum(map(len, file_lines)) == ie.text_size
1662
w.add_lines(rev_id, previous_revisions, file_lines)
1663
self.text_count += 1
1665
w.add_lines(rev_id, previous_revisions, [])
1666
ie.revision = rev_id
1668
def _make_order(self):
1669
"""Return a suitable order for importing revisions.
1671
The order must be such that an revision is imported after all
1672
its (present) parents.
1674
todo = set(self.revisions.keys())
1675
done = self.absent_revisions.copy()
1678
# scan through looking for a revision whose parents
1680
for rev_id in sorted(list(todo)):
1681
rev = self.revisions[rev_id]
1682
parent_ids = set(rev.parent_ids)
1683
if parent_ids.issubset(done):
1684
# can take this one now
1685
order.append(rev_id)
1691
class ConvertBzrDir5To6(Converter):
1692
"""Converts format 5 bzr dirs to format 6."""
1694
def convert(self, to_convert, pb):
1695
"""See Converter.convert()."""
1696
self.bzrdir = to_convert
1698
self.pb.note('starting upgrade from format 5 to 6')
1699
self._convert_to_prefixed()
1700
return BzrDir.open(self.bzrdir.root_transport.base)
1702
def _convert_to_prefixed(self):
1703
from bzrlib.store import TransportStore
1704
self.bzrdir.transport.delete('branch-format')
1705
for store_name in ["weaves", "revision-store"]:
1706
self.pb.note("adding prefixes to %s" % store_name)
1707
store_transport = self.bzrdir.transport.clone(store_name)
1708
store = TransportStore(store_transport, prefixed=True)
1709
for urlfilename in store_transport.list_dir('.'):
1710
filename = urlutils.unescape(urlfilename)
1711
if (filename.endswith(".weave") or
1712
filename.endswith(".gz") or
1713
filename.endswith(".sig")):
1714
file_id = os.path.splitext(filename)[0]
1717
prefix_dir = store.hash_prefix(file_id)
1718
# FIXME keep track of the dirs made RBC 20060121
1720
store_transport.move(filename, prefix_dir + '/' + filename)
1721
except errors.NoSuchFile: # catches missing dirs strangely enough
1722
store_transport.mkdir(prefix_dir)
1723
store_transport.move(filename, prefix_dir + '/' + filename)
1724
self.bzrdir._control_files.put_utf8('branch-format', BzrDirFormat6().get_format_string())
1727
class ConvertBzrDir6ToMeta(Converter):
1728
"""Converts format 6 bzr dirs to metadirs."""
1730
def convert(self, to_convert, pb):
1731
"""See Converter.convert()."""
1732
self.bzrdir = to_convert
1735
self.total = 20 # the steps we know about
1736
self.garbage_inventories = []
1738
self.pb.note('starting upgrade from format 6 to metadir')
1739
self.bzrdir._control_files.put_utf8('branch-format', "Converting to format 6")
1740
# its faster to move specific files around than to open and use the apis...
1741
# first off, nuke ancestry.weave, it was never used.
1743
self.step('Removing ancestry.weave')
1744
self.bzrdir.transport.delete('ancestry.weave')
1745
except errors.NoSuchFile:
1747
# find out whats there
1748
self.step('Finding branch files')
1749
last_revision = self.bzrdir.open_branch().last_revision()
1750
bzrcontents = self.bzrdir.transport.list_dir('.')
1751
for name in bzrcontents:
1752
if name.startswith('basis-inventory.'):
1753
self.garbage_inventories.append(name)
1754
# create new directories for repository, working tree and branch
1755
self.dir_mode = self.bzrdir._control_files._dir_mode
1756
self.file_mode = self.bzrdir._control_files._file_mode
1757
repository_names = [('inventory.weave', True),
1758
('revision-store', True),
1760
self.step('Upgrading repository ')
1761
self.bzrdir.transport.mkdir('repository', mode=self.dir_mode)
1762
self.make_lock('repository')
1763
# we hard code the formats here because we are converting into
1764
# the meta format. The meta format upgrader can take this to a
1765
# future format within each component.
1766
self.put_format('repository', bzrlib.repository.RepositoryFormat7())
1767
for entry in repository_names:
1768
self.move_entry('repository', entry)
1770
self.step('Upgrading branch ')
1771
self.bzrdir.transport.mkdir('branch', mode=self.dir_mode)
1772
self.make_lock('branch')
1773
self.put_format('branch', bzrlib.branch.BzrBranchFormat5())
1774
branch_files = [('revision-history', True),
1775
('branch-name', True),
1777
for entry in branch_files:
1778
self.move_entry('branch', entry)
1780
self.step('Upgrading working tree')
1781
self.bzrdir.transport.mkdir('checkout', mode=self.dir_mode)
1782
self.make_lock('checkout')
1783
self.put_format('checkout', bzrlib.workingtree.WorkingTreeFormat3())
1784
self.bzrdir.transport.delete_multi(self.garbage_inventories, self.pb)
1785
checkout_files = [('pending-merges', True),
1786
('inventory', True),
1787
('stat-cache', False)]
1788
for entry in checkout_files:
1789
self.move_entry('checkout', entry)
1790
if last_revision is not None:
1791
self.bzrdir._control_files.put_utf8('checkout/last-revision',
1793
self.bzrdir._control_files.put_utf8('branch-format', BzrDirMetaFormat1().get_format_string())
1794
return BzrDir.open(self.bzrdir.root_transport.base)
1796
def make_lock(self, name):
1797
"""Make a lock for the new control dir name."""
1798
self.step('Make %s lock' % name)
1799
ld = LockDir(self.bzrdir.transport,
1801
file_modebits=self.file_mode,
1802
dir_modebits=self.dir_mode)
1805
def move_entry(self, new_dir, entry):
1806
"""Move then entry name into new_dir."""
1808
mandatory = entry[1]
1809
self.step('Moving %s' % name)
1811
self.bzrdir.transport.move(name, '%s/%s' % (new_dir, name))
1812
except errors.NoSuchFile:
1816
def put_format(self, dirname, format):
1817
self.bzrdir._control_files.put_utf8('%s/format' % dirname, format.get_format_string())
1820
class ConvertMetaToMeta(Converter):
1821
"""Converts the components of metadirs."""
1823
def __init__(self, target_format):
1824
"""Create a metadir to metadir converter.
1826
:param target_format: The final metadir format that is desired.
1828
self.target_format = target_format
1830
def convert(self, to_convert, pb):
1831
"""See Converter.convert()."""
1832
self.bzrdir = to_convert
1836
self.step('checking repository format')
1838
repo = self.bzrdir.open_repository()
1839
except errors.NoRepositoryPresent:
1842
if not isinstance(repo._format, self.target_format.repository_format.__class__):
1843
from bzrlib.repository import CopyConverter
1844
self.pb.note('starting repository conversion')
1845
converter = CopyConverter(self.target_format.repository_format)
1846
converter.convert(repo, pb)