~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/branch.py

  • Committer: John Arbash Meinel
  • Date: 2011-01-25 22:54:08 UTC
  • mto: This revision was merged to the branch mainline in revision 5636.
  • Revision ID: john@arbash-meinel.com-20110125225408-w5b5mmh117q4jjz1
Implement a reset-to-known-state ability for DirState.

Use this in reset_state(). Allow it to use header information if it can
be parsed, otherwise allow us to pass in the information.

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
295
319
    def lock_read(self):
296
320
        """Lock the branch for read operations.
297
321
 
298
 
        :return: An object with an unlock method which will release the lock
299
 
            obtained.
 
322
        :return: A bzrlib.lock.LogicalLockResult.
300
323
        """
301
324
        raise NotImplementedError(self.lock_read)
302
325
 
793
816
            old_repository = self.repository
794
817
            if len(old_repository._fallback_repositories) != 1:
795
818
                raise AssertionError("can't cope with fallback repositories "
796
 
                    "of %r" % (self.repository,))
797
 
            # 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()
798
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.
799
869
            old_repository.lock_read()
800
870
            try:
801
 
                # Repositories don't offer an interface to remove fallback
802
 
                # repositories today; take the conceptually simpler option and just
803
 
                # reopen it.  We reopen it starting from the URL so that we
804
 
                # get a separate connection for RemoteRepositories and can
805
 
                # stream from one of them to the other.  This does mean doing
806
 
                # separate SSH connection setup, but unstacking is not a
807
 
                # common operation so it's tolerable.
808
 
                new_bzrdir = bzrdir.BzrDir.open(self.bzrdir.root_transport.base)
809
 
                new_repository = new_bzrdir.find_repository()
810
 
                self.repository = new_repository
811
 
                if self.repository._fallback_repositories:
812
 
                    raise AssertionError("didn't expect %r to have "
813
 
                        "fallback_repositories"
814
 
                        % (self.repository,))
815
 
                # this is not paired with an unlock because it's just restoring
816
 
                # the previous state; the lock's released when set_stacked_on_url
817
 
                # returns
818
 
                self.repository.lock_write()
819
871
                # XXX: If you unstack a branch while it has a working tree
820
872
                # with a pending merge, the pending-merged revisions will no
821
873
                # longer be present.  You can (probably) revert and remerge.
836
888
 
837
889
        :seealso: Branch._get_tags_bytes.
838
890
        """
839
 
        return _run_with_write_locked_target(self, self._transport.put_bytes,
840
 
            'tags', bytes)
 
891
        return _run_with_write_locked_target(self, self._set_tags_bytes_locked,
 
892
                bytes)
 
893
 
 
894
    def _set_tags_bytes_locked(self, bytes):
 
895
        self._tags_bytes = bytes
 
896
        return self._transport.put_bytes('tags', bytes)
841
897
 
842
898
    def _cache_revision_history(self, rev_history):
843
899
        """Set the cached revision history to rev_history.
873
929
        self._merge_sorted_revisions_cache = None
874
930
        self._partial_revision_history_cache = []
875
931
        self._partial_revision_id_to_revno_cache = {}
 
932
        self._tags_bytes = None
876
933
 
877
934
    def _gen_revision_history(self):
878
935
        """Return sequence of revision hashes on to this branch.
962
1019
                raise errors.NoSuchRevision(self, stop_revision)
963
1020
        return other_history[self_len:stop_revision]
964
1021
 
965
 
    @needs_write_lock
966
1022
    def update_revisions(self, other, stop_revision=None, overwrite=False,
967
1023
                         graph=None):
968
1024
        """Pull in new perfect-fit revisions.
1017
1073
            self._extend_partial_history(distance_from_last)
1018
1074
        return self._partial_revision_history_cache[distance_from_last]
1019
1075
 
1020
 
    @needs_write_lock
1021
1076
    def pull(self, source, overwrite=False, stop_revision=None,
1022
1077
             possible_transports=None, *args, **kwargs):
1023
1078
        """Mirror source into this branch.
1219
1274
        return result
1220
1275
 
1221
1276
    @needs_read_lock
1222
 
    def sprout(self, to_bzrdir, revision_id=None, repository_policy=None):
 
1277
    def sprout(self, to_bzrdir, revision_id=None, repository_policy=None,
 
1278
            repository=None):
1223
1279
        """Create a new line of development from the branch, into to_bzrdir.
1224
1280
 
1225
1281
        to_bzrdir controls the branch format.
1230
1286
        if (repository_policy is not None and
1231
1287
            repository_policy.requires_stacking()):
1232
1288
            to_bzrdir._format.require_stacking(_skip_repo=True)
1233
 
        result = to_bzrdir.create_branch()
 
1289
        result = to_bzrdir.create_branch(repository=repository)
1234
1290
        result.lock_write()
1235
1291
        try:
1236
1292
            if repository_policy is not None:
1266
1322
                revno = 1
1267
1323
        destination.set_last_revision_info(revno, revision_id)
1268
1324
 
1269
 
    @needs_read_lock
1270
1325
    def copy_content_into(self, destination, revision_id=None):
1271
1326
        """Copy the content of self into destination.
1272
1327
 
1273
1328
        revision_id: if not None, the revision history in the new branch will
1274
1329
                     be truncated to end with revision_id.
1275
1330
        """
1276
 
        self.update_references(destination)
1277
 
        self._synchronize_history(destination, revision_id)
1278
 
        try:
1279
 
            parent = self.get_parent()
1280
 
        except errors.InaccessibleParent, e:
1281
 
            mutter('parent was not accessible to copy: %s', e)
1282
 
        else:
1283
 
            if parent:
1284
 
                destination.set_parent(parent)
1285
 
        if self._push_should_merge_tags():
1286
 
            self.tags.merge_to(destination.tags)
 
1331
        return InterBranch.get(self, destination).copy_content_into(
 
1332
            revision_id=revision_id)
1287
1333
 
1288
1334
    def update_references(self, target):
1289
1335
        if not getattr(self._format, 'supports_reference_locations', False):
1334
1380
        """Return the most suitable metadir for a checkout of this branch.
1335
1381
        Weaves are used if this branch's repository uses weaves.
1336
1382
        """
1337
 
        if isinstance(self.bzrdir, bzrdir.BzrDirPreSplitOut):
1338
 
            from bzrlib.repofmt import weaverepo
1339
 
            format = bzrdir.BzrDirMetaFormat1()
1340
 
            format.repository_format = weaverepo.RepositoryFormat7()
1341
 
        else:
1342
 
            format = self.repository.bzrdir.checkout_metadir()
1343
 
            format.set_branch_format(self._format)
 
1383
        format = self.repository.bzrdir.checkout_metadir()
 
1384
        format.set_branch_format(self._format)
1344
1385
        return format
1345
1386
 
1346
1387
    def create_clone_on_transport(self, to_transport, revision_id=None,
1347
 
        stacked_on=None, create_prefix=False, use_existing_dir=False):
 
1388
        stacked_on=None, create_prefix=False, use_existing_dir=False,
 
1389
        no_tree=None):
1348
1390
        """Create a clone of this branch and its bzrdir.
1349
1391
 
1350
1392
        :param to_transport: The transport to clone onto.
1357
1399
        """
1358
1400
        # XXX: Fix the bzrdir API to allow getting the branch back from the
1359
1401
        # clone call. Or something. 20090224 RBC/spiv.
 
1402
        # XXX: Should this perhaps clone colocated branches as well, 
 
1403
        # rather than just the default branch? 20100319 JRV
1360
1404
        if revision_id is None:
1361
1405
            revision_id = self.last_revision()
1362
1406
        dir_to = self.bzrdir.clone_on_transport(to_transport,
1363
1407
            revision_id=revision_id, stacked_on=stacked_on,
1364
 
            create_prefix=create_prefix, use_existing_dir=use_existing_dir)
 
1408
            create_prefix=create_prefix, use_existing_dir=use_existing_dir,
 
1409
            no_tree=no_tree)
1365
1410
        return dir_to.open_branch()
1366
1411
 
1367
1412
    def create_checkout(self, to_location, revision_id=None,
1492
1537
     * an open routine.
1493
1538
 
1494
1539
    Formats are placed in an dict by their format string for reference
1495
 
    during branch opening. Its not required that these be instances, they
 
1540
    during branch opening. It's not required that these be instances, they
1496
1541
    can be classes themselves with class methods - it simply depends on
1497
1542
    whether state is needed for a given format or not.
1498
1543
 
1521
1566
        try:
1522
1567
            transport = a_bzrdir.get_branch_transport(None, name=name)
1523
1568
            format_string = transport.get_bytes("format")
1524
 
            return klass._formats[format_string]
 
1569
            format = klass._formats[format_string]
 
1570
            if isinstance(format, MetaDirBranchFormatFactory):
 
1571
                return format()
 
1572
            return format
1525
1573
        except errors.NoSuchFile:
1526
1574
            raise errors.NotBranchError(path=transport.base, bzrdir=a_bzrdir)
1527
1575
        except KeyError:
1532
1580
        """Return the current default format."""
1533
1581
        return klass._default_format
1534
1582
 
1535
 
    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):
