~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/check.py

  • Committer: Martin Pool
  • Date: 2005-05-05 06:38:18 UTC
  • Revision ID: mbp@sourcefrog.net-20050505063818-3eb3260343878325
- do upload CHANGELOG to web server, even though it's autogenerated

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
 
19
19
 
20
 
 
21
 
def check(branch):
22
 
    """Run consistency checks on a branch.
23
 
    """
24
 
    import sys
25
 
 
26
 
    from bzrlib.trace import mutter
27
 
    from bzrlib.errors import BzrCheckError
28
 
    from bzrlib.osutils import fingerprint_file
29
 
    from bzrlib.progress import ProgressBar, Progress
30
 
    
 
20
######################################################################
 
21
# consistency checks
 
22
 
 
23
import sys
 
24
from sets import Set
 
25
 
 
26
from trace import mutter
 
27
from errors import bailout
 
28
import osutils
 
29
 
 
30
def check(branch, progress=True):
31
31
    out = sys.stdout
32
32
 
33
 
    pb = ProgressBar()
 
33
    # TODO: factor out
 
34
    if not (hasattr(out, 'isatty') and out.isatty()):
 
35
        progress=False
 
36
 
 
37
    if progress:
 
38
        def p(m):
 
39
            mutter('checking ' + m)
 
40
            out.write('\rchecking: %-50.50s' % m)
 
41
            out.flush()
 
42
    else:
 
43
        def p(m):
 
44
            mutter('checking ' + m)
 
45
 
 
46
    p('history of %r' % branch.base)
34
47
    last_ptr = None
35
 
    checked_revs = {}
 
48
    checked_revs = Set()
36
49
    
37
50
    history = branch.revision_history()
38
51
    revno = 0
42
55
    
43
56
    for rid in history:
44
57
        revno += 1
45
 
        pb(Progress('revision', revno, revcount))
 
58
        p('revision %d/%d' % (revno, revcount))
46
59
        mutter('    revision {%s}' % rid)
47
60
        rev = branch.get_revision(rid)
48
61
        if rev.revision_id != rid:
49
 
            raise BzrCheckError('wrong internal revision id in revision {%s}' % rid)
 
62
            bailout('wrong internal revision id in revision {%s}' % rid)
50
63
        if rev.precursor != last_ptr:
51
 
            raise BzrCheckError('mismatched precursor in revision {%s}' % rid)
 
64
            bailout('mismatched precursor in revision {%s}' % rid)
52
65
        last_ptr = rid
53
66
        if rid in checked_revs:
54
 
            raise BzrCheckError('repeated revision {%s}' % rid)
55
 
        checked_revs[rid] = True
 
67
            bailout('repeated revision {%s}' % rid)
 
68
        checked_revs.add(rid)
56
69
 
57
70
        ## TODO: Check all the required fields are present on the revision.
58
71
 
59
72
        inv = branch.get_inventory(rev.inventory_id)
60
 
        seen_ids = {}
61
 
        seen_names = {}
 
73
        seen_ids = Set()
 
74
        seen_names = Set()
62
75
 
63
 
        ## p('revision %d/%d file ids' % (revno, revcount))
 
76
        p('revision %d/%d file ids' % (revno, revcount))
64
77
        for file_id in inv:
65
78
            if file_id in seen_ids:
66
 
                raise BzrCheckError('duplicated file_id {%s} '
67
 
                                    'in inventory for revision {%s}'
68
 
                                    % (file_id, rid))
69
 
            seen_ids[file_id] = True
 
79
                bailout('duplicated file_id {%s} in inventory for revision {%s}'
 
80
                        % (file_id, rid))
 
81
            seen_ids.add(file_id)
70
82
 
71
83
        i = 0
72
84
        len_inv = len(inv)
73
85
        for file_id in inv:
74
86
            i += 1
75
 
            #if (i % 100) == 0:
76
 
            #    p('revision %d/%d file text %d/%d' % (revno, revcount, i, len_inv))
 
87
            if (i % 100) == 0:
 
88
                p('revision %d/%d file text %d/%d' % (revno, revcount, i, len_inv))
77
89
 
78
90
            ie = inv[file_id]
79
91
 
80
92
            if ie.parent_id != None:
81
93
                if ie.parent_id not in seen_ids:
82
 
                    raise BzrCheckError('missing parent {%s} in inventory for revision {%s}'
 
94
                    bailout('missing parent {%s} in inventory for revision {%s}'
83
95
                            % (ie.parent_id, rid))
84
96
 
85
97
            if ie.kind == 'file':
87
99
                    fp = checked_texts[ie.text_id]
88
100
                else:
89
101
                    if not ie.text_id in branch.text_store:
90
 
                        raise BzrCheckError('text {%s} not in text_store' % ie.text_id)
 
102
                        bailout('text {%s} not in text_store' % ie.text_id)
91
103
 
92
104
                    tf = branch.text_store[ie.text_id]
93
 
                    fp = fingerprint_file(tf)
 
105
                    fp = osutils.fingerprint_file(tf)
94
106
                    checked_texts[ie.text_id] = fp
95
107
 
96
108
                if ie.text_size != fp['size']:
97
 
                    raise BzrCheckError('text {%s} wrong size' % ie.text_id)
 
109
                    bailout('text {%s} wrong size' % ie.text_id)
98
110
                if ie.text_sha1 != fp['sha1']:
99
 
                    raise BzrCheckError('text {%s} wrong sha1' % ie.text_id)
 
111
                    bailout('text {%s} wrong sha1' % ie.text_id)
100
112
            elif ie.kind == 'directory':
101
113
                if ie.text_sha1 != None or ie.text_size != None or ie.text_id != None:
102
 
                    raise BzrCheckError('directory {%s} has text in revision {%s}'
 
114
                    bailout('directory {%s} has text in revision {%s}'
103
115
                            % (file_id, rid))
104
116
 
105
 
        # p('revision %d/%d file paths' % (revno, revcount))
 
117
        p('revision %d/%d file paths' % (revno, revcount))
106
118
        for path, ie in inv.iter_entries():
107
119
            if path in seen_names:
108
 
                raise BzrCheckError('duplicated path %r '
109
 
                                    'in inventory for revision {%s}'
110
 
                                    % (path, revid))
111
 
            seen_names[path] = True
112
 
 
113
 
 
114
 
    pb.clear()
 
120
                bailout('duplicated path %r in inventory for revision {%s}' % (path, revid))
 
121
            seen_names.add(path)
 
122
 
 
123
 
 
124
    p('done')
 
125
    if progress:
 
126
        print 
115
127
    print 'checked %d revisions, %d file texts' % (revcount, len(checked_texts))
116
128