/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/branch.py

Merged unnecessary-test-applicable into weave-fmt-plugin.

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
25
25
        bzrdir,
26
26
        cache_utf8,
27
27
        config as _mod_config,
 
28
        controldir,
28
29
        debug,
29
30
        errors,
30
31
        lockdir,
31
32
        lockable_files,
 
33
        remote,
32
34
        repository,
33
35
        revision as _mod_revision,
34
36
        rio,
49
51
from bzrlib.decorators import needs_read_lock, needs_write_lock, only_raises
50
52
from bzrlib.hooks import HookPoint, Hooks
51
53
from bzrlib.inter import InterObject
52
 
from bzrlib.lock import _RelockDebugMixin
 
54
from bzrlib.lock import _RelockDebugMixin, LogicalLockResult
53
55
from bzrlib import registry
54
56
from bzrlib.symbol_versioning import (
55
57
    deprecated_in,
63
65
BZR_BRANCH_FORMAT_6 = "Bazaar Branch Format 6 (bzr 0.15)\n"
64
66
 
65
67
 
66
 
class Branch(bzrdir.ControlComponent):
 
68
class Branch(controldir.ControlComponent):
67
69
    """Branch holding a history of revisions.
68
70
 
69
71
    :ivar base:
90
92
        self._revision_id_to_revno_cache = None
91
93
        self._partial_revision_id_to_revno_cache = {}
92
94
        self._partial_revision_history_cache = []
 
95
        self._tags_bytes = None
93
96
        self._last_revision_info_cache = None
94
97
        self._merge_sorted_revisions_cache = None
95
98
        self._open_hook()
102
105
 
103
106
    def _activate_fallback_location(self, url):
104
107
        """Activate the branch/repository from url as a fallback repository."""
 
108
        for existing_fallback_repo in self.repository._fallback_repositories:
 
109
            if existing_fallback_repo.user_url == url:
 
110
                # This fallback is already configured.  This probably only
 
111
                # happens because BzrDir.sprout is a horrible mess.  To avoid
 
112
                # confusing _unstack we don't add this a second time.
 
113
                mutter('duplicate activation of fallback %r on %r', url, self)
 
114
                return
105
115
        repo = self._get_fallback_repository(url)
106
116
        if repo.has_same_location(self.repository):
107
117
            raise errors.UnstackableLocationError(self.user_url, url)
197
207
        return self.supports_tags() and self.tags.get_tag_dict()
198
208
 
199
209
    def get_config(self):
 
210
        """Get a bzrlib.config.BranchConfig for this Branch.
 
211
 
 
212
        This can then be used to get and set configuration options for the
 
213
        branch.
 
214
 
 
215
        :return: A bzrlib.config.BranchConfig.
 
216
        """
200
217
        return BranchConfig(self)
201
218
 
202
219
    def _get_config(self):
218
235
            possible_transports=[self.bzrdir.root_transport])
219
236
        return a_branch.repository
220
237
 
 
238
    @needs_read_lock
221
239
    def _get_tags_bytes(self):
222
240
        """Get the bytes of a serialised tags dict.
223
241
 
230
248
        :return: The bytes of the tags file.
231
249
        :seealso: Branch._set_tags_bytes.
232
250
        """
233
 
        return self._transport.get_bytes('tags')
 
251
        if self._tags_bytes is None:
 
252
            self._tags_bytes = self._transport.get_bytes('tags')
 
253
        return self._tags_bytes
234
254
 
235
255
    def _get_nick(self, local=False, possible_transports=None):
236
256
        config = self.get_config()
238
258
        if not local and not config.has_explicit_nickname():
239
259
            try:
240
260
                master = self.get_master_branch(possible_transports)
 
261
                if master and self.user_url == master.user_url:
 
262
                    raise errors.RecursiveBind(self.user_url)
241
263
                if master is not None:
242
264
                    # return the master branch value
243
265
                    return master.nick
 
266
            except errors.RecursiveBind, e:
 
267
                raise e
244
268
            except errors.BzrError, e:
245
269
                # Silently fall back to local implicit nick if the master is
246
270
                # unavailable
283
307
        new_history.reverse()
284
308
        return new_history
285
309
 
286
 
    def lock_write(self):
 
310
    def lock_write(self, token=None):
 
311
        """Lock the branch for write operations.
 
312
 
 
313
        :param token: A token to permit reacquiring a previously held and
 
314
            preserved lock.
 
315
        :return: A BranchWriteLockResult.
 
316
        """
287
317
        raise NotImplementedError(self.lock_write)
288
318
 
289
319
    def lock_read(self):
 
320
        """Lock the branch for read operations.
 
321
 
 
322
        :return: A bzrlib.lock.LogicalLockResult.
 
323
        """
290
324
        raise NotImplementedError(self.lock_read)
291
325
 
292
326
    def unlock(self):
782
816
            old_repository = self.repository
783
817
            if len(old_repository._fallback_repositories) != 1:
784
818
                raise AssertionError("can't cope with fallback repositories "
785
 
                    "of %r" % (self.repository,))
786
 
            # unlock it, including unlocking the fallback
 
819
                    "of %r (fallbacks: %r)" % (old_repository,
 
820
                        old_repository._fallback_repositories))
 
821
            # Open the new repository object.
 
822
            # Repositories don't offer an interface to remove fallback
 
823
            # repositories today; take the conceptually simpler option and just
 
824
            # reopen it.  We reopen it starting from the URL so that we
 
825
            # get a separate connection for RemoteRepositories and can
 
826
            # stream from one of them to the other.  This does mean doing
 
827
            # separate SSH connection setup, but unstacking is not a
 
828
            # common operation so it's tolerable.
 
829
            new_bzrdir = bzrdir.BzrDir.open(self.bzrdir.root_transport.base)
 
830
            new_repository = new_bzrdir.find_repository()
 
831
            if new_repository._fallback_repositories:
 
832
                raise AssertionError("didn't expect %r to have "
 
833
                    "fallback_repositories"
 
834
                    % (self.repository,))
 
835
            # Replace self.repository with the new repository.
 
836
            # Do our best to transfer the lock state (i.e. lock-tokens and
 
837
            # lock count) of self.repository to the new repository.
 
838
            lock_token = old_repository.lock_write().repository_token
 
839
            self.repository = new_repository
 
840
            if isinstance(self, remote.RemoteBranch):
 
841
                # Remote branches can have a second reference to the old
 
842
                # repository that need to be replaced.
 
843
                if self._real_branch is not None:
 
844
                    self._real_branch.repository = new_repository
 
845
            self.repository.lock_write(token=lock_token)
 
846
            if lock_token is not None:
 
847
                old_repository.leave_lock_in_place()
787
848
            old_repository.unlock()
 
849
            if lock_token is not None:
 
850
                # XXX: self.repository.leave_lock_in_place() before this
 
851
                # function will not be preserved.  Fortunately that doesn't
 
852
                # affect the current default format (2a), and would be a
 
853
                # corner-case anyway.
 
854
                #  - Andrew Bennetts, 2010/06/30
 
855
                self.repository.dont_leave_lock_in_place()
 
856
            old_lock_count = 0
 
857
            while True:
 
858
                try:
 
859
                    old_repository.unlock()
 
860
                except errors.LockNotHeld:
 
861
                    break
 
862
                old_lock_count += 1
 
863
            if old_lock_count == 0:
 
864
                raise AssertionError(
 
865
                    'old_repository should have been locked at least once.')
 
866
            for i in range(old_lock_count-1):
 
867
                self.repository.lock_write()
 
868
            # Fetch from the old repository into the new.
788
869
            old_repository.lock_read()
789
870
            try:
790
 
                # Repositories don't offer an interface to remove fallback
791
 
                # repositories today; take the conceptually simpler option and just
792
 
                # reopen it.  We reopen it starting from the URL so that we
793
 
                # get a separate connection for RemoteRepositories and can
794
 
                # stream from one of them to the other.  This does mean doing
795
 
                # separate SSH connection setup, but unstacking is not a
796
 
                # common operation so it's tolerable.
797
 
                new_bzrdir = bzrdir.BzrDir.open(self.bzrdir.root_transport.base)
798
 
                new_repository = new_bzrdir.find_repository()
799
 
                self.repository = new_repository
800
 
                if self.repository._fallback_repositories:
801
 
                    raise AssertionError("didn't expect %r to have "
802
 
                        "fallback_repositories"
803
 
                        % (self.repository,))
804
 
                # this is not paired with an unlock because it's just restoring
805
 
                # the previous state; the lock's released when set_stacked_on_url
806
 
                # returns
807
 
                self.repository.lock_write()
808
871
                # XXX: If you unstack a branch while it has a working tree
809
872
                # with a pending merge, the pending-merged revisions will no
810
873
                # longer be present.  You can (probably) revert and remerge.
825
888
 
826
889
        :seealso: Branch._get_tags_bytes.
827
890
        """
828
 
        return _run_with_write_locked_target(self, self._transport.put_bytes,
829
 
            '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)
830
897
 
831
898
    def _cache_revision_history(self, rev_history):
832
899
        """Set the cached revision history to rev_history.
862
929
        self._merge_sorted_revisions_cache = None
863
930
        self._partial_revision_history_cache = []
864
931
        self._partial_revision_id_to_revno_cache = {}
 
932
        self._tags_bytes = None
865
933
 
866
934
    def _gen_revision_history(self):
867
935
        """Return sequence of revision hashes on to this branch.
951
1019
                raise errors.NoSuchRevision(self, stop_revision)
952
1020
        return other_history[self_len:stop_revision]
953
1021
 
954
 
    @needs_write_lock
955
1022
    def update_revisions(self, other, stop_revision=None, overwrite=False,
956
1023
                         graph=None):
957
1024
        """Pull in new perfect-fit revisions.
1006
1073
            self._extend_partial_history(distance_from_last)
1007
1074
        return self._partial_revision_history_cache[distance_from_last]
1008
1075
 
1009
 
    @needs_write_lock
1010
1076
    def pull(self, source, overwrite=False, stop_revision=None,
1011
1077
             possible_transports=None, *args, **kwargs):
1012
1078
        """Mirror source into this branch.
1208
1274
        return result
1209
1275
 
1210
1276
    @needs_read_lock
1211
 
    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):
1212
1279
        """Create a new line of development from the branch, into to_bzrdir.
1213
1280
 
1214
1281
        to_bzrdir controls the branch format.
1219
1286
        if (repository_policy is not None and
1220
1287
            repository_policy.requires_stacking()):
1221
1288
            to_bzrdir._format.require_stacking(_skip_repo=True)
1222
 
        result = to_bzrdir.create_branch()
 
1289
        result = to_bzrdir.create_branch(repository=repository)
1223
1290
        result.lock_write()
1224
1291
        try:
1225
1292
            if repository_policy is not None:
1255
1322
                revno = 1
1256
1323
        destination.set_last_revision_info(revno, revision_id)
1257
1324
 
1258
 
    @needs_read_lock
1259
1325
    def copy_content_into(self, destination, revision_id=None):
1260
1326
        """Copy the content of self into destination.
1261
1327
 
1262
1328
        revision_id: if not None, the revision history in the new branch will
1263
1329
                     be truncated to end with revision_id.
1264
1330
        """
1265
 
        self.update_references(destination)
1266
 
        self._synchronize_history(destination, revision_id)
1267
 
        try:
1268
 
            parent = self.get_parent()
1269
 
        except errors.InaccessibleParent, e:
1270
 
            mutter('parent was not accessible to copy: %s', e)
1271
 
        else:
1272
 
            if parent:
1273
 
                destination.set_parent(parent)
1274
 
        if self._push_should_merge_tags():
1275
 
            self.tags.merge_to(destination.tags)
 
1331
        return InterBranch.get(self, destination).copy_content_into(
 
1332
            revision_id=revision_id)
1276
1333
 
1277
1334
    def update_references(self, target):
1278
1335
        if not getattr(self._format, 'supports_reference_locations', False):
1323
1380
        """Return the most suitable metadir for a checkout of this branch.
1324
1381
        Weaves are used if this branch's repository uses weaves.
1325
1382
        """
1326
 
        if isinstance(self.bzrdir, bzrdir.BzrDirPreSplitOut):
1327
 
            from bzrlib.repofmt import weaverepo
1328
 
            format = bzrdir.BzrDirMetaFormat1()
1329
 
            format.repository_format = weaverepo.RepositoryFormat7()
1330
 
        else:
1331
 
            format = self.repository.bzrdir.checkout_metadir()
1332
 
            format.set_branch_format(self._format)
 
1383
        format = self.repository.bzrdir.checkout_metadir()
 
1384
        format.set_branch_format(self._format)
1333
1385
        return format
1334
1386
 
1335
1387
    def create_clone_on_transport(self, to_transport, revision_id=None,
1336
 
        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):
1337
1390
        """Create a clone of this branch and its bzrdir.
1338
1391
 
1339
1392
        :param to_transport: The transport to clone onto.
1346
1399
        """
1347
1400
        # XXX: Fix the bzrdir API to allow getting the branch back from the
1348
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
1349
1404
        if revision_id is None:
1350
1405
            revision_id = self.last_revision()
1351
1406
        dir_to = self.bzrdir.clone_on_transport(to_transport,
1352
1407
            revision_id=revision_id, stacked_on=stacked_on,
1353
 
            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)
1354
1410
        return dir_to.open_branch()
1355
1411
 
1356
1412
    def create_checkout(self, to_location, revision_id=None,
1481
1537
     * an open routine.
1482
1538
 
1483
1539
    Formats are placed in an dict by their format string for reference
1484
 
    during branch opening. Its not required that these be instances, they
 
1540
    during branch opening. It's not required that these be instances, they
1485
1541
    can be classes themselves with class methods - it simply depends on
1486
1542
    whether state is needed for a given format or not.
1487
1543
 
1510
1566
        try:
1511
1567
            transport = a_bzrdir.get_branch_transport(None, name=name)
1512
1568
            format_string = transport.get_bytes("format")
1513
 
            return klass._formats[format_string]
 
1569
            format = klass._formats[format_string]
 
1570
            if isinstance(format, MetaDirBranchFormatFactory):
 
1571
                return format()
 
1572
            return format
1514
1573
        except errors.NoSuchFile:
1515
1574
            raise errors.NotBranchError(path=transport.base, bzrdir=a_bzrdir)
1516
1575
        except KeyError:
1521
1580
        """Return the current default format."""
1522
1581
        return klass._default_format
1523
1582
 
1524
 
    def get_reference(self, a_bzrdir):
 
1583
    @classmethod
 
1584
    def get_formats(klass):
 
1585
        """Get all the known formats.
 
1586
 
 
1587
        Warning: This triggers a load of all lazy registered formats: do not
 
1588
        use except when that is desireed.
 
1589
        """
 
1590
        result = []
 
1591
        for fmt in klass._formats.values():
 
1592
            if isinstance(fmt, MetaDirBranchFormatFactory):
 
1593
                fmt = fmt()
 
1594
            result.append(fmt)
 
1595
        return result
 
1596
 
 
1597
    def get_reference(self, a_bzrdir, name=None):
1525
1598
        """Get the target reference of the branch in a_bzrdir.
1526
1599
 
1527
1600
        format probing must have been completed before calling
1529
1602
        in a_bzrdir is correct.
1530
1603
 
1531
1604
        :param a_bzrdir: The bzrdir to get the branch data from.
 
1605
        :param name: Name of the colocated branch to fetch
1532
1606
        :return: None if the branch is not a reference branch.
1533
1607
        """
1534
1608
        return None
1535
1609
 
1536
1610
    @classmethod
1537
 
    def set_reference(self, a_bzrdir, to_branch):
 
1611
    def set_reference(self, a_bzrdir, name, to_branch):
1538
1612
        """Set the target reference of the branch in a_bzrdir.
1539
1613
 
1540
1614
        format probing must have been completed before calling
1542
1616
        in a_bzrdir is correct.
1543
1617
 
1544
1618
        :param a_bzrdir: The bzrdir to set the branch reference for.
 
1619
        :param name: Name of colocated branch to set, None for default
1545
1620
        :param to_branch: branch that the checkout is to reference
1546
1621
        """
1547
1622
        raise NotImplementedError(self.set_reference)
1563
1638
            hook(params)
1564
1639
 
1565
1640
    def _initialize_helper(self, a_bzrdir, utf8_files, name=None,
1566
 
                           lock_type='metadir', set_format=True):
 
1641
                           repository=None, lock_type='metadir',
 
1642
                           set_format=True):
1567
1643
        """Initialize a branch in a bzrdir, with specified files
1568
1644
 
1569
1645
        :param a_bzrdir: The bzrdir to initialize the branch in
1603
1679
        finally:
1604
1680
            if lock_taken:
1605
1681
                control_files.unlock()
1606
 
        branch = self.open(a_bzrdir, name, _found=True)
 
1682
        branch = self.open(a_bzrdir, name, _found=True,
 
1683
                found_repository=repository)
1607
1684
        self._run_post_branch_init_hooks(a_bzrdir, name, branch)
1608
1685
        return branch
1609
1686
 
1610
 
    def initialize(self, a_bzrdir, name=None):
 
1687
    def initialize(self, a_bzrdir, name=None, repository=None):
1611
1688
        """Create a branch of this format in a_bzrdir.
1612
1689
        
1613
1690
        :param name: Name of the colocated branch to create.
1647
1724
        """
1648
1725
        raise NotImplementedError(self.network_name)
1649
1726
 
1650
 
    def open(self, a_bzrdir, name=None, _found=False, ignore_fallbacks=False):
 
1727
    def open(self, a_bzrdir, name=None, _found=False, ignore_fallbacks=False,
 
1728
            found_repository=None):
1651
1729
        """Return the branch object for a_bzrdir
1652
1730
 
1653
1731
        :param a_bzrdir: A BzrDir that contains a branch.
1661
1739
 
1662
1740
    @classmethod
1663
1741
    def register_format(klass, format):
1664
 
        """Register a metadir format."""
 
1742
        """Register a metadir format.
 
1743
        
 
1744
        See MetaDirBranchFormatFactory for the ability to register a format
 
1745
        without loading the code the format needs until it is actually used.
 
1746
        """
1665
1747
        klass._formats[format.get_format_string()] = format
1666
1748
        # Metadir formats have a network name of their format string, and get
1667
 
        # registered as class factories.
1668
 
        network_format_registry.register(format.get_format_string(), format.__class__)
 
1749
        # registered as factories.
 
1750
        if isinstance(format, MetaDirBranchFormatFactory):
 
1751
            network_format_registry.register(format.get_format_string(), format)
 
1752
        else:
 
1753
            network_format_registry.register(format.get_format_string(),
 
1754
                format.__class__)
1669
1755
 
1670
1756
    @classmethod
1671
1757
    def set_default_format(klass, format):
1691
1777
        return False  # by default
1692
1778
 
1693
1779
 
 
1780
class MetaDirBranchFormatFactory(registry._LazyObjectGetter):
 
1781
    """A factory for a BranchFormat object, permitting simple lazy registration.
 
1782
    
 
1783
    While none of the built in BranchFormats are lazy registered yet,
 
1784
    bzrlib.tests.test_branch.TestMetaDirBranchFormatFactory demonstrates how to
 
1785
    use it, and the bzr-loom plugin uses it as well (see
 
1786
    bzrlib.plugins.loom.formats).
 
1787
    """
 
1788
 
 
1789
    def __init__(self, format_string, module_name, member_name):
 
1790
        """Create a MetaDirBranchFormatFactory.
 
1791
 
 
1792
        :param format_string: The format string the format has.
 
1793
        :param module_name: Module to load the format class from.
 
1794
        :param member_name: Attribute name within the module for the format class.
 
1795
        """
 
1796
        registry._LazyObjectGetter.__init__(self, module_name, member_name)
 
1797
        self._format_string = format_string
 
1798
        
 
1799
    def get_format_string(self):
 
1800
        """See BranchFormat.get_format_string."""
 
1801
        return self._format_string
 
1802
 
 
1803
    def __call__(self):
 
1804
        """Used for network_format_registry support."""
 
1805
        return self.get_obj()()
 
1806
 
 
1807
 
1694
1808
class BranchHooks(Hooks):
1695
1809
    """A dictionary mapping hook name to a list of callables for branch hooks.
1696
1810
 
1723
1837
            "with a bzrlib.branch.PullResult object and only runs in the "
1724
1838
            "bzr client.", (0, 15), None))
1725
1839
        self.create_hook(HookPoint('pre_commit',
1726
 
            "Called after a commit is calculated but before it is is "
 
1840
            "Called after a commit is calculated but before it is "
1727
1841
            "completed. pre_commit is called with (local, master, old_revno, "
1728
1842
            "old_revid, future_revno, future_revid, tree_delta, future_tree"
1729
1843
            "). old_revid is NULL_REVISION for the first commit to a branch, "
1766
1880
            "all are called with the url returned from the previous hook."
1767
1881
            "The order is however undefined.", (1, 9), None))
1768
1882
        self.create_hook(HookPoint('automatic_tag_name',
1769
 
            "Called to determine an automatic tag name for a revision."
 
1883
            "Called to determine an automatic tag name for a revision. "
1770
1884
            "automatic_tag_name is called with (branch, revision_id) and "
1771
1885
            "should return a tag name or None if no tag name could be "
1772
1886
            "determined. The first non-None tag name returned will be used.",
1863
1977
        return self.__dict__ == other.__dict__
1864
1978
 
1865
1979
    def __repr__(self):
1866
 
        if self.branch:
1867
 
            return "<%s of %s>" % (self.__class__.__name__, self.branch)
1868
 
        else:
1869
 
            return "<%s of format:%s bzrdir:%s>" % (
1870
 
                self.__class__.__name__, self.branch,
1871
 
                self.format, self.bzrdir)
 
1980
        return "<%s of %s>" % (self.__class__.__name__, self.branch)
1872
1981
 
1873
1982
 
1874
1983
class SwitchHookParams(object):
1904
2013
            self.revision_id)
1905
2014
 
1906
2015
 
1907
 
class BzrBranchFormat4(BranchFormat):
1908
 
    """Bzr branch format 4.
1909
 
 
1910
 
    This format has:
1911
 
     - a revision-history file.
1912
 
     - a branch-lock lock file [ to be shared with the bzrdir ]
1913
 
    """
1914
 
 
1915
 
    def get_format_description(self):
1916
 
        """See BranchFormat.get_format_description()."""
1917
 
        return "Branch format 4"
1918
 
 
1919
 
    def initialize(self, a_bzrdir, name=None):
1920
 
        """Create a branch of this format in a_bzrdir."""
1921
 
        utf8_files = [('revision-history', ''),
1922
 
                      ('branch-name', ''),
1923
 
                      ]
1924
 
        return self._initialize_helper(a_bzrdir, utf8_files, name=name,
1925
 
                                       lock_type='branch4', set_format=False)
1926
 
 
1927
 
    def __init__(self):
1928
 
        super(BzrBranchFormat4, self).__init__()
1929
 
        self._matchingbzrdir = bzrdir.BzrDirFormat6()
1930
 
 
1931
 
    def network_name(self):
1932
 
        """The network name for this format is the control dirs disk label."""
1933
 
        return self._matchingbzrdir.get_format_string()
1934
 
 
1935
 
    def open(self, a_bzrdir, name=None, _found=False, ignore_fallbacks=False):
1936
 
        """See BranchFormat.open()."""
1937
 
        if not _found:
1938
 
            # we are being called directly and must probe.
1939
 
            raise NotImplementedError
1940
 
        return BzrBranch(_format=self,
1941
 
                         _control_files=a_bzrdir._control_files,
1942
 
                         a_bzrdir=a_bzrdir,
1943
 
                         name=name,
1944
 
                         _repository=a_bzrdir.open_repository())
1945
 
 
1946
 
    def __str__(self):
1947
 
        return "Bazaar-NG branch format 4"
1948
 
 
1949
 
 
1950
2016
class BranchFormatMetadir(BranchFormat):
1951
2017
    """Common logic for meta-dir based branch formats."""
1952
2018
 
1961
2027
        """
1962
2028
        return self.get_format_string()
1963
2029
 
1964
 
    def open(self, a_bzrdir, name=None, _found=False, ignore_fallbacks=False):
 
2030
    def open(self, a_bzrdir, name=None, _found=False, ignore_fallbacks=False,
 
2031
            found_repository=None):
1965
2032
        """See BranchFormat.open()."""
1966
2033
        if not _found:
1967
2034
            format = BranchFormat.find_format(a_bzrdir, name=name)
1972
2039
        try:
1973
2040
            control_files = lockable_files.LockableFiles(transport, 'lock',
1974
2041
                                                         lockdir.LockDir)
 
2042
            if found_repository is None:
 
2043
                found_repository = a_bzrdir.find_repository()
1975
2044
            return self._branch_class()(_format=self,
1976
2045
                              _control_files=control_files,
1977
2046
                              name=name,
1978
2047
                              a_bzrdir=a_bzrdir,
1979
 
                              _repository=a_bzrdir.find_repository(),
 
2048
                              _repository=found_repository,
1980
2049
                              ignore_fallbacks=ignore_fallbacks)
1981
2050
        except errors.NoSuchFile:
1982
2051
            raise errors.NotBranchError(path=transport.base, bzrdir=a_bzrdir)
2014
2083
        """See BranchFormat.get_format_description()."""
2015
2084
        return "Branch format 5"
2016
2085
 
2017
 
    def initialize(self, a_bzrdir, name=None):
 
2086
    def initialize(self, a_bzrdir, name=None, repository=None):
2018
2087
        """Create a branch of this format in a_bzrdir."""
2019
2088
        utf8_files = [('revision-history', ''),
2020
2089
                      ('branch-name', ''),
2021
2090
                      ]
2022
 
        return self._initialize_helper(a_bzrdir, utf8_files, name)
 
2091
        return self._initialize_helper(a_bzrdir, utf8_files, name, repository)
2023
2092
 
2024
2093
    def supports_tags(self):
2025
2094
        return False
2047
2116
        """See BranchFormat.get_format_description()."""
2048
2117
        return "Branch format 6"
2049
2118
 
2050
 
    def initialize(self, a_bzrdir, name=None):
 
2119
    def initialize(self, a_bzrdir, name=None, repository=None):
2051
2120
        """Create a branch of this format in a_bzrdir."""
2052
2121
        utf8_files = [('last-revision', '0 null:\n'),
2053
2122
                      ('branch.conf', ''),
2054
2123
                      ('tags', ''),
2055
2124
                      ]
2056
 
        return self._initialize_helper(a_bzrdir, utf8_files, name)
 
2125
        return self._initialize_helper(a_bzrdir, utf8_files, name, repository)
2057
2126
 
2058
2127
    def make_tags(self, branch):
2059
2128
        """See bzrlib.branch.BranchFormat.make_tags()."""
2077
2146
        """See BranchFormat.get_format_description()."""
2078
2147
        return "Branch format 8"
2079
2148
 
2080
 
    def initialize(self, a_bzrdir, name=None):
 
2149
    def initialize(self, a_bzrdir, name=None, repository=None):
2081
2150
        """Create a branch of this format in a_bzrdir."""
2082
2151
        utf8_files = [('last-revision', '0 null:\n'),
2083
2152
                      ('branch.conf', ''),
2084
2153
                      ('tags', ''),
2085
2154
                      ('references', '')
2086
2155
                      ]
2087
 
        return self._initialize_helper(a_bzrdir, utf8_files, name)
 
2156
        return self._initialize_helper(a_bzrdir, utf8_files, name, repository)
2088
2157
 
2089
2158
    def __init__(self):
2090
2159
        super(BzrBranchFormat8, self).__init__()
2113
2182
    This format was introduced in bzr 1.6.
2114
2183
    """
2115
2184
 
2116
 
    def initialize(self, a_bzrdir, name=None):
 
2185
    def initialize(self, a_bzrdir, name=None, repository=None):
2117
2186
        """Create a branch of this format in a_bzrdir."""
2118
2187
        utf8_files = [('last-revision', '0 null:\n'),
2119
2188
                      ('branch.conf', ''),
2120
2189
                      ('tags', ''),
2121
2190
                      ]
2122
 
        return self._initialize_helper(a_bzrdir, utf8_files, name)
 
2191
        return self._initialize_helper(a_bzrdir, utf8_files, name, repository)
2123
2192
 
2124
2193
    def _branch_class(self):
2125
2194
        return BzrBranch7
2157
2226
        """See BranchFormat.get_format_description()."""
2158
2227
        return "Checkout reference format 1"
2159
2228
 
2160
 
    def get_reference(self, a_bzrdir):
 
2229
    def get_reference(self, a_bzrdir, name=None):
2161
2230
        """See BranchFormat.get_reference()."""
2162
 
        transport = a_bzrdir.get_branch_transport(None)
 
2231
        transport = a_bzrdir.get_branch_transport(None, name=name)
2163
2232
        return transport.get_bytes('location')
2164
2233
 
2165
 
    def set_reference(self, a_bzrdir, to_branch):
 
2234
    def set_reference(self, a_bzrdir, name, to_branch):
2166
2235
        """See BranchFormat.set_reference()."""
2167
 
        transport = a_bzrdir.get_branch_transport(None)
 
2236
        transport = a_bzrdir.get_branch_transport(None, name=name)
2168
2237
        location = transport.put_bytes('location', to_branch.base)
2169
2238
 
2170
 
    def initialize(self, a_bzrdir, name=None, target_branch=None):
 
2239
    def initialize(self, a_bzrdir, name=None, target_branch=None,
 
2240
            repository=None):
2171
2241
        """Create a branch of this format in a_bzrdir."""
2172
2242
        if target_branch is None:
2173
2243
            # this format does not implement branch itself, thus the implicit
2201
2271
        return clone
2202
2272
 
2203
2273
    def open(self, a_bzrdir, name=None, _found=False, location=None,
2204
 
             possible_transports=None, ignore_fallbacks=False):
 
2274
             possible_transports=None, ignore_fallbacks=False,
 
2275
             found_repository=None):