1536
1598
        """Get the target reference of the branch in a_bzrdir.
1537
1599
 
1538
1600
        format probing must have been completed before calling
1540
1602
        in a_bzrdir is correct.
1541
1603
 
1542
1604
        :param a_bzrdir: The bzrdir to get the branch data from.
 
1605
        :param name: Name of the colocated branch to fetch
1543
1606
        :return: None if the branch is not a reference branch.
1544
1607
        """
1545
1608
        return None
1546
1609
 
1547
1610
    @classmethod
1548
 
    def set_reference(self, a_bzrdir, to_branch):
 
1611
    def set_reference(self, a_bzrdir, name, to_branch):
1549
1612
        """Set the target reference of the branch in a_bzrdir.
1550
1613
 
1551
1614
        format probing must have been completed before calling
1553
1616
        in a_bzrdir is correct.
1554
1617
 
1555
1618
        :param a_bzrdir: The bzrdir to set the branch reference for.
 
1619
        :param name: Name of colocated branch to set, None for default
1556
1620
        :param to_branch: branch that the checkout is to reference
1557
1621
        """
1558
1622
        raise NotImplementedError(self.set_reference)
1574
1638
            hook(params)
1575
1639
 
1576
1640
    def _initialize_helper(self, a_bzrdir, utf8_files, name=None,
1577
 
                           lock_type='metadir', set_format=True):
 
1641
                           repository=None, lock_type='metadir',
 
1642
                           set_format=True):
1578
1643
        """Initialize a branch in a bzrdir, with specified files
1579
1644
 
1580
1645
        :param a_bzrdir: The bzrdir to initialize the branch in
1614
1679
        finally:
1615
1680
            if lock_taken:
1616
1681
                control_files.unlock()
1617
 
        branch = self.open(a_bzrdir, name, _found=True)
 
1682
        branch = self.open(a_bzrdir, name, _found=True,
 
1683
                found_repository=repository)
1618
1684
        self._run_post_branch_init_hooks(a_bzrdir, name, branch)
1619
1685
        return branch
1620
1686
 
1621
 
    def initialize(self, a_bzrdir, name=None):
 
1687
    def initialize(self, a_bzrdir, name=None, repository=None):
1622
1688
        """Create a branch of this format in a_bzrdir.
1623
1689
        
1624
1690
        :param name: Name of the colocated branch to create.
1658
1724
        """
1659
1725
        raise NotImplementedError(self.network_name)
1660
1726
 
1661
 
    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):
1662
1729
        """Return the branch object for a_bzrdir
1663
1730
 
1664
1731
        :param a_bzrdir: A BzrDir that contains a branch.
1672
1739
 
1673
1740
    @classmethod
1674
1741
    def register_format(klass, format):
1675
 
        """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
        """
1676
1747
        klass._formats[format.get_format_string()] = format
1677
1748
        # Metadir formats have a network name of their format string, and get
1678
 
        # registered as class factories.
1679
 
        network_format_registry.register(format.get_format_string(), format.__class__)
 
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__)
1680
1755
 
1681
1756
    @classmethod
1682
1757
    def set_default_format(klass, format):
1702
1777
        return False  # by default
1703
1778
 
1704
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
 
