~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 04:17:53 UTC
  • Revision ID: mbp@sourcefrog.net-20050606041753-abe590daf0d7f959
Updated merge patch from Aaron

This patch contains all the changes to merge that I'd like to get into
0.5, namely
* common ancestor BASE selection
* merge reports conflicts when they are encountered
* merge refuses to operate in working trees with changes
* introduces revert command to revert the working tree to the
last-committed state
* Adds some reasonable help text

Show diffs side-by-side

added added

removed removed

Lines of Context:
565
565
            self.unlock()
566
566
 
567
567
 
 
568
    def common_ancestor(self, other, self_revno=None, other_revno=None):
 
569
        """
 
570
        >>> import commit
 
571
        >>> sb = ScratchBranch(files=['foo', 'foo~'])
 
572
        >>> sb.common_ancestor(sb) == (None, None)
 
573
        True
 
574
        >>> commit.commit(sb, "Committing first revision", verbose=False)
 
575
        >>> sb.common_ancestor(sb)[0]
 
576
        1
 
577
        >>> clone = sb.clone()
 
578
        >>> commit.commit(sb, "Committing second revision", verbose=False)
 
579
        >>> sb.common_ancestor(sb)[0]
 
580
        2
 
581
        >>> sb.common_ancestor(clone)[0]
 
582
        1
 
583
        >>> commit.commit(clone, "Committing divergent second revision", 
 
584
        ...               verbose=False)
 
585
        >>> sb.common_ancestor(clone)[0]
 
586
        1
 
587
        >>> sb.common_ancestor(clone) == clone.common_ancestor(sb)
 
588
        True
 
589
        >>> sb.common_ancestor(sb) != clone.common_ancestor(clone)
 
590
        True
 
591
        >>> clone2 = sb.clone()
 
592
        >>> sb.common_ancestor(clone2)[0]
 
593
        2
 
594
        >>> sb.common_ancestor(clone2, self_revno=1)[0]
 
595
        1
 
596
        >>> sb.common_ancestor(clone2, other_revno=1)[0]
 
597
        1
 
598
        """
 
599
        my_history = self.revision_history()
 
600
        other_history = other.revision_history()
 
601
        if self_revno is None:
 
602
            self_revno = len(my_history)
 
603
        if other_revno is None:
 
604
            other_revno = len(other_history)
 
605
        indices = range(min((self_revno, other_revno)))
 
606
        indices.reverse()
 
607
        for r in indices:
 
608
            if my_history[r] == other_history[r]:
 
609
                return r+1, my_history[r]
 
610
        return None, None
 
611
 
568
612
    def enum_history(self, direction):
569
613
        """Return (revno, revision_id) for history of branch.
570
614
 
784
828
    >>> isdir(bd)
785
829
    False
786
830
    """
787
 
    def __init__(self, files=[], dirs=[]):
 
831
    def __init__(self, files=[], dirs=[], base=None):
788
832
        """Make a test branch.
789
833
 
790
834
        This creates a temporary directory and runs init-tree in it.
791
835
 
792
836
        If any files are listed, they are created in the working copy.
793
837
        """
794
 
        Branch.__init__(self, tempfile.mkdtemp(), init=True)
 
838
        init = False
 
839
        if base is None:
 
840
            base = tempfile.mkdtemp()
 
841
            init = True
 
842
        Branch.__init__(self, base, init=init)
795
843
        for d in dirs:
796
844
            os.mkdir(self.abspath(d))
797
845
            
799
847
            file(os.path.join(self.base, f), 'w').write('content of %s' % f)
800
848
 
801
849
 
 
850
    def clone(self):
 
851
        """
 
852
        >>> orig = ScratchBranch(files=["file1", "file2"])
 
853
        >>> clone = orig.clone()
 
854
        >>> os.path.samefile(orig.base, clone.base)
 
855
        False
 
856
        >>> os.path.isfile(os.path.join(clone.base, "file1"))
 
857
        True
 
858
        """
 
859
        base = tempfile.mkdtemp()
 
860
        os.rmdir(base)
 
861
        shutil.copytree(self.base, base, symlinks=True)
 
862
        return ScratchBranch(base=base)
 
863
        
802
864
    def __del__(self):
803
865
        self.destroy()
804
866