~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/remote.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-04-07 13:08:21 UTC
  • mfrom: (4222.2.12 ui-username)
  • Revision ID: pqm@pqm.ubuntu.com-20090407130821-e4wi39x60alhpnr4
(Jelmer) Add UIFactory.get_username.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2006, 2007, 2008 Canonical Ltd
 
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
# TODO: At some point, handle upgrades by just passing the whole request
 
18
# across to run on the server.
 
19
 
 
20
import bz2
 
21
 
 
22
from bzrlib import (
 
23
    branch,
 
24
    bzrdir,
 
25
    config,
 
26
    debug,
 
27
    errors,
 
28
    graph,
 
29
    lockdir,
 
30
    pack,
 
31
    repository,
 
32
    revision,
 
33
    symbol_versioning,
 
34
    urlutils,
 
35
)
 
36
from bzrlib.branch import BranchReferenceFormat
 
37
from bzrlib.bzrdir import BzrDir, RemoteBzrDirFormat
 
38
from bzrlib.decorators import needs_read_lock, needs_write_lock
 
39
from bzrlib.errors import (
 
40
    NoSuchRevision,
 
41
    SmartProtocolError,
 
42
    )
 
43
from bzrlib.lockable_files import LockableFiles
 
44
from bzrlib.smart import client, vfs, repository as smart_repo
 
45
from bzrlib.revision import ensure_null, NULL_REVISION
 
46
from bzrlib.trace import mutter, note, warning
 
47
from bzrlib.util import bencode
 
48
 
 
49
 
 
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)
 
58
 
 
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)
 
64
 
 
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)
 
72
 
 
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
 
 
84
# Note: RemoteBzrDirFormat is in bzrdir.py
 
85
 
 
86
class RemoteBzrDir(BzrDir, _RpcHelper):
 
87
    """Control directory on a remote server, accessed via bzr:// or similar."""
 
88
 
 
89
    def __init__(self, transport, format, _client=None):
 
90
        """Construct a RemoteBzrDir.
 
91
 
 
92
        :param _client: Private parameter for testing. Disables probing and the
 
93
            use of a real bzrdir.
 
94
        """
 
95
        BzrDir.__init__(self, transport, format)
 
96
        # this object holds a delegated bzrdir that uses file-level operations
 
97
        # to talk to the other side
 
98
        self._real_bzrdir = None
 
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
 
102
 
 
103
        if _client is None:
 
104
            medium = transport.get_smart_medium()
 
105
            self._client = client._SmartClient(medium)
 
106
        else:
 
107
            self._client = _client
 
108
            return
 
109
 
 
110
        path = self._path_for_remote_call(self._client)
 
111
        response = self._call('BzrDir.open', path)
 
112
        if response not in [('yes',), ('no',)]:
 
113
            raise errors.UnexpectedSmartServerResponse(response)
 
114
        if response == ('no',):
 
115
            raise errors.NotBranchError(path=transport.base)
 
116
 
 
117
    def _ensure_real(self):
 
118
        """Ensure that there is a _real_bzrdir set.
 
119
 
 
120
        Used before calls to self._real_bzrdir.
 
121
        """
 
122
        if not self._real_bzrdir:
 
123
            self._real_bzrdir = BzrDir.open_from_transport(
 
124
                self.root_transport, _server_formats=False)
 
125
            self._format._network_name = \
 
126
                self._real_bzrdir._format.network_name()
 
127
 
 
128
    def _translate_error(self, err, **context):
 
129
        _translate_error(err, bzrdir=self, **context)
 
130
 
 
131
    def break_lock(self):
 
132
        # Prevent aliasing problems in the next_open_branch_result cache.
 
133
        # See create_branch for rationale.
 
134
        self._next_open_branch_result = None
 
135
        return BzrDir.break_lock(self)
 
136
 
 
137
    def _vfs_cloning_metadir(self, require_stacking=False):
 
138
        self._ensure_real()
 
139
        return self._real_bzrdir.cloning_metadir(
 
140
            require_stacking=require_stacking)
 
141
 
 
142
    def cloning_metadir(self, require_stacking=False):
 
143
        medium = self._client._medium
 
144
        if medium._is_remote_before((1, 13)):
 
145
            return self._vfs_cloning_metadir(require_stacking=require_stacking)
 
146
        verb = 'BzrDir.cloning_metadir'
 
147
        if require_stacking:
 
148
            stacking = 'True'
 
149
        else:
 
150
            stacking = 'False'
 
151
        path = self._path_for_remote_call(self._client)
 
152
        try:
 
153
            response = self._call(verb, path, stacking)
 
154
        except errors.UnknownSmartMethod:
 
155
            medium._remember_remote_is_before((1, 13))
 
156
            return self._vfs_cloning_metadir(require_stacking=require_stacking)
 
157
        except errors.UnknownErrorFromSmartServer, err:
 
158
            if err.error_tuple != ('BranchReference',):
 
159
                raise
 
160
            # We need to resolve the branch reference to determine the
 
161
            # cloning_metadir.  This causes unnecessary RPCs to open the
 
162
            # referenced branch (and bzrdir, etc) but only when the caller
 
163
            # didn't already resolve the branch reference.
 
164
            referenced_branch = self.open_branch()
 
165
            return referenced_branch.bzrdir.cloning_metadir()
 
166
        if len(response) != 3:
 
167
            raise errors.UnexpectedSmartServerResponse(response)
 
168
        control_name, repo_name, branch_info = response
 
169
        if len(branch_info) != 2:
 
170
            raise errors.UnexpectedSmartServerResponse(response)
 
171
        branch_ref, branch_name = branch_info
 
172
        format = bzrdir.network_format_registry.get(control_name)
 
173
        if repo_name:
 
174
            format.repository_format = repository.network_format_registry.get(
 
175
                repo_name)
 
176
        if branch_ref == 'ref':
 
177
            # XXX: we need possible_transports here to avoid reopening the
 
178
            # connection to the referenced location
 
179
            ref_bzrdir = BzrDir.open(branch_name)
 
180
            branch_format = ref_bzrdir.cloning_metadir().get_branch_format()
 
181
            format.set_branch_format(branch_format)
 
182
        elif branch_ref == 'branch':
 
183
            if branch_name:
 
184
                format.set_branch_format(
 
185
                    branch.network_format_registry.get(branch_name))
 
186
        else:
 
187
            raise errors.UnexpectedSmartServerResponse(response)
 
188
        return format
 
189
 
 
190
    def create_repository(self, shared=False):
 
191
        # as per meta1 formats - just delegate to the format object which may
 
192
        # be parameterised.
 
193
        result = self._format.repository_format.initialize(self, shared)
 
194
        if not isinstance(result, RemoteRepository):
 
195
            return self.open_repository()
 
196
        else:
 
197
            return result
 
198
 
 
199
    def destroy_repository(self):
 
200
        """See BzrDir.destroy_repository"""
 
201
        self._ensure_real()
 
202
        self._real_bzrdir.destroy_repository()
 
203
 
 
204
    def create_branch(self):
 
205
        # as per meta1 formats - just delegate to the format object which may
 
206
        # be parameterised.
 
207
        real_branch = self._format.get_branch_format().initialize(self)
 
208
        if not isinstance(real_branch, RemoteBranch):
 
209
            result = RemoteBranch(self, self.find_repository(), real_branch)
 
210
        else:
 
211
            result = real_branch
 
212
        # BzrDir.clone_on_transport() uses the result of create_branch but does
 
213
        # not return it to its callers; we save approximately 8% of our round
 
214
        # trips by handing the branch we created back to the first caller to
 
215
        # open_branch rather than probing anew. Long term we need a API in
 
216
        # bzrdir that doesn't discard result objects (like result_branch).
 
217
        # RBC 20090225
 
218
        self._next_open_branch_result = result
 
219
        return result
 
220
 
 
221
    def destroy_branch(self):
 
222
        """See BzrDir.destroy_branch"""
 
223
        self._ensure_real()
 
224
        self._real_bzrdir.destroy_branch()
 
225
        self._next_open_branch_result = None
 
226
 
 
227
    def create_workingtree(self, revision_id=None, from_branch=None):
 
228
        raise errors.NotLocalUrl(self.transport.base)
 
229
 
 
230
    def find_branch_format(self):
 
231
        """Find the branch 'format' for this bzrdir.
 
232
 
 
233
        This might be a synthetic object for e.g. RemoteBranch and SVN.
 
234
        """
 
235
        b = self.open_branch()
 
236
        return b._format
 
237
 
 
238
    def get_branch_reference(self):
 
239
        """See BzrDir.get_branch_reference()."""
 
240
        response = self._get_branch_reference()
 
241
        if response[0] == 'ref':
 
242
            return response[1]
 
243
        else:
 
244
            return None
 
245
 
 
246
    def _get_branch_reference(self):
 
247
        path = self._path_for_remote_call(self._client)
 
248
        medium = self._client._medium
 
249
        if not medium._is_remote_before((1, 13)):
 
250
            try:
 
251
                response = self._call('BzrDir.open_branchV2', path)
 
252
                if response[0] not in ('ref', 'branch'):
 
253
                    raise errors.UnexpectedSmartServerResponse(response)
 
254
                return response
 
255
            except errors.UnknownSmartMethod:
 
256
                medium._remember_remote_is_before((1, 13))
 
257
        response = self._call('BzrDir.open_branch', path)
 
258
        if response[0] != 'ok':
 
259
            raise errors.UnexpectedSmartServerResponse(response)
 
260
        if response[1] != '':
 
261
            return ('ref', response[1])
 
262
        else:
 
263
            return ('branch', '')
 
264
 
 
265
    def _get_tree_branch(self):
 
266
        """See BzrDir._get_tree_branch()."""
 
267
        return None, self.open_branch()
 
268
 
 
269
    def open_branch(self, _unsupported=False, ignore_fallbacks=False):
 
270
        if _unsupported:
 
271
            raise NotImplementedError('unsupported flag support not implemented yet.')
 
272
        if self._next_open_branch_result is not None:
 
273
            # See create_branch for details.
 
274
            result = self._next_open_branch_result
 
275
            self._next_open_branch_result = None
 
276
            return result
 
277
        response = self._get_branch_reference()
 
278
        if response[0] == 'ref':
 
279
            # a branch reference, use the existing BranchReference logic.
 
280
            format = BranchReferenceFormat()
 
281
            return format.open(self, _found=True, location=response[1],
 
282
                ignore_fallbacks=ignore_fallbacks)
 
283
        branch_format_name = response[1]
 
284
        if not branch_format_name:
 
285
            branch_format_name = None
 
286
        format = RemoteBranchFormat(network_name=branch_format_name)
 
287
        return RemoteBranch(self, self.find_repository(), format=format,
 
288
            setup_stacking=not ignore_fallbacks)
 
289
 
 
290
    def _open_repo_v1(self, path):
 
291
        verb = 'BzrDir.find_repository'
 
292
        response = self._call(verb, path)
 
293
        if response[0] != 'ok':
 
294
            raise errors.UnexpectedSmartServerResponse(response)
 
295
        # servers that only support the v1 method don't support external
 
296
        # references either.
 
297
        self._ensure_real()
 
298
        repo = self._real_bzrdir.open_repository()
 
299
        response = response + ('no', repo._format.network_name())
 
300
        return response, repo
 
301
 
 
302
    def _open_repo_v2(self, path):
 
303
        verb = 'BzrDir.find_repositoryV2'
 
304
        response = self._call(verb, path)
 
305
        if response[0] != 'ok':
 
306
            raise errors.UnexpectedSmartServerResponse(response)
 
307
        self._ensure_real()
 
308
        repo = self._real_bzrdir.open_repository()
 
309
        response = response + (repo._format.network_name(),)
 
310
        return response, repo
 
311
 
 
312
    def _open_repo_v3(self, path):
 
313
        verb = 'BzrDir.find_repositoryV3'
 
314
        medium = self._client._medium
 
315
        if medium._is_remote_before((1, 13)):
 
316
            raise errors.UnknownSmartMethod(verb)
 
317
        try:
 
318
            response = self._call(verb, path)
 
319
        except errors.UnknownSmartMethod:
 
320
            medium._remember_remote_is_before((1, 13))
 
321
            raise
 
322
        if response[0] != 'ok':
 
323
            raise errors.UnexpectedSmartServerResponse(response)
 
324
        return response, None
 
325
 
 
326
    def open_repository(self):
 
327
        path = self._path_for_remote_call(self._client)
 
328
        response = None
 
329
        for probe in [self._open_repo_v3, self._open_repo_v2,
 
330
            self._open_repo_v1]:
 
331
            try:
 
332
                response, real_repo = probe(path)
 
333
                break
 
334
            except errors.UnknownSmartMethod:
 
335
                pass
 
336
        if response is None:
 
337
            raise errors.UnknownSmartMethod('BzrDir.find_repository{3,2,}')
 
338
        if response[0] != 'ok':
 
339
            raise errors.UnexpectedSmartServerResponse(response)
 
340
        if len(response) != 6:
 
341
            raise SmartProtocolError('incorrect response length %s' % (response,))
 
342
        if response[1] == '':
 
343
            # repo is at this dir.
 
344
            format = response_tuple_to_repo_format(response[2:])
 
345
            # Used to support creating a real format instance when needed.
 
346
            format._creating_bzrdir = self
 
347
            remote_repo = RemoteRepository(self, format)
 
348
            format._creating_repo = remote_repo
 
349
            if real_repo is not None:
 
350
                remote_repo._set_real_repository(real_repo)
 
351
            return remote_repo
 
352
        else:
 
353
            raise errors.NoRepositoryPresent(self)
 
354
 
 
355
    def open_workingtree(self, recommend_upgrade=True):
 
356
        self._ensure_real()
 
357
        if self._real_bzrdir.has_workingtree():
 
358
            raise errors.NotLocalUrl(self.root_transport)
 
359
        else:
 
360
            raise errors.NoWorkingTree(self.root_transport.base)
 
361
 
 
362
    def _path_for_remote_call(self, client):
 
363
        """Return the path to be used for this bzrdir in a remote call."""
 
364
        return client.remote_path_from_transport(self.root_transport)
 
365
 
 
366
    def get_branch_transport(self, branch_format):
 
367
        self._ensure_real()
 
368
        return self._real_bzrdir.get_branch_transport(branch_format)
 
369
 
 
370
    def get_repository_transport(self, repository_format):
 
371
        self._ensure_real()
 
372
        return self._real_bzrdir.get_repository_transport(repository_format)
 