1705
1808
class BranchHooks(Hooks):
1706
1809
    """A dictionary mapping hook name to a list of callables for branch hooks.
1707
1810
 
1734
1837
            "with a bzrlib.branch.PullResult object and only runs in the "
1735
1838
            "bzr client.", (0, 15), None))
1736
1839
        self.create_hook(HookPoint('pre_commit',
1737
 
            "Called after a commit is calculated but before it is is "
 
1840
            "Called after a commit is calculated but before it is "
1738
1841
            "completed. pre_commit is called with (local, master, old_revno, "
1739
1842
            "old_revid, future_revno, future_revid, tree_delta, future_tree"
1740
1843
            "). old_revid is NULL_REVISION for the first commit to a branch, "
1777
1880
            "all are called with the url returned from the previous hook."
1778
1881
            "The order is however undefined.", (1, 9), None))
1779
1882
        self.create_hook(HookPoint('automatic_tag_name',
1780
 
            "Called to determine an automatic tag name for a revision."
 
1883
            "Called to determine an automatic tag name for a revision. "
1781
1884
            "automatic_tag_name is called with (branch, revision_id) and "
1782
1885
            "should return a tag name or None if no tag name could be "
1783
1886
            "determined. The first non-None tag name returned will be used.",
1874
1977
        return self.__dict__ == other.__dict__
1875
1978
 
1876
1979
    def __repr__(self):
1877
 
        if self.branch:
1878
 
            return "<%s of %s>" % (self.__class__.__name__, self.branch)
1879
 
        else:
1880
 
            return "<%s of format:%s bzrdir:%s>" % (
1881
 
                self.__class__.__name__, self.branch,
1882
 
                self.format, self.bzrdir)
 
1980
        return "<%s of %s>" % (self.__class__.__name__, self.branch)
1883
1981
 
1884
1982
 
1885
1983
class SwitchHookParams(object):
1927
2025
        """See BranchFormat.get_format_description()."""
1928
2026
        return "Branch format 4"
1929
2027
 
1930
 
    def initialize(self, a_bzrdir, name=None):
 
2028
    def initialize(self, a_bzrdir, name=None, repository=None):
1931
2029
        """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,))
1932
2033
        utf8_files = [('revision-history', ''),
1933
2034
                      ('branch-name', ''),
1934
2035
                      ]
1943
2044
        """The network name for this format is the control dirs disk label."""
1944
2045
        return self._matchingbzrdir.get_format_string()
1945
2046
 
1946
 
    def open(self, a_bzrdir, name=None, _found=False, ignore_fallbacks=False):
 
2047
    def open(self, a_bzrdir, name=None, _found=False, ignore_fallbacks=False,
 
2048
            found_repository=None):
1947
2049
        """See BranchFormat.open()."""
1948
2050
        if not _found:
1949
2051
            # we are being called directly and must probe.
1950
2052
            raise NotImplementedError
1951
 
        return BzrBranch(_format=self,
 
2053
        if found_repository is None:
 
2054
            found_repository = a_bzrdir.open_repository()
 
2055
        return BzrBranchPreSplitOut(_format=self,
1952
2056
                         _control_files=a_bzrdir._control_files,
1953
2057
                         a_bzrdir=a_bzrdir,
1954
2058
                         name=name,
1955
 
                         _repository=a_bzrdir.open_repository())
 
2059
                         _repository=found_repository)
1956
2060
 
1957
2061
    def __str__(self):
1958
2062
        return "Bazaar-NG branch format 4"
1972
2076
        """
1973
2077
        return self.get_format_string()
1974
2078
 
1975
 
    def open(self, a_bzrdir, name=None, _found=False, ignore_fallbacks=False):
 
2079
    def open(self, a_bzrdir, name=None, _found=False, ignore_fallbacks=False,
 
2080
            found_repository=None):
1976
2081
        """See BranchFormat.open()."""
1977
2082
        if not _found:
1978
2083
            format = BranchFormat.find_format(a_bzrdir, name=name)
1983
2088
        try:
1984
2089
            control_files = lockable_files.LockableFiles(transport, 'lock',
1985
2090
                                                         lockdir.LockDir)
 
2091
            if found_repository is None:
 
2092
                found_repository = a_bzrdir.find_repository()
1986
2093
            return self._branch_class()(_format=self,
1987
2094
                              _control_files=control_files,
1988
2095
                              name=name,
1989
2096
                              a_bzrdir=a_bzrdir,
1990
 
                              _repository=a_bzrdir.find_repository(),
 
2097
                              _repository=found_repository,
1991
2098
                              ignore_fallbacks=ignore_fallbacks)
1992
2099
        except errors.NoSuchFile:
1993
2100
            raise errors.NotBranchError(path=transport.base, bzrdir=a_bzrdir)
2025
2132
        """See BranchFormat.get_format_description()."""
2026
2133
        return "Branch format 5"
2027
2134
 
2028
 
    def initialize(self, a_bzrdir, name=None):
 
2135
    def initialize(self, a_bzrdir, name=None, repository=None):
2029
2136
        """Create a branch of this format in a_bzrdir."""
2030
2137
        utf8_files = [('revision-history', ''),
2031
2138
                      ('branch-name', ''),
2032
2139
                      ]
2033
 
        return self._initialize_helper(a_bzrdir, utf8_files, name)
 
2140
        return self._initialize_helper(a_bzrdir, utf8_files, name, repository)
2034
2141
 
2035
2142
    def supports_tags(self):
2036
2143
        return False
2058
2165
        """See BranchFormat.get_format_description()."""
2059
2166
        return "Branch format 6"
2060
2167
 
2061
 
    def initialize(self, a_bzrdir, name=None):
 
2168
    def initialize(self, a_bzrdir, name=None, repository=None):
2062
2169
        """Create a branch of this format in a_bzrdir."""
2063
2170
        utf8_files = [('last-revision', '0 null:\n'),
2064
2171
                      ('branch.conf', ''),
2065
2172
                      ('tags', ''),
2066
2173
                      ]
2067
 
        return self._initialize_helper(a_bzrdir, utf8_files, name)
 
2174
        return self._initialize_helper(a_bzrdir, utf8_files, name, repository)
2068
2175
 
2069
2176
    def make_tags(self, branch):
2070
2177
        """See bzrlib.branch.BranchFormat.make_tags()."""
2088
2195
        """See BranchFormat.get_format_description()."""
2089
2196
        return "Branch format 8"
2090
2197
 
2091
 
    def initialize(self, a_bzrdir, name=None):
 
2198
    def initialize(self, a_bzrdir, name=None, repository=None):
2092
2199
        """Create a branch of this format in a_bzrdir."""
2093
2200
        utf8_files = [('last-revision', '0 null:\n'),
2094
2201
                      ('branch.conf', ''),
2095
2202
                      ('tags', ''),
2096
2203
                      ('references', '')
2097
2204
                      ]
2098
 
        return self._initialize_helper(a_bzrdir, utf8_files, name)
 
2205
        return self._initialize_helper(a_bzrdir, utf8_files, name, repository)
2099
2206
 
2100
2207
    def __init__(self):
2101
2208
        super(BzrBranchFormat8, self).__init__()
2124
2231
    This format was introduced in bzr 1.6.
2125
2232
    """
