~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/remote.py

  • Committer: Jelmer Vernooij
  • Date: 2011-12-16 16:40:10 UTC
  • mto: This revision was merged to the branch mainline in revision 6391.
  • Revision ID: jelmer@samba.org-20111216164010-z3hy00xrnclnkf7a
Update tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 2006-2011 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
import bz2
 
18
import zlib
18
19
 
19
20
from bzrlib import (
20
21
    bencode,
21
22
    branch,
22
 
    bzrdir,
 
23
    bzrdir as _mod_bzrdir,
23
24
    config,
24
25
    controldir,
25
26
    debug,
26
27
    errors,
 
28
    gpg,
27
29
    graph,
 
30
    inventory_delta,
28
31
    lock,
29
32
    lockdir,
30
 
    repository,
 
33
    osutils,
 
34
    registry,
31
35
    repository as _mod_repository,
32
 
    revision,
33
36
    revision as _mod_revision,
34
37
    static_tuple,
35
38
    symbol_versioning,
36
 
)
 
39
    testament as _mod_testament,
 
40
    urlutils,
 
41
    vf_repository,
 
42
    vf_search,
 
43
    )
37
44
from bzrlib.branch import BranchReferenceFormat, BranchWriteLockResult
38
 
from bzrlib.bzrdir import BzrDir, RemoteBzrDirFormat
39
45
from bzrlib.decorators import needs_read_lock, needs_write_lock, only_raises
40
46
from bzrlib.errors import (
41
47
    NoSuchRevision,
42
48
    SmartProtocolError,
43
49
    )
 
50
from bzrlib.i18n import gettext
 
51
from bzrlib.inventory import Inventory
44
52
from bzrlib.lockable_files import LockableFiles
45
53
from bzrlib.smart import client, vfs, repository as smart_repo
46
 
from bzrlib.revision import ensure_null, NULL_REVISION
47
 
from bzrlib.repository import RepositoryWriteLockResult
48
 
from bzrlib.trace import mutter, note, warning
 
54
from bzrlib.smart.client import _SmartClient
 
55
from bzrlib.revision import NULL_REVISION
 
56
from bzrlib.revisiontree import InventoryRevisionTree
 
57
from bzrlib.repository import RepositoryWriteLockResult, _LazyListJoin
 
58
from bzrlib.serializer import format_registry as serializer_format_registry
 
59
from bzrlib.trace import mutter, note, warning, log_exception_quietly
 
60
 
 
61
 
 
62
_DEFAULT_SEARCH_DEPTH = 100
49
63
 
50
64
 
51
65
class _RpcHelper(object):
88
102
    return format
89
103
 
90
104
 
91
 
# Note: RemoteBzrDirFormat is in bzrdir.py
92
 
 
93
 
class RemoteBzrDir(BzrDir, _RpcHelper):
 
105
# Note that RemoteBzrDirProber lives in bzrlib.bzrdir so bzrlib.remote
 
106
# does not have to be imported unless a remote format is involved.
 
107
 
 
108
class RemoteBzrDirFormat(_mod_bzrdir.BzrDirMetaFormat1):
 
109
    """Format representing bzrdirs accessed via a smart server"""
 
110
 
 
111
    supports_workingtrees = False
 
112
 
 
113
    def __init__(self):
 
114
        _mod_bzrdir.BzrDirMetaFormat1.__init__(self)
 
115
        # XXX: It's a bit ugly that the network name is here, because we'd
 
116
        # like to believe that format objects are stateless or at least
 
117
        # immutable,  However, we do at least avoid mutating the name after
 
118
        # it's returned.  See <https://bugs.launchpad.net/bzr/+bug/504102>
 
119
        self._network_name = None
 
120
 
 
121
    def __repr__(self):
 
122
        return "%s(_network_name=%r)" % (self.__class__.__name__,
 
123
            self._network_name)
 
124
 
 
125
    def get_format_description(self):
 
126
        if self._network_name:
 
127
            try:
 
128
                real_format = controldir.network_format_registry.get(
 
129
                        self._network_name)
 
130
            except KeyError:
 
131
                pass
 
132
            else:
 
133
                return 'Remote: ' + real_format.get_format_description()
 
134
        return 'bzr remote bzrdir'
 
135
 
 
136
    def get_format_string(self):
 
137
        raise NotImplementedError(self.get_format_string)
 
138
 
 
139
    def network_name(self):
 
140
        if self._network_name:
 
141
            return self._network_name
 
142
        else:
 
143
            raise AssertionError("No network name set.")
 
144
 
 
145
    def initialize_on_transport(self, transport):
 
146
        try:
 
147
            # hand off the request to the smart server
 
148
            client_medium = transport.get_smart_medium()
 
149
        except errors.NoSmartMedium:
 
150
            # TODO: lookup the local format from a server hint.
 
151
            local_dir_format = _mod_bzrdir.BzrDirMetaFormat1()
 
152
            return local_dir_format.initialize_on_transport(transport)
 
153
        client = _SmartClient(client_medium)
 
154
        path = client.remote_path_from_transport(transport)
 
155
        try:
 
156
            response = client.call('BzrDirFormat.initialize', path)
 
157
        except errors.ErrorFromSmartServer, err:
 
158
            _translate_error(err, path=path)
 
159
        if response[0] != 'ok':
 
160
            raise errors.SmartProtocolError('unexpected response code %s' % (response,))
 
161
        format = RemoteBzrDirFormat()
 
162
        self._supply_sub_formats_to(format)
 
163
        return RemoteBzrDir(transport, format)
 
164
 
 
165
    def parse_NoneTrueFalse(self, arg):
 
166
        if not arg:
 
167
            return None
 
168
        if arg == 'False':
 
169
            return False
 
170
        if arg == 'True':
 
171
            return True
 
172
        raise AssertionError("invalid arg %r" % arg)
 
173
 
 
174
    def _serialize_NoneTrueFalse(self, arg):
 
175
        if arg is False:
 
176
            return 'False'
 
177
        if arg:
 
178
            return 'True'
 
179
        return ''
 
180
 
 
181
    def _serialize_NoneString(self, arg):
 
182
        return arg or ''
 
183
 
 
184
    def initialize_on_transport_ex(self, transport, use_existing_dir=False,
 
185
        create_prefix=False, force_new_repo=False, stacked_on=None,
 
186
        stack_on_pwd=None, repo_format_name=None, make_working_trees=None,
 
187
        shared_repo=False):
 
188
        try:
 
189
            # hand off the request to the smart server
 
190
            client_medium = transport.get_smart_medium()
 
191
        except errors.NoSmartMedium:
 
192
            do_vfs = True
 
193
        else:
 
194
            # Decline to open it if the server doesn't support our required
 
195
            # version (3) so that the VFS-based transport will do it.
 
196
            if client_medium.should_probe():
 
197
                try:
 
198
                    server_version = client_medium.protocol_version()
 
199
                    if server_version != '2':
 
200
                        do_vfs = True
 
201
                    else:
 
202
                        do_vfs = False
 
203
                except errors.SmartProtocolError:
 
204
                    # Apparently there's no usable smart server there, even though
 
205
                    # the medium supports the smart protocol.
 
206
                    do_vfs = True
 
207
            else:
 
208
                do_vfs = False
 
209
        if not do_vfs:
 
210
            client = _SmartClient(client_medium)
 
211
            path = client.remote_path_from_transport(transport)
 
212
            if client_medium._is_remote_before((1, 16)):
 
213
                do_vfs = True
 
214
        if do_vfs:
 
215
            # TODO: lookup the local format from a server hint.
 
216
            local_dir_format = _mod_bzrdir.BzrDirMetaFormat1()
 
217
            self._supply_sub_formats_to(local_dir_format)
 
218
            return local_dir_format.initialize_on_transport_ex(transport,
 
219
                use_existing_dir=use_existing_dir, create_prefix=create_prefix,
 
220
                force_new_repo=force_new_repo, stacked_on=stacked_on,
 
221
                stack_on_pwd=stack_on_pwd, repo_format_name=repo_format_name,
 
222
                make_working_trees=make_working_trees, shared_repo=shared_repo,
 
223
                vfs_only=True)
 
224
        return self._initialize_on_transport_ex_rpc(client, path, transport,
 
225
            use_existing_dir, create_prefix, force_new_repo, stacked_on,
 
226
            stack_on_pwd, repo_format_name, make_working_trees, shared_repo)
 
227
 
 
228
    def _initialize_on_transport_ex_rpc(self, client, path, transport,
 
229
        use_existing_dir, create_prefix, force_new_repo, stacked_on,
 
230
        stack_on_pwd, repo_format_name, make_working_trees, shared_repo):
 
231
        args = []
 
232
        args.append(self._serialize_NoneTrueFalse(use_existing_dir))
 
233
        args.append(self._serialize_NoneTrueFalse(create_prefix))
 
234
        args.append(self._serialize_NoneTrueFalse(force_new_repo))
 
235
        args.append(self._serialize_NoneString(stacked_on))
 
236
        # stack_on_pwd is often/usually our transport
 
237
        if stack_on_pwd:
 
238
            try:
 
239
                stack_on_pwd = transport.relpath(stack_on_pwd)
 
240
                if not stack_on_pwd:
 
241
                    stack_on_pwd = '.'
 
242
            except errors.PathNotChild:
 
243
                pass
 
244
        args.append(self._serialize_NoneString(stack_on_pwd))
 
245
        args.append(self._serialize_NoneString(repo_format_name))
 
246
        args.append(self._serialize_NoneTrueFalse(make_working_trees))
 
247
        args.append(self._serialize_NoneTrueFalse(shared_repo))
 
248
        request_network_name = self._network_name or \
 
249
            _mod_bzrdir.BzrDirFormat.get_default_format().network_name()
 
250
        try:
 
251
            response = client.call('BzrDirFormat.initialize_ex_1.16',
 
252
                request_network_name, path, *args)
 
253
        except errors.UnknownSmartMethod:
 
254
            client._medium._remember_remote_is_before((1,16))
 
255
            local_dir_format = _mod_bzrdir.BzrDirMetaFormat1()
 
256
            self._supply_sub_formats_to(local_dir_format)
 
257
            return local_dir_format.initialize_on_transport_ex(transport,
 
258
                use_existing_dir=use_existing_dir, create_prefix=create_prefix,
 
259
                force_new_repo=force_new_repo, stacked_on=stacked_on,
 
260
                stack_on_pwd=stack_on_pwd, repo_format_name=repo_format_name,
 
261
                make_working_trees=make_working_trees, shared_repo=shared_repo,
 
262
                vfs_only=True)
 
263
        except errors.ErrorFromSmartServer, err:
 
264
            _translate_error(err, path=path)
 
265
        repo_path = response[0]
 
266
        bzrdir_name = response[6]
 
267
        require_stacking = response[7]
 
268
        require_stacking = self.parse_NoneTrueFalse(require_stacking)
 
269
        format = RemoteBzrDirFormat()
 
270
        format._network_name = bzrdir_name
 
271
        self._supply_sub_formats_to(format)
 
272
        bzrdir = RemoteBzrDir(transport, format, _client=client)
 
273
        if repo_path:
 
274
            repo_format = response_tuple_to_repo_format(response[1:])
 
275
            if repo_path == '.':
 
276
                repo_path = ''
 
277
            if repo_path:
 
278
                repo_bzrdir_format = RemoteBzrDirFormat()
 
279
                repo_bzrdir_format._network_name = response[5]
 
280
                repo_bzr = RemoteBzrDir(transport.clone(repo_path),
 
281
                    repo_bzrdir_format)
 
282
            else:
 
283
                repo_bzr = bzrdir
 
284
            final_stack = response[8] or None
 
285
            final_stack_pwd = response[9] or None
 
286
            if final_stack_pwd:
 
287
                final_stack_pwd = urlutils.join(
 
288
                    transport.base, final_stack_pwd)
 
289
            remote_repo = RemoteRepository(repo_bzr, repo_format)
 
290
            if len(response) > 10:
 
291
                # Updated server verb that locks remotely.
 
292
                repo_lock_token = response[10] or None
 
293
                remote_repo.lock_write(repo_lock_token, _skip_rpc=True)
 
294
                if repo_lock_token:
 
295
                    remote_repo.dont_leave_lock_in_place()
 
296
            else:
 
297
                remote_repo.lock_write()
 
298
            policy = _mod_bzrdir.UseExistingRepository(remote_repo, final_stack,
 
299
                final_stack_pwd, require_stacking)
 
300
            policy.acquire_repository()
 
301
        else:
 
302
            remote_repo = None
 
303
            policy = None
 
304
        bzrdir._format.set_branch_format(self.get_branch_format())
 
305
        if require_stacking:
 
306
            # The repo has already been created, but we need to make sure that
 
307
            # we'll make a stackable branch.
 
308
            bzrdir._format.require_stacking(_skip_repo=True)
 
309
        return remote_repo, bzrdir, require_stacking, policy
 
310
 
 
311
    def _open(self, transport):
 
312
        return RemoteBzrDir(transport, self)
 
313
 
 
314
    def __eq__(self, other):
 
315
        if not isinstance(other, RemoteBzrDirFormat):
 
316
            return False
 
317
        return self.get_format_description() == other.get_format_description()
 
318
 
 
319
    def __return_repository_format(self):
 
320
        # Always return a RemoteRepositoryFormat object, but if a specific bzr
 
321
        # repository format has been asked for, tell the RemoteRepositoryFormat
 
322
        # that it should use that for init() etc.
 
323
        result = RemoteRepositoryFormat()
 
324
        custom_format = getattr(self, '_repository_format', None)
 
325
        if custom_format:
 
326
            if isinstance(custom_format, RemoteRepositoryFormat):
 
327
                return custom_format
 
328
            else:
 
329
                # We will use the custom format to create repositories over the
 
330
                # wire; expose its details like rich_root_data for code to
 
331
                # query
 
332
                result._custom_format = custom_format
 
333
        return result
 
334
 
 
335
    def get_branch_format(self):
 
336
        result = _mod_bzrdir.BzrDirMetaFormat1.get_branch_format(self)
 
337
        if not isinstance(result, RemoteBranchFormat):
 
338
            new_result = RemoteBranchFormat()
 
339
            new_result._custom_format = result
 
340
            # cache the result
 
341
            self.set_branch_format(new_result)
 
342
            result = new_result
 
343
        return result
 
344
 
 
345
    repository_format = property(__return_repository_format,
 
346
        _mod_bzrdir.BzrDirMetaFormat1._set_repository_format) #.im_func)
 
347
 
 
348
 
 
349
class RemoteControlStore(config.IniFileStore):
 
350
    """Control store which attempts to use HPSS calls to retrieve control store.
 
351
 
 
352
    Note that this is specific to bzr-based formats.
 
353
    """
 
354
 
 
355
    def __init__(self, bzrdir):
 
356
        super(RemoteControlStore, self).__init__()
 
357
        self.bzrdir = bzrdir
 
358
        self._real_store = None
 
359
 
 
360
    def lock_write(self, token=None):
 
361
        self._ensure_real()
 
362
        return self._real_store.lock_write(token)
 
363
 
 
364
    def unlock(self):
 
365
        self._ensure_real()
 
