/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
3407.2.2 by Martin Pool
Remove special case in RemoteBranchLockableFiles for branch.conf
1
# Copyright (C) 2006, 2007, 2008 Canonical Ltd
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
1752.2.39 by Martin Pool
[broken] implement upgrade apis on remote bzrdirs
17
# TODO: At some point, handle upgrades by just passing the whole request
18
# across to run on the server.
19
3211.5.2 by Robert Collins
Change RemoteRepository.get_parent_map to use bz2 not gzip for compression.
20
import bz2
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
21
2490.2.5 by Aaron Bentley
Use GraphWalker.unique_ancestor to determine merge base
22
from bzrlib import (
23
    branch,
4017.3.2 by Robert Collins
Reduce the number of round trips required to create a repository over the network.
24
    bzrdir,
3192.1.1 by Andrew Bennetts
Add some -Dhpss debugging to get_parent_map.
25
    debug,
2490.2.5 by Aaron Bentley
Use GraphWalker.unique_ancestor to determine merge base
26
    errors,
3172.5.1 by Robert Collins
Create a RemoteRepository get_graph implementation and delegate get_parents_map to the real repository.
27
    graph,
2490.2.5 by Aaron Bentley
Use GraphWalker.unique_ancestor to determine merge base
28
    lockdir,
4022.1.6 by Robert Collins
Cherrypick and polish the RemoteSink for streaming push.
29
    pack,
2490.2.5 by Aaron Bentley
Use GraphWalker.unique_ancestor to determine merge base
30
    repository,
2948.3.1 by John Arbash Meinel
Fix bug #158333, make sure that Repository.fetch(self) is properly a no-op for all Repository implementations.
31
    revision,
3228.4.11 by John Arbash Meinel
Deprecations abound.
32
    symbol_versioning,
3691.2.2 by Martin Pool
Fix some problems in access to stacked repositories over hpss (#261315)
33
    urlutils,
2490.2.5 by Aaron Bentley
Use GraphWalker.unique_ancestor to determine merge base
34
)
2535.4.27 by Andrew Bennetts
Remove some unused imports.
35
from bzrlib.branch import BranchReferenceFormat
2018.5.174 by Andrew Bennetts
Various nits discovered by pyflakes.
36
from bzrlib.bzrdir import BzrDir, RemoteBzrDirFormat
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
37
from bzrlib.decorators import needs_read_lock, needs_write_lock
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
38
from bzrlib.errors import (
39
    NoSuchRevision,
40
    SmartProtocolError,
41
    )
2018.5.127 by Andrew Bennetts
Fix most of the lockable_files tests for RemoteBranchLockableFiles.
42
from bzrlib.lockable_files import LockableFiles
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
43
from bzrlib.smart import client, vfs
3297.4.1 by Andrew Bennetts
Merge 'Add Branch.set_last_revision_info smart method'.
44
from bzrlib.revision import ensure_null, NULL_REVISION
3441.5.5 by Andrew Bennetts
Some small tweaks and comments.
45
from bzrlib.trace import mutter, note, warning
4029.2.1 by Robert Collins
Support streaming push to stacked branches.
46
from bzrlib.util import bencode
4022.1.6 by Robert Collins
Cherrypick and polish the RemoteSink for streaming push.
47
from bzrlib.versionedfile import record_to_fulltext_bytes
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
48
3445.1.5 by John Arbash Meinel
allow passing a 'graph' object into Branch.update_revisions.
49
3786.2.3 by Andrew Bennetts
Remove duplicated 'call & translate errors' code in bzrlib.remote.
50
class _RpcHelper(object):
51
    """Mixin class that helps with issuing RPCs."""
52
53
    def _call(self, method, *args, **err_context):
54
        try:
55
            return self._client.call(method, *args)
56
        except errors.ErrorFromSmartServer, err:
57
            self._translate_error(err, **err_context)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
58
3786.2.3 by Andrew Bennetts
Remove duplicated 'call & translate errors' code in bzrlib.remote.
59
    def _call_expecting_body(self, method, *args, **err_context):
60
        try:
61
            return self._client.call_expecting_body(method, *args)
62
        except errors.ErrorFromSmartServer, err:
63
            self._translate_error(err, **err_context)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
64
3786.2.3 by Andrew Bennetts
Remove duplicated 'call & translate errors' code in bzrlib.remote.
65
    def _call_with_body_bytes_expecting_body(self, method, args, body_bytes,
66
                                             **err_context):
67
        try:
68
            return self._client.call_with_body_bytes_expecting_body(
69
                method, args, body_bytes)
70
        except errors.ErrorFromSmartServer, err:
71
            self._translate_error(err, **err_context)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
72
4032.3.2 by Robert Collins
Create and use a RPC call to create branches on bzr servers rather than using VFS calls.
73
74
def response_tuple_to_repo_format(response):
75
    """Convert a response tuple describing a repository format to a format."""
76
    format = RemoteRepositoryFormat()
77
    format.rich_root_data = (response[0] == 'yes')
78
    format.supports_tree_reference = (response[1] == 'yes')
79
    format.supports_external_lookups = (response[2] == 'yes')
80
    format._network_name = response[3]
81
    return format
82
83
2018.5.25 by Andrew Bennetts
Make sure RemoteBzrDirFormat is always registered (John Arbash Meinel, Robert Collins, Andrew Bennetts).
84
# Note: RemoteBzrDirFormat is in bzrdir.py
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
85
3786.2.3 by Andrew Bennetts
Remove duplicated 'call & translate errors' code in bzrlib.remote.
86
class RemoteBzrDir(BzrDir, _RpcHelper):
2018.5.163 by Andrew Bennetts
Deal with various review comments from Robert.
87
    """Control directory on a remote server, accessed via bzr:// or similar."""
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
88
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
89
    def __init__(self, transport, format, _client=None):
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
90
        """Construct a RemoteBzrDir.
91
92
        :param _client: Private parameter for testing. Disables probing and the
93
            use of a real bzrdir.
94
        """
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
95
        BzrDir.__init__(self, transport, format)
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
96
        # this object holds a delegated bzrdir that uses file-level operations
97
        # to talk to the other side
2018.5.70 by Robert Collins
Only try to get real repositories when an operation requires them.
98
        self._real_bzrdir = None
4044.1.3 by Robert Collins
Create a one-shot cache of the result of RemoteBzrDir.create_branch, eliminating 3 round trips for nonstacked branches and 5 for stacked.
99
        # 1-shot cache for the call pattern 'create_branch; open_branch' - see
100
        # create_branch for details.
101
        self._next_open_branch_result = None
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
102
103
        if _client is None:
3313.2.3 by Andrew Bennetts
Deprecate Transport.get_shared_medium.
104
            medium = transport.get_smart_medium()
3431.3.2 by Andrew Bennetts
Remove 'base' from _SmartClient entirely, now that the medium has it.
105
            self._client = client._SmartClient(medium)
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
106
        else:
107
            self._client = _client
108
            return
109
110
        path = self._path_for_remote_call(self._client)
3786.2.3 by Andrew Bennetts
Remove duplicated 'call & translate errors' code in bzrlib.remote.
111
        response = self._call('BzrDir.open', path)
2018.5.163 by Andrew Bennetts
Deal with various review comments from Robert.
112
        if response not in [('yes',), ('no',)]:
113
            raise errors.UnexpectedSmartServerResponse(response)
2018.5.26 by Andrew Bennetts
Extract a simple SmartClient class from RemoteTransport, and a hack to avoid VFS operations when probing for a bzrdir over a smart transport.
114
        if response == ('no',):
115
            raise errors.NotBranchError(path=transport.base)
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
116
2018.5.70 by Robert Collins
Only try to get real repositories when an operation requires them.
117
    def _ensure_real(self):
118
        """Ensure that there is a _real_bzrdir set.
119
2018.5.163 by Andrew Bennetts
Deal with various review comments from Robert.
120
        Used before calls to self._real_bzrdir.
2018.5.70 by Robert Collins
Only try to get real repositories when an operation requires them.
121
        """
122
        if not self._real_bzrdir:
2018.5.169 by Andrew Bennetts
Add a _server_formats flag to BzrDir.open_from_transport and BzrDirFormat.find_format, make RemoteBranch.control_files into a property.
123
            self._real_bzrdir = BzrDir.open_from_transport(
124
                self.root_transport, _server_formats=False)
2018.5.70 by Robert Collins
Only try to get real repositories when an operation requires them.
125
3786.2.3 by Andrew Bennetts
Remove duplicated 'call & translate errors' code in bzrlib.remote.
126
    def _translate_error(self, err, **context):
127
        _translate_error(err, bzrdir=self, **context)
128
4044.1.3 by Robert Collins
Create a one-shot cache of the result of RemoteBzrDir.create_branch, eliminating 3 round trips for nonstacked branches and 5 for stacked.
129
    def break_lock(self):
130
        # Prevent aliasing problems in the next_open_branch_result cache.
131
        # See create_branch for rationale.
132
        self._next_open_branch_result = None
133
        return BzrDir.break_lock(self)
134
3650.3.13 by Aaron Bentley
Make cloning_metadir handle stacking requirements
135
    def cloning_metadir(self, stacked=False):
3242.3.28 by Aaron Bentley
Use repository acquisition policy for sprouting
136
        self._ensure_real()
3650.3.13 by Aaron Bentley
Make cloning_metadir handle stacking requirements
137
        return self._real_bzrdir.cloning_metadir(stacked)
3242.3.28 by Aaron Bentley
Use repository acquisition policy for sprouting
138
1752.2.39 by Martin Pool
[broken] implement upgrade apis on remote bzrdirs
139
    def create_repository(self, shared=False):
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
140
        # as per meta1 formats - just delegate to the format object which may
141
        # be parameterised.
142
        result = self._format.repository_format.initialize(self, shared)
143
        if not isinstance(result, RemoteRepository):
144
            return self.open_repository()
145
        else:
146
            return result
1752.2.50 by Andrew Bennetts
Implement RemoteBzrDir.create_{branch,workingtree}
147
2796.2.19 by Aaron Bentley
Support reconfigure --lightweight-checkout
148
    def destroy_repository(self):
149
        """See BzrDir.destroy_repository"""
150
        self._ensure_real()
151
        self._real_bzrdir.destroy_repository()
152
1752.2.50 by Andrew Bennetts
Implement RemoteBzrDir.create_{branch,workingtree}
153
    def create_branch(self):
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
154
        # as per meta1 formats - just delegate to the format object which may
155
        # be parameterised.
156
        real_branch = self._format.get_branch_format().initialize(self)
157
        if not isinstance(real_branch, RemoteBranch):
4044.1.3 by Robert Collins
Create a one-shot cache of the result of RemoteBzrDir.create_branch, eliminating 3 round trips for nonstacked branches and 5 for stacked.
158
            result = RemoteBranch(self, self.find_repository(), real_branch)
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
159
        else:
4044.1.3 by Robert Collins
Create a one-shot cache of the result of RemoteBzrDir.create_branch, eliminating 3 round trips for nonstacked branches and 5 for stacked.
160
            result = real_branch
161
        # BzrDir.clone_on_transport() uses the result of create_branch but does
162
        # not return it to its callers; we save approximately 8% of our round
163
        # trips by handing the branch we created back to the first caller to
164
        # open_branch rather than probing anew. Long term we need a API in
165
        # bzrdir that doesn't discard result objects (like result_branch).
166
        # RBC 20090225
167
        self._next_open_branch_result = result
168
        return result
1752.2.50 by Andrew Bennetts
Implement RemoteBzrDir.create_{branch,workingtree}
169
2796.2.6 by Aaron Bentley
Implement destroy_branch
170
    def destroy_branch(self):
2796.2.16 by Aaron Bentley
Documentation updates from review
171
        """See BzrDir.destroy_branch"""
2796.2.6 by Aaron Bentley
Implement destroy_branch
172
        self._ensure_real()
173
        self._real_bzrdir.destroy_branch()
4044.1.3 by Robert Collins
Create a one-shot cache of the result of RemoteBzrDir.create_branch, eliminating 3 round trips for nonstacked branches and 5 for stacked.
174
        self._next_open_branch_result = None
2796.2.6 by Aaron Bentley
Implement destroy_branch
175
2955.5.3 by Vincent Ladeuil
Fix second unwanted connection by providing the right branch to create_checkout.
176
    def create_workingtree(self, revision_id=None, from_branch=None):
2018.5.174 by Andrew Bennetts
Various nits discovered by pyflakes.
177
        raise errors.NotLocalUrl(self.transport.base)
1752.2.39 by Martin Pool
[broken] implement upgrade apis on remote bzrdirs
178
2018.5.124 by Robert Collins
Fix test_format_initialize_find_open by delegating Branch formt lookup to the BzrDir, where it should have stayed from the start.
179
    def find_branch_format(self):
180
        """Find the branch 'format' for this bzrdir.
181
182
        This might be a synthetic object for e.g. RemoteBranch and SVN.
183
        """
184
        b = self.open_branch()
185
        return b._format
186
2018.5.132 by Robert Collins
Make all BzrDir implementation tests pass on RemoteBzrDir - fix some things, and remove the incomplete_with_basis tests as cruft.
187
    def get_branch_reference(self):
188
        """See BzrDir.get_branch_reference()."""
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
189
        path = self._path_for_remote_call(self._client)
3786.2.3 by Andrew Bennetts
Remove duplicated 'call & translate errors' code in bzrlib.remote.
190
        response = self._call('BzrDir.open_branch', path)
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
191
        if response[0] == 'ok':
192
            if response[1] == '':
193
                # branch at this location.
2018.5.132 by Robert Collins
Make all BzrDir implementation tests pass on RemoteBzrDir - fix some things, and remove the incomplete_with_basis tests as cruft.
194
                return None
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
195
            else:
196
                # a branch reference, use the existing BranchReference logic.
2018.5.132 by Robert Collins
Make all BzrDir implementation tests pass on RemoteBzrDir - fix some things, and remove the incomplete_with_basis tests as cruft.
197
                return response[1]
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
198
        else:
2555.1.1 by Martin Pool
Remove use of 'assert False' to raise an exception unconditionally
199
            raise errors.UnexpectedSmartServerResponse(response)
2018.5.132 by Robert Collins
Make all BzrDir implementation tests pass on RemoteBzrDir - fix some things, and remove the incomplete_with_basis tests as cruft.
200
3211.4.1 by Robert Collins
* ``RemoteBzrDir._get_tree_branch`` no longer triggers ``_ensure_real``,
201
    def _get_tree_branch(self):
202
        """See BzrDir._get_tree_branch()."""
203
        return None, self.open_branch()
204
2018.5.132 by Robert Collins
Make all BzrDir implementation tests pass on RemoteBzrDir - fix some things, and remove the incomplete_with_basis tests as cruft.
205
    def open_branch(self, _unsupported=False):
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
206
        if _unsupported:
207
            raise NotImplementedError('unsupported flag support not implemented yet.')
4044.1.3 by Robert Collins
Create a one-shot cache of the result of RemoteBzrDir.create_branch, eliminating 3 round trips for nonstacked branches and 5 for stacked.
208
        if self._next_open_branch_result is not None:
209
            # See create_branch for details.
210
            result = self._next_open_branch_result
211
            self._next_open_branch_result = None
212
            return result
2018.5.132 by Robert Collins
Make all BzrDir implementation tests pass on RemoteBzrDir - fix some things, and remove the incomplete_with_basis tests as cruft.
213
        reference_url = self.get_branch_reference()
214
        if reference_url is None:
215
            # branch at this location.
216
            return RemoteBranch(self, self.find_repository())
217
        else:
218
            # a branch reference, use the existing BranchReference logic.
219
            format = BranchReferenceFormat()
220
            return format.open(self, _found=True, location=reference_url)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
221
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
222
    def open_repository(self):
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
223
        path = self._path_for_remote_call(self._client)
3221.3.3 by Robert Collins
* Hook up the new remote method ``RemoteBzrDir.find_repositoryV2`` so
224
        verb = 'BzrDir.find_repositoryV2'
3297.3.3 by Andrew Bennetts
SmartClientRequestProtocol*.read_response_tuple can now raise UnknownSmartMethod. Callers no longer need to do their own ad hoc unknown smart method error detection.
225
        try:
3786.2.3 by Andrew Bennetts
Remove duplicated 'call & translate errors' code in bzrlib.remote.
226
            response = self._call(verb, path)
227
        except errors.UnknownSmartMethod:
228
            verb = 'BzrDir.find_repository'
229
            response = self._call(verb, path)
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
230
        if response[0] != 'ok':
231
            raise errors.UnexpectedSmartServerResponse(response)
3221.3.3 by Robert Collins
* Hook up the new remote method ``RemoteBzrDir.find_repositoryV2`` so
232
        if verb == 'BzrDir.find_repository':
233
            # servers that don't support the V2 method don't support external
234
            # references either.
235
            response = response + ('no', )
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
236
        if not (len(response) == 5):
237
            raise SmartProtocolError('incorrect response length %s' % (response,))
2018.5.34 by Robert Collins
Get test_remote.BasicRemoteObjectTests.test_open_remote_branch passing by implementing a remote method BzrDir.find_repository.
238
        if response[1] == '':
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
239
            format = RemoteRepositoryFormat()
2018.5.166 by Andrew Bennetts
Small changes in response to Aaron's review.
240
            format.rich_root_data = (response[2] == 'yes')
241
            format.supports_tree_reference = (response[3] == 'yes')
3221.3.1 by Robert Collins
* Repository formats have a new supported-feature attribute
242
            # No wire format to check this yet.
3221.3.3 by Robert Collins
* Hook up the new remote method ``RemoteBzrDir.find_repositoryV2`` so
243
            format.supports_external_lookups = (response[4] == 'yes')
3221.15.10 by Robert Collins
Add test that we can stack on a smart server from Jonathan Lange.
244
            # Used to support creating a real format instance when needed.
245
            format._creating_bzrdir = self
3990.5.1 by Andrew Bennetts
Add network_name() to RepositoryFormat.
246
            remote_repo = RemoteRepository(self, format)
247
            format._creating_repo = remote_repo
248
            return remote_repo
2018.5.34 by Robert Collins
Get test_remote.BasicRemoteObjectTests.test_open_remote_branch passing by implementing a remote method BzrDir.find_repository.
249
        else:
250
            raise errors.NoRepositoryPresent(self)
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
251
2018.5.138 by Robert Collins
Merge bzr.dev.
252
    def open_workingtree(self, recommend_upgrade=True):
2445.1.1 by Andrew Bennetts
Make RemoteBzrDir.open_workingtree raise NoWorkingTree rather than NotLocalUrl
253
        self._ensure_real()
254
        if self._real_bzrdir.has_workingtree():
255
            raise errors.NotLocalUrl(self.root_transport)
256
        else:
257
            raise errors.NoWorkingTree(self.root_transport.base)
1752.2.50 by Andrew Bennetts
Implement RemoteBzrDir.create_{branch,workingtree}
258
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
259
    def _path_for_remote_call(self, client):
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
260
        """Return the path to be used for this bzrdir in a remote call."""
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
261
        return client.remote_path_from_transport(self.root_transport)
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
262
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
263
    def get_branch_transport(self, branch_format):
2018.5.162 by Andrew Bennetts
Add some missing _ensure_real calls, and a missing import.
264
        self._ensure_real()
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
265
        return self._real_bzrdir.get_branch_transport(branch_format)
266
1752.2.43 by Andrew Bennetts
Fix get_{branch,repository,workingtree}_transport.
267
    def get_repository_transport(self, repository_format):
2018.5.162 by Andrew Bennetts
Add some missing _ensure_real calls, and a missing import.
268
        self._ensure_real()
1752.2.43 by Andrew Bennetts
Fix get_{branch,repository,workingtree}_transport.
269
        return self._real_bzrdir.get_repository_transport(repository_format)
270
271
    def get_workingtree_transport(self, workingtree_format):
2018.5.162 by Andrew Bennetts
Add some missing _ensure_real calls, and a missing import.
272
        self._ensure_real()
1752.2.43 by Andrew Bennetts
Fix get_{branch,repository,workingtree}_transport.
273
        return self._real_bzrdir.get_workingtree_transport(workingtree_format)
274
1752.2.39 by Martin Pool
[broken] implement upgrade apis on remote bzrdirs
275
    def can_convert_format(self):
276
        """Upgrading of remote bzrdirs is not supported yet."""
277
        return False
278
279
    def needs_format_conversion(self, format=None):
280
        """Upgrading of remote bzrdirs is not supported yet."""
3943.2.5 by Martin Pool
deprecate needs_format_conversion(format=None)
281
        if format is None:
282
            symbol_versioning.warn(symbol_versioning.deprecated_in((1, 13, 0))
283
                % 'needs_format_conversion(format=None)')
1752.2.39 by Martin Pool
[broken] implement upgrade apis on remote bzrdirs
284
        return False
285
3242.3.37 by Aaron Bentley
Updates from reviews
286
    def clone(self, url, revision_id=None, force_new_repo=False,
287
              preserve_stacking=False):
2018.5.94 by Andrew Bennetts
Various small changes in aid of making tests pass (including deleting one invalid test).
288
        self._ensure_real()
289
        return self._real_bzrdir.clone(url, revision_id=revision_id,
3242.3.37 by Aaron Bentley
Updates from reviews
290
            force_new_repo=force_new_repo, preserve_stacking=preserve_stacking)
2018.5.94 by Andrew Bennetts
Various small changes in aid of making tests pass (including deleting one invalid test).
291
3567.1.3 by Michael Hudson
fix problem
292
    def get_config(self):
293
        self._ensure_real()
294
        return self._real_bzrdir.get_config()
295
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
296
1752.2.52 by Andrew Bennetts
Flesh out more Remote* methods needed to open and initialise remote branches/trees/repositories.
297
class RemoteRepositoryFormat(repository.RepositoryFormat):
2018.5.159 by Andrew Bennetts
Rename SmartClient to _SmartClient.
298
    """Format for repositories accessed over a _SmartClient.
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
299
1752.2.50 by Andrew Bennetts
Implement RemoteBzrDir.create_{branch,workingtree}
300
    Instances of this repository are represented by RemoteRepository
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
301
    instances.
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
302
3128.1.3 by Vincent Ladeuil
Since we are there s/parameteris.*/parameteriz&/.
303
    The RemoteRepositoryFormat is parameterized during construction
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
304
    to reflect the capabilities of the real, remote format. Specifically
2018.5.138 by Robert Collins
Merge bzr.dev.
305
    the attributes rich_root_data and supports_tree_reference are set
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
306
    on a per instance basis, and are not set (and should not be) at
307
    the class level.
3990.5.3 by Robert Collins
Docs and polish on RepositoryFormat.network_name.
308
4032.1.1 by John Arbash Meinel
Merge the removal of all trailing whitespace, and resolve conflicts.
309
    :ivar _custom_format: If set, a specific concrete repository format that
3990.5.3 by Robert Collins
Docs and polish on RepositoryFormat.network_name.
310
        will be used when initializing a repository with this
311
        RemoteRepositoryFormat.
312
    :ivar _creating_repo: If set, the repository object that this
313
        RemoteRepositoryFormat was created for: it can be called into
3990.5.4 by Robert Collins
Review feedback.
314
        to obtain data like the network name.
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
315
    """
316
3543.1.2 by Michael Hudson
the two character fix
317
    _matchingbzrdir = RemoteBzrDirFormat()
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
318
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
319
    def __init__(self):
320
        repository.RepositoryFormat.__init__(self)
321
        self._custom_format = None
4017.3.2 by Robert Collins
Reduce the number of round trips required to create a repository over the network.
322
        self._network_name = None
4017.3.3 by Robert Collins
Review feedback - make RemoteRepository.initialize use helpers, and version-lock the new method to not attempt the method on older servers.
323
        self._creating_bzrdir = None
324
325
    def _vfs_initialize(self, a_bzrdir, shared):
326
        """Helper for common code in initialize."""
327
        if self._custom_format:
328
            # Custom format requested
329
            result = self._custom_format.initialize(a_bzrdir, shared=shared)
330
        elif self._creating_bzrdir is not None:
331
            # Use the format that the repository we were created to back
332
            # has.
333
            prior_repo = self._creating_bzrdir.open_repository()
334
            prior_repo._ensure_real()
335
            result = prior_repo._real_repository._format.initialize(
336
                a_bzrdir, shared=shared)
337
        else:
338
            # assume that a_bzr is a RemoteBzrDir but the smart server didn't
339
            # support remote initialization.
340
            # We delegate to a real object at this point (as RemoteBzrDir
341
            # delegate to the repository format which would lead to infinite
342
            # recursion if we just called a_bzrdir.create_repository.
343
            a_bzrdir._ensure_real()
344
            result = a_bzrdir._real_bzrdir.create_repository(shared=shared)
345
        if not isinstance(result, RemoteRepository):
346
            return self.open(a_bzrdir)
347
        else:
348
            return result
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
349
1752.2.52 by Andrew Bennetts
Flesh out more Remote* methods needed to open and initialise remote branches/trees/repositories.
350
    def initialize(self, a_bzrdir, shared=False):
4017.3.2 by Robert Collins
Reduce the number of round trips required to create a repository over the network.
351
        # Being asked to create on a non RemoteBzrDir:
352
        if not isinstance(a_bzrdir, RemoteBzrDir):
4017.3.3 by Robert Collins
Review feedback - make RemoteRepository.initialize use helpers, and version-lock the new method to not attempt the method on older servers.
353
            return self._vfs_initialize(a_bzrdir, shared)
354
        medium = a_bzrdir._client._medium
355
        if medium._is_remote_before((1, 13)):
356
            return self._vfs_initialize(a_bzrdir, shared)
4017.3.2 by Robert Collins
Reduce the number of round trips required to create a repository over the network.
357
        # Creating on a remote bzr dir.
358
        # 1) get the network name to use.
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
359
        if self._custom_format:
4017.3.2 by Robert Collins
Reduce the number of round trips required to create a repository over the network.
360
            network_name = self._custom_format.network_name()
361
        else:
362
            # Select the current bzrlib default and ask for that.
363
            reference_bzrdir_format = bzrdir.format_registry.get('default')()
364
            reference_format = reference_bzrdir_format.repository_format
365
            network_name = reference_format.network_name()
366
        # 2) try direct creation via RPC
367
        path = a_bzrdir._path_for_remote_call(a_bzrdir._client)
368
        verb = 'BzrDir.create_repository'
369
        if shared:
370
            shared_str = 'True'
371
        else:
372
            shared_str = 'False'
373
        try:
374
            response = a_bzrdir._call(verb, path, network_name, shared_str)
375
        except errors.UnknownSmartMethod:
4017.3.3 by Robert Collins
Review feedback - make RemoteRepository.initialize use helpers, and version-lock the new method to not attempt the method on older servers.
376
            # Fallback - use vfs methods
377
            return self._vfs_initialize(a_bzrdir, shared)
4017.3.2 by Robert Collins
Reduce the number of round trips required to create a repository over the network.
378
        else:
379
            # Turn the response into a RemoteRepository object.
4032.3.2 by Robert Collins
Create and use a RPC call to create branches on bzr servers rather than using VFS calls.
380
            format = response_tuple_to_repo_format(response[1:])
4017.3.2 by Robert Collins
Reduce the number of round trips required to create a repository over the network.
381
            # Used to support creating a real format instance when needed.
382
            format._creating_bzrdir = a_bzrdir
383
            remote_repo = RemoteRepository(a_bzrdir, format)
384
            format._creating_repo = remote_repo
385
            return remote_repo
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
386
1752.2.52 by Andrew Bennetts
Flesh out more Remote* methods needed to open and initialise remote branches/trees/repositories.
387
    def open(self, a_bzrdir):
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
388
        if not isinstance(a_bzrdir, RemoteBzrDir):
389
            raise AssertionError('%r is not a RemoteBzrDir' % (a_bzrdir,))
1752.2.72 by Andrew Bennetts
Make Remote* classes in remote.py more consistent and remove some dead code.
390
        return a_bzrdir.open_repository()
1752.2.52 by Andrew Bennetts
Flesh out more Remote* methods needed to open and initialise remote branches/trees/repositories.
391
392
    def get_format_description(self):
393
        return 'bzr remote repository'
394
395
    def __eq__(self, other):
1752.2.87 by Andrew Bennetts
Make tests pass.
396
        return self.__class__ == other.__class__
397
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
398
    def check_conversion_target(self, target_format):
399
        if self.rich_root_data and not target_format.rich_root_data:
400
            raise errors.BadConversionTarget(
401
                'Does not support rich root data.', target_format)
2018.5.138 by Robert Collins
Merge bzr.dev.
402
        if (self.supports_tree_reference and
403
            not getattr(target_format, 'supports_tree_reference', False)):
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
404
            raise errors.BadConversionTarget(
405
                'Does not support nested trees', target_format)
1752.2.52 by Andrew Bennetts
Flesh out more Remote* methods needed to open and initialise remote branches/trees/repositories.
406
3990.5.1 by Andrew Bennetts
Add network_name() to RepositoryFormat.
407
    def network_name(self):
4017.3.2 by Robert Collins
Reduce the number of round trips required to create a repository over the network.
408
        if self._network_name:
409
            return self._network_name
3990.5.1 by Andrew Bennetts
Add network_name() to RepositoryFormat.
410
        self._creating_repo._ensure_real()
411
        return self._creating_repo._real_repository._format.network_name()
412
4022.1.1 by Robert Collins
Refactoring of fetch to have a sender and sink component enabling splitting the logic over a network stream. (Robert Collins, Andrew Bennetts)
413
    @property
414
    def _serializer(self):
4032.3.2 by Robert Collins
Create and use a RPC call to create branches on bzr servers rather than using VFS calls.
415
        if self._custom_format is not None:
416
            return self._custom_format._serializer
417
        elif self._network_name is not None:
418
            self._custom_format = repository.network_format_registry.get(
419
                self._network_name)
420
            return self._custom_format._serializer
421
        else:
422
            # We should only be getting asked for the serializer for
423
            # RemoteRepositoryFormat objects when the RemoteRepositoryFormat object
424
            # is a concrete instance for a RemoteRepository. In this case we know
425
            # the creating_repo and can use it to supply the serializer.
426
            self._creating_repo._ensure_real()
427
            return self._creating_repo._real_repository._format._serializer
4022.1.1 by Robert Collins
Refactoring of fetch to have a sender and sink component enabling splitting the logic over a network stream. (Robert Collins, Andrew Bennetts)
428
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
429
3786.2.3 by Andrew Bennetts
Remove duplicated 'call & translate errors' code in bzrlib.remote.
430
class RemoteRepository(_RpcHelper):
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
431
    """Repository accessed over rpc.
432
2018.5.163 by Andrew Bennetts
Deal with various review comments from Robert.
433
    For the moment most operations are performed using local transport-backed
434
    Repository objects.
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
435
    """
436
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
437
    def __init__(self, remote_bzrdir, format, real_repository=None, _client=None):
2018.5.34 by Robert Collins
Get test_remote.BasicRemoteObjectTests.test_open_remote_branch passing by implementing a remote method BzrDir.find_repository.
438
        """Create a RemoteRepository instance.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
439
2018.5.34 by Robert Collins
Get test_remote.BasicRemoteObjectTests.test_open_remote_branch passing by implementing a remote method BzrDir.find_repository.
440
        :param remote_bzrdir: The bzrdir hosting this repository.
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
441
        :param format: The RemoteFormat object to use.
2018.5.34 by Robert Collins
Get test_remote.BasicRemoteObjectTests.test_open_remote_branch passing by implementing a remote method BzrDir.find_repository.
442
        :param real_repository: If not None, a local implementation of the
443
            repository logic for the repository, usually accessing the data
444
            via the VFS.
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
445
        :param _client: Private testing parameter - override the smart client
446
            to be used by the repository.
2018.5.34 by Robert Collins
Get test_remote.BasicRemoteObjectTests.test_open_remote_branch passing by implementing a remote method BzrDir.find_repository.
447
        """
448
        if real_repository:
2018.5.36 by Andrew Bennetts
Fix typo, and clean up some ununsed import warnings from pyflakes at the same time.
449
            self._real_repository = real_repository
2018.5.70 by Robert Collins
Only try to get real repositories when an operation requires them.
450
        else:
451
            self._real_repository = None
1752.2.50 by Andrew Bennetts
Implement RemoteBzrDir.create_{branch,workingtree}
452
        self.bzrdir = remote_bzrdir
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
453
        if _client is None:
3313.2.1 by Andrew Bennetts
Change _SmartClient's API to accept a medium and a base, rather than a _SharedConnection.
454
            self._client = remote_bzrdir._client
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
455
        else:
456
            self._client = _client
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
457
        self._format = format
2018.5.75 by Andrew Bennetts
Add Repository.{dont_,}leave_lock_in_place.
458
        self._lock_mode = None
459
        self._lock_token = None
460
        self._lock_count = 0
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
461
        self._leave_lock = False
3835.1.12 by Aaron Bentley
Unify CachingExtraParentsProvider and CachingParentsProvider.
462
        self._unstacked_provider = graph.CachingParentsProvider(
3896.1.1 by Andrew Bennetts
Remove broken debugging cruft, and some unused imports.
463
            get_parent_map=self._get_parent_map_rpc)
3835.1.12 by Aaron Bentley
Unify CachingExtraParentsProvider and CachingParentsProvider.
464
        self._unstacked_provider.disable_cache()
2951.1.10 by Robert Collins
Peer review feedback with Ian.
465
        # For tests:
466
        # These depend on the actual remote format, so force them off for
467
        # maximum compatibility. XXX: In future these should depend on the
468
        # remote repository instance, but this is irrelevant until we perform
469
        # reconcile via an RPC call.
2951.1.5 by Robert Collins
Some work towards including the correct changes for TREE_ROOT in check parameterised tests.
470
        self._reconcile_does_inventory_gc = False
471
        self._reconcile_fixes_text_parents = False
2951.1.3 by Robert Collins
Partial support for native reconcile with packs.
472
        self._reconcile_backsup_inventory = False
2592.4.5 by Martin Pool
Add Repository.base on all repositories.
473
        self.base = self.bzrdir.transport.base
3221.12.1 by Robert Collins
Backport development1 format (stackable packs) to before-shallow-branches.
474
        # Additional places to query for data.
475
        self._fallback_repositories = []
2592.4.5 by Martin Pool
Add Repository.base on all repositories.
476
477
    def __str__(self):
478
        return "%s(%s)" % (self.__class__.__name__, self.base)
479
480
    __repr__ = __str__
1752.2.52 by Andrew Bennetts
Flesh out more Remote* methods needed to open and initialise remote branches/trees/repositories.
481
3825.4.1 by Andrew Bennetts
Add suppress_errors to abort_write_group.
482
    def abort_write_group(self, suppress_errors=False):
2617.6.7 by Robert Collins
More review feedback.
483
        """Complete a write group on the decorated repository.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
484
2617.6.2 by Robert Collins
Add abort_write_group and wire write_groups into fetch and commit.
485
        Smart methods peform operations in a single step so this api
2617.6.6 by Robert Collins
Some review feedback.
486
        is not really applicable except as a compatibility thunk
2617.6.2 by Robert Collins
Add abort_write_group and wire write_groups into fetch and commit.
487
        for older plugins that don't use e.g. the CommitBuilder
488
        facility.
3825.4.6 by Andrew Bennetts
Document the suppress_errors flag in the docstring.
489
490
        :param suppress_errors: see Repository.abort_write_group.
2617.6.2 by Robert Collins
Add abort_write_group and wire write_groups into fetch and commit.
491
        """
492
        self._ensure_real()
3825.4.1 by Andrew Bennetts
Add suppress_errors to abort_write_group.
493
        return self._real_repository.abort_write_group(
494
            suppress_errors=suppress_errors)
2617.6.2 by Robert Collins
Add abort_write_group and wire write_groups into fetch and commit.
495
496
    def commit_write_group(self):
2617.6.7 by Robert Collins
More review feedback.
497
        """Complete a write group on the decorated repository.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
498
2617.6.2 by Robert Collins
Add abort_write_group and wire write_groups into fetch and commit.
499
        Smart methods peform operations in a single step so this api
2617.6.6 by Robert Collins
Some review feedback.
500
        is not really applicable except as a compatibility thunk
2617.6.2 by Robert Collins
Add abort_write_group and wire write_groups into fetch and commit.
501
        for older plugins that don't use e.g. the CommitBuilder
502
        facility.
503
        """
504
        self._ensure_real()
505
        return self._real_repository.commit_write_group()
2617.6.1 by Robert Collins
* New method on Repository - ``start_write_group``, ``end_write_group``
506
4002.1.1 by Andrew Bennetts
Implement suspend_write_group/resume_write_group.
507
    def resume_write_group(self, tokens):
508
        self._ensure_real()
509
        return self._real_repository.resume_write_group(tokens)
510
511
    def suspend_write_group(self):
512
        self._ensure_real()
513
        return self._real_repository.suspend_write_group()
514
2018.5.70 by Robert Collins
Only try to get real repositories when an operation requires them.
515
    def _ensure_real(self):
516
        """Ensure that there is a _real_repository set.
517
2018.5.163 by Andrew Bennetts
Deal with various review comments from Robert.
518
        Used before calls to self._real_repository.
2018.5.70 by Robert Collins
Only try to get real repositories when an operation requires them.
519
        """
3692.1.3 by Andrew Bennetts
Delete some cruft (like the _ensure_real call in RemoteBranch.lock_write), improve some comments, and wrap some long lines.
520
        if self._real_repository is None:
2018.5.70 by Robert Collins
Only try to get real repositories when an operation requires them.
521
            self.bzrdir._ensure_real()
3692.1.3 by Andrew Bennetts
Delete some cruft (like the _ensure_real call in RemoteBranch.lock_write), improve some comments, and wrap some long lines.
522
            self._set_real_repository(
523
                self.bzrdir._real_bzrdir.open_repository())
2018.5.70 by Robert Collins
Only try to get real repositories when an operation requires them.
524
3533.3.1 by Andrew Bennetts
Remove duplication of error translation in bzrlib/remote.py.
525
    def _translate_error(self, err, **context):
526
        self.bzrdir._translate_error(err, repository=self, **context)
527
2988.1.2 by Robert Collins
New Repository API find_text_key_references for use by reconcile and check.
528
    def find_text_key_references(self):
529
        """Find the text key references within the repository.
530
531
        :return: a dictionary mapping (file_id, revision_id) tuples to altered file-ids to an iterable of
532
        revision_ids. Each altered file-ids has the exact revision_ids that
533
        altered it listed explicitly.
534
        :return: A dictionary mapping text keys ((fileid, revision_id) tuples)
535
            to whether they were referred to by the inventory of the
536
            revision_id that they contain. The inventory texts from all present
537
            revision ids are assessed to generate this report.
538
        """
539
        self._ensure_real()
540
        return self._real_repository.find_text_key_references()
541
2988.1.3 by Robert Collins
Add a new repositoy method _generate_text_key_index for use by reconcile/check.
542
    def _generate_text_key_index(self):
543
        """Generate a new text key index for the repository.
544
545
        This is an expensive function that will take considerable time to run.
546
547
        :return: A dict mapping (file_id, revision_id) tuples to a list of
548
            parents, also (file_id, revision_id) tuples.
549
        """
550
        self._ensure_real()
551
        return self._real_repository._generate_text_key_index()
552
3287.6.1 by Robert Collins
* ``VersionedFile.get_graph`` is deprecated, with no replacement method.
553
    @symbol_versioning.deprecated_method(symbol_versioning.one_four)
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
554
    def get_revision_graph(self, revision_id=None):
555
        """See Repository.get_revision_graph()."""
3287.6.4 by Robert Collins
Fix up deprecation warnings for get_revision_graph.
556
        return self._get_revision_graph(revision_id)
557
558
    def _get_revision_graph(self, revision_id):
559
        """Private method for using with old (< 1.2) servers to fallback."""
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
560
        if revision_id is None:
561
            revision_id = ''
2948.3.1 by John Arbash Meinel
Fix bug #158333, make sure that Repository.fetch(self) is properly a no-op for all Repository implementations.
562
        elif revision.is_null(revision_id):
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
563
            return {}
564
565
        path = self.bzrdir._path_for_remote_call(self._client)
3786.2.3 by Andrew Bennetts
Remove duplicated 'call & translate errors' code in bzrlib.remote.
566
        response = self._call_expecting_body(
567
            'Repository.get_revision_graph', path, revision_id)
3245.4.58 by Andrew Bennetts
Unpack call_expecting_body's return value into variables, to avoid lots of ugly subscripting.
568
        response_tuple, response_handler = response
569
        if response_tuple[0] != 'ok':
570
            raise errors.UnexpectedSmartServerResponse(response_tuple)
571
        coded = response_handler.read_body_bytes()
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
572
        if coded == '':
573
            # no revisions in this repository!
574
            return {}
575
        lines = coded.split('\n')
576
        revision_graph = {}
577
        for line in lines:
578
            d = tuple(line.split())
579
            revision_graph[d[0]] = d[1:]
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
580
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
581
        return revision_graph
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
582
4022.1.1 by Robert Collins
Refactoring of fetch to have a sender and sink component enabling splitting the logic over a network stream. (Robert Collins, Andrew Bennetts)
583
    def _get_sink(self):
584
        """See Repository._get_sink()."""
4022.1.6 by Robert Collins
Cherrypick and polish the RemoteSink for streaming push.
585
        return RemoteStreamSink(self)
4022.1.1 by Robert Collins
Refactoring of fetch to have a sender and sink component enabling splitting the logic over a network stream. (Robert Collins, Andrew Bennetts)
586
2018.5.40 by Robert Collins
Implement a remote Repository.has_revision method.
587
    def has_revision(self, revision_id):
588
        """See Repository.has_revision()."""
3172.3.1 by Robert Collins
Repository has a new method ``has_revisions`` which signals the presence
589
        if revision_id == NULL_REVISION:
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
590
            # The null revision is always present.
591
            return True
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
592
        path = self.bzrdir._path_for_remote_call(self._client)
3786.2.3 by Andrew Bennetts
Remove duplicated 'call & translate errors' code in bzrlib.remote.
593
        response = self._call('Repository.has_revision', path, revision_id)
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
594
        if response[0] not in ('yes', 'no'):
3245.4.58 by Andrew Bennetts
Unpack call_expecting_body's return value into variables, to avoid lots of ugly subscripting.
595
            raise errors.UnexpectedSmartServerResponse(response)
3691.2.2 by Martin Pool
Fix some problems in access to stacked repositories over hpss (#261315)
596
        if response[0] == 'yes':
597
            return True
598
        for fallback_repo in self._fallback_repositories:
599
            if fallback_repo.has_revision(revision_id):
600
                return True
601
        return False
2018.5.40 by Robert Collins
Implement a remote Repository.has_revision method.
602
3172.3.1 by Robert Collins
Repository has a new method ``has_revisions`` which signals the presence
603
    def has_revisions(self, revision_ids):
604
        """See Repository.has_revisions()."""
3691.2.2 by Martin Pool
Fix some problems in access to stacked repositories over hpss (#261315)
605
        # FIXME: This does many roundtrips, particularly when there are
606
        # fallback repositories.  -- mbp 20080905
3172.3.1 by Robert Collins
Repository has a new method ``has_revisions`` which signals the presence
607
        result = set()
608
        for revision_id in revision_ids:
609
            if self.has_revision(revision_id):
610
                result.add(revision_id)
611
        return result
612
2617.6.9 by Robert Collins
Merge bzr.dev.
613
    def has_same_location(self, other):
2592.3.162 by Robert Collins
Remove some arbitrary differences from bzr.dev.
614
        return (self.__class__ == other.__class__ and
615
                self.bzrdir.transport.base == other.bzrdir.transport.base)
3835.1.1 by Aaron Bentley
Stack get_parent_map on fallback repos
616
2490.2.21 by Aaron Bentley
Rename graph to deprecated_graph
617
    def get_graph(self, other_repository=None):
618
        """Return the graph for this repository format"""
3835.1.17 by Aaron Bentley
Fix stacking bug
619
        parents_provider = self._make_parents_provider(other_repository)
3441.5.24 by Andrew Bennetts
Remove RemoteGraph experiment.
620
        return graph.Graph(parents_provider)
2490.2.5 by Aaron Bentley
Use GraphWalker.unique_ancestor to determine merge base
621
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
622
    def gather_stats(self, revid=None, committers=None):
2018.5.62 by Robert Collins
Stub out RemoteRepository.gather_stats while its implemented in parallel.
623
        """See Repository.gather_stats()."""
2018.10.3 by v.ladeuil+lp at free
more tests for gather_stats
624
        path = self.bzrdir._path_for_remote_call(self._client)
2948.3.4 by John Arbash Meinel
Repository.gather_stats() validly can get None for the revid.
625
        # revid can be None to indicate no revisions, not just NULL_REVISION
626
        if revid is None or revision.is_null(revid):
2018.10.3 by v.ladeuil+lp at free
more tests for gather_stats
627
            fmt_revid = ''
628
        else:
2018.5.83 by Andrew Bennetts
Fix some test failures caused by the switch from unicode to UTF-8-encoded strs for revision IDs.
629
            fmt_revid = revid
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
630
        if committers is None or not committers:
2018.10.3 by v.ladeuil+lp at free
more tests for gather_stats
631
            fmt_committers = 'no'
632
        else:
633
            fmt_committers = 'yes'
3786.2.3 by Andrew Bennetts
Remove duplicated 'call & translate errors' code in bzrlib.remote.
634
        response_tuple, response_handler = self._call_expecting_body(
2018.5.153 by Andrew Bennetts
Rename call2 to call_expecting_body, and other small changes prompted by review.
635
            'Repository.gather_stats', path, fmt_revid, fmt_committers)
3245.4.58 by Andrew Bennetts
Unpack call_expecting_body's return value into variables, to avoid lots of ugly subscripting.
636
        if response_tuple[0] != 'ok':
637
            raise errors.UnexpectedSmartServerResponse(response_tuple)
2018.10.3 by v.ladeuil+lp at free
more tests for gather_stats
638
3245.4.58 by Andrew Bennetts
Unpack call_expecting_body's return value into variables, to avoid lots of ugly subscripting.
639
        body = response_handler.read_body_bytes()
2018.10.3 by v.ladeuil+lp at free
more tests for gather_stats
640
        result = {}
641
        for line in body.split('\n'):
642
            if not line:
643
                continue
644
            key, val_text = line.split(':')
645
            if key in ('revisions', 'size', 'committers'):
646
                result[key] = int(val_text)
647
            elif key in ('firstrev', 'latestrev'):
648
                values = val_text.split(' ')[1:]
649
                result[key] = (float(values[0]), long(values[1]))
650
651
        return result
2018.5.62 by Robert Collins
Stub out RemoteRepository.gather_stats while its implemented in parallel.
652
3140.1.2 by Aaron Bentley
Add ability to find branches inside repositories
653
    def find_branches(self, using=False):
654
        """See Repository.find_branches()."""
655
        # should be an API call to the server.
656
        self._ensure_real()
657
        return self._real_repository.find_branches(using=using)
658
2018.5.60 by Robert Collins
More missing methods from RemoteBranch and RemoteRepository to let 'info' get further.
659
    def get_physical_lock_status(self):
660
        """See Repository.get_physical_lock_status()."""
3015.2.10 by Robert Collins
Fix regression due to other pack related fixes in tests with packs not-default.
661
        # should be an API call to the server.
662
        self._ensure_real()
663
        return self._real_repository.get_physical_lock_status()
2018.5.60 by Robert Collins
More missing methods from RemoteBranch and RemoteRepository to let 'info' get further.
664
2617.6.1 by Robert Collins
* New method on Repository - ``start_write_group``, ``end_write_group``
665
    def is_in_write_group(self):
666
        """Return True if there is an open write group.
667
668
        write groups are only applicable locally for the smart server..
669
        """
670
        if self._real_repository:
671
            return self._real_repository.is_in_write_group()
672
673
    def is_locked(self):
674
        return self._lock_count >= 1
675
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
676
    def is_shared(self):
677
        """See Repository.is_shared()."""
678
        path = self.bzrdir._path_for_remote_call(self._client)
3786.2.3 by Andrew Bennetts
Remove duplicated 'call & translate errors' code in bzrlib.remote.
679
        response = self._call('Repository.is_shared', path)
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
680
        if response[0] not in ('yes', 'no'):
681
            raise SmartProtocolError('unexpected response code %s' % (response,))
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
682
        return response[0] == 'yes'
683
2904.1.1 by Robert Collins
* New method ``bzrlib.repository.Repository.is_write_locked`` useful for
684
    def is_write_locked(self):
685
        return self._lock_mode == 'w'
686
2018.5.60 by Robert Collins
More missing methods from RemoteBranch and RemoteRepository to let 'info' get further.
687
    def lock_read(self):
688
        # wrong eventually - want a local lock cache context
2018.5.75 by Andrew Bennetts
Add Repository.{dont_,}leave_lock_in_place.
689
        if not self._lock_mode:
690
            self._lock_mode = 'r'
691
            self._lock_count = 1
3835.1.15 by Aaron Bentley
Allow miss caching to be disabled.
692
            self._unstacked_provider.enable_cache(cache_misses=False)
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
693
            if self._real_repository is not None:
694
                self._real_repository.lock_read()
2018.5.75 by Andrew Bennetts
Add Repository.{dont_,}leave_lock_in_place.
695
        else:
696
            self._lock_count += 1
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
697
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
698
    def _remote_lock_write(self, token):
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
699
        path = self.bzrdir._path_for_remote_call(self._client)
700
        if token is None:
701
            token = ''
3786.2.3 by Andrew Bennetts
Remove duplicated 'call & translate errors' code in bzrlib.remote.
702
        err_context = {'token': token}
703
        response = self._call('Repository.lock_write', path, token,
704
                              **err_context)
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
705
        if response[0] == 'ok':
706
            ok, token = response
707
            return token
708
        else:
2555.1.1 by Martin Pool
Remove use of 'assert False' to raise an exception unconditionally
709
            raise errors.UnexpectedSmartServerResponse(response)
2018.5.60 by Robert Collins
More missing methods from RemoteBranch and RemoteRepository to let 'info' get further.
710
3692.1.1 by Andrew Bennetts
Make RemoteBranch.lock_write lock the repository too.
711
    def lock_write(self, token=None, _skip_rpc=False):
2018.5.75 by Andrew Bennetts
Add Repository.{dont_,}leave_lock_in_place.
712
        if not self._lock_mode:
3692.1.1 by Andrew Bennetts
Make RemoteBranch.lock_write lock the repository too.
713
            if _skip_rpc:
714
                if self._lock_token is not None:
715
                    if token != self._lock_token:
3695.1.1 by Andrew Bennetts
Remove some unused imports and fix a couple of trivially broken raise statements.
716
                        raise errors.TokenMismatch(token, self._lock_token)
3692.1.1 by Andrew Bennetts
Make RemoteBranch.lock_write lock the repository too.
717
                self._lock_token = token
718
            else:
719
                self._lock_token = self._remote_lock_write(token)
3015.2.9 by Robert Collins
Handle repositories that do not allow remote locking, like pack repositories, in the client side remote server proxy objects.
720
            # if self._lock_token is None, then this is something like packs or
721
            # svn where we don't get to lock the repo, or a weave style repository
722
            # where we cannot lock it over the wire and attempts to do so will
723
            # fail.
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
724
            if self._real_repository is not None:
725
                self._real_repository.lock_write(token=self._lock_token)
726
            if token is not None:
727
                self._leave_lock = True
728
            else:
729
                self._leave_lock = False
2018.5.75 by Andrew Bennetts
Add Repository.{dont_,}leave_lock_in_place.
730
            self._lock_mode = 'w'
731
            self._lock_count = 1
3835.1.15 by Aaron Bentley
Allow miss caching to be disabled.
732
            self._unstacked_provider.enable_cache(cache_misses=False)
2018.5.75 by Andrew Bennetts
Add Repository.{dont_,}leave_lock_in_place.
733
        elif self._lock_mode == 'r':
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
734
            raise errors.ReadOnlyError(self)
2018.5.75 by Andrew Bennetts
Add Repository.{dont_,}leave_lock_in_place.
735
        else:
736
            self._lock_count += 1
3015.2.9 by Robert Collins
Handle repositories that do not allow remote locking, like pack repositories, in the client side remote server proxy objects.
737
        return self._lock_token or None
2018.5.75 by Andrew Bennetts
Add Repository.{dont_,}leave_lock_in_place.
738
739
    def leave_lock_in_place(self):
3015.2.9 by Robert Collins
Handle repositories that do not allow remote locking, like pack repositories, in the client side remote server proxy objects.
740
        if not self._lock_token:
741
            raise NotImplementedError(self.leave_lock_in_place)
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
742
        self._leave_lock = True
2018.5.75 by Andrew Bennetts
Add Repository.{dont_,}leave_lock_in_place.
743
744
    def dont_leave_lock_in_place(self):
3015.2.9 by Robert Collins
Handle repositories that do not allow remote locking, like pack repositories, in the client side remote server proxy objects.
745
        if not self._lock_token:
3015.2.15 by Robert Collins
Review feedback.
746
            raise NotImplementedError(self.dont_leave_lock_in_place)
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
747
        self._leave_lock = False
2018.5.75 by Andrew Bennetts
Add Repository.{dont_,}leave_lock_in_place.
748
749
    def _set_real_repository(self, repository):
750
        """Set the _real_repository for this repository.
751
752
        :param repository: The repository to fallback to for non-hpss
753
            implemented operations.
754
        """
3692.1.3 by Andrew Bennetts
Delete some cruft (like the _ensure_real call in RemoteBranch.lock_write), improve some comments, and wrap some long lines.
755
        if self._real_repository is not None:
756
            raise AssertionError('_real_repository is already set')
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
757
        if isinstance(repository, RemoteRepository):
758
            raise AssertionError()
2018.5.75 by Andrew Bennetts
Add Repository.{dont_,}leave_lock_in_place.
759
        self._real_repository = repository
3691.2.12 by Martin Pool
Add test for coping without Branch.get_stacked_on_url
760
        for fb in self._fallback_repositories:
761
            self._real_repository.add_fallback_repository(fb)
2018.5.75 by Andrew Bennetts
Add Repository.{dont_,}leave_lock_in_place.
762
        if self._lock_mode == 'w':
763
            # if we are already locked, the real repository must be able to
764
            # acquire the lock with our token.
765
            self._real_repository.lock_write(self._lock_token)
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
766
        elif self._lock_mode == 'r':
767
            self._real_repository.lock_read()
2018.5.60 by Robert Collins
More missing methods from RemoteBranch and RemoteRepository to let 'info' get further.
768
2617.6.1 by Robert Collins
* New method on Repository - ``start_write_group``, ``end_write_group``
769
    def start_write_group(self):
770
        """Start a write group on the decorated repository.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
771
2617.6.1 by Robert Collins
* New method on Repository - ``start_write_group``, ``end_write_group``
772
        Smart methods peform operations in a single step so this api
2617.6.6 by Robert Collins
Some review feedback.
773
        is not really applicable except as a compatibility thunk
2617.6.1 by Robert Collins
* New method on Repository - ``start_write_group``, ``end_write_group``
774
        for older plugins that don't use e.g. the CommitBuilder
775
        facility.
776
        """
777
        self._ensure_real()
778
        return self._real_repository.start_write_group()
779
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
780
    def _unlock(self, token):
781
        path = self.bzrdir._path_for_remote_call(self._client)
3015.2.9 by Robert Collins
Handle repositories that do not allow remote locking, like pack repositories, in the client side remote server proxy objects.
782
        if not token:
783
            # with no token the remote repository is not persistently locked.
784
            return
3786.2.3 by Andrew Bennetts
Remove duplicated 'call & translate errors' code in bzrlib.remote.
785
        err_context = {'token': token}
786
        response = self._call('Repository.unlock', path, token,
787
                              **err_context)
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
788
        if response == ('ok',):
789
            return
790
        else:
2555.1.1 by Martin Pool
Remove use of 'assert False' to raise an exception unconditionally
791
            raise errors.UnexpectedSmartServerResponse(response)
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
792
2018.5.60 by Robert Collins
More missing methods from RemoteBranch and RemoteRepository to let 'info' get further.
793
    def unlock(self):
4032.3.2 by Robert Collins
Create and use a RPC call to create branches on bzr servers rather than using VFS calls.
794
        if not self._lock_count:
795
            raise errors.LockNotHeld(self)
2018.5.75 by Andrew Bennetts
Add Repository.{dont_,}leave_lock_in_place.
796
        self._lock_count -= 1
2592.3.244 by Martin Pool
unlock while in a write group now aborts the write group, unlocks, and errors.
797
        if self._lock_count > 0:
798
            return
3835.1.8 by Aaron Bentley
Make UnstackedParentsProvider manage the cache
799
        self._unstacked_provider.disable_cache()
2592.3.244 by Martin Pool
unlock while in a write group now aborts the write group, unlocks, and errors.
800
        old_mode = self._lock_mode
801
        self._lock_mode = None
802
        try:
803
            # The real repository is responsible at present for raising an
804
            # exception if it's in an unfinished write group.  However, it
805
            # normally will *not* actually remove the lock from disk - that's
806
            # done by the server on receiving the Repository.unlock call.
807
            # This is just to let the _real_repository stay up to date.
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
808
            if self._real_repository is not None:
809
                self._real_repository.unlock()
2592.3.244 by Martin Pool
unlock while in a write group now aborts the write group, unlocks, and errors.
810
        finally:
811
            # The rpc-level lock should be released even if there was a
812
            # problem releasing the vfs-based lock.
813
            if old_mode == 'w':
2018.5.163 by Andrew Bennetts
Deal with various review comments from Robert.
814
                # Only write-locked repositories need to make a remote method
815
                # call to perfom the unlock.
2592.3.244 by Martin Pool
unlock while in a write group now aborts the write group, unlocks, and errors.
816
                old_token = self._lock_token
817
                self._lock_token = None
818
                if not self._leave_lock:
819
                    self._unlock(old_token)
2018.5.60 by Robert Collins
More missing methods from RemoteBranch and RemoteRepository to let 'info' get further.
820
821
    def break_lock(self):
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
822
        # should hand off to the network
2018.5.70 by Robert Collins
Only try to get real repositories when an operation requires them.
823
        self._ensure_real()
2018.5.60 by Robert Collins
More missing methods from RemoteBranch and RemoteRepository to let 'info' get further.
824
        return self._real_repository.break_lock()
825
2018.18.8 by Ian Clatworthy
Tarball proxy code & tests
826
    def _get_tarball(self, compression):
2814.10.2 by Andrew Bennetts
Make the fallback a little tidier.
827
        """Return a TemporaryFile containing a repository tarball.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
828
2814.10.2 by Andrew Bennetts
Make the fallback a little tidier.
829
        Returns None if the server does not support sending tarballs.
830
        """
2018.18.25 by Martin Pool
Repository.tarball fixes for python2.4
831
        import tempfile
2018.18.8 by Ian Clatworthy
Tarball proxy code & tests
832
        path = self.bzrdir._path_for_remote_call(self._client)
3297.3.3 by Andrew Bennetts
SmartClientRequestProtocol*.read_response_tuple can now raise UnknownSmartMethod. Callers no longer need to do their own ad hoc unknown smart method error detection.
833
        try:
3786.2.3 by Andrew Bennetts
Remove duplicated 'call & translate errors' code in bzrlib.remote.
834
            response, protocol = self._call_expecting_body(
3297.3.3 by Andrew Bennetts
SmartClientRequestProtocol*.read_response_tuple can now raise UnknownSmartMethod. Callers no longer need to do their own ad hoc unknown smart method error detection.
835
                'Repository.tarball', path, compression)
836
        except errors.UnknownSmartMethod:
837
            protocol.cancel_read_body()
838
            return None
2018.18.8 by Ian Clatworthy
Tarball proxy code & tests
839
        if response[0] == 'ok':
840
            # Extract the tarball and return it
2018.18.25 by Martin Pool
Repository.tarball fixes for python2.4
841
            t = tempfile.NamedTemporaryFile()
842
            # TODO: rpc layer should read directly into it...
843
            t.write(protocol.read_body_bytes())
844
            t.seek(0)
845
            return t
2814.10.1 by Andrew Bennetts
Cope gracefully if the server doesn't support the Repository.tarball smart request.
846
        raise errors.UnexpectedSmartServerResponse(response)
2018.18.8 by Ian Clatworthy
Tarball proxy code & tests
847
2440.1.1 by Martin Pool
Add new Repository.sprout,
848
    def sprout(self, to_bzrdir, revision_id=None):
849
        # TODO: Option to control what format is created?
3047.1.1 by Andrew Bennetts
Fix for bug 164626, add test that Repository.sprout preserves format.
850
        self._ensure_real()
3047.1.4 by Andrew Bennetts
Simplify RemoteRepository.sprout thanks to review comments.
851
        dest_repo = self._real_repository._format.initialize(to_bzrdir,
852
                                                             shared=False)
2535.3.17 by Andrew Bennetts
[broken] Closer to a working Repository.fetch_revisions smart request.
853
        dest_repo.fetch(self, revision_id=revision_id)
854
        return dest_repo
2440.1.1 by Martin Pool
Add new Repository.sprout,
855
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
856
    ### These methods are just thin shims to the VFS object for now.
857
858
    def revision_tree(self, revision_id):
859
        self._ensure_real()
860
        return self._real_repository.revision_tree(revision_id)
861
2520.4.113 by Aaron Bentley
Avoid peeking at Repository._serializer
862
    def get_serializer_format(self):
863
        self._ensure_real()
864
        return self._real_repository.get_serializer_format()
865
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
866
    def get_commit_builder(self, branch, parents, config, timestamp=None,
867
                           timezone=None, committer=None, revprops=None,
868
                           revision_id=None):
869
        # FIXME: It ought to be possible to call this without immediately
870
        # triggering _ensure_real.  For now it's the easiest thing to do.
871
        self._ensure_real()
3692.1.3 by Andrew Bennetts
Delete some cruft (like the _ensure_real call in RemoteBranch.lock_write), improve some comments, and wrap some long lines.
872
        real_repo = self._real_repository
873
        builder = real_repo.get_commit_builder(branch, parents,
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
874
                config, timestamp=timestamp, timezone=timezone,
875
                committer=committer, revprops=revprops, revision_id=revision_id)
876
        return builder
877
3221.12.1 by Robert Collins
Backport development1 format (stackable packs) to before-shallow-branches.
878
    def add_fallback_repository(self, repository):
879
        """Add a repository to use for looking up data not held locally.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
880
3221.12.1 by Robert Collins
Backport development1 format (stackable packs) to before-shallow-branches.
881
        :param repository: A repository.
882
        """
3691.2.11 by Martin Pool
More tests around RemoteBranch stacking.
883
        # XXX: At the moment the RemoteRepository will allow fallbacks
884
        # unconditionally - however, a _real_repository will usually exist,
885
        # and may raise an error if it's not accommodated by the underlying
886
        # format.  Eventually we should check when opening the repository
887
        # whether it's willing to allow them or not.
888
        #
3221.12.1 by Robert Collins
Backport development1 format (stackable packs) to before-shallow-branches.
889
        # We need to accumulate additional repositories here, to pass them in
890
        # on various RPC's.
4035.2.3 by Robert Collins
Fix trailing whitespace.
891
        #
3221.12.1 by Robert Collins
Backport development1 format (stackable packs) to before-shallow-branches.
892
        self._fallback_repositories.append(repository)
4035.2.2 by Robert Collins
Minor tweaks to fix failing tests.
893
        # If self._real_repository was parameterised already (e.g. because a
894
        # _real_branch had its get_stacked_on_url method called), then the
895
        # repository to be added may already be in the _real_repositories list.
4035.2.1 by Andrew Bennetts
Fix unnecessary get_parent_map calls after insert_stream during push.
896
        if self._real_repository is not None:
4035.2.2 by Robert Collins
Minor tweaks to fix failing tests.
897
            if repository not in self._real_repository._fallback_repositories:
898
                self._real_repository.add_fallback_repository(repository)
4035.2.1 by Andrew Bennetts
Fix unnecessary get_parent_map calls after insert_stream during push.
899
        else:
900
            # They are also seen by the fallback repository.  If it doesn't
901
            # exist yet they'll be added then.  This implicitly copies them.
902
            self._ensure_real()
3221.12.1 by Robert Collins
Backport development1 format (stackable packs) to before-shallow-branches.
903
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
904
    def add_inventory(self, revid, inv, parents):
905
        self._ensure_real()
906
        return self._real_repository.add_inventory(revid, inv, parents)
907
3879.2.2 by John Arbash Meinel
Rename add_inventory_delta to add_inventory_by_delta.
908
    def add_inventory_by_delta(self, basis_revision_id, delta, new_revision_id,
909
                               parents):
3775.2.1 by Robert Collins
Create bzrlib.repository.Repository.add_inventory_delta for adding inventories via deltas.
910
        self._ensure_real()
3879.2.2 by John Arbash Meinel
Rename add_inventory_delta to add_inventory_by_delta.
911
        return self._real_repository.add_inventory_by_delta(basis_revision_id,
3775.2.1 by Robert Collins
Create bzrlib.repository.Repository.add_inventory_delta for adding inventories via deltas.
912
            delta, new_revision_id, parents)
913
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
914
    def add_revision(self, rev_id, rev, inv=None, config=None):
915
        self._ensure_real()
916
        return self._real_repository.add_revision(
917
            rev_id, rev, inv=inv, config=config)
918
919
    @needs_read_lock
920
    def get_inventory(self, revision_id):
921
        self._ensure_real()
922
        return self._real_repository.get_inventory(revision_id)
923
3169.2.1 by Robert Collins
New method ``iter_inventories`` on Repository for access to many
924
    def iter_inventories(self, revision_ids):
925
        self._ensure_real()
926
        return self._real_repository.iter_inventories(revision_ids)
927
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
928
    @needs_read_lock
929
    def get_revision(self, revision_id):
930
        self._ensure_real()
931
        return self._real_repository.get_revision(revision_id)
932
933
    def get_transaction(self):
934
        self._ensure_real()
935
        return self._real_repository.get_transaction()
936
937
    @needs_read_lock
2018.5.138 by Robert Collins
Merge bzr.dev.
938
    def clone(self, a_bzrdir, revision_id=None):
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
939
        self._ensure_real()
2018.5.138 by Robert Collins
Merge bzr.dev.
940
        return self._real_repository.clone(a_bzrdir, revision_id=revision_id)
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
941
942
    def make_working_trees(self):
3349.1.1 by Aaron Bentley
Enable setting and getting make_working_trees for all repositories
943
        """See Repository.make_working_trees"""
944
        self._ensure_real()
945
        return self._real_repository.make_working_trees()
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
946
3184.1.9 by Robert Collins
* ``Repository.get_data_stream`` is now deprecated in favour of
947
    def revision_ids_to_search_result(self, result_set):
948
        """Convert a set of revision ids to a graph SearchResult."""
949
        result_parents = set()
950
        for parents in self.get_graph().get_parent_map(
951
            result_set).itervalues():
952
            result_parents.update(parents)
953
        included_keys = result_set.intersection(result_parents)
954
        start_keys = result_set.difference(included_keys)
955
        exclude_keys = result_parents.difference(result_set)
956
        result = graph.SearchResult(start_keys, exclude_keys,
957
            len(result_set), result_set)
958
        return result
959
960
    @needs_read_lock
961
    def search_missing_revision_ids(self, other, revision_id=None, find_ghosts=True):
962
        """Return the revision ids that other has that this does not.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
963
3184.1.9 by Robert Collins
* ``Repository.get_data_stream`` is now deprecated in favour of
964
        These are returned in topological order.
965
966
        revision_id: only return revision ids included by revision_id.
967
        """
968
        return repository.InterRepository.get(
969
            other, self).search_missing_revision_ids(revision_id, find_ghosts)
970
3452.2.1 by Andrew Bennetts
An experimental InterRepo for remote packs.
971
    def fetch(self, source, revision_id=None, pb=None, find_ghosts=False):
972
        # Not delegated to _real_repository so that InterRepository.get has a
973
        # chance to find an InterRepository specialised for RemoteRepository.
2881.4.1 by Robert Collins
Move responsibility for detecting same-repo fetching from the
974
        if self.has_same_location(source):
975
            # check that last_revision is in 'from' and then return a
976
            # no-operation.
977
            if (revision_id is not None and
2948.3.1 by John Arbash Meinel
Fix bug #158333, make sure that Repository.fetch(self) is properly a no-op for all Repository implementations.
978
                not revision.is_null(revision_id)):
2881.4.1 by Robert Collins
Move responsibility for detecting same-repo fetching from the
979
                self.get_revision(revision_id)
2592.4.5 by Martin Pool
Add Repository.base on all repositories.
980
            return 0, []
3709.5.1 by Andrew Bennetts
Allow pushing to a pack repo over HPSS use the get_parent_map RPC, and teach the get_parent_map client to cache missing revisions.
981
        inter = repository.InterRepository.get(source, self)
3452.2.1 by Andrew Bennetts
An experimental InterRepo for remote packs.
982
        try:
983
            return inter.fetch(revision_id=revision_id, pb=pb, find_ghosts=find_ghosts)
984
        except NotImplementedError:
985
            raise errors.IncompatibleRepositories(source, self)
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
986
2520.4.54 by Aaron Bentley
Hang a create_bundle method off repository
987
    def create_bundle(self, target, base, fileobj, format=None):
988
        self._ensure_real()
989
        self._real_repository.create_bundle(target, base, fileobj, format)
990
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
991
    @needs_read_lock
2530.1.1 by Aaron Bentley
Make topological sorting optional for get_ancestry
992
    def get_ancestry(self, revision_id, topo_sorted=True):
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
993
        self._ensure_real()
2530.1.1 by Aaron Bentley
Make topological sorting optional for get_ancestry
994
        return self._real_repository.get_ancestry(revision_id, topo_sorted)
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
995
996
    def fileids_altered_by_revision_ids(self, revision_ids):
997
        self._ensure_real()
998
        return self._real_repository.fileids_altered_by_revision_ids(revision_ids)
999
3036.1.3 by Robert Collins
Privatise VersionedFileChecker.
1000
    def _get_versioned_file_checker(self, revisions, revision_versions_cache):
2745.6.1 by Aaron Bentley
Initial checking of knit graphs
1001
        self._ensure_real()
3036.1.3 by Robert Collins
Privatise VersionedFileChecker.
1002
        return self._real_repository._get_versioned_file_checker(
2745.6.50 by Andrew Bennetts
Remove find_bad_ancestors; it's not needed anymore.
1003
            revisions, revision_versions_cache)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1004
2708.1.7 by Aaron Bentley
Rename extract_files_bytes to iter_files_bytes
1005
    def iter_files_bytes(self, desired_files):
2708.1.9 by Aaron Bentley
Clean-up docs and imports
1006
        """See Repository.iter_file_bytes.
2708.1.3 by Aaron Bentley
Implement extract_files_bytes on Repository
1007
        """
1008
        self._ensure_real()
2708.1.7 by Aaron Bentley
Rename extract_files_bytes to iter_files_bytes
1009
        return self._real_repository.iter_files_bytes(desired_files)
2708.1.3 by Aaron Bentley
Implement extract_files_bytes on Repository
1010
3565.3.1 by Robert Collins
* The generic fetch code now uses two attributes on Repository objects
1011
    @property
1012
    def _fetch_order(self):
1013
        """Decorate the real repository for now.
1014
1015
        In the long term getting this back from the remote repository as part
1016
        of open would be more efficient.
1017
        """
3842.3.20 by Andrew Bennetts
Re-revert changes from another thread that accidentally got reinstated here.
1018
        self._ensure_real()
1019
        return self._real_repository._fetch_order
3565.3.1 by Robert Collins
* The generic fetch code now uses two attributes on Repository objects
1020
1021
    @property
1022
    def _fetch_uses_deltas(self):
1023
        """Decorate the real repository for now.
1024
1025
        In the long term getting this back from the remote repository as part
1026
        of open would be more efficient.
1027
        """
1028
        self._ensure_real()
1029
        return self._real_repository._fetch_uses_deltas
1030
3565.3.4 by Robert Collins
Defer decision to reconcile to the repository being fetched into.
1031
    @property
1032
    def _fetch_reconcile(self):
1033
        """Decorate the real repository for now.
1034
1035
        In the long term getting this back from the remote repository as part
1036
        of open would be more efficient.
1037
        """
1038
        self._ensure_real()
1039
        return self._real_repository._fetch_reconcile
1040
3835.1.1 by Aaron Bentley
Stack get_parent_map on fallback repos
1041
    def get_parent_map(self, revision_ids):
3835.1.6 by Aaron Bentley
Reduce inefficiency when doing make_parents_provider frequently
1042
        """See bzrlib.Graph.get_parent_map()."""
3835.1.5 by Aaron Bentley
Fix make_parents_provider
1043
        return self._make_parents_provider().get_parent_map(revision_ids)
3835.1.1 by Aaron Bentley
Stack get_parent_map on fallback repos
1044
1045
    def _get_parent_map_rpc(self, keys):
3172.5.6 by Robert Collins
Create new smart server verb Repository.get_parent_map.
1046
        """Helper for get_parent_map that performs the RPC."""
3313.2.1 by Andrew Bennetts
Change _SmartClient's API to accept a medium and a base, rather than a _SharedConnection.
1047
        medium = self._client._medium
3453.4.10 by Andrew Bennetts
Change _is_remote_at_least to _is_remote_before.
1048
        if medium._is_remote_before((1, 2)):
3213.1.1 by Andrew Bennetts
Recover (by reconnecting) if the server turns out not to understand the new requests in 1.2 that send bodies.
1049
            # We already found out that the server can't understand
3213.1.3 by Andrew Bennetts
Fix typo in comment.
1050
            # Repository.get_parent_map requests, so just fetch the whole
3213.1.1 by Andrew Bennetts
Recover (by reconnecting) if the server turns out not to understand the new requests in 1.2 that send bodies.
1051
            # graph.
3287.6.1 by Robert Collins
* ``VersionedFile.get_graph`` is deprecated, with no replacement method.
1052
            # XXX: Note that this will issue a deprecation warning. This is ok
1053
            # :- its because we're working with a deprecated server anyway, and
1054
            # the user will almost certainly have seen a warning about the
1055
            # server version already.
3389.1.1 by John Arbash Meinel
Fix bug #214894. Fix RemoteRepository.get_parent_map() when server is <v1.2
1056
            rg = self.get_revision_graph()
1057
            # There is an api discrepency between get_parent_map and
1058
            # get_revision_graph. Specifically, a "key:()" pair in
1059
            # get_revision_graph just means a node has no parents. For
1060
            # "get_parent_map" it means the node is a ghost. So fix up the
1061
            # graph to correct this.
1062
            #   https://bugs.launchpad.net/bzr/+bug/214894
1063
            # There is one other "bug" which is that ghosts in
1064
            # get_revision_graph() are not returned at all. But we won't worry
1065
            # about that for now.
1066
            for node_id, parent_ids in rg.iteritems():
1067
                if parent_ids == ():
1068
                    rg[node_id] = (NULL_REVISION,)
1069
            rg[NULL_REVISION] = ()
1070
            return rg
3213.1.1 by Andrew Bennetts
Recover (by reconnecting) if the server turns out not to understand the new requests in 1.2 that send bodies.
1071
3172.5.6 by Robert Collins
Create new smart server verb Repository.get_parent_map.
1072
        keys = set(keys)
3373.5.2 by John Arbash Meinel
Add repository_implementation tests for get_parent_map
1073
        if None in keys:
1074
            raise ValueError('get_parent_map(None) is not valid')
3172.5.6 by Robert Collins
Create new smart server verb Repository.get_parent_map.
1075
        if NULL_REVISION in keys:
1076
            keys.discard(NULL_REVISION)
1077
            found_parents = {NULL_REVISION:()}
1078
            if not keys:
1079
                return found_parents
1080
        else:
1081
            found_parents = {}
3211.5.1 by Robert Collins
Change the smart server get_parents method to take a graph search to exclude already recieved parents from. This prevents history shortcuts causing huge numbers of duplicates.
1082
        # TODO(Needs analysis): We could assume that the keys being requested
1083
        # from get_parent_map are in a breadth first search, so typically they
1084
        # will all be depth N from some common parent, and we don't have to
1085
        # have the server iterate from the root parent, but rather from the
1086
        # keys we're searching; and just tell the server the keyspace we
1087
        # already have; but this may be more traffic again.
1088
1089
        # Transform self._parents_map into a search request recipe.
1090
        # TODO: Manage this incrementally to avoid covering the same path
1091
        # repeatedly. (The server will have to on each request, but the less
1092
        # work done the better).
3835.1.8 by Aaron Bentley
Make UnstackedParentsProvider manage the cache
1093
        parents_map = self._unstacked_provider.get_cached_map()
3213.1.8 by Andrew Bennetts
Merge from bzr.dev.
1094
        if parents_map is None:
1095
            # Repository is not locked, so there's no cache.
1096
            parents_map = {}
1097
        start_set = set(parents_map)
3211.5.1 by Robert Collins
Change the smart server get_parents method to take a graph search to exclude already recieved parents from. This prevents history shortcuts causing huge numbers of duplicates.
1098
        result_parents = set()
3213.1.8 by Andrew Bennetts
Merge from bzr.dev.
1099
        for parents in parents_map.itervalues():
3211.5.1 by Robert Collins
Change the smart server get_parents method to take a graph search to exclude already recieved parents from. This prevents history shortcuts causing huge numbers of duplicates.
1100
            result_parents.update(parents)
1101
        stop_keys = result_parents.difference(start_set)
1102
        included_keys = start_set.intersection(result_parents)
1103
        start_set.difference_update(included_keys)
3213.1.8 by Andrew Bennetts
Merge from bzr.dev.
1104
        recipe = (start_set, stop_keys, len(parents_map))
3842.3.20 by Andrew Bennetts
Re-revert changes from another thread that accidentally got reinstated here.
1105
        body = self._serialise_search_recipe(recipe)
3172.5.6 by Robert Collins
Create new smart server verb Repository.get_parent_map.
1106
        path = self.bzrdir._path_for_remote_call(self._client)
1107
        for key in keys:
3360.2.8 by Martin Pool
Change assertion to a plain raise
1108
            if type(key) is not str:
1109
                raise ValueError(
1110
                    "key %r not a plain string" % (key,))
3172.5.8 by Robert Collins
Review feedback.
1111
        verb = 'Repository.get_parent_map'
3211.5.1 by Robert Collins
Change the smart server get_parents method to take a graph search to exclude already recieved parents from. This prevents history shortcuts causing huge numbers of duplicates.
1112
        args = (path,) + tuple(keys)
3297.3.3 by Andrew Bennetts
SmartClientRequestProtocol*.read_response_tuple can now raise UnknownSmartMethod. Callers no longer need to do their own ad hoc unknown smart method error detection.
1113
        try:
3786.2.3 by Andrew Bennetts
Remove duplicated 'call & translate errors' code in bzrlib.remote.
1114
            response = self._call_with_body_bytes_expecting_body(
3842.3.20 by Andrew Bennetts
Re-revert changes from another thread that accidentally got reinstated here.
1115
                verb, args, body)
3297.3.3 by Andrew Bennetts
SmartClientRequestProtocol*.read_response_tuple can now raise UnknownSmartMethod. Callers no longer need to do their own ad hoc unknown smart method error detection.
1116
        except errors.UnknownSmartMethod:
3213.1.2 by Andrew Bennetts
Add test for reconnection if get_parent_map is unknown by the server.
1117
            # Server does not support this method, so get the whole graph.
3213.1.1 by Andrew Bennetts
Recover (by reconnecting) if the server turns out not to understand the new requests in 1.2 that send bodies.
1118
            # Worse, we have to force a disconnection, because the server now
1119
            # doesn't realise it has a body on the wire to consume, so the
1120
            # only way to recover is to abandon the connection.
3213.1.6 by Andrew Bennetts
Emit warnings when forcing a reconnect.
1121
            warning(
1122
                'Server is too old for fast get_parent_map, reconnecting.  '
1123
                '(Upgrade the server to Bazaar 1.2 to avoid this)')
3213.1.1 by Andrew Bennetts
Recover (by reconnecting) if the server turns out not to understand the new requests in 1.2 that send bodies.
1124
            medium.disconnect()
1125
            # To avoid having to disconnect repeatedly, we keep track of the
1126
            # fact the server doesn't understand remote methods added in 1.2.
3453.4.9 by Andrew Bennetts
Rename _remote_is_not to _remember_remote_is_before.
1127
            medium._remember_remote_is_before((1, 2))
3297.3.4 by Andrew Bennetts
Merge from bzr.dev.
1128
            return self.get_revision_graph(None)
3245.4.58 by Andrew Bennetts
Unpack call_expecting_body's return value into variables, to avoid lots of ugly subscripting.
1129
        response_tuple, response_handler = response
1130
        if response_tuple[0] not in ['ok']:
1131
            response_handler.cancel_read_body()
1132
            raise errors.UnexpectedSmartServerResponse(response_tuple)
1133
        if response_tuple[0] == 'ok':
1134
            coded = bz2.decompress(response_handler.read_body_bytes())
3172.5.6 by Robert Collins
Create new smart server verb Repository.get_parent_map.
1135
            if coded == '':
1136
                # no revisions found
1137
                return {}
1138
            lines = coded.split('\n')
1139
            revision_graph = {}
1140
            for line in lines:
1141
                d = tuple(line.split())
1142
                if len(d) > 1:
1143
                    revision_graph[d[0]] = d[1:]
1144
                else:
1145
                    # No parents - so give the Graph result (NULL_REVISION,).
1146
                    revision_graph[d[0]] = (NULL_REVISION,)
1147
            return revision_graph
1148
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
1149
    @needs_read_lock
1150
    def get_signature_text(self, revision_id):
1151
        self._ensure_real()
1152
        return self._real_repository.get_signature_text(revision_id)
1153
1154
    @needs_read_lock
3228.4.11 by John Arbash Meinel
Deprecations abound.
1155
    @symbol_versioning.deprecated_method(symbol_versioning.one_three)
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
1156
    def get_revision_graph_with_ghosts(self, revision_ids=None):
1157
        self._ensure_real()
1158
        return self._real_repository.get_revision_graph_with_ghosts(
1159
            revision_ids=revision_ids)
1160
1161
    @needs_read_lock
1162
    def get_inventory_xml(self, revision_id):
1163
        self._ensure_real()
1164
        return self._real_repository.get_inventory_xml(revision_id)
1165
1166
    def deserialise_inventory(self, revision_id, xml):
1167
        self._ensure_real()
1168
        return self._real_repository.deserialise_inventory(revision_id, xml)
1169
1170
    def reconcile(self, other=None, thorough=False):
1171
        self._ensure_real()
1172
        return self._real_repository.reconcile(other=other, thorough=thorough)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1173
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
1174
    def all_revision_ids(self):
1175
        self._ensure_real()
1176
        return self._real_repository.all_revision_ids()
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1177
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
1178
    @needs_read_lock
1179
    def get_deltas_for_revisions(self, revisions):
1180
        self._ensure_real()
1181
        return self._real_repository.get_deltas_for_revisions(revisions)
1182
1183
    @needs_read_lock
1184
    def get_revision_delta(self, revision_id):
1185
        self._ensure_real()
1186
        return self._real_repository.get_revision_delta(revision_id)
1187
1188
    @needs_read_lock
1189
    def revision_trees(self, revision_ids):
1190
        self._ensure_real()
1191
        return self._real_repository.revision_trees(revision_ids)
1192
1193
    @needs_read_lock
1194
    def get_revision_reconcile(self, revision_id):
1195
        self._ensure_real()
1196
        return self._real_repository.get_revision_reconcile(revision_id)
1197
1198
    @needs_read_lock
2745.6.36 by Andrew Bennetts
Deprecate revision_ids arg to Repository.check and other tweaks.
1199
    def check(self, revision_ids=None):
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
1200
        self._ensure_real()
2745.6.36 by Andrew Bennetts
Deprecate revision_ids arg to Repository.check and other tweaks.
1201
        return self._real_repository.check(revision_ids=revision_ids)
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
1202
2018.5.138 by Robert Collins
Merge bzr.dev.
1203
    def copy_content_into(self, destination, revision_id=None):
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
1204
        self._ensure_real()
1205
        return self._real_repository.copy_content_into(
2018.5.138 by Robert Collins
Merge bzr.dev.
1206
            destination, revision_id=revision_id)
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
1207
2814.10.2 by Andrew Bennetts
Make the fallback a little tidier.
1208
    def _copy_repository_tarball(self, to_bzrdir, revision_id=None):
2018.18.10 by Martin Pool
copy_content_into from Remote repositories by using temporary directories on both ends.
1209
        # get a tarball of the remote repository, and copy from that into the
1210
        # destination
1211
        from bzrlib import osutils
2018.18.9 by Martin Pool
remote Repository.tarball builds a temporary directory and tars that
1212
        import tarfile
2018.18.20 by Martin Pool
Route branch operations through remote copy_content_into
1213
        # TODO: Maybe a progress bar while streaming the tarball?
1214
        note("Copying repository content as tarball...")
2018.18.25 by Martin Pool
Repository.tarball fixes for python2.4
1215
        tar_file = self._get_tarball('bz2')
2814.10.2 by Andrew Bennetts
Make the fallback a little tidier.
1216
        if tar_file is None:
1217
            return None
1218
        destination = to_bzrdir.create_repository()
2018.18.10 by Martin Pool
copy_content_into from Remote repositories by using temporary directories on both ends.
1219
        try:
2018.18.25 by Martin Pool
Repository.tarball fixes for python2.4
1220
            tar = tarfile.open('repository', fileobj=tar_file,
1221
                mode='r|bz2')
3638.3.2 by Vincent Ladeuil
Fix all calls to tempfile.mkdtemp to osutils.mkdtemp.
1222
            tmpdir = osutils.mkdtemp()
2018.18.25 by Martin Pool
Repository.tarball fixes for python2.4
1223
            try:
1224
                _extract_tar(tar, tmpdir)
1225
                tmp_bzrdir = BzrDir.open(tmpdir)
1226
                tmp_repo = tmp_bzrdir.open_repository()
1227
                tmp_repo.copy_content_into(destination, revision_id)
1228
            finally:
1229
                osutils.rmtree(tmpdir)
2018.18.10 by Martin Pool
copy_content_into from Remote repositories by using temporary directories on both ends.
1230
        finally:
2018.18.25 by Martin Pool
Repository.tarball fixes for python2.4
1231
            tar_file.close()
2814.10.2 by Andrew Bennetts
Make the fallback a little tidier.
1232
        return destination
2018.18.23 by Martin Pool
review cleanups
1233
        # TODO: Suggestion from john: using external tar is much faster than
1234
        # python's tarfile library, but it may not work on windows.
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
1235
3842.3.20 by Andrew Bennetts
Re-revert changes from another thread that accidentally got reinstated here.
1236
    @property
1237
    def inventories(self):
1238
        """Decorate the real repository for now.
1239
1240
        In the long term a full blown network facility is needed to
1241
        avoid creating a real repository object locally.
1242
        """
1243
        self._ensure_real()
1244
        return self._real_repository.inventories
1245
2604.2.1 by Robert Collins
(robertc) Introduce a pack command.
1246
    @needs_write_lock
1247
    def pack(self):
1248
        """Compress the data within the repository.
1249
1250
        This is not currently implemented within the smart server.
1251
        """
1252
        self._ensure_real()
1253
        return self._real_repository.pack()
1254
3842.3.20 by Andrew Bennetts
Re-revert changes from another thread that accidentally got reinstated here.
1255
    @property
1256
    def revisions(self):
1257
        """Decorate the real repository for now.
1258
1259
        In the short term this should become a real object to intercept graph
1260
        lookups.
1261
1262
        In the long term a full blown network facility is needed.
1263
        """
1264
        self._ensure_real()
1265
        return self._real_repository.revisions
1266
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
1267
    def set_make_working_trees(self, new_value):
4017.3.4 by Robert Collins
Create a verb for Repository.set_make_working_trees.
1268
        if new_value:
1269
            new_value_str = "True"
1270
        else:
1271
            new_value_str = "False"
1272
        path = self.bzrdir._path_for_remote_call(self._client)
1273
        try:
1274
            response = self._call(
1275
                'Repository.set_make_working_trees', path, new_value_str)
1276
        except errors.UnknownSmartMethod:
1277
            self._ensure_real()
1278
            self._real_repository.set_make_working_trees(new_value)
1279
        else:
1280
            if response[0] != 'ok':
1281
                raise errors.UnexpectedSmartServerResponse(response)
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
1282
3842.3.20 by Andrew Bennetts
Re-revert changes from another thread that accidentally got reinstated here.
1283
    @property
1284
    def signatures(self):
1285
        """Decorate the real repository for now.
1286
1287
        In the long term a full blown network facility is needed to avoid
1288
        creating a real repository object locally.
1289
        """
1290
        self._ensure_real()
1291
        return self._real_repository.signatures
1292
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
1293
    @needs_write_lock
1294
    def sign_revision(self, revision_id, gpg_strategy):
1295
        self._ensure_real()
1296
        return self._real_repository.sign_revision(revision_id, gpg_strategy)
1297
3842.3.20 by Andrew Bennetts
Re-revert changes from another thread that accidentally got reinstated here.
1298
    @property
1299
    def texts(self):
1300
        """Decorate the real repository for now.
1301
1302
        In the long term a full blown network facility is needed to avoid
1303
        creating a real repository object locally.
1304
        """
1305
        self._ensure_real()
1306
        return self._real_repository.texts
1307
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
1308
    @needs_read_lock
1309
    def get_revisions(self, revision_ids):
1310
        self._ensure_real()
1311
        return self._real_repository.get_revisions(revision_ids)
1312
1313
    def supports_rich_root(self):
2018.5.84 by Andrew Bennetts
Merge in supports-rich-root, another test passing.
1314
        self._ensure_real()
1315
        return self._real_repository.supports_rich_root()
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
1316
2018.5.83 by Andrew Bennetts
Fix some test failures caused by the switch from unicode to UTF-8-encoded strs for revision IDs.
1317
    def iter_reverse_revision_history(self, revision_id):
1318
        self._ensure_real()
1319
        return self._real_repository.iter_reverse_revision_history(revision_id)
1320
2018.5.96 by Andrew Bennetts
Merge from bzr.dev, resolving the worst of the semantic conflicts, but there's
1321
    @property
1322
    def _serializer(self):
1323
        self._ensure_real()
1324
        return self._real_repository._serializer
1325
2018.5.97 by Andrew Bennetts
Fix more tests.
1326
    def store_revision_signature(self, gpg_strategy, plaintext, revision_id):
1327
        self._ensure_real()
1328
        return self._real_repository.store_revision_signature(
1329
            gpg_strategy, plaintext, revision_id)
1330
2996.2.8 by Aaron Bentley
Fix add_signature discrepancies
1331
    def add_signature_text(self, revision_id, signature):
2996.2.3 by Aaron Bentley
Add tests for install_revisions and add_signature
1332
        self._ensure_real()
2996.2.8 by Aaron Bentley
Fix add_signature discrepancies
1333
        return self._real_repository.add_signature_text(revision_id, signature)
2996.2.3 by Aaron Bentley
Add tests for install_revisions and add_signature
1334
2018.5.97 by Andrew Bennetts
Fix more tests.
1335
    def has_signature_for_revision_id(self, revision_id):
1336
        self._ensure_real()
1337
        return self._real_repository.has_signature_for_revision_id(revision_id)
1338
2535.3.45 by Andrew Bennetts
Add item_keys_introduced_by to RemoteRepository.
1339
    def item_keys_introduced_by(self, revision_ids, _files_pb=None):
1340
        self._ensure_real()
1341
        return self._real_repository.item_keys_introduced_by(revision_ids,
1342
            _files_pb=_files_pb)
1343
2819.2.4 by Andrew Bennetts
Add a 'revision_graph_can_have_wrong_parents' method to repository.
1344
    def revision_graph_can_have_wrong_parents(self):
1345
        # The answer depends on the remote repo format.
1346
        self._ensure_real()
1347
        return self._real_repository.revision_graph_can_have_wrong_parents()
1348
2819.2.5 by Andrew Bennetts
Make reconcile abort gracefully if the revision index has bad parents.
1349
    def _find_inconsistent_revision_parents(self):
1350
        self._ensure_real()
1351
        return self._real_repository._find_inconsistent_revision_parents()
1352
1353
    def _check_for_inconsistent_revision_parents(self):
1354
        self._ensure_real()
1355
        return self._real_repository._check_for_inconsistent_revision_parents()
1356
3835.1.17 by Aaron Bentley
Fix stacking bug
1357
    def _make_parents_provider(self, other=None):
3835.1.8 by Aaron Bentley
Make UnstackedParentsProvider manage the cache
1358
        providers = [self._unstacked_provider]
3835.1.17 by Aaron Bentley
Fix stacking bug
1359
        if other is not None:
1360
            providers.insert(0, other)
3835.1.7 by Aaron Bentley
Updates from review
1361
        providers.extend(r._make_parents_provider() for r in
1362
                         self._fallback_repositories)
1363
        return graph._StackedParentsProvider(providers)
3172.5.1 by Robert Collins
Create a RemoteRepository get_graph implementation and delegate get_parents_map to the real repository.
1364
3842.3.20 by Andrew Bennetts
Re-revert changes from another thread that accidentally got reinstated here.
1365
    def _serialise_search_recipe(self, recipe):
1366
        """Serialise a graph search recipe.
1367
1368
        :param recipe: A search recipe (start, stop, count).
1369
        :return: Serialised bytes.
1370
        """
1371
        start_keys = ' '.join(recipe[0])
1372
        stop_keys = ' '.join(recipe[1])
1373
        count = str(recipe[2])
1374
        return '\n'.join((start_keys, stop_keys, count))
1375
3842.3.2 by Andrew Bennetts
Revert the RemoteVersionedFiles.get_parent_map implementation, leaving just the skeleton of RemoteVersionedFiles.
1376
    def autopack(self):
1377
        path = self.bzrdir._path_for_remote_call(self._client)
1378
        try:
1379
            response = self._call('PackRepository.autopack', path)
1380
        except errors.UnknownSmartMethod:
1381
            self._ensure_real()
1382
            self._real_repository._pack_collection.autopack()
1383
            return
1384
        if self._real_repository is not None:
1385
            # Reset the real repository's cache of pack names.
1386
            # XXX: At some point we may be able to skip this and just rely on
1387
            # the automatic retry logic to do the right thing, but for now we
1388
            # err on the side of being correct rather than being optimal.
1389
            self._real_repository._pack_collection.reload_pack_names()
1390
        if response[0] != 'ok':
1391
            raise errors.UnexpectedSmartServerResponse(response)
1392
1393
4022.1.6 by Robert Collins
Cherrypick and polish the RemoteSink for streaming push.
1394
class RemoteStreamSink(repository.StreamSink):
1395
4029.2.1 by Robert Collins
Support streaming push to stacked branches.
1396
    def __init__(self, target_repo):
1397
        repository.StreamSink.__init__(self, target_repo)
1398
4032.3.7 by Robert Collins
Move write locking and write group responsibilities into the Sink objects themselves, allowing complete avoidance of unnecessary calls when the sink is a RemoteSink.
1399
    def _insert_real(self, stream, src_format, resume_tokens):
4022.1.6 by Robert Collins
Cherrypick and polish the RemoteSink for streaming push.
1400
        self.target_repo._ensure_real()
1401
        sink = self.target_repo._real_repository._get_sink()
4032.3.7 by Robert Collins
Move write locking and write group responsibilities into the Sink objects themselves, allowing complete avoidance of unnecessary calls when the sink is a RemoteSink.
1402
        result = sink.insert_stream(stream, src_format, resume_tokens)
4029.2.1 by Robert Collins
Support streaming push to stacked branches.
1403
        if not result:
1404
            self.target_repo.autopack()
1405
        return result
4022.1.6 by Robert Collins
Cherrypick and polish the RemoteSink for streaming push.
1406
4032.3.7 by Robert Collins
Move write locking and write group responsibilities into the Sink objects themselves, allowing complete avoidance of unnecessary calls when the sink is a RemoteSink.
1407
    def insert_stream(self, stream, src_format, resume_tokens):
4022.1.6 by Robert Collins
Cherrypick and polish the RemoteSink for streaming push.
1408
        repo = self.target_repo
1409
        client = repo._client
4022.1.9 by Robert Collins
Fix critical issue in bzr.dev - pushing to an old bzr:// server fails because the stream being consumed before the fallback code occurs, which makes it fail to do the fetch. (Robert Collins, Andrew Bennetts, #332314)
1410
        medium = client._medium
4029.2.1 by Robert Collins
Support streaming push to stacked branches.
1411
        if medium._is_remote_before((1, 13)):
4022.1.9 by Robert Collins
Fix critical issue in bzr.dev - pushing to an old bzr:// server fails because the stream being consumed before the fallback code occurs, which makes it fail to do the fetch. (Robert Collins, Andrew Bennetts, #332314)
1412
            # No possible way this can work.
4032.3.7 by Robert Collins
Move write locking and write group responsibilities into the Sink objects themselves, allowing complete avoidance of unnecessary calls when the sink is a RemoteSink.
1413
            return self._insert_real(stream, src_format, resume_tokens)
4022.1.6 by Robert Collins
Cherrypick and polish the RemoteSink for streaming push.
1414
        path = repo.bzrdir._path_for_remote_call(client)
4032.3.7 by Robert Collins
Move write locking and write group responsibilities into the Sink objects themselves, allowing complete avoidance of unnecessary calls when the sink is a RemoteSink.
1415
        if not resume_tokens:
4029.2.1 by Robert Collins
Support streaming push to stacked branches.
1416
            # XXX: Ugly but important for correctness, *will* be fixed during
1417
            # 1.13 cycle. Pushing a stream that is interrupted results in a
1418
            # fallback to the _real_repositories sink *with a partial stream*.
1419
            # Thats bad because we insert less data than bzr expected. To avoid
1420
            # this we do a trial push to make sure the verb is accessible, and
1421
            # do not fallback when actually pushing the stream. A cleanup patch
1422
            # is going to look at rewinding/restarting the stream/partial
1423
            # buffering etc.
1424
            byte_stream = self._stream_to_byte_stream([], src_format)
1425
            try:
1426
                response = client.call_with_body_stream(
4032.3.7 by Robert Collins
Move write locking and write group responsibilities into the Sink objects themselves, allowing complete avoidance of unnecessary calls when the sink is a RemoteSink.
1427
                    ('Repository.insert_stream', path, ''), byte_stream)
4029.2.1 by Robert Collins
Support streaming push to stacked branches.
1428
            except errors.UnknownSmartMethod:
1429
                medium._remember_remote_is_before((1,13))
4032.3.7 by Robert Collins
Move write locking and write group responsibilities into the Sink objects themselves, allowing complete avoidance of unnecessary calls when the sink is a RemoteSink.
1430
                return self._insert_real(stream, src_format, resume_tokens)
4022.1.9 by Robert Collins
Fix critical issue in bzr.dev - pushing to an old bzr:// server fails because the stream being consumed before the fallback code occurs, which makes it fail to do the fetch. (Robert Collins, Andrew Bennetts, #332314)
1431
        byte_stream = self._stream_to_byte_stream(stream, src_format)
4032.3.7 by Robert Collins
Move write locking and write group responsibilities into the Sink objects themselves, allowing complete avoidance of unnecessary calls when the sink is a RemoteSink.
1432
        resume_tokens = ' '.join(resume_tokens)
4022.1.9 by Robert Collins
Fix critical issue in bzr.dev - pushing to an old bzr:// server fails because the stream being consumed before the fallback code occurs, which makes it fail to do the fetch. (Robert Collins, Andrew Bennetts, #332314)
1433
        response = client.call_with_body_stream(
4029.2.1 by Robert Collins
Support streaming push to stacked branches.
1434
            ('Repository.insert_stream', path, resume_tokens), byte_stream)
1435
        if response[0][0] not in ('ok', 'missing-basis'):
4022.1.9 by Robert Collins
Fix critical issue in bzr.dev - pushing to an old bzr:// server fails because the stream being consumed before the fallback code occurs, which makes it fail to do the fetch. (Robert Collins, Andrew Bennetts, #332314)
1436
            raise errors.UnexpectedSmartServerResponse(response)
4029.2.1 by Robert Collins
Support streaming push to stacked branches.
1437
        if response[0][0] == 'missing-basis':
1438
            tokens, missing_keys = bencode.bdecode_as_tuple(response[0][1])
4032.3.7 by Robert Collins
Move write locking and write group responsibilities into the Sink objects themselves, allowing complete avoidance of unnecessary calls when the sink is a RemoteSink.
1439
            resume_tokens = tokens
1440
            return resume_tokens, missing_keys
4029.2.1 by Robert Collins
Support streaming push to stacked branches.
1441
        else:
1442
            if self.target_repo._real_repository is not None:
1443
                collection = getattr(self.target_repo._real_repository,
1444
                    '_pack_collection', None)
1445
                if collection is not None:
1446
                    collection.reload_pack_names()
4032.3.7 by Robert Collins
Move write locking and write group responsibilities into the Sink objects themselves, allowing complete avoidance of unnecessary calls when the sink is a RemoteSink.
1447
            return [], set()
4032.1.1 by John Arbash Meinel
Merge the removal of all trailing whitespace, and resolve conflicts.
1448
4022.1.6 by Robert Collins
Cherrypick and polish the RemoteSink for streaming push.
1449
    def _stream_to_byte_stream(self, stream, src_format):
1450
        bytes = []
1451
        pack_writer = pack.ContainerWriter(bytes.append)
1452
        pack_writer.begin()
1453
        pack_writer.add_bytes_record(src_format.network_name(), '')
1454
        adapters = {}
1455
        def get_adapter(adapter_key):
1456
            try:
1457
                return adapters[adapter_key]
1458
            except KeyError:
1459
                adapter_factory = adapter_registry.get(adapter_key)
1460
                adapter = adapter_factory(self)
1461
                adapters[adapter_key] = adapter
1462
                return adapter
1463
        for substream_type, substream in stream:
1464
            for record in substream:
1465
                if record.storage_kind in ('chunked', 'fulltext'):
1466
                    serialised = record_to_fulltext_bytes(record)
1467
                else:
1468
                    serialised = record.get_bytes_as(record.storage_kind)
1469
                pack_writer.add_bytes_record(serialised, [(substream_type,)])
1470
                for b in bytes:
1471
                    yield b
1472
                del bytes[:]
1473
        pack_writer.end()
1474
        for b in bytes:
1475
            yield b
4022.1.1 by Robert Collins
Refactoring of fetch to have a sender and sink component enabling splitting the logic over a network stream. (Robert Collins, Andrew Bennetts)
1476
1477
2018.5.127 by Andrew Bennetts
Fix most of the lockable_files tests for RemoteBranchLockableFiles.
1478
class RemoteBranchLockableFiles(LockableFiles):
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
1479
    """A 'LockableFiles' implementation that talks to a smart server.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1480
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
1481
    This is not a public interface class.
1482
    """
1483
1484
    def __init__(self, bzrdir, _client):
1485
        self.bzrdir = bzrdir
1486
        self._client = _client
2018.5.135 by Andrew Bennetts
Prevent remote branch clients from determining the 'right' mode for control files, because we don't want clients setting the mode anyway.
1487
        self._need_find_modes = True
2018.5.133 by Andrew Bennetts
All TestLockableFiles_RemoteLockDir tests passing.
1488
        LockableFiles.__init__(
2018.5.163 by Andrew Bennetts
Deal with various review comments from Robert.
1489
            self, bzrdir.get_branch_transport(None),
2018.5.133 by Andrew Bennetts
All TestLockableFiles_RemoteLockDir tests passing.
1490
            'lock', lockdir.LockDir)
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
1491
2018.5.135 by Andrew Bennetts
Prevent remote branch clients from determining the 'right' mode for control files, because we don't want clients setting the mode anyway.
1492
    def _find_modes(self):
1493
        # RemoteBranches don't let the client set the mode of control files.
1494
        self._dir_mode = None
1495
        self._file_mode = None
1496
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
1497
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
1498
class RemoteBranchFormat(branch.BranchFormat):
1499
3834.5.2 by John Arbash Meinel
Track down the various BranchFormats that weren't setting the branch format as part of the _matchingbzrdir format.
1500
    def __init__(self):
1501
        super(RemoteBranchFormat, self).__init__()
1502
        self._matchingbzrdir = RemoteBzrDirFormat()
1503
        self._matchingbzrdir.set_branch_format(self)
4032.3.2 by Robert Collins
Create and use a RPC call to create branches on bzr servers rather than using VFS calls.
1504
        self._custom_format = None
3834.5.2 by John Arbash Meinel
Track down the various BranchFormats that weren't setting the branch format as part of the _matchingbzrdir format.
1505
2018.5.124 by Robert Collins
Fix test_format_initialize_find_open by delegating Branch formt lookup to the BzrDir, where it should have stayed from the start.
1506
    def __eq__(self, other):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1507
        return (isinstance(other, RemoteBranchFormat) and
2018.5.124 by Robert Collins
Fix test_format_initialize_find_open by delegating Branch formt lookup to the BzrDir, where it should have stayed from the start.
1508
            self.__dict__ == other.__dict__)
1509
2018.5.60 by Robert Collins
More missing methods from RemoteBranch and RemoteRepository to let 'info' get further.
1510
    def get_format_description(self):
1511
        return 'Remote BZR Branch'
1512
4032.3.1 by Robert Collins
Add a BranchFormat.network_name() method as preparation for creating branches via RPC calls.
1513
    def network_name(self):
1514
        return self._network_name
1515
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
1516
    def open(self, a_bzrdir):
1752.2.72 by Andrew Bennetts
Make Remote* classes in remote.py more consistent and remove some dead code.
1517
        return a_bzrdir.open_branch()
1752.2.52 by Andrew Bennetts
Flesh out more Remote* methods needed to open and initialise remote branches/trees/repositories.
1518
4032.3.2 by Robert Collins
Create and use a RPC call to create branches on bzr servers rather than using VFS calls.
1519
    def _vfs_initialize(self, a_bzrdir):
1520
        # Initialisation when using a local bzrdir object, or a non-vfs init
1521
        # method is not available on the server.
1522
        # self._custom_format is always set - the start of initialize ensures
1523
        # that.
1524
        if isinstance(a_bzrdir, RemoteBzrDir):
1525
            a_bzrdir._ensure_real()
1526
            result = self._custom_format.initialize(a_bzrdir._real_bzrdir)
1527
        else:
1528
            # We assume the bzrdir is parameterised; it may not be.
1529
            result = self._custom_format.initialize(a_bzrdir)
1530
        if (isinstance(a_bzrdir, RemoteBzrDir) and
1531
            not isinstance(result, RemoteBranch)):
1532
            result = RemoteBranch(a_bzrdir, a_bzrdir.find_repository(), result)
1533
        return result
1534
1752.2.52 by Andrew Bennetts
Flesh out more Remote* methods needed to open and initialise remote branches/trees/repositories.
1535
    def initialize(self, a_bzrdir):
4032.3.2 by Robert Collins
Create and use a RPC call to create branches on bzr servers rather than using VFS calls.
1536
        # 1) get the network name to use.
1537
        if self._custom_format:
1538
            network_name = self._custom_format.network_name()
1539
        else:
1540
            # Select the current bzrlib default and ask for that.
1541
            reference_bzrdir_format = bzrdir.format_registry.get('default')()
1542
            reference_format = reference_bzrdir_format.get_branch_format()
1543
            self._custom_format = reference_format
1544
            network_name = reference_format.network_name()
1545
        # Being asked to create on a non RemoteBzrDir:
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
1546
        if not isinstance(a_bzrdir, RemoteBzrDir):
4032.3.2 by Robert Collins
Create and use a RPC call to create branches on bzr servers rather than using VFS calls.
1547
            return self._vfs_initialize(a_bzrdir)
1548
        medium = a_bzrdir._client._medium
1549
        if medium._is_remote_before((1, 13)):
1550
            return self._vfs_initialize(a_bzrdir)
1551
        # Creating on a remote bzr dir.
1552
        # 2) try direct creation via RPC
1553
        path = a_bzrdir._path_for_remote_call(a_bzrdir._client)
1554
        verb = 'BzrDir.create_branch'
1555
        try:
1556
            response = a_bzrdir._call(verb, path, network_name)
1557
        except errors.UnknownSmartMethod:
1558
            # Fallback - use vfs methods
1559
            return self._vfs_initialize(a_bzrdir)
1560
        if response[0] != 'ok':
1561
            raise errors.UnexpectedSmartServerResponse(response)
1562
        # Turn the response into a RemoteRepository object.
1563
        format = RemoteBranchFormat()
1564
        format._network_name = response[1]
1565
        repo_format = response_tuple_to_repo_format(response[3:])
1566
        if response[2] == '':
1567
            repo_bzrdir = a_bzrdir
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
1568
        else:
4032.3.2 by Robert Collins
Create and use a RPC call to create branches on bzr servers rather than using VFS calls.
1569
            repo_bzrdir = RemoteBzrDir(
1570
                a_bzrdir.root_transport.clone(response[2]), a_bzrdir._format,
1571
                a_bzrdir._client)
1572
        remote_repo = RemoteRepository(repo_bzrdir, repo_format)
1573
        remote_branch = RemoteBranch(a_bzrdir, remote_repo,
1574
            format=format, setup_stacking=False)
4044.1.4 by Robert Collins
Remove a wasted round trip determining the revno and revid of a newly created branch.
1575
        # XXX: We know this is a new branch, so it must have revno 0, revid
1576
        # NULL_REVISION. Creating the branch locked would make this be unable
1577
        # to be wrong; here its simply very unlikely to be wrong. RBC 20090225
1578
        remote_branch._last_revision_info_cache = 0, NULL_REVISION
4032.3.2 by Robert Collins
Create and use a RPC call to create branches on bzr servers rather than using VFS calls.
1579
        return remote_branch
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
1580
2696.3.6 by Martin Pool
Mark RemoteBranch as (possibly) supporting tags
1581
    def supports_tags(self):
1582
        # Remote branches might support tags, but we won't know until we
1583
        # access the real remote branch.
1584
        return True
1585
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
1586
3786.2.3 by Andrew Bennetts
Remove duplicated 'call & translate errors' code in bzrlib.remote.
1587
class RemoteBranch(branch.Branch, _RpcHelper):
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
1588
    """Branch stored on a server accessed by HPSS RPC.
1589
1590
    At the moment most operations are mapped down to simple file operations.
1591
    """
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
1592
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
1593
    def __init__(self, remote_bzrdir, remote_repository, real_branch=None,
4032.3.2 by Robert Collins
Create and use a RPC call to create branches on bzr servers rather than using VFS calls.
1594
        _client=None, format=None, setup_stacking=True):
2018.5.34 by Robert Collins
Get test_remote.BasicRemoteObjectTests.test_open_remote_branch passing by implementing a remote method BzrDir.find_repository.
1595
        """Create a RemoteBranch instance.
1596
1597
        :param real_branch: An optional local implementation of the branch
1598
            format, usually accessing the data via the VFS.
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
1599
        :param _client: Private parameter for testing.
4032.3.2 by Robert Collins
Create and use a RPC call to create branches on bzr servers rather than using VFS calls.
1600
        :param format: A RemoteBranchFormat object, None to create one
1601
            automatically. If supplied it should have a network_name already
1602
            supplied.
1603
        :param setup_stacking: If True make an RPC call to determine the
1604
            stacked (or not) status of the branch. If False assume the branch
1605
            is not stacked.
2018.5.34 by Robert Collins
Get test_remote.BasicRemoteObjectTests.test_open_remote_branch passing by implementing a remote method BzrDir.find_repository.
1606
        """
2018.5.163 by Andrew Bennetts
Deal with various review comments from Robert.
1607
        # We intentionally don't call the parent class's __init__, because it
1608
        # will try to assign to self.tags, which is a property in this subclass.
1609
        # And the parent's __init__ doesn't do much anyway.
2978.7.1 by John Arbash Meinel
Fix bug #162486, by having RemoteBranch properly initialize self._revision_id_to_revno_map.
1610
        self._revision_id_to_revno_cache = None
3949.2.6 by Ian Clatworthy
review feedback from jam
1611
        self._partial_revision_id_to_revno_cache = {}
2018.5.105 by Andrew Bennetts
Implement revision_history caching for RemoteBranch.
1612
        self._revision_history_cache = None
3441.5.1 by Andrew Bennetts
Avoid necessarily calling get_parent_map when pushing.
1613
        self._last_revision_info_cache = None
3949.3.4 by Ian Clatworthy
jam feedback: start & stop limits; simple caching
1614
        self._merge_sorted_revisions_cache = None
1752.2.64 by Andrew Bennetts
Improve how RemoteBzrDir.open_branch works to handle references and not double-open repositories.
1615
        self.bzrdir = remote_bzrdir
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
1616
        if _client is not None:
1617
            self._client = _client
1618
        else:
3313.2.1 by Andrew Bennetts
Change _SmartClient's API to accept a medium and a base, rather than a _SharedConnection.
1619
            self._client = remote_bzrdir._client
1752.2.64 by Andrew Bennetts
Improve how RemoteBzrDir.open_branch works to handle references and not double-open repositories.
1620
        self.repository = remote_repository
2018.5.34 by Robert Collins
Get test_remote.BasicRemoteObjectTests.test_open_remote_branch passing by implementing a remote method BzrDir.find_repository.
1621
        if real_branch is not None:
1622
            self._real_branch = real_branch
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
1623
            # Give the remote repository the matching real repo.
2018.5.97 by Andrew Bennetts
Fix more tests.
1624
            real_repo = self._real_branch.repository
1625
            if isinstance(real_repo, RemoteRepository):
1626
                real_repo._ensure_real()
1627
                real_repo = real_repo._real_repository
1628
            self.repository._set_real_repository(real_repo)
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
1629
            # Give the branch the remote repository to let fast-pathing happen.
1630
            self._real_branch.repository = self.repository
2018.5.70 by Robert Collins
Only try to get real repositories when an operation requires them.
1631
        else:
1632
            self._real_branch = None
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
1633
        # Fill out expected attributes of branch for bzrlib api users.
2018.5.55 by Robert Collins
Give RemoteBranch a base url in line with the Branch protocol.
1634
        self.base = self.bzrdir.root_transport.base
2018.5.169 by Andrew Bennetts
Add a _server_formats flag to BzrDir.open_from_transport and BzrDirFormat.find_format, make RemoteBranch.control_files into a property.
1635
        self._control_files = None
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
1636
        self._lock_mode = None
1637
        self._lock_token = None
2892.2.1 by Andrew Bennetts
Add Branch.set_last_revision_info smart method, and make the RemoteBranch client use it.
1638
        self._repo_lock_token = None
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
1639
        self._lock_count = 0
1640
        self._leave_lock = False
4032.3.2 by Robert Collins
Create and use a RPC call to create branches on bzr servers rather than using VFS calls.
1641
        # Setup a format: note that we cannot call _ensure_real until all the
1642
        # attributes above are set: This code cannot be moved higher up in this
1643
        # function.
1644
        if format is None:
1645
            self._format = RemoteBranchFormat()
1646
            if real_branch is not None:
1647
                self._format._network_name = \
1648
                    self._real_branch._format.network_name()
1649
            #else:
1650
            #    # XXX: Need to get this from BzrDir.open_branch's return value.
1651
            #    self._ensure_real()
1652
            #    self._format._network_name = \
1653
            #        self._real_branch._format.network_name()
4032.3.1 by Robert Collins
Add a BranchFormat.network_name() method as preparation for creating branches via RPC calls.
1654
        else:
4032.3.2 by Robert Collins
Create and use a RPC call to create branches on bzr servers rather than using VFS calls.
1655
            self._format = format
3681.1.2 by Robert Collins
Adjust for trunk.
1656
        # The base class init is not called, so we duplicate this:
3681.1.1 by Robert Collins
Create a new hook Branch.open. (Robert Collins)
1657
        hooks = branch.Branch.hooks['open']
1658
        for hook in hooks:
1659
            hook(self)
4032.3.2 by Robert Collins
Create and use a RPC call to create branches on bzr servers rather than using VFS calls.
1660
        if setup_stacking:
1661
            self._setup_stacking()
3691.2.1 by Martin Pool
RemoteBranch must configure stacking into the repository
1662
1663
    def _setup_stacking(self):
1664
        # configure stacking into the remote repository, by reading it from
3691.2.3 by Martin Pool
Factor out RemoteBranch._remote_path() and disable RemoteBranch stacking
1665
        # the vfs branch.
3691.2.1 by Martin Pool
RemoteBranch must configure stacking into the repository
1666
        try:
1667
            fallback_url = self.get_stacked_on_url()
1668
        except (errors.NotStacked, errors.UnstackableBranchFormat,
1669
            errors.UnstackableRepositoryFormat), e:
3691.2.7 by Martin Pool
FakeClient can know what calls to expect
1670
            return
1671
        # it's relative to this branch...
1672
        fallback_url = urlutils.join(self.base, fallback_url)
1673
        transports = [self.bzrdir.root_transport]
1674
        if self._real_branch is not None:
4035.2.2 by Robert Collins
Minor tweaks to fix failing tests.
1675
            # The real repository is setup already:
3691.2.7 by Martin Pool
FakeClient can know what calls to expect
1676
            transports.append(self._real_branch._transport)
4035.2.2 by Robert Collins
Minor tweaks to fix failing tests.
1677
            self.repository.add_fallback_repository(
1678
                self.repository._real_repository._fallback_repositories[0])
1679
        else:
1680
            stacked_on = branch.Branch.open(fallback_url,
1681
                                            possible_transports=transports)
1682
            self.repository.add_fallback_repository(stacked_on.repository)
1752.2.64 by Andrew Bennetts
Improve how RemoteBzrDir.open_branch works to handle references and not double-open repositories.
1683
3468.1.1 by Martin Pool
Update more users of default file modes from control_files to bzrdir
1684
    def _get_real_transport(self):
3407.2.16 by Martin Pool
Remove RemoteBranch reliance on control_files._transport
1685
        # if we try vfs access, return the real branch's vfs transport
1686
        self._ensure_real()
1687
        return self._real_branch._transport
1688
3468.1.1 by Martin Pool
Update more users of default file modes from control_files to bzrdir
1689
    _transport = property(_get_real_transport)
3407.2.16 by Martin Pool
Remove RemoteBranch reliance on control_files._transport
1690
2477.1.1 by Martin Pool
Add RemoteBranch repr
1691
    def __str__(self):
1692
        return "%s(%s)" % (self.__class__.__name__, self.base)
1693
1694
    __repr__ = __str__
1695
2018.5.70 by Robert Collins
Only try to get real repositories when an operation requires them.
1696
    def _ensure_real(self):
1697
        """Ensure that there is a _real_branch set.
1698
2018.5.163 by Andrew Bennetts
Deal with various review comments from Robert.
1699
        Used before calls to self._real_branch.
2018.5.70 by Robert Collins
Only try to get real repositories when an operation requires them.
1700
        """
3407.2.16 by Martin Pool
Remove RemoteBranch reliance on control_files._transport
1701
        if self._real_branch is None:
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
1702
            if not vfs.vfs_enabled():
1703
                raise AssertionError('smart server vfs must be enabled '
1704
                    'to use vfs implementation')
2018.5.70 by Robert Collins
Only try to get real repositories when an operation requires them.
1705
            self.bzrdir._ensure_real()
1706
            self._real_branch = self.bzrdir._real_bzrdir.open_branch()
3692.1.3 by Andrew Bennetts
Delete some cruft (like the _ensure_real call in RemoteBranch.lock_write), improve some comments, and wrap some long lines.
1707
            if self.repository._real_repository is None:
1708
                # Give the remote repository the matching real repo.
1709
                real_repo = self._real_branch.repository
1710
                if isinstance(real_repo, RemoteRepository):
1711
                    real_repo._ensure_real()
1712
                    real_repo = real_repo._real_repository
1713
                self.repository._set_real_repository(real_repo)
1714
            # Give the real branch the remote repository to let fast-pathing
1715
            # happen.
2018.5.70 by Robert Collins
Only try to get real repositories when an operation requires them.
1716
            self._real_branch.repository = self.repository
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
1717
            if self._lock_mode == 'r':
1718
                self._real_branch.lock_read()
3692.1.3 by Andrew Bennetts
Delete some cruft (like the _ensure_real call in RemoteBranch.lock_write), improve some comments, and wrap some long lines.
1719
            elif self._lock_mode == 'w':
1720
                self._real_branch.lock_write(token=self._lock_token)
2018.5.70 by Robert Collins
Only try to get real repositories when an operation requires them.
1721
3533.3.1 by Andrew Bennetts
Remove duplication of error translation in bzrlib/remote.py.
1722
    def _translate_error(self, err, **context):
1723
        self.repository._translate_error(err, branch=self, **context)
1724
3441.5.1 by Andrew Bennetts
Avoid necessarily calling get_parent_map when pushing.
1725
    def _clear_cached_state(self):
1726
        super(RemoteBranch, self)._clear_cached_state()
3441.5.5 by Andrew Bennetts
Some small tweaks and comments.
1727
        if self._real_branch is not None:
1728
            self._real_branch._clear_cached_state()
3441.5.29 by Andrew Bennetts
More review tweaks: whitespace nits in test_smart, add (and use) ._clear_cached_state_of_remote_branch_only method in bzrlib/remote.py.
1729
1730
    def _clear_cached_state_of_remote_branch_only(self):
1731
        """Like _clear_cached_state, but doesn't clear the cache of
1732
        self._real_branch.
1733
1734
        This is useful when falling back to calling a method of
1735
        self._real_branch that changes state.  In that case the underlying
1736
        branch changes, so we need to invalidate this RemoteBranch's cache of
1737
        it.  However, there's no need to invalidate the _real_branch's cache
1738
        too, in fact doing so might harm performance.
1739
        """
1740
        super(RemoteBranch, self)._clear_cached_state()
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1741
2018.5.169 by Andrew Bennetts
Add a _server_formats flag to BzrDir.open_from_transport and BzrDirFormat.find_format, make RemoteBranch.control_files into a property.
1742
    @property
1743
    def control_files(self):
1744
        # Defer actually creating RemoteBranchLockableFiles until its needed,
1745
        # because it triggers an _ensure_real that we otherwise might not need.
1746
        if self._control_files is None:
1747
            self._control_files = RemoteBranchLockableFiles(
1748
                self.bzrdir, self._client)
1749
        return self._control_files
1750
2018.5.166 by Andrew Bennetts
Small changes in response to Aaron's review.
1751
    def _get_checkout_format(self):
1752
        self._ensure_real()
1753
        return self._real_branch._get_checkout_format()
1754
2018.5.60 by Robert Collins
More missing methods from RemoteBranch and RemoteRepository to let 'info' get further.
1755
    def get_physical_lock_status(self):
1756
        """See Branch.get_physical_lock_status()."""
1757
        # should be an API call to the server, as branches must be lockable.
2018.5.70 by Robert Collins
Only try to get real repositories when an operation requires them.
1758
        self._ensure_real()
2018.5.60 by Robert Collins
More missing methods from RemoteBranch and RemoteRepository to let 'info' get further.
1759
        return self._real_branch.get_physical_lock_status()
1760
3537.3.1 by Martin Pool
Rename branch.get_stacked_on to get_stacked_on_url
1761
    def get_stacked_on_url(self):
3221.11.2 by Robert Collins
Create basic stackable branch facility.
1762
        """Get the URL this branch is stacked against.
1763
1764
        :raises NotStacked: If the branch is not stacked.
1765
        :raises UnstackableBranchFormat: If the branch does not support
1766
            stacking.
1767
        :raises UnstackableRepositoryFormat: If the repository does not support
1768
            stacking.
1769
        """
3691.2.12 by Martin Pool
Add test for coping without Branch.get_stacked_on_url
1770
        try:
3786.2.3 by Andrew Bennetts
Remove duplicated 'call & translate errors' code in bzrlib.remote.
1771
            # there may not be a repository yet, so we can't use
1772
            # self._translate_error, so we can't use self._call either.
3691.2.12 by Martin Pool
Add test for coping without Branch.get_stacked_on_url
1773
            response = self._client.call('Branch.get_stacked_on_url',
1774
                self._remote_path())
1775
        except errors.ErrorFromSmartServer, err:
1776
            # there may not be a repository yet, so we can't call through
1777
            # its _translate_error
1778
            _translate_error(err, branch=self)
1779
        except errors.UnknownSmartMethod, err:
1780
            self._ensure_real()
1781
            return self._real_branch.get_stacked_on_url()
3786.2.3 by Andrew Bennetts
Remove duplicated 'call & translate errors' code in bzrlib.remote.
1782
        if response[0] != 'ok':
1783
            raise errors.UnexpectedSmartServerResponse(response)
1784
        return response[1]
3221.11.2 by Robert Collins
Create basic stackable branch facility.
1785
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
1786
    def lock_read(self):
3692.1.1 by Andrew Bennetts
Make RemoteBranch.lock_write lock the repository too.
1787
        self.repository.lock_read()
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
1788
        if not self._lock_mode:
1789
            self._lock_mode = 'r'
1790
            self._lock_count = 1
1791
            if self._real_branch is not None:
1792
                self._real_branch.lock_read()
1793
        else:
1794
            self._lock_count += 1
1752.2.52 by Andrew Bennetts
Flesh out more Remote* methods needed to open and initialise remote branches/trees/repositories.
1795
2018.5.142 by Andrew Bennetts
Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens).
1796
    def _remote_lock_write(self, token):
1797
        if token is None:
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
1798
            branch_token = repo_token = ''
1799
        else:
2018.5.142 by Andrew Bennetts
Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens).
1800
            branch_token = token
1801
            repo_token = self.repository.lock_write()
1802
            self.repository.unlock()
3786.2.3 by Andrew Bennetts
Remove duplicated 'call & translate errors' code in bzrlib.remote.
1803
        err_context = {'token': token}
1804
        response = self._call(
1805
            'Branch.lock_write', self._remote_path(), branch_token,
1806
            repo_token or '', **err_context)
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
1807
        if response[0] != 'ok':
2555.1.1 by Martin Pool
Remove use of 'assert False' to raise an exception unconditionally
1808
            raise errors.UnexpectedSmartServerResponse(response)
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
1809
        ok, branch_token, repo_token = response
1810
        return branch_token, repo_token
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1811
2018.5.142 by Andrew Bennetts
Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens).
1812
    def lock_write(self, token=None):
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
1813
        if not self._lock_mode:
3692.1.3 by Andrew Bennetts
Delete some cruft (like the _ensure_real call in RemoteBranch.lock_write), improve some comments, and wrap some long lines.
1814
            # Lock the branch and repo in one remote call.
2018.5.142 by Andrew Bennetts
Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens).
1815
            remote_tokens = self._remote_lock_write(token)
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
1816
            self._lock_token, self._repo_lock_token = remote_tokens
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
1817
            if not self._lock_token:
1818
                raise SmartProtocolError('Remote server did not return a token!')
3692.1.3 by Andrew Bennetts
Delete some cruft (like the _ensure_real call in RemoteBranch.lock_write), improve some comments, and wrap some long lines.
1819
            # Tell the self.repository object that it is locked.
3692.1.2 by Andrew Bennetts
Fix regression introduced by fix, and add a test for that regression.
1820
            self.repository.lock_write(
1821
                self._repo_lock_token, _skip_rpc=True)
3692.1.1 by Andrew Bennetts
Make RemoteBranch.lock_write lock the repository too.
1822
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
1823
            if self._real_branch is not None:
3692.1.5 by Andrew Bennetts
Fix bug revealed by removing _ensure_real call from RemoteBranch.lock_write.
1824
                self._real_branch.lock_write(token=self._lock_token)
2018.5.142 by Andrew Bennetts
Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens).
1825
            if token is not None:
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
1826
                self._leave_lock = True
