~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/branch.py

Revert the dirstate/transform changes, so we have a pure 'lstat/fstat' change.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2005-2011 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
27
27
        config as _mod_config,
28
28
        debug,
29
29
        errors,
 
30
        fetch,
 
31
        graph as _mod_graph,
30
32
        lockdir,
31
33
        lockable_files,
 
34
        remote,
32
35
        repository,
33
36
        revision as _mod_revision,
34
37
        rio,
46
49
    )
47
50
""")
48
51
 
49
 
from bzrlib.decorators import needs_read_lock, needs_write_lock, only_raises
50
 
from bzrlib.hooks import HookPoint, Hooks
 
52
from bzrlib import (
 
53
    controldir,
 
54
    )
 
55
from bzrlib.decorators import (
 
56
    needs_read_lock,
 
57
    needs_write_lock,
 
58
    only_raises,
 
59
    )
 
60
from bzrlib.hooks import Hooks
51
61
from bzrlib.inter import InterObject
52
 
from bzrlib.lock import _RelockDebugMixin
 
62
from bzrlib.lock import _RelockDebugMixin, LogicalLockResult
53
63
from bzrlib import registry
54
64
from bzrlib.symbol_versioning import (
55
65
    deprecated_in,
63
73
BZR_BRANCH_FORMAT_6 = "Bazaar Branch Format 6 (bzr 0.15)\n"
64
74
 
65
75
 
66
 
class Branch(bzrdir.ControlComponent):
 
76
class Branch(controldir.ControlComponent):
67
77
    """Branch holding a history of revisions.
68
78
 
69
79
    :ivar base:
70
80
        Base directory/url of the branch; using control_url and
71
81
        control_transport is more standardized.
72
 
 
73
 
    hooks: An instance of BranchHooks.
 
82
    :ivar hooks: An instance of BranchHooks.
 
83
    :ivar _master_branch_cache: cached result of get_master_branch, see
 
84
        _clear_cached_state.
74
85
    """
75
86
    # this is really an instance variable - FIXME move it there
76
87
    # - RBC 20060112
90
101
        self._revision_id_to_revno_cache = None
91
102
        self._partial_revision_id_to_revno_cache = {}
92
103
        self._partial_revision_history_cache = []
 
104
        self._tags_bytes = None
93
105
        self._last_revision_info_cache = None
 
106
        self._master_branch_cache = None
94
107
        self._merge_sorted_revisions_cache = None
95
108
        self._open_hook()
96
109
        hooks = Branch.hooks['open']
102
115
 
103
116
    def _activate_fallback_location(self, url):
104
117
        """Activate the branch/repository from url as a fallback repository."""
 
118
        for existing_fallback_repo in self.repository._fallback_repositories:
 
119
            if existing_fallback_repo.user_url == url:
 
120
                # This fallback is already configured.  This probably only
 
121
                # happens because BzrDir.sprout is a horrible mess.  To avoid
 
122
                # confusing _unstack we don't add this a second time.
 
123
                mutter('duplicate activation of fallback %r on %r', url, self)
 
124
                return
105
125
        repo = self._get_fallback_repository(url)
106
126
        if repo.has_same_location(self.repository):
107
127
            raise errors.UnstackableLocationError(self.user_url, url)
197
217
        return self.supports_tags() and self.tags.get_tag_dict()
198
218
 
199
219
    def get_config(self):
 
220
        """Get a bzrlib.config.BranchConfig for this Branch.
 
221
 
 
222
        This can then be used to get and set configuration options for the
 
223
        branch.
 
224
 
 
225
        :return: A bzrlib.config.BranchConfig.
 
226
        """
200
227
        return BranchConfig(self)
201
228
 
202
229
    def _get_config(self):
218
245
            possible_transports=[self.bzrdir.root_transport])
219
246
        return a_branch.repository
220
247
 
 
248
    @needs_read_lock
221
249
    def _get_tags_bytes(self):
222
250
        """Get the bytes of a serialised tags dict.
223
251
 
230
258
        :return: The bytes of the tags file.
231
259
        :seealso: Branch._set_tags_bytes.
232
260
        """
233
 
        return self._transport.get_bytes('tags')
 
261
        if self._tags_bytes is None:
 
262
            self._tags_bytes = self._transport.get_bytes('tags')
 
263
        return self._tags_bytes
234
264
 
235
265
    def _get_nick(self, local=False, possible_transports=None):
236
266
        config = self.get_config()
238
268
        if not local and not config.has_explicit_nickname():
239
269
            try:
240
270
                master = self.get_master_branch(possible_transports)
 
271
                if master and self.user_url == master.user_url:
 
272
                    raise errors.RecursiveBind(self.user_url)
241
273
                if master is not None:
242
274
                    # return the master branch value
243
275
                    return master.nick
 
276
            except errors.RecursiveBind, e:
 
277
                raise e
244
278
            except errors.BzrError, e:
245
279
                # Silently fall back to local implicit nick if the master is
246
280
                # unavailable
295
329
    def lock_read(self):
296
330
        """Lock the branch for read operations.
297
331
 
298
 
        :return: An object with an unlock method which will release the lock
299
 
            obtained.
 
332
        :return: A bzrlib.lock.LogicalLockResult.
300
333
        """
301
334
        raise NotImplementedError(self.lock_read)
302
335
 
637
670
        raise errors.UnsupportedOperation(self.get_reference_info, self)
638
671
 
639
672
    @needs_write_lock
640
 
    def fetch(self, from_branch, last_revision=None, pb=None):
 
673
    def fetch(self, from_branch, last_revision=None, fetch_spec=None):
641
674
        """Copy revisions from from_branch into this branch.
642
675
 
643
676
        :param from_branch: Where to copy from.
644
677
        :param last_revision: What revision to stop at (None for at the end
645
678
                              of the branch.
646
 
        :param pb: An optional progress bar to use.
 
679
        :param fetch_spec: If specified, a SearchResult or
 
680
            PendingAncestryResult that describes which revisions to copy.  This
 
681
            allows copying multiple heads at once.  Mutually exclusive with
 
682
            last_revision.
647
683
        :return: None
648
684
        """
649
 
        if self.base == from_branch.base:
650
 
            return (0, [])
651
 
        if pb is not None:
652
 
            symbol_versioning.warn(
653
 
                symbol_versioning.deprecated_in((1, 14, 0))
654
 
                % "pb parameter to fetch()")
655
 
        from_branch.lock_read()
656
 
        try:
657
 
            if last_revision is None:
658
 
                last_revision = from_branch.last_revision()
659
 
                last_revision = _mod_revision.ensure_null(last_revision)
660
 
            return self.repository.fetch(from_branch.repository,
661
 
                                         revision_id=last_revision,
662
 
                                         pb=pb)
663
 
        finally:
664
 
            from_branch.unlock()
 
685
        return InterBranch.get(from_branch, self).fetch(last_revision,
 
686
            fetch_spec)
665
687
 
666
688
    def get_bound_location(self):
667
689
        """Return the URL of the branch we are bound to.
778
800
 
779
801
    def _unstack(self):
780
802
        """Change a branch to be unstacked, copying data as needed.
781
 
        
 
803
 
782
804
        Don't call this directly, use set_stacked_on_url(None).
783
805
        """
784
806
        pb = ui.ui_factory.nested_progress_bar()
793
815
            old_repository = self.repository
794
816
            if len(old_repository._fallback_repositories) != 1:
795
817
                raise AssertionError("can't cope with fallback repositories "
796
 
                    "of %r" % (self.repository,))
797
 
            # unlock it, including unlocking the fallback
 
818
                    "of %r (fallbacks: %r)" % (old_repository,
 
819
                        old_repository._fallback_repositories))
 
820
            # Open the new repository object.
 
821
            # Repositories don't offer an interface to remove fallback
 
822
            # repositories today; take the conceptually simpler option and just
 
823
            # reopen it.  We reopen it starting from the URL so that we
 
824
            # get a separate connection for RemoteRepositories and can
 
825
            # stream from one of them to the other.  This does mean doing
 
826
            # separate SSH connection setup, but unstacking is not a
 
827
            # common operation so it's tolerable.
 
828
            new_bzrdir = bzrdir.BzrDir.open(self.bzrdir.root_transport.base)
 
829
            new_repository = new_bzrdir.find_repository()
 
830
            if new_repository._fallback_repositories:
 
831
                raise AssertionError("didn't expect %r to have "
 
832
                    "fallback_repositories"
 
833
                    % (self.repository,))
 
834
            # Replace self.repository with the new repository.
 
835
            # Do our best to transfer the lock state (i.e. lock-tokens and
 
836
            # lock count) of self.repository to the new repository.
 
837
            lock_token = old_repository.lock_write().repository_token
 
838
            self.repository = new_repository
 
839
            if isinstance(self, remote.RemoteBranch):
 
840
                # Remote branches can have a second reference to the old
 
841
                # repository that need to be replaced.
 
842
                if self._real_branch is not None:
 
843
                    self._real_branch.repository = new_repository
 
844
            self.repository.lock_write(token=lock_token)
 
845
            if lock_token is not None:
 
846
                old_repository.leave_lock_in_place()
798
847
            old_repository.unlock()
 
848
            if lock_token is not None:
 
849
                # XXX: self.repository.leave_lock_in_place() before this
 
850
                # function will not be preserved.  Fortunately that doesn't
 
851
                # affect the current default format (2a), and would be a
 
852
                # corner-case anyway.
 
853
                #  - Andrew Bennetts, 2010/06/30
 
854
                self.repository.dont_leave_lock_in_place()
 
855
            old_lock_count = 0
 
