1
# Copyright (C) 2004, 2005 by Martin Pool
2
# Copyright (C) 2005 by Canonical Ltd
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.
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.
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
22
"""Run consistency checks on a branch.
24
from bzrlib.trace import mutter
25
from bzrlib.errors import BzrCheckError
26
from bzrlib.osutils import fingerprint_file
27
from bzrlib.progress import ProgressBar
32
pb = ProgressBar(show_spinner=True)
36
missing_inventory_sha_cnt = 0
38
history = branch.revision_history()
40
revcount = len(history)
44
for rev_id in history:
46
pb.update('checking revision', revno, revcount)
47
mutter(' revision {%s}' % rev_id)
48
rev = branch.get_revision(rev_id)
49
if rev.revision_id != rev_id:
50
raise BzrCheckError('wrong internal revision id in revision {%s}' % rev_id)
51
if rev.precursor != last_ptr:
52
raise BzrCheckError('mismatched precursor in revision {%s}' % rev_id)
54
if rev_id in checked_revs:
55
raise BzrCheckError('repeated revision {%s}' % rev_id)
56
checked_revs[rev_id] = True
58
## TODO: Check all the required fields are present on the revision.
60
if rev.inventory_sha1:
61
inv_sha1 = branch.get_inventory_sha1(rev.inventory_id)
62
if inv_sha1 != rev.inventory_sha1:
63
raise BzrCheckError('Inventory sha1 hash doesn\'t match'
64
' value in revision {%s}' % rev_id)
66
missing_inventory_sha_cnt += 1
67
mutter("no inventory_sha1 on revision {%s}" % rev_id)
70
if rev.precursor_sha1:
71
precursor_sha1 = branch.get_revision_sha1(rev.precursor)
72
#mutter(' checking precursor hash {%s}' % rev.precursor_sha1)
73
if rev.precursor_sha1 != precursor_sha1:
74
raise BzrCheckError('Precursor sha1 hash doesn\'t match'
75
' value in revision {%s}' % rev_id)
77
inv = branch.get_inventory(rev.inventory_id)
81
## p('revision %d/%d file ids' % (revno, revcount))
83
if file_id in seen_ids:
84
raise BzrCheckError('duplicated file_id {%s} '
85
'in inventory for revision {%s}'
87
seen_ids[file_id] = True
97
if ie.parent_id != None:
98
if ie.parent_id not in seen_ids:
99
raise BzrCheckError('missing parent {%s} in inventory for revision {%s}'
100
% (ie.parent_id, rev_id))
102
if ie.kind == 'file':
103
if ie.text_id in checked_texts:
104
fp = checked_texts[ie.text_id]
106
if not ie.text_id in branch.text_store:
107
raise BzrCheckError('text {%s} not in text_store' % ie.text_id)
109
tf = branch.text_store[ie.text_id]
110
fp = fingerprint_file(tf)
111
checked_texts[ie.text_id] = fp
113
if ie.text_size != fp['size']:
114
raise BzrCheckError('text {%s} wrong size' % ie.text_id)
115
if ie.text_sha1 != fp['sha1']:
116
raise BzrCheckError('text {%s} wrong sha1' % ie.text_id)
117
elif ie.kind == 'directory':
118
if ie.text_sha1 != None or ie.text_size != None or ie.text_id != None:
119
raise BzrCheckError('directory {%s} has text in revision {%s}'
123
for path, ie in inv.iter_entries():
124
if path in seen_names:
125
raise BzrCheckError('duplicated path %s '
126
'in inventory for revision {%s}'
128
seen_names[path] = True
135
print 'checked %d revisions, %d file texts' % (revcount, len(checked_texts))
136
if missing_inventory_sha_cnt:
137
print '%d revisions are missing inventory_sha1' % missing_inventory_sha_cnt
138
print ' (use "bzr upgrade" to fix them)'