1
# Copyright (C) 2010-2011 Canonical Ltd
 
 
3
# This program is free software; you can redistribute it and/or modify
 
 
4
# it under the terms of the GNU General Public License as published by
 
 
5
# the Free Software Foundation; either version 2 of the License, or
 
 
6
# (at your option) any later version.
 
 
8
# This program is distributed in the hope that it will be useful,
 
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
 
11
# GNU General Public License for more details.
 
 
13
# You should have received a copy of the GNU General Public License
 
 
14
# along with this program; if not, write to the Free Software
 
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 
17
"""Check each CHK page to make sure it is in 'canonical' form.
 
 
21
from __future__ import absolute_import
 
 
36
class cmd_check_chk(commands.Command):
 
 
37
    """Check the CHK pages for canonical form.
 
 
42
    takes_options = ['directory', 'revision']
 
 
44
    def run(self, directory='.', revision=None):
 
 
45
        wt, branch, relpath = controldir.ControlDir.open_containing_tree_or_branch(
 
 
47
        factory = groupcompress.make_pack_factory(False, False, 1)
 
 
48
        t = transport.get_transport('memory:///')
 
 
50
        self.add_cleanup(branch.lock_read().unlock)
 
 
51
        repo = branch.repository
 
 
52
        if revision is None or len(revision) == 0:
 
 
53
            inv_keys = repo.inventories.keys()
 
 
54
            inv_ids = [k[-1] for k in inv_keys]
 
 
55
        elif len(revision) == 1:
 
 
56
            inv_ids = [revision[0].as_revision_id(branch)]
 
 
57
        elif len(revision) == 2:
 
 
59
            r1 = revision[0].as_revision_id(branch)
 
 
60
            r2 = revision[1].as_revision_id(branch)
 
 
61
            inv_ids = g.find_unique_ancestors(r2, [r1])
 
 
62
        pb = ui.ui_factory.nested_progress_bar()
 
 
63
        self.add_cleanup(pb.finished)
 
 
64
        for idx, inv in enumerate(repo.iter_inventories(inv_ids)):
 
 
65
            pb.update('checking', idx, len(inv_ids))
 
 
66
            d = dict(inv.id_to_entry.iteritems())
 
 
67
            test_key = chk_map.CHKMap.from_dict(
 
 
68
                vf, d, maximum_size=inv.id_to_entry._root_node._maximum_size,
 
 
69
                key_width=inv.id_to_entry._root_node._key_width,
 
 
70
                search_key_func=inv.id_to_entry._search_key_func)
 
 
71
            if inv.id_to_entry.key() != test_key:
 
 
72
                trace.warning('Failed for id_to_entry inv: %s'
 
 
74
            pid = inv.parent_id_basename_to_file_id
 
 
75
            d = dict(pid.iteritems())
 
 
76
            test_key = chk_map.CHKMap.from_dict(
 
 
77
                vf, d, maximum_size=pid._root_node._maximum_size,
 
 
78
                key_width=pid._root_node._key_width,
 
 
79
                search_key_func=pid._search_key_func)
 
 
80
            if pid.key() != test_key:
 
 
81
                trace.warning('Failed for parent_id_to_basename inv: %s'