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