/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: mbp at sourcefrog
  • Date: 2005-03-29 00:23:20 UTC
  • Revision ID: mbp@sourcefrog.net-20050329002320-b494544b3ff546c9
- check file text for past revisions is correct
- new fingerprint_file function

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