22
"""Run consistency checks on a branch.
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, Progress
20
######################################################################
26
from trace import mutter
27
from errors import bailout
30
def check(branch, progress=True):
34
if not (hasattr(out, 'isatty') and out.isatty()):
39
mutter('checking ' + m)
40
out.write('\rchecking: %-50.50s' % m)
44
mutter('checking ' + m)
46
p('history of %r' % branch.base)
37
50
history = branch.revision_history()
43
56
for rid in history:
45
pb(Progress('revision', revno, revcount))
58
p('revision %d/%d' % (revno, revcount))
46
59
mutter(' revision {%s}' % rid)
47
60
rev = branch.get_revision(rid)
48
61
if rev.revision_id != rid:
49
raise BzrCheckError('wrong internal revision id in revision {%s}' % rid)
62
bailout('wrong internal revision id in revision {%s}' % rid)
50
63
if rev.precursor != last_ptr:
51
raise BzrCheckError('mismatched precursor in revision {%s}' % rid)
64
bailout('mismatched precursor in revision {%s}' % rid)
53
66
if rid in checked_revs:
54
raise BzrCheckError('repeated revision {%s}' % rid)
55
checked_revs[rid] = True
67
bailout('repeated revision {%s}' % rid)
57
70
## TODO: Check all the required fields are present on the revision.
59
72
inv = branch.get_inventory(rev.inventory_id)
63
## p('revision %d/%d file ids' % (revno, revcount))
76
p('revision %d/%d file ids' % (revno, revcount))
64
77
for file_id in inv:
65
78
if file_id in seen_ids:
66
raise BzrCheckError('duplicated file_id {%s} '
67
'in inventory for revision {%s}'
69
seen_ids[file_id] = True
79
bailout('duplicated file_id {%s} in inventory for revision {%s}'
73
85
for file_id in inv:
76
# p('revision %d/%d file text %d/%d' % (revno, revcount, i, len_inv))
88
p('revision %d/%d file text %d/%d' % (revno, revcount, i, len_inv))
80
92
if ie.parent_id != None:
81
93
if ie.parent_id not in seen_ids:
82
raise BzrCheckError('missing parent {%s} in inventory for revision {%s}'
94
bailout('missing parent {%s} in inventory for revision {%s}'
83
95
% (ie.parent_id, rid))
85
97
if ie.kind == 'file':
87
99
fp = checked_texts[ie.text_id]
89
101
if not ie.text_id in branch.text_store:
90
raise BzrCheckError('text {%s} not in text_store' % ie.text_id)
102
bailout('text {%s} not in text_store' % ie.text_id)
92
104
tf = branch.text_store[ie.text_id]
93
fp = fingerprint_file(tf)
105
fp = osutils.fingerprint_file(tf)
94
106
checked_texts[ie.text_id] = fp
96
108
if ie.text_size != fp['size']:
97
raise BzrCheckError('text {%s} wrong size' % ie.text_id)
109
bailout('text {%s} wrong size' % ie.text_id)
98
110
if ie.text_sha1 != fp['sha1']:
99
raise BzrCheckError('text {%s} wrong sha1' % ie.text_id)
111
bailout('text {%s} wrong sha1' % ie.text_id)
100
112
elif ie.kind == 'directory':
101
113
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}'
114
bailout('directory {%s} has text in revision {%s}'
103
115
% (file_id, rid))
105
# p('revision %d/%d file paths' % (revno, revcount))
117
p('revision %d/%d file paths' % (revno, revcount))
106
118
for path, ie in inv.iter_entries():
107
119
if path in seen_names:
108
raise BzrCheckError('duplicated path %r '
109
'in inventory for revision {%s}'
111
seen_names[path] = True
120
bailout('duplicated path %r in inventory for revision {%s}' % (path, revid))
115
127
print 'checked %d revisions, %d file texts' % (revcount, len(checked_texts))