373
 
 
374
    def get_workingtree_transport(self, workingtree_format):
 
375
        self._ensure_real()
 
376
        return self._real_bzrdir.get_workingtree_transport(workingtree_format)
 
377
 
 
378
    def can_convert_format(self):
 
379
        """Upgrading of remote bzrdirs is not supported yet."""
 
380
        return False
 
381
 
 
382
    def needs_format_conversion(self, format=None):
 
383
        """Upgrading of remote bzrdirs is not supported yet."""
 
384
        if format is None:
 
385
            symbol_versioning.warn(symbol_versioning.deprecated_in((1, 13, 0))
 
386
                % 'needs_format_conversion(format=None)')
 
387
        return False
 
388
 
 
389
    def clone(self, url, revision_id=None, force_new_repo=False,
 
390
              preserve_stacking=False):
 
391
        self._ensure_real()
 
392
        return self._real_bzrdir.clone(url, revision_id=revision_id,
 
393
            force_new_repo=force_new_repo, preserve_stacking=preserve_stacking)
 
394
 
 
395
    def get_config(self):
 
396
        self._ensure_real()
 
397
        return self._real_bzrdir.get_config()
 
398
 
 
399
 
 
400
class RemoteRepositoryFormat(repository.RepositoryFormat):
 
401
    """Format for repositories accessed over a _SmartClient.
 
402
 
 
403
    Instances of this repository are represented by RemoteRepository
 
404
    instances.
 
405
 
 
406
    The RemoteRepositoryFormat is parameterized during construction
 
407
    to reflect the capabilities of the real, remote format. Specifically
 
408
    the attributes rich_root_data and supports_tree_reference are set
 
409
    on a per instance basis, and are not set (and should not be) at
 
410
    the class level.
 
411
 
 
412
    :ivar _custom_format: If set, a specific concrete repository format that
 
413
        will be used when initializing a repository with this
 
414
        RemoteRepositoryFormat.
 
415
    :ivar _creating_repo: If set, the repository object that this
 
416
        RemoteRepositoryFormat was created for: it can be called into
 
417
        to obtain data like the network name.
 
418
    """
 
419
 
 
420
    _matchingbzrdir = RemoteBzrDirFormat()
 
421
 
 
422
    def __init__(self):
 
423
        repository.RepositoryFormat.__init__(self)
 
424
        self._custom_format = None
 
425
        self._network_name = None
 
426
        self._creating_bzrdir = None
 
427
        self._supports_external_lookups = None
 
428
        self._supports_tree_reference = None
 
429
        self._rich_root_data = None
 
430
 
 
431
    @property
 
432
    def fast_deltas(self):
 
433
        self._ensure_real()
 
434
        return self._custom_format.fast_deltas
 
435
 
 
436
    @property
 
437
    def rich_root_data(self):
 
438
        if self._rich_root_data is None:
 
439
            self._ensure_real()
 
440
            self._rich_root_data = self._custom_format.rich_root_data
 
441
        return self._rich_root_data
 
442
 
 
443
    @property
 
444
    def supports_external_lookups(self):
 
445
        if self._supports_external_lookups is None:
 
446
            self._ensure_real()
 
447
            self._supports_external_lookups = \
 
448
                self._custom_format.supports_external_lookups
 
449
        return self._supports_external_lookups
 
450
 
 
451
    @property
 
452
    def supports_tree_reference(self):
 
453
        if self._supports_tree_reference is None:
 
454
            self._ensure_real()
 
455
            self._supports_tree_reference = \
 
456
                self._custom_format.supports_tree_reference
 
457
        return self._supports_tree_reference
 
458
 
 
459
    def _vfs_initialize(self, a_bzrdir, shared):
 
460
        """Helper for common code in initialize."""
 
461
        if self._custom_format:
 
462
            # Custom format requested
 
463
            result = self._custom_format.initialize(a_bzrdir, shared=shared)
 
464
        elif self._creating_bzrdir is not None:
 
465
            # Use the format that the repository we were created to back
 
466
            # has.
 
467
            prior_repo = self._creating_bzrdir.open_repository()
 
468
            prior_repo._ensure_real()
 
469
            result = prior_repo._real_repository._format.initialize(
 
470
                a_bzrdir, shared=shared)
 
471
        else:
 
472
            # assume that a_bzr is a RemoteBzrDir but the smart server didn't
 
473
            # support remote initialization.
 
474
            # We delegate to a real object at this point (as RemoteBzrDir
 
475
            # delegate to the repository format which would lead to infinite
 
476
            # recursion if we just called a_bzrdir.create_repository.
 
477
            a_bzrdir._ensure_real()
 
478
            result = a_bzrdir._real_bzrdir.create_repository(shared=shared)
 
479
        if not isinstance(result, RemoteRepository):
 
480
            return self.open(a_bzrdir)
 
481
        else:
 
482
            return result
 
483
 
 
484
    def initialize(self, a_bzrdir, shared=False):
 
485
        # Being asked to create on a non RemoteBzrDir:
 
486
        if not isinstance(a_bzrdir, RemoteBzrDir):
 
487
            return self._vfs_initialize(a_bzrdir, shared)
 
488
        medium = a_bzrdir._client._medium
 
489
        if medium._is_remote_before((1, 13)):
 
490
            return self._vfs_initialize(a_bzrdir, shared)
 
491
        # Creating on a remote bzr dir.
 
492
        # 1) get the network name to use.
 
493
        if self._custom_format:
 
494
            network_name = self._custom_format.network_name()
 
495
        else:
 
496
            # Select the current bzrlib default and ask for that.
 
497
            reference_bzrdir_format = bzrdir.format_registry.get('default')()
 
498
            reference_format = reference_bzrdir_format.repository_format
 
499
            network_name = reference_format.network_name()
 
500
        # 2) try direct creation via RPC
 
501
        path = a_bzrdir._path_for_remote_call(a_bzrdir._client)
 
502
        verb = 'BzrDir.create_repository'
 
503
        if shared:
 
504
            shared_str = 'True'
 
505
        else:
 
506
            shared_str = 'False'
 
507
        try:
 
508
            response = a_bzrdir._call(verb, path, network_name, shared_str)
 
509
        except errors.UnknownSmartMethod:
 
510
            # Fallback - use vfs methods
 
511
            medium._remember_remote_is_before((1, 13))
 
512
            return self._vfs_initialize(a_bzrdir, shared)
 
513
        else:
 
514
            # Turn the response into a RemoteRepository object.
 
515
            format = response_tuple_to_repo_format(response[1:])
 
516
            # Used to support creating a real format instance when needed.
 
517
            format._creating_bzrdir = a_bzrdir
 
518
            remote_repo = RemoteRepository(a_bzrdir, format)
 
519
            format._creating_repo = remote_repo
 
520
            return remote_repo
 
521
 
 
522
    def open(self, a_bzrdir):
 
523
        if not isinstance(a_bzrdir, RemoteBzrDir):
 
524
            raise AssertionError('%r is not a RemoteBzrDir' % (a_bzrdir,))
 
525
        return a_bzrdir.open_repository()
 
526
 
 
527
    def _ensure_real(self):
 
528
        if self._custom_format is None:
 
529
            self._custom_format = repository.network_format_registry.get(
 
530
                self._network_name)
 
531
 
 
532
    @property
 
533
    def _fetch_order(self):
 
534
        self._ensure_real()
 
535
        return self._custom_format._fetch_order
 
536
 
 
537
    @property
 
538
    def _fetch_uses_deltas(self):
 
539
        self._ensure_real()
 
540
        return self._custom_format._fetch_uses_deltas
 
541
 
 
542
    @property
 
543
    def _fetch_reconcile(self):
 
544
        self._ensure_real()
 
545
        return self._custom_format._fetch_reconcile
 
546
 
 
547
    def get_format_description(self):
 
548
        return 'bzr remote repository'
 
549
 
 
550
    def __eq__(self, other):
 
551
        return self.__class__ is other.__class__
 
552
 
 
553
    def check_conversion_target(self, target_format):
 
554
        if self.rich_root_data and not target_format.rich_root_data:
 
555
            raise errors.BadConversionTarget(
 
556
                'Does not support rich root data.', target_format)
 
557
        if (self.supports_tree_reference and
 
558
            not getattr(target_format, 'supports_tree_reference', False)):
 
559
            raise errors.BadConversionTarget(
 
560
                'Does not support nested trees', target_format)
 
561
 
 
562
    def network_name(self):
 
563
        if self._network_name:
 
564
            return self._network_name
 
565
        self._creating_repo._ensure_real()
 
566
        return self._creating_repo._real_repository._format.network_name()
 
567
 
 
568
    @property
 
569
    def _serializer(self):
 
570
        self._ensure_real()
 
571
        return self._custom_format._serializer
 
572
 
 
573
 
 
574
class RemoteRepository(_RpcHelper):
 
575
    """Repository accessed over rpc.
 
576
 
 
577
    For the moment most operations are performed using local transport-backed
 
578
    Repository objects.
 
579
    """
 
580
 
 
581
    def __init__(self, remote_bzrdir, format, real_repository=None, _client=None):
 
582
        """Create a RemoteRepository instance.
 
583
 
 
584
        :param remote_bzrdir: The bzrdir hosting this repository.
 
585
        :param format: The RemoteFormat object to use.
 
586
        :param real_repository: If not None, a local implementation of the
 
587
            repository logic for the repository, usually accessing the data
 
588
            via the VFS.
 
589
        :param _client: Private testing parameter - override the smart client
 
590
            to be used by the repository.
 
591
        """
 
592
        if real_repository:
 
593
            self._real_repository = real_repository
 
594
        else:
 
595
            self._real_repository = None
 
596
        self.bzrdir = remote_bzrdir
 
597
        if _client is None:
 
598
            self._client = remote_bzrdir._client
 
599
        else:
 
600
            self._client = _client
 
601
        self._format = format
 
602
        self._lock_mode = None
 
603
        self._lock_token = None
 
604
        self._lock_count = 0
 
605
        self._leave_lock = False
 
606
        self._unstacked_provider = graph.CachingParentsProvider(
 
607
            get_parent_map=self._get_parent_map_rpc)
 
608
        self._unstacked_provider.disable_cache()
 
609
        # For tests:
 
610
        # These depend on the actual remote format, so force them off for
 
611
        # maximum compatibility. XXX: In future these should depend on the
 
612
        # remote repository instance, but this is irrelevant until we perform
 
613
        # reconcile via an RPC call.
 
614
        self._reconcile_does_inventory_gc = False
 
615
        self._reconcile_fixes_text_parents = False
 
616
        self._reconcile_backsup_inventory = False
 
617
        self.base = self.bzrdir.transport.base
 
618
        # Additional places to query for data.
 
619
        self._fallback_repositories = []
 
620
 
 
621
    def __str__(self):
 
622
        return "%s(%s)" % (self.__class__.__name__, self.base)
 
623
 
 
624
    __repr__ = __str__
 
625
 
 
626
    def abort_write_group(self, suppress_errors=False):
 
627
        """Complete a write group on the decorated repository.
 
628
 
 
629
        Smart methods perform operations in a single step so this API
 
630
        is not really applicable except as a compatibility thunk
 
631
        for older plugins that don't use e.g. the CommitBuilder
 
632
        facility.
 
633
 
 
634
        :param suppress_errors: see Repository.abort_write_group.
 
635
        """
 
636
        self._ensure_real()
 
637
        return self._real_repository.abort_write_group(
 
638
            suppress_errors=suppress_errors)
 
639
 
 
640
    @property
 
641
    def chk_bytes(self):
 
642
        """Decorate the real repository for now.
 
643
 
 
644
        In the long term a full blown network facility is needed to avoid
 
645
        creating a real repository object locally.
 
646
        """
 
647
        self._ensure_real()
 
648
        return self._real_repository.chk_bytes
 
649
 
 
650
    def commit_write_group(self):
 
651
        """Complete a write group on the decorated repository.
 
652
 
 
653
        Smart methods perform operations in a single step so this API
 
654
        is not really applicable except as a compatibility thunk
 
655
        for older plugins that don't use e.g. the CommitBuilder
 
656
        facility.
 
657
        """
 
658
        self._ensure_real()
 
659
        return self._real_repository.commit_write_group()
 
660
 
 
661
    def resume_write_group(self, tokens):
 
662
        self._ensure_real()
 
663
        return self._real_repository.resume_write_group(tokens)
 
664
 
 
665
    def suspend_write_group(self):
 
666
        self._ensure_real()
 
667
        return self._real_repository.suspend_write_group()
 
668
 
 
669
    def _ensure_real(self):
 
670
        """Ensure that there is a _real_repository set.
 
671
 
 
672
        Used before calls to self._real_repository.
 
673
 
 
674
        Note that _ensure_real causes many roundtrips to the server which are
 
675
        not desirable, and prevents the use of smart one-roundtrip RPC's to
 
676
        perform complex operations (such as accessing parent data, streaming
 
677
        revisions etc). Adding calls to _ensure_real should only be done when
 
678
        bringing up new functionality, adding fallbacks for smart methods that
 
679
        require a fallback path, and never to replace an existing smart method
 
680
        invocation. If in doubt chat to the bzr network team.
 
681
        """
 
682
        if self._real_repository is None:
 
683
            self.bzrdir._ensure_real()
 
684
            self._set_real_repository(
 
685
                self.bzrdir._real_bzrdir.open_repository())
 
686
 
 
687
    def _translate_error(self, err, **context):
 
688
        self.bzrdir._translate_error(err, repository=self, **context)
 
689
 
 
690
    def find_text_key_references(self):
 
691
        """Find the text key references within the repository.
 
692
 
 
693
        :return: a dictionary mapping (file_id, revision_id) tuples to altered file-ids to an iterable of
 
694
        revision_ids. Each altered file-ids has the exact revision_ids that
 
695
        altered it listed explicitly.
 
696
        :return: A dictionary mapping text keys ((fileid, revision_id) tuples)
 
697
            to whether they were referred to by the inventory of the
 
698
            revision_id that they contain. The inventory texts from all present
 
699
            revision ids are assessed to generate this report.
 
700
        """
 
701
        self._ensure_real()
 
702
        return self._real_repository.find_text_key_references()
 
703
 
 
704
    def _generate_text_key_index(self):
 
705
        """Generate a new text key index for the repository.
 
706
 
 
707
        This is an expensive function that will take considerable time to run.
 
708
 
 
709
        :return: A dict mapping (file_id, revision_id) tuples to a list of
 
710
            parents, also (file_id, revision_id) tuples.
 
711
        """
 
