~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/branch.py

  • Committer: Martin Pool
  • Date: 2005-08-25 08:19:55 UTC
  • Revision ID: mbp@sourcefrog.net-20050825081955-d7295021dcfcd2ba
- disable standard logging to .bzr.log and stderr while running 
  tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
from bzrlib.osutils import isdir, quotefn, compact_date, rand_bytes, \
24
24
     splitpath, \
25
25
     sha_file, appendpath, file_kind
 
26
 
26
27
from bzrlib.errors import BzrError, InvalidRevisionNumber, InvalidRevisionId
27
28
import bzrlib.errors
28
29
from bzrlib.textui import show_status
30
31
from bzrlib.xml import unpack_xml
31
32
from bzrlib.delta import compare_trees
32
33
from bzrlib.tree import EmptyTree, RevisionTree
33
 
        
 
34
import bzrlib.ui
 
35
 
 
36
 
 
37
 
34
38
BZR_BRANCH_FORMAT = "Bazaar-NG branch, format 0.0.4\n"
35
39
## TODO: Maybe include checks for common corruption of newlines, etc?
36
40
 
39
43
# repeatedly to calculate deltas.  We could perhaps have a weakref
40
44
# cache in memory to make this faster.
41
45
 
 
46
# TODO: please move the revision-string syntax stuff out of the branch
 
47
# object; it's clutter
 
48
 
42
49
 
43
50
def find_branch(f, **args):
44
51
    if f and (f.startswith('http://') or f.startswith('https://')):
101
108
    It is not necessary that f exists.
102
109
 
103
110
    Basically we keep looking up until we find the control directory or
104
 
    run into the root."""
 
111
    run into the root.  If there isn't one, raises NotBranchError.
 
112
    """
105
113
    if f == None:
106
114
        f = os.getcwd()
107
115
    elif hasattr(os.path, 'realpath'):
120
128
        head, tail = os.path.split(f)
121
129
        if head == f:
122
130
            # reached the root, whatever that may be
123
 
            raise BzrError('%r is not in a branch' % orig_f)
 
131
            raise bzrlib.errors.NotBranchError('%s is not in a branch' % orig_f)
124
132
        f = head
125
 
    
 
133
 
 
134
 
 
135
 
 
136
# XXX: move into bzrlib.errors; subclass BzrError    
126
137
class DivergedBranches(Exception):
127
138
    def __init__(self, branch1, branch2):
128
139
        self.branch1 = branch1
660
671
        from bzrlib.inventory import Inventory
661
672
        from bzrlib.xml import unpack_xml
662
673
 
663
 
        return unpack_xml(Inventory, self.inventory_store[inventory_id])
 
674
        return unpack_xml(Inventory, self.get_inventory_xml(inventory_id))
 
675
 
 
676
 
 
677
    def get_inventory_xml(self, inventory_id):
 
678
        """Get inventory XML as a file object."""
 
679
        return self.inventory_store[inventory_id]
664
680
            
665
681
 
666
682
    def get_inventory_sha1(self, inventory_id):
667
683
        """Return the sha1 hash of the inventory entry
668
684
        """
669
 
        return sha_file(self.inventory_store[inventory_id])
 
685
        return sha_file(self.get_inventory_xml(inventory_id))
670
686
 
671
687
 
672
688
    def get_revision_inventory(self, revision_id):
758
774
            return None
759
775
 
760
776
 
761
 
    def missing_revisions(self, other, stop_revision=None):
 
777
    def missing_revisions(self, other, stop_revision=None, diverged_ok=False):
762
778
        """
763
779
        If self and other have not diverged, return a list of the revisions
764
780
        present in other, but missing from self.
797
813
        if stop_revision is None:
798
814
            stop_revision = other_len
799
815
        elif stop_revision > other_len:
800
 
            raise NoSuchRevision(self, stop_revision)
 
816
            raise bzrlib.errors.NoSuchRevision(self, stop_revision)
801
817
        
802
818
        return other_history[self_len:stop_revision]
803
819
 
804
820
 
805
821
    def update_revisions(self, other, stop_revision=None):
806
822
        """Pull in all new revisions from other branch.
807
 
        
808
 
        >>> from bzrlib.commit import commit
809
 
        >>> bzrlib.trace.silent = True
810
 
        >>> br1 = ScratchBranch(files=['foo', 'bar'])
811
 
        >>> br1.add('foo')
812
 
        >>> br1.add('bar')
813
 
        >>> commit(br1, "lala!", rev_id="REVISION-ID-1", verbose=False)
814
 
        >>> br2 = ScratchBranch()
815
 
        >>> br2.update_revisions(br1)