2205
2276
        """Return the branch that the branch reference in a_bzrdir points at.
2206
2277
 
2207
2278
        :param a_bzrdir: A BzrDir that contains a branch.
2221
2292
                raise AssertionError("wrong format %r found for %r" %
2222
2293
                    (format, self))
2223
2294
        if location is None:
2224
 
            location = self.get_reference(a_bzrdir)
 
2295
            location = self.get_reference(a_bzrdir, name)
2225
2296
        real_bzrdir = bzrdir.BzrDir.open(
2226
2297
            location, possible_transports=possible_transports)
2227
2298
        result = real_bzrdir.open_branch(name=name, 
2259
2330
BranchFormat.register_format(__format7)
2260
2331
BranchFormat.register_format(__format8)
2261
2332
BranchFormat.set_default_format(__format7)
2262
 
_legacy_formats = [BzrBranchFormat4(),
2263
 
    ]
2264
 
network_format_registry.register(
2265
 
    _legacy_formats[0].network_name(), _legacy_formats[0].__class__)
 
2333
 
 
2334
 
 
2335
class BranchWriteLockResult(LogicalLockResult):
 
2336
    """The result of write locking a branch.
 
2337
 
 
2338
    :ivar branch_token: The token obtained from the underlying branch lock, or
 
2339
        None.
 
