~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/remote.py

  • Committer: John Arbash Meinel
  • Date: 2008-11-25 17:15:26 UTC
  • mto: This revision was merged to the branch mainline in revision 3851.
  • Revision ID: john@arbash-meinel.com-20081125171526-pi2g4m1w70pkie1f
Add a bit of help text when supplying --help.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
# TODO: At some point, handle upgrades by just passing the whole request
18
18
# across to run on the server.
21
21
 
22
22
from bzrlib import (
23
23
    branch,
24
 
    bzrdir,
25
 
    config,
26
24
    debug,
27
25
    errors,
28
26
    graph,
29
27
    lockdir,
30
 
    pack,
31
28
    repository,
32
29
    revision,
33
 
    revision as _mod_revision,
34
30
    symbol_versioning,
35
31
    urlutils,
36
32
)
42
38
    SmartProtocolError,
43
39
    )
44
40
from bzrlib.lockable_files import LockableFiles
45
 
from bzrlib.smart import client, vfs, repository as smart_repo
 
41
from bzrlib.smart import client, vfs
46
42
from bzrlib.revision import ensure_null, NULL_REVISION
47
43
from bzrlib.trace import mutter, note, warning
48
 
from bzrlib.util import bencode
49
44
 
50
45
 
51
46
class _RpcHelper(object):
56
51
            return self._client.call(method, *args)
57
52
        except errors.ErrorFromSmartServer, err:
58
53
            self._translate_error(err, **err_context)
59
 
 
 
54
        
60
55
    def _call_expecting_body(self, method, *args, **err_context):
61
56
        try:
62
57
            return self._client.call_expecting_body(method, *args)
63
58
        except errors.ErrorFromSmartServer, err:
64
59
            self._translate_error(err, **err_context)
65
 
 
 
60
        
66
61
    def _call_with_body_bytes_expecting_body(self, method, args, body_bytes,
67
62
                                             **err_context):
68
63
        try:
70
65
                method, args, body_bytes)
71
66
        except errors.ErrorFromSmartServer, err:
72
67
            self._translate_error(err, **err_context)
73
 
 
74
 
 
75
 
def response_tuple_to_repo_format(response):
76
 
    """Convert a response tuple describing a repository format to a format."""
77
 
    format = RemoteRepositoryFormat()
78
 
    format._rich_root_data = (response[0] == 'yes')
79
 
    format._supports_tree_reference = (response[1] == 'yes')
80
 
    format._supports_external_lookups = (response[2] == 'yes')
81
 
    format._network_name = response[3]
82
 
    return format
83
 
 
84
 
 
 
68
        
85
69
# Note: RemoteBzrDirFormat is in bzrdir.py
86
70
 
87
71
class RemoteBzrDir(BzrDir, _RpcHelper):
88
72
    """Control directory on a remote server, accessed via bzr:// or similar."""
89
73
 
90
 
    def __init__(self, transport, format, _client=None):
 
74
    def __init__(self, transport, _client=None):
91
75
        """Construct a RemoteBzrDir.
92
76
 
93
77
        :param _client: Private parameter for testing. Disables probing and the
94
78
            use of a real bzrdir.
95
79
        """
96
 
        BzrDir.__init__(self, transport, format)
 
80
        BzrDir.__init__(self, transport, RemoteBzrDirFormat())
97
81
        # this object holds a delegated bzrdir that uses file-level operations
98
82
        # to talk to the other side
99
83
        self._real_bzrdir = None
100
 
        # 1-shot cache for the call pattern 'create_branch; open_branch' - see
101
 
        # create_branch for details.
102
 
        self._next_open_branch_result = None
103
84
 
104
85
        if _client is None:
105
86
            medium = transport.get_smart_medium()
123
104
        if not self._real_bzrdir:
124
105
            self._real_bzrdir = BzrDir.open_from_transport(
125
106
                self.root_transport, _server_formats=False)
126
 
            self._format._network_name = \
127
 
                self._real_bzrdir._format.network_name()
128
107
 
129
108
    def _translate_error(self, err, **context):
130
109
        _translate_error(err, bzrdir=self, **context)
131
110
 
132
 
    def break_lock(self):
133
 
        # Prevent aliasing problems in the next_open_branch_result cache.
134
 
        # See create_branch for rationale.
135
 
        self._next_open_branch_result = None
136
 
        return BzrDir.break_lock(self)
137
 
 
138
 
    def _vfs_cloning_metadir(self, require_stacking=False):
 
111
    def cloning_metadir(self, stacked=False):
139
112
        self._ensure_real()
140
 
        return self._real_bzrdir.cloning_metadir(
141
 
            require_stacking=require_stacking)
142
 
 
143
 
    def cloning_metadir(self, require_stacking=False):
144
 
        medium = self._client._medium
145
 
        if medium._is_remote_before((1, 13)):
146
 
            return self._vfs_cloning_metadir(require_stacking=require_stacking)
147
 
        verb = 'BzrDir.cloning_metadir'
148
 
        if require_stacking:
149
 
            stacking = 'True'
150
 
        else:
151
 
            stacking = 'False'
152
 
        path = self._path_for_remote_call(self._client)
153
 
        try:
154
 
            response = self._call(verb, path, stacking)
155
 
        except errors.UnknownSmartMethod:
156
 
            medium._remember_remote_is_before((1, 13))
157
 
            return self._vfs_cloning_metadir(require_stacking=require_stacking)
158
 
        except errors.UnknownErrorFromSmartServer, err:
159
 
            if err.error_tuple != ('BranchReference',):
160
 
                raise
161
 
            # We need to resolve the branch reference to determine the
162
 
            # cloning_metadir.  This causes unnecessary RPCs to open the
163
 
            # referenced branch (and bzrdir, etc) but only when the caller
164
 
            # didn't already resolve the branch reference.
165
 
            referenced_branch = self.open_branch()
166
 
            return referenced_branch.bzrdir.cloning_metadir()
167
 
        if len(response) != 3:
168
 
            raise errors.UnexpectedSmartServerResponse(response)
169
 
        control_name, repo_name, branch_info = response
170
 
        if len(branch_info) != 2:
171
 
            raise errors.UnexpectedSmartServerResponse(response)
172
 
        branch_ref, branch_name = branch_info
173
 
        format = bzrdir.network_format_registry.get(control_name)
174
 
        if repo_name:
175
 
            format.repository_format = repository.network_format_registry.get(
176
 
                repo_name)
177
 
        if branch_ref == 'ref':
178
 
            # XXX: we need possible_transports here to avoid reopening the
179
 
            # connection to the referenced location
180
 
            ref_bzrdir = BzrDir.open(branch_name)
181
 
            branch_format = ref_bzrdir.cloning_metadir().get_branch_format()
182
 
            format.set_branch_format(branch_format)
183
 
        elif branch_ref == 'branch':
184
 
            if branch_name:
185
 
                format.set_branch_format(
186
 
                    branch.network_format_registry.get(branch_name))
187
 
        else:
188
 
            raise errors.UnexpectedSmartServerResponse(response)
189
 
        return format
 
113
        return self._real_bzrdir.cloning_metadir(stacked)
190
114
 
191
115
    def create_repository(self, shared=False):
192
 
        # as per meta1 formats - just delegate to the format object which may
193
 
        # be parameterised.
194
 
        result = self._format.repository_format.initialize(self, shared)
195
 
        if not isinstance(result, RemoteRepository):
196
 
            return self.open_repository()
197
 
        else:
198
 
            return result
 
116
        self._ensure_real()
 
117
        self._real_bzrdir.create_repository(shared=shared)
 
118
        return self.open_repository()
199
119
 
200
120
    def destroy_repository(self):
201
121
        """See BzrDir.destroy_repository"""
203
123
        self._real_bzrdir.destroy_repository()
204
124
 
205
125
    def create_branch(self):
206
 
        # as per meta1 formats - just delegate to the format object which may
207
 
        # be parameterised.
208
 
        real_branch = self._format.get_branch_format().initialize(self)
209
 
        if not isinstance(real_branch, RemoteBranch):
210
 
            result = RemoteBranch(self, self.find_repository(), real_branch)
211
 
        else:
212
 
            result = real_branch
213
 
        # BzrDir.clone_on_transport() uses the result of create_branch but does
214
 
        # not return it to its callers; we save approximately 8% of our round
215
 
        # trips by handing the branch we created back to the first caller to
216
 
        # open_branch rather than probing anew. Long term we need a API in
217
 
        # bzrdir that doesn't discard result objects (like result_branch).
218
 
        # RBC 20090225
219
 
        self._next_open_branch_result = result
220
 
        return result
 
126
        self._ensure_real()
 
127
        real_branch = self._real_bzrdir.create_branch()
 
128
        return RemoteBranch(self, self.find_repository(), real_branch)
221
129
 
222
130
    def destroy_branch(self):
223
131
        """See BzrDir.destroy_branch"""
224
132
        self._ensure_real()
225
133
        self._real_bzrdir.destroy_branch()
226
 
        self._next_open_branch_result = None
227
134
 
228
135
    def create_workingtree(self, revision_id=None, from_branch=None):
229
136
        raise errors.NotLocalUrl(self.transport.base)
238
145
 
239
146
    def get_branch_reference(self):
240
147
        """See BzrDir.get_branch_reference()."""
241
 
        response = self._get_branch_reference()
242
 
        if response[0] == 'ref':
243
 
            return response[1]
244
 
        else:
245
 
            return None
246
 
 
247
 
    def _get_branch_reference(self):
248
148
        path = self._path_for_remote_call(self._client)
249
 
        medium = self._client._medium
250
 
        if not medium._is_remote_before((1, 13)):
251
 
            try:
252
 
                response = self._call('BzrDir.open_branchV2', path)
253
 
                if response[0] not in ('ref', 'branch'):
254
 
                    raise errors.UnexpectedSmartServerResponse(response)
255
 
                return response
256
 
            except errors.UnknownSmartMethod:
257
 
                medium._remember_remote_is_before((1, 13))
258
149
        response = self._call('BzrDir.open_branch', path)
259
 
        if response[0] != 'ok':
 
150
        if response[0] == 'ok':
 
151
            if response[1] == '':
 
152
                # branch at this location.
 
153
                return None
 
154
            else:
 
155
                # a branch reference, use the existing BranchReference logic.
 
156
                return response[1]
 
157
        else:
260
158
            raise errors.UnexpectedSmartServerResponse(response)
261
 
        if response[1] != '':
262
 
            return ('ref', response[1])
263
 
        else:
264
 
            return ('branch', '')
265
159
 
266
160
    def _get_tree_branch(self):
267
161
        """See BzrDir._get_tree_branch()."""
268
162
        return None, self.open_branch()
269
163
 
270
 
    def open_branch(self, _unsupported=False, ignore_fallbacks=False):
 
164
    def open_branch(self, _unsupported=False):
271
165
        if _unsupported:
272
166
            raise NotImplementedError('unsupported flag support not implemented yet.')
273
 
        if self._next_open_branch_result is not None:
274
 
            # See create_branch for details.
275
 
            result = self._next_open_branch_result
276
 
            self._next_open_branch_result = None
277
 
            return result
278
 
        response = self._get_branch_reference()
279
 
        if response[0] == 'ref':
 
167
        reference_url = self.get_branch_reference()
 
168
        if reference_url is None:
 
169
            # branch at this location.
 
170
            return RemoteBranch(self, self.find_repository())
 
171
        else:
280
172
            # a branch reference, use the existing BranchReference logic.
281
173
            format = BranchReferenceFormat()
282
 
            return format.open(self, _found=True, location=response[1],
283
 
                ignore_fallbacks=ignore_fallbacks)
284
 
        branch_format_name = response[1]
285
 
        if not branch_format_name:
286
 
            branch_format_name = None
287
 
        format = RemoteBranchFormat(network_name=branch_format_name)
288
 
        return RemoteBranch(self, self.find_repository(), format=format,
289
 
            setup_stacking=not ignore_fallbacks)
290
 
 
291
 
    def _open_repo_v1(self, path):
292
 
        verb = 'BzrDir.find_repository'
293
 
        response = self._call(verb, path)
294
 
        if response[0] != 'ok':
295
 
            raise errors.UnexpectedSmartServerResponse(response)
296
 
        # servers that only support the v1 method don't support external
297
 
        # references either.
298
 
        self._ensure_real()
299
 
        repo = self._real_bzrdir.open_repository()
300
 
        response = response + ('no', repo._format.network_name())
301
 
        return response, repo
302
 
 
303
 
    def _open_repo_v2(self, path):
 
174
            return format.open(self, _found=True, location=reference_url)
 
175
                
 
176
    def open_repository(self):
 
177
        path = self._path_for_remote_call(self._client)
304
178
        verb = 'BzrDir.find_repositoryV2'
305
 
        response = self._call(verb, path)
306
 
        if response[0] != 'ok':
307
 
            raise errors.UnexpectedSmartServerResponse(response)
308
 
        self._ensure_real()
309
 
        repo = self._real_bzrdir.open_repository()
310
 
        response = response + (repo._format.network_name(),)
311
 
        return response, repo
312
 
 
313
 
    def _open_repo_v3(self, path):
314
 
        verb = 'BzrDir.find_repositoryV3'
315
 
        medium = self._client._medium
316
 
        if medium._is_remote_before((1, 13)):
317
 
            raise errors.UnknownSmartMethod(verb)
318
179
        try:
319
180
            response = self._call(verb, path)
320
181
        except errors.UnknownSmartMethod:
321
 
            medium._remember_remote_is_before((1, 13))
322
 
            raise
323
 
        if response[0] != 'ok':
324
 
            raise errors.UnexpectedSmartServerResponse(response)
325
 
        return response, None
326
 
 
327
 
    def open_repository(self):
328
 
        path = self._path_for_remote_call(self._client)
329
 
        response = None
330
 
        for probe in [self._open_repo_v3, self._open_repo_v2,
331
 
            self._open_repo_v1]:
332
 
            try:
333
 
                response, real_repo = probe(path)
334
 
                break
335
 
            except errors.UnknownSmartMethod:
336
 
                pass
337
 
        if response is None:
338
 
            raise errors.UnknownSmartMethod('BzrDir.find_repository{3,2,}')
339
 
        if response[0] != 'ok':
340
 
            raise errors.UnexpectedSmartServerResponse(response)
341
 
        if len(response) != 6:
 
182
            verb = 'BzrDir.find_repository'
 
183
            response = self._call(verb, path)
 
184
        if response[0] != 'ok':
 
185
            raise errors.UnexpectedSmartServerResponse(response)
 
186
        if verb == 'BzrDir.find_repository':
 
187
            # servers that don't support the V2 method don't support external
 
188
            # references either.
 
189
            response = response + ('no', )
 
190
        if not (len(response) == 5):
342
191
            raise SmartProtocolError('incorrect response length %s' % (response,))
343
192
        if response[1] == '':
344
 
            # repo is at this dir.
345
 
            format = response_tuple_to_repo_format(response[2:])
 
193
            format = RemoteRepositoryFormat()
 
194
            format.rich_root_data = (response[2] == 'yes')
 
195
            format.supports_tree_reference = (response[3] == 'yes')
 
196
            # No wire format to check this yet.
 
197
            format.supports_external_lookups = (response[4] == 'yes')
346
198
            # Used to support creating a real format instance when needed.
347
199
            format._creating_bzrdir = self
348
 
            remote_repo = RemoteRepository(self, format)
349
 
            format._creating_repo = remote_repo
350
 
            if real_repo is not None:
351
 
                remote_repo._set_real_repository(real_repo)
352
 
            return remote_repo
 
200
            return RemoteRepository(self, format)
353
201
        else:
354
202
            raise errors.NoRepositoryPresent(self)
355
203
 
382
230
 
383
231
    def needs_format_conversion(self, format=None):
384
232
        """Upgrading of remote bzrdirs is not supported yet."""
385
 
        if format is None:
386
 
            symbol_versioning.warn(symbol_versioning.deprecated_in((1, 13, 0))
387
 
                % 'needs_format_conversion(format=None)')
388
233
        return False
389
234
 