1827
            else:
1828
                self._leave_lock = False
1829
            self._lock_mode = 'w'
1830
            self._lock_count = 1
1831
        elif self._lock_mode == 'r':
1832
            raise errors.ReadOnlyTransaction
1833
        else:
2018.5.142 by Andrew Bennetts
Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens).
1834
            if token is not None:
3692.1.3 by Andrew Bennetts
Delete some cruft (like the _ensure_real call in RemoteBranch.lock_write), improve some comments, and wrap some long lines.
1835
                # A token was given to lock_write, and we're relocking, so
1836
                # check that the given token actually matches the one we
1837
                # already have.
2018.5.142 by Andrew Bennetts
Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens).
1838
                if token != self._lock_token:
1839
                    raise errors.TokenMismatch(token, self._lock_token)
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
1840
            self._lock_count += 1
3692.1.3 by Andrew Bennetts
Delete some cruft (like the _ensure_real call in RemoteBranch.lock_write), improve some comments, and wrap some long lines.
1841
            # Re-lock the repository too.
3692.1.2 by Andrew Bennetts
Fix regression introduced by fix, and add a test for that regression.
1842
            self.repository.lock_write(self._repo_lock_token)
3015.2.9 by Robert Collins
Handle repositories that do not allow remote locking, like pack repositories, in the client side remote server proxy objects.
1843
        return self._lock_token or None
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
1844
1845
    def _unlock(self, branch_token, repo_token):