712
        self._ensure_real()
 
713
        return self._real_repository._generate_text_key_index()
 
714
 
 
715
    def _get_revision_graph(self, revision_id):
 
716
        """Private method for using with old (< 1.2) servers to fallback."""
 
717
        if revision_id is None:
 
718
            revision_id = ''
 
719
        elif revision.is_null(revision_id):
 
720
            return {}
 
721
 
 
722
        path = self.bzrdir._path_for_remote_call(self._client)
 
723
        response = self._call_expecting_body(
 
724
            'Repository.get_revision_graph', path, revision_id)
 
725
        response_tuple, response_handler = response
 
726
        if response_tuple[0] != 'ok':
 
727
            raise errors.UnexpectedSmartServerResponse(response_tuple)
 
728
        coded = response_handler.read_body_bytes()
 
729
        if coded == '':
 
730
            # no revisions in this repository!
 
731
            return {}
 
732
        lines = coded.split('\n')
 
733
        revision_graph = {}
 
734
        for line in lines:
 
735
            d = tuple(line.split())
 
736
            revision_graph[d[0]] = d[1:]
 
737
 
 
738
        return revision_graph
 
739
 
 
740
    def _get_sink(self):
 
741
        """See Repository._get_sink()."""
 
742
        return RemoteStreamSink(self)
 
743
 
 
744
    def _get_source(self, to_format):
 
745
        """Return a source for streaming from this repository."""
 
746
        return RemoteStreamSource(self, to_format)
 
747
 
 
748
    def has_revision(self, revision_id):
 
749
        """See Repository.has_revision()."""
 
750
        if revision_id == NULL_REVISION:
 
751
            # The null revision is always present.
 
752
            return True
 
753
        path = self.bzrdir._path_for_remote_call(self._client)
 
754
        response = self._call('Repository.has_revision', path, revision_id)
 
755
        if response[0] not in ('yes', 'no'):
 
756
            raise errors.UnexpectedSmartServerResponse(response)
 
757
        if response[0] == 'yes':
 
758
            return True
 
759
        for fallback_repo in self._fallback_repositories:
 
760
            if fallback_repo.has_revision(revision_id):
 
761
                return True
 
762
        return False
 
763
 
 
764
    def has_revisions(self, revision_ids):
 
765
        """See Repository.has_revisions()."""
 
766
        # FIXME: This does many roundtrips, particularly when there are
 
767
        # fallback repositories.  -- mbp 20080905
 
768
        result = set()
 
769
        for revision_id in revision_ids:
 
770
            if self.has_revision(revision_id):
 
771
                result.add(revision_id)
 
772
        return result
 
773
 
 
774
    def has_same_location(self, other):
 
775
        return (self.__class__ is other.__class__ and
 
776
                self.bzrdir.transport.base == other.bzrdir.transport.base)
 
777
 
 
778
    def get_graph(self, other_repository=None):
 
779
        """Return the graph for this repository format"""
 
780
        parents_provider = self._make_parents_provider(other_repository)
 
781
        return graph.Graph(parents_provider)
 
782
 
 
783
    def gather_stats(self, revid=None, committers=None):
 
784
        """See Repository.gather_stats()."""
 
785
        path = self.bzrdir._path_for_remote_call(self._client)
 
786
        # revid can be None to indicate no revisions, not just NULL_REVISION
 
787
        if revid is None or revision.is_null(revid):
 
788
            fmt_revid = ''
 
789
        else:
 
790
            fmt_revid = revid
 
791
        if committers is None or not committers:
 
792
            fmt_committers = 'no'
 
793
        else:
 
794
            fmt_committers = 'yes'
 
795
        response_tuple, response_handler = self._call_expecting_body(
 
796
            'Repository.gather_stats', path, fmt_revid, fmt_committers)
 
797
        if response_tuple[0] != 'ok':
 
798
            raise errors.UnexpectedSmartServerResponse(response_tuple)
 
799
 
 
800
        body = response_handler.read_body_bytes()
 
801
        result = {}
 
802
        for line in body.split('\n'):
 
803
            if not line:
 
804
                continue
 
805
            key, val_text = line.split(':')
 
806
            if key in ('revisions', 'size', 'committers'):
 
807
                result[key] = int(val_text)
 
808
            elif key in ('firstrev', 'latestrev'):
 
809
                values = val_text.split(' ')[1:]
 
810
                result[key] = (float(values[0]), long(values[1]))
 
811
 
 
812
        return result
 
813
 
 
814
    def find_branches(self, using=False):
 
815
        """See Repository.find_branches()."""
 
816
        # should be an API call to the server.
 
817
        self._ensure_real()
 
818
        return self._real_repository.find_branches(using=using)
 
819
 
 
820
    def get_physical_lock_status(self):
 
821
        """See Repository.get_physical_lock_status()."""
 
822
        # should be an API call to the server.
 
823
        self._ensure_real()
 
824
        return self._real_repository.get_physical_lock_status()
 
825
 
 
826
    def is_in_write_group(self):
 
827
        """Return True if there is an open write group.
 
828
 
 
829
        write groups are only applicable locally for the smart server..
 
830
        """
 
831
        if self._real_repository:
 
832
            return self._real_repository.is_in_write_group()
 
833
 
 
834
    def is_locked(self):
 
835
        return self._lock_count >= 1
 
836
 
 
837
    def is_shared(self):
 
838
        """See Repository.is_shared()."""
 
839
        path = self.bzrdir._path_for_remote_call(self._client)
 
840
        response = self._call('Repository.is_shared', path)
 
841
        if response[0] not in ('yes', 'no'):
 
842
            raise SmartProtocolError('unexpected response code %s' % (response,))
 
843
        return response[0] == 'yes'
 
844
 
 
845
    def is_write_locked(self):
 
846
        return self._lock_mode == 'w'
 
847
 
 
848
    def lock_read(self):
 
849
        # wrong eventually - want a local lock cache context
 
850
        if not self._lock_mode:
 
851
            self._lock_mode = 'r'
 
852
            self._lock_count = 1
 
853
            self._unstacked_provider.enable_cache(cache_misses=True)
 
854
            if self._real_repository is not None:
 
855
                self._real_repository.lock_read()
 
856
        else:
 
857
            self._lock_count += 1
 
858
        for repo in self._fallback_repositories:
 
859
            repo.lock_read()
 
860
 
 
861
    def _remote_lock_write(self, token):
 
862
        path = self.bzrdir._path_for_remote_call(self._client)
 
863
        if token is None:
 
864
            token = ''
 
865
        err_context = {'token': token}
 
866
        response = self._call('Repository.lock_write', path, token,
 
867
                              **err_context)
 
868
        if response[0] == 'ok':
 
869
            ok, token = response
 
870
            return token
 
871
        else:
 
872
            raise errors.UnexpectedSmartServerResponse(response)
 
873
 
 
874
    def lock_write(self, token=None, _skip_rpc=False):
 
875
        if not self._lock_mode:
 
876
            if _skip_rpc:
 
877
                if self._lock_token is not None:
 
878
                    if token != self._lock_token:
 
879
                        raise errors.TokenMismatch(token, self._lock_token)
 
880
                self._lock_token = token
 
881
            else:
 
882
                self._lock_token = self._remote_lock_write(token)
 
883
            # if self._lock_token is None, then this is something like packs or
 
884
            # svn where we don't get to lock the repo, or a weave style repository
 
885
            # where we cannot lock it over the wire and attempts to do so will
 
886
            # fail.
 
887
            if self._real_repository is not None:
 
888
                self._real_repository.lock_write(token=self._lock_token)
 
889
            if token is not None:
 
890
                self._leave_lock = True
 
891
            else:
 
892
                self._leave_lock = False
 
893
            self._lock_mode = 'w'
 
894
            self._lock_count = 1
 
895
            self._unstacked_provider.enable_cache(cache_misses=False)
 
896
        elif self._lock_mode == 'r':
 
897
            raise errors.ReadOnlyError(self)
 
898
        else:
 
899
            self._lock_count += 1
 
900
        for repo in self._fallback_repositories:
 
901
            # Writes don't affect fallback repos
 
902
            repo.lock_read()
 
903
        return self._lock_token or None
 
904
 
 
905
    def leave_lock_in_place(self):
 
906
        if not self._lock_token:
 
907
            raise NotImplementedError(self.leave_lock_in_place)
 
908
        self._leave_lock = True
 
909
 
 
910
    def dont_leave_lock_in_place(self):
 
911
        if not self._lock_token:
 
912
            raise NotImplementedError(self.dont_leave_lock_in_place)
 
913
        self._leave_lock = False
 
914
 
 
915
    def _set_real_repository(self, repository):
 
916
        """Set the _real_repository for this repository.
 
917
 
 
918
        :param repository: The repository to fallback to for non-hpss
 
919
            implemented operations.
 
920
        """
 
921
        if self._real_repository is not None:
 
922
            # Replacing an already set real repository.
 
923
            # We cannot do this [currently] if the repository is locked -
 
924
            # synchronised state might be lost.
 
925
            if self.is_locked():
 
926
                raise AssertionError('_real_repository is already set')
 
927
        if isinstance(repository, RemoteRepository):
 
928
            raise AssertionError()
 
929
        self._real_repository = repository
 
930
        # three code paths happen here:
 
931
        # 1) old servers, RemoteBranch.open() calls _ensure_real before setting
 
932
        # up stacking. In this case self._fallback_repositories is [], and the
 
933
        # real repo is already setup. Preserve the real repo and
 
934
        # RemoteRepository.add_fallback_repository will avoid adding
 
935
        # duplicates.
 
936
        # 2) new servers, RemoteBranch.open() sets up stacking, and when
 
937
        # ensure_real is triggered from a branch, the real repository to
 
938
        # set already has a matching list with separate instances, but
 
939
        # as they are also RemoteRepositories we don't worry about making the
 
940
        # lists be identical.
 
941
        # 3) new servers, RemoteRepository.ensure_real is triggered before
 
942
        # RemoteBranch.ensure real, in this case we get a repo with no fallbacks
 
943
        # and need to populate it.
 
944
        if (self._fallback_repositories and
 
945
            len(self._real_repository._fallback_repositories) !=
 
946
            len(self._fallback_repositories)):
 
947
            if len(self._real_repository._fallback_repositories):
 
948
                raise AssertionError(
 
949
                    "cannot cleanly remove existing _fallback_repositories")
 
950
        for fb in self._fallback_repositories:
 
951
            self._real_repository.add_fallback_repository(fb)
 
952
        if self._lock_mode == 'w':
 
953
            # if we are already locked, the real repository must be able to
 
954
            # acquire the lock with our token.
 
955
            self._real_repository.lock_write(self._lock_token)
 
956
        elif self._lock_mode == 'r':
 
957
            self._real_repository.lock_read()
 
958
 
 
959
    def start_write_group(self):
 
960
        """Start a write group on the decorated repository.
 
961
 
 
962
        Smart methods perform operations in a single step so this API
 
963
        is not really applicable except as a compatibility thunk
 
964
        for older plugins that don't use e.g. the CommitBuilder
 
965
        facility.
 
966
        """
 
967
        self._ensure_real()
 
968
        return self._real_repository.start_write_group()
 
969
 
 
970
    def _unlock(self, token):
 
971
        path = self.bzrdir._path_for_remote_call(self._client)
 
972
        if not token:
 
973
            # with no token the remote repository is not persistently locked.
 
974
            return
 
975
        err_context = {'token': token}
 
976
        response = self._call('Repository.unlock', path, token,
 
977
                              **err_context)
 
978
        if response == ('ok',):
 
979
            return
 
980
        else:
 
981
            raise errors.UnexpectedSmartServerResponse(response)
 
982
 
 
983
    def unlock(self):
 
984
        if not self._lock_count:
 
985
            raise errors.LockNotHeld(self)
 
986
        self._lock_count -= 1
 
987
        if self._lock_count > 0:
 
988
            return
 
989
        self._unstacked_provider.disable_cache()
 
990
        old_mode = self._lock_mode
 
991
        self._lock_mode = None
 
992
        try:
 
993
            # The real repository is responsible at present for raising an
 
994
            # exception if it's in an unfinished write group.  However, it
 
995
            # normally will *not* actually remove the lock from disk - that's
 
996
            # done by the server on receiving the Repository.unlock call.
 
997
            # This is just to let the _real_repository stay up to date.
 
998
            if self._real_repository is not None:
 
999
                self._real_repository.unlock()
 
1000
        finally:
 
1001
            # The rpc-level lock should be released even if there was a
 
1002
            # problem releasing the vfs-based lock.
 
1003
            if old_mode == 'w':
 
1004
                # Only write-locked repositories need to make a remote method
 
1005
                # call to perform the unlock.
 
1006
                old_token = self._lock_token
 
1007
                self._lock_token = None
 
1008
                if not self._leave_lock:
 
1009
                    self._unlock(old_token)
 
1010
 
 
1011
    def break_lock(self):
 
1012
        # should hand off to the network
 
1013
        self._ensure_real()
 
1014
        return self._real_repository.break_lock()
 
1015
 
 
1016
    def _get_tarball(self, compression):
 
1017
        """Return a TemporaryFile containing a repository tarball.
 
1018
 
 
1019
        Returns None if the server does not support sending tarballs.
 
1020
        """
 
1021
        import tempfile
 
1022
        path = self.bzrdir._path_for_remote_call(self._client)
 
1023
        try:
 
1024
            response, protocol = self._call_expecting_body(
 
1025
                'Repository.tarball', path, compression)
 
1026
        except errors.UnknownSmartMethod:
 
1027
            protocol.cancel_read_body()
 
1028
            return None
 
1029
        if response[0] == 'ok':
 
1030
            # Extract the tarball and return it
 
1031
            t = tempfile.NamedTemporaryFile()
 
1032
            # TODO: rpc layer should read directly into it...
 
1033
            t.write(protocol.read_body_bytes())
 
1034
            t.seek(0)
 
1035
            return t
 
1036
        raise errors.UnexpectedSmartServerResponse(response)
 
1037
 
 
1038
    def sprout(self, to_bzrdir, revision_id=None):
 
1039
        # TODO: Option to control what format is created?
 
1040
        self._ensure_real()
 
1041
        dest_repo = self._real_repository._format.initialize(to_bzrdir,
 
1042
                                                             shared=False)
 
1043
        dest_repo.fetch(self, revision_id=revision_id)
 
1044
        return dest_repo
 
1045
 
 
1046
    ### These methods are just thin shims to the VFS object for now.
 
1047
 
 
1048
    def revision_tree(self, revision_id):
 
