~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/branch.py

  • Committer: Robert Collins
  • Date: 2005-10-17 21:20:18 UTC
  • mfrom: (1461)
  • mto: This revision was merged to the branch mainline in revision 1462.
  • Revision ID: robertc@robertcollins.net-20051017212018-5e2a78c67f36a026
merge from integration

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
                           DivergedBranches, LockError, UnlistableStore,
36
36
                           UnlistableBranch, NoSuchFile)
37
37
from bzrlib.textui import show_status
38
 
from bzrlib.revision import Revision
 
38
from bzrlib.revision import Revision, is_ancestor, get_intervening_revisions
 
39
 
39
40
from bzrlib.delta import compare_trees
40
41
from bzrlib.tree import EmptyTree, RevisionTree
41
42
from bzrlib.inventory import Inventory
855
856
        finally:
856
857
            self.unlock()
857
858
 
858
 
    def common_ancestor(self, other, self_revno=None, other_revno=None):
859
 
        """
860
 
        >>> from bzrlib.commit import commit
861
 
        >>> sb = ScratchBranch(files=['foo', 'foo~'])
862
 
        >>> sb.common_ancestor(sb) == (None, None)
863
 
        True
864
 
        >>> commit(sb, "Committing first revision", verbose=False)
865
 
        >>> sb.common_ancestor(sb)[0]
866
 
        1
867
 
        >>> clone = sb.clone()
868
 
        >>> commit(sb, "Committing second revision", verbose=False)
869
 
        >>> sb.common_ancestor(sb)[0]
870
 
        2
871
 
        >>> sb.common_ancestor(clone)[0]
872
 
        1
873
 
        >>> commit(clone, "Committing divergent second revision", 
874
 
        ...               verbose=False)
875
 
        >>> sb.common_ancestor(clone)[0]
876
 
        1
877
 
        >>> sb.common_ancestor(clone) == clone.common_ancestor(sb)
878
 
        True
879
 
        >>> sb.common_ancestor(sb) != clone.common_ancestor(clone)
880
 
        True
881
 
        >>> clone2 = sb.clone()
882
 
        >>> sb.common_ancestor(clone2)[0]
883
 
        2
884
 
        >>> sb.common_ancestor(clone2, self_revno=1)[0]
885
 
        1
886
 
        >>> sb.common_ancestor(clone2, other_revno=1)[0]
887
 
        1
888
 
        """
889
 
        my_history = self.revision_history()
890
 
        other_history = other.revision_history()
891
 
        if self_revno is None:
892
 
            self_revno = len(my_history)
893
 
        if other_revno is None:
894
 
            other_revno = len(other_history)
895
 
        indices = range(min((self_revno, other_revno)))
896
 
        indices.reverse()
897
 
        for r in indices:
898
 
            if my_history[r] == other_history[r]:
899
 
                return r+1, my_history[r]
900
 
        return None, None
901
 
 
902
 
 
903
859
    def revno(self):
904
860
        """Return current revision number for this branch.
905
861
 
947
903
        Traceback (most recent call last):
948
904
        DivergedBranches: These branches have diverged.
949
905
        """
950
 
        # FIXME: If the branches have diverged, but the latest
951
 
        # revision in this branch is completely merged into the other,
952
 
        # then we should still be able to pull.
953
906
        self_history = self.revision_history()
954
907
        self_len = len(self_history)
955
908
        other_history = other.revision_history()
969
922
 
970
923
    def update_revisions(self, other, stop_revision=None):
971
924
        """Pull in new perfect-fit revisions."""
 
925
        # FIXME: If the branches have diverged, but the latest
 
926
        # revision in this branch is completely merged into the other,
 
927
        # then we should still be able to pull.
972
928
        from bzrlib.fetch import greedy_fetch
973
 
        from bzrlib.revision import get_intervening_revisions
974
929
        if stop_revision is None:
975
930
            stop_revision = other.last_revision()
 
931
        ### Should this be checking is_ancestor instead of revision_history?
976
932
        if (stop_revision is not None and 
977
933
            stop_revision in self.revision_history()):
978
934
            return
979
935
        greedy_fetch(to_branch=self, from_branch=other,
980
936
                     revision=stop_revision)
981
 
        pullable_revs = self.missing_revisions(
982
 
            other, other.revision_id_to_revno(stop_revision))
983
 
        if pullable_revs:
984
 
            greedy_fetch(to_branch=self,
985
 
                         from_branch=other,
986
 
                         revision=pullable_revs[-1])
 
937
        pullable_revs = self.pullable_revisions(other, stop_revision)
 
938
        if len(pullable_revs) > 0:
987
939
            self.append_revision(*pullable_revs)
988
940
 
 
941
    def pullable_revisions(self, other, stop_revision):
 
942
        other_revno = other.revision_id_to_revno(stop_revision)
 
943
        try:
 
944
            return self.missing_revisions(other, other_revno)
 
945
        except DivergedBranches, e:
 
946
            try:
 
947
                pullable_revs = get_intervening_revisions(self.last_revision(),
 
948
                                                          stop_revision, self)
 
949
                assert self.last_revision() not in pullable_revs
 
950
                return pullable_revs
 
951
            except bzrlib.errors.NotAncestor:
 
952
                if is_ancestor(self.last_revision(), stop_revision, self):
 
953
                    return []
 
954
                else:
 
955
                    raise e
 
956
        
989
957
    def commit(self, *args, **kw):
990
958
        from bzrlib.commit import Commit
991
959
        Commit().commit(self, *args, **kw)