/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: Vincent Ladeuil
  • Date: 2010-10-15 12:35:00 UTC
  • mto: This revision was merged to the branch mainline in revision 5502.
  • Revision ID: v.ladeuil+lp@free.fr-20101015123500-iyqj7e0r62ie6qfy
Unbreak pqm by commenting out the bogus use of doctest +SKIP not supported by python2.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
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:
197
199
        return self.supports_tags() and self.tags.get_tag_dict()
198
200
 
199
201
    def get_config(self):
 
202
        """Get a bzrlib.config.BranchConfig for this Branch.
 
203
 
 
204
        This can then be used to get and set configuration options for the
 
205
        branch.
 
206
 
 
207
        :return: A bzrlib.config.BranchConfig.
 
208
        """
200
209
        return BranchConfig(self)
201
210
 
202
211
    def _get_config(self):
238
247
        if not local and not config.has_explicit_nickname():
239
248
            try:
240
249
                master = self.get_master_branch(possible_transports)
 
250
                if master and self.user_url == master.user_url:
 
251
                    raise errors.RecursiveBind(self.user_url)
241
252
                if master is not None:
242
253
                    # return the master branch value
243
254
                    return master.nick
 
255
            except errors.RecursiveBind, e:
 
256
                raise e
244
257
            except errors.BzrError, e:
245
258
                # Silently fall back to local implicit nick if the master is
246
259
                # unavailable
283
296
        new_history.reverse()
284
297
        return new_history
285
298
 
286
 
    def lock_write(self):
 
299
    def lock_write(self, token=None):
 
300
        """Lock the branch for write operations.
 
301
 
 
302
        :param token: A token to permit reacquiring a previously held and
 
303
            preserved lock.
 
304
        :return: A BranchWriteLockResult.
 
305
        """
287
306
        raise NotImplementedError(self.lock_write)
288
307
 
289
308
    def lock_read(self):
 
309
        """Lock the branch for read operations.
 
310
 
 
311
        :return: A bzrlib.lock.LogicalLockResult.
 
312
        """
290
313
        raise NotImplementedError(self.lock_read)
291
314
 
292
315
    def unlock(self):
783
806
            if len(old_repository._fallback_repositories) != 1:
784
807
                raise AssertionError("can't cope with fallback repositories "
785
808
                    "of %r" % (self.repository,))
786
 
            # unlock it, including unlocking the fallback
 
809
            # Open the new repository object.
 
810
            # Repositories don't offer an interface to remove fallback
 
811
            # repositories today; take the conceptually simpler option and just
 
812
            # reopen it.  We reopen it starting from the URL so that we
 
813
            # get a separate connection for RemoteRepositories and can
 
814
            # stream from one of them to the other.  This does mean doing
 
815
            # separate SSH connection setup, but unstacking is not a
 
816
            # common operation so it's tolerable.
 
817
            new_bzrdir = bzrdir.BzrDir.open(self.bzrdir.root_transport.base)
 
818
            new_repository = new_bzrdir.find_repository()
 
819
            if new_repository._fallback_repositories:
 
820
                raise AssertionError("didn't expect %r to have "
 
821
                    "fallback_repositories"
 
822
                    % (self.repository,))
 
823
            # Replace self.repository with the new repository.
 
824
            # Do our best to transfer the lock state (i.e. lock-tokens and
 
825
            # lock count) of self.repository to the new repository.
 
826
            lock_token = old_repository.lock_write().repository_token
 
827
            self.repository = new_repository
 
828
            if isinstance(self, remote.RemoteBranch):
 
829
                # Remote branches can have a second reference to the old
 
830
                # repository that need to be replaced.
 
831
                if self._real_branch is not None:
 
832
                    self._real_branch.repository = new_repository
 
833
            self.repository.lock_write(token=lock_token)
 
834
            if lock_token is not None:
 
835
                old_repository.leave_lock_in_place()
787
836
            old_repository.unlock()
 
837
            if lock_token is not None:
 
838
                # XXX: self.repository.leave_lock_in_place() before this
 
839
                # function will not be preserved.  Fortunately that doesn't
 
840
                # affect the current default format (2a), and would be a
 
841
                # corner-case anyway.
 
842
                #  - Andrew Bennetts, 2010/06/30
 
843
                self.repository.dont_leave_lock_in_place()
 
844
            old_lock_count = 0
 
845
            while True:
 
846
                try:
 
847
                    old_repository.unlock()
 
848
                except errors.LockNotHeld:
 
849
                    break
 
850
                old_lock_count += 1
 
851
            if old_lock_count == 0:
 
852
                raise AssertionError(
 
853
                    'old_repository should have been locked at least once.')
 
