/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

  • Committer: Robert Collins
  • Date: 2010-05-06 11:08:10 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100506110810-h3j07fh5gmw54s25
Cleaner matcher matching revised unlocking protocol.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2011 Canonical Ltd
 
1
# Copyright (C) 2005-2010 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,
29
28
        debug,
30
29
        errors,
31
30
        lockdir,
32
31
        lockable_files,
33
 
        remote,
34
32
        repository,
35
33
        revision as _mod_revision,
36
34
        rio,
51
49
from bzrlib.decorators import needs_read_lock, needs_write_lock, only_raises
52
50
from bzrlib.hooks import HookPoint, Hooks
53
51
from bzrlib.inter import InterObject
54
 
from bzrlib.lock import _RelockDebugMixin, LogicalLockResult
 
52
from bzrlib.lock import _RelockDebugMixin
55
53
from bzrlib import registry
56
54
from bzrlib.symbol_versioning import (
57
55
    deprecated_in,
65
63
BZR_BRANCH_FORMAT_6 = "Bazaar Branch Format 6 (bzr 0.15)\n"
66
64
 
67
65
 
68
 
class Branch(controldir.ControlComponent):
 
66
class Branch(bzrdir.ControlComponent):
69
67
    """Branch holding a history of revisions.
70
68
 
71
69
    :ivar base:
92
90
        self._revision_id_to_revno_cache = None
93
91
        self._partial_revision_id_to_revno_cache = {}
94
92
        self._partial_revision_history_cache = []
95
 
        self._tags_bytes = None
96
93
        self._last_revision_info_cache = None
97
94
        self._merge_sorted_revisions_cache = None
98
95
        self._open_hook()
105
102
 
106
103
    def _activate_fallback_location(self, url):
107
104
        """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
115
105
        repo = self._get_fallback_repository(url)
116
106
        if repo.has_same_location(self.repository):
117
107
            raise errors.UnstackableLocationError(self.user_url, url)
207
197
        return self.supports_tags() and self.tags.get_tag_dict()
208
198
 
209
199
    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
 
        """
217
200
        return BranchConfig(self)
218
201
 
219
202
    def _get_config(self):
235
218
            possible_transports=[self.bzrdir.root_transport])
236
219
        return a_branch.repository
237
220
 
238
 
    @needs_read_lock
239
221
    def _get_tags_bytes(self):
240
222
        """Get the bytes of a serialised tags dict.
241
223
 
248
230
        :return: The bytes of the tags file.
249
231
        :seealso: Branch._set_tags_bytes.
250
232
        """
251
 
        if self._tags_bytes is None:
252
 
            self._tags_bytes = self._transport.get_bytes('tags')
253
 
        return self._tags_bytes
 
233
        return self._transport.get_bytes('tags')
254
234
 
255
235
    def _get_nick(self, local=False, possible_transports=None):
256
236
        config = self.get_config()
258
238
        if not local and not config.has_explicit_nickname():
259
239
            try:
260
240
                master = self.get_master_branch(possible_transports)
261
 
                if master and self.user_url == master.user_url:
262
 
                    raise errors.RecursiveBind(self.user_url)
263
241
                if master is not None:
264
242
                    # return the master branch value
265
243
                    return master.nick
266
 
            except errors.RecursiveBind, e:
267
 
                raise e
268
244
            except errors.BzrError, e:
269
245
                # Silently fall back to local implicit nick if the master is
270
246
                # unavailable
307
283
        new_history.reverse()
308
284
        return new_history
309
285
 
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
 
        """
 
286
    def lock_write(self):
317
287
        raise NotImplementedError(self.lock_write)
318
288
 
319
289
    def lock_read(self):
320
 
        """Lock the branch for read operations.
321
 
 
322
 
        :return: A bzrlib.lock.LogicalLockResult.
323
 
        """
324
290
        raise NotImplementedError(self.lock_read)
325
291
 
326
292
    def unlock(self):
816
782
            old_repository = self.repository
817
783
            if len(old_repository._fallback_repositories) != 1:
818
784
                raise AssertionError("can't cope with fallback repositories "
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()
 
785
                    "of %r" % (self.repository,))
 
786
            # unlock it, including unlocking the fallback
848
787
            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):
 
788
            old_repository.lock_read()
 
789
            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
867
807
                self.repository.lock_write()
868
 
            # Fetch from the old repository into the new.
869
 
            old_repository.lock_read()
870
 
            try:
871
808
                # XXX: If you unstack a branch while it has a working tree
872
809
                # with a pending merge, the pending-merged revisions will no
873
810
                # longer be present.  You can (probably) revert and remerge.
888
825
 
889
826
        :seealso: Branch._get_tags_bytes.
890
827
        """
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)
 
828
        return _run_with_write_locked_target(self, self._transport.put_bytes,
 
829
            'tags', bytes)
897
830
 
898
831
    def _cache_revision_history(self, rev_history):
899
832
        """Set the cached revision history to rev_history.
929
862
        self._merge_sorted_revisions_cache = None
