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