~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/upgrade.py

  • Committer: Martin Pool
  • Date: 2005-06-20 03:45:02 UTC
  • Revision ID: mbp@sourcefrog.net-20050620034502-5a07cbdd4976a9e5
- bzr upgrade updates or checks SHA1 on all predecessor revisions

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
    If they change, their SHA-1 will of course change, which might
28
28
    break any later signatures, or backreferences that check the
29
29
    SHA-1.
 
30
 
 
31
    TODO: Check non-mainline revisions.
30
32
    """
31
33
    import sys
32
34
 
38
40
 
39
41
    try:
40
42
        pb = ProgressBar(show_spinner=True)
41
 
        last_ptr = None
42
 
        checked_revs = {}
 
43
        last_rev_id = None
43
44
 
44
45
        history = branch.revision_history()
45
46
        revno = 0
51
52
        # was updated, since this will affect future revision entries
52
53
        updated_previous_revision = False
53
54
 
54
 
        for rid in history:
 
55
        for rev_id in history:
55
56
            revno += 1
56
57
            pb.update('upgrading revision', revno, revcount)
57
 
            mutter('    revision {%s}' % rid)
58
 
            rev = branch.get_revision(rid)
59
 
            if rev.revision_id != rid:
60
 
                raise BzrCheckError('wrong internal revision id in revision {%s}' % rid)
61
 
            if rev.precursor != last_ptr:
62
 
                raise BzrCheckError('mismatched precursor in revision {%s}' % rid)
63
 
            last_ptr = rid
64
 
            if rid in checked_revs:
65
 
                raise BzrCheckError('repeated revision {%s}' % rid)
66
 
            checked_revs[rid] = True
67
 
 
68
 
            ## TODO: Check all the required fields are present on the revision.
69
 
 
 
58
            rev = branch.get_revision(rev_id)
 
59
            if rev.revision_id != rev_id:
 
60
                raise BzrCheckError('wrong internal revision id in revision {%s}'
 
61
                                    % rev_id)
 
62
 
 
63
            last_rev_id = rev_id
 
64
 
 
65
            # if set to true, revision must be written out
70
66
            updated = False
71
 
            if rev.inventory_sha1:
72
 
                #mutter('    checking inventory hash {%s}' % rev.inventory_sha1)
73
 
                inv_sha1 = branch.get_inventory_sha1(rev.inventory_id)
74
 
                if inv_sha1 != rev.inventory_sha1:
75
 
                    raise BzrCheckError('Inventory sha1 hash doesn\'t match'
76
 
                        ' value in revision {%s}' % rid)
77
 
            else:
78
 
                inv_sha1 = branch.get_inventory_sha1(rev.inventory_id)
79
 
                rev.inventory_sha1 = inv_sha1
 
67
 
 
68
            if rev.inventory_sha1 is None:
 
69
                rev.inventory_sha1 = branch.get_inventory_sha1(rev.inventory_id)
80
70
                updated = True
 
71
                mutter("  set inventory_sha1 on {%s}" % rev_id)
81
72
 
82
 
            if rev.precursor:
83
 
                if rev.precursor_sha1:
84
 
                    precursor_sha1 = branch.get_revision_sha1(rev.precursor)
85
 
                    if updated_previous_revision: 
86
 
                        # we don't expect the hashes to match, because
87
 
                        # we had to modify the previous revision_history entry.
88
 
                        rev.precursor_sha1 = precursor_sha1
 
73
            for prr in rev.parents:
 
74
                actual_sha1 = branch.get_revision_sha1(prr.revision_id)
 
75
                if (updated_previous_revision
 
76
                    or prr.revision_sha1 is None):
 
77
                    if prr.revision_sha1 != actual_sha1:
 
78
                        prr.revision_sha1 = actual_sha1
89
79
                        updated = True
90
 
                    else:
91
 
                        #mutter('    checking precursor hash {%s}' % rev.precursor_sha1)
92
 
                        if rev.precursor_sha1 != precursor_sha1:
93
 
                            raise BzrCheckError('Precursor sha1 hash doesn\'t match'
94
 
                                ' value in revision {%s}' % rid)
95
 
                else:
96
 
                    precursor_sha1 = branch.get_revision_sha1(rev.precursor)
97
 
                    rev.precursor_sha1 = precursor_sha1
98
 
                    updated = True
 
80
                elif actual_sha1 != prr.revision_sha1:
 
81
                    raise BzrCheckError("parent {%s} of {%s} sha1 mismatch: "
 
82
                                        "%s vs %s"
 
83
                                        % (prr.revision_id, rev_id,
 
84
                                           actual_sha1, prr.revision_sha1))
99
85
 
100
86
            if updated:
101
87
                updated_previous_revision = True
110
96
                rev.write_xml(rev_tmp)
111
97
                rev_tmp.seek(0)
112
98
 
113
 
                tmpfd, tmp_path = tempfile.mkstemp(prefix=rid, suffix='.gz',
 
99
                tmpfd, tmp_path = tempfile.mkstemp(prefix=rev_id, suffix='.gz',
114
100
                    dir=branch.controlfilename('revision-store'))
115
101
                os.close(tmpfd)
116
102
                def special_rename(p1, p2):
127
113
                    # entry was not compressed (and thus did not end with .gz)
128
114
 
129
115
                    # Remove the old revision entry out of the way
130
 
                    rev_path = branch.controlfilename(['revision-store', rid+'.gz'])
 
116
                    rev_path = branch.controlfilename(['revision-store', rev_id+'.gz'])
131
117
                    special_rename(rev_path, tmp_path)
132
 
                    branch.revision_store.add(rev_tmp, rid) # Add the new one
 
118
                    branch.revision_store.add(rev_tmp, rev_id) # Add the new one
133
119
                    os.remove(tmp_path) # Remove the old name
134
 
                    mutter('    Updated revision entry {%s}' % rid)
 
120
                    mutter('    Updated revision entry {%s}' % rev_id)
135
121
                except:
136
122
                    # On any exception, restore the old entry
137
123
                    special_rename(tmp_path, rev_path)
138
124
                    raise
139
125
                rev_tmp.close()
140
 
                updated_revisions.append(rid)
 
126
                updated_revisions.append(rev_id)
141
127
            else:
142
128
                updated_previous_revision = False
143
129