104
109
self._client = client._SmartClient(medium)
106
111
self._client = _client
117
def _probe_bzrdir(self):
118
medium = self._client._medium
109
119
path = self._path_for_remote_call(self._client)
120
if medium._is_remote_before((2, 1)):
124
self._rpc_open_2_1(path)
126
except errors.UnknownSmartMethod:
127
medium._remember_remote_is_before((2, 1))
130
def _rpc_open_2_1(self, path):
131
response = self._call('BzrDir.open_2.1', path)
132
if response == ('no',):
133
raise errors.NotBranchError(path=self.root_transport.base)
134
elif response[0] == 'yes':
135
if response[1] == 'yes':
136
self._has_working_tree = True
137
elif response[1] == 'no':
138
self._has_working_tree = False
140
raise errors.UnexpectedSmartServerResponse(response)
142
raise errors.UnexpectedSmartServerResponse(response)
144
def _rpc_open(self, path):
110
145
response = self._call('BzrDir.open', path)
111
146
if response not in [('yes',), ('no',)]:
112
147
raise errors.UnexpectedSmartServerResponse(response)
113
148
if response == ('no',):
114
raise errors.NotBranchError(path=transport.base)
149
raise errors.NotBranchError(path=self.root_transport.base)
116
151
def _ensure_real(self):
117
152
"""Ensure that there is a _real_bzrdir set.
665
716
self._ensure_real()
666
717
return self._real_repository.suspend_write_group()
719
def get_missing_parent_inventories(self, check_for_missing_texts=True):
721
return self._real_repository.get_missing_parent_inventories(
722
check_for_missing_texts=check_for_missing_texts)
724
def _get_rev_id_for_revno_vfs(self, revno, known_pair):
726
return self._real_repository.get_rev_id_for_revno(
729
def get_rev_id_for_revno(self, revno, known_pair):
730
"""See Repository.get_rev_id_for_revno."""
731
path = self.bzrdir._path_for_remote_call(self._client)
733
if self._client._medium._is_remote_before((1, 17)):
734
return self._get_rev_id_for_revno_vfs(revno, known_pair)
735
response = self._call(
736
'Repository.get_rev_id_for_revno', path, revno, known_pair)
737
except errors.UnknownSmartMethod:
738
self._client._medium._remember_remote_is_before((1, 17))
739
return self._get_rev_id_for_revno_vfs(revno, known_pair)
740
if response[0] == 'ok':
741
return True, response[1]
742
elif response[0] == 'history-incomplete':
743
known_pair = response[1:3]
744
for fallback in self._fallback_repositories:
745
found, result = fallback.get_rev_id_for_revno(revno, known_pair)
750
# Not found in any fallbacks
751
return False, known_pair
753
raise errors.UnexpectedSmartServerResponse(response)
668
755
def _ensure_real(self):
669
756
"""Ensure that there is a _real_repository set.
744
836
"""Return a source for streaming from this repository."""
745
837
return RemoteStreamSource(self, to_format)
747
840
def has_revision(self, revision_id):
748
"""See Repository.has_revision()."""
749
if revision_id == NULL_REVISION:
750
# The null revision is always present.
752
path = self.bzrdir._path_for_remote_call(self._client)
753
response = self._call('Repository.has_revision', path, revision_id)
754
if response[0] not in ('yes', 'no'):
755
raise errors.UnexpectedSmartServerResponse(response)
756
if response[0] == 'yes':
758
for fallback_repo in self._fallback_repositories:
759
if fallback_repo.has_revision(revision_id):
841
"""True if this repository has a copy of the revision."""
842
# Copy of bzrlib.repository.Repository.has_revision
843
return revision_id in self.has_revisions((revision_id,))
763
846
def has_revisions(self, revision_ids):
764
"""See Repository.has_revisions()."""
765
# FIXME: This does many roundtrips, particularly when there are
766
# fallback repositories. -- mbp 20080905
768
for revision_id in revision_ids:
769
if self.has_revision(revision_id):
770
result.add(revision_id)
847
"""Probe to find out the presence of multiple revisions.
849
:param revision_ids: An iterable of revision_ids.
850
:return: A set of the revision_ids that were present.
852
# Copy of bzrlib.repository.Repository.has_revisions
853
parent_map = self.get_parent_map(revision_ids)
854
result = set(parent_map)
855
if _mod_revision.NULL_REVISION in revision_ids:
856
result.add(_mod_revision.NULL_REVISION)
859
def _has_same_fallbacks(self, other_repo):
860
"""Returns true if the repositories have the same fallbacks."""
861
# XXX: copied from Repository; it should be unified into a base class
862
# <https://bugs.edge.launchpad.net/bzr/+bug/401622>
863
my_fb = self._fallback_repositories
864
other_fb = other_repo._fallback_repositories
865
if len(my_fb) != len(other_fb):
867
for f, g in zip(my_fb, other_fb):
868
if not f.has_same_location(g):
773
872
def has_same_location(self, other):
873
# TODO: Move to RepositoryBase and unify with the regular Repository
874
# one; unfortunately the tests rely on slightly different behaviour at
875
# present -- mbp 20090710
774
876
return (self.__class__ is other.__class__ and
775
877
self.bzrdir.transport.base == other.bzrdir.transport.base)
921
1029
if isinstance(repository, RemoteRepository):
922
1030
raise AssertionError()
923
1031
self._real_repository = repository
924
# If the _real_repository has _fallback_repositories, clear them out,
925
# because we want it to have the same set as this repository. This is
926
# reasonable to do because the fallbacks we clear here are from a
927
# "real" branch, and we're about to replace them with the equivalents
928
# from a RemoteBranch.
929
self._real_repository._fallback_repositories = []
1032
# three code paths happen here:
1033
# 1) old servers, RemoteBranch.open() calls _ensure_real before setting
1034
# up stacking. In this case self._fallback_repositories is [], and the
1035
# real repo is already setup. Preserve the real repo and
1036
# RemoteRepository.add_fallback_repository will avoid adding
1038
# 2) new servers, RemoteBranch.open() sets up stacking, and when
1039
# ensure_real is triggered from a branch, the real repository to
1040
# set already has a matching list with separate instances, but
1041
# as they are also RemoteRepositories we don't worry about making the
1042
# lists be identical.
1043
# 3) new servers, RemoteRepository.ensure_real is triggered before
1044
# RemoteBranch.ensure real, in this case we get a repo with no fallbacks
1045
# and need to populate it.
1046
if (self._fallback_repositories and
1047
len(self._real_repository._fallback_repositories) !=
1048
len(self._fallback_repositories)):
1049
if len(self._real_repository._fallback_repositories):
1050
raise AssertionError(
1051
"cannot cleanly remove existing _fallback_repositories")
930
1052
for fb in self._fallback_repositories:
931
1053
self._real_repository.add_fallback_repository(fb)
932
1054
if self._lock_mode == 'w':
1056
1182
# We need to accumulate additional repositories here, to pass them in
1057
1183
# on various RPC's.
1185
if self.is_locked():
1186
# We will call fallback.unlock() when we transition to the unlocked
1187
# state, so always add a lock here. If a caller passes us a locked
1188
# repository, they are responsible for unlocking it later.
1189
repository.lock_read()
1059
1190
self._fallback_repositories.append(repository)
1060
1191
# If self._real_repository was parameterised already (e.g. because a
1061
1192
# _real_branch had its get_stacked_on_url method called), then the
1062
1193
# repository to be added may already be in the _real_repositories list.
1063
1194
if self._real_repository is not None:
1064
if repository not in self._real_repository._fallback_repositories:
1195
fallback_locations = [repo.bzrdir.root_transport.base for repo in
1196
self._real_repository._fallback_repositories]
1197
if repository.bzrdir.root_transport.base not in fallback_locations:
1065
1198
self._real_repository.add_fallback_repository(repository)
1067
# They are also seen by the fallback repository. If it doesn't
1068
# exist yet they'll be added then. This implicitly copies them.
1071
1200
def add_inventory(self, revid, inv, parents):
1072
1201
self._ensure_real()
1580
1721
def insert_stream(self, stream, src_format, resume_tokens):
1581
1722
target = self.target_repo
1723
target._unstacked_provider.missing_keys.clear()
1724
candidate_calls = [('Repository.insert_stream_1.19', (1, 19))]
1582
1725
if target._lock_token:
1583
verb = 'Repository.insert_stream_locked'
1584
extra_args = (target._lock_token or '',)
1585
required_version = (1, 14)
1726
candidate_calls.append(('Repository.insert_stream_locked', (1, 14)))
1727
lock_args = (target._lock_token or '',)
1587
verb = 'Repository.insert_stream'
1589
required_version = (1, 13)
1729
candidate_calls.append(('Repository.insert_stream', (1, 13)))
1590
1731
client = target._client
1591
1732
medium = client._medium
1592
if medium._is_remote_before(required_version):
1593
# No possible way this can work.
1594
return self._insert_real(stream, src_format, resume_tokens)
1595
1733
path = target.bzrdir._path_for_remote_call(client)
1596
if not resume_tokens:
1597
# XXX: Ugly but important for correctness, *will* be fixed during
1598
# 1.13 cycle. Pushing a stream that is interrupted results in a
1599
# fallback to the _real_repositories sink *with a partial stream*.
1600
# Thats bad because we insert less data than bzr expected. To avoid
1601
# this we do a trial push to make sure the verb is accessible, and
1602
# do not fallback when actually pushing the stream. A cleanup patch
1603
# is going to look at rewinding/restarting the stream/partial
1734
# Probe for the verb to use with an empty stream before sending the
1735
# real stream to it. We do this both to avoid the risk of sending a
1736
# large request that is then rejected, and because we don't want to
1737
# implement a way to buffer, rewind, or restart the stream.
1739
for verb, required_version in candidate_calls:
1740
if medium._is_remote_before(required_version):
1743
# We've already done the probing (and set _is_remote_before) on
1744
# a previous insert.
1605
1747
byte_stream = smart_repo._stream_to_byte_stream([], src_format)
1607
1749
response = client.call_with_body_stream(
1608
(verb, path, '') + extra_args, byte_stream)
1750
(verb, path, '') + lock_args, byte_stream)
1609
1751
except errors.UnknownSmartMethod:
1610
1752
medium._remember_remote_is_before(required_version)
1611
return self._insert_real(stream, src_format, resume_tokens)
1758
return self._insert_real(stream, src_format, resume_tokens)
1759
self._last_inv_record = None
1760
self._last_substream = None
1761
if required_version < (1, 19):
1762
# Remote side doesn't support inventory deltas. Wrap the stream to
1763
# make sure we don't send any. If the stream contains inventory
1764
# deltas we'll interrupt the smart insert_stream request and
1766
stream = self._stop_stream_if_inventory_delta(stream)
1612
1767
byte_stream = smart_repo._stream_to_byte_stream(
1613
1768
stream, src_format)
1614
1769
resume_tokens = ' '.join(resume_tokens)
1615
1770
response = client.call_with_body_stream(
1616
(verb, path, resume_tokens) + extra_args, byte_stream)
1771
(verb, path, resume_tokens) + lock_args, byte_stream)
1617
1772
if response[0][0] not in ('ok', 'missing-basis'):
1618
1773
raise errors.UnexpectedSmartServerResponse(response)
1774
if self._last_substream is not None:
1775
# The stream included an inventory-delta record, but the remote
1776
# side isn't new enough to support them. So we need to send the
1777
# rest of the stream via VFS.
1778
self.target_repo.refresh_data()
1779
return self._resume_stream_with_vfs(response, src_format)
1619
1780
if response[0][0] == 'missing-basis':
1620
1781
tokens, missing_keys = bencode.bdecode_as_tuple(response[0][1])
1621
1782
resume_tokens = tokens
1622
return resume_tokens, missing_keys
1783
return resume_tokens, set(missing_keys)
1624
1785
self.target_repo.refresh_data()
1625
1786
return [], set()
1788
def _resume_stream_with_vfs(self, response, src_format):
1789
"""Resume sending a stream via VFS, first resending the record and
1790
substream that couldn't be sent via an insert_stream verb.
1792
if response[0][0] == 'missing-basis':
1793
tokens, missing_keys = bencode.bdecode_as_tuple(response[0][1])
1794
# Ignore missing_keys, we haven't finished inserting yet
1797
def resume_substream():
1798
# Yield the substream that was interrupted.
1799
for record in self._last_substream:
1801
self._last_substream = None
1802
def resume_stream():
1803
# Finish sending the interrupted substream
1804
yield ('inventory-deltas', resume_substream())
1805
# Then simply continue sending the rest of the stream.
1806
for substream_kind, substream in self._last_stream:
1807
yield substream_kind, substream
1808
return self._insert_real(resume_stream(), src_format, tokens)
1810
def _stop_stream_if_inventory_delta(self, stream):
1811
"""Normally this just lets the original stream pass-through unchanged.
1813
However if any 'inventory-deltas' substream occurs it will stop
1814
streaming, and store the interrupted substream and stream in
1815
self._last_substream and self._last_stream so that the stream can be
1816
resumed by _resume_stream_with_vfs.
1819
stream_iter = iter(stream)
1820
for substream_kind, substream in stream_iter:
1821
if substream_kind == 'inventory-deltas':
1822
self._last_substream = substream
1823
self._last_stream = stream_iter
1826
yield substream_kind, substream
1628
1829
class RemoteStreamSource(repository.StreamSource):
1629
1830
"""Stream data from a remote server."""
1669
1886
return self._real_stream(repo, search)
1670
1887
client = repo._client
1671
1888
medium = client._medium
1672
if medium._is_remote_before((1, 13)):
1673
# streaming was added in 1.13
1674
return self._real_stream(repo, search)
1675
1889
path = repo.bzrdir._path_for_remote_call(client)
1677
search_bytes = repo._serialise_search_result(search)
1678
response = repo._call_with_body_bytes_expecting_body(
1679
'Repository.get_stream',
1680
(path, self.to_format.network_name()), search_bytes)
1681
response_tuple, response_handler = response
1682
except errors.UnknownSmartMethod:
1683
medium._remember_remote_is_before((1,13))
1890
search_bytes = repo._serialise_search_result(search)
1891
args = (path, self.to_format.network_name())
1893
('Repository.get_stream_1.19', (1, 19)),
1894
('Repository.get_stream', (1, 13))]
1896
for verb, version in candidate_verbs:
1897
if medium._is_remote_before(version):
1900
response = repo._call_with_body_bytes_expecting_body(
1901
verb, args, search_bytes)
1902
except errors.UnknownSmartMethod:
1903
medium._remember_remote_is_before(version)
1905
response_tuple, response_handler = response
1684
1909
return self._real_stream(repo, search)
1685
1910
if response_tuple[0] != 'ok':
1686
1911
raise errors.UnexpectedSmartServerResponse(response_tuple)
2277
2548
self._ensure_real()
2278
2549
return self._real_branch._get_parent_location()
2280
def set_parent(self, url):
2282
return self._real_branch.set_parent(url)
2284
2551
def _set_parent_location(self, url):
2285
# Used by tests, to poke bad urls into branch configurations
2287
self.set_parent(url)
2290
return self._real_branch._set_parent_location(url)
2292
def set_stacked_on_url(self, stacked_location):
2293
"""Set the URL this branch is stacked against.
2295
:raises UnstackableBranchFormat: If the branch does not support
2297
:raises UnstackableRepositoryFormat: If the repository does not support
2552
medium = self._client._medium
2553
if medium._is_remote_before((1, 15)):
2554
return self._vfs_set_parent_location(url)
2556
call_url = url or ''
2557
if type(call_url) is not str:
2558
raise AssertionError('url must be a str or None (%s)' % url)
2559
response = self._call('Branch.set_parent_location',
2560
self._remote_path(), self._lock_token, self._repo_lock_token,
2562
except errors.UnknownSmartMethod:
2563
medium._remember_remote_is_before((1, 15))
2564
return self._vfs_set_parent_location(url)
2566
raise errors.UnexpectedSmartServerResponse(response)
2568
def _vfs_set_parent_location(self, url):
2300
2569
self._ensure_real()
2301
return self._real_branch.set_stacked_on_url(stacked_location)
2570
return self._real_branch._set_parent_location(url)
2303
2572
@needs_write_lock
2304
2573
def pull(self, source, overwrite=False, stop_revision=None,
2372
2641
return self._real_branch.set_push_location(location)
2644
class RemoteConfig(object):
2645
"""A Config that reads and writes from smart verbs.
2647
It is a low-level object that considers config data to be name/value pairs
2648
that may be associated with a section. Assigning meaning to the these
2649
values is done at higher levels like bzrlib.config.TreeConfig.
2652
def get_option(self, name, section=None, default=None):
2653
"""Return the value associated with a named option.
2655
:param name: The name of the value
2656
:param section: The section the option is in (if any)
2657
:param default: The value to return if the value is not set
2658
:return: The value or default value
2661
configobj = self._get_configobj()
2663
section_obj = configobj
2666
section_obj = configobj[section]
2669
return section_obj.get(name, default)
2670
except errors.UnknownSmartMethod:
2671
return self._vfs_get_option(name, section, default)
2673
def _response_to_configobj(self, response):
2674
if len(response[0]) and response[0][0] != 'ok':
2675
raise errors.UnexpectedSmartServerResponse(response)
2676
lines = response[1].read_body_bytes().splitlines()
2677
return config.ConfigObj(lines, encoding='utf-8')
2680
class RemoteBranchConfig(RemoteConfig):
2681
"""A RemoteConfig for Branches."""
2683
def __init__(self, branch):
2684
self._branch = branch
2686
def _get_configobj(self):
2687
path = self._branch._remote_path()
2688
response = self._branch._client.call_expecting_body(
2689
'Branch.get_config_file', path)
2690
return self._response_to_configobj(response)
2692
def set_option(self, value, name, section=None):
2693
"""Set the value associated with a named option.
2695
:param value: The value to set
2696
:param name: The name of the value to set
2697
:param section: The section the option is in (if any)
2699
medium = self._branch._client._medium
2700
if medium._is_remote_before((1, 14)):
2701
return self._vfs_set_option(value, name, section)
2703
path = self._branch._remote_path()
2704
response = self._branch._client.call('Branch.set_config_option',
2705
path, self._branch._lock_token, self._branch._repo_lock_token,
2706
value.encode('utf8'), name, section or '')
2707
except errors.UnknownSmartMethod:
2708
medium._remember_remote_is_before((1, 14))
2709
return self._vfs_set_option(value, name, section)
2711
raise errors.UnexpectedSmartServerResponse(response)
2713
def _real_object(self):
2714
self._branch._ensure_real()
2715
return self._branch._real_branch
2717
def _vfs_set_option(self, value, name, section=None):
2718
return self._real_object()._get_config().set_option(
2719
value, name, section)
2722
class RemoteBzrDirConfig(RemoteConfig):
2723
"""A RemoteConfig for BzrDirs."""
2725
def __init__(self, bzrdir):
2726
self._bzrdir = bzrdir
2728
def _get_configobj(self):
2729
medium = self._bzrdir._client._medium
2730
verb = 'BzrDir.get_config_file'
2731
if medium._is_remote_before((1, 15)):
2732
raise errors.UnknownSmartMethod(verb)
2733
path = self._bzrdir._path_for_remote_call(self._bzrdir._client)
2734
response = self._bzrdir._call_expecting_body(
2736
return self._response_to_configobj(response)
2738
def _vfs_get_option(self, name, section, default):
2739
return self._real_object()._get_config().get_option(
2740
name, section, default)
2742
def set_option(self, value, name, section=None):
2743
"""Set the value associated with a named option.
2745
:param value: The value to set
2746
:param name: The name of the value to set
2747
:param section: The section the option is in (if any)
2749
return self._real_object()._get_config().set_option(
2750
value, name, section)
2752
def _real_object(self):
2753
self._bzrdir._ensure_real()
2754
return self._bzrdir._real_bzrdir
2375
2758
def _extract_tar(tar, to_dir):
2376
2759
"""Extract all the contents of a tarfile object.