/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.205.33 by Jelmer Vernooij
Import simplifications for upgrade.
1
# Copyright (C) 2006-2009 by Jelmer Vernooij
0.205.12 by Jelmer Vernooij
Import upgrade code for upgrading mappings.
2
# 
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 3 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
"""Upgrading revisions made with older versions of the mapping."""
17
18
from bzrlib import ui
0.205.33 by Jelmer Vernooij
Import simplifications for upgrade.
19
from bzrlib.errors import (
20
    DependencyNotPresent,
21
    BzrError,
22
    InvalidRevisionId,
23
    NoSuchRevision,
24
    )
0.205.12 by Jelmer Vernooij
Import upgrade code for upgrading mappings.
25
from bzrlib.trace import info
26
27
import itertools
28
29
class RebaseNotPresent(DependencyNotPresent):
30
    _fmt = "Unable to import bzr-rebase (required for upgrade support): %(error)s"
31
32
    def __init__(self, error):
33
        DependencyNotPresent.__init__(self, 'bzr-rebase', error)
34
35
36
def check_rebase_version(min_version):
37
    """Check what version of bzr-rebase is installed.
38
39
    Raises an exception when the version installed is older than 
40
    min_version.
41
42
    :raises RebaseNotPresent: Raised if bzr-rebase is not installed or too old.
43
    """
44
    try:
45
        from bzrlib.plugins.rebase import version_info as rebase_version_info
0.205.33 by Jelmer Vernooij
Import simplifications for upgrade.
46
        if rebase_version_info[:len(min_version)] < min_version:
0.205.12 by Jelmer Vernooij
Import upgrade code for upgrading mappings.
47
            raise RebaseNotPresent("Version %r present, at least %r required" 
48
                                   % (rebase_version_info, min_version))
49
    except ImportError, e:
50
        raise RebaseNotPresent(e)
51
52
53
54
class UpgradeChangesContent(BzrError):
55
    """Inconsistency was found upgrading the mapping of a revision."""
56
    _fmt = """Upgrade will change contents in revision %(revid)s. Use --allow-changes to override."""
57
58
    def __init__(self, revid):
59
        self.revid = revid
60
61
62
63
def create_upgraded_revid(revid, mapping_suffix, upgrade_suffix="-upgrade"):
64
    """Create a new revision id for an upgraded version of a revision.
65
    
66
    Prevents suffix to be appended needlessly.
67
68
    :param revid: Original revision id.
69
    :return: New revision id
70
    """
71
    if revid.endswith(upgrade_suffix):
72
        return revid[0:revid.rfind("-svn")] + mapping_suffix + upgrade_suffix
73
    else:
74
        return revid + mapping_suffix + upgrade_suffix
75
76
0.205.33 by Jelmer Vernooij
Import simplifications for upgrade.
77
def determine_fileid_renames(old_inv, new_inv):
78
    """Determine the file ids based on a old and a new inventory that 
79
    are equal in content.
80
81
    :param old_inv: Old inventory
82
    :param new_inv: New inventory
83
    :return: Dictionary a (old_id, new_id) tuple for each path in the 
84
        inventories.
85
    """
86
    ret = {}
87
    if len(old_inv) != len(new_inv):
88
        raise AssertionError("Inventories are not of the same size")
89
    for old_file_id in old_inv:
90
        new_file_id = new_inv.path2id(old_inv.id2path(old_file_id))
91
        if new_file_id is None:
92
            raise AssertionError(
93
                "Unable to find %s in new inventory" % old_file_id)
0.205.36 by Jelmer Vernooij
Just replace all file ids, rather than worrying about unchanged children of changed directories.
94
        ret[new_inv.id2path(new_file_id)] = (old_file_id, new_file_id)
0.205.33 by Jelmer Vernooij
Import simplifications for upgrade.
95
    return ret