854
            for i in range(old_lock_count-1):
 
855
                self.repository.lock_write()
 
856
            # Fetch from the old repository into the new.
788
857
            old_repository.lock_read()
789
858
            try:
790
 
                # Repositories don't offer an interface to remove fallback
791
 
                # repositories today; take the conceptually simpler option and just
792
 
                # reopen it.  We reopen it starting from the URL so that we
793
 
                # get a separate connection for RemoteRepositories and can
794
 
                # stream from one of them to the other.  This does mean doing
795
 
                # separate SSH connection setup, but unstacking is not a
796
 
                # common operation so it's tolerable.
797
 
                new_bzrdir = bzrdir.BzrDir.open(self.bzrdir.root_transport.base)
798
 
                new_repository = new_bzrdir.find_repository()
799
 
                self.repository = new_repository
800
 
                if self.repository._fallback_repositories:
801
 
                    raise AssertionError("didn't expect %r to have "
802
 
                        "fallback_repositories"
803
 
                        % (self.repository,))
804
 
                # this is not paired with an unlock because it's just restoring
805
 
                # the previous state; the lock's released when set_stacked_on_url
806
 
                # returns
807
 
                self.repository.lock_write()
808
859
                # XXX: If you unstack a branch while it has a working tree
809
860
                # with a pending merge, the pending-merged revisions will no
810
861
                # longer be present.  You can (probably) revert and remerge.
951
1002
                raise errors.NoSuchRevision(self, stop_revision)
952
1003
        return other_history[self_len:stop_revision]
953
1004
 
954
 
    @needs_write_lock
955
1005
    def update_revisions(self, other, stop_revision=None, overwrite=False,
956
1006
                         graph=None):
957
1007
        """Pull in new perfect-fit revisions.
1006
1056
            self._extend_partial_history(distance_from_last)
1007
1057
        return self._partial_revision_history_cache[distance_from_last]
1008
1058
 
1009
 
    @needs_write_lock
1010
1059
    def pull(self, source, overwrite=False, stop_revision=None,
1011
1060
             possible_transports=None, *args, **kwargs):
1012
1061
        """Mirror source into this branch.
1255
1304
                revno = 1
1256
1305
        destination.set_last_revision_info(revno, revision_id)
1257
1306
 
1258
 
    @needs_read_lock
1259
1307
    def copy_content_into(self, destination, revision_id=None):
1260
1308
        """Copy the content of self into destination.
1261
1309
 
1262
1310
        revision_id: if not None, the revision history in the new branch will
1263
1311
                     be truncated to end with revision_id.
1264
1312
        """
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)
 
1313
        return InterBranch.get(self, destination).copy_content_into(
 
1314
            revision_id=revision_id)
1276
1315
 
1277
1316
    def update_references(self, target):
1278
1317
        if not getattr(self._format, 'supports_reference_locations', False):
1333
1372
        return format
1334
1373
 
1335
1374
    def create_clone_on_transport(self, to_transport, revision_id=None,
1336
 
        stacked_on=None, create_prefix=False, use_existing_dir=False):
 
1375
        stacked_on=None, create_prefix=False, use_existing_dir=False,
 
1376
        no_tree=None):
1337
1377
        """Create a clone of this branch and its bzrdir.
1338
1378
 
1339
1379
        :param to_transport: The transport to clone onto.
1346
1386
        """
1347
1387
        # XXX: Fix the bzrdir API to allow getting the branch back from the
1348
1388
        # clone call. Or something. 20090224 RBC/spiv.
 
1389
        # XXX: Should this perhaps clone colocated branches as well, 
 
1390
        # rather than just the default branch? 20100319 JRV
1349
1391
        if revision_id is None:
1350
1392
            revision_id = self.last_revision()
1351
1393
        dir_to = self.bzrdir.clone_on_transport(to_transport,
1352
1394
            revision_id=revision_id, stacked_on=stacked_on,
1353
 
            create_prefix=create_prefix, use_existing_dir=use_existing_dir)
 
1395
            create_prefix=create_prefix, use_existing_dir=use_existing_dir,
 
1396
            no_tree=no_tree)
1354
1397
        return dir_to.open_branch()
1355
1398
 
