/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/check.py

  • Committer: Martin Pool
  • Date: 2005-09-22 06:54:01 UTC
  • Revision ID: mbp@sourcefrog.net-20050922065401-6694b0f910701fca
- try to avoid redundant conversion of strings when retrieving from weaves

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
# TODO: Check ancestries are correct for every revision: includes
 
19
# every committed so far, and in a reasonable order.
 
20
 
 
21
# TODO: Also check non-mainline revisions mentioned as parents.
 
22
 
 
23
# TODO: Check for extra files in the control directory.
 
24
 
 
25
# TODO: Check revision, inventory and entry objects have all 
 
26
# required fields.
 
27
 
 
28
 
 
29
import bzrlib.ui
 
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
 
36
 
 
37
 
 
38
class Check(object):
 
39
    """Check a branch"""
 
40
    def __init__(self, branch):
 
41
        self.branch = branch
 
42
        self.run()
 
43
 
 
44
 
 
45
    def run(self):
 
46
        branch = self.branch
 
47
 
 
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 = {}
 
55
 
 
56
        history = branch.revision_history()
 
57
        revno = 0
 
58
        revcount = len(history)
 
59
 
 
60
        last_rev_id = None
 
61
        self.progress = bzrlib.ui.ui_factory.progress_bar()
 
62
        for rev_id in history:
 
63
            self.progress.update('checking revision', revno, revcount)
 
64
            revno += 1
 
65
            self.check_one_rev(rev_id, last_rev_id)
 
66
            last_rev_id = rev_id
 
67
        self.progress.clear()
 
68
        self.report_results()
 
69
 
 
70
 
 
71
    def report_results(self):
 
72
        note('checked branch %s format %d',
 
73
             self.branch.base, 
 
74
             self.branch._branch_format)
 
75
 
 
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)
 
85
 
 
86
 
 
87
    def check_one_rev(self, rev_id, last_rev_id):
 
88
        """Check one revision.
 
89
 
 
90
        rev_id - the one to check
 
91
 
 
92
        last_rev_id - the previous one on the mainline, if any.
 
93
        """
 
94
 
 
95
        # mutter('    revision {%s}' % rev_id)
 
96
        branch = self.branch
 
97
        rev = branch.get_revision(rev_id)
 
98
        if rev.revision_id != rev_id:
 
99
            raise BzrCheckError('wrong internal revision id in revision {%s}'
 
100
                                % rev_id)
 
101
 
 
102
        # check the previous history entry is a parent of this entry
 
103
        if rev.parent_ids:
 
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:
 
110
                    break
 
111
            else:
 
112
                raise BzrCheckError("previous revision {%s} not listed among "
 
113
                                    "parents of {%s}"
 
114
                                    % (last_rev_id, rev_id))
 
115
        elif last_rev_id:
 
116
            raise BzrCheckError("revision {%s} has no parents listed "
 
117
                                "but preceded by {%s}"
 
118
                                % (rev_id, last_rev_id))
 
119
 
 
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)
 
125
        else:
 
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
 
130
 
 
131
    def _check_revision_tree(self, rev_id):
 
132
        tree = self.branch.revision_tree(rev_id)
 
133
        inv = tree.inventory
 
134
        seen_ids = {}
 
135
        for file_id in inv:
 
136
            if file_id in seen_ids:
 
137
                raise BzrCheckError('duplicated file_id {%s} '
 
138
                                    'in inventory for revision {%s}'
 
139
                                    % (file_id, rev_id))
 
140
            seen_ids[file_id] = True
 
141
        for file_id in inv:
 
142
            self._check_one_entry(rev_id, inv, tree, file_id)
 
143
        seen_names = {}
 
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}'
 
148
                                    % (path, rev_id))
 
149
            seen_names[path] = True
 
150
 
 
151
        
 
152
    def _check_one_entry(self, rev_id, inv, tree, file_id):
 
153
        ie = inv[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}' %
 
165
                                        (file_id, rev_id))
 
166
                else:
 
167
                    self.repeated_text_cnt += 1
 
168
                    return
 
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}'
 
180
                        % (file_id, rev_id))
 
181
        elif ie.kind == 'root_directory':
 
182
            pass
 
183
        else:
 
184
            raise BzrCheckError('unknown entry kind %r in revision {%s}' % 
 
185
                                (ie.kind, rev_id))
 
186
 
 
187
 
 
188
def check(branch):
 
189
    """Run consistency checks on a branch."""
 
190
    Check(branch)