2340
    :ivar unlock: A callable which will unlock the lock.
 
2341
    """
 
2342
 
 
2343
    def __init__(self, unlock, branch_token):
 
2344
        LogicalLockResult.__init__(self, unlock)
 
2345
        self.branch_token = branch_token
 
2346
 
 
2347
    def __repr__(self):
 
2348
        return "BranchWriteLockResult(%s, %s)" % (self.branch_token,
 
2349
            self.unlock)
2266
2350
 
2267
2351
 
2268
2352
class BzrBranch(Branch, _RelockDebugMixin):
2324
2408
        return self.control_files.is_locked()
2325
2409
 
2326
2410
    def lock_write(self, token=None):
 
2411
        """Lock the branch for write operations.
 
2412
 
 
2413
        :param token: A token to permit reacquiring a previously held and
 
2414
            preserved lock.
 
2415
        :return: A BranchWriteLockResult.
 
2416
        """
2327
2417
        if not self.is_locked():
2328
2418
            self._note_lock('w')
2329
2419
        # All-in-one needs to always unlock/lock.
2335
2425
        else:
2336
2426
            took_lock = False
2337
2427
        try:
2338
 
            return self.control_files.lock_write(token=token)
 
2428
            return BranchWriteLockResult(self.unlock,
 
2429
                self.control_files.lock_write(token=token))
2339
2430
        except:
2340
2431
            if took_lock:
2341
2432
                self.repository.unlock()
2342
2433
            raise
2343
2434
 
2344
2435
    def lock_read(self):
 
2436
        """Lock the branch for read operations.
 