930
863
        self._partial_revision_history_cache = []
931
864
        self._partial_revision_id_to_revno_cache = {}
932
 
        self._tags_bytes = None
933
865
 
934
866
    def _gen_revision_history(self):
935
867
        """Return sequence of revision hashes on to this branch.
1019
951
                raise errors.NoSuchRevision(self, stop_revision)
1020
952
        return other_history[self_len:stop_revision]
1021
953
 
 
954
    @needs_write_lock
1022
955
    def update_revisions(self, other, stop_revision=None, overwrite=False,
1023
956
                         graph=None):
1024
957
        """Pull in new perfect-fit revisions.
1073
1006
            self._extend_partial_history(distance_from_last)
1074
1007
        return self._partial_revision_history_cache[distance_from_last]
1075
1008
 
 
1009
    @needs_write_lock
1076
1010
    def pull(self, source, overwrite=False, stop_revision=None,
1077
1011
             possible_transports=None, *args, **kwargs):
1078
1012
        """Mirror source into this branch.
1274
1208
        return result
1275
1209
 
1276
1210
    @needs_read_lock
1277
 
    def sprout(self, to_bzrdir, revision_id=None, repository_policy=None,
1278
 
            repository=None):
 
1211
    def sprout(self, to_bzrdir, revision_id=None, repository_policy=None):
1279
1212
        """Create a new line of development from the branch, into to_bzrdir.
1280
1213
 
1281
1214
        to_bzrdir controls the branch format.
1286
1219
        if (repository_policy is not None and
1287
1220
            repository_policy.requires_stacking()):
1288
1221
            to_bzrdir._format.require_stacking(_skip_repo=True)
1289
 
        result = to_bzrdir.create_branch(repository=repository)
 
1222
        result = to_bzrdir.create_branch()
1290
1223
        result.lock_write()
1291
1224
        try:
1292
1225
            if repository_policy is not None:
1322
1255
                revno = 1
1323
1256
        destination.set_last_revision_info(revno, revision_id)
1324
1257
 
 
1258
    @needs_read_lock
1325
1259
    def copy_content_into(self, destination, revision_id=None):
1326
1260
        """Copy the content of self into destination.
1327
1261
 
1328
1262
        revision_id: if not None, the revision history in the new branch will
1329
1263
                     be truncated to end with revision_id.
1330
1264
        """
1331
 
        return InterBranch.get(self, destination).copy_content_into(
1332
 
            revision_id=revision_id)
 
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)
1333
1276
 
1334
1277
    def update_references(self, target):
1335
1278
        if not getattr(self._format, 'supports_reference_locations', False):
1380
1323
        """Return the most suitable metadir for a checkout of this branch.
1381
1324
        Weaves are used if this branch's repository uses weaves.
1382
1325
        """
1383
 
        format = self.repository.bzrdir.checkout_metadir()
1384
 
        format.set_branch_format(self._format)
 
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)
1385
1333
        return format
1386
1334
 
1387
1335
    def create_clone_on_transport(self, to_transport, revision_id=None,
1388
 
        stacked_on=None, create_prefix=False, use_existing_dir=False,
1389
 
        no_tree=None):
 
1336
        stacked_on=None, create_prefix=False, use_existing_dir=False):
1390
1337
        """Create a clone of this branch and its bzrdir.
1391
1338
 
1392
1339
        :param to_transport: The transport to clone onto.
1399
1346
        """
1400
1347
        # XXX: Fix the bzrdir API to allow getting the branch back from the
1401
1348
        # 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
1404
1349
        if revision_id is None:
1405
1350
            revision_id = self.last_revision()
1406
1351
        dir_to = self.bzrdir.clone_on_transport(to_transport,
1407
1352
            revision_id=revision_id, stacked_on=stacked_on,
1408
 
            create_prefix=create_prefix, use_existing_dir=use_existing_dir,
1409
 
            no_tree=no_tree)
 
1353
            create_prefix=create_prefix, use_existing_dir=use_existing_dir)
1410
1354
        return dir_to.open_branch()
1411
1355
 
1412
1356
    def create_checkout(self, to_location, revision_id=None,
1537
1481
     * an open routine.
1538
1482
 
1539
1483
    Formats are placed in an dict by their format string for reference
1540
 
    during branch opening. It's not required that these be instances, they
 
1484
    during branch opening. Its not required that these be instances, they
1541
1485
    can be classes themselves with class methods - it simply depends on
1542
1486
    whether state is needed for a given format or not.
1543
1487
 
1566
1510
        try:
1567
1511
            transport = a_bzrdir.get_branch_transport(None, name=name)
1568
1512
            format_string = transport.get_bytes("format")
1569
 
            format = klass._formats[format_string]
1570
 
            if isinstance(format, MetaDirBranchFormatFactory):
1571
 
                return format()
1572
 
            return format
 
1513
            return klass._formats[format_string]
1573
1514
        except errors.NoSuchFile:
1574
1515
            raise errors.NotBranchError(path=transport.base, bzrdir=a_bzrdir)
1575
1516
        except KeyError:
1580
1521
        """Return the current default format."""
1581
1522
        return klass._default_format
1582
1523
 
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):
 