366
        return self._real_store.unlock()
 
367
 
 
368
    @needs_write_lock
 
369
    def save(self):
 
370
        # We need to be able to override the undecorated implementation
 
371
        self.save_without_locking()
 
372
 
 
373
    def save_without_locking(self):
 
374
        super(RemoteControlStore, self).save()
 
375
 
 
376
    def _ensure_real(self):
 
377
        self.bzrdir._ensure_real()
 
378
        if self._real_store is None:
 
379
            self._real_store = config.ControlStore(self.bzrdir)
 
380
 
 
381
    def external_url(self):
 
382
        return self.bzrdir.user_url
 
383
 
 
384
    def _load_content(self):
 
385
        medium = self.bzrdir._client._medium
 
386
        path = self.bzrdir._path_for_remote_call(self.bzrdir._client)
 
387
        try:
 
388
            response, handler = self.bzrdir._call_expecting_body(
 
389
                'BzrDir.get_config_file', path)
 
390
        except errors.UnknownSmartMethod:
 
391
            self._ensure_real()
 
392
            return self._real_store._load_content()
 
393
        if len(response) and response[0] != 'ok':
 
394
            raise errors.UnexpectedSmartServerResponse(response)
 
395
        return handler.read_body_bytes()
 
396
 
 
397
    def _save_content(self, content):
 
398
        # FIXME JRV 2011-11-22: Ideally this should use a
 
399
        # HPSS call too, but at the moment it is not possible
 
400
        # to write lock control directories.
 
401
        self._ensure_real()
 
402
        return self._real_store._save_content(content)
 
403
 
 
404
 
 
405
class RemoteBzrDir(_mod_bzrdir.BzrDir, _RpcHelper):
94
406
    """Control directory on a remote server, accessed via bzr:// or similar."""
95
407
 
96
408
    def __init__(self, transport, format, _client=None, _force_probe=False):
99
411
        :param _client: Private parameter for testing. Disables probing and the
100
412
            use of a real bzrdir.
101
413
        """
102
 
        BzrDir.__init__(self, transport, format)
 
414
        _mod_bzrdir.BzrDir.__init__(self, transport, format)
103
415
        # this object holds a delegated bzrdir that uses file-level operations
104
416
        # to talk to the other side
105
417
        self._real_bzrdir = None
165
477
                import traceback
166
478
                warning('VFS BzrDir access triggered\n%s',
167
479
                    ''.join(traceback.format_stack()))
168
 
            self._real_bzrdir = BzrDir.open_from_transport(
 
480
            self._real_bzrdir = _mod_bzrdir.BzrDir.open_from_transport(
169
481
                self.root_transport, _server_formats=False)
170
482
            self._format._network_name = \
171
483
                self._real_bzrdir._format.network_name()
177
489
        # Prevent aliasing problems in the next_open_branch_result cache.
178
490
        # See create_branch for rationale.
179
491
        self._next_open_branch_result = None
180
 
        return BzrDir.break_lock(self)
 
492
        return _mod_bzrdir.BzrDir.break_lock(self)
 
493
 
 
494
    def _vfs_checkout_metadir(self):
 
495
        self._ensure_real()
 
496
        return self._real_bzrdir.checkout_metadir()
 
497
 
 
498
    def checkout_metadir(self):
 
499
        """Retrieve the controldir format to use for checkouts of this one.
 
500
        """
 
501
        medium = self._client._medium
 
502
        if medium._is_remote_before((2, 5)):
 
503
            return self._vfs_checkout_metadir()
 
504
        path = self._path_for_remote_call(self._client)
 
505
        try:
 
506
            response = self._client.call('BzrDir.checkout_metadir',
 
507
                path)
 
508
        except errors.UnknownSmartMethod:
 
509
            medium._remember_remote_is_before((2, 5))
 
510
            return self._vfs_checkout_metadir()
 
511
        if len(response) != 3:
 
512
            raise errors.UnexpectedSmartServerResponse(response)
 
513
        control_name, repo_name, branch_name = response
 
514
        try:
 
515
            format = controldir.network_format_registry.get(control_name)
 
516
        except KeyError:
 
517
            raise errors.UnknownFormatError(kind='control',
 
518
                format=control_name)
 
519
        if repo_name:
 
520
            try:
 
521
                repo_format = _mod_repository.network_format_registry.get(
 
522
                    repo_name)
 
523
            except KeyError:
 
524
                raise errors.UnknownFormatError(kind='repository',
 
525
                    format=repo_name)
 
526
            format.repository_format = repo_format
 
527
        if branch_name:
 
528
            try:
 
529
                format.set_branch_format(
 
530
                    branch.network_format_registry.get(branch_name))
 
531
            except KeyError:
 
532
                raise errors.UnknownFormatError(kind='branch',
 
533
                    format=branch_name)
 
534
        return format
181
535
 
182
536
    def _vfs_cloning_metadir(self, require_stacking=False):
183
537
        self._ensure_real()
214
568
        if len(branch_info) != 2:
215
569
            raise errors.UnexpectedSmartServerResponse(response)
216
570
        branch_ref, branch_name = branch_info
217
 
        format = controldir.network_format_registry.get(control_name)
 
571
        try:
 
572
            format = controldir.network_format_registry.get(control_name)
 
573
        except KeyError:
 
574
            raise errors.UnknownFormatError(kind='control', format=control_name)
 
575
 
218
576
        if repo_name:
219
 
            format.repository_format = repository.network_format_registry.get(
220
 
                repo_name)
 
577
            try:
 
578
                format.repository_format = _mod_repository.network_format_registry.get(
 
579
                    repo_name)
 
580
            except KeyError:
 
581
                raise errors.UnknownFormatError(kind='repository',
 
582
                    format=repo_name)
221
583
        if branch_ref == 'ref':
222
584
            # XXX: we need possible_transports here to avoid reopening the
223
585
            # connection to the referenced location
224
 
            ref_bzrdir = BzrDir.open(branch_name)
 
586
            ref_bzrdir = _mod_bzrdir.BzrDir.open(branch_name)
225
587
            branch_format = ref_bzrdir.cloning_metadir().get_branch_format()
226
588
            format.set_branch_format(branch_format)
227
589
        elif branch_ref == 'branch':
228
590
            if branch_name:
229
 
                format.set_branch_format(
230
 
                    branch.network_format_registry.get(branch_name))
 
591
                try:
 
592
                    branch_format = branch.network_format_registry.get(
 
593
                        branch_name)
 
594
                except KeyError:
 
595
                    raise errors.UnknownFormatError(kind='branch',
 
596
                        format=branch_name)
 
597
                format.set_branch_format(branch_format)
231
598
        else:
232
599
            raise errors.UnexpectedSmartServerResponse(response)
233
600
        return format
243
610
 
244
611
    def destroy_repository(self):
245
612
        """See BzrDir.destroy_repository"""
246
 
        self._ensure_real()
247
 
        self._real_bzrdir.destroy_repository()
 
613
        path = self._path_for_remote_call(self._client)
 
614
        try:
 
615
            response = self._call('BzrDir.destroy_repository', path)
 
616
        except errors.UnknownSmartMethod:
 
617
            self._ensure_real()
 
618
            self._real_bzrdir.destroy_repository()
 
619
            return
 
620
        if response[0] != 'ok':
 
621
            raise SmartProtocolError('unexpected response code %s' % (response,))
248
622
 
249
 
    def create_branch(self, name=None):
 
623
    def create_branch(self, name=None, repository=None,
 
624
                      append_revisions_only=None):
250
625
        # as per meta1 formats - just delegate to the format object which may
251
626
        # be parameterised.
252
627
        real_branch = self._format.get_branch_format().initialize(self,
253
 
            name=name)
 
628
            name=name, repository=repository,
 
629
            append_revisions_only=append_revisions_only)
254
630
        if not isinstance(real_branch, RemoteBranch):
255
 
            result = RemoteBranch(self, self.find_repository(), real_branch,
256
 
                                  name=name)
 
631
            if not isinstance(repository, RemoteRepository):
 
632
                raise AssertionError(
 
633
                    'need a RemoteRepository to use with RemoteBranch, got %r'
 
634
                    % (repository,))
 
635
            result = RemoteBranch(self, repository, real_branch, name=name)
257
636
        else:
258
637
            result = real_branch
259
638
        # BzrDir.clone_on_transport() uses the result of create_branch but does
267
646
 
268
647
    def destroy_branch(self, name=None):
269
648
        """See BzrDir.destroy_branch"""
270
 
        self._ensure_real()
271
 
        self._real_bzrdir.destroy_branch(name=name)
 
649
        path = self._path_for_remote_call(self._client)
 
650
        try:
 
651
            if name is not None:
 
652
                args = (name, )
 
653
            else:
 
654
                args = ()
 
655
            response = self._call('BzrDir.destroy_branch', path, *args)
 
656
        except errors.UnknownSmartMethod:
 
657
            self._ensure_real()
 
658
            self._real_bzrdir.destroy_branch(name=name)
 
659
            self._next_open_branch_result = None
 
660
            return
272
661
        self._next_open_branch_result = None
 
662
        if response[0] != 'ok':
 
663
            raise SmartProtocolError('unexpected response code %s' % (response,))
273
664
 
274
665
    def create_workingtree(self, revision_id=None, from_branch=None,
275
666
        accelerator_tree=None, hardlink=False):
329
720
        return None, self.open_branch(name=name)
330
721
 
331
722
    def open_branch(self, name=None, unsupported=False,
332
 
                    ignore_fallbacks=False):
 
723
                    ignore_fallbacks=False, possible_transports=None):
333
724
        if unsupported:
334
725
            raise NotImplementedError('unsupported flag support not implemented yet.')
335
726
        if self._next_open_branch_result is not None:
342
733
            # a branch reference, use the existing BranchReference logic.
343
734
            format = BranchReferenceFormat()
344
735
            return format.open(self, name=name, _found=True,
345
 
                location=response[1], ignore_fallbacks=ignore_fallbacks)
 
736
                location=response[1], ignore_fallbacks=ignore_fallbacks,
 
737
                possible_transports=possible_transports)
346
738
        branch_format_name = response[1]
347
739
        if not branch_format_name:
348
740
            branch_format_name = None
349
741
        format = RemoteBranchFormat(network_name=branch_format_name)
350
742
        return RemoteBranch(self, self.find_repository(), format=format,
351
 
            setup_stacking=not ignore_fallbacks, name=name)
 
743
            setup_stacking=not ignore_fallbacks, name=name,
 
744
            possible_transports=possible_transports)
352
745
 
353
746
    def _open_repo_v1(self, path):
354
747
        verb = 'BzrDir.find_repository'
417
810
 
418
811
    def has_workingtree(self):
419
812
        if self._has_working_tree is None:
420
 
            self._ensure_real()
421
 
            self._has_working_tree = self._real_bzrdir.has_workingtree()
 
813
            path = self._path_for_remote_call(self._client)
 
814
            try:
 
815
                response = self._call('BzrDir.has_workingtree', path)
 
816
            except errors.UnknownSmartMethod:
 
817
                self._ensure_real()
 
818
                self._has_working_tree = self._real_bzrdir.has_workingtree()
 
819
            else:
 
820
                if response[0] not in ('yes', 'no'):
 
821
                    raise SmartProtocolError('unexpected response code %s' % (response,))
 
822
                self._has_working_tree = (response[0] == 'yes')
422
823
        return self._has_working_tree
423
824
 
424
825
    def open_workingtree(self, recommend_upgrade=True):
429
830
 
430
831
    def _path_for_remote_call(self, client):
431
832
        """Return the path to be used for this bzrdir in a remote call."""
432
 
        return client.remote_path_from_transport(self.root_transport)
 
833
        return urlutils.split_segment_parameters_raw(
 
834
            client.remote_path_from_transport(self.root_transport))[0]
433
835
 
434
836
    def get_branch_transport(self, branch_format, name=None):
435
837
        self._ensure_real()
447
849
        """Upgrading of remote bzrdirs is not supported yet."""
448
850
        return False
449
851
 
450
 
    def needs_format_conversion(self, format=None):
 
852
    def needs_format_conversion(self, format):
451
853
        """Upgrading of remote bzrdirs is not supported yet."""
452
 
        if format is None:
453
 
            symbol_versioning.warn(symbol_versioning.deprecated_in((1, 13, 0))
454
 
                % 'needs_format_conversion(format=None)')
455
854
        return False
456
855
 
457
 
    def clone(self, url, revision_id=None, force_new_repo=False,
458
 
              preserve_stacking=False):
459
 
        self._ensure_real()
460
 
        return self._real_bzrdir.clone(url, revision_id=revision_id,
461
 
            force_new_repo=force_new_repo, preserve_stacking=preserve_stacking)
462
 
 
463
856
    def _get_config(self):
464
857
        return RemoteBzrDirConfig(self)
465
858
 
466
 
 
467
 
class RemoteRepositoryFormat(repository.RepositoryFormat):
 
859
    def _get_config_store(self):
 
860
        return RemoteControlStore(self)
 
861
 
 
862
 
 
863
class RemoteRepositoryFormat(vf_repository.VersionedFileRepositoryFormat):
468
864
    """Format for repositories accessed over a _SmartClient.
469
865
 
470
866
    Instances of this repository are represented by RemoteRepository
485
881
    """
486
882
 
487
883
    _matchingbzrdir = RemoteBzrDirFormat()
 
884
    supports_full_versioned_files = True
 
885
    supports_leaving_lock = True
488
886
 
489
887
    def __init__(self):
490
 
        repository.RepositoryFormat.__init__(self)
 
888
        _mod_repository.RepositoryFormat.__init__(self)
491
889
        self._custom_format = None
492
890
        self._network_name = None
493
891
        self._creating_bzrdir = None
 
892
        self._revision_graph_can_have_wrong_parents = None
494
893
        self._supports_chks = None
495
894
        self._supports_external_lookups = None
496
895
        self._supports_tree_reference = None
 
896
        self._supports_funky_characters = None
 
897
        self._supports_nesting_repositories = None
497
898
        self._rich_root_data = None
498
899
 
499
900
    def __repr__(self):
528
929
        return self._supports_external_lookups
529
930
 
530
931
    @property
 
932
    def supports_funky_characters(self):
 
933
        if self._supports_funky_characters is None:
 
934
            self._ensure_real()
 
935
            self._supports_funky_characters = \
 
936
                self._custom_format.supports_funky_characters
 
937
        return self._supports_funky_characters
 
938
 
 
939
    @property
 
940
    def supports_nesting_repositories(self):
 
941
        if self._supports_nesting_repositories is None:
 
942
            self._ensure_real()
 
943
            self._supports_nesting_repositories = \
 
944
                self._custom_format.supports_nesting_repositories
 
945
        return self._supports_nesting_repositories
 
946
 
 
947
    @property
531
948
    def supports_tree_reference(self):
532
949
        if self._supports_tree_reference is None:
533
950
            self._ensure_real()
535
952
                self._custom_format.supports_tree_reference
536
953
        return self._supports_tree_reference
537
954
 
 
955
    @property
 
956
    def revision_graph_can_have_wrong_parents(self):
 
957
        if self._revision_graph_can_have_wrong_parents is None:
 
958
            self._ensure_real()
 
959
            self._revision_graph_can_have_wrong_parents = \
 
960
                self._custom_format.revision_graph_can_have_wrong_parents
 
961
        return self._revision_graph_can_have_wrong_parents
 
962
 
538
963
    def _vfs_initialize(self, a_bzrdir, shared):
539
964
        """Helper for common code in initialize."""
540
965
        if self._custom_format:
575
1000
            network_name = self._network_name
576
1001
        else:
577
1002
            # Select the current bzrlib default and ask for that.
578
 
            reference_bzrdir_format = bzrdir.format_registry.get('default')()
 
1003
            reference_bzrdir_format = _mod_bzrdir.format_registry.get('default')()
579
1004
            reference_format = reference_bzrdir_format.repository_format
580
1005
            network_name = reference_format.network_name()
581
1006
        # 2) try direct creation via RPC
607
1032
 
608
1033
    def _ensure_real(self):
609
1034
        if self._custom_format is None:
610
 
            self._custom_format = repository.network_format_registry.get(
611
 
                self._network_name)
 
1035
            try:
 
1036
                self._custom_format = _mod_repository.network_format_registry.get(
 
1037
                    self._network_name)
 
1038
            except KeyError:
 
1039
                raise errors.UnknownFormatError(kind='repository',
 
1040
                    format=self._network_name)
612
1041
 
613
1042
    @property
614
1043
    def _fetch_order(self):
649
1078
        return self._custom_format._serializer
650
1079
 
651
1080
 
652
 
class RemoteRepository(_RpcHelper, lock._RelockDebugMixin,
653
 
    controldir.ControlComponent):
 
1081
class RemoteRepository(_mod_repository.Repository, _RpcHelper,
 
1082
        lock._RelockDebugMixin):
654
1083
    """Repository accessed over rpc.
