~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/branch.py

  • Committer: Martin Pool
  • Date: 2005-06-06 11:53:29 UTC
  • Revision ID: mbp@sourcefrog.net-20050606115329-1596352add25bffd
- merge aaron's updated merge/pull code

Show diffs side-by-side

added added

removed removed

Lines of Context:
104
104
            raise BzrError('%r is not in a branch' % orig_f)
105
105
        f = head
106
106
    
107
 
 
 
107
class DivergedBranches(Exception):
 
108
    def __init__(self, branch1, branch2):
 
109
        self.branch1 = branch1
 
110
        self.branch2 = branch2
 
111
        Exception.__init__(self, "These branches have diverged.")
108
112
 
109
113
######################################################################
110
114
# branch objects
650
654
            return None
651
655
 
652
656
 
 
657
    def missing_revisions(self, other):
 
658
        """
 
659
        If self and other have not diverged, return a list of the revisions
 
660
        present in other, but missing from self.
 
661
 
 
662
        >>> from bzrlib.commit import commit
 
663
        >>> bzrlib.trace.silent = True
 
664
        >>> br1 = ScratchBranch()
 
665
        >>> br2 = ScratchBranch()
 
666
        >>> br1.missing_revisions(br2)
 
667
        []
 
668
        >>> commit(br2, "lala!", rev_id="REVISION-ID-1")
 
669
        >>> br1.missing_revisions(br2)
 
670
        [u'REVISION-ID-1']
 
671
        >>> br2.missing_revisions(br1)
 
672
        []
 
673
        >>> commit(br1, "lala!", rev_id="REVISION-ID-1")
 
674
        >>> br1.missing_revisions(br2)
 
675
        []
 
676
        >>> commit(br2, "lala!", rev_id="REVISION-ID-2A")
 
677
        >>> br1.missing_revisions(br2)
 
678
        [u'REVISION-ID-2A']
 
679
        >>> commit(br1, "lala!", rev_id="REVISION-ID-2B")
 
680
        >>> br1.missing_revisions(br2)
 
681
        Traceback (most recent call last):
 
682
        DivergedBranches: These branches have diverged.
 
683
        """
 
684
        self_history = self.revision_history()
 
685
        self_len = len(self_history)
 
686
        other_history = other.revision_history()
 
687
        other_len = len(other_history)
 
688
        common_index = min(self_len, other_len) -1
 
689
        if common_index >= 0 and \
 
690
            self_history[common_index] != other_history[common_index]:
 
691
            raise DivergedBranches(self, other)
 
692
        if self_len < other_len:
 
693
            return other_history[self_len:]
 
694
        return []
 
695
 
 
696
 
 
697
    def update_revisions(self, other):
 
698
        """If self and other have not diverged, ensure self has all the
 
699
        revisions in other
 
700
 
 
701
        >>> from bzrlib.commit import commit
 
702
        >>> bzrlib.trace.silent = True
 
703
        >>> br1 = ScratchBranch(files=['foo', 'bar'])
 
704
        >>> br1.add('foo')
 
705
        >>> br1.add('bar')
 
706
        >>> commit(br1, "lala!", rev_id="REVISION-ID-1", verbose=False)
 
707
        >>> br2 = ScratchBranch()
 
708
        >>> br2.update_revisions(br1)
 
709
        Added 2 texts.
 
710
        Added 1 inventories.
 
711
        Added 1 revisions.
 
712
        >>> br2.revision_history()
 
713
        [u'REVISION-ID-1']
 
714
        >>> br2.update_revisions(br1)
 
715
        Added 0 texts.
 
716
        Added 0 inventories.
 
717
        Added 0 revisions.
 
718
        >>> br1.text_store.total_size() == br2.text_store.total_size()
 
719
        True
 
720
        """
 
721
        revision_ids = self.missing_revisions(other)
 
722
        revisions = [other.get_revision(f) for f in revision_ids]
 
723
        needed_texts = sets.Set()
 
724
        for rev in revisions:
 
725
            inv = other.get_inventory(str(rev.inventory_id))
 
726
            for key, entry in inv.iter_entries():
 
727
                if entry.text_id is None:
 
728
                    continue
 
729
                if entry.text_id not in self.text_store:
 
730
                    needed_texts.add(entry.text_id)
 
731
        count = self.text_store.copy_multi(other.text_store, needed_texts)
 
732
        print "Added %d texts." % count 
 
733
        inventory_ids = [ f.inventory_id for f in revisions ]
 
734
        count = self.inventory_store.copy_multi(other.inventory_store, 
 
735
                                                inventory_ids)
 
736
        print "Added %d inventories." % count 
 
737
        revision_ids = [ f.revision_id for f in revisions]
 
738
        count = self.revision_store.copy_multi(other.revision_store, 
 
739
                                               revision_ids)
 
740
        for revision_id in revision_ids:
 
741
            self.append_revision(revision_id)
 
742
        print "Added %d revisions." % count
 
743
                    
 
744
        
653
745
    def commit(self, *args, **kw):
654
746
        """Deprecated"""
655
747
        from bzrlib.commit import commit