1356
1399
    def create_checkout(self, to_location, revision_id=None,
1481
1524
     * an open routine.
1482
1525
 
1483
1526
    Formats are placed in an dict by their format string for reference
1484
 
    during branch opening. Its not required that these be instances, they
 
1527
    during branch opening. It's not required that these be instances, they
1485
1528
    can be classes themselves with class methods - it simply depends on
1486
1529
    whether state is needed for a given format or not.
1487
1530
 
1510
1553
        try:
1511
1554
            transport = a_bzrdir.get_branch_transport(None, name=name)
1512
1555
            format_string = transport.get_bytes("format")
1513
 
            return klass._formats[format_string]
 
1556
            format = klass._formats[format_string]
 
1557
            if isinstance(format, MetaDirBranchFormatFactory):
 
1558
                return format()
 
1559
            return format
1514
1560
        except errors.NoSuchFile:
1515
1561
            raise errors.NotBranchError(path=transport.base, bzrdir=a_bzrdir)
1516
1562
        except KeyError:
1521
1567
        """Return the current default format."""
1522
1568
        return klass._default_format
1523
1569
 
1524
 
    def get_reference(self, a_bzrdir):
 
1570
    @classmethod
 
1571
    def get_formats(klass):
 
1572
        """Get all the known formats.
 
1573
 
 
1574
        Warning: This triggers a load of all lazy registered formats: do not
 
1575
        use except when that is desireed.
 
1576
        """
 
1577
        result = []
 
1578
        for fmt in klass._formats.values():
 
1579
            if isinstance(fmt, MetaDirBranchFormatFactory):
 
1580
                fmt = fmt()
 
1581
            result.append(fmt)
 
1582
        return result
 
1583
 
 
1584
    def get_reference(self, a_bzrdir, name=None):
1525
1585
        """Get the target reference of the branch in a_bzrdir.
1526
1586
 
1527
1587
        format probing must have been completed before calling
1529
1589
        in a_bzrdir is correct.
1530
1590
 
1531
1591
        :param a_bzrdir: The bzrdir to get the branch data from.
 
1592
        :param name: Name of the colocated branch to fetch
1532
1593
        :return: None if the branch is not a reference branch.
1533
1594
        """
1534
1595
        return None
1535
1596
 
1536
1597
    @classmethod
1537
 
    def set_reference(self, a_bzrdir, to_branch):
 
1598
    def set_reference(self, a_bzrdir, name, to_branch):
1538
1599
        """Set the target reference of the branch in a_bzrdir.
1539
1600
 
1540
1601
        format probing must have been completed before calling
1542
1603
        in a_bzrdir is correct.
1543
1604
 
1544
1605
        :param a_bzrdir: The bzrdir to set the branch reference for.
 
1606
        :param name: Name of colocated branch to set, None for default
1545
1607
        :param to_branch: branch that the checkout is to reference
1546
1608
        """
1547
1609
        raise NotImplementedError(self.set_reference)
1661
1723
 
1662
1724
    @classmethod
1663
1725
    def register_format(klass, format):
1664
 
        """Register a metadir format."""
 
1726
        """Register a metadir format.
 
1727
        
 
1728
        See MetaDirBranchFormatFactory for the ability to register a format
 
1729
        without loading the code the format needs until it is actually used.
 
1730
        """
1665
1731
        klass._formats[format.get_format_string()] = format
1666
1732
        # Metadir formats have a network name of their format string, and get
1667
 
        # registered as class factories.
1668
 
        network_format_registry.register(format.get_format_string(), format.__class__)
 
1733
        # registered as factories.
 
1734
        if isinstance(format, MetaDirBranchFormatFactory):
 
1735
            network_format_registry.register(format.get_format_string(), format)
 
1736
        else:
 
1737
            network_format_registry.register(format.get_format_string(),
 
1738
                format.__class__)
1669
1739
 
1670
1740
    @classmethod
1671
1741
    def set_default_format(klass, format):
1691
1761
        return False  # by default
1692
1762
 
1693
1763
 
 
1764
class MetaDirBranchFormatFactory(registry._LazyObjectGetter):
 
1765
    """A factory for a BranchFormat object, permitting simple lazy registration.
 
1766
    
 
1767
    While none of the built in BranchFormats are lazy registered yet,
 
1768
    bzrlib.tests.test_branch.TestMetaDirBranchFormatFactory demonstrates how to
 
1769
    use it, and the bzr-loom plugin uses it as well (see
 
1770
    bzrlib.plugins.loom.formats).
 
1771
    """
 
1772
 
 
1773
    def __init__(self, format_string, module_name, member_name):
 
1774
        """Create a MetaDirBranchFormatFactory.
 
1775
 
 
1776
        :param format_string: The format string the format has.
 
1777
        :param module_name: Module to load the format class from.
 
1778
        :param member_name: Attribute name within the module for the format class.
 
1779
        """
 
1780
        registry._LazyObjectGetter.__init__(self, module_name, member_name)
 
1781
        self._format_string = format_string
 
1782
        
 
1783
    def get_format_string(self):
 
1784
        """See BranchFormat.get_format_string."""
 
1785
        return self._format_string
 
1786
 
 
1787
    def __call__(self):
 
1788
        """Used for network_format_registry support."""
 
1789
        return self.get_obj()()
 
1790
 
 
1791
 
1694
1792
class BranchHooks(Hooks):
1695
1793
    """A dictionary mapping hook name to a list of callables for branch hooks.
1696
1794
 
1723
1821
            "with a bzrlib.branch.PullResult object and only runs in the "
1724
1822
            "bzr client.", (0, 15), None))
1725
1823
        self.create_hook(HookPoint('pre_commit',
1726
 
            "Called after a commit is calculated but before it is is "
 
1824
            "Called after a commit is calculated but before it is "
1727
1825
            "completed. pre_commit is called with (local, master, old_revno, "
1728
1826
            "old_revid, future_revno, future_revid, tree_delta, future_tree"
1729
1827
            "). old_revid is NULL_REVISION for the first commit to a branch, "
1766
1864
            "all are called with the url returned from the previous hook."
1767
1865
            "The order is however undefined.", (1, 9), None))
1768
1866
        self.create_hook(HookPoint('automatic_tag_name',
1769
 
            "Called to determine an automatic tag name for a revision."
 
1867
            "Called to determine an automatic tag name for a revision. "
1770
1868
            "automatic_tag_name is called with (branch, revision_id) and "
1771
1869
            "should return a tag name or None if no tag name could be "
1772
1870
            "determined. The first non-None tag name returned will be used.",
1863
1961
        return self.__dict__ == other.__dict__
1864
1962
 
1865
1963
    def __repr__(self):
1866
 
        if self.branch:
1867
 
            return "<%s of %s>" % (self.__class__.__name__, self.branch)
1868
 
        else:
1869
 
            return "<%s of format:%s bzrdir:%s>" % (
1870
 
                self.__class__.__name__, self.branch,
1871
 
                self.format, self.bzrdir)
 
1964
        return "<%s of %s>" % (self.__class__.__name__, self.branch)
1872
1965
 
1873
1966
 
1874
1967
class SwitchHookParams(object):
2157
2250
        """See BranchFormat.get_format_description()."""
2158
2251
        return "Checkout reference format 1"
2159
2252
 
2160
 
    def get_reference(self, a_bzrdir):
 
2253
    def get_reference(self, a_bzrdir, name=None):
2161
2254
        """See BranchFormat.get_reference()."""
2162
 
        transport = a_bzrdir.get_branch_transport(None)
 
2255
        transport = a_bzrdir.get_branch_transport(None, name=name)
2163
2256
        return transport.get_bytes('location')
2164
2257
 
2165
 
    def set_reference(self, a_bzrdir, to_branch):
 
2258
    def set_reference(self, a_bzrdir, name, to_branch):
2166
2259
        """See BranchFormat.set_reference()."""
2167
 
        transport = a_bzrdir.get_branch_transport(None)
 
2260
        transport = a_bzrdir.get_branch_transport(None, name=name)
2168
2261
        location = transport.put_bytes('location', to_branch.base)
2169
2262
 
2170
2263
    def initialize(self, a_bzrdir, name=None, target_branch=None):
2221
2314
                raise AssertionError("wrong format %r found for %r" %
2222
2315
                    (format, self))
2223
2316
        if location is None:
2224
 
            location = self.get_reference(a_bzrdir)
 
2317
            location = self.get_reference(a_bzrdir, name)
2225
2318
        real_bzrdir = bzrdir.BzrDir.open(
2226
2319
            location, possible_transports=possible_transports)
2227
2320
        result = real_bzrdir.open_branch(name=name, 
2265
2358
    _legacy_formats[0].network_name(), _legacy_formats[0].__class__)
2266
2359
 
2267
2360
 
 
2361
class BranchWriteLockResult(LogicalLockResult):
 
2362
    """The result of write locking a branch.
 
2363
 
 
2364
    :ivar branch_token: The token obtained from the underlying branch lock, or
 
2365
        None.
 
2366
    :ivar unlock: A callable which will unlock the lock.
 
2367
    """
 
2368
 
 
2369
    def __init__(self, unlock, branch_token):
 
2370
        LogicalLockResult.__init__(self, unlock)
 
2371
        self.branch_token = branch_token
 
2372
 
 
2373
    def __repr__(self):
 
2374
        return "BranchWriteLockResult(%s, %s)" % (self.branch_token,
 
2375
            self.unlock)
 
2376
 
 
2377
 
2268
2378
class BzrBranch(Branch, _RelockDebugMixin):
2269
2379
    """A branch stored in the actual filesystem.
2270
2380
 
2324
2434
        return self.control_files.is_locked()
2325
2435
 
2326
2436
    def lock_write(self, token=None):
 
2437
        """Lock the branch for write operations.
 
2438
 
 
2439
        :param token: A token to permit reacquiring a previously held and
 
2440
            preserved lock.
 
2441
        :return: A BranchWriteLockResult.
 
2442
        """
2327
2443
        if not self.is_locked():
2328
2444
            self._note_lock('w')
2329
2445
        # All-in-one needs to always unlock/lock.
2335
2451
        else:
2336
2452
            took_lock = False
2337
2453
        try:
2338
 
            return self.control_files.lock_write(token=token)
 
2454
            return BranchWriteLockResult(self.unlock,
 
2455
                self.control_files.lock_write(token=token))
2339
2456
        except:
2340
2457
            if took_lock:
2341
2458
                self.repository.unlock()
2342
2459
            raise
2343
2460
 
2344
2461
    def lock_read(self):
 
2462
        """Lock the branch for read operations.
 
2463
 
 
2464
        :return: A bzrlib.lock.LogicalLockResult.
 
2465
        """
2345
2466
        if not self.is_locked():
2346
2467
            self._note_lock('r')
2347
2468
        # All-in-one needs to always unlock/lock.
2354
2475
            took_lock = False
2355
2476
        try:
2356
2477
            self.control_files.lock_read()
 
2478
            return LogicalLockResult(self.unlock)
2357
2479
        except:
2358
2480
            if took_lock:
2359
2481
                self.repository.unlock()
2983
3105
    :ivar tag_conflicts: A list of tag conflicts, see BasicTags.merge_to
2984
3106
    """
2985
3107
 
 
3108
    @deprecated_method(deprecated_in((2, 3, 0)))
2986
3109
    def __int__(self):
2987
 
        # DEPRECATED: pull used to return the change in revno
 
3110
        """Return the relative change in revno.
 
3111
 
 
3112
        :deprecated: Use `new_revno` and `old_revno` instead.
 
3113
        """
2988
3114
        return self.new_revno - self.old_revno
2989
3115
 
2990
3116
    def report(self, to_file):
3015
3141
        target, otherwise it will be None.
3016
3142
    """
3017
3143
 
 
3144
    @deprecated_method(deprecated_in((2, 3, 0)))
3018
3145
    def __int__(self):
3019
 
        # DEPRECATED: push used to return the change in revno
 
3146
        """Return the relative change in revno.
 
3147
 
 
3148
        :deprecated: Use `new_revno` and `old_revno` instead.
 
3149
        """
3020
3150
        return self.new_revno - self.old_revno
3021
3151
 
3022
3152
    def report(self, to_file):
3145
3275
    _optimisers = []
3146
3276
    """The available optimised InterBranch types."""
3147
3277
 
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)
 