1524
    def get_reference(self, a_bzrdir):
1598
1525
        """Get the target reference of the branch in a_bzrdir.
1599
1526
 
1600
1527
        format probing must have been completed before calling
1602
1529
        in a_bzrdir is correct.
1603
1530
 
1604
1531
        :param a_bzrdir: The bzrdir to get the branch data from.
1605
 
        :param name: Name of the colocated branch to fetch
1606
1532
        :return: None if the branch is not a reference branch.
1607
1533
        """
1608
1534
        return None
1609
1535
 
1610
1536
    @classmethod
1611
 
    def set_reference(self, a_bzrdir, name, to_branch):
 
1537
    def set_reference(self, a_bzrdir, to_branch):
1612
1538
        """Set the target reference of the branch in a_bzrdir.
1613
1539
 
1614
1540
        format probing must have been completed before calling
1616
1542
        in a_bzrdir is correct.
1617
1543
 
1618
1544
        :param a_bzrdir: The bzrdir to set the branch reference for.
1619
 
        :param name: Name of colocated branch to set, None for default
1620
1545
        :param to_branch: branch that the checkout is to reference
1621
1546
        """
1622
1547
        raise NotImplementedError(self.set_reference)
1638
1563
            hook(params)
1639
1564
 
1640
1565
    def _initialize_helper(self, a_bzrdir, utf8_files, name=None,
1641
 
                           repository=None, lock_type='metadir',
1642
 
                           set_format=True):
 
1566
                           lock_type='metadir', set_format=True):
1643
1567
        """Initialize a branch in a bzrdir, with specified files
1644
1568
 
1645
1569
        :param a_bzrdir: The bzrdir to initialize the branch in
1679
1603
        finally:
1680
1604
            if lock_taken:
1681
1605
                control_files.unlock()
1682
 
        branch = self.open(a_bzrdir, name, _found=True,
1683
 
                found_repository=repository)
 
1606
        branch = self.open(a_bzrdir, name, _found=True)
1684
1607
        self._run_post_branch_init_hooks(a_bzrdir, name, branch)
1685
1608
        return branch
1686
1609
 
1687
 
    def initialize(self, a_bzrdir, name=None, repository=None):
 
1610
    def initialize(self, a_bzrdir, name=None):
1688
1611
        """Create a branch of this format in a_bzrdir.
1689
1612
        
1690
1613
        :param name: Name of the colocated branch to create.
1724
1647
        """
1725
1648
        raise NotImplementedError(self.network_name)
1726
1649
 
1727
 
    def open(self, a_bzrdir, name=None, _found=False, ignore_fallbacks=False,
1728
 
            found_repository=None):
 
1650
    def open(self, a_bzrdir, name=None, _found=False, ignore_fallbacks=False):
1729
1651
        """Return the branch object for a_bzrdir
1730
1652
 
1731
1653
        :param a_bzrdir: A BzrDir that contains a branch.
1739
1661
 
1740
1662
    @classmethod
1741
1663
    def register_format(klass, 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
 
        """
 
1664
        """Register a metadir format."""
1747
1665
        klass._formats[format.get_format_string()] = format
1748
1666
        # Metadir formats have a network name of their format string, and get
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__)
 
1667
        # registered as class factories.
 
1668
        network_format_registry.register(format.get_format_string(), format.__class__)
1755
1669
 
1756
1670
    @classmethod
1757
1671
    def set_default_format(klass, format):
1777
1691
        return False  # by default
1778
1692
 
1779
1693
 
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
 
 
1808
1694
class BranchHooks(Hooks):
1809
1695
    """A dictionary mapping hook name to a list of callables for branch hooks.
1810
1696
 
1837
1723
            "with a bzrlib.branch.PullResult object and only runs in the "
1838
1724
            "bzr client.", (0, 15), None))
1839
1725
        self.create_hook(HookPoint('pre_commit',
1840
 
            "Called after a commit is calculated but before it is "
 
1726
            "Called after a commit is calculated but before it is is "
1841
1727
            "completed. pre_commit is called with (local, master, old_revno, "
1842
1728
            "old_revid, future_revno, future_revid, tree_delta, future_tree"
1843
1729
            "). old_revid is NULL_REVISION for the first commit to a branch, "
1880
1766
            "all are called with the url returned from the previous hook."
1881
1767
            "The order is however undefined.", (1, 9), None))
