/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-06-20 02:50:08 UTC
  • Revision ID: mbp@sourcefrog.net-20050620025008-43e29d64489fe946
- split out a new 'bzr upgrade' command separate from 
  (but based on) 'bzr check', so that the code in each is simpler

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
 
 
19
 
 
20
 
 
21
def check(branch):
 
22
    """Run consistency checks on a branch.
 
23
    """
 
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
 
28
 
 
29
    branch.lock_read()
 
30
 
 
31
    try:
 
32
        pb = ProgressBar(show_spinner=True)
 
33
        last_ptr = None
 
34
        checked_revs = {}
 
35
 
 
36
        missing_inventory_sha_cnt = 0
 
37
 
 
38
        history = branch.revision_history()
 
39
        revno = 0
 
40
        revcount = len(history)
 
41
 
 
42
        checked_texts = {}
 
43
 
 
44
        for rev_id in history:
 
45
            revno += 1
 
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)
 
53
            last_ptr = rev_id
 
54
            if rev_id in checked_revs:
 
55
                raise BzrCheckError('repeated revision {%s}' % rev_id)
 
56
            checked_revs[rev_id] = True
 
57
 
 
58
            ## TODO: Check all the required fields are present on the revision.
 
59
 
 
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)
 
65
            else:
 
66
                missing_inventory_sha_cnt += 1
 
67
                mutter("no inventory_sha1 on revision {%s}" % rev_id)
 
68
 
 
69
            if rev.precursor:
 
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)
 
76
 
 
77
            inv = branch.get_inventory(rev.inventory_id)
 
78
            seen_ids = {}
 
79
            seen_names = {}
 
80
 
 
81
            ## p('revision %d/%d file ids' % (revno, revcount))
 
82
            for file_id in inv:
 
83
                if file_id in seen_ids:
 
84
                    raise BzrCheckError('duplicated file_id {%s} '
 
85
                                        'in inventory for revision {%s}'
 
86
                                        % (file_id, rev_id))
 
87
                seen_ids[file_id] = True
 
88
 
 
89
            i = 0
 
90
            for file_id in inv:
 
91
                i += 1
 
92
                if i & 31 == 0:
 
93
                    pb.tick()
 
94
 
 
95
                ie = inv[file_id]
 
96
 
 
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))
 
101
 
 
102
                if ie.kind == 'file':
 
103
                    if ie.text_id in checked_texts:
 
104
                        fp = checked_texts[ie.text_id]
 
105
                    else:
 
106
                        if not ie.text_id in branch.text_store:
 
107
                            raise BzrCheckError('text {%s} not in text_store' % ie.text_id)
 
108
 
 
109
                        tf = branch.text_store[ie.text_id]
 
110
                        fp = fingerprint_file(tf)
 
111
                        checked_texts[ie.text_id] = fp
 
112
 
 
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}'
 
120
                                % (file_id, rev_id))
 
121
 
 
122
            pb.tick()
 
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}'
 
127
                                        % (path, rev_id))
 
128
            seen_names[path] = True
 
129
 
 
130
    finally:
 
131
        branch.unlock()
 
132
 
 
133
    pb.clear()
 
134
 
 
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)'