460
499
self.change_reporter = change_reporter
461
500
if self.pp is None:
462
501
self.pp = ProgressPhase("Merge phase", 3, self.pb)
464
self.tt = TreeTransform(working_tree, self.pb)
506
self.this_tree.lock_tree_write()
507
self.base_tree.lock_read()
508
self.other_tree.lock_read()
509
self.tt = TreeTransform(self.this_tree, self.pb)
466
511
self.pp.next_phase()
467
entries = self._entries3()
468
child_pb = ui.ui_factory.nested_progress_bar()
470
for num, (file_id, changed, parents3, names3,
471
executable3) in enumerate(entries):
472
child_pb.update('Preparing file merge', num, len(entries))
473
self._merge_names(file_id, parents3, names3)
475
file_status = self.merge_contents(file_id)
477
file_status = 'unmodified'
478
self._merge_executable(file_id,
479
executable3, file_status)
484
child_pb = ui.ui_factory.nested_progress_bar()
486
fs_conflicts = resolve_conflicts(self.tt, child_pb,
487
lambda t, c: conflict_pass(t, c, self.other_tree))
490
if change_reporter is not None:
491
from bzrlib import delta
492
delta.report_changes(self.tt._iter_changes(), change_reporter)
493
self.cook_conflicts(fs_conflicts)
494
for conflict in self.cooked_conflicts:
512
self._compute_transform()
496
513
self.pp.next_phase()
497
514
results = self.tt.apply(no_conflicts=True)
498
515
self.write_modified(results)
500
working_tree.add_conflicts(self.cooked_conflicts)
517
self.this_tree.add_conflicts(self.cooked_conflicts)
501
518
except UnsupportedOperation:
974
1037
"""Three-way tree merger, text weave merger."""
975
1038
supports_reprocess = True
976
1039
supports_show_base = False
1040
supports_reverse_cherrypick = False
1041
history_based = True
978
1043
def __init__(self, working_tree, this_tree, base_tree, other_tree,
979
1044
interesting_ids=None, pb=DummyProgress(), pp=None,
980
1045
reprocess=False, change_reporter=None,
981
interesting_files=None):
1046
interesting_files=None, cherrypick=False, do_merge=True):
1047
self.cherrypick = cherrypick
982
1048
super(WeaveMerger, self).__init__(working_tree, this_tree,
983
1049
base_tree, other_tree,
984
1050
interesting_ids=interesting_ids,
985
1051
pb=pb, pp=pp, reprocess=reprocess,
986
change_reporter=change_reporter)
1052
change_reporter=change_reporter,
988
1055
def _merged_lines(self, file_id):
989
1056
"""Generate the merged lines.
990
1057
There is no distinction between lines that are meant to contain <<<<<<<
993
plan = self.this_tree.plan_file_merge(file_id, self.other_tree)
1061
base = self.base_tree
1064
plan = self.this_tree.plan_file_merge(file_id, self.other_tree,
1066
if 'merge' in debug.debug_flags:
1068
trans_id = self.tt.trans_id_file_id(file_id)
1069
name = self.tt.final_name(trans_id) + '.plan'
1070
contents = ('%10s|%s' % l for l in plan)
1071
self.tt.new_file(name, self.tt.final_parent(trans_id), contents)
994
1072
textmerge = PlanWeaveMerge(plan, '<<<<<<< TREE\n',
995
1073
'>>>>>>> MERGE-SOURCE\n')
996
1074
return textmerge.merge_lines(self.reprocess)
1137
1239
for text_a, text_b in zip(plain_a[ai:a_cur], plain_b[bi:b_cur]):
1138
1240
assert text_a == text_b
1139
1241
yield "unchanged", text_a
1244
class _PlanMergeBase(object):
1246
def __init__(self, a_rev, b_rev, vf):
1249
:param a_rev: Revision-id of one revision to merge
1250
:param b_rev: Revision-id of the other revision to merge
1251
:param vf: A versionedfile containing both revisions
1255
self.lines_a = vf.get_lines(a_rev)
1256
self.lines_b = vf.get_lines(b_rev)
1258
self._last_lines = None
1259
self._last_lines_revision_id = None
1260
self._cached_matching_blocks = {}
1262
def plan_merge(self):
1263
"""Generate a 'plan' for merging the two revisions.
1265
This involves comparing their texts and determining the cause of
1266
differences. If text A has a line and text B does not, then either the
1267
line was added to text A, or it was deleted from B. Once the causes
1268
are combined, they are written out in the format described in
1269
VersionedFile.plan_merge
1271
blocks = self._get_matching_blocks(self.a_rev, self.b_rev)
1272
unique_a, unique_b = self._unique_lines(blocks)
1273
new_a, killed_b = self._determine_status(self.a_rev, unique_a)
1274
new_b, killed_a = self._determine_status(self.b_rev, unique_b)
1275
return self._iter_plan(blocks, new_a, killed_b, new_b, killed_a)
1277
def _iter_plan(self, blocks, new_a, killed_b, new_b, killed_a):
1280
for i, j, n in blocks:
1281
for a_index in range(last_i, i):
1282
if a_index in new_a:
1283
if a_index in killed_b:
1284
yield 'conflicted-a', self.lines_a[a_index]
1286
yield 'new-a', self.lines_a[a_index]
1288
yield 'killed-b', self.lines_a[a_index]
1289
for b_index in range(last_j, j):
1290
if b_index in new_b:
1291
if b_index in killed_a:
1292
yield 'conflicted-b', self.lines_b[a_index]
1294
yield 'new-b', self.lines_b[b_index]
1296
yield 'killed-a', self.lines_b[b_index]
1297
# handle common lines
1298
for a_index in range(i, i+n):
1299
yield 'unchanged', self.lines_a[a_index]
1303
def _get_matching_blocks(self, left_revision, right_revision):
1304
"""Return a description of which sections of two revisions match.
1306
See SequenceMatcher.get_matching_blocks
1308
cached = self._cached_matching_blocks.get((left_revision,
1310
if cached is not None:
1312
if self._last_lines_revision_id == left_revision:
1313
left_lines = self._last_lines
1315
left_lines = self.vf.get_lines(left_revision)
1316
right_lines = self.vf.get_lines(right_revision)
1317
self._last_lines = right_lines
1318
self._last_lines_revision_id = right_revision
1319
matcher = patiencediff.PatienceSequenceMatcher(None, left_lines,
1321
return matcher.get_matching_blocks()
1323
def _unique_lines(self, matching_blocks):
1324
"""Analyse matching_blocks to determine which lines are unique
1326
:return: a tuple of (unique_left, unique_right), where the values are
1327
sets of line numbers of unique lines.
1333
for i, j, n in matching_blocks:
1334
unique_left.extend(range(last_i, i))
1335
unique_right.extend(range(last_j, j))
1338
return unique_left, unique_right
1341
def _subtract_plans(old_plan, new_plan):
1342
"""Remove changes from new_plan that came from old_plan.
1344
It is assumed that the difference between the old_plan and new_plan
1345
is their choice of 'b' text.
1347
All lines from new_plan that differ from old_plan are emitted
1348
verbatim. All lines from new_plan that match old_plan but are
1349
not about the 'b' revision are emitted verbatim.
1351
Lines that match and are about the 'b' revision are the lines we
1352
don't want, so we convert 'killed-b' -> 'unchanged', and 'new-b'
1353
is skipped entirely.
1355
matcher = patiencediff.PatienceSequenceMatcher(None, old_plan,
1358
for i, j, n in matcher.get_matching_blocks():
1359
for jj in range(last_j, j):
1361
for jj in range(j, j+n):
1362
plan_line = new_plan[jj]
1363
if plan_line[0] == 'new-b':
1365
elif plan_line[0] == 'killed-b':
1366
yield 'unchanged', plan_line[1]
1372
class _PlanMerge(_PlanMergeBase):
1373
"""Plan an annotate merge using on-the-fly annotation"""
1375
def __init__(self, a_rev, b_rev, vf):
1376
_PlanMergeBase.__init__(self, a_rev, b_rev, vf)
1377
a_ancestry = set(vf.get_ancestry(a_rev, topo_sorted=False))
1378
b_ancestry = set(vf.get_ancestry(b_rev, topo_sorted=False))
1379
self.uncommon = a_ancestry.symmetric_difference(b_ancestry)
1381
def _determine_status(self, revision_id, unique_line_numbers):
1382
"""Determines the status unique lines versus all lcas.
1384
Basically, determines why the line is unique to this revision.
1386
A line may be determined new or killed, but not both.
1388
:param revision_id: The id of the revision in which the lines are
1390
:param unique_line_numbers: The line numbers of unique lines.
1391
:return a tuple of (new_this, killed_other):
1393
new = self._find_new(revision_id)
1394
killed = set(unique_line_numbers).difference(new)
1397
def _find_new(self, version_id):
1398
"""Determine which lines are new in the ancestry of this version.
1400
If a lines is present in this version, and not present in any
1401
common ancestor, it is considered new.
1403
if version_id not in self.uncommon:
1405
parents = self.vf.get_parents(version_id)
1406
if len(parents) == 0:
1407
return set(range(len(self.vf.get_lines(version_id))))
1409
for parent in parents:
1410
blocks = self._get_matching_blocks(version_id, parent)
1411
result, unused = self._unique_lines(blocks)
1412
parent_new = self._find_new(parent)
1413
for i, j, n in blocks:
1414
for ii, jj in [(i+r, j+r) for r in range(n)]:
1415
if jj in parent_new:
1420
new.intersection_update(result)
1424
class _PlanLCAMerge(_PlanMergeBase):
1426
This merge algorithm differs from _PlanMerge in that:
1427
1. comparisons are done against LCAs only
1428
2. cases where a contested line is new versus one LCA but old versus
1429
another are marked as conflicts, by emitting the line as conflicted-a
1432
This is faster, and hopefully produces more useful output.
1435
def __init__(self, a_rev, b_rev, vf, graph):
1436
_PlanMergeBase.__init__(self, a_rev, b_rev, vf)
1437
self.lcas = graph.find_lca(a_rev, b_rev)
1438
for lca in self.lcas:
1439
lca_lines = self.vf.get_lines(lca)
1440
matcher = patiencediff.PatienceSequenceMatcher(None, self.lines_a,
1442
blocks = list(matcher.get_matching_blocks())
1443
self._cached_matching_blocks[(a_rev, lca)] = blocks
1444
matcher = patiencediff.PatienceSequenceMatcher(None, self.lines_b,
1446
blocks = list(matcher.get_matching_blocks())
1447
self._cached_matching_blocks[(b_rev, lca)] = blocks
1449
def _determine_status(self, revision_id, unique_line_numbers):
1450
"""Determines the status unique lines versus all lcas.
1452
Basically, determines why the line is unique to this revision.
1454
A line may be determined new, killed, or both.
1456
If a line is determined new, that means it was not present in at least
1457
one LCA, and is not present in the other merge revision.
1459
If a line is determined killed, that means the line was present in
1462
If a line is killed and new, this indicates that the two merge
1463
revisions contain differing conflict resolutions.
1464
:param revision_id: The id of the revision in which the lines are
1466
:param unique_line_numbers: The line numbers of unique lines.
1467
:return a tuple of (new_this, killed_other):
1471
unique_line_numbers = set(unique_line_numbers)
1472
for lca in self.lcas:
1473
blocks = self._get_matching_blocks(revision_id, lca)
1474
unique_vs_lca, _ignored = self._unique_lines(blocks)
1475
new.update(unique_line_numbers.intersection(unique_vs_lca))
1476
killed.update(unique_line_numbers.difference(unique_vs_lca))