2437
 
 
2438
        :return: A bzrlib.lock.LogicalLockResult.
 
2439
        """
2345
2440
        if not self.is_locked():
2346
2441
            self._note_lock('r')
2347
2442
        # All-in-one needs to always unlock/lock.
2354
2449
            took_lock = False
2355
2450
        try:
2356
2451
            self.control_files.lock_read()
 
2452
            return LogicalLockResult(self.unlock)
2357
2453
        except:
2358
2454
            if took_lock:
2359
2455
                self.repository.unlock()
2544
2640
                mode=self.bzrdir._get_file_mode())
2545
2641
 
2546
2642
 
 
2643
class BzrBranchPreSplitOut(BzrBranch):
 
2644
 
 
2645
    def _get_checkout_format(self):
 
2646
        """Return the most suitable metadir for a checkout of this branch.
 
2647
        Weaves are used if this branch's repository uses weaves.
 
2648
        """
 
2649
        from bzrlib.repofmt.weaverepo import RepositoryFormat7
 
2650
        from bzrlib.bzrdir import BzrDirMetaFormat1
 
2651
        format = BzrDirMetaFormat1()
 
2652
        format.repository_format = RepositoryFormat7()
 
2653
        return format
 
2654
 
 
2655
 
2547
2656
class BzrBranch5(BzrBranch):
2548
2657
    """A format 5 branch. This supports new features over plain branches.
