~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/branch.py

  • Committer: Jared Bunting
  • Date: 2010-10-21 22:27:43 UTC
  • mto: This revision was merged to the branch mainline in revision 5514.
  • Revision ID: jared.bunting@peachjean.com-20101021222743-tn9n0cgzg3z8cb25
Changed _win32_local_path_from_url to not allow "file:///C:" form.

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
295
308
    def lock_read(self):
296
309
        """Lock the branch for read operations.
297
310
 
298
 
        :return: An object with an unlock method which will release the lock
299
 
            obtained.
 
311
        :return: A bzrlib.lock.LogicalLockResult.
300
312
        """
301
313
        raise NotImplementedError(self.lock_read)
302
314
 
794
806
            if len(old_repository._fallback_repositories) != 1:
795
807
                raise AssertionError("can't cope with fallback repositories "
796
808
                    "of %r" % (self.repository,))
797
 
            # 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()
798
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.
799
857
            old_repository.lock_read()
800
858
            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
859
                # XXX: If you unstack a branch while it has a working tree
820
860
                # with a pending merge, the pending-merged revisions will no
821
861
                # longer be present.  You can (probably) revert and remerge.
962
1002
                raise errors.NoSuchRevision(self, stop_revision)
963
1003
        return other_history[self_len:stop_revision]
964
1004
 
965
 
    @needs_write_lock
966
1005
    def update_revisions(self, other, stop_revision=None, overwrite=False,
967
1006
                         graph=None):
968
1007
        """Pull in new perfect-fit revisions.
1017
1056
            self._extend_partial_history(distance_from_last)
1018
1057
        return self._partial_revision_history_cache[distance_from_last]
1019
1058
 
1020
 
    @needs_write_lock
1021
1059
    def pull(self, source, overwrite=False, stop_revision=None,
1022
1060
             possible_transports=None, *args, **kwargs):
1023
1061
        """Mirror source into this branch.
1266
1304
                revno = 1
1267
1305
        destination.set_last_revision_info(revno, revision_id)
1268
1306
 
1269
 
    @needs_read_lock
1270
1307
    def copy_content_into(self, destination, revision_id=None):
1271
1308
        """Copy the content of self into destination.
1272
1309
 
1273
1310
        revision_id: if not None, the revision history in the new branch will
1274
1311
                     be truncated to end with revision_id.
1275
1312
        """
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)
 
1313
        return InterBranch.get(self, destination).copy_content_into(
 
1314
            revision_id=revision_id)
1287
1315
 
1288
1316
    def update_references(self, target):
1289
1317
        if not getattr(self._format, 'supports_reference_locations', False):
1344
1372
        return format
1345
1373
 
1346
1374
    def create_clone_on_transport(self, to_transport, revision_id=None,
1347
 
        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):
1348
1377
        """Create a clone of this branch and its bzrdir.
1349
1378
 
1350
1379
        :param to_transport: The transport to clone onto.
1357
1386
        """
1358
1387
        # XXX: Fix the bzrdir API to allow getting the branch back from the
1359
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
1360
1391
        if revision_id is None:
1361
1392
            revision_id = self.last_revision()
1362
1393
        dir_to = self.bzrdir.clone_on_transport(to_transport,
1363
1394
            revision_id=revision_id, stacked_on=stacked_on,
1364
 
            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)
1365
1397
        return dir_to.open_branch()
1366
1398
 
1367
1399
    def create_checkout(self, to_location, revision_id=None,
1492
1524
     * an open routine.
1493
1525
 
1494
1526
    Formats are placed in an dict by their format string for reference
1495
 
    during branch opening. Its not required that these be instances, they
 
1527
    during branch opening. It's not required that these be instances, they
1496
1528
    can be classes themselves with class methods - it simply depends on
1497
1529
    whether state is needed for a given format or not.
1498
1530
 
1521
1553
        try:
1522
1554
            transport = a_bzrdir.get_branch_transport(None, name=name)
1523
1555
            format_string = transport.get_bytes("format")
1524
 
            return klass._formats[format_string]
 
1556
            format = klass._formats[format_string]
 
1557
            if isinstance(format, MetaDirBranchFormatFactory):
 
1558
                return format()
 
1559
            return format
1525
1560
        except errors.NoSuchFile:
1526
1561
            raise errors.NotBranchError(path=transport.base, bzrdir=a_bzrdir)
1527
1562
        except KeyError:
1532
1567
        """Return the current default format."""
1533
1568
        return klass._default_format
1534
1569
 
1535
 
    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):
