~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/remote.py

  • Committer: Joe Julian
  • Date: 2010-01-10 02:25:31 UTC
  • mto: (4634.119.7 2.0)
  • mto: This revision was merged to the branch mainline in revision 4959.
  • Revision ID: joe@julianfamily.org-20100110022531-wqk61rsagz8xsiga
Added MANIFEST.in to allow bdist_rpm to have all the required include files and tools. bdist_rpm will still fail to build correctly on some distributions due to a disttools bug http://bugs.python.org/issue644744

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2007, 2008 Canonical Ltd
 
1
# Copyright (C) 2006, 2007, 2008, 2009 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
 
# TODO: At some point, handle upgrades by just passing the whole request
18
 
# across to run on the server.
19
 
 
20
17
import bz2
21
18
 
22
19
from bzrlib import (
 
20
    bencode,
23
21
    branch,
24
22
    bzrdir,
25
23
    config,
26
24
    debug,
27
25
    errors,
28
26
    graph,
 
27
    lock,
29
28
    lockdir,
30
 
    pack,
31
29
    repository,
32
30
    revision,
 
31
    revision as _mod_revision,
33
32
    symbol_versioning,
34
 
    urlutils,
35
33
)
36
34
from bzrlib.branch import BranchReferenceFormat
37
35
from bzrlib.bzrdir import BzrDir, RemoteBzrDirFormat
44
42
from bzrlib.smart import client, vfs, repository as smart_repo
45
43
from bzrlib.revision import ensure_null, NULL_REVISION
46
44
from bzrlib.trace import mutter, note, warning
47
 
from bzrlib.util import bencode
48
45
 
49
46
 
50
47
class _RpcHelper(object):
62
59
        except errors.ErrorFromSmartServer, err:
63
60
            self._translate_error(err, **err_context)
64
61
 
 
62
    def _call_with_body_bytes(self, method, args, body_bytes, **err_context):
 
63
        try:
 
64
            return self._client.call_with_body_bytes(method, args, body_bytes)
 
65
        except errors.ErrorFromSmartServer, err:
 
66
            self._translate_error(err, **err_context)
 
67
 
65
68
    def _call_with_body_bytes_expecting_body(self, method, args, body_bytes,
66
69
                                             **err_context):
67
70
        try:
423
426
        self._custom_format = None
424
427
        self._network_name = None
425
428
        self._creating_bzrdir = None
 
429
        self._supports_chks = None
426
430
        self._supports_external_lookups = None
427
431
        self._supports_tree_reference = None
428
432
        self._rich_root_data = None
429
433
 
 
434
    def __repr__(self):
 
435
        return "%s(_network_name=%r)" % (self.__class__.__name__,
 
436
            self._network_name)
 
437
 
430
438
    @property
431
439
    def fast_deltas(self):
432
440
        self._ensure_real()
440
448
        return self._rich_root_data
441
449
 
442
450
    @property
 
451
    def supports_chks(self):
 
452
        if self._supports_chks is None:
 
453
            self._ensure_real()
 
454
            self._supports_chks = self._custom_format.supports_chks
 
455
        return self._supports_chks
 
456
 
 
457
    @property
443
458
    def supports_external_lookups(self):
444
459
        if self._supports_external_lookups is None:
445
460
            self._ensure_real()
491
506
        # 1) get the network name to use.
492
507
        if self._custom_format:
493
508
            network_name = self._custom_format.network_name()
 
509
        elif self._network_name:
 
510
            network_name = self._network_name
494
511
        else:
495
512
            # Select the current bzrlib default and ask for that.
496
513
            reference_bzrdir_format = bzrdir.format_registry.get('default')()
549
566
    def __eq__(self, other):
550
567
        return self.__class__ is other.__class__
551
568
 
552
 
    def check_conversion_target(self, target_format):
553
 
        if self.rich_root_data and not target_format.rich_root_data:
554
 
            raise errors.BadConversionTarget(
555
 
                'Does not support rich root data.', target_format)
556
 
        if (self.supports_tree_reference and
557
 
            not getattr(target_format, 'supports_tree_reference', False)):
558
 
            raise errors.BadConversionTarget(
559
 
                'Does not support nested trees', target_format)
560
 
 
561
569
    def network_name(self):
562
570
        if self._network_name:
563
571
            return self._network_name
565
573
        return self._creating_repo._real_repository._format.network_name()
566
574
 
567
575
    @property
 
576
    def pack_compresses(self):
 
577
        self._ensure_real()
 
578
        return self._custom_format.pack_compresses
 
