/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: Martin Pool
  • Date: 2005-07-18 13:12:43 UTC
  • Revision ID: mbp@sourcefrog.net-20050718131243-44532527fd065b31
- update convertinv to work with current weave code

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
 
18
 
import sys
19
 
import os
 
18
import sys, os
20
19
 
21
20
import bzrlib
22
21
from bzrlib.trace import mutter, note
23
 
from bzrlib.osutils import isdir, quotefn, compact_date, rand_bytes, \
24
 
     splitpath, \
 
22
from bzrlib.osutils import isdir, quotefn, compact_date, rand_bytes, splitpath, \
25
23
     sha_file, appendpath, file_kind
26
 
from bzrlib.errors import BzrError, InvalidRevisionNumber, InvalidRevisionId
27
 
import bzrlib.errors
28
 
from bzrlib.textui import show_status
29
 
from bzrlib.revision import Revision
30
 
from bzrlib.xml import unpack_xml
31
 
from bzrlib.delta import compare_trees
32
 
from bzrlib.tree import EmptyTree, RevisionTree
33
 
        
 
24
from bzrlib.errors import BzrError
 
25
 
34
26
BZR_BRANCH_FORMAT = "Bazaar-NG branch, format 0.0.4\n"
35
27
## TODO: Maybe include checks for common corruption of newlines, etc?
36
28
 
37
29
 
38
 
# TODO: Some operations like log might retrieve the same revisions
39
 
# repeatedly to calculate deltas.  We could perhaps have a weakref
40
 
# cache in memory to make this faster.
41
 
 
42
30
 
43
31
def find_branch(f, **args):
44
32
    if f and (f.startswith('http://') or f.startswith('https://')):
130
118
        Exception.__init__(self, "These branches have diverged.")
131
119
 
132
120
 
 
121
class NoSuchRevision(BzrError):
 
122
    def __init__(self, branch, revision):
 
123
        self.branch = branch
 
124
        self.revision = revision
 
125
        msg = "Branch %s has no revision %d" % (branch, revision)
 
126
        BzrError.__init__(self, msg)
 
127
 
 
128
 
133
129
######################################################################
134
130
# branch objects
135
131
 
365
361
            # ElementTree does its own conversion from UTF-8, so open in
366
362
            # binary.
367
363
            inv = unpack_xml(Inventory,
368
 
                             self.controlfile('inventory', 'rb'))
 
364
                                  self.controlfile('inventory', 'rb'))
369
365
            mutter("loaded inventory of %d items in %f"
370
366
                   % (len(inv), time() - before))
371
367
            return inv
426
422
              add all non-ignored children.  Perhaps do that in a
427
423
              higher-level method.
428
424
        """
 
425
        from bzrlib.textui import show_status
429
426
        # TODO: Re-adding a file that is removed in the working copy
430
427
        # should probably put it back with the previous ID.
431
428
        if isinstance(files, basestring):
504
501
        is the opposite of add.  Removing it is consistent with most
505
502
        other tools.  Maybe an option.
506
503
        """
 
504
        from bzrlib.textui import show_status
507
505
        ## TODO: Normalize names
508
506
        ## TODO: Remove nested loops; better scalability
509
507
        if isinstance(files, basestring):
584
582
            f.close()
585
583
 
586
584
 
587
 
    def get_revision_xml(self, revision_id):
588
 
        """Return XML file object for revision object."""
589
 
        if not revision_id or not isinstance(revision_id, basestring):
590
 
            raise InvalidRevisionId(revision_id)
591
 
 
592
 
        self.lock_read()
593
 
        try:
594
 
            try:
595
 
                return self.revision_store[revision_id]
596
 
            except IndexError:
597
 
                raise bzrlib.errors.NoSuchRevision(revision_id)
598
 
        finally:
599
 
            self.unlock()
600
 
 
601
 
 
602
585
    def get_revision(self, revision_id):
603
586
        """Return the Revision object for a named revision"""
604
 
        xml_file = self.get_revision_xml(revision_id)
 
587
        from bzrlib.revision import Revision
 
588
        from bzrlib.xml import unpack_xml
605
589
 
 
590
        self.lock_read()
606
591
        try:
607
 
            r = unpack_xml(Revision, xml_file)
608
 
        except SyntaxError, e:
609
 
            raise bzrlib.errors.BzrError('failed to unpack revision_xml',
610
 
                                         [revision_id,
611
 
                                          str(e)])
 
592
            if not revision_id or not isinstance(revision_id, basestring):
 
593
                raise ValueError('invalid revision-id: %r' % revision_id)
 
594
            r = unpack_xml(Revision, self.revision_store[revision_id])
 
595
        finally:
 
596
            self.unlock()
612
597
            
613
598
        assert r.revision_id == revision_id
614
599
        return r
615
 
 
616
 
 
617
 
    def get_revision_delta(self, revno):
618
 
        """Return the delta for one revision.
619
 
 
620
 
        The delta is relative to its mainline predecessor, or the
621
 
        empty tree for revision 1.
622
 
        """
623
 
        assert isinstance(revno, int)
624
 
        rh = self.revision_history()
625
 
        if not (1 <= revno <= len(rh)):
626
 
            raise InvalidRevisionNumber(revno)
627
 
 
628
 
        # revno is 1-based; list is 0-based
629
 
 
630
 
        new_tree = self.revision_tree(rh[revno-1])
631
 
        if revno == 1:
632
 
            old_tree = EmptyTree()
633
 
        else:
634
 
            old_tree = self.revision_tree(rh[revno-2])
635
 
 
636
 
        return compare_trees(old_tree, new_tree)
637
 
 
638
600
        
639
601
 
640
602
    def get_revision_sha1(self, revision_id):
645
607
        # the revision, (add signatures/remove signatures) and still
646
608
        # have all hash pointers stay consistent.
647
609
        # But for now, just hash the contents.
648
 
        return bzrlib.osutils.sha_file(self.get_revision_xml(revision_id))
 
610
        return sha_file(self.revision_store[revision_id])
649
611
 
650
612
 
651
613
    def get_inventory(self, inventory_id):
735
697
                return r+1, my_history[r]
736
698
        return None, None
737
699
 
 
700
    def enum_history(self, direction):
 
701
        """Return (revno, revision_id) for history of branch.
 
702
 
 
703
        direction
 
704
            'forward' is from earliest to latest
 
705
            'reverse' is from latest to earliest
 
706
        """
 
707
        rh = self.revision_history()
 
708
        if direction == 'forward':
 
709
            i = 1
 
710
            for rid in rh:
 
711
                yield i, rid
 
712
                i += 1
 
713
        elif direction == 'reverse':
 
714
            i = len(rh)
 
715
            while i > 0:
 
716
                yield i, rh[i-1]
 
717
                i -= 1
 
718
        else:
 
719
            raise ValueError('invalid history direction', direction)
 
720
 
738
721
 
739
722
    def revno(self):
740
723
        """Return current revision number for this branch.
823
806
        True
824
807
        """
825
808
        from bzrlib.progress import ProgressBar
 
809
        try:
 
810
            set
 
811
        except NameError:
 
812
            from sets import Set as set
826
813
 
827
814
        pb = ProgressBar()
828
815
 
1035
1022
 
1036
1023
        `revision_id` may be None for the null revision, in which case
1037
1024
        an `EmptyTree` is returned."""
 
1025
        from bzrlib.tree import EmptyTree, RevisionTree
1038
1026
        # TODO: refactor this to use an existing revision object
1039
1027
        # so we don't need to read it in twice.
1040
1028
        if revision_id == None:
1041
 
            return EmptyTree()
 
1029
            return EmptyTree(self.get_root_id())
1042
1030
        else:
1043
1031
            inv = self.get_revision_inventory(revision_id)
1044
1032
            return RevisionTree(self.text_store, inv)
1055
1043
 
1056
1044
        If there are no revisions yet, return an `EmptyTree`.
1057
1045
        """
 
1046
        from bzrlib.tree import EmptyTree, RevisionTree
1058
1047
        r = self.last_patch()
1059
1048
        if r == None:
1060
 
            return EmptyTree()
 
1049
            return EmptyTree(self.get_root_id())
1061
1050
        else:
1062
1051
            return RevisionTree(self.text_store, self.get_revision_inventory(r))
1063
1052