92
93
"""Attempt to merge the contents of a single file.
94
95
:param merge_params: A bzrlib.merge.MergeHookParams
95
:return : A tuple of (status, chunks), where status is one of
96
:return: A tuple of (status, chunks), where status is one of
96
97
'not_applicable', 'success', 'conflicted', or 'delete'. If status
97
98
is 'success' or 'conflicted', then chunks should be an iterable of
98
99
strings for the new file contents.
711
712
merge = operation.run_simple()
712
713
if len(merge.cooked_conflicts) == 0:
713
714
if not self.ignore_zero and not trace.is_quiet():
714
trace.note("All changes applied successfully.")
715
trace.note(gettext("All changes applied successfully."))
716
trace.note("%d conflicts encountered."
717
trace.note(gettext("%d conflicts encountered.")
717
718
% len(merge.cooked_conflicts))
719
720
return len(merge.cooked_conflicts)
858
859
self.active_hooks = [hook for hook in hooks if hook is not None]
859
860
for num, (file_id, changed, parents3, names3,
860
861
executable3) in enumerate(entries):
861
child_pb.update('Preparing file merge', num, len(entries))
862
child_pb.update(gettext('Preparing file merge'), num, len(entries))
862
863
self._merge_names(file_id, parents3, names3, resolver=resolver)
864
865
file_status = self._do_merge_contents(file_id)
868
869
executable3, file_status, resolver=resolver)
870
871
child_pb.finished()
872
self.tt.fixup_new_roots()
872
873
self._finish_computing_transform()
874
875
def _finish_computing_transform(self):
888
889
self.tt.iter_changes(), self.change_reporter)
889
890
self.cook_conflicts(fs_conflicts)
890
891
for conflict in self.cooked_conflicts:
891
trace.warning(conflict)
892
trace.warning(unicode(conflict))
893
894
def _entries3(self):
894
895
"""Gather data about files modified between three trees.
933
934
it then compares with THIS and BASE.
935
936
For the multi-valued entries, the format will be (BASE, [lca1, lca2])
936
:return: [(file_id, changed, parents, names, executable)]
937
file_id Simple file_id of the entry
938
changed Boolean, True if the kind or contents changed
940
parents ((base, [parent_id, in, lcas]), parent_id_other,
942
names ((base, [name, in, lcas]), name_in_other, name_in_this)
943
executable ((base, [exec, in, lcas]), exec_in_other, exec_in_this)
938
:return: [(file_id, changed, parents, names, executable)], where:
940
* file_id: Simple file_id of the entry
941
* changed: Boolean, True if the kind or contents changed else False
942
* parents: ((base, [parent_id, in, lcas]), parent_id_other,
944
* names: ((base, [name, in, lcas]), name_in_other, name_in_this)
945
* executable: ((base, [exec, in, lcas]), exec_in_other,
945
948
if self.interesting_files is not None:
946
949
lookup_trees = [self.this_tree, self.base_tree]
989
992
lca_entries.append(lca_ie)
991
if file_id in base_inventory:
994
if base_inventory.has_id(file_id):
992
995
base_ie = base_inventory[file_id]
994
997
base_ie = _none_entry
996
if file_id in this_inventory:
999
if this_inventory.has_id(file_id):
997
1000
this_ie = this_inventory[file_id]
999
1002
this_ie = _none_entry
1103
1107
other_root = self.tt.trans_id_file_id(other_root_file_id)
1104
1108
if other_root == self.tt.root:
1106
if self.other_tree.inventory.root.file_id in self.this_tree.inventory:
1107
# the other tree's root is a non-root in the current tree (as when
1108
# a previously unrelated branch is merged into another)
1110
if self.this_tree.inventory.has_id(
1111
self.other_tree.inventory.root.file_id):
1112
# the other tree's root is a non-root in the current tree (as
1113
# when a previously unrelated branch is merged into another)
1110
1115
if self.tt.final_kind(other_root) is not None:
1111
1116
other_root_is_present = True
1164
1169
def contents_sha1(tree, file_id):
1165
1170
"""Determine the sha1 of the file contents (used as a key method)."""
1166
if file_id not in tree:
1171
if not tree.has_id(file_id):
1168
1173
return tree.get_file_sha1(file_id)
1314
1319
self._raw_conflicts.append(('path conflict', trans_id, file_id,
1315
1320
this_parent, this_name,
1316
1321
other_parent, other_name))
1317
if other_name is None:
1322
if not self.other_tree.has_id(file_id):
1318
1323
# it doesn't matter whether the result was 'other' or
1319
# 'conflict'-- if there's no 'other', we leave it alone.
1324
# 'conflict'-- if it has no file id, we leave it alone.
1321
1326
parent_id = parents[self.winner_idx[parent_id_winner]]
1322
if parent_id is not None:
1327
name = names[self.winner_idx[name_winner]]
1328
if parent_id is not None or name is not None:
1323
1329
# if we get here, name_winner and parent_winner are set to safe
1325
self.tt.adjust_path(names[self.winner_idx[name_winner]],
1326
self.tt.trans_id_file_id(parent_id),
1331
if parent_id is None and name is not None:
1332
# if parent_id is None and name is non-None, current file is
1334
if names[self.winner_idx[parent_id_winner]] != '':
1335
raise AssertionError(
1336
'File looks like a root, but named %s' %
1337
names[self.winner_idx[parent_id_winner]])
1338
parent_trans_id = transform.ROOT_PARENT
1340
parent_trans_id = self.tt.trans_id_file_id(parent_id)
1341
self.tt.adjust_path(name, parent_trans_id,
1327
1342
self.tt.trans_id_file_id(file_id))
1329
1344
def _do_merge_contents(self, file_id):
1330
1345
"""Performs a merge on file_id contents."""
1331
1346
def contents_pair(tree):
1332
if file_id not in tree:
1347
if not tree.has_id(file_id):
1333
1348
return (None, None)
1334
1349
kind = tree.kind(file_id)
1335
1350
if kind == "file":
1604
1619
def cook_conflicts(self, fs_conflicts):
1605
1620
"""Convert all conflicts into a form that doesn't depend on trans_id"""
1606
self.cooked_conflicts.extend(transform.cook_conflicts(
1607
fs_conflicts, self.tt))
1621
content_conflict_file_ids = set()
1622
cooked_conflicts = transform.cook_conflicts(fs_conflicts, self.tt)
1608
1623
fp = transform.FinalPaths(self.tt)
1609
1624
for conflict in self._raw_conflicts:
1610
1625
conflict_type = conflict[0]
1621
1636
if other_parent is None or other_name is None:
1622
1637
other_path = '<deleted>'
1624
parent_path = fp.get_path(
1625
self.tt.trans_id_file_id(other_parent))
1639
if other_parent == self.other_tree.get_root_id():
1640
# The tree transform doesn't know about the other root,
1641
# so we special case here to avoid a NoFinalPath
1645
parent_path = fp.get_path(
1646
self.tt.trans_id_file_id(other_parent))
1626
1647
other_path = osutils.pathjoin(parent_path, other_name)
1627
1648
c = _mod_conflicts.Conflict.factory(
1628
1649
'path conflict', path=this_path,
1641
1662
c = _mod_conflicts.Conflict.factory(conflict_type,
1642
1663
path=path, file_id=file_id)
1664
content_conflict_file_ids.add(file_id)
1643
1665
elif conflict_type == 'text conflict':
1644
1666
trans_id = conflict[1]
1645
1667
path = fp.get_path(trans_id)
1648
1670
path=path, file_id=file_id)
1650
1672
raise AssertionError('bad conflict type: %r' % (conflict,))
1673
cooked_conflicts.append(c)
1675
self.cooked_conflicts = []
1676
# We want to get rid of path conflicts when a corresponding contents
1677
# conflict exists. This can occur when one branch deletes a file while
1678
# the other renames *and* modifies it. In this case, the content
1679
# conflict is enough.
1680
for c in cooked_conflicts:
1681
if (c.typestring == 'path conflict'
1682
and c.file_id in content_conflict_file_ids):
1651
1684
self.cooked_conflicts.append(c)
1652
1685
self.cooked_conflicts.sort(key=_mod_conflicts.Conflict.sort_key)
1874
1907
entries = self._entries_to_incorporate()
1875
1908
entries = list(entries)
1876
1909
for num, (entry, parent_id) in enumerate(entries):
1877
child_pb.update('Preparing file merge', num, len(entries))
1910
child_pb.update(gettext('Preparing file merge'), num, len(entries))
1878
1911
parent_trans_id = self.tt.trans_id_file_id(parent_id)
1879
1912
trans_id = transform.new_by_entry(self.tt, entry,
1880
1913
parent_trans_id, self.other_tree)
1898
1931
name_in_target = osutils.basename(self._target_subdir)
1899
1932
merge_into_root = subdir.copy()
1900
1933
merge_into_root.name = name_in_target
1901
if merge_into_root.file_id in self.this_tree.inventory:
1934
if self.this_tree.inventory.has_id(merge_into_root.file_id):
1902
1935
# Give the root a new file-id.
1903
1936
# This can happen fairly easily if the directory we are
1904
1937
# incorporating is the root, and both trees have 'TREE_ROOT' as
1942
1975
if this_tree is None:
1943
1976
raise errors.BzrError("bzrlib.merge.merge_inner requires a this_tree "
1944
"parameter as of bzrlib version 0.8.")
1945
1978
merger = Merger(this_branch, other_tree, base_tree, this_tree=this_tree,
1946
1979
pb=pb, change_reporter=change_reporter)
1947
1980
merger.backup_files = backup_files
2401
2434
class _PlanLCAMerge(_PlanMergeBase):
2403
2436
This merge algorithm differs from _PlanMerge in that:
2404
2438
1. comparisons are done against LCAs only
2405
2439
2. cases where a contested line is new versus one LCA but old versus
2406
2440
another are marked as conflicts, by emitting the line as conflicted-a
2448
2482
If a line is killed and new, this indicates that the two merge
2449
2483
revisions contain differing conflict resolutions.
2450
2485
:param revision_id: The id of the revision in which the lines are
2452
2487
:param unique_line_numbers: The line numbers of unique lines.
2453
:return a tuple of (new_this, killed_other):
2488
:return: a tuple of (new_this, killed_other)