~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/branch.py

  • Committer: Alexander Belchenko
  • Date: 2007-02-02 09:14:30 UTC
  • mfrom: (2256 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2259.
  • Revision ID: bialix@ukr.net-20070202091430-vdgouvazded1yfqw
merge bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
81
81
 
82
82
    base
83
83
        Base directory/url of the branch.
 
84
 
 
85
    hooks: An instance of BranchHooks.
84
86
    """
85
87
    # this is really an instance variable - FIXME move it there
86
88
    # - RBC 20060112
375
377
        return history[revno - 1]
376
378
 
377
379
    def pull(self, source, overwrite=False, stop_revision=None):
 
380
        """Mirror source into this branch.
 
381
 
 
382
        This branch is considered to be 'local', having low latency.
 
383
        """
378
384
        raise NotImplementedError(self.pull)
379
385
 
 
386
    def push(self, target, overwrite=False, stop_revision=None):
 
387
        """Mirror this branch into target.
 
388
 
 
389
        This branch is considered to be 'local', having low latency.
 
390
        """
 
391
        raise NotImplementedError(self.push)
 
392
 
380
393
    def basis_tree(self):
381
394
        """Return `Tree` object for last revision."""
382
395
        return self.repository.revision_tree(self.last_revision())
599
612
            format = self.repository.bzrdir.cloning_metadir()
600
613
        return format
601
614
 
602
 
    def create_checkout(self, to_location, revision_id=None, 
 
615
    def create_checkout(self, to_location, revision_id=None,
603
616
                        lightweight=False):
604
617
        """Create a checkout of a branch.
605
618
        
716
729
        return self.get_format_string().rstrip()
717
730
 
718
731
 
 
732
class BranchHooks(dict):
 
733
    """A dictionary mapping hook name to a list of callables for branch hooks.
 
734
    
 
735
    e.g. ['set_rh'] Is the list of items to be called when the
 
736
    set_revision_history function is invoked.
 
737
    """
 
738
 
 
739
    def __init__(self):
 
740
        """Create the default hooks.
 
741
 
 
742
        These are all empty initially, because by default nothing should get
 
743
        notified.
 
744
        """
 
745
        dict.__init__(self)
 
746
        # invoked whenever the revision history has been set
 
747
        # with set_revision_history. The api signature is
 
748
        # (branch, revision_history), and the branch will
 
749
        # be write-locked. Introduced in 0.15.
 
750
        self['set_rh'] = []
 
751
 
 
752
    def install_hook(self, hook_name, a_callable):
 
753
        """Install a_callable in to the hook hook_name.
 
754
 
 
755
        :param hook_name: A hook name. See the __init__ method of BranchHooks
 
756
            for the complete list of hooks.
 
757
        :param a_callable: The callable to be invoked when the hook triggers.
 
758
            The exact signature will depend on the hook - see the __init__ 
 
759
            method of BranchHooks for details on each hook.
 
760
        """
 
761
        try:
 
762
            self[hook_name].append(a_callable)
 
763
        except KeyError:
 
764
            raise errors.UnknownHook('branch', hook_name)
 
765
 
 
766
 
 
767
# install the default hooks into the Branch class.
 
768
Branch.hooks = BranchHooks()
 
769
 
 
770
 
719
771
class BzrBranchFormat4(BranchFormat):
720
772
    """Bzr branch format 4.
721
773
 
1101
1153
            # this call is disabled because revision_history is 
1102
1154
            # not really an object yet, and the transaction is for objects.
1103
1155
            # transaction.register_clean(history)
 
1156
        for hook in Branch.hooks['set_rh']:
 
1157
            hook(self, rev_history)
1104
1158
 
1105
1159
    @needs_read_lock
1106
1160
    def revision_history(self):
1205
1259
        finally:
1206
1260
            source.unlock()
1207
1261
 
 
1262
    @needs_read_lock
 
1263
    def push(self, target, overwrite=False, stop_revision=None):
 
1264
        """See Branch.push."""
 
1265
        target.lock_write()
 
1266
        try:
 
1267
            old_count = len(target.revision_history())
 
1268
            try:
 
1269
                target.update_revisions(self, stop_revision)
 
1270
            except DivergedBranches:
 
1271
                if not overwrite:
 
1272
                    raise
 
1273
            if overwrite:
 
1274
                target.set_revision_history(self.revision_history())
 
1275
            new_count = len(target.revision_history())
 
1276
            return new_count - old_count
 
1277
        finally:
 
1278
            target.unlock()
 
1279
 
1208
1280
    def get_parent(self):
1209
1281
        """See Branch.get_parent."""
1210
1282
 
1283
1355
        
1284
1356
    @needs_write_lock
1285
1357
    def pull(self, source, overwrite=False, stop_revision=None):
1286
 
        """Updates branch.pull to be bound branch aware."""
 
1358
        """Extends branch.pull to be bound branch aware."""
1287
1359
        bound_location = self.get_bound_location()
1288
1360
        if source.base != bound_location:
1289
1361
            # not pulling from master, so we need to update master.
1293
1365
                source = master_branch
1294
1366
        return super(BzrBranch5, self).pull(source, overwrite, stop_revision)
1295
1367
 
 
1368
    @needs_write_lock
 
1369
    def push(self, target, overwrite=False, stop_revision=None):
 
1370
        """Updates branch.push to be bound branch aware."""
 
1371
        bound_location = target.get_bound_location()
 
1372
        if target.base != bound_location:
 
1373
            # not pushing to master, so we need to update master.
 
1374
            master_branch = target.get_master_branch()
 
1375
            if master_branch:
 
1376
                # push into the master from this branch.
 
1377
                super(BzrBranch5, self).push(master_branch, overwrite,
 
1378
                    stop_revision)
 
1379
        # and push into the target branch from this. Note that we push from
 
1380
        # this branch again, because its considered the highest bandwidth
 
1381
        # repository.
 
1382
        return super(BzrBranch5, self).push(target, overwrite, stop_revision)
 
1383
 
1296
1384
    def get_bound_location(self):
1297
1385
        try:
1298
1386
            return self.control_files.get_utf8('bound').read()[:-1]