2549
2658
 
2983
3092
    :ivar tag_conflicts: A list of tag conflicts, see BasicTags.merge_to
2984
3093
    """
2985
3094
 
 
3095
    @deprecated_method(deprecated_in((2, 3, 0)))
2986
3096
    def __int__(self):
2987
 
        # DEPRECATED: pull used to return the change in revno
 
3097
        """Return the relative change in revno.
 
3098
 
 
3099
        :deprecated: Use `new_revno` and `old_revno` instead.
 
3100
        """
2988
3101
        return self.new_revno - self.old_revno
2989
3102
 
2990
3103
    def report(self, to_file):
3015
3128
        target, otherwise it will be None.
3016
3129
    """
3017
3130
 
 
3131
    @deprecated_method(deprecated_in((2, 3, 0)))
3018
3132
    def __int__(self):
3019
 
        # DEPRECATED: push used to return the change in revno
 
3133
        """Return the relative change in revno.
 
3134
 
 
3135
        :deprecated: Use `new_revno` and `old_revno` instead.
 
3136
        """
3020
3137
        return self.new_revno - self.old_revno
3021
3138
 
3022
3139
    def report(self, to_file):
3145
3262
    _optimisers = []
3146
3263
    """The available optimised InterBranch types."""
3147
3264
 
3148
 
    @staticmethod
3149
 
    def _get_branch_formats_to_test():
3150
 
        """Return a tuple with the Branch formats to use when testing."""
3151
 
        raise NotImplementedError(InterBranch._get_branch_formats_to_test)
 
3265
    @classmethod
 
3266
    def _get_branch_formats_to_test(klass):
 
3267
        """Return an iterable of format tuples for testing.
 
3268
        
 
3269
        :return: An iterable of (from_format, to_format) to use when testing
 
3270
            this InterBranch class. Each InterBranch class should define this
 
3271
            method itself.
 
3272
        """
 
3273
        raise NotImplementedError(klass._get_branch_formats_to_test)
3152
3274
 
 
3275
    @needs_write_lock
3153
3276
    def pull(self, overwrite=False, stop_revision=None,
3154
3277
             possible_transports=None, local=False):
3155
3278
        """Mirror source into target branch.
3160
3283
        """
3161
3284
        raise NotImplementedError(self.pull)
3162
3285
 
 
3286
    @needs_write_lock
3163
3287
    def update_revisions(self, stop_revision=None, overwrite=False,
3164
3288
                         graph=None):
3165
3289
        """Pull in new perfect-fit revisions.
3173
3297
        """
3174
3298
        raise NotImplementedError(self.update_revisions)
3175
3299
 
 
3300
    @needs_write_lock
3176
3301
    def push(self, overwrite=False, stop_revision=None,
3177
3302
             _override_hook_source_branch=None):
3178
3303
        """Mirror the source branch into the target branch.
3181
3306
        """
3182
3307
        raise NotImplementedError(self.push)
3183
3308
 
 
3309
    @needs_write_lock
 
3310
    def copy_content_into(self, revision_id=None):
 
3311
        """Copy the content of source into target
 
3312
 
 
3313
        revision_id: if not None, the revision history in the new branch will
 
3314
                     be truncated to end with revision_id.
 
3315
        """
 
3316
        raise NotImplementedError(self.copy_content_into)
 
3317
 
3184
3318
 
3185
3319
class GenericInterBranch(InterBranch):
3186
 
    """InterBranch implementation that uses public Branch functions.
3187
 
    """
3188
 
 
3189
 
    @staticmethod
3190
 
    def _get_branch_formats_to_test():
3191
 
        return BranchFormat._default_format, BranchFormat._default_format
3192
 
 
 
3320
    """InterBranch implementation that uses public Branch functions."""
 
3321
 
 
3322
    @classmethod
 
3323
    def is_compatible(klass, source, target):
 
3324
        # GenericBranch uses the public API, so always compatible
 
3325
        return True
 
3326
 
 
3327
    @classmethod
 
3328
    def _get_branch_formats_to_test(klass):
 
3329
        return [(BranchFormat._default_format, BranchFormat._default_format)]
 
3330
 
 
3331
    @classmethod
 
3332
    def unwrap_format(klass, format):
 
3333
        if isinstance(format, remote.RemoteBranchFormat):
 
3334
            format._ensure_real()
 
3335
            return format._custom_format
 
3336
        return format
 
3337
 
 
3338
    @needs_write_lock
 
3339
    def copy_content_into(self, revision_id=None):
 
3340
        """Copy the content of source into target
 
3341
 
 
3342
        revision_id: if not None, the revision history in the new branch will
 
3343
                     be truncated to end with revision_id.
 
3344
        """
 
3345
        self.source.update_references(self.target)
 
3346
        self.source._synchronize_history(self.target, revision_id)
 
3347
        try:
 
3348
            parent = self.source.get_parent()
 
3349
        except errors.InaccessibleParent, e:
 