856
            while True:
 
857
                try:
 
858
                    old_repository.unlock()
 
859
                except errors.LockNotHeld:
 
860
                    break
 
861
                old_lock_count += 1
 
862
            if old_lock_count == 0:
 
863
                raise AssertionError(
 
864
                    'old_repository should have been locked at least once.')
 
865
            for i in range(old_lock_count-1):
 
866
                self.repository.lock_write()
 
867
            # Fetch from the old repository into the new.
799
868
            old_repository.lock_read()
800
869
            try:
801
 
                # Repositories don't offer an interface to remove fallback
802
 
                # repositories today; take the conceptually simpler option and just
803
 
                # reopen it.  We reopen it starting from the URL so that we
804
 
                # get a separate connection for RemoteRepositories and can
805
 
                # stream from one of them to the other.  This does mean doing
806
 
                # separate SSH connection setup, but unstacking is not a
807
 
                # common operation so it's tolerable.
808
 
                new_bzrdir = bzrdir.BzrDir.open(self.bzrdir.root_transport.base)
809
 
                new_repository = new_bzrdir.find_repository()
810
 
                self.repository = new_repository
811
 
                if self.repository._fallback_repositories:
812
 
                    raise AssertionError("didn't expect %r to have "
813
 
                        "fallback_repositories"
814
 
                        % (self.repository,))
815
 
                # this is not paired with an unlock because it's just restoring
816
 
                # the previous state; the lock's released when set_stacked_on_url
817
 
                # returns
818
 
                self.repository.lock_write()
819
870
                # XXX: If you unstack a branch while it has a working tree
820
871
                # with a pending merge, the pending-merged revisions will no
821
872
                # longer be present.  You can (probably) revert and remerge.
822
 
                #
823
 
                # XXX: This only fetches up to the tip of the repository; it
824
 
                # doesn't bring across any tags.  That's fairly consistent
825
 
                # with how branch works, but perhaps not ideal.
826
 
                self.repository.fetch(old_repository,
827
 
                    revision_id=self.last_revision(),
828
 
                    find_ghosts=True)
 
873
                try:
 
874
                    tags_to_fetch = set(self.tags.get_reverse_tag_dict())
 
875
                except errors.TagsNotSupported:
 
876
                    tags_to_fetch = set()
 
877
                fetch_spec = _mod_graph.NotInOtherForRevs(self.repository,
 
878
                    old_repository, required_ids=[self.last_revision()],
 
879
                    if_present_ids=tags_to_fetch, find_ghosts=True).execute()
 
880
                self.repository.fetch(old_repository, fetch_spec=fetch_spec)
829
881
            finally:
830
882
                old_repository.unlock()
831
883
        finally:
836
888
 
837
889
        :seealso: Branch._get_tags_bytes.
838
890
        """
839
 
        return _run_with_write_locked_target(self, self._transport.put_bytes,
840
 
            'tags', bytes)
 
891
        return _run_with_write_locked_target(self, self._set_tags_bytes_locked,
 
892
                bytes)
 
893
 
 
894
    def _set_tags_bytes_locked(self, bytes):
 
895
        self._tags_bytes = bytes
 
896
        return self._transport.put_bytes('tags', bytes)
841
897
 
842
898
    def _cache_revision_history(self, rev_history):
843
899
        """Set the cached revision history to rev_history.
870
926
        self._revision_history_cache = None
871
927
        self._revision_id_to_revno_cache = None
872
928
        self._last_revision_info_cache = None
 
929
        self._master_branch_cache = None
873
930
        self._merge_sorted_revisions_cache = None
874
931
        self._partial_revision_history_cache = []
875
932
        self._partial_revision_id_to_revno_cache = {}
 
933
        self._tags_bytes = None
876
934
 
877
935
    def _gen_revision_history(self):
878
936
        """Return sequence of revision hashes on to this branch.
939
997
        else:
940
998
            return (0, _mod_revision.NULL_REVISION)
941
999
 
942
 
    @deprecated_method(deprecated_in((1, 6, 0)))
943
 
    def missing_revisions(self, other, stop_revision=None):
944
 
        """Return a list of new revisions that would perfectly fit.
945
 
 
946
 
        If self and other have not diverged, return a list of the revisions
947
 
        present in other, but missing from self.
948
 
        """
949
 
        self_history = self.revision_history()
950
 
        self_len = len(self_history)
951
 
        other_history = other.revision_history()
952
 
        other_len = len(other_history)
953
 
        common_index = min(self_len, other_len) -1
954
 
        if common_index >= 0 and \
955
 
            self_history[common_index] != other_history[common_index]:
956
 
            raise errors.DivergedBranches(self, other)
957
 
 
958
 
        if stop_revision is None:
959
 
            stop_revision = other_len
960
 
        else:
961
 
            if stop_revision > other_len:
962
 
                raise errors.NoSuchRevision(self, stop_revision)
963
 
        return other_history[self_len:stop_revision]
964
 
 
965
 
    @needs_write_lock
966
1000
    def update_revisions(self, other, stop_revision=None, overwrite=False,
967
 
                         graph=None):
 
1001
                         graph=None, fetch_tags=True):
968
1002
        """Pull in new perfect-fit revisions.
969
1003
 
970
1004
        :param other: Another Branch to pull from
973
1007
            to see if it is a proper descendant.
974
1008
        :param graph: A Graph object that can be used to query history
975
1009
            information. This can be None.
 
1010
        :param fetch_tags: Flag that specifies if tags from other should be
 
1011
            fetched too.
976
1012
        :return: None
977
1013
        """
978
1014
        return InterBranch.get(other, self).update_revisions(stop_revision,
979
 
            overwrite, graph)
 
1015
            overwrite, graph, fetch_tags=fetch_tags)
980
1016
 
 
1017
    @deprecated_method(deprecated_in((2, 4, 0)))
981
1018
    def import_last_revision_info(self, source_repo, revno, revid):
982
1019
        """Set the last revision info, importing from another repo if necessary.
983
1020
 
984
 
        This is used by the bound branch code to upload a revision to
985
 
        the master branch first before updating the tip of the local branch.
986
 
 
987
1021
        :param source_repo: Source repository to optionally fetch from
988
1022
        :param revno: Revision number of the new tip
989
1023
        :param revid: Revision id of the new tip
992
1026
            self.repository.fetch(source_repo, revision_id=revid)
993
1027
        self.set_last_revision_info(revno, revid)
994
1028
 
 
1029
    def import_last_revision_info_and_tags(self, source, revno, revid):
 
1030
        """Set the last revision info, importing from another repo if necessary.
 
1031
 
 
1032
        This is used by the bound branch code to upload a revision to
 
1033
        the master branch first before updating the tip of the local branch.
 
1034
        Revisions referenced by source's tags are also transferred.
 
1035
 
 
1036
        :param source: Source branch to optionally fetch from
 
1037
        :param revno: Revision number of the new tip
 
1038
        :param revid: Revision id of the new tip
 
1039
        """
 
1040
        if not self.repository.has_same_location(source.repository):
 
1041
            try:
 
1042
                tags_to_fetch = set(source.tags.get_reverse_tag_dict())
 
1043
            except errors.TagsNotSupported:
 
1044
                tags_to_fetch = set()
 
1045
            fetch_spec = _mod_graph.NotInOtherForRevs(self.repository,
 
1046
                source.repository, [revid],
 
1047
                if_present_ids=tags_to_fetch).execute()
 
1048
            self.repository.fetch(source.repository, fetch_spec=fetch_spec)
 
1049
        self.set_last_revision_info(revno, revid)
 
1050
 
995
1051
    def revision_id_to_revno(self, revision_id):
996
1052
        """Given a revision id, return its revno"""
997
1053
        if _mod_revision.is_null(revision_id):
1017
1073
            self._extend_partial_history(distance_from_last)
1018
1074
        return self._partial_revision_history_cache[distance_from_last]
1019
1075
 
1020
 
    @needs_write_lock
1021
1076
    def pull(self, source, overwrite=False, stop_revision=None,
1022
1077
             possible_transports=None, *args, **kwargs):
1023
1078
        """Mirror source into this branch.
1219
1274
        return result
1220
1275
 
1221
1276
    @needs_read_lock
1222
 
    def sprout(self, to_bzrdir, revision_id=None, repository_policy=None):
 
1277
    def sprout(self, to_bzrdir, revision_id=None, repository_policy=None,
 
1278
            repository=None):
1223
1279
        """Create a new line of development from the branch, into to_bzrdir.
1224
1280
 
1225
1281
        to_bzrdir controls the branch format.
1230
1286
        if (repository_policy is not None and
1231
1287
            repository_policy.requires_stacking()):
1232
1288
            to_bzrdir._format.require_stacking(_skip_repo=True)
1233
 
        result = to_bzrdir.create_branch()
 
1289
        result = to_bzrdir.create_branch(repository=repository)
1234
1290
        result.lock_write()
1235
1291
        try:
1236
1292
            if repository_policy is not None:
1266
1322
                revno = 1
1267
1323
        destination.set_last_revision_info(revno, revision_id)
1268
1324
 
1269
 
    @needs_read_lock
1270
1325
    def copy_content_into(self, destination, revision_id=None):
1271
1326
        """Copy the content of self into destination.
1272
1327
 
1273
1328
        revision_id: if not None, the revision history in the new branch will
1274
1329
                     be truncated to end with revision_id.
1275
1330
        """
1276
 
        self.update_references(destination)
1277
 
        self._synchronize_history(destination, revision_id)
1278
 
        try:
1279
 
            parent = self.get_parent()
1280
 
        except errors.InaccessibleParent, e:
