91
# Note that RemoteBzrDirProber lives in bzrlib.bzrdir so bzrlib.remote
92
# does not have to be imported unless a remote format is involved.
94
class RemoteBzrDirFormat(_mod_bzrdir.BzrDirMetaFormat1):
95
"""Format representing bzrdirs accessed via a smart server"""
97
supports_workingtrees = False
100
_mod_bzrdir.BzrDirMetaFormat1.__init__(self)
101
# XXX: It's a bit ugly that the network name is here, because we'd
102
# like to believe that format objects are stateless or at least
103
# immutable, However, we do at least avoid mutating the name after
104
# it's returned. See <https://bugs.launchpad.net/bzr/+bug/504102>
105
self._network_name = None
108
return "%s(_network_name=%r)" % (self.__class__.__name__,
111
def get_format_description(self):
112
if self._network_name:
113
real_format = controldir.network_format_registry.get(self._network_name)
114
return 'Remote: ' + real_format.get_format_description()
115
return 'bzr remote bzrdir'
117
def get_format_string(self):
118
raise NotImplementedError(self.get_format_string)
120
def network_name(self):
121
if self._network_name:
122
return self._network_name
124
raise AssertionError("No network name set.")
126
def initialize_on_transport(self, transport):
128
# hand off the request to the smart server
129
client_medium = transport.get_smart_medium()
130
except errors.NoSmartMedium:
131
# TODO: lookup the local format from a server hint.
132
local_dir_format = _mod_bzrdir.BzrDirMetaFormat1()
133
return local_dir_format.initialize_on_transport(transport)
134
client = _SmartClient(client_medium)
135
path = client.remote_path_from_transport(transport)
137
response = client.call('BzrDirFormat.initialize', path)
138
except errors.ErrorFromSmartServer, err:
139
_translate_error(err, path=path)
140
if response[0] != 'ok':
141
raise errors.SmartProtocolError('unexpected response code %s' % (response,))
142
format = RemoteBzrDirFormat()
143
self._supply_sub_formats_to(format)
144
return RemoteBzrDir(transport, format)
146
def parse_NoneTrueFalse(self, arg):
153
raise AssertionError("invalid arg %r" % arg)
155
def _serialize_NoneTrueFalse(self, arg):
162
def _serialize_NoneString(self, arg):
165
def initialize_on_transport_ex(self, transport, use_existing_dir=False,
166
create_prefix=False, force_new_repo=False, stacked_on=None,
167
stack_on_pwd=None, repo_format_name=None, make_working_trees=None,
170
# hand off the request to the smart server
171
client_medium = transport.get_smart_medium()
172
except errors.NoSmartMedium:
175
# Decline to open it if the server doesn't support our required
176
# version (3) so that the VFS-based transport will do it.
177
if client_medium.should_probe():
179
server_version = client_medium.protocol_version()
180
if server_version != '2':
184
except errors.SmartProtocolError:
185
# Apparently there's no usable smart server there, even though
186
# the medium supports the smart protocol.
191
client = _SmartClient(client_medium)
192
path = client.remote_path_from_transport(transport)
193
if client_medium._is_remote_before((1, 16)):
196
# TODO: lookup the local format from a server hint.
197
local_dir_format = _mod_bzrdir.BzrDirMetaFormat1()
198
self._supply_sub_formats_to(local_dir_format)
199
return local_dir_format.initialize_on_transport_ex(transport,
200
use_existing_dir=use_existing_dir, create_prefix=create_prefix,
201
force_new_repo=force_new_repo, stacked_on=stacked_on,
202
stack_on_pwd=stack_on_pwd, repo_format_name=repo_format_name,
203
make_working_trees=make_working_trees, shared_repo=shared_repo,
205
return self._initialize_on_transport_ex_rpc(client, path, transport,
206
use_existing_dir, create_prefix, force_new_repo, stacked_on,
207
stack_on_pwd, repo_format_name, make_working_trees, shared_repo)
209
def _initialize_on_transport_ex_rpc(self, client, path, transport,
210
use_existing_dir, create_prefix, force_new_repo, stacked_on,
211
stack_on_pwd, repo_format_name, make_working_trees, shared_repo):
213
args.append(self._serialize_NoneTrueFalse(use_existing_dir))
214
args.append(self._serialize_NoneTrueFalse(create_prefix))
215
args.append(self._serialize_NoneTrueFalse(force_new_repo))
216
args.append(self._serialize_NoneString(stacked_on))
217
# stack_on_pwd is often/usually our transport
220
stack_on_pwd = transport.relpath(stack_on_pwd)
223
except errors.PathNotChild:
225
args.append(self._serialize_NoneString(stack_on_pwd))
226
args.append(self._serialize_NoneString(repo_format_name))
227
args.append(self._serialize_NoneTrueFalse(make_working_trees))
228
args.append(self._serialize_NoneTrueFalse(shared_repo))
229
request_network_name = self._network_name or \
230
_mod_bzrdir.BzrDirFormat.get_default_format().network_name()
232
response = client.call('BzrDirFormat.initialize_ex_1.16',
233
request_network_name, path, *args)
234
except errors.UnknownSmartMethod:
235
client._medium._remember_remote_is_before((1,16))
236
local_dir_format = _mod_bzrdir.BzrDirMetaFormat1()
237
self._supply_sub_formats_to(local_dir_format)
238
return local_dir_format.initialize_on_transport_ex(transport,
239
use_existing_dir=use_existing_dir, create_prefix=create_prefix,
240
force_new_repo=force_new_repo, stacked_on=stacked_on,
241
stack_on_pwd=stack_on_pwd, repo_format_name=repo_format_name,
242
make_working_trees=make_working_trees, shared_repo=shared_repo,
244
except errors.ErrorFromSmartServer, err:
245
_translate_error(err, path=path)
246
repo_path = response[0]
247
bzrdir_name = response[6]
248
require_stacking = response[7]
249
require_stacking = self.parse_NoneTrueFalse(require_stacking)
250
format = RemoteBzrDirFormat()
251
format._network_name = bzrdir_name
252
self._supply_sub_formats_to(format)
253
bzrdir = RemoteBzrDir(transport, format, _client=client)
255
repo_format = response_tuple_to_repo_format(response[1:])
259
repo_bzrdir_format = RemoteBzrDirFormat()
260
repo_bzrdir_format._network_name = response[5]
261
repo_bzr = RemoteBzrDir(transport.clone(repo_path),
265
final_stack = response[8] or None
266
final_stack_pwd = response[9] or None
268
final_stack_pwd = urlutils.join(
269
transport.base, final_stack_pwd)
270
remote_repo = RemoteRepository(repo_bzr, repo_format)
271
if len(response) > 10:
272
# Updated server verb that locks remotely.
273
repo_lock_token = response[10] or None
274
remote_repo.lock_write(repo_lock_token, _skip_rpc=True)
276
remote_repo.dont_leave_lock_in_place()
278
remote_repo.lock_write()
279
policy = _mod_bzrdir.UseExistingRepository(remote_repo, final_stack,
280
final_stack_pwd, require_stacking)
281
policy.acquire_repository()
285
bzrdir._format.set_branch_format(self.get_branch_format())
287
# The repo has already been created, but we need to make sure that
288
# we'll make a stackable branch.
289
bzrdir._format.require_stacking(_skip_repo=True)
290
return remote_repo, bzrdir, require_stacking, policy
292
def _open(self, transport):
293
return RemoteBzrDir(transport, self)
295
def __eq__(self, other):
296
if not isinstance(other, RemoteBzrDirFormat):
298
return self.get_format_description() == other.get_format_description()
300
def __return_repository_format(self):
301
# Always return a RemoteRepositoryFormat object, but if a specific bzr
302
# repository format has been asked for, tell the RemoteRepositoryFormat
303
# that it should use that for init() etc.
304
result = RemoteRepositoryFormat()
305
custom_format = getattr(self, '_repository_format', None)
307
if isinstance(custom_format, RemoteRepositoryFormat):
310
# We will use the custom format to create repositories over the
311
# wire; expose its details like rich_root_data for code to
313
result._custom_format = custom_format
316
def get_branch_format(self):
317
result = _mod_bzrdir.BzrDirMetaFormat1.get_branch_format(self)
318
if not isinstance(result, RemoteBranchFormat):
319
new_result = RemoteBranchFormat()
320
new_result._custom_format = result
322
self.set_branch_format(new_result)
326
repository_format = property(__return_repository_format,
327
_mod_bzrdir.BzrDirMetaFormat1._set_repository_format) #.im_func)
330
class RemoteBzrDir(_mod_bzrdir.BzrDir, _RpcHelper):
84
# Note: RemoteBzrDirFormat is in bzrdir.py
86
class RemoteBzrDir(BzrDir, _RpcHelper):
331
87
"""Control directory on a remote server, accessed via bzr:// or similar."""
333
def __init__(self, transport, format, _client=None, _force_probe=False):
89
def __init__(self, transport, format, _client=None):
334
90
"""Construct a RemoteBzrDir.
336
92
:param _client: Private parameter for testing. Disables probing and the
337
93
use of a real bzrdir.
339
_mod_bzrdir.BzrDir.__init__(self, transport, format)
95
BzrDir.__init__(self, transport, format)
340
96
# this object holds a delegated bzrdir that uses file-level operations
341
97
# to talk to the other side
342
98
self._real_bzrdir = None
343
self._has_working_tree = None
344
99
# 1-shot cache for the call pattern 'create_branch; open_branch' - see
345
100
# create_branch for details.
346
101
self._next_open_branch_result = None
537
246
def _get_branch_reference(self):
538
247
path = self._path_for_remote_call(self._client)
539
248
medium = self._client._medium
541
('BzrDir.open_branchV3', (2, 1)),
542
('BzrDir.open_branchV2', (1, 13)),
543
('BzrDir.open_branch', None),
545
for verb, required_version in candidate_calls:
546
if required_version and medium._is_remote_before(required_version):
249
if not medium._is_remote_before((1, 13)):
549
response = self._call(verb, path)
251
response = self._call('BzrDir.open_branchV2', path)
252
if response[0] not in ('ref', 'branch'):
253
raise errors.UnexpectedSmartServerResponse(response)
550
255
except errors.UnknownSmartMethod:
551
if required_version is None:
553
medium._remember_remote_is_before(required_version)
556
if verb == 'BzrDir.open_branch':
557
if response[0] != 'ok':
558
raise errors.UnexpectedSmartServerResponse(response)
559
if response[1] != '':
560
return ('ref', response[1])
562
return ('branch', '')
563
if response[0] not in ('ref', 'branch'):
256
medium._remember_remote_is_before((1, 13))
257
response = self._call('BzrDir.open_branch', path)
258
if response[0] != 'ok':
564
259
raise errors.UnexpectedSmartServerResponse(response)
260
if response[1] != '':
261
return ('ref', response[1])
263
return ('branch', '')
567
def _get_tree_branch(self, name=None):
265
def _get_tree_branch(self):
568
266
"""See BzrDir._get_tree_branch()."""
569
return None, self.open_branch(name=name)
267
return None, self.open_branch()
571
def open_branch(self, name=None, unsupported=False,
572
ignore_fallbacks=False):
269
def open_branch(self, _unsupported=False, ignore_fallbacks=False):
574
271
raise NotImplementedError('unsupported flag support not implemented yet.')
575
272
if self._next_open_branch_result is not None:
576
273
# See create_branch for details.
1133
745
"""Return a source for streaming from this repository."""
1134
746
return RemoteStreamSource(self, to_format)
1137
def get_file_graph(self):
1138
return graph.Graph(self.texts)
1141
748
def has_revision(self, revision_id):
1142
"""True if this repository has a copy of the revision."""
1143
# Copy of bzrlib.repository.Repository.has_revision
1144
return revision_id in self.has_revisions((revision_id,))
749
"""See Repository.has_revision()."""
750
if revision_id == NULL_REVISION:
751
# The null revision is always present.
753
path = self.bzrdir._path_for_remote_call(self._client)
754
response = self._call('Repository.has_revision', path, revision_id)
755
if response[0] not in ('yes', 'no'):
756
raise errors.UnexpectedSmartServerResponse(response)
757
if response[0] == 'yes':
759
for fallback_repo in self._fallback_repositories:
760
if fallback_repo.has_revision(revision_id):
1147
764
def has_revisions(self, revision_ids):
1148
"""Probe to find out the presence of multiple revisions.
1150
:param revision_ids: An iterable of revision_ids.
1151
:return: A set of the revision_ids that were present.
1153
# Copy of bzrlib.repository.Repository.has_revisions
1154
parent_map = self.get_parent_map(revision_ids)
1155
result = set(parent_map)
1156
if _mod_revision.NULL_REVISION in revision_ids:
1157
result.add(_mod_revision.NULL_REVISION)
765
"""See Repository.has_revisions()."""
766
# FIXME: This does many roundtrips, particularly when there are
767
# fallback repositories. -- mbp 20080905
769
for revision_id in revision_ids:
770
if self.has_revision(revision_id):
771
result.add(revision_id)
1160
def _has_same_fallbacks(self, other_repo):
1161
"""Returns true if the repositories have the same fallbacks."""
1162
# XXX: copied from Repository; it should be unified into a base class
1163
# <https://bugs.launchpad.net/bzr/+bug/401622>
1164
my_fb = self._fallback_repositories
1165
other_fb = other_repo._fallback_repositories
1166
if len(my_fb) != len(other_fb):
1168
for f, g in zip(my_fb, other_fb):
1169
if not f.has_same_location(g):
1173
774
def has_same_location(self, other):
1174
# TODO: Move to RepositoryBase and unify with the regular Repository
1175
# one; unfortunately the tests rely on slightly different behaviour at
1176
# present -- mbp 20090710
1177
775
return (self.__class__ is other.__class__ and
1178
776
self.bzrdir.transport.base == other.bzrdir.transport.base)
1506
1076
# We need to accumulate additional repositories here, to pass them in
1507
1077
# on various RPC's.
1509
if self.is_locked():
1510
# We will call fallback.unlock() when we transition to the unlocked
1511
# state, so always add a lock here. If a caller passes us a locked
1512
# repository, they are responsible for unlocking it later.
1513
repository.lock_read()
1514
self._check_fallback_repository(repository)
1515
1079
self._fallback_repositories.append(repository)
1516
1080
# If self._real_repository was parameterised already (e.g. because a
1517
1081
# _real_branch had its get_stacked_on_url method called), then the
1518
1082
# repository to be added may already be in the _real_repositories list.
1519
1083
if self._real_repository is not None:
1520
fallback_locations = [repo.user_url for repo in
1084
fallback_locations = [repo.bzrdir.root_transport.base for repo in
1521
1085
self._real_repository._fallback_repositories]
1522
if repository.user_url not in fallback_locations:
1086
if repository.bzrdir.root_transport.base not in fallback_locations:
1523
1087
self._real_repository.add_fallback_repository(repository)
1525
def _check_fallback_repository(self, repository):
1526
"""Check that this repository can fallback to repository safely.
1528
Raise an error if not.
1530
:param repository: A repository to fallback to.
1532
return _mod_repository.InterRepository._assert_same_model(
1535
1089
def add_inventory(self, revid, inv, parents):
1536
1090
self._ensure_real()
1537
1091
return self._real_repository.add_inventory(revid, inv, parents)
1539
1093
def add_inventory_by_delta(self, basis_revision_id, delta, new_revision_id,
1540
parents, basis_inv=None, propagate_caches=False):
1541
1095
self._ensure_real()
1542
1096
return self._real_repository.add_inventory_by_delta(basis_revision_id,
1543
delta, new_revision_id, parents, basis_inv=basis_inv,
1544
propagate_caches=propagate_caches)
1097
delta, new_revision_id, parents)
1546
1099
def add_revision(self, rev_id, rev, inv=None, config=None):
1547
1100
self._ensure_real()
2060
1606
def insert_stream(self, stream, src_format, resume_tokens):
2061
1607
target = self.target_repo
2062
target._unstacked_provider.missing_keys.clear()
2063
candidate_calls = [('Repository.insert_stream_1.19', (1, 19))]
2064
1608
if target._lock_token:
2065
candidate_calls.append(('Repository.insert_stream_locked', (1, 14)))
2066
lock_args = (target._lock_token or '',)
1609
verb = 'Repository.insert_stream_locked'
1610
extra_args = (target._lock_token or '',)
1611
required_version = (1, 14)
2068
candidate_calls.append(('Repository.insert_stream', (1, 13)))
1613
verb = 'Repository.insert_stream'
1615
required_version = (1, 13)
2070
1616
client = target._client
2071
1617
medium = client._medium
1618
if medium._is_remote_before(required_version):
1619
# No possible way this can work.
1620
return self._insert_real(stream, src_format, resume_tokens)
2072
1621
path = target.bzrdir._path_for_remote_call(client)
2073
# Probe for the verb to use with an empty stream before sending the
2074
# real stream to it. We do this both to avoid the risk of sending a
2075
# large request that is then rejected, and because we don't want to
2076
# implement a way to buffer, rewind, or restart the stream.
2078
for verb, required_version in candidate_calls:
2079
if medium._is_remote_before(required_version):
2082
# We've already done the probing (and set _is_remote_before) on
2083
# a previous insert.
1622
if not resume_tokens:
1623
# XXX: Ugly but important for correctness, *will* be fixed during
1624
# 1.13 cycle. Pushing a stream that is interrupted results in a
1625
# fallback to the _real_repositories sink *with a partial stream*.
1626
# Thats bad because we insert less data than bzr expected. To avoid
1627
# this we do a trial push to make sure the verb is accessible, and
1628
# do not fallback when actually pushing the stream. A cleanup patch
1629
# is going to look at rewinding/restarting the stream/partial
2086
1631
byte_stream = smart_repo._stream_to_byte_stream([], src_format)
2088
1633
response = client.call_with_body_stream(
2089
(verb, path, '') + lock_args, byte_stream)
1634
(verb, path, '') + extra_args, byte_stream)
2090
1635
except errors.UnknownSmartMethod:
2091
1636
medium._remember_remote_is_before(required_version)
2097
return self._insert_real(stream, src_format, resume_tokens)
2098
self._last_inv_record = None
2099
self._last_substream = None
2100
if required_version < (1, 19):
2101
# Remote side doesn't support inventory deltas. Wrap the stream to
2102
# make sure we don't send any. If the stream contains inventory
2103
# deltas we'll interrupt the smart insert_stream request and
2105
stream = self._stop_stream_if_inventory_delta(stream)
1637
return self._insert_real(stream, src_format, resume_tokens)
2106
1638
byte_stream = smart_repo._stream_to_byte_stream(
2107
1639
stream, src_format)
2108
1640
resume_tokens = ' '.join(resume_tokens)
2109
1641
response = client.call_with_body_stream(
2110
(verb, path, resume_tokens) + lock_args, byte_stream)
1642
(verb, path, resume_tokens) + extra_args, byte_stream)
2111
1643
if response[0][0] not in ('ok', 'missing-basis'):
2112
1644
raise errors.UnexpectedSmartServerResponse(response)
2113
if self._last_substream is not None:
2114
# The stream included an inventory-delta record, but the remote
2115
# side isn't new enough to support them. So we need to send the
2116
# rest of the stream via VFS.
2117
self.target_repo.refresh_data()
2118
return self._resume_stream_with_vfs(response, src_format)
2119
1645
if response[0][0] == 'missing-basis':
2120
1646
tokens, missing_keys = bencode.bdecode_as_tuple(response[0][1])
2121
1647
resume_tokens = tokens
2122
return resume_tokens, set(missing_keys)
1648
return resume_tokens, missing_keys
2124
1650
self.target_repo.refresh_data()
2125
1651
return [], set()
2127
def _resume_stream_with_vfs(self, response, src_format):
2128
"""Resume sending a stream via VFS, first resending the record and
2129
substream that couldn't be sent via an insert_stream verb.
2131
if response[0][0] == 'missing-basis':
2132
tokens, missing_keys = bencode.bdecode_as_tuple(response[0][1])
2133
# Ignore missing_keys, we haven't finished inserting yet
2136
def resume_substream():
2137
# Yield the substream that was interrupted.
2138
for record in self._last_substream:
2140
self._last_substream = None
2141
def resume_stream():
2142
# Finish sending the interrupted substream
2143
yield ('inventory-deltas', resume_substream())
2144
# Then simply continue sending the rest of the stream.
2145
for substream_kind, substream in self._last_stream:
2146
yield substream_kind, substream
2147
return self._insert_real(resume_stream(), src_format, tokens)
2149
def _stop_stream_if_inventory_delta(self, stream):
2150
"""Normally this just lets the original stream pass-through unchanged.
2152
However if any 'inventory-deltas' substream occurs it will stop
2153
streaming, and store the interrupted substream and stream in
2154
self._last_substream and self._last_stream so that the stream can be
2155
resumed by _resume_stream_with_vfs.
2158
stream_iter = iter(stream)
2159
for substream_kind, substream in stream_iter:
2160
if substream_kind == 'inventory-deltas':
2161
self._last_substream = substream
2162
self._last_stream = stream_iter
2165
yield substream_kind, substream
2168
class RemoteStreamSource(vf_repository.StreamSource):
1654
class RemoteStreamSource(repository.StreamSource):
2169
1655
"""Stream data from a remote server."""
2171
1657
def get_stream(self, search):
2172
1658
if (self.from_repository._fallback_repositories and
2173
1659
self.to_format._fetch_order == 'topological'):
2174
1660
return self._real_stream(self.from_repository, search)
2177
repos = [self.from_repository]
2183
repos.extend(repo._fallback_repositories)
2184
sources.append(repo)
2185
return self.missing_parents_chain(search, sources)
2187
def get_stream_for_missing_keys(self, missing_keys):
2188
self.from_repository._ensure_real()
2189
real_repo = self.from_repository._real_repository
2190
real_source = real_repo._get_source(self.to_format)
2191
return real_source.get_stream_for_missing_keys(missing_keys)
1661
return self.missing_parents_chain(search, [self.from_repository] +
1662
self.from_repository._fallback_repositories)
2193
1664
def _real_stream(self, repo, search):
2194
1665
"""Get a stream for search from repo.
2225
1695
return self._real_stream(repo, search)
2226
1696
client = repo._client
2227
1697
medium = client._medium
1698
if medium._is_remote_before((1, 13)):
1699
# streaming was added in 1.13
1700
return self._real_stream(repo, search)
2228
1701
path = repo.bzrdir._path_for_remote_call(client)
2229
search_bytes = repo._serialise_search_result(search)
2230
args = (path, self.to_format.network_name())
2232
('Repository.get_stream_1.19', (1, 19)),
2233
('Repository.get_stream', (1, 13))]
2236
for verb, version in candidate_verbs:
2237
if medium._is_remote_before(version):
2240
response = repo._call_with_body_bytes_expecting_body(
2241
verb, args, search_bytes)
2242
except errors.UnknownSmartMethod:
2243
medium._remember_remote_is_before(version)
2244
except errors.UnknownErrorFromSmartServer, e:
2245
if isinstance(search, graph.EverythingResult):
2246
error_verb = e.error_from_smart_server.error_verb
2247
if error_verb == 'BadSearch':
2248
# Pre-2.4 servers don't support this sort of search.
2249
# XXX: perhaps falling back to VFS on BadSearch is a
2250
# good idea in general? It might provide a little bit
2251
# of protection against client-side bugs.
2252
medium._remember_remote_is_before((2, 4))
2256
response_tuple, response_handler = response
1703
search_bytes = repo._serialise_search_result(search)
1704
response = repo._call_with_body_bytes_expecting_body(
1705
'Repository.get_stream',
1706
(path, self.to_format.network_name()), search_bytes)
1707
response_tuple, response_handler = response
1708
except errors.UnknownSmartMethod:
1709
medium._remember_remote_is_before((1,13))
2260
1710
return self._real_stream(repo, search)
2261
1711
if response_tuple[0] != 'ok':
2262
1712
raise errors.UnexpectedSmartServerResponse(response_tuple)
2263
1713
byte_stream = response_handler.read_streamed_body()
2264
src_format, stream = smart_repo._byte_stream_to_stream(byte_stream,
2265
self._record_counter)
1714
src_format, stream = smart_repo._byte_stream_to_stream(byte_stream)
2266
1715
if src_format.network_name() != repo._format.network_name():
2267
1716
raise AssertionError(
2268
1717
"Mismatched RemoteRepository and stream src %r, %r" % (
2344
1792
self._network_name)
2346
1794
def get_format_description(self):
2348
return 'Remote: ' + self._custom_format.get_format_description()
1795
return 'Remote BZR Branch'
2350
1797
def network_name(self):
2351
1798
return self._network_name
2353
def open(self, a_bzrdir, name=None, ignore_fallbacks=False):
2354
return a_bzrdir.open_branch(name=name,
2355
ignore_fallbacks=ignore_fallbacks)
1800
def open(self, a_bzrdir, ignore_fallbacks=False):
1801
return a_bzrdir.open_branch(ignore_fallbacks=ignore_fallbacks)
2357
def _vfs_initialize(self, a_bzrdir, name):
1803
def _vfs_initialize(self, a_bzrdir):
2358
1804
# Initialisation when using a local bzrdir object, or a non-vfs init
2359
1805
# method is not available on the server.
2360
1806
# self._custom_format is always set - the start of initialize ensures
2362
1808
if isinstance(a_bzrdir, RemoteBzrDir):
2363
1809
a_bzrdir._ensure_real()
2364
result = self._custom_format.initialize(a_bzrdir._real_bzrdir,
1810
result = self._custom_format.initialize(a_bzrdir._real_bzrdir)
2367
1812
# We assume the bzrdir is parameterised; it may not be.
2368
result = self._custom_format.initialize(a_bzrdir, name)
1813
result = self._custom_format.initialize(a_bzrdir)
2369
1814
if (isinstance(a_bzrdir, RemoteBzrDir) and
2370
1815
not isinstance(result, RemoteBranch)):
2371
result = RemoteBranch(a_bzrdir, a_bzrdir.find_repository(), result,
1816
result = RemoteBranch(a_bzrdir, a_bzrdir.find_repository(), result)
2375
def initialize(self, a_bzrdir, name=None, repository=None):
1819
def initialize(self, a_bzrdir):
2376
1820
# 1) get the network name to use.
2377
1821
if self._custom_format:
2378
1822
network_name = self._custom_format.network_name()
2380
1824
# Select the current bzrlib default and ask for that.
2381
reference_bzrdir_format = _mod_bzrdir.format_registry.get('default')()
1825
reference_bzrdir_format = bzrdir.format_registry.get('default')()
2382
1826
reference_format = reference_bzrdir_format.get_branch_format()
2383
1827
self._custom_format = reference_format
2384
1828
network_name = reference_format.network_name()
2385
1829
# Being asked to create on a non RemoteBzrDir:
2386
1830
if not isinstance(a_bzrdir, RemoteBzrDir):
2387
return self._vfs_initialize(a_bzrdir, name=name)
1831
return self._vfs_initialize(a_bzrdir)
2388
1832
medium = a_bzrdir._client._medium
2389
1833
if medium._is_remote_before((1, 13)):
2390
return self._vfs_initialize(a_bzrdir, name=name)
1834
return self._vfs_initialize(a_bzrdir)
2391
1835
# Creating on a remote bzr dir.
2392
1836
# 2) try direct creation via RPC
2393
1837
path = a_bzrdir._path_for_remote_call(a_bzrdir._client)
2394
if name is not None:
2395
# XXX JRV20100304: Support creating colocated branches
2396
raise errors.NoColocatedBranchSupport(self)
2397
1838
verb = 'BzrDir.create_branch'
2399
1840
response = a_bzrdir._call(verb, path, network_name)
2400
1841
except errors.UnknownSmartMethod:
2401
1842
# Fallback - use vfs methods
2402
1843
medium._remember_remote_is_before((1, 13))
2403
return self._vfs_initialize(a_bzrdir, name=name)
1844
return self._vfs_initialize(a_bzrdir)
2404
1845
if response[0] != 'ok':
2405
1846
raise errors.UnexpectedSmartServerResponse(response)
2406
1847
# Turn the response into a RemoteRepository object.
2407
1848
format = RemoteBranchFormat(network_name=response[1])
2408
1849
repo_format = response_tuple_to_repo_format(response[3:])
2409
repo_path = response[2]
2410
if repository is not None:
2411
remote_repo_url = urlutils.join(a_bzrdir.user_url, repo_path)
2412
url_diff = urlutils.relative_url(repository.user_url,
2415
raise AssertionError(
2416
'repository.user_url %r does not match URL from server '
2417
'response (%r + %r)'
2418
% (repository.user_url, a_bzrdir.user_url, repo_path))
2419
remote_repo = repository
1850
if response[2] == '':
1851
repo_bzrdir = a_bzrdir
2422
repo_bzrdir = a_bzrdir
2424
repo_bzrdir = RemoteBzrDir(
2425
a_bzrdir.root_transport.clone(repo_path), a_bzrdir._format,
2427
remote_repo = RemoteRepository(repo_bzrdir, repo_format)
1853
repo_bzrdir = RemoteBzrDir(
1854
a_bzrdir.root_transport.clone(response[2]), a_bzrdir._format,
1856
remote_repo = RemoteRepository(repo_bzrdir, repo_format)
2428
1857
remote_branch = RemoteBranch(a_bzrdir, remote_repo,
2429
format=format, setup_stacking=False, name=name)
1858
format=format, setup_stacking=False)
2430
1859
# XXX: We know this is a new branch, so it must have revno 0, revid
2431
1860
# NULL_REVISION. Creating the branch locked would make this be unable
2432
1861
# to be wrong; here its simply very unlikely to be wrong. RBC 20090225
2690
2084
return self._vfs_get_tags_bytes()
2691
2085
return response[0]
2693
def _vfs_set_tags_bytes(self, bytes):
2695
return self._real_branch._set_tags_bytes(bytes)
2697
def _set_tags_bytes(self, bytes):
2698
if self.is_locked():
2699
self._tags_bytes = bytes
2700
medium = self._client._medium
2701
if medium._is_remote_before((1, 18)):
2702
self._vfs_set_tags_bytes(bytes)
2706
self._remote_path(), self._lock_token, self._repo_lock_token)
2707
response = self._call_with_body_bytes(
2708
'Branch.set_tags_bytes', args, bytes)
2709
except errors.UnknownSmartMethod:
2710
medium._remember_remote_is_before((1, 18))
2711
self._vfs_set_tags_bytes(bytes)
2713
2087
def lock_read(self):
2714
"""Lock the branch for read operations.
2716
:return: A bzrlib.lock.LogicalLockResult.
2718
2088
self.repository.lock_read()
2719
2089
if not self._lock_mode:
2720
self._note_lock('r')
2721
2090
self._lock_mode = 'r'
2722
2091
self._lock_count = 1
2723
2092
if self._real_branch is not None:
2724
2093
self._real_branch.lock_read()
2726
2095
self._lock_count += 1
2727
return lock.LogicalLockResult(self.unlock)
2729
2097
def _remote_lock_write(self, token):
2730
2098
if token is None:
2731
2099
branch_token = repo_token = ''
2733
2101
branch_token = token
2734
repo_token = self.repository.lock_write().repository_token
2102
repo_token = self.repository.lock_write()
2735
2103
self.repository.unlock()
2736
2104
err_context = {'token': token}
2738
response = self._call(
2739
'Branch.lock_write', self._remote_path(), branch_token,
2740
repo_token or '', **err_context)
2741
except errors.LockContention, e:
2742
# The LockContention from the server doesn't have any
2743
# information about the lock_url. We re-raise LockContention
2744
# with valid lock_url.
2745
raise errors.LockContention('(remote lock)',
2746
self.repository.base.split('.bzr/')[0])
2105
response = self._call(
2106
'Branch.lock_write', self._remote_path(), branch_token,
2107
repo_token or '', **err_context)
2747
2108
if response[0] != 'ok':
2748
2109
raise errors.UnexpectedSmartServerResponse(response)
2749
2110
ok, branch_token, repo_token = response
3051
2377
except errors.UnknownSmartMethod:
3052
2378
medium._remember_remote_is_before((1, 6))
3053
2379
self._clear_cached_state_of_remote_branch_only()
3054
self._set_revision_history(self._lefthand_history(revision_id,
2380
self.set_revision_history(self._lefthand_history(revision_id,
3055
2381
last_rev=last_rev,other_branch=other_branch))
3057
2383
def set_push_location(self, location):
3058
2384
self._ensure_real()
3059
2385
return self._real_branch.set_push_location(location)
3061
def heads_to_fetch(self):
3062
if self._format._use_default_local_heads_to_fetch():
3063
# We recognise this format, and its heads-to-fetch implementation
3064
# is the default one (tip + tags). In this case it's cheaper to
3065
# just use the default implementation rather than a special RPC as
3066
# the tip and tags data is cached.
3067
return branch.Branch.heads_to_fetch(self)
3068
medium = self._client._medium
3069
if medium._is_remote_before((2, 4)):
3070
return self._vfs_heads_to_fetch()
3072
return self._rpc_heads_to_fetch()
3073
except errors.UnknownSmartMethod:
3074
medium._remember_remote_is_before((2, 4))
3075
return self._vfs_heads_to_fetch()
3077
def _rpc_heads_to_fetch(self):
3078
response = self._call('Branch.heads_to_fetch', self._remote_path())
3079
if len(response) != 2:
3080
raise errors.UnexpectedSmartServerResponse(response)
3081
must_fetch, if_present_fetch = response
3082
return set(must_fetch), set(if_present_fetch)
3084
def _vfs_heads_to_fetch(self):
3086
return self._real_branch.heads_to_fetch()
3089
class RemoteConfig(object):
3090
"""A Config that reads and writes from smart verbs.
2388
class RemoteBranchConfig(object):
2389
"""A Config that reads from a smart branch and writes via smart methods.
3092
2391
It is a low-level object that considers config data to be name/value pairs
3093
2392
that may be associated with a section. Assigning meaning to the these
3094
2393
values is done at higher levels like bzrlib.config.TreeConfig.
2396
def __init__(self, branch):
2397
self._branch = branch
3097
2399
def get_option(self, name, section=None, default=None):
3098
2400
"""Return the value associated with a named option.
3154
2433
medium = self._branch._client._medium
3155
2434
if medium._is_remote_before((1, 14)):
3156
2435
return self._vfs_set_option(value, name, section)
3157
if isinstance(value, dict):
3158
if medium._is_remote_before((2, 2)):
3159
return self._vfs_set_option(value, name, section)
3160
return self._set_config_option_dict(value, name, section)
3162
return self._set_config_option(value, name, section)
3164
def _set_config_option(self, value, name, section):
3166
2437
path = self._branch._remote_path()
3167
2438
response = self._branch._client.call('Branch.set_config_option',
3168
2439
path, self._branch._lock_token, self._branch._repo_lock_token,
3169
2440
value.encode('utf8'), name, section or '')
3170
2441
except errors.UnknownSmartMethod:
3171
medium = self._branch._client._medium
3172
2442
medium._remember_remote_is_before((1, 14))
3173
2443
return self._vfs_set_option(value, name, section)
3174
2444
if response != ():
3175
2445
raise errors.UnexpectedSmartServerResponse(response)
3177
def _serialize_option_dict(self, option_dict):
3179
for key, value in option_dict.items():
3180
if isinstance(key, unicode):
3181
key = key.encode('utf8')
3182
if isinstance(value, unicode):
3183
value = value.encode('utf8')
3184
utf8_dict[key] = value
3185
return bencode.bencode(utf8_dict)
3187
def _set_config_option_dict(self, value, name, section):
3189
path = self._branch._remote_path()
3190
serialised_dict = self._serialize_option_dict(value)
3191
response = self._branch._client.call(
3192
'Branch.set_config_option_dict',
3193
path, self._branch._lock_token, self._branch._repo_lock_token,
3194
serialised_dict, name, section or '')
3195
except errors.UnknownSmartMethod:
3196
medium = self._branch._client._medium
3197
medium._remember_remote_is_before((2, 2))
3198
return self._vfs_set_option(value, name, section)
3200
raise errors.UnexpectedSmartServerResponse(response)
3202
def _real_object(self):
2447
def _vfs_set_option(self, value, name, section=None):
3203
2448
self._branch._ensure_real()
3204
return self._branch._real_branch
3206
def _vfs_set_option(self, value, name, section=None):
3207
return self._real_object()._get_config().set_option(
3208
value, name, section)
3211
class RemoteBzrDirConfig(RemoteConfig):
3212
"""A RemoteConfig for BzrDirs."""
3214
def __init__(self, bzrdir):
3215
self._bzrdir = bzrdir
3217
def _get_configobj(self):
3218
medium = self._bzrdir._client._medium
3219
verb = 'BzrDir.get_config_file'
3220
if medium._is_remote_before((1, 15)):
3221
raise errors.UnknownSmartMethod(verb)
3222
path = self._bzrdir._path_for_remote_call(self._bzrdir._client)
3223
response = self._bzrdir._call_expecting_body(
3225
return self._response_to_configobj(response)
3227
def _vfs_get_option(self, name, section, default):
3228
return self._real_object()._get_config().get_option(
3229
name, section, default)
3231
def set_option(self, value, name, section=None):
3232
"""Set the value associated with a named option.
3234
:param value: The value to set
3235
:param name: The name of the value to set
3236
:param section: The section the option is in (if any)
3238
return self._real_object()._get_config().set_option(
3239
value, name, section)
3241
def _real_object(self):
3242
self._bzrdir._ensure_real()
3243
return self._bzrdir._real_bzrdir
2449
return self._branch._real_branch._get_config().set_option(
2450
value, name, section)
3247
2453
def _extract_tar(tar, to_dir):