1882
1768
        self.create_hook(HookPoint('automatic_tag_name',
1883
 
            "Called to determine an automatic tag name for a revision. "
 
1769
            "Called to determine an automatic tag name for a revision."
1884
1770
            "automatic_tag_name is called with (branch, revision_id) and "
1885
1771
            "should return a tag name or None if no tag name could be "
1886
1772
            "determined. The first non-None tag name returned will be used.",
1977
1863
        return self.__dict__ == other.__dict__
1978
1864
 
1979
1865
    def __repr__(self):
1980
 
        return "<%s of %s>" % (self.__class__.__name__, self.branch)
 
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)
1981
1872
 
1982
1873
 
1983
1874
class SwitchHookParams(object):
2025
1916
        """See BranchFormat.get_format_description()."""
2026
1917
        return "Branch format 4"
2027
1918
 
2028
 
    def initialize(self, a_bzrdir, name=None, repository=None):
 
1919
    def initialize(self, a_bzrdir, name=None):
2029
1920
        """Create a branch of this format in a_bzrdir."""
2030
 
        if repository is not None:
2031
 
            raise NotImplementedError(
2032
 
                "initialize(repository=<not None>) on %r" % (self,))
2033
1921
        utf8_files = [('revision-history', ''),
2034
1922
                      ('branch-name', ''),
2035
1923
                      ]
2044
1932
        """The network name for this format is the control dirs disk label."""
2045
1933
        return self._matchingbzrdir.get_format_string()
2046
1934
 
2047
 
    def open(self, a_bzrdir, name=None, _found=False, ignore_fallbacks=False,
2048
 
            found_repository=None):
 
1935
    def open(self, a_bzrdir, name=None, _found=False, ignore_fallbacks=False):
2049
1936
        """See BranchFormat.open()."""
2050
1937
        if not _found:
2051
1938
            # we are being called directly and must probe.
2052
1939
            raise NotImplementedError
2053
 
        if found_repository is None:
2054
 
            found_repository = a_bzrdir.open_repository()
2055
 
        return BzrBranchPreSplitOut(_format=self,
 
1940
        return BzrBranch(_format=self,
2056
1941
                         _control_files=a_bzrdir._control_files,
2057
1942
                         a_bzrdir=a_bzrdir,
2058
1943
                         name=name,
2059
 
                         _repository=found_repository)
 
1944
                         _repository=a_bzrdir.open_repository())
2060
1945
 
2061
1946
    def __str__(self):
2062
1947
        return "Bazaar-NG branch format 4"
2076
1961
        """
2077
1962
        return self.get_format_string()
2078
1963
 
2079
 
    def open(self, a_bzrdir, name=None, _found=False, ignore_fallbacks=False,
2080
 
            found_repository=None):
 
1964
    def open(self, a_bzrdir, name=None, _found=False, ignore_fallbacks=False):
2081
1965
        """See BranchFormat.open()."""
2082
1966
        if not _found:
2083
1967
            format = BranchFormat.find_format(a_bzrdir, name=name)
2088
1972
        try:
2089
1973
            control_files = lockable_files.LockableFiles(transport, 'lock',
2090
1974
                                                         lockdir.LockDir)
2091
 
            if found_repository is None:
2092
 
                found_repository = a_bzrdir.find_repository()
2093
1975
            return self._branch_class()(_format=self,
2094
1976
                              _control_files=control_files,
2095
1977
                              name=name,
2096
1978
                              a_bzrdir=a_bzrdir,
2097
 
                              _repository=found_repository,
 
1979
                              _repository=a_bzrdir.find_repository(),
2098
1980
                              ignore_fallbacks=ignore_fallbacks)
2099
1981
        except errors.NoSuchFile:
2100
1982
            raise errors.NotBranchError(path=transport.base, bzrdir=a_bzrdir)
2132
2014
        """See BranchFormat.get_format_description()."""
2133
2015
        return "Branch format 5"
2134
2016
 
2135
 
    def initialize(self, a_bzrdir, name=None, repository=None):
 
2017
    def initialize(self, a_bzrdir, name=None):
2136
2018
        """Create a branch of this format in a_bzrdir."""
2137
2019
        utf8_files = [('revision-history', ''),
2138
2020
                      ('branch-name', ''),
2139
2021
                      ]
2140
 
        return self._initialize_helper(a_bzrdir, utf8_files, name, repository)
 
2022
        return self._initialize_helper(a_bzrdir, utf8_files, name)
2141
2023
 
2142
2024
    def supports_tags(self):
2143
2025
        return False
2165
2047
        """See BranchFormat.get_format_description()."""
2166
2048
        return "Branch format 6"
2167
2049
 
2168
 
    def initialize(self, a_bzrdir, name=None, repository=None):
 
2050
    def initialize(self, a_bzrdir, name=None):
2169
2051
        """Create a branch of this format in a_bzrdir."""
2170
2052
        utf8_files = [('last-revision', '0 null:\n'),
2171
2053
                      ('branch.conf', ''),
2172
2054
                      ('tags', ''),
2173
2055
                      ]
2174
 
        return self._initialize_helper(a_bzrdir, utf8_files, name, repository)
 
2056
        return self._initialize_helper(a_bzrdir, utf8_files, name)
2175
2057
 
2176
2058
    def make_tags(self, branch):
2177
2059
        """See bzrlib.branch.BranchFormat.make_tags()."""
2195
2077
        """See BranchFormat.get_format_description()."""
2196
2078
        return "Branch format 8"
2197
2079
 
2198
 
    def initialize(self, a_bzrdir, name=None, repository=None):
 
2080
    def initialize(self, a_bzrdir, name=None):
2199
2081
        """Create a branch of this format in a_bzrdir."""
2200
2082
        utf8_files = [('last-revision', '0 null:\n'),
2201
2083
                      ('branch.conf', ''),
2202
2084
                      ('tags', ''),
2203
2085
                      ('references', '')
2204
2086
                      ]
2205
 
        return self._initialize_helper(a_bzrdir, utf8_files, name, repository)
 
2087
        return self._initialize_helper(a_bzrdir, utf8_files, name)
2206
2088
 
2207
2089
    def __init__(self):
2208
2090
        super(BzrBranchFormat8, self).__init__()
2231
2113
    This format was introduced in bzr 1.6.
2232
2114
    """
2233
2115
 
2234
 
    def initialize(self, a_bzrdir, name=None, repository=None):
 
2116
    def initialize(self, a_bzrdir, name=None):
2235
2117
        """Create a branch of this format in a_bzrdir."""
2236
2118
        utf8_files = [('last-revision', '0 null:\n'),
2237
2119
                      ('branch.conf', ''),
2238
2120
                      ('tags', ''),
2239
2121
                      ]
2240
 
        return self._initialize_helper(a_bzrdir, utf8_files, name, repository)
 
2122
        return self._initialize_helper(a_bzrdir, utf8_files, name)
2241
2123
 
2242
2124
    def _branch_class(self):
2243
2125
        return BzrBranch7
2275
2157
        """See BranchFormat.get_format_description()."""
2276
2158
        return "Checkout reference format 1"
2277
2159
 
2278
 
    def get_reference(self, a_bzrdir, name=None):
 
2160
    def get_reference(self, a_bzrdir):
2279
2161
        """See BranchFormat.get_reference()."""
2280
 
        transport = a_bzrdir.get_branch_transport(None, name=name)
 
2162
        transport = a_bzrdir.get_branch_transport(None)
2281
2163
        return transport.get_bytes('location')
2282
2164
 
2283
 
    def set_reference(self, a_bzrdir, name, to_branch):
 
2165
    def set_reference(self, a_bzrdir, to_branch):
2284
2166
        """See BranchFormat.set_reference()."""
2285
 
        transport = a_bzrdir.get_branch_transport(None, name=name)
 
2167
        transport = a_bzrdir.get_branch_transport(None)
2286
2168
        location = transport.put_bytes('location', to_branch.base)
2287
2169
 
2288
 
    def initialize(self, a_bzrdir, name=None, target_branch=None,
2289
 
            repository=None):
 
2170
    def initialize(self, a_bzrdir, name=None, target_branch=None):
2290
2171
        """Create a branch of this format in a_bzrdir."""
2291
2172
        if target_branch is None:
2292
2173
            # this format does not implement branch itself, thus the implicit
2320
2201
        return clone
2321
2202
 
2322
2203
    def open(self, a_bzrdir, name=None, _found=False, location=None,
2323
 
             possible_transports=None, ignore_fallbacks=False,
2324
 
             found_repository=None):
 
2204
             possible_transports=None, ignore_fallbacks=False):