1281
 
            mutter('parent was not accessible to copy: %s', e)
1282
 
        else:
1283
 
            if parent:
1284
 
                destination.set_parent(parent)
1285
 
        if self._push_should_merge_tags():
1286
 
            self.tags.merge_to(destination.tags)
 
1331
        return InterBranch.get(self, destination).copy_content_into(
 
1332
            revision_id=revision_id)
1287
1333
 
1288
1334
    def update_references(self, target):
1289
1335
        if not getattr(self._format, 'supports_reference_locations', False):
1334
1380
        """Return the most suitable metadir for a checkout of this branch.
1335
1381
        Weaves are used if this branch's repository uses weaves.
1336
1382
        """
1337
 
        if isinstance(self.bzrdir, bzrdir.BzrDirPreSplitOut):
1338
 
            from bzrlib.repofmt import weaverepo
1339
 
            format = bzrdir.BzrDirMetaFormat1()
1340
 
            format.repository_format = weaverepo.RepositoryFormat7()
1341
 
        else:
1342
 
            format = self.repository.bzrdir.checkout_metadir()
1343
 
            format.set_branch_format(self._format)
 
1383
        format = self.repository.bzrdir.checkout_metadir()
 
1384
        format.set_branch_format(self._format)
1344
1385
        return format
1345
1386
 
1346
1387
    def create_clone_on_transport(self, to_transport, revision_id=None,
1347
 
        stacked_on=None, create_prefix=False, use_existing_dir=False):
 
1388
        stacked_on=None, create_prefix=False, use_existing_dir=False,
 
1389
        no_tree=None):
1348
1390
        """Create a clone of this branch and its bzrdir.
1349
1391
 
1350
1392
        :param to_transport: The transport to clone onto.
1357
1399
        """
1358
1400
        # XXX: Fix the bzrdir API to allow getting the branch back from the
1359
1401
        # clone call. Or something. 20090224 RBC/spiv.
 
1402
        # XXX: Should this perhaps clone colocated branches as well, 
 
1403
        # rather than just the default branch? 20100319 JRV
1360
1404
        if revision_id is None:
1361
1405
            revision_id = self.last_revision()
1362
1406
        dir_to = self.bzrdir.clone_on_transport(to_transport,
1363
1407
            revision_id=revision_id, stacked_on=stacked_on,
1364
 
            create_prefix=create_prefix, use_existing_dir=use_existing_dir)
 
1408
            create_prefix=create_prefix, use_existing_dir=use_existing_dir,
 
1409
            no_tree=no_tree)
1365
1410
        return dir_to.open_branch()
1366
1411
 
1367
1412
    def create_checkout(self, to_location, revision_id=None,
1482
1527
        else:
1483
1528
            raise AssertionError("invalid heads: %r" % (heads,))
1484
1529
 
1485
 
 
1486
 
class BranchFormat(object):
 
1530
    def heads_to_fetch(self):
 
1531
        """Return the heads that must and that should be fetched to copy this
 
1532
        branch into another repo.
 
1533
 
 
1534
        :returns: a 2-tuple of (must_fetch, if_present_fetch).  must_fetch is a
 
1535
            set of heads that must be fetched.  if_present_fetch is a set of
 
1536
            heads that must be fetched if present, but no error is necessary if
 
1537
            they are not present.
 
1538
        """
 
1539
        # For bzr native formats must_fetch is just the tip, and if_present_fetch
 
1540
        # are the tags.
 
1541
        must_fetch = set([self.last_revision()])
 
1542
        try:
 
1543
            if_present_fetch = set(self.tags.get_reverse_tag_dict())
 
1544
        except errors.TagsNotSupported:
 
1545
            if_present_fetch = set()
 
1546
        must_fetch.discard(_mod_revision.NULL_REVISION)
 
1547
        if_present_fetch.discard(_mod_revision.NULL_REVISION)
 
1548
        return must_fetch, if_present_fetch
 
1549
 
 
1550
 
 
1551
class BranchFormat(controldir.ControlComponentFormat):
1487
1552
    """An encapsulation of the initialization and open routines for a format.
1488
1553
 
1489
1554
    Formats provide three things:
1492
1557
     * an open routine.
1493
1558
 
1494
1559
    Formats are placed in an dict by their format string for reference
1495
 
    during branch opening. Its not required that these be instances, they
 
1560
    during branch opening. It's not required that these be instances, they
1496
1561
    can be classes themselves with class methods - it simply depends on
1497
1562
    whether state is needed for a given format or not.
1498
1563
 
1501
1566
    object will be created every time regardless.
1502
1567
    """
1503
1568
 
1504
 
    _default_format = None
1505
 
    """The default format used for new branches."""
1506
 
 
1507
 
    _formats = {}
1508
 
    """The known formats."""
1509
 
 
1510
1569
    can_set_append_revisions_only = True
1511
1570
 
1512
1571
    def __eq__(self, other):
1521
1580
        try:
1522
1581
            transport = a_bzrdir.get_branch_transport(None, name=name)
1523
1582
            format_string = transport.get_bytes("format")
1524
 
            return klass._formats[format_string]
 
1583
            return format_registry.get(format_string)
1525
1584
        except errors.NoSuchFile:
1526
1585
            raise errors.NotBranchError(path=transport.base, bzrdir=a_bzrdir)
1527
1586
        except KeyError:
1528
1587
            raise errors.UnknownFormatError(format=format_string, kind='branch')
1529
1588
 
1530
1589
    @classmethod
 
1590
    @deprecated_method(deprecated_in((2, 4, 0)))
1531
1591
    def get_default_format(klass):
1532
1592
        """Return the current default format."""
1533
 
        return klass._default_format
1534
 
 
1535
 
    def get_reference(self, a_bzrdir):
 
1593
        return format_registry.get_default()
 
1594
 
 
1595
    @classmethod
 
1596
    @deprecated_method(deprecated_in((2, 4, 0)))
 
1597
    def get_formats(klass):
 
1598
        """Get all the known formats.
 
1599
 
 
1600
        Warning: This triggers a load of all lazy registered formats: do not
 
1601
        use except when that is desireed.
 
1602
        """
 
1603
        return format_registry._get_all()
 
1604
 
 
1605
    def get_reference(self, a_bzrdir, name=None):
1536
1606
        """Get the target reference of the branch in a_bzrdir.
1537
1607
 
1538
1608
        format probing must have been completed before calling
1540
1610
        in a_bzrdir is correct.
1541
1611
 
1542
1612
        :param a_bzrdir: The bzrdir to get the branch data from.
 
1613
        :param name: Name of the colocated branch to fetch
1543
1614
        :return: None if the branch is not a reference branch.
1544
1615
        """
1545
1616
        return None
1546
1617
 
1547
1618
    @classmethod
1548
 
    def set_reference(self, a_bzrdir, to_branch):
 
1619
    def set_reference(self, a_bzrdir, name, to_branch):
1549
1620
        """Set the target reference of the branch in a_bzrdir.
1550
1621
 
1551
1622
        format probing must have been completed before calling
1553
1624
        in a_bzrdir is correct.
1554
1625
 
1555
1626
        :param a_bzrdir: The bzrdir to set the branch reference for.
 
1627
        :param name: Name of colocated branch to set, None for default
1556
1628
        :param to_branch: branch that the checkout is to reference
1557
1629
        """
1558
1630
        raise NotImplementedError(self.set_reference)
1574
1646
            hook(params)
1575
1647
 
1576
1648
    def _initialize_helper(self, a_bzrdir, utf8_files, name=None,
1577
 
                           lock_type='metadir', set_format=True):
 
1649
                           repository=None):
1578
1650
        """Initialize a branch in a bzrdir, with specified files
1579
1651
 
1580
1652
        :param a_bzrdir: The bzrdir to initialize the branch in
1581
1653
        :param utf8_files: The files to create as a list of
1582
1654
            (filename, content) tuples
1583
1655
        :param name: Name of colocated branch to create, if any
1584
 
        :param set_format: If True, set the format with
1585
 
            self.get_format_string.  (BzrBranch4 has its format set
1586
 
            elsewhere)
1587
1656
        :return: a branch in this format
1588
1657
        """
1589
1658
        mutter('creating branch %r in %s', self, a_bzrdir.user_url)
1590
1659
        branch_transport = a_bzrdir.get_branch_transport(self, name=name)
1591
 
        lock_map = {
1592
 
            'metadir': ('lock', lockdir.LockDir),
1593
 
            'branch4': ('branch-lock', lockable_files.TransportLock),
1594
 
        }
1595
 
        lock_name, lock_class = lock_map[lock_type]
1596
1660
        control_files = lockable_files.LockableFiles(branch_transport,
1597
 
            lock_name, lock_class)
 
1661
            'lock', lockdir.LockDir)
1598
1662
        control_files.create_lock()
 
1663
        control_files.lock_write()
1599
1664
        try:
1600
 
            control_files.lock_write()
1601
 
        except errors.LockContention:
1602
 
            if lock_type != 'branch4':
1603
 
                raise
1604
 
            lock_taken = False
1605
 
        else:
1606
 
            lock_taken = True
1607
 
        if set_format:
1608
1665
            utf8_files += [('format', self.get_format_string())]
1609
 
        try:
1610
1666
            for (filename, content) in utf8_files:
1611
1667
                branch_transport.put_bytes(
1612
1668
                    filename, content,
1613
1669
                    mode=a_bzrdir._get_file_mode())
1614
1670
        finally:
1615
 
            if lock_taken:
1616
 
                control_files.unlock()
1617
 
        branch = self.open(a_bzrdir, name, _found=True)
 
