14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
# TODO: At some point, handle upgrades by just passing the whole request
18
# across to run on the server.
19
22
from bzrlib import (
31
33
revision as _mod_revision,
34
37
from bzrlib.branch import BranchReferenceFormat
35
38
from bzrlib.bzrdir import BzrDir, RemoteBzrDirFormat
36
from bzrlib.decorators import needs_read_lock, needs_write_lock, only_raises
39
from bzrlib.decorators import needs_read_lock, needs_write_lock
37
40
from bzrlib.errors import (
39
42
SmartProtocolError,
59
63
except errors.ErrorFromSmartServer, err:
60
64
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)
68
66
def _call_with_body_bytes_expecting_body(self, method, args, body_bytes,
109
106
self._client = client._SmartClient(medium)
111
108
self._client = _client
118
return '%s(%r)' % (self.__class__.__name__, self._client)
120
def _probe_bzrdir(self):
121
medium = self._client._medium
122
111
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):
148
112
response = self._call('BzrDir.open', path)
149
113
if response not in [('yes',), ('no',)]:
150
114
raise errors.UnexpectedSmartServerResponse(response)
151
115
if response == ('no',):
152
raise errors.NotBranchError(path=self.root_transport.base)
116
raise errors.NotBranchError(path=transport.base)
154
118
def _ensure_real(self):
155
119
"""Ensure that there is a _real_bzrdir set.
287
247
def _get_branch_reference(self):
288
248
path = self._path_for_remote_call(self._client)
289
249
medium = self._client._medium
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
if not medium._is_remote_before((1, 13)):
299
response = self._call(verb, path)
252
response = self._call('BzrDir.open_branchV2', path)
253
if response[0] not in ('ref', 'branch'):
254
raise errors.UnexpectedSmartServerResponse(response)
300
256
except errors.UnknownSmartMethod:
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'):
257
medium._remember_remote_is_before((1, 13))
258
response = self._call('BzrDir.open_branch', path)
259
if response[0] != 'ok':
314
260
raise errors.UnexpectedSmartServerResponse(response)
261
if response[1] != '':
262
return ('ref', response[1])
264
return ('branch', '')
317
266
def _get_tree_branch(self):
318
267
"""See BzrDir._get_tree_branch()."""
615
547
return self._custom_format._fetch_reconcile
617
549
def get_format_description(self):
619
return 'Remote: ' + self._custom_format.get_format_description()
550
return 'bzr remote repository'
621
552
def __eq__(self, other):
622
553
return self.__class__ is other.__class__
555
def check_conversion_target(self, target_format):
556
if self.rich_root_data and not target_format.rich_root_data:
557
raise errors.BadConversionTarget(
558
'Does not support rich root data.', target_format)
559
if (self.supports_tree_reference and
560
not getattr(target_format, 'supports_tree_reference', False)):
561
raise errors.BadConversionTarget(
562
'Does not support nested trees', target_format)
624
564
def network_name(self):
625
565
if self._network_name:
626
566
return self._network_name
735
670
self._ensure_real()
736
671
return self._real_repository.suspend_write_group()
738
def get_missing_parent_inventories(self, check_for_missing_texts=True):
740
return self._real_repository.get_missing_parent_inventories(
741
check_for_missing_texts=check_for_missing_texts)
743
def _get_rev_id_for_revno_vfs(self, revno, known_pair):
745
return self._real_repository.get_rev_id_for_revno(
748
def get_rev_id_for_revno(self, revno, known_pair):
749
"""See Repository.get_rev_id_for_revno."""
750
path = self.bzrdir._path_for_remote_call(self._client)
752
if self._client._medium._is_remote_before((1, 17)):
753
return self._get_rev_id_for_revno_vfs(revno, known_pair)
754
response = self._call(
755
'Repository.get_rev_id_for_revno', path, revno, known_pair)
756
except errors.UnknownSmartMethod:
757
self._client._medium._remember_remote_is_before((1, 17))
758
return self._get_rev_id_for_revno_vfs(revno, known_pair)
759
if response[0] == 'ok':
760
return True, response[1]
761
elif response[0] == 'history-incomplete':
762
known_pair = response[1:3]
763
for fallback in self._fallback_repositories:
764
found, result = fallback.get_rev_id_for_revno(revno, known_pair)
769
# Not found in any fallbacks
770
return False, known_pair
772
raise errors.UnexpectedSmartServerResponse(response)
673
def get_missing_parent_inventories(self):
675
return self._real_repository.get_missing_parent_inventories()
774
677
def _ensure_real(self):
775
678
"""Ensure that there is a _real_repository set.
875
778
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):
891
781
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
895
782
return (self.__class__ is other.__class__ and
896
783
self.bzrdir.transport.base == other.bzrdir.transport.base)
965
852
def is_write_locked(self):
966
853
return self._lock_mode == 'w'
968
def _warn_if_deprecated(self, branch=None):
969
# If we have a real repository, the check will be done there, if we
970
# don't the check will be done remotely.
973
855
def lock_read(self):
974
856
# wrong eventually - want a local lock cache context
975
857
if not self._lock_mode:
977
858
self._lock_mode = 'r'
978
859
self._lock_count = 1
979
860
self._unstacked_provider.enable_cache(cache_misses=True)
980
861
if self._real_repository is not None:
981
862
self._real_repository.lock_read()
982
for repo in self._fallback_repositories:
985
864
self._lock_count += 1
865
for repo in self._fallback_repositories:
987
868
def _remote_lock_write(self, token):
988
869
path = self.bzrdir._path_for_remote_call(self._client)
1497
1365
return self._real_repository.get_signature_text(revision_id)
1499
1367
@needs_read_lock
1500
def _get_inventory_xml(self, revision_id):
1368
def get_inventory_xml(self, revision_id):
1501
1369
self._ensure_real()
1502
return self._real_repository._get_inventory_xml(revision_id)
1370
return self._real_repository.get_inventory_xml(revision_id)
1504
def _deserialise_inventory(self, revision_id, xml):
1372
def deserialise_inventory(self, revision_id, xml):
1505
1373
self._ensure_real()
1506
return self._real_repository._deserialise_inventory(revision_id, xml)
1374
return self._real_repository.deserialise_inventory(revision_id, xml)
1508
1376
def reconcile(self, other=None, thorough=False):
1509
1377
self._ensure_real()
1748
1614
def insert_stream(self, stream, src_format, resume_tokens):
1749
1615
target = self.target_repo
1750
1616
target._unstacked_provider.missing_keys.clear()
1751
candidate_calls = [('Repository.insert_stream_1.19', (1, 19))]
1752
1617
if target._lock_token:
1753
candidate_calls.append(('Repository.insert_stream_locked', (1, 14)))
1754
lock_args = (target._lock_token or '',)
1618
verb = 'Repository.insert_stream_locked'
1619
extra_args = (target._lock_token or '',)
1620
required_version = (1, 14)
1756
candidate_calls.append(('Repository.insert_stream', (1, 13)))
1622
verb = 'Repository.insert_stream'
1624
required_version = (1, 13)
1758
1625
client = target._client
1759
1626
medium = client._medium
1627
if medium._is_remote_before(required_version):
1628
# No possible way this can work.
1629
return self._insert_real(stream, src_format, resume_tokens)
1760
1630
path = target.bzrdir._path_for_remote_call(client)
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.
1631
if not resume_tokens:
1632
# XXX: Ugly but important for correctness, *will* be fixed during
1633
# 1.13 cycle. Pushing a stream that is interrupted results in a
1634
# fallback to the _real_repositories sink *with a partial stream*.
1635
# Thats bad because we insert less data than bzr expected. To avoid
1636
# this we do a trial push to make sure the verb is accessible, and
1637
# do not fallback when actually pushing the stream. A cleanup patch
1638
# is going to look at rewinding/restarting the stream/partial
1774
1640
byte_stream = smart_repo._stream_to_byte_stream([], src_format)
1776
1642
response = client.call_with_body_stream(
1777
(verb, path, '') + lock_args, byte_stream)
1643
(verb, path, '') + extra_args, byte_stream)
1778
1644
except errors.UnknownSmartMethod:
1779
1645
medium._remember_remote_is_before(required_version)
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)
1646
return self._insert_real(stream, src_format, resume_tokens)
1794
1647
byte_stream = smart_repo._stream_to_byte_stream(
1795
1648
stream, src_format)
1796
1649
resume_tokens = ' '.join(resume_tokens)
1797
1650
response = client.call_with_body_stream(
1798
(verb, path, resume_tokens) + lock_args, byte_stream)
1651
(verb, path, resume_tokens) + extra_args, byte_stream)
1799
1652
if response[0][0] not in ('ok', 'missing-basis'):
1800
1653
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)
1807
1654
if response[0][0] == 'missing-basis':
1808
1655
tokens, missing_keys = bencode.bdecode_as_tuple(response[0][1])
1809
1656
resume_tokens = tokens
1812
1659
self.target_repo.refresh_data()
1813
1660
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
1856
1663
class RemoteStreamSource(repository.StreamSource):
1857
1664
"""Stream data from a remote server."""
1860
1667
if (self.from_repository._fallback_repositories and
1861
1668
self.to_format._fetch_order == 'topological'):
1862
1669
return self._real_stream(self.from_repository, search)
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)
1670
return self.missing_parents_chain(search, [self.from_repository] +
1671
self.from_repository._fallback_repositories)
1881
1673
def _real_stream(self, repo, search):
1882
1674
"""Get a stream for search from repo.
1913
1704
return self._real_stream(repo, search)
1914
1705
client = repo._client
1915
1706
medium = client._medium
1707
if medium._is_remote_before((1, 13)):
1708
# streaming was added in 1.13
1709
return self._real_stream(repo, search)
1916
1710
path = repo.bzrdir._path_for_remote_call(client)
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
1712
search_bytes = repo._serialise_search_result(search)
1713
response = repo._call_with_body_bytes_expecting_body(
1714
'Repository.get_stream',
1715
(path, self.to_format.network_name()), search_bytes)
1716
response_tuple, response_handler = response
1717
except errors.UnknownSmartMethod:
1718
medium._remember_remote_is_before((1,13))
1936
1719
return self._real_stream(repo, search)
1937
1720
if response_tuple[0] != 'ok':
1938
1721
raise errors.UnexpectedSmartServerResponse(response_tuple)
2325
2093
return self._vfs_get_tags_bytes()
2326
2094
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)
2346
2096
def lock_read(self):
2347
2097
self.repository.lock_read()
2348
2098
if not self._lock_mode:
2349
self._note_lock('r')
2350
2099
self._lock_mode = 'r'
2351
2100
self._lock_count = 1
2352
2101
if self._real_branch is not None:
2459
2210
raise NotImplementedError(self.dont_leave_lock_in_place)
2460
2211
self._leave_lock = False
2463
def get_rev_id(self, revno, history=None):
2465
return _mod_revision.NULL_REVISION
2466
last_revision_info = self.last_revision_info()
2467
ok, result = self.repository.get_rev_id_for_revno(
2468
revno, last_revision_info)
2471
missing_parent = result[1]
2472
# Either the revision named by the server is missing, or its parent
2473
# is. Call get_parent_map to determine which, so that we report a
2475
parent_map = self.repository.get_parent_map([missing_parent])
2476
if missing_parent in parent_map:
2477
missing_parent = parent_map[missing_parent]
2478
raise errors.RevisionNotPresent(missing_parent, self.repository)
2480
2213
def _last_revision_info(self):
2481
2214
response = self._call('Branch.last_revision_info', self._remote_path())
2482
2215
if response[0] != 'ok':
2830
2560
'Missing key %r in context %r', key_err.args[0], context)
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':
2563
if err.error_verb == 'NoSuchRevision':
2837
2564
raise NoSuchRevision(find('branch'), err.error_args[0])
2838
2565
elif err.error_verb == 'nosuchrevision':
2839
2566
raise NoSuchRevision(find('repository'), err.error_args[0])
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,
2567
elif err.error_tuple == ('nobranch',):
2568
raise errors.NotBranchError(path=find('bzrdir').root_transport.base)
2847
2569
elif err.error_verb == 'norepository':
2848
2570
raise errors.NoRepositoryPresent(find('bzrdir'))
2849
2571
elif err.error_verb == 'LockContention':