96
97
98
def update_workinginv_fileids(wt, old_inv, new_inv):
99
    """Update all file ids in wt according to old_tree/new_tree. 
100
101
    old_tree and new_tree should be two RevisionTree's that differ only
102
    in file ids.
103
    """
104
    fileid_renames = determine_fileid_renames(old_inv, new_inv)
105
    old_fileids = []
106
    new_fileids = []
107
    new_root_id = None
108
    # Adjust file ids in working tree
109
    # Sorted, so we process parents before children
0.205.36 by Jelmer Vernooij
Just replace all file ids, rather than worrying about unchanged children of changed directories.
110
    for path in sorted(fileid_renames.keys()):
0.205.33 by Jelmer Vernooij
Import simplifications for upgrade.
111
        if path != "":
112
            old_fileids.append(fileid_renames[path][0])
113
            new_fileids.append((path, fileid_renames[path][1]))
114
        else:
115
            new_root_id = fileid_renames[path][1]
116
    new_fileids.reverse()
117
    wt.unversion(old_fileids)
118
    if new_root_id is not None:
119
        wt.set_root_id(new_root_id)
120
    wt.add([x[0] for x in new_fileids], [x[1] for x in new_fileids])
121
    wt.set_last_revision(new_inv.revision_id)
0.205.12 by Jelmer Vernooij
Import upgrade code for upgrading mappings.
122
123
0.205.37 by Jelmer Vernooij
Import foreign-mapping-upgrade command, fixes for bzr-svn.
124
def upgrade_workingtree(wt, foreign_repository, new_mapping, 
0.205.12 by Jelmer Vernooij
Import upgrade code for upgrading mappings.
125
                        allow_changes=False, verbose=False):
126
    """Upgrade a working tree.
127
128
    :param foreign_repository: Foreign repository object
129
    """
130
    wt.lock_write()
131
    try:
132
        old_revid = wt.last_revision()
0.205.16 by Jelmer Vernooij
Import fixes from bzr-svn.
133
        revid_renames = upgrade_branch(wt.branch, foreign_repository, new_mapping=new_mapping,
0.205.12 by Jelmer Vernooij
Import upgrade code for upgrading mappings.
134
                                 allow_changes=allow_changes, verbose=verbose)
135
        last_revid = wt.branch.last_revision()
0.205.16 by Jelmer Vernooij
Import fixes from bzr-svn.
136
        if old_revid == last_revid:
137
            return revid_renames
0.205.33 by Jelmer Vernooij
Import simplifications for upgrade.
138
        old_inv = wt.branch.repository.get_inventory(old_revid)
139
        new_inv = wt.branch.repository.get_inventory(last_revid)
140
        update_workinginv_fileids(wt, old_inv, new_inv)
0.205.12 by Jelmer Vernooij
Import upgrade code for upgrading mappings.
141
    finally:
142
        wt.unlock()
143
0.205.16 by Jelmer Vernooij
Import fixes from bzr-svn.
144
    return revid_renames
145
146
0.205.37 by Jelmer Vernooij
Import foreign-mapping-upgrade command, fixes for bzr-svn.
147
def upgrade_tags(tags, repository, foreign_repository, new_mapping, 
0.205.16 by Jelmer Vernooij
Import fixes from bzr-svn.
148
                 allow_changes=False, verbose=False, branch_renames=None):
149
    """Upgrade a tags dictionary."""
0.205.33 by Jelmer Vernooij
Import simplifications for upgrade.
150
    renames = {}
151
    if branch_renames is not None:
152
        renames.update(branch_renames)
0.205.16 by Jelmer Vernooij
Import fixes from bzr-svn.
153
    pb = ui.ui_factory.nested_progress_bar()
154
    try:
155
        tags_dict = tags.get_tag_dict()
156
        for i, (name, revid) in enumerate(tags_dict.items()):
157
            pb.update("upgrading tags", i, len(tags_dict))
0.205.33 by Jelmer Vernooij
Import simplifications for upgrade.
158
            if not revid in renames:
159
                renames.update(upgrade_repository(repository, foreign_repository, 
0.205.16 by Jelmer Vernooij
Import fixes from bzr-svn.
160
                      revision_id=revid, new_mapping=new_mapping,
0.205.33 by Jelmer Vernooij
Import simplifications for upgrade.
161
                      allow_changes=allow_changes, verbose=verbose))
0.205.16 by Jelmer Vernooij
Import fixes from bzr-svn.
162
            if revid in renames:
163
                tags.set_tag(name, renames[revid])
164
    finally:
165
        pb.finished()
0.205.12 by Jelmer Vernooij
Import upgrade code for upgrading mappings.
166
167
0.205.14 by Jelmer Vernooij
Fix upgrade_branch.
168
def upgrade_branch(branch, foreign_repository, new_mapping, 
0.205.37 by Jelmer Vernooij
Import foreign-mapping-upgrade command, fixes for bzr-svn.
169
                   allow_changes=False, verbose=False):
0.205.12 by Jelmer Vernooij
Import upgrade code for upgrading mappings.
170
    """Upgrade a branch to the current mapping version.
171
    
172
    :param branch: Branch to upgrade.
173
    :param foreign_repository: Repository to fetch new revisions from
174
    :param allow_changes: Allow changes in mappings.
175
    :param verbose: Whether to print verbose list of rewrites
176
    """
177
    revid = branch.last_revision()
178
    renames = upgrade_repository(branch.repository, foreign_repository, 
179
              revision_id=revid, new_mapping=new_mapping,
180
              allow_changes=allow_changes, verbose=verbose)
0.205.16 by Jelmer Vernooij
Import fixes from bzr-svn.
181
    upgrade_tags(branch.tags, branch.repository, foreign_repository, 
0.205.37 by Jelmer Vernooij
Import foreign-mapping-upgrade command, fixes for bzr-svn.
182
           new_mapping=new_mapping, 
0.205.33 by Jelmer Vernooij
Import simplifications for upgrade.
183
           allow_changes=allow_changes, verbose=verbose, branch_renames=renames)
0.205.37 by Jelmer Vernooij
Import foreign-mapping-upgrade command, fixes for bzr-svn.
184
    if revid in renames:
0.205.12 by Jelmer Vernooij
Import upgrade code for upgrading mappings.
185
        branch.generate_revision_history(renames[revid])
186
    return renames
187
188
189
def check_revision_changed(oldrev, newrev):
190
    """Check if two revisions are different. This is exactly the same 
191
    as Revision.equals() except that it does not check the revision_id."""
192
    if (newrev.inventory_sha1 != oldrev.inventory_sha1 or
193
        newrev.timestamp != oldrev.timestamp or
194
        newrev.message != oldrev.message or
195
        newrev.timezone != oldrev.timezone or
196
        newrev.committer != oldrev.committer or
197
        newrev.properties != oldrev.properties):
198
        raise UpgradeChangesContent(oldrev.revision_id)
199
200
0.205.37 by Jelmer Vernooij
Import foreign-mapping-upgrade command, fixes for bzr-svn.
201
def generate_upgrade_map(revs, vcs, determine_upgraded_revid):
0.205.12 by Jelmer Vernooij
Import upgrade code for upgrading mappings.
202
    """Generate an upgrade map for use by bzr-rebase.
203
204
    :param new_mapping: Mapping to upgrade revisions to.
0.205.37 by Jelmer Vernooij
Import foreign-mapping-upgrade command, fixes for bzr-svn.
205
    :param vcs: The foreign vcs
0.205.12 by Jelmer Vernooij
Import upgrade code for upgrading mappings.
206
    :param revs: Iterator over revisions to upgrade.
207
    :return: Map from old revids as keys, new revids as values stored in a 
208
             dictionary.
209
    """
210
    rename_map = {}
0.205.16 by Jelmer Vernooij
Import fixes from bzr-svn.
211
    # Create a list of revisions that can be renamed during the upgrade