1671
            control_files.unlock()
 
1672
        branch = self.open(a_bzrdir, name, _found=True,
 
1673
                found_repository=repository)
1618
1674
        self._run_post_branch_init_hooks(a_bzrdir, name, branch)
1619
1675
        return branch
1620
1676
 
1621
 
    def initialize(self, a_bzrdir, name=None):
 
1677
    def initialize(self, a_bzrdir, name=None, repository=None):
1622
1678
        """Create a branch of this format in a_bzrdir.
1623
1679
        
1624
1680
        :param name: Name of the colocated branch to create.
1658
1714
        """
1659
1715
        raise NotImplementedError(self.network_name)
1660
1716
 
1661
 
    def open(self, a_bzrdir, name=None, _found=False, ignore_fallbacks=False):
 
1717
    def open(self, a_bzrdir, name=None, _found=False, ignore_fallbacks=False,
 
1718
            found_repository=None):
1662
1719
        """Return the branch object for a_bzrdir
1663
1720
 
1664
1721
        :param a_bzrdir: A BzrDir that contains a branch.
1671
1728
        raise NotImplementedError(self.open)
1672
1729
 
1673
1730
    @classmethod
 
1731
    @deprecated_method(deprecated_in((2, 4, 0)))
1674
1732
    def register_format(klass, format):
1675
 
        """Register a metadir format."""
1676
 
        klass._formats[format.get_format_string()] = format
1677
 
        # Metadir formats have a network name of their format string, and get
1678
 
        # registered as class factories.
1679
 
        network_format_registry.register(format.get_format_string(), format.__class__)
 
1733
        """Register a metadir format.
 
1734
 
 
1735
        See MetaDirBranchFormatFactory for the ability to register a format
 
1736
        without loading the code the format needs until it is actually used.
 
1737
        """
 
1738
        format_registry.register(format)
1680
1739
 
1681
1740
    @classmethod
 
1741
    @deprecated_method(deprecated_in((2, 4, 0)))
1682
1742
    def set_default_format(klass, format):
1683
 
        klass._default_format = format
 
1743
        format_registry.set_default(format)
1684
1744
 
1685
1745
    def supports_set_append_revisions_only(self):
1686
1746
        """True if this format supports set_append_revisions_only."""
1690
1750
        """True if this format records a stacked-on branch."""
1691
1751
        return False
1692
1752
 
 
1753
    def supports_leaving_lock(self):
 
1754
        """True if this format supports leaving locks in place."""
 
1755
        return False # by default
 
1756
 
1693
1757
    @classmethod
 
1758
    @deprecated_method(deprecated_in((2, 4, 0)))
1694
1759
    def unregister_format(klass, format):
1695
 
        del klass._formats[format.get_format_string()]
 
1760
        format_registry.remove(format)
1696
1761
 
1697
1762
    def __str__(self):
1698
1763
        return self.get_format_description().rstrip()
1702
1767
        return False  # by default
1703
1768
 
1704
1769
 
 
1770
class MetaDirBranchFormatFactory(registry._LazyObjectGetter):
 
1771
    """A factory for a BranchFormat object, permitting simple lazy registration.
 
1772
    
 
1773
    While none of the built in BranchFormats are lazy registered yet,
 
1774
    bzrlib.tests.test_branch.TestMetaDirBranchFormatFactory demonstrates how to
 
1775
    use it, and the bzr-loom plugin uses it as well (see
 
1776
    bzrlib.plugins.loom.formats).
 
1777
    """
 
1778
 
 
1779
    def __init__(self, format_string, module_name, member_name):
 
1780
        """Create a MetaDirBranchFormatFactory.
 
1781
 
 
1782
        :param format_string: The format string the format has.
 
1783
        :param module_name: Module to load the format class from.
 
1784
        :param member_name: Attribute name within the module for the format class.
 
1785
        """
 
1786
        registry._LazyObjectGetter.__init__(self, module_name, member_name)
 
1787
        self._format_string = format_string
 
1788
        
 
1789
    def get_format_string(self):
 
1790
        """See BranchFormat.get_format_string."""
 
1791
        return self._format_string
 
1792
 
 
1793
    def __call__(self):
 
1794
        """Used for network_format_registry support."""
 
1795
        return self.get_obj()()
 
1796
 
 
1797
 
1705
1798
class BranchHooks(Hooks):
1706
1799
    """A dictionary mapping hook name to a list of callables for branch hooks.
1707
1800
 
1715
1808
        These are all empty initially, because by default nothing should get
1716
1809
        notified.
1717
1810
        """
1718
 
        Hooks.__init__(self)
1719
 
        self.create_hook(HookPoint('set_rh',
 
1811
        Hooks.__init__(self, "bzrlib.branch", "Branch.hooks")
 
1812
        self.add_hook('set_rh',
1720
1813
            "Invoked whenever the revision history has been set via "
1721
1814
            "set_revision_history. The api signature is (branch, "
1722
1815
            "revision_history), and the branch will be write-locked. "
1723
1816
            "The set_rh hook can be expensive for bzr to trigger, a better "
1724
 
            "hook to use is Branch.post_change_branch_tip.", (0, 15), None))
1725
 
        self.create_hook(HookPoint('open',
 
1817
            "hook to use is Branch.post_change_branch_tip.", (0, 15))
 
1818
        self.add_hook('open',
1726
1819
            "Called with the Branch object that has been opened after a "
1727
 
            "branch is opened.", (1, 8), None))
1728
 
        self.create_hook(HookPoint('post_push',
 
1820
            "branch is opened.", (1, 8))
 
1821
        self.add_hook('post_push',
1729
1822
            "Called after a push operation completes. post_push is called "
1730
1823
            "with a bzrlib.branch.BranchPushResult object and only runs in the "
1731
 
            "bzr client.", (0, 15), None))
1732
 
        self.create_hook(HookPoint('post_pull',
 
1824
            "bzr client.", (0, 15))
 
1825
        self.add_hook('post_pull',
1733
1826
            "Called after a pull operation completes. post_pull is called "
1734
1827
            "with a bzrlib.branch.PullResult object and only runs in the "
1735
 
            "bzr client.", (0, 15), None))
1736
 
        self.create_hook(HookPoint('pre_commit',
1737
 
            "Called after a commit is calculated but before it is is "
 
1828
            "bzr client.", (0, 15))
 
1829
        self.add_hook('pre_commit',
 
1830
            "Called after a commit is calculated but before it is "
1738
1831
            "completed. pre_commit is called with (local, master, old_revno, "
1739
1832
            "old_revid, future_revno, future_revid, tree_delta, future_tree"
1740
1833
            "). old_revid is NULL_REVISION for the first commit to a branch, "
1742
1835
            "basis revision. hooks MUST NOT modify this delta. "
1743
1836
            " future_tree is an in-memory tree obtained from "
1744
1837
            "CommitBuilder.revision_tree() and hooks MUST NOT modify this "
1745
 
            "tree.", (0,91), None))
1746
 
        self.create_hook(HookPoint('post_commit',
 
1838
            "tree.", (0,91))
 
1839
        self.add_hook('post_commit',
1747
1840
            "Called in the bzr client after a commit has completed. "
1748
1841
            "post_commit is called with (local, master, old_revno, old_revid, "
1749
1842
            "new_revno, new_revid). old_revid is NULL_REVISION for the first "
1750
 
            "commit to a branch.", (0, 15), None))
1751
 
        self.create_hook(HookPoint('post_uncommit',
 
1843
            "commit to a branch.", (0, 15))
 
1844
        self.add_hook('post_uncommit',
1752
1845
            "Called in the bzr client after an uncommit completes. "
1753
1846
            "post_uncommit is called with (local, master, old_revno, "
1754
1847
            "old_revid, new_revno, new_revid) where local is the local branch "
1755
1848
            "or None, master is the target branch, and an empty branch "
1756
 
            "receives new_revno of 0, new_revid of None.", (0, 15), None))
1757
 
        self.create_hook(HookPoint('pre_change_branch_tip',
 
1849
            "receives new_revno of 0, new_revid of None.", (0, 15))
 
1850
        self.add_hook('pre_change_branch_tip',
1758
1851
            "Called in bzr client and server before a change to the tip of a "
1759
1852
            "branch is made. pre_change_branch_tip is called with a "
1760
1853
            "bzrlib.branch.ChangeBranchTipParams. Note that push, pull, "
1761
 
            "commit, uncommit will all trigger this hook.", (1, 6), None))
1762
 
        self.create_hook(HookPoint('post_change_branch_tip',
 
1854
            "commit, uncommit will all trigger this hook.", (1, 6))
 
1855
        self.add_hook('post_change_branch_tip',
1763
1856
            "Called in bzr client and server after a change to the tip of a "
1764
1857
            "branch is made. post_change_branch_tip is called with a "
1765
1858
            "bzrlib.branch.ChangeBranchTipParams. Note that push, pull, "
1766
 
            "commit, uncommit will all trigger this hook.", (1, 4), None))
1767
 
        self.create_hook(HookPoint('transform_fallback_location',
 
1859
            "commit, uncommit will all trigger this hook.", (1, 4))
 
1860
        self.add_hook('transform_fallback_location',
1768
1861
            "Called when a stacked branch is activating its fallback "
1769
1862
            "locations. transform_fallback_location is called with (branch, "
1770
1863
            "url), and should return a new url. Returning the same url "
1775
1868
            "fallback locations have not been activated. When there are "
1776
1869
            "multiple hooks installed for transform_fallback_location, "
1777
1870
            "all are called with the url returned from the previous hook."
1778
 
            "The order is however undefined.", (1, 9), None))
1779
 
        self.create_hook(HookPoint('automatic_tag_name',
1780
 
            "Called to determine an automatic tag name for a revision."
 
1871
            "The order is however undefined.", (1, 9))
 
1872
        self.add_hook('automatic_tag_name',
 
1873
            "Called to determine an automatic tag name for a revision. "
1781
1874
            "automatic_tag_name is called with (branch, revision_id) and "
1782
1875
            "should return a tag name or None if no tag name could be "
1783
1876
            "determined. The first non-None tag name returned will be used.",
1784
 
            (2, 2), None))
1785
 
        self.create_hook(HookPoint('post_branch_init',
 
1877
            (2, 2))
 
1878
        self.add_hook('post_branch_init',
1786
1879
            "Called after new branch initialization completes. "
1787
1880
            "post_branch_init is called with a "
1788
1881
            "bzrlib.branch.BranchInitHookParams. "
1789
1882
            "Note that init, branch and checkout (both heavyweight and "
1790
 
            "lightweight) will all trigger this hook.", (2, 2), None))
1791
 
        self.create_hook(HookPoint('post_switch',
 
1883
            "lightweight) will all trigger this hook.", (2, 2))
 
1884
        self.add_hook('post_switch',
1792
1885
            "Called after a checkout switches branch. "
1793
1886
            "post_switch is called with a "
1794
 
            "bzrlib.branch.SwitchHookParams.", (2, 2), None))
 
1887
            "bzrlib.branch.SwitchHookParams.", (2, 2))
1795
1888
 
1796
1889
 
1797
1890
 
1874
1967
        return self.__dict__ == other.__dict__
1875
1968
 
1876
1969
    def __repr__(self):
1877
 
        if self.branch:
1878
 
            return "<%s of %s>" % (self.__class__.__name__, self.branch)
1879
 
        else:
1880
 
            return "<%s of format:%s bzrdir:%s>" % (
1881
 
                self.__class__.__name__, self.branch,
1882
 
                self.format, self.bzrdir)
 
1970
        return "<%s of %s>" % (self.__class__.__name__, self.branch)
1883
1971
 
1884
1972
 
1885
1973
class SwitchHookParams(object):
1915
2003
            self.revision_id)
1916
2004
 
1917
2005
 
1918
 
class BzrBranchFormat4(BranchFormat):
1919
 
    """Bzr branch format 4.