1536
1585
        """Get the target reference of the branch in a_bzrdir.
1537
1586
 
1538
1587
        format probing must have been completed before calling
1540
1589
        in a_bzrdir is correct.
1541
1590
 
1542
1591
        :param a_bzrdir: The bzrdir to get the branch data from.
 
1592
        :param name: Name of the colocated branch to fetch
1543
1593
        :return: None if the branch is not a reference branch.
1544
1594
        """
1545
1595
        return None
1546
1596
 
1547
1597
    @classmethod
1548
 
    def set_reference(self, a_bzrdir, to_branch):
 
1598
    def set_reference(self, a_bzrdir, name, to_branch):
1549
1599
        """Set the target reference of the branch in a_bzrdir.
1550
1600
 
1551
1601
        format probing must have been completed before calling
1553
1603
        in a_bzrdir is correct.
1554
1604
 
1555
1605
        :param a_bzrdir: The bzrdir to set the branch reference for.
 
1606
        :param name: Name of colocated branch to set, None for default
1556
1607
        :param to_branch: branch that the checkout is to reference
1557
1608
        """
1558
1609
        raise NotImplementedError(self.set_reference)
1672
1723
 
1673
1724
    @classmethod
1674
1725
    def register_format(klass, format):
1675
 
        """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
        """
1676
1731
        klass._formats[format.get_format_string()] = format
1677
1732
        # Metadir formats have a network name of their format string, and get
1678
 
        # registered as class factories.
1679
 
        network_format_registry.register(format.get_format_string(), format.__class__)
 
1733
        # 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__)
1680
1739
 
1681
1740
    @classmethod
1682
1741
    def set_default_format(klass, format):
1702
1761
        return False  # by default
1703
1762
 
1704
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
 