655
1084
 
656
1085
    For the moment most operations are performed using local transport-backed
680
1109
        self._format = format
681
1110
        self._lock_mode = None
682
1111
        self._lock_token = None
 
1112
        self._write_group_tokens = None
683
1113
        self._lock_count = 0
684
1114
        self._leave_lock = False
685
1115
        # Cache of revision parents; misses are cached during read locks, and
709
1139
        # transport, but I'm not sure it's worth making this method
710
1140
        # optional -- mbp 2010-04-21
711
1141
        return self.bzrdir.get_repository_transport(None)
712
 
        
 
1142
 
713
1143
    def __str__(self):
714
1144
        return "%s(%s)" % (self.__class__.__name__, self.base)
715
1145
 
725
1155
 
726
1156
        :param suppress_errors: see Repository.abort_write_group.
727
1157
        """
728
 
        self._ensure_real()
729
 
        return self._real_repository.abort_write_group(
730
 
            suppress_errors=suppress_errors)
 
1158
        if self._real_repository:
 
1159
            self._ensure_real()
 
1160
            return self._real_repository.abort_write_group(
 
1161
                suppress_errors=suppress_errors)
 
1162
        if not self.is_in_write_group():
 
1163
            if suppress_errors:
 
1164
                mutter('(suppressed) not in write group')
 
1165
                return
 
1166
            raise errors.BzrError("not in write group")
 
1167
        path = self.bzrdir._path_for_remote_call(self._client)
 
1168
        try:
 
1169
            response = self._call('Repository.abort_write_group', path,
 
1170
                self._lock_token, self._write_group_tokens)
 
1171
        except Exception, exc:
 
1172
            self._write_group = None
 
1173
            if not suppress_errors:
 
1174
                raise
 
1175
            mutter('abort_write_group failed')
 
1176
            log_exception_quietly()
 
1177
            note(gettext('bzr: ERROR (ignored): %s'), exc)
 
1178
        else:
 
1179
            if response != ('ok', ):
 
1180
                raise errors.UnexpectedSmartServerResponse(response)
 
1181
            self._write_group_tokens = None
731
1182
 
732
1183
    @property
733
1184
    def chk_bytes(self):
747
1198
        for older plugins that don't use e.g. the CommitBuilder
748
1199
        facility.
749
1200
        """
750
 
        self._ensure_real()
751
 
        return self._real_repository.commit_write_group()
 
1201
        if self._real_repository:
 
1202
            self._ensure_real()
 
1203
            return self._real_repository.commit_write_group()
 
1204
        if not self.is_in_write_group():
 
1205
            raise errors.BzrError("not in write group")
 
1206
        path = self.bzrdir._path_for_remote_call(self._client)
 
1207
        response = self._call('Repository.commit_write_group', path,
 
1208
            self._lock_token, self._write_group_tokens)
 
1209
        if response != ('ok', ):
 
1210
            raise errors.UnexpectedSmartServerResponse(response)
 
1211
        self._write_group_tokens = None
752
1212
 
753
1213
    def resume_write_group(self, tokens):
754
 
        self._ensure_real()
755
 
        return self._real_repository.resume_write_group(tokens)
 
1214
        if self._real_repository:
 
1215
            return self._real_repository.resume_write_group(tokens)
 
1216
        path = self.bzrdir._path_for_remote_call(self._client)
 
1217
        try:
 
1218
            response = self._call('Repository.check_write_group', path,
 
1219
               self._lock_token, tokens)
 
1220
        except errors.UnknownSmartMethod:
 
1221
            self._ensure_real()
 
1222
            return self._real_repository.resume_write_group(tokens)
 
1223
        if response != ('ok', ):
 
1224
            raise errors.UnexpectedSmartServerResponse(response)
 
1225
        self._write_group_tokens = tokens
756
1226
 
757
1227
    def suspend_write_group(self):
758
 
        self._ensure_real()
759
 
        return self._real_repository.suspend_write_group()
 
1228
        if self._real_repository:
 
1229
            return self._real_repository.suspend_write_group()
 
1230
        ret = self._write_group_tokens or []
 
1231
        self._write_group_tokens = None
 
1232
        return ret
760
1233
 
761
1234
    def get_missing_parent_inventories(self, check_for_missing_texts=True):
762
1235
        self._ensure_real()
823
1296
    def find_text_key_references(self):
824
1297
        """Find the text key references within the repository.
825
1298
 
826
 
        :return: a dictionary mapping (file_id, revision_id) tuples to altered file-ids to an iterable of
827
 
        revision_ids. Each altered file-ids has the exact revision_ids that
828
 
        altered it listed explicitly.
829
1299
        :return: A dictionary mapping text keys ((fileid, revision_id) tuples)
830
1300
            to whether they were referred to by the inventory of the
831
1301
            revision_id that they contain. The inventory texts from all present
849
1319
        """Private method for using with old (< 1.2) servers to fallback."""
850
1320
        if revision_id is None:
851
1321
            revision_id = ''
852
 
        elif revision.is_null(revision_id):
 
1322
        elif _mod_revision.is_null(revision_id):
853
1323
            return {}
854
1324
 
855
1325
        path = self.bzrdir._path_for_remote_call(self._client)
879
1349
        return RemoteStreamSource(self, to_format)
880
1350
 
881
1351
    @needs_read_lock
 
1352
    def get_file_graph(self):
 
1353
        return graph.Graph(self.texts)
 
1354
 
 
1355
    @needs_read_lock
882
1356
    def has_revision(self, revision_id):
883
1357
        """True if this repository has a copy of the revision."""
884
1358
        # Copy of bzrlib.repository.Repository.has_revision
936
1410
        """See Repository.gather_stats()."""
937
1411
        path = self.bzrdir._path_for_remote_call(self._client)
938
1412
        # revid can be None to indicate no revisions, not just NULL_REVISION
939
 
        if revid is None or revision.is_null(revid):
 
1413
        if revid is None or _mod_revision.is_null(revid):
940
1414
            fmt_revid = ''
941
1415
        else:
942
1416
            fmt_revid = revid
971
1445
 
972
1446
    def get_physical_lock_status(self):
973
1447
        """See Repository.get_physical_lock_status()."""
974
 
        # should be an API call to the server.
975
 
        self._ensure_real()
976
 
        return self._real_repository.get_physical_lock_status()
 
1448
        path = self.bzrdir._path_for_remote_call(self._client)
 
1449
        try:
 
1450
            response = self._call('Repository.get_physical_lock_status', path)
 
1451
        except errors.UnknownSmartMethod:
 
1452
            self._ensure_real()
 
1453
            return self._real_repository.get_physical_lock_status()
 
1454
        if response[0] not in ('yes', 'no'):
 
1455
            raise errors.UnexpectedSmartServerResponse(response)
 
1456
        return (response[0] == 'yes')
977
1457
 
978
1458
    def is_in_write_group(self):
979
1459
        """Return True if there is an open write group.
980
1460
 
981
1461
        write groups are only applicable locally for the smart server..
982
1462
        """
 
1463
        if self._write_group_tokens is not None:
 
1464
            return True
983
1465
        if self._real_repository:
984
1466
            return self._real_repository.is_in_write_group()
985
1467
 
1120
1602
            self._real_repository.lock_write(self._lock_token)
1121
1603
        elif self._lock_mode == 'r':
1122
1604
            self._real_repository.lock_read()
 
1605
        if self._write_group_tokens is not None:
 
1606
            # if we are already in a write group, resume it
 
1607
            self._real_repository.resume_write_group(self._write_group_tokens)
 
1608
            self._write_group_tokens = None
1123
1609
 
1124
1610
    def start_write_group(self):
1125
1611
        """Start a write group on the decorated repository.
1129
1615
        for older plugins that don't use e.g. the CommitBuilder
1130
1616
        facility.
1131
1617
        """
1132
 
        self._ensure_real()
1133
 
        return self._real_repository.start_write_group()
 
1618
        if self._real_repository:
 
1619
            self._ensure_real()
 
1620
            return self._real_repository.start_write_group()
 
1621
        if not self.is_write_locked():
 
1622
            raise errors.NotWriteLocked(self)
 
1623
        if self._write_group_tokens is not None:
 
1624
            raise errors.BzrError('already in a write group')
 
1625
        path = self.bzrdir._path_for_remote_call(self._client)
 
1626
        try:
 
1627
            response = self._call('Repository.start_write_group', path,
 
1628
                self._lock_token)
 
1629
        except (errors.UnknownSmartMethod, errors.UnsuspendableWriteGroup):
 
1630
            self._ensure_real()
 
1631
            return self._real_repository.start_write_group()
 
1632
        if response[0] != 'ok':
 
1633
            raise errors.UnexpectedSmartServerResponse(response)
 
1634
        self._write_group_tokens = response[1]
1134
1635
 
1135
1636
    def _unlock(self, token):
1136
1637
        path = self.bzrdir._path_for_remote_call(self._client)
1163
1664
            # This is just to let the _real_repository stay up to date.
1164
1665
            if self._real_repository is not None:
1165
1666
                self._real_repository.unlock()
 
1667
            elif self._write_group_tokens is not None:
 
1668
                self.abort_write_group()
1166
1669
        finally:
1167
1670
            # The rpc-level lock should be released even if there was a
1168
1671
            # problem releasing the vfs-based lock.
1180
1683
 
1181
1684
    def break_lock(self):
1182
1685
        # should hand off to the network
1183
 
        self._ensure_real()
1184
 
        return self._real_repository.break_lock()
 
1686
        path = self.bzrdir._path_for_remote_call(self._client)
 
1687
        try:
 
1688
            response = self._call("Repository.break_lock", path)
 
1689
        except errors.UnknownSmartMethod:
 
1690
            self._ensure_real()
 
1691
            return self._real_repository.break_lock()
 
1692
        if response != ('ok',):
 
1693
            raise errors.UnexpectedSmartServerResponse(response)
1185
1694
 
1186
1695
    def _get_tarball(self, compression):
1187
1696
        """Return a TemporaryFile containing a repository tarball.
1205
1714
            return t
1206
1715
        raise errors.UnexpectedSmartServerResponse(response)
1207
1716
 
 
1717
    @needs_read_lock
1208
1718
    def sprout(self, to_bzrdir, revision_id=None):
1209
 
        # TODO: Option to control what format is created?
1210
 
        self._ensure_real()
1211
 
        dest_repo = self._real_repository._format.initialize(to_bzrdir,
1212
 
                                                             shared=False)
 
1719
        """Create a descendent repository for new development.
 
1720
 
 
1721
        Unlike clone, this does not copy the settings of the repository.
 
1722
        """
 
1723
        dest_repo = self._create_sprouting_repo(to_bzrdir, shared=False)
1213
1724
        dest_repo.fetch(self, revision_id=revision_id)
1214
1725
        return dest_repo
1215
1726
 
 
1727
    def _create_sprouting_repo(self, a_bzrdir, shared):
 
1728
        if not isinstance(a_bzrdir._format, self.bzrdir._format.__class__):
 
1729
            # use target default format.
 
1730
            dest_repo = a_bzrdir.create_repository()
 
1731
        else:
 
1732
            # Most control formats need the repository to be specifically
 
1733
            # created, but on some old all-in-one formats it's not needed
 
1734
            try:
 
1735
                dest_repo = self._format.initialize(a_bzrdir, shared=shared)
 
1736
            except errors.UninitializableFormat:
 
1737
                dest_repo = a_bzrdir.open_repository()
 
1738
        return dest_repo
 
1739
 
1216
1740
    ### These methods are just thin shims to the VFS object for now.
1217
1741
 
 
1742
    @needs_read_lock
1218
1743
    def revision_tree(self, revision_id):
1219
 
        self._ensure_real()
1220
 
        return self._real_repository.revision_tree(revision_id)
 
1744
        revision_id = _mod_revision.ensure_null(revision_id)
 
1745
        if revision_id == _mod_revision.NULL_REVISION:
 
1746
            return InventoryRevisionTree(self,
 
1747
                Inventory(root_id=None), _mod_revision.NULL_REVISION)
 
1748
        else:
 
1749
            return list(self.revision_trees([revision_id]))[0]
1221
1750
 
1222
1751
    def get_serializer_format(self):
1223
 
        self._ensure_real()
1224
 
        return self._real_repository.get_serializer_format()
 
1752
        path = self.bzrdir._path_for_remote_call(self._client)
 
1753
        try:
 
1754
            response = self._call('VersionedFileRepository.get_serializer_format',
 
1755
                path)
 
1756
        except errors.UnknownSmartMethod:
 
1757
            self._ensure_real()
 
1758
            return self._real_repository.get_serializer_format()
 
1759
        if response[0] != 'ok':
 
1760
            raise errors.UnexpectedSmartServerResponse(response)
 
1761
        return response[1]
1225
1762
 
1226
1763
    def get_commit_builder(self, branch, parents, config, timestamp=None,
1227
1764
                           timezone=None, committer=None, revprops=None,
1228
 
                           revision_id=None):
 
1765
                           revision_id=None, lossy=False):
1229
1766
        # FIXME: It ought to be possible to call this without immediately
1230
1767
        # triggering _ensure_real.  For now it's the easiest thing to do.
1231
1768
        self._ensure_real()
1232
1769
        real_repo = self._real_repository
1233
1770
        builder = real_repo.get_commit_builder(branch, parents,
1234
1771
                config, timestamp=timestamp, timezone=timezone,
1235
 
                committer=committer, revprops=revprops, revision_id=revision_id)
 