3786.2.3 by Andrew Bennetts
Remove duplicated 'call & translate errors' code in bzrlib.remote.
1846
        err_context = {'token': str((branch_token, repo_token))}
1847
        response = self._call(
1848
            'Branch.unlock', self._remote_path(), branch_token,
1849
            repo_token or '', **err_context)
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
1850
        if response == ('ok',):
1851
            return
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
1852
        raise errors.UnexpectedSmartServerResponse(response)
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
1853
1854
    def unlock(self):
3692.1.1 by Andrew Bennetts
Make RemoteBranch.lock_write lock the repository too.
1855
        try:
1856
            self._lock_count -= 1
1857
            if not self._lock_count:
1858
                self._clear_cached_state()
1859
                mode = self._lock_mode
1860
                self._lock_mode = None
1861
                if self._real_branch is not None:
1862
                    if (not self._leave_lock and mode == 'w' and
1863
                        self._repo_lock_token):
1864
                        # If this RemoteBranch will remove the physical lock
1865
                        # for the repository, make sure the _real_branch
1866
                        # doesn't do it first.  (Because the _real_branch's
1867
                        # repository is set to be the RemoteRepository.)
1868
                        self._real_branch.repository.leave_lock_in_place()
1869
                    self._real_branch.unlock()
1870
                if mode != 'w':