2126
2233
 
2127
 
    def initialize(self, a_bzrdir, name=None):
 
2234
    def initialize(self, a_bzrdir, name=None, repository=None):
2128
2235
        """Create a branch of this format in a_bzrdir."""
2129
2236
        utf8_files = [('last-revision', '0 null:\n'),
2130
2237
                      ('branch.conf', ''),
2131
2238
                      ('tags', ''),
2132
2239
                      ]
2133
 
        return self._initialize_helper(a_bzrdir, utf8_files, name)
 
2240
        return self._initialize_helper(a_bzrdir, utf8_files, name, repository)
2134
2241
 
2135
2242
    def _branch_class(self):
2136
2243
        return BzrBranch7
2168
2275
        """See BranchFormat.get_format_description()."""
2169
2276
        return "Checkout reference format 1"
2170
2277
 
2171
 
    def get_reference(self, a_bzrdir):
 
2278
    def get_reference(self, a_bzrdir, name=None):
2172
2279
        """See BranchFormat.get_reference()."""
2173
 
        transport = a_bzrdir.get_branch_transport(None)
 
2280
        transport = a_bzrdir.get_branch_transport(None, name=name)
2174
2281
        return transport.get_bytes('location')
2175
2282
 
2176
 
    def set_reference(self, a_bzrdir, to_branch):
 
2283
    def set_reference(self, a_bzrdir, name, to_branch):
2177
2284
        """See BranchFormat.set_reference()."""
2178
 
        transport = a_bzrdir.get_branch_transport(None)
 
2285
        transport = a_bzrdir.get_branch_transport(None, name=name)
2179
2286
        location = transport.put_bytes('location', to_branch.base)
2180
2287
 
2181
 
    def initialize(self, a_bzrdir, name=None, target_branch=None):
 
2288
    def initialize(self, a_bzrdir, name=None, target_branch=None,
 
2289
            repository=None):
2182
2290
        """Create a branch of this format in a_bzrdir."""
2183
2291
        if target_branch is None:
2184
2292
            # this format does not implement branch itself, thus the implicit
2212
2320
        return clone
2213
2321
 
2214
2322
    def open(self, a_bzrdir, name=None, _found=False, location=None,
2215
 
             possible_transports=None, ignore_fallbacks=False):
 
2323
             possible_transports=None, ignore_fallbacks=False,
 
2324
             found_repository=None):