1049
        self._ensure_real()
 
1050
        return self._real_repository.revision_tree(revision_id)
 
1051
 
 
1052
    def get_serializer_format(self):
 
1053
        self._ensure_real()
 
1054
        return self._real_repository.get_serializer_format()
 
1055
 
 
1056
    def get_commit_builder(self, branch, parents, config, timestamp=None,
 
1057
                           timezone=None, committer=None, revprops=None,
 
1058
                           revision_id=None):
 
1059
        # FIXME: It ought to be possible to call this without immediately
 
1060
        # triggering _ensure_real.  For now it's the easiest thing to do.
 
1061
        self._ensure_real()
 
1062
        real_repo = self._real_repository
 
1063
        builder = real_repo.get_commit_builder(branch, parents,
 
1064
                config, timestamp=timestamp, timezone=timezone,
 
1065
                committer=committer, revprops=revprops, revision_id=revision_id)
 
1066
        return builder
 
1067
 
 
1068
    def add_fallback_repository(self, repository):
 
1069
        """Add a repository to use for looking up data not held locally.
 
1070
 
 
1071
        :param repository: A repository.
 
1072
        """
 
1073
        if not self._format.supports_external_lookups:
 
1074
            raise errors.UnstackableRepositoryFormat(
 
1075
                self._format.network_name(), self.base)
 
1076
        # We need to accumulate additional repositories here, to pass them in
 
1077
        # on various RPC's.
 
1078
        #
 
1079
        self._fallback_repositories.append(repository)
 
1080
        # If self._real_repository was parameterised already (e.g. because a
 
1081
        # _real_branch had its get_stacked_on_url method called), then the
 
1082
        # repository to be added may already be in the _real_repositories list.
 
1083
        if self._real_repository is not None:
 
1084
            fallback_locations = [repo.bzrdir.root_transport.base for repo in
 
1085
                self._real_repository._fallback_repositories]
 
1086
            if repository.bzrdir.root_transport.base not in fallback_locations:
 
1087
                self._real_repository.add_fallback_repository(repository)
 
1088
 
 
1089
    def add_inventory(self, revid, inv, parents):
 
1090
        self._ensure_real()
 
1091
        return self._real_repository.add_inventory(revid, inv, parents)
 
1092
 
 
1093
    def add_inventory_by_delta(self, basis_revision_id, delta, new_revision_id,
 
1094
                               parents):
 
1095
        self._ensure_real()
 
1096
        return self._real_repository.add_inventory_by_delta(basis_revision_id,
 
1097
            delta, new_revision_id, parents)
 
1098
 
 
1099
    def add_revision(self, rev_id, rev, inv=None, config=None):
 
1100
        self._ensure_real()
 
1101
        return self._real_repository.add_revision(
 
1102
            rev_id, rev, inv=inv, config=config)
 
1103
 
 
1104
    @needs_read_lock
 
1105
    def get_inventory(self, revision_id):
 
1106
        self._ensure_real()
 
1107
        return self._real_repository.get_inventory(revision_id)
 
1108
 
 
1109
    def iter_inventories(self, revision_ids):
 
1110
        self._ensure_real()
 
1111
        return self._real_repository.iter_inventories(revision_ids)
 
1112
 
 
1113
    @needs_read_lock
 
1114
    def get_revision(self, revision_id):
 
1115
        self._ensure_real()
 
1116
        return self._real_repository.get_revision(revision_id)
 
1117
 
 
1118
    def get_transaction(self):
 
1119
        self._ensure_real()
 
1120
        return self._real_repository.get_transaction()
 
1121
 
 
1122
    @needs_read_lock
 
1123
    def clone(self, a_bzrdir, revision_id=None):
 
1124
        self._ensure_real()
 
1125
        return self._real_repository.clone(a_bzrdir, revision_id=revision_id)
 
1126
 
 
1127
    def make_working_trees(self):
 
1128
        """See Repository.make_working_trees"""
 
1129
        self._ensure_real()
 
1130
        return self._real_repository.make_working_trees()
 
1131
 
 
1132
    def refresh_data(self):
 
1133
        """Re-read any data needed to to synchronise with disk.
 
1134
 
 
1135
        This method is intended to be called after another repository instance
 
1136
        (such as one used by a smart server) has inserted data into the
 
1137
        repository. It may not be called during a write group, but may be
 
1138
        called at any other time.
 
1139
        """
 
1140
        if self.is_in_write_group():
 
1141
            raise errors.InternalBzrError(
 
1142
                "May not refresh_data while in a write group.")
 
1143
        if self._real_repository is not None:
 
1144
            self._real_repository.refresh_data()
 
1145
 
 
1146
    def revision_ids_to_search_result(self, result_set):
 
1147
        """Convert a set of revision ids to a graph SearchResult."""
 
1148
        result_parents = set()
 
1149
        for parents in self.get_graph().get_parent_map(
 
1150
            result_set).itervalues():
 
1151
            result_parents.update(parents)
 
1152
        included_keys = result_set.intersection(result_parents)
 
1153
        start_keys = result_set.difference(included_keys)
 
1154
        exclude_keys = result_parents.difference(result_set)
 
1155
        result = graph.SearchResult(start_keys, exclude_keys,
 
1156
            len(result_set), result_set)
 
1157
        return result
 
1158
 
 
1159
    @needs_read_lock
 
1160
    def search_missing_revision_ids(self, other, revision_id=None, find_ghosts=True):
 
1161
        """Return the revision ids that other has that this does not.
 
1162
 
 
1163
        These are returned in topological order.
 
1164
 
 
1165
        revision_id: only return revision ids included by revision_id.
 
1166
        """
 
1167
        return repository.InterRepository.get(
 
1168
            other, self).search_missing_revision_ids(revision_id, find_ghosts)
 
1169
 
 
1170
    def fetch(self, source, revision_id=None, pb=None, find_ghosts=False,
 
1171
            fetch_spec=None):
 
1172
        # No base implementation to use as RemoteRepository is not a subclass
 
1173
        # of Repository; so this is a copy of Repository.fetch().
 
1174
        if fetch_spec is not None and revision_id is not None:
 
1175
            raise AssertionError(
 
1176
                "fetch_spec and revision_id are mutually exclusive.")
 
1177
        if self.is_in_write_group():
 
1178
            raise errors.InternalBzrError(
 
1179
                "May not fetch while in a write group.")
 
1180
        # fast path same-url fetch operations
 
1181
        if self.has_same_location(source) and fetch_spec is None:
 
1182
            # check that last_revision is in 'from' and then return a
 
1183
            # no-operation.
 
1184
            if (revision_id is not None and
 
1185
                not revision.is_null(revision_id)):
 
1186
                self.get_revision(revision_id)
 
1187
            return 0, []
 
1188
        # if there is no specific appropriate InterRepository, this will get
 
1189
        # the InterRepository base class, which raises an
 
1190
        # IncompatibleRepositories when asked to fetch.
 
1191
        inter = repository.InterRepository.get(source, self)
 
1192
        return inter.fetch(revision_id=revision_id, pb=pb,
 
1193
            find_ghosts=find_ghosts, fetch_spec=fetch_spec)
 
1194
 
 
1195
    def create_bundle(self, target, base, fileobj, format=None):
 
1196
        self._ensure_real()
 
1197
        self._real_repository.create_bundle(target, base, fileobj, format)
 
1198
 
 
1199
    @needs_read_lock
 
1200
    def get_ancestry(self, revision_id, topo_sorted=True):
 
1201
        self._ensure_real()
 
1202
        return self._real_repository.get_ancestry(revision_id, topo_sorted)
 
1203
 
 
1204
    def fileids_altered_by_revision_ids(self, revision_ids):
 
1205
        self._ensure_real()
 
1206
        return self._real_repository.fileids_altered_by_revision_ids(revision_ids)
 
1207
 
 
1208
    def _get_versioned_file_checker(self, revisions, revision_versions_cache):
 
1209
        self._ensure_real()
 
1210
        return self._real_repository._get_versioned_file_checker(
 
1211
            revisions, revision_versions_cache)
 
1212
 
 
1213
    def iter_files_bytes(self, desired_files):
 
1214
        """See Repository.iter_file_bytes.
 
1215
        """
 
1216
        self._ensure_real()
 
1217
        return self._real_repository.iter_files_bytes(desired_files)
 
1218
 
 
1219
    def get_parent_map(self, revision_ids):
 
1220
        """See bzrlib.Graph.get_parent_map()."""
 
1221
        return self._make_parents_provider().get_parent_map(revision_ids)
 
1222
 
 
1223
    def _get_parent_map_rpc(self, keys):
 
1224
        """Helper for get_parent_map that performs the RPC."""
 
1225
        medium = self._client._medium
 
1226
        if medium._is_remote_before((1, 2)):
 
1227
            # We already found out that the server can't understand
 
1228
            # Repository.get_parent_map requests, so just fetch the whole
 
1229
            # graph.
 
1230
            #
 
1231
            # Note that this reads the whole graph, when only some keys are
 
1232
            # wanted.  On this old server there's no way (?) to get them all
 
1233
            # in one go, and the user probably will have seen a warning about
 
1234
            # the server being old anyhow.
 
1235
            rg = self._get_revision_graph(None)
 
1236
            # There is an API discrepancy between get_parent_map and
 
1237
            # get_revision_graph. Specifically, a "key:()" pair in
 
1238
            # get_revision_graph just means a node has no parents. For
 
1239
            # "get_parent_map" it means the node is a ghost. So fix up the
 
1240
            # graph to correct this.
 
1241
            #   https://bugs.launchpad.net/bzr/+bug/214894
 
1242
            # There is one other "bug" which is that ghosts in
 
1243
            # get_revision_graph() are not returned at all. But we won't worry
 
1244
            # about that for now.
 
1245
            for node_id, parent_ids in rg.iteritems():
 
1246
                if parent_ids == ():
 
1247
                    rg[node_id] = (NULL_REVISION,)
 
1248
            rg[NULL_REVISION] = ()
 
1249
            return rg
 
1250
 
 
1251
        keys = set(keys)
 
1252
        if None in keys:
 
1253
            raise ValueError('get_parent_map(None) is not valid')
 
1254
        if NULL_REVISION in keys:
 
1255
            keys.discard(NULL_REVISION)
 
1256
            found_parents = {NULL_REVISION:()}
 
1257
            if not keys:
 
1258
                return found_parents
 
1259
        else:
 
1260
            found_parents = {}
 
1261
        # TODO(Needs analysis): We could assume that the keys being requested
 
1262
        # from get_parent_map are in a breadth first search, so typically they
 
1263
        # will all be depth N from some common parent, and we don't have to
 
1264
        # have the server iterate from the root parent, but rather from the
 
1265
        # keys we're searching; and just tell the server the keyspace we
 
1266
        # already have; but this may be more traffic again.
 
1267
 
 
1268
        # Transform self._parents_map into a search request recipe.
 
1269
        # TODO: Manage this incrementally to avoid covering the same path
 
1270
        # repeatedly. (The server will have to on each request, but the less
 
1271
        # work done the better).
 
1272
        #
 
1273
        # Negative caching notes:
 
1274
        # new server sends missing when a request including the revid
 
1275
        # 'include-missing:' is present in the request.
 
1276
        # missing keys are serialised as missing:X, and we then call
 
1277
        # provider.note_missing(X) for-all X
 
1278
        parents_map = self._unstacked_provider.get_cached_map()
 
1279
        if parents_map is None:
 
1280
            # Repository is not locked, so there's no cache.
 
1281
            parents_map = {}
 
1282
        # start_set is all the keys in the cache
 
1283
        start_set = set(parents_map)
 
1284
        # result set is all the references to keys in the cache
 
1285
        result_parents = set()
 
1286
        for parents in parents_map.itervalues():
 
1287
            result_parents.update(parents)
 
1288
        stop_keys = result_parents.difference(start_set)
 
1289
        # We don't need to send ghosts back to the server as a position to
 
1290
        # stop either.
 
1291
        stop_keys.difference_update(self._unstacked_provider.missing_keys)
 
1292
        key_count = len(parents_map)
 
1293
        if (NULL_REVISION in result_parents
 
1294
            and NULL_REVISION in self._unstacked_provider.missing_keys):
 
1295
            # If we pruned NULL_REVISION from the stop_keys because it's also
 
1296
            # in our cache of "missing" keys we need to increment our key count
 
1297
            # by 1, because the reconsitituted SearchResult on the server will
 
1298
            # still consider NULL_REVISION to be an included key.
 
1299
            key_count += 1
 
1300
        included_keys = start_set.intersection(result_parents)
 
1301
        start_set.difference_update(included_keys)
 
1302
        recipe = ('manual', start_set, stop_keys, key_count)
 
1303
        body = self._serialise_search_recipe(recipe)
 
1304
        path = self.bzrdir._path_for_remote_call(self._client)
 
1305
        for key in keys:
 
1306
            if type(key) is not str:
 
1307
                raise ValueError(
 
1308
                    "key %r not a plain string" % (key,))
 
1309
        verb = 'Repository.get_parent_map'
 
1310
        args = (path, 'include-missing:') + tuple(keys)
 
1311
        try:
 
1312
            response = self._call_with_body_bytes_expecting_body(
 
1313
                verb, args, body)
 
1314
        except errors.UnknownSmartMethod:
 
1315
            # Server does not support this method, so get the whole graph.
 
1316
            # Worse, we have to force a disconnection, because the server now
 
1317
            # doesn't realise it has a body on the wire to consume, so the
 
1318
            # only way to recover is to abandon the connection.
 
1319
            warning(
 
1320
                'Server is too old for fast get_parent_map, reconnecting.  '
 
1321
                '(Upgrade the server to Bazaar 1.2 to avoid this)')
 
1322
            medium.disconnect()
 
1323
            # To avoid having to disconnect repeatedly, we keep track of the
 
1324
            # fact the server doesn't understand remote methods added in 1.2.
 
1325
            medium._remember_remote_is_before((1, 2))
 
1326
            # Recurse just once and we should use the fallback code.
 
1327
            return self._get_parent_map_rpc(keys)
 
1328
        response_tuple, response_handler = response
 
1329
        if response_tuple[0] not in ['ok']:
 
1330
            response_handler.cancel_read_body()
 
1331
            raise errors.UnexpectedSmartServerResponse(response_tuple)
 
1332
        if response_tuple[0] == 'ok':
 
1333
            coded = bz2.decompress(response_handler.read_body_bytes())
 
1334
            if coded == '':
 
1335
                # no revisions found
 