3350
            mutter('parent was not accessible to copy: %s', e)
 
3351
        else:
 
3352
            if parent:
 
3353
                self.target.set_parent(parent)
 
3354
        if self.source._push_should_merge_tags():
 
3355
            self.source.tags.merge_to(self.target.tags)
 
3356
 
 
3357
    @needs_write_lock
3193
3358
    def update_revisions(self, stop_revision=None, overwrite=False,
3194
3359
        graph=None):
3195
3360
        """See InterBranch.update_revisions()."""
3196
 
        self.source.lock_read()
3197
 
        try:
3198
 
            other_revno, other_last_revision = self.source.last_revision_info()
3199
 
            stop_revno = None # unknown
3200
 
            if stop_revision is None:
3201
 
                stop_revision = other_last_revision
3202
 
                if _mod_revision.is_null(stop_revision):
3203
 
                    # if there are no commits, we're done.
3204
 
                    return
3205
 
                stop_revno = other_revno
3206
 
 
3207
 
            # what's the current last revision, before we fetch [and change it
3208
 
            # possibly]
3209
 
            last_rev = _mod_revision.ensure_null(self.target.last_revision())
3210
 
            # we fetch here so that we don't process data twice in the common
3211
 
            # case of having something to pull, and so that the check for
3212
 
            # already merged can operate on the just fetched graph, which will
3213
 
            # be cached in memory.
3214
 
            self.target.fetch(self.source, stop_revision)
3215
 
            # Check to see if one is an ancestor of the other
3216
 
            if not overwrite:
3217
 
                if graph is None:
3218
 
                    graph = self.target.repository.get_graph()
3219
 
                if self.target._check_if_descendant_or_diverged(
3220
 
                        stop_revision, last_rev, graph, self.source):
3221
 
                    # stop_revision is a descendant of last_rev, but we aren't
3222
 
                    # overwriting, so we're done.
3223
 
                    return
3224
 
            if stop_revno is None:
3225
 
                if graph is None:
3226
 
                    graph = self.target.repository.get_graph()
3227
 
                this_revno, this_last_revision = \
3228
 
                        self.target.last_revision_info()
3229
 
                stop_revno = graph.find_distance_to_null(stop_revision,
3230
 
                                [(other_last_revision, other_revno),
3231
 
                                 (this_last_revision, this_revno)])
3232
 
            self.target.set_last_revision_info(stop_revno, stop_revision)
3233
 
        finally:
3234
 
            self.source.unlock()
3235
 
 
 
3361
        other_revno, other_last_revision = self.source.last_revision_info()
 
3362
        stop_revno = None # unknown
 
3363
        if stop_revision is None:
 
3364
            stop_revision = other_last_revision
 
3365
            if _mod_revision.is_null(stop_revision):
 
3366
                # if there are no commits, we're done.
 
3367
                return
 
3368
            stop_revno = other_revno
 
3369
 
 
3370
        # what's the current last revision, before we fetch [and change it
 
3371
        # possibly]
 
3372
        last_rev = _mod_revision.ensure_null(self.target.last_revision())
 
3373
        # we fetch here so that we don't process data twice in the common
 
3374
        # case of having something to pull, and so that the check for
 
3375
        # already merged can operate on the just fetched graph, which will
 
3376
        # be cached in memory.
 
3377
        self.target.fetch(self.source, stop_revision)
 
3378
        # Check to see if one is an ancestor of the other
 
3379
        if not overwrite:
 
3380
            if graph is None:
 
3381
                graph = self.target.repository.get_graph()
 
3382
            if self.target._check_if_descendant_or_diverged(
 
3383
                    stop_revision, last_rev, graph, self.source):
 
3384
                # stop_revision is a descendant of last_rev, but we aren't
 
3385
                # overwriting, so we're done.
 
3386
                return
 
3387
        if stop_revno is None:
 
3388
            if graph is None:
 
3389
                graph = self.target.repository.get_graph()
 
3390
            this_revno, this_last_revision = \
 
3391
                    self.target.last_revision_info()
 
3392
            stop_revno = graph.find_distance_to_null(stop_revision,
 
3393
                            [(other_last_revision, other_revno),
 
3394
                             (this_last_revision, this_revno)])
 
3395
        self.target.set_last_revision_info(stop_revno, stop_revision)
 
3396
 
 
3397
    @needs_write_lock
3236
3398
    def pull(self, overwrite=False, stop_revision=None,
3237
 
             possible_transports=None, _hook_master=None, run_hooks=True,
 
3399
             possible_transports=None, run_hooks=True,
3238
3400
             _override_hook_target=None, local=False):
3239
 
        """See Branch.pull.
 
3401
        """Pull from source into self, updating my master if any.
3240
3402
 
3241
 
        :param _hook_master: Private parameter - set the branch to
3242
 
            be supplied as the master to pull hooks.
3243
3403
        :param run_hooks: Private parameter - if false, this branch
3244
3404
            is being called because it's the master of the primary branch,
3245
3405
            so it should not run its hooks.
3246
 
        :param _override_hook_target: Private parameter - set the branch to be
3247
 
            supplied as the target_branch to pull hooks.
3248
 
        :param local: Only update the local branch, and not the bound branch.