1772
                committer=committer, revprops=revprops,
 
1773
                revision_id=revision_id, lossy=lossy)
1236
1774
        return builder
1237
1775
 
1238
1776
    def add_fallback_repository(self, repository):
1246
1784
        # We need to accumulate additional repositories here, to pass them in
1247
1785
        # on various RPC's.
1248
1786
        #
 
1787
        # Make the check before we lock: this raises an exception.
 
1788
        self._check_fallback_repository(repository)
1249
1789
        if self.is_locked():
1250
1790
            # We will call fallback.unlock() when we transition to the unlocked
1251
1791
            # state, so always add a lock here. If a caller passes us a locked
1252
1792
            # repository, they are responsible for unlocking it later.
1253
1793
            repository.lock_read()
1254
 
        self._check_fallback_repository(repository)
1255
1794
        self._fallback_repositories.append(repository)
1256
1795
        # If self._real_repository was parameterised already (e.g. because a
1257
1796
        # _real_branch had its get_stacked_on_url method called), then the
1290
1829
 
1291
1830
    @needs_read_lock
1292
1831
    def get_inventory(self, revision_id):
 
1832
        return list(self.iter_inventories([revision_id]))[0]
 
1833
 
 
1834
    def _iter_inventories_rpc(self, revision_ids, ordering):
 
1835
        if ordering is None:
 
1836
            ordering = 'unordered'
 
1837
        path = self.bzrdir._path_for_remote_call(self._client)
 
1838
        body = "\n".join(revision_ids)
 
1839
        response_tuple, response_handler = (
 
1840
            self._call_with_body_bytes_expecting_body(
 
1841
                "VersionedFileRepository.get_inventories",
 
1842
                (path, ordering), body))
 
1843
        if response_tuple[0] != "ok":
 
1844
            raise errors.UnexpectedSmartServerResponse(response_tuple)
 
1845
        deserializer = inventory_delta.InventoryDeltaDeserializer()
 
1846
        byte_stream = response_handler.read_streamed_body()
 
1847
        decoded = smart_repo._byte_stream_to_stream(byte_stream)
 
1848
        if decoded is None:
 
1849
            # no results whatsoever
 
1850
            return
 
1851
        src_format, stream = decoded
 
1852
        if src_format.network_name() != self._format.network_name():
 
1853
            raise AssertionError(
 
1854
                "Mismatched RemoteRepository and stream src %r, %r" % (
 
1855
                src_format.network_name(), self._format.network_name()))
 
1856
        # ignore the src format, it's not really relevant
 
1857
        prev_inv = Inventory(root_id=None,
 
1858
            revision_id=_mod_revision.NULL_REVISION)
 
1859
        # there should be just one substream, with inventory deltas
 
1860
        substream_kind, substream = stream.next()
 
1861
        if substream_kind != "inventory-deltas":
 
1862
            raise AssertionError(
 
1863
                 "Unexpected stream %r received" % substream_kind)
 
1864
        for record in substream:
 
1865
            (parent_id, new_id, versioned_root, tree_references, invdelta) = (
 
1866
                deserializer.parse_text_bytes(record.get_bytes_as("fulltext")))
 
1867
            if parent_id != prev_inv.revision_id:
 
1868
                raise AssertionError("invalid base %r != %r" % (parent_id,
 
1869
                    prev_inv.revision_id))
 
1870
            inv = prev_inv.create_by_apply_delta(invdelta, new_id)
 
1871
            yield inv, inv.revision_id
 
1872
            prev_inv = inv
 
1873
 
 
1874
    def _iter_inventories_vfs(self, revision_ids, ordering=None):
1293
1875
        self._ensure_real()
1294
 
        return self._real_repository.get_inventory(revision_id)
 
1876
        return self._real_repository._iter_inventories(revision_ids, ordering)
1295
1877
 
1296
1878
    def iter_inventories(self, revision_ids, ordering=None):
1297
 
        self._ensure_real()
1298
 
        return self._real_repository.iter_inventories(revision_ids, ordering)
 
1879
        """Get many inventories by revision_ids.
 
1880
 
 
1881
        This will buffer some or all of the texts used in constructing the
 
1882
        inventories in memory, but will only parse a single inventory at a
 
1883
        time.
 
1884
 
 
1885
        :param revision_ids: The expected revision ids of the inventories.
 
1886
        :param ordering: optional ordering, e.g. 'topological'.  If not
 
1887
            specified, the order of revision_ids will be preserved (by
 
1888
            buffering if necessary).
 
1889
        :return: An iterator of inventories.
 
1890
        """
 
1891
        if ((None in revision_ids)
 
1892
            or (_mod_revision.NULL_REVISION in revision_ids)):
 
1893
            raise ValueError('cannot get null revision inventory')
 
1894
        for inv, revid in self._iter_inventories(revision_ids, ordering):
 
1895
            if inv is None:
 
1896
                raise errors.NoSuchRevision(self, revid)
 
1897
            yield inv
 
1898
 
 
1899
    def _iter_inventories(self, revision_ids, ordering=None):
 
1900
        if len(revision_ids) == 0:
 
1901
            return
 
1902
        missing = set(revision_ids)
 
1903
        if ordering is None:
 
1904
            order_as_requested = True
 
1905
            invs = {}
 
1906
            order = list(revision_ids)
 
1907
            order.reverse()
 
1908
            next_revid = order.pop()
 
1909
        else:
 
1910
            order_as_requested = False
 
1911
            if ordering != 'unordered' and self._fallback_repositories:
 
1912
                raise ValueError('unsupported ordering %r' % ordering)
 
1913
        iter_inv_fns = [self._iter_inventories_rpc] + [
 
1914
            fallback._iter_inventories for fallback in
 
1915
            self._fallback_repositories]
 
1916
        try:
 
1917
            for iter_inv in iter_inv_fns:
 
1918
                request = [revid for revid in revision_ids if revid in missing]
 
1919
                for inv, revid in iter_inv(request, ordering):
 
1920
                    if inv is None:
 
1921
                        continue
 
1922
                    missing.remove(inv.revision_id)
 
1923
                    if ordering != 'unordered':
 
1924
                        invs[revid] = inv
 
1925
                    else:
 
1926
                        yield inv, revid
 
1927
                if order_as_requested:
 
1928
                    # Yield as many results as we can while preserving order.
 
1929
                    while next_revid in invs:
 
1930
                        inv = invs.pop(next_revid)
 
1931
                        yield inv, inv.revision_id
 
1932
                        try:
 
1933
                            next_revid = order.pop()
 
1934
                        except IndexError:
 
1935
                            # We still want to fully consume the stream, just
 
1936
                            # in case it is not actually finished at this point
 
1937
                            next_revid = None
 
1938
                            break
 
1939
        except errors.UnknownSmartMethod:
 
1940
            for inv, revid in self._iter_inventories_vfs(revision_ids, ordering):
 
1941
                yield inv, revid
 
1942
            return
 
1943
        # Report missing
 
1944
        if order_as_requested:
 
1945
            if next_revid is not None:
 
1946
                yield None, next_revid
 
1947
            while order:
 
1948
                revid = order.pop()
 
1949
                yield invs.get(revid), revid
 
1950
        else:
 
1951
            while missing:
 
1952
                yield None, missing.pop()
1299
1953
 
1300
1954
    @needs_read_lock
1301
1955
    def get_revision(self, revision_id):
1302
 
        self._ensure_real()
1303
 
        return self._real_repository.get_revision(revision_id)
 
1956
        return self.get_revisions([revision_id])[0]
1304
1957
 
1305
1958
    def get_transaction(self):
1306
1959
        self._ensure_real()
1308
1961
 
1309
1962
    @needs_read_lock
1310
1963
    def clone(self, a_bzrdir, revision_id=None):
1311
 
        self._ensure_real()
1312
 
        return self._real_repository.clone(a_bzrdir, revision_id=revision_id)
 
1964
        dest_repo = self._create_sprouting_repo(
 
1965
            a_bzrdir, shared=self.is_shared())
 
1966
        self.copy_content_into(dest_repo, revision_id)
 
1967
        return dest_repo
1313
1968
 
1314
1969
    def make_working_trees(self):
1315
1970
        """See Repository.make_working_trees"""
1316
 
        self._ensure_real()
1317
 
        return self._real_repository.make_working_trees()
 
1971
        path = self.bzrdir._path_for_remote_call(self._client)
 
1972
        try:
 
1973
            response = self._call('Repository.make_working_trees', path)
 
1974
        except errors.UnknownSmartMethod:
 
1975
            self._ensure_real()
 
1976
            return self._real_repository.make_working_trees()
 
1977
        if response[0] not in ('yes', 'no'):
 
1978
            raise SmartProtocolError('unexpected response code %s' % (response,))
 
1979
        return response[0] == 'yes'
1318
1980
 
1319
1981
    def refresh_data(self):
1320
1982
        """Re-read any data needed to synchronise with disk.
1339
2001
        included_keys = result_set.intersection(result_parents)
1340
2002
        start_keys = result_set.difference(included_keys)
1341
2003
        exclude_keys = result_parents.difference(result_set)
1342
 
        result = graph.SearchResult(start_keys, exclude_keys,
 
2004
        result = vf_search.SearchResult(start_keys, exclude_keys,
1343
2005
            len(result_set), result_set)
1344
2006
        return result
1345
2007
 
1346
2008
    @needs_read_lock
1347
 
    def search_missing_revision_ids(self, other, revision_id=None, find_ghosts=True):
 
2009
    def search_missing_revision_ids(self, other,
 
2010
            revision_id=symbol_versioning.DEPRECATED_PARAMETER,
 
2011
            find_ghosts=True, revision_ids=None, if_present_ids=None,
 
2012
            limit=None):
1348
2013
        """Return the revision ids that other has that this does not.
1349
2014
 
1350
2015
        These are returned in topological order.
1351
2016
 
1352
2017
        revision_id: only return revision ids included by revision_id.
1353
2018
        """
1354
 
        return repository.InterRepository.get(
1355
 
            other, self).search_missing_revision_ids(revision_id, find_ghosts)
 
2019
        if symbol_versioning.deprecated_passed(revision_id):
 
2020
            symbol_versioning.warn(
 
2021
                'search_missing_revision_ids(revision_id=...) was '
 
2022
                'deprecated in 2.4.  Use revision_ids=[...] instead.',
 
2023
                DeprecationWarning, stacklevel=2)
 
2024
            if revision_ids is not None:
 
2025
                raise AssertionError(
 
2026
                    'revision_ids is mutually exclusive with revision_id')
 
2027
            if revision_id is not None:
 
2028
                revision_ids = [revision_id]
 
2029
        inter_repo = _mod_repository.InterRepository.get(other, self)
 
2030
        return inter_repo.search_missing_revision_ids(
 
2031
            find_ghosts=find_ghosts, revision_ids=revision_ids,
 
2032
            if_present_ids=if_present_ids, limit=limit)
1356
2033
 
1357
 
    def fetch(self, source, revision_id=None, pb=None, find_ghosts=False,
 
2034
    def fetch(self, source, revision_id=None, find_ghosts=False,
1358
2035
            fetch_spec=None):
1359
2036
        # No base implementation to use as RemoteRepository is not a subclass
1360
2037
        # of Repository; so this is a copy of Repository.fetch().
1371
2048
            # check that last_revision is in 'from' and then return a
1372
2049
            # no-operation.
1373
2050
            if (revision_id is not None and
1374
 
                not revision.is_null(revision_id)):
 
2051
                not _mod_revision.is_null(revision_id)):
1375
2052
                self.get_revision(revision_id)
1376
2053
            return 0, []
1377
2054
        # if there is no specific appropriate InterRepository, this will get
1378
2055
        # the InterRepository base class, which raises an
1379
2056
        # IncompatibleRepositories when asked to fetch.
1380
 
        inter = repository.InterRepository.get(source, self)
1381
 
        return inter.fetch(revision_id=revision_id, pb=pb,
 
2057
        inter = _mod_repository.InterRepository.get(source, self)
 
2058
        if (fetch_spec is not None and
 
2059
            not getattr(inter, "supports_fetch_spec", False)):
 
2060
            raise errors.UnsupportedOperation(
 
2061
                "fetch_spec not supported for %r" % inter)
 
2062
        return inter.fetch(revision_id=revision_id,
1382
2063
            find_ghosts=find_ghosts, fetch_spec=fetch_spec)
1383
2064
 
1384
2065
    def create_bundle(self, target, base, fileobj, format=None):
1386
2067
        self._real_repository.create_bundle(target, base, fileobj, format)
1387
2068
 
1388
2069
    @needs_read_lock
 
2070
    @symbol_versioning.deprecated_method(
 
2071
        symbol_versioning.deprecated_in((2, 4, 0)))
1389
2072
    def get_ancestry(self, revision_id, topo_sorted=True):
1390
2073
        self._ensure_real()
1391
2074
        return self._real_repository.get_ancestry(revision_id, topo_sorted)
1399
2082
        return self._real_repository._get_versioned_file_checker(
1400
2083
            revisions, revision_versions_cache)
1401
2084
 
 
2085
    def _iter_files_bytes_rpc(self, desired_files, absent):
 
2086
        path = self.bzrdir._path_for_remote_call(self._client)
 
2087
        lines = []
 
2088
        identifiers = []
 
2089
        for (file_id, revid, identifier) in desired_files:
 
2090
            lines.append("%s\0%s" % (
 
2091
                osutils.safe_file_id(file_id),
 
2092
                osutils.safe_revision_id(revid)))
 
2093
            identifiers.append(identifier)
 
2094
        (response_tuple, response_handler) = (
 
2095
            self._call_with_body_bytes_expecting_body(
 
2096
            "Repository.iter_files_bytes", (path, ), "\n".join(lines)))
 
2097
        if response_tuple != ('ok', ):
 
2098
            response_handler.cancel_read_body()
 
2099
            raise errors.UnexpectedSmartServerResponse(response_tuple)
 
2100
        byte_stream = response_handler.read_streamed_body()
 
2101
        def decompress_stream(start, byte_stream, unused):
 
2102
            decompressor = zlib.decompressobj()
 
2103
            yield decompressor.decompress(start)
 
2104
            while decompressor.unused_data == "":
 
2105
                try:
 
2106
                    data = byte_stream.next()
 
2107
                except StopIteration:
 
2108
                    break
 
2109
                yield decompressor.decompress(data)
 
2110
            yield decompressor.flush()
 
2111
            unused.append(decompressor.unused_data)
 
2112
        unused = ""
 
2113
        while True:
 
2114
            while not "\n" in unused:
 
2115
                unused += byte_stream.next()
 
2116
            header, rest = unused.split("\n", 1)
 
2117
            args = header.split("\0")
 
2118
            if args[0] == "absent":
 
2119
                absent[identifiers[int(args[3])]] = (args[1], args[2])
 
2120
                unused = rest
 
2121
                continue
 
2122
            elif args[0] == "ok":
 
2123
                idx = int(args[1])
 
2124
            else:
 
2125
                raise errors.UnexpectedSmartServerResponse(args)
 
2126
            unused_chunks = []
 
2127
            yield (identifiers[idx],
 
2128
                decompress_stream(rest, byte_stream, unused_chunks))
 
2129
            unused = "".join(unused_chunks)
 
2130
 
1402
2131
    def iter_files_bytes(self, desired_files):
1403
2132
        """See Repository.iter_file_bytes.
