/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: Aaron Bentley
  • Date: 2007-02-09 07:16:20 UTC
  • mfrom: (2272 +trunk)
  • mto: (2255.6.1 dirstate)
  • mto: This revision was merged to the branch mainline in revision 2322.
  • Revision ID: aaron.bentley@utoronto.ca-20070209071620-gp2n7vtjyb0f2x1e
Merge from 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
223
225
        try:
224
226
            if last_revision is None:
225
227
                pb.update('get source history')
226
 
                from_history = from_branch.revision_history()
227
 
                if from_history:
228
 
                    last_revision = from_history[-1]
229
 
                else:
230
 
                    # no history in the source branch
231
 
                    last_revision = _mod_revision.NULL_REVISION
 
228
                last_revision = from_branch.last_revision_info()[1]
232
229
            return self.repository.fetch(from_branch.repository,
233
230
                                         revision_id=last_revision,
234
231
                                         pb=nested_pb)
322
319
        else:
323
320
            return None
324
321
 
 
322
    def last_revision_info(self):
 
323
        """Return information about the last revision.
 
324
 
 
325
        :return: A tuple (revno, last_revision_id).
 
326
        """
 
327
        rh = self.revision_history()
 
328
        revno = len(rh)
 
329
        if revno:
 
330
            return (revno, rh[-1])
 
331
        else:
 
332
            return (0, _mod_revision.NULL_REVISION)
 
333
 
325
334
    def missing_revisions(self, other, stop_revision=None):
326
335
        """Return a list of new revisions that would perfectly fit.
327
336
        
375
384
        return history[revno - 1]
376
385
 
377
386
    def pull(self, source, overwrite=False, stop_revision=None):
 
387
        """Mirror source into this branch.
 
388
 
 
389
        This branch is considered to be 'local', having low latency.
 
390
        """
378
391
        raise NotImplementedError(self.pull)
379
392
 
 
393
    def push(self, target, overwrite=False, stop_revision=None):
 
394
        """Mirror this branch into target.
 
395
 
 
396
        This branch is considered to be 'local', having low latency.
 
397
        """
 
398
        raise NotImplementedError(self.push)
 
399
 
380
400
    def basis_tree(self):
381
401
        """Return `Tree` object for last revision."""
382
402
        return self.repository.revision_tree(self.last_revision())
599
619
            format = self.repository.bzrdir.checkout_metadir()
600
620
        return format
601
621
 
602
 
    def create_checkout(self, to_location, revision_id=None, 
 
622
    def create_checkout(self, to_location, revision_id=None,
603
623
                        lightweight=False):
604
624
        """Create a checkout of a branch.
605
625
        
693
713
 
694
714
    def get_format_description(self):
695
715
        """Return the short format description for this format."""
696
 
        raise NotImplementedError(self.get_format_string)
 
716
        raise NotImplementedError(self.get_format_description)
697
717
 
698
718
    def initialize(self, a_bzrdir):
699
719
        """Create a branch of this format in a_bzrdir."""
733
753
        return self.get_format_string().rstrip()
734
754
 
735
755
 
 
756
class BranchHooks(dict):
 
757
    """A dictionary mapping hook name to a list of callables for branch hooks.
 
758
    
 
759
    e.g. ['set_rh'] Is the list of items to be called when the
 
760
    set_revision_history function is invoked.
 
761
    """
 
762
 
 
763
    def __init__(self):
 
764
        """Create the default hooks.
 
765
 
 
766
        These are all empty initially, because by default nothing should get
 
767
        notified.
 
768
        """
 
769
        dict.__init__(self)
 
770
        # invoked whenever the revision history has been set
 
771
        # with set_revision_history. The api signature is
 
772
        # (branch, revision_history), and the branch will
 
773
        # be write-locked. Introduced in 0.15.
 
774
        self['set_rh'] = []
 
775
 
 
776
    def install_hook(self, hook_name, a_callable):
 