1336
                return {}
 
1337
            lines = coded.split('\n')
 
1338
            revision_graph = {}
 
1339
            for line in lines:
 
1340
                d = tuple(line.split())
 
1341
                if len(d) > 1:
 
1342
                    revision_graph[d[0]] = d[1:]
 
1343
                else:
 
1344
                    # No parents:
 
1345
                    if d[0].startswith('missing:'):
 
1346
                        revid = d[0][8:]
 
1347
                        self._unstacked_provider.note_missing_key(revid)
 
1348
                    else:
 
1349
                        # no parents - so give the Graph result
 
1350
                        # (NULL_REVISION,).
 
1351
                        revision_graph[d[0]] = (NULL_REVISION,)
 
1352
            return revision_graph
 
1353
 
 
1354
    @needs_read_lock
 
1355
    def get_signature_text(self, revision_id):
 
1356
        self._ensure_real()
 
1357
        return self._real_repository.get_signature_text(revision_id)
 
1358
 
 
1359
    @needs_read_lock
 
1360
    def get_inventory_xml(self, revision_id):
 
1361
        self._ensure_real()
 
1362
        return self._real_repository.get_inventory_xml(revision_id)
 
1363
 
 
1364
    def deserialise_inventory(self, revision_id, xml):
 
1365
        self._ensure_real()
 
1366
        return self._real_repository.deserialise_inventory(revision_id, xml)
 
1367
 
 
1368
    def reconcile(self, other=None, thorough=False):
 
1369
        self._ensure_real()
 
1370
        return self._real_repository.reconcile(other=other, thorough=thorough)
 
1371
 
 
1372
    def all_revision_ids(self):
 
1373
        self._ensure_real()
 
1374
        return self._real_repository.all_revision_ids()
 
1375
 
 
1376
    @needs_read_lock
 
1377
    def get_deltas_for_revisions(self, revisions, specific_fileids=None):
 
1378
        self._ensure_real()
 
1379
        return self._real_repository.get_deltas_for_revisions(revisions,
 
1380
            specific_fileids=specific_fileids)
 
1381
 
 
1382
    @needs_read_lock
 
1383
    def get_revision_delta(self, revision_id, specific_fileids=None):
 
1384
        self._ensure_real()
 
1385
        return self._real_repository.get_revision_delta(revision_id,
 
1386
            specific_fileids=specific_fileids)
 
1387
 
 
1388
    @needs_read_lock
 
1389
    def revision_trees(self, revision_ids):
 
1390
        self._ensure_real()
 
1391
        return self._real_repository.revision_trees(revision_ids)
 
1392
 
 
1393
    @needs_read_lock
 
1394
    def get_revision_reconcile(self, revision_id):
 
1395
        self._ensure_real()
 
1396
        return self._real_repository.get_revision_reconcile(revision_id)
 
1397
 
 
1398
    @needs_read_lock
 
1399
    def check(self, revision_ids=None):
 
1400
        self._ensure_real()
 
1401
        return self._real_repository.check(revision_ids=revision_ids)
 
1402
 
 
1403
    def copy_content_into(self, destination, revision_id=None):
 
1404
        self._ensure_real()
 
1405
        return self._real_repository.copy_content_into(
 
1406
            destination, revision_id=revision_id)
 
1407
 
 
1408
    def _copy_repository_tarball(self, to_bzrdir, revision_id=None):
 
1409
        # get a tarball of the remote repository, and copy from that into the
 
1410
        # destination
 
1411
        from bzrlib import osutils
 
1412
        import tarfile
 
1413
        # TODO: Maybe a progress bar while streaming the tarball?
 
1414
        note("Copying repository content as tarball...")
 
1415
        tar_file = self._get_tarball('bz2')
 
1416
        if tar_file is None:
 
1417
            return None
 
1418
        destination = to_bzrdir.create_repository()
 
1419
        try:
 
1420
            tar = tarfile.open('repository', fileobj=tar_file,
 
1421
                mode='r|bz2')
 
1422
            tmpdir = osutils.mkdtemp()
 
1423
            try:
 
1424
                _extract_tar(tar, tmpdir)
 
1425
                tmp_bzrdir = BzrDir.open(tmpdir)
 
1426
                tmp_repo = tmp_bzrdir.open_repository()
 
1427
                tmp_repo.copy_content_into(destination, revision_id)
 
1428
            finally:
 
1429
                osutils.rmtree(tmpdir)
 
1430
        finally:
 
1431
            tar_file.close()
 
1432
        return destination
 
1433
        # TODO: Suggestion from john: using external tar is much faster than
 
1434
        # python's tarfile library, but it may not work on windows.
 
1435
 
 
1436
    @property
 
1437
    def inventories(self):
 
1438
        """Decorate the real repository for now.
 
1439
 
 
1440
        In the long term a full blown network facility is needed to
 
1441
        avoid creating a real repository object locally.
 
1442
        """
 
1443
        self._ensure_real()
 
1444
        return self._real_repository.inventories
 
1445
 
 
1446
    @needs_write_lock
 
1447
    def pack(self):
 
1448
        """Compress the data within the repository.
 
1449
 
 
1450
        This is not currently implemented within the smart server.
 
1451
        """
 
1452
        self._ensure_real()
 
1453
        return self._real_repository.pack()
 
1454
 
 
1455
    @property
 
1456
    def revisions(self):
 
1457
        """Decorate the real repository for now.
 
1458
 
 
1459
        In the short term this should become a real object to intercept graph
 
1460
        lookups.
 
1461
 
 
1462
        In the long term a full blown network facility is needed.
 
1463
        """
 
1464
        self._ensure_real()
 
1465
        return self._real_repository.revisions
 
1466
 
 
1467
    def set_make_working_trees(self, new_value):
 
1468
        if new_value:
 
1469
            new_value_str = "True"
 
1470
        else:
 
1471
            new_value_str = "False"
 
1472
        path = self.bzrdir._path_for_remote_call(self._client)
 
1473
        try:
 
1474
            response = self._call(
 
1475
                'Repository.set_make_working_trees', path, new_value_str)
 
1476
        except errors.UnknownSmartMethod:
 
1477
            self._ensure_real()
 
1478
            self._real_repository.set_make_working_trees(new_value)
 
1479
        else:
 
1480
            if response[0] != 'ok':
 
1481
                raise errors.UnexpectedSmartServerResponse(response)
 
1482
 
 
1483
    @property
 
1484
    def signatures(self):
 
1485
        """Decorate the real repository for now.
 
1486
 
 
1487
        In the long term a full blown network facility is needed to avoid
 
1488
        creating a real repository object locally.
 
1489
        """
 
1490
        self._ensure_real()
 
1491
        return self._real_repository.signatures
 
1492
 
 
1493
    @needs_write_lock
 
1494
    def sign_revision(self, revision_id, gpg_strategy):
 
1495
        self._ensure_real()
 
1496
        return self._real_repository.sign_revision(revision_id, gpg_strategy)
 
1497
 
 
1498
    @property
 
1499
    def texts(self):
 
1500
        """Decorate the real repository for now.
 
1501
 
 
1502
        In the long term a full blown network facility is needed to avoid
 
1503
        creating a real repository object locally.
 
1504
        """
 
1505
        self._ensure_real()
 
1506
        return self._real_repository.texts
 
1507
 
 
1508
    @needs_read_lock
 
1509
    def get_revisions(self, revision_ids):
 
1510
        self._ensure_real()
 
1511
        return self._real_repository.get_revisions(revision_ids)
 
1512
 
 
1513
    def supports_rich_root(self):
 
1514
        return self._format.rich_root_data
 
1515
 
 
1516
    def iter_reverse_revision_history(self, revision_id):
 
1517
        self._ensure_real()
 
1518
        return self._real_repository.iter_reverse_revision_history(revision_id)
 
1519
 
 
1520
    @property
 
1521
    def _serializer(self):
 
1522
        return self._format._serializer
 
1523
 
 
1524
    def store_revision_signature(self, gpg_strategy, plaintext, revision_id):
 
1525
        self._ensure_real()
 
1526
        return self._real_repository.store_revision_signature(
 
1527
            gpg_strategy, plaintext, revision_id)
 
1528
 
 
1529
    def add_signature_text(self, revision_id, signature):
 
1530
        self._ensure_real()
 
1531
        return self._real_repository.add_signature_text(revision_id, signature)
 
1532
 
 
1533
    def has_signature_for_revision_id(self, revision_id):
 
1534
        self._ensure_real()
 
1535
        return self._real_repository.has_signature_for_revision_id(revision_id)
 
1536
 
 
1537
    def item_keys_introduced_by(self, revision_ids, _files_pb=None):
 
1538
        self._ensure_real()
 
1539
        return self._real_repository.item_keys_introduced_by(revision_ids,
 
1540
            _files_pb=_files_pb)
 
1541
 
 
1542
    def revision_graph_can_have_wrong_parents(self):
 
1543
        # The answer depends on the remote repo format.
 
1544
        self._ensure_real()
 
1545
        return self._real_repository.revision_graph_can_have_wrong_parents()
 
1546
 
 
1547
    def _find_inconsistent_revision_parents(self):
 
1548
        self._ensure_real()
 
1549
        return self._real_repository._find_inconsistent_revision_parents()
 
1550
 
 
1551
    def _check_for_inconsistent_revision_parents(self):
 
1552
        self._ensure_real()
 
1553
        return self._real_repository._check_for_inconsistent_revision_parents()
 
1554
 
 
1555
    def _make_parents_provider(self, other=None):
 
1556
        providers = [self._unstacked_provider]
 
1557
        if other is not None:
 
1558
            providers.insert(0, other)
 
1559
        providers.extend(r._make_parents_provider() for r in
 
1560
                         self._fallback_repositories)
 
1561
        return graph._StackedParentsProvider(providers)
 
1562
 
 
1563
    def _serialise_search_recipe(self, recipe):
 
1564
        """Serialise a graph search recipe.
 
1565
 
 
1566
        :param recipe: A search recipe (start, stop, count).
 
1567
        :return: Serialised bytes.
 
1568
        """
 
1569
        start_keys = ' '.join(recipe[1])
 
1570
        stop_keys = ' '.join(recipe[2])
 
1571
        count = str(recipe[3])
 
1572
        return '\n'.join((start_keys, stop_keys, count))
 
1573
 
 
1574
    def _serialise_search_result(self, search_result):
 
1575
        if isinstance(search_result, graph.PendingAncestryResult):
 
1576
            parts = ['ancestry-of']
 
1577
            parts.extend(search_result.heads)
 
1578
        else:
 
1579
            recipe = search_result.get_recipe()
 
1580
            parts = [recipe[0], self._serialise_search_recipe(recipe)]
 
1581
        return '\n'.join(parts)
 
1582
 
 
1583
    def autopack(self):
 
1584
        path = self.bzrdir._path_for_remote_call(self._client)
 
1585
        try:
 
1586
            response = self._call('PackRepository.autopack', path)
 
1587
        except errors.UnknownSmartMethod:
 
1588
            self._ensure_real()
 
1589
            self._real_repository._pack_collection.autopack()
 
1590
            return
 
1591
        self.refresh_data()
 
1592
        if response[0] != 'ok':
 
1593
            raise errors.UnexpectedSmartServerResponse(response)
 
1594
 
 
1595
 
 
1596
class RemoteStreamSink(repository.StreamSink):
 
1597
 
 
1598
    def _insert_real(self, stream, src_format, resume_tokens):
 
1599
        self.target_repo._ensure_real()
 
1600
        sink = self.target_repo._real_repository._get_sink()
 
1601
        result = sink.insert_stream(stream, src_format, resume_tokens)
 
1602
        if not result:
 
1603
            self.target_repo.autopack()
 
1604
        return result
 
1605
 
 
1606
    def insert_stream(self, stream, src_format, resume_tokens):
 
1607
        target = self.target_repo
 
1608
        if target._lock_token:
 
1609
            verb = 'Repository.insert_stream_locked'
 
1610
            extra_args = (target._lock_token or '',)
 
1611
            required_version = (1, 14)
 
1612
        else:
 
1613
            verb = 'Repository.insert_stream'
 
1614
            extra_args = ()
 
1615
            required_version = (1, 13)
 
1616
        client = target._client
 
1617
        medium = client._medium
 
1618
        if medium._is_remote_before(required_version):
 
1619
            # No possible way this can work.
 
1620
            return self._insert_real(stream, src_format, resume_tokens)
 
1621
        path = target.bzrdir._path_for_remote_call(client)
 
1622
        if not resume_tokens:
 
1623
            # XXX: Ugly but important for correctness, *will* be fixed during
 
1624
            # 1.13 cycle. Pushing a stream that is interrupted results in a
 
1625
            # fallback to the _real_repositories sink *with a partial stream*.
 
1626
            # Thats bad because we insert less data than bzr expected. To avoid
 
1627
            # this we do a trial push to make sure the verb is accessible, and
 
1628
            # do not fallback when actually pushing the stream. A cleanup patch
 
1629
            # is going to look at rewinding/restarting the stream/partial
 
1630
            # buffering etc.
 
1631
            byte_stream = smart_repo._stream_to_byte_stream([], src_format)
 
1632
            try:
 
1633
                response = client.call_with_body_stream(
 
1634
                    (verb, path, '') + extra_args, byte_stream)
 
1635
            except errors.UnknownSmartMethod:
 
1636
                medium._remember_remote_is_before(required_version)
 
1637
                return self._insert_real(stream, src_format, resume_tokens)
 
1638
        byte_stream = smart_repo._stream_to_byte_stream(
 
1639
            stream, src_format)
 
1640
        resume_tokens = ' '.join(resume_tokens)
 
1641
        response = client.call_with_body_stream(
 
1642
            (verb, path, resume_tokens) + extra_args, byte_stream)
 
1643
        if response[0][0] not in ('ok', 'missing-basis'):
 
1644
            raise errors.UnexpectedSmartServerResponse(response)
 
1645
        if response[0][0] == 'missing-basis':
 
1646
            tokens, missing_keys = bencode.bdecode_as_tuple(response[0][1])
 
1647
            resume_tokens = tokens
 
1648
            return resume_tokens, missing_keys
 
1649
        else:
 
1650
            self.target_repo.refresh_data()
 
1651
            return [], set()
 
1652
 
 
1653
 
 
1654
class RemoteStreamSource(repository.StreamSource):
 
1655
    """Stream data from a remote server."""
 
1656
 
 
1657
    def get_stream(self, search):
 