3278
    @classmethod
 
3279
    def _get_branch_formats_to_test(klass):
 
3280
        """Return an iterable of format tuples for testing.
 
3281
        
 
3282
        :return: An iterable of (from_format, to_format) to use when testing
 
3283
            this InterBranch class. Each InterBranch class should define this
 
3284
            method itself.
 
3285
        """
 
3286
        raise NotImplementedError(klass._get_branch_formats_to_test)
3152
3287
 
 
3288
    @needs_write_lock
3153
3289
    def pull(self, overwrite=False, stop_revision=None,
3154
3290
             possible_transports=None, local=False):
3155
3291
        """Mirror source into target branch.
3160
3296
        """
3161
3297
        raise NotImplementedError(self.pull)
3162
3298
 
 
3299
    @needs_write_lock
3163
3300
    def update_revisions(self, stop_revision=None, overwrite=False,
3164
3301
                         graph=None):
3165
3302
        """Pull in new perfect-fit revisions.
3173
3310
        """
3174
3311
        raise NotImplementedError(self.update_revisions)
3175
3312
 
 
3313
    @needs_write_lock
3176
3314
    def push(self, overwrite=False, stop_revision=None,
3177
3315
             _override_hook_source_branch=None):
3178
3316
        """Mirror the source branch into the target branch.
3181
3319
        """