579
 
 
580
    @property
568
581
    def _serializer(self):
569
582
        self._ensure_real()
570
583
        return self._custom_format._serializer
602
615
        self._lock_token = None
603
616
        self._lock_count = 0
604
617
        self._leave_lock = False
 
618
        # Cache of revision parents; misses are cached during read locks, and
 
619
        # write locks when no _real_repository has been set.
605
620
        self._unstacked_provider = graph.CachingParentsProvider(
606
621
            get_parent_map=self._get_parent_map_rpc)
607
622
        self._unstacked_provider.disable_cache()
665
680
        self._ensure_real()
666
681
        return self._real_repository.suspend_write_group()
667
682
 
 
683
    def get_missing_parent_inventories(self, check_for_missing_texts=True):
 
684
        self._ensure_real()
 
685
        return self._real_repository.get_missing_parent_inventories(
 
686
            check_for_missing_texts=check_for_missing_texts)
 
687
 
 
688
    def _get_rev_id_for_revno_vfs(self, revno, known_pair):
 
689
        self._ensure_real()
 
690
        return self._real_repository.get_rev_id_for_revno(
 
691
            revno, known_pair)
 
692
 
 
693
    def get_rev_id_for_revno(self, revno, known_pair):
 
694
        """See Repository.get_rev_id_for_revno."""
 
695
        path = self.bzrdir._path_for_remote_call(self._client)
 
696
        try:
 
697
            if self._client._medium._is_remote_before((1, 17)):
 
698
                return self._get_rev_id_for_revno_vfs(revno, known_pair)
 
699
            response = self._call(
 
700
                'Repository.get_rev_id_for_revno', path, revno, known_pair)
 
701
        except errors.UnknownSmartMethod:
 
702
            self._client._medium._remember_remote_is_before((1, 17))
 
703
            return self._get_rev_id_for_revno_vfs(revno, known_pair)
 
704
        if response[0] == 'ok':
 
705
            return True, response[1]
 
706
        elif response[0] == 'history-incomplete':
 
707
            known_pair = response[1:3]
 
708
            for fallback in self._fallback_repositories:
 
709
                found, result = fallback.get_rev_id_for_revno(revno, known_pair)
 
710
                if found:
 
711
                    return True, result
 
712
                else:
 
713
                    known_pair = result
 
714
            # Not found in any fallbacks
 
715
            return False, known_pair
 
716
        else:
 
717
            raise errors.UnexpectedSmartServerResponse(response)
 
718
 
668
719
    def _ensure_real(self):
669
720
        """Ensure that there is a _real_repository set.
670
721
 
679
730
        invocation. If in doubt chat to the bzr network team.
680
731
        """
681
732
        if self._real_repository is None:
 
733
            if 'hpssvfs' in debug.debug_flags:
 
734
                import traceback
 
735
                warning('VFS Repository access triggered\n%s',
 
736
                    ''.join(traceback.format_stack()))
 
737
            self._unstacked_provider.missing_keys.clear()
682
738
            self.bzrdir._ensure_real()
683
739
            self._set_real_repository(
684
740
                self.bzrdir._real_bzrdir.open_repository())
744
800
        """Return a source for streaming from this repository."""
745
801
        return RemoteStreamSource(self, to_format)
746
802
 
 
803
    @needs_read_lock
747
804
    def has_revision(self, revision_id):
748
 
        """See Repository.has_revision()."""
749
 
        if revision_id == NULL_REVISION:
750
 
            # The null revision is always present.
751
 
            return True
752
 
        path = self.bzrdir._path_for_remote_call(self._client)
753
 
        response = self._call('Repository.has_revision', path, revision_id)
754
 
        if response[0] not in ('yes', 'no'):
755
 
            raise errors.UnexpectedSmartServerResponse(response)
756
 
        if response[0] == 'yes':
757
 
            return True
758
 
        for fallback_repo in self._fallback_repositories:
759
 
            if fallback_repo.has_revision(revision_id):
760
 
                return True
761
 
        return False
 
805
        """True if this repository has a copy of the revision."""
 
806
        # Copy of bzrlib.repository.Repository.has_revision
 
807
        return revision_id in self.has_revisions((revision_id,))
762
808
 
 
809
    @needs_read_lock
763
810
    def has_revisions(self, revision_ids):
764
 
        """See Repository.has_revisions()."""
765
 
        # FIXME: This does many roundtrips, particularly when there are
766
 
        # fallback repositories.  -- mbp 20080905
767
 
        result = set()
