/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/check.py

  • Committer: Martin Pool
  • Date: 2005-06-10 09:30:57 UTC
  • Revision ID: mbp@sourcefrog.net-20050610093057-23705fac7f7f956e
todo

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2004, 2005 by Martin Pool
 
2
# Copyright (C) 2005 by Canonical Ltd
 
3
 
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; either version 2 of the License, or
 
7
# (at your option) any later version.
 
8
 
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details.
 
13
 
 
14
# You should have received a copy of the GNU General Public License
 
15
# along with this program; if not, write to the Free Software
 
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
17
 
 
18
 
 
19
 
 
20
 
 
21
def check(branch):
 
22
    """Run consistency checks on a branch.
 
23
    """
 
24
    import sys
 
25
 
 
26
    from bzrlib.trace import mutter
 
27
    from bzrlib.errors import BzrCheckError
 
28
    from bzrlib.osutils import fingerprint_file
 
29
    from bzrlib.progress import ProgressBar
 
30
    
 
31
    out = sys.stdout
 
32
 
 
33
    pb = ProgressBar(show_spinner=True)
 
34
    last_ptr = None
 
35
    checked_revs = {}
 
36
    
 
37
    history = branch.revision_history()
 
38
    revno = 0
 
39
    revcount = len(history)
 
40
 
 
41
    checked_texts = {}
 
42
    
 
43
    for rid in history:
 
44
        revno += 1
 
45
        pb.update('checking revision', revno, revcount)
 
46
        mutter('    revision {%s}' % rid)
 
47
        rev = branch.get_revision(rid)
 
48
        if rev.revision_id != rid:
 
49
            raise BzrCheckError('wrong internal revision id in revision {%s}' % rid)
 
50
        if rev.precursor != last_ptr:
 
51
            raise BzrCheckError('mismatched precursor in revision {%s}' % rid)
 
52
        last_ptr = rid
 
53
        if rid in checked_revs:
 
54
            raise BzrCheckError('repeated revision {%s}' % rid)
 
55
        checked_revs[rid] = True
 
56
 
 
57
        ## TODO: Check all the required fields are present on the revision.
 
58
 
 
59
        inv = branch.get_inventory(rev.inventory_id)
 
60
        seen_ids = {}
 
61
        seen_names = {}
 
62
 
 
63
        ## p('revision %d/%d file ids' % (revno, revcount))
 
64
        for file_id in inv:
 
65
            if file_id in seen_ids:
 
66
                raise BzrCheckError('duplicated file_id {%s} '
 
67
                                    'in inventory for revision {%s}'
 
68
                                    % (file_id, rid))
 
69
            seen_ids[file_id] = True
 
70
 
 
71
        i = 0
 
72
        len_inv = len(inv)
 
73
        for file_id in inv:
 
74
            i += 1
 
75
            if i & 31 == 0:
 
76
                pb.tick()
 
77
 
 
78
            ie = inv[file_id]
 
79
 
 
80
            if ie.parent_id != None:
 
81
                if ie.parent_id not in seen_ids:
 
82
                    raise BzrCheckError('missing parent {%s} in inventory for revision {%s}'
 
83
                            % (ie.parent_id, rid))
 
84
 
 
85
            if ie.kind == 'file':
 
86
                if ie.text_id in checked_texts:
 
87
                    fp = checked_texts[ie.text_id]
 
88
                else:
 
89
                    if not ie.text_id in branch.text_store:
 
90
                        raise BzrCheckError('text {%s} not in text_store' % ie.text_id)
 
91
 
 
92
                    tf = branch.text_store[ie.text_id]
 
93
                    fp = fingerprint_file(tf)
 
94
                    checked_texts[ie.text_id] = fp
 
95
 
 
96
                if ie.text_size != fp['size']:
 
97
                    raise BzrCheckError('text {%s} wrong size' % ie.text_id)
 
98
                if ie.text_sha1 != fp['sha1']:
 
99
                    raise BzrCheckError('text {%s} wrong sha1' % ie.text_id)
 
100
            elif ie.kind == 'directory':
 
101
                if ie.text_sha1 != None or ie.text_size != None or ie.text_id != None:
 
102
                    raise BzrCheckError('directory {%s} has text in revision {%s}'
 
103
                            % (file_id, rid))
 
104
 
 
105
        pb.tick()
 
106
        for path, ie in inv.iter_entries():
 
107
            if path in seen_names:
 
108
                raise BzrCheckError('duplicated path %r '
 
109
                                    'in inventory for revision {%s}'
 
110
                                    % (path, revid))
 
111
            seen_names[path] = True
 
112
 
 
113
 
 
114
    pb.clear()
 
115
    print 'checked %d revisions, %d file texts' % (revcount, len(checked_texts))
 
116