1920
 
 
1921
 
    This format has:
1922
 
     - a revision-history file.
1923
 
     - a branch-lock lock file [ to be shared with the bzrdir ]
1924
 
    """
1925
 
 
1926
 
    def get_format_description(self):
1927
 
        """See BranchFormat.get_format_description()."""
1928
 
        return "Branch format 4"
1929
 
 
1930
 
    def initialize(self, a_bzrdir, name=None):
1931
 
        """Create a branch of this format in a_bzrdir."""
1932
 
        utf8_files = [('revision-history', ''),
1933
 
                      ('branch-name', ''),
1934
 
                      ]
1935
 
        return self._initialize_helper(a_bzrdir, utf8_files, name=name,
1936
 
                                       lock_type='branch4', set_format=False)
1937
 
 
1938
 
    def __init__(self):
1939
 
        super(BzrBranchFormat4, self).__init__()
1940
 
        self._matchingbzrdir = bzrdir.BzrDirFormat6()
1941
 
 
1942
 
    def network_name(self):
1943
 
        """The network name for this format is the control dirs disk label."""
1944
 
        return self._matchingbzrdir.get_format_string()
1945
 
 
1946
 
    def open(self, a_bzrdir, name=None, _found=False, ignore_fallbacks=False):
1947
 
        """See BranchFormat.open()."""
1948
 
        if not _found:
1949
 
            # we are being called directly and must probe.
1950
 
            raise NotImplementedError
1951
 
        return BzrBranch(_format=self,
1952
 
                         _control_files=a_bzrdir._control_files,
1953
 
                         a_bzrdir=a_bzrdir,
1954
 
                         name=name,
1955
 
                         _repository=a_bzrdir.open_repository())
1956
 
 
1957
 
    def __str__(self):
1958
 
        return "Bazaar-NG branch format 4"
1959
 
 
1960
 
 
1961
2006
class BranchFormatMetadir(BranchFormat):
1962
2007
    """Common logic for meta-dir based branch formats."""
1963
2008
 
1972
2017
        """
1973
2018
        return self.get_format_string()
1974
2019
 
1975
 
    def open(self, a_bzrdir, name=None, _found=False, ignore_fallbacks=False):
 
2020
    def open(self, a_bzrdir, name=None, _found=False, ignore_fallbacks=False,
 
2021
            found_repository=None):
1976
2022
        """See BranchFormat.open()."""
1977
2023
        if not _found:
1978
2024
            format = BranchFormat.find_format(a_bzrdir, name=name)
1983
2029
        try:
1984
2030
            control_files = lockable_files.LockableFiles(transport, 'lock',
1985
2031
                                                         lockdir.LockDir)
 
2032
            if found_repository is None:
 
2033
                found_repository = a_bzrdir.find_repository()
1986
2034
            return self._branch_class()(_format=self,
1987
2035
                              _control_files=control_files,
1988
2036
                              name=name,
1989
2037
                              a_bzrdir=a_bzrdir,
1990
 
                              _repository=a_bzrdir.find_repository(),
 
2038
                              _repository=found_repository,
1991
2039
                              ignore_fallbacks=ignore_fallbacks)
1992
2040
        except errors.NoSuchFile:
1993
2041
            raise errors.NotBranchError(path=transport.base, bzrdir=a_bzrdir)
2000
2048
    def supports_tags(self):
2001
2049
        return True
2002
2050
 
 
2051
    def supports_leaving_lock(self):
 
2052
        return True
 
2053
 
2003
2054
 
2004
2055
class BzrBranchFormat5(BranchFormatMetadir):
2005
2056
    """Bzr branch format 5.
2025
2076
        """See BranchFormat.get_format_description()."""
2026
2077
        return "Branch format 5"
2027
2078
 
2028
 
    def initialize(self, a_bzrdir, name=None):
 
2079
    def initialize(self, a_bzrdir, name=None, repository=None):
2029
2080
        """Create a branch of this format in a_bzrdir."""
2030
2081
        utf8_files = [('revision-history', ''),
2031
2082
                      ('branch-name', ''),
2032
2083
                      ]
2033
 
        return self._initialize_helper(a_bzrdir, utf8_files, name)
 
2084
        return self._initialize_helper(a_bzrdir, utf8_files, name, repository)
2034
2085
 
2035
2086
    def supports_tags(self):
2036
2087
        return False
2058
2109
        """See BranchFormat.get_format_description()."""
2059
2110
        return "Branch format 6"
2060
2111
 
2061
 
    def initialize(self, a_bzrdir, name=None):
 
2112
    def initialize(self, a_bzrdir, name=None, repository=None):
2062
2113
        """Create a branch of this format in a_bzrdir."""
2063
2114
        utf8_files = [('last-revision', '0 null:\n'),
2064
2115
                      ('branch.conf', ''),
2065
2116
                      ('tags', ''),
2066
2117
                      ]
2067
 
        return self._initialize_helper(a_bzrdir, utf8_files, name)
 
2118
        return self._initialize_helper(a_bzrdir, utf8_files, name, repository)
2068
2119
 
2069
2120
    def make_tags(self, branch):
2070
2121
        """See bzrlib.branch.BranchFormat.make_tags()."""
2088
2139
        """See BranchFormat.get_format_description()."""
2089
2140
        return "Branch format 8"
2090
2141
 
2091
 
    def initialize(self, a_bzrdir, name=None):
 
2142
    def initialize(self, a_bzrdir, name=None, repository=None):
2092
2143
        """Create a branch of this format in a_bzrdir."""
2093
2144
        utf8_files = [('last-revision', '0 null:\n'),
2094
2145
                      ('branch.conf', ''),
2095
2146
                      ('tags', ''),
2096
2147
                      ('references', '')
2097
2148
                      ]
2098
 
        return self._initialize_helper(a_bzrdir, utf8_files, name)
 
2149
        return self._initialize_helper(a_bzrdir, utf8_files, name, repository)
2099
2150
 
2100
2151
    def __init__(self):
2101
2152
        super(BzrBranchFormat8, self).__init__()
2124
2175
    This format was introduced in bzr 1.6.
2125
2176
    """
2126
2177
 
2127
 
    def initialize(self, a_bzrdir, name=None):
 
2178
    def initialize(self, a_bzrdir, name=None, repository=None):
2128
2179
        """Create a branch of this format in a_bzrdir."""
2129
2180
        utf8_files = [('last-revision', '0 null:\n'),
2130
2181
                      ('branch.conf', ''),
2131
2182
                      ('tags', ''),
2132
2183
                      ]
2133
 
        return self._initialize_helper(a_bzrdir, utf8_files, name)
 
2184
        return self._initialize_helper(a_bzrdir, utf8_files, name, repository)
2134
2185
 
2135
2186
    def _branch_class(self):
2136
2187
        return BzrBranch7
2168
2219
        """See BranchFormat.get_format_description()."""
2169
2220
        return "Checkout reference format 1"
2170
2221
 
2171
 
    def get_reference(self, a_bzrdir):
 
2222
    def get_reference(self, a_bzrdir, name=None):
2172
2223
        """See BranchFormat.get_reference()."""
2173
 
        transport = a_bzrdir.get_branch_transport(None)
 
2224
        transport = a_bzrdir.get_branch_transport(None, name=name)
2174
2225
        return transport.get_bytes('location')
