587
594
self.base_rev_id = self.revision_graph.find_unique_lca(
589
self._is_criss_cross = True
596
sorted_lca_keys = self.revision_graph.find_merge_order(
598
if self.base_rev_id == _mod_revision.NULL_REVISION:
599
self.base_rev_id = sorted_lca_keys[0]
590
601
if self.base_rev_id == _mod_revision.NULL_REVISION:
591
602
raise errors.UnrelatedBranches()
592
603
if self._is_criss_cross:
593
604
trace.warning('Warning: criss-cross merge encountered. See bzr'
594
605
' help criss-cross.')
595
606
trace.mutter('Criss-cross lcas: %r' % lcas)
596
interesting_revision_ids = [self.base_rev_id]
597
interesting_revision_ids.extend(lcas)
607
if self.base_rev_id in lcas:
608
trace.mutter('Unable to find unique lca. '
609
'Fallback %r as best option.' % self.base_rev_id)
610
interesting_revision_ids = set(lcas)
611
interesting_revision_ids.add(self.base_rev_id)
598
612
interesting_trees = dict((t.get_revision_id(), t)
599
613
for t in self.this_branch.repository.revision_trees(
600
614
interesting_revision_ids))
601
615
self._cached_trees.update(interesting_trees)
602
self.base_tree = interesting_trees.pop(self.base_rev_id)
603
sorted_lca_keys = self.revision_graph.find_merge_order(
616
if self.base_rev_id in lcas:
617
self.base_tree = interesting_trees[self.base_rev_id]
619
self.base_tree = interesting_trees.pop(self.base_rev_id)
605
620
self._lca_trees = [interesting_trees[key]
606
621
for key in sorted_lca_keys]
1750
1757
osutils.rmtree(temp_dir)
1760
class PathNotInTree(errors.BzrError):
1762
_fmt = """Merge-into failed because %(tree)s does not contain %(path)s."""
1764
def __init__(self, path, tree):
1765
errors.BzrError.__init__(self, path=path, tree=tree)
1768
class MergeIntoMerger(Merger):
1769
"""Merger that understands other_tree will be merged into a subdir.
1771
This also changes the Merger api so that it uses real Branch, revision_id,
1772
and RevisonTree objects, rather than using revision specs.
1775
def __init__(self, this_tree, other_branch, other_tree, target_subdir,
1776
source_subpath, other_rev_id=None):
1777
"""Create a new MergeIntoMerger object.
1779
source_subpath in other_tree will be effectively copied to
1780
target_subdir in this_tree.
1782
:param this_tree: The tree that we will be merging into.
1783
:param other_branch: The Branch we will be merging from.
1784
:param other_tree: The RevisionTree object we want to merge.
1785
:param target_subdir: The relative path where we want to merge
1786
other_tree into this_tree
1787
:param source_subpath: The relative path specifying the subtree of
1788
other_tree to merge into this_tree.
1790
# It is assumed that we are merging a tree that is not in our current
1791
# ancestry, which means we are using the "EmptyTree" as our basis.
1792
null_ancestor_tree = this_tree.branch.repository.revision_tree(
1793
_mod_revision.NULL_REVISION)
1794
super(MergeIntoMerger, self).__init__(
1795
this_branch=this_tree.branch,
1796
this_tree=this_tree,
1797
other_tree=other_tree,
1798
base_tree=null_ancestor_tree,
1800
self._target_subdir = target_subdir
1801
self._source_subpath = source_subpath
1802
self.other_branch = other_branch
1803
if other_rev_id is None:
1804
other_rev_id = other_tree.get_revision_id()
1805
self.other_rev_id = self.other_basis = other_rev_id
1806
self.base_is_ancestor = True
1807
self.backup_files = True
1808
self.merge_type = Merge3Merger
1809
self.show_base = False
1810
self.reprocess = False
1811
self.interesting_ids = None
1812
self.merge_type = _MergeTypeParameterizer(MergeIntoMergeType,
1813
target_subdir=self._target_subdir,
1814
source_subpath=self._source_subpath)
1815
if self._source_subpath != '':
1816
# If this isn't a partial merge make sure the revisions will be
1818
self._maybe_fetch(self.other_branch, self.this_branch,
1821
def set_pending(self):
1822
if self._source_subpath != '':
1824
Merger.set_pending(self)
1827
class _MergeTypeParameterizer(object):
1828
"""Wrap a merge-type class to provide extra parameters.
1830
This is hack used by MergeIntoMerger to pass some extra parameters to its
1831
merge_type. Merger.do_merge() sets up its own set of parameters to pass to
1832
the 'merge_type' member. It is difficult override do_merge without
1833
re-writing the whole thing, so instead we create a wrapper which will pass
1834
the extra parameters.
1837
def __init__(self, merge_type, **kwargs):
1838
self._extra_kwargs = kwargs
1839
self._merge_type = merge_type
1841
def __call__(self, *args, **kwargs):
1842
kwargs.update(self._extra_kwargs)
1843
return self._merge_type(*args, **kwargs)
1845
def __getattr__(self, name):
1846
return getattr(self._merge_type, name)
1849
class MergeIntoMergeType(Merge3Merger):
1850
"""Merger that incorporates a tree (or part of a tree) into another."""
1852
def __init__(self, *args, **kwargs):
1853
"""Initialize the merger object.
1855
:param args: See Merge3Merger.__init__'s args.
1856
:param kwargs: See Merge3Merger.__init__'s keyword args, except for
1857
source_subpath and target_subdir.
1858
:keyword source_subpath: The relative path specifying the subtree of
1859
other_tree to merge into this_tree.
1860
:keyword target_subdir: The relative path where we want to merge
1861
other_tree into this_tree
1863
# All of the interesting work happens during Merge3Merger.__init__(),
1864
# so we have have to hack in to get our extra parameters set.
1865
self._source_subpath = kwargs.pop('source_subpath')
1866
self._target_subdir = kwargs.pop('target_subdir')
1867
super(MergeIntoMergeType, self).__init__(*args, **kwargs)
1869
def _compute_transform(self):
1870
child_pb = ui.ui_factory.nested_progress_bar()
1872
entries = self._entries_to_incorporate()
1873
entries = list(entries)
1874
for num, (entry, parent_id) in enumerate(entries):
1875
child_pb.update('Preparing file merge', num, len(entries))
1876
parent_trans_id = self.tt.trans_id_file_id(parent_id)
1877
trans_id = transform.new_by_entry(self.tt, entry,
1878
parent_trans_id, self.other_tree)
1881
self._finish_computing_transform()
1883
def _entries_to_incorporate(self):
1884
"""Yields pairs of (inventory_entry, new_parent)."""
1885
other_inv = self.other_tree.inventory
1886
subdir_id = other_inv.path2id(self._source_subpath)
1887
if subdir_id is None:
1888
# XXX: The error would be clearer if it gave the URL of the source
1889
# branch, but we don't have a reference to that here.
1890
raise PathNotInTree(self._source_subpath, "Source tree")
1891
subdir = other_inv[subdir_id]
1892
parent_in_target = osutils.dirname(self._target_subdir)
1893
target_id = self.this_tree.inventory.path2id(parent_in_target)
1894
if target_id is None:
1895
raise PathNotInTree(self._target_subdir, "Target tree")
1896
name_in_target = osutils.basename(self._target_subdir)
1897
merge_into_root = subdir.copy()
1898
merge_into_root.name = name_in_target
1899
if merge_into_root.file_id in self.this_tree.inventory:
1900
# Give the root a new file-id.
1901
# This can happen fairly easily if the directory we are
1902
# incorporating is the root, and both trees have 'TREE_ROOT' as
1903
# their root_id. Users will expect this to Just Work, so we
1904
# change the file-id here.
1905
# Non-root file-ids could potentially conflict too. That's really
1906
# an edge case, so we don't do anything special for those. We let
1907
# them cause conflicts.
1908
merge_into_root.file_id = generate_ids.gen_file_id(name_in_target)
1909
yield (merge_into_root, target_id)
1910
if subdir.kind != 'directory':
1911
# No children, so we are done.
1913
for ignored_path, entry in other_inv.iter_entries_by_dir(subdir_id):
1914
parent_id = entry.parent_id
1915
if parent_id == subdir.file_id:
1916
# The root's parent ID has changed, so make sure children of
1917
# the root refer to the new ID.
1918
parent_id = merge_into_root.file_id
1919
yield (entry, parent_id)
1753
1922
def merge_inner(this_branch, other_tree, base_tree, ignore_zero=False,
1754
1923
backup_files=False,
1755
1924
merge_type=Merge3Merger,