~bzr-pqm/bzr/bzr.dev

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