2216
2325
        """Return the branch that the branch reference in a_bzrdir points at.
2217
2326
 
2218
2327
        :param a_bzrdir: A BzrDir that contains a branch.
2232
2341
                raise AssertionError("wrong format %r found for %r" %
2233
2342
                    (format, self))
2234
2343
        if location is None:
2235
 
            location = self.get_reference(a_bzrdir)
 
2344
            location = self.get_reference(a_bzrdir, name)
2236
2345
        real_bzrdir = bzrdir.BzrDir.open(
2237
2346
            location, possible_transports=possible_transports)
2238
2347
        result = real_bzrdir.open_branch(name=name, 
2276
2385
    _legacy_formats[0].network_name(), _legacy_formats[0].__class__)
2277
2386
 
2278
2387
 
2279
 
class BranchWriteLockResult(object):
 
2388
class BranchWriteLockResult(LogicalLockResult):
2280
2389
    """The result of write locking a branch.
2281
2390
 
2282
2391
    :ivar branch_token: The token obtained from the underlying branch lock, or
2285
2394
    """
2286
2395
 
2287
2396
    def __init__(self, unlock, branch_token):
 
2397
        LogicalLockResult.__init__(self, unlock)
2288
2398
        self.branch_token = branch_token
2289
 
        self.unlock = unlock
2290
2399
 
2291
 
    def __str__(self):
 
2400
    def __repr__(self):
2292
2401
        return "BranchWriteLockResult(%s, %s)" % (self.branch_token,
2293
2402
            self.unlock)
2294
2403
 
2379
2488
    def lock_read(self):
2380
2489
        """Lock the branch for read operations.
2381
2490
 
2382
 
        :return: An object with an unlock method which will release the lock
2383
 
            obtained.
 
2491
        :return: A bzrlib.lock.LogicalLockResult.
2384
2492
        """
2385
2493
        if not self.is_locked():
2386
2494
            self._note_lock('r')
2394
2502
            took_lock = False
2395
2503
        try:
2396
2504
            self.control_files.lock_read()
2397
 
            return self
 
2505
            return LogicalLockResult(self.unlock)
2398
2506
        except:
2399
2507
            if took_lock:
2400
2508
                self.repository.unlock()
2556
2664
        result.target_branch = target
2557
2665
        result.old_revno, result.old_revid = target.last_revision_info()
2558
2666
        self.update_references(target)
2559
 
        if result.old_revid != self.last_revision():
 
2667
        if result.old_revid != stop_revision:
2560
2668
            # We assume that during 'push' this repository is closer than
2561
2669
            # the target.
2562
2670
            graph = self.repository.get_graph(target.repository)
2585
2693
                mode=self.bzrdir._get_file_mode())
2586
2694
 
2587
2695
 
 
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
 
2588
2709
class BzrBranch5(BzrBranch):
2589
2710
    """A format 5 branch. This supports new features over plain branches.
2590
2711
 
3024
3145
    :ivar tag_conflicts: A list of tag conflicts, see BasicTags.merge_to
3025
3146
    """
3026
3147
 
 
3148
    @deprecated_method(deprecated_in((2, 3, 0)))
3027
3149
    def __int__(self):
3028
 
        # DEPRECATED: pull used to return the change in revno
 
3150
        """Return the relative change in revno.
 
3151
 
 
3152
        :deprecated: Use `new_revno` and `old_revno` instead.
 
3153
        """
3029
3154
        return self.new_revno - self.old_revno
3030
3155
 
3031
3156
    def report(self, to_file):
3056
3181
        target, otherwise it will be None.
3057
3182
    """
3058
3183
 
 
3184
    @deprecated_method(deprecated_in((2, 3, 0)))
3059
3185
    def __int__(self):
3060
 
        # DEPRECATED: push used to return the change in revno
 
3186
        """Return the relative change in revno.
 
3187
 
 
3188
        :deprecated: Use `new_revno` and `old_revno` instead.
 
3189
        """
3061
3190
        return self.new_revno - self.old_revno
3062
3191
 
3063
3192
    def report(self, to_file):
3186
3315
    _optimisers = []
3187
3316
    """The available optimised InterBranch types."""
3188
3317
 
3189
 
    @staticmethod
3190
 
    def _get_branch_formats_to_test():
3191
 
        """Return a tuple with the Branch formats to use when testing."""
3192
 
        raise NotImplementedError(InterBranch._get_branch_formats_to_test)
 
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)
3193
3327
 
 
3328
    @needs_write_lock
3194
3329
    def pull(self, overwrite=False, stop_revision=None,
3195
3330
             possible_transports=None, local=False):
3196
3331
        """Mirror source into target branch.
3201
3336
        """
3202
3337
        raise NotImplementedError(self.pull)
3203
3338
 
 
3339
    @needs_write_lock