816
 
        Added 2 texts.
817
 
        Added 1 inventories.
818
 
        Added 1 revisions.
819
 
        >>> br2.revision_history()
820
 
        [u'REVISION-ID-1']
821
 
        >>> br2.update_revisions(br1)
822
 
        Added 0 texts.
823
 
        Added 0 inventories.
824
 
        Added 0 revisions.
825
 
        >>> br1.text_store.total_size() == br2.text_store.total_size()
826
 
        True
827
823
        """
828
 
        from bzrlib.progress import ProgressBar
829
 
 
830
 
        pb = ProgressBar()
831
 
 
 
824
        from bzrlib.fetch import greedy_fetch
 
825
 
 
826
        pb = bzrlib.ui.ui_factory.progress_bar()
832
827
        pb.update('comparing histories')
 
828
 
833
829
        revision_ids = self.missing_revisions(other, stop_revision)
834
830
 
 
831
        if len(revision_ids) > 0:
 
832
            count = greedy_fetch(self, other, revision_ids[-1], pb)[0]
 
833
        else:
 
834
            count = 0
 
835
        self.append_revision(*revision_ids)
 
836
        ## note("Added %d revisions." % count)
 
837
 
 
838
        
 
839
    def install_revisions(self, other, revision_ids, pb):
835
840
        if hasattr(other.revision_store, "prefetch"):
836
841
            other.revision_store.prefetch(revision_ids)
837
842
        if hasattr(other.inventory_store, "prefetch"):
838
843
            inventory_ids = [other.get_revision(r).inventory_id
839
844
                             for r in revision_ids]
840
845
            other.inventory_store.prefetch(inventory_ids)
 
846
 
 
847
        if pb is None:
 
848
            pb = bzrlib.ui.ui_factory.progress_bar()
841
849
                
842
850
        revisions = []
843
851
        needed_texts = set()
844
852
        i = 0
845
 
        for rev_id in revision_ids:
846
 
            i += 1
847
 
            pb.update('fetching revision', i, len(revision_ids))
848
 
            rev = other.get_revision(rev_id)
 
853
 
 
854
        failures = set()
 
855
        for i, rev_id in enumerate(revision_ids):
 
856
            pb.update('fetching revision', i+1, len(revision_ids))
 
857
            try:
 
858
                rev = other.get_revision(rev_id)
 
859
            except bzrlib.errors.NoSuchRevision:
 
860
                failures.add(rev_id)
 
861
                continue
 
862
 
849
863
            revisions.append(rev)
850
864
            inv = other.get_inventory(str(rev.inventory_id))
851
865
            for key, entry in inv.iter_entries():
856
870
 
857
871
        pb.clear()
858
872
                    
859
 
        count = self.text_store.copy_multi(other.text_store, needed_texts)
 
873
        count, cp_fail = self.text_store.copy_multi(other.text_store, 
 
874
                                                    needed_texts)
860
875
        print "Added %d texts." % count 
861
876
        inventory_ids = [ f.inventory_id for f in revisions ]
862
 
        count = self.inventory_store.copy_multi(other.inventory_store, 
863
 
                                                inventory_ids)
 
877
        count, cp_fail = self.inventory_store.copy_multi(other.inventory_store, 
 
878
                                                         inventory_ids)
864
879
        print "Added %d inventories." % count 
865
880
        revision_ids = [ f.revision_id for f in revisions]
866
 
        count = self.revision_store.copy_multi(other.revision_store, 
867
 
                                               revision_ids)
868
 
        for revision_id in revision_ids:
869
 
            self.append_revision(revision_id)
870
 
        print "Added %d revisions." % count
871
 
                    
872
 
        
 
881
 
 
882
        count, cp_fail = self.revision_store.copy_multi(other.revision_store, 
 
883
                                                          revision_ids,
 
884
                                                          permit_failure=True)
 
885
        assert len(cp_fail) == 0 
 
886
        return count, failures
 
887
       
 
888
 
873
889
    def commit(self, *args, **kw):
874
890
        from bzrlib.commit import commit
875
891
        commit(self, *args, **kw)
880
896
        revno, info = self.get_revision_info(revision)
881
897
        return info
882
898
 
 
899
 
 
900
    def revision_id_to_revno(self, revision_id):
 
901
        """Given a revision id, return its revno"""
 
902
        history = self.revision_history()
 
903
        try:
 
904
            return history.index(revision_id) + 1
 
905
        except ValueError:
 
906
            raise bzrlib.errors.NoSuchRevision(self, revision_id)
 
907
 
 
908
 
883
909
    def get_revision_info(self, revision):
884
910
        """Return (revno, revision id) for revision identifier.
885
911