3182
3320
        raise NotImplementedError(self.push)
3183
3321
 
 
3322
    @needs_write_lock
 
3323
    def copy_content_into(self, revision_id=None):
 
3324
        """Copy the content of source into target
 
3325
 
 
3326
        revision_id: if not None, the revision history in the new branch will
 
3327
                     be truncated to end with revision_id.
 
3328
        """
 
3329
        raise NotImplementedError(self.copy_content_into)
 
3330
 
3184
3331
 
3185
3332
class GenericInterBranch(InterBranch):
3186
 
    """InterBranch implementation that uses public Branch functions.
3187
 
    """
3188
 
 
3189
 
    @staticmethod
3190
 
    def _get_branch_formats_to_test():
3191
 
        return BranchFormat._default_format, BranchFormat._default_format
3192
 
 
 
3333
    """InterBranch implementation that uses public Branch functions."""
 
3334
 
 
3335
    @classmethod
 
3336
    def is_compatible(klass, source, target):
 
3337
        # GenericBranch uses the public API, so always compatible
 
3338
        return True
 
3339
 
 
3340
    @classmethod
 
3341
    def _get_branch_formats_to_test(klass):
 
3342
        return [(BranchFormat._default_format, BranchFormat._default_format)]
 
3343
 
 
3344
    @classmethod
 
