83
62
a transport connected to the directory this bzr was opened from.
87
"""Invoke break_lock on the first object in the bzrdir.
89
If there is a tree, the tree is opened and break_lock() called.
90
Otherwise, branch is tried, and finally repository.
92
# XXX: This seems more like a UI function than something that really
93
# belongs in this class.
95
thing_to_unlock = self.open_workingtree()
96
except (errors.NotLocalUrl, errors.NoWorkingTree):
98
thing_to_unlock = self.open_branch()
99
except errors.NotBranchError:
101
thing_to_unlock = self.open_repository()
102
except errors.NoRepositoryPresent:
104
thing_to_unlock.break_lock()
106
65
def can_convert_format(self):
107
66
"""Return true if this bzrdir is one whose format we can convert from."""
110
def check_conversion_target(self, target_format):
111
target_repo_format = target_format.repository_format
112
source_repo_format = self._format.repository_format
113
source_repo_format.check_conversion_target(target_repo_format)
116
def _check_supported(format, allow_unsupported,
117
recommend_upgrade=True,
119
"""Give an error or warning on old formats.
121
:param format: may be any kind of format - workingtree, branch,
124
:param allow_unsupported: If true, allow opening
125
formats that are strongly deprecated, and which may
126
have limited functionality.
128
:param recommend_upgrade: If true (default), warn
129
the user through the ui object that they may wish
130
to upgrade the object.
70
def _check_supported(format, allow_unsupported):
71
"""Check whether format is a supported format.
73
If allow_unsupported is True, this is a no-op.
132
# TODO: perhaps move this into a base Format class; it's not BzrDir
133
# specific. mbp 20070323
134
75
if not allow_unsupported and not format.is_supported():
135
76
# see open_downlevel to open legacy branches.
136
raise errors.UnsupportedFormatError(format=format)
137
if recommend_upgrade \
138
and getattr(format, 'upgrade_recommended', False):
139
ui.ui_factory.recommend_upgrade(
140
format.get_format_description(),
77
raise errors.UnsupportedFormatError(
78
'sorry, format %s not supported' % format,
79
['use a different bzr version',
80
'or remove the .bzr directory'
81
' and "bzr init" again'])
143
def clone(self, url, revision_id=None, force_new_repo=False):
83
def clone(self, url, revision_id=None, basis=None, force_new_repo=False):
144
84
"""Clone this bzrdir and its contents to url verbatim.
146
86
If urls last component does not exist, it will be created.
197
121
except errors.NotBranchError:
200
self.open_workingtree().clone(result)
124
self.open_workingtree().clone(result, basis=basis_tree)
201
125
except (errors.NoWorkingTree, errors.NotLocalUrl):
205
# TODO: This should be given a Transport, and should chdir up; otherwise
206
# this will open a new connection.
129
def _get_basis_components(self, basis):
130
"""Retrieve the basis components that are available at basis."""
132
return None, None, None
134
basis_tree = basis.open_workingtree()
135
basis_branch = basis_tree.branch
136
basis_repo = basis_branch.repository
137
except (errors.NoWorkingTree, errors.NotLocalUrl):
140
basis_branch = basis.open_branch()
141
basis_repo = basis_branch.repository
142
except errors.NotBranchError:
145
basis_repo = basis.open_repository()
146
except errors.NoRepositoryPresent:
148
return basis_repo, basis_branch, basis_tree
207
150
def _make_tail(self, url):
208
t = get_transport(url)
151
segments = url.split('/')
152
if segments and segments[-1] not in ('', '.'):
153
parent = '/'.join(segments[:-1])
154
t = bzrlib.transport.get_transport(parent)
156
t.mkdir(segments[-1])
157
except errors.FileExists:
212
def create(cls, base, format=None, possible_transports=None):
161
def create(cls, base):
213
162
"""Create a new BzrDir at the url 'base'.
215
164
This will call the current default formats initialize with base
216
165
as the only parameter.
218
:param format: If supplied, the format of branch to create. If not
219
supplied, the default is used.
220
:param possible_transports: If supplied, a list of transports that
221
can be reused to share a remote connection.
167
If you need a specific format, consider creating an instance
168
of that and calling initialize().
223
170
if cls is not BzrDir:
224
raise AssertionError("BzrDir.create always creates the default"
225
" format, not one of %r" % cls)
226
t = get_transport(base, possible_transports)
229
format = BzrDirFormat.get_default_format()
230
return format.initialize(base, possible_transports)
171
raise AssertionError("BzrDir.create always creates the default format, "
172
"not one of %r" % cls)
173
segments = base.split('/')
174
if segments and segments[-1] not in ('', '.'):
175
parent = '/'.join(segments[:-1])
176
t = bzrlib.transport.get_transport(parent)
178
t.mkdir(segments[-1])
179
except errors.FileExists:
181
return BzrDirFormat.get_default_format().initialize(safe_unicode(base))
232
183
def create_branch(self):
233
184
"""Create a branch in this BzrDir.
528
404
_unsupported is a private parameter to the BzrDir class.
530
406
t = get_transport(base)
531
return BzrDir.open_from_transport(t, _unsupported=_unsupported)
534
def open_from_transport(transport, _unsupported=False,
535
_server_formats=True):
536
"""Open a bzrdir within a particular directory.
538
:param transport: Transport containing the bzrdir.
539
:param _unsupported: private.
541
base = transport.base
543
def find_format(transport):
544
return transport, BzrDirFormat.find_format(
545
transport, _server_formats=_server_formats)
547
def redirected(transport, e, redirection_notice):
548
qualified_source = e.get_source_url()
549
relpath = transport.relpath(qualified_source)
550
if not e.target.endswith(relpath):
551
# Not redirected to a branch-format, not a branch
552
raise errors.NotBranchError(path=e.target)
553
target = e.target[:-len(relpath)]
554
note('%s is%s redirected to %s',
555
transport.base, e.permanently, target)
556
# Let's try with a new transport
557
qualified_target = e.get_target_url()[:-len(relpath)]
558
# FIXME: If 'transport' has a qualifier, this should
559
# be applied again to the new transport *iff* the
560
# schemes used are the same. It's a bit tricky to
561
# verify, so I'll punt for now
563
return get_transport(target)
566
transport, format = do_catching_redirections(find_format,
569
except errors.TooManyRedirections:
570
raise errors.NotBranchError(base)
407
mutter("trying to open %r with transport %r", base, t)
408
format = BzrDirFormat.find_format(t)
572
409
BzrDir._check_supported(format, _unsupported)
573
return format.open(transport, _found=True)
410
return format.open(t, _found=True)
575
412
def open_branch(self, unsupported=False):
576
413
"""Open the branch object at this BzrDir if one is present.
603
439
If there is one and it is either an unrecognised format or an unsupported
604
440
format, UnknownFormatError or UnsupportedFormatError are raised.
605
441
If there is one, it is returned, along with the unused portion of url.
607
:return: The BzrDir that contains the path, and a Unicode path
608
for the rest of the URL.
610
443
# this gets the normalised url back. I.e. '.' -> the full path.
611
444
url = a_transport.base
614
result = BzrDir.open_from_transport(a_transport)
615
return result, urlutils.unescape(a_transport.relpath(url))
447
format = BzrDirFormat.find_format(a_transport)
448
BzrDir._check_supported(format, False)
449
return format.open(a_transport), a_transport.relpath(url)
616
450
except errors.NotBranchError, e:
619
new_t = a_transport.clone('..')
620
except errors.InvalidURLJoin:
621
# reached the root, whatever that may be
622
raise errors.NotBranchError(path=url)
451
mutter('not a branch in: %r %s', a_transport.base, e)
452
new_t = a_transport.clone('..')
623
453
if new_t.base == a_transport.base:
624
454
# reached the root, whatever that may be
625
455
raise errors.NotBranchError(path=url)
626
456
a_transport = new_t
629
def open_containing_tree_or_branch(klass, location):
630
"""Return the branch and working tree contained by a location.
632
Returns (tree, branch, relpath).
633
If there is no tree at containing the location, tree will be None.
634
If there is no branch containing the location, an exception will be
636
relpath is the portion of the path that is contained by the branch.
638
bzrdir, relpath = klass.open_containing(location)
640
tree = bzrdir.open_workingtree()
641
except (errors.NoWorkingTree, errors.NotLocalUrl):
643
branch = bzrdir.open_branch()
646
return tree, branch, relpath
648
458
def open_repository(self, _unsupported=False):
649
459
"""Open the repository object at this BzrDir if one is present.
658
468
raise NotImplementedError(self.open_repository)
660
def open_workingtree(self, _unsupported=False,
661
recommend_upgrade=True):
470
def open_workingtree(self, _unsupported=False):
662
471
"""Open the workingtree object at this BzrDir if one is present.
664
:param recommend_upgrade: Optional keyword parameter, when True (the
665
default), emit through the ui module a recommendation that the user
666
upgrade the working tree when the workingtree being opened is old
667
(but still fully supported).
473
TODO: static convenience version of this?
669
475
raise NotImplementedError(self.open_workingtree)
671
def has_branch(self):
672
"""Tell if this bzrdir contains a branch.
674
Note: if you're going to open the branch, you should just go ahead
675
and try, and not ask permission first. (This method just opens the
676
branch and discards it, and that's somewhat expensive.)
681
except errors.NotBranchError:
684
def has_workingtree(self):
685
"""Tell if this bzrdir contains a working tree.
687
This will still raise an exception if the bzrdir has a workingtree that
688
is remote & inaccessible.
690
Note: if you're going to open the working tree, you should just go ahead
691
and try, and not ask permission first. (This method just opens the
692
workingtree and discards it, and that's somewhat expensive.)
695
self.open_workingtree(recommend_upgrade=False)
697
except errors.NoWorkingTree:
700
def _cloning_metadir(self):
701
"""Produce a metadir suitable for cloning with"""
702
result_format = self._format.__class__()
705
branch = self.open_branch()
706
source_repository = branch.repository
707
except errors.NotBranchError:
709
source_repository = self.open_repository()
710
except errors.NoRepositoryPresent:
711
source_repository = None
713
# XXX TODO: This isinstance is here because we have not implemented
714
# the fix recommended in bug # 103195 - to delegate this choice the
716
repo_format = source_repository._format
717
if not isinstance(repo_format, remote.RemoteRepositoryFormat):
718
result_format.repository_format = repo_format
720
# TODO: Couldn't we just probe for the format in these cases,
721
# rather than opening the whole tree? It would be a little
722
# faster. mbp 20070401
723
tree = self.open_workingtree(recommend_upgrade=False)
724
except (errors.NoWorkingTree, errors.NotLocalUrl):
725
result_format.workingtree_format = None
727
result_format.workingtree_format = tree._format.__class__()
728
return result_format, source_repository
730
def cloning_metadir(self):
731
"""Produce a metadir suitable for cloning or sprouting with.
733
These operations may produce workingtrees (yes, even though they're
734
"cloning" something that doesn't have a tree, so a viable workingtree
735
format must be selected.
737
format, repository = self._cloning_metadir()
738
if format._workingtree_format is None:
739
if repository is None:
741
tree_format = repository._format._matchingbzrdir.workingtree_format
742
format.workingtree_format = tree_format.__class__()
745
def checkout_metadir(self):
746
return self.cloning_metadir()
748
def sprout(self, url, revision_id=None, force_new_repo=False,
749
recurse='down', possible_transports=None):
477
def sprout(self, url, revision_id=None, basis=None, force_new_repo=False):
750
478
"""Create a copy of this bzrdir prepared for use as a new line of
786
515
# no repo available, make a new one
787
516
result.create_repository()
788
517
elif source_repository is not None and result_repo is None:
789
# have source, and want to make a new target repo
790
result_repo = source_repository.sprout(result,
791
revision_id=revision_id)
518
# have soure, and want to make a new target repo
519
source_repository.clone(result,
520
revision_id=revision_id,
793
523
# fetch needed content into target.
794
if source_repository is not None:
796
# source_repository.copy_content_into(result_repo,
797
# revision_id=revision_id)
798
# so we can override the copy method
799
result_repo.fetch(source_repository, revision_id=revision_id)
525
# XXX FIXME RBC 20060214 need tests for this when the basis
527
result_repo.fetch(basis_repo, revision_id=revision_id)
528
result_repo.fetch(source_repository, revision_id=revision_id)
800
529
if source_branch is not None:
801
530
source_branch.sprout(result, revision_id=revision_id)
803
532
result.create_branch()
804
if isinstance(target_transport, LocalTransport) and (
805
result_repo is None or result_repo.make_working_trees()):
806
wt = result.create_workingtree()
809
if wt.path2id('') is None:
811
wt.set_root_id(self.open_workingtree.get_root_id())
812
except errors.NoWorkingTree:
818
if recurse == 'down':
820
basis = wt.basis_tree()
822
subtrees = basis.iter_references()
823
recurse_branch = wt.branch
824
elif source_branch is not None:
825
basis = source_branch.basis_tree()
827
subtrees = basis.iter_references()
828
recurse_branch = source_branch
833
for path, file_id in subtrees:
834
target = urlutils.join(url, urlutils.escape(path))
835
sublocation = source_branch.reference_parent(file_id, path)
836
sublocation.bzrdir.sprout(target,
837
basis.get_reference_revision(file_id, path),
838
force_new_repo=force_new_repo, recurse=recurse)
840
if basis is not None:
533
result.create_workingtree()
848
540
def __init__(self, _transport, _format):
849
541
"""See BzrDir.__init__."""
850
542
super(BzrDirPreSplitOut, self).__init__(_transport, _format)
851
assert self._format._lock_class == lockable_files.TransportLock
543
assert self._format._lock_class == TransportLock
852
544
assert self._format._lock_file_name == 'branch-lock'
853
self._control_files = lockable_files.LockableFiles(
854
self.get_branch_transport(None),
545
self._control_files = LockableFiles(self.get_branch_transport(None),
855
546
self._format._lock_file_name,
856
547
self._format._lock_class)
858
def break_lock(self):
859
"""Pre-splitout bzrdirs do not suffer from stale locks."""
860
raise NotImplementedError(self.break_lock)
862
def clone(self, url, revision_id=None, force_new_repo=False):
549
def clone(self, url, revision_id=None, basis=None, force_new_repo=False):
863
550
"""See BzrDir.clone()."""
864
551
from bzrlib.workingtree import WorkingTreeFormat2
865
552
self._make_tail(url)
866
result = self._format._initialize_for_clone(url)
867
self.open_repository().clone(result, revision_id=revision_id)
868
from_branch = self.open_branch()
869
from_branch.clone(result, revision_id=revision_id)
553
result = self._format.initialize(url, _cloning=True)
554
basis_repo, basis_branch, basis_tree = self._get_basis_components(basis)
555
self.open_repository().clone(result, revision_id=revision_id, basis=basis_repo)
556
self.open_branch().clone(result, revision_id=revision_id)
871
self.open_workingtree().clone(result)
558
self.open_workingtree().clone(result, basis=basis_tree)
872
559
except errors.NotLocalUrl:
873
560
# make a new one, this format always has to have one.
875
562
WorkingTreeFormat2().initialize(result)
876
563
except errors.NotLocalUrl:
877
# but we cannot do it for remote trees.
878
to_branch = result.open_branch()
879
WorkingTreeFormat2().stub_initialize_remote(to_branch.control_files)
564
# but we canot do it for remote trees.
882
568
def create_branch(self):
1274
864
"""Return the ASCII format string that identifies this format."""
1275
865
raise NotImplementedError(self.get_format_string)
1277
def get_format_description(self):
1278
"""Return the short description for this format."""
1279
raise NotImplementedError(self.get_format_description)
1281
867
def get_converter(self, format=None):
1282
868
"""Return the converter to use to convert bzrdirs needing converts.
1284
870
This returns a bzrlib.bzrdir.Converter object.
1286
872
This should return the best upgrader to step this format towards the
1287
current default format. In the case of plugins we can/should provide
873
current default format. In the case of plugins we can/shouold provide
1288
874
some means for them to extend the range of returnable converters.
1290
:param format: Optional format to override the default format of the
876
:param format: Optional format to override the default foramt of the
1293
879
raise NotImplementedError(self.get_converter)
1295
def initialize(self, url, possible_transports=None):
1296
"""Create a bzr control dir at this url and return an opened copy.
1298
Subclasses should typically override initialize_on_transport
1299
instead of this method.
1301
return self.initialize_on_transport(get_transport(url,
1302
possible_transports))
1304
def initialize_on_transport(self, transport):
1305
"""Initialize a new bzrdir in the base directory of a Transport."""
881
def initialize(self, url):
882
"""Create a bzr control dir at this url and return an opened copy."""
1306
883
# Since we don't have a .bzr directory, inherit the
1307
884
# mode from the root directory
1308
temp_control = lockable_files.LockableFiles(transport,
1309
'', lockable_files.TransportLock)
885
t = get_transport(url)
886
temp_control = LockableFiles(t, '', TransportLock)
1310
887
temp_control._transport.mkdir('.bzr',
1311
# FIXME: RBC 20060121 don't peek under
888
# FIXME: RBC 20060121 dont peek under
1313
890
mode=temp_control._dir_mode)
1314
891
file_mode = temp_control._file_mode
1315
892
del temp_control
1316
mutter('created control directory in ' + transport.base)
1317
control = transport.clone('.bzr')
893
mutter('created control directory in ' + t.base)
894
control = t.clone('.bzr')
1318
895
utf8_files = [('README',
1319
896
"This is a Bazaar-NG control directory.\n"
1320
897
"Do not change any files in this directory.\n"),
1321
898
('branch-format', self.get_format_string()),
1323
900
# NB: no need to escape relative paths that are url safe.
1324
control_files = lockable_files.LockableFiles(control,
1325
self._lock_file_name, self._lock_class)
901
control_files = LockableFiles(control, self._lock_file_name, self._lock_class)
1326
902
control_files.create_lock()
1327
903
control_files.lock_write()
1497
1011
Unhashed stores in the repository.
1500
_lock_class = lockable_files.TransportLock
1014
_lock_class = TransportLock
1502
1016
def get_format_string(self):
1503
1017
"""See BzrDirFormat.get_format_string()."""
1504
1018
return "Bazaar-NG branch, format 5\n"
1506
def get_format_description(self):
1507
"""See BzrDirFormat.get_format_description()."""
1508
return "All-in-one format 5"
1510
1020
def get_converter(self, format=None):
1511
1021
"""See BzrDirFormat.get_converter()."""
1512
1022
# there is one and only one upgrade path here.
1513
1023
return ConvertBzrDir5To6()
1515
def _initialize_for_clone(self, url):
1516
return self.initialize_on_transport(get_transport(url), _cloning=True)
1518
def initialize_on_transport(self, transport, _cloning=False):
1025
def initialize(self, url, _cloning=False):
1519
1026
"""Format 5 dirs always have working tree, branch and repository.
1521
1028
Except when they are being cloned.
1523
1030
from bzrlib.branch import BzrBranchFormat4
1524
from bzrlib.repofmt.weaverepo import RepositoryFormat5
1031
from bzrlib.repository import RepositoryFormat5
1525
1032
from bzrlib.workingtree import WorkingTreeFormat2
1526
result = (super(BzrDirFormat5, self).initialize_on_transport(transport))
1033
result = super(BzrDirFormat5, self).initialize(url)
1527
1034
RepositoryFormat5().initialize(result, _internal=True)
1528
1035
if not _cloning:
1529
branch = BzrBranchFormat4().initialize(result)
1531
WorkingTreeFormat2().initialize(result)
1532
except errors.NotLocalUrl:
1533
# Even though we can't access the working tree, we need to
1534
# create its control files.
1535
WorkingTreeFormat2().stub_initialize_remote(branch.control_files)
1036
BzrBranchFormat4().initialize(result)
1037
WorkingTreeFormat2().initialize(result)
1538
1040
def _open(self, transport):
1556
1058
- Format 6 repositories [always]
1559
_lock_class = lockable_files.TransportLock
1061
_lock_class = TransportLock
1561
1063
def get_format_string(self):
1562
1064
"""See BzrDirFormat.get_format_string()."""
1563
1065
return "Bazaar-NG branch, format 6\n"
1565
def get_format_description(self):
1566
"""See BzrDirFormat.get_format_description()."""
1567
return "All-in-one format 6"
1569
1067
def get_converter(self, format=None):
1570
1068
"""See BzrDirFormat.get_converter()."""
1571
1069
# there is one and only one upgrade path here.
1572
1070
return ConvertBzrDir6ToMeta()
1574
def _initialize_for_clone(self, url):
1575
return self.initialize_on_transport(get_transport(url), _cloning=True)
1577
def initialize_on_transport(self, transport, _cloning=False):
1072
def initialize(self, url, _cloning=False):
1578
1073
"""Format 6 dirs always have working tree, branch and repository.
1580
1075
Except when they are being cloned.
1582
1077
from bzrlib.branch import BzrBranchFormat4
1583
from bzrlib.repofmt.weaverepo import RepositoryFormat6
1078
from bzrlib.repository import RepositoryFormat6
1584
1079
from bzrlib.workingtree import WorkingTreeFormat2
1585
result = super(BzrDirFormat6, self).initialize_on_transport(transport)
1080
result = super(BzrDirFormat6, self).initialize(url)
1586
1081
RepositoryFormat6().initialize(result, _internal=True)
1587
1082
if not _cloning:
1588
branch = BzrBranchFormat4().initialize(result)
1083
BzrBranchFormat4().initialize(result)
1590
1085
WorkingTreeFormat2().initialize(result)
1591
1086
except errors.NotLocalUrl:
1592
# Even though we can't access the working tree, we need to
1593
# create its control files.
1594
WorkingTreeFormat2().stub_initialize_remote(branch.control_files)
1087
# emulate pre-check behaviour for working tree and silently
1597
1092
def _open(self, transport):
1678
1144
repository_format = property(__return_repository_format, __set_repository_format)
1680
def __get_workingtree_format(self):
1681
if self._workingtree_format is None:
1682
from bzrlib.workingtree import WorkingTreeFormat
1683
self._workingtree_format = WorkingTreeFormat.get_default_format()
1684
return self._workingtree_format
1686
def __set_workingtree_format(self, wt_format):
1687
self._workingtree_format = wt_format
1689
workingtree_format = property(__get_workingtree_format,
1690
__set_workingtree_format)
1693
# Register bzr control format
1694
BzrDirFormat.register_control_format(BzrDirFormat)
1696
# Register bzr formats
1697
1147
BzrDirFormat.register_format(BzrDirFormat4())
1698
1148
BzrDirFormat.register_format(BzrDirFormat5())
1699
BzrDirFormat.register_format(BzrDirFormat6())
1700
__default_format = BzrDirMetaFormat1()
1149
BzrDirFormat.register_format(BzrDirMetaFormat1())
1150
__default_format = BzrDirFormat6()
1701
1151
BzrDirFormat.register_format(__default_format)
1702
BzrDirFormat._default_format = __default_format
1152
BzrDirFormat.set_default_format(__default_format)
1155
class BzrDirTestProviderAdapter(object):
1156
"""A tool to generate a suite testing multiple bzrdir formats at once.
1158
This is done by copying the test once for each transport and injecting
1159
the transport_server, transport_readonly_server, and bzrdir_format
1160
classes into each copy. Each copy is also given a new id() to make it
1164
def __init__(self, transport_server, transport_readonly_server, formats):
1165
self._transport_server = transport_server
1166
self._transport_readonly_server = transport_readonly_server
1167
self._formats = formats
1169
def adapt(self, test):
1170
result = TestSuite()
1171
for format in self._formats:
1172
new_test = deepcopy(test)
1173
new_test.transport_server = self._transport_server
1174
new_test.transport_readonly_server = self._transport_readonly_server
1175
new_test.bzrdir_format = format
1176
def make_new_test_id():
1177
new_id = "%s(%s)" % (new_test.id(), format.__class__.__name__)
1178
return lambda: new_id
1179
new_test.id = make_new_test_id()
1180
result.addTest(new_test)
1184
class ScratchDir(BzrDir6):
1185
"""Special test class: a bzrdir that cleans up itself..
1187
>>> d = ScratchDir()
1188
>>> base = d.transport.base
1191
>>> b.transport.__del__()
1196
def __init__(self, files=[], dirs=[], transport=None):
1197
"""Make a test branch.
1199
This creates a temporary directory and runs init-tree in it.
1201
If any files are listed, they are created in the working copy.
1203
if transport is None:
1204
transport = bzrlib.transport.local.ScratchTransport()
1205
# local import for scope restriction
1206
BzrDirFormat6().initialize(transport.base)
1207
super(ScratchDir, self).__init__(transport, BzrDirFormat6())
1208
self.create_repository()
1209
self.create_branch()
1210
self.create_workingtree()
1212
super(ScratchDir, self).__init__(transport, BzrDirFormat6())
1214
# BzrBranch creates a clone to .bzr and then forgets about the
1215
# original transport. A ScratchTransport() deletes itself and
1216
# everything underneath it when it goes away, so we need to
1217
# grab a local copy to prevent that from happening
1218
self._transport = transport
1221
self._transport.mkdir(d)
1224
self._transport.put(f, 'content of %s' % f)
1228
>>> orig = ScratchDir(files=["file1", "file2"])
1229
>>> os.listdir(orig.base)
1230
[u'.bzr', u'file1', u'file2']
1231
>>> clone = orig.clone()
1232
>>> if os.name != 'nt':
1233
... os.path.samefile(orig.base, clone.base)
1235
... orig.base == clone.base
1238
>>> os.listdir(clone.base)
1239
[u'.bzr', u'file1', u'file2']
1241
from shutil import copytree
1242
from bzrlib.osutils import mkdtemp
1245
copytree(self.base, base, symlinks=True)
1247
transport=bzrlib.transport.local.ScratchTransport(base))
1705
1250
class Converter(object):
2060
1601
# we hard code the formats here because we are converting into
2061
1602
# the meta format. The meta format upgrader can take this to a
2062
1603
# future format within each component.
2063
self.put_format('repository', RepositoryFormat7())
1604
self.put_format('repository', bzrlib.repository.RepositoryFormat7())
2064
1605
for entry in repository_names:
2065
1606
self.move_entry('repository', entry)
2067
1608
self.step('Upgrading branch ')
2068
1609
self.bzrdir.transport.mkdir('branch', mode=self.dir_mode)
2069
1610
self.make_lock('branch')
2070
self.put_format('branch', BzrBranchFormat5())
1611
self.put_format('branch', bzrlib.branch.BzrBranchFormat5())
2071
1612
branch_files = [('revision-history', True),
2072
1613
('branch-name', True),
2073
1614
('parent', False)]
2074
1615
for entry in branch_files:
2075
1616
self.move_entry('branch', entry)
1618
self.step('Upgrading working tree')
1619
self.bzrdir.transport.mkdir('checkout', mode=self.dir_mode)
1620
self.make_lock('checkout')
1621
self.put_format('checkout', bzrlib.workingtree.WorkingTreeFormat3())
1622
self.bzrdir.transport.delete_multi(self.garbage_inventories, self.pb)
2077
1623
checkout_files = [('pending-merges', True),
2078
1624
('inventory', True),
2079
1625
('stat-cache', False)]
2080
# If a mandatory checkout file is not present, the branch does not have
2081
# a functional checkout. Do not create a checkout in the converted
2083
for name, mandatory in checkout_files:
2084
if mandatory and name not in bzrcontents:
2085
has_checkout = False
2089
if not has_checkout:
2090
self.pb.note('No working tree.')
2091
# If some checkout files are there, we may as well get rid of them.
2092
for name, mandatory in checkout_files:
2093
if name in bzrcontents:
2094
self.bzrdir.transport.delete(name)
2096
from bzrlib.workingtree import WorkingTreeFormat3
2097
self.step('Upgrading working tree')
2098
self.bzrdir.transport.mkdir('checkout', mode=self.dir_mode)
2099
self.make_lock('checkout')
2101
'checkout', WorkingTreeFormat3())
2102
self.bzrdir.transport.delete_multi(
2103
self.garbage_inventories, self.pb)
2104
for entry in checkout_files:
2105
self.move_entry('checkout', entry)
2106
if last_revision is not None:
2107
self.bzrdir._control_files.put_utf8(
2108
'checkout/last-revision', last_revision)
2109
self.bzrdir._control_files.put_utf8(
2110
'branch-format', BzrDirMetaFormat1().get_format_string())
1626
for entry in checkout_files:
1627
self.move_entry('checkout', entry)
1628
if last_revision is not None:
1629
self.bzrdir._control_files.put_utf8('checkout/last-revision',
1631
self.bzrdir._control_files.put_utf8('branch-format', BzrDirMetaFormat1().get_format_string())
2111
1632
return BzrDir.open(self.bzrdir.root_transport.base)
2113
1634
def make_lock(self, name):
2114
1635
"""Make a lock for the new control dir name."""
2115
1636
self.step('Make %s lock' % name)
2116
ld = lockdir.LockDir(self.bzrdir.transport,
2118
file_modebits=self.file_mode,
2119
dir_modebits=self.dir_mode)
1637
ld = LockDir(self.bzrdir.transport,
1639
file_modebits=self.file_mode,
1640
dir_modebits=self.dir_mode)
2122
1643
def move_entry(self, new_dir, entry):
2161
1682
self.pb.note('starting repository conversion')
2162
1683
converter = CopyConverter(self.target_format.repository_format)
2163
1684
converter.convert(repo, pb)
2165
branch = self.bzrdir.open_branch()
2166
except errors.NotBranchError:
2169
# TODO: conversions of Branch and Tree should be done by
2170
# InterXFormat lookups
2171
# Avoid circular imports
2172
from bzrlib import branch as _mod_branch
2173
if (branch._format.__class__ is _mod_branch.BzrBranchFormat5 and
2174
self.target_format.get_branch_format().__class__ is
2175
_mod_branch.BzrBranchFormat6):
2176
branch_converter = _mod_branch.Converter5to6()
2177
branch_converter.convert(branch)
2179
tree = self.bzrdir.open_workingtree(recommend_upgrade=False)
2180
except (errors.NoWorkingTree, errors.NotLocalUrl):
2183
# TODO: conversions of Branch and Tree should be done by
2184
# InterXFormat lookups
2185
if (isinstance(tree, workingtree.WorkingTree3) and
2186
not isinstance(tree, workingtree_4.WorkingTree4) and
2187
isinstance(self.target_format.workingtree_format,
2188
workingtree_4.WorkingTreeFormat4)):
2189
workingtree_4.Converter3to4().convert(tree)
2190
1685
return to_convert
2193
# This is not in remote.py because it's small, and needs to be registered.
2194
# Putting it in remote.py creates a circular import problem.
2195
# we can make it a lazy object if the control formats is turned into something
2197
class RemoteBzrDirFormat(BzrDirMetaFormat1):
2198
"""Format representing bzrdirs accessed via a smart server"""
2200
def get_format_description(self):
2201
return 'bzr remote bzrdir'
2204
def probe_transport(klass, transport):
2205
"""Return a RemoteBzrDirFormat object if it looks possible."""
2207
client = transport.get_smart_client()
2208
except (NotImplementedError, AttributeError,
2209
errors.TransportNotPossible):
2210
# no smart server, so not a branch for this format type.
2211
raise errors.NotBranchError(path=transport.base)
2213
# Send a 'hello' request in protocol version one, and decline to
2214
# open it if the server doesn't support our required version (2) so
2215
# that the VFS-based transport will do it.
2216
request = client.get_request()
2217
smart_protocol = protocol.SmartClientRequestProtocolOne(request)
2218
server_version = smart_protocol.query_version()
2219
if server_version != 2:
2220
raise errors.NotBranchError(path=transport.base)
2223
def initialize_on_transport(self, transport):
2225
# hand off the request to the smart server
2226
shared_medium = transport.get_shared_medium()
2227
except errors.NoSmartMedium:
2228
# TODO: lookup the local format from a server hint.
2229
local_dir_format = BzrDirMetaFormat1()
2230
return local_dir_format.initialize_on_transport(transport)
2231
client = _SmartClient(shared_medium)
2232
path = client.remote_path_from_transport(transport)
2233
response = _SmartClient(shared_medium).call('BzrDirFormat.initialize',
2235
assert response[0] in ('ok', ), 'unexpected response code %s' % (response,)
2236
return remote.RemoteBzrDir(transport)
2238
def _open(self, transport):
2239
return remote.RemoteBzrDir(transport)
2241
def __eq__(self, other):
2242
if not isinstance(other, RemoteBzrDirFormat):
2244
return self.get_format_description() == other.get_format_description()
2247
BzrDirFormat.register_control_server_format(RemoteBzrDirFormat)
2250
class BzrDirFormatInfo(object):
2252
def __init__(self, native, deprecated, hidden):
2253
self.deprecated = deprecated
2254
self.native = native
2255
self.hidden = hidden
2258
class BzrDirFormatRegistry(registry.Registry):
2259
"""Registry of user-selectable BzrDir subformats.
2261
Differs from BzrDirFormat._control_formats in that it provides sub-formats,
2262
e.g. BzrDirMeta1 with weave repository. Also, it's more user-oriented.
2265
def register_metadir(self, key,
2266
repository_format, help, native=True, deprecated=False,
2270
"""Register a metadir subformat.
2272
These all use a BzrDirMetaFormat1 bzrdir, but can be parameterized
2273
by the Repository format.
2275
:param repository_format: The fully-qualified repository format class
2277
:param branch_format: Fully-qualified branch format class name as
2279
:param tree_format: Fully-qualified tree format class name as
2282
# This should be expanded to support setting WorkingTree and Branch
2283
# formats, once BzrDirMetaFormat1 supports that.
2284
def _load(full_name):
2285
mod_name, factory_name = full_name.rsplit('.', 1)
2287
mod = __import__(mod_name, globals(), locals(),
2289
except ImportError, e:
2290
raise ImportError('failed to load %s: %s' % (full_name, e))
2292
factory = getattr(mod, factory_name)
2293
except AttributeError:
2294
raise AttributeError('no factory %s in module %r'
2299
bd = BzrDirMetaFormat1()
2300
if branch_format is not None:
2301
bd.set_branch_format(_load(branch_format))
2302
if tree_format is not None:
2303
bd.workingtree_format = _load(tree_format)
2304
if repository_format is not None:
2305
bd.repository_format = _load(repository_format)
2307
self.register(key, helper, help, native, deprecated, hidden)
2309
def register(self, key, factory, help, native=True, deprecated=False,
2311
"""Register a BzrDirFormat factory.
2313
The factory must be a callable that takes one parameter: the key.
2314
It must produce an instance of the BzrDirFormat when called.
2316
This function mainly exists to prevent the info object from being
2319
registry.Registry.register(self, key, factory, help,
2320
BzrDirFormatInfo(native, deprecated, hidden))
2322
def register_lazy(self, key, module_name, member_name, help, native=True,
2323
deprecated=False, hidden=False):
2324
registry.Registry.register_lazy(self, key, module_name, member_name,
2325
help, BzrDirFormatInfo(native, deprecated, hidden))
2327
def set_default(self, key):
2328
"""Set the 'default' key to be a clone of the supplied key.
2330
This method must be called once and only once.
2332
registry.Registry.register(self, 'default', self.get(key),
2333
self.get_help(key), info=self.get_info(key))
2335
def set_default_repository(self, key):
2336
"""Set the FormatRegistry default and Repository default.
2338
This is a transitional method while Repository.set_default_format
2341
if 'default' in self:
2342
self.remove('default')
2343
self.set_default(key)
2344
format = self.get('default')()
2345
assert isinstance(format, BzrDirMetaFormat1)
2347
def make_bzrdir(self, key):
2348
return self.get(key)()
2350
def help_topic(self, topic):
2351
output = textwrap.dedent("""\
2352
Bazaar directory formats
2353
------------------------
2355
These formats can be used for creating branches, working trees, and
2359
default_help = self.get_help('default')
2361
for key in self.keys():
2362
if key == 'default':
2364
help = self.get_help(key)
2365
if help == default_help:
2366
default_realkey = key
2368
help_pairs.append((key, help))
2370
def wrapped(key, help, info):
2372
help = '(native) ' + help
2373
return ' %s:\n%s\n\n' % (key,
2374
textwrap.fill(help, initial_indent=' ',
2375
subsequent_indent=' '))
2376
output += wrapped('%s/default' % default_realkey, default_help,
2377
self.get_info('default'))
2378
deprecated_pairs = []
2379
for key, help in help_pairs:
2380
info = self.get_info(key)
2383
elif info.deprecated:
2384
deprecated_pairs.append((key, help))
2386
output += wrapped(key, help, info)
2387
if len(deprecated_pairs) > 0:
2388
output += "Deprecated formats\n------------------\n\n"
2389
for key, help in deprecated_pairs:
2390
info = self.get_info(key)
2391
output += wrapped(key, help, info)
2396
format_registry = BzrDirFormatRegistry()
2397
format_registry.register('weave', BzrDirFormat6,
2398
'Pre-0.8 format. Slower than knit and does not'
2399
' support checkouts or shared repositories.',
2401
format_registry.register_metadir('knit',
2402
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
2403
'Format using knits. Recommended for interoperation with bzr <= 0.14.',
2404
branch_format='bzrlib.branch.BzrBranchFormat5',
2405
tree_format='bzrlib.workingtree.WorkingTreeFormat3')
2406
format_registry.register_metadir('metaweave',
2407
'bzrlib.repofmt.weaverepo.RepositoryFormat7',
2408
'Transitional format in 0.8. Slower than knit.',
2409
branch_format='bzrlib.branch.BzrBranchFormat5',
2410
tree_format='bzrlib.workingtree.WorkingTreeFormat3',
2412
format_registry.register_metadir('dirstate',
2413
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
2414
help='New in 0.15: Fast local operations. Compatible with bzr 0.8 and '
2415
'above when accessed over the network.',
2416
branch_format='bzrlib.branch.BzrBranchFormat5',
2417
# this uses bzrlib.workingtree.WorkingTreeFormat4 because importing
2418
# directly from workingtree_4 triggers a circular import.
2419
tree_format='bzrlib.workingtree.WorkingTreeFormat4',
2421
format_registry.register_metadir('dirstate-tags',
2422
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
2423
help='New in 0.15: Fast local operations and improved scaling for '
2424
'network operations. Additionally adds support for tags.'
2425
' Incompatible with bzr < 0.15.',
2426
branch_format='bzrlib.branch.BzrBranchFormat6',
2427
tree_format='bzrlib.workingtree.WorkingTreeFormat4',
2429
format_registry.register_metadir('dirstate-with-subtree',
2430
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
2431
help='New in 0.15: Fast local operations and improved scaling for '
2432
'network operations. Additionally adds support for versioning nested '
2433
'bzr branches. Incompatible with bzr < 0.15.',
2434
branch_format='bzrlib.branch.BzrBranchFormat6',
2435
tree_format='bzrlib.workingtree.WorkingTreeFormat4',
2438
format_registry.set_default('dirstate')