2325
2205
        """Return the branch that the branch reference in a_bzrdir points at.
2326
2206
 
2327
2207
        :param a_bzrdir: A BzrDir that contains a branch.
2341
2221
                raise AssertionError("wrong format %r found for %r" %
2342
2222
                    (format, self))
2343
2223
        if location is None:
2344
 
            location = self.get_reference(a_bzrdir, name)
 
2224
            location = self.get_reference(a_bzrdir)
2345
2225
        real_bzrdir = bzrdir.BzrDir.open(
2346
2226
            location, possible_transports=possible_transports)
2347
2227
        result = real_bzrdir.open_branch(name=name, 
2385
2265
    _legacy_formats[0].network_name(), _legacy_formats[0].__class__)
2386
2266
 
2387
2267
 
2388
 
class BranchWriteLockResult(LogicalLockResult):
2389
 
    """The result of write locking a branch.
2390
 
 
2391
 
    :ivar branch_token: The token obtained from the underlying branch lock, or
2392
 
        None.
2393
 
    :ivar unlock: A callable which will unlock the lock.
2394
 
    """
2395
 
 
2396
 
    def __init__(self, unlock, branch_token):
2397
 
        LogicalLockResult.__init__(self, unlock)
2398
 
        self.branch_token = branch_token
2399
 
 
2400
 
    def __repr__(self):
2401
 
        return "BranchWriteLockResult(%s, %s)" % (self.branch_token,
2402
 
            self.unlock)
2403
 
 
2404
 
 
2405
2268
class BzrBranch(Branch, _RelockDebugMixin):
2406
2269
    """A branch stored in the actual filesystem.
2407
2270
 
2461
2324
        return self.control_files.is_locked()
2462
2325
 
2463
2326
    def lock_write(self, token=None):
2464
 
        """Lock the branch for write operations.
2465
 
 
2466
 
        :param token: A token to permit reacquiring a previously held and
2467
 
            preserved lock.
2468
 
        :return: A BranchWriteLockResult.
2469
 
        """
2470
2327
        if not self.is_locked():
2471
2328
            self._note_lock('w')
2472
2329
        # All-in-one needs to always unlock/lock.
2478
2335
        else:
2479
2336
            took_lock = False
2480
2337
        try:
2481
 
            return BranchWriteLockResult(self.unlock,
2482
 
                self.control_files.lock_write(token=token))
 
2338
            return self.control_files.lock_write(token=token)
2483
2339
        except:
2484
2340
            if took_lock:
2485
2341
                self.repository.unlock()
2486
2342
            raise
2487
2343
 
2488
2344
    def lock_read(self):
2489
 
        """Lock the branch for read operations.
2490
 
 
2491
 
        :return: A bzrlib.lock.LogicalLockResult.
2492
 
        """
2493
2345
        if not self.is_locked():
2494
2346
            self._note_lock('r')
2495
2347
        # All-in-one needs to always unlock/lock.
2502
2354
            took_lock = False
2503
2355
        try:
2504
2356
            self.control_files.lock_read()
2505
 
            return LogicalLockResult(self.unlock)
2506
2357
        except:
2507
2358
            if took_lock:
2508
2359
                self.repository.unlock()
2693
2544
                mode=self.bzrdir._get_file_mode())
2694
2545
 
2695
2546
 
2696
 
class BzrBranchPreSplitOut(BzrBranch):
2697
 
 
2698
 
    def _get_checkout_format(self):
2699
 
        """Return the most suitable metadir for a checkout of this branch.
2700
 
        Weaves are used if this branch's repository uses weaves.
2701
 
        """
2702
 
        from bzrlib.repofmt.weaverepo import RepositoryFormat7
2703
 
        from bzrlib.bzrdir import BzrDirMetaFormat1
2704
 
        format = BzrDirMetaFormat1()
2705
 
        format.repository_format = RepositoryFormat7()
2706
 
        return format
2707
 
 
2708
 
 
2709
2547
class BzrBranch5(BzrBranch):
2710
2548
    """A format 5 branch. This supports new features over plain branches.
2711
2549
 
3145
2983
    :ivar tag_conflicts: A list of tag conflicts, see BasicTags.merge_to
3146
2984
    """
3147
2985
 
3148
 
    @deprecated_method(deprecated_in((2, 3, 0)))
3149
2986
    def __int__(self):
3150
 
        """Return the relative change in revno.
3151
 
 
3152
 
        :deprecated: Use `new_revno` and `old_revno` instead.
3153
 
        """
 
2987
        # DEPRECATED: pull used to return the change in revno
3154
2988
        return self.new_revno - self.old_revno
3155
2989
 
3156
2990
    def report(self, to_file):
3181
3015
        target, otherwise it will be None.
3182
3016
    """
3183
3017
 
3184
 
    @deprecated_method(deprecated_in((2, 3, 0)))
3185
3018
    def __int__(self):
3186
 
        """Return the relative change in revno.
3187
 
 
3188
 
        :deprecated: Use `new_revno` and `old_revno` instead.
3189
 
        """
 
3019
        # DEPRECATED: push used to return the change in revno
3190
3020
        return self.new_revno - self.old_revno
3191
3021
 
3192
3022
    def report(self, to_file):
3315
3145
    _optimisers = []
3316
3146
    """The available optimised InterBranch types."""
3317
3147
 
3318
 
    @classmethod
3319
 
    def _get_branch_formats_to_test(klass):
3320
 
        """Return an iterable of format tuples for testing.
3321
 
        
3322
 
        :return: An iterable of (from_format, to_format) to use when testing
3323
 
            this InterBranch class. Each InterBranch class should define this
3324
 
            method itself.
3325
 
        """
3326
 
        raise NotImplementedError(klass._get_branch_formats_to_test)
 
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)
3327
3152
 
3328
 
    @needs_write_lock
3329
3153
    def pull(self, overwrite=False, stop_revision=None,
3330
3154
             possible_transports=None, local=False):
3331
3155
        """Mirror source into target branch.
3336
3160
        """
3337
3161
        raise NotImplementedError(self.pull)
3338
3162
 
3339
 
    @needs_write_lock
3340
3163
    def update_revisions(self, stop_revision=None, overwrite=False,
3341
3164
                         graph=None):
3342
3165
        """Pull in new perfect-fit revisions.
3350
3173
        """
3351
3174
        raise NotImplementedError(self.update_revisions)
3352
3175
 
3353
 
    @needs_write_lock
3354
3176
    def push(self, overwrite=False, stop_revision=None,
3355
3177
             _override_hook_source_branch=None):
3356
3178
        """Mirror the source branch into the target branch.
