55
54
def transform_tree(from_tree, to_tree, interesting_ids=None):
56
55
from_tree.lock_tree_write()
57
operation = cleanup.OperationWithCleanups(merge_inner)
56
operation = OperationWithCleanups(merge_inner)
58
57
operation.add_cleanup(from_tree.unlock)
59
58
operation.run_simple(from_tree.branch, to_tree, from_tree,
60
59
ignore_zero=True, interesting_ids=interesting_ids, this_tree=from_tree)
63
62
class MergeHooks(hooks.Hooks):
65
64
def __init__(self):
66
hooks.Hooks.__init__(self, "bzrlib.merge", "Merger.hooks")
67
self.add_hook('merge_file_content',
65
hooks.Hooks.__init__(self)
66
self.create_hook(hooks.HookPoint('merge_file_content',
68
67
"Called with a bzrlib.merge.Merger object to create a per file "
69
68
"merge object when starting a merge. "
70
69
"Should return either None or a subclass of "
93
92
"""Attempt to merge the contents of a single file.
95
94
:param merge_params: A bzrlib.merge.MergeHookParams
96
:return: A tuple of (status, chunks), where status is one of
95
:return : A tuple of (status, chunks), where status is one of
97
96
'not_applicable', 'success', 'conflicted', or 'delete'. If status
98
97
is 'success' or 'conflicted', then chunks should be an iterable of
99
98
strings for the new file contents.
459
458
@deprecated_method(deprecated_in((2, 1, 0)))
460
459
def file_revisions(self, file_id):
461
460
self.ensure_revision_trees()
461
def get_id(tree, file_id):
462
revision_id = tree.inventory[file_id].revision
462
464
if self.this_rev_id is None:
463
465
if self.this_basis_tree.get_file_sha1(file_id) != \
464
466
self.this_tree.get_file_sha1(file_id):
465
467
raise errors.WorkingTreeNotRevision(self.this_tree)
467
469
trees = (self.this_basis_tree, self.other_tree)
468
return [tree.get_file_revision(file_id) for tree in trees]
470
return [get_id(tree, file_id) for tree in trees]
470
472
@deprecated_method(deprecated_in((2, 1, 0)))
471
473
def check_basis(self, check_clean, require_commits=True):
499
501
def _add_parent(self):
500
502
new_parents = self.this_tree.get_parent_ids() + [self.other_rev_id]
501
503
new_parent_trees = []
502
operation = cleanup.OperationWithCleanups(
503
self.this_tree.set_parent_trees)
504
operation = OperationWithCleanups(self.this_tree.set_parent_trees)
504
505
for revision_id in new_parents:
506
507
tree = self.revision_tree(revision_id)
593
593
self.base_rev_id = self.revision_graph.find_unique_lca(
595
sorted_lca_keys = self.revision_graph.find_merge_order(
597
if self.base_rev_id == _mod_revision.NULL_REVISION:
598
self.base_rev_id = sorted_lca_keys[0]
595
self._is_criss_cross = True
600
596
if self.base_rev_id == _mod_revision.NULL_REVISION:
601
597
raise errors.UnrelatedBranches()
602
598
if self._is_criss_cross:
603
599
trace.warning('Warning: criss-cross merge encountered. See bzr'
604
600
' help criss-cross.')
605
601
trace.mutter('Criss-cross lcas: %r' % lcas)
606
if self.base_rev_id in lcas:
607
trace.mutter('Unable to find unique lca. '
608
'Fallback %r as best option.' % self.base_rev_id)
609
interesting_revision_ids = set(lcas)
610
interesting_revision_ids.add(self.base_rev_id)
602
interesting_revision_ids = [self.base_rev_id]
603
interesting_revision_ids.extend(lcas)
611
604
interesting_trees = dict((t.get_revision_id(), t)
612
605
for t in self.this_branch.repository.revision_trees(
613
606
interesting_revision_ids))
614
607
self._cached_trees.update(interesting_trees)
615
if self.base_rev_id in lcas:
616
self.base_tree = interesting_trees[self.base_rev_id]
618
self.base_tree = interesting_trees.pop(self.base_rev_id)
608
self.base_tree = interesting_trees.pop(self.base_rev_id)
609
sorted_lca_keys = self.revision_graph.find_merge_order(
619
611
self._lca_trees = [interesting_trees[key]
620
612
for key in sorted_lca_keys]
702
694
def do_merge(self):
703
operation = cleanup.OperationWithCleanups(self._do_merge_to)
695
operation = OperationWithCleanups(self._do_merge_to)
704
696
self.this_tree.lock_tree_write()
705
697
operation.add_cleanup(self.this_tree.unlock)
706
698
if self.base_tree is not None:
712
704
merge = operation.run_simple()
713
705
if len(merge.cooked_conflicts) == 0:
714
706
if not self.ignore_zero and not trace.is_quiet():
715
trace.note(gettext("All changes applied successfully."))
707
trace.note("All changes applied successfully.")
717
trace.note(gettext("%d conflicts encountered.")
709
trace.note("%d conflicts encountered."
718
710
% len(merge.cooked_conflicts))
720
712
return len(merge.cooked_conflicts)
812
804
warnings.warn("pb argument to Merge3Merger is deprecated")
814
806
def do_merge(self):
815
operation = cleanup.OperationWithCleanups(self._do_merge)
807
operation = OperationWithCleanups(self._do_merge)
816
808
self.this_tree.lock_tree_write()
817
809
operation.add_cleanup(self.this_tree.unlock)
818
810
self.base_tree.lock_read()
835
827
def make_preview_transform(self):
836
operation = cleanup.OperationWithCleanups(self._make_preview_transform)
828
operation = OperationWithCleanups(self._make_preview_transform)
837
829
self.base_tree.lock_read()
838
830
operation.add_cleanup(self.base_tree.unlock)
839
831
self.other_tree.lock_read()
859
851
self.active_hooks = [hook for hook in hooks if hook is not None]
860
852
for num, (file_id, changed, parents3, names3,
861
853
executable3) in enumerate(entries):
862
child_pb.update(gettext('Preparing file merge'), num, len(entries))
854
child_pb.update('Preparing file merge', num, len(entries))
863
855
self._merge_names(file_id, parents3, names3, resolver=resolver)
865
857
file_status = self._do_merge_contents(file_id)
889
881
self.tt.iter_changes(), self.change_reporter)
890
882
self.cook_conflicts(fs_conflicts)
891
883
for conflict in self.cooked_conflicts:
892
trace.warning(unicode(conflict))
884
trace.warning(conflict)
894
886
def _entries3(self):
895
887
"""Gather data about files modified between three trees.
904
896
iterator = self.other_tree.iter_changes(self.base_tree,
905
specific_files=self.interesting_files,
897
include_unchanged=True, specific_files=self.interesting_files,
906
898
extra_trees=[self.this_tree])
907
899
this_entries = dict((e.file_id, e) for p, e in
908
900
self.this_tree.iter_entries_by_dir(
934
926
it then compares with THIS and BASE.
936
928
For the multi-valued entries, the format will be (BASE, [lca1, lca2])
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,
929
:return: [(file_id, changed, parents, names, executable)]
930
file_id Simple file_id of the entry
931
changed Boolean, True if the kind or contents changed
933
parents ((base, [parent_id, in, lcas]), parent_id_other,
935
names ((base, [name, in, lcas]), name_in_other, name_in_this)
936
executable ((base, [exec, in, lcas]), exec_in_other, exec_in_this)
948
938
if self.interesting_files is not None:
949
939
lookup_trees = [self.this_tree, self.base_tree]
992
982
lca_entries.append(lca_ie)
994
if base_inventory.has_id(file_id):
984
if file_id in base_inventory:
995
985
base_ie = base_inventory[file_id]
997
987
base_ie = _none_entry
999
if this_inventory.has_id(file_id):
989
if file_id in this_inventory:
1000
990
this_ie = this_inventory[file_id]
1002
992
this_ie = _none_entry
1097
@deprecated_method(deprecated_in((2, 4, 0)))
1098
1087
def fix_root(self):
1099
if self.tt.final_kind(self.tt.root) is None:
1089
self.tt.final_kind(self.tt.root)
1090
except errors.NoSuchFile:
1100
1091
self.tt.cancel_deletion(self.tt.root)
1101
1092
if self.tt.final_file_id(self.tt.root) is None:
1102
1093
self.tt.version_file(self.tt.tree_file_id(self.tt.root),
1107
1098
other_root = self.tt.trans_id_file_id(other_root_file_id)
1108
1099
if other_root == self.tt.root:
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)
1101
if self.other_tree.inventory.root.file_id in self.this_tree.inventory:
1102
# the other tree's root is a non-root in the current tree (as when
1103
# a previously unrelated branch is merged into another)
1115
if self.tt.final_kind(other_root) is not None:
1106
self.tt.final_kind(other_root)
1116
1107
other_root_is_present = True
1108
except errors.NoSuchFile:
1118
1109
# other_root doesn't have a physical representation. We still need
1119
1110
# to move any references to the actual root of the tree.
1120
1111
other_root_is_present = False
1121
1112
# 'other_tree.inventory.root' is not present in this tree. We are
1122
1113
# calling adjust_path for children which *want* to be present with a
1123
1114
# correct place to go.
1124
for _, child in self.other_tree.inventory.root.children.iteritems():
1115
for thing, child in self.other_tree.inventory.root.children.iteritems():
1125
1116
trans_id = self.tt.trans_id_file_id(child.file_id)
1126
1117
if not other_root_is_present:
1127
if self.tt.final_kind(trans_id) is not None:
1118
# FIXME: Make final_kind returns None instead of raising
1119
# NoSuchFile to avoid the ugly construct below -- vila 20100402
1121
self.tt.final_kind(trans_id)
1128
1122
# The item exist in the final tree and has a defined place
1129
1123
# to go already.
1125
except errors.NoSuchFile, e:
1131
1127
# Move the item into the root
1133
final_name = self.tt.final_name(trans_id)
1134
except errors.NoFinalPath:
1135
# This file is not present anymore, ignore it.
1137
self.tt.adjust_path(final_name, self.tt.root, trans_id)
1128
self.tt.adjust_path(self.tt.final_name(trans_id),
1129
self.tt.root, trans_id)
1138
1130
if other_root_is_present:
1139
1131
self.tt.cancel_creation(other_root)
1140
1132
self.tt.cancel_versioning(other_root)
1319
1311
self._raw_conflicts.append(('path conflict', trans_id, file_id,
1320
1312
this_parent, this_name,
1321
1313
other_parent, other_name))
1322
if not self.other_tree.has_id(file_id):
1314
if other_name is None:
1323
1315
# it doesn't matter whether the result was 'other' or
1324
# 'conflict'-- if it has no file id, we leave it alone.
1316
# 'conflict'-- if there's no 'other', we leave it alone.
1326
1318
parent_id = parents[self.winner_idx[parent_id_winner]]
1327
name = names[self.winner_idx[name_winner]]
1328
if parent_id is not None or name is not None:
1319
if parent_id is not None:
1329
1320
# if we get here, name_winner and parent_winner are set to safe
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,
1322
self.tt.adjust_path(names[self.winner_idx[name_winner]],
1323
self.tt.trans_id_file_id(parent_id),
1342
1324
self.tt.trans_id_file_id(file_id))
1344
1326
def _do_merge_contents(self, file_id):
1345
1327
"""Performs a merge on file_id contents."""
1346
1328
def contents_pair(tree):
1347
if not tree.has_id(file_id):
1329
if file_id not in tree:
1348
1330
return (None, None)
1349
1331
kind = tree.kind(file_id)
1350
1332
if kind == "file":
1423
1405
self.tt.version_file(file_id, trans_id)
1424
1406
# The merge has been performed, so the old contents should not be
1426
self.tt.delete_contents(trans_id)
1409
self.tt.delete_contents(trans_id)
1410
except errors.NoSuchFile:
1429
1414
def _default_other_winner_merge(self, merge_hook_params):
1601
1586
if winner == 'this' and file_status != "modified":
1603
1588
trans_id = self.tt.trans_id_file_id(file_id)
1604
if self.tt.final_kind(trans_id) != "file":
1590
if self.tt.final_kind(trans_id) != "file":
1592
except errors.NoSuchFile:
1606
1594
if winner == "this":
1607
1595
executability = this_executable
1619
1607
def cook_conflicts(self, fs_conflicts):
1620
1608
"""Convert all conflicts into a form that doesn't depend on trans_id"""
1621
content_conflict_file_ids = set()
1622
cooked_conflicts = transform.cook_conflicts(fs_conflicts, self.tt)
1609
self.cooked_conflicts.extend(transform.cook_conflicts(
1610
fs_conflicts, self.tt))
1623
1611
fp = transform.FinalPaths(self.tt)
1624
1612
for conflict in self._raw_conflicts:
1625
1613
conflict_type = conflict[0]
1636
1624
if other_parent is None or other_name is None:
1637
1625
other_path = '<deleted>'
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))
1627
parent_path = fp.get_path(
1628
self.tt.trans_id_file_id(other_parent))
1647
1629
other_path = osutils.pathjoin(parent_path, other_name)
1648
1630
c = _mod_conflicts.Conflict.factory(
1649
1631
'path conflict', path=this_path,
1662
1644
c = _mod_conflicts.Conflict.factory(conflict_type,
1663
1645
path=path, file_id=file_id)
1664
content_conflict_file_ids.add(file_id)
1665
1646
elif conflict_type == 'text conflict':
1666
1647
trans_id = conflict[1]
1667
1648
path = fp.get_path(trans_id)
1670
1651
path=path, file_id=file_id)
1672
1653
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):
1684
1654
self.cooked_conflicts.append(c)
1685
1655
self.cooked_conflicts.sort(key=_mod_conflicts.Conflict.sort_key)
1907
1877
entries = self._entries_to_incorporate()
1908
1878
entries = list(entries)
1909
1879
for num, (entry, parent_id) in enumerate(entries):
1910
child_pb.update(gettext('Preparing file merge'), num, len(entries))
1880
child_pb.update('Preparing file merge', num, len(entries))
1911
1881
parent_trans_id = self.tt.trans_id_file_id(parent_id)
1912
1882
trans_id = transform.new_by_entry(self.tt, entry,
1913
1883
parent_trans_id, self.other_tree)
1931
1901
name_in_target = osutils.basename(self._target_subdir)
1932
1902
merge_into_root = subdir.copy()
1933
1903
merge_into_root.name = name_in_target
1934
if self.this_tree.inventory.has_id(merge_into_root.file_id):
1904
if merge_into_root.file_id in self.this_tree.inventory:
1935
1905
# Give the root a new file-id.
1936
1906
# This can happen fairly easily if the directory we are
1937
1907
# incorporating is the root, and both trees have 'TREE_ROOT' as
1975
1945
if this_tree is None:
1976
1946
raise errors.BzrError("bzrlib.merge.merge_inner requires a this_tree "
1947
"parameter as of bzrlib version 0.8.")
1978
1948
merger = Merger(this_branch, other_tree, base_tree, this_tree=this_tree,
1979
1949
pb=pb, change_reporter=change_reporter)
1980
1950
merger.backup_files = backup_files
2434
2404
class _PlanLCAMerge(_PlanMergeBase):
2436
2406
This merge algorithm differs from _PlanMerge in that:
2438
2407
1. comparisons are done against LCAs only
2439
2408
2. cases where a contested line is new versus one LCA but old versus
2440
2409
another are marked as conflicts, by emitting the line as conflicted-a
2482
2451
If a line is killed and new, this indicates that the two merge
2483
2452
revisions contain differing conflict resolutions.
2485
2453
:param revision_id: The id of the revision in which the lines are
2487
2455
:param unique_line_numbers: The line numbers of unique lines.
2488
:return: a tuple of (new_this, killed_other)
2456
:return a tuple of (new_this, killed_other):