3249
3406
        """
3250
 
        # This type of branch can't be bound.
3251
 
        if local:
 
3407
        bound_location = self.target.get_bound_location()
 
3408
        if local and not bound_location:
3252
3409
            raise errors.LocalRequiresBoundBranch()
3253
 
        result = PullResult()
3254
 
        result.source_branch = self.source
3255
 
        if _override_hook_target is None:
3256
 
            result.target_branch = self.target
3257
 
        else:
3258
 
            result.target_branch = _override_hook_target
3259
 
        self.source.lock_read()
 
3410
        master_branch = None
 
3411
        source_is_master = (self.source.user_url == bound_location)
 
3412
        if not local and bound_location and not source_is_master:
 
3413
            # not pulling from master, so we need to update master.
 
3414
            master_branch = self.target.get_master_branch(possible_transports)
 
3415
            master_branch.lock_write()
3260
3416
        try:
3261
 
            # We assume that during 'pull' the target repository is closer than
3262
 
            # the source one.
3263
 
            self.source.update_references(self.target)
3264
 
            graph = self.target.repository.get_graph(self.source.repository)
3265
 
            # TODO: Branch formats should have a flag that indicates 
3266
 
            # that revno's are expensive, and pull() should honor that flag.
3267
 
            # -- JRV20090506
3268
 
            result.old_revno, result.old_revid = \
3269
 
                self.target.last_revision_info()
3270
 
            self.target.update_revisions(self.source, stop_revision,
3271
 
                overwrite=overwrite, graph=graph)
3272
 
            # TODO: The old revid should be specified when merging tags, 
3273
 
            # so a tags implementation that versions tags can only 
3274
 
            # pull in the most recent changes. -- JRV20090506
3275
 
            result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
3276
 
                overwrite)
3277
 
            result.new_revno, result.new_revid = self.target.last_revision_info()
3278
 
            if _hook_master:
3279
 
                result.master_branch = _hook_master
3280
 
                result.local_branch = result.target_branch
3281
 
            else:
3282
 
                result.master_branch = result.target_branch
3283
 
                result.local_branch = None
3284
 
            if run_hooks:
3285
 
                for hook in Branch.hooks['post_pull']:
3286
 
                    hook(result)
 
3417
            if master_branch:
 
3418
                # pull from source into master.
 
3419
                master_branch.pull(self.source, overwrite, stop_revision,
 
3420
                    run_hooks=False)
 
3421
            return self._pull(overwrite,
 
3422
                stop_revision, _hook_master=master_branch,
 
3423
                run_hooks=run_hooks,
 
3424
                _override_hook_target=_override_hook_target,
 
3425
                merge_tags_to_master=not source_is_master)
3287
3426
        finally:
3288
 
            self.source.unlock()
3289
 
        return result
 
3427
            if master_branch:
 
3428
                master_branch.unlock()
3290
3429
 
3291
3430
    def push(self, overwrite=False, stop_revision=None,
3292
3431
             _override_hook_source_branch=None):
3332
3471
                # push into the master from the source branch.
3333
3472
                self.source._basic_push(master_branch, overwrite, stop_revision)
3334
3473
                # and push into the target branch from the source. Note that we
3335
 
                # push from the source branch again, because its considered the
 
3474
                # push from the source branch again, because it's considered the
3336
3475
                # highest bandwidth repository.
3337
3476
                result = self.source._basic_push(self.target, overwrite,
3338
3477
                    stop_revision)
3354
3493
            _run_hooks()
3355
3494
            return result
3356
3495
 
3357
 
    @classmethod
3358
 
    def is_compatible(self, source, target):
3359
 
        # GenericBranch uses the public API, so always compatible
3360
 
        return True
3361
 
 
3362
 
 
3363
 
class InterToBranch5(GenericInterBranch):
3364
 
 
3365
 
    @staticmethod
3366
 
    def _get_branch_formats_to_test():
3367
 
        return BranchFormat._default_format, BzrBranchFormat5()
3368
 
 
3369
 
    def pull(self, overwrite=False, stop_revision=None,
3370
 
             possible_transports=None, run_hooks=True,
3371
 
             _override_hook_target=None, local=False):
3372
 
        """Pull from source into self, updating my master if any.
3373
 
 
 
3496
    def _pull(self, overwrite=False, stop_revision=None,
 
3497
             possible_transports=None, _hook_master=None, run_hooks=True,
 
3498
             _override_hook_target=None, local=False,
 
3499
             merge_tags_to_master=True):
 
3500
        """See Branch.pull.
 
3501
 
 
3502
        This function is the core worker, used by GenericInterBranch.pull to
 
3503
        avoid duplication when pulling source->master and source->local.
 
3504
 
 
3505
        :param _hook_master: Private parameter - set the branch to
 
3506
            be supplied as the master to pull hooks.
3374
3507
        :param run_hooks: Private parameter - if false, this branch
3375
3508
            is being called because it's the master of the primary branch,
3376
3509
            so it should not run its hooks.
 
3510
        :param _override_hook_target: Private parameter - set the branch to be
 
3511
            supplied as the target_branch to pull hooks.
 
3512
        :param local: Only update the local branch, and not the bound branch.
3377
3513
        """
3378
 
        bound_location = self.target.get_bound_location()
3379
 
        if local and not bound_location:
 
3514
        # This type of branch can't be bound.
 
3515
        if local:
3380
3516
            raise errors.LocalRequiresBoundBranch()
3381
 
        master_branch = None
3382
 
        if not local and bound_location and self.source.user_url != bound_location:
3383
 
            # not pulling from master, so we need to update master.
3384
 
            master_branch = self.target.get_master_branch(possible_transports)
3385
 
            master_branch.lock_write()
 
3517
        result = PullResult()
 
3518
        result.source_branch = self.source
 
3519
        if _override_hook_target is None:
 
3520
            result.target_branch = self.target
 
3521
        else:
 
3522
            result.target_branch = _override_hook_target
 
3523
        self.source.lock_read()
3386
3524
        try:
3387
 
            if master_branch:
3388
 
                # pull from source into master.
3389
 
                master_branch.pull(self.source, overwrite, stop_revision,
3390
 
                    run_hooks=False)
3391
 
            return super(InterToBranch5, self).pull(overwrite,
3392
 
                stop_revision, _hook_master=master_branch,
3393
 
                run_hooks=run_hooks,
3394
 
                _override_hook_target=_override_hook_target)
 
3525
            # We assume that during 'pull' the target repository is closer than
 
3526
            # the source one.
 
3527
            self.source.update_references(self.target)
 
3528
            graph = self.target.repository.get_graph(self.source.repository)
 
3529
            # TODO: Branch formats should have a flag that indicates 
 
3530
            # that revno's are expensive, and pull() should honor that flag.
 
3531
            # -- JRV20090506
 
3532
            result.old_revno, result.old_revid = \
 
3533
                self.target.last_revision_info()
 
3534
            self.target.update_revisions(self.source, stop_revision,
 
3535
                overwrite=overwrite, graph=graph)
 
3536
            # TODO: The old revid should be specified when merging tags, 
 
3537
            # so a tags implementation that versions tags can only 
 
3538
            # pull in the most recent changes. -- JRV20090506
 
3539
            result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
 
3540
                overwrite, ignore_master=not merge_tags_to_master)
 
3541
            result.new_revno, result.new_revid = self.target.last_revision_info()
 
3542
            if _hook_master:
 
3543
                result.master_branch = _hook_master
 
3544
                result.local_branch = result.target_branch
 
3545
            else:
 
3546
                result.master_branch = result.target_branch
 
3547
                result.local_branch = None
 
3548
            if run_hooks:
 
3549
                for hook in Branch.hooks['post_pull']:
 
3550
                    hook(result)
3395
3551
        finally:
3396
 
            if master_branch:
3397
 
                master_branch.unlock()
 
3552
            self.source.unlock()
 
3553
        return result
3398
3554
 
3399
3555
 
3400
3556
InterBranch.register_optimiser(GenericInterBranch)
3401
 
InterBranch.register_optimiser(InterToBranch5)