3359
3181
        """
3360
3182
        raise NotImplementedError(self.push)
3361
3183
 
3362
 
    @needs_write_lock
3363
 
    def copy_content_into(self, revision_id=None):
3364
 
        """Copy the content of source into target
3365
 
 
3366
 
        revision_id: if not None, the revision history in the new branch will
3367
 
                     be truncated to end with revision_id.
3368
 
        """
3369
 
        raise NotImplementedError(self.copy_content_into)
3370
 
 
3371
3184
 
3372
3185
class GenericInterBranch(InterBranch):
3373
 
    """InterBranch implementation that uses public Branch functions."""
3374
 
 
3375
 
    @classmethod
3376
 
    def is_compatible(klass, source, target):
3377
 
        # GenericBranch uses the public API, so always compatible
3378
 
        return True
3379
 
 
3380
 
    @classmethod
3381
 
    def _get_branch_formats_to_test(klass):
3382
 
        return [(BranchFormat._default_format, BranchFormat._default_format)]
3383
 
 
3384
 
    @classmethod
3385
 
    def unwrap_format(klass, format):
3386
 
        if isinstance(format, remote.RemoteBranchFormat):
3387
 
            format._ensure_real()
3388
 
            return format._custom_format
3389
 
        return format
3390
 
 
3391
 
    @needs_write_lock
3392
 
    def copy_content_into(self, revision_id=None):
3393
 
        """Copy the content of source into target
3394
 
 
3395
 
        revision_id: if not None, the revision history in the new branch will
3396
 
                     be truncated to end with revision_id.
3397
 
        """
3398
 
        self.source.update_references(self.target)
3399
 
        self.source._synchronize_history(self.target, revision_id)
3400
 
        try:
3401
 
            parent = self.source.get_parent()
3402
 
        except errors.InaccessibleParent, e:
3403
 
            mutter('parent was not accessible to copy: %s', e)
3404
 
        else:
3405
 
            if parent:
3406
 
                self.target.set_parent(parent)
3407
 
        if self.source._push_should_merge_tags():
3408
 
            self.source.tags.merge_to(self.target.tags)
3409
 
 
3410
 
    @needs_write_lock
 
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
 
3411
3193
    def update_revisions(self, stop_revision=None, overwrite=False,
3412
3194
        graph=None):
3413
3195
        """See InterBranch.update_revisions()."""
3414
 
        other_revno, other_last_revision = self.source.last_revision_info()
3415
 
        stop_revno = None # unknown
3416
 
        if stop_revision is None:
3417
 
            stop_revision = other_last_revision
3418
 
            if _mod_revision.is_null(stop_revision):
3419
 
                # if there are no commits, we're done.
3420
 
                return
3421
 
            stop_revno = other_revno
3422
 
 
3423
 
        # what's the current last revision, before we fetch [and change it
3424
 
        # possibly]
3425
 
        last_rev = _mod_revision.ensure_null(self.target.last_revision())
3426
 
        # we fetch here so that we don't process data twice in the common
3427
 
        # case of having something to pull, and so that the check for
3428
 
        # already merged can operate on the just fetched graph, which will
3429
 
        # be cached in memory.
3430
 
        self.target.fetch(self.source, stop_revision)
3431
 
        # Check to see if one is an ancestor of the other
3432
 
        if not overwrite:
3433
 
            if graph is None:
3434
 
                graph = self.target.repository.get_graph()
3435
 
            if self.target._check_if_descendant_or_diverged(
3436
 
                    stop_revision, last_rev, graph, self.source):
3437
 
                # stop_revision is a descendant of last_rev, but we aren't
3438
 
                # overwriting, so we're done.
3439
 
                return
3440
 
        if stop_revno is None:
3441
 
            if graph is None:
3442
 
                graph = self.target.repository.get_graph()
3443
 
            this_revno, this_last_revision = \
3444
 
                    self.target.last_revision_info()
3445
 
            stop_revno = graph.find_distance_to_null(stop_revision,
3446
 
                            [(other_last_revision, other_revno),
3447
 
                             (this_last_revision, this_revno)])
3448
 
        self.target.set_last_revision_info(stop_revno, stop_revision)
3449
 
 
3450
 
    @needs_write_lock
 
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
 
3451
3236
    def pull(self, overwrite=False, stop_revision=None,
3452
 
             possible_transports=None, run_hooks=True,
 
3237
             possible_transports=None, _hook_master=None, run_hooks=True,
3453
3238
             _override_hook_target=None, local=False):
3454
 
        """Pull from source into self, updating my master if any.
 
3239
        """See Branch.pull.
3455
3240
 
 
3241
        :param _hook_master: Private parameter - set the branch to
 
3242
            be supplied as the master to pull hooks.
3456
3243
        :param run_hooks: Private parameter - if false, this branch
3457
3244
            is being called because it's the master of the primary branch,
3458
3245
            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.
3459
3249
        """
3460
 
        bound_location = self.target.get_bound_location()
3461
 
        if local and not bound_location:
 
3250
        # This type of branch can't be bound.
 
3251
        if local:
