~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/check.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2007-10-24 12:49:17 UTC
  • mfrom: (2935.1.1 ianc-integration)
  • Revision ID: pqm@pqm.ubuntu.com-20071024124917-xb75eckyxx6vkrlg
Makefile fixes - hooks.html generation & allow python to be overridden (Ian Clatworthy)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 by Canonical Ltd
 
1
# Copyright (C) 2005, 2006 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
32
32
# raising them.  If there's more than one exception it'd be good to see them
33
33
# all.
34
34
 
 
35
from bzrlib import errors
 
36
from bzrlib import repository as _mod_repository
 
37
from bzrlib import revision
35
38
from bzrlib.errors import BzrCheckError
36
39
import bzrlib.ui
37
 
from bzrlib.trace import note
 
40
from bzrlib.trace import log_error, note
38
41
 
39
42
class Check(object):
40
43
    """Check a repository"""
53
56
        # maps (file-id, version) -> sha1; used by InventoryFile._check
54
57
        self.checked_texts = {}
55
58
        self.checked_weaves = {}
 
59
        self.revision_versions = _mod_repository._RevisionTextVersionCache(
 
60
            self.repository)
 
61
        self.unreferenced_ancestors = set()
 
62
        self.inconsistent_parents = []
56
63
 
57
64
    def check(self):
58
65
        self.repository.lock_read()
59
66
        self.progress = bzrlib.ui.ui_factory.nested_progress_bar()
60
67
        try:
61
 
            self.progress.update('retrieving inventory', 0, 0)
 
68
            self.progress.update('retrieving inventory', 0, 2)
62
69
            # do not put in init, as it should be done with progess,
63
70
            # and inside the lock.
64
71
            self.inventory_weave = self.repository.get_inventory_weave()
 
72
            self.progress.update('checking revision graph', 1)
 
73
            self.check_revision_graph()
65
74
            self.plan_revisions()
66
75
            revno = 0
67
 
            self.check_weaves()
68
76
            while revno < len(self.planned_revisions):
69
77
                rev_id = self.planned_revisions[revno]
70
78
                self.progress.update('checking revision', revno,
71
79
                                     len(self.planned_revisions))
72
80
                revno += 1
73
81
                self.check_one_rev(rev_id)
 
82
            # check_weaves is done after the revision scan so that
 
83
            # revision_versions is pre-populated
 
84
            self.check_weaves()
74
85
        finally:
75
86
            self.progress.finished()
76
87
            self.repository.unlock()
77
88
 
 
89
    def check_revision_graph(self):
 
90
        if not self.repository.revision_graph_can_have_wrong_parents():
 
91
            # This check is not necessary.
 
92
            self.revs_with_bad_parents_in_index = None
 
93
            return
 
94
        bad_revisions = self.repository._find_inconsistent_revision_parents()
 
95
        self.revs_with_bad_parents_in_index = list(bad_revisions)
 
96
 
78
97
    def plan_revisions(self):
79
98
        repository = self.repository
80
 
        self.planned_revisions = set(repository.all_revision_ids())
 
99
        self.planned_revisions = repository.all_revision_ids()
81
100
        self.progress.clear()
82
101
        inventoried = set(self.inventory_weave.versions())
83
 
        awol = self.planned_revisions - inventoried
 
102
        awol = set(self.planned_revisions) - inventoried
84
103
        if len(awol) > 0:
85
104
            raise BzrCheckError('Stored revisions missing from inventory'
86
105
                '{%s}' % ','.join([f for f in awol]))
87
 
        self.planned_revisions = list(self.planned_revisions)
88
106
 
89
107
    def report_results(self, verbose):
90
108
        note('checked repository %s format %s',
91
109
             self.repository.bzrdir.root_transport,
92
110
             self.repository._format)
93
111
        note('%6d revisions', self.checked_rev_cnt)
 
112
        note('%6d file-ids', len(self.checked_weaves))
94
113
        note('%6d unique file texts', self.checked_text_cnt)
