54
56
def transform_tree(from_tree, to_tree, interesting_ids=None):
55
57
from_tree.lock_tree_write()
56
operation = OperationWithCleanups(merge_inner)
58
operation = cleanup.OperationWithCleanups(merge_inner)
57
59
operation.add_cleanup(from_tree.unlock)
58
60
operation.run_simple(from_tree.branch, to_tree, from_tree,
59
61
ignore_zero=True, interesting_ids=interesting_ids, this_tree=from_tree)
62
64
class MergeHooks(hooks.Hooks):
64
66
def __init__(self):
65
hooks.Hooks.__init__(self)
66
self.create_hook(hooks.HookPoint('merge_file_content',
67
hooks.Hooks.__init__(self, "bzrlib.merge", "Merger.hooks")
68
self.add_hook('merge_file_content',
67
69
"Called with a bzrlib.merge.Merger object to create a per file "
68
70
"merge object when starting a merge. "
69
71
"Should return either None or a subclass of "
92
94
"""Attempt to merge the contents of a single file.
94
96
:param merge_params: A bzrlib.merge.MergeHookParams
95
:return : A tuple of (status, chunks), where status is one of
97
:return: A tuple of (status, chunks), where status is one of
96
98
'not_applicable', 'success', 'conflicted', or 'delete'. If status
97
99
is 'success' or 'conflicted', then chunks should be an iterable of
98
100
strings for the new file contents.
138
140
params.winner == 'other' or
139
141
# THIS and OTHER aren't both files.
140
142
not params.is_file_merge() or
141
# The filename doesn't match *.xml
143
# The filename doesn't match
142
144
not self.file_matches(params)):
143
145
return 'not_applicable', None
144
146
return self.merge_matching(params)
458
460
@deprecated_method(deprecated_in((2, 1, 0)))
459
461
def file_revisions(self, file_id):
460
462
self.ensure_revision_trees()
461
def get_id(tree, file_id):
462
revision_id = tree.inventory[file_id].revision
464
463
if self.this_rev_id is None:
465
464
if self.this_basis_tree.get_file_sha1(file_id) != \
466
465
self.this_tree.get_file_sha1(file_id):
467
466
raise errors.WorkingTreeNotRevision(self.this_tree)
469
468
trees = (self.this_basis_tree, self.other_tree)
470
return [get_id(tree, file_id) for tree in trees]
469
return [tree.get_file_revision(file_id) for tree in trees]
472
471
@deprecated_method(deprecated_in((2, 1, 0)))
473
472
def check_basis(self, check_clean, require_commits=True):
593
594
self.base_rev_id = self.revision_graph.find_unique_lca(
595
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]
596
601
if self.base_rev_id == _mod_revision.NULL_REVISION:
597
602
raise errors.UnrelatedBranches()
598
603
if self._is_criss_cross:
599
604
trace.warning('Warning: criss-cross merge encountered. See bzr'
600
605
' help criss-cross.')
601
606
trace.mutter('Criss-cross lcas: %r' % lcas)
602
interesting_revision_ids = [self.base_rev_id]
603
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.'
611
interesting_revision_ids = set(lcas)
612
interesting_revision_ids.add(self.base_rev_id)
604
613
interesting_trees = dict((t.get_revision_id(), t)
605
614
for t in self.this_branch.repository.revision_trees(
606
615
interesting_revision_ids))
607
616
self._cached_trees.update(interesting_trees)
608
self.base_tree = interesting_trees.pop(self.base_rev_id)
609
sorted_lca_keys = self.revision_graph.find_merge_order(
617
if self.base_rev_id in lcas:
618
self.base_tree = interesting_trees[self.base_rev_id]
620
self.base_tree = interesting_trees.pop(self.base_rev_id)
611
621
self._lca_trees = [interesting_trees[key]
612
622
for key in sorted_lca_keys]
704
715
merge = operation.run_simple()
705
716
if len(merge.cooked_conflicts) == 0:
706
717
if not self.ignore_zero and not trace.is_quiet():
707
trace.note("All changes applied successfully.")
718
trace.note(gettext("All changes applied successfully."))
709
trace.note("%d conflicts encountered."
720
trace.note(gettext("%d conflicts encountered.")
710
721
% len(merge.cooked_conflicts))
712
723
return len(merge.cooked_conflicts)
845
856
entries = self._entries_lca()
846
857
resolver = self._lca_multi_way
858
# Prepare merge hooks
859
factories = Merger.hooks['merge_file_content']
860
# One hook for each registered one plus our default merger
861
hooks = [factory(self) for factory in factories] + [self]
862
self.active_hooks = [hook for hook in hooks if hook is not None]
847
863
child_pb = ui.ui_factory.nested_progress_bar()
849
factories = Merger.hooks['merge_file_content']
850
hooks = [factory(self) for factory in factories] + [self]
851
self.active_hooks = [hook for hook in hooks if hook is not None]
852
865
for num, (file_id, changed, parents3, names3,
853
866
executable3) in enumerate(entries):
854
child_pb.update('Preparing file merge', num, len(entries))
867
# Try merging each entry
868
child_pb.update(gettext('Preparing file merge'),
855
870
self._merge_names(file_id, parents3, names3, resolver=resolver)
857
872
file_status = self._do_merge_contents(file_id)
896
911
iterator = self.other_tree.iter_changes(self.base_tree,
897
include_unchanged=True, specific_files=self.interesting_files,
912
specific_files=self.interesting_files,
898
913
extra_trees=[self.this_tree])
899
914
this_entries = dict((e.file_id, e) for p, e in
900
915
self.this_tree.iter_entries_by_dir(
926
941
it then compares with THIS and BASE.
928
943
For the multi-valued entries, the format will be (BASE, [lca1, lca2])
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)
945
:return: [(file_id, changed, parents, names, executable)], where:
947
* file_id: Simple file_id of the entry
948
* changed: Boolean, True if the kind or contents changed else False
949
* parents: ((base, [parent_id, in, lcas]), parent_id_other,
951
* names: ((base, [name, in, lcas]), name_in_other, name_in_this)
952
* executable: ((base, [exec, in, lcas]), exec_in_other,
938
955
if self.interesting_files is not None:
939
956
lookup_trees = [self.this_tree, self.base_tree]
1096
1114
other_root = self.tt.trans_id_file_id(other_root_file_id)
1097
1115
if other_root == self.tt.root:
1099
if self.other_tree.inventory.root.file_id in self.this_tree.inventory:
1100
# the other tree's root is a non-root in the current tree (as when
1101
# a previously unrelated branch is merged into another)
1117
if self.this_tree.inventory.has_id(
1118
self.other_tree.inventory.root.file_id):
1119
# the other tree's root is a non-root in the current tree (as
1120
# when a previously unrelated branch is merged into another)
1103
1122
if self.tt.final_kind(other_root) is not None:
1104
1123
other_root_is_present = True
1109
1128
# 'other_tree.inventory.root' is not present in this tree. We are
1110
1129
# calling adjust_path for children which *want* to be present with a
1111
1130
# correct place to go.
1112
for thing, child in self.other_tree.inventory.root.children.iteritems():
1131
for _, child in self.other_tree.inventory.root.children.iteritems():
1113
1132
trans_id = self.tt.trans_id_file_id(child.file_id)
1114
1133
if not other_root_is_present:
1115
1134
if self.tt.final_kind(trans_id) is not None:
1117
1136
# to go already.
1119
1138
# Move the item into the root
1120
self.tt.adjust_path(self.tt.final_name(trans_id),
1121
self.tt.root, trans_id)
1140
final_name = self.tt.final_name(trans_id)
1141
except errors.NoFinalPath:
1142
# This file is not present anymore, ignore it.
1144
self.tt.adjust_path(final_name, self.tt.root, trans_id)
1122
1145
if other_root_is_present:
1123
1146
self.tt.cancel_creation(other_root)
1124
1147
self.tt.cancel_versioning(other_root)
1303
1326
self._raw_conflicts.append(('path conflict', trans_id, file_id,
1304
1327
this_parent, this_name,
1305
1328
other_parent, other_name))
1306
if other_name is None:
1329
if not self.other_tree.has_id(file_id):
1307
1330
# it doesn't matter whether the result was 'other' or
1308
# 'conflict'-- if there's no 'other', we leave it alone.
1331
# 'conflict'-- if it has no file id, we leave it alone.
1310
1333
parent_id = parents[self.winner_idx[parent_id_winner]]
1311
if parent_id is not None:
1334
name = names[self.winner_idx[name_winner]]
1335
if parent_id is not None or name is not None:
1312
1336
# if we get here, name_winner and parent_winner are set to safe
1314
self.tt.adjust_path(names[self.winner_idx[name_winner]],
1315
self.tt.trans_id_file_id(parent_id),
1338
if parent_id is None and name is not None:
1339
# if parent_id is None and name is non-None, current file is
1341
if names[self.winner_idx[parent_id_winner]] != '':
1342
raise AssertionError(
1343
'File looks like a root, but named %s' %
1344
names[self.winner_idx[parent_id_winner]])
1345
parent_trans_id = transform.ROOT_PARENT
1347
parent_trans_id = self.tt.trans_id_file_id(parent_id)
1348
self.tt.adjust_path(name, parent_trans_id,
1316
1349
self.tt.trans_id_file_id(file_id))
1318
1351
def _do_merge_contents(self, file_id):
1319
1352
"""Performs a merge on file_id contents."""
1320
1353
def contents_pair(tree):
1321
if file_id not in tree:
1354
if not tree.has_id(file_id):
1322
1355
return (None, None)
1323
1356
kind = tree.kind(file_id)
1324
1357
if kind == "file":
1362
1395
if hook_status != 'not_applicable':
1363
1396
# Don't try any more hooks, this one applies.
1398
# If the merge ends up replacing the content of the file, we get rid of
1399
# it at the end of this method (this variable is used to track the
1400
# exceptions to this rule).
1365
1402
result = "modified"
1366
1403
if hook_status == 'not_applicable':
1367
# This is a contents conflict, because none of the available
1368
# functions could merge it.
1404
# No merge hook was able to resolve the situation. Two cases exist:
1405
# a content conflict or a duplicate one.
1370
1407
name = self.tt.final_name(trans_id)
1371
1408
parent_id = self.tt.final_parent(trans_id)
1372
if self.this_tree.has_id(file_id):
1373
self.tt.unversion_file(trans_id)
1374
file_group = self._dump_conflicts(name, parent_id, file_id,
1376
self._raw_conflicts.append(('contents conflict', file_group))
1410
inhibit_content_conflict = False
1411
if params.this_kind is None: # file_id is not in THIS
1412
# Is the name used for a different file_id ?
1413
dupe_path = self.other_tree.id2path(file_id)
1414
this_id = self.this_tree.path2id(dupe_path)
1415
if this_id is not None:
1416
# Two entries for the same path
1418
# versioning the merged file will trigger a duplicate
1420
self.tt.version_file(file_id, trans_id)
1421
transform.create_from_tree(
1422
self.tt, trans_id, self.other_tree, file_id,
1423
filter_tree_path=self._get_filter_tree_path(file_id))
1424
inhibit_content_conflict = True
1425
elif params.other_kind is None: # file_id is not in OTHER
1426
# Is the name used for a different file_id ?
1427
dupe_path = self.this_tree.id2path(file_id)
1428
other_id = self.other_tree.path2id(dupe_path)
1429
if other_id is not None:
1430
# Two entries for the same path again, but here, the other
1431
# entry will also be merged. We simply inhibit the
1432
# 'content' conflict creation because we know OTHER will
1433
# create (or has already created depending on ordering) an
1434
# entry at the same path. This will trigger a 'duplicate'
1437
inhibit_content_conflict = True
1438
if not inhibit_content_conflict:
1439
if params.this_kind is not None:
1440
self.tt.unversion_file(trans_id)
1441
# This is a contents conflict, because none of the available
1442
# functions could merge it.
1443
file_group = self._dump_conflicts(name, parent_id, file_id,
1445
self._raw_conflicts.append(('contents conflict', file_group))
1377
1446
elif hook_status == 'success':
1378
1447
self.tt.create_file(lines, trans_id)
1379
1448
elif hook_status == 'conflicted':
1395
1464
raise AssertionError('unknown hook_status: %r' % (hook_status,))
1396
1465
if not self.this_tree.has_id(file_id) and result == "modified":
1397
1466
self.tt.version_file(file_id, trans_id)
1398
# The merge has been performed, so the old contents should not be
1400
self.tt.delete_contents(trans_id)
1468
# The merge has been performed and produced a new content, so the
1469
# old contents should not be retained.
1470
self.tt.delete_contents(trans_id)
1403
1473
def _default_other_winner_merge(self, merge_hook_params):
1404
1474
"""Replace this contents with other."""
1405
1475
file_id = merge_hook_params.file_id
1406
1476
trans_id = merge_hook_params.trans_id
1407
file_in_this = self.this_tree.has_id(file_id)
1408
1477
if self.other_tree.has_id(file_id):
1409
1478
# OTHER changed the file
1411
if wt.supports_content_filtering():
1412
# We get the path from the working tree if it exists.
1413
# That fails though when OTHER is adding a file, so
1414
# we fall back to the other tree to find the path if
1415
# it doesn't exist locally.
1417
filter_tree_path = wt.id2path(file_id)
1418
except errors.NoSuchId:
1419
filter_tree_path = self.other_tree.id2path(file_id)
1421
# Skip the id2path lookup for older formats
1422
filter_tree_path = None
1423
transform.create_from_tree(self.tt, trans_id,
1424
self.other_tree, file_id,
1425
filter_tree_path=filter_tree_path)
1479
transform.create_from_tree(
1480
self.tt, trans_id, self.other_tree, file_id,
1481
filter_tree_path=self._get_filter_tree_path(file_id))
1426
1482
return 'done', None
1483
elif self.this_tree.has_id(file_id):
1428
1484
# OTHER deleted the file
1429
1485
return 'delete', None
1505
1561
file_group.append(trans_id)
1564
def _get_filter_tree_path(self, file_id):
1565
if self.this_tree.supports_content_filtering():
1566
# We get the path from the working tree if it exists.
1567
# That fails though when OTHER is adding a file, so
1568
# we fall back to the other tree to find the path if
1569
# it doesn't exist locally.
1571
return self.this_tree.id2path(file_id)
1572
except errors.NoSuchId:
1573
return self.other_tree.id2path(file_id)
1574
# Skip the id2path lookup for older formats
1507
1577
def _dump_conflicts(self, name, parent_id, file_id, this_lines=None,
1508
1578
base_lines=None, other_lines=None, set_version=False,
1509
1579
no_base=False):
1593
1663
def cook_conflicts(self, fs_conflicts):
1594
1664
"""Convert all conflicts into a form that doesn't depend on trans_id"""
1595
self.cooked_conflicts.extend(transform.cook_conflicts(
1596
fs_conflicts, self.tt))
1665
content_conflict_file_ids = set()
1666
cooked_conflicts = transform.cook_conflicts(fs_conflicts, self.tt)
1597
1667
fp = transform.FinalPaths(self.tt)
1598
1668
for conflict in self._raw_conflicts:
1599
1669
conflict_type = conflict[0]
1610
1680
if other_parent is None or other_name is None:
1611
1681
other_path = '<deleted>'
1613
parent_path = fp.get_path(
1614
self.tt.trans_id_file_id(other_parent))
1683
if other_parent == self.other_tree.get_root_id():
1684
# The tree transform doesn't know about the other root,
1685
# so we special case here to avoid a NoFinalPath
1689
parent_path = fp.get_path(
1690
self.tt.trans_id_file_id(other_parent))
1615
1691
other_path = osutils.pathjoin(parent_path, other_name)
1616
1692
c = _mod_conflicts.Conflict.factory(
1617
1693
'path conflict', path=this_path,
1621
1697
for trans_id in conflict[1]:
1622
1698
file_id = self.tt.final_file_id(trans_id)
1623
1699
if file_id is not None:
1700
# Ok we found the relevant file-id
1625
1702
path = fp.get_path(trans_id)
1626
1703
for suffix in ('.BASE', '.THIS', '.OTHER'):
1627
1704
if path.endswith(suffix):
1705
# Here is the raw path
1628
1706
path = path[:-len(suffix)]
1630
1708
c = _mod_conflicts.Conflict.factory(conflict_type,
1631
1709
path=path, file_id=file_id)
1710
content_conflict_file_ids.add(file_id)
1632
1711
elif conflict_type == 'text conflict':
1633
1712
trans_id = conflict[1]
1634
1713
path = fp.get_path(trans_id)
1637
1716
path=path, file_id=file_id)
1639
1718
raise AssertionError('bad conflict type: %r' % (conflict,))
1719
cooked_conflicts.append(c)
1721
self.cooked_conflicts = []
1722
# We want to get rid of path conflicts when a corresponding contents
1723
# conflict exists. This can occur when one branch deletes a file while
1724
# the other renames *and* modifies it. In this case, the content
1725
# conflict is enough.
1726
for c in cooked_conflicts:
1727
if (c.typestring == 'path conflict'
1728
and c.file_id in content_conflict_file_ids):
1640
1730
self.cooked_conflicts.append(c)
1641
1731
self.cooked_conflicts.sort(key=_mod_conflicts.Conflict.sort_key)
1863
1953
entries = self._entries_to_incorporate()
1864
1954
entries = list(entries)
1865
1955
for num, (entry, parent_id) in enumerate(entries):
1866
child_pb.update('Preparing file merge', num, len(entries))
1956
child_pb.update(gettext('Preparing file merge'), num, len(entries))
1867
1957
parent_trans_id = self.tt.trans_id_file_id(parent_id)
1868
1958
trans_id = transform.new_by_entry(self.tt, entry,
1869
1959
parent_trans_id, self.other_tree)
1887
1977
name_in_target = osutils.basename(self._target_subdir)
1888
1978
merge_into_root = subdir.copy()
1889
1979
merge_into_root.name = name_in_target
1890
if merge_into_root.file_id in self.this_tree.inventory:
1980
if self.this_tree.inventory.has_id(merge_into_root.file_id):
1891
1981
# Give the root a new file-id.
1892
1982
# This can happen fairly easily if the directory we are
1893
1983
# incorporating is the root, and both trees have 'TREE_ROOT' as
1953
2043
merger.set_base_revision(get_revision_id(), this_branch)
1954
2044
return merger.do_merge()
2047
merge_type_registry = registry.Registry()
2048
merge_type_registry.register('diff3', Diff3Merger,
2049
"Merge using external diff3.")
2050
merge_type_registry.register('lca', LCAMerger,
2051
"LCA-newness merge.")
2052
merge_type_registry.register('merge3', Merge3Merger,
2053
"Native diff3-style merge.")
2054
merge_type_registry.register('weave', WeaveMerger,
2055
"Weave-based merge.")
1956
2058
def get_merge_type_registry():
1957
"""Merge type registry is in bzrlib.option to avoid circular imports.
2059
"""Merge type registry was previously in bzrlib.option
1959
This method provides a sanctioned way to retrieve it.
2061
This method provides a backwards compatible way to retrieve it.
1961
from bzrlib import option
1962
return option._merge_type_registry
2063
return merge_type_registry
1965
2066
def _plan_annotate_merge(annotated_a, annotated_b, ancestors_a, ancestors_b):