1705
1792
class BranchHooks(Hooks):
1706
1793
    """A dictionary mapping hook name to a list of callables for branch hooks.
1707
1794
 
1734
1821
            "with a bzrlib.branch.PullResult object and only runs in the "
1735
1822
            "bzr client.", (0, 15), None))
1736
1823
        self.create_hook(HookPoint('pre_commit',
1737
 
            "Called after a commit is calculated but before it is is "
 
1824
            "Called after a commit is calculated but before it is "
1738
1825
            "completed. pre_commit is called with (local, master, old_revno, "
1739
1826
            "old_revid, future_revno, future_revid, tree_delta, future_tree"
1740
1827
            "). old_revid is NULL_REVISION for the first commit to a branch, "
1777
1864
            "all are called with the url returned from the previous hook."
1778
1865
            "The order is however undefined.", (1, 9), None))
1779
1866
        self.create_hook(HookPoint('automatic_tag_name',
1780
 
            "Called to determine an automatic tag name for a revision."
 
1867
            "Called to determine an automatic tag name for a revision. "
1781
1868
            "automatic_tag_name is called with (branch, revision_id) and "
1782
1869
            "should return a tag name or None if no tag name could be "
1783
1870
            "determined. The first non-None tag name returned will be used.",
1874
1961
        return self.__dict__ == other.__dict__
1875
1962
 
1876
1963
    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)
 
1964
        return "<%s of %s>" % (self.__class__.__name__, self.branch)
1883
1965
 
1884
1966
 
1885
1967
class SwitchHookParams(object):
2168
2250
        """See BranchFormat.get_format_description()."""
2169
2251
        return "Checkout reference format 1"
2170
2252
 
2171
 
    def get_reference(self, a_bzrdir):
 
2253
    def get_reference(self, a_bzrdir, name=None):
2172
2254
        """See BranchFormat.get_reference()."""
2173
 
        transport = a_bzrdir.get_branch_transport(None)
 
2255
        transport = a_bzrdir.get_branch_transport(None, name=name)
2174
2256
        return transport.get_bytes('location')
2175
2257
 
2176
 
    def set_reference(self, a_bzrdir, to_branch):
 
2258
    def set_reference(self, a_bzrdir, name, to_branch):
2177
2259
        """See BranchFormat.set_reference()."""
2178
 
        transport = a_bzrdir.get_branch_transport(None)
 
2260
        transport = a_bzrdir.get_branch_transport(None, name=name)
2179
2261
        location = transport.put_bytes('location', to_branch.base)
2180
2262
 
2181
2263
    def initialize(self, a_bzrdir, name=None, target_branch=None):
2232
2314
                raise AssertionError("wrong format %r found for %r" %
2233
2315
                    (format, self))
2234
2316
        if location is None:
2235
 
            location = self.get_reference(a_bzrdir)
 
2317
            location = self.get_reference(a_bzrdir, name)
2236
2318
        real_bzrdir = bzrdir.BzrDir.open(
2237
2319
            location, possible_transports=possible_transports)
2238
2320
        result = real_bzrdir.open_branch(name=name, 
2276
2358
    _legacy_formats[0].network_name(), _legacy_formats[0].__class__)
2277
2359
 
2278
2360
 
2279
 
class BranchWriteLockResult(object):
 
2361
class BranchWriteLockResult(LogicalLockResult):
2280
2362
    """The result of write locking a branch.
2281
2363
 
2282
2364
    :ivar branch_token: The token obtained from the underlying branch lock, or
2285
2367
    """
2286
2368
 
2287
2369
    def __init__(self, unlock, branch_token):
 
2370
        LogicalLockResult.__init__(self, unlock)
2288
2371
        self.branch_token = branch_token
2289
 
        self.unlock = unlock
2290
2372
 
2291
 
    def __str__(self):
 
2373
    def __repr__(self):
2292
2374
        return "BranchWriteLockResult(%s, %s)" % (self.branch_token,
2293
2375
            self.unlock)
2294
2376
 
2379
2461
    def lock_read(self):
2380
2462
        """Lock the branch for read operations.
2381
2463
 
2382
 
        :return: An object with an unlock method which will release the lock
2383
 
            obtained.
 
2464
        :return: A bzrlib.lock.LogicalLockResult.
2384
2465
        """
2385
2466
        if not self.is_locked():
2386
2467
            self._note_lock('r')
2394
2475
            took_lock = False
2395
2476
        try:
2396
2477
            self.control_files.lock_read()
2397
 
            return self
 
2478
            return LogicalLockResult(self.unlock)
2398
2479
        except:
2399
2480
            if took_lock:
2400
2481
                self.repository.unlock()
3024
3105
    :ivar tag_conflicts: A list of tag conflicts, see BasicTags.merge_to
3025
3106
    """
3026
3107
 
 
3108
    @deprecated_method(deprecated_in((2, 3, 0)))
3027
3109
    def __int__(self):
3028
 
        # 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
        """
3029
3114
        return self.new_revno - self.old_revno
3030
3115
 
3031
3116
    def report(self, to_file):
3056
3141
        target, otherwise it will be None.
3057
3142
    """
3058
3143
 
 
3144
    @deprecated_method(deprecated_in((2, 3, 0)))
3059
3145
    def __int__(self):
3060
 
        # 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
        """
3061
3150
        return self.new_revno - self.old_revno
3062
3151
 
3063
3152
    def report(self, to_file):
3186
3275
    _optimisers = []
3187
3276
    """The available optimised InterBranch types."""
3188
3277
 
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)
 
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)
3193
3287
 
 
3288
    @needs_write_lock
3194
3289
    def pull(self, overwrite=False, stop_revision=None,
3195
3290
             possible_transports=None, local=False):
3196
3291
        """Mirror source into target branch.
3201
3296
        """
3202
3297
        raise NotImplementedError(self.pull)
3203
3298
 
 
3299
    @needs_write_lock
3204
3300
    def update_revisions(self, stop_revision=None, overwrite=False,
3205
3301
                         graph=None):
3206
3302
        """Pull in new perfect-fit revisions.
3214
3310
        """
3215
3311
        raise NotImplementedError(self.update_revisions)
3216
3312
 
 
3313
    @needs_write_lock
3217
3314
    def push(self, overwrite=False, stop_revision=None,
3218
3315
             _override_hook_source_branch=None):
3219
3316
        """Mirror the source branch into the target branch.
3222
3319
        """
3223
3320
        raise NotImplementedError(self.push)
3224
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
 
3225
3331
 
3226
3332
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
 
 
 
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
3234
3371
    def update_revisions(self, stop_revision=None, overwrite=False,
3235
3372
        graph=None):
3236
3373
        """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
 
 
 
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
3277
3411
    def pull(self, overwrite=False, stop_revision=None,
3278
 
             possible_transports=None, _hook_master=None, run_hooks=True,
 
3412
             possible_transports=None, run_hooks=True,
3279
3413
             _override_hook_target=None, local=False):
3280
 
        """See Branch.pull.
 
3414
        """Pull from source into self, updating my master if any.
3281
3415
 
3282
 
        :param _hook_master: Private parameter - set the branch to
3283
 
            be supplied as the master to pull hooks.
3284
3416
        :param run_hooks: Private parameter - if false, this branch
3285
3417
            is being called because it's the master of the primary branch,
3286
3418
            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
3419
        """
3291
 
        # This type of branch can't be bound.
3292
 
        if local:
 
3420
        bound_location = self.target.get_bound_location()
 
3421
        if local and not bound_location:
3293
3422
            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()
 
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()
3301
3428
        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)
 
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)
3328
3437
        finally:
3329
 
            self.source.unlock()
3330
 
        return result
 
3438
            if master_branch:
 
3439
                master_branch.unlock()
3331
3440
 
3332
3441
    def push(self, overwrite=False, stop_revision=None,
3333
3442
             _override_hook_source_branch=None):
3373
3482
                # push into the master from the source branch.
3374
3483
                self.source._basic_push(master_branch, overwrite, stop_revision)
3375
3484
                # and push into the target branch from the source. Note that we
3376
 
                # push from the source branch again, because its considered the
 
3485
                # push from the source branch again, because it's considered the
3377
3486
                # highest bandwidth repository.
3378
3487
                result = self.source._basic_push(self.target, overwrite,
3379
3488
                    stop_revision)
3395
3504
            _run_hooks()
3396
3505
            return result
3397
3506
 
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,
 
3507
    def _pull(self, overwrite=False, stop_revision=None,
 
3508
             possible_transports=None, _hook_master=None, run_hooks=True,
3412
3509
             _override_hook_target=None, local=False):
3413
 
        """Pull from source into self, updating my master if any.
3414
 
 
 
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.
3415
3517
        :param run_hooks: Private parameter - if false, this branch
3416
3518
            is being called because it's the master of the primary branch,
3417
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.
3418
3523
        """
3419
 
        bound_location = self.target.get_bound_location()
3420
 
        if local and not bound_location:
 
3524
        # This type of branch can't be bound.
 
3525
        if local:
3421
3526
            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()
 
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()
3427
3534
        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)
 
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)
3436
3561
        finally:
3437
 
            if master_branch:
3438
 
                master_branch.unlock()
 
3562
            self.source.unlock()
 
3563
        return result
3439
3564
 
3440
3565
 
3441
3566
InterBranch.register_optimiser(GenericInterBranch)
3442
 
InterBranch.register_optimiser(InterToBranch5)