1871
                    # Only write-locked branched need to make a remote method
1872
                    # call to perfom the unlock.
1873
                    return
1874
                if not self._lock_token:
1875
                    raise AssertionError('Locked, but no token!')
1876
                branch_token = self._lock_token
1877
                repo_token = self._repo_lock_token
1878
                self._lock_token = None
1879
                self._repo_lock_token = None
1880
                if not self._leave_lock:
1881
                    self._unlock(branch_token, repo_token)
1882
        finally:
1883
            self.repository.unlock()
1752.2.52 by Andrew Bennetts
Flesh out more Remote* methods needed to open and initialise remote branches/trees/repositories.
1884
1885
    def break_lock(self):
2018.5.70 by Robert Collins
Only try to get real repositories when an operation requires them.
1886
        self._ensure_real()
1752.2.52 by Andrew Bennetts
Flesh out more Remote* methods needed to open and initialise remote branches/trees/repositories.
1887
        return self._real_branch.break_lock()
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
1888
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
1889
    def leave_lock_in_place(self):
3015.2.9 by Robert Collins
Handle repositories that do not allow remote locking, like pack repositories, in the client side remote server proxy objects.
1890
        if not self._lock_token:
1891
            raise NotImplementedError(self.leave_lock_in_place)
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
1892
        self._leave_lock = True
