1109
630
('conflicted-b', 'e\n'),
1112
def test_plan_lca_merge_with_null(self):
1113
self.add_version(('root', 'A'), [], 'ab')
1114
self.add_version(('root', 'B'), [], 'bc')
1115
plan = self.plan_merge_vf.plan_lca_merge('A', 'B')
1116
self.assertEqual([('new-a', 'a\n'),
1117
('unchanged', 'b\n'),
1121
def test_plan_merge_with_delete_and_change(self):
1122
self.add_rev('root', 'C', [], 'a')
1123
self.add_rev('root', 'A', ['C'], 'b')
1124
self.add_rev('root', 'B', ['C'], '')
1125
plan = self.plan_merge_vf.plan_merge('A', 'B')
1126
self.assertEqual([('killed-both', 'a\n'),
1130
def test_plan_merge_with_move_and_change(self):
1131
self.add_rev('root', 'C', [], 'abcd')
1132
self.add_rev('root', 'A', ['C'], 'acbd')
1133
self.add_rev('root', 'B', ['C'], 'aBcd')
1134
plan = self.plan_merge_vf.plan_merge('A', 'B')
1135
self.assertEqual([('unchanged', 'a\n'),
1137
('killed-b', 'b\n'),
1139
('killed-a', 'c\n'),
1140
('unchanged', 'd\n'),
1144
class LoggingMerger(object):
1145
# These seem to be the required attributes
1146
requires_base = False
1147
supports_reprocess = False
1148
supports_show_base = False
1149
supports_cherrypick = False
1150
# We intentionally do not define supports_lca_trees
1152
def __init__(self, *args, **kwargs):
1154
self.kwargs = kwargs
1157
class TestMergerBase(TestCaseWithMemoryTransport):
1158
"""Common functionality for Merger tests that don't write to disk."""
1160
def get_builder(self):
1161
builder = self.make_branch_builder('path')
1162
builder.start_series()
1163
self.addCleanup(builder.finish_series)
1166
def setup_simple_graph(self):
1167
"""Create a simple 3-node graph.
1169
:return: A BranchBuilder
1176
builder = self.get_builder()
1177
builder.build_snapshot('A-id', None,
1178
[('add', ('', None, 'directory', None))])
1179
builder.build_snapshot('C-id', ['A-id'], [])
1180
builder.build_snapshot('B-id', ['A-id'], [])
1183
def setup_criss_cross_graph(self):
1184
"""Create a 5-node graph with a criss-cross.
1186
:return: A BranchBuilder
1193
builder = self.setup_simple_graph()
1194
builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
1195
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1198
def make_Merger(self, builder, other_revision_id,
1199
interesting_files=None, interesting_ids=None):
1200
"""Make a Merger object from a branch builder"""
1201
mem_tree = memorytree.MemoryTree.create_on_branch(builder.get_branch())
1202
mem_tree.lock_write()
1203
self.addCleanup(mem_tree.unlock)
1204
merger = _mod_merge.Merger.from_revision_ids(None,
1205
mem_tree, other_revision_id)
1206
merger.set_interesting_files(interesting_files)
1207
# It seems there is no matching function for set_interesting_ids
1208
merger.interesting_ids = interesting_ids
1209
merger.merge_type = _mod_merge.Merge3Merger
1213
class TestMergerInMemory(TestMergerBase):
1215
def test_cache_trees_with_revision_ids_None(self):
1216
merger = self.make_Merger(self.setup_simple_graph(), 'C-id')
1217
original_cache = dict(merger._cached_trees)
1218
merger.cache_trees_with_revision_ids([None])
1219
self.assertEqual(original_cache, merger._cached_trees)
1221
def test_cache_trees_with_revision_ids_no_revision_id(self):
1222
merger = self.make_Merger(self.setup_simple_graph(), 'C-id')
1223
original_cache = dict(merger._cached_trees)
1224
tree = self.make_branch_and_memory_tree('tree')
1225
merger.cache_trees_with_revision_ids([tree])
1226
self.assertEqual(original_cache, merger._cached_trees)
1228
def test_cache_trees_with_revision_ids_having_revision_id(self):
1229
merger = self.make_Merger(self.setup_simple_graph(), 'C-id')
1230
original_cache = dict(merger._cached_trees)
1231
tree = merger.this_branch.repository.revision_tree('B-id')
1232
original_cache['B-id'] = tree
1233
merger.cache_trees_with_revision_ids([tree])
1234
self.assertEqual(original_cache, merger._cached_trees)
1236
def test_find_base(self):
1237
merger = self.make_Merger(self.setup_simple_graph(), 'C-id')
1238
self.assertEqual('A-id', merger.base_rev_id)
1239
self.assertFalse(merger._is_criss_cross)
1240
self.assertIs(None, merger._lca_trees)
1242
def test_find_base_criss_cross(self):
1243
builder = self.setup_criss_cross_graph()
1244
merger = self.make_Merger(builder, 'E-id')
1245
self.assertEqual('A-id', merger.base_rev_id)
1246
self.assertTrue(merger._is_criss_cross)
1247
self.assertEqual(['B-id', 'C-id'], [t.get_revision_id()
1248
for t in merger._lca_trees])
1249
# If we swap the order, we should get a different lca order
1250
builder.build_snapshot('F-id', ['E-id'], [])
1251
merger = self.make_Merger(builder, 'D-id')
1252
self.assertEqual(['C-id', 'B-id'], [t.get_revision_id()
1253
for t in merger._lca_trees])
1255
def test_find_base_triple_criss_cross(self):
1258
# B C F # F is merged into both branches
1265
builder = self.setup_criss_cross_graph()
1266
builder.build_snapshot('F-id', ['A-id'], [])
1267
builder.build_snapshot('H-id', ['E-id', 'F-id'], [])
1268
builder.build_snapshot('G-id', ['D-id', 'F-id'], [])
1269
merger = self.make_Merger(builder, 'H-id')
1270
self.assertEqual(['B-id', 'C-id', 'F-id'],
1271
[t.get_revision_id() for t in merger._lca_trees])
1273
def test_no_criss_cross_passed_to_merge_type(self):
1274
class LCATreesMerger(LoggingMerger):
1275
supports_lca_trees = True
1277
merger = self.make_Merger(self.setup_simple_graph(), 'C-id')
1278
merger.merge_type = LCATreesMerger
1279
merge_obj = merger.make_merger()
1280
self.assertIsInstance(merge_obj, LCATreesMerger)
1281
self.assertFalse('lca_trees' in merge_obj.kwargs)
1283
def test_criss_cross_passed_to_merge_type(self):
1284
merger = self.make_Merger(self.setup_criss_cross_graph(), 'E-id')
1285
merger.merge_type = _mod_merge.Merge3Merger
1286
merge_obj = merger.make_merger()
1287
self.assertEqual(['B-id', 'C-id'], [t.get_revision_id()
1288
for t in merger._lca_trees])
1290
def test_criss_cross_not_supported_merge_type(self):
1291
merger = self.make_Merger(self.setup_criss_cross_graph(), 'E-id')
1292
# We explicitly do not define supports_lca_trees
1293
merger.merge_type = LoggingMerger
1294
merge_obj = merger.make_merger()
1295
self.assertIsInstance(merge_obj, LoggingMerger)
1296
self.assertFalse('lca_trees' in merge_obj.kwargs)
1298
def test_criss_cross_unsupported_merge_type(self):
1299
class UnsupportedLCATreesMerger(LoggingMerger):
1300
supports_lca_trees = False
1302
merger = self.make_Merger(self.setup_criss_cross_graph(), 'E-id')
1303
merger.merge_type = UnsupportedLCATreesMerger
1304
merge_obj = merger.make_merger()
1305
self.assertIsInstance(merge_obj, UnsupportedLCATreesMerger)
1306
self.assertFalse('lca_trees' in merge_obj.kwargs)
1309
class TestMergerEntriesLCA(TestMergerBase):
1311
def make_merge_obj(self, builder, other_revision_id,
1312
interesting_files=None, interesting_ids=None):
1313
merger = self.make_Merger(builder, other_revision_id,
1314
interesting_files=interesting_files,
1315
interesting_ids=interesting_ids)
1316
return merger.make_merger()
1318
def test_simple(self):
1319
builder = self.get_builder()
1320
builder.build_snapshot('A-id', None,
1321
[('add', (u'', 'a-root-id', 'directory', None)),
1322
('add', (u'a', 'a-id', 'file', 'a\nb\nc\n'))])
1323
builder.build_snapshot('C-id', ['A-id'],
1324
[('modify', ('a-id', 'a\nb\nC\nc\n'))])
1325
builder.build_snapshot('B-id', ['A-id'],
1326
[('modify', ('a-id', 'a\nB\nb\nc\n'))])
1327
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1328
[('modify', ('a-id', 'a\nB\nb\nC\nc\nE\n'))])
1329
builder.build_snapshot('D-id', ['B-id', 'C-id'],
1330
[('modify', ('a-id', 'a\nB\nb\nC\nc\n'))])
1331
merge_obj = self.make_merge_obj(builder, 'E-id')
1333
self.assertEqual(['B-id', 'C-id'], [t.get_revision_id()
1334
for t in merge_obj._lca_trees])
1335
self.assertEqual('A-id', merge_obj.base_tree.get_revision_id())
1336
entries = list(merge_obj._entries_lca())
1338
# (file_id, changed, parents, names, executable)
1339
# BASE, lca1, lca2, OTHER, THIS
1340
root_id = 'a-root-id'
1341
self.assertEqual([('a-id', True,
1342
((root_id, [root_id, root_id]), root_id, root_id),
1343
((u'a', [u'a', u'a']), u'a', u'a'),
1344
((False, [False, False]), False, False)),
1347
def test_not_in_base(self):
1348
# LCAs all have the same last-modified revision for the file, as do
1349
# the tips, but the base has something different
1350
# A base, doesn't have the file
1352
# B C B introduces 'foo', C introduces 'bar'
1354
# D E D and E now both have 'foo' and 'bar'
1356
# F G the files are now in F, G, D and E, but not in A
1359
builder = self.get_builder()
1360
builder.build_snapshot('A-id', None,
1361
[('add', (u'', 'a-root-id', 'directory', None))])
1362
builder.build_snapshot('B-id', ['A-id'],
1363
[('add', (u'foo', 'foo-id', 'file', 'a\nb\nc\n'))])
1364
builder.build_snapshot('C-id', ['A-id'],
1365
[('add', (u'bar', 'bar-id', 'file', 'd\ne\nf\n'))])
1366
builder.build_snapshot('D-id', ['B-id', 'C-id'],
1367
[('add', (u'bar', 'bar-id', 'file', 'd\ne\nf\n'))])
1368
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1369
[('add', (u'foo', 'foo-id', 'file', 'a\nb\nc\n'))])
1370
builder.build_snapshot('G-id', ['E-id', 'D-id'],
1371
[('modify', (u'bar-id', 'd\ne\nf\nG\n'))])
1372
builder.build_snapshot('F-id', ['D-id', 'E-id'], [])
1373
merge_obj = self.make_merge_obj(builder, 'G-id')
1375
self.assertEqual(['D-id', 'E-id'], [t.get_revision_id()
1376
for t in merge_obj._lca_trees])
1377
self.assertEqual('A-id', merge_obj.base_tree.get_revision_id())
1378
entries = list(merge_obj._entries_lca())
1379
root_id = 'a-root-id'
1380
self.assertEqual([('bar-id', True,
1381
((None, [root_id, root_id]), root_id, root_id),
1382
((None, [u'bar', u'bar']), u'bar', u'bar'),
1383
((None, [False, False]), False, False)),
1386
def test_not_in_this(self):
1387
builder = self.get_builder()
1388
builder.build_snapshot('A-id', None,
1389
[('add', (u'', 'a-root-id', 'directory', None)),
1390
('add', (u'a', 'a-id', 'file', 'a\nb\nc\n'))])
1391
builder.build_snapshot('B-id', ['A-id'],
1392
[('modify', ('a-id', 'a\nB\nb\nc\n'))])
1393
builder.build_snapshot('C-id', ['A-id'],
1394
[('modify', ('a-id', 'a\nb\nC\nc\n'))])
1395
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1396
[('modify', ('a-id', 'a\nB\nb\nC\nc\nE\n'))])
1397
builder.build_snapshot('D-id', ['B-id', 'C-id'],
1398
[('unversion', 'a-id')])
1399
merge_obj = self.make_merge_obj(builder, 'E-id')
1401
self.assertEqual(['B-id', 'C-id'], [t.get_revision_id()
1402
for t in merge_obj._lca_trees])
1403
self.assertEqual('A-id', merge_obj.base_tree.get_revision_id())
1405
entries = list(merge_obj._entries_lca())
1406
root_id = 'a-root-id'
1407
self.assertEqual([('a-id', True,
1408
((root_id, [root_id, root_id]), root_id, None),
1409
((u'a', [u'a', u'a']), u'a', None),
1410
((False, [False, False]), False, None)),
1413
def test_file_not_in_one_lca(self):
1416
# B C # B no file, C introduces a file
1418
# D E # D and E both have the file, unchanged from C
1419
builder = self.get_builder()
1420
builder.build_snapshot('A-id', None,
1421
[('add', (u'', 'a-root-id', 'directory', None))])
1422
builder.build_snapshot('B-id', ['A-id'], [])
1423
builder.build_snapshot('C-id', ['A-id'],
1424
[('add', (u'a', 'a-id', 'file', 'a\nb\nc\n'))])
1425
builder.build_snapshot('E-id', ['C-id', 'B-id'], []) # Inherited from C
1426
builder.build_snapshot('D-id', ['B-id', 'C-id'], # Merged from C
1427
[('add', (u'a', 'a-id', 'file', 'a\nb\nc\n'))])
1428
merge_obj = self.make_merge_obj(builder, 'E-id')
1430
self.assertEqual(['B-id', 'C-id'], [t.get_revision_id()
1431
for t in merge_obj._lca_trees])
1432
self.assertEqual('A-id', merge_obj.base_tree.get_revision_id())
1434
entries = list(merge_obj._entries_lca())
1435
self.assertEqual([], entries)
1437
def test_not_in_other(self):
1438
builder = self.get_builder()
1439
builder.build_snapshot('A-id', None,
1440
[('add', (u'', 'a-root-id', 'directory', None)),
1441
('add', (u'a', 'a-id', 'file', 'a\nb\nc\n'))])
1442
builder.build_snapshot('B-id', ['A-id'], [])
1443
builder.build_snapshot('C-id', ['A-id'], [])
1444
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1445
[('unversion', 'a-id')])
1446
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1447
merge_obj = self.make_merge_obj(builder, 'E-id')
1449
entries = list(merge_obj._entries_lca())
1450
root_id = 'a-root-id'
1451
self.assertEqual([('a-id', True,
1452
((root_id, [root_id, root_id]), None, root_id),
1453
((u'a', [u'a', u'a']), None, u'a'),
1454
((False, [False, False]), None, False)),
1457
def test_not_in_other_or_lca(self):
1458
# A base, introduces 'foo'
1460
# B C B nothing, C deletes foo
1462
# D E D restores foo (same as B), E leaves it deleted
1464
# A => B, no changes
1465
# A => C, delete foo (C should supersede B)
1466
# C => D, restore foo
1467
# C => E, no changes
1468
# D would then win 'cleanly' and no record would be given
1469
builder = self.get_builder()
1470
builder.build_snapshot('A-id', None,
1471
[('add', (u'', 'a-root-id', 'directory', None)),
1472
('add', (u'foo', 'foo-id', 'file', 'content\n'))])
1473
builder.build_snapshot('B-id', ['A-id'], [])
1474
builder.build_snapshot('C-id', ['A-id'],
1475
[('unversion', 'foo-id')])
1476
builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
1477
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1478
merge_obj = self.make_merge_obj(builder, 'E-id')
1480
entries = list(merge_obj._entries_lca())
1481
self.assertEqual([], entries)
1483
def test_not_in_other_mod_in_lca1_not_in_lca2(self):
1484
# A base, introduces 'foo'
1486
# B C B changes 'foo', C deletes foo
1488
# D E D restores foo (same as B), E leaves it deleted (as C)
1490
# A => B, modified foo
1491
# A => C, delete foo, C does not supersede B
1492
# B => D, no changes
1493
# C => D, resolve in favor of B
1494
# B => E, resolve in favor of E
1495
# C => E, no changes
1496
# In this case, we have a conflict of how the changes were resolved. E
1497
# picked C and D picked B, so we should issue a conflict
1498
builder = self.get_builder()
1499
builder.build_snapshot('A-id', None,
1500
[('add', (u'', 'a-root-id', 'directory', None)),
1501
('add', (u'foo', 'foo-id', 'file', 'content\n'))])
1502
builder.build_snapshot('B-id', ['A-id'], [
1503
('modify', ('foo-id', 'new-content\n'))])
1504
builder.build_snapshot('C-id', ['A-id'],
1505
[('unversion', 'foo-id')])
1506
builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
1507
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1508
merge_obj = self.make_merge_obj(builder, 'E-id')
1510
entries = list(merge_obj._entries_lca())
1511
root_id = 'a-root-id'
1512
self.assertEqual([('foo-id', True,
1513
((root_id, [root_id, None]), None, root_id),
1514
((u'foo', [u'foo', None]), None, 'foo'),
1515
((False, [False, None]), None, False)),
1518
def test_only_in_one_lca(self):
1521
# B C B nothing, C add file
1523
# D E D still has nothing, E removes file
1526
# C => D, removed the file
1528
# C => E, removed the file
1529
# Thus D & E have identical changes, and this is a no-op
1532
# A => C, add file, thus C supersedes B
1533
# w/ C=BASE, D=THIS, E=OTHER we have 'happy convergence'
1534
builder = self.get_builder()
1535
builder.build_snapshot('A-id', None,
1536
[('add', (u'', 'a-root-id', 'directory', None))])
1537
builder.build_snapshot('B-id', ['A-id'], [])
1538
builder.build_snapshot('C-id', ['A-id'],
1539
[('add', (u'a', 'a-id', 'file', 'a\nb\nc\n'))])
1540
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1541
[('unversion', 'a-id')])
1542
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1543
merge_obj = self.make_merge_obj(builder, 'E-id')
1545
entries = list(merge_obj._entries_lca())
1546
self.assertEqual([], entries)
1548
def test_only_in_other(self):
1549
builder = self.get_builder()
1550
builder.build_snapshot('A-id', None,
1551
[('add', (u'', 'a-root-id', 'directory', None))])
1552
builder.build_snapshot('B-id', ['A-id'], [])
1553
builder.build_snapshot('C-id', ['A-id'], [])
1554
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1555
[('add', (u'a', 'a-id', 'file', 'a\nb\nc\n'))])
1556
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1557
merge_obj = self.make_merge_obj(builder, 'E-id')
1559
entries = list(merge_obj._entries_lca())
1560
root_id = 'a-root-id'
1561
self.assertEqual([('a-id', True,
1562
((None, [None, None]), root_id, None),
1563
((None, [None, None]), u'a', None),
1564
((None, [None, None]), False, None)),
1567
def test_one_lca_supersedes(self):
1568
# One LCA supersedes the other LCAs last modified value, but the
1569
# value is not the same as BASE.
1570
# A base, introduces 'foo', last mod A
1572
# B C B modifies 'foo' (mod B), C does nothing (mod A)
1574
# D E D does nothing (mod B), E updates 'foo' (mod E)
1576
# F G F updates 'foo' (mod F). G does nothing (mod E)
1578
# At this point, G should not be considered to modify 'foo', even
1579
# though its LCAs disagree. This is because the modification in E
1580
# completely supersedes the value in D.
1581
builder = self.get_builder()
1582
builder.build_snapshot('A-id', None,
1583
[('add', (u'', 'a-root-id', 'directory', None)),
1584
('add', (u'foo', 'foo-id', 'file', 'A content\n'))])
1585
builder.build_snapshot('C-id', ['A-id'], [])
1586
builder.build_snapshot('B-id', ['A-id'],
1587
[('modify', ('foo-id', 'B content\n'))])
1588
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1589
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1590
[('modify', ('foo-id', 'E content\n'))])
1591
builder.build_snapshot('G-id', ['E-id', 'D-id'], [])
1592
builder.build_snapshot('F-id', ['D-id', 'E-id'],
1593
[('modify', ('foo-id', 'F content\n'))])
1594
merge_obj = self.make_merge_obj(builder, 'G-id')
1596
self.assertEqual([], list(merge_obj._entries_lca()))
1598
def test_one_lca_supersedes_path(self):
1599
# Double-criss-cross merge, the ultimate base value is different from
1603
# B C B value 'bar', C = 'foo'
1605
# D E D = 'bar', E supersedes to 'bing'
1607
# F G F = 'bing', G supersedes to 'barry'
1609
# In this case, we technically should not care about the value 'bar' for
1610
# D, because it was clearly superseded by E's 'bing'. The
1611
# per-file/attribute graph would actually look like:
1620
# Because the other side of the merge never modifies the value, it just
1621
# takes the value from the merge.
1623
# ATM this fails because we will prune 'foo' from the LCAs, but we
1624
# won't prune 'bar'. This is getting far off into edge-case land, so we
1625
# aren't supporting it yet.
1627
builder = self.get_builder()
1628
builder.build_snapshot('A-id', None,
1629
[('add', (u'', 'a-root-id', 'directory', None)),
1630
('add', (u'foo', 'foo-id', 'file', 'A content\n'))])
1631
builder.build_snapshot('C-id', ['A-id'], [])
1632
builder.build_snapshot('B-id', ['A-id'],
1633
[('rename', ('foo', 'bar'))])
1634
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1635
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1636
[('rename', ('foo', 'bing'))]) # override to bing
1637
builder.build_snapshot('G-id', ['E-id', 'D-id'],
1638
[('rename', ('bing', 'barry'))]) # override to barry
1639
builder.build_snapshot('F-id', ['D-id', 'E-id'],
1640
[('rename', ('bar', 'bing'))]) # Merge in E's change
1641
merge_obj = self.make_merge_obj(builder, 'G-id')
1643
self.expectFailure("We don't do an actual heads() check on lca values,"
1644
" or use the per-attribute graph",
1645
self.assertEqual, [], list(merge_obj._entries_lca()))
1647
def test_one_lca_accidentally_pruned(self):
1648
# Another incorrect resolution from the same basic flaw:
1651
# B C B value 'bar', C = 'foo'
1653
# D E D = 'bar', E reverts to 'foo'
1655
# F G F = 'bing', G switches to 'bar'
1657
# 'bar' will not be seen as an interesting change, because 'foo' will
1658
# be pruned from the LCAs, even though it was newly introduced by E
1660
builder = self.get_builder()
1661
builder.build_snapshot('A-id', None,
1662
[('add', (u'', 'a-root-id', 'directory', None)),
1663
('add', (u'foo', 'foo-id', 'file', 'A content\n'))])
1664
builder.build_snapshot('C-id', ['A-id'], [])
1665
builder.build_snapshot('B-id', ['A-id'],
1666
[('rename', ('foo', 'bar'))])
1667
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1668
builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
1669
builder.build_snapshot('G-id', ['E-id', 'D-id'],
1670
[('rename', ('foo', 'bar'))])
1671
builder.build_snapshot('F-id', ['D-id', 'E-id'],
1672
[('rename', ('bar', 'bing'))]) # should end up conflicting
1673
merge_obj = self.make_merge_obj(builder, 'G-id')
1675
entries = list(merge_obj._entries_lca())
1676
root_id = 'a-root-id'
1677
self.expectFailure("We prune values from BASE even when relevant.",
1680
((root_id, [root_id, root_id]), root_id, root_id),
1681
((u'foo', [u'bar', u'foo']), u'bar', u'bing'),
1682
((False, [False, False]), False, False)),
1685
def test_both_sides_revert(self):
1686
# Both sides of a criss-cross revert the text to the lca
1687
# A base, introduces 'foo'
1689
# B C B modifies 'foo', C modifies 'foo'
1691
# D E D reverts to B, E reverts to C
1692
# This should conflict
1693
builder = self.get_builder()
1694
builder.build_snapshot('A-id', None,
1695
[('add', (u'', 'a-root-id', 'directory', None)),
1696
('add', (u'foo', 'foo-id', 'file', 'A content\n'))])
1697
builder.build_snapshot('B-id', ['A-id'],
1698
[('modify', ('foo-id', 'B content\n'))])
1699
builder.build_snapshot('C-id', ['A-id'],
1700
[('modify', ('foo-id', 'C content\n'))])
1701
builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
1702
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1703
merge_obj = self.make_merge_obj(builder, 'E-id')
1705
entries = list(merge_obj._entries_lca())
1706
root_id = 'a-root-id'
1707
self.assertEqual([('foo-id', True,
1708
((root_id, [root_id, root_id]), root_id, root_id),
1709
((u'foo', [u'foo', u'foo']), u'foo', u'foo'),
1710
((False, [False, False]), False, False)),
1713
def test_different_lca_resolve_one_side_updates_content(self):
1714
# Both sides converge, but then one side updates the text.
1715
# A base, introduces 'foo'
1717
# B C B modifies 'foo', C modifies 'foo'
1719
# D E D reverts to B, E reverts to C
1721
# F F updates to a new value
1722
# We need to emit an entry for 'foo', because D & E differed on the
1724
builder = self.get_builder()
1725
builder.build_snapshot('A-id', None,
1726
[('add', (u'', 'a-root-id', 'directory', None)),
1727
('add', (u'foo', 'foo-id', 'file', 'A content\n'))])
1728
builder.build_snapshot('B-id', ['A-id'],
1729
[('modify', ('foo-id', 'B content\n'))])
1730
builder.build_snapshot('C-id', ['A-id'],
1731
[('modify', ('foo-id', 'C content\n'))])
1732
builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
1733
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1734
builder.build_snapshot('F-id', ['D-id'],
1735
[('modify', ('foo-id', 'F content\n'))])
1736
merge_obj = self.make_merge_obj(builder, 'E-id')
1738
entries = list(merge_obj._entries_lca())
1739
root_id = 'a-root-id'
1740
self.assertEqual([('foo-id', True,
1741
((root_id, [root_id, root_id]), root_id, root_id),
1742
((u'foo', [u'foo', u'foo']), u'foo', u'foo'),
1743
((False, [False, False]), False, False)),
1746
def test_same_lca_resolution_one_side_updates_content(self):
1747
# Both sides converge, but then one side updates the text.
1748
# A base, introduces 'foo'
1750
# B C B modifies 'foo', C modifies 'foo'
1752
# D E D and E use C's value
1754
# F F updates to a new value
1755
# I think it is a bug that this conflicts, but we don't have a way to
1756
# detect otherwise. And because of:
1757
# test_different_lca_resolve_one_side_updates_content
1758
# We need to conflict.
1760
builder = self.get_builder()
1761
builder.build_snapshot('A-id', None,
1762
[('add', (u'', 'a-root-id', 'directory', None)),
1763
('add', (u'foo', 'foo-id', 'file', 'A content\n'))])
1764
builder.build_snapshot('B-id', ['A-id'],
1765
[('modify', ('foo-id', 'B content\n'))])
1766
builder.build_snapshot('C-id', ['A-id'],
1767
[('modify', ('foo-id', 'C content\n'))])
1768
builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
1769
builder.build_snapshot('D-id', ['B-id', 'C-id'],
1770
[('modify', ('foo-id', 'C content\n'))]) # Same as E
1771
builder.build_snapshot('F-id', ['D-id'],
1772
[('modify', ('foo-id', 'F content\n'))])
1773
merge_obj = self.make_merge_obj(builder, 'E-id')
1775
entries = list(merge_obj._entries_lca())
1776
self.expectFailure("We don't detect that LCA resolution was the"
1777
" same on both sides",
1778
self.assertEqual, [], entries)
1780
def test_only_path_changed(self):
1781
builder = self.get_builder()
1782
builder.build_snapshot('A-id', None,
1783
[('add', (u'', 'a-root-id', 'directory', None)),
1784
('add', (u'a', 'a-id', 'file', 'content\n'))])
1785
builder.build_snapshot('B-id', ['A-id'], [])
1786
builder.build_snapshot('C-id', ['A-id'], [])
1787
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1788
[('rename', (u'a', u'b'))])
1789
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1790
merge_obj = self.make_merge_obj(builder, 'E-id')
1791
entries = list(merge_obj._entries_lca())
1792
root_id = 'a-root-id'
1793
# The content was not changed, only the path
1794
self.assertEqual([('a-id', False,
1795
((root_id, [root_id, root_id]), root_id, root_id),
1796
((u'a', [u'a', u'a']), u'b', u'a'),
1797
((False, [False, False]), False, False)),
1800
def test_kind_changed(self):
1801
# Identical content, except 'D' changes a-id into a directory
1802
builder = self.get_builder()
1803
builder.build_snapshot('A-id', None,
1804
[('add', (u'', 'a-root-id', 'directory', None)),
1805
('add', (u'a', 'a-id', 'file', 'content\n'))])
1806
builder.build_snapshot('B-id', ['A-id'], [])
1807
builder.build_snapshot('C-id', ['A-id'], [])
1808
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1809
[('unversion', 'a-id'),
1810
('add', (u'a', 'a-id', 'directory', None))])
1811
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1812
merge_obj = self.make_merge_obj(builder, 'E-id')
1813
entries = list(merge_obj._entries_lca())
1814
root_id = 'a-root-id'
1815
# Only the kind was changed (content)
1816
self.assertEqual([('a-id', True,
1817
((root_id, [root_id, root_id]), root_id, root_id),
1818
((u'a', [u'a', u'a']), u'a', u'a'),
1819
((False, [False, False]), False, False)),
1822
def test_this_changed_kind(self):
1823
# Identical content, but THIS changes a file to a directory
1824
builder = self.get_builder()
1825
builder.build_snapshot('A-id', None,
1826
[('add', (u'', 'a-root-id', 'directory', None)),
1827
('add', (u'a', 'a-id', 'file', 'content\n'))])
1828
builder.build_snapshot('B-id', ['A-id'], [])
1829
builder.build_snapshot('C-id', ['A-id'], [])
1830
builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
1831
builder.build_snapshot('D-id', ['B-id', 'C-id'],
1832
[('unversion', 'a-id'),
1833
('add', (u'a', 'a-id', 'directory', None))])
1834
merge_obj = self.make_merge_obj(builder, 'E-id')
1835
entries = list(merge_obj._entries_lca())
1836
# Only the kind was changed (content)
1837
self.assertEqual([], entries)
1839
def test_interesting_files(self):
1840
# Two files modified, but we should filter one of them
1841
builder = self.get_builder()
1842
builder.build_snapshot('A-id', None,
1843
[('add', (u'', 'a-root-id', 'directory', None)),
1844
('add', (u'a', 'a-id', 'file', 'content\n')),
1845
('add', (u'b', 'b-id', 'file', 'content\n'))])
1846
builder.build_snapshot('B-id', ['A-id'], [])
1847
builder.build_snapshot('C-id', ['A-id'], [])
1848
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1849
[('modify', ('a-id', 'new-content\n')),
1850
('modify', ('b-id', 'new-content\n'))])
1851
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1852
merge_obj = self.make_merge_obj(builder, 'E-id',
1853
interesting_files=['b'])
1854
entries = list(merge_obj._entries_lca())
1855
root_id = 'a-root-id'
1856
self.assertEqual([('b-id', True,
1857
((root_id, [root_id, root_id]), root_id, root_id),
1858
((u'b', [u'b', u'b']), u'b', u'b'),
1859
((False, [False, False]), False, False)),
1862
def test_interesting_file_in_this(self):
1863
# This renamed the file, but it should still match the entry in other
1864
builder = self.get_builder()
1865
builder.build_snapshot('A-id', None,
1866
[('add', (u'', 'a-root-id', 'directory', None)),
1867
('add', (u'a', 'a-id', 'file', 'content\n')),
1868
('add', (u'b', 'b-id', 'file', 'content\n'))])
1869
builder.build_snapshot('B-id', ['A-id'], [])
1870
builder.build_snapshot('C-id', ['A-id'], [])
1871
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1872
[('modify', ('a-id', 'new-content\n')),
1873
('modify', ('b-id', 'new-content\n'))])
1874
builder.build_snapshot('D-id', ['B-id', 'C-id'],
1875
[('rename', ('b', 'c'))])
1876
merge_obj = self.make_merge_obj(builder, 'E-id',
1877
interesting_files=['c'])
1878
entries = list(merge_obj._entries_lca())
1879
root_id = 'a-root-id'
1880
self.assertEqual([('b-id', True,
1881
((root_id, [root_id, root_id]), root_id, root_id),
1882
((u'b', [u'b', u'b']), u'b', u'c'),
1883
((False, [False, False]), False, False)),
1886
def test_interesting_file_in_base(self):
1887
# This renamed the file, but it should still match the entry in BASE
1888
builder = self.get_builder()
1889
builder.build_snapshot('A-id', None,
1890
[('add', (u'', 'a-root-id', 'directory', None)),
1891
('add', (u'a', 'a-id', 'file', 'content\n')),
1892
('add', (u'c', 'c-id', 'file', 'content\n'))])
1893
builder.build_snapshot('B-id', ['A-id'],
1894
[('rename', ('c', 'b'))])
1895
builder.build_snapshot('C-id', ['A-id'],
1896
[('rename', ('c', 'b'))])
1897
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1898
[('modify', ('a-id', 'new-content\n')),
1899
('modify', ('c-id', 'new-content\n'))])
1900
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1901
merge_obj = self.make_merge_obj(builder, 'E-id',
1902
interesting_files=['c'])
1903
entries = list(merge_obj._entries_lca())
1904
root_id = 'a-root-id'
1905
self.assertEqual([('c-id', True,
1906
((root_id, [root_id, root_id]), root_id, root_id),
1907
((u'c', [u'b', u'b']), u'b', u'b'),
1908
((False, [False, False]), False, False)),
1911
def test_interesting_file_in_lca(self):
1912
# This renamed the file, but it should still match the entry in LCA
1913
builder = self.get_builder()
1914
builder.build_snapshot('A-id', None,
1915
[('add', (u'', 'a-root-id', 'directory', None)),
1916
('add', (u'a', 'a-id', 'file', 'content\n')),
1917
('add', (u'b', 'b-id', 'file', 'content\n'))])
1918
builder.build_snapshot('B-id', ['A-id'],
1919
[('rename', ('b', 'c'))])
1920
builder.build_snapshot('C-id', ['A-id'], [])
1921
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1922
[('modify', ('a-id', 'new-content\n')),
1923
('modify', ('b-id', 'new-content\n'))])
1924
builder.build_snapshot('D-id', ['B-id', 'C-id'],
1925
[('rename', ('c', 'b'))])
1926
merge_obj = self.make_merge_obj(builder, 'E-id',
1927
interesting_files=['c'])
1928
entries = list(merge_obj._entries_lca())
1929
root_id = 'a-root-id'
1930
self.assertEqual([('b-id', True,
1931
((root_id, [root_id, root_id]), root_id, root_id),
1932
((u'b', [u'c', u'b']), u'b', u'b'),
1933
((False, [False, False]), False, False)),
1936
def test_interesting_ids(self):
1937
# Two files modified, but we should filter one of them
1938
builder = self.get_builder()
1939
builder.build_snapshot('A-id', None,
1940
[('add', (u'', 'a-root-id', 'directory', None)),
1941
('add', (u'a', 'a-id', 'file', 'content\n')),
1942
('add', (u'b', 'b-id', 'file', 'content\n'))])
1943
builder.build_snapshot('B-id', ['A-id'], [])
1944
builder.build_snapshot('C-id', ['A-id'], [])
1945
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1946
[('modify', ('a-id', 'new-content\n')),
1947
('modify', ('b-id', 'new-content\n'))])
1948
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1949
merge_obj = self.make_merge_obj(builder, 'E-id',
1950
interesting_ids=['b-id'])
1951
entries = list(merge_obj._entries_lca())
1952
root_id = 'a-root-id'
1953
self.assertEqual([('b-id', True,
1954
((root_id, [root_id, root_id]), root_id, root_id),
1955
((u'b', [u'b', u'b']), u'b', u'b'),
1956
((False, [False, False]), False, False)),
1961
class TestMergerEntriesLCAOnDisk(tests.TestCaseWithTransport):
1963
def get_builder(self):
1964
builder = self.make_branch_builder('path')
1965
builder.start_series()
1966
self.addCleanup(builder.finish_series)
1969
def get_wt_from_builder(self, builder):
1970
"""Get a real WorkingTree from the builder."""
1971
the_branch = builder.get_branch()
1972
wt = the_branch.bzrdir.create_workingtree()
1973
# Note: This is a little bit ugly, but we are holding the branch
1974
# write-locked as part of the build process, and we would like to
1975
# maintain that. So we just force the WT to re-use the same
1977
wt._branch = the_branch
1979
self.addCleanup(wt.unlock)
1982
def do_merge(self, builder, other_revision_id):
1983
wt = self.get_wt_from_builder(builder)
1984
merger = _mod_merge.Merger.from_revision_ids(None,
1985
wt, other_revision_id)
1986
merger.merge_type = _mod_merge.Merge3Merger
1987
return wt, merger.do_merge()
1989
def test_simple_lca(self):
1990
builder = self.get_builder()
1991
builder.build_snapshot('A-id', None,
1992
[('add', (u'', 'a-root-id', 'directory', None)),
1993
('add', (u'a', 'a-id', 'file', 'a\nb\nc\n'))])
1994
builder.build_snapshot('C-id', ['A-id'], [])
1995
builder.build_snapshot('B-id', ['A-id'], [])
1996
builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
1997
builder.build_snapshot('D-id', ['B-id', 'C-id'],
1998
[('modify', ('a-id', 'a\nb\nc\nd\ne\nf\n'))])
1999
wt, conflicts = self.do_merge(builder, 'E-id')
2000
self.assertEqual(0, conflicts)
2001
# The merge should have simply update the contents of 'a'
2002
self.assertEqual('a\nb\nc\nd\ne\nf\n', wt.get_file_text('a-id'))
2004
def test_conflict_without_lca(self):
2005
# This test would cause a merge conflict, unless we use the lca trees
2006
# to determine the real ancestry
2009
# B C Path renamed to 'bar' in B
2013
# D E Path at 'bar' in D and E
2015
# F Path at 'baz' in F, which supersedes 'bar' and 'foo'
2016
builder = self.get_builder()
2017
builder.build_snapshot('A-id', None,
2018
[('add', (u'', 'a-root-id', 'directory', None)),
2019
('add', (u'foo', 'foo-id', 'file', 'a\nb\nc\n'))])
2020
builder.build_snapshot('C-id', ['A-id'], [])
2021
builder.build_snapshot('B-id', ['A-id'],
2022
[('rename', ('foo', 'bar'))])
2023
builder.build_snapshot('E-id', ['C-id', 'B-id'], # merge the rename
2024
[('rename', ('foo', 'bar'))])
2025
builder.build_snapshot('F-id', ['E-id'],
2026
[('rename', ('bar', 'baz'))])
2027
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
2028
wt, conflicts = self.do_merge(builder, 'F-id')
2029
self.assertEqual(0, conflicts)
2030
# The merge should simply recognize that the final rename takes
2032
self.assertEqual('baz', wt.id2path('foo-id'))
2034
def test_other_deletes_lca_renames(self):
2035
# This test would cause a merge conflict, unless we use the lca trees
2036
# to determine the real ancestry
2039
# B C Path renamed to 'bar' in B
2043
# D E Path at 'bar' in D and E
2046
builder = self.get_builder()
2047
builder.build_snapshot('A-id', None,
2048
[('add', (u'', 'a-root-id', 'directory', None)),
2049
('add', (u'foo', 'foo-id', 'file', 'a\nb\nc\n'))])
2050
builder.build_snapshot('C-id', ['A-id'], [])
2051
builder.build_snapshot('B-id', ['A-id'],
2052
[('rename', ('foo', 'bar'))])
2053
builder.build_snapshot('E-id', ['C-id', 'B-id'], # merge the rename
2054
[('rename', ('foo', 'bar'))])
2055
builder.build_snapshot('F-id', ['E-id'],
2056
[('unversion', 'foo-id')])
2057
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
2058
wt, conflicts = self.do_merge(builder, 'F-id')
2059
self.assertEqual(0, conflicts)
2060
self.assertRaises(errors.NoSuchId, wt.id2path, 'foo-id')
2062
def test_executable_changes(self):
2071
# F Executable bit changed
2072
builder = self.get_builder()
2073
builder.build_snapshot('A-id', None,
2074
[('add', (u'', 'a-root-id', 'directory', None)),
2075
('add', (u'foo', 'foo-id', 'file', 'a\nb\nc\n'))])
2076
builder.build_snapshot('C-id', ['A-id'], [])
2077
builder.build_snapshot('B-id', ['A-id'], [])
2078
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
2079
builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
2080
# Have to use a real WT, because BranchBuilder doesn't support exec bit
2081
wt = self.get_wt_from_builder(builder)
2082
tt = transform.TreeTransform(wt)
2084
tt.set_executability(True, tt.trans_id_tree_file_id('foo-id'))
2089
self.assertTrue(wt.is_executable('foo-id'))
2090
wt.commit('F-id', rev_id='F-id')
2091
# Reset to D, so that we can merge F
2092
wt.set_parent_ids(['D-id'])
2093
wt.branch.set_last_revision_info(3, 'D-id')
2095
self.assertFalse(wt.is_executable('foo-id'))
2096
conflicts = wt.merge_from_branch(wt.branch, to_revision='F-id')
2097
self.assertEqual(0, conflicts)
2098
self.assertTrue(wt.is_executable('foo-id'))
2100
def test_create_symlink(self):
2101
self.requireFeature(tests.SymlinkFeature)
2110
# F Add a symlink 'foo' => 'bar'
2111
# Have to use a real WT, because BranchBuilder and MemoryTree don't
2112
# have symlink support
2113
builder = self.get_builder()
2114
builder.build_snapshot('A-id', None,
2115
[('add', (u'', 'a-root-id', 'directory', None))])
2116
builder.build_snapshot('C-id', ['A-id'], [])
2117
builder.build_snapshot('B-id', ['A-id'], [])
2118
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
2119
builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
2120
# Have to use a real WT, because BranchBuilder doesn't support exec bit
2121
wt = self.get_wt_from_builder(builder)
2122
os.symlink('bar', 'path/foo')
2123
wt.add(['foo'], ['foo-id'])
2124
self.assertEqual('bar', wt.get_symlink_target('foo-id'))
2125
wt.commit('add symlink', rev_id='F-id')
2126
# Reset to D, so that we can merge F
2127
wt.set_parent_ids(['D-id'])
2128
wt.branch.set_last_revision_info(3, 'D-id')
2130
self.assertIs(None, wt.path2id('foo'))
2131
conflicts = wt.merge_from_branch(wt.branch, to_revision='F-id')
2132
self.assertEqual(0, conflicts)
2133
self.assertEqual('foo-id', wt.path2id('foo'))
2134
self.assertEqual('bar', wt.get_symlink_target('foo-id'))
2136
def test_both_sides_revert(self):
2137
# Both sides of a criss-cross revert the text to the lca
2138
# A base, introduces 'foo'
2140
# B C B modifies 'foo', C modifies 'foo'
2142
# D E D reverts to B, E reverts to C
2143
# This should conflict
2144
# This must be done with a real WorkingTree, because normally their
2145
# inventory contains "None" rather than a real sha1
2146
builder = self.get_builder()
2147
builder.build_snapshot('A-id', None,
2148
[('add', (u'', 'a-root-id', 'directory', None)),
2149
('add', (u'foo', 'foo-id', 'file', 'A content\n'))])
2150
builder.build_snapshot('B-id', ['A-id'],
2151
[('modify', ('foo-id', 'B content\n'))])
2152
builder.build_snapshot('C-id', ['A-id'],
2153
[('modify', ('foo-id', 'C content\n'))])
2154
builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
2155
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
2156
wt, conflicts = self.do_merge(builder, 'E-id')
2157
self.assertEqual(1, conflicts)
2158
self.assertEqualDiff('<<<<<<< TREE\n'
2162
'>>>>>>> MERGE-SOURCE\n',
2163
wt.get_file_text('foo-id'))
2165
def test_modified_symlink(self):
2166
self.requireFeature(tests.SymlinkFeature)
2167
# A Create symlink foo => bar
2169
# B C B relinks foo => baz
2173
# D E D & E have foo => baz
2175
# F F changes it to bing
2177
# Merging D & F should result in F cleanly overriding D, because D's
2178
# value actually comes from B
2180
# Have to use a real WT, because BranchBuilder and MemoryTree don't
2181
# have symlink support
2182
wt = self.make_branch_and_tree('path')
2184
self.addCleanup(wt.unlock)
2185
os.symlink('bar', 'path/foo')
2186
wt.add(['foo'], ['foo-id'])
2187
wt.commit('add symlink', rev_id='A-id')
2188
os.remove('path/foo')
2189
os.symlink('baz', 'path/foo')
2190
wt.commit('foo => baz', rev_id='B-id')
2191
wt.set_last_revision('A-id')
2192
wt.branch.set_last_revision_info(1, 'A-id')
2194
wt.commit('C', rev_id='C-id')
2195
wt.merge_from_branch(wt.branch, 'B-id')
2196
self.assertEqual('baz', wt.get_symlink_target('foo-id'))
2197
wt.commit('E merges C & B', rev_id='E-id')
2198
os.remove('path/foo')
2199
os.symlink('bing', 'path/foo')
2200
wt.commit('F foo => bing', rev_id='F-id')
2201
wt.set_last_revision('B-id')
2202
wt.branch.set_last_revision_info(2, 'B-id')
2204
wt.merge_from_branch(wt.branch, 'C-id')
2205
wt.commit('D merges B & C', rev_id='D-id')
2206
conflicts = wt.merge_from_branch(wt.branch, to_revision='F-id')
2207
self.assertEqual(0, conflicts)
2208
self.assertEqual('bing', wt.get_symlink_target('foo-id'))
2210
def test_renamed_symlink(self):
2211
self.requireFeature(tests.SymlinkFeature)
2212
# A Create symlink foo => bar
2214
# B C B renames foo => barry
2218
# D E D & E have barry
2220
# F F renames barry to blah
2222
# Merging D & F should result in F cleanly overriding D, because D's
2223
# value actually comes from B
2225
wt = self.make_branch_and_tree('path')
2227
self.addCleanup(wt.unlock)
2228
os.symlink('bar', 'path/foo')
2229
wt.add(['foo'], ['foo-id'])
2230
wt.commit('A add symlink', rev_id='A-id')
2231
wt.rename_one('foo', 'barry')
2232
wt.commit('B foo => barry', rev_id='B-id')
2233
wt.set_last_revision('A-id')
2234
wt.branch.set_last_revision_info(1, 'A-id')
2236
wt.commit('C', rev_id='C-id')
2237
wt.merge_from_branch(wt.branch, 'B-id')
2238
self.assertEqual('barry', wt.id2path('foo-id'))
2239
self.assertEqual('bar', wt.get_symlink_target('foo-id'))
2240
wt.commit('E merges C & B', rev_id='E-id')
2241
wt.rename_one('barry', 'blah')
2242
wt.commit('F barry => blah', rev_id='F-id')
2243
wt.set_last_revision('B-id')
2244
wt.branch.set_last_revision_info(2, 'B-id')
2246
wt.merge_from_branch(wt.branch, 'C-id')
2247
wt.commit('D merges B & C', rev_id='D-id')
2248
self.assertEqual('barry', wt.id2path('foo-id'))
2249
# Check the output of the Merger object directly
2250
merger = _mod_merge.Merger.from_revision_ids(None,
2252
merger.merge_type = _mod_merge.Merge3Merger
2253
merge_obj = merger.make_merger()
2254
root_id = wt.path2id('')
2255
entries = list(merge_obj._entries_lca())
2256
# No content change, just a path change
2257
self.assertEqual([('foo-id', False,
2258
((root_id, [root_id, root_id]), root_id, root_id),
2259
((u'foo', [u'barry', u'foo']), u'blah', u'barry'),
2260
((False, [False, False]), False, False)),
2262
conflicts = wt.merge_from_branch(wt.branch, to_revision='F-id')
2263
self.assertEqual(0, conflicts)
2264
self.assertEqual('blah', wt.id2path('foo-id'))
2266
def test_symlink_no_content_change(self):
2267
self.requireFeature(tests.SymlinkFeature)
2268
# A Create symlink foo => bar
2270
# B C B relinks foo => baz
2274
# D E D & E have foo => baz
2276
# F F has foo => bing
2278
# Merging E into F should not cause a conflict, because E doesn't have
2279
# a content change relative to the LCAs (it does relative to A)
2280
wt = self.make_branch_and_tree('path')
2282
self.addCleanup(wt.unlock)
2283
os.symlink('bar', 'path/foo')
2284
wt.add(['foo'], ['foo-id'])
2285
wt.commit('add symlink', rev_id='A-id')
2286
os.remove('path/foo')
2287
os.symlink('baz', 'path/foo')
2288
wt.commit('foo => baz', rev_id='B-id')
2289
wt.set_last_revision('A-id')
2290
wt.branch.set_last_revision_info(1, 'A-id')
2292
wt.commit('C', rev_id='C-id')
2293
wt.merge_from_branch(wt.branch, 'B-id')
2294
self.assertEqual('baz', wt.get_symlink_target('foo-id'))
2295
wt.commit('E merges C & B', rev_id='E-id')
2296
wt.set_last_revision('B-id')
2297
wt.branch.set_last_revision_info(2, 'B-id')
2299
wt.merge_from_branch(wt.branch, 'C-id')
2300
wt.commit('D merges B & C', rev_id='D-id')
2301
os.remove('path/foo')
2302
os.symlink('bing', 'path/foo')
2303
wt.commit('F foo => bing', rev_id='F-id')
2305
# Check the output of the Merger object directly
2306
merger = _mod_merge.Merger.from_revision_ids(None,
2308
merger.merge_type = _mod_merge.Merge3Merger
2309
merge_obj = merger.make_merger()
2310
# Nothing interesting happened in OTHER relative to BASE
2311
self.assertEqual([], list(merge_obj._entries_lca()))
2312
# Now do a real merge, just to test the rest of the stack
2313
conflicts = wt.merge_from_branch(wt.branch, to_revision='E-id')
2314
self.assertEqual(0, conflicts)
2315
self.assertEqual('bing', wt.get_symlink_target('foo-id'))
2317
def test_symlink_this_changed_kind(self):
2318
self.requireFeature(tests.SymlinkFeature)
2321
# B C B creates symlink foo => bar
2325
# D E D changes foo into a file, E has foo => bing
2327
# Mostly, this is trying to test that we don't try to os.readlink() on
2328
# a file, or when there is nothing there
2329
wt = self.make_branch_and_tree('path')
2331
self.addCleanup(wt.unlock)
2332
wt.commit('base', rev_id='A-id')
2333
os.symlink('bar', 'path/foo')
2334
wt.add(['foo'], ['foo-id'])
2335
wt.commit('add symlink foo => bar', rev_id='B-id')
2336
wt.set_last_revision('A-id')
2337
wt.branch.set_last_revision_info(1, 'A-id')
2339
wt.commit('C', rev_id='C-id')
2340
wt.merge_from_branch(wt.branch, 'B-id')
2341
self.assertEqual('bar', wt.get_symlink_target('foo-id'))
2342
os.remove('path/foo')
2343
# We have to change the link in E, or it won't try to do a comparison
2344
os.symlink('bing', 'path/foo')
2345
wt.commit('E merges C & B, overrides to bing', rev_id='E-id')
2346
wt.set_last_revision('B-id')
2347
wt.branch.set_last_revision_info(2, 'B-id')
2349
wt.merge_from_branch(wt.branch, 'C-id')
2350
os.remove('path/foo')
2351
self.build_tree_contents([('path/foo', 'file content\n')])
2352
# XXX: workaround, WT doesn't detect kind changes unless you do
2354
list(wt.iter_changes(wt.basis_tree()))
2355
wt.commit('D merges B & C, makes it a file', rev_id='D-id')
2357
merger = _mod_merge.Merger.from_revision_ids(None,
2359
merger.merge_type = _mod_merge.Merge3Merger
2360
merge_obj = merger.make_merger()
2361
entries = list(merge_obj._entries_lca())
2362
root_id = wt.path2id('')
2363
self.assertEqual([('foo-id', True,
2364
((None, [root_id, None]), root_id, root_id),
2365
((None, [u'foo', None]), u'foo', u'foo'),
2366
((None, [False, None]), False, False)),
2369
def test_symlink_all_wt(self):
2370
"""Check behavior if all trees are Working Trees."""
2371
self.requireFeature(tests.SymlinkFeature)
2372
# The big issue is that entry.symlink_target is None for WorkingTrees.
2373
# So we need to make sure we handle that case correctly.
2376
# B C B relinks foo => baz
2378
# D E D & E have foo => baz
2380
# F F changes it to bing
2381
# Merging D & F should result in F cleanly overriding D, because D's
2382
# value actually comes from B
2384
wt = self.make_branch_and_tree('path')
2386
self.addCleanup(wt.unlock)
2387
os.symlink('bar', 'path/foo')
2388
wt.add(['foo'], ['foo-id'])
2389
wt.commit('add symlink', rev_id='A-id')
2390
os.remove('path/foo')
2391
os.symlink('baz', 'path/foo')
2392
wt.commit('foo => baz', rev_id='B-id')
2393
wt.set_last_revision('A-id')
2394
wt.branch.set_last_revision_info(1, 'A-id')
2396
wt.commit('C', rev_id='C-id')
2397
wt.merge_from_branch(wt.branch, 'B-id')
2398
self.assertEqual('baz', wt.get_symlink_target('foo-id'))
2399
wt.commit('E merges C & B', rev_id='E-id')
2400
os.remove('path/foo')
2401
os.symlink('bing', 'path/foo')
2402
wt.commit('F foo => bing', rev_id='F-id')
2403
wt.set_last_revision('B-id')
2404
wt.branch.set_last_revision_info(2, 'B-id')
2406
wt.merge_from_branch(wt.branch, 'C-id')
2407
wt.commit('D merges B & C', rev_id='D-id')
2408
wt_base = wt.bzrdir.sprout('base', 'A-id').open_workingtree()
2410
self.addCleanup(wt_base.unlock)
2411
wt_lca1 = wt.bzrdir.sprout('b-tree', 'B-id').open_workingtree()
2413
self.addCleanup(wt_lca1.unlock)
2414
wt_lca2 = wt.bzrdir.sprout('c-tree', 'C-id').open_workingtree()
2416
self.addCleanup(wt_lca2.unlock)
2417
wt_other = wt.bzrdir.sprout('other', 'F-id').open_workingtree()
2418
wt_other.lock_read()
2419
self.addCleanup(wt_other.unlock)
2420
merge_obj = _mod_merge.Merge3Merger(wt, wt, wt_base,
2421
wt_other, lca_trees=[wt_lca1, wt_lca2], do_merge=False)
2422
entries = list(merge_obj._entries_lca())
2423
root_id = wt.path2id('')
2424
self.assertEqual([('foo-id', True,
2425
((root_id, [root_id, root_id]), root_id, root_id),
2426
((u'foo', [u'foo', u'foo']), u'foo', u'foo'),
2427
((False, [False, False]), False, False)),
2430
def test_other_reverted_path_to_base(self):
2433
# B C Path at 'bar' in B
2440
builder = self.get_builder()
2441
builder.build_snapshot('A-id', None,
2442
[('add', (u'', 'a-root-id', 'directory', None)),
2443
('add', (u'foo', 'foo-id', 'file', 'a\nb\nc\n'))])
2444
builder.build_snapshot('C-id', ['A-id'], [])
2445
builder.build_snapshot('B-id', ['A-id'],
2446
[('rename', ('foo', 'bar'))])
2447
builder.build_snapshot('E-id', ['C-id', 'B-id'],
2448
[('rename', ('foo', 'bar'))]) # merge the rename
2449
builder.build_snapshot('F-id', ['E-id'],
2450
[('rename', ('bar', 'foo'))]) # Rename back to BASE
2451
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
2452
wt, conflicts = self.do_merge(builder, 'F-id')
2453
self.assertEqual(0, conflicts)
2454
self.assertEqual('foo', wt.id2path('foo-id'))
2456
def test_other_reverted_content_to_base(self):
2457
builder = self.get_builder()
2458
builder.build_snapshot('A-id', None,
2459
[('add', (u'', 'a-root-id', 'directory', None)),
2460
('add', (u'foo', 'foo-id', 'file', 'base content\n'))])
2461
builder.build_snapshot('C-id', ['A-id'], [])
2462
builder.build_snapshot('B-id', ['A-id'],
2463
[('modify', ('foo-id', 'B content\n'))])
2464
builder.build_snapshot('E-id', ['C-id', 'B-id'],
2465
[('modify', ('foo-id', 'B content\n'))]) # merge the content
2466
builder.build_snapshot('F-id', ['E-id'],
2467
[('modify', ('foo-id', 'base content\n'))]) # Revert back to BASE
2468
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
2469
wt, conflicts = self.do_merge(builder, 'F-id')
2470
self.assertEqual(0, conflicts)
2471
# TODO: We need to use the per-file graph to properly select a BASE
2472
# before this will work. Or at least use the LCA trees to find
2473
# the appropriate content base. (which is B, not A).
2474
self.assertEqual('base content\n', wt.get_file_text('foo-id'))
2476
def test_other_modified_content(self):
2477
builder = self.get_builder()
2478
builder.build_snapshot('A-id', None,
2479
[('add', (u'', 'a-root-id', 'directory', None)),
2480
('add', (u'foo', 'foo-id', 'file', 'base content\n'))])
2481
builder.build_snapshot('C-id', ['A-id'], [])
2482
builder.build_snapshot('B-id', ['A-id'],
2483
[('modify', ('foo-id', 'B content\n'))])
2484
builder.build_snapshot('E-id', ['C-id', 'B-id'],
2485
[('modify', ('foo-id', 'B content\n'))]) # merge the content
2486
builder.build_snapshot('F-id', ['E-id'],
2487
[('modify', ('foo-id', 'F content\n'))]) # Override B content
2488
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
2489
wt, conflicts = self.do_merge(builder, 'F-id')
2490
self.assertEqual(0, conflicts)
2491
self.assertEqual('F content\n', wt.get_file_text('foo-id'))
2493
def test_all_wt(self):
2494
"""Check behavior if all trees are Working Trees."""
2495
# The big issue is that entry.revision is None for WorkingTrees. (as is
2496
# entry.text_sha1, etc. So we need to make sure we handle that case
2498
# A Content of 'foo', path of 'a'
2500
# B C B modifies content, C renames 'a' => 'b'
2502
# D E E updates content, renames 'b' => 'c'
2503
builder = self.get_builder()
2504
builder.build_snapshot('A-id', None,
2505
[('add', (u'', 'a-root-id', 'directory', None)),
2506
('add', (u'a', 'a-id', 'file', 'base content\n')),
2507
('add', (u'foo', 'foo-id', 'file', 'base content\n'))])
2508
builder.build_snapshot('B-id', ['A-id'],
2509
[('modify', ('foo-id', 'B content\n'))])
2510
builder.build_snapshot('C-id', ['A-id'],
2511
[('rename', ('a', 'b'))])
2512
builder.build_snapshot('E-id', ['C-id', 'B-id'],
2513
[('rename', ('b', 'c')),
2514
('modify', ('foo-id', 'E content\n'))])
2515
builder.build_snapshot('D-id', ['B-id', 'C-id'],
2516
[('rename', ('a', 'b'))]) # merged change
2517
wt_this = self.get_wt_from_builder(builder)
2518
wt_base = wt_this.bzrdir.sprout('base', 'A-id').open_workingtree()
2520
self.addCleanup(wt_base.unlock)
2521
wt_lca1 = wt_this.bzrdir.sprout('b-tree', 'B-id').open_workingtree()
2523
self.addCleanup(wt_lca1.unlock)
2524
wt_lca2 = wt_this.bzrdir.sprout('c-tree', 'C-id').open_workingtree()
2526
self.addCleanup(wt_lca2.unlock)
2527
wt_other = wt_this.bzrdir.sprout('other', 'E-id').open_workingtree()
2528
wt_other.lock_read()
2529
self.addCleanup(wt_other.unlock)
2530
merge_obj = _mod_merge.Merge3Merger(wt_this, wt_this, wt_base,
2531
wt_other, lca_trees=[wt_lca1, wt_lca2], do_merge=False)
2532
entries = list(merge_obj._entries_lca())
2533
root_id = 'a-root-id'
2534
self.assertEqual([('a-id', False,
2535
((root_id, [root_id, root_id]), root_id, root_id),
2536
((u'a', [u'a', u'b']), u'c', u'b'),
2537
((False, [False, False]), False, False)),
2539
((root_id, [root_id, root_id]), root_id, root_id),
2540
((u'foo', [u'foo', u'foo']), u'foo', u'foo'),
2541
((False, [False, False]), False, False)),
2544
def test_nested_tree_unmodified(self):
2545
# Tested with a real WT, because BranchBuilder/MemoryTree don't handle
2547
wt = self.make_branch_and_tree('tree',
2548
format='dirstate-with-subtree')
2550
self.addCleanup(wt.unlock)
2551
sub_tree = self.make_branch_and_tree('tree/sub-tree',
2552
format='dirstate-with-subtree')
2553
wt.set_root_id('a-root-id')
2554
sub_tree.set_root_id('sub-tree-root')
2555
self.build_tree_contents([('tree/sub-tree/file', 'text1')])
2556
sub_tree.add('file')
2557
sub_tree.commit('foo', rev_id='sub-A-id')
2558
wt.add_reference(sub_tree)
2559
wt.commit('set text to 1', rev_id='A-id', recursive=None)
2560
# Now create a criss-cross merge in the parent, without modifying the
2562
wt.commit('B', rev_id='B-id', recursive=None)
2563
wt.set_last_revision('A-id')
2564
wt.branch.set_last_revision_info(1, 'A-id')
2565
wt.commit('C', rev_id='C-id', recursive=None)
2566
wt.merge_from_branch(wt.branch, to_revision='B-id')
2567
wt.commit('E', rev_id='E-id', recursive=None)
2568
wt.set_parent_ids(['B-id', 'C-id'])
2569
wt.branch.set_last_revision_info(2, 'B-id')
2570
wt.commit('D', rev_id='D-id', recursive=None)
2572
merger = _mod_merge.Merger.from_revision_ids(None,
2574
merger.merge_type = _mod_merge.Merge3Merger
2575
merge_obj = merger.make_merger()
2576
entries = list(merge_obj._entries_lca())
2577
self.assertEqual([], entries)
2579
def test_nested_tree_subtree_modified(self):
2580
# Tested with a real WT, because BranchBuilder/MemoryTree don't handle
2582
wt = self.make_branch_and_tree('tree',
2583
format='dirstate-with-subtree')
2585
self.addCleanup(wt.unlock)
2586
sub_tree = self.make_branch_and_tree('tree/sub',
2587
format='dirstate-with-subtree')
2588
wt.set_root_id('a-root-id')
2589
sub_tree.set_root_id('sub-tree-root')
2590
self.build_tree_contents([('tree/sub/file', 'text1')])
2591
sub_tree.add('file')
2592
sub_tree.commit('foo', rev_id='sub-A-id')
2593
wt.add_reference(sub_tree)
2594
wt.commit('set text to 1', rev_id='A-id', recursive=None)
2595
# Now create a criss-cross merge in the parent, without modifying the
2597
wt.commit('B', rev_id='B-id', recursive=None)
2598
wt.set_last_revision('A-id')
2599
wt.branch.set_last_revision_info(1, 'A-id')
2600
wt.commit('C', rev_id='C-id', recursive=None)
2601
wt.merge_from_branch(wt.branch, to_revision='B-id')
2602
self.build_tree_contents([('tree/sub/file', 'text2')])
2603
sub_tree.commit('modify contents', rev_id='sub-B-id')
2604
wt.commit('E', rev_id='E-id', recursive=None)
2605
wt.set_parent_ids(['B-id', 'C-id'])
2606
wt.branch.set_last_revision_info(2, 'B-id')
2607
wt.commit('D', rev_id='D-id', recursive=None)
2609
merger = _mod_merge.Merger.from_revision_ids(None,
2611
merger.merge_type = _mod_merge.Merge3Merger
2612
merge_obj = merger.make_merger()
2613
entries = list(merge_obj._entries_lca())
2614
# Nothing interesting about this sub-tree, because content changes are
2615
# computed at a higher level
2616
self.assertEqual([], entries)
2618
def test_nested_tree_subtree_renamed(self):
2619
# Tested with a real WT, because BranchBuilder/MemoryTree don't handle
2621
wt = self.make_branch_and_tree('tree',
2622
format='dirstate-with-subtree')
2624
self.addCleanup(wt.unlock)
2625
sub_tree = self.make_branch_and_tree('tree/sub',
2626
format='dirstate-with-subtree')
2627
wt.set_root_id('a-root-id')
2628
sub_tree.set_root_id('sub-tree-root')
2629
self.build_tree_contents([('tree/sub/file', 'text1')])
2630
sub_tree.add('file')
2631
sub_tree.commit('foo', rev_id='sub-A-id')
2632
wt.add_reference(sub_tree)
2633
wt.commit('set text to 1', rev_id='A-id', recursive=None)
2634
# Now create a criss-cross merge in the parent, without modifying the
2636
wt.commit('B', rev_id='B-id', recursive=None)
2637
wt.set_last_revision('A-id')
2638
wt.branch.set_last_revision_info(1, 'A-id')
2639
wt.commit('C', rev_id='C-id', recursive=None)
2640
wt.merge_from_branch(wt.branch, to_revision='B-id')
2641
wt.rename_one('sub', 'alt_sub')
2642
wt.commit('E', rev_id='E-id', recursive=None)
2643
wt.set_last_revision('B-id')
2645
wt.set_parent_ids(['B-id', 'C-id'])
2646
wt.branch.set_last_revision_info(2, 'B-id')
2647
wt.commit('D', rev_id='D-id', recursive=None)
2649
merger = _mod_merge.Merger.from_revision_ids(None,
2651
merger.merge_type = _mod_merge.Merge3Merger
2652
merge_obj = merger.make_merger()
2653
entries = list(merge_obj._entries_lca())
2654
root_id = 'a-root-id'
2655
self.assertEqual([('sub-tree-root', False,
2656
((root_id, [root_id, root_id]), root_id, root_id),
2657
((u'sub', [u'sub', u'sub']), u'alt_sub', u'sub'),
2658
((False, [False, False]), False, False)),
2661
def test_nested_tree_subtree_renamed_and_modified(self):
2662
# Tested with a real WT, because BranchBuilder/MemoryTree don't handle
2664
wt = self.make_branch_and_tree('tree',
2665
format='dirstate-with-subtree')
2667
self.addCleanup(wt.unlock)
2668
sub_tree = self.make_branch_and_tree('tree/sub',
2669
format='dirstate-with-subtree')
2670
wt.set_root_id('a-root-id')
2671
sub_tree.set_root_id('sub-tree-root')
2672
self.build_tree_contents([('tree/sub/file', 'text1')])
2673
sub_tree.add('file')
2674
sub_tree.commit('foo', rev_id='sub-A-id')
2675
wt.add_reference(sub_tree)
2676
wt.commit('set text to 1', rev_id='A-id', recursive=None)
2677
# Now create a criss-cross merge in the parent, without modifying the
2679
wt.commit('B', rev_id='B-id', recursive=None)
2680
wt.set_last_revision('A-id')
2681
wt.branch.set_last_revision_info(1, 'A-id')
2682
wt.commit('C', rev_id='C-id', recursive=None)
2683
wt.merge_from_branch(wt.branch, to_revision='B-id')
2684
self.build_tree_contents([('tree/sub/file', 'text2')])
2685
sub_tree.commit('modify contents', rev_id='sub-B-id')
2686
wt.rename_one('sub', 'alt_sub')
2687
wt.commit('E', rev_id='E-id', recursive=None)
2688
wt.set_last_revision('B-id')
2690
wt.set_parent_ids(['B-id', 'C-id'])
2691
wt.branch.set_last_revision_info(2, 'B-id')
2692
wt.commit('D', rev_id='D-id', recursive=None)
2694
merger = _mod_merge.Merger.from_revision_ids(None,
2696
merger.merge_type = _mod_merge.Merge3Merger
2697
merge_obj = merger.make_merger()
2698
entries = list(merge_obj._entries_lca())
2699
root_id = 'a-root-id'
2700
self.assertEqual([('sub-tree-root', False,
2701
((root_id, [root_id, root_id]), root_id, root_id),
2702
((u'sub', [u'sub', u'sub']), u'alt_sub', u'sub'),
2703
((False, [False, False]), False, False)),
2707
class TestLCAMultiWay(tests.TestCase):
2709
def assertLCAMultiWay(self, expected, base, lcas, other, this,
2710
allow_overriding_lca=True):
2711
self.assertEqual(expected, _mod_merge.Merge3Merger._lca_multi_way(
2712
(base, lcas), other, this,
2713
allow_overriding_lca=allow_overriding_lca))
2715
def test_other_equal_equal_lcas(self):
2716
"""Test when OTHER=LCA and all LCAs are identical."""
2717
self.assertLCAMultiWay('this',
2718
'bval', ['bval', 'bval'], 'bval', 'bval')
2719
self.assertLCAMultiWay('this',
2720
'bval', ['lcaval', 'lcaval'], 'lcaval', 'bval')
2721
self.assertLCAMultiWay('this',
2722
'bval', ['lcaval', 'lcaval', 'lcaval'], 'lcaval', 'bval')
2723
self.assertLCAMultiWay('this',
2724
'bval', ['lcaval', 'lcaval', 'lcaval'], 'lcaval', 'tval')
2725
self.assertLCAMultiWay('this',
2726
'bval', ['lcaval', 'lcaval', 'lcaval'], 'lcaval', None)
2728
def test_other_equal_this(self):
2729
"""Test when other and this are identical."""
2730
self.assertLCAMultiWay('this',
2731
'bval', ['bval', 'bval'], 'oval', 'oval')
2732
self.assertLCAMultiWay('this',
2733
'bval', ['lcaval', 'lcaval'], 'oval', 'oval')
2734
self.assertLCAMultiWay('this',
2735
'bval', ['cval', 'dval'], 'oval', 'oval')
2736
self.assertLCAMultiWay('this',
2737
'bval', [None, 'lcaval'], 'oval', 'oval')
2738
self.assertLCAMultiWay('this',
2739
None, [None, 'lcaval'], 'oval', 'oval')
2740
self.assertLCAMultiWay('this',
2741
None, ['lcaval', 'lcaval'], 'oval', 'oval')
2742
self.assertLCAMultiWay('this',
2743
None, ['cval', 'dval'], 'oval', 'oval')
2744
self.assertLCAMultiWay('this',
2745
None, ['cval', 'dval'], None, None)
2746
self.assertLCAMultiWay('this',
2747
None, ['cval', 'dval', 'eval', 'fval'], 'oval', 'oval')
2749
def test_no_lcas(self):
2750
self.assertLCAMultiWay('this',
2751
'bval', [], 'bval', 'tval')
2752
self.assertLCAMultiWay('other',
2753
'bval', [], 'oval', 'bval')
2754
self.assertLCAMultiWay('conflict',
2755
'bval', [], 'oval', 'tval')
2756
self.assertLCAMultiWay('this',
2757
'bval', [], 'oval', 'oval')
2759
def test_lca_supersedes_other_lca(self):
2760
"""If one lca == base, the other lca takes precedence"""
2761
self.assertLCAMultiWay('this',
2762
'bval', ['bval', 'lcaval'], 'lcaval', 'tval')
2763
self.assertLCAMultiWay('this',
2764
'bval', ['bval', 'lcaval'], 'lcaval', 'bval')
2765
# This is actually considered a 'revert' because the 'lcaval' in LCAS
2766
# supersedes the BASE val (in the other LCA) but then OTHER reverts it
2768
self.assertLCAMultiWay('other',
2769
'bval', ['bval', 'lcaval'], 'bval', 'lcaval')
2770
self.assertLCAMultiWay('conflict',
2771
'bval', ['bval', 'lcaval'], 'bval', 'tval')
2773
def test_other_and_this_pick_different_lca(self):
2774
# OTHER and THIS resolve the lca conflict in different ways
2775
self.assertLCAMultiWay('conflict',
2776
'bval', ['lca1val', 'lca2val'], 'lca1val', 'lca2val')
2777
self.assertLCAMultiWay('conflict',
2778
'bval', ['lca1val', 'lca2val', 'lca3val'], 'lca1val', 'lca2val')
2779
self.assertLCAMultiWay('conflict',
2780
'bval', ['lca1val', 'lca2val', 'bval'], 'lca1val', 'lca2val')
2782
def test_other_in_lca(self):
2783
# OTHER takes a value of one of the LCAs, THIS takes a new value, which
2784
# theoretically supersedes both LCA values and 'wins'
2785
self.assertLCAMultiWay('this',
2786
'bval', ['lca1val', 'lca2val'], 'lca1val', 'newval')
2787
self.assertLCAMultiWay('this',
2788
'bval', ['lca1val', 'lca2val', 'lca3val'], 'lca1val', 'newval')
2789
self.assertLCAMultiWay('conflict',
2790
'bval', ['lca1val', 'lca2val'], 'lca1val', 'newval',
2791
allow_overriding_lca=False)
2792
self.assertLCAMultiWay('conflict',
2793
'bval', ['lca1val', 'lca2val', 'lca3val'], 'lca1val', 'newval',
2794
allow_overriding_lca=False)
2795
# THIS reverted back to BASE, but that is an explicit supersede of all
2797
self.assertLCAMultiWay('this',
2798
'bval', ['lca1val', 'lca2val', 'lca3val'], 'lca1val', 'bval')
2799
self.assertLCAMultiWay('this',
2800
'bval', ['lca1val', 'lca2val', 'bval'], 'lca1val', 'bval')
2801
self.assertLCAMultiWay('conflict',
2802
'bval', ['lca1val', 'lca2val', 'lca3val'], 'lca1val', 'bval',
2803
allow_overriding_lca=False)
2804
self.assertLCAMultiWay('conflict',
2805
'bval', ['lca1val', 'lca2val', 'bval'], 'lca1val', 'bval',
2806
allow_overriding_lca=False)
2808
def test_this_in_lca(self):
2809
# THIS takes a value of one of the LCAs, OTHER takes a new value, which
2810
# theoretically supersedes both LCA values and 'wins'
2811
self.assertLCAMultiWay('other',
2812
'bval', ['lca1val', 'lca2val'], 'oval', 'lca1val')
2813
self.assertLCAMultiWay('other',
2814
'bval', ['lca1val', 'lca2val'], 'oval', 'lca2val')
2815
self.assertLCAMultiWay('conflict',
2816
'bval', ['lca1val', 'lca2val'], 'oval', 'lca1val',
2817
allow_overriding_lca=False)
2818
self.assertLCAMultiWay('conflict',
2819
'bval', ['lca1val', 'lca2val'], 'oval', 'lca2val',
2820
allow_overriding_lca=False)
2821
# OTHER reverted back to BASE, but that is an explicit supersede of all
2823
self.assertLCAMultiWay('other',
2824
'bval', ['lca1val', 'lca2val', 'lca3val'], 'bval', 'lca3val')
2825
self.assertLCAMultiWay('conflict',
2826
'bval', ['lca1val', 'lca2val', 'lca3val'], 'bval', 'lca3val',
2827
allow_overriding_lca=False)
2829
def test_all_differ(self):
2830
self.assertLCAMultiWay('conflict',
2831
'bval', ['lca1val', 'lca2val'], 'oval', 'tval')
2832
self.assertLCAMultiWay('conflict',
2833
'bval', ['lca1val', 'lca2val', 'lca2val'], 'oval', 'tval')
2834
self.assertLCAMultiWay('conflict',
2835
'bval', ['lca1val', 'lca2val', 'lca3val'], 'oval', 'tval')
2838
class TestConfigurableFileMerger(tests.TestCaseWithTransport):
2841
super(TestConfigurableFileMerger, self).setUp()
2844
def get_merger_factory(self):
2845
# Allows the inner methods to access the test attributes
2848
class FooMerger(_mod_merge.ConfigurableFileMerger):
2850
default_files = ['bar']
2852
def merge_text(self, params):
2853
test.calls.append('merge_text')
2854
return ('not_applicable', None)
2856
def factory(merger):
2857
result = FooMerger(merger)
2858
# Make sure we start with a clean slate
2859
self.assertEqual(None, result.affected_files)
2860
# Track the original merger
2861
self.merger = result
2866
def _install_hook(self, factory):
2867
_mod_merge.Merger.hooks.install_named_hook('merge_file_content',
2868
factory, 'test factory')
2870
def make_builder(self):
2871
builder = test_merge_core.MergeBuilder(self.test_base_dir)
2872
self.addCleanup(builder.cleanup)
2875
def make_text_conflict(self, file_name='bar'):
2876
factory = self.get_merger_factory()
2877
self._install_hook(factory)
2878
builder = self.make_builder()
2879
builder.add_file('bar-id', builder.tree_root, file_name, 'text1', True)
2880
builder.change_contents('bar-id', other='text4', this='text3')
2883
def make_kind_change(self):
2884
factory = self.get_merger_factory()
2885
self._install_hook(factory)
2886
builder = self.make_builder()
2887
builder.add_file('bar-id', builder.tree_root, 'bar', 'text1', True,
2889
builder.add_dir('bar-dir', builder.tree_root, 'bar-id',
2890
base=False, other=False)
2893
def test_uses_this_branch(self):
2894
builder = self.make_text_conflict()
2895
tt = builder.make_preview_transform()
2896
self.addCleanup(tt.finalize)
2898
def test_affected_files_cached(self):
2899
"""Ensures that the config variable is cached"""
2900
builder = self.make_text_conflict()
2901
conflicts = builder.merge()
2902
# The hook should set the variable
2903
self.assertEqual(['bar'], self.merger.affected_files)
2904
self.assertEqual(1, len(conflicts))
2906
def test_hook_called_for_text_conflicts(self):
2907
builder = self.make_text_conflict()
2908
conflicts = builder.merge()
2909
# The hook should call the merge_text() method
2910
self.assertEqual(['merge_text'], self.calls)
2912
def test_hook_not_called_for_kind_change(self):
2913
builder = self.make_kind_change()
2914
conflicts = builder.merge()
2915
# The hook should not call the merge_text() method
2916
self.assertEqual([], self.calls)
2918
def test_hook_not_called_for_other_files(self):
2919
builder = self.make_text_conflict('foobar')
2920
conflicts = builder.merge()
2921
# The hook should not call the merge_text() method
2922
self.assertEqual([], self.calls)
2925
class TestMergeIntoBase(tests.TestCaseWithTransport):
2927
def setup_simple_branch(self, relpath, shape=None, root_id=None):
2928
"""One commit, containing tree specified by optional shape.
2930
Default is empty tree (just root entry).
2933
root_id = '%s-root-id' % (relpath,)
2934
wt = self.make_branch_and_tree(relpath)
2935
wt.set_root_id(root_id)
2936
if shape is not None:
2937
adjusted_shape = [relpath + '/' + elem for elem in shape]
2938
self.build_tree(adjusted_shape)
2939
ids = ['%s-%s-id' % (relpath, basename(elem.rstrip('/')))
2941
wt.add(shape, ids=ids)
2942
rev_id = 'r1-%s' % (relpath,)
2943
wt.commit("Initial commit of %s" % (relpath,), rev_id=rev_id)
2944
self.assertEqual(root_id, wt.path2id(''))
2947
def setup_two_branches(self, custom_root_ids=True):
2948
"""Setup 2 branches, one will be a library, the other a project."""
2952
root_id = inventory.ROOT_ID
2953
project_wt = self.setup_simple_branch(
2954
'project', ['README', 'dir/', 'dir/file.c'],
2956
lib_wt = self.setup_simple_branch(
2957
'lib1', ['README', 'Makefile', 'foo.c'], root_id)
2959
return project_wt, lib_wt
2961
def do_merge_into(self, location, merge_as):
2962
"""Helper for using MergeIntoMerger.
2964
:param location: location of directory to merge from, either the
2965
location of a branch or of a path inside a branch.
2966
:param merge_as: the path in a tree to add the new directory as.
2967
:returns: the conflicts from 'do_merge'.
2969
operation = cleanup.OperationWithCleanups(self._merge_into)
2970
return operation.run(location, merge_as)
2972
def _merge_into(self, op, location, merge_as):
2973
# Open and lock the various tree and branch objects
2974
wt, subdir_relpath = WorkingTree.open_containing(merge_as)
2975
op.add_cleanup(wt.lock_write().unlock)
2976
branch_to_merge, subdir_to_merge = _mod_branch.Branch.open_containing(
2978
op.add_cleanup(branch_to_merge.lock_read().unlock)
2979
other_tree = branch_to_merge.basis_tree()
2980
op.add_cleanup(other_tree.lock_read().unlock)
2982
merger = _mod_merge.MergeIntoMerger(this_tree=wt, other_tree=other_tree,
2983
other_branch=branch_to_merge, target_subdir=subdir_relpath,
2984
source_subpath=subdir_to_merge)
2985
merger.set_base_revision(_mod_revision.NULL_REVISION, branch_to_merge)
2986
conflicts = merger.do_merge()
2987
merger.set_pending()
2990
def assertTreeEntriesEqual(self, expected_entries, tree):
2991
"""Assert that 'tree' contains the expected inventory entries.
2993
:param expected_entries: sequence of (path, file-id) pairs.
2995
files = [(path, ie.file_id) for path, ie in tree.iter_entries_by_dir()]
2996
self.assertEqual(expected_entries, files)
2999
class TestMergeInto(TestMergeIntoBase):
3001
def test_newdir_with_unique_roots(self):
3002
"""Merge a branch with a unique root into a new directory."""
3003
project_wt, lib_wt = self.setup_two_branches()
3004
self.do_merge_into('lib1', 'project/lib1')
3005
project_wt.lock_read()
3006
self.addCleanup(project_wt.unlock)
3007
# The r1-lib1 revision should be merged into this one
3008
self.assertEqual(['r1-project', 'r1-lib1'], project_wt.get_parent_ids())
3009
self.assertTreeEntriesEqual(
3010
[('', 'project-root-id'),
3011
('README', 'project-README-id'),
3012
('dir', 'project-dir-id'),
3013
('lib1', 'lib1-root-id'),
3014
('dir/file.c', 'project-file.c-id'),
3015
('lib1/Makefile', 'lib1-Makefile-id'),
3016
('lib1/README', 'lib1-README-id'),
3017
('lib1/foo.c', 'lib1-foo.c-id'),
3020
def test_subdir(self):
3021
"""Merge a branch into a subdirectory of an existing directory."""
3022
project_wt, lib_wt = self.setup_two_branches()
3023
self.do_merge_into('lib1', 'project/dir/lib1')
3024
project_wt.lock_read()
3025
self.addCleanup(project_wt.unlock)
3026
# The r1-lib1 revision should be merged into this one
3027
self.assertEqual(['r1-project', 'r1-lib1'], project_wt.get_parent_ids())
3028
self.assertTreeEntriesEqual(
3029
[('', 'project-root-id'),
3030
('README', 'project-README-id'),
3031
('dir', 'project-dir-id'),
3032
('dir/file.c', 'project-file.c-id'),
3033
('dir/lib1', 'lib1-root-id'),
3034
('dir/lib1/Makefile', 'lib1-Makefile-id'),
3035
('dir/lib1/README', 'lib1-README-id'),
3036
('dir/lib1/foo.c', 'lib1-foo.c-id'),
3039
def test_newdir_with_repeat_roots(self):
3040
"""If the file-id of the dir to be merged already exists a new ID will
3041
be allocated to let the merge happen.
3043
project_wt, lib_wt = self.setup_two_branches(custom_root_ids=False)
3044
root_id = project_wt.path2id('')
3045
self.do_merge_into('lib1', 'project/lib1')
3046
project_wt.lock_read()
3047
self.addCleanup(project_wt.unlock)
3048
# The r1-lib1 revision should be merged into this one
3049
self.assertEqual(['r1-project', 'r1-lib1'], project_wt.get_parent_ids())
3050
new_lib1_id = project_wt.path2id('lib1')
3051
self.assertNotEqual(None, new_lib1_id)
3052
self.assertTreeEntriesEqual(
3054
('README', 'project-README-id'),
3055
('dir', 'project-dir-id'),
3056
('lib1', new_lib1_id),
3057
('dir/file.c', 'project-file.c-id'),
3058
('lib1/Makefile', 'lib1-Makefile-id'),
3059
('lib1/README', 'lib1-README-id'),
3060
('lib1/foo.c', 'lib1-foo.c-id'),
3063
def test_name_conflict(self):
3064
"""When the target directory name already exists a conflict is
3065
generated and the original directory is renamed to foo.moved.
3067
dest_wt = self.setup_simple_branch('dest', ['dir/', 'dir/file.txt'])
3068
src_wt = self.setup_simple_branch('src', ['README'])
3069
conflicts = self.do_merge_into('src', 'dest/dir')
3070
self.assertEqual(1, conflicts)
3072
self.addCleanup(dest_wt.unlock)
3073
# The r1-lib1 revision should be merged into this one
3074
self.assertEqual(['r1-dest', 'r1-src'], dest_wt.get_parent_ids())
3075
self.assertTreeEntriesEqual(
3076
[('', 'dest-root-id'),
3077
('dir', 'src-root-id'),
3078
('dir.moved', 'dest-dir-id'),
3079
('dir/README', 'src-README-id'),
3080
('dir.moved/file.txt', 'dest-file.txt-id'),
3083
def test_file_id_conflict(self):
3084
"""A conflict is generated if the merge-into adds a file (or other
3085
inventory entry) with a file-id that already exists in the target tree.
3087
dest_wt = self.setup_simple_branch('dest', ['file.txt'])
3088
# Make a second tree with a file-id that will clash with file.txt in
3090
src_wt = self.make_branch_and_tree('src')
3091
self.build_tree(['src/README'])
3092
src_wt.add(['README'], ids=['dest-file.txt-id'])
3093
src_wt.commit("Rev 1 of src.", rev_id='r1-src')
3094
conflicts = self.do_merge_into('src', 'dest/dir')
3095
# This is an edge case that shouldn't happen to users very often. So
3096
# we don't care really about the exact presentation of the conflict,
3097
# just that there is one.
3098
self.assertEqual(1, conflicts)
3100
def test_only_subdir(self):
3101
"""When the location points to just part of a tree, merge just that
3104
dest_wt = self.setup_simple_branch('dest')
3105
src_wt = self.setup_simple_branch(
3106
'src', ['hello.txt', 'dir/', 'dir/foo.c'])
3107
conflicts = self.do_merge_into('src/dir', 'dest/dir')
3109
self.addCleanup(dest_wt.unlock)
3110
# The r1-lib1 revision should NOT be merged into this one (this is a
3112
self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3113
self.assertTreeEntriesEqual(
3114
[('', 'dest-root-id'),
3115
('dir', 'src-dir-id'),
3116
('dir/foo.c', 'src-foo.c-id'),
3119
def test_only_file(self):
3120
"""An edge case: merge just one file, not a whole dir."""
3121
dest_wt = self.setup_simple_branch('dest')
3122
two_file_wt = self.setup_simple_branch(
3123
'two-file', ['file1.txt', 'file2.txt'])
3124
conflicts = self.do_merge_into('two-file/file1.txt', 'dest/file1.txt')
3126
self.addCleanup(dest_wt.unlock)
3127
# The r1-lib1 revision should NOT be merged into this one
3128
self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3129
self.assertTreeEntriesEqual(
3130
[('', 'dest-root-id'), ('file1.txt', 'two-file-file1.txt-id')],
3133
def test_no_such_source_path(self):
3134
"""PathNotInTree is raised if the specified path in the source tree
3137
dest_wt = self.setup_simple_branch('dest')
3138
two_file_wt = self.setup_simple_branch('src', ['dir/'])
3139
self.assertRaises(_mod_merge.PathNotInTree, self.do_merge_into,
3140
'src/no-such-dir', 'dest/foo')
3142
self.addCleanup(dest_wt.unlock)
3143
# The dest tree is unmodified.
3144
self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3145
self.assertTreeEntriesEqual([('', 'dest-root-id')], dest_wt)
3147
def test_no_such_target_path(self):
3148
"""PathNotInTree is also raised if the specified path in the target
3149
tree does not exist.
3151
dest_wt = self.setup_simple_branch('dest')
3152
two_file_wt = self.setup_simple_branch('src', ['file.txt'])
3153
self.assertRaises(_mod_merge.PathNotInTree, self.do_merge_into,
3154
'src', 'dest/no-such-dir/foo')
3156
self.addCleanup(dest_wt.unlock)
3157
# The dest tree is unmodified.
3158
self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3159
self.assertTreeEntriesEqual([('', 'dest-root-id')], dest_wt)
634
class TestMergeImplementation(object):
636
def do_merge(self, target_tree, source_tree, **kwargs):
637
merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
638
target_tree, source_tree.last_revision(),
639
other_branch=source_tree.branch)
640
merger.merge_type=self.merge_type
641
for name, value in kwargs.items():
642
setattr(merger, name, value)
645
def test_merge_specific_file(self):
646
this_tree = self.make_branch_and_tree('this')
647
this_tree.lock_write()
648
self.addCleanup(this_tree.unlock)
649
self.build_tree_contents([
650
('this/file1', 'a\nb\n'),
651
('this/file2', 'a\nb\n')
653
this_tree.add(['file1', 'file2'])
654
this_tree.commit('Added files')
655
other_tree = this_tree.bzrdir.sprout('other').open_workingtree()
656
self.build_tree_contents([
657
('other/file1', 'a\nb\nc\n'),
658
('other/file2', 'a\nb\nc\n')
660
other_tree.commit('modified both')
661
self.build_tree_contents([
662
('this/file1', 'd\na\nb\n'),
663
('this/file2', 'd\na\nb\n')
665
this_tree.commit('modified both')
666
self.do_merge(this_tree, other_tree, interesting_files=['file1'])
667
self.assertFileEqual('d\na\nb\nc\n', 'this/file1')
668
self.assertFileEqual('d\na\nb\n', 'this/file2')
671
class TestMerge3Merge(TestCaseWithTransport, TestMergeImplementation):
673
merge_type = _mod_merge.Merge3Merger
676
class TestWeaveMerge(TestCaseWithTransport, TestMergeImplementation):
678
merge_type = _mod_merge.WeaveMerger
681
class TestLCAMerge(TestCaseWithTransport, TestMergeImplementation):
683
merge_type = _mod_merge.LCAMerger