768
 
        for revision_id in revision_ids:
769
 
            if self.has_revision(revision_id):
770
 
                result.add(revision_id)
 
811
        """Probe to find out the presence of multiple revisions.
 
812
 
 
813
        :param revision_ids: An iterable of revision_ids.
 
814
        :return: A set of the revision_ids that were present.
 
815
        """
 
816
        # Copy of bzrlib.repository.Repository.has_revisions
 
817
        parent_map = self.get_parent_map(revision_ids)
 
818
        result = set(parent_map)
 
819
        if _mod_revision.NULL_REVISION in revision_ids:
 
820
            result.add(_mod_revision.NULL_REVISION)
771
821
        return result
772
822
 
 
823
    def _has_same_fallbacks(self, other_repo):
 
824
        """Returns true if the repositories have the same fallbacks."""
 
825
        # XXX: copied from Repository; it should be unified into a base class
 
826
        # <https://bugs.edge.launchpad.net/bzr/+bug/401622>
 
827
        my_fb = self._fallback_repositories
 
828
        other_fb = other_repo._fallback_repositories
 
829
        if len(my_fb) != len(other_fb):
 
830
            return False
 
831
        for f, g in zip(my_fb, other_fb):
 
832
            if not f.has_same_location(g):
 
833
                return False
 
834
        return True
 
835
 
773
836
    def has_same_location(self, other):
 
837
        # TODO: Move to RepositoryBase and unify with the regular Repository
 
838
        # one; unfortunately the tests rely on slightly different behaviour at
 
839
        # present -- mbp 20090710
774
840
        return (self.__class__ is other.__class__ and
775
841
                self.bzrdir.transport.base == other.bzrdir.transport.base)
776
842
 
852
918
            self._unstacked_provider.enable_cache(cache_misses=True)
853
919
            if self._real_repository is not None:
854
920
                self._real_repository.lock_read()
 
921
            for repo in self._fallback_repositories:
 
922
                repo.lock_read()
855
923
        else:
856
924
            self._lock_count += 1
857
 
        for repo in self._fallback_repositories:
858
 
            repo.lock_read()
859
925
 
860
926
    def _remote_lock_write(self, token):
861
927
        path = self.bzrdir._path_for_remote_call(self._client)
891
957
                self._leave_lock = False
892
958
            self._lock_mode = 'w'
893
959
            self._lock_count = 1
894
 
            self._unstacked_provider.enable_cache(cache_misses=False)
 
960
            cache_misses = self._real_repository is None
 
961
            self._unstacked_provider.enable_cache(cache_misses=cache_misses)
 
962
            for repo in self._fallback_repositories:
 
963
                # Writes don't affect fallback repos
 
964
                repo.lock_read()
895
965
        elif self._lock_mode == 'r':
896
966
            raise errors.ReadOnlyError(self)
897
967
        else:
898
968
            self._lock_count += 1
899
 
        for repo in self._fallback_repositories:
900
 
            # Writes don't affect fallback repos
901
 
            repo.lock_read()
902
969
        return self._lock_token or None
903
970
 
904
971
    def leave_lock_in_place(self):
981
1048
 
982
1049
    def unlock(self):
983
1050
        if not self._lock_count:
984
 
            raise errors.LockNotHeld(self)
 
1051
            return lock.cant_unlock_not_held(self)
985
1052
        self._lock_count -= 1
986
1053
        if self._lock_count > 0:
987
1054
            return
1006
1073
                self._lock_token = None
1007
1074
                if not self._leave_lock:
1008
1075
                    self._unlock(old_token)
 
1076
        # Fallbacks are always 'lock_read()' so we don't pay attention to
 
1077
        # self._leave_lock
 
1078
        for repo in self._fallback_repositories:
 
1079
            repo.unlock()
1009
1080
 
1010
1081
    def break_lock(self):
1011
1082
        # should hand off to the network
1075
1146
        # We need to accumulate additional repositories here, to pass them in
1076
1147
        # on various RPC's.
1077
1148
        #
 
1149
        if self.is_locked():
 
1150
            # We will call fallback.unlock() when we transition to the unlocked
 
1151
            # state, so always add a lock here. If a caller passes us a locked
 
1152
            # repository, they are responsible for unlocking it later.
 
1153
            repository.lock_read()
1078
1154
        self._fallback_repositories.append(repository)
1079
1155
        # If self._real_repository was parameterised already (e.g. because a
1080
1156
        # _real_branch had its get_stacked_on_url method called), then the
1105
1181
        self._ensure_real()