3462
3252
            raise errors.LocalRequiresBoundBranch()
3463
 
        master_branch = None
3464
 
        source_is_master = (self.source.user_url == bound_location)
3465
 
        if not local and bound_location and not source_is_master:
3466
 
            # not pulling from master, so we need to update master.
3467
 
            master_branch = self.target.get_master_branch(possible_transports)
3468
 
            master_branch.lock_write()
 
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()
3469
3260
        try:
3470
 
            if master_branch:
3471
 
                # pull from source into master.
3472
 
                master_branch.pull(self.source, overwrite, stop_revision,
3473
 
                    run_hooks=False)
3474
 
            return self._pull(overwrite,
3475
 
                stop_revision, _hook_master=master_branch,
3476
 
                run_hooks=run_hooks,
3477
 
                _override_hook_target=_override_hook_target,
3478
 
                merge_tags_to_master=not source_is_master)
 
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)
3479
3287
        finally:
3480
 
            if master_branch:
3481
 
                master_branch.unlock()
 
3288
            self.source.unlock()
 
3289
        return result
3482
3290
 
3483
3291
    def push(self, overwrite=False, stop_revision=None,
3484
3292
             _override_hook_source_branch=None):
3524
3332
                # push into the master from the source branch.
3525
3333
                self.source._basic_push(master_branch, overwrite, stop_revision)
3526
3334
                # and push into the target branch from the source. Note that we
3527
 
                # push from the source branch again, because it's considered the
 
3335
                # push from the source branch again, because its considered the
3528
3336
                # highest bandwidth repository.
3529
3337
                result = self.source._basic_push(self.target, overwrite,
3530
3338
                    stop_revision)
3546
3354
            _run_hooks()
3547
3355
            return result
3548
3356
 
3549
 
    def _pull(self, overwrite=False, stop_revision=None,
3550
 
             possible_transports=None, _hook_master=None, run_hooks=True,
3551
 
             _override_hook_target=None, local=False,
3552
 
             merge_tags_to_master=True):
3553
 
        """See Branch.pull.
3554
 
 
3555
 
        This function is the core worker, used by GenericInterBranch.pull to
3556
 
        avoid duplication when pulling source->master and source->local.
3557
 
 
3558
 
        :param _hook_master: Private parameter - set the branch to
3559
 
            be supplied as the master to pull hooks.
 
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
 
3560
3374
        :param run_hooks: Private parameter - if false, this branch
3561
3375
            is being called because it's the master of the primary branch,
3562
3376
            so it should not run its hooks.
3563
 
        :param _override_hook_target: Private parameter - set the branch to be
3564
 
            supplied as the target_branch to pull hooks.
3565
 
        :param local: Only update the local branch, and not the bound branch.
3566
3377
        """
3567
 
        # This type of branch can't be bound.
3568
 
        if local:
 
3378
        bound_location = self.target.get_bound_location()
 
3379
        if local and not bound_location:
3569
3380
            raise errors.LocalRequiresBoundBranch()
3570
 
        result = PullResult()
3571
 
        result.source_branch = self.source
3572
 
        if _override_hook_target is None:
3573
 
            result.target_branch = self.target
3574
 
        else:
3575
 
            result.target_branch = _override_hook_target
3576
 
        self.source.lock_read()
 
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()
3577
3386
        try:
3578
 
            # We assume that during 'pull' the target repository is closer than
3579
 
            # the source one.
3580
 
            self.source.update_references(self.target)
3581
 
            graph = self.target.repository.get_graph(self.source.repository)
3582
 
            # TODO: Branch formats should have a flag that indicates 
3583
 
            # that revno's are expensive, and pull() should honor that flag.
3584
 
            # -- JRV20090506
3585
 
            result.old_revno, result.old_revid = \
3586
 
                self.target.last_revision_info()
3587
 
            self.target.update_revisions(self.source, stop_revision,
3588
 
                overwrite=overwrite, graph=graph)
3589
 
            # TODO: The old revid should be specified when merging tags, 
3590
 
            # so a tags implementation that versions tags can only 
3591
 
            # pull in the most recent changes. -- JRV20090506
3592
 
            result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
3593
 
                overwrite, ignore_master=not merge_tags_to_master)
3594
 
            result.new_revno, result.new_revid = self.target.last_revision_info()
3595
 
            if _hook_master:
3596
 
                result.master_branch = _hook_master
3597
 
                result.local_branch = result.target_branch
3598
 
            else:
3599
 
                result.master_branch = result.target_branch
3600
 
                result.local_branch = None
3601
 
            if run_hooks:
3602
 
                for hook in Branch.hooks['post_pull']:
3603
 
                    hook(result)
 
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)
3604
3395
        finally:
3605
 
            self.source.unlock()
3606
 
        return result
 
3396
            if master_branch:
 
3397
                master_branch.unlock()
3607
3398
 
3608
3399
 
3609
3400
InterBranch.register_optimiser(GenericInterBranch)
 
3401
InterBranch.register_optimiser(InterToBranch5)