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