1404
2133
        """
1405
 
        self._ensure_real()
1406
 
        return self._real_repository.iter_files_bytes(desired_files)
 
2134
        try:
 
2135
            absent = {}
 
2136
            for (identifier, bytes_iterator) in self._iter_files_bytes_rpc(
 
2137
                    desired_files, absent):
 
2138
                yield identifier, bytes_iterator
 
2139
            for fallback in self._fallback_repositories:
 
2140
                if not absent:
 
2141
                    break
 
2142
                desired_files = [(key[0], key[1], identifier) for
 
2143
                    (identifier, key) in absent.iteritems()]
 
2144
                for (identifier, bytes_iterator) in fallback.iter_files_bytes(desired_files):
 
2145
                    del absent[identifier]
 
2146
                    yield identifier, bytes_iterator
 
2147
            if absent:
 
2148
                # There may be more missing items, but raise an exception
 
2149
                # for just one.
 
2150
                missing_identifier = absent.keys()[0]
 
2151
                missing_key = absent[missing_identifier]
 
2152
                raise errors.RevisionNotPresent(revision_id=missing_key[1],
 
2153
                    file_id=missing_key[0])
 
2154
        except errors.UnknownSmartMethod:
 
2155
            self._ensure_real()
 
2156
            for (identifier, bytes_iterator) in (
 
2157
                self._real_repository.iter_files_bytes(desired_files)):
 
2158
                yield identifier, bytes_iterator
 
2159
 
 
2160
    def get_cached_parent_map(self, revision_ids):
 
2161
        """See bzrlib.CachingParentsProvider.get_cached_parent_map"""
 
2162
        return self._unstacked_provider.get_cached_parent_map(revision_ids)
1407
2163
 
1408
2164
    def get_parent_map(self, revision_ids):
1409
2165
        """See bzrlib.Graph.get_parent_map()."""
1468
2224
        if parents_map is None:
1469
2225
            # Repository is not locked, so there's no cache.
1470
2226
            parents_map = {}
1471
 
        # start_set is all the keys in the cache
1472
 
        start_set = set(parents_map)
1473
 
        # result set is all the references to keys in the cache
1474
 
        result_parents = set()
1475
 
        for parents in parents_map.itervalues():
1476
 
            result_parents.update(parents)
1477
 
        stop_keys = result_parents.difference(start_set)
1478
 
        # We don't need to send ghosts back to the server as a position to
1479
 
        # stop either.
1480
 
        stop_keys.difference_update(self._unstacked_provider.missing_keys)
1481
 
        key_count = len(parents_map)
1482
 
        if (NULL_REVISION in result_parents
1483
 
            and NULL_REVISION in self._unstacked_provider.missing_keys):
1484
 
            # If we pruned NULL_REVISION from the stop_keys because it's also
1485
 
            # in our cache of "missing" keys we need to increment our key count
1486
 
            # by 1, because the reconsitituted SearchResult on the server will
1487
 
            # still consider NULL_REVISION to be an included key.
1488
 
            key_count += 1
1489
 
        included_keys = start_set.intersection(result_parents)
1490
 
        start_set.difference_update(included_keys)
 
2227
        if _DEFAULT_SEARCH_DEPTH <= 0:
 
2228
            (start_set, stop_keys,
 
2229
             key_count) = vf_search.search_result_from_parent_map(
 
2230
                parents_map, self._unstacked_provider.missing_keys)
 
2231
        else:
 
2232
            (start_set, stop_keys,
 
2233
             key_count) = vf_search.limited_search_result_from_parent_map(
 
2234
                parents_map, self._unstacked_provider.missing_keys,
 
2235
                keys, depth=_DEFAULT_SEARCH_DEPTH)
1491
2236
        recipe = ('manual', start_set, stop_keys, key_count)
1492
2237
        body = self._serialise_search_recipe(recipe)
1493
2238
        path = self.bzrdir._path_for_remote_call(self._client)
1542
2287
 
1543
2288
    @needs_read_lock
1544
2289
    def get_signature_text(self, revision_id):
1545
 
        self._ensure_real()
1546
 
        return self._real_repository.get_signature_text(revision_id)
 
2290
        path = self.bzrdir._path_for_remote_call(self._client)
 
2291
        try:
 
2292
            response_tuple, response_handler = self._call_expecting_body(
 
2293
                'Repository.get_revision_signature_text', path, revision_id)
 
2294
        except errors.UnknownSmartMethod:
 
2295
            self._ensure_real()
 
2296
            return self._real_repository.get_signature_text(revision_id)
 
2297
        except errors.NoSuchRevision, err:
 
2298
            for fallback in self._fallback_repositories:
 
2299
                try:
 
2300
                    return fallback.get_signature_text(revision_id)
 
2301
                except errors.NoSuchRevision:
 
2302
                    pass
 
2303
            raise err
 
2304
        else:
 
2305
            if response_tuple[0] != 'ok':
 
2306
                raise errors.UnexpectedSmartServerResponse(response_tuple)
 
2307
            return response_handler.read_body_bytes()
1547
2308
 
1548
2309
    @needs_read_lock
1549
2310
    def _get_inventory_xml(self, revision_id):
 
2311
        # This call is used by older working tree formats,
 
2312
        # which stored a serialized basis inventory.
1550
2313
        self._ensure_real()
1551
2314
        return self._real_repository._get_inventory_xml(revision_id)
1552
2315
 
 
2316
    @needs_write_lock
1553
2317
    def reconcile(self, other=None, thorough=False):
1554
 
        self._ensure_real()
1555
 
        return self._real_repository.reconcile(other=other, thorough=thorough)
 
2318
        from bzrlib.reconcile import RepoReconciler
 
2319
        path = self.bzrdir._path_for_remote_call(self._client)
 
2320
        try:
 
2321
            response, handler = self._call_expecting_body(
 
2322
                'Repository.reconcile', path, self._lock_token)
 
2323
        except (errors.UnknownSmartMethod, errors.TokenLockingNotSupported):
 
2324
            self._ensure_real()
 
2325
            return self._real_repository.reconcile(other=other, thorough=thorough)
 
2326
        if response != ('ok', ):
 
2327
            raise errors.UnexpectedSmartServerResponse(response)
 
2328
        body = handler.read_body_bytes()
 
2329
        result = RepoReconciler(self)
 
2330
        for line in body.split('\n'):
 
2331
            if not line:
 
2332
                continue
 
2333
            key, val_text = line.split(':')
 
2334
            if key == "garbage_inventories":
 
2335
                result.garbage_inventories = int(val_text)
 
2336
            elif key == "inconsistent_parents":
 
2337
                result.inconsistent_parents = int(val_text)
 
2338
            else:
 
2339
                mutter("unknown reconcile key %r" % key)
 
2340
        return result
1556
2341
 
1557
2342
    def all_revision_ids(self):
1558
 
        self._ensure_real()
1559
 
        return self._real_repository.all_revision_ids()
 
2343
        path = self.bzrdir._path_for_remote_call(self._client)
 
2344
        try:
 
2345
            response_tuple, response_handler = self._call_expecting_body(
 
2346
                "Repository.all_revision_ids", path)
 
2347
        except errors.UnknownSmartMethod:
 
2348
            self._ensure_real()
 
2349
            return self._real_repository.all_revision_ids()
 
2350
        if response_tuple != ("ok", ):
 
2351
            raise errors.UnexpectedSmartServerResponse(response_tuple)
 
2352
        revids = set(response_handler.read_body_bytes().splitlines())
 
2353
        for fallback in self._fallback_repositories:
 
2354
            revids.update(set(fallback.all_revision_ids()))
 
2355
        return list(revids)
 
2356
 
 
2357
    def _filtered_revision_trees(self, revision_ids, file_ids):
 
2358
        """Return Tree for a revision on this branch with only some files.
 
2359
 
 
2360
        :param revision_ids: a sequence of revision-ids;
 
2361
          a revision-id may not be None or 'null:'
 
2362
        :param file_ids: if not None, the result is filtered
 
2363
          so that only those file-ids, their parents and their
 
2364
          children are included.
 
2365
        """
 
2366
        inventories = self.iter_inventories(revision_ids)
 
2367
        for inv in inventories:
 
2368
            # Should we introduce a FilteredRevisionTree class rather
 
2369
            # than pre-filter the inventory here?
 
2370
            filtered_inv = inv.filter(file_ids)
 
2371
            yield InventoryRevisionTree(self, filtered_inv, filtered_inv.revision_id)
1560
2372
 
1561
2373
    @needs_read_lock
1562
2374
    def get_deltas_for_revisions(self, revisions, specific_fileids=None):
1563
 
        self._ensure_real()
1564
 
        return self._real_repository.get_deltas_for_revisions(revisions,
1565
 
            specific_fileids=specific_fileids)
 
2375
        medium = self._client._medium
 
2376
        if medium._is_remote_before((1, 2)):
 
2377
            self._ensure_real()
 
2378
            for delta in self._real_repository.get_deltas_for_revisions(
 
2379
                    revisions, specific_fileids):
 
2380
                yield delta
 
2381
            return
 
2382
        # Get the revision-ids of interest
 
2383
        required_trees = set()
 
2384
        for revision in revisions:
 
2385
            required_trees.add(revision.revision_id)
 
2386
            required_trees.update(revision.parent_ids[:1])
 
2387
 
 
2388
        # Get the matching filtered trees. Note that it's more
 
2389
        # efficient to pass filtered trees to changes_from() rather
 
2390
        # than doing the filtering afterwards. changes_from() could
 
2391
        # arguably do the filtering itself but it's path-based, not
 
2392
        # file-id based, so filtering before or afterwards is
 
2393
        # currently easier.
 
2394
        if specific_fileids is None:
 
2395
            trees = dict((t.get_revision_id(), t) for
 
2396
                t in self.revision_trees(required_trees))
 
2397
        else:
 
2398
            trees = dict((t.get_revision_id(), t) for
 
2399
                t in self._filtered_revision_trees(required_trees,
 
2400
                specific_fileids))
 
2401
 
 
2402
        # Calculate the deltas
 
2403
        for revision in revisions:
 
2404
            if not revision.parent_ids:
 
2405
                old_tree = self.revision_tree(_mod_revision.NULL_REVISION)
 
2406
            else:
 
2407
                old_tree = trees[revision.parent_ids[0]]
 
2408
            yield trees[revision.revision_id].changes_from(old_tree)
1566
2409
 
1567
2410
    @needs_read_lock
1568
2411
    def get_revision_delta(self, revision_id, specific_fileids=None):
1569
 
        self._ensure_real()
1570
 
        return self._real_repository.get_revision_delta(revision_id,
1571
 
            specific_fileids=specific_fileids)
 
2412
        r = self.get_revision(revision_id)
 
2413
        return list(self.get_deltas_for_revisions([r],
 
2414
            specific_fileids=specific_fileids))[0]
1572
2415
 
1573
2416
    @needs_read_lock
1574
2417
    def revision_trees(self, revision_ids):
1575
 
        self._ensure_real()
1576
 
        return self._real_repository.revision_trees(revision_ids)
 
2418
        inventories = self.iter_inventories(revision_ids)
 
2419
        for inv in inventories:
 
2420
            yield InventoryRevisionTree(self, inv, inv.revision_id)
1577
2421
 
1578
2422
    @needs_read_lock
1579
2423
    def get_revision_reconcile(self, revision_id):
1587
2431
            callback_refs=callback_refs, check_repo=check_repo)
1588
2432
 
1589
2433
    def copy_content_into(self, destination, revision_id=None):
1590
 
        self._ensure_real()
1591
 
        return self._real_repository.copy_content_into(
1592
 
            destination, revision_id=revision_id)
 
2434
        """Make a complete copy of the content in self into destination.
 
2435
 
 
2436
        This is a destructive operation! Do not use it on existing
 
2437
        repositories.
 
2438
        """
 
2439
        interrepo = _mod_repository.InterRepository.get(self, destination)
 
2440
        return interrepo.copy_content(revision_id)
1593
2441
 
1594
2442
    def _copy_repository_tarball(self, to_bzrdir, revision_id=None):
1595
2443
        # get a tarball of the remote repository, and copy from that into the
1596
2444
        # destination
1597
 
        from bzrlib import osutils
1598
2445
        import tarfile
1599
2446
        # TODO: Maybe a progress bar while streaming the tarball?
1600
 
        note("Copying repository content as tarball...")
 
2447
        note(gettext("Copying repository content as tarball..."))
1601
2448
        tar_file = self._get_tarball('bz2')
1602
2449
        if tar_file is None:
1603
2450
            return None
1608
2455
            tmpdir = osutils.mkdtemp()
1609
2456
            try:
1610
2457
                _extract_tar(tar, tmpdir)
1611
 
                tmp_bzrdir = BzrDir.open(tmpdir)
 
2458
                tmp_bzrdir = _mod_bzrdir.BzrDir.open(tmpdir)
1612
2459
                tmp_repo = tmp_bzrdir.open_repository()
1613
2460
                tmp_repo.copy_content_into(destination, revision_id)
1614
2461
            finally:
1632
2479
    @needs_write_lock
1633
2480
    def pack(self, hint=None, clean_obsolete_packs=False):
1634
2481
        """Compress the data within the repository.
1635
 
 
1636
 
        This is not currently implemented within the smart server.
1637
2482
        """
1638
 
        self._ensure_real()
1639
 
        return self._real_repository.pack(hint=hint, clean_obsolete_packs=clean_obsolete_packs)
 
2483
        if hint is None:
 
2484
            body = ""
 
2485
        else:
 
2486
            body = "".join([l+"\n" for l in hint])
 
2487
        path = self.bzrdir._path_for_remote_call(self._client)
 
2488
        try:
 
2489
            response, handler = self._call_with_body_bytes_expecting_body(
 
2490
                'Repository.pack', (path, self._lock_token,
 
2491
                    str(clean_obsolete_packs)), body)
 
2492
        except errors.UnknownSmartMethod:
 
2493
            self._ensure_real()
 
2494
            return self._real_repository.pack(hint=hint,
 
2495
                clean_obsolete_packs=clean_obsolete_packs)
 
2496
        handler.cancel_read_body()
 
2497
        if response != ('ok', ):
 
2498
            raise errors.UnexpectedSmartServerResponse(response)
1640
2499
 
1641
2500
    @property
1642
2501
    def revisions(self):
1643
2502
        """Decorate the real repository for now.
1644
2503
 
1645
 
        In the short term this should become a real object to intercept graph
1646
 
        lookups.
1647
 
 
1648
2504
        In the long term a full blown network facility is needed.
