245
245
self.get_revision(revision_id))
248
class ForeignBranch(Branch):
249
"""Branch that exists in a foreign version control system."""
251
def __init__(self, mapping):
252
self.mapping = mapping
253
super(ForeignBranch, self).__init__()
255
def dpull(self, source, stop_revision=None):
256
"""Pull deltas from another branch.
258
:note: This does not, like pull, retain the revision ids from
259
the source branch and will, rather than adding bzr-specific
260
metadata, push only those semantics of the revision that can be
261
natively represented by this branch' VCS.
263
:param source: Source branch
264
:param stop_revision: Revision to pull, defaults to last revision.
265
:return: Dictionary mapping revision ids from the source branch
266
to new revision ids in the target branch, for each
267
revision that was pull.
269
raise NotImplementedError(self.dpull)
272
def _determine_fileid_renames(old_inv, new_inv):
273
"""Determine the file ids based on a old and a new inventory that
274
are equal in content.
276
:param old_inv: Old inventory
277
:param new_inv: New inventory
278
:return: Dictionary a (old_id, new_id) tuple for each path in the
282
if len(old_inv) != len(new_inv):
283
raise AssertionError("Inventories are not of the same size")
284
for old_file_id in old_inv:
285
path = old_inv.id2path(old_file_id)
286
new_file_id = new_inv.path2id(path)
287
if new_file_id is None:
288
raise AssertionError(
289
"Unable to find %s in new inventory" % old_file_id)
290
ret[path] = (old_file_id, new_file_id)
294
def update_workinginv_fileids(wt, old_inv, new_inv):
295
"""Update all file ids in wt according to old_tree/new_tree.
297
old_tree and new_tree should be two RevisionTree's that differ only
300
fileid_renames = _determine_fileid_renames(old_inv, new_inv)
304
# Adjust file ids in working tree
305
# Sorted, so we process parents before children
306
for path in sorted(fileid_renames.keys()):
307
(old_fileid, new_fileid) = fileid_renames[path]
309
new_fileids.append((path, new_fileid))
310
# unversion() works recursively so we only have to unversion the
311
# top-level. Unfortunately unversioning / is not supported yet,
312
# so unversion its children instead and use set_root_id() for /
313
if old_inv[old_fileid].parent_id == old_inv.root.file_id:
314
old_fileids.append(old_fileid)
316
new_root_id = new_fileid
317
new_fileids.reverse()
318
wt.unversion(old_fileids)
319
if new_root_id is not None:
320
wt.set_root_id(new_root_id)
321
wt.add([x[0] for x in new_fileids], [x[1] for x in new_fileids])
322
wt.set_last_revision(new_inv.revision_id)
325
class cmd_dpush(Command):
326
"""Push diffs into a foreign version control system without any
327
Bazaar-specific metadata.
329
This will afterwards rebase the local Bazaar branch on the remote
330
branch unless the --no-rebase option is used, in which case
331
the two branches will be out of sync.
334
takes_args = ['location?']
335
takes_options = ['remember', Option('directory',
336
help='Branch to push from, '
337
'rather than the one containing the working directory.',
341
Option('no-rebase', help="Do not rebase after push.")]
343
def run(self, location=None, remember=False, directory=None,
345
from bzrlib import urlutils
346
from bzrlib.bzrdir import BzrDir
347
from bzrlib.errors import BzrCommandError, NoWorkingTree
348
from bzrlib.trace import info
349
from bzrlib.workingtree import WorkingTree
351
if directory is None:
354
source_wt = WorkingTree.open_containing(directory)[0]
355
source_branch = source_wt.branch
356
except NoWorkingTree:
357
source_branch = Branch.open(directory)
359
stored_loc = source_branch.get_push_location()
361
if stored_loc is None:
362
raise BzrCommandError("No push location known or specified.")
364
display_url = urlutils.unescape_for_display(stored_loc,
366
self.outf.write("Using saved location: %s\n" % display_url)
367
location = stored_loc
369
bzrdir = BzrDir.open(location)
370
target_branch = bzrdir.open_branch()
371
dpull = getattr(target_branch, "dpull", None)
373
raise BzrCommandError("%r is not a foreign branch, use "
374
"regular push." % target_branch)
375
target_branch.lock_write()
377
revid_map = dpull(source_branch)
378
# We successfully created the target, remember it
379
if source_branch.get_push_location() is None or remember:
380
source_branch.set_push_location(target_branch.base)
382
old_last_revid = source_branch.last_revision()
383
source_branch.pull(target_branch, overwrite=True)
384
new_last_revid = source_branch.last_revision()
385
if source_wt is not None and old_last_revid != new_last_revid:
386
source_wt.lock_write()
388
update_workinginv_fileids(source_wt,
389
source_wt.branch.repository.get_inventory(
391
source_wt.branch.repository.get_inventory(
396
target_branch.unlock()