3204
3340
    def update_revisions(self, stop_revision=None, overwrite=False,
3205
3341
                         graph=None):
3206
3342
        """Pull in new perfect-fit revisions.
3214
3350
        """
3215
3351
        raise NotImplementedError(self.update_revisions)
3216
3352
 
 
3353
    @needs_write_lock
3217
3354
    def push(self, overwrite=False, stop_revision=None,
3218
3355
             _override_hook_source_branch=None):
3219
3356
        """Mirror the source branch into the target branch.
3222
3359
        """
3223
3360
        raise NotImplementedError(self.push)
3224
3361
 
 
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
 
3225
3371
 
3226
3372
class GenericInterBranch(InterBranch):
3227
 
    """InterBranch implementation that uses public Branch functions.
3228
 
    """
3229
 
 
3230
 
    @staticmethod
3231
 
    def _get_branch_formats_to_test():
3232
 
        return BranchFormat._default_format, BranchFormat._default_format
3233
 
 
 
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
3234
3411
    def update_revisions(self, stop_revision=None, overwrite=False,
3235
3412
        graph=None):
3236
3413
        """See InterBranch.update_revisions()."""
3237
 
        self.source.lock_read()
3238
 
        try:
3239
 
            other_revno, other_last_revision = self.source.last_revision_info()
3240
 
            stop_revno = None # unknown
3241
 
            if stop_revision is None:
3242
 
                stop_revision = other_last_revision
3243
 
                if _mod_revision.is_null(stop_revision):
3244
 
                    # if there are no commits, we're done.
3245
 
                    return
3246
 
                stop_revno = other_revno
3247
 
 
3248
 
            # what's the current last revision, before we fetch [and change it
3249
 
            # possibly]
3250
 
            last_rev = _mod_revision.ensure_null(self.target.last_revision())
3251
 
            # we fetch here so that we don't process data twice in the common
3252
 
            # case of having something to pull, and so that the check for
3253
 
            # already merged can operate on the just fetched graph, which will
3254
 
            # be cached in memory.
3255
 
            self.target.fetch(self.source, stop_revision)
3256
 
            # Check to see if one is an ancestor of the other
3257
 
            if not overwrite:
3258
 
                if graph is None:
3259
 
                    graph = self.target.repository.get_graph()
3260
 
                if self.target._check_if_descendant_or_diverged(
3261
 
                        stop_revision, last_rev, graph, self.source):
3262
 
                    # stop_revision is a descendant of last_rev, but we aren't
3263
 
                    # overwriting, so we're done.
3264
 
                    return
3265
 
            if stop_revno is None:
3266
 
                if graph is None:
3267
 
                    graph = self.target.repository.get_graph()
3268
 
                this_revno, this_last_revision = \
3269
 
                        self.target.last_revision_info()
3270
 
                stop_revno = graph.find_distance_to_null(stop_revision,
3271
 
                                [(other_last_revision, other_revno),
3272
 
                                 (this_last_revision, this_revno)])
3273
 
            self.target.set_last_revision_info(stop_revno, stop_revision)
3274
 
        finally:
3275
 
            self.source.unlock()
3276
 
 
 
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
3277
3451
    def pull(self, overwrite=False, stop_revision=None,
3278
 
             possible_transports=None, _hook_master=None, run_hooks=True,
 
3452
             possible_transports=None, run_hooks=True,
3279
3453
             _override_hook_target=None, local=False):
3280
 
        """See Branch.pull.
 
3454
        """Pull from source into self, updating my master if any.
3281
3455
 
3282
 
        :param _hook_master: Private parameter - set the branch to
3283
 
            be supplied as the master to pull hooks.
3284
3456
        :param run_hooks: Private parameter - if false, this branch
3285
3457
            is being called because it's the master of the primary branch,
3286
3458
            so it should not run its hooks.
3287
 
        :param _override_hook_target: Private parameter - set the branch to be
3288
 
            supplied as the target_branch to pull hooks.
3289
 
        :param local: Only update the local branch, and not the bound branch.