3345
    def unwrap_format(klass, format):
 
3346
        if isinstance(format, remote.RemoteBranchFormat):
 
3347
            format._ensure_real()
 
3348
            return format._custom_format
 
3349
        return format                                                                                                  
 
3350
 
 
3351
    @needs_write_lock
 
3352
    def copy_content_into(self, revision_id=None):
 
3353
        """Copy the content of source into target
 
3354
 
 
3355
        revision_id: if not None, the revision history in the new branch will
 
3356
                     be truncated to end with revision_id.
 
3357
        """
 
3358
        self.source.update_references(self.target)
 
3359
        self.source._synchronize_history(self.target, revision_id)
 
3360
        try:
 
3361
            parent = self.source.get_parent()
 
3362
        except errors.InaccessibleParent, e:
 
3363
            mutter('parent was not accessible to copy: %s', e)
 
3364
        else:
 
3365
            if parent:
 
3366
                self.target.set_parent(parent)
 
3367
        if self.source._push_should_merge_tags():
 
3368
            self.source.tags.merge_to(self.target.tags)
 
3369
 
 
3370
    @needs_write_lock
3193
3371
    def update_revisions(self, stop_revision=None, overwrite=False,
3194
3372
        graph=None):
3195
3373
        """See InterBranch.update_revisions()."""
3196
 
        self.source.lock_read()
3197
 
        try:
3198
 
            other_revno, other_last_revision = self.source.last_revision_info()
3199
 
            stop_revno = None # unknown
3200
 
            if stop_revision is None:
3201
 
                stop_revision = other_last_revision
3202
 
                if _mod_revision.is_null(stop_revision):
3203
 
                    # if there are no commits, we're done.
3204
 
                    return
3205
 
                stop_revno = other_revno
3206
 
 
3207
 
            # what's the current last revision, before we fetch [and change it
3208
 
            # possibly]
3209
 
            last_rev = _mod_revision.ensure_null(self.target.last_revision())
3210
 
            # we fetch here so that we don't process data twice in the common
3211
 
            # case of having something to pull, and so that the check for
3212
 
            # already merged can operate on the just fetched graph, which will
3213
 
            # be cached in memory.
3214
 
            self.target.fetch(self.source, stop_revision)
3215
 
            # Check to see if one is an ancestor of the other
3216
 
            if not overwrite:
3217
 
                if graph is None:
3218
 
                    graph = self.target.repository.get_graph()
3219
 
                if self.target._check_if_descendant_or_diverged(
3220
 
                        stop_revision, last_rev, graph, self.source):
3221
 
                    # stop_revision is a descendant of last_rev, but we aren't
3222
 
                    # overwriting, so we're done.
3223
 
                    return
3224
 
            if stop_revno is None:
3225
 
                if graph is None:
3226
 
                    graph = self.target.repository.get_graph()
3227
 
                this_revno, this_last_revision = \
3228
 
                        self.target.last_revision_info()
3229
 
                stop_revno = graph.find_distance_to_null(stop_revision,
3230
 
                                [(other_last_revision, other_revno),
3231
 
                                 (this_last_revision, this_revno)])
3232
 
            self.target.set_last_revision_info(stop_revno, stop_revision)
3233
 
        finally:
3234
 
            self.source.unlock()
3235
 
 
 
3374
        other_revno, other_last_revision = self.source.last_revision_info()
 
3375
        stop_revno = None # unknown
 
3376
        if stop_revision is None:
 
3377
            stop_revision = other_last_revision
 
3378
            if _mod_revision.is_null(stop_revision):
 
3379
                # if there are no commits, we're done.
 
3380
                return
 
3381
            stop_revno = other_revno
 
3382
 
 
3383
        # what's the current last revision, before we fetch [and change it
 
3384
        # possibly]
 
3385
        last_rev = _mod_revision.ensure_null(self.target.last_revision())
 
3386
        # we fetch here so that we don't process data twice in the common
 
3387
        # case of having something to pull, and so that the check for
 