1893
1894
    def dont_leave_lock_in_place(self):
3015.2.9 by Robert Collins
Handle repositories that do not allow remote locking, like pack repositories, in the client side remote server proxy objects.
1895
        if not self._lock_token:
3015.2.15 by Robert Collins
Review feedback.
1896
            raise NotImplementedError(self.dont_leave_lock_in_place)
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
1897
        self._leave_lock = False
1898
3441.5.1 by Andrew Bennetts
Avoid necessarily calling get_parent_map when pushing.
1899
    def _last_revision_info(self):
3786.2.3 by Andrew Bennetts
Remove duplicated 'call & translate errors' code in bzrlib.remote.
1900
        response = self._call('Branch.last_revision_info', self._remote_path())
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
1901
        if response[0] != 'ok':
1902
            raise SmartProtocolError('unexpected response code %s' % (response,))
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
1903
        revno = int(response[1])
2018.5.83 by Andrew Bennetts
Fix some test failures caused by the switch from unicode to UTF-8-encoded strs for revision IDs.
1904
        last_revision = response[2]
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
1905
        return (revno, last_revision)
1906
2018.5.105 by Andrew Bennetts
Implement revision_history caching for RemoteBranch.
1907
    def _gen_revision_history(self):
1908
        """See Branch._gen_revision_history()."""