777
        """Install a_callable in to the hook hook_name.
 
778
 
 
779
        :param hook_name: A hook name. See the __init__ method of BranchHooks
 
780
            for the complete list of hooks.
 
781
        :param a_callable: The callable to be invoked when the hook triggers.
 
782
            The exact signature will depend on the hook - see the __init__ 
 
783
            method of BranchHooks for details on each hook.
 
784
        """
 
785
        try:
 
786
            self[hook_name].append(a_callable)
 
787
        except KeyError:
 
788
            raise errors.UnknownHook('branch', hook_name)
 
789
 
 
790
 
 
791
# install the default hooks into the Branch class.
 
792
Branch.hooks = BranchHooks()
 
793
 
 
794
 
736
795
class BzrBranchFormat4(BranchFormat):
737
796
    """Bzr branch format 4.
738
797
 
1118
1177
            # this call is disabled because revision_history is 
1119
1178
            # not really an object yet, and the transaction is for objects.
1120
1179
            # transaction.register_clean(history)
 
1180
        for hook in Branch.hooks['set_rh']:
 
1181
            hook(self, rev_history)
1121
1182
 
1122
1183
    @needs_read_lock
1123
1184
    def revision_history(self):
1209
1270
        """See Branch.pull."""
1210
1271
        source.lock_read()
1211
1272
        try:
1212
 
            old_count = len(self.revision_history())
 
1273
            old_count = self.last_revision_info()[0]
1213
1274
            try:
1214
1275
                self.update_revisions(source, stop_revision)
1215
1276
            except DivergedBranches:
1217
1278
                    raise
1218
1279
            if overwrite:
1219
1280
                self.set_revision_history(source.revision_history())
1220
 
            new_count = len(self.revision_history())
 
1281
            new_count = self.last_revision_info()[0]
1221
1282
            return new_count - old_count
1222
1283
        finally:
1223
1284
            source.unlock()
1224
1285
 
 
1286
    @needs_read_lock
 
1287
    def push(self, target, overwrite=False, stop_revision=None):
 
1288
        """See Branch.push."""
 
1289
        target.lock_write()
 
1290
        try:
 
1291
            old_count = len(target.revision_history())
 
1292
            try:
 
1293
                target.update_revisions(self, stop_revision)
 
1294
            except DivergedBranches:
 
1295
                if not overwrite:
 
1296
                    raise
 
1297
            if overwrite:
 
1298
                target.set_revision_history(self.revision_history())
 
1299
            new_count = len(target.revision_history())
 
1300
            return new_count - old_count
 
1301
        finally:
 
1302
            target.unlock()
 
1303
 
1225
1304
    def get_parent(self):
1226
1305
        """See Branch.get_parent."""
1227
1306
 
1300
1379
        
1301
1380
    @needs_write_lock
1302
1381
    def pull(self, source, overwrite=False, stop_revision=None):
1303
 
        """Updates branch.pull to be bound branch aware."""
 
1382
        """Extends branch.pull to be bound branch aware."""
1304
1383
        bound_location = self.get_bound_location()
1305
1384
        if source.base != bound_location:
1306
1385
            # not pulling from master, so we need to update master.
1310
1389
                source = master_branch
1311
1390
        return super(BzrBranch5, self).pull(source, overwrite, stop_revision)
1312
1391
 
 
1392
    @needs_write_lock
 
1393
    def push(self, target, overwrite=False, stop_revision=None):
 
1394
        """Updates branch.push to be bound branch aware."""
 
1395
        bound_location = target.get_bound_location()
 
1396
        if target.base != bound_location:
 
1397
            # not pushing to master, so we need to update master.
 
1398
            master_branch = target.get_master_branch()
 
1399
            if master_branch:
 
1400
                # push into the master from this branch.
 
1401
                super(BzrBranch5, self).push(master_branch, overwrite,
 
1402
                    stop_revision)
 
1403
        # and push into the target branch from this. Note that we push from
 
1404
        # this branch again, because its considered the highest bandwidth
 
1405
        # repository.
 
1406
        return super(BzrBranch5, self).push(target, overwrite, stop_revision)
 
1407
 
1313
1408
    def get_bound_location(self):
1314
1409
        try:
1315
1410
            return self.control_files.get_utf8('bound').read()[:-1]