/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
1
# Copyright (C) 2006, 2007 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
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
20
from cStringIO import StringIO
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.
21
from urlparse import urlparse
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
22
2018.5.127 by Andrew Bennetts
Fix most of the lockable_files tests for RemoteBranchLockableFiles.
23
from bzrlib import branch, errors, lockdir, repository
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
24
from bzrlib.branch import BranchReferenceFormat
2018.5.25 by Andrew Bennetts
Make sure RemoteBzrDirFormat is always registered (John Arbash Meinel, Robert Collins, Andrew Bennetts).
25
from bzrlib.bzrdir import BzrDir, BzrDirFormat, RemoteBzrDirFormat
2018.14.2 by Andrew Bennetts
All but one repository_implementation tests for RemoteRepository passing.
26
from bzrlib.config import BranchConfig, TreeConfig
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
27
from bzrlib.decorators import needs_read_lock, needs_write_lock
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
28
from bzrlib.errors import NoSuchRevision
2018.5.127 by Andrew Bennetts
Fix most of the lockable_files tests for RemoteBranchLockableFiles.
29
from bzrlib.lockable_files import LockableFiles
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
30
from bzrlib.revision import NULL_REVISION
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
31
from bzrlib.smart import client, vfs
2018.5.32 by Robert Collins
Unescape urls before handing over the wire to the smart server for the probe_transport method.
32
from bzrlib.urlutils import unescape
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
33
2018.5.25 by Andrew Bennetts
Make sure RemoteBzrDirFormat is always registered (John Arbash Meinel, Robert Collins, Andrew Bennetts).
34
# Note: RemoteBzrDirFormat is in bzrdir.py
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
35
36
class RemoteBzrDir(BzrDir):
37
    """Control directory on a remote server, accessed by HPSS."""
38
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
39
    def __init__(self, transport, _client=None):
40
        """Construct a RemoteBzrDir.
41
42
        :param _client: Private parameter for testing. Disables probing and the
43
            use of a real bzrdir.
44
        """
1752.2.39 by Martin Pool
[broken] implement upgrade apis on remote bzrdirs
45
        BzrDir.__init__(self, transport, RemoteBzrDirFormat())
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
46
        # this object holds a delegated bzrdir that uses file-level operations
47
        # to talk to the other side
1752.2.52 by Andrew Bennetts
Flesh out more Remote* methods needed to open and initialise remote branches/trees/repositories.
48
        # XXX: We should go into find_format, but not allow it to find
49
        # RemoteBzrDirFormat and make sure it finds the real underlying format.
2018.5.70 by Robert Collins
Only try to get real repositories when an operation requires them.
50
        self._real_bzrdir = None
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
51
52
        if _client is None:
53
            self._medium = transport.get_smart_client()
54
            self._client = client.SmartClient(self._medium)
55
        else:
56
            self._client = _client
57
            self._medium = None
58
            return
59
2018.5.70 by Robert Collins
Only try to get real repositories when an operation requires them.
60
        self._ensure_real()
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
61
        path = self._path_for_remote_call(self._client)
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.
62
        #self._real_bzrdir._format.probe_transport(transport)
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
63
        response = self._client.call('probe_dont_use', path)
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.
64
        if response == ('no',):
65
            raise errors.NotBranchError(path=transport.base)
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
66
2018.5.70 by Robert Collins
Only try to get real repositories when an operation requires them.
67
    def _ensure_real(self):
68
        """Ensure that there is a _real_bzrdir set.
69
70
        used before calls to self._real_bzrdir.
71
        """
72
        if not self._real_bzrdir:
73
            default_format = BzrDirFormat.get_default_format()
74
            self._real_bzrdir = default_format.open(self.root_transport,
75
                _found=True)
76
1752.2.39 by Martin Pool
[broken] implement upgrade apis on remote bzrdirs
77
    def create_repository(self, shared=False):
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
78
        self._real_bzrdir.create_repository(shared=shared)
79
        return self.open_repository()
1752.2.50 by Andrew Bennetts
Implement RemoteBzrDir.create_{branch,workingtree}
80
81
    def create_branch(self):
1752.2.72 by Andrew Bennetts
Make Remote* classes in remote.py more consistent and remove some dead code.
82
        real_branch = self._real_bzrdir.create_branch()
2018.5.70 by Robert Collins
Only try to get real repositories when an operation requires them.
83
        return RemoteBranch(self, self.find_repository(), real_branch)
1752.2.50 by Andrew Bennetts
Implement RemoteBzrDir.create_{branch,workingtree}
84
85
    def create_workingtree(self, revision_id=None):
1752.2.72 by Andrew Bennetts
Make Remote* classes in remote.py more consistent and remove some dead code.
86
        real_workingtree = self._real_bzrdir.create_workingtree(revision_id=revision_id)
87
        return RemoteWorkingTree(self, real_workingtree)
1752.2.39 by Martin Pool
[broken] implement upgrade apis on remote bzrdirs
88
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.
89
    def find_branch_format(self):
90
        """Find the branch 'format' for this bzrdir.
91
92
        This might be a synthetic object for e.g. RemoteBranch and SVN.
93
        """
94
        b = self.open_branch()
95
        return b._format
96
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.
97
    def get_branch_reference(self):
98
        """See BzrDir.get_branch_reference()."""
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
99
        path = self._path_for_remote_call(self._client)
100
        response = self._client.call('BzrDir.open_branch', path)
101
        if response[0] == 'ok':
102
            if response[1] == '':
103
                # 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.
104
                return None
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
105
            else:
106
                # 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.
107
                return response[1]
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
108
        elif response == ('nobranch',):
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
109
            raise errors.NotBranchError(path=self.root_transport.base)