0.205.12 by Jelmer Vernooij
Import upgrade code for upgrading mappings.
212
    for revid in revs:
213
        assert isinstance(revid, str)
214
        try:
0.205.37 by Jelmer Vernooij
Import foreign-mapping-upgrade command, fixes for bzr-svn.
215
            (foreign_revid, old_mapping) = vcs.mapping_registry.parse_revision_id(revid)
0.205.12 by Jelmer Vernooij
Import upgrade code for upgrading mappings.
216
        except InvalidRevisionId:
217
            # Not a foreign revision, nothing to do
218
            continue
0.205.33 by Jelmer Vernooij
Import simplifications for upgrade.
219
        newrevid = determine_upgraded_revid(foreign_revid)
220
        if newrevid in (revid, None):
0.205.12 by Jelmer Vernooij
Import upgrade code for upgrading mappings.
221
            continue
222
        rename_map[revid] = newrevid
223
    return rename_map
224
0.205.33 by Jelmer Vernooij
Import simplifications for upgrade.
225
MIN_REBASE_VERSION = (0, 4, 3)
0.205.12 by Jelmer Vernooij
Import upgrade code for upgrading mappings.
226
227
def create_upgrade_plan(repository, foreign_repository, new_mapping,
0.205.37 by Jelmer Vernooij
Import foreign-mapping-upgrade command, fixes for bzr-svn.
228
                        revision_id=None, allow_changes=False):
0.205.12 by Jelmer Vernooij
Import upgrade code for upgrading mappings.
229
    """Generate a rebase plan for upgrading revisions.
230
231
    :param repository: Repository to do upgrade in
232
    :param foreign_repository: Subversion repository to fetch new revisions from.
233
    :param new_mapping: New mapping to use.
234
    :param revision_id: Revision to upgrade (None for all revisions in 
235
        repository.)
236
    :param allow_changes: Whether an upgrade is allowed to change the contents
237
        of revisions.
238
    :return: Tuple with a rebase plan and map of renamed revisions.
239
    """
240
    from bzrlib.plugins.rebase.rebase import generate_transpose_plan
241
    check_rebase_version(MIN_REBASE_VERSION)
242
243
    graph = repository.get_graph()
244
    if revision_id is None:
245
        potential = repository.all_revision_ids()
246
    else:
247
        potential = itertools.imap(lambda (rev, parents): rev, 
248
                graph.iter_ancestry([revision_id]))
0.205.33 by Jelmer Vernooij
Import simplifications for upgrade.
249
250
    def determine_upgraded_revid(foreign_revid):
251
        # FIXME: Try all mappings until new_mapping rather than just new_mapping
0.205.37 by Jelmer Vernooij
Import foreign-mapping-upgrade command, fixes for bzr-svn.
252
        new_revid = foreign_repository.upgrade_foreign_revision_id(foreign_revid, new_mapping)
253
        if new_revid is None:
254
            return None
0.205.33 by Jelmer Vernooij
Import simplifications for upgrade.
255
        # Make sure the revision is there
256
        if not repository.has_revision(new_revid):
257
            try:
258
                repository.fetch(foreign_repository, new_revid)
259
            except NoSuchRevision:
260
                return None
0.205.37 by Jelmer Vernooij
Import foreign-mapping-upgrade command, fixes for bzr-svn.
261
            if not repository.has_revision(new_revid):
262
                return None
0.205.33 by Jelmer Vernooij
Import simplifications for upgrade.
263
        return new_revid
264
0.205.37 by Jelmer Vernooij
Import foreign-mapping-upgrade command, fixes for bzr-svn.
265
    upgrade_map = generate_upgrade_map(potential, foreign_repository.vcs, 
266
                                       determine_upgraded_revid)
0.205.12 by Jelmer Vernooij
Import upgrade code for upgrading mappings.
267
   
268
    if not allow_changes:
0.205.22 by Jelmer Vernooij
Merge improvements from bzr-svn.
269
        for oldrevid, newrevid in upgrade_map.iteritems():
0.205.12 by Jelmer Vernooij
Import upgrade code for upgrading mappings.
270
            oldrev = repository.get_revision(oldrevid)
271
            newrev = repository.get_revision(newrevid)
272
            check_revision_changed(oldrev, newrev)
273
274
    if revision_id is None:
275
        heads = repository.all_revision_ids() 
276
    else:
277
        heads = [revision_id]
278
0.205.37 by Jelmer Vernooij
Import foreign-mapping-upgrade command, fixes for bzr-svn.
279
    def determine_new_revid(old_revid):
280
        # If this revision id already exists round-tripped upstream, 
281
        # leave it alone.
282
        if foreign_repository.has_revision(old_revid):
283
            return old_revid
284
        # if not, return old_revid'
285
        return create_upgraded_revid(old_revid, new_mapping.upgrade_suffix)
286
0.205.12 by Jelmer Vernooij
Import upgrade code for upgrading mappings.
287
    plan = generate_transpose_plan(graph.iter_ancestry(heads), upgrade_map, 
0.205.37 by Jelmer Vernooij
Import foreign-mapping-upgrade command, fixes for bzr-svn.
288
      graph, determine_new_revid)
0.205.12 by Jelmer Vernooij
Import upgrade code for upgrading mappings.
289
    def remove_parents((oldrevid, (newrevid, parents))):
290
        return (oldrevid, newrevid)
0.205.22 by Jelmer Vernooij
Merge improvements from bzr-svn.
291
    upgrade_map.update(dict(map(remove_parents, plan.iteritems())))
0.205.12 by Jelmer Vernooij
Import upgrade code for upgrading mappings.
292
293
    return (plan, upgrade_map)
294
295
 
296
def upgrade_repository(repository, foreign_repository, new_mapping, 
0.205.37 by Jelmer Vernooij
Import foreign-mapping-upgrade command, fixes for bzr-svn.
297
                       revision_id=None, allow_changes=False, 
0.205.12 by Jelmer Vernooij
Import upgrade code for upgrading mappings.
298
                       verbose=False):
299
    """Upgrade the revisions in repository until the specified stop revision.
300
301
    :param repository: Repository in which to upgrade.
302
    :param foreign_repository: Repository to fetch new revisions from.
303
    :param new_mapping: New mapping.
304
    :param revision_id: Revision id up until which to upgrade, or None for 
305
                        all revisions.
306
    :param allow_changes: Allow changes to mappings.
307
    :param verbose: Whether to print list of rewrites
308
    :return: Dictionary of mapped revisions
309
    """
310
    check_rebase_version(MIN_REBASE_VERSION)
311
    from bzrlib.plugins.rebase.rebase import (
312
        replay_snapshot, rebase, rebase_todo)
313
314
    # Find revisions that need to be upgraded, create
315
    # dictionary with revision ids in key, new parents in value
316
    try:
317
        repository.lock_write()
318
        foreign_repository.lock_read()
319
        (plan, revid_renames) = create_upgrade_plan(repository, foreign_repository, 
0.205.37 by Jelmer Vernooij
Import foreign-mapping-upgrade command, fixes for bzr-svn.
320
                                                    new_mapping, 
0.205.12 by Jelmer Vernooij
Import upgrade code for upgrading mappings.
321
                                                    revision_id=revision_id,
322
                                                    allow_changes=allow_changes)
323
        if verbose:
324
            for revid in rebase_todo(repository, plan):
325
                info("%s -> %s" % (revid, plan[revid][0]))
0.205.33 by Jelmer Vernooij
Import simplifications for upgrade.
326
        rebase(repository, plan, replay_snapshot)
0.205.12 by Jelmer Vernooij
Import upgrade code for upgrading mappings.
327
        return revid_renames
328
    finally:
329
        repository.unlock()
330
        foreign_repository.unlock()
331