3388
        # already merged can operate on the just fetched graph, which will
 
3389
        # be cached in memory.
 
3390
        self.target.fetch(self.source, stop_revision)
 
3391
        # Check to see if one is an ancestor of the other
 
3392
        if not overwrite:
 
3393
            if graph is None:
 
3394
                graph = self.target.repository.get_graph()
 
3395
            if self.target._check_if_descendant_or_diverged(
 
3396
                    stop_revision, last_rev, graph, self.source):
 
3397
                # stop_revision is a descendant of last_rev, but we aren't
 
3398
                # overwriting, so we're done.
 
3399
                return
 
3400
        if stop_revno is None:
 
3401
            if graph is None:
 
3402
                graph = self.target.repository.get_graph()
 
3403
            this_revno, this_last_revision = \
 
3404
                    self.target.last_revision_info()
 
3405
            stop_revno = graph.find_distance_to_null(stop_revision,
 
3406
                            [(other_last_revision, other_revno),
 
3407
                             (this_last_revision, this_revno)])
 
3408
        self.target.set_last_revision_info(stop_revno, stop_revision)
 
3409
 
 
3410
    @needs_write_lock
3236
3411
    def pull(self, overwrite=False, stop_revision=None,
3237
 
             possible_transports=None, _hook_master=None, run_hooks=True,
 
3412
             possible_transports=None, run_hooks=True,
3238
3413
             _override_hook_target=None, local=False):
3239
 
        """See Branch.pull.
 
3414
        """Pull from source into self, updating my master if any.
3240
3415
 
3241
 
        :param _hook_master: Private parameter - set the branch to
3242
 
            be supplied as the master to pull hooks.
3243
3416
        :param run_hooks: Private parameter - if false, this branch
3244
3417
            is being called because it's the master of the primary branch,
3245
3418
            so it should not run its hooks.
3246
 
        :param _override_hook_target: Private parameter - set the branch to be
3247
 
            supplied as the target_branch to pull hooks.
3248
 
        :param local: Only update the local branch, and not the bound branch.
3249
3419
        """
3250
 
        # This type of branch can't be bound.
3251
 
        if local:
 
3420
        bound_location = self.target.get_bound_location()
 
3421
        if local and not bound_location:
3252
3422
            raise errors.LocalRequiresBoundBranch()
3253
 
        result = PullResult()
3254
 
        result.source_branch = self.source
3255
 
        if _override_hook_target is None:
3256
 
            result.target_branch = self.target
3257
 
        else:
3258
 
            result.target_branch = _override_hook_target
3259
 
        self.source.lock_read()
 
3423
        master_branch = None
 
3424
        if not local and bound_location and self.source.user_url != bound_location:
 
3425
            # not pulling from master, so we need to update master.
 
3426
            master_branch = self.target.get_master_branch(possible_transports)
 
3427
            master_branch.lock_write()
3260
3428
        try:
3261
 
            # We assume that during 'pull' the target repository is closer than
3262
 
            # the source one.
3263
 
            self.source.update_references(self.target)
3264
 
            graph = self.target.repository.get_graph(self.source.repository)
3265
 
            # TODO: Branch formats should have a flag that indicates 
3266
 
            # that revno's are expensive, and pull() should honor that flag.
3267
 
            # -- JRV20090506
3268
 
            result.old_revno, result.old_revid = \
3269
 
                self.target.last_revision_info()
3270
 
            self.target.update_revisions(self.source, stop_revision,
3271
 
                overwrite=overwrite, graph=graph)
3272
 
            # TODO: The old revid should be specified when merging tags, 
3273
 
            # so a tags implementation that versions tags can only 
3274
 
            # pull in the most recent changes. -- JRV20090506
3275
 
            result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
3276
 
                overwrite)
3277
 
            result.new_revno, result.new_revid = self.target.last_revision_info()
3278
 
            if _hook_master:
3279
 
                result.master_branch = _hook_master
3280
 
                result.local_branch = result.target_branch
3281
 
            else:
3282
 
                result.master_branch = result.target_branch
3283
 
                result.local_branch = None
3284
 
            if run_hooks:
3285
 
                for hook in Branch.hooks['post_pull']:
3286
 
                    hook(result)
 
3429
            if master_branch:
 
3430
                # pull from source into master.
 
3431
                master_branch.pull(self.source, overwrite, stop_revision,
 
3432
                    run_hooks=False)
 
3433
            return self._pull(overwrite,
 
3434
                stop_revision, _hook_master=master_branch,
 
3435
                run_hooks=run_hooks,
 
3436
                _override_hook_target=_override_hook_target)
3287
3437
        finally:
3288
 
            self.source.unlock()
3289
 
        return result
 
3438
            if master_branch:
 
3439
                master_branch.unlock()
3290
3440
 
3291
3441
    def push(self, overwrite=False, stop_revision=None,
3292
3442
             _override_hook_source_branch=None):
3332
3482
                # push into the master from the source branch.
3333
3483
                self.source._basic_push(master_branch, overwrite, stop_revision)
3334
3484
                # and push into the target branch from the source. Note that we
3335
 
                # push from the source branch again, because its considered the
 
3485
                # push from the source branch again, because it's considered the
3336
3486
                # highest bandwidth repository.
3337
3487
                result = self.source._basic_push(self.target, overwrite,
3338
3488
                    stop_revision)
3354
3504
            _run_hooks()
3355
3505
            return result
3356
3506
 
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,
 
3507
    def _pull(self, overwrite=False, stop_revision=None,
 
3508
             possible_transports=None, _hook_master=None, run_hooks=True,
3371
3509
             _override_hook_target=None, local=False):
3372
 
        """Pull from source into self, updating my master if any.