110
        else:
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
111
            assert False, 'unexpected response code %r' % (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.
112
113
    def open_branch(self, _unsupported=False):
114
        assert _unsupported == False, 'unsupported flag support not implemented yet.'
115
        reference_url = self.get_branch_reference()
116
        if reference_url is None:
117
            # branch at this location.
118
            return RemoteBranch(self, self.find_repository())
119
        else:
120
            # a branch reference, use the existing BranchReference logic.
121
            format = BranchReferenceFormat()
122
            return format.open(self, _found=True, location=reference_url)
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
123
                
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
124
    def open_repository(self):
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
125
        path = self._path_for_remote_call(self._client)
126
        response = self._client.call('BzrDir.find_repository', path)
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
127
        assert response[0] in ('ok', 'norepository'), \
2018.5.52 by Wouter van Heyst
Provide more information when encountering unexpected responses from a smart
128
            'unexpected response code %s' % (response,)
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
129
        if response[0] == 'norepository':
130
            raise errors.NoRepositoryPresent(self)
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
131
        assert len(response) == 4, '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.
132
        if response[1] == '':
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
133
            format = RemoteRepositoryFormat()
134
            format.rich_root_data = response[2] == 'True'
135
            format.support_tree_reference = response[3] == 'True'
136
            return RemoteRepository(self, format)
2018.5.34 by Robert Collins
Get test_remote.BasicRemoteObjectTests.test_open_remote_branch passing by implementing a remote method BzrDir.find_repository.
137
        else:
138
            raise errors.NoRepositoryPresent(self)
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
139
1752.2.52 by Andrew Bennetts
Flesh out more Remote* methods needed to open and initialise remote branches/trees/repositories.
140
    def open_workingtree(self):
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.
141
        raise errors.NotLocalUrl(self.root_transport)
1752.2.50 by Andrew Bennetts
Implement RemoteBzrDir.create_{branch,workingtree}
142
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
143
    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.
144
        """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 :).
145
        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.
146
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
147
    def get_branch_transport(self, branch_format):
148
        return self._real_bzrdir.get_branch_transport(branch_format)
149
1752.2.43 by Andrew Bennetts
Fix get_{branch,repository,workingtree}_transport.
150
    def get_repository_transport(self, repository_format):
151
        return self._real_bzrdir.get_repository_transport(repository_format)
152
153
    def get_workingtree_transport(self, workingtree_format):
154
        return self._real_bzrdir.get_workingtree_transport(workingtree_format)
155
1752.2.39 by Martin Pool
[broken] implement upgrade apis on remote bzrdirs
156
    def can_convert_format(self):
157
        """Upgrading of remote bzrdirs is not supported yet."""
158
        return False
159
160
    def needs_format_conversion(self, format=None):
161
        """Upgrading of remote bzrdirs is not supported yet."""
162
        return False
163
2018.5.94 by Andrew Bennetts
Various small changes in aid of making tests pass (including deleting one invalid test).
164
    def clone(self, url, revision_id=None, basis=None, force_new_repo=False):
165
        self._ensure_real()
166
        return self._real_bzrdir.clone(url, revision_id=revision_id,
167
            basis=basis, force_new_repo=force_new_repo)
168
169
    #def sprout(self, url, revision_id=None, basis=None, force_new_repo=False):
170
    #    self._ensure_real()
171
    #    return self._real_bzrdir.sprout(url, revision_id=revision_id,
172
    #        basis=basis, force_new_repo=force_new_repo)
173
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
174
1752.2.52 by Andrew Bennetts
Flesh out more Remote* methods needed to open and initialise remote branches/trees/repositories.
175
class RemoteRepositoryFormat(repository.RepositoryFormat):
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
176
    """Format for repositories accessed over a SmartClient.
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
177
1752.2.50 by Andrew Bennetts
Implement RemoteBzrDir.create_{branch,workingtree}
178
    Instances of this repository are represented by RemoteRepository
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
179
    instances.
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
180
181
    The RemoteRepositoryFormat is parameterised during construction
182
    to reflect the capabilities of the real, remote format. Specifically
183
    the attributes rich_root_data and support_tree_reference are set
184
    on a per instance basis, and are not set (and should not be) at
185
    the class level.
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
186
    """
187
188
    _matchingbzrdir = RemoteBzrDirFormat
189
1752.2.52 by Andrew Bennetts
Flesh out more Remote* methods needed to open and initialise remote branches/trees/repositories.
190
    def initialize(self, a_bzrdir, shared=False):
1752.2.72 by Andrew Bennetts
Make Remote* classes in remote.py more consistent and remove some dead code.
191
        assert isinstance(a_bzrdir, RemoteBzrDir)
192
        return a_bzrdir.create_repository(shared=shared)
1752.2.52 by Andrew Bennetts
Flesh out more Remote* methods needed to open and initialise remote branches/trees/repositories.
193
    
194
    def open(self, a_bzrdir):
1752.2.72 by Andrew Bennetts
Make Remote* classes in remote.py more consistent and remove some dead code.
195
        assert isinstance(a_bzrdir, RemoteBzrDir)
196
        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.
197
198
    def get_format_description(self):
199
        return 'bzr remote repository'
200
201
    def __eq__(self, other):
1752.2.87 by Andrew Bennetts
Make tests pass.
202
        return self.__class__ == other.__class__
203
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
204
    def check_conversion_target(self, target_format):
205
        if self.rich_root_data and not target_format.rich_root_data:
206
            raise errors.BadConversionTarget(
207
                'Does not support rich root data.', target_format)
208
        if (self.support_tree_reference and
209
            not getattr(target_format, 'support_tree_reference', False)):
210
            raise errors.BadConversionTarget(
211
                '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.
212
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
213
1752.2.50 by Andrew Bennetts
Implement RemoteBzrDir.create_{branch,workingtree}
214
class RemoteRepository(object):
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
215
    """Repository accessed over rpc.
216
1752.2.50 by Andrew Bennetts
Implement RemoteBzrDir.create_{branch,workingtree}
217
    For the moment everything is delegated to IO-like operations over
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
218
    the transport.
219
    """
220
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
221
    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.
222
        """Create a RemoteRepository instance.
223
        
224
        :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.
225
        :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.
226
        :param real_repository: If not None, a local implementation of the
227
            repository logic for the repository, usually accessing the data
228
            via the VFS.
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
229
        :param _client: Private testing parameter - override the smart client
230
            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.
231
        """
232
        if real_repository:
2018.5.36 by Andrew Bennetts
Fix typo, and clean up some ununsed import warnings from pyflakes at the same time.
233
            self._real_repository = real_repository
2018.5.70 by Robert Collins
Only try to get real repositories when an operation requires them.
234
        else:
235
            self._real_repository = None
1752.2.50 by Andrew Bennetts
Implement RemoteBzrDir.create_{branch,workingtree}
236
        self.bzrdir = remote_bzrdir
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
237
        if _client is None:
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
238
            self._client = client.SmartClient(self.bzrdir._medium)
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
239
        else:
240
            self._client = _client
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
241
        self._format = format
2018.5.75 by Andrew Bennetts
Add Repository.{dont_,}leave_lock_in_place.
242
        self._lock_mode = None
243
        self._lock_token = None
244
        self._lock_count = 0
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
245
        self._leave_lock = False
1752.2.52 by Andrew Bennetts
Flesh out more Remote* methods needed to open and initialise remote branches/trees/repositories.
246
2018.5.70 by Robert Collins
Only try to get real repositories when an operation requires them.
247
    def _ensure_real(self):
248
        """Ensure that there is a _real_repository set.
249
250
        used before calls to self._real_repository.
251
        """
252
        if not self._real_repository:
253
            self.bzrdir._ensure_real()
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
254
            #self._real_repository = self.bzrdir._real_bzrdir.open_repository()
255
            self._set_real_repository(self.bzrdir._real_bzrdir.open_repository())
2018.5.70 by Robert Collins
Only try to get real repositories when an operation requires them.
256
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
257
    def get_revision_graph(self, revision_id=None):
258
        """See Repository.get_revision_graph()."""
259
        if revision_id is None:
260
            revision_id = ''
261
        elif revision_id == NULL_REVISION:
262
            return {}
263
264
        path = self.bzrdir._path_for_remote_call(self._client)
2018.5.83 by Andrew Bennetts
Fix some test failures caused by the switch from unicode to UTF-8-encoded strs for revision IDs.
265
        assert type(revision_id) is str
266
        response = self._client.call2(
267
            'Repository.get_revision_graph', path, revision_id)
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
268
        assert response[0][0] in ('ok', 'nosuchrevision'), 'unexpected response code %s' % (response[0],)
269
        if response[0][0] == 'ok':
270
            coded = response[1].read_body_bytes()
2018.5.105 by Andrew Bennetts
Implement revision_history caching for RemoteBranch.
271
            if coded == '':
272
                # no revisions in this repository!
273
                return {}
2018.5.83 by Andrew Bennetts
Fix some test failures caused by the switch from unicode to UTF-8-encoded strs for revision IDs.
274
            lines = coded.split('\n')
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
275
            revision_graph = {}
276
            # FIXME
277
            for line in lines:
278
                d = list(line.split())
279
                revision_graph[d[0]] = d[1:]
280
                
281
            return revision_graph
282
        else:
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
283
            response_body = response[1].read_body_bytes()
284
            assert response_body == ''
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
285
            raise NoSuchRevision(self, revision_id)
286
2018.5.40 by Robert Collins
Implement a remote Repository.has_revision method.
287
    def has_revision(self, revision_id):
288
        """See Repository.has_revision()."""
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
289
        if revision_id is None:
290
            # The null revision is always present.
291
            return True
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
292
        path = self.bzrdir._path_for_remote_call(self._client)
2018.5.83 by Andrew Bennetts
Fix some test failures caused by the switch from unicode to UTF-8-encoded strs for revision IDs.
293
        response = self._client.call('Repository.has_revision', path, revision_id)
2018.5.52 by Wouter van Heyst
Provide more information when encountering unexpected responses from a smart
294
        assert response[0] in ('ok', 'no'), 'unexpected response code %s' % (response,)
2018.5.40 by Robert Collins
Implement a remote Repository.has_revision method.
295
        return response[0] == 'ok'
296
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
297
    def gather_stats(self, revid=None, committers=None):
2018.5.62 by Robert Collins
Stub out RemoteRepository.gather_stats while its implemented in parallel.
298
        """See Repository.gather_stats()."""
2018.10.3 by v.ladeuil+lp at free
more tests for gather_stats
299
        path = self.bzrdir._path_for_remote_call(self._client)
300
        if revid in (None, NULL_REVISION):
301
            fmt_revid = ''
302
        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.
303
            fmt_revid = revid
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
304
        if committers is None or not committers:
2018.10.3 by v.ladeuil+lp at free
more tests for gather_stats
305
            fmt_committers = 'no'
306
        else:
307
            fmt_committers = 'yes'
308
        response = self._client.call2('Repository.gather_stats', path,
309
                                      fmt_revid, fmt_committers)
310
        assert response[0][0] == 'ok', \
311
            'unexpected response code %s' % (response[0],)
312
313
        body = response[1].read_body_bytes()
314
        result = {}
315
        for line in body.split('\n'):
316
            if not line:
317
                continue
318
            key, val_text = line.split(':')
319
            if key in ('revisions', 'size', 'committers'):
320
                result[key] = int(val_text)
321
            elif key in ('firstrev', 'latestrev'):
322
                values = val_text.split(' ')[1:]
323
                result[key] = (float(values[0]), long(values[1]))
324
325
        return result
2018.5.62 by Robert Collins
Stub out RemoteRepository.gather_stats while its implemented in parallel.
326
2018.5.60 by Robert Collins
More missing methods from RemoteBranch and RemoteRepository to let 'info' get further.
327
    def get_physical_lock_status(self):
328
        """See Repository.get_physical_lock_status()."""
329
        return False
330
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
331
    def is_shared(self):
332
        """See Repository.is_shared()."""
333
        path = self.bzrdir._path_for_remote_call(self._client)
334
        response = self._client.call('Repository.is_shared', path)
335
        assert response[0] in ('yes', 'no'), 'unexpected response code %s' % (response,)
336
        return response[0] == 'yes'
337
2018.5.60 by Robert Collins
More missing methods from RemoteBranch and RemoteRepository to let 'info' get further.
338
    def lock_read(self):
339
        # wrong eventually - want a local lock cache context
2018.5.75 by Andrew Bennetts
Add Repository.{dont_,}leave_lock_in_place.
340
        if not self._lock_mode:
341
            self._lock_mode = 'r'
342
            self._lock_count = 1
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
343
            if self._real_repository is not None:
344
                self._real_repository.lock_read()
2018.5.75 by Andrew Bennetts
Add Repository.{dont_,}leave_lock_in_place.
345
        else:
346
            self._lock_count += 1
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
347
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
348
    def _remote_lock_write(self, token):
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
349
        path = self.bzrdir._path_for_remote_call(self._client)
350
        if token is None:
351
            token = ''
352
        response = self._client.call('Repository.lock_write', path, token)
353
        if response[0] == 'ok':
354
            ok, token = response
355
            return token
356
        elif response[0] == 'LockContention':
357
            raise errors.LockContention('(remote 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.
358
        elif response[0] == 'UnlockableTransport':
359
            raise errors.UnlockableTransport(self.bzrdir.root_transport)
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
360
        else:
361
            assert False, 'unexpected response code %s' % (response,)
2018.5.60 by Robert Collins
More missing methods from RemoteBranch and RemoteRepository to let 'info' get further.
362
2018.5.75 by Andrew Bennetts
Add Repository.{dont_,}leave_lock_in_place.
363
    def lock_write(self, token=None):
364
        if not self._lock_mode:
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
365
            self._lock_token = self._remote_lock_write(token)
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
366
            assert self._lock_token, 'Remote server did not return a token!'
367
            if self._real_repository is not None:
368
                self._real_repository.lock_write(token=self._lock_token)
369
            if token is not None:
370
                self._leave_lock = True
371
            else:
372
                self._leave_lock = False
2018.5.75 by Andrew Bennetts
Add Repository.{dont_,}leave_lock_in_place.
373
            self._lock_mode = 'w'
374
            self._lock_count = 1
375
        elif self._lock_mode == 'r':
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
376
            raise errors.ReadOnlyError(self)
2018.5.75 by Andrew Bennetts
Add Repository.{dont_,}leave_lock_in_place.
377
        else:
378
            self._lock_count += 1
379
        return self._lock_token
380
381
    def leave_lock_in_place(self):
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
382
        self._leave_lock = True
2018.5.75 by Andrew Bennetts
Add Repository.{dont_,}leave_lock_in_place.
383
384
    def dont_leave_lock_in_place(self):
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
385
        self._leave_lock = False
2018.5.75 by Andrew Bennetts
Add Repository.{dont_,}leave_lock_in_place.
386
387
    def _set_real_repository(self, repository):
388
        """Set the _real_repository for this repository.
389
390
        :param repository: The repository to fallback to for non-hpss
391
            implemented operations.
392
        """
2018.5.97 by Andrew Bennetts
Fix more tests.
393
        assert not isinstance(repository, RemoteRepository)
2018.5.75 by Andrew Bennetts
Add Repository.{dont_,}leave_lock_in_place.
394
        self._real_repository = repository
395
        if self._lock_mode == 'w':
396
            # if we are already locked, the real repository must be able to
397
            # acquire the lock with our token.
398
            self._real_repository.lock_write(self._lock_token)
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
399
        elif self._lock_mode == 'r':
400
            self._real_repository.lock_read()
2018.5.60 by Robert Collins
More missing methods from RemoteBranch and RemoteRepository to let 'info' get further.
401
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
402
    def _unlock(self, token):
403
        path = self.bzrdir._path_for_remote_call(self._client)
404
        response = self._client.call('Repository.unlock', path, token)
405
        if response == ('ok',):
406
            return
407
        elif response[0] == 'TokenMismatch':
408
            raise errors.TokenMismatch(token, '(remote token)')
409
        else:
410
            assert False, 'unexpected response code %s' % (response,)
411
2018.5.60 by Robert Collins
More missing methods from RemoteBranch and RemoteRepository to let 'info' get further.
412
    def unlock(self):
2018.5.75 by Andrew Bennetts
Add Repository.{dont_,}leave_lock_in_place.
413
        self._lock_count -= 1
414
        if not self._lock_count:
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
415
            mode = self._lock_mode
2018.5.75 by Andrew Bennetts
Add Repository.{dont_,}leave_lock_in_place.
416
            self._lock_mode = None
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
417
            if self._real_repository is not None:
418
                self._real_repository.unlock()
419
            if mode != 'w':
420
                return
421
            assert self._lock_token, 'Locked, but no token!'
422
            token = self._lock_token
2018.5.75 by Andrew Bennetts
Add Repository.{dont_,}leave_lock_in_place.
423
            self._lock_token = None
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
424
            if not self._leave_lock:
425
                self._unlock(token)
2018.5.60 by Robert Collins
More missing methods from RemoteBranch and RemoteRepository to let 'info' get further.
426
427
    def break_lock(self):
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
428
        # should hand off to the network
2018.5.70 by Robert Collins
Only try to get real repositories when an operation requires them.
429
        self._ensure_real()
2018.5.60 by Robert Collins
More missing methods from RemoteBranch and RemoteRepository to let 'info' get further.
430
        return self._real_repository.break_lock()
431
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
432
    ### These methods are just thin shims to the VFS object for now.
433
434
    def revision_tree(self, revision_id):
435
        self._ensure_real()
436
        return self._real_repository.revision_tree(revision_id)
437
438
    def get_commit_builder(self, branch, parents, config, timestamp=None,
439
                           timezone=None, committer=None, revprops=None,
440
                           revision_id=None):
441
        # FIXME: It ought to be possible to call this without immediately
442
        # triggering _ensure_real.  For now it's the easiest thing to do.
443
        self._ensure_real()
444
        builder = self._real_repository.get_commit_builder(branch, parents,
445
                config, timestamp=timestamp, timezone=timezone,
446
                committer=committer, revprops=revprops, revision_id=revision_id)
447
        # Make the builder use this RemoteRepository rather than the real one.
448
        builder.repository = self
449
        return builder
450
451
    @needs_write_lock
452
    def add_inventory(self, revid, inv, parents):
453
        self._ensure_real()
454
        return self._real_repository.add_inventory(revid, inv, parents)
455
456
    @needs_write_lock
457
    def add_revision(self, rev_id, rev, inv=None, config=None):
458
        self._ensure_real()
459
        return self._real_repository.add_revision(
460
            rev_id, rev, inv=inv, config=config)
461
462
    @needs_read_lock
463
    def get_inventory(self, revision_id):
464
        self._ensure_real()
465
        return self._real_repository.get_inventory(revision_id)
466
467
    @needs_read_lock
468
    def get_revision(self, revision_id):
469
        self._ensure_real()
470
        return self._real_repository.get_revision(revision_id)
471
472
    @property
473
    def weave_store(self):
474
        self._ensure_real()
475
        return self._real_repository.weave_store
476
477
    def get_transaction(self):
478
        self._ensure_real()
479
        return self._real_repository.get_transaction()
480
481
    @needs_read_lock
482
    def clone(self, a_bzrdir, revision_id=None, basis=None):
483
        self._ensure_real()
484
        return self._real_repository.clone(
485
            a_bzrdir, revision_id=revision_id, basis=basis)
486
487
    def make_working_trees(self):
2018.5.120 by Robert Collins
The Repository API ``make_working_trees`` is now permitted to return
488
        """RemoteRepositories never create working trees by default."""
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
489
        return False
490
491
    def fetch(self, source, revision_id=None, pb=None):
492
        self._ensure_real()
493
        return self._real_repository.fetch(
494
            source, revision_id=revision_id, pb=pb)
495
496
    @property
497
    def control_weaves(self):
498
        self._ensure_real()
499
        return self._real_repository.control_weaves
500
501
    @needs_read_lock
502
    def get_ancestry(self, revision_id):
503
        self._ensure_real()
504
        return self._real_repository.get_ancestry(revision_id)
505
506
    @needs_read_lock
507
    def get_inventory_weave(self):
508
        self._ensure_real()
509
        return self._real_repository.get_inventory_weave()
510
511
    def fileids_altered_by_revision_ids(self, revision_ids):
512
        self._ensure_real()
513
        return self._real_repository.fileids_altered_by_revision_ids(revision_ids)
514
515
    @needs_read_lock
516
    def get_signature_text(self, revision_id):
517
        self._ensure_real()
518
        return self._real_repository.get_signature_text(revision_id)
519
520
    @needs_read_lock
521
    def get_revision_graph_with_ghosts(self, revision_ids=None):
522
        self._ensure_real()
523
        return self._real_repository.get_revision_graph_with_ghosts(
524
            revision_ids=revision_ids)
525
526
    @needs_read_lock
527
    def get_inventory_xml(self, revision_id):
528
        self._ensure_real()
529
        return self._real_repository.get_inventory_xml(revision_id)
530
531
    def deserialise_inventory(self, revision_id, xml):
532
        self._ensure_real()
533
        return self._real_repository.deserialise_inventory(revision_id, xml)
534
535
    def reconcile(self, other=None, thorough=False):
536
        self._ensure_real()
537
        return self._real_repository.reconcile(other=other, thorough=thorough)
538
        
539
    def all_revision_ids(self):
540
        self._ensure_real()
541
        return self._real_repository.all_revision_ids()
542
    
543
    @needs_read_lock
544
    def get_deltas_for_revisions(self, revisions):
545
        self._ensure_real()
546
        return self._real_repository.get_deltas_for_revisions(revisions)
547
548
    @needs_read_lock
549
    def get_revision_delta(self, revision_id):
550
        self._ensure_real()
551
        return self._real_repository.get_revision_delta(revision_id)
552
553
    @needs_read_lock
554
    def revision_trees(self, revision_ids):
555
        self._ensure_real()
556
        return self._real_repository.revision_trees(revision_ids)
557
558
    @needs_read_lock
559
    def get_revision_reconcile(self, revision_id):
560
        self._ensure_real()
561
        return self._real_repository.get_revision_reconcile(revision_id)
562
563
    @needs_read_lock
564
    def check(self, revision_ids):
565
        self._ensure_real()
566
        return self._real_repository.check(revision_ids)
567
568
    def copy_content_into(self, destination, revision_id=None, basis=None):
569
        self._ensure_real()
570
        return self._real_repository.copy_content_into(
571
            destination, revision_id=revision_id, basis=basis)
572
573
    def set_make_working_trees(self, new_value):
574
        raise NotImplementedError(self.set_make_working_trees)
575
576
    @needs_write_lock
577
    def sign_revision(self, revision_id, gpg_strategy):
578
        self._ensure_real()
579
        return self._real_repository.sign_revision(revision_id, gpg_strategy)
580
581
    @needs_read_lock
582
    def get_revisions(self, revision_ids):
583
        self._ensure_real()
584
        return self._real_repository.get_revisions(revision_ids)
585
586
    def supports_rich_root(self):
2018.5.84 by Andrew Bennetts
Merge in supports-rich-root, another test passing.
587
        self._ensure_real()
588
        return self._real_repository.supports_rich_root()
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
589
2018.5.83 by Andrew Bennetts
Fix some test failures caused by the switch from unicode to UTF-8-encoded strs for revision IDs.
590
    def iter_reverse_revision_history(self, revision_id):
591
        self._ensure_real()
592
        return self._real_repository.iter_reverse_revision_history(revision_id)
593
2018.5.96 by Andrew Bennetts
Merge from bzr.dev, resolving the worst of the semantic conflicts, but there's
594
    @property
595
    def _serializer(self):
596
        self._ensure_real()
597
        return self._real_repository._serializer
598
2018.5.97 by Andrew Bennetts
Fix more tests.
599
    def store_revision_signature(self, gpg_strategy, plaintext, revision_id):
600
        self._ensure_real()
601
        return self._real_repository.store_revision_signature(
602
            gpg_strategy, plaintext, revision_id)
603
604
    def has_signature_for_revision_id(self, revision_id):
605
        self._ensure_real()
606
        return self._real_repository.has_signature_for_revision_id(revision_id)
607
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
608
2018.5.127 by Andrew Bennetts
Fix most of the lockable_files tests for RemoteBranchLockableFiles.
609
class RemoteBranchLockableFiles(LockableFiles):
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
610
    """A 'LockableFiles' implementation that talks to a smart server.
611
    
612
    This is not a public interface class.
613
    """
614
615
    def __init__(self, bzrdir, _client):
616
        self.bzrdir = bzrdir
617
        self._client = _client
2018.5.133 by Andrew Bennetts
All TestLockableFiles_RemoteLockDir tests passing.
618
        # XXX: This assumes that the branch control directory is .bzr/branch,
619
        # which isn't necessarily true.
620
        LockableFiles.__init__(
621
            self, bzrdir.root_transport.clone('.bzr/branch'),
622
            'lock', lockdir.LockDir)
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
623
624
    def get(self, path):
625
        """'get' a remote path as per the LockableFiles interface.
626
627
        :param path: the file to 'get'. If this is 'branch.conf', we do not
628
             just retrieve a file, instead we ask the smart server to generate
629
             a configuration for us - which is retrieved as an INI file.
630
        """
2018.5.133 by Andrew Bennetts
All TestLockableFiles_RemoteLockDir tests passing.
631
        if path == 'branch.conf':
632
            path = self.bzrdir._path_for_remote_call(self._client)
633
            response = self._client.call2('Branch.get_config_file', path)
634
            assert response[0][0] == 'ok', \
635
                'unexpected response code %s' % (response[0],)
636
            return StringIO(response[1].read_body_bytes())
637
        else:
638
            # VFS fallback.
639
            return LockableFiles.get(self, path)
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
640
641
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
642
class RemoteBranchFormat(branch.BranchFormat):
643
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.
644
    def __eq__(self, other):
645
        return (isinstance(other, RemoteBranchFormat) and 
646
            self.__dict__ == other.__dict__)
647
2018.5.60 by Robert Collins
More missing methods from RemoteBranch and RemoteRepository to let 'info' get further.
648
    def get_format_description(self):
649
        return 'Remote BZR Branch'
650
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
651
    def get_format_string(self):
652
        return 'Remote BZR Branch'
653
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
654
    def open(self, a_bzrdir):
1752.2.72 by Andrew Bennetts
Make Remote* classes in remote.py more consistent and remove some dead code.
655
        assert isinstance(a_bzrdir, RemoteBzrDir)
656
        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.
657
658
    def initialize(self, a_bzrdir):
659
        assert isinstance(a_bzrdir, RemoteBzrDir)
660
        return a_bzrdir.create_branch()
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
661
662
663
class RemoteBranch(branch.Branch):
664
    """Branch stored on a server accessed by HPSS RPC.
665
666
    At the moment most operations are mapped down to simple file operations.
667
    """
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
668
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
669
    def __init__(self, remote_bzrdir, remote_repository, real_branch=None,
670
        _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.
671
        """Create a RemoteBranch instance.
672
673
        :param real_branch: An optional local implementation of the branch
674
            format, usually accessing the data via the VFS.
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
675
        :param _client: Private parameter for testing.
2018.5.34 by Robert Collins
Get test_remote.BasicRemoteObjectTests.test_open_remote_branch passing by implementing a remote method BzrDir.find_repository.
676
        """
2018.5.105 by Andrew Bennetts
Implement revision_history caching for RemoteBranch.
677
        #branch.Branch.__init__(self)
678
        self._revision_history_cache = None
1752.2.64 by Andrew Bennetts
Improve how RemoteBzrDir.open_branch works to handle references and not double-open repositories.
679
        self.bzrdir = remote_bzrdir
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
680
        if _client is not None:
681
            self._client = _client
682
        else:
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
683
            self._client = client.SmartClient(self.bzrdir._medium)
1752.2.64 by Andrew Bennetts
Improve how RemoteBzrDir.open_branch works to handle references and not double-open repositories.
684
        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.
685
        if real_branch is not None:
686
            self._real_branch = real_branch
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
687
            # Give the remote repository the matching real repo.
2018.5.97 by Andrew Bennetts
Fix more tests.
688
            real_repo = self._real_branch.repository
689
            if isinstance(real_repo, RemoteRepository):
690
                real_repo._ensure_real()
691
                real_repo = real_repo._real_repository
692
            self.repository._set_real_repository(real_repo)
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
693
            # Give the branch the remote repository to let fast-pathing happen.
694
            self._real_branch.repository = self.repository
2018.5.70 by Robert Collins
Only try to get real repositories when an operation requires them.
695
        else:
696
            self._real_branch = None
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
697
        # Fill out expected attributes of branch for bzrlib api users.
1752.2.64 by Andrew Bennetts
Improve how RemoteBzrDir.open_branch works to handle references and not double-open repositories.
698
        self._format = RemoteBranchFormat()
2018.5.55 by Robert Collins
Give RemoteBranch a base url in line with the Branch protocol.
699
        self.base = self.bzrdir.root_transport.base
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
700
        self.control_files = RemoteBranchLockableFiles(self.bzrdir, self._client)
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
701
        self._lock_mode = None
702
        self._lock_token = None
703
        self._lock_count = 0
704
        self._leave_lock = False
1752.2.64 by Andrew Bennetts
Improve how RemoteBzrDir.open_branch works to handle references and not double-open repositories.
705
2018.5.70 by Robert Collins
Only try to get real repositories when an operation requires them.
706
    def _ensure_real(self):
707
        """Ensure that there is a _real_branch set.
708
709
        used before calls to self._real_branch.
710
        """
711
        if not self._real_branch:
712
            assert vfs.vfs_enabled()
713
            self.bzrdir._ensure_real()
714
            self._real_branch = self.bzrdir._real_bzrdir.open_branch()
2018.5.75 by Andrew Bennetts
Add Repository.{dont_,}leave_lock_in_place.
715
            # Give the remote repository the matching real repo.
2018.5.97 by Andrew Bennetts
Fix more tests.
716
            real_repo = self._real_branch.repository
717
            if isinstance(real_repo, RemoteRepository):
718
                real_repo._ensure_real()
719
                real_repo = real_repo._real_repository
720
            self.repository._set_real_repository(real_repo)
2018.5.75 by Andrew Bennetts
Add Repository.{dont_,}leave_lock_in_place.
721
            # Give the branch the remote repository to let fast-pathing happen.
2018.5.70 by Robert Collins
Only try to get real repositories when an operation requires them.
722
            self._real_branch.repository = self.repository
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
723
            # XXX: deal with _lock_mode == 'w'
724
            if self._lock_mode == 'r':
725
                self._real_branch.lock_read()
2018.5.70 by Robert Collins
Only try to get real repositories when an operation requires them.
726
2018.5.60 by Robert Collins
More missing methods from RemoteBranch and RemoteRepository to let 'info' get further.
727
    def get_physical_lock_status(self):
728
        """See Branch.get_physical_lock_status()."""
729
        # 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.
730
        self._ensure_real()
2018.5.60 by Robert Collins
More missing methods from RemoteBranch and RemoteRepository to let 'info' get further.
731
        return self._real_branch.get_physical_lock_status()
732
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
733
    def lock_read(self):
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
734
        if not self._lock_mode:
735
            self._lock_mode = 'r'
736
            self._lock_count = 1
737
            if self._real_branch is not None:
738
                self._real_branch.lock_read()
739
        else:
740
            self._lock_count += 1
1752.2.52 by Andrew Bennetts
Flesh out more Remote* methods needed to open and initialise remote branches/trees/repositories.
741
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
742
    def _remote_lock_write(self, tokens):
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
743
        if tokens is None:
744
            branch_token = repo_token = ''
745
        else:
746
            branch_token, repo_token = tokens
747
        path = self.bzrdir._path_for_remote_call(self._client)
748
        response = self._client.call('Branch.lock_write', path, branch_token,
749
                                     repo_token)
750
        if response[0] == 'ok':
751
            ok, branch_token, repo_token = response
752
            return branch_token, repo_token
753
        elif response[0] == 'LockContention':
754
            raise errors.LockContention('(remote lock)')
755
        elif response[0] == 'TokenMismatch':
756
            raise errors.TokenMismatch(tokens, '(remote tokens)')
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.
757
        elif response[0] == 'UnlockableTransport':
758
            raise errors.UnlockableTransport(self.bzrdir.root_transport)
2018.5.123 by Robert Collins
Translate ReadOnlyError in RemoteBranch._remote_lock_write.
759
        elif response[0] == 'ReadOnlyError':
760
            raise errors.ReadOnlyError(self)
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
761
        else:
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
762
            assert False, 'unexpected response code %r' % (response,)
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
763
            
764
    def lock_write(self, tokens=None):
765
        if not self._lock_mode:
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
766
            remote_tokens = self._remote_lock_write(tokens)
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
767
            self._lock_token, self._repo_lock_token = remote_tokens
768
            assert self._lock_token, 'Remote server did not return a token!'
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
769
            # TODO: We really, really, really don't want to call _ensure_real
770
            # here, but it's the easiest way to ensure coherency between the
771
            # state of the RemoteBranch and RemoteRepository objects and the
772
            # physical locks.  If we don't materialise the real objects here,
773
            # then getting everything in the right state later is complex, so
774
            # for now we just do it the lazy way.
775
            #   -- Andrew Bennetts, 2007-02-22.
776
            self._ensure_real()
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
777
            if self._real_branch is not None:
778
                self._real_branch.lock_write(tokens=remote_tokens)
779
            if tokens is not None:
780
                self._leave_lock = True
781
            else:
782
                # XXX: this case seems to be unreachable; tokens cannot be None.
783
                self._leave_lock = False
784
            self._lock_mode = 'w'
785
            self._lock_count = 1
786
        elif self._lock_mode == 'r':
787
            raise errors.ReadOnlyTransaction
788
        else:
789
            if tokens is not None:
790
                # Tokens were given to lock_write, and we're relocking, so check
791
                # that the given tokens actually match the ones we already have.
792
                held_tokens = (self._lock_token, self._repo_lock_token)
793
                if tokens != held_tokens:
794
                    raise errors.TokenMismatch(str(tokens), str(held_tokens))
795
            self._lock_count += 1
796
        return self._lock_token, self._repo_lock_token
797
798
    def _unlock(self, branch_token, repo_token):
799
        path = self.bzrdir._path_for_remote_call(self._client)
800
        response = self._client.call('Branch.unlock', path, branch_token,
801
                                     repo_token)
802
        if response == ('ok',):
803
            return
804
        elif response[0] == 'TokenMismatch':
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
805
            raise errors.TokenMismatch(
806
                str((branch_token, repo_token)), '(remote tokens)')
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
807
        else:
808
            assert False, 'unexpected response code %s' % (response,)
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
809
810
    def unlock(self):
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
811
        self._lock_count -= 1
812
        if not self._lock_count:
2018.5.105 by Andrew Bennetts
Implement revision_history caching for RemoteBranch.
813
            self._clear_cached_state()
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
814
            mode = self._lock_mode
815
            self._lock_mode = None
816
            if self._real_branch is not None:
2018.15.1 by Andrew Bennetts
All branch_implementations/test_locking tests passing.
817
                if not self._leave_lock:
818
                    # If this RemoteBranch will remove the physical lock for the
819
                    # repository, make sure the _real_branch doesn't do it
820
                    # first.  (Because the _real_branch's repository is set to
821
                    # be the RemoteRepository.)
822
                    self._real_branch.repository.leave_lock_in_place()
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
823
                self._real_branch.unlock()
824
            if mode != 'w':
825
                return
826
            assert self._lock_token, 'Locked, but no token!'
827
            branch_token = self._lock_token
828
            repo_token = self._repo_lock_token
829
            self._lock_token = None
830
            self._repo_lock_token = None
831
            if not self._leave_lock:
832
                self._unlock(branch_token, repo_token)
1752.2.52 by Andrew Bennetts
Flesh out more Remote* methods needed to open and initialise remote branches/trees/repositories.
833
834
    def break_lock(self):
2018.5.70 by Robert Collins
Only try to get real repositories when an operation requires them.
835
        self._ensure_real()
1752.2.52 by Andrew Bennetts
Flesh out more Remote* methods needed to open and initialise remote branches/trees/repositories.
836
        return self._real_branch.break_lock()
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
837
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
838
    def leave_lock_in_place(self):
839
        self._leave_lock = True
840
841
    def dont_leave_lock_in_place(self):
842
        self._leave_lock = False
843
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
844
    def last_revision_info(self):
845
        """See Branch.last_revision_info()."""
846
        path = self.bzrdir._path_for_remote_call(self._client)
847
        response = self._client.call('Branch.last_revision_info', path)
2018.5.52 by Wouter van Heyst
Provide more information when encountering unexpected responses from a smart
848
        assert response[0] == 'ok', 'unexpected response code %s' % (response,)
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
849
        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.
850
        last_revision = response[2]
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
851
        if last_revision == '':
852
            last_revision = NULL_REVISION
853
        return (revno, last_revision)
854
2018.5.105 by Andrew Bennetts
Implement revision_history caching for RemoteBranch.
855
    def _gen_revision_history(self):
856
        """See Branch._gen_revision_history()."""
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
857
        path = self.bzrdir._path_for_remote_call(self._client)
2018.5.38 by Robert Collins
Implement RemoteBranch.revision_history().
858
        response = self._client.call2('Branch.revision_history', path)
2018.5.52 by Wouter van Heyst
Provide more information when encountering unexpected responses from a smart
859
        assert response[0][0] == 'ok', 'unexpected response code %s' % (response[0],)
2018.5.83 by Andrew Bennetts
Fix some test failures caused by the switch from unicode to UTF-8-encoded strs for revision IDs.
860
        result = response[1].read_body_bytes().split('\x00')
2018.5.38 by Robert Collins
Implement RemoteBranch.revision_history().
861
        if result == ['']:
862
            return []
863
        return result
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
864
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
865
    @needs_write_lock
1752.2.52 by Andrew Bennetts
Flesh out more Remote* methods needed to open and initialise remote branches/trees/repositories.
866
    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.
867
        # Send just the tip revision of the history; the server will generate
868
        # the full history from that.  If the revision doesn't exist in this
869
        # branch, NoSuchRevision will be raised.
870
        path = self.bzrdir._path_for_remote_call(self._client)
871
        if rev_history == []:
872
            rev_id = ''
873
        else:
874
            rev_id = rev_history[-1]
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
875
        response = self._client.call('Branch.set_last_revision',
876
            path, self._lock_token, self._repo_lock_token, rev_id)
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
877
        if response[0] == 'NoSuchRevision':
878
            raise NoSuchRevision(self, rev_id)
879
        else:
880
            assert response == ('ok',), (
881
                'unexpected response code %r' % (response,))
2018.5.105 by Andrew Bennetts
Implement revision_history caching for RemoteBranch.
882
        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.
883
884
    def get_parent(self):
2018.5.70 by Robert Collins
Only try to get real repositories when an operation requires them.
885
        self._ensure_real()
1752.2.52 by Andrew Bennetts
Flesh out more Remote* methods needed to open and initialise remote branches/trees/repositories.
886
        return self._real_branch.get_parent()
887
        
1752.2.63 by Andrew Bennetts
Delegate set_parent.
888
    def set_parent(self, url):
2018.5.70 by Robert Collins
Only try to get real repositories when an operation requires them.
889
        self._ensure_real()
1752.2.63 by Andrew Bennetts
Delegate set_parent.
890
        return self._real_branch.set_parent(url)
891
        
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
892
    def get_config(self):
893
        return RemoteBranchConfig(self)
894
2018.5.94 by Andrew Bennetts
Various small changes in aid of making tests pass (including deleting one invalid test).
895
    def sprout(self, to_bzrdir, revision_id=None):
896
        # Like Branch.sprout, except that it sprouts a branch in the default
897
        # format, because RemoteBranches can't be created at arbitrary URLs.
898
        # XXX: if to_bzrdir is a RemoteBranch, this should perhaps do
899
        # to_bzrdir.create_branch...
900
        self._ensure_real()
901
        result = branch.BranchFormat.get_default_format().initialize(to_bzrdir)
902
        self._real_branch.copy_content_into(result, revision_id=revision_id)
903
        result.set_parent(self.bzrdir.root_transport.base)
904
        return result
905
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
906
    @needs_write_lock
907
    def append_revision(self, *revision_ids):
908
        self._ensure_real()
909
        return self._real_branch.append_revision(*revision_ids)
910
911
    @needs_write_lock
912
    def pull(self, source, overwrite=False, stop_revision=None):
913
        self._ensure_real()
914
        self._real_branch.pull(
915
            source, overwrite=overwrite, stop_revision=stop_revision)
916
2018.14.3 by Andrew Bennetts
Make a couple more branch_implementations tests pass.
917
    @needs_read_lock
918
    def push(self, target, overwrite=False, stop_revision=None):
919
        self._ensure_real()
2018.5.97 by Andrew Bennetts
Fix more tests.
920
        return self._real_branch.push(
2018.14.3 by Andrew Bennetts
Make a couple more branch_implementations tests pass.
921
            target, overwrite=overwrite, stop_revision=stop_revision)
922
923
    def is_locked(self):
924
        return self._lock_count >= 1
925
2018.5.83 by Andrew Bennetts
Fix some test failures caused by the switch from unicode to UTF-8-encoded strs for revision IDs.
926
    def set_last_revision_info(self, revno, revision_id):
927
        self._ensure_real()
2018.5.105 by Andrew Bennetts
Implement revision_history caching for RemoteBranch.
928
        self._clear_cached_state()
2018.5.83 by Andrew Bennetts
Fix some test failures caused by the switch from unicode to UTF-8-encoded strs for revision IDs.
929
        return self._real_branch.set_last_revision_info(revno, revision_id)
930
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.
931
    def generate_revision_history(self, revision_id, last_rev=None,
932
                                  other_branch=None):
933
        self._ensure_real()
934
        return self._real_branch.generate_revision_history(
935
            revision_id, last_rev=last_rev, other_branch=other_branch)
936
2018.5.96 by Andrew Bennetts
Merge from bzr.dev, resolving the worst of the semantic conflicts, but there's
937
    @property
938
    def tags(self):
939
        self._ensure_real()
940
        return self._real_branch.tags
941
2018.5.97 by Andrew Bennetts
Fix more tests.
942
    def set_push_location(self, location):
943
        self._ensure_real()
944
        return self._real_branch.set_push_location(location)
945
946
    def update_revisions(self, other, stop_revision=None):
947
        self._ensure_real()
948
        return self._real_branch.update_revisions(
949
            other, stop_revision=stop_revision)
950
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
951
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
952
class RemoteBranchConfig(BranchConfig):
953
954
    def username(self):
955
        self.branch._ensure_real()
956
        return self.branch._real_branch.get_config().username()
957
2018.14.2 by Andrew Bennetts
All but one repository_implementation tests for RemoteRepository passing.
958
    def _get_branch_data_config(self):
959
        self.branch._ensure_real()
960
        if self._branch_data_config is None:
961
            self._branch_data_config = TreeConfig(self.branch._real_branch)
962
        return self._branch_data_config
963