1658
        if (self.from_repository._fallback_repositories and
 
1659
            self.to_format._fetch_order == 'topological'):
 
1660
            return self._real_stream(self.from_repository, search)
 
1661
        return self.missing_parents_chain(search, [self.from_repository] +
 
1662
            self.from_repository._fallback_repositories)
 
1663
 
 
1664
    def _real_stream(self, repo, search):
 
1665
        """Get a stream for search from repo.
 
1666
        
 
1667
        This never called RemoteStreamSource.get_stream, and is a heler
 
1668
        for RemoteStreamSource._get_stream to allow getting a stream 
 
1669
        reliably whether fallback back because of old servers or trying
 
1670
        to stream from a non-RemoteRepository (which the stacked support
 
1671
        code will do).
 
1672
        """
 
1673
        source = repo._get_source(self.to_format)
 
1674
        if isinstance(source, RemoteStreamSource):
 
1675
            return repository.StreamSource.get_stream(source, search)
 
1676
        return source.get_stream(search)
 
1677
 
 
1678
    def _get_stream(self, repo, search):
 
1679
        """Core worker to get a stream from repo for search.
 
1680
 
 
1681
        This is used by both get_stream and the stacking support logic. It
 
1682
        deliberately gets a stream for repo which does not need to be
 
1683
        self.from_repository. In the event that repo is not Remote, or
 
1684
        cannot do a smart stream, a fallback is made to the generic
 
1685
        repository._get_stream() interface, via self._real_stream.
 
1686
 
 
1687
        In the event of stacking, streams from _get_stream will not
 
1688
        contain all the data for search - this is normal (see get_stream).
 
1689
 
 
1690
        :param repo: A repository.
 
1691
        :param search: A search.
 
1692
        """
 
1693
        # Fallbacks may be non-smart
 
1694
        if not isinstance(repo, RemoteRepository):
 
1695
            return self._real_stream(repo, search)
 
1696
        client = repo._client
 
1697
        medium = client._medium
 
1698
        if medium._is_remote_before((1, 13)):
 
1699
            # streaming was added in 1.13
 
1700
            return self._real_stream(repo, search)
 
1701
        path = repo.bzrdir._path_for_remote_call(client)
 
1702
        try:
 
1703
            search_bytes = repo._serialise_search_result(search)
 
1704
            response = repo._call_with_body_bytes_expecting_body(
 
1705
                'Repository.get_stream',
 
1706
                (path, self.to_format.network_name()), search_bytes)
 
1707
            response_tuple, response_handler = response
 
1708
        except errors.UnknownSmartMethod:
 
1709
            medium._remember_remote_is_before((1,13))
 
1710
            return self._real_stream(repo, search)
 
1711
        if response_tuple[0] != 'ok':
 
1712
            raise errors.UnexpectedSmartServerResponse(response_tuple)
 
1713
        byte_stream = response_handler.read_streamed_body()
 
1714
        src_format, stream = smart_repo._byte_stream_to_stream(byte_stream)
 
1715
        if src_format.network_name() != repo._format.network_name():
 
1716
            raise AssertionError(
 
1717
                "Mismatched RemoteRepository and stream src %r, %r" % (
 
1718
                src_format.network_name(), repo._format.network_name()))
 
1719
        return stream
 
1720
 
 
1721
    def missing_parents_chain(self, search, sources):
 
1722
        """Chain multiple streams together to handle stacking.
 
1723
 
 
1724
        :param search: The overall search to satisfy with streams.
 
1725
        :param sources: A list of Repository objects to query.
 
1726
        """
 
1727
        self.serialiser = self.to_format._serializer
 
1728
        self.seen_revs = set()
 
1729
        self.referenced_revs = set()
 
1730
        # If there are heads in the search, or the key count is > 0, we are not
 
1731
        # done.
 
1732
        while not search.is_empty() and len(sources) > 1:
 
1733
            source = sources.pop(0)
 
1734
            stream = self._get_stream(source, search)
 
1735
            for kind, substream in stream:
 
1736
                if kind != 'revisions':
 
1737
                    yield kind, substream
 
1738
                else:
 
1739
                    yield kind, self.missing_parents_rev_handler(substream)
 
1740
            search = search.refine(self.seen_revs, self.referenced_revs)
 
1741
            self.seen_revs = set()
 
1742
            self.referenced_revs = set()
 
1743
        if not search.is_empty():
 
1744
            for kind, stream in self._get_stream(sources[0], search):
 
1745
                yield kind, stream
 
1746
 
 
1747
    def missing_parents_rev_handler(self, substream):
 
1748
        for content in substream:
 
1749
            revision_bytes = content.get_bytes_as('fulltext')
 
1750
            revision = self.serialiser.read_revision_from_string(revision_bytes)
 
1751
            self.seen_revs.add(content.key[-1])
 
1752
            self.referenced_revs.update(revision.parent_ids)
 
1753
            yield content
 
1754
 
 
1755
 
 
1756
class RemoteBranchLockableFiles(LockableFiles):
 
1757
    """A 'LockableFiles' implementation that talks to a smart server.
 
1758
 
 
1759
    This is not a public interface class.
 
1760
    """
 
1761
 
 
1762
    def __init__(self, bzrdir, _client):
 
1763
        self.bzrdir = bzrdir
 
1764
        self._client = _client
 
1765
        self._need_find_modes = True
 
1766
        LockableFiles.__init__(
 
1767
            self, bzrdir.get_branch_transport(None),
 
1768
            'lock', lockdir.LockDir)
 
1769
 
 
1770
    def _find_modes(self):
 
1771
        # RemoteBranches don't let the client set the mode of control files.
 
1772
        self._dir_mode = None
 
1773
        self._file_mode = None
 
1774
 
 
1775
 
 
1776
class RemoteBranchFormat(branch.BranchFormat):
 
1777
 
 
1778
    def __init__(self, network_name=None):
 
1779
        super(RemoteBranchFormat, self).__init__()
 
1780
        self._matchingbzrdir = RemoteBzrDirFormat()
 
1781
        self._matchingbzrdir.set_branch_format(self)
 
1782
        self._custom_format = None
 
1783
        self._network_name = network_name
 
1784
 
 
1785
    def __eq__(self, other):
 
1786
        return (isinstance(other, RemoteBranchFormat) and
 
1787
            self.__dict__ == other.__dict__)
 
1788
 
 
1789
    def _ensure_real(self):
 
1790
        if self._custom_format is None:
 
1791
            self._custom_format = branch.network_format_registry.get(
 
1792
                self._network_name)
 
1793
 
 
1794
    def get_format_description(self):
 
1795
        return 'Remote BZR Branch'
 
1796
 
 
1797
    def network_name(self):
 
1798
        return self._network_name
 
1799
 
 
1800
    def open(self, a_bzrdir, ignore_fallbacks=False):
 
1801
        return a_bzrdir.open_branch(ignore_fallbacks=ignore_fallbacks)
 
1802
 
 
1803
    def _vfs_initialize(self, a_bzrdir):
 
1804
        # Initialisation when using a local bzrdir object, or a non-vfs init
 
1805
        # method is not available on the server.
 
1806
        # self._custom_format is always set - the start of initialize ensures
 
1807
        # that.
 
1808
        if isinstance(a_bzrdir, RemoteBzrDir):
 
1809
            a_bzrdir._ensure_real()
 
1810
            result = self._custom_format.initialize(a_bzrdir._real_bzrdir)
 
1811
        else:
 
1812
            # We assume the bzrdir is parameterised; it may not be.
 
1813
            result = self._custom_format.initialize(a_bzrdir)
 
1814
        if (isinstance(a_bzrdir, RemoteBzrDir) and
 
1815
            not isinstance(result, RemoteBranch)):
 
1816
            result = RemoteBranch(a_bzrdir, a_bzrdir.find_repository(), result)
 
1817
        return result
 
1818
 
 
1819
    def initialize(self, a_bzrdir):
 
1820
        # 1) get the network name to use.
 
1821
        if self._custom_format:
 
1822
            network_name = self._custom_format.network_name()
 
1823
        else:
 
1824
            # Select the current bzrlib default and ask for that.
 
1825
            reference_bzrdir_format = bzrdir.format_registry.get('default')()
 
1826
            reference_format = reference_bzrdir_format.get_branch_format()
 
1827
            self._custom_format = reference_format
 
1828
            network_name = reference_format.network_name()
 
1829
        # Being asked to create on a non RemoteBzrDir:
 
1830
        if not isinstance(a_bzrdir, RemoteBzrDir):
 
1831
            return self._vfs_initialize(a_bzrdir)
 
1832
        medium = a_bzrdir._client._medium
 
1833
        if medium._is_remote_before((1, 13)):
 
1834
            return self._vfs_initialize(a_bzrdir)
 
1835
        # Creating on a remote bzr dir.
 
1836
        # 2) try direct creation via RPC
 
1837
        path = a_bzrdir._path_for_remote_call(a_bzrdir._client)
 
1838
        verb = 'BzrDir.create_branch'
 
1839
        try:
 
1840
            response = a_bzrdir._call(verb, path, network_name)
 
1841
        except errors.UnknownSmartMethod:
 
1842
            # Fallback - use vfs methods
 
1843
            medium._remember_remote_is_before((1, 13))
 
1844
            return self._vfs_initialize(a_bzrdir)
 
1845
        if response[0] != 'ok':
 
1846
            raise errors.UnexpectedSmartServerResponse(response)
 
1847
        # Turn the response into a RemoteRepository object.
 
1848
        format = RemoteBranchFormat(network_name=response[1])
 
1849
        repo_format = response_tuple_to_repo_format(response[3:])
 
1850
        if response[2] == '':
 
1851
            repo_bzrdir = a_bzrdir
 
1852
        else:
 
1853
            repo_bzrdir = RemoteBzrDir(
 
1854
                a_bzrdir.root_transport.clone(response[2]), a_bzrdir._format,
 
1855
                a_bzrdir._client)
 
1856
        remote_repo = RemoteRepository(repo_bzrdir, repo_format)
 
1857
        remote_branch = RemoteBranch(a_bzrdir, remote_repo,
 
1858
            format=format, setup_stacking=False)
 
1859
        # XXX: We know this is a new branch, so it must have revno 0, revid
 
1860
        # NULL_REVISION. Creating the branch locked would make this be unable
 
1861
        # to be wrong; here its simply very unlikely to be wrong. RBC 20090225
 
1862
        remote_branch._last_revision_info_cache = 0, NULL_REVISION
 
1863
        return remote_branch
 
1864
 
 
1865
    def make_tags(self, branch):
 
1866
        self._ensure_real()
 
1867
        return self._custom_format.make_tags(branch)
 
1868
 
 
1869
    def supports_tags(self):
 
1870
        # Remote branches might support tags, but we won't know until we
 
1871
        # access the real remote branch.
 
1872
        self._ensure_real()
 
1873
        return self._custom_format.supports_tags()
 
1874
 
 
1875
    def supports_stacking(self):
 
1876
        self._ensure_real()
 
1877
        return self._custom_format.supports_stacking()
 
1878
 
 
1879
 
 
1880
class RemoteBranch(branch.Branch, _RpcHelper):
 
1881
    """Branch stored on a server accessed by HPSS RPC.
 
1882
 
 
1883
    At the moment most operations are mapped down to simple file operations.
 
1884
    """
 
1885
 
 
1886
    def __init__(self, remote_bzrdir, remote_repository, real_branch=None,
 
1887
        _client=None, format=None, setup_stacking=True):
 
1888
        """Create a RemoteBranch instance.
 
1889
 
 
1890
        :param real_branch: An optional local implementation of the branch
 
1891
            format, usually accessing the data via the VFS.
 
1892
        :param _client: Private parameter for testing.
 
1893
        :param format: A RemoteBranchFormat object, None to create one
 
1894
            automatically. If supplied it should have a network_name already
 
1895
            supplied.
 
1896
        :param setup_stacking: If True make an RPC call to determine the
 
1897
            stacked (or not) status of the branch. If False assume the branch
 
1898
            is not stacked.
 
1899
        """
 
1900
        # We intentionally don't call the parent class's __init__, because it
 
1901
        # will try to assign to self.tags, which is a property in this subclass.
 
1902
        # And the parent's __init__ doesn't do much anyway.
 
1903
        self._revision_id_to_revno_cache = None
 
1904
        self._partial_revision_id_to_revno_cache = {}
 
1905
        self._revision_history_cache = None
 
1906
        self._last_revision_info_cache = None
 
1907
        self._merge_sorted_revisions_cache = None
 
1908
        self.bzrdir = remote_bzrdir
 
1909
        if _client is not None:
 
1910
            self._client = _client
 
1911
        else:
 
1912
            self._client = remote_bzrdir._client
 
1913
        self.repository = remote_repository
 
1914
        if real_branch is not None:
 
1915
            self._real_branch = real_branch
 
1916
            # Give the remote repository the matching real repo.
 
1917
            real_repo = self._real_branch.repository
 
1918
            if isinstance(real_repo, RemoteRepository):
 
1919
                real_repo._ensure_real()
 
1920
                real_repo = real_repo._real_repository
 
1921
            self.repository._set_real_repository(real_repo)
 
1922
            # Give the branch the remote repository to let fast-pathing happen.
 
1923
            self._real_branch.repository = self.repository
 
1924
        else:
 
1925
            self._real_branch = None
 
1926
        # Fill out expected attributes of branch for bzrlib API users.
 
1927
        self.base = self.bzrdir.root_transport.base
 
1928
        self._control_files = None
 
1929
        self._lock_mode = None
 
1930
        self._lock_token = None
 
1931
        self._repo_lock_token = None
 
1932
        self._lock_count = 0
 
1933
        self._leave_lock = False
 
1934
        # Setup a format: note that we cannot call _ensure_real until all the
 
1935
        # attributes above are set: This code cannot be moved higher up in this
 
1936
        # function.
 
1937
        if format is None:
 
1938
            self._format = RemoteBranchFormat()
 
1939
            if real_branch is not None:
 
1940
                self._format._network_name = \
 
1941
                    self._real_branch._format.network_name()
 
1942
        else:
 
1943
            self._format = format
 
1944
        if not self._format._network_name:
 
1945
            # Did not get from open_branchV2 - old server.
 
1946
            self._ensure_real()
 
1947
            self._format._network_name = \
 
1948
                self._real_branch._format.network_name()
 
1949
        self.tags = self._format.make_tags(self)
 
1950
        # The base class init is not called, so we duplicate this:
 
1951
        hooks = branch.Branch.hooks['open']
 
1952
        for hook in hooks:
 
1953
            hook(self)
 
1954
        if setup_stacking:
 
1955
            self._setup_stacking()
 
1956
 
 
1957
    def _setup_stacking(self):
 
1958
        # configure stacking into the remote repository, by reading it from
 
1959
        # the vfs branch.
 
1960
        try:
 
1961
            fallback_url = self.get_stacked_on_url()
 
1962
        except (errors.NotStacked, errors.UnstackableBranchFormat,
 
1963
            errors.UnstackableRepositoryFormat), e:
 
1964
            return
 
1965
        self._activate_fallback_location(fallback_url)
 
1966
 
 
1967
    def _get_config(self):
 
1968
        return RemoteBranchConfig(self)
 
1969
 
 
1970
    def _get_real_transport(self):
 
1971
        # if we try vfs access, return the real branch's vfs transport
 
1972
        self._ensure_real()
 
1973
        return self._real_branch._transport
 
1974
 
 
1975
    _transport = property(_get_real_transport)
 
1976
 
 
1977
    def __str__(self):
 
1978
        return "%s(%s)" % (self.__class__.__name__, self.base)
 
1979
 
 
1980
    __repr__ = __str__
 
1981
 
 
1982
    def _ensure_real(self):
 
1983
        """Ensure that there is a _real_branch set.
 
1984
 
 
1985
        Used before calls to self._real_branch.
 
1986
        """
 
1987
        if self._real_branch is None:
 
1988
            if not vfs.vfs_enabled():
 
1989
                raise AssertionError('smart server vfs must be enabled '
 
1990
                    'to use vfs implementation')
 
1991
            self.bzrdir._ensure_real()
 
1992
            self._real_branch = self.bzrdir._real_bzrdir.open_branch()
 
1993
            if self.repository._real_repository is None:
 
1994
                # Give the remote repository the matching real repo.
 
1995
                real_repo = self._real_branch.repository
 
1996
                if isinstance(real_repo, RemoteRepository):
 
1997
                    real_repo._ensure_real()
 
1998
                    real_repo = real_repo._real_repository
 
1999
                self.repository._set_real_repository(real_repo)
 
2000
            # Give the real branch the remote repository to let fast-pathing
 
2001
            # happen.
 
2002
            self._real_branch.repository = self.repository
 
2003
            if self._lock_mode == 'r':
 
2004
                self._real_branch.lock_read()
 
2005
            elif self._lock_mode == 'w':
 
2006
                self._real_branch.lock_write(token=self._lock_token)
 
2007
 
 
2008
    def _translate_error(self, err, **context):
 
2009
        self.repository._translate_error(err, branch=self, **context)
 
2010
 
 
2011
    def _clear_cached_state(self):
 
2012
        super(RemoteBranch, self)._clear_cached_state()
 
2013
        if self._real_branch is not None:
 
2014
            self._real_branch._clear_cached_state()
 
2015
 
 
2016
    def _clear_cached_state_of_remote_branch_only(self):
 
2017
        """Like _clear_cached_state, but doesn't clear the cache of
 
2018
        self._real_branch.
 
2019
 
 
2020
        This is useful when falling back to calling a method of
 
2021
        self._real_branch that changes state.  In that case the underlying
 
2022
        branch changes, so we need to invalidate this RemoteBranch's cache of
 
2023
        it.  However, there's no need to invalidate the _real_branch's cache
 
2024
        too, in fact doing so might harm performance.
 
2025
        """
 
2026
        super(RemoteBranch, self)._clear_cached_state()
 
2027
 
 
2028
    @property
 
2029
    def control_files(self):
 
2030
        # Defer actually creating RemoteBranchLockableFiles until its needed,
 
2031
        # because it triggers an _ensure_real that we otherwise might not need.
 
2032
        if self._control_files is None:
 
2033
            self._control_files = RemoteBranchLockableFiles(
 
2034
                self.bzrdir, self._client)
 
2035
        return self._control_files
 
2036
 
 
2037
    def _get_checkout_format(self):
 
2038
        self._ensure_real()
 
2039
        return self._real_branch._get_checkout_format()
 
2040
 
 
2041
    def get_physical_lock_status(self):
 
2042
        """See Branch.get_physical_lock_status()."""
 
2043
        # should be an API call to the server, as branches must be lockable.
 
2044
        self._ensure_real()
 
2045
        return self._real_branch.get_physical_lock_status()
 
2046
 
 
2047
    def get_stacked_on_url(self):
 
2048
        """Get the URL this branch is stacked against.
 
2049
 
 
2050
        :raises NotStacked: If the branch is not stacked.
 
2051
        :raises UnstackableBranchFormat: If the branch does not support
 
2052
            stacking.
 
2053
        :raises UnstackableRepositoryFormat: If the repository does not support
 
2054
            stacking.
 
2055
        """
 
2056
        try:
 
2057
            # there may not be a repository yet, so we can't use
 
2058
            # self._translate_error, so we can't use self._call either.
 
2059
            response = self._client.call('Branch.get_stacked_on_url',
 
2060
                self._remote_path())
 
2061
        except errors.ErrorFromSmartServer, err:
 
2062
            # there may not be a repository yet, so we can't call through
 
2063
            # its _translate_error
 
2064
            _translate_error(err, branch=self)
 
2065
        except errors.UnknownSmartMethod, err:
 
2066
            self._ensure_real()
 
2067
            return self._real_branch.get_stacked_on_url()
 
2068
        if response[0] != 'ok':
 
2069
            raise errors.UnexpectedSmartServerResponse(response)
 
2070
        return response[1]
 
2071
 
 
2072
    def _vfs_get_tags_bytes(self):
 
2073
        self._ensure_real()
 
2074
        return self._real_branch._get_tags_bytes()
 
2075
 
 
2076
    def _get_tags_bytes(self):
 
2077
        medium = self._client._medium
 
2078
        if medium._is_remote_before((1, 13)):
 
2079
            return self._vfs_get_tags_bytes()
 
2080
        try:
 
2081
            response = self._call('Branch.get_tags_bytes', self._remote_path())
 
2082
        except errors.UnknownSmartMethod:
 
2083
            medium._remember_remote_is_before((1, 13))
 
2084
            return self._vfs_get_tags_bytes()
 
2085
        return response[0]
 
2086
 
 
2087
    def lock_read(self):
 
2088
        self.repository.lock_read()
 
2089
        if not self._lock_mode:
 
2090
            self._lock_mode = 'r'
 
2091
            self._lock_count = 1
 
2092
            if self._real_branch is not None:
 
2093
                self._real_branch.lock_read()
 
2094
        else:
 
2095
            self._lock_count += 1
 
2096
 
 
2097
    def _remote_lock_write(self, token):
 
2098
        if token is None:
 
2099
            branch_token = repo_token = ''
 
2100
        else:
 
2101
            branch_token = token
 
2102
            repo_token = self.repository.lock_write()
 
2103
            self.repository.unlock()
 
2104
        err_context = {'token': token}
 
2105
        response = self._call(
 
2106
            'Branch.lock_write', self._remote_path(), branch_token,
 
2107
            repo_token or '', **err_context)
 
2108
        if response[0] != 'ok':
 
2109
            raise errors.UnexpectedSmartServerResponse(response)
 
2110
        ok, branch_token, repo_token = response
 
2111
        return branch_token, repo_token
 
2112
 
 
2113
    def lock_write(self, token=None):
 
2114
        if not self._lock_mode:
 
2115
            # Lock the branch and repo in one remote call.
 
2116
            remote_tokens = self._remote_lock_write(token)
 
2117
            self._lock_token, self._repo_lock_token = remote_tokens
 
2118
            if not self._lock_token:
 
2119
                raise SmartProtocolError('Remote server did not return a token!')
 
2120
            # Tell the self.repository object that it is locked.
 
2121
            self.repository.lock_write(
 
2122
                self._repo_lock_token, _skip_rpc=True)
 
2123
 
 
2124
            if self._real_branch is not None:
 
2125
                self._real_branch.lock_write(token=self._lock_token)
 
2126
            if token is not None:
 
2127
                self._leave_lock = True
 
2128
            else:
 
2129
                self._leave_lock = False
 
2130
            self._lock_mode = 'w'
 
2131
            self._lock_count = 1
 
2132
        elif self._lock_mode == 'r':
 
2133
            raise errors.ReadOnlyTransaction
 
2134
        else:
 
2135
            if token is not None:
 
2136
                # A token was given to lock_write, and we're relocking, so
 
2137
                # check that the given token actually matches the one we
 
2138
                # already have.
 
2139
                if token != self._lock_token:
 
2140
                    raise errors.TokenMismatch(token, self._lock_token)
 
2141
            self._lock_count += 1
 
2142
            # Re-lock the repository too.
 
2143
            self.repository.lock_write(self._repo_lock_token)
 
2144
        return self._lock_token or None
 
2145
 
 
2146
    def _set_tags_bytes(self, bytes):
 
2147
        self._ensure_real()
 
2148
        return self._real_branch._set_tags_bytes(bytes)
 
2149
 
 
2150
    def _unlock(self, branch_token, repo_token):
 
2151
        err_context = {'token': str((branch_token, repo_token))}
 
2152
        response = self._call(
 
2153
            'Branch.unlock', self._remote_path(), branch_token,
 
2154
            repo_token or '', **err_context)
 
2155
        if response == ('ok',):
 
2156
            return
 
2157
        raise errors.UnexpectedSmartServerResponse(response)
 
2158
 
 
2159
    def unlock(self):
 
2160
        try:
 
2161
            self._lock_count -= 1
 
2162
            if not self._lock_count:
 
2163
                self._clear_cached_state()
 
2164
                mode = self._lock_mode
 
2165
                self._lock_mode = None
 
2166
                if self._real_branch is not None:
 
2167
                    if (not self._leave_lock and mode == 'w' and
 
2168
                        self._repo_lock_token):
 
2169
                        # If this RemoteBranch will remove the physical lock
 
2170
                        # for the repository, make sure the _real_branch
 
2171
                        # doesn't do it first.  (Because the _real_branch's
 
2172
                        # repository is set to be the RemoteRepository.)
 
2173
                        self._real_branch.repository.leave_lock_in_place()
 
2174
                    self._real_branch.unlock()
 
2175
                if mode != 'w':
 
2176
                    # Only write-locked branched need to make a remote method
 
2177
                    # call to perform the unlock.
 
2178
                    return
 
2179
                if not self._lock_token:
 
2180
                    raise AssertionError('Locked, but no token!')
 
2181
                branch_token = self._lock_token
 
2182
                repo_token = self._repo_lock_token
 
2183
                self._lock_token = None
 
2184
                self._repo_lock_token = None
 
2185
                if not self._leave_lock:
 
2186
                    self._unlock(branch_token, repo_token)
 
2187
        finally:
 
2188
            self.repository.unlock()
 
2189
 
 
2190
    def break_lock(self):
 
2191
        self._ensure_real()
 
2192
        return self._real_branch.break_lock()
 
2193
 
 
2194
    def leave_lock_in_place(self):
 
2195
        if not self._lock_token:
 
2196
            raise NotImplementedError(self.leave_lock_in_place)
 
2197
        self._leave_lock = True
 
2198
 
 
2199
    def dont_leave_lock_in_place(self):
 
2200
        if not self._lock_token:
 
2201
            raise NotImplementedError(self.dont_leave_lock_in_place)
 
2202
        self._leave_lock = False
 
2203
 
 
2204
    def _last_revision_info(self):
 
2205
        response = self._call('Branch.last_revision_info', self._remote_path())
 
2206
        if response[0] != 'ok':
 
2207
            raise SmartProtocolError('unexpected response code %s' % (response,))
 
2208
        revno = int(response[1])
 
2209
        last_revision = response[2]
 
2210
        return (revno, last_revision)
 
2211
 
 
2212
    def _gen_revision_history(self):
 
2213
        """See Branch._gen_revision_history()."""
 
2214
        response_tuple, response_handler = self._call_expecting_body(
 
2215
            'Branch.revision_history', self._remote_path())
 
2216
        if response_tuple[0] != 'ok':
 
2217
            raise errors.UnexpectedSmartServerResponse(response_tuple)
 
2218
        result = response_handler.read_body_bytes().split('\x00')
 
2219
        if result == ['']:
 
2220
            return []
 
2221
        return result
 
2222
 
 
2223
    def _remote_path(self):
 
2224
        return self.bzrdir._path_for_remote_call(self._client)
 
2225
 
 
2226
    def _set_last_revision_descendant(self, revision_id, other_branch,
 
2227
            allow_diverged=False, allow_overwrite_descendant=False):
 
2228
        # This performs additional work to meet the hook contract; while its
 
2229
        # undesirable, we have to synthesise the revno to call the hook, and
 
2230
        # not calling the hook is worse as it means changes can't be prevented.
 
2231
        # Having calculated this though, we can't just call into
 
2232
        # set_last_revision_info as a simple call, because there is a set_rh
 
2233
        # hook that some folk may still be using.
 
2234
        old_revno, old_revid = self.last_revision_info()
 
2235
        history = self._lefthand_history(revision_id)
 
2236
        self._run_pre_change_branch_tip_hooks(len(history), revision_id)
 
2237
        err_context = {'other_branch': other_branch}
 
2238
        response = self._call('Branch.set_last_revision_ex',
 
2239
            self._remote_path(), self._lock_token, self._repo_lock_token,
 
2240
            revision_id, int(allow_diverged), int(allow_overwrite_descendant),
 
2241
            **err_context)
 
2242
        self._clear_cached_state()
 
2243
        if len(response) != 3 and response[0] != 'ok':
 
2244
            raise errors.UnexpectedSmartServerResponse(response)
 
2245
        new_revno, new_revision_id = response[1:]
 
2246
        self._last_revision_info_cache = new_revno, new_revision_id
 
2247
        self._run_post_change_branch_tip_hooks(old_revno, old_revid)
 
2248
        if self._real_branch is not None:
 
2249
            cache = new_revno, new_revision_id
 
2250
            self._real_branch._last_revision_info_cache = cache
 
2251
 
 
2252
    def _set_last_revision(self, revision_id):
 
2253
        old_revno, old_revid = self.last_revision_info()
 
2254
        # This performs additional work to meet the hook contract; while its
 
2255
        # undesirable, we have to synthesise the revno to call the hook, and
 
2256
        # not calling the hook is worse as it means changes can't be prevented.
 
2257
        # Having calculated this though, we can't just call into
 
2258
        # set_last_revision_info as a simple call, because there is a set_rh
 
2259
        # hook that some folk may still be using.
 
