61
59
except errors.ErrorFromSmartServer, err:
62
60
self._translate_error(err, **err_context)
62
def _call_with_body_bytes(self, method, args, body_bytes, **err_context):
64
return self._client.call_with_body_bytes(method, args, body_bytes)
65
except errors.ErrorFromSmartServer, err:
66
self._translate_error(err, **err_context)
64
68
def _call_with_body_bytes_expecting_body(self, method, args, body_bytes,
104
109
self._client = client._SmartClient(medium)
106
111
self._client = _client
118
return '%s(%r)' % (self.__class__.__name__, self._client)
120
def _probe_bzrdir(self):
121
medium = self._client._medium
109
122
path = self._path_for_remote_call(self._client)
123
if medium._is_remote_before((2, 1)):
127
self._rpc_open_2_1(path)
129
except errors.UnknownSmartMethod:
130
medium._remember_remote_is_before((2, 1))
133
def _rpc_open_2_1(self, path):
134
response = self._call('BzrDir.open_2.1', path)
135
if response == ('no',):
136
raise errors.NotBranchError(path=self.root_transport.base)
137
elif response[0] == 'yes':
138
if response[1] == 'yes':
139
self._has_working_tree = True
140
elif response[1] == 'no':
141
self._has_working_tree = False
143
raise errors.UnexpectedSmartServerResponse(response)
145
raise errors.UnexpectedSmartServerResponse(response)
147
def _rpc_open(self, path):
110
148
response = self._call('BzrDir.open', path)
111
149
if response not in [('yes',), ('no',)]:
112
150
raise errors.UnexpectedSmartServerResponse(response)
113
151
if response == ('no',):
114
raise errors.NotBranchError(path=transport.base)
152
raise errors.NotBranchError(path=self.root_transport.base)
116
154
def _ensure_real(self):
117
155
"""Ensure that there is a _real_bzrdir set.
245
287
def _get_branch_reference(self):
246
288
path = self._path_for_remote_call(self._client)
247
289
medium = self._client._medium
248
if not medium._is_remote_before((1, 13)):
291
('BzrDir.open_branchV3', (2, 1)),
292
('BzrDir.open_branchV2', (1, 13)),
293
('BzrDir.open_branch', None),
295
for verb, required_version in candidate_calls:
296
if required_version and medium._is_remote_before(required_version):
250
response = self._call('BzrDir.open_branchV2', path)
251
if response[0] not in ('ref', 'branch'):
252
raise errors.UnexpectedSmartServerResponse(response)
299
response = self._call(verb, path)
254
300
except errors.UnknownSmartMethod:
255
medium._remember_remote_is_before((1, 13))
256
response = self._call('BzrDir.open_branch', path)
257
if response[0] != 'ok':
301
if required_version is None:
303
medium._remember_remote_is_before(required_version)
306
if verb == 'BzrDir.open_branch':
307
if response[0] != 'ok':
308
raise errors.UnexpectedSmartServerResponse(response)
309
if response[1] != '':
310
return ('ref', response[1])
312
return ('branch', '')
313
if response[0] not in ('ref', 'branch'):
258
314
raise errors.UnexpectedSmartServerResponse(response)
259
if response[1] != '':
260
return ('ref', response[1])
262
return ('branch', '')
264
317
def _get_tree_branch(self):
265
318
"""See BzrDir._get_tree_branch()."""
545
615
return self._custom_format._fetch_reconcile
547
617
def get_format_description(self):
548
return 'bzr remote repository'
619
return 'Remote: ' + self._custom_format.get_format_description()
550
621
def __eq__(self, other):
551
622
return self.__class__ is other.__class__
553
def check_conversion_target(self, target_format):
554
if self.rich_root_data and not target_format.rich_root_data:
555
raise errors.BadConversionTarget(
556
'Does not support rich root data.', target_format)
557
if (self.supports_tree_reference and
558
not getattr(target_format, 'supports_tree_reference', False)):
559
raise errors.BadConversionTarget(
560
'Does not support nested trees', target_format)
562
624
def network_name(self):
563
625
if self._network_name:
564
626
return self._network_name
813
875
result.add(_mod_revision.NULL_REVISION)
878
def _has_same_fallbacks(self, other_repo):
879
"""Returns true if the repositories have the same fallbacks."""
880
# XXX: copied from Repository; it should be unified into a base class
881
# <https://bugs.edge.launchpad.net/bzr/+bug/401622>
882
my_fb = self._fallback_repositories
883
other_fb = other_repo._fallback_repositories
884
if len(my_fb) != len(other_fb):
886
for f, g in zip(my_fb, other_fb):
887
if not f.has_same_location(g):
816
891
def has_same_location(self, other):
892
# TODO: Move to RepositoryBase and unify with the regular Repository
893
# one; unfortunately the tests rely on slightly different behaviour at
894
# present -- mbp 20090710
817
895
return (self.__class__ is other.__class__ and
818
896
self.bzrdir.transport.base == other.bzrdir.transport.base)
1409
1497
return self._real_repository.get_signature_text(revision_id)
1411
1499
@needs_read_lock
1412
def get_inventory_xml(self, revision_id):
1500
def _get_inventory_xml(self, revision_id):
1413
1501
self._ensure_real()
1414
return self._real_repository.get_inventory_xml(revision_id)
1502
return self._real_repository._get_inventory_xml(revision_id)
1416
def deserialise_inventory(self, revision_id, xml):
1504
def _deserialise_inventory(self, revision_id, xml):
1417
1505
self._ensure_real()
1418
return self._real_repository.deserialise_inventory(revision_id, xml)
1506
return self._real_repository._deserialise_inventory(revision_id, xml)
1420
1508
def reconcile(self, other=None, thorough=False):
1421
1509
self._ensure_real()
1658
1748
def insert_stream(self, stream, src_format, resume_tokens):
1659
1749
target = self.target_repo
1660
1750
target._unstacked_provider.missing_keys.clear()
1751
candidate_calls = [('Repository.insert_stream_1.19', (1, 19))]
1661
1752
if target._lock_token:
1662
verb = 'Repository.insert_stream_locked'
1663
extra_args = (target._lock_token or '',)
1664
required_version = (1, 14)
1753
candidate_calls.append(('Repository.insert_stream_locked', (1, 14)))
1754
lock_args = (target._lock_token or '',)
1666
verb = 'Repository.insert_stream'
1668
required_version = (1, 13)
1756
candidate_calls.append(('Repository.insert_stream', (1, 13)))
1669
1758
client = target._client
1670
1759
medium = client._medium
1671
if medium._is_remote_before(required_version):
1672
# No possible way this can work.
1673
return self._insert_real(stream, src_format, resume_tokens)
1674
1760
path = target.bzrdir._path_for_remote_call(client)
1675
if not resume_tokens:
1676
# XXX: Ugly but important for correctness, *will* be fixed during
1677
# 1.13 cycle. Pushing a stream that is interrupted results in a
1678
# fallback to the _real_repositories sink *with a partial stream*.
1679
# Thats bad because we insert less data than bzr expected. To avoid
1680
# this we do a trial push to make sure the verb is accessible, and
1681
# do not fallback when actually pushing the stream. A cleanup patch
1682
# is going to look at rewinding/restarting the stream/partial
1761
# Probe for the verb to use with an empty stream before sending the
1762
# real stream to it. We do this both to avoid the risk of sending a
1763
# large request that is then rejected, and because we don't want to
1764
# implement a way to buffer, rewind, or restart the stream.
1766
for verb, required_version in candidate_calls:
1767
if medium._is_remote_before(required_version):
1770
# We've already done the probing (and set _is_remote_before) on
1771
# a previous insert.
1684
1774
byte_stream = smart_repo._stream_to_byte_stream([], src_format)
1686
1776
response = client.call_with_body_stream(
1687
(verb, path, '') + extra_args, byte_stream)
1777
(verb, path, '') + lock_args, byte_stream)
1688
1778
except errors.UnknownSmartMethod:
1689
1779
medium._remember_remote_is_before(required_version)
1690
return self._insert_real(stream, src_format, resume_tokens)
1785
return self._insert_real(stream, src_format, resume_tokens)
1786
self._last_inv_record = None
1787
self._last_substream = None
1788
if required_version < (1, 19):
1789
# Remote side doesn't support inventory deltas. Wrap the stream to
1790
# make sure we don't send any. If the stream contains inventory
1791
# deltas we'll interrupt the smart insert_stream request and
1793
stream = self._stop_stream_if_inventory_delta(stream)
1691
1794
byte_stream = smart_repo._stream_to_byte_stream(
1692
1795
stream, src_format)
1693
1796
resume_tokens = ' '.join(resume_tokens)
1694
1797
response = client.call_with_body_stream(
1695
(verb, path, resume_tokens) + extra_args, byte_stream)
1798
(verb, path, resume_tokens) + lock_args, byte_stream)
1696
1799
if response[0][0] not in ('ok', 'missing-basis'):
1697
1800
raise errors.UnexpectedSmartServerResponse(response)
1801
if self._last_substream is not None:
1802
# The stream included an inventory-delta record, but the remote
1803
# side isn't new enough to support them. So we need to send the
1804
# rest of the stream via VFS.
1805
self.target_repo.refresh_data()
1806
return self._resume_stream_with_vfs(response, src_format)
1698
1807
if response[0][0] == 'missing-basis':
1699
1808
tokens, missing_keys = bencode.bdecode_as_tuple(response[0][1])
1700
1809
resume_tokens = tokens
1703
1812
self.target_repo.refresh_data()
1704
1813
return [], set()
1815
def _resume_stream_with_vfs(self, response, src_format):
1816
"""Resume sending a stream via VFS, first resending the record and
1817
substream that couldn't be sent via an insert_stream verb.
1819
if response[0][0] == 'missing-basis':
1820
tokens, missing_keys = bencode.bdecode_as_tuple(response[0][1])
1821
# Ignore missing_keys, we haven't finished inserting yet
1824
def resume_substream():
1825
# Yield the substream that was interrupted.
1826
for record in self._last_substream:
1828
self._last_substream = None
1829
def resume_stream():
1830
# Finish sending the interrupted substream
1831
yield ('inventory-deltas', resume_substream())
1832
# Then simply continue sending the rest of the stream.
1833
for substream_kind, substream in self._last_stream:
1834
yield substream_kind, substream
1835
return self._insert_real(resume_stream(), src_format, tokens)
1837
def _stop_stream_if_inventory_delta(self, stream):
1838
"""Normally this just lets the original stream pass-through unchanged.
1840
However if any 'inventory-deltas' substream occurs it will stop
1841
streaming, and store the interrupted substream and stream in
1842
self._last_substream and self._last_stream so that the stream can be
1843
resumed by _resume_stream_with_vfs.
1846
stream_iter = iter(stream)
1847
for substream_kind, substream in stream_iter:
1848
if substream_kind == 'inventory-deltas':
1849
self._last_substream = substream
1850
self._last_stream = stream_iter
1853
yield substream_kind, substream
1707
1856
class RemoteStreamSource(repository.StreamSource):
1708
1857
"""Stream data from a remote server."""
1711
1860
if (self.from_repository._fallback_repositories and
1712
1861
self.to_format._fetch_order == 'topological'):
1713
1862
return self._real_stream(self.from_repository, search)
1714
return self.missing_parents_chain(search, [self.from_repository] +
1715
self.from_repository._fallback_repositories)
1865
repos = [self.from_repository]
1871
repos.extend(repo._fallback_repositories)
1872
sources.append(repo)
1873
return self.missing_parents_chain(search, sources)
1875
def get_stream_for_missing_keys(self, missing_keys):
1876
self.from_repository._ensure_real()
1877
real_repo = self.from_repository._real_repository
1878
real_source = real_repo._get_source(self.to_format)
1879
return real_source.get_stream_for_missing_keys(missing_keys)
1717
1881
def _real_stream(self, repo, search):
1718
1882
"""Get a stream for search from repo.
1748
1913
return self._real_stream(repo, search)
1749
1914
client = repo._client
1750
1915
medium = client._medium
1751
if medium._is_remote_before((1, 13)):
1752
# streaming was added in 1.13
1753
return self._real_stream(repo, search)
1754
1916
path = repo.bzrdir._path_for_remote_call(client)
1756
search_bytes = repo._serialise_search_result(search)
1757
response = repo._call_with_body_bytes_expecting_body(
1758
'Repository.get_stream',
1759
(path, self.to_format.network_name()), search_bytes)
1760
response_tuple, response_handler = response
1761
except errors.UnknownSmartMethod:
1762
medium._remember_remote_is_before((1,13))
1917
search_bytes = repo._serialise_search_result(search)
1918
args = (path, self.to_format.network_name())
1920
('Repository.get_stream_1.19', (1, 19)),
1921
('Repository.get_stream', (1, 13))]
1923
for verb, version in candidate_verbs:
1924
if medium._is_remote_before(version):
1927
response = repo._call_with_body_bytes_expecting_body(
1928
verb, args, search_bytes)
1929
except errors.UnknownSmartMethod:
1930
medium._remember_remote_is_before(version)
1932
response_tuple, response_handler = response
1763
1936
return self._real_stream(repo, search)
1764
1937
if response_tuple[0] != 'ok':
1765
1938
raise errors.UnexpectedSmartServerResponse(response_tuple)
2146
2325
return self._vfs_get_tags_bytes()
2147
2326
return response[0]
2328
def _vfs_set_tags_bytes(self, bytes):
2330
return self._real_branch._set_tags_bytes(bytes)
2332
def _set_tags_bytes(self, bytes):
2333
medium = self._client._medium
2334
if medium._is_remote_before((1, 18)):
2335
self._vfs_set_tags_bytes(bytes)
2339
self._remote_path(), self._lock_token, self._repo_lock_token)
2340
response = self._call_with_body_bytes(
2341
'Branch.set_tags_bytes', args, bytes)
2342
except errors.UnknownSmartMethod:
2343
medium._remember_remote_is_before((1, 18))
2344
self._vfs_set_tags_bytes(bytes)
2149
2346
def lock_read(self):
2150
2347
self.repository.lock_read()
2151
2348
if not self._lock_mode:
2349
self._note_lock('r')
2152
2350
self._lock_mode = 'r'
2153
2351
self._lock_count = 1
2154
2352
if self._real_branch is not None:
2633
2830
'Missing key %r in context %r', key_err.args[0], context)
2636
if err.error_verb == 'NoSuchRevision':
2833
if err.error_verb == 'IncompatibleRepositories':
2834
raise errors.IncompatibleRepositories(err.error_args[0],
2835
err.error_args[1], err.error_args[2])
2836
elif err.error_verb == 'NoSuchRevision':
2637
2837
raise NoSuchRevision(find('branch'), err.error_args[0])
2638
2838
elif err.error_verb == 'nosuchrevision':
2639
2839
raise NoSuchRevision(find('repository'), err.error_args[0])
2640
elif err.error_tuple == ('nobranch',):
2641
raise errors.NotBranchError(path=find('bzrdir').root_transport.base)
2840
elif err.error_verb == 'nobranch':
2841
if len(err.error_args) >= 1:
2842
extra = err.error_args[0]
2845
raise errors.NotBranchError(path=find('bzrdir').root_transport.base,
2642
2847
elif err.error_verb == 'norepository':
2643
2848
raise errors.NoRepositoryPresent(find('bzrdir'))
2644
2849
elif err.error_verb == 'LockContention':