14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
from __future__ import absolute_import
21
from bzrlib.lazy_import import lazy_import
19
from .lazy_import import lazy_import
22
20
lazy_import(globals(), """
24
24
branch as _mod_branch,
26
25
conflicts as _mod_conflicts,
29
27
graph as _mod_graph,
33
30
revision as _mod_revision,
38
from breezy.bzr import (
39
conflicts as _mod_bzr_conflicts,
43
from bzrlib.i18n import gettext
43
from breezy.i18n import gettext
45
from breezy.bzr.conflicts import Conflict as BzrConflict
51
from bzrlib.symbol_versioning import (
55
53
# TODO: Report back as changes are merged in
58
def transform_tree(from_tree, to_tree, interesting_ids=None):
59
from_tree.lock_tree_write()
60
operation = cleanup.OperationWithCleanups(merge_inner)
61
operation.add_cleanup(from_tree.unlock)
62
operation.run_simple(from_tree.branch, to_tree, from_tree,
63
ignore_zero=True, interesting_ids=interesting_ids, this_tree=from_tree)
56
def transform_tree(from_tree, to_tree, interesting_files=None):
57
with from_tree.lock_tree_write():
58
merge_inner(from_tree.branch, to_tree, from_tree,
59
ignore_zero=True, this_tree=from_tree,
60
interesting_files=interesting_files)
66
63
class MergeHooks(hooks.Hooks):
68
65
def __init__(self):
69
hooks.Hooks.__init__(self, "bzrlib.merge", "Merger.hooks")
66
hooks.Hooks.__init__(self, "breezy.merge", "Merger.hooks")
70
67
self.add_hook('merge_file_content',
71
"Called with a bzrlib.merge.Merger object to create a per file "
72
"merge object when starting a merge. "
73
"Should return either None or a subclass of "
74
"``bzrlib.merge.AbstractPerFileMerger``. "
75
"Such objects will then be called per file "
76
"that needs to be merged (including when one "
77
"side has deleted the file and the other has changed it). "
78
"See the AbstractPerFileMerger API docs for details on how it is "
68
"Called with a breezy.merge.Merger object to create a per file "
69
"merge object when starting a merge. "
70
"Should return either None or a subclass of "
71
"``breezy.merge.AbstractPerFileMerger``. "
72
"Such objects will then be called per file "
73
"that needs to be merged (including when one "
74
"side has deleted the file and the other has changed it). "
75
"See the AbstractPerFileMerger API docs for details on how it is "
81
78
self.add_hook('pre_merge',
82
'Called before a merge. '
83
'Receives a Merger object as the single argument.',
79
'Called before a merge. '
80
'Receives a Merger object as the single argument.',
85
82
self.add_hook('post_merge',
86
'Called after a merge. '
87
'Receives a Merger object as the single argument. '
88
'The return value is ignored.',
83
'Called after a merge. '
84
'Receives a Merger object as the single argument. '
85
'The return value is ignored.',
92
89
class AbstractPerFileMerger(object):
93
"""PerFileMerger objects are used by plugins extending merge for bzrlib.
95
See ``bzrlib.plugins.news_merge.news_merge`` for an example concrete class.
90
"""PerFileMerger objects are used by plugins extending merge for breezy.
92
See ``breezy.plugins.news_merge.news_merge`` for an example concrete class.
97
94
:ivar merger: The Merge3Merger performing the merge.
784
753
self.cherrypick = cherrypick
788
warnings.warn("pp argument to Merge3Merger is deprecated")
790
warnings.warn("pb argument to Merge3Merger is deprecated")
792
757
def do_merge(self):
793
operation = cleanup.OperationWithCleanups(self._do_merge)
794
self.working_tree.lock_tree_write()
795
operation.add_cleanup(self.working_tree.unlock)
796
self.this_tree.lock_read()
797
operation.add_cleanup(self.this_tree.unlock)
798
self.base_tree.lock_read()
799
operation.add_cleanup(self.base_tree.unlock)
800
self.other_tree.lock_read()
801
operation.add_cleanup(self.other_tree.unlock)
804
def _do_merge(self, operation):
805
self.tt = transform.TreeTransform(self.working_tree, None)
806
operation.add_cleanup(self.tt.finalize)
807
self._compute_transform()
808
results = self.tt.apply(no_conflicts=True)
809
self.write_modified(results)
811
self.working_tree.add_conflicts(self.cooked_conflicts)
812
except errors.UnsupportedOperation:
758
with contextlib.ExitStack() as stack:
759
stack.enter_context(self.working_tree.lock_tree_write())
760
stack.enter_context(self.this_tree.lock_read())
761
stack.enter_context(self.base_tree.lock_read())
762
stack.enter_context(self.other_tree.lock_read())
763
self.tt = self.working_tree.transform()
764
stack.enter_context(self.tt)
765
self._compute_transform()
766
results = self.tt.apply(no_conflicts=True)
767
self.write_modified(results)
769
self.working_tree.add_conflicts(self.cooked_conflicts)
770
except errors.UnsupportedOperation:
815
773
def make_preview_transform(self):
816
operation = cleanup.OperationWithCleanups(self._make_preview_transform)
817
self.base_tree.lock_read()
818
operation.add_cleanup(self.base_tree.unlock)
819
self.other_tree.lock_read()
820
operation.add_cleanup(self.other_tree.unlock)
821
return operation.run_simple()
823
def _make_preview_transform(self):
824
self.tt = transform.TransformPreview(self.working_tree)
825
self._compute_transform()
774
with self.base_tree.lock_read(), self.other_tree.lock_read():
775
self.tt = self.working_tree.preview_transform()
776
self._compute_transform()
828
779
def _compute_transform(self):
829
780
if self._lca_trees is None:
830
entries = self._entries3()
781
entries = list(self._entries3())
831
782
resolver = self._three_way
833
entries = self._entries_lca()
784
entries = list(self._entries_lca())
834
785
resolver = self._lca_multi_way
835
786
# Prepare merge hooks
836
787
factories = Merger.hooks['merge_file_content']
837
788
# One hook for each registered one plus our default merger
838
789
hooks = [factory(self) for factory in factories] + [self]
839
790
self.active_hooks = [hook for hook in hooks if hook is not None]
840
child_pb = ui.ui_factory.nested_progress_bar()
842
for num, (file_id, changed, parents3, names3,
843
executable3) in enumerate(entries):
791
with ui.ui_factory.nested_progress_bar() as child_pb:
792
for num, (file_id, changed, paths3, parents3, names3,
793
executable3, copied) in enumerate(entries):
795
# Treat copies as simple adds for now
796
paths3 = (None, paths3[1], None)
797
parents3 = (None, parents3[1], None)
798
names3 = (None, names3[1], None)
799
executable3 = (None, executable3[1], None)
802
trans_id = self.tt.trans_id_file_id(file_id)
844
803
# Try merging each entry
845
804
child_pb.update(gettext('Preparing file merge'),
846
805
num, len(entries))
847
self._merge_names(file_id, parents3, names3, resolver=resolver)
806
self._merge_names(trans_id, file_id, paths3, parents3,
807
names3, resolver=resolver)
849
file_status = self._do_merge_contents(file_id)
809
file_status = self._do_merge_contents(paths3, trans_id, file_id)
851
811
file_status = 'unmodified'
852
self._merge_executable(file_id,
853
executable3, file_status, resolver=resolver)
812
self._merge_executable(paths3, trans_id, executable3,
813
file_status, resolver=resolver)
856
814
self.tt.fixup_new_roots()
857
815
self._finish_computing_transform()
884
840
other and this. names3 is a tuple of names for base, other and this.
885
841
executable3 is a tuple of execute-bit values for base, other and this.
888
843
iterator = self.other_tree.iter_changes(self.base_tree,
889
specific_files=self.interesting_files,
890
extra_trees=[self.this_tree])
891
this_entries = dict((e.file_id, e) for p, e in
892
self.this_tree.iter_entries_by_dir(
893
self.interesting_ids))
894
for (file_id, paths, changed, versioned, parents, names, kind,
895
executable) in iterator:
896
if (self.interesting_ids is not None and
897
file_id not in self.interesting_ids):
899
entry = this_entries.get(file_id)
900
if entry is not None:
901
this_name = entry.name
902
this_parent = entry.parent_id
903
this_executable = entry.executable
844
specific_files=self.interesting_files,
845
extra_trees=[self.this_tree])
846
this_interesting_files = self.this_tree.find_related_paths_across_trees(
847
self.interesting_files, trees=[self.other_tree])
848
this_entries = dict(self.this_tree.iter_entries_by_dir(
849
specific_files=this_interesting_files))
850
for change in iterator:
851
if change.path[0] is not None:
852
this_path = _mod_tree.find_previous_path(
853
self.base_tree, self.this_tree, change.path[0])
855
this_path = _mod_tree.find_previous_path(
856
self.other_tree, self.this_tree, change.path[1])
857
this_entry = this_entries.get(this_path)
858
if this_entry is not None:
859
this_name = this_entry.name
860
this_parent = this_entry.parent_id
861
this_executable = this_entry.executable
906
864
this_parent = None
907
865
this_executable = None
908
parents3 = parents + (this_parent,)
909
names3 = names + (this_name,)
910
executable3 = executable + (this_executable,)
911
result.append((file_id, changed, parents3, names3, executable3))
866
parents3 = change.parent_id + (this_parent,)
867
names3 = change.name + (this_name,)
868
paths3 = change.path + (this_path, )
869
executable3 = change.executable + (this_executable,)
871
(change.file_id, change.changed_content, paths3,
872
parents3, names3, executable3, change.copied))
914
874
def _entries_lca(self):
915
875
"""Gather data about files modified between multiple trees.
1030
1001
(base_ie.executable, lca_executable),
1031
1002
other_ie.executable, this_ie.executable)
1032
1003
if (parent_id_winner == 'this' and name_winner == 'this'
1033
and sha1_winner == 'this' and exec_winner == 'this'):
1004
and sha1_winner == 'this' and exec_winner == 'this'):
1034
1005
# No kind, parent, name, exec, or content change for
1035
1006
# OTHER, so this node is not considered interesting
1037
1008
if sha1_winner == 'this':
1038
1009
content_changed = False
1039
1010
elif other_ie.kind == 'symlink':
1040
def get_target(ie, tree):
1011
def get_target(ie, tree, path):
1041
1012
if ie.kind != 'symlink':
1043
return tree.get_symlink_target(file_id)
1044
base_target = get_target(base_ie, self.base_tree)
1045
lca_targets = [get_target(ie, tree) for ie, tree
1046
in zip(lca_entries, self._lca_trees)]
1047
this_target = get_target(this_ie, self.this_tree)
1048
other_target = get_target(other_ie, self.other_tree)
1014
return tree.get_symlink_target(path)
1015
base_target = get_target(base_ie, self.base_tree, base_path)
1016
lca_targets = [get_target(ie, tree, lca_path) for ie, tree, lca_path
1017
in zip(lca_entries, self._lca_trees, lca_paths)]
1018
this_target = get_target(
1019
this_ie, self.this_tree, this_path)
1020
other_target = get_target(
1021
other_ie, self.other_tree, other_path)
1049
1022
target_winner = self._lca_multi_way(
1050
1023
(base_target, lca_targets),
1051
1024
other_target, this_target)
1052
1025
if (parent_id_winner == 'this' and name_winner == 'this'
1053
and target_winner == 'this'):
1026
and target_winner == 'this'):
1054
1027
# No kind, parent, name, or symlink target change
1055
1028
# not interesting
1068
1041
raise AssertionError('unhandled kind: %s' % other_ie.kind)
1070
1043
# If we have gotten this far, that means something has changed
1071
result.append((file_id, content_changed,
1044
yield (file_id, content_changed,
1045
((base_path, lca_paths),
1046
other_path, this_path),
1072
1047
((base_ie.parent_id, lca_parent_ids),
1073
1048
other_ie.parent_id, this_ie.parent_id),
1074
1049
((base_ie.name, lca_names),
1075
1050
other_ie.name, this_ie.name),
1076
1051
((base_ie.executable, lca_executable),
1077
other_ie.executable, this_ie.executable)
1052
other_ie.executable, this_ie.executable),
1053
# Copy detection is not yet supported, so nothing is
1081
1058
def write_modified(self, results):
1059
if not self.working_tree.supports_merge_modified():
1082
1061
modified_hashes = {}
1083
1062
for path in results.modified_paths:
1084
file_id = self.working_tree.path2id(self.working_tree.relpath(path))
1063
wt_relpath = self.working_tree.relpath(path)
1064
if not self.working_tree.is_versioned(wt_relpath):
1087
hash = self.working_tree.get_file_sha1(file_id)
1066
hash = self.working_tree.get_file_sha1(wt_relpath)
1088
1067
if hash is None:
1090
modified_hashes[file_id] = hash
1069
modified_hashes[wt_relpath] = hash
1091
1070
self.working_tree.set_merge_modified(modified_hashes)
1094
def parent(entry, file_id):
1095
1074
"""Determine the parent for a file_id (used as a key method)"""
1096
1075
if entry is None:
1098
1077
return entry.parent_id
1101
def name(entry, file_id):
1102
1081
"""Determine the name for a file_id (used as a key method)"""
1103
1082
if entry is None:
1105
1084
return entry.name
1108
def contents_sha1(tree, file_id):
1087
def contents_sha1(tree, path):
1109
1088
"""Determine the sha1 of the file contents (used as a key method)."""
1110
if not tree.has_id(file_id):
1090
return tree.get_file_sha1(path)
1091
except errors.NoSuchFile:
1112
return tree.get_file_sha1(file_id)
1115
def executable(tree, file_id):
1095
def executable(tree, path):
1116
1096
"""Determine the executability of a file-id (used as a key method)."""
1117
if not tree.has_id(file_id):
1098
if tree.kind(path) != "file":
1100
except errors.NoSuchFile:
1119
if tree.kind(file_id) != "file":
1121
return tree.is_executable(file_id)
1102
return tree.is_executable(path)
1124
def kind(tree, file_id):
1105
def kind(tree, path):
1125
1106
"""Determine the kind of a file-id (used as a key method)."""
1126
if not tree.has_id(file_id):
1108
return tree.kind(path)
1109
except errors.NoSuchFile:
1128
return tree.kind(file_id)
1131
1113
def _three_way(base, other, this):
1257
1217
parent_trans_id = transform.ROOT_PARENT
1259
1219
parent_trans_id = self.tt.trans_id_file_id(parent_id)
1260
self.tt.adjust_path(name, parent_trans_id,
1261
self.tt.trans_id_file_id(file_id))
1220
self.tt.adjust_path(name, parent_trans_id, trans_id)
1263
def _do_merge_contents(self, file_id):
1222
def _do_merge_contents(self, paths, trans_id, file_id):
1264
1223
"""Performs a merge on file_id contents."""
1265
def contents_pair(tree):
1266
if not tree.has_id(file_id):
1268
kind = tree.kind(file_id)
1224
def contents_pair(tree, path):
1228
kind = tree.kind(path)
1229
except errors.NoSuchFile:
1269
1231
if kind == "file":
1270
contents = tree.get_file_sha1(file_id)
1232
contents = tree.get_file_sha1(path)
1271
1233
elif kind == "symlink":
1272
contents = tree.get_symlink_target(file_id)
1234
contents = tree.get_symlink_target(path)
1274
1236
contents = None
1275
1237
return kind, contents
1239
base_path, other_path, this_path = paths
1277
1240
# See SPOT run. run, SPOT, run.
1278
1241
# So we're not QUITE repeating ourselves; we do tricky things with
1280
base_pair = contents_pair(self.base_tree)
1281
other_pair = contents_pair(self.other_tree)
1243
other_pair = contents_pair(self.other_tree, other_path)
1244
this_pair = contents_pair(self.this_tree, this_path)
1282
1245
if self._lca_trees:
1283
this_pair = contents_pair(self.this_tree)
1284
lca_pairs = [contents_pair(tree) for tree in self._lca_trees]
1246
(base_path, lca_paths) = base_path
1247
base_pair = contents_pair(self.base_tree, base_path)
1248
lca_pairs = [contents_pair(tree, path)
1249
for tree, path in zip(self._lca_trees, lca_paths)]
1285
1250
winner = self._lca_multi_way((base_pair, lca_pairs), other_pair,
1286
1251
this_pair, allow_overriding_lca=False)
1253
base_pair = contents_pair(self.base_tree, base_path)
1288
1254
if base_pair == other_pair:
1289
1255
winner = 'this'
1291
1257
# We delayed evaluating this_pair as long as we can to avoid
1292
1258
# unnecessary sha1 calculation
1293
this_pair = contents_pair(self.this_tree)
1259
this_pair = contents_pair(self.this_tree, this_path)
1294
1260
winner = self._three_way(base_pair, other_pair, this_pair)
1295
1261
if winner == 'this':
1296
1262
# No interesting changes introduced by OTHER
1297
1263
return "unmodified"
1298
1264
# We have a hypothetical conflict, but if we have files, then we
1299
1265
# can try to merge the content
1300
trans_id = self.tt.trans_id_file_id(file_id)
1301
params = MergeFileHookParams(self, file_id, trans_id, this_pair[0],
1266
params = MergeFileHookParams(
1267
self, (base_path, other_path, this_path), trans_id, this_pair[0],
1302
1268
other_pair[0], winner)
1303
1269
hooks = self.active_hooks
1304
1270
hook_status = 'not_applicable'
1413
1379
# BASE is a file, or both converted to files, so at least we
1414
1380
# have agreement that output should be a file.
1416
self.text_merge(merge_hook_params.file_id,
1417
merge_hook_params.trans_id)
1382
self.text_merge(merge_hook_params.trans_id,
1383
merge_hook_params.paths)
1418
1384
except errors.BinaryFile:
1419
1385
return 'not_applicable', None
1420
1386
return 'done', None
1422
1388
return 'not_applicable', None
1424
def get_lines(self, tree, file_id):
1390
def get_lines(self, tree, path):
1425
1391
"""Return the lines in a file, or an empty list."""
1426
if tree.has_id(file_id):
1427
return tree.get_file_lines(file_id)
1395
kind = tree.kind(path)
1396
except errors.NoSuchFile:
1401
return tree.get_file_lines(path)
1431
def text_merge(self, file_id, trans_id):
1432
"""Perform a three-way text merge on a file_id"""
1403
def text_merge(self, trans_id, paths):
1404
"""Perform a three-way text merge on a file"""
1433
1405
# it's possible that we got here with base as a different type.
1434
1406
# if so, we just want two-way text conflicts.
1435
if self.base_tree.has_id(file_id) and \
1436
self.base_tree.kind(file_id) == "file":
1437
base_lines = self.get_lines(self.base_tree, file_id)
1440
other_lines = self.get_lines(self.other_tree, file_id)
1441
this_lines = self.get_lines(self.this_tree, file_id)
1407
base_path, other_path, this_path = paths
1408
base_lines = self.get_lines(self.base_tree, base_path)
1409
other_lines = self.get_lines(self.other_tree, other_path)
1410
this_lines = self.get_lines(self.this_tree, this_path)
1442
1411
m3 = merge3.Merge3(base_lines, this_lines, other_lines,
1443
1412
is_cherrypick=self.cherrypick)
1444
start_marker = "!START OF MERGE CONFLICT!" + "I HOPE THIS IS UNIQUE"
1413
start_marker = b"!START OF MERGE CONFLICT!" + b"I HOPE THIS IS UNIQUE"
1445
1414
if self.show_base is True:
1446
base_marker = '|' * 7
1415
base_marker = b'|' * 7
1448
1417
base_marker = None
1450
1419
def iter_merge3(retval):
1451
1420
retval["text_conflicts"] = False
1452
for line in m3.merge_lines(name_a = "TREE",
1453
name_b = "MERGE-SOURCE",
1454
name_base = "BASE-REVISION",
1421
for line in m3.merge_lines(name_a=b"TREE",
1422
name_b=b"MERGE-SOURCE",
1423
name_base=b"BASE-REVISION",
1455
1424
start_marker=start_marker,
1456
1425
base_marker=base_marker,
1457
1426
reprocess=self.reprocess):
1458
1427
if line.startswith(start_marker):
1459
1428
retval["text_conflicts"] = True
1460
yield line.replace(start_marker, '<' * 7)
1429
yield line.replace(start_marker, b'<' * 7)
1467
1436
self._raw_conflicts.append(('text conflict', trans_id))
1468
1437
name = self.tt.final_name(trans_id)
1469
1438
parent_id = self.tt.final_parent(trans_id)
1470
file_group = self._dump_conflicts(name, parent_id, file_id,
1471
this_lines, base_lines,
1439
file_group = self._dump_conflicts(
1440
name, paths, parent_id,
1441
lines=(base_lines, other_lines, this_lines))
1473
1442
file_group.append(trans_id)
1476
def _get_filter_tree_path(self, file_id):
1444
def _get_filter_tree_path(self, path):
1477
1445
if self.this_tree.supports_content_filtering():
1478
1446
# We get the path from the working tree if it exists.
1479
1447
# That fails though when OTHER is adding a file, so
1480
1448
# we fall back to the other tree to find the path if
1481
1449
# it doesn't exist locally.
1483
return self.this_tree.id2path(file_id)
1484
except errors.NoSuchId:
1485
return self.other_tree.id2path(file_id)
1486
# Skip the id2path lookup for older formats
1450
filter_path = _mod_tree.find_previous_path(
1451
self.other_tree, self.working_tree, path)
1452
if filter_path is None:
1455
# Skip the lookup for older formats
1489
def _dump_conflicts(self, name, parent_id, file_id, this_lines=None,
1490
base_lines=None, other_lines=None, set_version=False,
1458
def _dump_conflicts(self, name, paths, parent_id, lines=None,
1491
1459
no_base=False):
1492
1460
"""Emit conflict files.
1493
1461
If this_lines, base_lines, or other_lines are omitted, they will be
1494
1462
determined automatically. If set_version is true, the .OTHER, .THIS
1495
1463
or .BASE (in that order) will be created as versioned files.
1497
data = [('OTHER', self.other_tree, other_lines),
1498
('THIS', self.this_tree, this_lines)]
1465
base_path, other_path, this_path = paths
1467
base_lines, other_lines, this_lines = lines
1469
base_lines = other_lines = this_lines = None
1470
data = [('OTHER', self.other_tree, other_path, other_lines),
1471
('THIS', self.this_tree, this_path, this_lines)]
1499
1472
if not no_base:
1500
data.append(('BASE', self.base_tree, base_lines))
1473
data.append(('BASE', self.base_tree, base_path, base_lines))
1502
1475
# We need to use the actual path in the working tree of the file here,
1503
# ignoring the conflict suffixes
1505
if wt.supports_content_filtering():
1507
filter_tree_path = wt.id2path(file_id)
1508
except errors.NoSuchId:
1509
# file has been deleted
1510
filter_tree_path = None
1476
if self.this_tree.supports_content_filtering():
1477
filter_tree_path = this_path
1512
# Skip the id2path lookup for older formats
1513
1479
filter_tree_path = None
1516
1481
file_group = []
1517
for suffix, tree, lines in data:
1518
if tree.has_id(file_id):
1519
trans_id = self._conflict_file(name, parent_id, tree, file_id,
1520
suffix, lines, filter_tree_path)
1482
for suffix, tree, path, lines in data:
1483
if path is not None:
1484
trans_id = self._conflict_file(
1485
name, parent_id, path, tree, suffix, lines,
1521
1487
file_group.append(trans_id)
1522
if set_version and not versioned:
1523
self.tt.version_file(file_id, trans_id)
1525
1488
return file_group
1527
def _conflict_file(self, name, parent_id, tree, file_id, suffix,
1490
def _conflict_file(self, name, parent_id, path, tree, suffix,
1528
1491
lines=None, filter_tree_path=None):
1529
1492
"""Emit a single conflict file."""
1530
1493
name = name + '.' + suffix
1531
1494
trans_id = self.tt.create_path(name, parent_id)
1532
transform.create_from_tree(self.tt, trans_id, tree, file_id, lines,
1495
transform.create_from_tree(
1496
self.tt, trans_id, tree, path,
1498
filter_tree_path=filter_tree_path)
1534
1499
return trans_id
1536
def merge_executable(self, file_id, file_status):
1537
"""Perform a merge on the execute bit."""
1538
executable = [self.executable(t, file_id) for t in (self.base_tree,
1539
self.other_tree, self.this_tree)]
1540
self._merge_executable(file_id, executable, file_status,
1541
resolver=self._three_way)
1543
def _merge_executable(self, file_id, executable, file_status,
1501
def _merge_executable(self, paths, trans_id, executable, file_status,
1545
1503
"""Perform a merge on the execute bit."""
1546
1504
base_executable, other_executable, this_executable = executable
1505
base_path, other_path, this_path = paths
1547
1506
if file_status == "deleted":
1549
1508
winner = resolver(*executable)
1550
1509
if winner == "conflict":
1551
# There must be a None in here, if we have a conflict, but we
1552
# need executability since file status was not deleted.
1553
if self.executable(self.other_tree, file_id) is None:
1510
# There must be a None in here, if we have a conflict, but we
1511
# need executability since file status was not deleted.
1512
if other_path is None:
1554
1513
winner = "this"
1556
1515
winner = "other"
1557
1516
if winner == 'this' and file_status != "modified":
1559
trans_id = self.tt.trans_id_file_id(file_id)
1560
1518
if self.tt.final_kind(trans_id) != "file":
1562
1520
if winner == "this":
1563
1521
executability = this_executable
1565
if self.other_tree.has_id(file_id):
1523
if other_path is not None:
1566
1524
executability = other_executable
1567
elif self.this_tree.has_id(file_id):
1525
elif this_path is not None:
1568
1526
executability = this_executable
1569
elif self.base_tree_has_id(file_id):
1527
elif base_path is not None:
1570
1528
executability = base_executable
1571
1529
if executability is not None:
1572
trans_id = self.tt.trans_id_file_id(file_id)
1573
1530
self.tt.set_executability(executability, trans_id)
1575
1532
def cook_conflicts(self, fs_conflicts):
1576
1533
"""Convert all conflicts into a form that doesn't depend on trans_id"""
1577
content_conflict_file_ids = set()
1578
cooked_conflicts = transform.cook_conflicts(fs_conflicts, self.tt)
1579
fp = transform.FinalPaths(self.tt)
1580
for conflict in self._raw_conflicts:
1581
conflict_type = conflict[0]
1582
if conflict_type == 'path conflict':
1584
this_parent, this_name,
1585
other_parent, other_name) = conflict[1:]
1586
if this_parent is None or this_name is None:
1587
this_path = '<deleted>'
1589
parent_path = fp.get_path(
1590
self.tt.trans_id_file_id(this_parent))
1591
this_path = osutils.pathjoin(parent_path, this_name)
1592
if other_parent is None or other_name is None:
1593
other_path = '<deleted>'
1595
if other_parent == self.other_tree.get_root_id():
1596
# The tree transform doesn't know about the other root,
1597
# so we special case here to avoid a NoFinalPath
1601
parent_path = fp.get_path(
1602
self.tt.trans_id_file_id(other_parent))
1603
other_path = osutils.pathjoin(parent_path, other_name)
1604
c = _mod_conflicts.Conflict.factory(
1605
'path conflict', path=this_path,
1606
conflict_path=other_path,
1608
elif conflict_type == 'contents conflict':
1609
for trans_id in conflict[1]:
1610
file_id = self.tt.final_file_id(trans_id)
1611
if file_id is not None:
1612
# Ok we found the relevant file-id
1614
path = fp.get_path(trans_id)
1615
for suffix in ('.BASE', '.THIS', '.OTHER'):
1616
if path.endswith(suffix):
1617
# Here is the raw path
1618
path = path[:-len(suffix)]
1620
c = _mod_conflicts.Conflict.factory(conflict_type,
1621
path=path, file_id=file_id)
1622
content_conflict_file_ids.add(file_id)
1623
elif conflict_type == 'text conflict':
1624
trans_id = conflict[1]
1625
path = fp.get_path(trans_id)
1626
file_id = self.tt.final_file_id(trans_id)
1627
c = _mod_conflicts.Conflict.factory(conflict_type,
1628
path=path, file_id=file_id)
1630
raise AssertionError('bad conflict type: %r' % (conflict,))
1631
cooked_conflicts.append(c)
1633
self.cooked_conflicts = []
1634
# We want to get rid of path conflicts when a corresponding contents
1635
# conflict exists. This can occur when one branch deletes a file while
1636
# the other renames *and* modifies it. In this case, the content
1637
# conflict is enough.
1638
for c in cooked_conflicts:
1639
if (c.typestring == 'path conflict'
1640
and c.file_id in content_conflict_file_ids):
1642
self.cooked_conflicts.append(c)
1643
self.cooked_conflicts.sort(key=_mod_conflicts.Conflict.sort_key)
1534
self.cooked_conflicts = list(self.tt.cook_conflicts(
1535
list(fs_conflicts) + self._raw_conflicts))
1646
1538
class WeaveMerger(Merge3Merger):
1695
1589
self._raw_conflicts.append(('text conflict', trans_id))
1696
1590
name = self.tt.final_name(trans_id)
1697
1591
parent_id = self.tt.final_parent(trans_id)
1698
file_group = self._dump_conflicts(name, parent_id, file_id,
1700
base_lines=base_lines)
1592
file_group = self._dump_conflicts(name, paths, parent_id,
1593
(base_lines, None, None),
1701
1595
file_group.append(trans_id)
1704
1598
class LCAMerger(WeaveMerger):
1706
def _generate_merge_plan(self, file_id, base):
1707
return self.this_tree.plan_file_lca_merge(file_id, self.other_tree,
1600
requires_file_merge_plan = True
1602
def _generate_merge_plan(self, this_path, base):
1603
return self.this_tree.plan_file_lca_merge(this_path, self.other_tree,
1710
1607
class Diff3Merger(Merge3Merger):
1711
1608
"""Three-way merger using external diff3 for text merging"""
1713
def dump_file(self, temp_dir, name, tree, file_id):
1610
requires_file_merge_plan = False
1612
def dump_file(self, temp_dir, name, tree, path):
1714
1613
out_path = osutils.pathjoin(temp_dir, name)
1715
out_file = open(out_path, "wb")
1717
in_file = tree.get_file(file_id)
1614
with open(out_path, "wb") as out_file:
1615
in_file = tree.get_file(path)
1718
1616
for line in in_file:
1719
1617
out_file.write(line)
1722
1618
return out_path
1724
def text_merge(self, file_id, trans_id):
1620
def text_merge(self, trans_id, paths):
1725
1621
"""Perform a diff3 merge using a specified file-id and trans-id.
1726
1622
If conflicts are encountered, .BASE, .THIS. and .OTHER conflict files
1727
1623
will be dumped, and a will be conflict noted.
1626
base_path, other_path, this_path = paths
1730
1627
temp_dir = osutils.mkdtemp(prefix="bzr-")
1732
1629
new_file = osutils.pathjoin(temp_dir, "new")
1733
this = self.dump_file(temp_dir, "this", self.this_tree, file_id)
1734
base = self.dump_file(temp_dir, "base", self.base_tree, file_id)
1735
other = self.dump_file(temp_dir, "other", self.other_tree, file_id)
1736
status = bzrlib.patch.diff3(new_file, this, base, other)
1630
this = self.dump_file(
1631
temp_dir, "this", self.this_tree, this_path)
1632
base = self.dump_file(
1633
temp_dir, "base", self.base_tree, base_path)
1634
other = self.dump_file(
1635
temp_dir, "other", self.other_tree, other_path)
1636
status = breezy.patch.diff3(new_file, this, base, other)
1737
1637
if status not in (0, 1):
1738
1638
raise errors.BzrError("Unhandled diff3 exit code")
1739
f = open(new_file, 'rb')
1639
with open(new_file, 'rb') as f:
1741
1640
self.tt.create_file(f, trans_id)
1744
1641
if status == 1:
1745
1642
name = self.tt.final_name(trans_id)
1746
1643
parent_id = self.tt.final_parent(trans_id)
1747
self._dump_conflicts(name, parent_id, file_id)
1644
self._dump_conflicts(name, paths, parent_id)
1748
1645
self._raw_conflicts.append(('text conflict', trans_id))
1750
1647
osutils.rmtree(temp_dir)
1860
1757
super(MergeIntoMergeType, self).__init__(*args, **kwargs)
1862
1759
def _compute_transform(self):
1863
child_pb = ui.ui_factory.nested_progress_bar()
1760
with ui.ui_factory.nested_progress_bar() as child_pb:
1865
1761
entries = self._entries_to_incorporate()
1866
1762
entries = list(entries)
1867
for num, (entry, parent_id) in enumerate(entries):
1868
child_pb.update(gettext('Preparing file merge'), num, len(entries))
1763
for num, (entry, parent_id, relpath) in enumerate(entries):
1764
child_pb.update(gettext('Preparing file merge'),
1869
1766
parent_trans_id = self.tt.trans_id_file_id(parent_id)
1870
trans_id = transform.new_by_entry(self.tt, entry,
1871
parent_trans_id, self.other_tree)
1767
path = osutils.pathjoin(self._source_subpath, relpath)
1768
trans_id = transform.new_by_entry(path, self.tt, entry,
1769
parent_trans_id, self.other_tree)
1874
1770
self._finish_computing_transform()
1876
1772
def _entries_to_incorporate(self):
1877
1773
"""Yields pairs of (inventory_entry, new_parent)."""
1878
other_inv = self.other_tree.root_inventory
1879
subdir_id = other_inv.path2id(self._source_subpath)
1774
subdir_id = self.other_tree.path2id(self._source_subpath)
1880
1775
if subdir_id is None:
1881
1776
# XXX: The error would be clearer if it gave the URL of the source
1882
1777
# branch, but we don't have a reference to that here.
1883
1778
raise PathNotInTree(self._source_subpath, "Source tree")
1884
subdir = other_inv[subdir_id]
1779
subdir = next(self.other_tree.iter_entries_by_dir(
1780
specific_files=[self._source_subpath]))[1]
1885
1781
parent_in_target = osutils.dirname(self._target_subdir)
1886
1782
target_id = self.this_tree.path2id(parent_in_target)
1887
1783
if target_id is None: