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."""
25
from ...errors import (
29
generate_transpose_plan,
30
CommitBuilderRevisionRewriter,
36
class UpgradeChangesContent(BzrError):
37
"""Inconsistency was found upgrading the mapping of a revision."""
38
_fmt = """Upgrade will change contents in revision %(revid)s. Use --allow-changes to override."""
40
def __init__(self, revid):
44
def create_deterministic_revid(revid, new_parents):
45
"""Create a new deterministic revision id with specified new parents.
47
Prevents suffix to be appended needlessly.
49
:param revid: Original revision id.
50
:return: New revision id
52
if "-rebase-" in revid:
53
revid = revid[0:revid.rfind("-rebase-")]
54
return revid + "-rebase-" + osutils.sha_string(":".join(new_parents))[:8]
57
def upgrade_tags(tags, repository, generate_rebase_map, determine_new_revid,
58
allow_changes=False, verbose=False, branch_renames=None,
59
branch_ancestry=None):
60
"""Upgrade a tags dictionary."""
62
if branch_renames is not None:
63
renames.update(branch_renames)
64
pb = ui.ui_factory.nested_progress_bar()
66
tags_dict = tags.get_tag_dict()
67
for i, (name, revid) in enumerate(tags_dict.iteritems()):
68
pb.update("upgrading tags", i, len(tags_dict))
69
if revid not in renames:
71
repository.lock_read()
72
revid_exists = repository.has_revision(revid)
76
renames.update(upgrade_repository(
78
generate_rebase_map, determine_new_revid,
79
revision_id=revid, allow_changes=allow_changes,
81
if (revid in renames and
82
(branch_ancestry is None or revid not in branch_ancestry)):
83
tags.set_tag(name, renames[revid])
88
def upgrade_branch(branch, generate_rebase_map, determine_new_revid,
89
allow_changes=False, verbose=False):
90
"""Upgrade a branch to the current mapping version.
92
:param branch: Branch to upgrade.
93
:param foreign_repository: Repository to fetch new revisions from
94
:param allow_changes: Allow changes in mappings.
95
:param verbose: Whether to print verbose list of rewrites
97
revid = branch.last_revision()
98
renames = upgrade_repository(
99
branch.repository, generate_rebase_map,
100
determine_new_revid, revision_id=revid,
101
allow_changes=allow_changes, verbose=verbose)
103
branch.generate_revision_history(renames[revid])
104
ancestry = branch.repository.get_ancestry(
105
branch.last_revision(), topo_sorted=False)
107
branch.tags, branch.repository, generate_rebase_map,
108
determine_new_revid, allow_changes=allow_changes, verbose=verbose,
109
branch_renames=renames, branch_ancestry=ancestry)
113
def check_revision_changed(oldrev, newrev):
114
"""Check if two revisions are different. This is exactly the same
115
as Revision.equals() except that it does not check the revision_id."""
116
if (newrev.inventory_sha1 != oldrev.inventory_sha1 or
117
newrev.timestamp != oldrev.timestamp or
118
newrev.message != oldrev.message or
119
newrev.timezone != oldrev.timezone or
120
newrev.committer != oldrev.committer or
121
newrev.properties != oldrev.properties):
122
raise UpgradeChangesContent(oldrev.revision_id)
125
def create_upgrade_plan(repository, generate_rebase_map, determine_new_revid,
126
revision_id=None, allow_changes=False):
127
"""Generate a rebase plan for upgrading revisions.
129
:param repository: Repository to do upgrade in
130
:param foreign_repository: Subversion repository to fetch new revisions
132
:param new_mapping: New mapping to use.
133
:param revision_id: Revision to upgrade (None for all revisions in
135
:param allow_changes: Whether an upgrade is allowed to change the contents
137
:return: Tuple with a rebase plan and map of renamed revisions.
140
graph = repository.get_graph()
141
upgrade_map = generate_rebase_map(revision_id)
143
if not allow_changes:
144
for oldrevid, newrevid in upgrade_map.iteritems():
145
oldrev = repository.get_revision(oldrevid)
146
newrev = repository.get_revision(newrevid)
147
check_revision_changed(oldrev, newrev)
149
if revision_id is None:
150
heads = repository.all_revision_ids()
152
heads = [revision_id]
154
plan = generate_transpose_plan(
155
graph.iter_ancestry(heads), upgrade_map, graph, determine_new_revid)
156
def remove_parents(entry):
157
(oldrevid, (newrevid, parents)) = entry
158
return (oldrevid, newrevid)
159
upgrade_map.update(dict(map(remove_parents, plan.iteritems())))
161
return (plan, upgrade_map)
164
def upgrade_repository(repository, generate_rebase_map,
165
determine_new_revid, revision_id=None,
166
allow_changes=False, verbose=False):
167
"""Upgrade the revisions in repository until the specified stop revision.
169
:param repository: Repository in which to upgrade.
170
:param foreign_repository: Repository to fetch new revisions from.
171
:param new_mapping: New mapping.
172
:param revision_id: Revision id up until which to upgrade, or None for
174
:param allow_changes: Allow changes to mappings.
175
:param verbose: Whether to print list of rewrites
176
:return: Dictionary of mapped revisions
178
# Find revisions that need to be upgraded, create
179
# dictionary with revision ids in key, new parents in value
180
with repository.lock_write():
181
(plan, revid_renames) = create_upgrade_plan(
182
repository, generate_rebase_map, determine_new_revid,
183
revision_id=revision_id, allow_changes=allow_changes)
185
for revid in rebase_todo(repository, plan):
186
trace.note("%s -> %s" % (revid, plan[revid][0]))
187
rebase(repository, plan, CommitBuilderRevisionRewriter(repository))