/brz/remove-bazaar

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