2175
2226
 
2176
 
    def set_reference(self, a_bzrdir, to_branch):
 
2227
    def set_reference(self, a_bzrdir, name, to_branch):
2177
2228
        """See BranchFormat.set_reference()."""
2178
 
        transport = a_bzrdir.get_branch_transport(None)
 
2229
        transport = a_bzrdir.get_branch_transport(None, name=name)
2179
2230
        location = transport.put_bytes('location', to_branch.base)
2180
2231
 
2181
 
    def initialize(self, a_bzrdir, name=None, target_branch=None):
 
2232
    def initialize(self, a_bzrdir, name=None, target_branch=None,
 
2233
            repository=None):
2182
2234
        """Create a branch of this format in a_bzrdir."""
2183
2235
        if target_branch is None:
2184
2236
            # this format does not implement branch itself, thus the implicit
2212
2264
        return clone
2213
2265
 
2214
2266
    def open(self, a_bzrdir, name=None, _found=False, location=None,
2215
 
             possible_transports=None, ignore_fallbacks=False):
 
2267
             possible_transports=None, ignore_fallbacks=False,
 
2268
             found_repository=None):
2216
2269
        """Return the branch that the branch reference in a_bzrdir points at.
2217
2270
 
2218
2271
        :param a_bzrdir: A BzrDir that contains a branch.
2232
2285
                raise AssertionError("wrong format %r found for %r" %
2233
2286
                    (format, self))
2234
2287
        if location is None:
2235
 
            location = self.get_reference(a_bzrdir)
 
2288
            location = self.get_reference(a_bzrdir, name)
2236
2289
        real_bzrdir = bzrdir.BzrDir.open(
2237
2290
            location, possible_transports=possible_transports)
2238
2291
        result = real_bzrdir.open_branch(name=name, 
2249
2302
        return result
2250
2303
 
2251
2304
 
 
2305
class BranchFormatRegistry(controldir.ControlComponentFormatRegistry):
 
2306
    """Branch format registry."""
 
2307
 
 
2308
    def __init__(self, other_registry=None):
 
2309
        super(BranchFormatRegistry, self).__init__(other_registry)
 
2310
        self._default_format = None
 
2311
 
 
2312
    def set_default(self, format):
 
2313
        self._default_format = format
 
2314
 
 
2315
    def get_default(self):
 
2316
        return self._default_format
 
2317
 
 
2318
 
2252
2319
network_format_registry = registry.FormatRegistry()
2253
2320
"""Registry of formats indexed by their network name.
2254
2321
 
2257
2324
BranchFormat.network_name() for more detail.
2258
2325
"""
2259
2326
 
 
2327
format_registry = BranchFormatRegistry(network_format_registry)
 
2328
 
2260
2329
 
2261
2330
# formats which have no format string are not discoverable
2262
2331
# and not independently creatable, so are not registered.
2264
2333
__format6 = BzrBranchFormat6()
2265
2334
__format7 = BzrBranchFormat7()
2266
2335
__format8 = BzrBranchFormat8()
2267
 
BranchFormat.register_format(__format5)
2268
 
BranchFormat.register_format(BranchReferenceFormat())
2269
 
BranchFormat.register_format(__format6)
2270
 
BranchFormat.register_format(__format7)
2271
 
BranchFormat.register_format(__format8)
2272
 
BranchFormat.set_default_format(__format7)
2273
 
_legacy_formats = [BzrBranchFormat4(),
2274
 
    ]
2275
 
network_format_registry.register(
2276
 
    _legacy_formats[0].network_name(), _legacy_formats[0].__class__)
2277
 
 
2278
 
 
2279
 
class BranchWriteLockResult(object):
 
2336
format_registry.register(__format5)
 
2337
format_registry.register(BranchReferenceFormat())
 
2338
format_registry.register(__format6)
 
2339
format_registry.register(__format7)
 
2340
format_registry.register(__format8)
 
2341
format_registry.set_default(__format7)
 
2342
 
 
2343
 
 
2344
class BranchWriteLockResult(LogicalLockResult):
2280
2345
    """The result of write locking a branch.
2281
2346
 
2282
2347
    :ivar branch_token: The token obtained from the underlying branch lock, or
2285
2350
    """
2286
2351
 
2287
2352
    def __init__(self, unlock, branch_token):
 
2353
        LogicalLockResult.__init__(self, unlock)
2288
2354
        self.branch_token = branch_token
2289
 
        self.unlock = unlock
2290
2355
 
2291
 
    def __str__(self):
 
2356
    def __repr__(self):
2292
2357
        return "BranchWriteLockResult(%s, %s)" % (self.branch_token,
2293
2358
            self.unlock)
2294
2359
 
2379
2444
    def lock_read(self):
2380
2445
        """Lock the branch for read operations.
2381
2446
 
2382
 
        :return: An object with an unlock method which will release the lock
2383
 
            obtained.
 
2447
        :return: A bzrlib.lock.LogicalLockResult.
2384
2448
        """
2385
2449
        if not self.is_locked():
2386
2450
            self._note_lock('r')
2394
2458
            took_lock = False
2395
2459
        try:
2396
2460
            self.control_files.lock_read()
2397
 
            return self
 
2461
            return LogicalLockResult(self.unlock)
2398
2462
        except:
2399
2463
            if took_lock:
2400
2464
                self.repository.unlock()
2556
2620
        result.target_branch = target
2557
2621
        result.old_revno, result.old_revid = target.last_revision_info()
2558
2622
        self.update_references(target)
2559
 
        if result.old_revid != self.last_revision():
 
2623
        if result.old_revid != stop_revision:
2560
2624
            # We assume that during 'push' this repository is closer than
2561
2625
            # the target.
2562
2626
            graph = self.repository.get_graph(target.repository)
2563
2627
            target.update_revisions(self, stop_revision,
2564
2628
                overwrite=overwrite, graph=graph)
2565
2629
        if self._push_should_merge_tags():
2566
 
            result.tag_conflicts = self.tags.merge_to(target.tags,
2567
 
                overwrite)
 
2630
            result.tag_conflicts = self.tags.merge_to(target.tags, overwrite)
2568
2631
        result.new_revno, result.new_revid = target.last_revision_info()
2569
2632
        return result
2570
2633
 
2602
2665
        """Return the branch we are bound to.
2603
2666
 
2604
2667
        :return: Either a Branch, or None
2605
 
 
2606
 
        This could memoise the branch, but if thats done
2607
 
        it must be revalidated on each new lock.
2608
 
        So for now we just don't memoise it.
2609
 
        # RBC 20060304 review this decision.
2610
2668
        """
 
2669
        if self._master_branch_cache is None:
 
2670
            self._master_branch_cache = self._get_master_branch(
 
2671
                possible_transports)
 
2672
        return self._master_branch_cache
 
2673
 
 
2674
    def _get_master_branch(self, possible_transports):
2611
2675
        bound_loc = self.get_bound_location()
2612
2676
        if not bound_loc:
2613
2677
            return None
2624
2688
 
2625
2689
        :param location: URL to the target branch
2626
2690
        """
 
2691
        self._master_branch_cache = None
2627
2692
        if location:
2628
2693
            self._transport.put_bytes('bound', location+'\n',
2629
2694
                mode=self.bzrdir._get_file_mode())
2881
2946
 
2882
2947
    def set_bound_location(self, location):
2883
2948
        """See Branch.set_push_location."""
 
2949
        self._master_branch_cache = None
2884
2950
        result = None
2885
2951
        config = self.get_config()
2886
2952
        if location is None:
2963
3029
        try:
2964
3030
            index = self._partial_revision_history_cache.index(revision_id)
2965
3031
        except ValueError:
2966
 
            self._extend_partial_history(stop_revision=revision_id)
 
3032
            try:
 
3033
                self._extend_partial_history(stop_revision=revision_id)
 
3034
            except errors.RevisionNotPresent, e:
 
3035
                raise errors.GhostRevisionsHaveNoRevno(revision_id, e.revision_id)
2967
3036
            index = len(self._partial_revision_history_cache) - 1
2968
3037
            if self._partial_revision_history_cache[index] != revision_id:
2969
3038
                raise errors.NoSuchRevision(self, revision_id)
3024
3093
    :ivar tag_conflicts: A list of tag conflicts, see BasicTags.merge_to
3025
3094
    """
3026
3095
 
 
3096
    @deprecated_method(deprecated_in((2, 3, 0)))
3027
3097
    def __int__(self):
3028
 
        # DEPRECATED: pull used to return the change in revno
 
3098
        """Return the relative change in revno.
 
3099
 
 
3100
        :deprecated: Use `new_revno` and `old_revno` instead.
 
3101
        """
3029
3102
        return self.new_revno - self.old_revno
3030
3103
 
3031
3104
    def report(self, to_file):
3056
3129
        target, otherwise it will be None.
3057
3130
    """
3058
3131
 
 
3132
    @deprecated_method(deprecated_in((2, 3, 0)))
3059
3133
    def __int__(self):
3060
 
        # DEPRECATED: push used to return the change in revno
 
3134
        """Return the relative change in revno.
 
3135
 
 
3136
        :deprecated: Use `new_revno` and `old_revno` instead.
 
3137
        """
3061
3138
        return self.new_revno - self.old_revno
3062
3139
 
3063
3140
    def report(self, to_file):
3186
3263
    _optimisers = []
3187
3264
    """The available optimised InterBranch types."""
3188
3265
 
3189
 
    @staticmethod
3190
 
    def _get_branch_formats_to_test():
3191
 
        """Return a tuple with the Branch formats to use when testing."""
