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
# TODO: The Format probe_transport seems a bit redundant with just trying to
26
# open the bzrdir. -- mbp
28
# TODO: Can we move specific formats into separate modules to make this file
31
from copy import deepcopy
32
from cStringIO import StringIO
34
from stat import S_ISDIR
35
from unittest import TestSuite
38
import bzrlib.errors as errors
39
from bzrlib.lockable_files import LockableFiles, TransportLock
40
from bzrlib.lockdir import LockDir
41
from bzrlib.osutils import (
48
from bzrlib.store.revision.text import TextRevisionStore
49
from bzrlib.store.text import TextStore
50
from bzrlib.store.versioned import WeaveStore
51
from bzrlib.trace import mutter
52
from bzrlib.transactions import WriteTransaction
53
from bzrlib.transport import get_transport
54
from bzrlib.transport.local import LocalTransport
55
import bzrlib.urlutils as urlutils
56
from bzrlib.weave import Weave
57
from bzrlib.xml4 import serializer_v4
62
"""A .bzr control diretory.
64
BzrDir instances let you create or open any of the things that can be
65
found within .bzr - checkouts, branches and repositories.
68
the transport which this bzr dir is rooted at (i.e. file:///.../.bzr/)
70
a transport connected to the directory this bzr was opened from.
74
"""Invoke break_lock on the first object in the bzrdir.
76
If there is a tree, the tree is opened and break_lock() called.
77
Otherwise, branch is tried, and finally repository.
80
thing_to_unlock = self.open_workingtree()
81
except (errors.NotLocalUrl, errors.NoWorkingTree):
83
thing_to_unlock = self.open_branch()
84
except errors.NotBranchError:
86
thing_to_unlock = self.open_repository()
87
except errors.NoRepositoryPresent:
89
thing_to_unlock.break_lock()
91
def can_convert_format(self):
92
"""Return true if this bzrdir is one whose format we can convert from."""
96
def _check_supported(format, allow_unsupported):
97
"""Check whether format is a supported format.
99
If allow_unsupported is True, this is a no-op.
101
if not allow_unsupported and not format.is_supported():
102
# see open_downlevel to open legacy branches.
103
raise errors.UnsupportedFormatError(format=format)
105
def clone(self, url, revision_id=None, basis=None, force_new_repo=False):
106
"""Clone this bzrdir and its contents to url verbatim.
108
If urls last component does not exist, it will be created.
110
if revision_id is not None, then the clone operation may tune
111
itself to download less data.
112
:param force_new_repo: Do not use a shared repository for the target
113
even if one is available.
116
basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
117
result = self._format.initialize(url)
119
local_repo = self.find_repository()
120
except errors.NoRepositoryPresent:
123
# may need to copy content in
125
result_repo = local_repo.clone(
127
revision_id=revision_id,
129
result_repo.set_make_working_trees(local_repo.make_working_trees())
132
result_repo = result.find_repository()
133
# fetch content this dir needs.
135
# XXX FIXME RBC 20060214 need tests for this when the basis
137
result_repo.fetch(basis_repo, revision_id=revision_id)
138
result_repo.fetch(local_repo, revision_id=revision_id)
139
except errors.NoRepositoryPresent:
140
# needed to make one anyway.
141
result_repo = local_repo.clone(
143
revision_id=revision_id,
145
result_repo.set_make_working_trees(local_repo.make_working_trees())
146
# 1 if there is a branch present
147
# make sure its content is available in the target repository
150
self.open_branch().clone(result, revision_id=revision_id)
151
except errors.NotBranchError:
154
self.open_workingtree().clone(result, basis=basis_tree)
155
except (errors.NoWorkingTree, errors.NotLocalUrl):
159
def _get_basis_components(self, basis):
160
"""Retrieve the basis components that are available at basis."""
162
return None, None, None
164
basis_tree = basis.open_workingtree()
165
basis_branch = basis_tree.branch
166
basis_repo = basis_branch.repository
167
except (errors.NoWorkingTree, errors.NotLocalUrl):
170
basis_branch = basis.open_branch()
171
basis_repo = basis_branch.repository
172
except errors.NotBranchError:
175
basis_repo = basis.open_repository()
176
except errors.NoRepositoryPresent:
178
return basis_repo, basis_branch, basis_tree
180
# TODO: This should be given a Transport, and should chdir up; otherwise
181
# this will open a new connection.
182
def _make_tail(self, url):
183
head, tail = urlutils.split(url)
184
if tail and tail != '.':
185
t = bzrlib.transport.get_transport(head)
188
except errors.FileExists:
191
# TODO: Should take a Transport
193
def create(cls, base):
194
"""Create a new BzrDir at the url 'base'.
196
This will call the current default formats initialize with base
197
as the only parameter.
199
If you need a specific format, consider creating an instance
200
of that and calling initialize().
202
if cls is not BzrDir:
203
raise AssertionError("BzrDir.create always creates the default format, "
204
"not one of %r" % cls)
205
head, tail = urlutils.split(base)
206
if tail and tail != '.':
207
t = bzrlib.transport.get_transport(head)
210
except errors.FileExists:
212
return BzrDirFormat.get_default_format().initialize(safe_unicode(base))
214
def create_branch(self):
215
"""Create a branch in this BzrDir.
217
The bzrdirs format will control what branch format is created.
218
For more control see BranchFormatXX.create(a_bzrdir).
220
raise NotImplementedError(self.create_branch)
223
def create_branch_and_repo(base, force_new_repo=False):
224
"""Create a new BzrDir, Branch and Repository at the url 'base'.
226
This will use the current default BzrDirFormat, and use whatever
227
repository format that that uses via bzrdir.create_branch and
228
create_repository. If a shared repository is available that is used
231
The created Branch object is returned.
233
:param base: The URL to create the branch at.
234
:param force_new_repo: If True a new repository is always created.
236
bzrdir = BzrDir.create(base)
237
bzrdir._find_or_create_repository(force_new_repo)
238
return bzrdir.create_branch()
240
def _find_or_create_repository(self, force_new_repo):
241
"""Create a new repository if needed, returning the repository."""
243
return self.create_repository()
245
return self.find_repository()
246
except errors.NoRepositoryPresent:
247
return self.create_repository()
250
def create_branch_convenience(base, force_new_repo=False,
251
force_new_tree=None, format=None):
252
"""Create a new BzrDir, Branch and Repository at the url 'base'.
254
This is a convenience function - it will use an existing repository
255
if possible, can be told explicitly whether to create a working tree or
258
This will use the current default BzrDirFormat, and use whatever
259
repository format that that uses via bzrdir.create_branch and
260
create_repository. If a shared repository is available that is used
261
preferentially. Whatever repository is used, its tree creation policy
264
The created Branch object is returned.
265
If a working tree cannot be made due to base not being a file:// url,
266
no error is raised unless force_new_tree is True, in which case no
267
data is created on disk and NotLocalUrl is raised.
269
:param base: The URL to create the branch at.
270
:param force_new_repo: If True a new repository is always created.
271
:param force_new_tree: If True or False force creation of a tree or
272
prevent such creation respectively.
273
:param format: Override for the for the bzrdir format to create
276
# check for non local urls
277
t = get_transport(safe_unicode(base))
278
if not isinstance(t, LocalTransport):
279
raise errors.NotLocalUrl(base)
281
bzrdir = BzrDir.create(base)
283
bzrdir = format.initialize(base)
284
repo = bzrdir._find_or_create_repository(force_new_repo)
285
result = bzrdir.create_branch()
286
if force_new_tree or (repo.make_working_trees() and
287
force_new_tree is None):
289
bzrdir.create_workingtree()
290
except errors.NotLocalUrl:
295
def create_repository(base, shared=False):
296
"""Create a new BzrDir and Repository at the url 'base'.
298
This will use the current default BzrDirFormat, and use whatever
299
repository format that that uses for bzrdirformat.create_repository.
301
:param shared: Create a shared repository rather than a standalone
303
The Repository object is returned.
305
This must be overridden as an instance method in child classes, where
306
it should take no parameters and construct whatever repository format
307
that child class desires.
309
bzrdir = BzrDir.create(base)
310
return bzrdir.create_repository(shared)
313
def create_standalone_workingtree(base):
314
"""Create a new BzrDir, WorkingTree, Branch and Repository at 'base'.
316
'base' must be a local path or a file:// url.
318
This will use the current default BzrDirFormat, and use whatever
319
repository format that that uses for bzrdirformat.create_workingtree,
320
create_branch and create_repository.
322
:return: The WorkingTree object.
324
t = get_transport(safe_unicode(base))
325
if not isinstance(t, LocalTransport):
326
raise errors.NotLocalUrl(base)
327
bzrdir = BzrDir.create_branch_and_repo(safe_unicode(base),
328
force_new_repo=True).bzrdir
329
return bzrdir.create_workingtree()
331
def create_workingtree(self, revision_id=None):
332
"""Create a working tree at this BzrDir.
334
revision_id: create it as of this revision id.
336
raise NotImplementedError(self.create_workingtree)
338
def find_repository(self):
339
"""Find the repository that should be used for a_bzrdir.
341
This does not require a branch as we use it to find the repo for
342
new branches as well as to hook existing branches up to their
346
return self.open_repository()
347
except errors.NoRepositoryPresent:
349
next_transport = self.root_transport.clone('..')
351
# find the next containing bzrdir
353
found_bzrdir = BzrDir.open_containing_from_transport(
355
except errors.NotBranchError:
357
raise errors.NoRepositoryPresent(self)
358
# does it have a repository ?
360
repository = found_bzrdir.open_repository()
361
except errors.NoRepositoryPresent:
362
next_transport = found_bzrdir.root_transport.clone('..')
363
if (found_bzrdir.root_transport.base == next_transport.base):
364
# top of the file system
368
if ((found_bzrdir.root_transport.base ==
369
self.root_transport.base) or repository.is_shared()):
372
raise errors.NoRepositoryPresent(self)
373
raise errors.NoRepositoryPresent(self)
375
def get_branch_transport(self, branch_format):
376
"""Get the transport for use by branch format in this BzrDir.
378
Note that bzr dirs that do not support format strings will raise
379
IncompatibleFormat if the branch format they are given has
380
a format string, and vice versa.
382
If branch_format is None, the transport is returned with no
383
checking. if it is not None, then the returned transport is
384
guaranteed to point to an existing directory ready for use.
386
raise NotImplementedError(self.get_branch_transport)
388
def get_repository_transport(self, repository_format):
389
"""Get the transport for use by repository format in this BzrDir.
391
Note that bzr dirs that do not support format strings will raise
392
IncompatibleFormat if the repository format they are given has
393
a format string, and vice versa.
395
If repository_format is None, the transport is returned with no
396
checking. if it is not None, then the returned transport is
397
guaranteed to point to an existing directory ready for use.
399
raise NotImplementedError(self.get_repository_transport)
401
def get_workingtree_transport(self, tree_format):
402
"""Get the transport for use by workingtree format in this BzrDir.
404
Note that bzr dirs that do not support format strings will raise
405
IncompatibleFormat if the workingtree format they are given has
406
a format string, and vice versa.
408
If workingtree_format is None, the transport is returned with no
409
checking. if it is not None, then the returned transport is
410
guaranteed to point to an existing directory ready for use.
412
raise NotImplementedError(self.get_workingtree_transport)
414
def __init__(self, _transport, _format):
415
"""Initialize a Bzr control dir object.
417
Only really common logic should reside here, concrete classes should be
418
made with varying behaviours.
420
:param _format: the format that is creating this BzrDir instance.
421
:param _transport: the transport this dir is based at.
423
self._format = _format
424
self.transport = _transport.clone('.bzr')
425
self.root_transport = _transport
427
def is_control_filename(self, filename):
428
"""True if filename is the name of a path which is reserved for bzrdir's.
430
:param filename: A filename within the root transport of this bzrdir.
432
This is true IF and ONLY IF the filename is part of the namespace reserved
433
for bzr control dirs. Currently this is the '.bzr' directory in the root
434
of the root_transport. it is expected that plugins will need to extend
435
this in the future - for instance to make bzr talk with svn working
438
# this might be better on the BzrDirFormat class because it refers to
439
# all the possible bzrdir disk formats.
440
# This method is tested via the workingtree is_control_filename tests-
441
# it was extracted from WorkingTree.is_control_filename. If the methods
442
# contract is extended beyond the current trivial implementation please
443
# add new tests for it to the appropriate place.
444
return filename == '.bzr' or filename.startswith('.bzr/')
446
def needs_format_conversion(self, format=None):
447
"""Return true if this bzrdir needs convert_format run on it.
449
For instance, if the repository format is out of date but the
450
branch and working tree are not, this should return True.
452
:param format: Optional parameter indicating a specific desired
453
format we plan to arrive at.
455
raise NotImplementedError(self.needs_format_conversion)
458
def open_unsupported(base):
459
"""Open a branch which is not supported."""
460
return BzrDir.open(base, _unsupported=True)
463
def open(base, _unsupported=False):
464
"""Open an existing bzrdir, rooted at 'base' (url)
466
_unsupported is a private parameter to the BzrDir class.
468
t = get_transport(base)
469
# mutter("trying to open %r with transport %r", base, t)
470
format = BzrDirFormat.find_format(t)
471
BzrDir._check_supported(format, _unsupported)
472
return format.open(t, _found=True)
474
def open_branch(self, unsupported=False):
475
"""Open the branch object at this BzrDir if one is present.
477
If unsupported is True, then no longer supported branch formats can
480
TODO: static convenience version of this?
482
raise NotImplementedError(self.open_branch)
485
def open_containing(url):
486
"""Open an existing branch which contains url.
488
:param url: url to search from.
489
See open_containing_from_transport for more detail.
491
return BzrDir.open_containing_from_transport(get_transport(url))
494
def open_containing_from_transport(a_transport):
495
"""Open an existing branch which contains a_transport.base
497
This probes for a branch at a_transport, and searches upwards from there.
499
Basically we keep looking up until we find the control directory or
500
run into the root. If there isn't one, raises NotBranchError.
501
If there is one and it is either an unrecognised format or an unsupported
502
format, UnknownFormatError or UnsupportedFormatError are raised.
503
If there is one, it is returned, along with the unused portion of url.
505
:return: The BzrDir that contains the path, and a Unicode path
506
for the rest of the URL.
508
# this gets the normalised url back. I.e. '.' -> the full path.
509
url = a_transport.base
512
format = BzrDirFormat.find_format(a_transport)
513
BzrDir._check_supported(format, False)
514
return format.open(a_transport), urlutils.unescape(a_transport.relpath(url))
515
except errors.NotBranchError, e:
516
## mutter('not a branch in: %r %s', a_transport.base, e)
518
new_t = a_transport.clone('..')
519
if new_t.base == a_transport.base:
520
# reached the root, whatever that may be
521
raise errors.NotBranchError(path=url)
524
def open_repository(self, _unsupported=False):
525
"""Open the repository object at this BzrDir if one is present.
527
This will not follow the Branch object pointer - its strictly a direct
528
open facility. Most client code should use open_branch().repository to
531
_unsupported is a private parameter, not part of the api.
532
TODO: static convenience version of this?
534
raise NotImplementedError(self.open_repository)
536
def open_workingtree(self, _unsupported=False):
537
"""Open the workingtree object at this BzrDir if one is present.
539
TODO: static convenience version of this?
541
raise NotImplementedError(self.open_workingtree)
543
def has_branch(self):
544
"""Tell if this bzrdir contains a branch.
546
Note: if you're going to open the branch, you should just go ahead
547
and try, and not ask permission first. (This method just opens the
548
branch and discards it, and that's somewhat expensive.)
553
except errors.NotBranchError:
556
def has_workingtree(self):
557
"""Tell if this bzrdir contains a working tree.
559
This will still raise an exception if the bzrdir has a workingtree that
560
is remote & inaccessible.
562
Note: if you're going to open the working tree, you should just go ahead
563
and try, and not ask permission first. (This method just opens the
564
workingtree and discards it, and that's somewhat expensive.)
567
self.open_workingtree()
569
except errors.NoWorkingTree:
572
def sprout(self, url, revision_id=None, basis=None, force_new_repo=False):
573
"""Create a copy of this bzrdir prepared for use as a new line of
576
If urls last component does not exist, it will be created.
578
Attributes related to the identity of the source branch like
579
branch nickname will be cleaned, a working tree is created
580
whether one existed before or not; and a local branch is always
583
if revision_id is not None, then the clone operation may tune
584
itself to download less data.
587
result = self._format.initialize(url)
588
basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
590
source_branch = self.open_branch()
591
source_repository = source_branch.repository
592
except errors.NotBranchError:
595
source_repository = self.open_repository()
596
except errors.NoRepositoryPresent:
597
# copy the entire basis one if there is one
598
# but there is no repository.
599
source_repository = basis_repo
604
result_repo = result.find_repository()
605
except errors.NoRepositoryPresent:
607
if source_repository is None and result_repo is not None:
609
elif source_repository is None and result_repo is None:
610
# no repo available, make a new one
611
result.create_repository()
612
elif source_repository is not None and result_repo is None:
613
# have source, and want to make a new target repo
614
# we don't clone the repo because that preserves attributes
615
# like is_shared(), and we have not yet implemented a
616
# repository sprout().
617
result_repo = result.create_repository()
618
if result_repo is not None:
619
# fetch needed content into target.
621
# XXX FIXME RBC 20060214 need tests for this when the basis
623
result_repo.fetch(basis_repo, revision_id=revision_id)
624
if source_repository is not None:
625
result_repo.fetch(source_repository, revision_id=revision_id)
626
if source_branch is not None:
627
source_branch.sprout(result, revision_id=revision_id)
629
result.create_branch()
630
# TODO: jam 20060426 we probably need a test in here in the
631
# case that the newly sprouted branch is a remote one
632
if result_repo is None or result_repo.make_working_trees():
633
result.create_workingtree()
637
class BzrDirPreSplitOut(BzrDir):
638
"""A common class for the all-in-one formats."""
640
def __init__(self, _transport, _format):
641
"""See BzrDir.__init__."""
642
super(BzrDirPreSplitOut, self).__init__(_transport, _format)
643
assert self._format._lock_class == TransportLock
644
assert self._format._lock_file_name == 'branch-lock'
645
self._control_files = LockableFiles(self.get_branch_transport(None),
646
self._format._lock_file_name,
647
self._format._lock_class)
649
def break_lock(self):
650
"""Pre-splitout bzrdirs do not suffer from stale locks."""
651
raise NotImplementedError(self.break_lock)
653
def clone(self, url, revision_id=None, basis=None, force_new_repo=False):
654
"""See BzrDir.clone()."""
655
from bzrlib.workingtree import WorkingTreeFormat2
657
result = self._format._initialize_for_clone(url)
658
basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
659
self.open_repository().clone(result, revision_id=revision_id, basis=basis_repo)
660
from_branch = self.open_branch()
661
from_branch.clone(result, revision_id=revision_id)
663
self.open_workingtree().clone(result, basis=basis_tree)
664
except errors.NotLocalUrl:
665
# make a new one, this format always has to have one.
667
WorkingTreeFormat2().initialize(result)
668
except errors.NotLocalUrl:
669
# but we cannot do it for remote trees.
670
to_branch = result.open_branch()
671
WorkingTreeFormat2().stub_initialize_remote(to_branch.control_files)
674
def create_branch(self):
675
"""See BzrDir.create_branch."""
676
return self.open_branch()
678
def create_repository(self, shared=False):
679
"""See BzrDir.create_repository."""
681
raise errors.IncompatibleFormat('shared repository', self._format)
682
return self.open_repository()
684
def create_workingtree(self, revision_id=None):
685
"""See BzrDir.create_workingtree."""
686
# this looks buggy but is not -really-
687
# clone and sprout will have set the revision_id
688
# and that will have set it for us, its only
689
# specific uses of create_workingtree in isolation
690
# that can do wonky stuff here, and that only
691
# happens for creating checkouts, which cannot be
692
# done on this format anyway. So - acceptable wart.
693
result = self.open_workingtree()
694
if revision_id is not None:
695
result.set_last_revision(revision_id)
698
def get_branch_transport(self, branch_format):
699
"""See BzrDir.get_branch_transport()."""
700
if branch_format is None:
701
return self.transport
703
branch_format.get_format_string()
704
except NotImplementedError:
705
return self.transport
706
raise errors.IncompatibleFormat(branch_format, self._format)
708
def get_repository_transport(self, repository_format):
709
"""See BzrDir.get_repository_transport()."""
710
if repository_format is None:
711
return self.transport
713
repository_format.get_format_string()
714
except NotImplementedError:
715
return self.transport
716
raise errors.IncompatibleFormat(repository_format, self._format)
718
def get_workingtree_transport(self, workingtree_format):
719
"""See BzrDir.get_workingtree_transport()."""
720
if workingtree_format is None:
721
return self.transport
723
workingtree_format.get_format_string()
724
except NotImplementedError:
725
return self.transport
726
raise errors.IncompatibleFormat(workingtree_format, self._format)
728
def needs_format_conversion(self, format=None):
729
"""See BzrDir.needs_format_conversion()."""
730
# if the format is not the same as the system default,
731
# an upgrade is needed.
733
format = BzrDirFormat.get_default_format()
734
return not isinstance(self._format, format.__class__)
736
def open_branch(self, unsupported=False):
737
"""See BzrDir.open_branch."""
738
from bzrlib.branch import BzrBranchFormat4
739
format = BzrBranchFormat4()
740
self._check_supported(format, unsupported)
741
return format.open(self, _found=True)
743
def sprout(self, url, revision_id=None, basis=None, force_new_repo=False):
744
"""See BzrDir.sprout()."""
745
from bzrlib.workingtree import WorkingTreeFormat2
747
result = self._format._initialize_for_clone(url)
748
basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
750
self.open_repository().clone(result, revision_id=revision_id, basis=basis_repo)
751
except errors.NoRepositoryPresent:
754
self.open_branch().sprout(result, revision_id=revision_id)
755
except errors.NotBranchError:
757
# we always want a working tree
758
WorkingTreeFormat2().initialize(result)
762
class BzrDir4(BzrDirPreSplitOut):
763
"""A .bzr version 4 control object.
765
This is a deprecated format and may be removed after sept 2006.
768
def create_repository(self, shared=False):
769
"""See BzrDir.create_repository."""
770
return self._format.repository_format.initialize(self, shared)
772
def needs_format_conversion(self, format=None):
773
"""Format 4 dirs are always in need of conversion."""
776
def open_repository(self):
777
"""See BzrDir.open_repository."""
778
from bzrlib.repository import RepositoryFormat4
779
return RepositoryFormat4().open(self, _found=True)
782
class BzrDir5(BzrDirPreSplitOut):
783
"""A .bzr version 5 control object.
785
This is a deprecated format and may be removed after sept 2006.
788
def open_repository(self):
789
"""See BzrDir.open_repository."""
790
from bzrlib.repository import RepositoryFormat5
791
return RepositoryFormat5().open(self, _found=True)
793
def open_workingtree(self, _unsupported=False):
794
"""See BzrDir.create_workingtree."""
795
from bzrlib.workingtree import WorkingTreeFormat2
796
return WorkingTreeFormat2().open(self, _found=True)
799
class BzrDir6(BzrDirPreSplitOut):
800
"""A .bzr version 6 control object.
802
This is a deprecated format and may be removed after sept 2006.
805
def open_repository(self):
806
"""See BzrDir.open_repository."""
807
from bzrlib.repository import RepositoryFormat6
808
return RepositoryFormat6().open(self, _found=True)
810
def open_workingtree(self, _unsupported=False):
811
"""See BzrDir.create_workingtree."""
812
from bzrlib.workingtree import WorkingTreeFormat2
813
return WorkingTreeFormat2().open(self, _found=True)
816
class BzrDirMeta1(BzrDir):
817
"""A .bzr meta version 1 control object.
819
This is the first control object where the
820
individual aspects are really split out: there are separate repository,
821
workingtree and branch subdirectories and any subset of the three can be
822
present within a BzrDir.
825
def can_convert_format(self):
826
"""See BzrDir.can_convert_format()."""
829
def create_branch(self):
830
"""See BzrDir.create_branch."""
831
from bzrlib.branch import BranchFormat
832
return BranchFormat.get_default_format().initialize(self)
834
def create_repository(self, shared=False):
835
"""See BzrDir.create_repository."""
836
return self._format.repository_format.initialize(self, shared)
838
def create_workingtree(self, revision_id=None):
839
"""See BzrDir.create_workingtree."""
840
from bzrlib.workingtree import WorkingTreeFormat
841
return WorkingTreeFormat.get_default_format().initialize(self, revision_id)
843
def _get_mkdir_mode(self):
844
"""Figure out the mode to use when creating a bzrdir subdir."""
845
temp_control = LockableFiles(self.transport, '', TransportLock)
846
return temp_control._dir_mode
848
def get_branch_transport(self, branch_format):
849
"""See BzrDir.get_branch_transport()."""
850
if branch_format is None:
851
return self.transport.clone('branch')
853
branch_format.get_format_string()
854
except NotImplementedError:
855
raise errors.IncompatibleFormat(branch_format, self._format)
857
self.transport.mkdir('branch', mode=self._get_mkdir_mode())
858
except errors.FileExists:
860
return self.transport.clone('branch')
862
def get_repository_transport(self, repository_format):
863
"""See BzrDir.get_repository_transport()."""
864
if repository_format is None:
865
return self.transport.clone('repository')
867
repository_format.get_format_string()
868
except NotImplementedError:
869
raise errors.IncompatibleFormat(repository_format, self._format)
871
self.transport.mkdir('repository', mode=self._get_mkdir_mode())
872
except errors.FileExists:
874
return self.transport.clone('repository')
876
def get_workingtree_transport(self, workingtree_format):
877
"""See BzrDir.get_workingtree_transport()."""
878
if workingtree_format is None:
879
return self.transport.clone('checkout')
881
workingtree_format.get_format_string()
882
except NotImplementedError:
883
raise errors.IncompatibleFormat(workingtree_format, self._format)
885
self.transport.mkdir('checkout', mode=self._get_mkdir_mode())
886
except errors.FileExists:
888
return self.transport.clone('checkout')
890
def needs_format_conversion(self, format=None):
891
"""See BzrDir.needs_format_conversion()."""
893
format = BzrDirFormat.get_default_format()
894
if not isinstance(self._format, format.__class__):
895
# it is not a meta dir format, conversion is needed.
897
# we might want to push this down to the repository?
899
if not isinstance(self.open_repository()._format,
900
format.repository_format.__class__):
901
# the repository needs an upgrade.
903
except errors.NoRepositoryPresent:
905
# currently there are no other possible conversions for meta1 formats.
908
def open_branch(self, unsupported=False):
909
"""See BzrDir.open_branch."""
910
from bzrlib.branch import BranchFormat
911
format = BranchFormat.find_format(self)
912
self._check_supported(format, unsupported)
913
return format.open(self, _found=True)
915
def open_repository(self, unsupported=False):
916
"""See BzrDir.open_repository."""
917
from bzrlib.repository import RepositoryFormat
918
format = RepositoryFormat.find_format(self)
919
self._check_supported(format, unsupported)
920
return format.open(self, _found=True)
922
def open_workingtree(self, unsupported=False):
923
"""See BzrDir.open_workingtree."""
924
from bzrlib.workingtree import WorkingTreeFormat
925
format = WorkingTreeFormat.find_format(self)
926
self._check_supported(format, unsupported)
927
return format.open(self, _found=True)
930
class BzrDirFormat(object):
931
"""An encapsulation of the initialization and open routines for a format.
933
Formats provide three things:
934
* An initialization routine,
938
Formats are placed in an dict by their format string for reference
939
during bzrdir opening. These should be subclasses of BzrDirFormat
942
Once a format is deprecated, just deprecate the initialize and open
943
methods on the format class. Do not deprecate the object, as the
944
object will be created every system load.
947
_default_format = None
948
"""The default format used for new .bzr dirs."""
951
"""The known formats."""
953
_control_formats = []
954
"""The registered control formats - .bzr, ....
956
This is a list of BzrDirFormat objects.
959
_lock_file_name = 'branch-lock'
961
# _lock_class must be set in subclasses to the lock type, typ.
962
# TransportLock or LockDir
965
def find_format(klass, transport):
966
"""Return the format present at transport."""
967
for format in klass._control_formats:
969
return format.probe_transport(transport)
970
except errors.NotBranchError:
971
# this format does not find a control dir here.
973
raise errors.NotBranchError(path=transport.base)
976
def probe_transport(klass, transport):
977
"""Return the .bzrdir style transport present at URL."""
979
format_string = transport.get(".bzr/branch-format").read()
980
except errors.NoSuchFile:
981
raise errors.NotBranchError(path=transport.base)
984
return klass._formats[format_string]
986
raise errors.UnknownFormatError(format=format_string)
989
def get_default_format(klass):
990
"""Return the current default format."""
991
return klass._default_format
993
def get_format_string(self):
994
"""Return the ASCII format string that identifies this format."""
995
raise NotImplementedError(self.get_format_string)
997
def get_format_description(self):
998
"""Return the short description for this format."""
999
raise NotImplementedError(self.get_format_description)
1001
def get_converter(self, format=None):
1002
"""Return the converter to use to convert bzrdirs needing converts.
1004
This returns a bzrlib.bzrdir.Converter object.
1006
This should return the best upgrader to step this format towards the
1007
current default format. In the case of plugins we can/should provide
1008
some means for them to extend the range of returnable converters.
1010
:param format: Optional format to override the default format of the
1013
raise NotImplementedError(self.get_converter)
1015
def initialize(self, url):
1016
"""Create a bzr control dir at this url and return an opened copy.
1018
Subclasses should typically override initialize_on_transport
1019
instead of this method.
1021
return self.initialize_on_transport(get_transport(url))
1023
def initialize_on_transport(self, transport):
1024
"""Initialize a new bzrdir in the base directory of a Transport."""
1025
# Since we don't have a .bzr directory, inherit the
1026
# mode from the root directory
1027
temp_control = LockableFiles(transport, '', TransportLock)
1028
temp_control._transport.mkdir('.bzr',
1029
# FIXME: RBC 20060121 don't peek under
1031
mode=temp_control._dir_mode)
1032
file_mode = temp_control._file_mode
1034
mutter('created control directory in ' + transport.base)
1035
control = transport.clone('.bzr')
1036
utf8_files = [('README',
1037
"This is a Bazaar-NG control directory.\n"
1038
"Do not change any files in this directory.\n"),
1039
('branch-format', self.get_format_string()),
1041
# NB: no need to escape relative paths that are url safe.
1042
control_files = LockableFiles(control, self._lock_file_name,
1044
control_files.create_lock()
1045
control_files.lock_write()
1047
for file, content in utf8_files:
1048
control_files.put_utf8(file, content)
1050
control_files.unlock()
1051
return self.open(transport, _found=True)
1053
def is_supported(self):
1054
"""Is this format supported?
1056
Supported formats must be initializable and openable.
1057
Unsupported formats may not support initialization or committing or
1058
some other features depending on the reason for not being supported.
1063
def known_formats(klass):
1064
"""Return all the known formats.
1066
Concrete formats should override _known_formats.
1068
# There is double indirection here to make sure that control
1069
# formats used by more than one dir format will only be probed
1070
# once. This can otherwise be quite expensive for remote connections.
1072
for format in klass._control_formats:
1073
result.update(format._known_formats())
1077
def _known_formats(klass):
1078
"""Return the known format instances for this control format."""
1079
return set(klass._formats.values())
1081
def open(self, transport, _found=False):
1082
"""Return an instance of this format for the dir transport points at.
1084
_found is a private parameter, do not use it.
1087
assert isinstance(BzrDirFormat.find_format(transport),
1089
return self._open(transport)
1091
def _open(self, transport):
1092
"""Template method helper for opening BzrDirectories.
1094
This performs the actual open and any additional logic or parameter
1097
raise NotImplementedError(self._open)
1100
def register_format(klass, format):
1101
klass._formats[format.get_format_string()] = format
1104
def register_control_format(klass, format):
1105
"""Register a format that does not use '.bzrdir' for its control dir.
1107
TODO: This should be pulled up into a 'ControlDirFormat' base class
1108
which BzrDirFormat can inherit from, and renamed to register_format
1109
there. It has been done without that for now for simplicity of
1112
klass._control_formats.append(format)
1115
def set_default_format(klass, format):
1116
klass._default_format = format
1119
return self.get_format_string()[:-1]
1122
def unregister_format(klass, format):
1123
assert klass._formats[format.get_format_string()] is format
1124
del klass._formats[format.get_format_string()]
1127
def unregister_control_format(klass, format):
1128
klass._control_formats.remove(format)
1131
# register BzrDirFormat as a control format
1132
BzrDirFormat.register_control_format(BzrDirFormat)
1135
class BzrDirFormat4(BzrDirFormat):
1136
"""Bzr dir format 4.
1138
This format is a combined format for working tree, branch and repository.
1140
- Format 1 working trees [always]
1141
- Format 4 branches [always]
1142
- Format 4 repositories [always]
1144
This format is deprecated: it indexes texts using a text it which is
1145
removed in format 5; write support for this format has been removed.
1148
_lock_class = TransportLock
1150
def get_format_string(self):
1151
"""See BzrDirFormat.get_format_string()."""
1152
return "Bazaar-NG branch, format 0.0.4\n"
1154
def get_format_description(self):
1155
"""See BzrDirFormat.get_format_description()."""
1156
return "All-in-one format 4"
1158
def get_converter(self, format=None):
1159
"""See BzrDirFormat.get_converter()."""
1160
# there is one and only one upgrade path here.
1161
return ConvertBzrDir4To5()
1163
def initialize_on_transport(self, transport):
1164
"""Format 4 branches cannot be created."""
1165
raise errors.UninitializableFormat(self)
1167
def is_supported(self):
1168
"""Format 4 is not supported.
1170
It is not supported because the model changed from 4 to 5 and the
1171
conversion logic is expensive - so doing it on the fly was not
1176
def _open(self, transport):
1177
"""See BzrDirFormat._open."""
1178
return BzrDir4(transport, self)
1180
def __return_repository_format(self):
1181
"""Circular import protection."""
1182
from bzrlib.repository import RepositoryFormat4
1183
return RepositoryFormat4(self)
1184
repository_format = property(__return_repository_format)
1187
class BzrDirFormat5(BzrDirFormat):
1188
"""Bzr control format 5.
1190
This format is a combined format for working tree, branch and repository.
1192
- Format 2 working trees [always]
1193
- Format 4 branches [always]
1194
- Format 5 repositories [always]
1195
Unhashed stores in the repository.
1198
_lock_class = TransportLock
1200
def get_format_string(self):
1201
"""See BzrDirFormat.get_format_string()."""
1202
return "Bazaar-NG branch, format 5\n"
1204
def get_format_description(self):
1205
"""See BzrDirFormat.get_format_description()."""
1206
return "All-in-one format 5"
1208
def get_converter(self, format=None):
1209
"""See BzrDirFormat.get_converter()."""
1210
# there is one and only one upgrade path here.
1211
return ConvertBzrDir5To6()
1213
def _initialize_for_clone(self, url):
1214
return self.initialize_on_transport(get_transport(url), _cloning=True)
1216
def initialize_on_transport(self, transport, _cloning=False):
1217
"""Format 5 dirs always have working tree, branch and repository.
1219
Except when they are being cloned.
1221
from bzrlib.branch import BzrBranchFormat4
1222
from bzrlib.repository import RepositoryFormat5
1223
from bzrlib.workingtree import WorkingTreeFormat2
1224
result = (super(BzrDirFormat5, self).initialize_on_transport(transport))
1225
RepositoryFormat5().initialize(result, _internal=True)
1227
branch = BzrBranchFormat4().initialize(result)
1229
WorkingTreeFormat2().initialize(result)
1230
except errors.NotLocalUrl:
1231
# Even though we can't access the working tree, we need to
1232
# create its control files.
1233
WorkingTreeFormat2().stub_initialize_remote(branch.control_files)
1236
def _open(self, transport):
1237
"""See BzrDirFormat._open."""
1238
return BzrDir5(transport, self)
1240
def __return_repository_format(self):
1241
"""Circular import protection."""
1242
from bzrlib.repository import RepositoryFormat5
1243
return RepositoryFormat5(self)
1244
repository_format = property(__return_repository_format)
1247
class BzrDirFormat6(BzrDirFormat):
1248
"""Bzr control format 6.
1250
This format is a combined format for working tree, branch and repository.
1252
- Format 2 working trees [always]
1253
- Format 4 branches [always]
1254
- Format 6 repositories [always]
1257
_lock_class = TransportLock
1259
def get_format_string(self):
1260
"""See BzrDirFormat.get_format_string()."""
1261
return "Bazaar-NG branch, format 6\n"
1263
def get_format_description(self):
1264
"""See BzrDirFormat.get_format_description()."""
1265
return "All-in-one format 6"
1267
def get_converter(self, format=None):
1268
"""See BzrDirFormat.get_converter()."""
1269
# there is one and only one upgrade path here.
1270
return ConvertBzrDir6ToMeta()
1272
def _initialize_for_clone(self, url):
1273
return self.initialize_on_transport(get_transport(url), _cloning=True)
1275
def initialize_on_transport(self, transport, _cloning=False):
1276
"""Format 6 dirs always have working tree, branch and repository.
1278
Except when they are being cloned.
1280
from bzrlib.branch import BzrBranchFormat4
1281
from bzrlib.repository import RepositoryFormat6
1282
from bzrlib.workingtree import WorkingTreeFormat2
1283
result = super(BzrDirFormat6, self).initialize_on_transport(transport)
1284
RepositoryFormat6().initialize(result, _internal=True)
1286
branch = BzrBranchFormat4().initialize(result)
1288
WorkingTreeFormat2().initialize(result)
1289
except errors.NotLocalUrl:
1290
# Even though we can't access the working tree, we need to
1291
# create its control files.
1292
WorkingTreeFormat2().stub_initialize_remote(branch.control_files)
1295
def _open(self, transport):
1296
"""See BzrDirFormat._open."""
1297
return BzrDir6(transport, self)
1299
def __return_repository_format(self):
1300
"""Circular import protection."""
1301
from bzrlib.repository import RepositoryFormat6
1302
return RepositoryFormat6(self)
1303
repository_format = property(__return_repository_format)
1306
class BzrDirMetaFormat1(BzrDirFormat):
1307
"""Bzr meta control format 1
1309
This is the first format with split out working tree, branch and repository
1312
- Format 3 working trees [optional]
1313
- Format 5 branches [optional]
1314
- Format 7 repositories [optional]
1317
_lock_class = LockDir
1319
def get_converter(self, format=None):
1320
"""See BzrDirFormat.get_converter()."""
1322
format = BzrDirFormat.get_default_format()
1323
if not isinstance(self, format.__class__):
1324
# converting away from metadir is not implemented
1325
raise NotImplementedError(self.get_converter)
1326
return ConvertMetaToMeta(format)
1328
def get_format_string(self):
1329
"""See BzrDirFormat.get_format_string()."""
1330
return "Bazaar-NG meta directory, format 1\n"
1332
def get_format_description(self):
1333
"""See BzrDirFormat.get_format_description()."""
1334
return "Meta directory format 1"
1336
def _open(self, transport):
1337
"""See BzrDirFormat._open."""
1338
return BzrDirMeta1(transport, self)
1340
def __return_repository_format(self):
1341
"""Circular import protection."""
1342
if getattr(self, '_repository_format', None):
1343
return self._repository_format
1344
from bzrlib.repository import RepositoryFormat
1345
return RepositoryFormat.get_default_format()
1347
def __set_repository_format(self, value):
1348
"""Allow changint the repository format for metadir formats."""
1349
self._repository_format = value
1351
repository_format = property(__return_repository_format, __set_repository_format)
1354
BzrDirFormat.register_format(BzrDirFormat4())
1355
BzrDirFormat.register_format(BzrDirFormat5())
1356
BzrDirFormat.register_format(BzrDirFormat6())
1357
__default_format = BzrDirMetaFormat1()
1358
BzrDirFormat.register_format(__default_format)
1359
BzrDirFormat.set_default_format(__default_format)
1362
class BzrDirTestProviderAdapter(object):
1363
"""A tool to generate a suite testing multiple bzrdir formats at once.
1365
This is done by copying the test once for each transport and injecting
1366
the transport_server, transport_readonly_server, and bzrdir_format
1367
classes into each copy. Each copy is also given a new id() to make it
1371
def __init__(self, transport_server, transport_readonly_server, formats):
1372
self._transport_server = transport_server
1373
self._transport_readonly_server = transport_readonly_server
1374
self._formats = formats
1376
def adapt(self, test):
1377
result = TestSuite()
1378
for format in self._formats:
1379
new_test = deepcopy(test)
1380
new_test.transport_server = self._transport_server
1381
new_test.transport_readonly_server = self._transport_readonly_server
1382
new_test.bzrdir_format = format
1383
def make_new_test_id():
1384
new_id = "%s(%s)" % (new_test.id(), format.__class__.__name__)
1385
return lambda: new_id
1386
new_test.id = make_new_test_id()
1387
result.addTest(new_test)
1391
class Converter(object):
1392
"""Converts a disk format object from one format to another."""
1394
def convert(self, to_convert, pb):
1395
"""Perform the conversion of to_convert, giving feedback via pb.
1397
:param to_convert: The disk object to convert.
1398
:param pb: a progress bar to use for progress information.
1401
def step(self, message):
1402
"""Update the pb by a step."""
1404
self.pb.update(message, self.count, self.total)
1407
class ConvertBzrDir4To5(Converter):
1408
"""Converts format 4 bzr dirs to format 5."""
1411
super(ConvertBzrDir4To5, self).__init__()
1412
self.converted_revs = set()
1413
self.absent_revisions = set()
1417
def convert(self, to_convert, pb):
1418
"""See Converter.convert()."""
1419
self.bzrdir = to_convert
1421
self.pb.note('starting upgrade from format 4 to 5')
1422
if isinstance(self.bzrdir.transport, LocalTransport):
1423
self.bzrdir.get_workingtree_transport(None).delete('stat-cache')
1424
self._convert_to_weaves()
1425
return BzrDir.open(self.bzrdir.root_transport.base)
1427
def _convert_to_weaves(self):
1428
self.pb.note('note: upgrade may be faster if all store files are ungzipped first')
1431
stat = self.bzrdir.transport.stat('weaves')
1432
if not S_ISDIR(stat.st_mode):
1433
self.bzrdir.transport.delete('weaves')
1434
self.bzrdir.transport.mkdir('weaves')
1435
except errors.NoSuchFile:
1436
self.bzrdir.transport.mkdir('weaves')
1437
# deliberately not a WeaveFile as we want to build it up slowly.
1438
self.inv_weave = Weave('inventory')
1439
# holds in-memory weaves for all files
1440
self.text_weaves = {}
1441
self.bzrdir.transport.delete('branch-format')
1442
self.branch = self.bzrdir.open_branch()
1443
self._convert_working_inv()
1444
rev_history = self.branch.revision_history()
1445
# to_read is a stack holding the revisions we still need to process;
1446
# appending to it adds new highest-priority revisions
1447
self.known_revisions = set(rev_history)
1448
self.to_read = rev_history[-1:]
1450
rev_id = self.to_read.pop()
1451
if (rev_id not in self.revisions
1452
and rev_id not in self.absent_revisions):
1453
self._load_one_rev(rev_id)
1455
to_import = self._make_order()
1456
for i, rev_id in enumerate(to_import):
1457
self.pb.update('converting revision', i, len(to_import))
1458
self._convert_one_rev(rev_id)
1460
self._write_all_weaves()
1461
self._write_all_revs()
1462
self.pb.note('upgraded to weaves:')
1463
self.pb.note(' %6d revisions and inventories', len(self.revisions))
1464
self.pb.note(' %6d revisions not present', len(self.absent_revisions))
1465
self.pb.note(' %6d texts', self.text_count)
1466
self._cleanup_spare_files_after_format4()
1467
self.branch.control_files.put_utf8('branch-format', BzrDirFormat5().get_format_string())
1469
def _cleanup_spare_files_after_format4(self):
1470
# FIXME working tree upgrade foo.
1471
for n in 'merged-patches', 'pending-merged-patches':
1473
## assert os.path.getsize(p) == 0
1474
self.bzrdir.transport.delete(n)
1475
except errors.NoSuchFile:
1477
self.bzrdir.transport.delete_tree('inventory-store')
1478
self.bzrdir.transport.delete_tree('text-store')
1480
def _convert_working_inv(self):
1481
inv = serializer_v4.read_inventory(self.branch.control_files.get('inventory'))
1482
new_inv_xml = bzrlib.xml5.serializer_v5.write_inventory_to_string(inv)
1483
# FIXME inventory is a working tree change.
1484
self.branch.control_files.put('inventory', new_inv_xml)
1486
def _write_all_weaves(self):
1487
controlweaves = WeaveStore(self.bzrdir.transport, prefixed=False)
1488
weave_transport = self.bzrdir.transport.clone('weaves')
1489
weaves = WeaveStore(weave_transport, prefixed=False)
1490
transaction = WriteTransaction()
1494
for file_id, file_weave in self.text_weaves.items():
1495
self.pb.update('writing weave', i, len(self.text_weaves))
1496
weaves._put_weave(file_id, file_weave, transaction)
1498
self.pb.update('inventory', 0, 1)
1499
controlweaves._put_weave('inventory', self.inv_weave, transaction)
1500
self.pb.update('inventory', 1, 1)
1504
def _write_all_revs(self):
1505
"""Write all revisions out in new form."""
1506
self.bzrdir.transport.delete_tree('revision-store')
1507
self.bzrdir.transport.mkdir('revision-store')
1508
revision_transport = self.bzrdir.transport.clone('revision-store')
1510
_revision_store = TextRevisionStore(TextStore(revision_transport,
1514
transaction = bzrlib.transactions.WriteTransaction()
1515
for i, rev_id in enumerate(self.converted_revs):
1516
self.pb.update('write revision', i, len(self.converted_revs))
1517
_revision_store.add_revision(self.revisions[rev_id], transaction)
1521
def _load_one_rev(self, rev_id):
1522
"""Load a revision object into memory.
1524
Any parents not either loaded or abandoned get queued to be
1526
self.pb.update('loading revision',
1527
len(self.revisions),
1528
len(self.known_revisions))
1529
if not self.branch.repository.has_revision(rev_id):
1531
self.pb.note('revision {%s} not present in branch; '
1532
'will be converted as a ghost',
1534
self.absent_revisions.add(rev_id)
1536
rev = self.branch.repository._revision_store.get_revision(rev_id,
1537
self.branch.repository.get_transaction())
1538
for parent_id in rev.parent_ids:
1539
self.known_revisions.add(parent_id)
1540
self.to_read.append(parent_id)
1541
self.revisions[rev_id] = rev
1543
def _load_old_inventory(self, rev_id):
1544
assert rev_id not in self.converted_revs
1545
old_inv_xml = self.branch.repository.inventory_store.get(rev_id).read()
1546
inv = serializer_v4.read_inventory_from_string(old_inv_xml)
1547
rev = self.revisions[rev_id]
1548
if rev.inventory_sha1:
1549
assert rev.inventory_sha1 == sha_string(old_inv_xml), \
1550
'inventory sha mismatch for {%s}' % rev_id
1553
def _load_updated_inventory(self, rev_id):
1554
assert rev_id in self.converted_revs
1555
inv_xml = self.inv_weave.get_text(rev_id)
1556
inv = bzrlib.xml5.serializer_v5.read_inventory_from_string(inv_xml)
1559
def _convert_one_rev(self, rev_id):
1560
"""Convert revision and all referenced objects to new format."""
1561
rev = self.revisions[rev_id]
1562
inv = self._load_old_inventory(rev_id)
1563
present_parents = [p for p in rev.parent_ids
1564
if p not in self.absent_revisions]
1565
self._convert_revision_contents(rev, inv, present_parents)
1566
self._store_new_weave(rev, inv, present_parents)
1567
self.converted_revs.add(rev_id)
1569
def _store_new_weave(self, rev, inv, present_parents):
1570
# the XML is now updated with text versions
1572
entries = inv.iter_entries()
1574
for path, ie in entries:
1575
assert hasattr(ie, 'revision'), \
1576
'no revision on {%s} in {%s}' % \
1577
(file_id, rev.revision_id)
1578
new_inv_xml = bzrlib.xml5.serializer_v5.write_inventory_to_string(inv)
1579
new_inv_sha1 = sha_string(new_inv_xml)
1580
self.inv_weave.add_lines(rev.revision_id,
1582
new_inv_xml.splitlines(True))
1583
rev.inventory_sha1 = new_inv_sha1
1585
def _convert_revision_contents(self, rev, inv, present_parents):
1586
"""Convert all the files within a revision.
1588
Also upgrade the inventory to refer to the text revision ids."""
1589
rev_id = rev.revision_id
1590
mutter('converting texts of revision {%s}',
1592
parent_invs = map(self._load_updated_inventory, present_parents)
1593
entries = inv.iter_entries()
1595
for path, ie in entries:
1596
self._convert_file_version(rev, ie, parent_invs)
1598
def _convert_file_version(self, rev, ie, parent_invs):
1599
"""Convert one version of one file.
1601
The file needs to be added into the weave if it is a merge
1602
of >=2 parents or if it's changed from its parent.
1604
file_id = ie.file_id
1605
rev_id = rev.revision_id
1606
w = self.text_weaves.get(file_id)
1609
self.text_weaves[file_id] = w
1610
text_changed = False
1611
previous_entries = ie.find_previous_heads(parent_invs,
1615
for old_revision in previous_entries:
1616
# if this fails, its a ghost ?
1617
assert old_revision in self.converted_revs
1618
self.snapshot_ie(previous_entries, ie, w, rev_id)
1620
assert getattr(ie, 'revision', None) is not None
1622
def snapshot_ie(self, previous_revisions, ie, w, rev_id):
1623
# TODO: convert this logic, which is ~= snapshot to
1624
# a call to:. This needs the path figured out. rather than a work_tree
1625
# a v4 revision_tree can be given, or something that looks enough like
1626
# one to give the file content to the entry if it needs it.
1627
# and we need something that looks like a weave store for snapshot to
1629
#ie.snapshot(rev, PATH, previous_revisions, REVISION_TREE, InMemoryWeaveStore(self.text_weaves))
1630
if len(previous_revisions) == 1:
1631
previous_ie = previous_revisions.values()[0]
1632
if ie._unchanged(previous_ie):
1633
ie.revision = previous_ie.revision
1636
text = self.branch.repository.text_store.get(ie.text_id)
1637
file_lines = text.readlines()
1638
assert sha_strings(file_lines) == ie.text_sha1
1639
assert sum(map(len, file_lines)) == ie.text_size
1640
w.add_lines(rev_id, previous_revisions, file_lines)
1641
self.text_count += 1
1643
w.add_lines(rev_id, previous_revisions, [])
1644
ie.revision = rev_id
1646
def _make_order(self):
1647
"""Return a suitable order for importing revisions.
1649
The order must be such that an revision is imported after all
1650
its (present) parents.
1652
todo = set(self.revisions.keys())
1653
done = self.absent_revisions.copy()
1656
# scan through looking for a revision whose parents
1658
for rev_id in sorted(list(todo)):
1659
rev = self.revisions[rev_id]
1660
parent_ids = set(rev.parent_ids)
1661
if parent_ids.issubset(done):
1662
# can take this one now
1663
order.append(rev_id)
1669
class ConvertBzrDir5To6(Converter):
1670
"""Converts format 5 bzr dirs to format 6."""
1672
def convert(self, to_convert, pb):
1673
"""See Converter.convert()."""
1674
self.bzrdir = to_convert
1676
self.pb.note('starting upgrade from format 5 to 6')
1677
self._convert_to_prefixed()
1678
return BzrDir.open(self.bzrdir.root_transport.base)
1680
def _convert_to_prefixed(self):
1681
from bzrlib.store import TransportStore
1682
self.bzrdir.transport.delete('branch-format')
1683
for store_name in ["weaves", "revision-store"]:
1684
self.pb.note("adding prefixes to %s" % store_name)
1685
store_transport = self.bzrdir.transport.clone(store_name)
1686
store = TransportStore(store_transport, prefixed=True)
1687
for urlfilename in store_transport.list_dir('.'):
1688
filename = urlutils.unescape(urlfilename)
1689
if (filename.endswith(".weave") or
1690
filename.endswith(".gz") or
1691
filename.endswith(".sig")):
1692
file_id = os.path.splitext(filename)[0]
1695
prefix_dir = store.hash_prefix(file_id)
1696
# FIXME keep track of the dirs made RBC 20060121
1698
store_transport.move(filename, prefix_dir + '/' + filename)
1699
except errors.NoSuchFile: # catches missing dirs strangely enough
1700
store_transport.mkdir(prefix_dir)
1701
store_transport.move(filename, prefix_dir + '/' + filename)
1702
self.bzrdir._control_files.put_utf8('branch-format', BzrDirFormat6().get_format_string())
1705
class ConvertBzrDir6ToMeta(Converter):
1706
"""Converts format 6 bzr dirs to metadirs."""
1708
def convert(self, to_convert, pb):
1709
"""See Converter.convert()."""
1710
self.bzrdir = to_convert
1713
self.total = 20 # the steps we know about
1714
self.garbage_inventories = []
1716
self.pb.note('starting upgrade from format 6 to metadir')
1717
self.bzrdir._control_files.put_utf8('branch-format', "Converting to format 6")
1718
# its faster to move specific files around than to open and use the apis...
1719
# first off, nuke ancestry.weave, it was never used.
1721
self.step('Removing ancestry.weave')
1722
self.bzrdir.transport.delete('ancestry.weave')
1723
except errors.NoSuchFile:
1725
# find out whats there
1726
self.step('Finding branch files')
1727
last_revision = self.bzrdir.open_branch().last_revision()
1728
bzrcontents = self.bzrdir.transport.list_dir('.')
1729
for name in bzrcontents:
1730
if name.startswith('basis-inventory.'):
1731
self.garbage_inventories.append(name)
1732
# create new directories for repository, working tree and branch
1733
self.dir_mode = self.bzrdir._control_files._dir_mode
1734
self.file_mode = self.bzrdir._control_files._file_mode
1735
repository_names = [('inventory.weave', True),
1736
('revision-store', True),
1738
self.step('Upgrading repository ')
1739
self.bzrdir.transport.mkdir('repository', mode=self.dir_mode)
1740
self.make_lock('repository')
1741
# we hard code the formats here because we are converting into
1742
# the meta format. The meta format upgrader can take this to a
1743
# future format within each component.
1744
self.put_format('repository', bzrlib.repository.RepositoryFormat7())
1745
for entry in repository_names:
1746
self.move_entry('repository', entry)
1748
self.step('Upgrading branch ')
1749
self.bzrdir.transport.mkdir('branch', mode=self.dir_mode)
1750
self.make_lock('branch')
1751
self.put_format('branch', bzrlib.branch.BzrBranchFormat5())
1752
branch_files = [('revision-history', True),
1753
('branch-name', True),
1755
for entry in branch_files:
1756
self.move_entry('branch', entry)
1758
checkout_files = [('pending-merges', True),
1759
('inventory', True),
1760
('stat-cache', False)]
1761
# If a mandatory checkout file is not present, the branch does not have
1762
# a functional checkout. Do not create a checkout in the converted
1764
for name, mandatory in checkout_files:
1765
if mandatory and name not in bzrcontents:
1766
has_checkout = False
1770
if not has_checkout:
1771
self.pb.note('No working tree.')
1772
# If some checkout files are there, we may as well get rid of them.
1773
for name, mandatory in checkout_files:
1774
if name in bzrcontents:
1775
self.bzrdir.transport.delete(name)
1777
self.step('Upgrading working tree')
1778
self.bzrdir.transport.mkdir('checkout', mode=self.dir_mode)
1779
self.make_lock('checkout')
1781
'checkout', bzrlib.workingtree.WorkingTreeFormat3())
1782
self.bzrdir.transport.delete_multi(
1783
self.garbage_inventories, self.pb)
1784
for entry in checkout_files:
1785
self.move_entry('checkout', entry)
1786
if last_revision is not None:
1787
self.bzrdir._control_files.put_utf8(
1788
'checkout/last-revision', last_revision)
1789
self.bzrdir._control_files.put_utf8(
1790
'branch-format', BzrDirMetaFormat1().get_format_string())
1791
return BzrDir.open(self.bzrdir.root_transport.base)
1793
def make_lock(self, name):
1794
"""Make a lock for the new control dir name."""
1795
self.step('Make %s lock' % name)
1796
ld = LockDir(self.bzrdir.transport,
1798
file_modebits=self.file_mode,
1799
dir_modebits=self.dir_mode)
1802
def move_entry(self, new_dir, entry):
1803
"""Move then entry name into new_dir."""
1805
mandatory = entry[1]
1806
self.step('Moving %s' % name)
1808
self.bzrdir.transport.move(name, '%s/%s' % (new_dir, name))
1809
except errors.NoSuchFile:
1813
def put_format(self, dirname, format):
1814
self.bzrdir._control_files.put_utf8('%s/format' % dirname, format.get_format_string())
1817
class ConvertMetaToMeta(Converter):
1818
"""Converts the components of metadirs."""
1820
def __init__(self, target_format):
1821
"""Create a metadir to metadir converter.
1823
:param target_format: The final metadir format that is desired.
1825
self.target_format = target_format
1827
def convert(self, to_convert, pb):
1828
"""See Converter.convert()."""
1829
self.bzrdir = to_convert
1833
self.step('checking repository format')
1835
repo = self.bzrdir.open_repository()
1836
except errors.NoRepositoryPresent:
1839
if not isinstance(repo._format, self.target_format.repository_format.__class__):
1840
from bzrlib.repository import CopyConverter
1841
self.pb.note('starting repository conversion')
1842
converter = CopyConverter(self.target_format.repository_format)
1843
converter.convert(repo, pb)