3373
 
 
 
3510
        """See Branch.pull.
 
3511
 
 
3512
        This function is the core worker, used by GenericInterBranch.pull to
 
3513
        avoid duplication when pulling source->master and source->local.
 
3514
 
 
3515
        :param _hook_master: Private parameter - set the branch to
 
3516
            be supplied as the master to pull hooks.
3374
3517
        :param run_hooks: Private parameter - if false, this branch
3375
3518
            is being called because it's the master of the primary branch,
3376
3519
            so it should not run its hooks.
 
3520
        :param _override_hook_target: Private parameter - set the branch to be
 
3521
            supplied as the target_branch to pull hooks.
 
3522
        :param local: Only update the local branch, and not the bound branch.
3377
3523
        """
3378
 
        bound_location = self.target.get_bound_location()
3379
 
        if local and not bound_location:
 
3524
        # This type of branch can't be bound.
 
3525
        if local:
3380
3526
            raise errors.LocalRequiresBoundBranch()
3381
 
        master_branch = None
3382
 
        if not local and bound_location and self.source.user_url != bound_location:
3383
 
            # not pulling from master, so we need to update master.
3384
 
            master_branch = self.target.get_master_branch(possible_transports)
3385
 
            master_branch.lock_write()
 
3527
        result = PullResult()
 
3528
        result.source_branch = self.source
 
3529
        if _override_hook_target is None:
 
3530
            result.target_branch = self.target
 
3531
        else:
 
3532
            result.target_branch = _override_hook_target
 
3533
        self.source.lock_read()
3386
3534
        try:
3387
 
            if master_branch:
3388
 
                # pull from source into master.
3389
 
                master_branch.pull(self.source, overwrite, stop_revision,
3390
 
                    run_hooks=False)
3391
 
            return super(InterToBranch5, self).pull(overwrite,
3392
 
                stop_revision, _hook_master=master_branch,
3393
 
                run_hooks=run_hooks,
3394
 
                _override_hook_target=_override_hook_target)
 
3535
            # We assume that during 'pull' the target repository is closer than
 
3536
            # the source one.
 
3537
            self.source.update_references(self.target)
 
3538
            graph = self.target.repository.get_graph(self.source.repository)
 
3539
            # TODO: Branch formats should have a flag that indicates 
 
3540
            # that revno's are expensive, and pull() should honor that flag.
 
3541
            # -- JRV20090506
 
3542
            result.old_revno, result.old_revid = \
 
3543
                self.target.last_revision_info()
 
3544
            self.target.update_revisions(self.source, stop_revision,
 
3545
                overwrite=overwrite, graph=graph)
 
3546
            # TODO: The old revid should be specified when merging tags, 
 
3547
            # so a tags implementation that versions tags can only 
 
3548
            # pull in the most recent changes. -- JRV20090506
 
3549
            result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
 
3550
                overwrite)
 
3551
            result.new_revno, result.new_revid = self.target.last_revision_info()
 
3552
            if _hook_master:
 
3553
                result.master_branch = _hook_master
 
3554
                result.local_branch = result.target_branch
 
3555
            else:
 
3556
                result.master_branch = result.target_branch
 
3557
                result.local_branch = None
 
3558
            if run_hooks:
 
3559
                for hook in Branch.hooks['post_pull']:
 
3560
                    hook(result)
3395
3561
        finally:
3396
 
            if master_branch:
3397
 
                master_branch.unlock()
 
3562
            self.source.unlock()
 
3563
        return result
3398
3564
 
3399
3565
 
3400
3566
InterBranch.register_optimiser(GenericInterBranch)
3401
 
InterBranch.register_optimiser(InterToBranch5)