3192
 
        raise NotImplementedError(InterBranch._get_branch_formats_to_test)
 
3266
    @classmethod
 
3267
    def _get_branch_formats_to_test(klass):
 
3268
        """Return an iterable of format tuples for testing.
 
3269
        
 
3270
        :return: An iterable of (from_format, to_format) to use when testing
 
3271
            this InterBranch class. Each InterBranch class should define this
 
3272
            method itself.
 
3273
        """
 
3274
        raise NotImplementedError(klass._get_branch_formats_to_test)
3193
3275
 
 
3276
    @needs_write_lock
3194
3277
    def pull(self, overwrite=False, stop_revision=None,
3195
3278
             possible_transports=None, local=False):
3196
3279
        """Mirror source into target branch.
3201
3284
        """
3202
3285
        raise NotImplementedError(self.pull)
3203
3286
 
 
3287
    @needs_write_lock
3204
3288
    def update_revisions(self, stop_revision=None, overwrite=False,
3205
 
                         graph=None):
 
3289
                         graph=None, fetch_tags=True):
3206
3290
        """Pull in new perfect-fit revisions.
3207
3291
 
3208
3292
        :param stop_revision: Updated until the given revision
3210
3294
            to see if it is a proper descendant.
3211
3295
        :param graph: A Graph object that can be used to query history
3212
3296
            information. This can be None.
 
3297
        :param fetch_tags: Flag that specifies if tags from source should be
 
3298
            fetched too.
3213
3299
        :return: None
3214
3300
        """
3215
3301
        raise NotImplementedError(self.update_revisions)
3216
3302
 
 
3303
    @needs_write_lock
3217
3304
    def push(self, overwrite=False, stop_revision=None,
3218
3305
             _override_hook_source_branch=None):
3219
3306
        """Mirror the source branch into the target branch.
3222
3309
        """
3223
3310
        raise NotImplementedError(self.push)
3224
3311
 
 
3312
    @needs_write_lock
 
3313
    def copy_content_into(self, revision_id=None):
 
3314
        """Copy the content of source into target
 
3315
 
 
3316
        revision_id: if not None, the revision history in the new branch will
 
3317
                     be truncated to end with revision_id.
 
3318
        """
 
3319
        raise NotImplementedError(self.copy_content_into)
 
3320
 
 
3321
    @needs_write_lock
 
3322
    def fetch(self, stop_revision=None, fetch_spec=None):
 
3323
        """Fetch revisions.
 
3324
 
 
3325
        :param stop_revision: Last revision to fetch
 
3326
        :param fetch_spec: Fetch spec.
 
3327
        """
 
3328
        raise NotImplementedError(self.fetch)
 
3329
 
3225
3330
 
3226
3331
class GenericInterBranch(InterBranch):
3227
 
    """InterBranch implementation that uses public Branch functions.
3228
 
    """
3229
 
 
3230
 
    @staticmethod
3231
 
    def _get_branch_formats_to_test():
3232
 
        return BranchFormat._default_format, BranchFormat._default_format
3233
 
 
 
3332
    """InterBranch implementation that uses public Branch functions."""
 
3333
 
 
3334
    @classmethod
 
3335
    def is_compatible(klass, source, target):
 
3336
        # GenericBranch uses the public API, so always compatible
 
3337
        return True
 
3338
 
 
3339
    @classmethod
 
3340
    def _get_branch_formats_to_test(klass):
 
3341
        return [(format_registry.get_default(), format_registry.get_default())]
 
3342
 
 
3343
    @classmethod
 
3344
    def unwrap_format(klass, format):
 
3345
        if isinstance(format, remote.RemoteBranchFormat):
 
3346
            format._ensure_real()
 
3347
            return format._custom_format
 
3348
        return format
 
3349
 
 
3350
    @needs_write_lock
 
3351
    def copy_content_into(self, revision_id=None):
 
3352
        """Copy the content of source into target
 
3353
 
 
3354
        revision_id: if not None, the revision history in the new branch will
 
3355
                     be truncated to end with revision_id.
 
3356
        """
 
3357
        self.source.update_references(self.target)
 
3358
        self.source._synchronize_history(self.target, revision_id)
 
3359
        try:
 
3360
            parent = self.source.get_parent()
 
3361
        except errors.InaccessibleParent, e:
 
3362
            mutter('parent was not accessible to copy: %s', e)
 
3363
        else:
 
3364
            if parent:
 
3365
                self.target.set_parent(parent)
 
3366
        if self.source._push_should_merge_tags():
 
3367
            self.source.tags.merge_to(self.target.tags)
 
3368
 
 
3369
    @needs_write_lock
 
3370
    def fetch(self, stop_revision=None, fetch_spec=None):
 
3371
        if fetch_spec is not None and stop_revision is not None:
 
3372
            raise AssertionError(
 
3373
                "fetch_spec and last_revision are mutually exclusive.")
 
3374
        if self.target.base == self.source.base:
 
3375
            return (0, [])
 
3376
        self.source.lock_read()
 
3377
        try:
 
3378
            if stop_revision is None and fetch_spec is None:
 
3379
                stop_revision = self.source.last_revision()
 
3380
                stop_revision = _mod_revision.ensure_null(stop_revision)
 
3381
            return self.target.repository.fetch(self.source.repository,
 
3382
                revision_id=stop_revision, fetch_spec=fetch_spec)
 
3383
        finally:
 
3384
            self.source.unlock()
 
3385
 
 
3386
    @needs_write_lock
3234
3387
    def update_revisions(self, stop_revision=None, overwrite=False,
3235
 
        graph=None):
 
3388
        graph=None, fetch_tags=True):
3236
3389
        """See InterBranch.update_revisions()."""
3237
 
        self.source.lock_read()
3238
 
        try:
3239
 
            other_revno, other_last_revision = self.source.last_revision_info()
3240
 
            stop_revno = None # unknown
3241
 
            if stop_revision is None:
3242
 
                stop_revision = other_last_revision
3243
 
                if _mod_revision.is_null(stop_revision):
3244
 
                    # if there are no commits, we're done.
3245
 
                    return
3246
 
                stop_revno = other_revno
3247
 
 
3248
 
            # what's the current last revision, before we fetch [and change it
3249
 
            # possibly]
3250
 
            last_rev = _mod_revision.ensure_null(self.target.last_revision())
3251
 
            # we fetch here so that we don't process data twice in the common
3252
 
            # case of having something to pull, and so that the check for
3253
 
            # already merged can operate on the just fetched graph, which will
3254
 
            # be cached in memory.
3255
 
            self.target.fetch(self.source, stop_revision)
3256
 
            # Check to see if one is an ancestor of the other
3257
 
            if not overwrite:
3258
 
                if graph is None:
3259
 
                    graph = self.target.repository.get_graph()
3260
 
                if self.target._check_if_descendant_or_diverged(
3261
 
                        stop_revision, last_rev, graph, self.source):
3262
 
                    # stop_revision is a descendant of last_rev, but we aren't
3263
 
                    # overwriting, so we're done.
3264
 
                    return
3265
 
            if stop_revno is None:
3266
 
                if graph is None:
3267
 
                    graph = self.target.repository.get_graph()
3268
 
                this_revno, this_last_revision = \
3269
 
                        self.target.last_revision_info()
3270
 
                stop_revno = graph.find_distance_to_null(stop_revision,
3271
 
                                [(other_last_revision, other_revno),
3272
 
                                 (this_last_revision, this_revno)])
3273
 
            self.target.set_last_revision_info(stop_revno, stop_revision)
3274
 
        finally:
3275
 
            self.source.unlock()
3276
 
 
 
3390
        other_revno, other_last_revision = self.source.last_revision_info()
 
3391
        stop_revno = None # unknown
 
3392
        if stop_revision is None:
 
3393
            stop_revision = other_last_revision
 
3394
            if _mod_revision.is_null(stop_revision):
 
3395
                # if there are no commits, we're done.
 
3396
                return
 
3397
            stop_revno = other_revno
 
3398
 
 
3399
        # what's the current last revision, before we fetch [and change it
 
3400
        # possibly]
 
3401
        last_rev = _mod_revision.ensure_null(self.target.last_revision())
 
3402
        # we fetch here so that we don't process data twice in the common
 
3403
        # case of having something to pull, and so that the check for
 
3404
        # already merged can operate on the just fetched graph, which will
 
3405
        # be cached in memory.
 
3406
        if fetch_tags:
 
3407
            fetch_spec_factory = fetch.FetchSpecFactory()
 
3408
            fetch_spec_factory.source_branch = self.source
 
3409
            fetch_spec_factory.source_branch_stop_revision_id = stop_revision
 
3410
            fetch_spec_factory.source_repo = self.source.repository
 
3411
            fetch_spec_factory.target_repo = self.target.repository
 
3412
            fetch_spec_factory.target_repo_kind = fetch.TargetRepoKinds.PREEXISTING
 
3413
            fetch_spec = fetch_spec_factory.make_fetch_spec()
 
3414
        else:
 
3415
            fetch_spec = _mod_graph.NotInOtherForRevs(self.target.repository,
 
3416
                self.source.repository, revision_ids=[stop_revision]).execute()
 
3417
        self.target.fetch(self.source, fetch_spec=fetch_spec)
 
3418
        # Check to see if one is an ancestor of the other
 
3419
        if not overwrite:
 
3420
            if graph is None:
 
3421
                graph = self.target.repository.get_graph()
 
3422
            if self.target._check_if_descendant_or_diverged(
 
3423
                    stop_revision, last_rev, graph, self.source):
 
3424
                # stop_revision is a descendant of last_rev, but we aren't
 