3786.2.3 by Andrew Bennetts
Remove duplicated 'call & translate errors' code in bzrlib.remote.
1909
        response_tuple, response_handler = self._call_expecting_body(
3691.2.3 by Martin Pool
Factor out RemoteBranch._remote_path() and disable RemoteBranch stacking
1910
            'Branch.revision_history', self._remote_path())
3245.4.58 by Andrew Bennetts
Unpack call_expecting_body's return value into variables, to avoid lots of ugly subscripting.
1911
        if response_tuple[0] != 'ok':
3452.2.2 by Andrew Bennetts
Experimental PackRepository.{check_references,autopack} RPCs.
1912
            raise errors.UnexpectedSmartServerResponse(response_tuple)
3245.4.58 by Andrew Bennetts
Unpack call_expecting_body's return value into variables, to avoid lots of ugly subscripting.
1913
        result = response_handler.read_body_bytes().split('\x00')
2018.5.38 by Robert Collins
Implement RemoteBranch.revision_history().
1914
        if result == ['']:
1915
            return []
1916
        return result
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
1917
3691.2.3 by Martin Pool
Factor out RemoteBranch._remote_path() and disable RemoteBranch stacking
1918
    def _remote_path(self):
1919
        return self.bzrdir._path_for_remote_call(self._client)
1920
3441.5.18 by Andrew Bennetts
Fix some test failures.
1921
    def _set_last_revision_descendant(self, revision_id, other_branch,
3441.5.28 by Andrew Bennetts
Another review tweak: rename do_not_overwrite_descendant to allow_overwrite_descendant.
1922
            allow_diverged=False, allow_overwrite_descendant=False):
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
1923
        # This performs additional work to meet the hook contract; while its
1924
        # undesirable, we have to synthesise the revno to call the hook, and
1925
        # not calling the hook is worse as it means changes can't be prevented.
1926
        # Having calculated this though, we can't just call into
1927
        # set_last_revision_info as a simple call, because there is a set_rh
1928
        # hook that some folk may still be using.
1929
        old_revno, old_revid = self.last_revision_info()
1930
        history = self._lefthand_history(revision_id)
1931
        self._run_pre_change_branch_tip_hooks(len(history), revision_id)
3786.2.3 by Andrew Bennetts
Remove duplicated 'call & translate errors' code in bzrlib.remote.
1932
        err_context = {'other_branch': other_branch}
1933
        response = self._call('Branch.set_last_revision_ex',
1934
            self._remote_path(), self._lock_token, self._repo_lock_token,
1935
            revision_id, int(allow_diverged), int(allow_overwrite_descendant),
1936
            **err_context)
3441.5.6 by Andrew Bennetts
Greatly simplify RemoteBranch.update_revisions. Still needs more tests.
1937
        self._clear_cached_state()
3441.5.18 by Andrew Bennetts
Fix some test failures.
1938
        if len(response) != 3 and response[0] != 'ok':
3441.5.6 by Andrew Bennetts
Greatly simplify RemoteBranch.update_revisions. Still needs more tests.
1939
            raise errors.UnexpectedSmartServerResponse(response)
3441.5.18 by Andrew Bennetts
Fix some test failures.
1940
        new_revno, new_revision_id = response[1:]
1941
        self._last_revision_info_cache = new_revno, new_revision_id
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
1942
        self._run_post_change_branch_tip_hooks(old_revno, old_revid)
3692.1.5 by Andrew Bennetts
Fix bug revealed by removing _ensure_real call from RemoteBranch.lock_write.
1943
        if self._real_branch is not None:
1944
            cache = new_revno, new_revision_id
1945
            self._real_branch._last_revision_info_cache = cache
3441.5.6 by Andrew Bennetts
Greatly simplify RemoteBranch.update_revisions. Still needs more tests.
1946
3441.5.1 by Andrew Bennetts
Avoid necessarily calling get_parent_map when pushing.
1947
    def _set_last_revision(self, revision_id):
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
1948
        old_revno, old_revid = self.last_revision_info()
1949
        # This performs additional work to meet the hook contract; while its
1950
        # undesirable, we have to synthesise the revno to call the hook, and
1951
        # not calling the hook is worse as it means changes can't be prevented.
1952
        # Having calculated this though, we can't just call into
1953
        # set_last_revision_info as a simple call, because there is a set_rh
1954
        # hook that some folk may still be using.
1955
        history = self._lefthand_history(revision_id)
1956
        self._run_pre_change_branch_tip_hooks(len(history), revision_id)
3441.5.1 by Andrew Bennetts
Avoid necessarily calling get_parent_map when pushing.
1957
        self._clear_cached_state()
