/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-05-19 07:14:26 UTC
  • Revision ID: mbp@sourcefrog.net-20050519071426-88678406e8501019
- add lazy test for 'bzr info'

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
# consistency checks
 
22
 
 
23
import sys
 
24
 
 
25
from trace import mutter
 
26
from errors import bailout
 
27
import osutils
 
28
 
 
29
def check(branch, progress=True):
 
30
    from bzrlib import set
 
31
 
 
32
    out = sys.stdout
 
33
 
 
34
    # TODO: factor out
 
35
    if not (hasattr(out, 'isatty') and out.isatty()):
 
36
        progress=False
 
37
 
 
38
    if progress:
 
39
        def p(m):
 
40
            mutter('checking ' + m)
 
41
            out.write('\rchecking: %-50.50s' % m)
 
42
            out.flush()
 
43
    else:
 
44
        def p(m):
 
45
            mutter('checking ' + m)
 
46
 
 
47
    p('history of %r' % branch.base)
 
48
    last_ptr = None
 
49
    checked_revs = set()
 
50
    
 
51
    history = branch.revision_history()
 
52
    revno = 0
 
53
    revcount = len(history)
 
54
 
 
55
    checked_texts = {}
 
56
    
 
57
    for rid in history:
 
58
        revno += 1
 
59
        p('revision %d/%d' % (revno, revcount))
 
60
        mutter('    revision {%s}' % rid)
 
61
        rev = branch.get_revision(rid)
 
62
        if rev.revision_id != rid:
 
63
            bailout('wrong internal revision id in revision {%s}' % rid)
 
64
        if rev.precursor != last_ptr:
 
65
            bailout('mismatched precursor in revision {%s}' % rid)
 
66
        last_ptr = rid
 
67
        if rid in checked_revs:
 
68
            bailout('repeated revision {%s}' % rid)
 
69
        checked_revs.add(rid)
 
70
 
 
71
        ## TODO: Check all the required fields are present on the revision.
 
72
 
 
73
        inv = branch.get_inventory(rev.inventory_id)
 
74
        seen_ids = set()
 
75
        seen_names = set()
 
76
 
 
77
        p('revision %d/%d file ids' % (revno, revcount))
 
78
        for file_id in inv:
 
79
            if file_id in seen_ids:
 
80
                bailout('duplicated file_id {%s} in inventory for revision {%s}'
 
81
                        % (file_id, rid))
 
82
            seen_ids.add(file_id)
 
83
 
 
84
        i = 0
 
85
        len_inv = len(inv)
 
86
        for file_id in inv:
 
87
            i += 1
 
88
            if (i % 100) == 0:
 
89
                p('revision %d/%d file text %d/%d' % (revno, revcount, i, len_inv))
 
90
 
 
91
            ie = inv[file_id]
 
92
 
 
93
            if ie.parent_id != None:
 
94
                if ie.parent_id not in seen_ids:
 
95
                    bailout('missing parent {%s} in inventory for revision {%s}'
 
96
                            % (ie.parent_id, rid))
 
97
 
 
98
            if ie.kind == 'file':
 
99
                if ie.text_id in checked_texts:
 
100
                    fp = checked_texts[ie.text_id]
 
101
                else:
 
102
                    if not ie.text_id in branch.text_store:
 
103
                        bailout('text {%s} not in text_store' % ie.text_id)
 
104
 
 
105
                    tf = branch.text_store[ie.text_id]
 
106
                    fp = osutils.fingerprint_file(tf)
 
107
                    checked_texts[ie.text_id] = fp
 
108
 
 
109
                if ie.text_size != fp['size']:
 
110
                    bailout('text {%s} wrong size' % ie.text_id)
 
111
                if ie.text_sha1 != fp['sha1']:
 
112
                    bailout('text {%s} wrong sha1' % ie.text_id)
 
113
            elif ie.kind == 'directory':
 
114
                if ie.text_sha1 != None or ie.text_size != None or ie.text_id != None:
 
115
                    bailout('directory {%s} has text in revision {%s}'
 
116
                            % (file_id, rid))
 
117
 
 
118
        p('revision %d/%d file paths' % (revno, revcount))
 
119
        for path, ie in inv.iter_entries():
 
120
            if path in seen_names:
 
121
                bailout('duplicated path %r in inventory for revision {%s}' % (path, revid))
 
122
            seen_names.add(path)
 
123
 
 
124
 
 
125
    p('done')
 
126
    if progress:
 
127
        print 
 
128
    print 'checked %d revisions, %d file texts' % (revcount, len(checked_texts))
 
129