~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/check.py

  • Committer: Martin Pool
  • Date: 2005-06-10 09:42:14 UTC
  • Revision ID: mbp@sourcefrog.net-20050610094214-644149de33083153
- fix sweeping bar progress indicator

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#! /usr/bin/python
2
 
 
3
 
 
4
1
# Copyright (C) 2004, 2005 by Martin Pool
5
2
# Copyright (C) 2005 by Canonical Ltd
6
3
 
7
 
 
8
4
# This program is free software; you can redistribute it and/or modify
9
5
# it under the terms of the GNU General Public License as published by
10
6
# the Free Software Foundation; either version 2 of the License, or
21
17
 
22
18
 
23
19
 
24
 
######################################################################
25
 
# consistency checks
26
 
 
27
 
def check():
28
 
    """Consistency check of tree."""
29
 
    assert_in_tree()
30
 
    mutter("checking tree")
31
 
    check_patches_exist()
32
 
    check_patch_chaining()
33
 
    check_patch_uniqueness()
34
 
    check_inventory()
35
 
    mutter("tree looks OK")
36
 
    ## TODO: Check that previous-inventory and previous-manifest
37
 
    ## are the same as those stored in the previous changeset.
38
 
 
39
 
    ## TODO: Check all patches present in patch directory are
40
 
    ## mentioned in patch history; having an orphaned patch only gives
41
 
    ## a warning.
42
 
 
43
 
    ## TODO: Check cached data is consistent with data reconstructed
44
 
    ## from scratch.
45
 
 
46
 
    ## TODO: Check no control files are versioned.
47
 
 
48
 
    ## TODO: Check that the before-hash of each file in a later
49
 
    ## revision matches the after-hash in the previous revision to
50
 
    ## touch it.
51
 
 
52
 
 
53
 
def check_inventory():
54
 
    mutter("checking inventory file and ids...")
55
 
    seen_ids = Set()
56
 
    seen_names = Set()
57
 
    
58
 
    for l in controlfile('inventory').readlines():
59
 
        parts = l.split()
60
 
        if len(parts) != 2:
61
 
            bailout("malformed inventory line: " + `l`)
62
 
        file_id, name = parts
63
 
        
64
 
        if file_id in seen_ids:
65
 
            bailout("duplicated file id " + file_id)
66
 
        seen_ids.add(file_id)
67
 
 
68
 
        if name in seen_names:
69
 
            bailout("duplicated file name in inventory: " + quotefn(name))
70
 
        seen_names.add(name)
71
 
        
72
 
        if is_control_file(name):
73
 
            raise BzrError("control file %s present in inventory" % quotefn(name))
74
 
 
75
 
 
76
 
def check_patches_exist():
77
 
    """Check constraint of current version: all patches exist"""
78
 
    mutter("checking all patches are present...")
79
 
    for pid in revision_history():
80
 
        read_patch_header(pid)
81
 
 
82
 
 
83
 
def check_patch_chaining():
84
 
    """Check ancestry of patches and history file is consistent"""
85
 
    mutter("checking patch chaining...")
86
 
    prev = None
87
 
    for pid in revision_history():
88
 
        log_prev = read_patch_header(pid).precursor
89
 
        if log_prev != prev:
90
 
            bailout("inconsistent precursor links on " + pid)
91
 
        prev = pid
92
 
 
93
 
 
94
 
def check_patch_uniqueness():
95
 
    """Make sure no patch is listed twice in the history.
96
 
 
97
 
    This should be implied by having correct ancestry but I'll check it
98
 
    anyhow."""
99
 
    mutter("checking history for duplicates...")
100
 
    seen = Set()
101
 
    for pid in revision_history():
102
 
        if pid in seen:
103
 
            bailout("patch " + pid + " appears twice in history")
104
 
        seen.add(pid)
105
 
        
 
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
 
30
    
 
31
    out = sys.stdout
 
32
 
 
33
    pb = ProgressBar(show_spinner=True)
 
34
    last_ptr = None
 
35
    checked_revs = {}
 
36
    
 
37
    history = branch.revision_history()
 
38
    revno = 0
 
39
    revcount = len(history)
 
40
 
 
41
    checked_texts = {}
 
42
    
 
43
    for rid in history:
 
44
        revno += 1
 
45
        pb.update('checking revision', revno, revcount)
 
46
        mutter('    revision {%s}' % rid)
 
47
        rev = branch.get_revision(rid)
 
48
        if rev.revision_id != rid:
 
49
            raise BzrCheckError('wrong internal revision id in revision {%s}' % rid)
 
50
        if rev.precursor != last_ptr:
 
51
            raise BzrCheckError('mismatched precursor in revision {%s}' % rid)
 
52
        last_ptr = rid
 
53
        if rid in checked_revs:
 
54
            raise BzrCheckError('repeated revision {%s}' % rid)
 
55
        checked_revs[rid] = True
 
56
 
 
57
        ## TODO: Check all the required fields are present on the revision.
 
58
 
 
59
        inv = branch.get_inventory(rev.inventory_id)
 
60
        seen_ids = {}
 
61
        seen_names = {}
 
62
 
 
63
        ## p('revision %d/%d file ids' % (revno, revcount))
 
64
        for file_id in inv:
 
65
            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
 
70
 
 
71
        i = 0
 
72
        len_inv = len(inv)
 
73
        for file_id in inv:
 
74
            i += 1
 
75
            if i & 31 == 0:
 
76
                pb.tick()
 
77
 
 
78
            ie = inv[file_id]
 
79
 
 
80
            if ie.parent_id != None:
 
81
                if ie.parent_id not in seen_ids:
 
82
                    raise BzrCheckError('missing parent {%s} in inventory for revision {%s}'
 
83
                            % (ie.parent_id, rid))
 
84
 
 
85
            if ie.kind == 'file':
 
86
                if ie.text_id in checked_texts:
 
87
                    fp = checked_texts[ie.text_id]
 
88
                else:
 
89
                    if not ie.text_id in branch.text_store:
 
90
                        raise BzrCheckError('text {%s} not in text_store' % ie.text_id)
 
91
 
 
92
                    tf = branch.text_store[ie.text_id]
 
93
                    fp = fingerprint_file(tf)
 
94
                    checked_texts[ie.text_id] = fp
 
95
 
 
96
                if ie.text_size != fp['size']:
 
97
                    raise BzrCheckError('text {%s} wrong size' % ie.text_id)
 
98
                if ie.text_sha1 != fp['sha1']:
 
99
                    raise BzrCheckError('text {%s} wrong sha1' % ie.text_id)
 
100
            elif ie.kind == 'directory':
 
101
                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}'
 
103
                            % (file_id, rid))
 
104
 
 
105
        pb.tick()
 
106
        for path, ie in inv.iter_entries():
 
107
            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()
 
115
    print 'checked %d revisions, %d file texts' % (revcount, len(checked_texts))
106
116