3786.2.3 by Andrew Bennetts
Remove duplicated 'call & translate errors' code in bzrlib.remote.
1958
        response = self._call('Branch.set_last_revision',
1959
            self._remote_path(), self._lock_token, self._repo_lock_token,
1960
            revision_id)
3441.5.1 by Andrew Bennetts
Avoid necessarily calling get_parent_map when pushing.
1961
        if response != ('ok',):
1962
            raise errors.UnexpectedSmartServerResponse(response)
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
1963
        self._run_post_change_branch_tip_hooks(old_revno, old_revid)
3441.5.1 by Andrew Bennetts
Avoid necessarily calling get_parent_map when pushing.
1964
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
1965
    @needs_write_lock
1752.2.52 by Andrew Bennetts
Flesh out more Remote* methods needed to open and initialise remote branches/trees/repositories.
1966
    def set_revision_history(self, rev_history):
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
1967
        # Send just the tip revision of the history; the server will generate
1968
        # the full history from that.  If the revision doesn't exist in this
1969
        # branch, NoSuchRevision will be raised.
1970
        if rev_history == []:
2018.5.170 by Andrew Bennetts
Use 'null:' instead of '' to mean NULL_REVISION on the wire.
1971
            rev_id = 'null:'
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
1972
        else:
1973
            rev_id = rev_history[-1]
3441.5.1 by Andrew Bennetts
Avoid necessarily calling get_parent_map when pushing.
1974
        self._set_last_revision(rev_id)
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
1975
        for hook in branch.Branch.hooks['set_rh']:
1976
            hook(self, rev_history)
2018.5.105 by Andrew Bennetts
Implement revision_history caching for RemoteBranch.
1977
        self._cache_revision_history(rev_history)
1752.2.52 by Andrew Bennetts
Flesh out more Remote* methods needed to open and initialise remote branches/trees/repositories.
1978
1979
    def get_parent(self):
2018.5.70 by Robert Collins
Only try to get real repositories when an operation requires them.
1980
        self._ensure_real()
1752.2.52 by Andrew Bennetts
Flesh out more Remote* methods needed to open and initialise remote branches/trees/repositories.
1981
        return self._real_branch.get_parent()
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1982
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
1983
    def _get_parent_location(self):
1984
        # Used by tests, when checking normalisation of given vs stored paths.
1985
        self._ensure_real()
1986
        return self._real_branch._get_parent_location()
4032.1.1 by John Arbash Meinel
Merge the removal of all trailing whitespace, and resolve conflicts.
1987
1752.2.63 by Andrew Bennetts
Delegate set_parent.
1988
    def set_parent(self, url):
2018.5.70 by Robert Collins
Only try to get real repositories when an operation requires them.
1989
        self._ensure_real()
1752.2.63 by Andrew Bennetts
Delegate set_parent.
1990
        return self._real_branch.set_parent(url)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1991
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
1992
    def _set_parent_location(self, url):
1993
        # Used by tests, to poke bad urls into branch configurations
1994
        if url is None:
1995
            self.set_parent(url)
1996
        else:
1997
            self._ensure_real()
1998
            return self._real_branch._set_parent_location(url)
4032.1.1 by John Arbash Meinel
Merge the removal of all trailing whitespace, and resolve conflicts.
1999
3537.3.3 by Martin Pool
Rename Branch.set_stacked_on to set_stacked_on_url
2000
    def set_stacked_on_url(self, stacked_location):
3221.18.1 by Ian Clatworthy
tweaks by ianc during review
2001
        """Set the URL this branch is stacked against.
3221.11.2 by Robert Collins
Create basic stackable branch facility.
2002
2003
        :raises UnstackableBranchFormat: If the branch does not support
2004
            stacking.
2005
        :raises UnstackableRepositoryFormat: If the repository does not support
2006
            stacking.
2007
        """
2008
        self._ensure_real()
3537.3.3 by Martin Pool
Rename Branch.set_stacked_on to set_stacked_on_url
2009
        return self._real_branch.set_stacked_on_url(stacked_location)
3221.11.2 by Robert Collins
Create basic stackable branch facility.
2010
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
2011
    @needs_write_lock
2477.1.2 by Martin Pool
Rename push/pull back to 'run_hooks' (jameinel)
2012
    def pull(self, source, overwrite=False, stop_revision=None,
2477.1.9 by Martin Pool
Review cleanups from John, mostly docs
2013
             **kwargs):
3441.5.29 by Andrew Bennetts
More review tweaks: whitespace nits in test_smart, add (and use) ._clear_cached_state_of_remote_branch_only method in bzrlib/remote.py.
2014
        self._clear_cached_state_of_remote_branch_only()
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
2015
        self._ensure_real()
3482.1.1 by John Arbash Meinel
Fix bug #238149, RemoteBranch.pull needs to return the _real_branch's pull result.
2016
        return self._real_branch.pull(
2477.1.2 by Martin Pool
Rename push/pull back to 'run_hooks' (jameinel)
2017
            source, overwrite=overwrite, stop_revision=stop_revision,
3489.2.4 by Andrew Bennetts
Fix all tests broken by fixing make_branch_and_tree.
2018
            _override_hook_target=self, **kwargs)
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
2019
2018.14.3 by Andrew Bennetts
Make a couple more branch_implementations tests pass.
2020
    @needs_read_lock
2021
    def push(self, target, overwrite=False, stop_revision=None):
2022
        self._ensure_real()
2018.5.97 by Andrew Bennetts
Fix more tests.
2023
        return self._real_branch.push(
2477.1.5 by Martin Pool
More cleanups of Branch.push to get the right behaviour with RemoteBranches
2024
            target, overwrite=overwrite, stop_revision=stop_revision,
2025
            _override_hook_source_branch=self)
2018.14.3 by Andrew Bennetts
Make a couple more branch_implementations tests pass.
2026
2027
    def is_locked(self):
2028
        return self._lock_count >= 1
2029
3634.2.1 by John Arbash Meinel
Thunk over to the real branch's revision_id_to_revno.
2030
    @needs_read_lock
2031
    def revision_id_to_revno(self, revision_id):
2032
        self._ensure_real()
2033
        return self._real_branch.revision_id_to_revno(revision_id)
2034
2892.2.1 by Andrew Bennetts
Add Branch.set_last_revision_info smart method, and make the RemoteBranch client use it.
2035
    @needs_write_lock
2018.5.83 by Andrew Bennetts
Fix some test failures caused by the switch from unicode to UTF-8-encoded strs for revision IDs.
2036
    def set_last_revision_info(self, revno, revision_id):
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
2037
        # XXX: These should be returned by the set_last_revision_info verb
2038
        old_revno, old_revid = self.last_revision_info()
2039
        self._run_pre_change_branch_tip_hooks(revno, revision_id)
2892.2.1 by Andrew Bennetts
Add Branch.set_last_revision_info smart method, and make the RemoteBranch client use it.
2040
        revision_id = ensure_null(revision_id)
3297.4.2 by Andrew Bennetts
Add backwards compatibility for servers older than 1.4.
2041
        try:
3786.2.3 by Andrew Bennetts
Remove duplicated 'call & translate errors' code in bzrlib.remote.
2042
            response = self._call('Branch.set_last_revision_info',
2043
                self._remote_path(), self._lock_token, self._repo_lock_token,
2044
                str(revno), revision_id)
3297.4.2 by Andrew Bennetts
Add backwards compatibility for servers older than 1.4.
2045
        except errors.UnknownSmartMethod:
2046
            self._ensure_real()
3441.5.29 by Andrew Bennetts
More review tweaks: whitespace nits in test_smart, add (and use) ._clear_cached_state_of_remote_branch_only method in bzrlib/remote.py.
2047
            self._clear_cached_state_of_remote_branch_only()
3441.5.1 by Andrew Bennetts
Avoid necessarily calling get_parent_map when pushing.
2048
            self._real_branch.set_last_revision_info(revno, revision_id)
2049
            self._last_revision_info_cache = revno, revision_id
2050
            return
2892.2.1 by Andrew Bennetts
Add Branch.set_last_revision_info smart method, and make the RemoteBranch client use it.
2051
        if response == ('ok',):
2052
            self._clear_cached_state()
3441.5.1 by Andrew Bennetts
Avoid necessarily calling get_parent_map when pushing.
2053
            self._last_revision_info_cache = revno, revision_id
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
2054
            self._run_post_change_branch_tip_hooks(old_revno, old_revid)
3441.5.29 by Andrew Bennetts
More review tweaks: whitespace nits in test_smart, add (and use) ._clear_cached_state_of_remote_branch_only method in bzrlib/remote.py.
2055
            # Update the _real_branch's cache too.
2056
            if self._real_branch is not None:
2057
                cache = self._last_revision_info_cache
2058
                self._real_branch._last_revision_info_cache = cache
2892.2.1 by Andrew Bennetts
Add Branch.set_last_revision_info smart method, and make the RemoteBranch client use it.
2059
        else:
2060
            raise errors.UnexpectedSmartServerResponse(response)
2018.5.83 by Andrew Bennetts
Fix some test failures caused by the switch from unicode to UTF-8-encoded strs for revision IDs.
2061
3441.5.27 by Andrew Bennetts
Tweaks suggested by John's review: rename _check_if_descendant_or_diverged, move caching last_revision_info into base Branch, better use of lock decorators.
2062
    @needs_write_lock
2018.5.95 by Andrew Bennetts
Add a Transport.is_readonly remote call, let {Branch,Repository}.lock_write remote call return UnlockableTransport, and miscellaneous test fixes.
2063
    def generate_revision_history(self, revision_id, last_rev=None,
2064
                                  other_branch=None):
3441.5.6 by Andrew Bennetts
Greatly simplify RemoteBranch.update_revisions. Still needs more tests.
2065
        medium = self._client._medium
3441.5.23 by Andrew Bennetts
Fix test failures.
2066
        if not medium._is_remote_before((1, 6)):
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
2067
            # Use a smart method for 1.6 and above servers
3441.5.6 by Andrew Bennetts
Greatly simplify RemoteBranch.update_revisions. Still needs more tests.
2068
            try:
3441.5.18 by Andrew Bennetts
Fix some test failures.
2069
                self._set_last_revision_descendant(revision_id, other_branch,
3441.5.28 by Andrew Bennetts
Another review tweak: rename do_not_overwrite_descendant to allow_overwrite_descendant.
2070
                    allow_diverged=True, allow_overwrite_descendant=True)
3441.5.6 by Andrew Bennetts
Greatly simplify RemoteBranch.update_revisions. Still needs more tests.
2071
                return
3441.5.18 by Andrew Bennetts
Fix some test failures.
2072
            except errors.UnknownSmartMethod:
3441.5.23 by Andrew Bennetts
Fix test failures.
2073
                medium._remember_remote_is_before((1, 6))
3441.5.29 by Andrew Bennetts
More review tweaks: whitespace nits in test_smart, add (and use) ._clear_cached_state_of_remote_branch_only method in bzrlib/remote.py.
2074
        self._clear_cached_state_of_remote_branch_only()
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
2075
        self.set_revision_history(self._lefthand_history(revision_id,
2076
            last_rev=last_rev,other_branch=other_branch))
2018.5.95 by Andrew Bennetts
Add a Transport.is_readonly remote call, let {Branch,Repository}.lock_write remote call return UnlockableTransport, and miscellaneous test fixes.
2077
2018.5.96 by Andrew Bennetts
Merge from bzr.dev, resolving the worst of the semantic conflicts, but there's
2078
    @property
2079
    def tags(self):
2080
        self._ensure_real()
2081
        return self._real_branch.tags
2082
2018.5.97 by Andrew Bennetts
Fix more tests.
2083
    def set_push_location(self, location):
2084
        self._ensure_real()
2085
        return self._real_branch.set_push_location(location)
2086
2018.18.25 by Martin Pool
Repository.tarball fixes for python2.4
2087
2088
def _extract_tar(tar, to_dir):
2089
    """Extract all the contents of a tarfile object.
2090
2091
    A replacement for extractall, which is not present in python2.4
2092
    """
2093
    for tarinfo in tar:
2094
        tar.extract(tarinfo, to_dir)
3533.3.1 by Andrew Bennetts
Remove duplication of error translation in bzrlib/remote.py.
2095
2096
2097
def _translate_error(err, **context):
2098
    """Translate an ErrorFromSmartServer into a more useful error.
2099
2100
    Possible context keys:
2101
      - branch
2102
      - repository
2103
      - bzrdir
2104
      - token
2105
      - other_branch
3779.3.2 by Andrew Bennetts
Unify error translation done in bzrlib.remote and bzrlib.transport.remote.
2106
      - path
3690.1.1 by Andrew Bennetts
Unexpected error responses from a smart server no longer cause the client to traceback.
2107
2108
    If the error from the server doesn't match a known pattern, then
3690.1.2 by Andrew Bennetts
Rename UntranslateableErrorFromSmartServer -> UnknownErrorFromSmartServer.
2109
    UnknownErrorFromSmartServer is raised.
3533.3.1 by Andrew Bennetts
Remove duplication of error translation in bzrlib/remote.py.
2110
    """
2111
    def find(name):
3533.3.4 by Andrew Bennetts
Add tests for _translate_error's robustness.
2112
        try:
2113
            return context[name]
3779.3.2 by Andrew Bennetts
Unify error translation done in bzrlib.remote and bzrlib.transport.remote.
2114
        except KeyError, key_err:
2115
            mutter('Missing key %r in context %r', key_err.args[0], context)
3533.3.4 by Andrew Bennetts
Add tests for _translate_error's robustness.
2116
            raise err
3779.3.2 by Andrew Bennetts
Unify error translation done in bzrlib.remote and bzrlib.transport.remote.
2117
    def get_path():
3779.3.3 by Andrew Bennetts
Add a docstring.
2118
        """Get the path from the context if present, otherwise use first error
2119
        arg.
2120
        """
3779.3.2 by Andrew Bennetts
Unify error translation done in bzrlib.remote and bzrlib.transport.remote.
2121
        try:
2122
            return context['path']
2123
        except KeyError, key_err:
2124
            try:
2125
                return err.error_args[0]
2126
            except IndexError, idx_err:
2127
                mutter(
2128
                    'Missing key %r in context %r', key_err.args[0], context)
2129
                raise err
2130
3533.3.1 by Andrew Bennetts
Remove duplication of error translation in bzrlib/remote.py.
2131
    if err.error_verb == 'NoSuchRevision':
2132
        raise NoSuchRevision(find('branch'), err.error_args[0])
2133
    elif err.error_verb == 'nosuchrevision':
2134
        raise NoSuchRevision(find('repository'), err.error_args[0])
2135
    elif err.error_tuple == ('nobranch',):
3533.3.4 by Andrew Bennetts
Add tests for _translate_error's robustness.
2136
        raise errors.NotBranchError(path=find('bzrdir').root_transport.base)
3533.3.1 by Andrew Bennetts
Remove duplication of error translation in bzrlib/remote.py.
2137
    elif err.error_verb == 'norepository':
2138
        raise errors.NoRepositoryPresent(find('bzrdir'))
2139
    elif err.error_verb == 'LockContention':
2140
        raise errors.LockContention('(remote lock)')
2141
    elif err.error_verb == 'UnlockableTransport':
3533.3.4 by Andrew Bennetts
Add tests for _translate_error's robustness.
2142
        raise errors.UnlockableTransport(find('bzrdir').root_transport)
3533.3.1 by Andrew Bennetts
Remove duplication of error translation in bzrlib/remote.py.
2143
    elif err.error_verb == 'LockFailed':
2144
        raise errors.LockFailed(err.error_args[0], err.error_args[1])
2145
    elif err.error_verb == 'TokenMismatch':
2146
        raise errors.TokenMismatch(find('token'), '(remote token)')
2147
    elif err.error_verb == 'Diverged':
2148
        raise errors.DivergedBranches(find('branch'), find('other_branch'))
3577.1.1 by Andrew Bennetts
Cherry-pick TipChangeRejected changes from pre-branch-tip-changed-hook loom.
2149
    elif err.error_verb == 'TipChangeRejected':
2150
        raise errors.TipChangeRejected(err.error_args[0].decode('utf8'))
3691.2.6 by Martin Pool
Disable RemoteBranch stacking, but get get_stacked_on_url working, and passing back exceptions
2151
    elif err.error_verb == 'UnstackableBranchFormat':
2152
        raise errors.UnstackableBranchFormat(*err.error_args)
2153
    elif err.error_verb == 'UnstackableRepositoryFormat':
2154
        raise errors.UnstackableRepositoryFormat(*err.error_args)
2155
    elif err.error_verb == 'NotStacked':
2156
        raise errors.NotStacked(branch=find('branch'))
3779.3.1 by Andrew Bennetts
Move encoding/decoding logic of PermissionDenied and ReadError so that it happens for all RPCs.
2157
    elif err.error_verb == 'PermissionDenied':
3779.3.2 by Andrew Bennetts
Unify error translation done in bzrlib.remote and bzrlib.transport.remote.
2158
        path = get_path()
3779.3.1 by Andrew Bennetts
Move encoding/decoding logic of PermissionDenied and ReadError so that it happens for all RPCs.
2159
        if len(err.error_args) >= 2:
2160
            extra = err.error_args[1]
3779.3.2 by Andrew Bennetts
Unify error translation done in bzrlib.remote and bzrlib.transport.remote.
2161
        else:
2162
            extra = None
3779.3.1 by Andrew Bennetts
Move encoding/decoding logic of PermissionDenied and ReadError so that it happens for all RPCs.
2163
        raise errors.PermissionDenied(path, extra=extra)
3779.3.2 by Andrew Bennetts
Unify error translation done in bzrlib.remote and bzrlib.transport.remote.
2164
    elif err.error_verb == 'ReadError':
2165
        path = get_path()
2166
        raise errors.ReadError(path)
2167
    elif err.error_verb == 'NoSuchFile':
2168
        path = get_path()
2169
        raise errors.NoSuchFile(path)
2170
    elif err.error_verb == 'FileExists':
2171
        raise errors.FileExists(err.error_args[0])
2172
    elif err.error_verb == 'DirectoryNotEmpty':
2173
        raise errors.DirectoryNotEmpty(err.error_args[0])
2174
    elif err.error_verb == 'ShortReadvError':
2175
        args = err.error_args
2176
        raise errors.ShortReadvError(
2177
            args[0], int(args[1]), int(args[2]), int(args[3]))
2178
    elif err.error_verb in ('UnicodeEncodeError', 'UnicodeDecodeError'):
2179
        encoding = str(err.error_args[0]) # encoding must always be a string
2180
        val = err.error_args[1]
2181
        start = int(err.error_args[2])
2182
        end = int(err.error_args[3])
2183
        reason = str(err.error_args[4]) # reason must always be a string
2184
        if val.startswith('u:'):
2185
            val = val[2:].decode('utf-8')
2186
        elif val.startswith('s:'):
2187
            val = val[2:].decode('base64')
3786.2.3 by Andrew Bennetts
Remove duplicated 'call & translate errors' code in bzrlib.remote.
2188
        if err.error_verb == 'UnicodeDecodeError':
3779.3.2 by Andrew Bennetts
Unify error translation done in bzrlib.remote and bzrlib.transport.remote.
2189
            raise UnicodeDecodeError(encoding, val, start, end, reason)
3786.2.3 by Andrew Bennetts
Remove duplicated 'call & translate errors' code in bzrlib.remote.
2190
        elif err.error_verb == 'UnicodeEncodeError':
3779.3.2 by Andrew Bennetts
Unify error translation done in bzrlib.remote and bzrlib.transport.remote.
2191
            raise UnicodeEncodeError(encoding, val, start, end, reason)
2192
    elif err.error_verb == 'ReadOnlyError':
2193
        raise errors.TransportNotPossible('readonly transport')
3690.1.2 by Andrew Bennetts
Rename UntranslateableErrorFromSmartServer -> UnknownErrorFromSmartServer.
2194
    raise errors.UnknownErrorFromSmartServer(err)