1
# Copyright (C) 2006-2009 by Jelmer Vernooij
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.
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.
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
18
"""Upgrading revisions made with older versions of the mapping."""
20
from __future__ import absolute_import
27
from ...errors import (
31
generate_transpose_plan,
32
CommitBuilderRevisionRewriter,
38
class UpgradeChangesContent(BzrError):
39
"""Inconsistency was found upgrading the mapping of a revision."""
40
_fmt = """Upgrade will change contents in revision %(revid)s. Use --allow-changes to override."""
42
def __init__(self, revid):
46
def create_deterministic_revid(revid, new_parents):
47
"""Create a new deterministic revision id with specified new parents.
49
Prevents suffix to be appended needlessly.
51
:param revid: Original revision id.
52
:return: New revision id
54
if "-rebase-" in revid:
55
revid = revid[0:revid.rfind("-rebase-")]
56
return revid + "-rebase-" + osutils.sha_string(":".join(new_parents))[:8]
59
def upgrade_tags(tags, repository, generate_rebase_map, determine_new_revid,
60
allow_changes=False, verbose=False, branch_renames=None,
61
branch_ancestry=None):
62
"""Upgrade a tags dictionary."""
64
if branch_renames is not None:
65
renames.update(branch_renames)
66
pb = ui.ui_factory.nested_progress_bar()
68
tags_dict = tags.get_tag_dict()
69
for i, (name, revid) in enumerate(tags_dict.iteritems()):
70
pb.update("upgrading tags", i, len(tags_dict))
71
if revid not in renames:
73
repository.lock_read()
74
revid_exists = repository.has_revision(revid)
78
renames.update(upgrade_repository(
80
generate_rebase_map, determine_new_revid,
81
revision_id=revid, allow_changes=allow_changes,
83
if (revid in renames and
84
(branch_ancestry is None or revid not in branch_ancestry)):
85
tags.set_tag(name, renames[revid])
90
def upgrade_branch(branch, generate_rebase_map, determine_new_revid,
91
allow_changes=False, verbose=False):
92
"""Upgrade a branch to the current mapping version.
94
:param branch: Branch to upgrade.
95
:param foreign_repository: Repository to fetch new revisions from
96
:param allow_changes: Allow changes in mappings.
97
:param verbose: Whether to print verbose list of rewrites
99
revid = branch.last_revision()
100
renames = upgrade_repository(
101
branch.repository, generate_rebase_map,
102
determine_new_revid, revision_id=revid,
103
allow_changes=allow_changes, verbose=verbose)
105
branch.generate_revision_history(renames[revid])
106
ancestry = branch.repository.get_ancestry(
107
branch.last_revision(), topo_sorted=False)
109
branch.tags, branch.repository, generate_rebase_map,
110
determine_new_revid, allow_changes=allow_changes, verbose=verbose,
111
branch_renames=renames, branch_ancestry=ancestry)
115
def check_revision_changed(oldrev, newrev):
116
"""Check if two revisions are different. This is exactly the same
117
as Revision.equals() except that it does not check the revision_id."""
118
if (newrev.inventory_sha1 != oldrev.inventory_sha1 or
119
newrev.timestamp != oldrev.timestamp or
120
newrev.message != oldrev.message or
121
newrev.timezone != oldrev.timezone or
122
newrev.committer != oldrev.committer or
123
newrev.properties != oldrev.properties):
124
raise UpgradeChangesContent(oldrev.revision_id)
127
def create_upgrade_plan(repository, generate_rebase_map, determine_new_revid,
128
revision_id=None, allow_changes=False):
129
"""Generate a rebase plan for upgrading revisions.
131
:param repository: Repository to do upgrade in
132
:param foreign_repository: Subversion repository to fetch new revisions
134
:param new_mapping: New mapping to use.
135
:param revision_id: Revision to upgrade (None for all revisions in
137
:param allow_changes: Whether an upgrade is allowed to change the contents
139
:return: Tuple with a rebase plan and map of renamed revisions.
142
graph = repository.get_graph()
143
upgrade_map = generate_rebase_map(revision_id)
145
if not allow_changes:
146
for oldrevid, newrevid in upgrade_map.iteritems():
147
oldrev = repository.get_revision(oldrevid)
148
newrev = repository.get_revision(newrevid)
149
check_revision_changed(oldrev, newrev)
151
if revision_id is None:
152
heads = repository.all_revision_ids()
154
heads = [revision_id]
156
plan = generate_transpose_plan(
157
graph.iter_ancestry(heads), upgrade_map, graph, determine_new_revid)
158
def remove_parents(entry):
159
(oldrevid, (newrevid, parents)) = entry
160
return (oldrevid, newrevid)
161
upgrade_map.update(dict(map(remove_parents, plan.iteritems())))
163
return (plan, upgrade_map)
166
def upgrade_repository(repository, generate_rebase_map,
167
determine_new_revid, revision_id=None,
168
allow_changes=False, verbose=False):
169
"""Upgrade the revisions in repository until the specified stop revision.
171
:param repository: Repository in which to upgrade.
172
:param foreign_repository: Repository to fetch new revisions from.
173
:param new_mapping: New mapping.
174
:param revision_id: Revision id up until which to upgrade, or None for
176
:param allow_changes: Allow changes to mappings.
177
:param verbose: Whether to print list of rewrites
178
:return: Dictionary of mapped revisions
180
# Find revisions that need to be upgraded, create
181
# dictionary with revision ids in key, new parents in value
182
with repository.lock_write():
183
(plan, revid_renames) = create_upgrade_plan(
184
repository, generate_rebase_map, determine_new_revid,
185
revision_id=revision_id, allow_changes=allow_changes)
187
for revid in rebase_todo(repository, plan):
188
trace.note("%s -> %s" % (revid, plan[revid][0]))
189
rebase(repository, plan, CommitBuilderRevisionRewriter(repository))