370
373
if revision_id is not None:
371
374
fetch_spec_factory.add_revision_ids([revision_id])
372
375
fetch_spec_factory.source_branch_stop_revision_id = revision_id
376
if possible_transports is None:
377
possible_transports = []
379
possible_transports = list(possible_transports) + [
373
381
target_transport = _mod_transport.get_transport(url,
374
382
possible_transports)
375
383
target_transport.ensure_base()
376
384
cloning_format = self.cloning_metadir(stacked)
377
385
# Create/update the result branch
378
result = cloning_format.initialize_on_transport(target_transport)
387
result = controldir.ControlDir.open_from_transport(target_transport)
388
except errors.NotBranchError:
389
result = cloning_format.initialize_on_transport(target_transport)
379
390
source_branch, source_repository = self._find_source_repo(
380
391
add_cleanup, source_branch)
381
392
fetch_spec_factory.source_branch = source_branch
771
783
return controldir.ControlDir.create(base, format=format,
772
784
possible_transports=possible_transports)
787
return "<%s at %r>" % (self.__class__.__name__, self.user_url)
789
def update_feature_flags(self, updated_flags):
790
"""Update the features required by this bzrdir.
792
:param updated_flags: Dictionary mapping feature names to necessities
793
A necessity can be None to indicate the feature should be removed
795
self.control_files.lock_write()
797
self._format._update_feature_flags(updated_flags)
798
self.transport.put_bytes('branch-format', self._format.as_string())
800
self.control_files.unlock()
775
803
class BzrDirMeta1(BzrDir):
776
804
"""A .bzr meta version 1 control object.
788
821
def create_branch(self, name=None, repository=None,
789
822
append_revisions_only=None):
790
823
"""See BzrDir.create_branch."""
825
name = self._get_selected_branch()
791
826
return self._format.get_branch_format().initialize(self, name=name,
792
827
repository=repository,
793
828
append_revisions_only=append_revisions_only)
795
830
def destroy_branch(self, name=None):
796
831
"""See BzrDir.create_branch."""
833
name = self._get_selected_branch()
798
835
raise errors.NoColocatedBranchSupport(self)
799
836
self.transport.delete_tree('branch')
844
884
def get_branch_reference(self, name=None):
845
885
"""See BzrDir.get_branch_reference()."""
846
from bzrlib.branch import BranchFormat
847
format = BranchFormat.find_format(self, name=name)
886
from bzrlib.branch import BranchFormatMetadir
887
format = BranchFormatMetadir.find_format(self, name=name)
848
888
return format.get_reference(self, name=name)
890
def set_branch_reference(self, target_branch, name=None):
891
format = _mod_branch.BranchReferenceFormat()
892
return format.initialize(self, target_branch=target_branch, name=name)
850
894
def get_branch_transport(self, branch_format, name=None):
851
895
"""See BzrDir.get_branch_transport()."""
897
name = self._get_selected_branch()
853
899
raise errors.NoColocatedBranchSupport(self)
854
900
# XXX: this shouldn't implicitly create the directory if it's just
855
901
# promising to get a transport -- mbp 20090727
938
984
def open_branch(self, name=None, unsupported=False,
939
ignore_fallbacks=False):
940
"""See BzrDir.open_branch."""
985
ignore_fallbacks=False, possible_transports=None):
986
"""See ControlDir.open_branch."""
988
name = self._get_selected_branch()
941
989
format = self.find_branch_format(name=name)
942
990
format.check_support_status(unsupported)
943
991
return format.open(self, name=name,
944
_found=True, ignore_fallbacks=ignore_fallbacks)
992
_found=True, ignore_fallbacks=ignore_fallbacks,
993
possible_transports=possible_transports)
946
995
def open_repository(self, unsupported=False):
947
996
"""See BzrDir.open_repository."""
948
from bzrlib.repository import RepositoryFormat
949
format = RepositoryFormat.find_format(self)
997
from bzrlib.repository import RepositoryFormatMetaDir
998
format = RepositoryFormatMetaDir.find_format(self)
950
999
format.check_support_status(unsupported)
951
1000
return format.open(self, _found=True)
953
1002
def open_workingtree(self, unsupported=False,
954
1003
recommend_upgrade=True):
955
1004
"""See BzrDir.open_workingtree."""
956
from bzrlib.workingtree import WorkingTreeFormat
957
format = WorkingTreeFormat.find_format(self)
1005
from bzrlib.workingtree import WorkingTreeFormatMetaDir
1006
format = WorkingTreeFormatMetaDir.find_format(self)
958
1007
format.check_support_status(unsupported, recommend_upgrade,
959
1008
basedir=self.root_transport.base)
960
1009
return format.open(self, _found=True)
1020
1062
def destroy_branch(self, name=None):
1021
1063
"""See BzrDir.create_branch."""
1022
path, name = self._get_branch_path(name)
1023
if name is not None:
1065
name = self._get_selected_branch()
1066
path = self._get_branch_path(name)
1024
1068
self.control_files.lock_write()
1026
1070
branches = self._read_branch_list()
1028
branches.remove(name)
1072
branches.remove(name.encode("utf-8"))
1029
1073
except ValueError:
1030
1074
raise errors.NotBranchError(name)
1031
self._write_branch_list(name)
1075
self._write_branch_list(branches)
1033
1077
self.control_files.unlock()
1034
1078
self.transport.delete_tree(path)
1036
def list_branches(self):
1037
"""See ControlDir.list_branches."""
1080
def get_branches(self):
1081
"""See ControlDir.get_branches."""
1041
ret.append(self.open_branch())
1084
ret[""] = self.open_branch(name="")
1042
1085
except (errors.NotBranchError, errors.NoRepositoryPresent):
1045
# colocated branches
1046
ret.extend([self.open_branch(name) for name in
1047
self._read_branch_list()])
1088
for name in self._read_branch_list():
1089
ret[name] = self.open_branch(name=name.decode('utf-8'))
1051
1093
def get_branch_transport(self, branch_format, name=None):
1052
1094
"""See BzrDir.get_branch_transport()."""
1053
path, name = self._get_branch_path(name)
1096
name = self._get_selected_branch()
1097
path = self._get_branch_path(name)
1054
1098
# XXX: this shouldn't implicitly create the directory if it's just
1055
1099
# promising to get a transport -- mbp 20090727
1056
1100
if branch_format is None:
1059
1103
branch_format.get_format_string()
1060
1104
except NotImplementedError:
1061
1105
raise errors.IncompatibleFormat(branch_format, self._format)
1062
if name is not None:
1064
1108
self.transport.mkdir('branches', mode=self._get_mkdir_mode())
1065
1109
except errors.FileExists:
1067
1111
branches = self._read_branch_list()
1068
if not name in branches:
1112
utf8_name = name.encode("utf-8")
1113
if not utf8_name in branches:
1069
1114
self.control_files.lock_write()
1071
1116
branches = self._read_branch_list()
1072
branches.append(name)
1117
branches.append(utf8_name)
1073
1118
self._write_branch_list(branches)
1075
1120
self.control_files.unlock()
1080
1125
return self.transport.clone(path)
1128
class BzrFormat(object):
1129
"""Base class for all formats of things living in metadirs.
1131
This class manages the format string that is stored in the 'format'
1132
or 'branch-format' file.
1134
All classes for (branch-, repository-, workingtree-) formats that
1135
live in meta directories and have their own 'format' file
1136
(i.e. different from .bzr/branch-format) derive from this class,
1137
as well as the relevant base class for their kind
1138
(BranchFormat, WorkingTreeFormat, RepositoryFormat).
1140
Each format is identified by a "format" or "branch-format" file with a
1141
single line containing the base format name and then an optional list of
1144
Feature flags are supported as of bzr 2.5. Setting feature flags on formats
1145
will render them inaccessible to older versions of bzr.
1147
:ivar features: Dictionary mapping feature names to their necessity
1150
_present_features = set()
1156
def register_feature(cls, name):
1157
"""Register a feature as being present.
1159
:param name: Name of the feature
1162
raise ValueError("spaces are not allowed in feature names")
1163
if name in cls._present_features:
1164
raise errors.FeatureAlreadyRegistered(name)
1165
cls._present_features.add(name)
1168
def unregister_feature(cls, name):
1169
"""Unregister a feature."""
1170
cls._present_features.remove(name)
1172
def check_support_status(self, allow_unsupported, recommend_upgrade=True,
1174
for name, necessity in self.features.iteritems():
1175
if name in self._present_features:
1177
if necessity == "optional":
1178
mutter("ignoring optional missing feature %s", name)
1180
elif necessity == "required":
1181
raise errors.MissingFeature(name)
1183
mutter("treating unknown necessity as require for %s",
1185
raise errors.MissingFeature(name)
1188
def get_format_string(cls):
1189
"""Return the ASCII format string that identifies this format."""
1190
raise NotImplementedError(cls.get_format_string)
1193
def from_string(cls, text):
1194
format_string = cls.get_format_string()
1195
if not text.startswith(format_string):
1196
raise AssertionError("Invalid format header %r for %r" % (text, cls))
1197
lines = text[len(format_string):].splitlines()
1199
for lineno, line in enumerate(lines):
1201
(necessity, feature) = line.split(" ", 1)
1203
raise errors.ParseFormatError(format=cls, lineno=lineno+2,
1204
line=line, text=text)
1205
ret.features[feature] = necessity
1208
def as_string(self):
1209
"""Return the string representation of this format.
1211
lines = [self.get_format_string()]
1212
lines.extend([("%s %s\n" % (item[1], item[0])) for item in
1213
self.features.iteritems()])
1214
return "".join(lines)
1217
def _find_format(klass, registry, kind, format_string):
1219
first_line = format_string[:format_string.index("\n")+1]
1221
first_line = format_string
1223
cls = registry.get(first_line)
1225
raise errors.UnknownFormatError(format=first_line, kind=kind)
1226
return cls.from_string(format_string)
1228
def network_name(self):
1229
"""A simple byte string uniquely identifying this format for RPC calls.
1231
Metadir branch formats use their format string.
1233
return self.as_string()
1235
def __eq__(self, other):
1236
return (self.__class__ is other.__class__ and
1237
self.features == other.features)
1239
def _update_feature_flags(self, updated_flags):
1240
"""Update the feature flags in this format.
1242
:param updated_flags: Updated feature flags
1244
for name, necessity in updated_flags.iteritems():
1245
if necessity is None:
1247
del self.features[name]
1251
self.features[name] = necessity
1083
1254
class BzrProber(controldir.Prober):
1084
1255
"""Prober for formats that use a .bzr/ control directory."""
1304
1475
# mode from the root directory
1305
1476
temp_control = lockable_files.LockableFiles(transport,
1306
1477
'', lockable_files.TransportLock)
1307
temp_control._transport.mkdir('.bzr',
1308
# FIXME: RBC 20060121 don't peek under
1310
mode=temp_control._dir_mode)
1479
temp_control._transport.mkdir('.bzr',
1480
# FIXME: RBC 20060121 don't peek under
1482
mode=temp_control._dir_mode)
1483
except errors.FileExists:
1484
raise errors.AlreadyControlDirError(transport.base)
1311
1485
if sys.platform == 'win32' and isinstance(transport, local.LocalTransport):
1312
1486
win32utils.set_file_attr_hidden(transport._abspath('.bzr'))
1313
1487
file_mode = temp_control._file_mode
1367
1541
compatible with whatever sub formats are supported by self.
1544
other_format.features = dict(self.features)
1371
1546
def supports_transport(self, transport):
1372
1547
# bzr formats can be opened over all known transports
1550
def check_support_status(self, allow_unsupported, recommend_upgrade=True,
1552
controldir.ControlDirFormat.check_support_status(self,
1553
allow_unsupported=allow_unsupported, recommend_upgrade=recommend_upgrade,
1555
BzrFormat.check_support_status(self, allow_unsupported=allow_unsupported,
1556
recommend_upgrade=recommend_upgrade, basedir=basedir)
1376
1559
class BzrDirMetaFormat1(BzrDirFormat):
1377
1560
"""Bzr meta control format 1
1862
2050
require_stacking)
1863
2051
self._bzrdir = bzrdir
1865
def acquire_repository(self, make_working_trees=None, shared=False):
2053
def acquire_repository(self, make_working_trees=None, shared=False,
2054
possible_transports=None):
1866
2055
"""Implementation of RepositoryAcquisitionPolicy.acquire_repository
1868
2057
Creates the desired repository in the bzrdir we already have.
2059
if possible_transports is None:
2060
possible_transports = []
2062
possible_transports = list(possible_transports)
2063
possible_transports.append(self._bzrdir.root_transport)
1870
2064
stack_on = self._get_full_stack_on()
1872
2066
format = self._bzrdir._format
1873
2067
format.require_stacking(stack_on=stack_on,
1874
possible_transports=[self._bzrdir.root_transport])
2068
possible_transports=possible_transports)
1875
2069
if not self._require_stacking:
1876
2070
# We have picked up automatic stacking somewhere.
1877
2071
note(gettext('Using default stacking branch {0} at {1}').format(
1878
2072
self._stack_on, self._stack_on_pwd))
1879
2073
repository = self._bzrdir.create_repository(shared=shared)
1880
2074
self._add_fallback(repository,
1881
possible_transports=[self._bzrdir.transport])
2075
possible_transports=possible_transports)
1882
2076
if make_working_trees is not None:
1883
2077
repository.set_make_working_trees(make_working_trees)
1884
2078
return repository, True
1900
2094
require_stacking)
1901
2095
self._repository = repository
1903
def acquire_repository(self, make_working_trees=None, shared=False):
2097
def acquire_repository(self, make_working_trees=None, shared=False,
2098
possible_transports=None):
1904
2099
"""Implementation of RepositoryAcquisitionPolicy.acquire_repository
1906
2101
Returns an existing repository to use.
2103
if possible_transports is None:
2104
possible_transports = []
2106
possible_transports = list(possible_transports)
2107
possible_transports.append(self._repository.bzrdir.transport)
1908
2108
self._add_fallback(self._repository,
1909
possible_transports=[self._repository.bzrdir.transport])
2109
possible_transports=possible_transports)
1910
2110
return self._repository, False
1965
2165
deprecated=True)
1966
2166
register_metadir(controldir.format_registry, 'dirstate',
1967
2167
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
1968
help='New in 0.15: Fast local operations. Compatible with bzr 0.8 and '
1969
'above when accessed over the network.',
2168
help='Format using dirstate for working trees. '
2169
'Compatible with bzr 0.8 and '
2170
'above when accessed over the network. Introduced in bzr 0.15.',
1970
2171
branch_format='bzrlib.branch.BzrBranchFormat5',
1971
2172
tree_format='bzrlib.workingtree_4.WorkingTreeFormat4',
1973
2174
deprecated=True)
1974
2175
register_metadir(controldir.format_registry, 'dirstate-tags',
1975
2176
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit1',
1976
help='New in 0.15: Fast local operations and improved scaling for '
1977
'network operations. Additionally adds support for tags.'
1978
' Incompatible with bzr < 0.15.',
2177
help='Variant of dirstate with support for tags. '
2178
'Introduced in bzr 0.15.',
1979
2179
branch_format='bzrlib.branch.BzrBranchFormat6',
1980
2180
tree_format='bzrlib.workingtree_4.WorkingTreeFormat4',
1982
2182
deprecated=True)
1983
2183
register_metadir(controldir.format_registry, 'rich-root',
1984
2184
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit4',
1985
help='New in 1.0. Better handling of tree roots. Incompatible with'
2185
help='Variant of dirstate with better handling of tree roots. '
2186
'Introduced in bzr 1.0',
1987
2187
branch_format='bzrlib.branch.BzrBranchFormat6',
1988
2188
tree_format='bzrlib.workingtree_4.WorkingTreeFormat4',
1990
2190
deprecated=True)
1991
2191
register_metadir(controldir.format_registry, 'dirstate-with-subtree',
1992
2192
'bzrlib.repofmt.knitrepo.RepositoryFormatKnit3',
1993
help='New in 0.15: Fast local operations and improved scaling for '
1994
'network operations. Additionally adds support for versioning nested '
1995
'bzr branches. Incompatible with bzr < 0.15.',
2193
help='Variant of dirstate with support for nested trees. '
2194
'Introduced in 0.15.',
1996
2195
branch_format='bzrlib.branch.BzrBranchFormat6',
1997
2196
tree_format='bzrlib.workingtree_4.WorkingTreeFormat4',
1998
2197
experimental=True,
2001
2200
register_metadir(controldir.format_registry, 'pack-0.92',
2002
2201
'bzrlib.repofmt.knitpack_repo.RepositoryFormatKnitPack1',
2003
help='New in 0.92: Pack-based format with data compatible with '
2004
'dirstate-tags format repositories. Interoperates with '
2005
'bzr repositories before 0.92 but cannot be read by bzr < 0.92. '
2202
help='Pack-based format used in 1.x series. Introduced in 0.92. '
2203
'Interoperates with bzr repositories before 0.92 but cannot be '
2204
'read by bzr < 0.92. '
2007
2206
branch_format='bzrlib.branch.BzrBranchFormat6',
2008
2207
tree_format='bzrlib.workingtree_4.WorkingTreeFormat4',
2010
2210
register_metadir(controldir.format_registry, 'pack-0.92-subtree',
2011
2211
'bzrlib.repofmt.knitpack_repo.RepositoryFormatKnitPack3',
2012
help='New in 0.92: Pack-based format with data compatible with '
2013
'dirstate-with-subtree format repositories. Interoperates with '
2212
help='Pack-based format used in 1.x series, with subtree support. '
2213
'Introduced in 0.92. Interoperates with '
2014
2214
'bzr repositories before 0.92 but cannot be read by bzr < 0.92. '
2016
2216
branch_format='bzrlib.branch.BzrBranchFormat6',
2017
2217
tree_format='bzrlib.workingtree_4.WorkingTreeFormat4',
2019
2220
experimental=True,
2021
2222
register_metadir(controldir.format_registry, 'rich-root-pack',
2022
2223
'bzrlib.repofmt.knitpack_repo.RepositoryFormatKnitPack4',
2023
help='New in 1.0: A variant of pack-0.92 that supports rich-root data '
2024
'(needed for bzr-svn and bzr-git).',
2224
help='A variant of pack-0.92 that supports rich-root data '
2225
'(needed for bzr-svn and bzr-git). Introduced in 1.0.',
2025
2226
branch_format='bzrlib.branch.BzrBranchFormat6',
2026
2227
tree_format='bzrlib.workingtree_4.WorkingTreeFormat4',
2029
2231
register_metadir(controldir.format_registry, '1.6',
2030
2232
'bzrlib.repofmt.knitpack_repo.RepositoryFormatKnitPack5',
2059
2264
branch_format='bzrlib.branch.BzrBranchFormat7',
2060
2265
tree_format='bzrlib.workingtree_4.WorkingTreeFormat4',
2063
2269
register_metadir(controldir.format_registry, '1.14',
2064
2270
'bzrlib.repofmt.knitpack_repo.RepositoryFormatKnitPack6',
2065
2271
help='A working-tree format that supports content filtering.',
2066
2272
branch_format='bzrlib.branch.BzrBranchFormat7',
2067
2273
tree_format='bzrlib.workingtree_4.WorkingTreeFormat5',
2069
2277
register_metadir(controldir.format_registry, '1.14-rich-root',
2070
2278
'bzrlib.repofmt.knitpack_repo.RepositoryFormatKnitPack6RichRoot',