1649
2505
        """
1650
2506
        self._ensure_real()
1678
2534
 
1679
2535
    @needs_write_lock
1680
2536
    def sign_revision(self, revision_id, gpg_strategy):
1681
 
        self._ensure_real()
1682
 
        return self._real_repository.sign_revision(revision_id, gpg_strategy)
 
2537
        testament = _mod_testament.Testament.from_revision(self, revision_id)
 
2538
        plaintext = testament.as_short_text()
 
2539
        self.store_revision_signature(gpg_strategy, plaintext, revision_id)
1683
2540
 
1684
2541
    @property
1685
2542
    def texts(self):
1691
2548
        self._ensure_real()
1692
2549
        return self._real_repository.texts
1693
2550
 
 
2551
    def _iter_revisions_rpc(self, revision_ids):
 
2552
        body = "\n".join(revision_ids)
 
2553
        path = self.bzrdir._path_for_remote_call(self._client)
 
2554
        response_tuple, response_handler = (
 
2555
            self._call_with_body_bytes_expecting_body(
 
2556
            "Repository.iter_revisions", (path, ), body))
 
2557
        if response_tuple[0] != "ok":
 
2558
            raise errors.UnexpectedSmartServerResponse(response_tuple)
 
2559
        serializer_format = response_tuple[1]
 
2560
        serializer = serializer_format_registry.get(serializer_format)
 
2561
        byte_stream = response_handler.read_streamed_body()
 
2562
        decompressor = zlib.decompressobj()
 
2563
        chunks = []
 
2564
        for bytes in byte_stream:
 
2565
            chunks.append(decompressor.decompress(bytes))
 
2566
            if decompressor.unused_data != "":
 
2567
                chunks.append(decompressor.flush())
 
2568
                yield serializer.read_revision_from_string("".join(chunks))
 
2569
                unused = decompressor.unused_data
 
2570
                decompressor = zlib.decompressobj()
 
2571
                chunks = [decompressor.decompress(unused)]
 
2572
        chunks.append(decompressor.flush())
 
2573
        text = "".join(chunks)
 
2574
        if text != "":
 
2575
            yield serializer.read_revision_from_string("".join(chunks))
 
2576
 
1694
2577
    @needs_read_lock
1695
2578
    def get_revisions(self, revision_ids):
1696
 
        self._ensure_real()
1697
 
        return self._real_repository.get_revisions(revision_ids)
 
2579
        if revision_ids is None:
 
2580
            revision_ids = self.all_revision_ids()
 
2581
        else:
 
2582
            for rev_id in revision_ids:
 
2583
                if not rev_id or not isinstance(rev_id, basestring):
 
2584
                    raise errors.InvalidRevisionId(
 
2585
                        revision_id=rev_id, branch=self)
 
2586
        try:
 
2587
            missing = set(revision_ids)
 
2588
            revs = {}
 
2589
            for rev in self._iter_revisions_rpc(revision_ids):
 
2590
                missing.remove(rev.revision_id)
 
2591
                revs[rev.revision_id] = rev
 
2592
        except errors.UnknownSmartMethod:
 
2593
            self._ensure_real()
 
2594
            return self._real_repository.get_revisions(revision_ids)
 
2595
        for fallback in self._fallback_repositories:
 
2596
            if not missing:
 
2597
                break
 
2598
            for revid in list(missing):
 
2599
                # XXX JRV 2011-11-20: It would be nice if there was a
 
2600
                # public method on Repository that could be used to query
 
2601
                # for revision objects *without* failing completely if one
 
2602
                # was missing. There is VersionedFileRepository._iter_revisions,
 
2603
                # but unfortunately that's private and not provided by
 
2604
                # all repository implementations.
 
2605
                try:
 
2606
                    revs[revid] = fallback.get_revision(revid)
 
2607
                except errors.NoSuchRevision:
 
2608
                    pass
 
2609
                else:
 
2610
                    missing.remove(revid)
 
2611
        if missing:
 
2612
            raise errors.NoSuchRevision(self, list(missing)[0])
 
2613
        return [revs[revid] for revid in revision_ids]
1698
2614
 
1699
2615
    def supports_rich_root(self):
1700
2616
        return self._format.rich_root_data
1701
2617
 
 
2618
    @symbol_versioning.deprecated_method(symbol_versioning.deprecated_in((2, 4, 0)))
1702
2619
    def iter_reverse_revision_history(self, revision_id):
1703
2620
        self._ensure_real()
1704
2621
        return self._real_repository.iter_reverse_revision_history(revision_id)
1707
2624
    def _serializer(self):
1708
2625
        return self._format._serializer
1709
2626
 
 
2627
    @needs_write_lock
1710
2628
    def store_revision_signature(self, gpg_strategy, plaintext, revision_id):
1711
 
        self._ensure_real()
1712
 
        return self._real_repository.store_revision_signature(
1713
 
            gpg_strategy, plaintext, revision_id)
 
2629
        signature = gpg_strategy.sign(plaintext)
 
2630
        self.add_signature_text(revision_id, signature)
1714
2631
 
1715
2632
    def add_signature_text(self, revision_id, signature):
1716
 
        self._ensure_real()
1717
 
        return self._real_repository.add_signature_text(revision_id, signature)
 
2633
        if self._real_repository:
 
2634
            # If there is a real repository the write group will
 
2635
            # be in the real repository as well, so use that:
 
2636
            self._ensure_real()
 
2637
            return self._real_repository.add_signature_text(
 
2638
                revision_id, signature)
 
2639
        path = self.bzrdir._path_for_remote_call(self._client)
 
2640
        response, handler = self._call_with_body_bytes_expecting_body(
 
2641
            'Repository.add_signature_text', (path, self._lock_token,
 
2642
                revision_id) + tuple(self._write_group_tokens), signature)
 
2643
        handler.cancel_read_body()
 
2644
        self.refresh_data()
 
2645
        if response[0] != 'ok':
 
2646
            raise errors.UnexpectedSmartServerResponse(response)
 
2647
        self._write_group_tokens = response[1:]
1718
2648
 
1719
2649
    def has_signature_for_revision_id(self, revision_id):
1720
 
        self._ensure_real()
1721
 
        return self._real_repository.has_signature_for_revision_id(revision_id)
 
2650
        path = self.bzrdir._path_for_remote_call(self._client)
 
2651
        try:
 
2652
            response = self._call('Repository.has_signature_for_revision_id',
 
2653
                path, revision_id)
 
2654
        except errors.UnknownSmartMethod:
 
2655
            self._ensure_real()
 
2656
            return self._real_repository.has_signature_for_revision_id(
 
2657
                revision_id)
 
2658
        if response[0] not in ('yes', 'no'):
 
2659
            raise SmartProtocolError('unexpected response code %s' % (response,))
 
2660
        if response[0] == 'yes':
 
2661
            return True
 
2662
        for fallback in self._fallback_repositories:
 
2663
            if fallback.has_signature_for_revision_id(revision_id):
 
2664
                return True
 
2665
        return False
 
2666
 
 
2667
    @needs_read_lock
 
2668
    def verify_revision_signature(self, revision_id, gpg_strategy):
 
2669
        if not self.has_signature_for_revision_id(revision_id):
 
2670
            return gpg.SIGNATURE_NOT_SIGNED, None
 
2671
        signature = self.get_signature_text(revision_id)
 
2672
 
 
2673
        testament = _mod_testament.Testament.from_revision(self, revision_id)
 
2674
        plaintext = testament.as_short_text()
 
2675
 
 
2676
        return gpg_strategy.verify(signature, plaintext)
1722
2677
 
1723
2678
    def item_keys_introduced_by(self, revision_ids, _files_pb=None):
1724
2679
        self._ensure_real()
1725
2680
        return self._real_repository.item_keys_introduced_by(revision_ids,
1726
2681
            _files_pb=_files_pb)
1727
2682
 
1728
 
    def revision_graph_can_have_wrong_parents(self):
1729
 
        # The answer depends on the remote repo format.
1730
 
        self._ensure_real()
1731
 
        return self._real_repository.revision_graph_can_have_wrong_parents()
1732
 
 
1733
2683
    def _find_inconsistent_revision_parents(self, revisions_iterator=None):
1734
2684
        self._ensure_real()
1735
2685
        return self._real_repository._find_inconsistent_revision_parents(
1743
2693
        providers = [self._unstacked_provider]
1744
2694
        if other is not None:
1745
2695
            providers.insert(0, other)
1746
 
        providers.extend(r._make_parents_provider() for r in
1747
 
                         self._fallback_repositories)
1748
 
        return graph.StackedParentsProvider(providers)
 
2696
        return graph.StackedParentsProvider(_LazyListJoin(
 
2697
            providers, self._fallback_repositories))
1749
2698
 
1750
2699
    def _serialise_search_recipe(self, recipe):
1751
2700
        """Serialise a graph search recipe.
1759
2708
        return '\n'.join((start_keys, stop_keys, count))
1760
2709
 
1761
2710
    def _serialise_search_result(self, search_result):
1762
 
        if isinstance(search_result, graph.PendingAncestryResult):
1763
 
            parts = ['ancestry-of']
1764
 
            parts.extend(search_result.heads)
1765
 
        else:
1766
 
            recipe = search_result.get_recipe()
1767
 
            parts = [recipe[0], self._serialise_search_recipe(recipe)]
 
2711
        parts = search_result.get_network_struct()
1768
2712
        return '\n'.join(parts)
1769
2713
 
1770
2714
    def autopack(self):
1780
2724
            raise errors.UnexpectedSmartServerResponse(response)
1781
2725
 
1782
2726
 
1783
 
class RemoteStreamSink(repository.StreamSink):
 
2727
class RemoteStreamSink(vf_repository.StreamSink):
1784
2728
 
1785
2729
    def _insert_real(self, stream, src_format, resume_tokens):
1786
2730
        self.target_repo._ensure_real()
1887
2831
        self._last_substream and self._last_stream so that the stream can be
1888
2832
        resumed by _resume_stream_with_vfs.
1889
2833
        """
1890
 
                    
 
2834
 
1891
2835
        stream_iter = iter(stream)
1892
2836
        for substream_kind, substream in stream_iter:
1893
2837
            if substream_kind == 'inventory-deltas':
1896
2840
                return
1897
2841
            else:
1898
2842
                yield substream_kind, substream
1899
 
            
1900
 
 
1901
 
class RemoteStreamSource(repository.StreamSource):
 
2843
 
 
2844
 
 
2845
class RemoteStreamSource(vf_repository.StreamSource):
1902
2846
    """Stream data from a remote server."""
1903
2847
 
1904
2848
    def get_stream(self, search):
1925
2869
 
1926
2870
    def _real_stream(self, repo, search):
1927
2871
        """Get a stream for search from repo.
1928
 
        
1929
 
        This never called RemoteStreamSource.get_stream, and is a heler
1930
 
        for RemoteStreamSource._get_stream to allow getting a stream 
 
2872
 
 
2873
        This never called RemoteStreamSource.get_stream, and is a helper
 
2874
        for RemoteStreamSource._get_stream to allow getting a stream
1931
2875
        reliably whether fallback back because of old servers or trying
1932
2876
        to stream from a non-RemoteRepository (which the stacked support
1933
2877
        code will do).
1964
2908
        candidate_verbs = [
1965
2909
            ('Repository.get_stream_1.19', (1, 19)),
1966
2910
            ('Repository.get_stream', (1, 13))]
 
2911
 
1967
2912
        found_verb = False
1968
2913
        for verb, version in candidate_verbs:
1969
2914
            if medium._is_remote_before(version):
1973
2918
                    verb, args, search_bytes)
1974
2919
            except errors.UnknownSmartMethod:
1975
2920
                medium._remember_remote_is_before(version)
 
2921
            except errors.UnknownErrorFromSmartServer, e:
 
2922
                if isinstance(search, vf_search.EverythingResult):
 
2923
                    error_verb = e.error_from_smart_server.error_verb
 
2924
                    if error_verb == 'BadSearch':
 
2925
                        # Pre-2.4 servers don't support this sort of search.
 
2926
                        # XXX: perhaps falling back to VFS on BadSearch is a
 
2927
                        # good idea in general?  It might provide a little bit
 
2928
                        # of protection against client-side bugs.
 
2929
                        medium._remember_remote_is_before((2, 4))
 
2930
                        break
 
2931
                raise
1976
2932
            else:
1977
2933
                response_tuple, response_handler = response
1978
2934
                found_verb = True
2061
3017
 
2062
3018
    def _ensure_real(self):
2063
3019
        if self._custom_format is None:
2064
 
            self._custom_format = branch.network_format_registry.get(
2065
 
                self._network_name)
 
3020
            try:
 
3021
                self._custom_format = branch.network_format_registry.get(
 
3022
                    self._network_name)
 
3023
            except KeyError:
 
3024
                raise errors.UnknownFormatError(kind='branch',
 
3025
                    format=self._network_name)
2066
3026
 
2067
3027
    def get_format_description(self):
2068
3028
        self._ensure_real()
2075
3035
        return a_bzrdir.open_branch(name=name, 
2076
3036
            ignore_fallbacks=ignore_fallbacks)
2077
3037
 
2078
 
    def _vfs_initialize(self, a_bzrdir, name):
 
3038
    def _vfs_initialize(self, a_bzrdir, name, append_revisions_only):
2079
3039
        # Initialisation when using a local bzrdir object, or a non-vfs init
2080
3040
        # method is not available on the server.
2081
3041
        # self._custom_format is always set - the start of initialize ensures
2083
3043
        if isinstance(a_bzrdir, RemoteBzrDir):
2084
3044
            a_bzrdir._ensure_real()
2085
3045
            result = self._custom_format.initialize(a_bzrdir._real_bzrdir,
2086
 
                name)
 
3046
                name, append_revisions_only=append_revisions_only)
2087
3047
        else:
2088
3048
            # We assume the bzrdir is parameterised; it may not be.
2089
 
            result = self._custom_format.initialize(a_bzrdir, name)
 
3049
            result = self._custom_format.initialize(a_bzrdir, name,
 
3050
                append_revisions_only=append_revisions_only)
2090
3051
        if (isinstance(a_bzrdir, RemoteBzrDir) and
2091
3052
            not isinstance(result, RemoteBranch)):
2092
3053
            result = RemoteBranch(a_bzrdir, a_bzrdir.find_repository(), result,
2093
3054
                                  name=name)
2094
3055
        return result
2095
3056
 
2096
 
    def initialize(self, a_bzrdir, name=None):
 
3057
    def initialize(self, a_bzrdir, name=None, repository=None,
 
3058
                   append_revisions_only=None):
2097
3059
        # 1) get the network name to use.
2098
3060
        if self._custom_format:
2099
3061
            network_name = self._custom_format.network_name()
2100
3062
        else:
2101
3063
            # Select the current bzrlib default and ask for that.
2102
 
            reference_bzrdir_format = bzrdir.format_registry.get('default')()
 
3064
            reference_bzrdir_format = _mod_bzrdir.format_registry.get('default')()
2103
3065
            reference_format = reference_bzrdir_format.get_branch_format()
2104
3066
            self._custom_format = reference_format
2105
3067
            network_name = reference_format.network_name()
2106
3068
        # Being asked to create on a non RemoteBzrDir:
2107
3069
        if not isinstance(a_bzrdir, RemoteBzrDir):
2108
 
            return self._vfs_initialize(a_bzrdir, name=name)
 
3070
            return self._vfs_initialize(a_bzrdir, name=name,
 
3071
                append_revisions_only=append_revisions_only)
2109
3072
        medium = a_bzrdir._client._medium