3425
                # overwriting, so we're done.
 
3426
                return
 
3427
        if stop_revno is None:
 
3428
            if graph is None:
 
3429
                graph = self.target.repository.get_graph()
 
3430
            this_revno, this_last_revision = \
 
3431
                    self.target.last_revision_info()
 
3432
            stop_revno = graph.find_distance_to_null(stop_revision,
 
3433
                            [(other_last_revision, other_revno),
 
3434
                             (this_last_revision, this_revno)])
 
3435
        self.target.set_last_revision_info(stop_revno, stop_revision)
 
3436
 
 
3437
    @needs_write_lock
3277
3438
    def pull(self, overwrite=False, stop_revision=None,
3278
 
             possible_transports=None, _hook_master=None, run_hooks=True,
 
3439
             possible_transports=None, run_hooks=True,
3279
3440
             _override_hook_target=None, local=False):
3280
 
        """See Branch.pull.
 
3441
        """Pull from source into self, updating my master if any.
3281
3442
 
3282
 
        :param _hook_master: Private parameter - set the branch to
3283
 
            be supplied as the master to pull hooks.
3284
3443
        :param run_hooks: Private parameter - if false, this branch
3285
3444
            is being called because it's the master of the primary branch,
3286
3445
            so it should not run its hooks.
3287
 
        :param _override_hook_target: Private parameter - set the branch to be
3288
 
            supplied as the target_branch to pull hooks.
3289
 
        :param local: Only update the local branch, and not the bound branch.
3290
3446
        """
3291
 
        # This type of branch can't be bound.
3292
 
        if local:
 
3447
        bound_location = self.target.get_bound_location()
 
3448
        if local and not bound_location:
3293
3449
            raise errors.LocalRequiresBoundBranch()
3294
 
        result = PullResult()
3295
 
        result.source_branch = self.source
3296
 
        if _override_hook_target is None:
3297
 
            result.target_branch = self.target
3298
 
        else:
3299
 
            result.target_branch = _override_hook_target
3300
 
        self.source.lock_read()
 
3450
        master_branch = None
 
3451
        source_is_master = (self.source.user_url == bound_location)
 
3452
        if not local and bound_location and not source_is_master:
 
3453
            # not pulling from master, so we need to update master.
 
3454
            master_branch = self.target.get_master_branch(possible_transports)
 
3455
            master_branch.lock_write()
3301
3456
        try:
3302
 
            # We assume that during 'pull' the target repository is closer than
3303
 
            # the source one.
3304
 
            self.source.update_references(self.target)
3305
 
            graph = self.target.repository.get_graph(self.source.repository)
3306
 
            # TODO: Branch formats should have a flag that indicates 
3307
 
            # that revno's are expensive, and pull() should honor that flag.
3308
 
            # -- JRV20090506
3309
 
            result.old_revno, result.old_revid = \
3310
 
                self.target.last_revision_info()
3311
 
            self.target.update_revisions(self.source, stop_revision,
3312
 
                overwrite=overwrite, graph=graph)
3313
 
            # TODO: The old revid should be specified when merging tags, 
3314
 
            # so a tags implementation that versions tags can only 
3315
 
            # pull in the most recent changes. -- JRV20090506
3316
 
            result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
3317
 
                overwrite)
3318
 
            result.new_revno, result.new_revid = self.target.last_revision_info()
3319
 
            if _hook_master:
3320
 
                result.master_branch = _hook_master
3321
 
                result.local_branch = result.target_branch
3322
 
            else:
3323
 
                result.master_branch = result.target_branch
3324
 
                result.local_branch = None
3325
 
            if run_hooks:
3326
 
                for hook in Branch.hooks['post_pull']:
3327
 
                    hook(result)
 
3457
            if master_branch:
 
3458
                # pull from source into master.
 
3459
                master_branch.pull(self.source, overwrite, stop_revision,
 
3460
                    run_hooks=False)
 
3461
            return self._pull(overwrite,
 
3462
                stop_revision, _hook_master=master_branch,
 
3463
                run_hooks=run_hooks,
 
3464
                _override_hook_target=_override_hook_target,
 
3465
                merge_tags_to_master=not source_is_master)
3328
3466
        finally:
3329
 
            self.source.unlock()
3330
 
        return result
 
3467
            if master_branch:
 
3468
                master_branch.unlock()
3331
3469
 
3332
3470
    def push(self, overwrite=False, stop_revision=None,
3333
3471
             _override_hook_source_branch=None):
3373
3511
                # push into the master from the source branch.
3374
3512
                self.source._basic_push(master_branch, overwrite, stop_revision)
3375
3513
                # and push into the target branch from the source. Note that we
3376
 
                # push from the source branch again, because its considered the
 
3514
                # push from the source branch again, because it's considered the
3377
3515
                # highest bandwidth repository.
3378
3516
                result = self.source._basic_push(self.target, overwrite,
3379
3517
                    stop_revision)
3395
3533
            _run_hooks()
3396
3534
            return result
3397
3535
 
3398
 
    @classmethod
3399
 
    def is_compatible(self, source, target):
3400
 
        # GenericBranch uses the public API, so always compatible
3401
 
        return True
3402
 
 
3403
 
 
3404
 
class InterToBranch5(GenericInterBranch):
3405
 
 
3406
 
    @staticmethod
3407
 
    def _get_branch_formats_to_test():
3408
 
        return BranchFormat._default_format, BzrBranchFormat5()
3409
 
 
3410
 
    def pull(self, overwrite=False, stop_revision=None,
3411
 
             possible_transports=None, run_hooks=True,
3412
 
             _override_hook_target=None, local=False):
3413
 
        """Pull from source into self, updating my master if any.
3414
 
 
 
3536
    def _pull(self, overwrite=False, stop_revision=None,
 
3537
             possible_transports=None, _hook_master=None, run_hooks=True,
 
3538
             _override_hook_target=None, local=False,
 
3539
             merge_tags_to_master=True):
 
3540
        """See Branch.pull.
 
3541
 
 
3542
        This function is the core worker, used by GenericInterBranch.pull to
 
3543
        avoid duplication when pulling source->master and source->local.
 
3544
 
 
3545
        :param _hook_master: Private parameter - set the branch to
 
3546
            be supplied as the master to pull hooks.
3415
3547
        :param run_hooks: Private parameter - if false, this branch
3416
3548
            is being called because it's the master of the primary branch,
3417
3549
            so it should not run its hooks.
 
3550
            is being called because it's the master of the primary branch,
 
3551
            so it should not run its hooks.
 
3552
        :param _override_hook_target: Private parameter - set the branch to be
 
3553
            supplied as the target_branch to pull hooks.
 
3554
        :param local: Only update the local branch, and not the bound branch.
3418
3555
        """
3419
 
        bound_location = self.target.get_bound_location()
3420
 
        if local and not bound_location:
 
3556
        # This type of branch can't be bound.
 
3557
        if local:
3421
3558
            raise errors.LocalRequiresBoundBranch()
3422
 
        master_branch = None
3423
 
        if not local and bound_location and self.source.user_url != bound_location:
3424
 
            # not pulling from master, so we need to update master.
3425
 
            master_branch = self.target.get_master_branch(possible_transports)
3426
 
            master_branch.lock_write()
 
3559
        result = PullResult()
 
3560
        result.source_branch = self.source
 
3561
        if _override_hook_target is None:
 
3562
            result.target_branch = self.target
 
3563
        else:
 
3564
            result.target_branch = _override_hook_target
 
3565
        self.source.lock_read()
3427
3566
        try:
3428
 
            if master_branch:
3429
 
                # pull from source into master.
3430
 
                master_branch.pull(self.source, overwrite, stop_revision,
3431
 
                    run_hooks=False)
3432
 
            return super(InterToBranch5, self).pull(overwrite,
3433
 
                stop_revision, _hook_master=master_branch,
3434
 
                run_hooks=run_hooks,
3435
 
                _override_hook_target=_override_hook_target)
 
3567
            # We assume that during 'pull' the target repository is closer than
 
3568
            # the source one.
 
3569
            self.source.update_references(self.target)
 
3570
            graph = self.target.repository.get_graph(self.source.repository)
 
3571
            # TODO: Branch formats should have a flag that indicates 
 
3572
            # that revno's are expensive, and pull() should honor that flag.
 
3573
            # -- JRV20090506
 
3574
            result.old_revno, result.old_revid = \
 
3575
                self.target.last_revision_info()
 
3576
            self.target.update_revisions(self.source, stop_revision,
 
3577
                overwrite=overwrite, graph=graph)
 
3578
            # TODO: The old revid should be specified when merging tags, 
 
3579
            # so a tags implementation that versions tags can only 
 
3580
            # pull in the most recent changes. -- JRV20090506
 
3581
            result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
 
3582
                overwrite, ignore_master=not merge_tags_to_master)
 
3583
            result.new_revno, result.new_revid = self.target.last_revision_info()
 
3584
            if _hook_master:
 
3585
                result.master_branch = _hook_master
 
3586
                result.local_branch = result.target_branch
 
3587
            else:
 
3588
                result.master_branch = result.target_branch
 
3589
                result.local_branch = None
 
3590
            if run_hooks:
 
3591
                for hook in Branch.hooks['post_pull']:
 
3592
                    hook(result)
3436
3593
        finally:
3437
 
            if master_branch:
3438
 
                master_branch.unlock()
 
3594
            self.source.unlock()
 
3595
        return result
3439
3596
 
3440
3597
 
3441
3598
InterBranch.register_optimiser(GenericInterBranch)
3442
 
InterBranch.register_optimiser(InterToBranch5)