17
17
"""Foreign branch utilities."""
19
from bzrlib import errors
19
20
from bzrlib.branch import Branch
20
21
from bzrlib.commands import Command, Option
21
from bzrlib.repository import Repository
22
from bzrlib.revision import Revision
23
from bzrlib.lazy_import import lazy_import
24
lazy_import(globals(), """
34
24
class ForeignBranch(Branch):
90
80
from bzrlib import urlutils
91
81
from bzrlib.bzrdir import BzrDir
92
82
from bzrlib.errors import BzrCommandError, NoWorkingTree
83
from bzrlib.inventory import Inventory
84
from bzrlib.revision import NULL_REVISION
93
85
from bzrlib.trace import info
94
86
from bzrlib.workingtree import WorkingTree
95
from upgrade import update_workingtree_fileids
87
from upgrade import update_workinginv_fileids
89
def get_inv(wt, revid):
90
if revid == NULL_REVISION:
93
return wt.branch.repository.get_inventory(revid)
97
95
if directory is None:
116
114
target_branch = bzrdir.open_branch()
117
115
target_branch.lock_write()
119
if not isinstance(target_branch, ForeignBranch):
117
if getattr(target_branch, "dpull", None) is None:
120
118
info("target branch is not a foreign branch, using regular push.")
121
119
target_branch.pull(source_branch)
133
131
stop_revision=new_last_revid)
134
132
source_wt.lock_write()
136
update_workingtree_fileids(source_wt,
137
source_wt.branch.repository.revision_tree(old_last_revid),
138
source_wt.branch.repository.revision_tree(new_last_revid))
134
update_workinginv_fileids(source_wt,
135
get_inv(source_wt, old_last_revid),
136
get_inv(source_wt, new_last_revid))
140
138
source_wt.unlock()
145
143
target_branch.unlock()
146
class cmd_foreign_mapping_upgrade(Command):
147
"""Upgrade revisions mapped from a foreign version control system
150
This will change the identity of revisions whose parents
151
were mapped from revisions in the other version control system.
153
You are recommended to run "bzr check" in the local repository
154
after running this command.
156
aliases = ['svn-upgrade']
157
takes_args = ['from_repository?']
158
takes_options = ['verbose',
159
Option("idmap-file", help="Write map with old and new revision ids.", type=str)]
161
def run(self, from_repository=None, verbose=False, idmap_file=None):
162
from upgrade import upgrade_branch, upgrade_workingtree
163
from bzrlib.branch import Branch
164
from bzrlib.errors import NoWorkingTree, BzrCommandError
165
from bzrlib.repository import Repository
166
from bzrlib.trace import info
167
from bzrlib.workingtree import WorkingTree
169
wt_to = WorkingTree.open(".")
170
branch_to = wt_to.branch
171
except NoWorkingTree:
173
branch_to = Branch.open(".")
175
stored_loc = branch_to.get_parent()
176
if from_repository is None:
177
if stored_loc is None:
178
raise BzrCommandError("No pull location known or"
181
import bzrlib.urlutils as urlutils
182
display_url = urlutils.unescape_for_display(stored_loc,
184
self.outf.write("Using saved location: %s\n" % display_url)
185
from_repository = Branch.open(stored_loc).repository
187
from_repository = Repository.open(from_repository)
189
vcs = getattr(from_repository, "vcs", None)
191
raise BzrCommandError("Repository at %s is not a foreign repository.a" % from_repository.base)
193
new_mapping = from_repository.get_mapping()
195
if wt_to is not None:
196
renames = upgrade_workingtree(wt_to, from_repository,
197
new_mapping=new_mapping,
198
allow_changes=True, verbose=verbose)
200
renames = upgrade_branch(branch_to, from_repository,
201
new_mapping=new_mapping,
202
allow_changes=True, verbose=verbose)
205
info("Nothing to do.")
207
if idmap_file is not None:
208
f = open(idmap_file, 'w')
210
for oldid, newid in renames.iteritems():
211
f.write("%s\t%s\n" % (oldid, newid))
215
if wt_to is not None:
216
wt_to.set_last_revision(branch_to.last_revision())
147
219
def test_suite():
148
220
from unittest import TestSuite
149
221
from bzrlib.tests import TestUtil