1
# Copyright (C) 2005, 2006 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""BzrDir logic. The BzrDir is the basic control directory used by bzr.
19
At format 7 this was split out into Branch, Repository and Checkout control
23
from copy import deepcopy
25
from cStringIO import StringIO
26
from unittest import TestSuite
29
import bzrlib.errors as errors
30
from bzrlib.lockable_files import LockableFiles, TransportLock
31
from bzrlib.lockdir import LockDir
32
from bzrlib.osutils import safe_unicode
33
from bzrlib.osutils import (
40
from bzrlib.store.revision.text import TextRevisionStore
41
from bzrlib.store.text import TextStore
42
from bzrlib.store.versioned import WeaveStore
43
from bzrlib.symbol_versioning import *
44
from bzrlib.trace import mutter
45
from bzrlib.transactions import WriteTransaction
46
from bzrlib.transport import get_transport, urlunescape
47
from bzrlib.transport.local import LocalTransport
48
from bzrlib.weave import Weave
49
from bzrlib.xml4 import serializer_v4
50
from bzrlib.xml5 import serializer_v5
54
"""A .bzr control diretory.
56
BzrDir instances let you create or open any of the things that can be
57
found within .bzr - checkouts, branches and repositories.
60
the transport which this bzr dir is rooted at (i.e. file:///.../.bzr/)
62
a transport connected to the directory this bzr was opened from.
66
"""Invoke break_lock on the first object in the bzrdir.
68
If there is a tree, the tree is opened and break_lock() called.
69
Otherwise, branch is tried, and finally repository.
72
thing_to_unlock = self.open_workingtree()
73
except (errors.NotLocalUrl, errors.NoWorkingTree):
75
thing_to_unlock = self.open_branch()
76
except errors.NotBranchError:
78
thing_to_unlock = self.open_repository()
79
except errors.NoRepositoryPresent:
81
thing_to_unlock.break_lock()
83
def can_convert_format(self):
84
"""Return true if this bzrdir is one whose format we can convert from."""
88
def _check_supported(format, allow_unsupported):
89
"""Check whether format is a supported format.
91
If allow_unsupported is True, this is a no-op.
93
if not allow_unsupported and not format.is_supported():
94
# see open_downlevel to open legacy branches.
95
raise errors.UnsupportedFormatError(
96
'sorry, format %s not supported' % format,
97
['use a different bzr version',
98
'or remove the .bzr directory'
99
' and "bzr init" again'])
101
def clone(self, url, revision_id=None, basis=None, force_new_repo=False):
102
"""Clone this bzrdir and its contents to url verbatim.
104
If urls last component does not exist, it will be created.
106
if revision_id is not None, then the clone operation may tune
107
itself to download less data.
108
:param force_new_repo: Do not use a shared repository for the target
109
even if one is available.
112
basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
113
result = self._format.initialize(url)
115
local_repo = self.find_repository()
116
except errors.NoRepositoryPresent:
119
# may need to copy content in
121
result_repo = local_repo.clone(
123
revision_id=revision_id,
125
result_repo.set_make_working_trees(local_repo.make_working_trees())
128
result_repo = result.find_repository()
129
# fetch content this dir needs.
131
# XXX FIXME RBC 20060214 need tests for this when the basis
133
result_repo.fetch(basis_repo, revision_id=revision_id)
134
result_repo.fetch(local_repo, revision_id=revision_id)
135
except errors.NoRepositoryPresent:
136
# needed to make one anyway.
137
result_repo = local_repo.clone(
139
revision_id=revision_id,
141
result_repo.set_make_working_trees(local_repo.make_working_trees())
142
# 1 if there is a branch present
143
# make sure its content is available in the target repository
146
self.open_branch().clone(result, revision_id=revision_id)
147
except errors.NotBranchError:
150
self.open_workingtree().clone(result, basis=basis_tree)
151
except (errors.NoWorkingTree, errors.NotLocalUrl):
155
def _get_basis_components(self, basis):
156
"""Retrieve the basis components that are available at basis."""
158
return None, None, None
160
basis_tree = basis.open_workingtree()
161
basis_branch = basis_tree.branch
162
basis_repo = basis_branch.repository
163
except (errors.NoWorkingTree, errors.NotLocalUrl):
166
basis_branch = basis.open_branch()
167
basis_repo = basis_branch.repository
168
except errors.NotBranchError:
171
basis_repo = basis.open_repository()
172
except errors.NoRepositoryPresent:
174
return basis_repo, basis_branch, basis_tree
176
def _make_tail(self, url):
177
segments = url.split('/')
178
if segments and segments[-1] not in ('', '.'):
179
parent = '/'.join(segments[:-1])
180
t = bzrlib.transport.get_transport(parent)
182
t.mkdir(segments[-1])
183
except errors.FileExists:
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
segments = base.split('/')
200
if segments and segments[-1] not in ('', '.'):
201
parent = '/'.join(segments[:-1])
202
t = bzrlib.transport.get_transport(parent)
204
t.mkdir(segments[-1])
205
except errors.FileExists:
207
return BzrDirFormat.get_default_format().initialize(safe_unicode(base))
209
def create_branch(self):
210
"""Create a branch in this BzrDir.
212
The bzrdirs format will control what branch format is created.
213
For more control see BranchFormatXX.create(a_bzrdir).
215
raise NotImplementedError(self.create_branch)
218
def create_branch_and_repo(base, force_new_repo=False):
219
"""Create a new BzrDir, Branch and Repository at the url 'base'.
221
This will use the current default BzrDirFormat, and use whatever
222
repository format that that uses via bzrdir.create_branch and
223
create_repository. If a shared repository is available that is used
226
The created Branch object is returned.
228
:param base: The URL to create the branch at.
229
:param force_new_repo: If True a new repository is always created.
231
bzrdir = BzrDir.create(base)
232
bzrdir._find_or_create_repository(force_new_repo)
233
return bzrdir.create_branch()
235
def _find_or_create_repository(self, force_new_repo):
236
"""Create a new repository if needed, returning the repository."""
238
return self.create_repository()
240
return self.find_repository()
241
except errors.NoRepositoryPresent:
242
return self.create_repository()
245
def create_branch_convenience(base, force_new_repo=False,
246
force_new_tree=None, format=None):
247
"""Create a new BzrDir, Branch and Repository at the url 'base'.
249
This is a convenience function - it will use an existing repository
250
if possible, can be told explicitly whether to create a working tree or
253
This will use the current default BzrDirFormat, and use whatever
254
repository format that that uses via bzrdir.create_branch and
255
create_repository. If a shared repository is available that is used
256
preferentially. Whatever repository is used, its tree creation policy
259
The created Branch object is returned.
260
If a working tree cannot be made due to base not being a file:// url,
261
no error is raised unless force_new_tree is True, in which case no
262
data is created on disk and NotLocalUrl is raised.
264
:param base: The URL to create the branch at.
265
:param force_new_repo: If True a new repository is always created.
266
:param force_new_tree: If True or False force creation of a tree or
267
prevent such creation respectively.
268
:param format: Override for the for the bzrdir format to create
271
# check for non local urls
272
t = get_transport(safe_unicode(base))
273
if not isinstance(t, LocalTransport):
274
raise errors.NotLocalUrl(base)
276
bzrdir = BzrDir.create(base)
278
bzrdir = format.initialize(base)
279
repo = bzrdir._find_or_create_repository(force_new_repo)
280
result = bzrdir.create_branch()
281
if force_new_tree or (repo.make_working_trees() and
282
force_new_tree is None):
284
bzrdir.create_workingtree()
285
except errors.NotLocalUrl:
290
def create_repository(base, shared=False):
291
"""Create a new BzrDir and Repository at the url 'base'.
293
This will use the current default BzrDirFormat, and use whatever
294
repository format that that uses for bzrdirformat.create_repository.
296
;param shared: Create a shared repository rather than a standalone
298
The Repository object is returned.
300
This must be overridden as an instance method in child classes, where
301
it should take no parameters and construct whatever repository format
302
that child class desires.
304
bzrdir = BzrDir.create(base)
305
return bzrdir.create_repository()
308
def create_standalone_workingtree(base):
309
"""Create a new BzrDir, WorkingTree, Branch and Repository at 'base'.
311
'base' must be a local path or a file:// url.
313
This will use the current default BzrDirFormat, and use whatever
314
repository format that that uses for bzrdirformat.create_workingtree,
315
create_branch and create_repository.
317
The WorkingTree object is returned.
319
t = get_transport(safe_unicode(base))
320
if not isinstance(t, LocalTransport):
321
raise errors.NotLocalUrl(base)
322
bzrdir = BzrDir.create_branch_and_repo(safe_unicode(base),
323
force_new_repo=True).bzrdir
324
return bzrdir.create_workingtree()
326
def create_workingtree(self, revision_id=None):
327
"""Create a working tree at this BzrDir.
329
revision_id: create it as of this revision id.
331
raise NotImplementedError(self.create_workingtree)
333
def find_repository(self):
334
"""Find the repository that should be used for a_bzrdir.
336
This does not require a branch as we use it to find the repo for
337
new branches as well as to hook existing branches up to their
341
return self.open_repository()
342
except errors.NoRepositoryPresent:
344
next_transport = self.root_transport.clone('..')
347
found_bzrdir = BzrDir.open_containing_from_transport(
349
except errors.NotBranchError:
350
raise errors.NoRepositoryPresent(self)
352
repository = found_bzrdir.open_repository()
353
except errors.NoRepositoryPresent:
354
next_transport = found_bzrdir.root_transport.clone('..')
356
if ((found_bzrdir.root_transport.base ==
357
self.root_transport.base) or repository.is_shared()):
360
raise errors.NoRepositoryPresent(self)
361
raise errors.NoRepositoryPresent(self)
363
def get_branch_transport(self, branch_format):
364
"""Get the transport for use by branch format in this BzrDir.
366
Note that bzr dirs that do not support format strings will raise
367
IncompatibleFormat if the branch format they are given has
368
a format string, and vice verca.
370
If branch_format is None, the transport is returned with no
371
checking. if it is not None, then the returned transport is
372
guaranteed to point to an existing directory ready for use.
374
raise NotImplementedError(self.get_branch_transport)
376
def get_repository_transport(self, repository_format):
377
"""Get the transport for use by repository format in this BzrDir.
379
Note that bzr dirs that do not support format strings will raise
380
IncompatibleFormat if the repository format they are given has
381
a format string, and vice verca.
383
If repository_format is None, the transport is returned with no
384
checking. if it is not None, then the returned transport is
385
guaranteed to point to an existing directory ready for use.
387
raise NotImplementedError(self.get_repository_transport)
389
def get_workingtree_transport(self, tree_format):
390
"""Get the transport for use by workingtree format in this BzrDir.
392
Note that bzr dirs that do not support format strings will raise
393
IncompatibleFormat if the workingtree format they are given has
394
a format string, and vice verca.
396
If workingtree_format is None, the transport is returned with no
397
checking. if it is not None, then the returned transport is
398
guaranteed to point to an existing directory ready for use.
400
raise NotImplementedError(self.get_workingtree_transport)
402
def __init__(self, _transport, _format):
403
"""Initialize a Bzr control dir object.
405
Only really common logic should reside here, concrete classes should be
406
made with varying behaviours.
408
:param _format: the format that is creating this BzrDir instance.
409
:param _transport: the transport this dir is based at.
411
self._format = _format
412
self.transport = _transport.clone('.bzr')
413
self.root_transport = _transport
415
def needs_format_conversion(self, format=None):
416
"""Return true if this bzrdir needs convert_format run on it.
418
For instance, if the repository format is out of date but the
419
branch and working tree are not, this should return True.
421
:param format: Optional parameter indicating a specific desired
422
format we plan to arrive at.
424
raise NotImplementedError(self.needs_format_conversion)
427
def open_unsupported(base):
428
"""Open a branch which is not supported."""
429
return BzrDir.open(base, _unsupported=True)
432
def open(base, _unsupported=False):
433
"""Open an existing bzrdir, rooted at 'base' (url)
435
_unsupported is a private parameter to the BzrDir class.
437
t = get_transport(base)
438
mutter("trying to open %r with transport %r", base, t)
439
format = BzrDirFormat.find_format(t)
440
BzrDir._check_supported(format, _unsupported)
441
return format.open(t, _found=True)
443
def open_branch(self, unsupported=False):
444
"""Open the branch object at this BzrDir if one is present.
446
If unsupported is True, then no longer supported branch formats can
449
TODO: static convenience version of this?
451
raise NotImplementedError(self.open_branch)
454
def open_containing(url):
455
"""Open an existing branch which contains url.
457
:param url: url to search from.
458
See open_containing_from_transport for more detail.
460
return BzrDir.open_containing_from_transport(get_transport(url))
463
def open_containing_from_transport(a_transport):
464
"""Open an existing branch which contains a_transport.base
466
This probes for a branch at a_transport, and searches upwards from there.
468
Basically we keep looking up until we find the control directory or
469
run into the root. If there isn't one, raises NotBranchError.
470
If there is one and it is either an unrecognised format or an unsupported
471
format, UnknownFormatError or UnsupportedFormatError are raised.
472
If there is one, it is returned, along with the unused portion of url.
474
# this gets the normalised url back. I.e. '.' -> the full path.
475
url = a_transport.base
478
format = BzrDirFormat.find_format(a_transport)
479
BzrDir._check_supported(format, False)
480
return format.open(a_transport), a_transport.relpath(url)
481
except errors.NotBranchError, e:
482
mutter('not a branch in: %r %s', a_transport.base, e)
483
new_t = a_transport.clone('..')
484
if new_t.base == a_transport.base:
485
# reached the root, whatever that may be
486
raise errors.NotBranchError(path=url)
489
def open_repository(self, _unsupported=False):
490
"""Open the repository object at this BzrDir if one is present.
492
This will not follow the Branch object pointer - its strictly a direct
493
open facility. Most client code should use open_branch().repository to
496
_unsupported is a private parameter, not part of the api.
497
TODO: static convenience version of this?
499
raise NotImplementedError(self.open_repository)
501
def open_workingtree(self, _unsupported=False):
502
"""Open the workingtree object at this BzrDir if one is present.
504
TODO: static convenience version of this?
506
raise NotImplementedError(self.open_workingtree)
508
def has_branch(self):
509
"""Tell if this bzrdir contains a branch.
511
Note: if you're going to open the branch, you should just go ahead
512
and try, and not ask permission first. (This method just opens the
513
branch and discards it, and that's somewhat expensive.)
518
except errors.NotBranchError:
521
def has_workingtree(self):
522
"""Tell if this bzrdir contains a working tree.
524
This will still raise an exception if the bzrdir has a workingtree that
525
is remote & inaccessible.
527
Note: if you're going to open the working tree, you should just go ahead
528
and try, and not ask permission first. (This method just opens the
529
workingtree and discards it, and that's somewhat expensive.)
532
self.open_workingtree()
534
except errors.NoWorkingTree:
537
def sprout(self, url, revision_id=None, basis=None, force_new_repo=False):
538
"""Create a copy of this bzrdir prepared for use as a new line of
541
If urls last component does not exist, it will be created.
543
Attributes related to the identity of the source branch like
544
branch nickname will be cleaned, a working tree is created
545
whether one existed before or not; and a local branch is always
548
if revision_id is not None, then the clone operation may tune
549
itself to download less data.
552
result = self._format.initialize(url)
553
basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
555
source_branch = self.open_branch()
556
source_repository = source_branch.repository
557
except errors.NotBranchError:
560
source_repository = self.open_repository()
561
except errors.NoRepositoryPresent:
562
# copy the entire basis one if there is one
563
# but there is no repository.
564
source_repository = basis_repo
569
result_repo = result.find_repository()
570
except errors.NoRepositoryPresent:
572
if source_repository is None and result_repo is not None:
574
elif source_repository is None and result_repo is None:
575
# no repo available, make a new one
576
result.create_repository()
577
elif source_repository is not None and result_repo is None:
578
# have source, and want to make a new target repo
579
# we dont clone the repo because that preserves attributes
580
# like is_shared(), and we have not yet implemented a
581
# repository sprout().
582
result_repo = result.create_repository()
583
if result_repo is not None:
584
# fetch needed content into target.
586
# XXX FIXME RBC 20060214 need tests for this when the basis
588
result_repo.fetch(basis_repo, revision_id=revision_id)
589
result_repo.fetch(source_repository, revision_id=revision_id)
590
if source_branch is not None:
591
source_branch.sprout(result, revision_id=revision_id)
593
result.create_branch()
594
if result_repo is None or result_repo.make_working_trees():
595
result.create_workingtree()
599
class BzrDirPreSplitOut(BzrDir):
600
"""A common class for the all-in-one formats."""
602
def __init__(self, _transport, _format):
603
"""See BzrDir.__init__."""
604
super(BzrDirPreSplitOut, self).__init__(_transport, _format)
605
assert self._format._lock_class == TransportLock
606
assert self._format._lock_file_name == 'branch-lock'
607
self._control_files = LockableFiles(self.get_branch_transport(None),
608
self._format._lock_file_name,
609
self._format._lock_class)
611
def break_lock(self):
612
"""Pre-splitout bzrdirs do not suffer from stale locks."""
613
raise NotImplementedError(self.break_lock)
615
def clone(self, url, revision_id=None, basis=None, force_new_repo=False):
616
"""See BzrDir.clone()."""
617
from bzrlib.workingtree import WorkingTreeFormat2
619
result = self._format._initialize_for_clone(url)
620
basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
621
self.open_repository().clone(result, revision_id=revision_id, basis=basis_repo)
622
from_branch = self.open_branch()
623
from_branch.clone(result, revision_id=revision_id)
625
self.open_workingtree().clone(result, basis=basis_tree)
626
except errors.NotLocalUrl:
627
# make a new one, this format always has to have one.
629
WorkingTreeFormat2().initialize(result)
630
except errors.NotLocalUrl:
631
# but we cannot do it for remote trees.
632
to_branch = result.open_branch()
633
WorkingTreeFormat2().stub_initialize_remote(to_branch.control_files)
636
def create_branch(self):
637
"""See BzrDir.create_branch."""
638
return self.open_branch()
640
def create_repository(self, shared=False):
641
"""See BzrDir.create_repository."""
643
raise errors.IncompatibleFormat('shared repository', self._format)
644
return self.open_repository()
646
def create_workingtree(self, revision_id=None):
647
"""See BzrDir.create_workingtree."""
648
# this looks buggy but is not -really-
649
# clone and sprout will have set the revision_id
650
# and that will have set it for us, its only
651
# specific uses of create_workingtree in isolation
652
# that can do wonky stuff here, and that only
653
# happens for creating checkouts, which cannot be
654
# done on this format anyway. So - acceptable wart.
655
result = self.open_workingtree()
656
if revision_id is not None:
657
result.set_last_revision(revision_id)
660
def get_branch_transport(self, branch_format):
661
"""See BzrDir.get_branch_transport()."""
662
if branch_format is None:
663
return self.transport
665
branch_format.get_format_string()
666
except NotImplementedError:
667
return self.transport
668
raise errors.IncompatibleFormat(branch_format, self._format)
670
def get_repository_transport(self, repository_format):
671
"""See BzrDir.get_repository_transport()."""
672
if repository_format is None:
673
return self.transport
675
repository_format.get_format_string()
676
except NotImplementedError:
677
return self.transport
678
raise errors.IncompatibleFormat(repository_format, self._format)
680
def get_workingtree_transport(self, workingtree_format):
681
"""See BzrDir.get_workingtree_transport()."""
682
if workingtree_format is None:
683
return self.transport
685
workingtree_format.get_format_string()
686
except NotImplementedError:
687
return self.transport
688
raise errors.IncompatibleFormat(workingtree_format, self._format)
690
def needs_format_conversion(self, format=None):
691
"""See BzrDir.needs_format_conversion()."""
692
# if the format is not the same as the system default,
693
# an upgrade is needed.
695
format = BzrDirFormat.get_default_format()
696
return not isinstance(self._format, format.__class__)
698
def open_branch(self, unsupported=False):
699
"""See BzrDir.open_branch."""
700
from bzrlib.branch import BzrBranchFormat4
701
format = BzrBranchFormat4()
702
self._check_supported(format, unsupported)
703
return format.open(self, _found=True)
705
def sprout(self, url, revision_id=None, basis=None):
706
"""See BzrDir.sprout()."""
707
from bzrlib.workingtree import WorkingTreeFormat2
709
result = self._format._initialize_for_clone(url)
710
basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
712
self.open_repository().clone(result, revision_id=revision_id, basis=basis_repo)
713
except errors.NoRepositoryPresent:
716
self.open_branch().sprout(result, revision_id=revision_id)
717
except errors.NotBranchError:
719
# we always want a working tree
720
WorkingTreeFormat2().initialize(result)
724
class BzrDir4(BzrDirPreSplitOut):
725
"""A .bzr version 4 control object.
727
This is a deprecated format and may be removed after sept 2006.
730
def create_repository(self, shared=False):
731
"""See BzrDir.create_repository."""
732
return self._format.repository_format.initialize(self, shared)
734
def needs_format_conversion(self, format=None):
735
"""Format 4 dirs are always in need of conversion."""
738
def open_repository(self):
739
"""See BzrDir.open_repository."""
740
from bzrlib.repository import RepositoryFormat4
741
return RepositoryFormat4().open(self, _found=True)
744
class BzrDir5(BzrDirPreSplitOut):
745
"""A .bzr version 5 control object.
747
This is a deprecated format and may be removed after sept 2006.
750
def open_repository(self):
751
"""See BzrDir.open_repository."""
752
from bzrlib.repository import RepositoryFormat5
753
return RepositoryFormat5().open(self, _found=True)
755
def open_workingtree(self, _unsupported=False):
756
"""See BzrDir.create_workingtree."""
757
from bzrlib.workingtree import WorkingTreeFormat2
758
return WorkingTreeFormat2().open(self, _found=True)
761
class BzrDir6(BzrDirPreSplitOut):
762
"""A .bzr version 6 control object.
764
This is a deprecated format and may be removed after sept 2006.
767
def open_repository(self):
768
"""See BzrDir.open_repository."""
769
from bzrlib.repository import RepositoryFormat6
770
return RepositoryFormat6().open(self, _found=True)
772
def open_workingtree(self, _unsupported=False):
773
"""See BzrDir.create_workingtree."""
774
from bzrlib.workingtree import WorkingTreeFormat2
775
return WorkingTreeFormat2().open(self, _found=True)
778
class BzrDirMeta1(BzrDir):
779
"""A .bzr meta version 1 control object.
781
This is the first control object where the
782
individual aspects are really split out: there are separate repository,
783
workingtree and branch subdirectories and any subset of the three can be
784
present within a BzrDir.
787
def can_convert_format(self):
788
"""See BzrDir.can_convert_format()."""
791
def create_branch(self):
792
"""See BzrDir.create_branch."""
793
from bzrlib.branch import BranchFormat
794
return BranchFormat.get_default_format().initialize(self)
796
def create_repository(self, shared=False):
797
"""See BzrDir.create_repository."""
798
return self._format.repository_format.initialize(self, shared)
800
def create_workingtree(self, revision_id=None):
801
"""See BzrDir.create_workingtree."""
802
from bzrlib.workingtree import WorkingTreeFormat
803
return WorkingTreeFormat.get_default_format().initialize(self, revision_id)
805
def _get_mkdir_mode(self):
806
"""Figure out the mode to use when creating a bzrdir subdir."""
807
temp_control = LockableFiles(self.transport, '', TransportLock)
808
return temp_control._dir_mode
810
def get_branch_transport(self, branch_format):
811
"""See BzrDir.get_branch_transport()."""
812
if branch_format is None:
813
return self.transport.clone('branch')
815
branch_format.get_format_string()
816
except NotImplementedError:
817
raise errors.IncompatibleFormat(branch_format, self._format)
819
self.transport.mkdir('branch', mode=self._get_mkdir_mode())
820
except errors.FileExists:
822
return self.transport.clone('branch')
824
def get_repository_transport(self, repository_format):
825
"""See BzrDir.get_repository_transport()."""
826
if repository_format is None:
827
return self.transport.clone('repository')
829
repository_format.get_format_string()
830
except NotImplementedError:
831
raise errors.IncompatibleFormat(repository_format, self._format)
833
self.transport.mkdir('repository', mode=self._get_mkdir_mode())
834
except errors.FileExists:
836
return self.transport.clone('repository')
838
def get_workingtree_transport(self, workingtree_format):
839
"""See BzrDir.get_workingtree_transport()."""
840
if workingtree_format is None:
841
return self.transport.clone('checkout')
843
workingtree_format.get_format_string()
844
except NotImplementedError:
845
raise errors.IncompatibleFormat(workingtree_format, self._format)
847
self.transport.mkdir('checkout', mode=self._get_mkdir_mode())
848
except errors.FileExists:
850
return self.transport.clone('checkout')
852
def needs_format_conversion(self, format=None):
853
"""See BzrDir.needs_format_conversion()."""
855
format = BzrDirFormat.get_default_format()
856
if not isinstance(self._format, format.__class__):
857
# it is not a meta dir format, conversion is needed.
859
# we might want to push this down to the repository?
861
if not isinstance(self.open_repository()._format,
862
format.repository_format.__class__):
863
# the repository needs an upgrade.
865
except errors.NoRepositoryPresent:
867
# currently there are no other possible conversions for meta1 formats.
870
def open_branch(self, unsupported=False):
871
"""See BzrDir.open_branch."""
872
from bzrlib.branch import BranchFormat
873
format = BranchFormat.find_format(self)
874
self._check_supported(format, unsupported)
875
return format.open(self, _found=True)
877
def open_repository(self, unsupported=False):
878
"""See BzrDir.open_repository."""
879
from bzrlib.repository import RepositoryFormat
880
format = RepositoryFormat.find_format(self)
881
self._check_supported(format, unsupported)
882
return format.open(self, _found=True)
884
def open_workingtree(self, unsupported=False):
885
"""See BzrDir.open_workingtree."""
886
from bzrlib.workingtree import WorkingTreeFormat
887
format = WorkingTreeFormat.find_format(self)
888
self._check_supported(format, unsupported)
889
return format.open(self, _found=True)
892
class BzrDirFormat(object):
893
"""An encapsulation of the initialization and open routines for a format.
895
Formats provide three things:
896
* An initialization routine,
900
Formats are placed in an dict by their format string for reference
901
during bzrdir opening. These should be subclasses of BzrDirFormat
904
Once a format is deprecated, just deprecate the initialize and open
905
methods on the format class. Do not deprecate the object, as the
906
object will be created every system load.
909
_default_format = None
910
"""The default format used for new .bzr dirs."""
913
"""The known formats."""
915
_lock_file_name = 'branch-lock'
917
# _lock_class must be set in subclasses to the lock type, typ.
918
# TransportLock or LockDir
921
def find_format(klass, transport):
922
"""Return the format registered for URL."""
924
format_string = transport.get(".bzr/branch-format").read()
925
return klass._formats[format_string]
926
except errors.NoSuchFile:
927
raise errors.NotBranchError(path=transport.base)
929
raise errors.UnknownFormatError(format_string)
932
def get_default_format(klass):
933
"""Return the current default format."""
934
return klass._default_format
936
def get_format_string(self):
937
"""Return the ASCII format string that identifies this format."""
938
raise NotImplementedError(self.get_format_string)
940
def get_format_description(self):
941
"""Return the short description for this format."""
942
raise NotImplementedError(self.get_format_description)
944
def get_converter(self, format=None):
945
"""Return the converter to use to convert bzrdirs needing converts.
947
This returns a bzrlib.bzrdir.Converter object.
949
This should return the best upgrader to step this format towards the
950
current default format. In the case of plugins we can/shouold provide
951
some means for them to extend the range of returnable converters.
953
:param format: Optional format to override the default foramt of the
956
raise NotImplementedError(self.get_converter)
958
def initialize(self, url):
959
"""Create a bzr control dir at this url and return an opened copy.
961
Subclasses should typically override initialize_on_transport
962
instead of this method.
964
return self.initialize_on_transport(get_transport(url))
966
def initialize_on_transport(self, transport):
967
"""Initialize a new bzrdir in the base directory of a Transport."""
968
# Since we don'transport have a .bzr directory, inherit the
969
# mode from the root directory
970
temp_control = LockableFiles(transport, '', TransportLock)
971
temp_control._transport.mkdir('.bzr',
972
# FIXME: RBC 20060121 dont peek under
974
mode=temp_control._dir_mode)
975
file_mode = temp_control._file_mode
977
mutter('created control directory in ' + transport.base)
978
control = transport.clone('.bzr')
979
utf8_files = [('README',
980
"This is a Bazaar-NG control directory.\n"
981
"Do not change any files in this directory.\n"),
982
('branch-format', self.get_format_string()),
984
# NB: no need to escape relative paths that are url safe.
985
control_files = LockableFiles(control, self._lock_file_name,
987
control_files.create_lock()
988
control_files.lock_write()
990
for file, content in utf8_files:
991
control_files.put_utf8(file, content)
993
control_files.unlock()
994
return self.open(transport, _found=True)
996
def is_supported(self):
997
"""Is this format supported?
999
Supported formats must be initializable and openable.
1000
Unsupported formats may not support initialization or committing or
1001
some other features depending on the reason for not being supported.
1005
def open(self, transport, _found=False):
1006
"""Return an instance of this format for the dir transport points at.
1008
_found is a private parameter, do not use it.
1011
assert isinstance(BzrDirFormat.find_format(transport),
1013
return self._open(transport)
1015
def _open(self, transport):
1016
"""Template method helper for opening BzrDirectories.
1018
This performs the actual open and any additional logic or parameter
1021
raise NotImplementedError(self._open)
1024
def register_format(klass, format):
1025
klass._formats[format.get_format_string()] = format
1028
def set_default_format(klass, format):
1029
klass._default_format = format
1032
return self.get_format_string()[:-1]
1035
def unregister_format(klass, format):
1036
assert klass._formats[format.get_format_string()] is format
1037
del klass._formats[format.get_format_string()]
1040
class BzrDirFormat4(BzrDirFormat):
1041
"""Bzr dir format 4.
1043
This format is a combined format for working tree, branch and repository.
1045
- Format 1 working trees [always]
1046
- Format 4 branches [always]
1047
- Format 4 repositories [always]
1049
This format is deprecated: it indexes texts using a text it which is
1050
removed in format 5; write support for this format has been removed.
1053
_lock_class = TransportLock
1055
def get_format_string(self):
1056
"""See BzrDirFormat.get_format_string()."""
1057
return "Bazaar-NG branch, format 0.0.4\n"
1059
def get_format_description(self):
1060
"""See BzrDirFormat.get_format_description()."""
1061
return "All-in-one format 4"
1063
def get_converter(self, format=None):
1064
"""See BzrDirFormat.get_converter()."""
1065
# there is one and only one upgrade path here.
1066
return ConvertBzrDir4To5()
1068
def initialize_on_transport(self, transport):
1069
"""Format 4 branches cannot be created."""
1070
raise errors.UninitializableFormat(self)
1072
def is_supported(self):
1073
"""Format 4 is not supported.
1075
It is not supported because the model changed from 4 to 5 and the
1076
conversion logic is expensive - so doing it on the fly was not
1081
def _open(self, transport):
1082
"""See BzrDirFormat._open."""
1083
return BzrDir4(transport, self)
1085
def __return_repository_format(self):
1086
"""Circular import protection."""
1087
from bzrlib.repository import RepositoryFormat4
1088
return RepositoryFormat4(self)
1089
repository_format = property(__return_repository_format)
1092
class BzrDirFormat5(BzrDirFormat):
1093
"""Bzr control format 5.
1095
This format is a combined format for working tree, branch and repository.
1097
- Format 2 working trees [always]
1098
- Format 4 branches [always]
1099
- Format 5 repositories [always]
1100
Unhashed stores in the repository.
1103
_lock_class = TransportLock
1105
def get_format_string(self):
1106
"""See BzrDirFormat.get_format_string()."""
1107
return "Bazaar-NG branch, format 5\n"
1109
def get_format_description(self):
1110
"""See BzrDirFormat.get_format_description()."""
1111
return "All-in-one format 5"
1113
def get_converter(self, format=None):
1114
"""See BzrDirFormat.get_converter()."""
1115
# there is one and only one upgrade path here.
1116
return ConvertBzrDir5To6()
1118
def _initialize_for_clone(self, url):
1119
return self.initialize_on_transport(get_transport(url), _cloning=True)
1121
def initialize_on_transport(self, transport, _cloning=False):
1122
"""Format 5 dirs always have working tree, branch and repository.
1124
Except when they are being cloned.
1126
from bzrlib.branch import BzrBranchFormat4
1127
from bzrlib.repository import RepositoryFormat5
1128
from bzrlib.workingtree import WorkingTreeFormat2
1129
result = (super(BzrDirFormat5, self).initialize_on_transport(transport))
1130
RepositoryFormat5().initialize(result, _internal=True)
1132
BzrBranchFormat4().initialize(result)
1133
WorkingTreeFormat2().initialize(result)
1136
def _open(self, transport):
1137
"""See BzrDirFormat._open."""
1138
return BzrDir5(transport, self)
1140
def __return_repository_format(self):
1141
"""Circular import protection."""
1142
from bzrlib.repository import RepositoryFormat5
1143
return RepositoryFormat5(self)
1144
repository_format = property(__return_repository_format)
1147
class BzrDirFormat6(BzrDirFormat):
1148
"""Bzr control format 6.
1150
This format is a combined format for working tree, branch and repository.
1152
- Format 2 working trees [always]
1153
- Format 4 branches [always]
1154
- Format 6 repositories [always]
1157
_lock_class = TransportLock
1159
def get_format_string(self):
1160
"""See BzrDirFormat.get_format_string()."""
1161
return "Bazaar-NG branch, format 6\n"
1163
def get_format_description(self):
1164
"""See BzrDirFormat.get_format_description()."""
1165
return "All-in-one format 6"
1167
def get_converter(self, format=None):
1168
"""See BzrDirFormat.get_converter()."""
1169
# there is one and only one upgrade path here.
1170
return ConvertBzrDir6ToMeta()
1172
def _initialize_for_clone(self, url):
1173
return self.initialize_on_transport(get_transport(url), _cloning=True)
1175
def initialize_on_transport(self, transport, _cloning=False):
1176
"""Format 6 dirs always have working tree, branch and repository.
1178
Except when they are being cloned.
1180
from bzrlib.branch import BzrBranchFormat4
1181
from bzrlib.repository import RepositoryFormat6
1182
from bzrlib.workingtree import WorkingTreeFormat2
1183
result = super(BzrDirFormat6, self).initialize_on_transport(transport)
1184
RepositoryFormat6().initialize(result, _internal=True)
1186
BzrBranchFormat4().initialize(result)
1188
WorkingTreeFormat2().initialize(result)
1189
except errors.NotLocalUrl:
1190
# emulate pre-check behaviour for working tree and silently
1195
def _open(self, transport):
1196
"""See BzrDirFormat._open."""
1197
return BzrDir6(transport, self)
1199
def __return_repository_format(self):
1200
"""Circular import protection."""
1201
from bzrlib.repository import RepositoryFormat6
1202
return RepositoryFormat6(self)
1203
repository_format = property(__return_repository_format)
1206
class BzrDirMetaFormat1(BzrDirFormat):
1207
"""Bzr meta control format 1
1209
This is the first format with split out working tree, branch and repository
1212
- Format 3 working trees [optional]
1213
- Format 5 branches [optional]
1214
- Format 7 repositories [optional]
1217
_lock_class = LockDir
1219
def get_converter(self, format=None):
1220
"""See BzrDirFormat.get_converter()."""
1222
format = BzrDirFormat.get_default_format()
1223
if not isinstance(self, format.__class__):
1224
# converting away from metadir is not implemented
1225
raise NotImplementedError(self.get_converter)
1226
return ConvertMetaToMeta(format)
1228
def get_format_string(self):
1229
"""See BzrDirFormat.get_format_string()."""
1230
return "Bazaar-NG meta directory, format 1\n"
1232
def get_format_description(self):
1233
"""See BzrDirFormat.get_format_description()."""
1234
return "Meta directory format 1"
1236
def _open(self, transport):
1237
"""See BzrDirFormat._open."""
1238
return BzrDirMeta1(transport, self)
1240
def __return_repository_format(self):
1241
"""Circular import protection."""
1242
if getattr(self, '_repository_format', None):
1243
return self._repository_format
1244
from bzrlib.repository import RepositoryFormat
1245
return RepositoryFormat.get_default_format()
1247
def __set_repository_format(self, value):
1248
"""Allow changint the repository format for metadir formats."""
1249
self._repository_format = value
1251
repository_format = property(__return_repository_format, __set_repository_format)
1254
BzrDirFormat.register_format(BzrDirFormat4())
1255
BzrDirFormat.register_format(BzrDirFormat5())
1256
BzrDirFormat.register_format(BzrDirFormat6())
1257
__default_format = BzrDirMetaFormat1()
1258
BzrDirFormat.register_format(__default_format)
1259
BzrDirFormat.set_default_format(__default_format)
1262
class BzrDirTestProviderAdapter(object):
1263
"""A tool to generate a suite testing multiple bzrdir formats at once.
1265
This is done by copying the test once for each transport and injecting
1266
the transport_server, transport_readonly_server, and bzrdir_format
1267
classes into each copy. Each copy is also given a new id() to make it
1271
def __init__(self, transport_server, transport_readonly_server, formats):
1272
self._transport_server = transport_server
1273
self._transport_readonly_server = transport_readonly_server
1274
self._formats = formats
1276
def adapt(self, test):
1277
result = TestSuite()
1278
for format in self._formats:
1279
new_test = deepcopy(test)
1280
new_test.transport_server = self._transport_server
1281
new_test.transport_readonly_server = self._transport_readonly_server
1282
new_test.bzrdir_format = format
1283
def make_new_test_id():
1284
new_id = "%s(%s)" % (new_test.id(), format.__class__.__name__)
1285
return lambda: new_id
1286
new_test.id = make_new_test_id()
1287
result.addTest(new_test)
1291
class ScratchDir(BzrDir6):
1292
"""Special test class: a bzrdir that cleans up itself..
1294
>>> d = ScratchDir()
1295
>>> base = d.transport.base
1298
>>> b.transport.__del__()
1303
def __init__(self, files=[], dirs=[], transport=None):
1304
"""Make a test branch.
1306
This creates a temporary directory and runs init-tree in it.
1308
If any files are listed, they are created in the working copy.
1310
if transport is None:
1311
transport = bzrlib.transport.local.ScratchTransport()
1312
# local import for scope restriction
1313
BzrDirFormat6().initialize(transport.base)
1314
super(ScratchDir, self).__init__(transport, BzrDirFormat6())
1315
self.create_repository()
1316
self.create_branch()
1317
self.create_workingtree()
1319
super(ScratchDir, self).__init__(transport, BzrDirFormat6())
1321
# BzrBranch creates a clone to .bzr and then forgets about the
1322
# original transport. A ScratchTransport() deletes itself and
1323
# everything underneath it when it goes away, so we need to
1324
# grab a local copy to prevent that from happening
1325
self._transport = transport
1328
self._transport.mkdir(d)
1331
self._transport.put(f, 'content of %s' % f)
1335
>>> orig = ScratchDir(files=["file1", "file2"])
1336
>>> os.listdir(orig.base)
1337
[u'.bzr', u'file1', u'file2']
1338
>>> clone = orig.clone()
1339
>>> if os.name != 'nt':
1340
... os.path.samefile(orig.base, clone.base)
1342
... orig.base == clone.base
1345
>>> os.listdir(clone.base)
1346
[u'.bzr', u'file1', u'file2']
1348
from shutil import copytree
1349
from bzrlib.osutils import mkdtemp
1352
copytree(self.base, base, symlinks=True)
1354
transport=bzrlib.transport.local.ScratchTransport(base))
1357
class Converter(object):
1358
"""Converts a disk format object from one format to another."""
1360
def convert(self, to_convert, pb):
1361
"""Perform the conversion of to_convert, giving feedback via pb.
1363
:param to_convert: The disk object to convert.
1364
:param pb: a progress bar to use for progress information.
1367
def step(self, message):
1368
"""Update the pb by a step."""
1370
self.pb.update(message, self.count, self.total)
1373
class ConvertBzrDir4To5(Converter):
1374
"""Converts format 4 bzr dirs to format 5."""
1377
super(ConvertBzrDir4To5, self).__init__()
1378
self.converted_revs = set()
1379
self.absent_revisions = set()
1383
def convert(self, to_convert, pb):
1384
"""See Converter.convert()."""
1385
self.bzrdir = to_convert
1387
self.pb.note('starting upgrade from format 4 to 5')
1388
if isinstance(self.bzrdir.transport, LocalTransport):
1389
self.bzrdir.get_workingtree_transport(None).delete('stat-cache')
1390
self._convert_to_weaves()
1391
return BzrDir.open(self.bzrdir.root_transport.base)
1393
def _convert_to_weaves(self):
1394
self.pb.note('note: upgrade may be faster if all store files are ungzipped first')
1397
stat = self.bzrdir.transport.stat('weaves')
1398
if not S_ISDIR(stat.st_mode):
1399
self.bzrdir.transport.delete('weaves')
1400
self.bzrdir.transport.mkdir('weaves')
1401
except errors.NoSuchFile:
1402
self.bzrdir.transport.mkdir('weaves')
1403
# deliberately not a WeaveFile as we want to build it up slowly.
1404
self.inv_weave = Weave('inventory')
1405
# holds in-memory weaves for all files
1406
self.text_weaves = {}
1407
self.bzrdir.transport.delete('branch-format')
1408
self.branch = self.bzrdir.open_branch()
1409
self._convert_working_inv()
1410
rev_history = self.branch.revision_history()
1411
# to_read is a stack holding the revisions we still need to process;
1412
# appending to it adds new highest-priority revisions
1413
self.known_revisions = set(rev_history)
1414
self.to_read = rev_history[-1:]
1416
rev_id = self.to_read.pop()
1417
if (rev_id not in self.revisions
1418
and rev_id not in self.absent_revisions):
1419
self._load_one_rev(rev_id)
1421
to_import = self._make_order()
1422
for i, rev_id in enumerate(to_import):
1423
self.pb.update('converting revision', i, len(to_import))
1424
self._convert_one_rev(rev_id)
1426
self._write_all_weaves()
1427
self._write_all_revs()
1428
self.pb.note('upgraded to weaves:')
1429
self.pb.note(' %6d revisions and inventories', len(self.revisions))
1430
self.pb.note(' %6d revisions not present', len(self.absent_revisions))
1431
self.pb.note(' %6d texts', self.text_count)
1432
self._cleanup_spare_files_after_format4()
1433
self.branch.control_files.put_utf8('branch-format', BzrDirFormat5().get_format_string())
1435
def _cleanup_spare_files_after_format4(self):
1436
# FIXME working tree upgrade foo.
1437
for n in 'merged-patches', 'pending-merged-patches':
1439
## assert os.path.getsize(p) == 0
1440
self.bzrdir.transport.delete(n)
1441
except errors.NoSuchFile:
1443
self.bzrdir.transport.delete_tree('inventory-store')
1444
self.bzrdir.transport.delete_tree('text-store')
1446
def _convert_working_inv(self):
1447
inv = serializer_v4.read_inventory(self.branch.control_files.get('inventory'))
1448
new_inv_xml = serializer_v5.write_inventory_to_string(inv)
1449
# FIXME inventory is a working tree change.
1450
self.branch.control_files.put('inventory', new_inv_xml)
1452
def _write_all_weaves(self):
1453
controlweaves = WeaveStore(self.bzrdir.transport, prefixed=False)
1454
weave_transport = self.bzrdir.transport.clone('weaves')
1455
weaves = WeaveStore(weave_transport, prefixed=False)
1456
transaction = WriteTransaction()
1460
for file_id, file_weave in self.text_weaves.items():
1461
self.pb.update('writing weave', i, len(self.text_weaves))
1462
weaves._put_weave(file_id, file_weave, transaction)
1464
self.pb.update('inventory', 0, 1)
1465
controlweaves._put_weave('inventory', self.inv_weave, transaction)
1466
self.pb.update('inventory', 1, 1)
1470
def _write_all_revs(self):
1471
"""Write all revisions out in new form."""
1472
self.bzrdir.transport.delete_tree('revision-store')
1473
self.bzrdir.transport.mkdir('revision-store')
1474
revision_transport = self.bzrdir.transport.clone('revision-store')
1476
_revision_store = TextRevisionStore(TextStore(revision_transport,
1480
transaction = bzrlib.transactions.WriteTransaction()
1481
for i, rev_id in enumerate(self.converted_revs):
1482
self.pb.update('write revision', i, len(self.converted_revs))
1483
_revision_store.add_revision(self.revisions[rev_id], transaction)
1487
def _load_one_rev(self, rev_id):
1488
"""Load a revision object into memory.
1490
Any parents not either loaded or abandoned get queued to be
1492
self.pb.update('loading revision',
1493
len(self.revisions),
1494
len(self.known_revisions))
1495
if not self.branch.repository.has_revision(rev_id):
1497
self.pb.note('revision {%s} not present in branch; '
1498
'will be converted as a ghost',
1500
self.absent_revisions.add(rev_id)
1502
rev = self.branch.repository._revision_store.get_revision(rev_id,
1503
self.branch.repository.get_transaction())
1504
for parent_id in rev.parent_ids:
1505
self.known_revisions.add(parent_id)
1506
self.to_read.append(parent_id)
1507
self.revisions[rev_id] = rev
1509
def _load_old_inventory(self, rev_id):
1510
assert rev_id not in self.converted_revs
1511
old_inv_xml = self.branch.repository.inventory_store.get(rev_id).read()
1512
inv = serializer_v4.read_inventory_from_string(old_inv_xml)
1513
rev = self.revisions[rev_id]
1514
if rev.inventory_sha1:
1515
assert rev.inventory_sha1 == sha_string(old_inv_xml), \
1516
'inventory sha mismatch for {%s}' % rev_id
1519
def _load_updated_inventory(self, rev_id):
1520
assert rev_id in self.converted_revs
1521
inv_xml = self.inv_weave.get_text(rev_id)
1522
inv = serializer_v5.read_inventory_from_string(inv_xml)
1525
def _convert_one_rev(self, rev_id):
1526
"""Convert revision and all referenced objects to new format."""
1527
rev = self.revisions[rev_id]
1528
inv = self._load_old_inventory(rev_id)
1529
present_parents = [p for p in rev.parent_ids
1530
if p not in self.absent_revisions]
1531
self._convert_revision_contents(rev, inv, present_parents)
1532
self._store_new_weave(rev, inv, present_parents)
1533
self.converted_revs.add(rev_id)
1535
def _store_new_weave(self, rev, inv, present_parents):
1536
# the XML is now updated with text versions
1540
if ie.kind == 'root_directory':
1542
assert hasattr(ie, 'revision'), \
1543
'no revision on {%s} in {%s}' % \
1544
(file_id, rev.revision_id)
1545
new_inv_xml = serializer_v5.write_inventory_to_string(inv)
1546
new_inv_sha1 = sha_string(new_inv_xml)
1547
self.inv_weave.add_lines(rev.revision_id,
1549
new_inv_xml.splitlines(True))
1550
rev.inventory_sha1 = new_inv_sha1
1552
def _convert_revision_contents(self, rev, inv, present_parents):
1553
"""Convert all the files within a revision.
1555
Also upgrade the inventory to refer to the text revision ids."""
1556
rev_id = rev.revision_id
1557
mutter('converting texts of revision {%s}',
1559
parent_invs = map(self._load_updated_inventory, present_parents)
1562
self._convert_file_version(rev, ie, parent_invs)
1564
def _convert_file_version(self, rev, ie, parent_invs):
1565
"""Convert one version of one file.
1567
The file needs to be added into the weave if it is a merge
1568
of >=2 parents or if it's changed from its parent.
1570
if ie.kind == 'root_directory':
1572
file_id = ie.file_id
1573
rev_id = rev.revision_id
1574
w = self.text_weaves.get(file_id)
1577
self.text_weaves[file_id] = w
1578
text_changed = False
1579
previous_entries = ie.find_previous_heads(parent_invs,
1583
for old_revision in previous_entries:
1584
# if this fails, its a ghost ?
1585
assert old_revision in self.converted_revs
1586
self.snapshot_ie(previous_entries, ie, w, rev_id)
1588
assert getattr(ie, 'revision', None) is not None
1590
def snapshot_ie(self, previous_revisions, ie, w, rev_id):
1591
# TODO: convert this logic, which is ~= snapshot to
1592
# a call to:. This needs the path figured out. rather than a work_tree
1593
# a v4 revision_tree can be given, or something that looks enough like
1594
# one to give the file content to the entry if it needs it.
1595
# and we need something that looks like a weave store for snapshot to
1597
#ie.snapshot(rev, PATH, previous_revisions, REVISION_TREE, InMemoryWeaveStore(self.text_weaves))
1598
if len(previous_revisions) == 1:
1599
previous_ie = previous_revisions.values()[0]
1600
if ie._unchanged(previous_ie):
1601
ie.revision = previous_ie.revision
1604
text = self.branch.repository.text_store.get(ie.text_id)
1605
file_lines = text.readlines()
1606
assert sha_strings(file_lines) == ie.text_sha1
1607
assert sum(map(len, file_lines)) == ie.text_size
1608
w.add_lines(rev_id, previous_revisions, file_lines)
1609
self.text_count += 1
1611
w.add_lines(rev_id, previous_revisions, [])
1612
ie.revision = rev_id
1614
def _make_order(self):
1615
"""Return a suitable order for importing revisions.
1617
The order must be such that an revision is imported after all
1618
its (present) parents.
1620
todo = set(self.revisions.keys())
1621
done = self.absent_revisions.copy()
1624
# scan through looking for a revision whose parents
1626
for rev_id in sorted(list(todo)):
1627
rev = self.revisions[rev_id]
1628
parent_ids = set(rev.parent_ids)
1629
if parent_ids.issubset(done):
1630
# can take this one now
1631
order.append(rev_id)
1637
class ConvertBzrDir5To6(Converter):
1638
"""Converts format 5 bzr dirs to format 6."""
1640
def convert(self, to_convert, pb):
1641
"""See Converter.convert()."""
1642
self.bzrdir = to_convert
1644
self.pb.note('starting upgrade from format 5 to 6')
1645
self._convert_to_prefixed()
1646
return BzrDir.open(self.bzrdir.root_transport.base)
1648
def _convert_to_prefixed(self):
1649
from bzrlib.store import TransportStore
1650
self.bzrdir.transport.delete('branch-format')
1651
for store_name in ["weaves", "revision-store"]:
1652
self.pb.note("adding prefixes to %s" % store_name)
1653
store_transport = self.bzrdir.transport.clone(store_name)
1654
store = TransportStore(store_transport, prefixed=True)
1655
for urlfilename in store_transport.list_dir('.'):
1656
filename = urlunescape(urlfilename)
1657
if (filename.endswith(".weave") or
1658
filename.endswith(".gz") or
1659
filename.endswith(".sig")):
1660
file_id = os.path.splitext(filename)[0]
1663
prefix_dir = store.hash_prefix(file_id)
1664
# FIXME keep track of the dirs made RBC 20060121
1666
store_transport.move(filename, prefix_dir + '/' + filename)
1667
except errors.NoSuchFile: # catches missing dirs strangely enough
1668
store_transport.mkdir(prefix_dir)
1669
store_transport.move(filename, prefix_dir + '/' + filename)
1670
self.bzrdir._control_files.put_utf8('branch-format', BzrDirFormat6().get_format_string())
1673
class ConvertBzrDir6ToMeta(Converter):
1674
"""Converts format 6 bzr dirs to metadirs."""
1676
def convert(self, to_convert, pb):
1677
"""See Converter.convert()."""
1678
self.bzrdir = to_convert
1681
self.total = 20 # the steps we know about
1682
self.garbage_inventories = []
1684
self.pb.note('starting upgrade from format 6 to metadir')
1685
self.bzrdir._control_files.put_utf8('branch-format', "Converting to format 6")
1686
# its faster to move specific files around than to open and use the apis...
1687
# first off, nuke ancestry.weave, it was never used.
1689
self.step('Removing ancestry.weave')
1690
self.bzrdir.transport.delete('ancestry.weave')
1691
except errors.NoSuchFile:
1693
# find out whats there
1694
self.step('Finding branch files')
1695
last_revision = self.bzrdir.open_branch().last_revision()
1696
bzrcontents = self.bzrdir.transport.list_dir('.')
1697
for name in bzrcontents:
1698
if name.startswith('basis-inventory.'):
1699
self.garbage_inventories.append(name)
1700
# create new directories for repository, working tree and branch
1701
self.dir_mode = self.bzrdir._control_files._dir_mode
1702
self.file_mode = self.bzrdir._control_files._file_mode
1703
repository_names = [('inventory.weave', True),
1704
('revision-store', True),
1706
self.step('Upgrading repository ')
1707
self.bzrdir.transport.mkdir('repository', mode=self.dir_mode)
1708
self.make_lock('repository')
1709
# we hard code the formats here because we are converting into
1710
# the meta format. The meta format upgrader can take this to a
1711
# future format within each component.
1712
self.put_format('repository', bzrlib.repository.RepositoryFormat7())
1713
for entry in repository_names:
1714
self.move_entry('repository', entry)
1716
self.step('Upgrading branch ')
1717
self.bzrdir.transport.mkdir('branch', mode=self.dir_mode)
1718
self.make_lock('branch')
1719
self.put_format('branch', bzrlib.branch.BzrBranchFormat5())
1720
branch_files = [('revision-history', True),
1721
('branch-name', True),
1723
for entry in branch_files:
1724
self.move_entry('branch', entry)
1726
self.step('Upgrading working tree')
1727
self.bzrdir.transport.mkdir('checkout', mode=self.dir_mode)
1728
self.make_lock('checkout')
1729
self.put_format('checkout', bzrlib.workingtree.WorkingTreeFormat3())
1730
self.bzrdir.transport.delete_multi(self.garbage_inventories, self.pb)
1731
checkout_files = [('pending-merges', True),
1732
('inventory', True),
1733
('stat-cache', False)]
1734
for entry in checkout_files:
1735
self.move_entry('checkout', entry)
1736
if last_revision is not None:
1737
self.bzrdir._control_files.put_utf8('checkout/last-revision',
1739
self.bzrdir._control_files.put_utf8('branch-format', BzrDirMetaFormat1().get_format_string())
1740
return BzrDir.open(self.bzrdir.root_transport.base)
1742
def make_lock(self, name):
1743
"""Make a lock for the new control dir name."""
1744
self.step('Make %s lock' % name)
1745
ld = LockDir(self.bzrdir.transport,
1747
file_modebits=self.file_mode,
1748
dir_modebits=self.dir_mode)
1751
def move_entry(self, new_dir, entry):
1752
"""Move then entry name into new_dir."""
1754
mandatory = entry[1]
1755
self.step('Moving %s' % name)
1757
self.bzrdir.transport.move(name, '%s/%s' % (new_dir, name))
1758
except errors.NoSuchFile:
1762
def put_format(self, dirname, format):
1763
self.bzrdir._control_files.put_utf8('%s/format' % dirname, format.get_format_string())
1766
class ConvertMetaToMeta(Converter):
1767
"""Converts the components of metadirs."""
1769
def __init__(self, target_format):
1770
"""Create a metadir to metadir converter.
1772
:param target_format: The final metadir format that is desired.
1774
self.target_format = target_format
1776
def convert(self, to_convert, pb):
1777
"""See Converter.convert()."""
1778
self.bzrdir = to_convert
1782
self.step('checking repository format')
1784
repo = self.bzrdir.open_repository()
1785
except errors.NoRepositoryPresent:
1788
if not isinstance(repo._format, self.target_format.repository_format.__class__):
1789
from bzrlib.repository import CopyConverter
1790
self.pb.note('starting repository conversion')
1791
converter = CopyConverter(self.target_format.repository_format)
1792
converter.convert(repo, pb)