1106
1182
        return self._real_repository.get_inventory(revision_id)
1107
1183
 
1108
 
    def iter_inventories(self, revision_ids):
 
1184
    def iter_inventories(self, revision_ids, ordering=None):
1109
1185
        self._ensure_real()
1110
 
        return self._real_repository.iter_inventories(revision_ids)
 
1186
        return self._real_repository.iter_inventories(revision_ids, ordering)
1111
1187
 
1112
1188
    @needs_read_lock
1113
1189
    def get_revision(self, revision_id):
1177
1253
            raise errors.InternalBzrError(
1178
1254
                "May not fetch while in a write group.")
1179
1255
        # fast path same-url fetch operations
1180
 
        if self.has_same_location(source) and fetch_spec is None:
 
1256
        if (self.has_same_location(source)
 
1257
            and fetch_spec is None
 
1258
            and self._has_same_fallbacks(source)):
1181
1259
            # check that last_revision is in 'from' and then return a
1182
1260
            # no-operation.
1183
1261
            if (revision_id is not None and
1395
1473
        return self._real_repository.get_revision_reconcile(revision_id)
1396
1474
 
1397
1475
    @needs_read_lock
1398
 
    def check(self, revision_ids=None):
 
1476
    def check(self, revision_ids=None, callback_refs=None, check_repo=True):
1399
1477
        self._ensure_real()
1400
 
        return self._real_repository.check(revision_ids=revision_ids)
 
1478
        return self._real_repository.check(revision_ids=revision_ids,
 
1479
            callback_refs=callback_refs, check_repo=check_repo)
1401
1480
 
1402
1481
    def copy_content_into(self, destination, revision_id=None):
1403
1482
        self._ensure_real()
1443
1522
        return self._real_repository.inventories
1444
1523
 
1445
1524
    @needs_write_lock
1446
 
    def pack(self):
 
1525
    def pack(self, hint=None):
1447
1526
        """Compress the data within the repository.
1448
1527
 
1449
1528
        This is not currently implemented within the smart server.
1450
1529
        """
1451
1530
        self._ensure_real()
1452
 
        return self._real_repository.pack()
 
1531
        return self._real_repository.pack(hint=hint)
1453
1532
 
1454
1533
    @property
1455
1534
    def revisions(self):
1543
1622
        self._ensure_real()
1544
1623
        return self._real_repository.revision_graph_can_have_wrong_parents()
1545
1624
 
1546
 
    def _find_inconsistent_revision_parents(self):
 
1625
    def _find_inconsistent_revision_parents(self, revisions_iterator=None):
1547
1626
        self._ensure_real()
1548
 
        return self._real_repository._find_inconsistent_revision_parents()
 
1627
        return self._real_repository._find_inconsistent_revision_parents(
 
1628
            revisions_iterator)
1549
1629
 
1550
1630
    def _check_for_inconsistent_revision_parents(self):
1551
1631
        self._ensure_real()
1557
1637
            providers.insert(0, other)
1558
1638
        providers.extend(r._make_parents_provider() for r in
1559
1639
                         self._fallback_repositories)
1560
 
        return graph._StackedParentsProvider(providers)
 
1640
        return graph.StackedParentsProvider(providers)
1561
1641
 
1562
1642
    def _serialise_search_recipe(self, recipe):
1563
1643
        """Serialise a graph search recipe.
1604
1684
 
1605
1685
    def insert_stream(self, stream, src_format, resume_tokens):
1606
1686
        target = self.target_repo
 
1687
        target._unstacked_provider.missing_keys.clear()
 
1688
        candidate_calls = [('Repository.insert_stream_1.19', (1, 19))]
1607
1689
        if target._lock_token:
1608
 
            verb = 'Repository.insert_stream_locked'
1609
 
            extra_args = (target._lock_token or '',)
1610
 
            required_version = (1, 14)
 
1690
            candidate_calls.append(('Repository.insert_stream_locked', (1, 14)))
 
1691
            lock_args = (target._lock_token or '',)
1611
1692
        else:
1612
 
            verb = 'Repository.insert_stream'
1613
 
            extra_args = ()
1614
 
            required_version = (1, 13)
 
1693
            candidate_calls.append(('Repository.insert_stream', (1, 13)))
 
1694
            lock_args = ()
1615
1695
        client = target._client
1616
1696
        medium = client._medium
1617
 
        if medium._is_remote_before(required_version):
1618
 
            # No possible way this can work.
1619
 
            return self._insert_real(stream, src_format, resume_tokens)
1620
1697
        path = target.bzrdir._path_for_remote_call(client)
1621
 
        if not resume_tokens:
1622
 
            # XXX: Ugly but important for correctness, *will* be fixed during
1623
 
            # 1.13 cycle. Pushing a stream that is interrupted results in a
1624
 
            # fallback to the _real_repositories sink *with a partial stream*.
1625
 
            # Thats bad because we insert less data than bzr expected. To avoid
1626
 
            # this we do a trial push to make sure the verb is accessible, and
1627
 
            # do not fallback when actually pushing the stream. A cleanup patch
1628
 
            # is going to look at rewinding/restarting the stream/partial
1629
 
            # buffering etc.
 
1698
        # Probe for the verb to use with an empty stream before sending the
 
1699
        # real stream to it.  We do this both to avoid the risk of sending a
 
1700
        # large request that is then rejected, and because we don't want to
 
1701
        # implement a way to buffer, rewind, or restart the stream.
 
1702
        found_verb = False
 
1703
        for verb, required_version in candidate_calls:
 
1704
            if medium._is_remote_before(required_version):
 
1705
                continue
 
1706
            if resume_tokens:
 
1707
                # We've already done the probing (and set _is_remote_before) on
 
1708
                # a previous insert.
 
1709
                found_verb = True
 
1710
                break
1630
1711
            byte_stream = smart_repo._stream_to_byte_stream([], src_format)
1631
1712
            try:
1632
1713
                response = client.call_with_body_stream(
1633
 
                    (verb, path, '') + extra_args, byte_stream)
 
1714
                    (verb, path, '') + lock_args, byte_stream)
1634
1715
            except errors.UnknownSmartMethod:
1635
1716
                medium._remember_remote_is_before(required_version)
1636
 
                return self._insert_real(stream, src_format, resume_tokens)
 
1717
            else:
 
1718
                found_verb = True
 
1719
                break
 
1720
        if not found_verb:
 
1721
            # Have to use VFS.
 
1722
            return self._insert_real(stream, src_format, resume_tokens)
 
1723
        self._last_inv_record = None
 
1724
        self._last_substream = None
 
1725
        if required_version < (1, 19):
 
1726
            # Remote side doesn't support inventory deltas.  Wrap the stream to
 
1727
            # make sure we don't send any.  If the stream contains inventory
 
1728
            # deltas we'll interrupt the smart insert_stream request and
 
1729
            # fallback to VFS.
 
1730
            stream = self._stop_stream_if_inventory_delta(stream)
1637
1731
        byte_stream = smart_repo._stream_to_byte_stream(
1638
1732
            stream, src_format)
1639
1733
        resume_tokens = ' '.join(resume_tokens)
1640
1734
        response = client.call_with_body_stream(
1641
 
            (verb, path, resume_tokens) + extra_args, byte_stream)
 
1735
            (verb, path, resume_tokens) + lock_args, byte_stream)
1642
1736
        if response[0][0] not in ('ok', 'missing-basis'):
1643
1737
            raise errors.UnexpectedSmartServerResponse(response)
 
1738
        if self._last_substream is not None:
 
1739
            # The stream included an inventory-delta record, but the remote
 
1740
            # side isn't new enough to support them.  So we need to send the
 
1741
            # rest of the stream via VFS.
 
1742
            self.target_repo.refresh_data()
 
1743
            return self._resume_stream_with_vfs(response, src_format)
1644
1744
        if response[0][0] == 'missing-basis':
1645
1745
            tokens, missing_keys = bencode.bdecode_as_tuple(response[0][1])
1646
1746
            resume_tokens = tokens
1649
1749
            self.target_repo.refresh_data()
1650
1750
            return [], set()
1651
1751
 
 
1752
    def _resume_stream_with_vfs(self, response, src_format):
 
1753
        """Resume sending a stream via VFS, first resending the record and
 
1754
        substream that couldn't be sent via an insert_stream verb.
 
1755
        """
 
1756
        if response[0][0] == 'missing-basis':
 
1757
            tokens, missing_keys = bencode.bdecode_as_tuple(response[0][1])
 
1758
            # Ignore missing_keys, we haven't finished inserting yet
 
1759
        else:
 
1760
            tokens = []
 
1761
        def resume_substream():
 
1762
            # Yield the substream that was interrupted.
 
1763
            for record in self._last_substream:
 
1764
                yield record
 
1765
            self._last_substream = None
 
1766
        def resume_stream():
 
1767
            # Finish sending the interrupted substream
 
1768
            yield ('inventory-deltas', resume_substream())
 
1769
            # Then simply continue sending the rest of the stream.
 
1770
            for substream_kind, substream in self._last_stream:
 
1771
                yield substream_kind, substream
 
1772
        return self._insert_real(resume_stream(), src_format, tokens)
 
1773
 
 
1774
    def _stop_stream_if_inventory_delta(self, stream):
 
1775
        """Normally this just lets the original stream pass-through unchanged.
 
1776
 
 
1777
        However if any 'inventory-deltas' substream occurs it will stop
 
1778
        streaming, and store the interrupted substream and stream in
 
1779
        self._last_substream and self._last_stream so that the stream can be
 
1780
        resumed by _resume_stream_with_vfs.
 
1781
        """
 
1782
                    
 
1783
        stream_iter = iter(stream)
 
1784
        for substream_kind, substream in stream_iter:
 
1785
            if substream_kind == 'inventory-deltas':
 
1786
                self._last_substream = substream
 
1787
                self._last_stream = stream_iter
 
1788
                return
 
1789
            else:
 
1790
                yield substream_kind, substream
 
1791
            
1652
1792
 
1653
1793
class RemoteStreamSource(repository.StreamSource):
1654
1794
    """Stream data from a remote server."""
1657
1797
        if (self.from_repository._fallback_repositories and
1658
1798
            self.to_format._fetch_order == 'topological'):
1659
1799
            return self._real_stream(self.from_repository, search)
1660
 
        return self.missing_parents_chain(search, [self.from_repository] +
1661
 
            self.from_repository._fallback_repositories)
 
1800
        sources = []
 
1801
        seen = set()
 
1802
        repos = [self.from_repository]
 
1803
        while repos:
 
1804
            repo = repos.pop(0)
 
1805
            if repo in seen:
 
1806
                continue
 
1807
            seen.add(repo)
 
1808
            repos.extend(repo._fallback_repositories)
 
1809
            sources.append(repo)
 
1810
        return self.missing_parents_chain(search, sources)
 
1811
 
 
1812
    def get_stream_for_missing_keys(self, missing_keys):
 
1813
        self.from_repository._ensure_real()
 
1814
        real_repo = self.from_repository._real_repository
 
1815
        real_source = real_repo._get_source(self.to_format)
 
1816
        return real_source.get_stream_for_missing_keys(missing_keys)
1662
1817
 
1663
1818
    def _real_stream(self, repo, search):
1664
1819
        """Get a stream for search from repo.
1671
1826
        """
1672
1827
        source = repo._get_source(self.to_format)
1673
1828
        if isinstance(source, RemoteStreamSource):
1674
 
            return repository.StreamSource.get_stream(source, search)
 
1829
            repo._ensure_real()
 
1830
            source = repo._real_repository._get_source(self.to_format)
1675
1831
        return source.get_stream(search)
1676
1832
 
1677
1833
    def _get_stream(self, repo, search):
1694
1850
            return self._real_stream(repo, search)
1695
1851
        client = repo._client
1696
1852
        medium = client._medium
1697
 
        if medium._is_remote_before((1, 13)):
1698
 
            # streaming was added in 1.13
1699
 
            return self._real_stream(repo, search)
1700
1853
        path = repo.bzrdir._path_for_remote_call(client)
1701
 
        try:
1702
 
            search_bytes = repo._serialise_search_result(search)
1703
 
            response = repo._call_with_body_bytes_expecting_body(
1704
 
                'Repository.get_stream',
1705
 
                (path, self.to_format.network_name()), search_bytes)
1706
 
            response_tuple, response_handler = response
1707
 
        except errors.UnknownSmartMethod:
1708
 
            medium._remember_remote_is_before((1,13))
 
1854
        search_bytes = repo._serialise_search_result(search)
 
1855
        args = (path, self.to_format.network_name())
 
1856
        candidate_verbs = [
 
1857
            ('Repository.get_stream_1.19', (1, 19)),
 
1858
            ('Repository.get_stream', (1, 13))]
 
1859
        found_verb = False
 
1860
        for verb, version in candidate_verbs:
 
1861
            if medium._is_remote_before(version):
 
1862
                continue
 
1863
            try:
 
1864
                response = repo._call_with_body_bytes_expecting_body(
 
1865
                    verb, args, search_bytes)
 
1866
            except errors.UnknownSmartMethod:
 
1867
                medium._remember_remote_is_before(version)
 
1868
            else:
 
1869
                response_tuple, response_handler = response
 
1870
                found_verb = True
 
1871
                break
 
1872
        if not found_verb:
1709
1873
            return self._real_stream(repo, search)
1710
1874
        if response_tuple[0] != 'ok':
1711
1875
            raise errors.UnexpectedSmartServerResponse(response_tuple)
1723
1887
        :param search: The overall search to satisfy with streams.
1724
1888
        :param sources: A list of Repository objects to query.
1725
1889
        """
1726
 
        self.serialiser = self.to_format._serializer
 
1890
        self.from_serialiser = self.from_repository._format._serializer
1727
1891
        self.seen_revs = set()
1728
1892
        self.referenced_revs = set()
1729
1893
        # If there are heads in the search, or the key count is > 0, we are not
1746
1910
    def missing_parents_rev_handler(self, substream):
1747
1911
        for content in substream:
1748
1912
            revision_bytes = content.get_bytes_as('fulltext')
1749
 
            revision = self.serialiser.read_revision_from_string(revision_bytes)
 
1913
            revision = self.from_serialiser.read_revision_from_string(
 
1914
                revision_bytes)
1750
1915
            self.seen_revs.add(content.key[-1])
1751
1916
            self.referenced_revs.update(revision.parent_ids)
1752
1917
            yield content
1875
2040
        self._ensure_real()
1876
2041
        return self._custom_format.supports_stacking()
1877
2042
 
 
2043
    def supports_set_append_revisions_only(self):
 
2044
        self._ensure_real()
 
2045
        return self._custom_format.supports_set_append_revisions_only()
 
2046
 
1878
2047
 
1879
2048
class RemoteBranch(branch.Branch, _RpcHelper):
1880
2049
    """Branch stored on a server accessed by HPSS RPC.
1899
2068
        # We intentionally don't call the parent class's __init__, because it
1900
2069
        # will try to assign to self.tags, which is a property in this subclass.
1901
2070
        # And the parent's __init__ doesn't do much anyway.
1902
 
        self._revision_id_to_revno_cache = None
1903
 
        self._partial_revision_id_to_revno_cache = {}
1904
 
        self._revision_history_cache = None
1905
 
        self._last_revision_info_cache = None
1906
 
        self._merge_sorted_revisions_cache = None
1907
2071
        self.bzrdir = remote_bzrdir
1908
2072
        if _client is not None:
1909
2073
            self._client = _client
1923
2087
        else:
1924
2088
            self._real_branch = None
1925
2089
        # Fill out expected attributes of branch for bzrlib API users.
 
2090
        self._clear_cached_state()
1926
2091
        self.base = self.bzrdir.root_transport.base
1927
2092
        self._control_files = None
1928
2093
        self._lock_mode = None
1940
2105
                    self._real_branch._format.network_name()
1941
2106
        else:
1942
2107
            self._format = format
 
2108
        # when we do _ensure_real we may need to pass ignore_fallbacks to the
 
2109
        # branch.open_branch method.
 
2110
        self._real_ignore_fallbacks = not setup_stacking
1943
2111
        if not self._format._network_name:
1944
2112
            # Did not get from open_branchV2 - old server.
1945
2113
            self._ensure_real()
1950
2118
        hooks = branch.Branch.hooks['open']
1951
2119
        for hook in hooks:
1952
2120
            hook(self)
 
2121
        self._is_stacked = False
1953
2122
        if setup_stacking:
1954
2123
            self._setup_stacking()
1955
2124
 
1961
2130
        except (errors.NotStacked, errors.UnstackableBranchFormat,
1962
2131
            errors.UnstackableRepositoryFormat), e:
1963
2132
            return
1964
 
        self._activate_fallback_location(fallback_url, None)
 
2133
        self._is_stacked = True
 
2134
        self._activate_fallback_location(fallback_url)
1965
2135
 
1966
2136
    def _get_config(self):
1967
2137
        return RemoteBranchConfig(self)
1988
2158
                raise AssertionError('smart server vfs must be enabled '
1989
2159
                    'to use vfs implementation')
1990
2160
            self.bzrdir._ensure_real()
1991
 
            self._real_branch = self.bzrdir._real_bzrdir.open_branch()
 
2161
            self._real_branch = self.bzrdir._real_bzrdir.open_branch(
 
2162
                ignore_fallbacks=self._real_ignore_fallbacks)
1992
2163
            if self.repository._real_repository is None:
1993
2164
                # Give the remote repository the matching real repo.
1994
2165
                real_repo = self._real_branch.repository
2068
2239
            raise errors.UnexpectedSmartServerResponse(response)
2069
2240
        return response[1]
2070
2241
 
 
2242
    def set_stacked_on_url(self, url):
 
2243
        branch.Branch.set_stacked_on_url(self, url)
 
2244
        if not url:
 
2245
            self._is_stacked = False
 
2246
        else:
 
2247
            self._is_stacked = True
 
2248
        
2071
2249
    def _vfs_get_tags_bytes(self):
2072
2250
        self._ensure_real()
2073
2251
        return self._real_branch._get_tags_bytes()
2083
2261
            return self._vfs_get_tags_bytes()
2084
2262
        return response[0]
2085
2263
 
 
2264
    def _vfs_set_tags_bytes(self, bytes):
 
2265
        self._ensure_real()
 
2266
        return self._real_branch._set_tags_bytes(bytes)
 
2267
 
 
2268
    def _set_tags_bytes(self, bytes):
 
2269
        medium = self._client._medium
 
2270
        if medium._is_remote_before((1, 18)):
 
2271
            self._vfs_set_tags_bytes(bytes)
 
2272
            return
 
2273
        try:
 
2274
            args = (
 
2275
                self._remote_path(), self._lock_token, self._repo_lock_token)
 
2276
            response = self._call_with_body_bytes(
 
2277
                'Branch.set_tags_bytes', args, bytes)
 
2278
        except errors.UnknownSmartMethod:
 
2279
            medium._remember_remote_is_before((1, 18))
 
2280
            self._vfs_set_tags_bytes(bytes)
 
2281
 
2086
2282
    def lock_read(self):
2087
2283
        self.repository.lock_read()
2088
2284
        if not self._lock_mode:
2142
2338
            self.repository.lock_write(self._repo_lock_token)
2143
2339
        return self._lock_token or None
2144
2340
 
2145
 
    def _set_tags_bytes(self, bytes):
2146
 
        self._ensure_real()
2147
 
        return self._real_branch._set_tags_bytes(bytes)
2148
 
 
2149
2341
    def _unlock(self, branch_token, repo_token):
2150
2342
        err_context = {'token': str((branch_token, repo_token))}
2151
2343
        response = self._call(
2200
2392
            raise NotImplementedError(self.dont_leave_lock_in_place)
2201
2393
        self._leave_lock = False
2202
2394
 
 
2395
    @needs_read_lock
 
2396
    def get_rev_id(self, revno, history=None):
 
2397
        if revno == 0:
 
2398
            return _mod_revision.NULL_REVISION
 
2399
        last_revision_info = self.last_revision_info()
 
2400
        ok, result = self.repository.get_rev_id_for_revno(
 
2401
            revno, last_revision_info)
 
2402
        if ok:
 
2403
            return result
 
2404
        missing_parent = result[1]
 
2405
        # Either the revision named by the server is missing, or its parent
 
2406
        # is.  Call get_parent_map to determine which, so that we report a
 
2407
        # useful error.
 
2408
        parent_map = self.repository.get_parent_map([missing_parent])
 
2409
        if missing_parent in parent_map:
 
2410
            missing_parent = parent_map[missing_parent]
 
2411
        raise errors.RevisionNotPresent(missing_parent, self.repository)
 
2412
 
2203
2413
    def _last_revision_info(self):
2204
2414
        response = self._call('Branch.last_revision_info', self._remote_path())
2205
2415
        if response[0] != 'ok':
2210
2420
 
2211
2421
    def _gen_revision_history(self):
2212
2422
        """See Branch._gen_revision_history()."""
 
2423
        if self._is_stacked:
 
2424
            self._ensure_real()
 
2425
            return self._real_branch._gen_revision_history()
2213
2426
        response_tuple, response_handler = self._call_expecting_body(
2214
2427
            'Branch.revision_history', self._remote_path())
2215
2428
        if response_tuple[0] != 'ok':
2550
2763
                    'Missing key %r in context %r', key_err.args[0], context)
2551
2764
                raise err
2552
2765
 
2553
 
    if err.error_verb == 'NoSuchRevision':
 
2766
    if err.error_verb == 'IncompatibleRepositories':
 
2767
        raise errors.IncompatibleRepositories(err.error_args[0],
 
2768
            err.error_args[1], err.error_args[2])
 
2769
    elif err.error_verb == 'NoSuchRevision':
2554
2770
        raise NoSuchRevision(find('branch'), err.error_args[0])
2555
2771
    elif err.error_verb == 'nosuchrevision':
2556
2772
        raise NoSuchRevision(find('repository'), err.error_args[0])