2260
        history = self._lefthand_history(revision_id)
 
2261
        self._run_pre_change_branch_tip_hooks(len(history), revision_id)
 
2262
        self._clear_cached_state()
 
2263
        response = self._call('Branch.set_last_revision',
 
2264
            self._remote_path(), self._lock_token, self._repo_lock_token,
 
2265
            revision_id)
 
2266
        if response != ('ok',):
 
2267
            raise errors.UnexpectedSmartServerResponse(response)
 
2268
        self._run_post_change_branch_tip_hooks(old_revno, old_revid)
 
2269
 
 
2270
    @needs_write_lock
 
2271
    def set_revision_history(self, rev_history):
 
2272
        # Send just the tip revision of the history; the server will generate
 
2273
        # the full history from that.  If the revision doesn't exist in this
 
2274
        # branch, NoSuchRevision will be raised.
 
2275
        if rev_history == []:
 
2276
            rev_id = 'null:'
 
2277
        else:
 
2278
            rev_id = rev_history[-1]
 
2279
        self._set_last_revision(rev_id)
 
2280
        for hook in branch.Branch.hooks['set_rh']:
 
2281
            hook(self, rev_history)
 
2282
        self._cache_revision_history(rev_history)
 
2283
 
 
2284
    def _get_parent_location(self):
 
2285
        medium = self._client._medium
 
2286
        if medium._is_remote_before((1, 13)):
 
2287
            return self._vfs_get_parent_location()
 
2288
        try:
 
2289
            response = self._call('Branch.get_parent', self._remote_path())
 
2290
        except errors.UnknownSmartMethod:
 
2291
            medium._remember_remote_is_before((1, 13))
 
2292
            return self._vfs_get_parent_location()
 
2293
        if len(response) != 1:
 
2294
            raise errors.UnexpectedSmartServerResponse(response)
 
2295
        parent_location = response[0]
 
2296
        if parent_location == '':
 
2297
            return None
 
2298
        return parent_location
 
2299
 
 
2300
    def _vfs_get_parent_location(self):
 
2301
        self._ensure_real()
 
2302
        return self._real_branch._get_parent_location()
 
2303
 
 
2304
    def set_parent(self, url):
 
2305
        self._ensure_real()
 
2306
        return self._real_branch.set_parent(url)
 
2307
 
 
2308
    def _set_parent_location(self, url):
 
2309
        # Used by tests, to poke bad urls into branch configurations
 
2310
        if url is None:
 
2311
            self.set_parent(url)
 
2312
        else:
 
2313
            self._ensure_real()
 
2314
            return self._real_branch._set_parent_location(url)
 
2315
 
 
2316
    @needs_write_lock
 
2317
    def pull(self, source, overwrite=False, stop_revision=None,
 
2318
             **kwargs):
 
2319
        self._clear_cached_state_of_remote_branch_only()
 
2320
        self._ensure_real()
 
2321
        return self._real_branch.pull(
 
2322
            source, overwrite=overwrite, stop_revision=stop_revision,
 
2323
            _override_hook_target=self, **kwargs)
 
2324
 
 
2325
    @needs_read_lock
 
2326
    def push(self, target, overwrite=False, stop_revision=None):
 
2327
        self._ensure_real()
 
2328
        return self._real_branch.push(
 
2329
            target, overwrite=overwrite, stop_revision=stop_revision,
 
2330
            _override_hook_source_branch=self)
 
2331
 
 
2332
    def is_locked(self):
 
2333
        return self._lock_count >= 1
 
2334
 
 
2335
    @needs_read_lock
 
2336
    def revision_id_to_revno(self, revision_id):
 
2337
        self._ensure_real()
 
2338
        return self._real_branch.revision_id_to_revno(revision_id)
 
2339
 
 
2340
    @needs_write_lock
 
2341
    def set_last_revision_info(self, revno, revision_id):
 
2342
        # XXX: These should be returned by the set_last_revision_info verb
 
2343
        old_revno, old_revid = self.last_revision_info()
 
2344
        self._run_pre_change_branch_tip_hooks(revno, revision_id)
 
2345
        revision_id = ensure_null(revision_id)
 
2346
        try:
 
2347
            response = self._call('Branch.set_last_revision_info',
 
2348
                self._remote_path(), self._lock_token, self._repo_lock_token,
 
2349
                str(revno), revision_id)
 
2350
        except errors.UnknownSmartMethod:
 
2351
            self._ensure_real()
 
2352
            self._clear_cached_state_of_remote_branch_only()
 
2353
            self._real_branch.set_last_revision_info(revno, revision_id)
 
2354
            self._last_revision_info_cache = revno, revision_id
 
2355
            return
 
2356
        if response == ('ok',):
 
2357
            self._clear_cached_state()
 
2358
            self._last_revision_info_cache = revno, revision_id
 
2359
            self._run_post_change_branch_tip_hooks(old_revno, old_revid)
 
2360
            # Update the _real_branch's cache too.
 
2361
            if self._real_branch is not None:
 
2362
                cache = self._last_revision_info_cache
 
2363
                self._real_branch._last_revision_info_cache = cache
 
2364
        else:
 
2365
            raise errors.UnexpectedSmartServerResponse(response)
 
2366
 
 
2367
    @needs_write_lock
 
2368
    def generate_revision_history(self, revision_id, last_rev=None,
 
2369
                                  other_branch=None):
 
2370
        medium = self._client._medium
 
2371
        if not medium._is_remote_before((1, 6)):
 
2372
            # Use a smart method for 1.6 and above servers
 
2373
            try:
 
2374
                self._set_last_revision_descendant(revision_id, other_branch,
 
2375
                    allow_diverged=True, allow_overwrite_descendant=True)
 
2376
                return
 
2377
            except errors.UnknownSmartMethod:
 
2378
                medium._remember_remote_is_before((1, 6))
 
2379
        self._clear_cached_state_of_remote_branch_only()
 
2380
        self.set_revision_history(self._lefthand_history(revision_id,
 
2381
            last_rev=last_rev,other_branch=other_branch))
 
2382
 
 
2383
    def set_push_location(self, location):
 
2384
        self._ensure_real()
 
2385
        return self._real_branch.set_push_location(location)
 
2386
 
 
2387
 
 
2388
class RemoteBranchConfig(object):
 
2389
    """A Config that reads from a smart branch and writes via smart methods.
 
2390
 
 
2391
    It is a low-level object that considers config data to be name/value pairs
 
2392
    that may be associated with a section. Assigning meaning to the these
 
2393
    values is done at higher levels like bzrlib.config.TreeConfig.
 
2394
    """
 
2395
 
 
2396
    def __init__(self, branch):
 
2397
        self._branch = branch
 
2398
 
 
2399
    def get_option(self, name, section=None, default=None):
 
2400
        """Return the value associated with a named option.
 
2401
 
 
2402
        :param name: The name of the value
 
2403
        :param section: The section the option is in (if any)
 
2404
        :param default: The value to return if the value is not set
 
2405
        :return: The value or default value
 
2406
        """
 
2407
        configobj = self._get_configobj()
 
2408
        if section is None:
 
2409
            section_obj = configobj
 
2410
        else:
 
2411
            try:
 
2412
                section_obj = configobj[section]
 
2413
            except KeyError:
 
2414
                return default
 
2415
        return section_obj.get(name, default)
 
2416
 
 
2417
    def _get_configobj(self):
 
2418
        path = self._branch._remote_path()
 
2419
        response = self._branch._client.call_expecting_body(
 
2420
            'Branch.get_config_file', path)
 
2421
        if response[0][0] != 'ok':
 
2422
            raise UnexpectedSmartServerResponse(response)
 
2423
        lines = response[1].read_body_bytes().splitlines()
 
2424
        return config.ConfigObj(lines, encoding='utf-8')
 
2425
 
 
2426
    def set_option(self, value, name, section=None):
 
2427
        """Set the value associated with a named option.
 
2428
 
 
2429
        :param value: The value to set
 
2430
        :param name: The name of the value to set
 
2431
        :param section: The section the option is in (if any)
 
2432
        """
 
2433
        medium = self._branch._client._medium
 
2434
        if medium._is_remote_before((1, 14)):
 
2435
            return self._vfs_set_option(value, name, section)
 
2436
        try:
 
2437
            path = self._branch._remote_path()
 
2438
            response = self._branch._client.call('Branch.set_config_option',
 
2439
                path, self._branch._lock_token, self._branch._repo_lock_token,
 
2440
                value.encode('utf8'), name, section or '')
 
2441
        except errors.UnknownSmartMethod:
 
2442
            medium._remember_remote_is_before((1, 14))
 
2443
            return self._vfs_set_option(value, name, section)
 
2444
        if response != ():
 
2445
            raise errors.UnexpectedSmartServerResponse(response)
 
2446
 
 
2447
    def _vfs_set_option(self, value, name, section=None):
 
2448
        self._branch._ensure_real()
 
2449
        return self._branch._real_branch._get_config().set_option(
 
2450
            value, name, section)
 
2451
 
 
2452
 
 
2453
def _extract_tar(tar, to_dir):
 
2454
    """Extract all the contents of a tarfile object.
 
2455
 
 
2456
    A replacement for extractall, which is not present in python2.4
 
2457
    """
 
2458
    for tarinfo in tar:
 
2459
        tar.extract(tarinfo, to_dir)
 
2460
 
 
2461
 
 
2462
def _translate_error(err, **context):
 
2463
    """Translate an ErrorFromSmartServer into a more useful error.
 
2464
 
 
2465
    Possible context keys:
 
2466
      - branch
 
2467
      - repository
 
2468
      - bzrdir
 
2469
      - token
 
2470
      - other_branch
 
2471
      - path
 
2472
 
 
2473
    If the error from the server doesn't match a known pattern, then
 
2474
    UnknownErrorFromSmartServer is raised.
 
2475
    """
 
2476
    def find(name):
 
2477
        try:
 
2478
            return context[name]
 
2479
        except KeyError, key_err:
 
2480
            mutter('Missing key %r in context %r', key_err.args[0], context)
 
2481
            raise err
 
2482
    def get_path():
 
2483
        """Get the path from the context if present, otherwise use first error
 
2484
        arg.
 
2485
        """
 
2486
        try:
 
2487
            return context['path']
 
2488
        except KeyError, key_err:
 
2489
            try:
 
2490
                return err.error_args[0]
 
2491
            except IndexError, idx_err:
 
2492
                mutter(
 
2493
                    'Missing key %r in context %r', key_err.args[0], context)
 
2494
                raise err
 
2495
 
 
2496
    if err.error_verb == 'NoSuchRevision':
 
2497
        raise NoSuchRevision(find('branch'), err.error_args[0])
 
2498
    elif err.error_verb == 'nosuchrevision':
 
2499
        raise NoSuchRevision(find('repository'), err.error_args[0])
 
2500
    elif err.error_tuple == ('nobranch',):
 
2501
        raise errors.NotBranchError(path=find('bzrdir').root_transport.base)
 
2502
    elif err.error_verb == 'norepository':
 
2503
        raise errors.NoRepositoryPresent(find('bzrdir'))
 
2504
    elif err.error_verb == 'LockContention':
 
2505
        raise errors.LockContention('(remote lock)')
 
2506
    elif err.error_verb == 'UnlockableTransport':
 
2507
        raise errors.UnlockableTransport(find('bzrdir').root_transport)
 
2508
    elif err.error_verb == 'LockFailed':
 
2509
        raise errors.LockFailed(err.error_args[0], err.error_args[1])
 
2510
    elif err.error_verb == 'TokenMismatch':
 
2511
        raise errors.TokenMismatch(find('token'), '(remote token)')
 
2512
    elif err.error_verb == 'Diverged':
 
2513
        raise errors.DivergedBranches(find('branch'), find('other_branch'))
 
2514
    elif err.error_verb == 'TipChangeRejected':
 
2515
        raise errors.TipChangeRejected(err.error_args[0].decode('utf8'))
 
2516
    elif err.error_verb == 'UnstackableBranchFormat':
 
2517
        raise errors.UnstackableBranchFormat(*err.error_args)
 
2518
    elif err.error_verb == 'UnstackableRepositoryFormat':
 
2519
        raise errors.UnstackableRepositoryFormat(*err.error_args)
 
2520
    elif err.error_verb == 'NotStacked':
 
2521
        raise errors.NotStacked(branch=find('branch'))
 
2522
    elif err.error_verb == 'PermissionDenied':
 
2523
        path = get_path()
 
2524
        if len(err.error_args) >= 2:
 
2525
            extra = err.error_args[1]
 
2526
        else:
 
2527
            extra = None
 
2528
        raise errors.PermissionDenied(path, extra=extra)
 
2529
    elif err.error_verb == 'ReadError':
 
2530
        path = get_path()
 
2531
        raise errors.ReadError(path)
 
2532
    elif err.error_verb == 'NoSuchFile':
 
2533
        path = get_path()
 
2534
        raise errors.NoSuchFile(path)
 
2535
    elif err.error_verb == 'FileExists':
 
2536
        raise errors.FileExists(err.error_args[0])
 
2537
    elif err.error_verb == 'DirectoryNotEmpty':
 
2538
        raise errors.DirectoryNotEmpty(err.error_args[0])
 
2539
    elif err.error_verb == 'ShortReadvError':
 
2540
        args = err.error_args
 
2541
        raise errors.ShortReadvError(
 
2542
            args[0], int(args[1]), int(args[2]), int(args[3]))
 
2543
    elif err.error_verb in ('UnicodeEncodeError', 'UnicodeDecodeError'):
 
2544
        encoding = str(err.error_args[0]) # encoding must always be a string
 
2545
        val = err.error_args[1]
 
2546
        start = int(err.error_args[2])
 
2547
        end = int(err.error_args[3])
 
2548
        reason = str(err.error_args[4]) # reason must always be a string
 
2549
        if val.startswith('u:'):
 
2550
            val = val[2:].decode('utf-8')
 
2551
        elif val.startswith('s:'):
 
2552
            val = val[2:].decode('base64')
 
2553
        if err.error_verb == 'UnicodeDecodeError':
 
2554
            raise UnicodeDecodeError(encoding, val, start, end, reason)
 
2555
        elif err.error_verb == 'UnicodeEncodeError':
 
2556
            raise UnicodeEncodeError(encoding, val, start, end, reason)
 
2557
    elif err.error_verb == 'ReadOnlyError':
 
2558
        raise errors.TransportNotPossible('readonly transport')
 
2559
    raise errors.UnknownErrorFromSmartServer(err)