~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/check.py

  • Committer: Martin Pool
  • Date: 2005-06-11 01:52:47 UTC
  • Revision ID: mbp@sourcefrog.net-20050611015247-bb9c42191cfb743b
- check command now also checks new inventory_sha1 and 
 precursor_sha1 
- check has --update option to fix these if they're missing
  patch from John A Meinel

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 
19
19
 
20
20
 
21
 
def check(branch):
 
21
def check(branch, update=False):
22
22
    """Run consistency checks on a branch.
 
23
 
 
24
    If update is True, for revisions missing certain information
 
25
    (right now this is inventory_sha1 and revision_sha1),
 
26
    update them to include the appropriate values.
23
27
    """
24
28
    import sys
25
29
 
33
37
    pb = ProgressBar(show_spinner=True)
34
38
    last_ptr = None
35
39
    checked_revs = {}
 
40
 
 
41
    missing_inventory_sha_cnt = 0
36
42
    
37
43
    history = branch.revision_history()
38
44
    revno = 0
39
45
    revcount = len(history)
40
46
 
41
47
    checked_texts = {}
 
48
 
 
49
    updated_revisions = []
 
50
 
 
51
    # Set to True in the case that the previous revision entry
 
52
    # was updated, since this will affect future revision entries
 
53
    updated_previous_revision = False
42
54
    
43
55
    for rid in history:
44
56
        revno += 1
56
68
 
57
69
        ## TODO: Check all the required fields are present on the revision.
58
70
 
 
71
        updated = False
 
72
        if rev.inventory_sha1:
 
73
            #mutter('    checking inventory hash {%s}' % rev.inventory_sha1)
 
74
            inv_sha1 = branch.get_inventory_sha1(rev.inventory_id)
 
75
            if inv_sha1 != rev.inventory_sha1:
 
76
                raise BzrCheckError('Inventory sha1 hash doesn\'t match'
 
77
                    ' value in revision {%s}' % rid)
 
78
        elif update:
 
79
            inv_sha1 = branch.get_inventory_sha1(rev.inventory_id)
 
80
            rev.inventory_sha1 = inv_sha1
 
81
            updated = True
 
82
        else:
 
83
            missing_inventory_sha_cnt += 1
 
84
            mutter("no inventory_sha1 on revision {%s}" % rid)
 
85
 
 
86
        if rev.precursor:
 
87
            if rev.precursor_sha1:
 
88
                precursor_sha1 = branch.get_revision_sha1(rev.precursor)
 
89
                if updated_previous_revision: 
 
90
                    # we don't expect the hashes to match, because
 
91
                    # we had to modify the previous revision_history entry.
 
92
                    rev.precursor_sha1 = precursor_sha1
 
93
                    updated = True
 
94
                else:
 
95
                    #mutter('    checking precursor hash {%s}' % rev.precursor_sha1)
 
96
                    if rev.precursor_sha1 != precursor_sha1:
 
97
                        raise BzrCheckError('Precursor sha1 hash doesn\'t match'
 
98
                            ' value in revision {%s}' % rid)
 
99
            elif update:
 
100
                precursor_sha1 = branch.get_revision_sha1(rev.precursor)
 
101
                rev.precursor_sha1 = precursor_sha1
 
102
                updated = True
 
103
 
 
104
        if updated:
 
105
            updated_previous_revision = True
 
106
            # We had to update this revision entries hashes
 
107
            # Now we need to write out a new value
 
108
            # This is a little bit invasive, since we are *rewriting* a
 
109
            # revision entry. I'm not supremely happy about it, but
 
110
            # there should be *some* way of making old entries have
 
111
            # the full meta information.
 
112
            import tempfile, os, errno
 
113
            rev_tmp = tempfile.TemporaryFile()
 
114
            rev.write_xml(rev_tmp)
 
115
            rev_tmp.seek(0)
 
116
 
 
117
            tmpfd, tmp_path = tempfile.mkstemp(prefix=rid, suffix='.gz',
 
118
                dir=branch.controlfilename('revision-store'))
 
119
            os.close(tmpfd)
 
120
            def special_rename(p1, p2):
 
121
                if sys.platform == 'win32':
 
122
                    try:
 
123
                        os.remove(p2)
 
124
                    except OSError, e:
 
125
                        if e.errno != e.ENOENT:
 
126
                            raise
 
127
                os.rename(p1, p2)
 
128
 
 
129
            try:
 
130
                # TODO: We may need to handle the case where the old revision
 
131
                # entry was not compressed (and thus did not end with .gz)
 
132
 
 
133
                # Remove the old revision entry out of the way
 
134
                rev_path = branch.controlfilename(['revision-store', rid+'.gz'])
 
135
                special_rename(rev_path, tmp_path)
 
136
                branch.revision_store.add(rev_tmp, rid) # Add the new one
 
137
                os.remove(tmp_path) # Remove the old name
 
138
                mutter('    Updated revision entry {%s}' % rid)
 
139
            except:
 
140
                # On any exception, restore the old entry
 
141
                special_rename(tmp_path, rev_path)
 
142
                raise
 
143
            rev_tmp.close()
 
144
            updated_revisions.append(rid)
 
145
        else:
 
146
            updated_previous_revision = False
 
147
            
59
148
        inv = branch.get_inventory(rev.inventory_id)
60
149
        seen_ids = {}
61
150
        seen_names = {}
112
201
 
113
202
 
114
203
    pb.clear()
 
204
 
115
205
    print 'checked %d revisions, %d file texts' % (revcount, len(checked_texts))
116
 
 
 
206
    if updated_revisions:
 
207
        print '%d revisions updated to current format' % len(updated_revisions)
 
208
    if missing_inventory_sha_cnt:
 
209
        print '%d revisions are missing inventory_sha1' % missing_inventory_sha_cnt
 
210
        print '  (use bzr check --update to fix them)'