/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1 by mbp at sourcefrog
import from baz patch-364
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
703 by Martin Pool
- split out a new 'bzr upgrade' command separate from
21
def check(branch):
654 by Martin Pool
- update check command to use aaron's progress code
22
    """Run consistency checks on a branch.
23
    """
651 by Martin Pool
- clean up imports
24
    from bzrlib.trace import mutter
25
    from bzrlib.errors import BzrCheckError
26
    from bzrlib.osutils import fingerprint_file
658 by Martin Pool
- clean up and add a bunch of options to the progress indicator
27
    from bzrlib.progress import ProgressBar
676 by Martin Pool
- lock branch while checking
28
703 by Martin Pool
- split out a new 'bzr upgrade' command separate from
29
    branch.lock_read()
676 by Martin Pool
- lock branch while checking
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
703 by Martin Pool
- split out a new 'bzr upgrade' command separate from
44
        for rev_id in history:
676 by Martin Pool
- lock branch while checking
45
            revno += 1
46
            pb.update('checking revision', revno, revcount)
703 by Martin Pool
- split out a new 'bzr upgrade' command separate from
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)
676 by Martin Pool
- lock branch while checking
51
            if rev.precursor != last_ptr:
703 by Martin Pool
- split out a new 'bzr upgrade' command separate from
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
676 by Martin Pool
- lock branch while checking
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'
703 by Martin Pool
- split out a new 'bzr upgrade' command separate from
64
                        ' value in revision {%s}' % rev_id)
676 by Martin Pool
- lock branch while checking
65
            else:
66
                missing_inventory_sha_cnt += 1
703 by Martin Pool
- split out a new 'bzr upgrade' command separate from
67
                mutter("no inventory_sha1 on revision {%s}" % rev_id)
676 by Martin Pool
- lock branch while checking
68
69
            if rev.precursor:
70
                if rev.precursor_sha1:
71
                    precursor_sha1 = branch.get_revision_sha1(rev.precursor)
703 by Martin Pool
- split out a new 'bzr upgrade' command separate from
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)
676 by Martin Pool
- lock branch while checking
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}'
703 by Martin Pool
- split out a new 'bzr upgrade' command separate from
86
                                        % (file_id, rev_id))
676 by Martin Pool
- lock branch while checking
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}'
703 by Martin Pool
- split out a new 'bzr upgrade' command separate from
100
                                % (ie.parent_id, rev_id))
676 by Martin Pool
- lock branch while checking
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}'
703 by Martin Pool
- split out a new 'bzr upgrade' command separate from
120
                                % (file_id, rev_id))
676 by Martin Pool
- lock branch while checking
121
122
            pb.tick()
123
            for path, ie in inv.iter_entries():
124
                if path in seen_names:
703 by Martin Pool
- split out a new 'bzr upgrade' command separate from
125
                    raise BzrCheckError('duplicated path %s '
676 by Martin Pool
- lock branch while checking
126
                                        'in inventory for revision {%s}'
703 by Martin Pool
- split out a new 'bzr upgrade' command separate from
127
                                        % (path, rev_id))
543 by Martin Pool
- More cleanups for set type
128
            seen_names[path] = True
125 by mbp at sourcefrog
- check progress indicator for file texts
129
676 by Martin Pool
- lock branch while checking
130
    finally:
131
        branch.unlock()
121 by mbp at sourcefrog
- progress indicator while checking
132
654 by Martin Pool
- update check command to use aaron's progress code
133
    pb.clear()
674 by Martin Pool
- check command now also checks new inventory_sha1 and
134
248 by mbp at sourcefrog
- Better progress and completion indicator from check command
135
    print 'checked %d revisions, %d file texts' % (revcount, len(checked_texts))
674 by Martin Pool
- check command now also checks new inventory_sha1 and
136
    if missing_inventory_sha_cnt:
137
        print '%d revisions are missing inventory_sha1' % missing_inventory_sha_cnt
703 by Martin Pool
- split out a new 'bzr upgrade' command separate from
138
        print '  (use "bzr upgrade" to fix them)'