390
235
    def clone(self, url, revision_id=None, force_new_repo=False,
393
238
        return self._real_bzrdir.clone(url, revision_id=revision_id,
394
239
            force_new_repo=force_new_repo, preserve_stacking=preserve_stacking)
395
240
 
396
 
    def _get_config(self):
397
 
        return RemoteBzrDirConfig(self)
 
241
    def get_config(self):
 
242
        self._ensure_real()
 
243
        return self._real_bzrdir.get_config()
398
244
 
399
245
 
400
246
class RemoteRepositoryFormat(repository.RepositoryFormat):
408
254
    the attributes rich_root_data and supports_tree_reference are set
409
255
    on a per instance basis, and are not set (and should not be) at
410
256
    the class level.
411
 
 
412
 
    :ivar _custom_format: If set, a specific concrete repository format that
413
 
        will be used when initializing a repository with this
414
 
        RemoteRepositoryFormat.
415
 
    :ivar _creating_repo: If set, the repository object that this
416
 
        RemoteRepositoryFormat was created for: it can be called into
417
 
        to obtain data like the network name.
418
257
    """
419
258
 
420
259
    _matchingbzrdir = RemoteBzrDirFormat()
421
260
 
422
 
    def __init__(self):
423
 
        repository.RepositoryFormat.__init__(self)
424
 
        self._custom_format = None
425
 
        self._network_name = None
426
 
        self._creating_bzrdir = None
427
 
        self._supports_external_lookups = None
428
 
        self._supports_tree_reference = None
429
 
        self._rich_root_data = None
430
 
 
431
 
    @property
432
 
    def fast_deltas(self):
433
 
        self._ensure_real()
434
 
        return self._custom_format.fast_deltas
435
 
 
436
 
    @property
437
 
    def rich_root_data(self):
438
 
        if self._rich_root_data is None:
439
 
            self._ensure_real()
440
 
            self._rich_root_data = self._custom_format.rich_root_data
441
 
        return self._rich_root_data
442
 
 
443
 
    @property
444
 
    def supports_external_lookups(self):
445
 
        if self._supports_external_lookups is None:
446
 
            self._ensure_real()
447
 
            self._supports_external_lookups = \
448
 
                self._custom_format.supports_external_lookups
449
 
        return self._supports_external_lookups
450
 
 
451
 
    @property
452
 
    def supports_tree_reference(self):
453
 
        if self._supports_tree_reference is None:
454
 
            self._ensure_real()
455
 
            self._supports_tree_reference = \
456
 
                self._custom_format.supports_tree_reference
457
 
        return self._supports_tree_reference
458
 
 
459
 
    def _vfs_initialize(self, a_bzrdir, shared):
460
 
        """Helper for common code in initialize."""
461
 
        if self._custom_format:
462
 
            # Custom format requested
463
 
            result = self._custom_format.initialize(a_bzrdir, shared=shared)
464
 
        elif self._creating_bzrdir is not None:
465
 
            # Use the format that the repository we were created to back
466
 
            # has.
 
261
    def initialize(self, a_bzrdir, shared=False):
 
262
        if not isinstance(a_bzrdir, RemoteBzrDir):
467
263
            prior_repo = self._creating_bzrdir.open_repository()
468
264
            prior_repo._ensure_real()
469
 
            result = prior_repo._real_repository._format.initialize(
 
265
            return prior_repo._real_repository._format.initialize(
470
266
                a_bzrdir, shared=shared)
471
 
        else:
472
 
            # assume that a_bzr is a RemoteBzrDir but the smart server didn't
473
 
            # support remote initialization.
474
 
            # We delegate to a real object at this point (as RemoteBzrDir
475
 
            # delegate to the repository format which would lead to infinite
476
 
            # recursion if we just called a_bzrdir.create_repository.
477
 
            a_bzrdir._ensure_real()
478
 
            result = a_bzrdir._real_bzrdir.create_repository(shared=shared)
479
 
        if not isinstance(result, RemoteRepository):
480
 
            return self.open(a_bzrdir)
481
 
        else:
482
 
            return result
483
 
 
484
 
    def initialize(self, a_bzrdir, shared=False):
485
 
        # Being asked to create on a non RemoteBzrDir:
486
 
        if not isinstance(a_bzrdir, RemoteBzrDir):
487
 
            return self._vfs_initialize(a_bzrdir, shared)
488
 
        medium = a_bzrdir._client._medium
489
 
        if medium._is_remote_before((1, 13)):
490
 
            return self._vfs_initialize(a_bzrdir, shared)
491
 
        # Creating on a remote bzr dir.
492
 
        # 1) get the network name to use.
493
 
        if self._custom_format:
494
 
            network_name = self._custom_format.network_name()
495
 
        elif self._network_name:
496
 
            network_name = self._network_name
497
 
        else:
498
 
            # Select the current bzrlib default and ask for that.
499
 
            reference_bzrdir_format = bzrdir.format_registry.get('default')()
500
 
            reference_format = reference_bzrdir_format.repository_format
501
 
            network_name = reference_format.network_name()
502
 
        # 2) try direct creation via RPC
503
 
        path = a_bzrdir._path_for_remote_call(a_bzrdir._client)
504
 
        verb = 'BzrDir.create_repository'
505
 
        if shared:
506
 
            shared_str = 'True'
507
 
        else:
508
 
            shared_str = 'False'
509
 
        try:
510
 
            response = a_bzrdir._call(verb, path, network_name, shared_str)
511
 
        except errors.UnknownSmartMethod:
512
 
            # Fallback - use vfs methods
513
 
            medium._remember_remote_is_before((1, 13))
514
 
            return self._vfs_initialize(a_bzrdir, shared)
515
 
        else:
516
 
            # Turn the response into a RemoteRepository object.
517
 
            format = response_tuple_to_repo_format(response[1:])
518
 
            # Used to support creating a real format instance when needed.
519
 
            format._creating_bzrdir = a_bzrdir
520
 
            remote_repo = RemoteRepository(a_bzrdir, format)
521
 
            format._creating_repo = remote_repo
522
 
            return remote_repo
523
 
 
 
267
        return a_bzrdir.create_repository(shared=shared)
 
268
    
524
269
    def open(self, a_bzrdir):
525
270
        if not isinstance(a_bzrdir, RemoteBzrDir):
526
271
            raise AssertionError('%r is not a RemoteBzrDir' % (a_bzrdir,))
527
272
        return a_bzrdir.open_repository()
528
273
 
529
 
    def _ensure_real(self):
530
 
        if self._custom_format is None:
531
 
            self._custom_format = repository.network_format_registry.get(
532
 
                self._network_name)
533
 
 
534
 
    @property
535
 
    def _fetch_order(self):
536
 
        self._ensure_real()
537
 
        return self._custom_format._fetch_order
538
 
 
539
 
    @property
540
 
    def _fetch_uses_deltas(self):
541
 
        self._ensure_real()
542
 
        return self._custom_format._fetch_uses_deltas
543
 
 
544
 
    @property
545
 
    def _fetch_reconcile(self):
546
 
        self._ensure_real()
547
 
        return self._custom_format._fetch_reconcile
548
 
 
549
274
    def get_format_description(self):
550
275
        return 'bzr remote repository'
551
276
 
552
277
    def __eq__(self, other):
553
 
        return self.__class__ is other.__class__
 
278
        return self.__class__ == other.__class__
554
279
 
555
280
    def check_conversion_target(self, target_format):
556
281
        if self.rich_root_data and not target_format.rich_root_data:
561
286
            raise errors.BadConversionTarget(
562
287
                'Does not support nested trees', target_format)
563
288
 
564
 
    def network_name(self):
565
 
        if self._network_name:
566
 
            return self._network_name
567
 
        self._creating_repo._ensure_real()
568
 
        return self._creating_repo._real_repository._format.network_name()
569
 
 
570
 
    @property
571
 
    def _serializer(self):
572
 
        self._ensure_real()
573
 
        return self._custom_format._serializer
574
 
 
575
289
 
576
290
class RemoteRepository(_RpcHelper):
577
291
    """Repository accessed over rpc.
582
296
 
583
297
    def __init__(self, remote_bzrdir, format, real_repository=None, _client=None):
584
298
        """Create a RemoteRepository instance.
585
 
 
 
299
        
586
300
        :param remote_bzrdir: The bzrdir hosting this repository.
587
301
        :param format: The RemoteFormat object to use.
588
302
        :param real_repository: If not None, a local implementation of the
605
319
        self._lock_token = None
606
320
        self._lock_count = 0
607
321
        self._leave_lock = False
608
 
        # Cache of revision parents; misses are cached during read locks, and
609
 
        # write locks when no _real_repository has been set.
610
 
        self._unstacked_provider = graph.CachingParentsProvider(
611
 
            get_parent_map=self._get_parent_map_rpc)
612
 
        self._unstacked_provider.disable_cache()
 
322
        # A cache of looked up revision parent data; reset at unlock time.
 
323
        self._parents_map = None
 
324
        if 'hpss' in debug.debug_flags:
 
325
            self._requested_parents = None
613
326
        # For tests:
614
327
        # These depend on the actual remote format, so force them off for
615
328
        # maximum compatibility. XXX: In future these should depend on the
627
340
 
628
341
    __repr__ = __str__
629
342
 
630
 
    def abort_write_group(self, suppress_errors=False):
 
343
    def abort_write_group(self):
631
344
        """Complete a write group on the decorated repository.
632
 
 
633
 
        Smart methods perform operations in a single step so this API
 
345
        
 
346
        Smart methods peform operations in a single step so this api
634
347
        is not really applicable except as a compatibility thunk
635
348
        for older plugins that don't use e.g. the CommitBuilder
636
349
        facility.
637
 
 
638
 
        :param suppress_errors: see Repository.abort_write_group.
639
 
        """
640
 
        self._ensure_real()
641
 
        return self._real_repository.abort_write_group(
642
 
            suppress_errors=suppress_errors)
643
 
 
644
 
    @property
645
 
    def chk_bytes(self):
646
 
        """Decorate the real repository for now.
647
 
 
648
 
        In the long term a full blown network facility is needed to avoid
649
 
        creating a real repository object locally.
650
 
        """
651
 
        self._ensure_real()
652
 
        return self._real_repository.chk_bytes
 
350
        """
 
351
        self._ensure_real()
 
352
        return self._real_repository.abort_write_group()
653
353
 
654
354
    def commit_write_group(self):
655
355
        """Complete a write group on the decorated repository.
656
 
 
657
 
        Smart methods perform operations in a single step so this API
 
356
        
 
357
        Smart methods peform operations in a single step so this api
658
358
        is not really applicable except as a compatibility thunk
659
359
        for older plugins that don't use e.g. the CommitBuilder
660
360
        facility.
662
362
        self._ensure_real()
663
363
        return self._real_repository.commit_write_group()
664
364
 
665
 
    def resume_write_group(self, tokens):
666
 
        self._ensure_real()
667
 
        return self._real_repository.resume_write_group(tokens)
668
 
 
669
 
    def suspend_write_group(self):
670
 
        self._ensure_real()
671
 
        return self._real_repository.suspend_write_group()
672
 
 
673
 
    def get_missing_parent_inventories(self):
674
 
        self._ensure_real()
675
 
        return self._real_repository.get_missing_parent_inventories()
676
 
 
677
365
    def _ensure_real(self):
678
366
        """Ensure that there is a _real_repository set.
679
367
 
680
368
        Used before calls to self._real_repository.
681
 
 
682
 
        Note that _ensure_real causes many roundtrips to the server which are
683
 
        not desirable, and prevents the use of smart one-roundtrip RPC's to
684
 
        perform complex operations (such as accessing parent data, streaming
685
 
        revisions etc). Adding calls to _ensure_real should only be done when
686
 
        bringing up new functionality, adding fallbacks for smart methods that
687
 
        require a fallback path, and never to replace an existing smart method
688
 
        invocation. If in doubt chat to the bzr network team.
689
369
        """
690
370
        if self._real_repository is None:
691
 
            self._unstacked_provider.missing_keys.clear()
692
371
            self.bzrdir._ensure_real()
693
372
            self._set_real_repository(
694
373
                self.bzrdir._real_bzrdir.open_repository())
721
400
        self._ensure_real()
722
401
        return self._real_repository._generate_text_key_index()
723
402
 
 
403
    @symbol_versioning.deprecated_method(symbol_versioning.one_four)
 
404
    def get_revision_graph(self, revision_id=None):
 
405
        """See Repository.get_revision_graph()."""
 
406
        return self._get_revision_graph(revision_id)
 
407
 
724
408
    def _get_revision_graph(self, revision_id):
725
409
        """Private method for using with old (< 1.2) servers to fallback."""
726
410
        if revision_id is None:
743
427
        for line in lines:
744
428
            d = tuple(line.split())
745
429
            revision_graph[d[0]] = d[1:]
746
 
 
 
430
            
747
431
        return revision_graph
748
432
 
749
 
    def _get_sink(self):
750
 
        """See Repository._get_sink()."""
751
 
        return RemoteStreamSink(self)
752
 
 
753
 
    def _get_source(self, to_format):
754
 
        """Return a source for streaming from this repository."""
755
 
        return RemoteStreamSource(self, to_format)
756
 
 
757
 
    @needs_read_lock
758
433
    def has_revision(self, revision_id):
759
 
        """True if this repository has a copy of the revision."""
760
 
        # Copy of bzrlib.repository.Repository.has_revision
761
 
        return revision_id in self.has_revisions((revision_id,))
 
434
        """See Repository.has_revision()."""
 
435
        if revision_id == NULL_REVISION:
 
436
            # The null revision is always present.
 
437
            return True
 
438
        path = self.bzrdir._path_for_remote_call(self._client)
 
439
        response = self._call('Repository.has_revision', path, revision_id)
 
440
        if response[0] not in ('yes', 'no'):
 
441
            raise errors.UnexpectedSmartServerResponse(response)
 
442
        if response[0] == 'yes':
 
443
            return True
 
444
        for fallback_repo in self._fallback_repositories:
 
445
            if fallback_repo.has_revision(revision_id):
 
446
                return True
 
447
        return False
762
448
 
763
 
    @needs_read_lock
764
449
    def has_revisions(self, revision_ids):
765
 
        """Probe to find out the presence of multiple revisions.
766
 
 
767
 
        :param revision_ids: An iterable of revision_ids.
768
 
        :return: A set of the revision_ids that were present.
769
 
        """
770
 
        # Copy of bzrlib.repository.Repository.has_revisions
771
 
        parent_map = self.get_parent_map(revision_ids)
772
 
        result = set(parent_map)
773
 
        if _mod_revision.NULL_REVISION in revision_ids:
774
 
            result.add(_mod_revision.NULL_REVISION)
 
450
        """See Repository.has_revisions()."""
 
451
        # FIXME: This does many roundtrips, particularly when there are
 
452
        # fallback repositories.  -- mbp 20080905
 
453
        result = set()
 
454
        for revision_id in revision_ids:
 
455
            if self.has_revision(revision_id):
 
456
                result.add(revision_id)
775
457
        return result
776
458
 
777
459
    def has_same_location(self, other):
778
 
        return (self.__class__ is other.__class__ and
 
460
        return (self.__class__ == other.__class__ and
779
461
                self.bzrdir.transport.base == other.bzrdir.transport.base)
780
 
 
 
462
        
781
463
    def get_graph(self, other_repository=None):
782
464
        """Return the graph for this repository format"""
783
 
        parents_provider = self._make_parents_provider(other_repository)
 
465
        parents_provider = self
 
466
        if (other_repository is not None and
 
467
            other_repository.bzrdir.transport.base !=
 
468
            self.bzrdir.transport.base):
 
469
            parents_provider = graph._StackedParentsProvider(
 
470
                [parents_provider, other_repository._make_parents_provider()])
784
471
        return graph.Graph(parents_provider)
785
472
 
786
473
    def gather_stats(self, revid=None, committers=None):
853
540
        if not self._lock_mode:
854
541
            self._lock_mode = 'r'
855
542
            self._lock_count = 1
856
 
            self._unstacked_provider.enable_cache(cache_misses=True)
 
543
            self._parents_map = {}
 
544
            if 'hpss' in debug.debug_flags:
 
545
                self._requested_parents = set()
857
546
            if self._real_repository is not None:
858
547
                self._real_repository.lock_read()
859
548
        else:
860
549
            self._lock_count += 1
861
 
        for repo in self._fallback_repositories:
862
 
            repo.lock_read()
863
550
 
864
551
    def _remote_lock_write(self, token):
865
552
        path = self.bzrdir._path_for_remote_call(self._client)
895
582
                self._leave_lock = False
896
583
            self._lock_mode = 'w'
897
584
            self._lock_count = 1
898
 
            cache_misses = self._real_repository is None
899
 
            self._unstacked_provider.enable_cache(cache_misses=cache_misses)
 
585
            self._parents_map = {}
 
586
            if 'hpss' in debug.debug_flags:
 
587
                self._requested_parents = set()
900
588
        elif self._lock_mode == 'r':
901
589
            raise errors.ReadOnlyError(self)
902
590
        else:
903
591
            self._lock_count += 1
904
 
        for repo in self._fallback_repositories:
905
 
            # Writes don't affect fallback repos
906
 
            repo.lock_read()
907
592
        return self._lock_token or None
908
593
 
909
594
    def leave_lock_in_place(self):
923
608
            implemented operations.
924
609
        """
925
610
        if self._real_repository is not None:
926
 
            # Replacing an already set real repository.
927
 
            # We cannot do this [currently] if the repository is locked -
928
 
            # synchronised state might be lost.
929
 
            if self.is_locked():
930
 
                raise AssertionError('_real_repository is already set')
 
611
            raise AssertionError('_real_repository is already set')
931
612
        if isinstance(repository, RemoteRepository):
932
613
            raise AssertionError()
933
614
        self._real_repository = repository
934
 
        # three code paths happen here:
935
 
        # 1) old servers, RemoteBranch.open() calls _ensure_real before setting
936
 
        # up stacking. In this case self._fallback_repositories is [], and the
937
 
        # real repo is already setup. Preserve the real repo and
938
 
        # RemoteRepository.add_fallback_repository will avoid adding
939
 
        # duplicates.
940
 
        # 2) new servers, RemoteBranch.open() sets up stacking, and when
941
 
        # ensure_real is triggered from a branch, the real repository to
942
 
        # set already has a matching list with separate instances, but
943
 
        # as they are also RemoteRepositories we don't worry about making the
944
 
        # lists be identical.
945
 
        # 3) new servers, RemoteRepository.ensure_real is triggered before
946
 
        # RemoteBranch.ensure real, in this case we get a repo with no fallbacks
947
 
        # and need to populate it.
948
 
        if (self._fallback_repositories and
949
 
            len(self._real_repository._fallback_repositories) !=
950
 
            len(self._fallback_repositories)):
951
 
            if len(self._real_repository._fallback_repositories):
952
 
                raise AssertionError(
953
 
                    "cannot cleanly remove existing _fallback_repositories")
954
615
        for fb in self._fallback_repositories:
955
616
            self._real_repository.add_fallback_repository(fb)
956
617
        if self._lock_mode == 'w':
962
623
 
963
624
    def start_write_group(self):
964
625
        """Start a write group on the decorated repository.
965
 
 
966
 
        Smart methods perform operations in a single step so this API
 
626
        
 
627
        Smart methods peform operations in a single step so this api
967
628
        is not really applicable except as a compatibility thunk
968
629
        for older plugins that don't use e.g. the CommitBuilder
969
630
        facility.
985
646
            raise errors.UnexpectedSmartServerResponse(response)
986
647
 
987
648
    def unlock(self):
988
 
        if not self._lock_count:
989
 
            raise errors.LockNotHeld(self)
990
649
        self._lock_count -= 1
991
650
        if self._lock_count > 0:
992
651
            return
993
 
        self._unstacked_provider.disable_cache()
 
652
        self._parents_map = None
 
653
        if 'hpss' in debug.debug_flags:
 
654
            self._requested_parents = None
994
655
        old_mode = self._lock_mode
995
656
        self._lock_mode = None
996
657
        try:
1006
667
            # problem releasing the vfs-based lock.
1007
668
            if old_mode == 'w':
1008
669
                # Only write-locked repositories need to make a remote method
1009
 
                # call to perform the unlock.
 
670
                # call to perfom the unlock.
1010
671
                old_token = self._lock_token
1011
672
                self._lock_token = None
1012
673
                if not self._leave_lock:
1019
680
 
1020
681
    def _get_tarball(self, compression):
1021
682
        """Return a TemporaryFile containing a repository tarball.
1022
 
 
 
683
        
1023
684
        Returns None if the server does not support sending tarballs.
1024
685
        """
1025
686
        import tempfile
1071
732
 
1072
733
    def add_fallback_repository(self, repository):
1073
734
        """Add a repository to use for looking up data not held locally.
1074
 
 
 
735
        
1075
736
        :param repository: A repository.
1076
737
        """
1077
 
        if not self._format.supports_external_lookups:
1078
 
            raise errors.UnstackableRepositoryFormat(
1079
 
                self._format.network_name(), self.base)
 
738
        # XXX: At the moment the RemoteRepository will allow fallbacks
 
739
        # unconditionally - however, a _real_repository will usually exist,
 
740
        # and may raise an error if it's not accommodated by the underlying
 
741
        # format.  Eventually we should check when opening the repository
 
742
        # whether it's willing to allow them or not.
 
743
        #
1080
744
        # We need to accumulate additional repositories here, to pass them in
1081
745
        # on various RPC's.
1082
 
        #
1083
746
        self._fallback_repositories.append(repository)
1084
 
        # If self._real_repository was parameterised already (e.g. because a
1085
 
        # _real_branch had its get_stacked_on_url method called), then the
1086
 
        # repository to be added may already be in the _real_repositories list.
1087
 
        if self._real_repository is not None:
1088
 
            fallback_locations = [repo.bzrdir.root_transport.base for repo in
1089
 
                self._real_repository._fallback_repositories]
1090
 
            if repository.bzrdir.root_transport.base not in fallback_locations:
1091
 
                self._real_repository.add_fallback_repository(repository)
 
747
        # They are also seen by the fallback repository.  If it doesn't exist
 
748
        # yet they'll be added then.  This implicitly copies them.
 
749
        self._ensure_real()
1092
750
 
1093
751
    def add_inventory(self, revid, inv, parents):
1094
752
        self._ensure_real()
1095
753
        return self._real_repository.add_inventory(revid, inv, parents)
1096
754
 
1097
 
    def add_inventory_by_delta(self, basis_revision_id, delta, new_revision_id,
1098
 
                               parents):
1099
 
        self._ensure_real()
1100
 
        return self._real_repository.add_inventory_by_delta(basis_revision_id,
1101
 
            delta, new_revision_id, parents)
1102
 
 
1103
755
    def add_revision(self, rev_id, rev, inv=None, config=None):
1104
756
        self._ensure_real()
1105
757
        return self._real_repository.add_revision(
1133
785
        self._ensure_real()
1134
786
        return self._real_repository.make_working_trees()
1135
787
 
1136
 
    def refresh_data(self):
1137
 
        """Re-read any data needed to to synchronise with disk.
1138
 
 
1139
 
        This method is intended to be called after another repository instance
1140
 
        (such as one used by a smart server) has inserted data into the
1141
 
        repository. It may not be called during a write group, but may be
1142
 
        called at any other time.
1143
 
        """
1144
 
        if self.is_in_write_group():
1145
 
            raise errors.InternalBzrError(
1146
 
                "May not refresh_data while in a write group.")
1147
 
        if self._real_repository is not None:
1148
 
            self._real_repository.refresh_data()
1149
 
 
1150
788
    def revision_ids_to_search_result(self, result_set):
1151
789
        """Convert a set of revision ids to a graph SearchResult."""
1152
790
        result_parents = set()
1163
801
    @needs_read_lock
1164
802
    def search_missing_revision_ids(self, other, revision_id=None, find_ghosts=True):
1165
803
        """Return the revision ids that other has that this does not.
1166
 
 
 
804
        
1167
805
        These are returned in topological order.
1168
806
 
1169
807
        revision_id: only return revision ids included by revision_id.
1171
809
        return repository.InterRepository.get(
1172
810
            other, self).search_missing_revision_ids(revision_id, find_ghosts)
1173
811
 
1174
 
    def fetch(self, source, revision_id=None, pb=None, find_ghosts=False,
1175
 
            fetch_spec=None):
1176
 
        # No base implementation to use as RemoteRepository is not a subclass
1177
 
        # of Repository; so this is a copy of Repository.fetch().
1178
 
        if fetch_spec is not None and revision_id is not None:
1179
 
            raise AssertionError(
1180
 
                "fetch_spec and revision_id are mutually exclusive.")
1181
 
        if self.is_in_write_group():
1182
 
            raise errors.InternalBzrError(
1183
 
                "May not fetch while in a write group.")
1184
 
        # fast path same-url fetch operations
1185
 
        if self.has_same_location(source) and fetch_spec is None:
 
812
    def fetch(self, source, revision_id=None, pb=None, find_ghosts=False):
 
813
        # Not delegated to _real_repository so that InterRepository.get has a
 
814
        # chance to find an InterRepository specialised for RemoteRepository.
 
815
        if self.has_same_location(source):
1186
816
            # check that last_revision is in 'from' and then return a
1187
817
            # no-operation.
1188
818
            if (revision_id is not None and
1189
819
                not revision.is_null(revision_id)):
1190
820
                self.get_revision(revision_id)
1191
821
            return 0, []
1192
 
        # if there is no specific appropriate InterRepository, this will get
1193
 
        # the InterRepository base class, which raises an
1194
 
        # IncompatibleRepositories when asked to fetch.
1195
822
        inter = repository.InterRepository.get(source, self)
1196
 
        return inter.fetch(revision_id=revision_id, pb=pb,
1197
 
            find_ghosts=find_ghosts, fetch_spec=fetch_spec)
 
823
        try:
 
824
            return inter.fetch(revision_id=revision_id, pb=pb, find_ghosts=find_ghosts)
 
825
        except NotImplementedError:
 
826
            raise errors.IncompatibleRepositories(source, self)
1198
827
 
1199
828
    def create_bundle(self, target, base, fileobj, format=None):
1200
829
        self._ensure_real()
1213
842
        self._ensure_real()
1214
843
        return self._real_repository._get_versioned_file_checker(
1215
844
            revisions, revision_versions_cache)
1216
 
 
 
845
        
1217
846
    def iter_files_bytes(self, desired_files):
1218
847
        """See Repository.iter_file_bytes.
1219
848
        """
1220
849
        self._ensure_real()
1221
850
        return self._real_repository.iter_files_bytes(desired_files)
1222
851
 
1223
 
    def get_parent_map(self, revision_ids):
 
852
    @property
 
853
    def _fetch_order(self):
 
854
        """Decorate the real repository for now.
 
855
 
 
856
        In the long term getting this back from the remote repository as part
 
857
        of open would be more efficient.
 
858
        """
 
859
        self._ensure_real()
 
860
        return self._real_repository._fetch_order
 
861
 
 
862
    @property
 
863
    def _fetch_uses_deltas(self):
 
864
        """Decorate the real repository for now.
 
865
 
 
866
        In the long term getting this back from the remote repository as part
 
867
        of open would be more efficient.
 
868
        """
 
869
        self._ensure_real()
 
870
        return self._real_repository._fetch_uses_deltas
 
871
 
 
872
    @property
 
873
    def _fetch_reconcile(self):
 
874
        """Decorate the real repository for now.
 
875
 
 
876
        In the long term getting this back from the remote repository as part
 
877
        of open would be more efficient.
 
878
        """
 
879
        self._ensure_real()
 
880
        return self._real_repository._fetch_reconcile
 
881
 
 
882
    def get_parent_map(self, keys):
1224
883
        """See bzrlib.Graph.get_parent_map()."""
1225
 
        return self._make_parents_provider().get_parent_map(revision_ids)
 
884
        # Hack to build up the caching logic.
 
885
        ancestry = self._parents_map
 
886
        if ancestry is None:
 
887
            # Repository is not locked, so there's no cache.
 
888
            missing_revisions = set(keys)
 
889
            ancestry = {}
 
890
        else:
 
891
            missing_revisions = set(key for key in keys if key not in ancestry)
 
892
        if missing_revisions:
 
893
            parent_map = self._get_parent_map(missing_revisions)
 
894
            if 'hpss' in debug.debug_flags:
 
895
                mutter('retransmitted revisions: %d of %d',
 
896
                        len(set(ancestry).intersection(parent_map)),
 
897
                        len(parent_map))
 
898
            ancestry.update(parent_map)
 
899
        present_keys = [k for k in keys if k in ancestry]
 
900
        if 'hpss' in debug.debug_flags:
 
901
            if self._requested_parents is not None and len(ancestry) != 0:
 
902
                self._requested_parents.update(present_keys)
 
903
                mutter('Current RemoteRepository graph hit rate: %d%%',
 
904
                    100.0 * len(self._requested_parents) / len(ancestry))
 
905
        return dict((k, ancestry[k]) for k in present_keys)
1226
906
 
1227
 
    def _get_parent_map_rpc(self, keys):
 
907
    def _get_parent_map(self, keys):
1228
908
        """Helper for get_parent_map that performs the RPC."""
1229
909
        medium = self._client._medium
1230
910
        if medium._is_remote_before((1, 2)):
1231
911
            # We already found out that the server can't understand
1232
912
            # Repository.get_parent_map requests, so just fetch the whole
1233
913
            # graph.
1234
 
            #
1235
 
            # Note that this reads the whole graph, when only some keys are
1236
 
            # wanted.  On this old server there's no way (?) to get them all
1237
 
            # in one go, and the user probably will have seen a warning about
1238
 
            # the server being old anyhow.
1239
 
            rg = self._get_revision_graph(None)
1240
 
            # There is an API discrepancy between get_parent_map and
 
914
            # XXX: Note that this will issue a deprecation warning. This is ok
 
915
            # :- its because we're working with a deprecated server anyway, and
 
916
            # the user will almost certainly have seen a warning about the
 
917
            # server version already.
 
918
            rg = self.get_revision_graph()
 
919
            # There is an api discrepency between get_parent_map and
1241
920
            # get_revision_graph. Specifically, a "key:()" pair in
1242
921
            # get_revision_graph just means a node has no parents. For
1243
922
            # "get_parent_map" it means the node is a ghost. So fix up the
1273
952
        # TODO: Manage this incrementally to avoid covering the same path
1274
953
        # repeatedly. (The server will have to on each request, but the less
1275
954
        # work done the better).
1276
 
        #
1277
 
        # Negative caching notes:
1278
 
        # new server sends missing when a request including the revid
1279
 
        # 'include-missing:' is present in the request.
1280
 
        # missing keys are serialised as missing:X, and we then call
1281
 
        # provider.note_missing(X) for-all X
1282
 
        parents_map = self._unstacked_provider.get_cached_map()
 
955
        parents_map = self._parents_map
1283
956
        if parents_map is None:
1284
957
            # Repository is not locked, so there's no cache.
1285
958
            parents_map = {}
1286
 
        # start_set is all the keys in the cache
1287
959
        start_set = set(parents_map)
1288
 
        # result set is all the references to keys in the cache
1289
960
        result_parents = set()
1290
961
        for parents in parents_map.itervalues():
1291
962
            result_parents.update(parents)
1292
963
        stop_keys = result_parents.difference(start_set)
1293
 
        # We don't need to send ghosts back to the server as a position to
1294
 
        # stop either.
1295
 
        stop_keys.difference_update(self._unstacked_provider.missing_keys)
1296
 
        key_count = len(parents_map)
1297
 
        if (NULL_REVISION in result_parents
1298
 
            and NULL_REVISION in self._unstacked_provider.missing_keys):
1299
 
            # If we pruned NULL_REVISION from the stop_keys because it's also
1300
 
            # in our cache of "missing" keys we need to increment our key count
1301
 
            # by 1, because the reconsitituted SearchResult on the server will
1302
 
            # still consider NULL_REVISION to be an included key.
1303
 
            key_count += 1
1304
964
        included_keys = start_set.intersection(result_parents)
1305
965
        start_set.difference_update(included_keys)
1306
 
        recipe = ('manual', start_set, stop_keys, key_count)
 
966
        recipe = (start_set, stop_keys, len(parents_map))
1307
967
        body = self._serialise_search_recipe(recipe)
1308
968
        path = self.bzrdir._path_for_remote_call(self._client)
1309
969
        for key in keys:
1311
971
                raise ValueError(
1312
972
                    "key %r not a plain string" % (key,))
1313
973
        verb = 'Repository.get_parent_map'
1314
 
        args = (path, 'include-missing:') + tuple(keys)
 
974
        args = (path,) + tuple(keys)
1315
975
        try:
1316
976
            response = self._call_with_body_bytes_expecting_body(
1317
977
                verb, args, body)
1327
987
            # To avoid having to disconnect repeatedly, we keep track of the
1328
988
            # fact the server doesn't understand remote methods added in 1.2.
1329
989
            medium._remember_remote_is_before((1, 2))
1330
 
            # Recurse just once and we should use the fallback code.
1331
 
            return self._get_parent_map_rpc(keys)
 
990
            return self.get_revision_graph(None)
1332
991
        response_tuple, response_handler = response
1333
992
        if response_tuple[0] not in ['ok']:
1334
993
            response_handler.cancel_read_body()
1345
1004
                if len(d) > 1:
1346
1005
                    revision_graph[d[0]] = d[1:]
1347
1006
                else:
1348
 
                    # No parents:
1349
 
                    if d[0].startswith('missing:'):
1350
 
                        revid = d[0][8:]
1351
 
                        self._unstacked_provider.note_missing_key(revid)
1352
 
                    else:
1353
 
                        # no parents - so give the Graph result
1354
 
                        # (NULL_REVISION,).
1355
 
                        revision_graph[d[0]] = (NULL_REVISION,)
 
1007
                    # No parents - so give the Graph result (NULL_REVISION,).
 
1008
                    revision_graph[d[0]] = (NULL_REVISION,)
1356
1009
            return revision_graph
1357
1010
 
1358
1011
    @needs_read_lock
1361
1014
        return self._real_repository.get_signature_text(revision_id)
1362
1015
 
1363
1016
    @needs_read_lock
 
1017
    @symbol_versioning.deprecated_method(symbol_versioning.one_three)
 
1018
    def get_revision_graph_with_ghosts(self, revision_ids=None):
 
1019
        self._ensure_real()
 
1020
        return self._real_repository.get_revision_graph_with_ghosts(
 
1021
            revision_ids=revision_ids)
 
1022
 
 
1023
    @needs_read_lock
1364
1024
    def get_inventory_xml(self, revision_id):
1365
1025
        self._ensure_real()
1366
1026
        return self._real_repository.get_inventory_xml(revision_id)
1372
1032
    def reconcile(self, other=None, thorough=False):
1373
1033
        self._ensure_real()
1374
1034
        return self._real_repository.reconcile(other=other, thorough=thorough)
1375
 
 
 
1035
        
1376
1036
    def all_revision_ids(self):
1377
1037
        self._ensure_real()
1378
1038
        return self._real_repository.all_revision_ids()
1379
 
 
1380
 
    @needs_read_lock
1381
 
    def get_deltas_for_revisions(self, revisions, specific_fileids=None):
1382
 
        self._ensure_real()
1383
 
        return self._real_repository.get_deltas_for_revisions(revisions,
1384
 
            specific_fileids=specific_fileids)
1385
 
 
1386
 
    @needs_read_lock
1387
 
    def get_revision_delta(self, revision_id, specific_fileids=None):
1388
 
        self._ensure_real()
1389
 
        return self._real_repository.get_revision_delta(revision_id,
1390
 
            specific_fileids=specific_fileids)
 
1039
    
 
1040
    @needs_read_lock
 
1041
    def get_deltas_for_revisions(self, revisions):
 
1042
        self._ensure_real()
 
1043
        return self._real_repository.get_deltas_for_revisions(revisions)
 
1044
 
 
1045
    @needs_read_lock
 
1046
    def get_revision_delta(self, revision_id):
 
1047
        self._ensure_real()
 
1048
        return self._real_repository.get_revision_delta(revision_id)
1391
1049
 
1392
1050
    @needs_read_lock
1393
1051
    def revision_trees(self, revision_ids):
1469
1127
        return self._real_repository.revisions
1470
1128
 
1471
1129
    def set_make_working_trees(self, new_value):
1472
 
        if new_value:
1473
 
            new_value_str = "True"
1474
 
        else:
1475
 
            new_value_str = "False"
1476
 
        path = self.bzrdir._path_for_remote_call(self._client)
1477
 
        try:
1478
 
            response = self._call(
1479
 
                'Repository.set_make_working_trees', path, new_value_str)
1480
 
        except errors.UnknownSmartMethod:
1481
 
            self._ensure_real()
1482
 
            self._real_repository.set_make_working_trees(new_value)
1483
 
        else:
1484
 
            if response[0] != 'ok':
1485
 
                raise errors.UnexpectedSmartServerResponse(response)
 
1130
        self._ensure_real()
 
1131
        self._real_repository.set_make_working_trees(new_value)
1486
1132
 
1487
1133
    @property
1488
1134
    def signatures(self):
1515
1161
        return self._real_repository.get_revisions(revision_ids)
1516
1162
 
1517
1163
    def supports_rich_root(self):
1518
 
        return self._format.rich_root_data
 
1164
        self._ensure_real()
 
1165
        return self._real_repository.supports_rich_root()
1519
1166
 
1520
1167
    def iter_reverse_revision_history(self, revision_id):
1521
1168
        self._ensure_real()
1523
1170
 
1524
1171
    @property
1525
1172
    def _serializer(self):
1526
 
        return self._format._serializer
 
1173
        self._ensure_real()
 
1174
        return self._real_repository._serializer
1527
1175
 
1528
1176
    def store_revision_signature(self, gpg_strategy, plaintext, revision_id):
1529
1177
        self._ensure_real()
1556
1204
        self._ensure_real()
1557
1205
        return self._real_repository._check_for_inconsistent_revision_parents()
1558
1206
 
1559
 
    def _make_parents_provider(self, other=None):
1560
 
        providers = [self._unstacked_provider]
1561
 
        if other is not None:
1562
 
            providers.insert(0, other)
1563
 
        providers.extend(r._make_parents_provider() for r in
1564
 
                         self._fallback_repositories)
1565
 
        return graph._StackedParentsProvider(providers)
 
1207
    def _make_parents_provider(self):
 
1208
        return self
1566
1209
 
1567
1210
    def _serialise_search_recipe(self, recipe):
1568
1211
        """Serialise a graph search recipe.
1570
1213
        :param recipe: A search recipe (start, stop, count).
1571
1214
        :return: Serialised bytes.
1572
1215
        """
1573
 
        start_keys = ' '.join(recipe[1])
1574
 
        stop_keys = ' '.join(recipe[2])
1575
 
        count = str(recipe[3])
 
1216
        start_keys = ' '.join(recipe[0])
 
1217
        stop_keys = ' '.join(recipe[1])
 
1218
        count = str(recipe[2])
1576
1219
        return '\n'.join((start_keys, stop_keys, count))
1577
1220
 
1578
 
    def _serialise_search_result(self, search_result):
1579
 
        if isinstance(search_result, graph.PendingAncestryResult):
1580
 
            parts = ['ancestry-of']
1581
 
            parts.extend(search_result.heads)
1582
 
        else:
1583
 
            recipe = search_result.get_recipe()
1584
 
            parts = [recipe[0], self._serialise_search_recipe(recipe)]
1585
 
        return '\n'.join(parts)
1586
 
 
1587
1221
    def autopack(self):
1588
1222
        path = self.bzrdir._path_for_remote_call(self._client)
1589
1223
        try:
1592
1226
            self._ensure_real()
1593
1227
            self._real_repository._pack_collection.autopack()
1594
1228
            return
1595
 
        self.refresh_data()
 
1229
        if self._real_repository is not None:
 
1230
            # Reset the real repository's cache of pack names.
 
1231
            # XXX: At some point we may be able to skip this and just rely on
 
1232
            # the automatic retry logic to do the right thing, but for now we
 
1233
            # err on the side of being correct rather than being optimal.
 
1234
            self._real_repository._pack_collection.reload_pack_names()
1596
1235
        if response[0] != 'ok':
1597
1236
            raise errors.UnexpectedSmartServerResponse(response)
1598
1237
 
1599
1238
 
1600
 
class RemoteStreamSink(repository.StreamSink):
1601
 
 
1602
 
    def _insert_real(self, stream, src_format, resume_tokens):
1603
 
        self.target_repo._ensure_real()
1604
 
        sink = self.target_repo._real_repository._get_sink()
1605
 
        result = sink.insert_stream(stream, src_format, resume_tokens)
1606
 
        if not result:
1607
 
            self.target_repo.autopack()
1608
 
        return result
1609
 
 
1610
 
    def insert_stream(self, stream, src_format, resume_tokens):
1611
 
        target = self.target_repo
1612
 
        target._unstacked_provider.missing_keys.clear()
1613
 
        if target._lock_token:
1614
 
            verb = 'Repository.insert_stream_locked'
1615
 
            extra_args = (target._lock_token or '',)
1616
 
            required_version = (1, 14)
1617
 
        else:
1618
 
            verb = 'Repository.insert_stream'
1619
 
            extra_args = ()
1620
 
            required_version = (1, 13)
1621
 
        client = target._client
1622
 
        medium = client._medium
1623
 
        if medium._is_remote_before(required_version):
1624
 
            # No possible way this can work.
1625
 
            return self._insert_real(stream, src_format, resume_tokens)
1626
 
        path = target.bzrdir._path_for_remote_call(client)
1627
 
        if not resume_tokens:
1628
 
            # XXX: Ugly but important for correctness, *will* be fixed during
1629
 
            # 1.13 cycle. Pushing a stream that is interrupted results in a
1630
 
            # fallback to the _real_repositories sink *with a partial stream*.
1631
 
            # Thats bad because we insert less data than bzr expected. To avoid
1632
 
            # this we do a trial push to make sure the verb is accessible, and
1633
 
            # do not fallback when actually pushing the stream. A cleanup patch
1634
 
            # is going to look at rewinding/restarting the stream/partial
1635
 
            # buffering etc.
1636
 
            byte_stream = smart_repo._stream_to_byte_stream([], src_format)
1637
 
            try:
1638
 
                response = client.call_with_body_stream(
1639
 
                    (verb, path, '') + extra_args, byte_stream)
1640
 
            except errors.UnknownSmartMethod:
1641
 
                medium._remember_remote_is_before(required_version)
1642
 
                return self._insert_real(stream, src_format, resume_tokens)
1643
 
        byte_stream = smart_repo._stream_to_byte_stream(
1644
 
            stream, src_format)
1645
 
        resume_tokens = ' '.join(resume_tokens)
1646
 
        response = client.call_with_body_stream(
1647
 
            (verb, path, resume_tokens) + extra_args, byte_stream)
1648
 
        if response[0][0] not in ('ok', 'missing-basis'):
1649
 
            raise errors.UnexpectedSmartServerResponse(response)
1650
 
        if response[0][0] == 'missing-basis':
1651
 
            tokens, missing_keys = bencode.bdecode_as_tuple(response[0][1])
1652
 
            resume_tokens = tokens
1653
 
            return resume_tokens, set(missing_keys)
1654
 
        else:
1655
 
            self.target_repo.refresh_data()
1656
 
            return [], set()
1657
 
 
1658
 
 
1659
 
class RemoteStreamSource(repository.StreamSource):
1660
 
    """Stream data from a remote server."""
1661
 
 
1662
 
    def get_stream(self, search):
1663
 
        if (self.from_repository._fallback_repositories and
1664
 
            self.to_format._fetch_order == 'topological'):
1665
 
            return self._real_stream(self.from_repository, search)
1666
 
        return self.missing_parents_chain(search, [self.from_repository] +
1667
 
            self.from_repository._fallback_repositories)
1668
 
 
1669
 
    def _real_stream(self, repo, search):
1670
 
        """Get a stream for search from repo.
1671
 
        
1672
 
        This never called RemoteStreamSource.get_stream, and is a heler
1673
 
        for RemoteStreamSource._get_stream to allow getting a stream 
1674
 
        reliably whether fallback back because of old servers or trying
1675
 
        to stream from a non-RemoteRepository (which the stacked support
1676
 
        code will do).
1677
 
        """
1678
 
        source = repo._get_source(self.to_format)
1679
 
        if isinstance(source, RemoteStreamSource):
1680
 
            return repository.StreamSource.get_stream(source, search)
1681
 
        return source.get_stream(search)
1682
 
 
1683
 
    def _get_stream(self, repo, search):
1684
 
        """Core worker to get a stream from repo for search.
1685
 
 
1686
 
        This is used by both get_stream and the stacking support logic. It
1687
 
        deliberately gets a stream for repo which does not need to be
1688
 
        self.from_repository. In the event that repo is not Remote, or
1689
 
        cannot do a smart stream, a fallback is made to the generic
1690
 
        repository._get_stream() interface, via self._real_stream.
1691
 
 
1692
 
        In the event of stacking, streams from _get_stream will not
1693
 
        contain all the data for search - this is normal (see get_stream).
1694
 
 
1695
 
        :param repo: A repository.
1696
 
        :param search: A search.
1697
 
        """
1698
 
        # Fallbacks may be non-smart
1699
 
        if not isinstance(repo, RemoteRepository):
1700
 
            return self._real_stream(repo, search)
1701
 
        client = repo._client
1702
 
        medium = client._medium
1703
 
        if medium._is_remote_before((1, 13)):
1704
 
            # streaming was added in 1.13
1705
 
            return self._real_stream(repo, search)
1706
 
        path = repo.bzrdir._path_for_remote_call(client)
1707
 
        try:
1708
 
            search_bytes = repo._serialise_search_result(search)
1709
 
            response = repo._call_with_body_bytes_expecting_body(
1710
 
                'Repository.get_stream',
1711
 
                (path, self.to_format.network_name()), search_bytes)
1712
 
            response_tuple, response_handler = response
1713
 
        except errors.UnknownSmartMethod:
1714
 
            medium._remember_remote_is_before((1,13))
1715
 
            return self._real_stream(repo, search)
1716
 
        if response_tuple[0] != 'ok':
1717
 
            raise errors.UnexpectedSmartServerResponse(response_tuple)
1718
 
        byte_stream = response_handler.read_streamed_body()
1719
 
        src_format, stream = smart_repo._byte_stream_to_stream(byte_stream)
1720
 
        if src_format.network_name() != repo._format.network_name():
1721
 
            raise AssertionError(
1722
 
                "Mismatched RemoteRepository and stream src %r, %r" % (
1723
 
                src_format.network_name(), repo._format.network_name()))
1724
 
        return stream
1725
 
 
1726
 
    def missing_parents_chain(self, search, sources):
1727
 
        """Chain multiple streams together to handle stacking.
1728
 
 
1729
 
        :param search: The overall search to satisfy with streams.
1730
 
        :param sources: A list of Repository objects to query.
1731
 
        """
1732
 
        self.serialiser = self.to_format._serializer
1733
 
        self.seen_revs = set()
1734
 
        self.referenced_revs = set()
1735
 
        # If there are heads in the search, or the key count is > 0, we are not
1736
 
        # done.
1737
 
        while not search.is_empty() and len(sources) > 1:
1738
 
            source = sources.pop(0)
1739
 
            stream = self._get_stream(source, search)
1740
 
            for kind, substream in stream:
1741
 
                if kind != 'revisions':
1742
 
                    yield kind, substream
1743
 
                else:
1744
 
                    yield kind, self.missing_parents_rev_handler(substream)
1745
 
            search = search.refine(self.seen_revs, self.referenced_revs)
1746
 
            self.seen_revs = set()
1747
 
            self.referenced_revs = set()
1748
 
        if not search.is_empty():
1749
 
            for kind, stream in self._get_stream(sources[0], search):
1750
 
                yield kind, stream
1751
 
 
1752
 
    def missing_parents_rev_handler(self, substream):
1753
 
        for content in substream:
1754
 
            revision_bytes = content.get_bytes_as('fulltext')
1755
 
            revision = self.serialiser.read_revision_from_string(revision_bytes)
1756
 
            self.seen_revs.add(content.key[-1])
1757
 
            self.referenced_revs.update(revision.parent_ids)
1758
 
            yield content
1759
 
 
1760
 
 
1761
1239
class RemoteBranchLockableFiles(LockableFiles):
1762
1240
    """A 'LockableFiles' implementation that talks to a smart server.
1763
 
 
 
1241
    
1764
1242
    This is not a public interface class.
1765
1243
    """
1766
1244
 
1780
1258
 
1781
1259
class RemoteBranchFormat(branch.BranchFormat):
1782
1260
 
1783
 
    def __init__(self, network_name=None):
1784
 
        super(RemoteBranchFormat, self).__init__()
1785
 
        self._matchingbzrdir = RemoteBzrDirFormat()
1786
 
        self._matchingbzrdir.set_branch_format(self)
1787
 
        self._custom_format = None
1788
 
        self._network_name = network_name
1789
 
 
1790
1261
    def __eq__(self, other):
1791
 
        return (isinstance(other, RemoteBranchFormat) and
 
1262
        return (isinstance(other, RemoteBranchFormat) and 
1792
1263
            self.__dict__ == other.__dict__)
1793
1264
 
1794
 
    def _ensure_real(self):
1795
 
        if self._custom_format is None:
1796
 
            self._custom_format = branch.network_format_registry.get(
1797
 
                self._network_name)
1798
 
 
1799
1265
    def get_format_description(self):
1800
1266
        return 'Remote BZR Branch'
1801
1267
 
1802
 
    def network_name(self):
1803
 
        return self._network_name
1804
 
 
1805
 
    def open(self, a_bzrdir, ignore_fallbacks=False):
1806
 
        return a_bzrdir.open_branch(ignore_fallbacks=ignore_fallbacks)
1807
 
 
1808
 
    def _vfs_initialize(self, a_bzrdir):
1809
 
        # Initialisation when using a local bzrdir object, or a non-vfs init
1810
 
        # method is not available on the server.
1811
 
        # self._custom_format is always set - the start of initialize ensures
1812
 
        # that.
1813
 
        if isinstance(a_bzrdir, RemoteBzrDir):
1814
 
            a_bzrdir._ensure_real()
1815
 
            result = self._custom_format.initialize(a_bzrdir._real_bzrdir)
1816
 
        else:
1817
 
            # We assume the bzrdir is parameterised; it may not be.
1818
 
            result = self._custom_format.initialize(a_bzrdir)
1819
 
        if (isinstance(a_bzrdir, RemoteBzrDir) and
1820
 
            not isinstance(result, RemoteBranch)):
1821
 
            result = RemoteBranch(a_bzrdir, a_bzrdir.find_repository(), result)
1822
 
        return result
 
1268
    def get_format_string(self):
 
1269
        return 'Remote BZR Branch'
 
1270
 
 
1271
    def open(self, a_bzrdir):
 
1272
        return a_bzrdir.open_branch()
1823
1273
 
1824
1274
    def initialize(self, a_bzrdir):
1825
 
        # 1) get the network name to use.
1826
 
        if self._custom_format:
1827
 
            network_name = self._custom_format.network_name()
1828
 
        else:
1829
 
            # Select the current bzrlib default and ask for that.
1830
 
            reference_bzrdir_format = bzrdir.format_registry.get('default')()
1831
 
            reference_format = reference_bzrdir_format.get_branch_format()
1832
 
            self._custom_format = reference_format
1833
 
            network_name = reference_format.network_name()
1834
 
        # Being asked to create on a non RemoteBzrDir:
1835
 
        if not isinstance(a_bzrdir, RemoteBzrDir):
1836
 
            return self._vfs_initialize(a_bzrdir)
1837
 
        medium = a_bzrdir._client._medium
1838
 
        if medium._is_remote_before((1, 13)):
1839
 
            return self._vfs_initialize(a_bzrdir)
1840
 
        # Creating on a remote bzr dir.
1841
 
        # 2) try direct creation via RPC
1842
 
        path = a_bzrdir._path_for_remote_call(a_bzrdir._client)
1843
 
        verb = 'BzrDir.create_branch'
1844
 
        try:
1845
 
            response = a_bzrdir._call(verb, path, network_name)
1846
 
        except errors.UnknownSmartMethod:
1847
 
            # Fallback - use vfs methods
1848
 
            medium._remember_remote_is_before((1, 13))
1849
 
            return self._vfs_initialize(a_bzrdir)
1850
 
        if response[0] != 'ok':
1851
 
            raise errors.UnexpectedSmartServerResponse(response)
1852
 
        # Turn the response into a RemoteRepository object.
1853
 
        format = RemoteBranchFormat(network_name=response[1])
1854
 
        repo_format = response_tuple_to_repo_format(response[3:])
1855
 
        if response[2] == '':
1856
 
            repo_bzrdir = a_bzrdir
1857
 
        else:
1858
 
            repo_bzrdir = RemoteBzrDir(
1859
 
                a_bzrdir.root_transport.clone(response[2]), a_bzrdir._format,
1860
 
                a_bzrdir._client)
1861
 
        remote_repo = RemoteRepository(repo_bzrdir, repo_format)
1862
 
        remote_branch = RemoteBranch(a_bzrdir, remote_repo,
1863
 
            format=format, setup_stacking=False)
1864
 
        # XXX: We know this is a new branch, so it must have revno 0, revid
1865
 
        # NULL_REVISION. Creating the branch locked would make this be unable
1866
 
        # to be wrong; here its simply very unlikely to be wrong. RBC 20090225
1867
 
        remote_branch._last_revision_info_cache = 0, NULL_REVISION
1868
 
        return remote_branch
1869
 
 
1870
 
    def make_tags(self, branch):
1871
 
        self._ensure_real()
1872
 
        return self._custom_format.make_tags(branch)
 
1275
        return a_bzrdir.create_branch()
1873
1276
 
1874
1277
    def supports_tags(self):
1875
1278
        # Remote branches might support tags, but we won't know until we
1876
1279
        # access the real remote branch.
1877
 
        self._ensure_real()
1878
 
        return self._custom_format.supports_tags()
1879
 
 
1880
 
    def supports_stacking(self):
1881
 
        self._ensure_real()
1882
 
        return self._custom_format.supports_stacking()
 
1280
        return True
1883
1281
 
1884
1282
 
1885
1283
class RemoteBranch(branch.Branch, _RpcHelper):
1889
1287
    """
1890
1288
 
1891
1289
    def __init__(self, remote_bzrdir, remote_repository, real_branch=None,
1892
 
        _client=None, format=None, setup_stacking=True):
 
1290
        _client=None):
1893
1291
        """Create a RemoteBranch instance.
1894
1292
 
1895
1293
        :param real_branch: An optional local implementation of the branch
1896
1294
            format, usually accessing the data via the VFS.
1897
1295
        :param _client: Private parameter for testing.
1898
 
        :param format: A RemoteBranchFormat object, None to create one
1899
 
            automatically. If supplied it should have a network_name already
1900
 
            supplied.
1901
 
        :param setup_stacking: If True make an RPC call to determine the
1902
 
            stacked (or not) status of the branch. If False assume the branch
1903
 
            is not stacked.
1904
1296
        """
1905
1297
        # We intentionally don't call the parent class's __init__, because it
1906
1298
        # will try to assign to self.tags, which is a property in this subclass.
1907
1299
        # And the parent's __init__ doesn't do much anyway.
1908
1300
        self._revision_id_to_revno_cache = None
1909
 
        self._partial_revision_id_to_revno_cache = {}
1910
1301
        self._revision_history_cache = None
1911
1302
        self._last_revision_info_cache = None
1912
 
        self._merge_sorted_revisions_cache = None
1913
1303
        self.bzrdir = remote_bzrdir
1914
1304
        if _client is not None:
1915
1305
            self._client = _client
1928
1318
            self._real_branch.repository = self.repository
1929
1319
        else:
1930
1320
            self._real_branch = None
1931
 
        # Fill out expected attributes of branch for bzrlib API users.
 
1321
        # Fill out expected attributes of branch for bzrlib api users.
 
1322
        self._format = RemoteBranchFormat()
1932
1323
        self.base = self.bzrdir.root_transport.base
1933
1324
        self._control_files = None
1934
1325
        self._lock_mode = None
1936
1327
        self._repo_lock_token = None
1937
1328
        self._lock_count = 0
1938
1329
        self._leave_lock = False
1939
 
        # Setup a format: note that we cannot call _ensure_real until all the
1940
 
        # attributes above are set: This code cannot be moved higher up in this
1941
 
        # function.
1942
 
        if format is None:
1943
 
            self._format = RemoteBranchFormat()
1944
 
            if real_branch is not None:
1945
 
                self._format._network_name = \
1946
 
                    self._real_branch._format.network_name()
1947
 
        else:
1948
 
            self._format = format
1949
 
        if not self._format._network_name:
1950
 
            # Did not get from open_branchV2 - old server.
1951
 
            self._ensure_real()
1952
 
            self._format._network_name = \
1953
 
                self._real_branch._format.network_name()
1954
 
        self.tags = self._format.make_tags(self)
1955
1330
        # The base class init is not called, so we duplicate this:
1956
1331
        hooks = branch.Branch.hooks['open']
1957
1332
        for hook in hooks:
1958
1333
            hook(self)
1959
 
        if setup_stacking:
1960
 
            self._setup_stacking()
 
1334
        self._setup_stacking()
1961
1335
 
1962
1336
    def _setup_stacking(self):
1963
1337
        # configure stacking into the remote repository, by reading it from
1967
1341
        except (errors.NotStacked, errors.UnstackableBranchFormat,
1968
1342
            errors.UnstackableRepositoryFormat), e:
1969
1343
            return
1970
 
        self._activate_fallback_location(fallback_url, None)
1971
 
 
1972
 
    def _get_config(self):
1973
 
        return RemoteBranchConfig(self)
 
1344
        # it's relative to this branch...
 
1345
        fallback_url = urlutils.join(self.base, fallback_url)
 
1346
        transports = [self.bzrdir.root_transport]
 
1347
        if self._real_branch is not None:
 
1348
            transports.append(self._real_branch._transport)
 
1349
        fallback_bzrdir = BzrDir.open(fallback_url, transports)
 
1350
        fallback_repo = fallback_bzrdir.open_repository()
 
1351
        self.repository.add_fallback_repository(fallback_repo)
1974
1352
 
1975
1353
    def _get_real_transport(self):
1976
1354
        # if we try vfs access, return the real branch's vfs transport
2029
1407
        too, in fact doing so might harm performance.
2030
1408
        """
2031
1409
        super(RemoteBranch, self)._clear_cached_state()
2032
 
 
 
1410
        
2033
1411
    @property
2034
1412
    def control_files(self):
2035
1413
        # Defer actually creating RemoteBranchLockableFiles until its needed,
2074
1452
            raise errors.UnexpectedSmartServerResponse(response)
2075
1453
        return response[1]
2076
1454
 
2077
 
    def _vfs_get_tags_bytes(self):
2078
 
        self._ensure_real()
2079
 
        return self._real_branch._get_tags_bytes()
2080
 
 
2081
 
    def _get_tags_bytes(self):
2082
 
        medium = self._client._medium
2083
 
        if medium._is_remote_before((1, 13)):
2084
 
            return self._vfs_get_tags_bytes()
2085
 
        try:
2086
 
            response = self._call('Branch.get_tags_bytes', self._remote_path())
2087
 
        except errors.UnknownSmartMethod:
2088
 
            medium._remember_remote_is_before((1, 13))
2089
 
            return self._vfs_get_tags_bytes()
2090
 
        return response[0]
2091
 
 
2092
1455
    def lock_read(self):
2093
1456
        self.repository.lock_read()
2094
1457
        if not self._lock_mode:
2114
1477
            raise errors.UnexpectedSmartServerResponse(response)
2115
1478
        ok, branch_token, repo_token = response
2116
1479
        return branch_token, repo_token
2117
 
 
 
1480
            
2118
1481
    def lock_write(self, token=None):
2119
1482
        if not self._lock_mode:
2120
1483
            # Lock the branch and repo in one remote call.
2148
1511
            self.repository.lock_write(self._repo_lock_token)
2149
1512
        return self._lock_token or None
2150
1513
 
2151
 
    def _set_tags_bytes(self, bytes):
2152
 
        self._ensure_real()
2153
 
        return self._real_branch._set_tags_bytes(bytes)
2154
 
 
2155
1514
    def _unlock(self, branch_token, repo_token):
2156
1515
        err_context = {'token': str((branch_token, repo_token))}
2157
1516
        response = self._call(
2179
1538
                    self._real_branch.unlock()
2180
1539
                if mode != 'w':
2181
1540
                    # Only write-locked branched need to make a remote method
2182
 
                    # call to perform the unlock.
 
1541
                    # call to perfom the unlock.
2183
1542
                    return
2184
1543
                if not self._lock_token:
2185
1544
                    raise AssertionError('Locked, but no token!')
2230
1589
 
2231
1590
    def _set_last_revision_descendant(self, revision_id, other_branch,
2232
1591
            allow_diverged=False, allow_overwrite_descendant=False):
2233
 
        # This performs additional work to meet the hook contract; while its
2234
 
        # undesirable, we have to synthesise the revno to call the hook, and
2235
 
        # not calling the hook is worse as it means changes can't be prevented.
2236
 
        # Having calculated this though, we can't just call into
2237
 
        # set_last_revision_info as a simple call, because there is a set_rh
2238
 
        # hook that some folk may still be using.
2239
 
        old_revno, old_revid = self.last_revision_info()
2240
 
        history = self._lefthand_history(revision_id)
2241
 
        self._run_pre_change_branch_tip_hooks(len(history), revision_id)
2242
1592
        err_context = {'other_branch': other_branch}
2243
1593
        response = self._call('Branch.set_last_revision_ex',
2244
1594
            self._remote_path(), self._lock_token, self._repo_lock_token,
2249
1599
            raise errors.UnexpectedSmartServerResponse(response)
2250
1600
        new_revno, new_revision_id = response[1:]
2251
1601
        self._last_revision_info_cache = new_revno, new_revision_id
2252
 
        self._run_post_change_branch_tip_hooks(old_revno, old_revid)
2253
1602
        if self._real_branch is not None:
2254
1603
            cache = new_revno, new_revision_id
2255
1604
            self._real_branch._last_revision_info_cache = cache
2256
1605
 
2257
1606
    def _set_last_revision(self, revision_id):
2258
 
        old_revno, old_revid = self.last_revision_info()
2259
 
        # This performs additional work to meet the hook contract; while its
2260
 
        # undesirable, we have to synthesise the revno to call the hook, and
2261
 
        # not calling the hook is worse as it means changes can't be prevented.
2262
 
        # Having calculated this though, we can't just call into
2263
 
        # set_last_revision_info as a simple call, because there is a set_rh
2264
 
        # hook that some folk may still be using.
2265
 
        history = self._lefthand_history(revision_id)
2266
 
        self._run_pre_change_branch_tip_hooks(len(history), revision_id)
2267
1607
        self._clear_cached_state()
2268
1608
        response = self._call('Branch.set_last_revision',
2269
1609
            self._remote_path(), self._lock_token, self._repo_lock_token,
2270
1610
            revision_id)
2271
1611
        if response != ('ok',):
2272
1612
            raise errors.UnexpectedSmartServerResponse(response)
2273
 
        self._run_post_change_branch_tip_hooks(old_revno, old_revid)
2274
1613
 
2275
1614
    @needs_write_lock
2276
1615
    def set_revision_history(self, rev_history):
2282
1621
        else:
2283
1622
            rev_id = rev_history[-1]
2284
1623
        self._set_last_revision(rev_id)
2285
 
        for hook in branch.Branch.hooks['set_rh']:
2286
 
            hook(self, rev_history)
2287
1624
        self._cache_revision_history(rev_history)
2288
1625
 
2289
 
    def _get_parent_location(self):
2290
 
        medium = self._client._medium
2291
 
        if medium._is_remote_before((1, 13)):
2292
 
            return self._vfs_get_parent_location()
2293
 
        try:
2294
 
            response = self._call('Branch.get_parent', self._remote_path())
2295
 
        except errors.UnknownSmartMethod:
2296
 
            medium._remember_remote_is_before((1, 13))
2297
 
            return self._vfs_get_parent_location()
2298
 
        if len(response) != 1:
2299
 
            raise errors.UnexpectedSmartServerResponse(response)
2300
 
        parent_location = response[0]
2301
 
        if parent_location == '':
2302
 
            return None
2303
 
        return parent_location
2304
 
 
2305
 
    def _vfs_get_parent_location(self):
2306
 
        self._ensure_real()
2307
 
        return self._real_branch._get_parent_location()
2308
 
 
2309
 
    def _set_parent_location(self, url):
2310
 
        medium = self._client._medium
2311
 
        if medium._is_remote_before((1, 15)):
2312
 
            return self._vfs_set_parent_location(url)
2313
 
        try:
2314
 
            call_url = url or ''
2315
 
            if type(call_url) is not str:
2316
 
                raise AssertionError('url must be a str or None (%s)' % url)
2317
 
            response = self._call('Branch.set_parent_location',
2318
 
                self._remote_path(), self._lock_token, self._repo_lock_token,
2319
 
                call_url)
2320
 
        except errors.UnknownSmartMethod:
2321
 
            medium._remember_remote_is_before((1, 15))
2322
 
            return self._vfs_set_parent_location(url)
2323
 
        if response != ():
2324
 
            raise errors.UnexpectedSmartServerResponse(response)
2325
 
 
2326
 
    def _vfs_set_parent_location(self, url):
2327
 
        self._ensure_real()
2328
 
        return self._real_branch._set_parent_location(url)
 
1626
    def get_parent(self):
 
1627
        self._ensure_real()
 
1628
        return self._real_branch.get_parent()
 
1629
        
 
1630
    def set_parent(self, url):
 
1631
        self._ensure_real()
 
1632
        return self._real_branch.set_parent(url)
 
1633
        
 
1634
    def set_stacked_on_url(self, stacked_location):
 
1635
        """Set the URL this branch is stacked against.
 
1636
 
 
1637
        :raises UnstackableBranchFormat: If the branch does not support
 
1638
            stacking.
 
1639
        :raises UnstackableRepositoryFormat: If the repository does not support
 
1640
            stacking.
 
1641
        """
 
1642
        self._ensure_real()
 
1643
        return self._real_branch.set_stacked_on_url(stacked_location)
 
1644
 
 
1645
    def sprout(self, to_bzrdir, revision_id=None):
 
1646
        branch_format = to_bzrdir._format._branch_format
 
1647
        if (branch_format is None or
 
1648
            isinstance(branch_format, RemoteBranchFormat)):
 
1649
            # The to_bzrdir specifies RemoteBranchFormat (or no format, which
 
1650
            # implies the same thing), but RemoteBranches can't be created at
 
1651
            # arbitrary URLs.  So create a branch in the same format as
 
1652
            # _real_branch instead.
 
1653
            # XXX: if to_bzrdir is a RemoteBzrDir, this should perhaps do
 
1654
            # to_bzrdir.create_branch to create a RemoteBranch after all...
 
1655
            self._ensure_real()
 
1656
            result = self._real_branch._format.initialize(to_bzrdir)
 
1657
            self.copy_content_into(result, revision_id=revision_id)
 
1658
            result.set_parent(self.bzrdir.root_transport.base)
 
1659
        else:
 
1660
            result = branch.Branch.sprout(
 
1661
                self, to_bzrdir, revision_id=revision_id)
 
1662
        return result
2329
1663
 
2330
1664
    @needs_write_lock
2331
1665
    def pull(self, source, overwrite=False, stop_revision=None,
2353
1687
 
2354
1688
    @needs_write_lock
2355
1689
    def set_last_revision_info(self, revno, revision_id):
2356
 
        # XXX: These should be returned by the set_last_revision_info verb
2357
 
        old_revno, old_revid = self.last_revision_info()
2358
 
        self._run_pre_change_branch_tip_hooks(revno, revision_id)
2359
1690
        revision_id = ensure_null(revision_id)
2360
1691
        try:
2361
1692
            response = self._call('Branch.set_last_revision_info',
2370
1701
        if response == ('ok',):
2371
1702
            self._clear_cached_state()
2372
1703
            self._last_revision_info_cache = revno, revision_id
2373
 
            self._run_post_change_branch_tip_hooks(old_revno, old_revid)
2374
1704
            # Update the _real_branch's cache too.
2375
1705
            if self._real_branch is not None:
2376
1706
                cache = self._last_revision_info_cache
2383
1713
                                  other_branch=None):
2384
1714
        medium = self._client._medium
2385
1715
        if not medium._is_remote_before((1, 6)):
2386
 
            # Use a smart method for 1.6 and above servers
2387
1716
            try:
2388
1717
                self._set_last_revision_descendant(revision_id, other_branch,
2389
1718
                    allow_diverged=True, allow_overwrite_descendant=True)
2391
1720
            except errors.UnknownSmartMethod:
2392
1721
                medium._remember_remote_is_before((1, 6))
2393
1722
        self._clear_cached_state_of_remote_branch_only()
2394
 
        self.set_revision_history(self._lefthand_history(revision_id,
2395
 
            last_rev=last_rev,other_branch=other_branch))
 
1723
        self._ensure_real()
 
1724
        self._real_branch.generate_revision_history(
 
1725
            revision_id, last_rev=last_rev, other_branch=other_branch)
 
1726
 
 
1727
    @property
 
1728
    def tags(self):
 
1729
        self._ensure_real()
 
1730
        return self._real_branch.tags
2396
1731
 
2397
1732
    def set_push_location(self, location):
2398
1733
        self._ensure_real()
2399
1734
        return self._real_branch.set_push_location(location)
2400
1735
 
2401
 
 
2402
 
class RemoteConfig(object):
2403
 
    """A Config that reads and writes from smart verbs.
2404
 
 
2405
 
    It is a low-level object that considers config data to be name/value pairs
2406
 
    that may be associated with a section. Assigning meaning to the these
2407
 
    values is done at higher levels like bzrlib.config.TreeConfig.
2408
 
    """
2409
 
 
2410
 
    def get_option(self, name, section=None, default=None):
2411
 
        """Return the value associated with a named option.
2412
 
 
2413
 
        :param name: The name of the value
2414
 
        :param section: The section the option is in (if any)
2415
 
        :param default: The value to return if the value is not set
2416
 
        :return: The value or default value
2417
 
        """
 
1736
    @needs_write_lock
 
1737
    def update_revisions(self, other, stop_revision=None, overwrite=False,
 
1738
                         graph=None):
 
1739
        """See Branch.update_revisions."""
 
1740
        other.lock_read()
2418
1741
        try:
2419
 
            configobj = self._get_configobj()
2420
 
            if section is None:
2421
 
                section_obj = configobj
 
1742
            if stop_revision is None:
 
1743
                stop_revision = other.last_revision()
 
1744
                if revision.is_null(stop_revision):
 
1745
                    # if there are no commits, we're done.
 
1746
                    return
 
1747
            self.fetch(other, stop_revision)
 
1748
 
 
1749
            if overwrite:
 
1750
                # Just unconditionally set the new revision.  We don't care if
 
1751
                # the branches have diverged.
 
1752
                self._set_last_revision(stop_revision)
2422
1753
            else:
2423
 
                try:
2424
 
                    section_obj = configobj[section]
2425
 
                except KeyError:
2426
 
                    return default
2427
 
            return section_obj.get(name, default)
2428
 
        except errors.UnknownSmartMethod:
2429
 
            return self._vfs_get_option(name, section, default)
2430
 
 
2431
 
    def _response_to_configobj(self, response):
2432
 
        if len(response[0]) and response[0][0] != 'ok':
2433
 
            raise errors.UnexpectedSmartServerResponse(response)
2434
 
        lines = response[1].read_body_bytes().splitlines()
2435
 
        return config.ConfigObj(lines, encoding='utf-8')
2436
 
 
2437
 
 
2438
 
class RemoteBranchConfig(RemoteConfig):
2439
 
    """A RemoteConfig for Branches."""
2440
 
 
2441
 
    def __init__(self, branch):
2442
 
        self._branch = branch
2443
 
 
2444
 
    def _get_configobj(self):
2445
 
        path = self._branch._remote_path()
2446
 
        response = self._branch._client.call_expecting_body(
2447
 
            'Branch.get_config_file', path)
2448
 
        return self._response_to_configobj(response)
2449
 
 
2450
 
    def set_option(self, value, name, section=None):
2451
 
        """Set the value associated with a named option.
2452
 
 
2453
 
        :param value: The value to set
2454
 
        :param name: The name of the value to set
2455
 
        :param section: The section the option is in (if any)
2456
 
        """
2457
 
        medium = self._branch._client._medium
2458
 
        if medium._is_remote_before((1, 14)):
2459
 
            return self._vfs_set_option(value, name, section)
2460
 
        try:
2461
 
            path = self._branch._remote_path()
2462
 
            response = self._branch._client.call('Branch.set_config_option',
2463
 
                path, self._branch._lock_token, self._branch._repo_lock_token,
2464
 
                value.encode('utf8'), name, section or '')
2465
 
        except errors.UnknownSmartMethod:
2466
 
            medium._remember_remote_is_before((1, 14))
2467
 
            return self._vfs_set_option(value, name, section)
2468
 
        if response != ():
2469
 
            raise errors.UnexpectedSmartServerResponse(response)
2470
 
 
2471
 
    def _real_object(self):
2472
 
        self._branch._ensure_real()
2473
 
        return self._branch._real_branch
2474
 
 
2475
 
    def _vfs_set_option(self, value, name, section=None):
2476
 
        return self._real_object()._get_config().set_option(
2477
 
            value, name, section)
2478
 
 
2479
 
 
2480
 
class RemoteBzrDirConfig(RemoteConfig):
2481
 
    """A RemoteConfig for BzrDirs."""
2482
 
 
2483
 
    def __init__(self, bzrdir):
2484
 
        self._bzrdir = bzrdir
2485
 
 
2486
 
    def _get_configobj(self):
2487
 
        medium = self._bzrdir._client._medium
2488
 
        verb = 'BzrDir.get_config_file'
2489
 
        if medium._is_remote_before((1, 15)):
2490
 
            raise errors.UnknownSmartMethod(verb)
2491
 
        path = self._bzrdir._path_for_remote_call(self._bzrdir._client)
2492
 
        response = self._bzrdir._call_expecting_body(
2493
 
            verb, path)
2494
 
        return self._response_to_configobj(response)
2495
 
 
2496
 
    def _vfs_get_option(self, name, section, default):
2497
 
        return self._real_object()._get_config().get_option(
2498
 
            name, section, default)
2499
 
 
2500
 
    def set_option(self, value, name, section=None):
2501
 
        """Set the value associated with a named option.
2502
 
 
2503
 
        :param value: The value to set
2504
 
        :param name: The name of the value to set
2505
 
        :param section: The section the option is in (if any)
2506
 
        """
2507
 
        return self._real_object()._get_config().set_option(
2508
 
            value, name, section)
2509
 
 
2510
 
    def _real_object(self):
2511
 
        self._bzrdir._ensure_real()
2512
 
        return self._bzrdir._real_bzrdir
2513
 
 
 
1754
                medium = self._client._medium
 
1755
                if not medium._is_remote_before((1, 6)):
 
1756
                    try:
 
1757
                        self._set_last_revision_descendant(stop_revision, other)
 
1758
                        return
 
1759
                    except errors.UnknownSmartMethod:
 
1760
                        medium._remember_remote_is_before((1, 6))
 
1761
                # Fallback for pre-1.6 servers: check for divergence
 
1762
                # client-side, then do _set_last_revision.
 
1763
                last_rev = revision.ensure_null(self.last_revision())
 
1764
                if graph is None:
 
1765
                    graph = self.repository.get_graph()
 
1766
                if self._check_if_descendant_or_diverged(
 
1767
                        stop_revision, last_rev, graph, other):
 
1768
                    # stop_revision is a descendant of last_rev, but we aren't
 
1769
                    # overwriting, so we're done.
 
1770
                    return
 
1771
                self._set_last_revision(stop_revision)
 
1772
        finally:
 
1773
            other.unlock()
2514
1774
 
2515
1775
 
2516
1776
def _extract_tar(tar, to_dir):