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
18
# TODO: Check ancestries are correct for every revision: includes
19
# every committed so far, and in a reasonable order.
21
# TODO: Also check non-mainline revisions mentioned as parents.
23
# TODO: Check for extra files in the control directory.
25
# TODO: Check revision, inventory and entry objects have all
30
from bzrlib.trace import note, warning
31
from bzrlib.osutils import rename, sha_string, fingerprint_file, sha_strings
32
from bzrlib.trace import mutter
33
from bzrlib.errors import BzrCheckError, NoSuchRevision
34
from bzrlib.inventory import ROOT_ID
35
from bzrlib.branch import gen_root_id
40
def __init__(self, branch):
48
self.checked_text_cnt = 0
49
self.checked_rev_cnt = 0
50
self.repeated_text_cnt = 0
51
self.missing_inventory_sha_cnt = 0
52
self.missing_revision_cnt = 0
53
# maps (file-id, version) -> sha1
54
self.checked_texts = {}
56
history = branch.revision_history()
58
revcount = len(history)
61
self.progress = bzrlib.ui.ui_factory.progress_bar()
62
for rev_id in history:
63
self.progress.update('checking revision', revno, revcount)
65
self.check_one_rev(rev_id, last_rev_id)
71
def report_results(self):
72
note('checked branch %s format %d',
74
self.branch._branch_format)
76
note('%6d revisions', self.checked_rev_cnt)
77
note('%6d unique file texts', self.checked_text_cnt)
78
note('%6d repeated file texts', self.repeated_text_cnt)
79
if self.missing_inventory_sha_cnt:
80
note('%d revisions are missing inventory_sha1',
81
self.missing_inventory_sha_cnt)
82
if self.missing_revision_cnt:
83
note('%d revisions are mentioned but not present',
84
self.missing_revision_cnt)
87
def check_one_rev(self, rev_id, last_rev_id):
88
"""Check one revision.
90
rev_id - the one to check
92
last_rev_id - the previous one on the mainline, if any.
95
# mutter(' revision {%s}' % rev_id)
97
rev = branch.get_revision(rev_id)
98
if rev.revision_id != rev_id:
99
raise BzrCheckError('wrong internal revision id in revision {%s}'
102
# check the previous history entry is a parent of this entry
104
if last_rev_id is None:
105
raise BzrCheckError("revision {%s} has %d parents, but is the "
106
"start of the branch"
107
% (rev_id, len(rev.parent_ids)))
108
for parent_id in rev.parent_ids:
109
if parent_id == last_rev_id:
112
raise BzrCheckError("previous revision {%s} not listed among "
114
% (last_rev_id, rev_id))
116
raise BzrCheckError("revision {%s} has no parents listed "
117
"but preceded by {%s}"
118
% (rev_id, last_rev_id))
120
if rev.inventory_sha1:
121
inv_sha1 = branch.get_inventory_sha1(rev_id)
122
if inv_sha1 != rev.inventory_sha1:
123
raise BzrCheckError('Inventory sha1 hash doesn\'t match'
124
' value in revision {%s}' % rev_id)
126
missing_inventory_sha_cnt += 1
127
mutter("no inventory_sha1 on revision {%s}" % rev_id)
128
self._check_revision_tree(rev_id)
129
self.checked_rev_cnt += 1
131
def _check_revision_tree(self, rev_id):
132
tree = self.branch.revision_tree(rev_id)
136
if file_id in seen_ids:
137
raise BzrCheckError('duplicated file_id {%s} '
138
'in inventory for revision {%s}'
140
seen_ids[file_id] = True
142
self._check_one_entry(rev_id, inv, tree, file_id)
144
for path, ie in inv.iter_entries():
145
if path in seen_names:
146
raise BzrCheckError('duplicated path %s '
147
'in inventory for revision {%s}'
149
seen_names[path] = True
152
def _check_one_entry(self, rev_id, inv, tree, file_id):
154
if ie.parent_id != None:
155
if not inv.has_id(ie.parent_id):
156
raise BzrCheckError('missing parent {%s} in inventory for revision {%s}'
157
% (ie.parent_id, rev_id))
158
if ie.kind == 'file':
159
text_version = ie.text_version
160
t = (file_id, text_version)
161
if t in self.checked_texts:
162
prev_sha = self.checked_texts[t]
163
if prev_sha != ie.text_sha1:
164
raise BzrCheckError('mismatched sha1 on {%s} in {%s}' %
167
self.repeated_text_cnt += 1
169
mutter('check version {%s} of {%s}', rev_id, file_id)
170
file_lines = tree.get_file_lines(file_id)
171
self.checked_text_cnt += 1
172
if ie.text_size != sum(map(len, file_lines)):
173
raise BzrCheckError('text {%s} wrong size' % ie.text_id)
174
if ie.text_sha1 != sha_strings(file_lines):
175
raise BzrCheckError('text {%s} wrong sha1' % ie.text_id)
176
self.checked_texts[t] = ie.text_sha1
177
elif ie.kind == 'directory':
178
if ie.text_sha1 != None or ie.text_size != None or ie.text_id != None:
179
raise BzrCheckError('directory {%s} has text in revision {%s}'
181
elif ie.kind == 'root_directory':
184
raise BzrCheckError('unknown entry kind %r in revision {%s}' %
189
"""Run consistency checks on a branch."""