95
114
        note('%6d repeated file texts', self.repeated_text_cnt)
96
 
        note('%6d weaves', len(self.checked_weaves))
 
115
        note('%6d unreferenced text ancestors',
 
116
             len(self.unreferenced_ancestors))
97
117
        if self.missing_inventory_sha_cnt:
98
118
            note('%6d revisions are missing inventory_sha1',
99
119
                 self.missing_inventory_sha_cnt)
113
133
                    note('      %s should be in the ancestry for:', link)
114
134
                    for linker in linkers:
115
135
                        note('       * %s', linker)
 
136
            if verbose:
 
137
                for file_id, revision_id in self.unreferenced_ancestors:
 
138
                    log_error('unreferenced ancestor: {%s} in %s', revision_id,
 
139
                        file_id)
 
140
        if len(self.inconsistent_parents):
 
141
            note('%6d inconsistent parents', len(self.inconsistent_parents))
 
142
            if verbose:
 
143
                for info in self.inconsistent_parents:
 
144
                    revision_id, file_id, found_parents, correct_parents = info
 
145
                    note('      * %s version %s has parents %r '
 
146
                         'but should have %r'
 
147
                         % (file_id, revision_id, found_parents,
 
148
                             correct_parents))
 
149
        if self.revs_with_bad_parents_in_index:
 
150
            note('%6d revisions have incorrect parents in the revision index',
 
151
                 len(self.revs_with_bad_parents_in_index))
 
152
            if verbose:
 
153
                for item in self.revs_with_bad_parents_in_index:
 
154
                    revision_id, index_parents, actual_parents = item
 
155
                    note(
 
156
                        '       %s has wrong parents in index: '
 
157
                        '%r should be %r',
 
158
                        revision_id, index_parents, actual_parents)
116
159
 
117
160
    def check_one_rev(self, rev_id):
118
161
        """Check one revision.
156
199
        weave_ids = []
157
200
        if self.repository.weave_store.listable():
158
201
            weave_ids = list(self.repository.weave_store)
159
 
            n_weaves = len(weave_ids)
160
 
        self.progress.update('checking weave', 0, n_weaves)
 
202
            n_weaves = len(weave_ids) + 1
 
203
        self.progress.update('checking versionedfile', 0, n_weaves)
161
204
        self.inventory_weave.check(progress_bar=self.progress)
 
205
        files_in_revisions = {}
 
206
        revisions_of_files = {}
162
207
        for i, weave_id in enumerate(weave_ids):
163
 
            self.progress.update('checking weave', i, n_weaves)
 
208
            self.progress.update('checking versionedfile', i, n_weaves)
164
209
            w = self.repository.weave_store.get_weave(weave_id,
165
210
                    self.repository.get_transaction())
166
211
            # No progress here, because it looks ugly.
167
212
            w.check()
 
213
 
 
214
            weave_checker = self.repository.get_versioned_file_checker(
 
215
                self.planned_revisions, self.revision_versions)
 
216
            result = weave_checker.check_file_version_parents(w, weave_id)
 
217
 
 
218
            for revision_id, (weave_parents,correct_parents) in result.items():
 
219
                self.inconsistent_parents.append(
 
220
                    (revision_id, weave_id, weave_parents, correct_parents))
 
221
                unreferenced_parents = set(weave_parents)-set(correct_parents)
 
222
                for unreferenced_parent in unreferenced_parents:
 
223
                    self.unreferenced_ancestors.add(
 
224
                        (weave_id, unreferenced_parent))
168
225
            self.checked_weaves[weave_id] = True
169
226
 
170
227
    def _check_revision_tree(self, rev_id):
171
228
        tree = self.repository.revision_tree(rev_id)
 
229
        self.revision_versions.add_revision_text_versions(tree)
172
230
        inv = tree.inventory
173
231
        seen_ids = {}
174
232
        for file_id in inv:
204
262
        branch.unlock()
205
263
    branch_result.report_results(verbose)
206
264
    repo_result.report_results(verbose)
 
265
 
 
266
 
 
267