2110
3073
        if medium._is_remote_before((1, 13)):
2111
 
            return self._vfs_initialize(a_bzrdir, name=name)
 
3074
            return self._vfs_initialize(a_bzrdir, name=name,
 
3075
                append_revisions_only=append_revisions_only)
2112
3076
        # Creating on a remote bzr dir.
2113
3077
        # 2) try direct creation via RPC
2114
3078
        path = a_bzrdir._path_for_remote_call(a_bzrdir._client)
2121
3085
        except errors.UnknownSmartMethod:
2122
3086
            # Fallback - use vfs methods
2123
3087
            medium._remember_remote_is_before((1, 13))
2124
 
            return self._vfs_initialize(a_bzrdir, name=name)
 
3088
            return self._vfs_initialize(a_bzrdir, name=name,
 
3089
                    append_revisions_only=append_revisions_only)
2125
3090
        if response[0] != 'ok':
2126
3091
            raise errors.UnexpectedSmartServerResponse(response)
2127
3092
        # Turn the response into a RemoteRepository object.
2128
3093
        format = RemoteBranchFormat(network_name=response[1])
2129
3094
        repo_format = response_tuple_to_repo_format(response[3:])
2130
 
        if response[2] == '':
2131
 
            repo_bzrdir = a_bzrdir
 
3095
        repo_path = response[2]
 
3096
        if repository is not None:
 
3097
            remote_repo_url = urlutils.join(a_bzrdir.user_url, repo_path)
 
3098
            url_diff = urlutils.relative_url(repository.user_url,
 
3099
                    remote_repo_url)
 
3100
            if url_diff != '.':
 
3101
                raise AssertionError(
 
3102
                    'repository.user_url %r does not match URL from server '
 
3103
                    'response (%r + %r)'
 
3104
                    % (repository.user_url, a_bzrdir.user_url, repo_path))
 
3105
            remote_repo = repository
2132
3106
        else:
2133
 
            repo_bzrdir = RemoteBzrDir(
2134
 
                a_bzrdir.root_transport.clone(response[2]), a_bzrdir._format,
2135
 
                a_bzrdir._client)
2136
 
        remote_repo = RemoteRepository(repo_bzrdir, repo_format)
 
3107
            if repo_path == '':
 
3108
                repo_bzrdir = a_bzrdir
 
3109
            else:
 
3110
                repo_bzrdir = RemoteBzrDir(
 
3111
                    a_bzrdir.root_transport.clone(repo_path), a_bzrdir._format,
 
3112
                    a_bzrdir._client)
 
3113
            remote_repo = RemoteRepository(repo_bzrdir, repo_format)
2137
3114
        remote_branch = RemoteBranch(a_bzrdir, remote_repo,
2138
3115
            format=format, setup_stacking=False, name=name)
 
3116
        if append_revisions_only:
 
3117
            remote_branch.set_append_revisions_only(append_revisions_only)
2139
3118
        # XXX: We know this is a new branch, so it must have revno 0, revid
2140
3119
        # NULL_REVISION. Creating the branch locked would make this be unable
2141
3120
        # to be wrong; here its simply very unlikely to be wrong. RBC 20090225
2160
3139
        self._ensure_real()
2161
3140
        return self._custom_format.supports_set_append_revisions_only()
2162
3141
 
 
3142
    def _use_default_local_heads_to_fetch(self):
 
3143
        # If the branch format is a metadir format *and* its heads_to_fetch
 
3144
        # implementation is not overridden vs the base class, we can use the
 
3145
        # base class logic rather than use the heads_to_fetch RPC.  This is
 
3146
        # usually cheaper in terms of net round trips, as the last-revision and
 
3147
        # tags info fetched is cached and would be fetched anyway.
 
3148
        self._ensure_real()
 
3149
        if isinstance(self._custom_format, branch.BranchFormatMetadir):
 
3150
            branch_class = self._custom_format._branch_class()
 
3151
            heads_to_fetch_impl = branch_class.heads_to_fetch.im_func
 
3152
            if heads_to_fetch_impl is branch.Branch.heads_to_fetch.im_func:
 
3153
                return True
 
3154
        return False
 
3155
 
 
3156
 
 
3157
class RemoteBranchStore(config.IniFileStore):
 
3158
    """Branch store which attempts to use HPSS calls to retrieve branch store.
 
3159
 
 
3160
    Note that this is specific to bzr-based formats.
 
3161
    """
 
3162
 
 
3163
    def __init__(self, branch):
 
3164
        super(RemoteBranchStore, self).__init__()
 
3165
        self.branch = branch
 
3166
        self.id = "branch"
 
3167
        self._real_store = None
 
3168
 
 
3169
    def lock_write(self, token=None):
 
3170
        return self.branch.lock_write(token)
 
3171
 
 
3172
    def unlock(self):
 
3173
        return self.branch.unlock()
 
3174
 
 
3175
    @needs_write_lock
 
3176
    def save(self):
 
3177
        # We need to be able to override the undecorated implementation
 
3178
        self.save_without_locking()
 
3179
 
 
3180
    def save_without_locking(self):
 
3181
        super(RemoteBranchStore, self).save()
 
3182
 
 
3183
    def external_url(self):
 
3184
        return self.branch.user_url
 
3185
 
 
3186
    def _load_content(self):
 
3187
        path = self.branch._remote_path()
 
3188
        try:
 
3189
            response, handler = self.branch._call_expecting_body(
 
3190
                'Branch.get_config_file', path)
 
3191
        except errors.UnknownSmartMethod:
 
3192
            self._ensure_real()
 
3193
            return self._real_store._load_content()
 
3194
        if len(response) and response[0] != 'ok':
 
3195
            raise errors.UnexpectedSmartServerResponse(response)
 
3196
        return handler.read_body_bytes()
 
3197
 
 
3198
    def _save_content(self, content):
 
3199
        path = self.branch._remote_path()
 
3200
        try:
 
3201
            response, handler = self.branch._call_with_body_bytes_expecting_body(
 
3202
                'Branch.put_config_file', (path,
 
3203
                    self.branch._lock_token, self.branch._repo_lock_token),
 
3204
                content)
 
3205
        except errors.UnknownSmartMethod:
 
3206
            self._ensure_real()
 
3207
            return self._real_store._save_content(content)
 
3208
        handler.cancel_read_body()
 
3209
        if response != ('ok', ):
 
3210
            raise errors.UnexpectedSmartServerResponse(response)
 
3211
 
 
3212
    def _ensure_real(self):
 
3213
        self.branch._ensure_real()
 
3214
        if self._real_store is None:
 
3215
            self._real_store = config.BranchStore(self.branch)
 
3216
 
2163
3217
 
2164
3218
class RemoteBranch(branch.Branch, _RpcHelper, lock._RelockDebugMixin):
2165
3219
    """Branch stored on a server accessed by HPSS RPC.
2168
3222
    """
2169
3223
 
2170
3224
    def __init__(self, remote_bzrdir, remote_repository, real_branch=None,
2171
 
        _client=None, format=None, setup_stacking=True, name=None):
 
3225
        _client=None, format=None, setup_stacking=True, name=None,
 
3226
        possible_transports=None):
2172
3227
        """Create a RemoteBranch instance.
2173
3228
 
2174
3229
        :param real_branch: An optional local implementation of the branch
2239
3294
            hook(self)
2240
3295
        self._is_stacked = False
2241
3296
        if setup_stacking:
2242
 
            self._setup_stacking()
 
3297
            self._setup_stacking(possible_transports)
2243
3298
 
2244
 
    def _setup_stacking(self):
 
3299
    def _setup_stacking(self, possible_transports):
2245
3300
        # configure stacking into the remote repository, by reading it from
2246
3301
        # the vfs branch.
2247
3302
        try:
2250
3305
            errors.UnstackableRepositoryFormat), e:
2251
3306
            return
2252
3307
        self._is_stacked = True
2253
 
        self._activate_fallback_location(fallback_url)
 
3308
        if possible_transports is None:
 
3309
            possible_transports = []
 
3310
        else:
 
3311
            possible_transports = list(possible_transports)
 
3312
        possible_transports.append(self.bzrdir.root_transport)
 
3313
        self._activate_fallback_location(fallback_url,
 
3314
            possible_transports=possible_transports)
2254
3315
 
2255
3316
    def _get_config(self):
2256
3317
        return RemoteBranchConfig(self)
2257
3318
 
 
3319
    def _get_config_store(self):
 
3320
        return RemoteBranchStore(self)
 
3321
 
2258
3322
    def _get_real_transport(self):
2259
3323
        # if we try vfs access, return the real branch's vfs transport
2260
3324
        self._ensure_real()
2323
3387
                self.bzrdir, self._client)
2324
3388
        return self._control_files
2325
3389
 
2326
 
    def _get_checkout_format(self):
2327
 
        self._ensure_real()
2328
 
        return self._real_branch._get_checkout_format()
2329
 
 
2330
3390
    def get_physical_lock_status(self):
2331
3391
        """See Branch.get_physical_lock_status()."""
2332
 
        # should be an API call to the server, as branches must be lockable.
2333
 
        self._ensure_real()
2334
 
        return self._real_branch.get_physical_lock_status()
 
3392
        try:
 
3393
            response = self._client.call('Branch.get_physical_lock_status',
 
3394
                self._remote_path())
 
3395
        except errors.UnknownSmartMethod:
 
3396
            self._ensure_real()
 
3397
            return self._real_branch.get_physical_lock_status()
 
3398
        if response[0] not in ('yes', 'no'):
 
3399
            raise errors.UnexpectedSmartServerResponse(response)
 
3400
        return (response[0] == 'yes')
2335
3401
 
2336
3402
    def get_stacked_on_url(self):
2337
3403
        """Get the URL this branch is stacked against.
2364
3430
            self._is_stacked = False
2365
3431
        else:
2366
3432
            self._is_stacked = True
2367
 
        
 
3433
 
2368
3434
    def _vfs_get_tags_bytes(self):
2369
3435
        self._ensure_real()
2370
3436
        return self._real_branch._get_tags_bytes()
2371
3437
 
 
3438
    @needs_read_lock
2372
3439
    def _get_tags_bytes(self):
 
3440
        if self._tags_bytes is None:
 
3441
            self._tags_bytes = self._get_tags_bytes_via_hpss()
 
3442
        return self._tags_bytes
 
3443
 
 
3444
    def _get_tags_bytes_via_hpss(self):
2373
3445
        medium = self._client._medium
2374
3446
        if medium._is_remote_before((1, 13)):
2375
3447
            return self._vfs_get_tags_bytes()
2385
3457
        return self._real_branch._set_tags_bytes(bytes)
2386
3458
 
2387
3459
    def _set_tags_bytes(self, bytes):
 
3460
        if self.is_locked():
 
3461
            self._tags_bytes = bytes
2388
3462
        medium = self._client._medium
2389
3463
        if medium._is_remote_before((1, 18)):
2390
3464
            self._vfs_set_tags_bytes(bytes)
2513
3587
            self.repository.unlock()
2514
3588
 
2515
3589
    def break_lock(self):
2516
 
        self._ensure_real()
2517
 
        return self._real_branch.break_lock()
 
3590
        try:
 
3591
            response = self._call(
 
3592
                'Branch.break_lock', self._remote_path())
 
3593
        except errors.UnknownSmartMethod:
 
3594
            self._ensure_real()
 
3595
            return self._real_branch.break_lock()
 
3596
        if response != ('ok',):
 
3597
            raise errors.UnexpectedSmartServerResponse(response)
2518
3598
 
2519
3599
    def leave_lock_in_place(self):
2520
3600
        if not self._lock_token:
2544
3624
            missing_parent = parent_map[missing_parent]
2545
3625
        raise errors.RevisionNotPresent(missing_parent, self.repository)
2546
3626
 
2547
 
    def _last_revision_info(self):
 
3627
    def _read_last_revision_info(self):
2548
3628
        response = self._call('Branch.last_revision_info', self._remote_path())
2549
3629
        if response[0] != 'ok':
2550
3630
            raise SmartProtocolError('unexpected response code %s' % (response,))
2613
3693
            raise errors.UnexpectedSmartServerResponse(response)
2614
3694
        self._run_post_change_branch_tip_hooks(old_revno, old_revid)
2615
3695
 
 
3696
    @symbol_versioning.deprecated_method(symbol_versioning.deprecated_in((2, 4, 0)))
2616
3697
    @needs_write_lock
2617
3698
    def set_revision_history(self, rev_history):
 
3699
        """See Branch.set_revision_history."""
 
3700
        self._set_revision_history(rev_history)
 
3701
 
 
3702
    @needs_write_lock
 
3703
    def _set_revision_history(self, rev_history):
2618
3704
        # Send just the tip revision of the history; the server will generate
2619
3705
        # the full history from that.  If the revision doesn't exist in this
2620
3706
        # branch, NoSuchRevision will be raised.
2678
3764
            _override_hook_target=self, **kwargs)
2679
3765
 
2680
3766
    @needs_read_lock
2681
 
    def push(self, target, overwrite=False, stop_revision=None):
 
3767
    def push(self, target, overwrite=False, stop_revision=None, lossy=False):
2682
3768
        self._ensure_real()
2683
3769
        return self._real_branch.push(
2684
 
            target, overwrite=overwrite, stop_revision=stop_revision,
 
3770
            target, overwrite=overwrite, stop_revision=stop_revision, lossy=lossy,
2685
3771
            _override_hook_source_branch=self)
2686
3772
 
2687
3773
    def is_locked(self):
2688
3774
        return self._lock_count >= 1
2689
3775
 
2690
3776
    @needs_read_lock
 
3777
    def revision_id_to_dotted_revno(self, revision_id):
 
3778
        """Given a revision id, return its dotted revno.
 
3779
 
 
3780
        :return: a tuple like (1,) or (400,1,3).
 
3781
        """
 
3782
        try:
 
3783
            response = self._call('Branch.revision_id_to_revno',
 
3784
                self._remote_path(), revision_id)
 
3785
        except errors.UnknownSmartMethod:
 
3786
            self._ensure_real()
 
3787
            return self._real_branch.revision_id_to_dotted_revno(revision_id)
 
3788
        if response[0] == 'ok':
 
3789
            return tuple([int(x) for x in response[1:]])
 
3790
        else:
 
3791
            raise errors.UnexpectedSmartServerResponse(response)
 
3792
 
 
3793
    @needs_read_lock
2691
3794
    def revision_id_to_revno(self, revision_id):
2692
 
        self._ensure_real()
2693
 
        return self._real_branch.revision_id_to_revno(revision_id)
 
3795
        """Given a revision id on the branch mainline, return its revno.
 
3796
 
 
3797
        :return: an integer
 
3798
        """
 
3799
        try:
 
3800
            response = self._call('Branch.revision_id_to_revno',
 
3801
                self._remote_path(), revision_id)
 
3802
        except errors.UnknownSmartMethod:
 
3803
            self._ensure_real()
 
3804
            return self._real_branch.revision_id_to_revno(revision_id)
 
3805
        if response[0] == 'ok':
 
3806
            if len(response) == 2:
 
3807
                return int(response[1])
 
3808
            raise NoSuchRevision(self, revision_id)
 
3809
        else:
 
