~bzr-pqm/bzr/bzr.dev

703 by Martin Pool
- split out a new 'bzr upgrade' command separate from
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
def upgrade(branch):
22
    """
23
    Upgrade branch to current format.
24
25
    This causes objects to be rewritten into the current format.
26
27
    If they change, their SHA-1 will of course change, which might
28
    break any later signatures, or backreferences that check the
29
    SHA-1.
710 by Martin Pool
- bzr upgrade updates or checks SHA1 on all predecessor revisions
30
31
    TODO: Check non-mainline revisions.
703 by Martin Pool
- split out a new 'bzr upgrade' command separate from
32
    """
33
    import sys
34
35
    from bzrlib.trace import mutter
36
    from bzrlib.errors import BzrCheckError
37
    from bzrlib.progress import ProgressBar
38
39
    branch.lock_write()
40
41
    try:
42
        pb = ProgressBar(show_spinner=True)
710 by Martin Pool
- bzr upgrade updates or checks SHA1 on all predecessor revisions
43
        last_rev_id = None
703 by Martin Pool
- split out a new 'bzr upgrade' command separate from
44
45
        history = branch.revision_history()
46
        revno = 0
47
        revcount = len(history)
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
54
710 by Martin Pool
- bzr upgrade updates or checks SHA1 on all predecessor revisions
55
        for rev_id in history:
703 by Martin Pool
- split out a new 'bzr upgrade' command separate from
56
            revno += 1
57
            pb.update('upgrading revision', revno, revcount)
710 by Martin Pool
- bzr upgrade updates or checks SHA1 on all predecessor revisions
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
703 by Martin Pool
- split out a new 'bzr upgrade' command separate from
66
            updated = False
710 by Martin Pool
- bzr upgrade updates or checks SHA1 on all predecessor revisions
67
68
            if rev.inventory_sha1 is None:
69
                rev.inventory_sha1 = branch.get_inventory_sha1(rev.inventory_id)
703 by Martin Pool
- split out a new 'bzr upgrade' command separate from
70
                updated = True
710 by Martin Pool
- bzr upgrade updates or checks SHA1 on all predecessor revisions
71
                mutter("  set inventory_sha1 on {%s}" % rev_id)
703 by Martin Pool
- split out a new 'bzr upgrade' command separate from
72
710 by Martin Pool
- bzr upgrade updates or checks SHA1 on all predecessor revisions
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
703 by Martin Pool
- split out a new 'bzr upgrade' command separate from
79
                        updated = True
710 by Martin Pool
- bzr upgrade updates or checks SHA1 on all predecessor revisions
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))
703 by Martin Pool
- split out a new 'bzr upgrade' command separate from
85
86
            if updated:
87
                updated_previous_revision = True
88
                # We had to update this revision entries hashes
89
                # Now we need to write out a new value
90
                # This is a little bit invasive, since we are *rewriting* a
91
                # revision entry. I'm not supremely happy about it, but
92
                # there should be *some* way of making old entries have
93
                # the full meta information.
94
                import tempfile, os, errno
95
                rev_tmp = tempfile.TemporaryFile()
96
                rev.write_xml(rev_tmp)
97
                rev_tmp.seek(0)
98
710 by Martin Pool
- bzr upgrade updates or checks SHA1 on all predecessor revisions
99
                tmpfd, tmp_path = tempfile.mkstemp(prefix=rev_id, suffix='.gz',
703 by Martin Pool
- split out a new 'bzr upgrade' command separate from
100
                    dir=branch.controlfilename('revision-store'))
101
                os.close(tmpfd)
102
                def special_rename(p1, p2):
103
                    if sys.platform == 'win32':
104
                        try:
105
                            os.remove(p2)
106
                        except OSError, e:
107
                            if e.errno != errno.ENOENT:
108
                                raise
109
                    os.rename(p1, p2)
110
111
                try:
112
                    # TODO: We may need to handle the case where the old revision
113
                    # entry was not compressed (and thus did not end with .gz)
114
115
                    # Remove the old revision entry out of the way
710 by Martin Pool
- bzr upgrade updates or checks SHA1 on all predecessor revisions
116
                    rev_path = branch.controlfilename(['revision-store', rev_id+'.gz'])
703 by Martin Pool
- split out a new 'bzr upgrade' command separate from
117
                    special_rename(rev_path, tmp_path)
710 by Martin Pool
- bzr upgrade updates or checks SHA1 on all predecessor revisions
118
                    branch.revision_store.add(rev_tmp, rev_id) # Add the new one
703 by Martin Pool
- split out a new 'bzr upgrade' command separate from
119
                    os.remove(tmp_path) # Remove the old name
710 by Martin Pool
- bzr upgrade updates or checks SHA1 on all predecessor revisions
120
                    mutter('    Updated revision entry {%s}' % rev_id)
703 by Martin Pool
- split out a new 'bzr upgrade' command separate from
121
                except:
122
                    # On any exception, restore the old entry
123
                    special_rename(tmp_path, rev_path)
124
                    raise
125
                rev_tmp.close()
710 by Martin Pool
- bzr upgrade updates or checks SHA1 on all predecessor revisions
126
                updated_revisions.append(rev_id)
703 by Martin Pool
- split out a new 'bzr upgrade' command separate from
127
            else:
128
                updated_previous_revision = False
129
130
    finally:
131
        branch.unlock()
132
133
    pb.clear()
134
135
    if updated_revisions:
136
        print '%d revisions updated to current format' % len(updated_revisions)