3290
3459
        """
3291
 
        # This type of branch can't be bound.
3292
 
        if local:
 
3460
        bound_location = self.target.get_bound_location()
 
3461
        if local and not bound_location:
3293
3462
            raise errors.LocalRequiresBoundBranch()
3294
 
        result = PullResult()
3295
 
        result.source_branch = self.source
3296
 
        if _override_hook_target is None:
3297
 
            result.target_branch = self.target
3298
 
        else:
3299
 
            result.target_branch = _override_hook_target
3300
 
        self.source.lock_read()
 
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()
3301
3469
        try:
3302
 
            # We assume that during 'pull' the target repository is closer than
3303
 
            # the source one.
3304
 
            self.source.update_references(self.target)
3305
 
            graph = self.target.repository.get_graph(self.source.repository)
3306
 
            # TODO: Branch formats should have a flag that indicates 
3307
 
            # that revno's are expensive, and pull() should honor that flag.
3308
 
            # -- JRV20090506
3309
 
            result.old_revno, result.old_revid = \
3310
 
                self.target.last_revision_info()
3311
 
            self.target.update_revisions(self.source, stop_revision,
3312
 
                overwrite=overwrite, graph=graph)
3313
 
            # TODO: The old revid should be specified when merging tags, 
3314
 
            # so a tags implementation that versions tags can only 
3315
 
            # pull in the most recent changes. -- JRV20090506
3316
 
            result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
3317
 
                overwrite)
3318
 
            result.new_revno, result.new_revid = self.target.last_revision_info()
3319
 
            if _hook_master:
3320
 
                result.master_branch = _hook_master
3321
 
                result.local_branch = result.target_branch
3322
 
            else:
3323
 
                result.master_branch = result.target_branch
3324
 
                result.local_branch = None
3325
 
            if run_hooks:
3326
 
                for hook in Branch.hooks['post_pull']:
3327
 
                    hook(result)
 
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)
3328
3479
        finally:
3329
 
            self.source.unlock()
3330
 
        return result
 
3480
            if master_branch:
 
3481
                master_branch.unlock()
3331
3482
 
3332
3483
    def push(self, overwrite=False, stop_revision=None,
3333
3484
             _override_hook_source_branch=None):
3373
3524
                # push into the master from the source branch.
3374
3525
                self.source._basic_push(master_branch, overwrite, stop_revision)
3375
3526
                # and push into the target branch from the source. Note that we
3376
 
                # push from the source branch again, because its considered the
 
3527
                # push from the source branch again, because it's considered the
3377
3528
                # highest bandwidth repository.
3378
3529
                result = self.source._basic_push(self.target, overwrite,
3379
3530
                    stop_revision)
3395
3546
            _run_hooks()
3396
3547
            return result
3397
3548
 
3398
 
    @classmethod
3399
 
    def is_compatible(self, source, target):
3400
 
        # GenericBranch uses the public API, so always compatible
3401
 
        return True
3402
 
 
3403
 
 
3404
 
class InterToBranch5(GenericInterBranch):
3405
 
 
3406
 
    @staticmethod
3407
 
    def _get_branch_formats_to_test():
3408
 
        return BranchFormat._default_format, BzrBranchFormat5()
3409
 
 
3410
 
    def pull(self, overwrite=False, stop_revision=None,
3411
 
             possible_transports=None, run_hooks=True,
3412
 
             _override_hook_target=None, local=False):
3413
 
        """Pull from source into self, updating my master if any.
3414
 
 
 
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.
3415
3560
        :param run_hooks: Private parameter - if false, this branch
3416
3561
            is being called because it's the master of the primary branch,
3417
3562
            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.
3418
3566
        """
3419
 
        bound_location = self.target.get_bound_location()
3420
 
        if local and not bound_location:
 
3567
        # This type of branch can't be bound.
 
3568
        if local:
3421
3569
            raise errors.LocalRequiresBoundBranch()
3422
 
        master_branch = None
3423
 
        if not local and bound_location and self.source.user_url != bound_location:
3424
 
            # not pulling from master, so we need to update master.
3425
 
            master_branch = self.target.get_master_branch(possible_transports)
3426
 
            master_branch.lock_write()
 
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()
3427
3577
        try:
3428
 
            if master_branch:
3429
 
                # pull from source into master.
3430
 
                master_branch.pull(self.source, overwrite, stop_revision,
3431
 
                    run_hooks=False)
3432
 
            return super(InterToBranch5, self).pull(overwrite,
3433
 
                stop_revision, _hook_master=master_branch,
3434
 
                run_hooks=run_hooks,
3435
 
                _override_hook_target=_override_hook_target)
 
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)
3436
3604
        finally:
3437
 
            if master_branch:
3438
 
                master_branch.unlock()
 
3605
            self.source.unlock()
 
3606
        return result
3439
3607
 
3440
3608
 
3441
3609
InterBranch.register_optimiser(GenericInterBranch)
3442
 
InterBranch.register_optimiser(InterToBranch5)