~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
1185.1.41 by Robert Collins
massive patch from Alexander Belchenko - many PEP8 fixes, removes unused function uuid
18
import errno
19
import os
20
import tempfile
21
1092 by Martin Pool
- fix bug where bzr upgrade aborts when trying to fix trees that mention revisions
22
import bzrlib.errors
1104 by Martin Pool
- Add a simple UIFactory
23
import bzrlib.progress
1182 by Martin Pool
- more disentangling of xml storage format from objects
24
from bzrlib.xml import serializer_v4
1185.1.41 by Robert Collins
massive patch from Alexander Belchenko - many PEP8 fixes, removes unused function uuid
25
from bzrlib.osutils import rename
703 by Martin Pool
- split out a new 'bzr upgrade' command separate from
26
27
28
def upgrade(branch):
29
    """
30
    Upgrade branch to current format.
31
32
    This causes objects to be rewritten into the current format.
33
34
    If they change, their SHA-1 will of course change, which might
35
    break any later signatures, or backreferences that check the
36
    SHA-1.
710 by Martin Pool
- bzr upgrade updates or checks SHA1 on all predecessor revisions
37
38
    TODO: Check non-mainline revisions.
703 by Martin Pool
- split out a new 'bzr upgrade' command separate from
39
    """
40
    import sys
41
42
    from bzrlib.trace import mutter
43
    from bzrlib.errors import BzrCheckError
1104 by Martin Pool
- Add a simple UIFactory
44
    import bzrlib.ui
703 by Martin Pool
- split out a new 'bzr upgrade' command separate from
45
46
    branch.lock_write()
47
1124 by Martin Pool
- fix broken import in upgrade code
48
    pb = bzrlib.ui.ui_factory.progress_bar()
1104 by Martin Pool
- Add a simple UIFactory
49
703 by Martin Pool
- split out a new 'bzr upgrade' command separate from
50
    try:
710 by Martin Pool
- bzr upgrade updates or checks SHA1 on all predecessor revisions
51
        last_rev_id = None
703 by Martin Pool
- split out a new 'bzr upgrade' command separate from
52
53
        history = branch.revision_history()
54
        revno = 0
55
        revcount = len(history)
56
57
        updated_revisions = []
58
59
        # Set to True in the case that the previous revision entry
60
        # was updated, since this will affect future revision entries
61
        updated_previous_revision = False
62
710 by Martin Pool
- bzr upgrade updates or checks SHA1 on all predecessor revisions
63
        for rev_id in history:
703 by Martin Pool
- split out a new 'bzr upgrade' command separate from
64
            revno += 1
65
            pb.update('upgrading revision', revno, revcount)
710 by Martin Pool
- bzr upgrade updates or checks SHA1 on all predecessor revisions
66
            rev = branch.get_revision(rev_id)
67
            if rev.revision_id != rev_id:
68
                raise BzrCheckError('wrong internal revision id in revision {%s}'
69
                                    % rev_id)
70
71
            last_rev_id = rev_id
72
73
            # if set to true, revision must be written out
703 by Martin Pool
- split out a new 'bzr upgrade' command separate from
74
            updated = False
710 by Martin Pool
- bzr upgrade updates or checks SHA1 on all predecessor revisions
75
76
            if rev.inventory_sha1 is None:
77
                rev.inventory_sha1 = branch.get_inventory_sha1(rev.inventory_id)
703 by Martin Pool
- split out a new 'bzr upgrade' command separate from
78
                updated = True
710 by Martin Pool
- bzr upgrade updates or checks SHA1 on all predecessor revisions
79
                mutter("  set inventory_sha1 on {%s}" % rev_id)
703 by Martin Pool
- split out a new 'bzr upgrade' command separate from
80
710 by Martin Pool
- bzr upgrade updates or checks SHA1 on all predecessor revisions
81
            for prr in rev.parents:
1092 by Martin Pool
- fix bug where bzr upgrade aborts when trying to fix trees that mention revisions
82
                try:
83
                    actual_sha1 = branch.get_revision_sha1(prr.revision_id)
84
                except bzrlib.errors.NoSuchRevision:
85
                    mutter("parent {%s} of {%s} not present in branch; skipped"
86
                           % (prr.revision_id, rev_id))
87
                    continue
88
                    
89
                if actual_sha1 != prr.revision_sha1:
90
                    mutter("parent {%s} of {%s} sha1 mismatch: "
91
                           "%s vs %s; fixed"
92
                           % (prr.revision_id, rev_id,
93
                              actual_sha1, prr.revision_sha1))
94
                    prr.revision_sha1 = actual_sha1
95
                    updated = True
703 by Martin Pool
- split out a new 'bzr upgrade' command separate from
96
97
            if updated:
98
                updated_previous_revision = True
99
                # We had to update this revision entries hashes
100
                # Now we need to write out a new value
101
                # This is a little bit invasive, since we are *rewriting* a
102
                # revision entry. I'm not supremely happy about it, but
103
                # there should be *some* way of making old entries have
104
                # the full meta information.
105
                rev_tmp = tempfile.TemporaryFile()
1182 by Martin Pool
- more disentangling of xml storage format from objects
106
                serializer_v4.write_revision(rev, rev_tmp)
703 by Martin Pool
- split out a new 'bzr upgrade' command separate from
107
                rev_tmp.seek(0)
108
710 by Martin Pool
- bzr upgrade updates or checks SHA1 on all predecessor revisions
109
                tmpfd, tmp_path = tempfile.mkstemp(prefix=rev_id, suffix='.gz',
703 by Martin Pool
- split out a new 'bzr upgrade' command separate from
110
                    dir=branch.controlfilename('revision-store'))
111
                os.close(tmpfd)
112
113
                try:
114
                    # TODO: We may need to handle the case where the old revision
115
                    # entry was not compressed (and thus did not end with .gz)
116
117
                    # Remove the old revision entry out of the way
710 by Martin Pool
- bzr upgrade updates or checks SHA1 on all predecessor revisions
118
                    rev_path = branch.controlfilename(['revision-store', rev_id+'.gz'])
1185.1.40 by Robert Collins
Merge what applied of Alexander Belchenko's win32 patch.
119
                    rename(rev_path, tmp_path)
710 by Martin Pool
- bzr upgrade updates or checks SHA1 on all predecessor revisions
120
                    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
121
                    os.remove(tmp_path) # Remove the old name
710 by Martin Pool
- bzr upgrade updates or checks SHA1 on all predecessor revisions
122
                    mutter('    Updated revision entry {%s}' % rev_id)
703 by Martin Pool
- split out a new 'bzr upgrade' command separate from
123
                except:
124
                    # On any exception, restore the old entry
1185.1.40 by Robert Collins
Merge what applied of Alexander Belchenko's win32 patch.
125
                    rename(tmp_path, rev_path)
703 by Martin Pool
- split out a new 'bzr upgrade' command separate from
126
                    raise
127
                rev_tmp.close()
710 by Martin Pool
- bzr upgrade updates or checks SHA1 on all predecessor revisions
128
                updated_revisions.append(rev_id)
703 by Martin Pool
- split out a new 'bzr upgrade' command separate from
129
            else:
130
                updated_previous_revision = False
131
132
    finally:
133
        branch.unlock()
134
135
    pb.clear()
136
137
    if updated_revisions:
138
        print '%d revisions updated to current format' % len(updated_revisions)