3810
            raise errors.UnexpectedSmartServerResponse(response)
2694
3811
 
2695
3812
    @needs_write_lock
2696
3813
    def set_last_revision_info(self, revno, revision_id):
2697
3814
        # XXX: These should be returned by the set_last_revision_info verb
2698
3815
        old_revno, old_revid = self.last_revision_info()
2699
3816
        self._run_pre_change_branch_tip_hooks(revno, revision_id)
2700
 
        revision_id = ensure_null(revision_id)
 
3817
        if not revision_id or not isinstance(revision_id, basestring):
 
3818
            raise errors.InvalidRevisionId(revision_id=revision_id, branch=self)
2701
3819
        try:
2702
3820
            response = self._call('Branch.set_last_revision_info',
2703
3821
                self._remote_path(), self._lock_token, self._repo_lock_token,
2732
3850
            except errors.UnknownSmartMethod:
2733
3851
                medium._remember_remote_is_before((1, 6))
2734
3852
        self._clear_cached_state_of_remote_branch_only()
2735
 
        self.set_revision_history(self._lefthand_history(revision_id,
 
3853
        self._set_revision_history(self._lefthand_history(revision_id,
2736
3854
            last_rev=last_rev,other_branch=other_branch))
2737
3855
 
2738
3856
    def set_push_location(self, location):
2739
3857
        self._ensure_real()
2740
3858
        return self._real_branch.set_push_location(location)
2741
3859
 
 
3860
    def heads_to_fetch(self):
 
3861
        if self._format._use_default_local_heads_to_fetch():
 
3862
            # We recognise this format, and its heads-to-fetch implementation
 
3863
            # is the default one (tip + tags).  In this case it's cheaper to
 
3864
            # just use the default implementation rather than a special RPC as
 
3865
            # the tip and tags data is cached.
 
3866
            return branch.Branch.heads_to_fetch(self)
 
3867
        medium = self._client._medium
 
3868
        if medium._is_remote_before((2, 4)):
 
3869
            return self._vfs_heads_to_fetch()
 
3870
        try:
 
3871
            return self._rpc_heads_to_fetch()
 
3872
        except errors.UnknownSmartMethod:
 
3873
            medium._remember_remote_is_before((2, 4))
 
3874
            return self._vfs_heads_to_fetch()
 
3875
 
 
3876
    def _rpc_heads_to_fetch(self):
 
3877
        response = self._call('Branch.heads_to_fetch', self._remote_path())
 
3878
        if len(response) != 2:
 
3879
            raise errors.UnexpectedSmartServerResponse(response)
 
3880
        must_fetch, if_present_fetch = response
 
3881
        return set(must_fetch), set(if_present_fetch)
 
3882
 
 
3883
    def _vfs_heads_to_fetch(self):
 
3884
        self._ensure_real()
 
3885
        return self._real_branch.heads_to_fetch()
 
3886
 
2742
3887
 
2743
3888
class RemoteConfig(object):
2744
3889
    """A Config that reads and writes from smart verbs.
2758
3903
        """
2759
3904
        try:
2760
3905
            configobj = self._get_configobj()
 
3906
            section_obj = None
2761
3907
            if section is None:
2762
3908
                section_obj = configobj
2763
3909
            else:
2764
3910
                try:
2765
3911
                    section_obj = configobj[section]
2766
3912
                except KeyError:
2767
 
                    return default
2768
 
            return section_obj.get(name, default)
 
3913
                    pass
 
3914
            if section_obj is None:
 
3915
                value = default
 
3916
            else:
 
3917
                value = section_obj.get(name, default)
2769
3918
        except errors.UnknownSmartMethod:
2770
 
            return self._vfs_get_option(name, section, default)
 
3919
            value = self._vfs_get_option(name, section, default)
 
3920
        for hook in config.OldConfigHooks['get']:
 
3921
            hook(self, name, value)
 
3922
        return value
2771
3923
 
2772
3924
    def _response_to_configobj(self, response):
2773
3925
        if len(response[0]) and response[0][0] != 'ok':
2774
3926
            raise errors.UnexpectedSmartServerResponse(response)
2775
3927
        lines = response[1].read_body_bytes().splitlines()
2776
 
        return config.ConfigObj(lines, encoding='utf-8')
 
3928
        conf = config.ConfigObj(lines, encoding='utf-8')
 
3929
        for hook in config.OldConfigHooks['load']:
 
3930
            hook(self)
 
3931
        return conf
2777
3932
 
2778
3933
 
2779
3934
class RemoteBranchConfig(RemoteConfig):
2887
4042
        return self._bzrdir._real_bzrdir
2888
4043
 
2889
4044
 
2890
 
 
2891
4045
def _extract_tar(tar, to_dir):
2892
4046
    """Extract all the contents of a tarfile object.
2893
4047
 
2897
4051
        tar.extract(tarinfo, to_dir)
2898
4052
 
2899
4053
 
 
4054
error_translators = registry.Registry()
 
4055
no_context_error_translators = registry.Registry()
 
4056
 
 
4057
 
2900
4058
def _translate_error(err, **context):
2901
4059
    """Translate an ErrorFromSmartServer into a more useful error.
2902
4060
 
2931
4089
                    'Missing key %r in context %r', key_err.args[0], context)
2932
4090
                raise err
2933
4091
 
2934
 
    if err.error_verb == 'IncompatibleRepositories':
2935
 
        raise errors.IncompatibleRepositories(err.error_args[0],
2936
 
            err.error_args[1], err.error_args[2])
2937
 
    elif err.error_verb == 'NoSuchRevision':
2938
 
        raise NoSuchRevision(find('branch'), err.error_args[0])
2939
 
    elif err.error_verb == 'nosuchrevision':
2940
 
        raise NoSuchRevision(find('repository'), err.error_args[0])
2941
 
    elif err.error_verb == 'nobranch':
2942
 
        if len(err.error_args) >= 1:
2943
 
            extra = err.error_args[0]
2944
 
        else:
2945
 
            extra = None
2946
 
        raise errors.NotBranchError(path=find('bzrdir').root_transport.base,
2947
 
            detail=extra)
2948
 
    elif err.error_verb == 'norepository':
2949
 
        raise errors.NoRepositoryPresent(find('bzrdir'))
2950
 
    elif err.error_verb == 'LockContention':
2951
 
        raise errors.LockContention('(remote lock)')
2952
 
    elif err.error_verb == 'UnlockableTransport':
2953
 
        raise errors.UnlockableTransport(find('bzrdir').root_transport)
2954
 
    elif err.error_verb == 'LockFailed':
2955
 
        raise errors.LockFailed(err.error_args[0], err.error_args[1])
2956
 
    elif err.error_verb == 'TokenMismatch':
2957
 
        raise errors.TokenMismatch(find('token'), '(remote token)')
2958
 
    elif err.error_verb == 'Diverged':
2959
 
        raise errors.DivergedBranches(find('branch'), find('other_branch'))
2960
 
    elif err.error_verb == 'TipChangeRejected':
2961
 
        raise errors.TipChangeRejected(err.error_args[0].decode('utf8'))
2962
 
    elif err.error_verb == 'UnstackableBranchFormat':
2963
 
        raise errors.UnstackableBranchFormat(*err.error_args)
2964
 
    elif err.error_verb == 'UnstackableRepositoryFormat':
2965
 
        raise errors.UnstackableRepositoryFormat(*err.error_args)
2966
 
    elif err.error_verb == 'NotStacked':
2967
 
        raise errors.NotStacked(branch=find('branch'))
2968
 
    elif err.error_verb == 'PermissionDenied':
2969
 
        path = get_path()
2970
 
        if len(err.error_args) >= 2:
2971
 
            extra = err.error_args[1]
2972
 
        else:
2973
 
            extra = None
2974
 
        raise errors.PermissionDenied(path, extra=extra)
2975
 
    elif err.error_verb == 'ReadError':
2976
 
        path = get_path()
2977
 
        raise errors.ReadError(path)
2978
 
    elif err.error_verb == 'NoSuchFile':
2979
 
        path = get_path()
2980
 
        raise errors.NoSuchFile(path)
2981
 
    elif err.error_verb == 'FileExists':
2982
 
        raise errors.FileExists(err.error_args[0])
2983
 
    elif err.error_verb == 'DirectoryNotEmpty':
2984
 
        raise errors.DirectoryNotEmpty(err.error_args[0])
2985
 
    elif err.error_verb == 'ShortReadvError':
2986
 
        args = err.error_args
2987
 
        raise errors.ShortReadvError(
2988
 
            args[0], int(args[1]), int(args[2]), int(args[3]))
2989
 
    elif err.error_verb in ('UnicodeEncodeError', 'UnicodeDecodeError'):
 
4092
    try:
 
4093
        translator = error_translators.get(err.error_verb)
 
4094
    except KeyError:
 
4095
        pass
 
4096
    else:
 
4097
        raise translator(err, find, get_path)
 
4098
    try:
 
4099
        translator = no_context_error_translators.get(err.error_verb)
 
4100
    except KeyError:
 
4101
        raise errors.UnknownErrorFromSmartServer(err)
 
4102
    else:
 
4103
        raise translator(err)
 
4104
 
 
4105
 
 
4106
error_translators.register('NoSuchRevision',
 
4107
    lambda err, find, get_path: NoSuchRevision(
 
4108
        find('branch'), err.error_args[0]))
 
4109
error_translators.register('nosuchrevision',
 
4110
    lambda err, find, get_path: NoSuchRevision(
 
4111
        find('repository'), err.error_args[0]))
 
4112
 
 
4113
def _translate_nobranch_error(err, find, get_path):
 
4114
    if len(err.error_args) >= 1:
 
4115
        extra = err.error_args[0]
 
4116
    else:
 
4117
        extra = None
 
4118
    return errors.NotBranchError(path=find('bzrdir').root_transport.base,
 
4119
        detail=extra)
 
4120
 
 
4121
error_translators.register('nobranch', _translate_nobranch_error)
 
4122
error_translators.register('norepository',
 
4123
    lambda err, find, get_path: errors.NoRepositoryPresent(
 
4124
        find('bzrdir')))
 
4125
error_translators.register('UnlockableTransport',
 
4126
    lambda err, find, get_path: errors.UnlockableTransport(
 
4127
        find('bzrdir').root_transport))
 
4128
error_translators.register('TokenMismatch',
 
4129
    lambda err, find, get_path: errors.TokenMismatch(
 
4130
        find('token'), '(remote token)'))
 
4131
error_translators.register('Diverged',
 
4132
    lambda err, find, get_path: errors.DivergedBranches(
 
4133
        find('branch'), find('other_branch')))
 
4134
error_translators.register('NotStacked',
 
4135
    lambda err, find, get_path: errors.NotStacked(branch=find('branch')))
 
4136
 
 
4137
def _translate_PermissionDenied(err, find, get_path):
 
4138
    path = get_path()
 
4139
    if len(err.error_args) >= 2:
 
4140
        extra = err.error_args[1]
 
4141
    else:
 
4142
        extra = None
 
4143
    return errors.PermissionDenied(path, extra=extra)
 
4144
 
 
4145
error_translators.register('PermissionDenied', _translate_PermissionDenied)
 
4146
error_translators.register('ReadError',
 
4147
    lambda err, find, get_path: errors.ReadError(get_path()))
 
4148
error_translators.register('NoSuchFile',
 
4149
    lambda err, find, get_path: errors.NoSuchFile(get_path()))
 
4150
error_translators.register('TokenLockingNotSupported',
 
4151
    lambda err, find, get_path: errors.TokenLockingNotSupported(
 
4152
        find('repository')))
 
4153
error_translators.register('UnsuspendableWriteGroup',
 
4154
    lambda err, find, get_path: errors.UnsuspendableWriteGroup(
 
4155
        repository=find('repository')))
 
4156
error_translators.register('UnresumableWriteGroup',
 
4157
    lambda err, find, get_path: errors.UnresumableWriteGroup(
 
4158
        repository=find('repository'), write_groups=err.error_args[0],
 
4159
        reason=err.error_args[1]))
 
4160
no_context_error_translators.register('IncompatibleRepositories',
 
4161
    lambda err: errors.IncompatibleRepositories(
 
4162
        err.error_args[0], err.error_args[1], err.error_args[2]))
 
4163
no_context_error_translators.register('LockContention',
 
4164
    lambda err: errors.LockContention('(remote lock)'))
 
4165
no_context_error_translators.register('LockFailed',
 
4166
    lambda err: errors.LockFailed(err.error_args[0], err.error_args[1]))
 
4167
no_context_error_translators.register('TipChangeRejected',
 
4168
    lambda err: errors.TipChangeRejected(err.error_args[0].decode('utf8')))
 
4169
no_context_error_translators.register('UnstackableBranchFormat',
 
4170
    lambda err: errors.UnstackableBranchFormat(*err.error_args))
 
4171
no_context_error_translators.register('UnstackableRepositoryFormat',
 
4172
    lambda err: errors.UnstackableRepositoryFormat(*err.error_args))
 
4173
no_context_error_translators.register('FileExists',
 
4174
    lambda err: errors.FileExists(err.error_args[0]))
 
4175
no_context_error_translators.register('DirectoryNotEmpty',
 
4176
    lambda err: errors.DirectoryNotEmpty(err.error_args[0]))
 
4177
 
 
4178
def _translate_short_readv_error(err):
 
4179
    args = err.error_args
 
4180
    return errors.ShortReadvError(args[0], int(args[1]), int(args[2]),
 
4181
        int(args[3]))
 
4182
 
 
4183
no_context_error_translators.register('ShortReadvError',
 
4184
    _translate_short_readv_error)
 
4185
 
 
4186
def _translate_unicode_error(err):
2990
4187
        encoding = str(err.error_args[0]) # encoding must always be a string
2991
4188
        val = err.error_args[1]
2992
4189
        start = int(err.error_args[2])
3000
4197
            raise UnicodeDecodeError(encoding, val, start, end, reason)
3001
4198
        elif err.error_verb == 'UnicodeEncodeError':
3002
4199
            raise UnicodeEncodeError(encoding, val, start, end, reason)
3003
 
    elif err.error_verb == 'ReadOnlyError':
3004
 
        raise errors.TransportNotPossible('readonly transport')
3005
 
    raise errors.UnknownErrorFromSmartServer(err)
 
4200
 
 
4201
no_context_error_translators.register('UnicodeEncodeError',
 
4202
    _translate_unicode_error)
 
4203
no_context_error_translators.register('UnicodeDecodeError',
 
4204
    _translate_unicode_error)
 
4205
no_context_error_translators.register('ReadOnlyError',
 
4206
    lambda err: errors.TransportNotPossible('readonly transport'))
 
4207
no_context_error_translators.register('MemoryError',
 
4208
    lambda err: errors.BzrError("remote server out of memory\n"
 
4209
        "Retry non-remotely, or contact the server admin for details."))
 
4210
no_context_error_translators.register('RevisionNotPresent',
 
4211
    lambda err: errors.RevisionNotPresent(err.error_args[0], err.error_args[1]))
 
4212
 
 
4213
no_context_error_translators.register('BzrCheckError',
 
4214
    lambda err: errors.BzrCheckError(msg=err.error_args[0]))
 
4215