1
# Copyright (C) 2005-2011 Canonical Ltd
1
# Copyright (C) 2005-2010 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
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)
702
703
def do_merge(self):
703
operation = cleanup.OperationWithCleanups(self._do_merge_to)
704
operation = OperationWithCleanups(self._do_merge_to)
704
705
self.this_tree.lock_tree_write()
705
706
operation.add_cleanup(self.this_tree.unlock)
706
707
if self.base_tree is not None:
712
713
merge = operation.run_simple()
713
714
if len(merge.cooked_conflicts) == 0:
714
715
if not self.ignore_zero and not trace.is_quiet():
715
trace.note(gettext("All changes applied successfully."))
716
trace.note("All changes applied successfully.")
717
trace.note(gettext("%d conflicts encountered.")
718
trace.note("%d conflicts encountered."
718
719
% len(merge.cooked_conflicts))
720
721
return len(merge.cooked_conflicts)
812
813
warnings.warn("pb argument to Merge3Merger is deprecated")
814
815
def do_merge(self):
815
operation = cleanup.OperationWithCleanups(self._do_merge)
816
operation = OperationWithCleanups(self._do_merge)
816
817
self.this_tree.lock_tree_write()
817
818
operation.add_cleanup(self.this_tree.unlock)
818
819
self.base_tree.lock_read()
835
836
def make_preview_transform(self):
836
operation = cleanup.OperationWithCleanups(self._make_preview_transform)
837
operation = OperationWithCleanups(self._make_preview_transform)
837
838
self.base_tree.lock_read()
838
839
operation.add_cleanup(self.base_tree.unlock)
839
840
self.other_tree.lock_read()
859
860
self.active_hooks = [hook for hook in hooks if hook is not None]
860
861
for num, (file_id, changed, parents3, names3,
861
862
executable3) in enumerate(entries):
862
child_pb.update(gettext('Preparing file merge'), num, len(entries))
863
child_pb.update('Preparing file merge', num, len(entries))
863
864
self._merge_names(file_id, parents3, names3, resolver=resolver)
865
866
file_status = self._do_merge_contents(file_id)
869
870
executable3, file_status, resolver=resolver)
871
872
child_pb.finished()
872
self.tt.fixup_new_roots()
873
874
self._finish_computing_transform()
875
876
def _finish_computing_transform(self):
889
890
self.tt.iter_changes(), self.change_reporter)
890
891
self.cook_conflicts(fs_conflicts)
891
892
for conflict in self.cooked_conflicts:
892
trace.warning(unicode(conflict))
893
trace.warning(conflict)
894
895
def _entries3(self):
895
896
"""Gather data about files modified between three trees.
904
905
iterator = self.other_tree.iter_changes(self.base_tree,
905
specific_files=self.interesting_files,
906
include_unchanged=True, specific_files=self.interesting_files,
906
907
extra_trees=[self.this_tree])
907
908
this_entries = dict((e.file_id, e) for p, e in
908
909
self.this_tree.iter_entries_by_dir(
934
935
it then compares with THIS and BASE.
936
937
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,
938
:return: [(file_id, changed, parents, names, executable)]
939
file_id Simple file_id of the entry
940
changed Boolean, True if the kind or contents changed
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, exec_in_this)
948
947
if self.interesting_files is not None:
949
948
lookup_trees = [self.this_tree, self.base_tree]
992
991
lca_entries.append(lca_ie)
994
if base_inventory.has_id(file_id):
993
if file_id in base_inventory:
995
994
base_ie = base_inventory[file_id]
997
996
base_ie = _none_entry
999
if this_inventory.has_id(file_id):
998
if file_id in this_inventory:
1000
999
this_ie = this_inventory[file_id]
1002
1001
this_ie = _none_entry
1107
1105
other_root = self.tt.trans_id_file_id(other_root_file_id)
1108
1106
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)
1108
if self.other_tree.inventory.root.file_id in self.this_tree.inventory:
1109
# the other tree's root is a non-root in the current tree (as when
1110
# a previously unrelated branch is merged into another)
1115
1112
if self.tt.final_kind(other_root) is not None:
1116
1113
other_root_is_present = True
1121
1118
# 'other_tree.inventory.root' is not present in this tree. We are
1122
1119
# calling adjust_path for children which *want* to be present with a
1123
1120
# correct place to go.
1124
for _, child in self.other_tree.inventory.root.children.iteritems():
1121
for thing, child in self.other_tree.inventory.root.children.iteritems():
1125
1122
trans_id = self.tt.trans_id_file_id(child.file_id)
1126
1123
if not other_root_is_present:
1127
1124
if self.tt.final_kind(trans_id) is not None:
1129
1126
# to go already.
1131
1128
# 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)
1129
self.tt.adjust_path(self.tt.final_name(trans_id),
1130
self.tt.root, trans_id)
1138
1131
if other_root_is_present:
1139
1132
self.tt.cancel_creation(other_root)
1140
1133
self.tt.cancel_versioning(other_root)
1169
1162
def contents_sha1(tree, file_id):
1170
1163
"""Determine the sha1 of the file contents (used as a key method)."""
1171
if not tree.has_id(file_id):
1164
if file_id not in tree:
1173
1166
return tree.get_file_sha1(file_id)
1319
1312
self._raw_conflicts.append(('path conflict', trans_id, file_id,
1320
1313
this_parent, this_name,
1321
1314
other_parent, other_name))
1322
if not self.other_tree.has_id(file_id):
1315
if other_name is None:
1323
1316
# it doesn't matter whether the result was 'other' or
1324
# 'conflict'-- if it has no file id, we leave it alone.
1317
# 'conflict'-- if there's no 'other', we leave it alone.
1326
1319
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:
1320
if parent_id is not None:
1329
1321
# 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,
1323
self.tt.adjust_path(names[self.winner_idx[name_winner]],
1324
self.tt.trans_id_file_id(parent_id),
1342
1325
self.tt.trans_id_file_id(file_id))
1344
1327
def _do_merge_contents(self, file_id):
1345
1328
"""Performs a merge on file_id contents."""
1346
1329
def contents_pair(tree):
1347
if not tree.has_id(file_id):
1330
if file_id not in tree:
1348
1331
return (None, None)
1349
1332
kind = tree.kind(file_id)
1350
1333
if kind == "file":
1619
1602
def cook_conflicts(self, fs_conflicts):
1620
1603
"""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)
1604
self.cooked_conflicts.extend(transform.cook_conflicts(
1605
fs_conflicts, self.tt))
1623
1606
fp = transform.FinalPaths(self.tt)
1624
1607
for conflict in self._raw_conflicts:
1625
1608
conflict_type = conflict[0]
1636
1619
if other_parent is None or other_name is None:
1637
1620
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))
1622
parent_path = fp.get_path(
1623
self.tt.trans_id_file_id(other_parent))
1647
1624
other_path = osutils.pathjoin(parent_path, other_name)
1648
1625
c = _mod_conflicts.Conflict.factory(
1649
1626
'path conflict', path=this_path,
1662
1639
c = _mod_conflicts.Conflict.factory(conflict_type,
1663
1640
path=path, file_id=file_id)
1664
content_conflict_file_ids.add(file_id)
1665
1641
elif conflict_type == 'text conflict':
1666
1642
trans_id = conflict[1]
1667
1643
path = fp.get_path(trans_id)
1670
1646
path=path, file_id=file_id)
1672
1648
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
1649
self.cooked_conflicts.append(c)
1685
1650
self.cooked_conflicts.sort(key=_mod_conflicts.Conflict.sort_key)
1907
1872
entries = self._entries_to_incorporate()
1908
1873
entries = list(entries)
1909
1874
for num, (entry, parent_id) in enumerate(entries):
1910
child_pb.update(gettext('Preparing file merge'), num, len(entries))
1875
child_pb.update('Preparing file merge', num, len(entries))
1911
1876
parent_trans_id = self.tt.trans_id_file_id(parent_id)
1912
1877
trans_id = transform.new_by_entry(self.tt, entry,
1913
1878
parent_trans_id, self.other_tree)
1931
1896
name_in_target = osutils.basename(self._target_subdir)
1932
1897
merge_into_root = subdir.copy()
1933
1898
merge_into_root.name = name_in_target
1934
if self.this_tree.inventory.has_id(merge_into_root.file_id):
1899
if merge_into_root.file_id in self.this_tree.inventory:
1935
1900
# Give the root a new file-id.
1936
1901
# This can happen fairly easily if the directory we are
1937
1902
# incorporating is the root, and both trees have 'TREE_ROOT' as
1975
1940
if this_tree is None:
1976
1941
raise errors.BzrError("bzrlib.merge.merge_inner requires a this_tree "
1942
"parameter as of bzrlib version 0.8.")
1978
1943
merger = Merger(this_branch, other_tree, base_tree, this_tree=this_tree,
1979
1944
pb=pb, change_reporter=change_reporter)
1980
1945
merger.backup_files = backup_files
2434
2399
class _PlanLCAMerge(_PlanMergeBase):
2436
2401
This merge algorithm differs from _PlanMerge in that:
2438
2402
1. comparisons are done against LCAs only
2439
2403
2. cases where a contested line is new versus one LCA but old versus
2440
2404
another are marked as conflicts, by emitting the line as conflicted-a
2482
2446
If a line is killed and new, this indicates that the two merge
2483
2447
revisions contain differing conflict resolutions.
2485
2448
:param revision_id: The id of the revision in which the lines are
2487
2450
:param unique_line_numbers: The line numbers of unique lines.
2488
:return: a tuple of (new_this, killed_other)
2451
:return a tuple of (new_this, killed_other):