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