1172
630
('conflicted-b', 'e\n'),
1175
def test_plan_lca_merge_with_null(self):
1176
self.add_version(('root', 'A'), [], 'ab')
1177
self.add_version(('root', 'B'), [], 'bc')
1178
plan = self.plan_merge_vf.plan_lca_merge('A', 'B')
1179
self.assertEqual([('new-a', 'a\n'),
1180
('unchanged', 'b\n'),
1184
def test_plan_merge_with_delete_and_change(self):
1185
self.add_rev('root', 'C', [], 'a')
1186
self.add_rev('root', 'A', ['C'], 'b')
1187
self.add_rev('root', 'B', ['C'], '')
1188
plan = self.plan_merge_vf.plan_merge('A', 'B')
1189
self.assertEqual([('killed-both', 'a\n'),
1193
def test_plan_merge_with_move_and_change(self):
1194
self.add_rev('root', 'C', [], 'abcd')
1195
self.add_rev('root', 'A', ['C'], 'acbd')
1196
self.add_rev('root', 'B', ['C'], 'aBcd')
1197
plan = self.plan_merge_vf.plan_merge('A', 'B')
1198
self.assertEqual([('unchanged', 'a\n'),
1200
('killed-b', 'b\n'),
1202
('killed-a', 'c\n'),
1203
('unchanged', 'd\n'),
1207
class LoggingMerger(object):
1208
# These seem to be the required attributes
1209
requires_base = False
1210
supports_reprocess = False
1211
supports_show_base = False
1212
supports_cherrypick = False
1213
# We intentionally do not define supports_lca_trees
1215
def __init__(self, *args, **kwargs):
1217
self.kwargs = kwargs
1220
class TestMergerBase(TestCaseWithMemoryTransport):
1221
"""Common functionality for Merger tests that don't write to disk."""
1223
def get_builder(self):
1224
builder = self.make_branch_builder('path')
1225
builder.start_series()
1226
self.addCleanup(builder.finish_series)
1229
def setup_simple_graph(self):
1230
"""Create a simple 3-node graph.
1232
:return: A BranchBuilder
1239
builder = self.get_builder()
1240
builder.build_snapshot('A-id', None,
1241
[('add', ('', None, 'directory', None))])
1242
builder.build_snapshot('C-id', ['A-id'], [])
1243
builder.build_snapshot('B-id', ['A-id'], [])
1246
def setup_criss_cross_graph(self):
1247
"""Create a 5-node graph with a criss-cross.
1249
:return: A BranchBuilder
1256
builder = self.setup_simple_graph()
1257
builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
1258
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1261
def make_Merger(self, builder, other_revision_id,
1262
interesting_files=None, interesting_ids=None):
1263
"""Make a Merger object from a branch builder"""
1264
mem_tree = memorytree.MemoryTree.create_on_branch(builder.get_branch())
1265
mem_tree.lock_write()
1266
self.addCleanup(mem_tree.unlock)
1267
merger = _mod_merge.Merger.from_revision_ids(None,
1268
mem_tree, other_revision_id)
1269
merger.set_interesting_files(interesting_files)
1270
# It seems there is no matching function for set_interesting_ids
1271
merger.interesting_ids = interesting_ids
1272
merger.merge_type = _mod_merge.Merge3Merger
1276
class TestMergerInMemory(TestMergerBase):
1278
def test_cache_trees_with_revision_ids_None(self):
1279
merger = self.make_Merger(self.setup_simple_graph(), 'C-id')
1280
original_cache = dict(merger._cached_trees)
1281
merger.cache_trees_with_revision_ids([None])
1282
self.assertEqual(original_cache, merger._cached_trees)
1284
def test_cache_trees_with_revision_ids_no_revision_id(self):
1285
merger = self.make_Merger(self.setup_simple_graph(), 'C-id')
1286
original_cache = dict(merger._cached_trees)
1287
tree = self.make_branch_and_memory_tree('tree')
1288
merger.cache_trees_with_revision_ids([tree])
1289
self.assertEqual(original_cache, merger._cached_trees)
1291
def test_cache_trees_with_revision_ids_having_revision_id(self):
1292
merger = self.make_Merger(self.setup_simple_graph(), 'C-id')
1293
original_cache = dict(merger._cached_trees)
1294
tree = merger.this_branch.repository.revision_tree('B-id')
1295
original_cache['B-id'] = tree
1296
merger.cache_trees_with_revision_ids([tree])
1297
self.assertEqual(original_cache, merger._cached_trees)
1299
def test_find_base(self):
1300
merger = self.make_Merger(self.setup_simple_graph(), 'C-id')
1301
self.assertEqual('A-id', merger.base_rev_id)
1302
self.assertFalse(merger._is_criss_cross)
1303
self.assertIs(None, merger._lca_trees)
1305
def test_find_base_criss_cross(self):
1306
builder = self.setup_criss_cross_graph()
1307
merger = self.make_Merger(builder, 'E-id')
1308
self.assertEqual('A-id', merger.base_rev_id)
1309
self.assertTrue(merger._is_criss_cross)
1310
self.assertEqual(['B-id', 'C-id'], [t.get_revision_id()
1311
for t in merger._lca_trees])
1312
# If we swap the order, we should get a different lca order
1313
builder.build_snapshot('F-id', ['E-id'], [])
1314
merger = self.make_Merger(builder, 'D-id')
1315
self.assertEqual(['C-id', 'B-id'], [t.get_revision_id()
1316
for t in merger._lca_trees])
1318
def test_find_base_triple_criss_cross(self):
1321
# B C F # F is merged into both branches
1328
builder = self.setup_criss_cross_graph()
1329
builder.build_snapshot('F-id', ['A-id'], [])
1330
builder.build_snapshot('H-id', ['E-id', 'F-id'], [])
1331
builder.build_snapshot('G-id', ['D-id', 'F-id'], [])
1332
merger = self.make_Merger(builder, 'H-id')
1333
self.assertEqual(['B-id', 'C-id', 'F-id'],
1334
[t.get_revision_id() for t in merger._lca_trees])
1336
def test_find_base_new_root_criss_cross(self):
1343
builder = self.get_builder()
1344
builder.build_snapshot('A-id', None,
1345
[('add', ('', None, 'directory', None))])
1346
builder.build_snapshot('B-id', [],
1347
[('add', ('', None, 'directory', None))])
1348
builder.build_snapshot('D-id', ['A-id', 'B-id'], [])
1349
builder.build_snapshot('C-id', ['A-id', 'B-id'], [])
1350
merger = self.make_Merger(builder, 'D-id')
1351
self.assertEqual('A-id', merger.base_rev_id)
1352
self.assertTrue(merger._is_criss_cross)
1353
self.assertEqual(['A-id', 'B-id'], [t.get_revision_id()
1354
for t in merger._lca_trees])
1356
def test_no_criss_cross_passed_to_merge_type(self):
1357
class LCATreesMerger(LoggingMerger):
1358
supports_lca_trees = True
1360
merger = self.make_Merger(self.setup_simple_graph(), 'C-id')
1361
merger.merge_type = LCATreesMerger
1362
merge_obj = merger.make_merger()
1363
self.assertIsInstance(merge_obj, LCATreesMerger)
1364
self.assertFalse('lca_trees' in merge_obj.kwargs)
1366
def test_criss_cross_passed_to_merge_type(self):
1367
merger = self.make_Merger(self.setup_criss_cross_graph(), 'E-id')
1368
merger.merge_type = _mod_merge.Merge3Merger
1369
merge_obj = merger.make_merger()
1370
self.assertEqual(['B-id', 'C-id'], [t.get_revision_id()
1371
for t in merger._lca_trees])
1373
def test_criss_cross_not_supported_merge_type(self):
1374
merger = self.make_Merger(self.setup_criss_cross_graph(), 'E-id')
1375
# We explicitly do not define supports_lca_trees
1376
merger.merge_type = LoggingMerger
1377
merge_obj = merger.make_merger()
1378
self.assertIsInstance(merge_obj, LoggingMerger)
1379
self.assertFalse('lca_trees' in merge_obj.kwargs)
1381
def test_criss_cross_unsupported_merge_type(self):
1382
class UnsupportedLCATreesMerger(LoggingMerger):
1383
supports_lca_trees = False
1385
merger = self.make_Merger(self.setup_criss_cross_graph(), 'E-id')
1386
merger.merge_type = UnsupportedLCATreesMerger
1387
merge_obj = merger.make_merger()
1388
self.assertIsInstance(merge_obj, UnsupportedLCATreesMerger)
1389
self.assertFalse('lca_trees' in merge_obj.kwargs)
1392
class TestMergerEntriesLCA(TestMergerBase):
1394
def make_merge_obj(self, builder, other_revision_id,
1395
interesting_files=None, interesting_ids=None):
1396
merger = self.make_Merger(builder, other_revision_id,
1397
interesting_files=interesting_files,
1398
interesting_ids=interesting_ids)
1399
return merger.make_merger()
1401
def test_simple(self):
1402
builder = self.get_builder()
1403
builder.build_snapshot('A-id', None,
1404
[('add', (u'', 'a-root-id', 'directory', None)),
1405
('add', (u'a', 'a-id', 'file', 'a\nb\nc\n'))])
1406
builder.build_snapshot('C-id', ['A-id'],
1407
[('modify', ('a-id', 'a\nb\nC\nc\n'))])
1408
builder.build_snapshot('B-id', ['A-id'],
1409
[('modify', ('a-id', 'a\nB\nb\nc\n'))])
1410
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1411
[('modify', ('a-id', 'a\nB\nb\nC\nc\nE\n'))])
1412
builder.build_snapshot('D-id', ['B-id', 'C-id'],
1413
[('modify', ('a-id', 'a\nB\nb\nC\nc\n'))])
1414
merge_obj = self.make_merge_obj(builder, 'E-id')
1416
self.assertEqual(['B-id', 'C-id'], [t.get_revision_id()
1417
for t in merge_obj._lca_trees])
1418
self.assertEqual('A-id', merge_obj.base_tree.get_revision_id())
1419
entries = list(merge_obj._entries_lca())
1421
# (file_id, changed, parents, names, executable)
1422
# BASE, lca1, lca2, OTHER, THIS
1423
root_id = 'a-root-id'
1424
self.assertEqual([('a-id', True,
1425
((root_id, [root_id, root_id]), root_id, root_id),
1426
((u'a', [u'a', u'a']), u'a', u'a'),
1427
((False, [False, False]), False, False)),
1430
def test_not_in_base(self):
1431
# LCAs all have the same last-modified revision for the file, as do
1432
# the tips, but the base has something different
1433
# A base, doesn't have the file
1435
# B C B introduces 'foo', C introduces 'bar'
1437
# D E D and E now both have 'foo' and 'bar'
1439
# F G the files are now in F, G, D and E, but not in A
1442
builder = self.get_builder()
1443
builder.build_snapshot('A-id', None,
1444
[('add', (u'', 'a-root-id', 'directory', None))])
1445
builder.build_snapshot('B-id', ['A-id'],
1446
[('add', (u'foo', 'foo-id', 'file', 'a\nb\nc\n'))])
1447
builder.build_snapshot('C-id', ['A-id'],
1448
[('add', (u'bar', 'bar-id', 'file', 'd\ne\nf\n'))])
1449
builder.build_snapshot('D-id', ['B-id', 'C-id'],
1450
[('add', (u'bar', 'bar-id', 'file', 'd\ne\nf\n'))])
1451
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1452
[('add', (u'foo', 'foo-id', 'file', 'a\nb\nc\n'))])
1453
builder.build_snapshot('G-id', ['E-id', 'D-id'],
1454
[('modify', (u'bar-id', 'd\ne\nf\nG\n'))])
1455
builder.build_snapshot('F-id', ['D-id', 'E-id'], [])
1456
merge_obj = self.make_merge_obj(builder, 'G-id')
1458
self.assertEqual(['D-id', 'E-id'], [t.get_revision_id()
1459
for t in merge_obj._lca_trees])
1460
self.assertEqual('A-id', merge_obj.base_tree.get_revision_id())
1461
entries = list(merge_obj._entries_lca())
1462
root_id = 'a-root-id'
1463
self.assertEqual([('bar-id', True,
1464
((None, [root_id, root_id]), root_id, root_id),
1465
((None, [u'bar', u'bar']), u'bar', u'bar'),
1466
((None, [False, False]), False, False)),
1469
def test_not_in_this(self):
1470
builder = self.get_builder()
1471
builder.build_snapshot('A-id', None,
1472
[('add', (u'', 'a-root-id', 'directory', None)),
1473
('add', (u'a', 'a-id', 'file', 'a\nb\nc\n'))])
1474
builder.build_snapshot('B-id', ['A-id'],
1475
[('modify', ('a-id', 'a\nB\nb\nc\n'))])
1476
builder.build_snapshot('C-id', ['A-id'],
1477
[('modify', ('a-id', 'a\nb\nC\nc\n'))])
1478
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1479
[('modify', ('a-id', 'a\nB\nb\nC\nc\nE\n'))])
1480
builder.build_snapshot('D-id', ['B-id', 'C-id'],
1481
[('unversion', 'a-id')])
1482
merge_obj = self.make_merge_obj(builder, 'E-id')
1484
self.assertEqual(['B-id', 'C-id'], [t.get_revision_id()
1485
for t in merge_obj._lca_trees])
1486
self.assertEqual('A-id', merge_obj.base_tree.get_revision_id())
1488
entries = list(merge_obj._entries_lca())
1489
root_id = 'a-root-id'
1490
self.assertEqual([('a-id', True,
1491
((root_id, [root_id, root_id]), root_id, None),
1492
((u'a', [u'a', u'a']), u'a', None),
1493
((False, [False, False]), False, None)),
1496
def test_file_not_in_one_lca(self):
1499
# B C # B no file, C introduces a file
1501
# D E # D and E both have the file, unchanged from C
1502
builder = self.get_builder()
1503
builder.build_snapshot('A-id', None,
1504
[('add', (u'', 'a-root-id', 'directory', None))])
1505
builder.build_snapshot('B-id', ['A-id'], [])
1506
builder.build_snapshot('C-id', ['A-id'],
1507
[('add', (u'a', 'a-id', 'file', 'a\nb\nc\n'))])
1508
builder.build_snapshot('E-id', ['C-id', 'B-id'], []) # Inherited from C
1509
builder.build_snapshot('D-id', ['B-id', 'C-id'], # Merged from C
1510
[('add', (u'a', 'a-id', 'file', 'a\nb\nc\n'))])
1511
merge_obj = self.make_merge_obj(builder, 'E-id')
1513
self.assertEqual(['B-id', 'C-id'], [t.get_revision_id()
1514
for t in merge_obj._lca_trees])
1515
self.assertEqual('A-id', merge_obj.base_tree.get_revision_id())
1517
entries = list(merge_obj._entries_lca())
1518
self.assertEqual([], entries)
1520
def test_not_in_other(self):
1521
builder = self.get_builder()
1522
builder.build_snapshot('A-id', None,
1523
[('add', (u'', 'a-root-id', 'directory', None)),
1524
('add', (u'a', 'a-id', 'file', 'a\nb\nc\n'))])
1525
builder.build_snapshot('B-id', ['A-id'], [])
1526
builder.build_snapshot('C-id', ['A-id'], [])
1527
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1528
[('unversion', 'a-id')])
1529
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1530
merge_obj = self.make_merge_obj(builder, 'E-id')
1532
entries = list(merge_obj._entries_lca())
1533
root_id = 'a-root-id'
1534
self.assertEqual([('a-id', True,
1535
((root_id, [root_id, root_id]), None, root_id),
1536
((u'a', [u'a', u'a']), None, u'a'),
1537
((False, [False, False]), None, False)),
1540
def test_not_in_other_or_lca(self):
1541
# A base, introduces 'foo'
1543
# B C B nothing, C deletes foo
1545
# D E D restores foo (same as B), E leaves it deleted
1547
# A => B, no changes
1548
# A => C, delete foo (C should supersede B)
1549
# C => D, restore foo
1550
# C => E, no changes
1551
# D would then win 'cleanly' and no record would be given
1552
builder = self.get_builder()
1553
builder.build_snapshot('A-id', None,
1554
[('add', (u'', 'a-root-id', 'directory', None)),
1555
('add', (u'foo', 'foo-id', 'file', 'content\n'))])
1556
builder.build_snapshot('B-id', ['A-id'], [])
1557
builder.build_snapshot('C-id', ['A-id'],
1558
[('unversion', 'foo-id')])
1559
builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
1560
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1561
merge_obj = self.make_merge_obj(builder, 'E-id')
1563
entries = list(merge_obj._entries_lca())
1564
self.assertEqual([], entries)
1566
def test_not_in_other_mod_in_lca1_not_in_lca2(self):
1567
# A base, introduces 'foo'
1569
# B C B changes 'foo', C deletes foo
1571
# D E D restores foo (same as B), E leaves it deleted (as C)
1573
# A => B, modified foo
1574
# A => C, delete foo, C does not supersede B
1575
# B => D, no changes
1576
# C => D, resolve in favor of B
1577
# B => E, resolve in favor of E
1578
# C => E, no changes
1579
# In this case, we have a conflict of how the changes were resolved. E
1580
# picked C and D picked B, so we should issue a conflict
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', 'content\n'))])
1585
builder.build_snapshot('B-id', ['A-id'], [
1586
('modify', ('foo-id', 'new-content\n'))])
1587
builder.build_snapshot('C-id', ['A-id'],
1588
[('unversion', 'foo-id')])
1589
builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
1590
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1591
merge_obj = self.make_merge_obj(builder, 'E-id')
1593
entries = list(merge_obj._entries_lca())
1594
root_id = 'a-root-id'
1595
self.assertEqual([('foo-id', True,
1596
((root_id, [root_id, None]), None, root_id),
1597
((u'foo', [u'foo', None]), None, 'foo'),
1598
((False, [False, None]), None, False)),
1601
def test_only_in_one_lca(self):
1604
# B C B nothing, C add file
1606
# D E D still has nothing, E removes file
1609
# C => D, removed the file
1611
# C => E, removed the file
1612
# Thus D & E have identical changes, and this is a no-op
1615
# A => C, add file, thus C supersedes B
1616
# w/ C=BASE, D=THIS, E=OTHER we have 'happy convergence'
1617
builder = self.get_builder()
1618
builder.build_snapshot('A-id', None,
1619
[('add', (u'', 'a-root-id', 'directory', None))])
1620
builder.build_snapshot('B-id', ['A-id'], [])
1621
builder.build_snapshot('C-id', ['A-id'],
1622
[('add', (u'a', 'a-id', 'file', 'a\nb\nc\n'))])
1623
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1624
[('unversion', 'a-id')])
1625
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1626
merge_obj = self.make_merge_obj(builder, 'E-id')
1628
entries = list(merge_obj._entries_lca())
1629
self.assertEqual([], entries)
1631
def test_only_in_other(self):
1632
builder = self.get_builder()
1633
builder.build_snapshot('A-id', None,
1634
[('add', (u'', 'a-root-id', 'directory', None))])
1635
builder.build_snapshot('B-id', ['A-id'], [])
1636
builder.build_snapshot('C-id', ['A-id'], [])
1637
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1638
[('add', (u'a', 'a-id', 'file', 'a\nb\nc\n'))])
1639
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1640
merge_obj = self.make_merge_obj(builder, 'E-id')
1642
entries = list(merge_obj._entries_lca())
1643
root_id = 'a-root-id'
1644
self.assertEqual([('a-id', True,
1645
((None, [None, None]), root_id, None),
1646
((None, [None, None]), u'a', None),
1647
((None, [None, None]), False, None)),
1650
def test_one_lca_supersedes(self):
1651
# One LCA supersedes the other LCAs last modified value, but the
1652
# value is not the same as BASE.
1653
# A base, introduces 'foo', last mod A
1655
# B C B modifies 'foo' (mod B), C does nothing (mod A)
1657
# D E D does nothing (mod B), E updates 'foo' (mod E)
1659
# F G F updates 'foo' (mod F). G does nothing (mod E)
1661
# At this point, G should not be considered to modify 'foo', even
1662
# though its LCAs disagree. This is because the modification in E
1663
# completely supersedes the value in D.
1664
builder = self.get_builder()
1665
builder.build_snapshot('A-id', None,
1666
[('add', (u'', 'a-root-id', 'directory', None)),
1667
('add', (u'foo', 'foo-id', 'file', 'A content\n'))])
1668
builder.build_snapshot('C-id', ['A-id'], [])
1669
builder.build_snapshot('B-id', ['A-id'],
1670
[('modify', ('foo-id', 'B content\n'))])
1671
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1672
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1673
[('modify', ('foo-id', 'E content\n'))])
1674
builder.build_snapshot('G-id', ['E-id', 'D-id'], [])
1675
builder.build_snapshot('F-id', ['D-id', 'E-id'],
1676
[('modify', ('foo-id', 'F content\n'))])
1677
merge_obj = self.make_merge_obj(builder, 'G-id')
1679
self.assertEqual([], list(merge_obj._entries_lca()))
1681
def test_one_lca_supersedes_path(self):
1682
# Double-criss-cross merge, the ultimate base value is different from
1686
# B C B value 'bar', C = 'foo'
1688
# D E D = 'bar', E supersedes to 'bing'
1690
# F G F = 'bing', G supersedes to 'barry'
1692
# In this case, we technically should not care about the value 'bar' for
1693
# D, because it was clearly superseded by E's 'bing'. The
1694
# per-file/attribute graph would actually look like:
1703
# Because the other side of the merge never modifies the value, it just
1704
# takes the value from the merge.
1706
# ATM this fails because we will prune 'foo' from the LCAs, but we
1707
# won't prune 'bar'. This is getting far off into edge-case land, so we
1708
# aren't supporting it yet.
1710
builder = self.get_builder()
1711
builder.build_snapshot('A-id', None,
1712
[('add', (u'', 'a-root-id', 'directory', None)),
1713
('add', (u'foo', 'foo-id', 'file', 'A content\n'))])
1714
builder.build_snapshot('C-id', ['A-id'], [])
1715
builder.build_snapshot('B-id', ['A-id'],
1716
[('rename', ('foo', 'bar'))])
1717
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1718
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1719
[('rename', ('foo', 'bing'))]) # override to bing
1720
builder.build_snapshot('G-id', ['E-id', 'D-id'],
1721
[('rename', ('bing', 'barry'))]) # override to barry
1722
builder.build_snapshot('F-id', ['D-id', 'E-id'],
1723
[('rename', ('bar', 'bing'))]) # Merge in E's change
1724
merge_obj = self.make_merge_obj(builder, 'G-id')
1726
self.expectFailure("We don't do an actual heads() check on lca values,"
1727
" or use the per-attribute graph",
1728
self.assertEqual, [], list(merge_obj._entries_lca()))
1730
def test_one_lca_accidentally_pruned(self):
1731
# Another incorrect resolution from the same basic flaw:
1734
# B C B value 'bar', C = 'foo'
1736
# D E D = 'bar', E reverts to 'foo'
1738
# F G F = 'bing', G switches to 'bar'
1740
# 'bar' will not be seen as an interesting change, because 'foo' will
1741
# be pruned from the LCAs, even though it was newly introduced by E
1743
builder = self.get_builder()
1744
builder.build_snapshot('A-id', None,
1745
[('add', (u'', 'a-root-id', 'directory', None)),
1746
('add', (u'foo', 'foo-id', 'file', 'A content\n'))])
1747
builder.build_snapshot('C-id', ['A-id'], [])
1748
builder.build_snapshot('B-id', ['A-id'],
1749
[('rename', ('foo', 'bar'))])
1750
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1751
builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
1752
builder.build_snapshot('G-id', ['E-id', 'D-id'],
1753
[('rename', ('foo', 'bar'))])
1754
builder.build_snapshot('F-id', ['D-id', 'E-id'],
1755
[('rename', ('bar', 'bing'))]) # should end up conflicting
1756
merge_obj = self.make_merge_obj(builder, 'G-id')
1758
entries = list(merge_obj._entries_lca())
1759
root_id = 'a-root-id'
1760
self.expectFailure("We prune values from BASE even when relevant.",
1763
((root_id, [root_id, root_id]), root_id, root_id),
1764
((u'foo', [u'bar', u'foo']), u'bar', u'bing'),
1765
((False, [False, False]), False, False)),
1768
def test_both_sides_revert(self):
1769
# Both sides of a criss-cross revert the text to the lca
1770
# A base, introduces 'foo'
1772
# B C B modifies 'foo', C modifies 'foo'
1774
# D E D reverts to B, E reverts to C
1775
# This should conflict
1776
builder = self.get_builder()
1777
builder.build_snapshot('A-id', None,
1778
[('add', (u'', 'a-root-id', 'directory', None)),
1779
('add', (u'foo', 'foo-id', 'file', 'A content\n'))])
1780
builder.build_snapshot('B-id', ['A-id'],
1781
[('modify', ('foo-id', 'B content\n'))])
1782
builder.build_snapshot('C-id', ['A-id'],
1783
[('modify', ('foo-id', 'C content\n'))])
1784
builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
1785
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1786
merge_obj = self.make_merge_obj(builder, 'E-id')
1788
entries = list(merge_obj._entries_lca())
1789
root_id = 'a-root-id'
1790
self.assertEqual([('foo-id', True,
1791
((root_id, [root_id, root_id]), root_id, root_id),
1792
((u'foo', [u'foo', u'foo']), u'foo', u'foo'),
1793
((False, [False, False]), False, False)),
1796
def test_different_lca_resolve_one_side_updates_content(self):
1797
# Both sides converge, but then one side updates the text.
1798
# A base, introduces 'foo'
1800
# B C B modifies 'foo', C modifies 'foo'
1802
# D E D reverts to B, E reverts to C
1804
# F F updates to a new value
1805
# We need to emit an entry for 'foo', because D & E differed on the
1807
builder = self.get_builder()
1808
builder.build_snapshot('A-id', None,
1809
[('add', (u'', 'a-root-id', 'directory', None)),
1810
('add', (u'foo', 'foo-id', 'file', 'A content\n'))])
1811
builder.build_snapshot('B-id', ['A-id'],
1812
[('modify', ('foo-id', 'B content\n'))])
1813
builder.build_snapshot('C-id', ['A-id'],
1814
[('modify', ('foo-id', 'C content\n'))])
1815
builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
1816
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1817
builder.build_snapshot('F-id', ['D-id'],
1818
[('modify', ('foo-id', 'F content\n'))])
1819
merge_obj = self.make_merge_obj(builder, 'E-id')
1821
entries = list(merge_obj._entries_lca())
1822
root_id = 'a-root-id'
1823
self.assertEqual([('foo-id', True,
1824
((root_id, [root_id, root_id]), root_id, root_id),
1825
((u'foo', [u'foo', u'foo']), u'foo', u'foo'),
1826
((False, [False, False]), False, False)),
1829
def test_same_lca_resolution_one_side_updates_content(self):
1830
# Both sides converge, but then one side updates the text.
1831
# A base, introduces 'foo'
1833
# B C B modifies 'foo', C modifies 'foo'
1835
# D E D and E use C's value
1837
# F F updates to a new value
1838
# I think it is a bug that this conflicts, but we don't have a way to
1839
# detect otherwise. And because of:
1840
# test_different_lca_resolve_one_side_updates_content
1841
# We need to conflict.
1843
builder = self.get_builder()
1844
builder.build_snapshot('A-id', None,
1845
[('add', (u'', 'a-root-id', 'directory', None)),
1846
('add', (u'foo', 'foo-id', 'file', 'A content\n'))])
1847
builder.build_snapshot('B-id', ['A-id'],
1848
[('modify', ('foo-id', 'B content\n'))])
1849
builder.build_snapshot('C-id', ['A-id'],
1850
[('modify', ('foo-id', 'C content\n'))])
1851
builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
1852
builder.build_snapshot('D-id', ['B-id', 'C-id'],
1853
[('modify', ('foo-id', 'C content\n'))]) # Same as E
1854
builder.build_snapshot('F-id', ['D-id'],
1855
[('modify', ('foo-id', 'F content\n'))])
1856
merge_obj = self.make_merge_obj(builder, 'E-id')
1858
entries = list(merge_obj._entries_lca())
1859
self.expectFailure("We don't detect that LCA resolution was the"
1860
" same on both sides",
1861
self.assertEqual, [], entries)
1863
def test_only_path_changed(self):
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
builder.build_snapshot('B-id', ['A-id'], [])
1869
builder.build_snapshot('C-id', ['A-id'], [])
1870
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1871
[('rename', (u'a', u'b'))])
1872
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1873
merge_obj = self.make_merge_obj(builder, 'E-id')
1874
entries = list(merge_obj._entries_lca())
1875
root_id = 'a-root-id'
1876
# The content was not changed, only the path
1877
self.assertEqual([('a-id', False,
1878
((root_id, [root_id, root_id]), root_id, root_id),
1879
((u'a', [u'a', u'a']), u'b', u'a'),
1880
((False, [False, False]), False, False)),
1883
def test_kind_changed(self):
1884
# Identical content, except 'D' changes a-id into a directory
1885
builder = self.get_builder()
1886
builder.build_snapshot('A-id', None,
1887
[('add', (u'', 'a-root-id', 'directory', None)),
1888
('add', (u'a', 'a-id', 'file', 'content\n'))])
1889
builder.build_snapshot('B-id', ['A-id'], [])
1890
builder.build_snapshot('C-id', ['A-id'], [])
1891
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1892
[('unversion', 'a-id'),
1894
('add', (u'a', 'a-id', 'directory', None))])
1895
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1896
merge_obj = self.make_merge_obj(builder, 'E-id')
1897
entries = list(merge_obj._entries_lca())
1898
root_id = 'a-root-id'
1899
# Only the kind was changed (content)
1900
self.assertEqual([('a-id', True,
1901
((root_id, [root_id, root_id]), root_id, root_id),
1902
((u'a', [u'a', u'a']), u'a', u'a'),
1903
((False, [False, False]), False, False)),
1906
def test_this_changed_kind(self):
1907
# Identical content, but THIS changes a file to a directory
1908
builder = self.get_builder()
1909
builder.build_snapshot('A-id', None,
1910
[('add', (u'', 'a-root-id', 'directory', None)),
1911
('add', (u'a', 'a-id', 'file', 'content\n'))])
1912
builder.build_snapshot('B-id', ['A-id'], [])
1913
builder.build_snapshot('C-id', ['A-id'], [])
1914
builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
1915
builder.build_snapshot('D-id', ['B-id', 'C-id'],
1916
[('unversion', 'a-id'),
1918
('add', (u'a', 'a-id', 'directory', None))])
1919
merge_obj = self.make_merge_obj(builder, 'E-id')
1920
entries = list(merge_obj._entries_lca())
1921
# Only the kind was changed (content)
1922
self.assertEqual([], entries)
1924
def test_interesting_files(self):
1925
# Two files modified, but we should filter one of them
1926
builder = self.get_builder()
1927
builder.build_snapshot('A-id', None,
1928
[('add', (u'', 'a-root-id', 'directory', None)),
1929
('add', (u'a', 'a-id', 'file', 'content\n')),
1930
('add', (u'b', 'b-id', 'file', 'content\n'))])
1931
builder.build_snapshot('B-id', ['A-id'], [])
1932
builder.build_snapshot('C-id', ['A-id'], [])
1933
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1934
[('modify', ('a-id', 'new-content\n')),
1935
('modify', ('b-id', 'new-content\n'))])
1936
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1937
merge_obj = self.make_merge_obj(builder, 'E-id',
1938
interesting_files=['b'])
1939
entries = list(merge_obj._entries_lca())
1940
root_id = 'a-root-id'
1941
self.assertEqual([('b-id', True,
1942
((root_id, [root_id, root_id]), root_id, root_id),
1943
((u'b', [u'b', u'b']), u'b', u'b'),
1944
((False, [False, False]), False, False)),
1947
def test_interesting_file_in_this(self):
1948
# This renamed the file, but it should still match the entry in other
1949
builder = self.get_builder()
1950
builder.build_snapshot('A-id', None,
1951
[('add', (u'', 'a-root-id', 'directory', None)),
1952
('add', (u'a', 'a-id', 'file', 'content\n')),
1953
('add', (u'b', 'b-id', 'file', 'content\n'))])
1954
builder.build_snapshot('B-id', ['A-id'], [])
1955
builder.build_snapshot('C-id', ['A-id'], [])
1956
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1957
[('modify', ('a-id', 'new-content\n')),
1958
('modify', ('b-id', 'new-content\n'))])
1959
builder.build_snapshot('D-id', ['B-id', 'C-id'],
1960
[('rename', ('b', 'c'))])
1961
merge_obj = self.make_merge_obj(builder, 'E-id',
1962
interesting_files=['c'])
1963
entries = list(merge_obj._entries_lca())
1964
root_id = 'a-root-id'
1965
self.assertEqual([('b-id', True,
1966
((root_id, [root_id, root_id]), root_id, root_id),
1967
((u'b', [u'b', u'b']), u'b', u'c'),
1968
((False, [False, False]), False, False)),
1971
def test_interesting_file_in_base(self):
1972
# This renamed the file, but it should still match the entry in BASE
1973
builder = self.get_builder()
1974
builder.build_snapshot('A-id', None,
1975
[('add', (u'', 'a-root-id', 'directory', None)),
1976
('add', (u'a', 'a-id', 'file', 'content\n')),
1977
('add', (u'c', 'c-id', 'file', 'content\n'))])
1978
builder.build_snapshot('B-id', ['A-id'],
1979
[('rename', ('c', 'b'))])
1980
builder.build_snapshot('C-id', ['A-id'],
1981
[('rename', ('c', 'b'))])
1982
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1983
[('modify', ('a-id', 'new-content\n')),
1984
('modify', ('c-id', 'new-content\n'))])
1985
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1986
merge_obj = self.make_merge_obj(builder, 'E-id',
1987
interesting_files=['c'])
1988
entries = list(merge_obj._entries_lca())
1989
root_id = 'a-root-id'
1990
self.assertEqual([('c-id', True,
1991
((root_id, [root_id, root_id]), root_id, root_id),
1992
((u'c', [u'b', u'b']), u'b', u'b'),
1993
((False, [False, False]), False, False)),
1996
def test_interesting_file_in_lca(self):
1997
# This renamed the file, but it should still match the entry in LCA
1998
builder = self.get_builder()
1999
builder.build_snapshot('A-id', None,
2000
[('add', (u'', 'a-root-id', 'directory', None)),
2001
('add', (u'a', 'a-id', 'file', 'content\n')),
2002
('add', (u'b', 'b-id', 'file', 'content\n'))])
2003
builder.build_snapshot('B-id', ['A-id'],
2004
[('rename', ('b', 'c'))])
2005
builder.build_snapshot('C-id', ['A-id'], [])
2006
builder.build_snapshot('E-id', ['C-id', 'B-id'],
2007
[('modify', ('a-id', 'new-content\n')),
2008
('modify', ('b-id', 'new-content\n'))])
2009
builder.build_snapshot('D-id', ['B-id', 'C-id'],
2010
[('rename', ('c', 'b'))])
2011
merge_obj = self.make_merge_obj(builder, 'E-id',
2012
interesting_files=['c'])
2013
entries = list(merge_obj._entries_lca())
2014
root_id = 'a-root-id'
2015
self.assertEqual([('b-id', True,
2016
((root_id, [root_id, root_id]), root_id, root_id),
2017
((u'b', [u'c', u'b']), u'b', u'b'),
2018
((False, [False, False]), False, False)),
2021
def test_interesting_ids(self):
2022
# Two files modified, but we should filter one of them
2023
builder = self.get_builder()
2024
builder.build_snapshot('A-id', None,
2025
[('add', (u'', 'a-root-id', 'directory', None)),
2026
('add', (u'a', 'a-id', 'file', 'content\n')),
2027
('add', (u'b', 'b-id', 'file', 'content\n'))])
2028
builder.build_snapshot('B-id', ['A-id'], [])
2029
builder.build_snapshot('C-id', ['A-id'], [])
2030
builder.build_snapshot('E-id', ['C-id', 'B-id'],
2031
[('modify', ('a-id', 'new-content\n')),
2032
('modify', ('b-id', 'new-content\n'))])
2033
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
2034
merge_obj = self.make_merge_obj(builder, 'E-id',
2035
interesting_ids=['b-id'])
2036
entries = list(merge_obj._entries_lca())
2037
root_id = 'a-root-id'
2038
self.assertEqual([('b-id', True,
2039
((root_id, [root_id, root_id]), root_id, root_id),
2040
((u'b', [u'b', u'b']), u'b', u'b'),
2041
((False, [False, False]), False, False)),
2046
class TestMergerEntriesLCAOnDisk(tests.TestCaseWithTransport):
2048
def get_builder(self):
2049
builder = self.make_branch_builder('path')
2050
builder.start_series()
2051
self.addCleanup(builder.finish_series)
2054
def get_wt_from_builder(self, builder):
2055
"""Get a real WorkingTree from the builder."""
2056
the_branch = builder.get_branch()
2057
wt = the_branch.bzrdir.create_workingtree()
2058
# Note: This is a little bit ugly, but we are holding the branch
2059
# write-locked as part of the build process, and we would like to
2060
# maintain that. So we just force the WT to re-use the same
2062
wt._branch = the_branch
2064
self.addCleanup(wt.unlock)
2067
def do_merge(self, builder, other_revision_id):
2068
wt = self.get_wt_from_builder(builder)
2069
merger = _mod_merge.Merger.from_revision_ids(None,
2070
wt, other_revision_id)
2071
merger.merge_type = _mod_merge.Merge3Merger
2072
return wt, merger.do_merge()
2074
def test_simple_lca(self):
2075
builder = self.get_builder()
2076
builder.build_snapshot('A-id', None,
2077
[('add', (u'', 'a-root-id', 'directory', None)),
2078
('add', (u'a', 'a-id', 'file', 'a\nb\nc\n'))])
2079
builder.build_snapshot('C-id', ['A-id'], [])
2080
builder.build_snapshot('B-id', ['A-id'], [])
2081
builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
2082
builder.build_snapshot('D-id', ['B-id', 'C-id'],
2083
[('modify', ('a-id', 'a\nb\nc\nd\ne\nf\n'))])
2084
wt, conflicts = self.do_merge(builder, 'E-id')
2085
self.assertEqual(0, conflicts)
2086
# The merge should have simply update the contents of 'a'
2087
self.assertEqual('a\nb\nc\nd\ne\nf\n', wt.get_file_text('a-id'))
2089
def test_conflict_without_lca(self):
2090
# This test would cause a merge conflict, unless we use the lca trees
2091
# to determine the real ancestry
2094
# B C Path renamed to 'bar' in B
2098
# D E Path at 'bar' in D and E
2100
# F Path at 'baz' in F, which supersedes 'bar' and 'foo'
2101
builder = self.get_builder()
2102
builder.build_snapshot('A-id', None,
2103
[('add', (u'', 'a-root-id', 'directory', None)),
2104
('add', (u'foo', 'foo-id', 'file', 'a\nb\nc\n'))])
2105
builder.build_snapshot('C-id', ['A-id'], [])
2106
builder.build_snapshot('B-id', ['A-id'],
2107
[('rename', ('foo', 'bar'))])
2108
builder.build_snapshot('E-id', ['C-id', 'B-id'], # merge the rename
2109
[('rename', ('foo', 'bar'))])
2110
builder.build_snapshot('F-id', ['E-id'],
2111
[('rename', ('bar', 'baz'))])
2112
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
2113
wt, conflicts = self.do_merge(builder, 'F-id')
2114
self.assertEqual(0, conflicts)
2115
# The merge should simply recognize that the final rename takes
2117
self.assertEqual('baz', wt.id2path('foo-id'))
2119
def test_other_deletes_lca_renames(self):
2120
# This test would cause a merge conflict, unless we use the lca trees
2121
# to determine the real ancestry
2124
# B C Path renamed to 'bar' in B
2128
# D E Path at 'bar' in D and E
2131
builder = self.get_builder()
2132
builder.build_snapshot('A-id', None,
2133
[('add', (u'', 'a-root-id', 'directory', None)),
2134
('add', (u'foo', 'foo-id', 'file', 'a\nb\nc\n'))])
2135
builder.build_snapshot('C-id', ['A-id'], [])
2136
builder.build_snapshot('B-id', ['A-id'],
2137
[('rename', ('foo', 'bar'))])
2138
builder.build_snapshot('E-id', ['C-id', 'B-id'], # merge the rename
2139
[('rename', ('foo', 'bar'))])
2140
builder.build_snapshot('F-id', ['E-id'],
2141
[('unversion', 'foo-id')])
2142
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
2143
wt, conflicts = self.do_merge(builder, 'F-id')
2144
self.assertEqual(0, conflicts)
2145
self.assertRaises(errors.NoSuchId, wt.id2path, 'foo-id')
2147
def test_executable_changes(self):
2156
# F Executable bit changed
2157
builder = self.get_builder()
2158
builder.build_snapshot('A-id', None,
2159
[('add', (u'', 'a-root-id', 'directory', None)),
2160
('add', (u'foo', 'foo-id', 'file', 'a\nb\nc\n'))])
2161
builder.build_snapshot('C-id', ['A-id'], [])
2162
builder.build_snapshot('B-id', ['A-id'], [])
2163
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
2164
builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
2165
# Have to use a real WT, because BranchBuilder doesn't support exec bit
2166
wt = self.get_wt_from_builder(builder)
2167
tt = transform.TreeTransform(wt)
2169
tt.set_executability(True, tt.trans_id_tree_file_id('foo-id'))
2174
self.assertTrue(wt.is_executable('foo-id'))
2175
wt.commit('F-id', rev_id='F-id')
2176
# Reset to D, so that we can merge F
2177
wt.set_parent_ids(['D-id'])
2178
wt.branch.set_last_revision_info(3, 'D-id')
2180
self.assertFalse(wt.is_executable('foo-id'))
2181
conflicts = wt.merge_from_branch(wt.branch, to_revision='F-id')
2182
self.assertEqual(0, conflicts)
2183
self.assertTrue(wt.is_executable('foo-id'))
2185
def test_create_symlink(self):
2186
self.requireFeature(features.SymlinkFeature)
2195
# F Add a symlink 'foo' => 'bar'
2196
# Have to use a real WT, because BranchBuilder and MemoryTree don't
2197
# have symlink support
2198
builder = self.get_builder()
2199
builder.build_snapshot('A-id', None,
2200
[('add', (u'', 'a-root-id', 'directory', None))])
2201
builder.build_snapshot('C-id', ['A-id'], [])
2202
builder.build_snapshot('B-id', ['A-id'], [])
2203
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
2204
builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
2205
# Have to use a real WT, because BranchBuilder doesn't support exec bit
2206
wt = self.get_wt_from_builder(builder)
2207
os.symlink('bar', 'path/foo')
2208
wt.add(['foo'], ['foo-id'])
2209
self.assertEqual('bar', wt.get_symlink_target('foo-id'))
2210
wt.commit('add symlink', rev_id='F-id')
2211
# Reset to D, so that we can merge F
2212
wt.set_parent_ids(['D-id'])
2213
wt.branch.set_last_revision_info(3, 'D-id')
2215
self.assertIs(None, wt.path2id('foo'))
2216
conflicts = wt.merge_from_branch(wt.branch, to_revision='F-id')
2217
self.assertEqual(0, conflicts)
2218
self.assertEqual('foo-id', wt.path2id('foo'))
2219
self.assertEqual('bar', wt.get_symlink_target('foo-id'))
2221
def test_both_sides_revert(self):
2222
# Both sides of a criss-cross revert the text to the lca
2223
# A base, introduces 'foo'
2225
# B C B modifies 'foo', C modifies 'foo'
2227
# D E D reverts to B, E reverts to C
2228
# This should conflict
2229
# This must be done with a real WorkingTree, because normally their
2230
# inventory contains "None" rather than a real sha1
2231
builder = self.get_builder()
2232
builder.build_snapshot('A-id', None,
2233
[('add', (u'', 'a-root-id', 'directory', None)),
2234
('add', (u'foo', 'foo-id', 'file', 'A content\n'))])
2235
builder.build_snapshot('B-id', ['A-id'],
2236
[('modify', ('foo-id', 'B content\n'))])
2237
builder.build_snapshot('C-id', ['A-id'],
2238
[('modify', ('foo-id', 'C content\n'))])
2239
builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
2240
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
2241
wt, conflicts = self.do_merge(builder, 'E-id')
2242
self.assertEqual(1, conflicts)
2243
self.assertEqualDiff('<<<<<<< TREE\n'
2247
'>>>>>>> MERGE-SOURCE\n',
2248
wt.get_file_text('foo-id'))
2250
def test_modified_symlink(self):
2251
self.requireFeature(features.SymlinkFeature)
2252
# A Create symlink foo => bar
2254
# B C B relinks foo => baz
2258
# D E D & E have foo => baz
2260
# F F changes it to bing
2262
# Merging D & F should result in F cleanly overriding D, because D's
2263
# value actually comes from B
2265
# Have to use a real WT, because BranchBuilder and MemoryTree don't
2266
# have symlink support
2267
wt = self.make_branch_and_tree('path')
2269
self.addCleanup(wt.unlock)
2270
os.symlink('bar', 'path/foo')
2271
wt.add(['foo'], ['foo-id'])
2272
wt.commit('add symlink', rev_id='A-id')
2273
os.remove('path/foo')
2274
os.symlink('baz', 'path/foo')
2275
wt.commit('foo => baz', rev_id='B-id')
2276
wt.set_last_revision('A-id')
2277
wt.branch.set_last_revision_info(1, 'A-id')
2279
wt.commit('C', rev_id='C-id')
2280
wt.merge_from_branch(wt.branch, 'B-id')
2281
self.assertEqual('baz', wt.get_symlink_target('foo-id'))
2282
wt.commit('E merges C & B', rev_id='E-id')
2283
os.remove('path/foo')
2284
os.symlink('bing', 'path/foo')
2285
wt.commit('F foo => bing', rev_id='F-id')
2286
wt.set_last_revision('B-id')
2287
wt.branch.set_last_revision_info(2, 'B-id')
2289
wt.merge_from_branch(wt.branch, 'C-id')
2290
wt.commit('D merges B & C', rev_id='D-id')
2291
conflicts = wt.merge_from_branch(wt.branch, to_revision='F-id')
2292
self.assertEqual(0, conflicts)
2293
self.assertEqual('bing', wt.get_symlink_target('foo-id'))
2295
def test_renamed_symlink(self):
2296
self.requireFeature(features.SymlinkFeature)
2297
# A Create symlink foo => bar
2299
# B C B renames foo => barry
2303
# D E D & E have barry
2305
# F F renames barry to blah
2307
# Merging D & F should result in F cleanly overriding D, because D's
2308
# value actually comes from B
2310
wt = self.make_branch_and_tree('path')
2312
self.addCleanup(wt.unlock)
2313
os.symlink('bar', 'path/foo')
2314
wt.add(['foo'], ['foo-id'])
2315
wt.commit('A add symlink', rev_id='A-id')
2316
wt.rename_one('foo', 'barry')
2317
wt.commit('B foo => barry', rev_id='B-id')
2318
wt.set_last_revision('A-id')
2319
wt.branch.set_last_revision_info(1, 'A-id')
2321
wt.commit('C', rev_id='C-id')
2322
wt.merge_from_branch(wt.branch, 'B-id')
2323
self.assertEqual('barry', wt.id2path('foo-id'))
2324
self.assertEqual('bar', wt.get_symlink_target('foo-id'))
2325
wt.commit('E merges C & B', rev_id='E-id')
2326
wt.rename_one('barry', 'blah')
2327
wt.commit('F barry => blah', rev_id='F-id')
2328
wt.set_last_revision('B-id')
2329
wt.branch.set_last_revision_info(2, 'B-id')
2331
wt.merge_from_branch(wt.branch, 'C-id')
2332
wt.commit('D merges B & C', rev_id='D-id')
2333
self.assertEqual('barry', wt.id2path('foo-id'))
2334
# Check the output of the Merger object directly
2335
merger = _mod_merge.Merger.from_revision_ids(None,
2337
merger.merge_type = _mod_merge.Merge3Merger
2338
merge_obj = merger.make_merger()
2339
root_id = wt.path2id('')
2340
entries = list(merge_obj._entries_lca())
2341
# No content change, just a path change
2342
self.assertEqual([('foo-id', False,
2343
((root_id, [root_id, root_id]), root_id, root_id),
2344
((u'foo', [u'barry', u'foo']), u'blah', u'barry'),
2345
((False, [False, False]), False, False)),
2347
conflicts = wt.merge_from_branch(wt.branch, to_revision='F-id')
2348
self.assertEqual(0, conflicts)
2349
self.assertEqual('blah', wt.id2path('foo-id'))
2351
def test_symlink_no_content_change(self):
2352
self.requireFeature(features.SymlinkFeature)
2353
# A Create symlink foo => bar
2355
# B C B relinks foo => baz
2359
# D E D & E have foo => baz
2361
# F F has foo => bing
2363
# Merging E into F should not cause a conflict, because E doesn't have
2364
# a content change relative to the LCAs (it does relative to A)
2365
wt = self.make_branch_and_tree('path')
2367
self.addCleanup(wt.unlock)
2368
os.symlink('bar', 'path/foo')
2369
wt.add(['foo'], ['foo-id'])
2370
wt.commit('add symlink', rev_id='A-id')
2371
os.remove('path/foo')
2372
os.symlink('baz', 'path/foo')
2373
wt.commit('foo => baz', rev_id='B-id')
2374
wt.set_last_revision('A-id')
2375
wt.branch.set_last_revision_info(1, 'A-id')
2377
wt.commit('C', rev_id='C-id')
2378
wt.merge_from_branch(wt.branch, 'B-id')
2379
self.assertEqual('baz', wt.get_symlink_target('foo-id'))
2380
wt.commit('E merges C & B', rev_id='E-id')
2381
wt.set_last_revision('B-id')
2382
wt.branch.set_last_revision_info(2, 'B-id')
2384
wt.merge_from_branch(wt.branch, 'C-id')
2385
wt.commit('D merges B & C', rev_id='D-id')
2386
os.remove('path/foo')
2387
os.symlink('bing', 'path/foo')
2388
wt.commit('F foo => bing', rev_id='F-id')
2390
# Check the output of the Merger object directly
2391
merger = _mod_merge.Merger.from_revision_ids(None,
2393
merger.merge_type = _mod_merge.Merge3Merger
2394
merge_obj = merger.make_merger()
2395
# Nothing interesting happened in OTHER relative to BASE
2396
self.assertEqual([], list(merge_obj._entries_lca()))
2397
# Now do a real merge, just to test the rest of the stack
2398
conflicts = wt.merge_from_branch(wt.branch, to_revision='E-id')
2399
self.assertEqual(0, conflicts)
2400
self.assertEqual('bing', wt.get_symlink_target('foo-id'))
2402
def test_symlink_this_changed_kind(self):
2403
self.requireFeature(features.SymlinkFeature)
2406
# B C B creates symlink foo => bar
2410
# D E D changes foo into a file, E has foo => bing
2412
# Mostly, this is trying to test that we don't try to os.readlink() on
2413
# a file, or when there is nothing there
2414
wt = self.make_branch_and_tree('path')
2416
self.addCleanup(wt.unlock)
2417
wt.commit('base', rev_id='A-id')
2418
os.symlink('bar', 'path/foo')
2419
wt.add(['foo'], ['foo-id'])
2420
wt.commit('add symlink foo => bar', rev_id='B-id')
2421
wt.set_last_revision('A-id')
2422
wt.branch.set_last_revision_info(1, 'A-id')
2424
wt.commit('C', rev_id='C-id')
2425
wt.merge_from_branch(wt.branch, 'B-id')
2426
self.assertEqual('bar', wt.get_symlink_target('foo-id'))
2427
os.remove('path/foo')
2428
# We have to change the link in E, or it won't try to do a comparison
2429
os.symlink('bing', 'path/foo')
2430
wt.commit('E merges C & B, overrides to bing', rev_id='E-id')
2431
wt.set_last_revision('B-id')
2432
wt.branch.set_last_revision_info(2, 'B-id')
2434
wt.merge_from_branch(wt.branch, 'C-id')
2435
os.remove('path/foo')
2436
self.build_tree_contents([('path/foo', 'file content\n')])
2437
# XXX: workaround, WT doesn't detect kind changes unless you do
2439
list(wt.iter_changes(wt.basis_tree()))
2440
wt.commit('D merges B & C, makes it a file', rev_id='D-id')
2442
merger = _mod_merge.Merger.from_revision_ids(None,
2444
merger.merge_type = _mod_merge.Merge3Merger
2445
merge_obj = merger.make_merger()
2446
entries = list(merge_obj._entries_lca())
2447
root_id = wt.path2id('')
2448
self.assertEqual([('foo-id', True,
2449
((None, [root_id, None]), root_id, root_id),
2450
((None, [u'foo', None]), u'foo', u'foo'),
2451
((None, [False, None]), False, False)),
2454
def test_symlink_all_wt(self):
2455
"""Check behavior if all trees are Working Trees."""
2456
self.requireFeature(features.SymlinkFeature)
2457
# The big issue is that entry.symlink_target is None for WorkingTrees.
2458
# So we need to make sure we handle that case correctly.
2461
# B C B relinks foo => baz
2463
# D E D & E have foo => baz
2465
# F F changes it to bing
2466
# Merging D & F should result in F cleanly overriding D, because D's
2467
# value actually comes from B
2469
wt = self.make_branch_and_tree('path')
2471
self.addCleanup(wt.unlock)
2472
os.symlink('bar', 'path/foo')
2473
wt.add(['foo'], ['foo-id'])
2474
wt.commit('add symlink', rev_id='A-id')
2475
os.remove('path/foo')
2476
os.symlink('baz', 'path/foo')
2477
wt.commit('foo => baz', rev_id='B-id')
2478
wt.set_last_revision('A-id')
2479
wt.branch.set_last_revision_info(1, 'A-id')
2481
wt.commit('C', rev_id='C-id')
2482
wt.merge_from_branch(wt.branch, 'B-id')
2483
self.assertEqual('baz', wt.get_symlink_target('foo-id'))
2484
wt.commit('E merges C & B', rev_id='E-id')
2485
os.remove('path/foo')
2486
os.symlink('bing', 'path/foo')
2487
wt.commit('F foo => bing', rev_id='F-id')
2488
wt.set_last_revision('B-id')
2489
wt.branch.set_last_revision_info(2, 'B-id')
2491
wt.merge_from_branch(wt.branch, 'C-id')
2492
wt.commit('D merges B & C', rev_id='D-id')
2493
wt_base = wt.bzrdir.sprout('base', 'A-id').open_workingtree()
2495
self.addCleanup(wt_base.unlock)
2496
wt_lca1 = wt.bzrdir.sprout('b-tree', 'B-id').open_workingtree()
2498
self.addCleanup(wt_lca1.unlock)
2499
wt_lca2 = wt.bzrdir.sprout('c-tree', 'C-id').open_workingtree()
2501
self.addCleanup(wt_lca2.unlock)
2502
wt_other = wt.bzrdir.sprout('other', 'F-id').open_workingtree()
2503
wt_other.lock_read()
2504
self.addCleanup(wt_other.unlock)
2505
merge_obj = _mod_merge.Merge3Merger(wt, wt, wt_base,
2506
wt_other, lca_trees=[wt_lca1, wt_lca2], do_merge=False)
2507
entries = list(merge_obj._entries_lca())
2508
root_id = wt.path2id('')
2509
self.assertEqual([('foo-id', True,
2510
((root_id, [root_id, root_id]), root_id, root_id),
2511
((u'foo', [u'foo', u'foo']), u'foo', u'foo'),
2512
((False, [False, False]), False, False)),
2515
def test_other_reverted_path_to_base(self):
2518
# B C Path at 'bar' in B
2525
builder = self.get_builder()
2526
builder.build_snapshot('A-id', None,
2527
[('add', (u'', 'a-root-id', 'directory', None)),
2528
('add', (u'foo', 'foo-id', 'file', 'a\nb\nc\n'))])
2529
builder.build_snapshot('C-id', ['A-id'], [])
2530
builder.build_snapshot('B-id', ['A-id'],
2531
[('rename', ('foo', 'bar'))])
2532
builder.build_snapshot('E-id', ['C-id', 'B-id'],
2533
[('rename', ('foo', 'bar'))]) # merge the rename
2534
builder.build_snapshot('F-id', ['E-id'],
2535
[('rename', ('bar', 'foo'))]) # Rename back to BASE
2536
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
2537
wt, conflicts = self.do_merge(builder, 'F-id')
2538
self.assertEqual(0, conflicts)
2539
self.assertEqual('foo', wt.id2path('foo-id'))
2541
def test_other_reverted_content_to_base(self):
2542
builder = self.get_builder()
2543
builder.build_snapshot('A-id', None,
2544
[('add', (u'', 'a-root-id', 'directory', None)),
2545
('add', (u'foo', 'foo-id', 'file', 'base content\n'))])
2546
builder.build_snapshot('C-id', ['A-id'], [])
2547
builder.build_snapshot('B-id', ['A-id'],
2548
[('modify', ('foo-id', 'B content\n'))])
2549
builder.build_snapshot('E-id', ['C-id', 'B-id'],
2550
[('modify', ('foo-id', 'B content\n'))]) # merge the content
2551
builder.build_snapshot('F-id', ['E-id'],
2552
[('modify', ('foo-id', 'base content\n'))]) # Revert back to BASE
2553
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
2554
wt, conflicts = self.do_merge(builder, 'F-id')
2555
self.assertEqual(0, conflicts)
2556
# TODO: We need to use the per-file graph to properly select a BASE
2557
# before this will work. Or at least use the LCA trees to find
2558
# the appropriate content base. (which is B, not A).
2559
self.assertEqual('base content\n', wt.get_file_text('foo-id'))
2561
def test_other_modified_content(self):
2562
builder = self.get_builder()
2563
builder.build_snapshot('A-id', None,
2564
[('add', (u'', 'a-root-id', 'directory', None)),
2565
('add', (u'foo', 'foo-id', 'file', 'base content\n'))])
2566
builder.build_snapshot('C-id', ['A-id'], [])
2567
builder.build_snapshot('B-id', ['A-id'],
2568
[('modify', ('foo-id', 'B content\n'))])
2569
builder.build_snapshot('E-id', ['C-id', 'B-id'],
2570
[('modify', ('foo-id', 'B content\n'))]) # merge the content
2571
builder.build_snapshot('F-id', ['E-id'],
2572
[('modify', ('foo-id', 'F content\n'))]) # Override B content
2573
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
2574
wt, conflicts = self.do_merge(builder, 'F-id')
2575
self.assertEqual(0, conflicts)
2576
self.assertEqual('F content\n', wt.get_file_text('foo-id'))
2578
def test_all_wt(self):
2579
"""Check behavior if all trees are Working Trees."""
2580
# The big issue is that entry.revision is None for WorkingTrees. (as is
2581
# entry.text_sha1, etc. So we need to make sure we handle that case
2583
# A Content of 'foo', path of 'a'
2585
# B C B modifies content, C renames 'a' => 'b'
2587
# D E E updates content, renames 'b' => 'c'
2588
builder = self.get_builder()
2589
builder.build_snapshot('A-id', None,
2590
[('add', (u'', 'a-root-id', 'directory', None)),
2591
('add', (u'a', 'a-id', 'file', 'base content\n')),
2592
('add', (u'foo', 'foo-id', 'file', 'base content\n'))])
2593
builder.build_snapshot('B-id', ['A-id'],
2594
[('modify', ('foo-id', 'B content\n'))])
2595
builder.build_snapshot('C-id', ['A-id'],
2596
[('rename', ('a', 'b'))])
2597
builder.build_snapshot('E-id', ['C-id', 'B-id'],
2598
[('rename', ('b', 'c')),
2599
('modify', ('foo-id', 'E content\n'))])
2600
builder.build_snapshot('D-id', ['B-id', 'C-id'],
2601
[('rename', ('a', 'b'))]) # merged change
2602
wt_this = self.get_wt_from_builder(builder)
2603
wt_base = wt_this.bzrdir.sprout('base', 'A-id').open_workingtree()
2605
self.addCleanup(wt_base.unlock)
2606
wt_lca1 = wt_this.bzrdir.sprout('b-tree', 'B-id').open_workingtree()
2608
self.addCleanup(wt_lca1.unlock)
2609
wt_lca2 = wt_this.bzrdir.sprout('c-tree', 'C-id').open_workingtree()
2611
self.addCleanup(wt_lca2.unlock)
2612
wt_other = wt_this.bzrdir.sprout('other', 'E-id').open_workingtree()
2613
wt_other.lock_read()
2614
self.addCleanup(wt_other.unlock)
2615
merge_obj = _mod_merge.Merge3Merger(wt_this, wt_this, wt_base,
2616
wt_other, lca_trees=[wt_lca1, wt_lca2], do_merge=False)
2617
entries = list(merge_obj._entries_lca())
2618
root_id = 'a-root-id'
2619
self.assertEqual([('a-id', False,
2620
((root_id, [root_id, root_id]), root_id, root_id),
2621
((u'a', [u'a', u'b']), u'c', u'b'),
2622
((False, [False, False]), False, False)),
2624
((root_id, [root_id, root_id]), root_id, root_id),
2625
((u'foo', [u'foo', u'foo']), u'foo', u'foo'),
2626
((False, [False, False]), False, False)),
2629
def test_nested_tree_unmodified(self):
2630
# Tested with a real WT, because BranchBuilder/MemoryTree don't handle
2632
wt = self.make_branch_and_tree('tree',
2633
format='dirstate-with-subtree')
2635
self.addCleanup(wt.unlock)
2636
sub_tree = self.make_branch_and_tree('tree/sub-tree',
2637
format='dirstate-with-subtree')
2638
wt.set_root_id('a-root-id')
2639
sub_tree.set_root_id('sub-tree-root')
2640
self.build_tree_contents([('tree/sub-tree/file', 'text1')])
2641
sub_tree.add('file')
2642
sub_tree.commit('foo', rev_id='sub-A-id')
2643
wt.add_reference(sub_tree)
2644
wt.commit('set text to 1', rev_id='A-id', recursive=None)
2645
# Now create a criss-cross merge in the parent, without modifying the
2647
wt.commit('B', rev_id='B-id', recursive=None)
2648
wt.set_last_revision('A-id')
2649
wt.branch.set_last_revision_info(1, 'A-id')
2650
wt.commit('C', rev_id='C-id', recursive=None)
2651
wt.merge_from_branch(wt.branch, to_revision='B-id')
2652
wt.commit('E', rev_id='E-id', recursive=None)
2653
wt.set_parent_ids(['B-id', 'C-id'])
2654
wt.branch.set_last_revision_info(2, 'B-id')
2655
wt.commit('D', rev_id='D-id', recursive=None)
2657
merger = _mod_merge.Merger.from_revision_ids(None,
2659
merger.merge_type = _mod_merge.Merge3Merger
2660
merge_obj = merger.make_merger()
2661
entries = list(merge_obj._entries_lca())
2662
self.assertEqual([], entries)
2664
def test_nested_tree_subtree_modified(self):
2665
# Tested with a real WT, because BranchBuilder/MemoryTree don't handle
2667
wt = self.make_branch_and_tree('tree',
2668
format='dirstate-with-subtree')
2670
self.addCleanup(wt.unlock)
2671
sub_tree = self.make_branch_and_tree('tree/sub',
2672
format='dirstate-with-subtree')
2673
wt.set_root_id('a-root-id')
2674
sub_tree.set_root_id('sub-tree-root')
2675
self.build_tree_contents([('tree/sub/file', 'text1')])
2676
sub_tree.add('file')
2677
sub_tree.commit('foo', rev_id='sub-A-id')
2678
wt.add_reference(sub_tree)
2679
wt.commit('set text to 1', rev_id='A-id', recursive=None)
2680
# Now create a criss-cross merge in the parent, without modifying the
2682
wt.commit('B', rev_id='B-id', recursive=None)
2683
wt.set_last_revision('A-id')
2684
wt.branch.set_last_revision_info(1, 'A-id')
2685
wt.commit('C', rev_id='C-id', recursive=None)
2686
wt.merge_from_branch(wt.branch, to_revision='B-id')
2687
self.build_tree_contents([('tree/sub/file', 'text2')])
2688
sub_tree.commit('modify contents', rev_id='sub-B-id')
2689
wt.commit('E', rev_id='E-id', recursive=None)
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
# Nothing interesting about this sub-tree, because content changes are
2700
# computed at a higher level
2701
self.assertEqual([], entries)
2703
def test_nested_tree_subtree_renamed(self):
2704
# Tested with a real WT, because BranchBuilder/MemoryTree don't handle
2706
wt = self.make_branch_and_tree('tree',
2707
format='dirstate-with-subtree')
2709
self.addCleanup(wt.unlock)
2710
sub_tree = self.make_branch_and_tree('tree/sub',
2711
format='dirstate-with-subtree')
2712
wt.set_root_id('a-root-id')
2713
sub_tree.set_root_id('sub-tree-root')
2714
self.build_tree_contents([('tree/sub/file', 'text1')])
2715
sub_tree.add('file')
2716
sub_tree.commit('foo', rev_id='sub-A-id')
2717
wt.add_reference(sub_tree)
2718
wt.commit('set text to 1', rev_id='A-id', recursive=None)
2719
# Now create a criss-cross merge in the parent, without modifying the
2721
wt.commit('B', rev_id='B-id', recursive=None)
2722
wt.set_last_revision('A-id')
2723
wt.branch.set_last_revision_info(1, 'A-id')
2724
wt.commit('C', rev_id='C-id', recursive=None)
2725
wt.merge_from_branch(wt.branch, to_revision='B-id')
2726
wt.rename_one('sub', 'alt_sub')
2727
wt.commit('E', rev_id='E-id', recursive=None)
2728
wt.set_last_revision('B-id')
2730
wt.set_parent_ids(['B-id', 'C-id'])
2731
wt.branch.set_last_revision_info(2, 'B-id')
2732
wt.commit('D', rev_id='D-id', recursive=None)
2734
merger = _mod_merge.Merger.from_revision_ids(None,
2736
merger.merge_type = _mod_merge.Merge3Merger
2737
merge_obj = merger.make_merger()
2738
entries = list(merge_obj._entries_lca())
2739
root_id = 'a-root-id'
2740
self.assertEqual([('sub-tree-root', False,
2741
((root_id, [root_id, root_id]), root_id, root_id),
2742
((u'sub', [u'sub', u'sub']), u'alt_sub', u'sub'),
2743
((False, [False, False]), False, False)),
2746
def test_nested_tree_subtree_renamed_and_modified(self):
2747
# Tested with a real WT, because BranchBuilder/MemoryTree don't handle
2749
wt = self.make_branch_and_tree('tree',
2750
format='dirstate-with-subtree')
2752
self.addCleanup(wt.unlock)
2753
sub_tree = self.make_branch_and_tree('tree/sub',
2754
format='dirstate-with-subtree')
2755
wt.set_root_id('a-root-id')
2756
sub_tree.set_root_id('sub-tree-root')
2757
self.build_tree_contents([('tree/sub/file', 'text1')])
2758
sub_tree.add('file')
2759
sub_tree.commit('foo', rev_id='sub-A-id')
2760
wt.add_reference(sub_tree)
2761
wt.commit('set text to 1', rev_id='A-id', recursive=None)
2762
# Now create a criss-cross merge in the parent, without modifying the
2764
wt.commit('B', rev_id='B-id', recursive=None)
2765
wt.set_last_revision('A-id')
2766
wt.branch.set_last_revision_info(1, 'A-id')
2767
wt.commit('C', rev_id='C-id', recursive=None)
2768
wt.merge_from_branch(wt.branch, to_revision='B-id')
2769
self.build_tree_contents([('tree/sub/file', 'text2')])
2770
sub_tree.commit('modify contents', rev_id='sub-B-id')
2771
wt.rename_one('sub', 'alt_sub')
2772
wt.commit('E', rev_id='E-id', recursive=None)
2773
wt.set_last_revision('B-id')
2775
wt.set_parent_ids(['B-id', 'C-id'])
2776
wt.branch.set_last_revision_info(2, 'B-id')
2777
wt.commit('D', rev_id='D-id', recursive=None)
2779
merger = _mod_merge.Merger.from_revision_ids(None,
2781
merger.merge_type = _mod_merge.Merge3Merger
2782
merge_obj = merger.make_merger()
2783
entries = list(merge_obj._entries_lca())
2784
root_id = 'a-root-id'
2785
self.assertEqual([('sub-tree-root', False,
2786
((root_id, [root_id, root_id]), root_id, root_id),
2787
((u'sub', [u'sub', u'sub']), u'alt_sub', u'sub'),
2788
((False, [False, False]), False, False)),
2792
class TestLCAMultiWay(tests.TestCase):
2794
def assertLCAMultiWay(self, expected, base, lcas, other, this,
2795
allow_overriding_lca=True):
2796
self.assertEqual(expected, _mod_merge.Merge3Merger._lca_multi_way(
2797
(base, lcas), other, this,
2798
allow_overriding_lca=allow_overriding_lca))
2800
def test_other_equal_equal_lcas(self):
2801
"""Test when OTHER=LCA and all LCAs are identical."""
2802
self.assertLCAMultiWay('this',
2803
'bval', ['bval', 'bval'], 'bval', 'bval')
2804
self.assertLCAMultiWay('this',
2805
'bval', ['lcaval', 'lcaval'], 'lcaval', 'bval')
2806
self.assertLCAMultiWay('this',
2807
'bval', ['lcaval', 'lcaval', 'lcaval'], 'lcaval', 'bval')
2808
self.assertLCAMultiWay('this',
2809
'bval', ['lcaval', 'lcaval', 'lcaval'], 'lcaval', 'tval')
2810
self.assertLCAMultiWay('this',
2811
'bval', ['lcaval', 'lcaval', 'lcaval'], 'lcaval', None)
2813
def test_other_equal_this(self):
2814
"""Test when other and this are identical."""
2815
self.assertLCAMultiWay('this',
2816
'bval', ['bval', 'bval'], 'oval', 'oval')
2817
self.assertLCAMultiWay('this',
2818
'bval', ['lcaval', 'lcaval'], 'oval', 'oval')
2819
self.assertLCAMultiWay('this',
2820
'bval', ['cval', 'dval'], 'oval', 'oval')
2821
self.assertLCAMultiWay('this',
2822
'bval', [None, 'lcaval'], 'oval', 'oval')
2823
self.assertLCAMultiWay('this',
2824
None, [None, 'lcaval'], 'oval', 'oval')
2825
self.assertLCAMultiWay('this',
2826
None, ['lcaval', 'lcaval'], 'oval', 'oval')
2827
self.assertLCAMultiWay('this',
2828
None, ['cval', 'dval'], 'oval', 'oval')
2829
self.assertLCAMultiWay('this',
2830
None, ['cval', 'dval'], None, None)
2831
self.assertLCAMultiWay('this',
2832
None, ['cval', 'dval', 'eval', 'fval'], 'oval', 'oval')
2834
def test_no_lcas(self):
2835
self.assertLCAMultiWay('this',
2836
'bval', [], 'bval', 'tval')
2837
self.assertLCAMultiWay('other',
2838
'bval', [], 'oval', 'bval')
2839
self.assertLCAMultiWay('conflict',
2840
'bval', [], 'oval', 'tval')
2841
self.assertLCAMultiWay('this',
2842
'bval', [], 'oval', 'oval')
2844
def test_lca_supersedes_other_lca(self):
2845
"""If one lca == base, the other lca takes precedence"""
2846
self.assertLCAMultiWay('this',
2847
'bval', ['bval', 'lcaval'], 'lcaval', 'tval')
2848
self.assertLCAMultiWay('this',
2849
'bval', ['bval', 'lcaval'], 'lcaval', 'bval')
2850
# This is actually considered a 'revert' because the 'lcaval' in LCAS
2851
# supersedes the BASE val (in the other LCA) but then OTHER reverts it
2853
self.assertLCAMultiWay('other',
2854
'bval', ['bval', 'lcaval'], 'bval', 'lcaval')
2855
self.assertLCAMultiWay('conflict',
2856
'bval', ['bval', 'lcaval'], 'bval', 'tval')
2858
def test_other_and_this_pick_different_lca(self):
2859
# OTHER and THIS resolve the lca conflict in different ways
2860
self.assertLCAMultiWay('conflict',
2861
'bval', ['lca1val', 'lca2val'], 'lca1val', 'lca2val')
2862
self.assertLCAMultiWay('conflict',
2863
'bval', ['lca1val', 'lca2val', 'lca3val'], 'lca1val', 'lca2val')
2864
self.assertLCAMultiWay('conflict',
2865
'bval', ['lca1val', 'lca2val', 'bval'], 'lca1val', 'lca2val')
2867
def test_other_in_lca(self):
2868
# OTHER takes a value of one of the LCAs, THIS takes a new value, which
2869
# theoretically supersedes both LCA values and 'wins'
2870
self.assertLCAMultiWay('this',
2871
'bval', ['lca1val', 'lca2val'], 'lca1val', 'newval')
2872
self.assertLCAMultiWay('this',
2873
'bval', ['lca1val', 'lca2val', 'lca3val'], 'lca1val', 'newval')
2874
self.assertLCAMultiWay('conflict',
2875
'bval', ['lca1val', 'lca2val'], 'lca1val', 'newval',
2876
allow_overriding_lca=False)
2877
self.assertLCAMultiWay('conflict',
2878
'bval', ['lca1val', 'lca2val', 'lca3val'], 'lca1val', 'newval',
2879
allow_overriding_lca=False)
2880
# THIS reverted back to BASE, but that is an explicit supersede of all
2882
self.assertLCAMultiWay('this',
2883
'bval', ['lca1val', 'lca2val', 'lca3val'], 'lca1val', 'bval')
2884
self.assertLCAMultiWay('this',
2885
'bval', ['lca1val', 'lca2val', 'bval'], 'lca1val', 'bval')
2886
self.assertLCAMultiWay('conflict',
2887
'bval', ['lca1val', 'lca2val', 'lca3val'], 'lca1val', 'bval',
2888
allow_overriding_lca=False)
2889
self.assertLCAMultiWay('conflict',
2890
'bval', ['lca1val', 'lca2val', 'bval'], 'lca1val', 'bval',
2891
allow_overriding_lca=False)
2893
def test_this_in_lca(self):
2894
# THIS takes a value of one of the LCAs, OTHER takes a new value, which
2895
# theoretically supersedes both LCA values and 'wins'
2896
self.assertLCAMultiWay('other',
2897
'bval', ['lca1val', 'lca2val'], 'oval', 'lca1val')
2898
self.assertLCAMultiWay('other',
2899
'bval', ['lca1val', 'lca2val'], 'oval', 'lca2val')
2900
self.assertLCAMultiWay('conflict',
2901
'bval', ['lca1val', 'lca2val'], 'oval', 'lca1val',
2902
allow_overriding_lca=False)
2903
self.assertLCAMultiWay('conflict',
2904
'bval', ['lca1val', 'lca2val'], 'oval', 'lca2val',
2905
allow_overriding_lca=False)
2906
# OTHER reverted back to BASE, but that is an explicit supersede of all
2908
self.assertLCAMultiWay('other',
2909
'bval', ['lca1val', 'lca2val', 'lca3val'], 'bval', 'lca3val')
2910
self.assertLCAMultiWay('conflict',
2911
'bval', ['lca1val', 'lca2val', 'lca3val'], 'bval', 'lca3val',
2912
allow_overriding_lca=False)
2914
def test_all_differ(self):
2915
self.assertLCAMultiWay('conflict',
2916
'bval', ['lca1val', 'lca2val'], 'oval', 'tval')
2917
self.assertLCAMultiWay('conflict',
2918
'bval', ['lca1val', 'lca2val', 'lca2val'], 'oval', 'tval')
2919
self.assertLCAMultiWay('conflict',
2920
'bval', ['lca1val', 'lca2val', 'lca3val'], 'oval', 'tval')
2923
class TestConfigurableFileMerger(tests.TestCaseWithTransport):
2926
super(TestConfigurableFileMerger, self).setUp()
2929
def get_merger_factory(self):
2930
# Allows the inner methods to access the test attributes
2933
class FooMerger(_mod_merge.ConfigurableFileMerger):
2935
default_files = ['bar']
2937
def merge_text(self, params):
2938
calls.append('merge_text')
2939
return ('not_applicable', None)
2941
def factory(merger):
2942
result = FooMerger(merger)
2943
# Make sure we start with a clean slate
2944
self.assertEqual(None, result.affected_files)
2945
# Track the original merger
2946
self.merger = result
2951
def _install_hook(self, factory):
2952
_mod_merge.Merger.hooks.install_named_hook('merge_file_content',
2953
factory, 'test factory')
2955
def make_builder(self):
2956
builder = test_merge_core.MergeBuilder(self.test_base_dir)
2957
self.addCleanup(builder.cleanup)
2960
def make_text_conflict(self, file_name='bar'):
2961
factory = self.get_merger_factory()
2962
self._install_hook(factory)
2963
builder = self.make_builder()
2964
builder.add_file('bar-id', builder.tree_root, file_name, 'text1', True)
2965
builder.change_contents('bar-id', other='text4', this='text3')
2968
def make_kind_change(self):
2969
factory = self.get_merger_factory()
2970
self._install_hook(factory)
2971
builder = self.make_builder()
2972
builder.add_file('bar-id', builder.tree_root, 'bar', 'text1', True,
2974
builder.add_dir('bar-dir', builder.tree_root, 'bar-id',
2975
base=False, other=False)
2978
def test_uses_this_branch(self):
2979
builder = self.make_text_conflict()
2980
tt = builder.make_preview_transform()
2981
self.addCleanup(tt.finalize)
2983
def test_affected_files_cached(self):
2984
"""Ensures that the config variable is cached"""
2985
builder = self.make_text_conflict()
2986
conflicts = builder.merge()
2987
# The hook should set the variable
2988
self.assertEqual(['bar'], self.merger.affected_files)
2989
self.assertEqual(1, len(conflicts))
2991
def test_hook_called_for_text_conflicts(self):
2992
builder = self.make_text_conflict()
2993
conflicts = builder.merge()
2994
# The hook should call the merge_text() method
2995
self.assertEqual(['merge_text'], self.calls)
2997
def test_hook_not_called_for_kind_change(self):
2998
builder = self.make_kind_change()
2999
conflicts = builder.merge()
3000
# The hook should not call the merge_text() method
3001
self.assertEqual([], self.calls)
3003
def test_hook_not_called_for_other_files(self):
3004
builder = self.make_text_conflict('foobar')
3005
conflicts = builder.merge()
3006
# The hook should not call the merge_text() method
3007
self.assertEqual([], self.calls)
3010
class TestMergeIntoBase(tests.TestCaseWithTransport):
3012
def setup_simple_branch(self, relpath, shape=None, root_id=None):
3013
"""One commit, containing tree specified by optional shape.
3015
Default is empty tree (just root entry).
3018
root_id = '%s-root-id' % (relpath,)
3019
wt = self.make_branch_and_tree(relpath)
3020
wt.set_root_id(root_id)
3021
if shape is not None:
3022
adjusted_shape = [relpath + '/' + elem for elem in shape]
3023
self.build_tree(adjusted_shape)
3024
ids = ['%s-%s-id' % (relpath, basename(elem.rstrip('/')))
3026
wt.add(shape, ids=ids)
3027
rev_id = 'r1-%s' % (relpath,)
3028
wt.commit("Initial commit of %s" % (relpath,), rev_id=rev_id)
3029
self.assertEqual(root_id, wt.path2id(''))
3032
def setup_two_branches(self, custom_root_ids=True):
3033
"""Setup 2 branches, one will be a library, the other a project."""
3037
root_id = inventory.ROOT_ID
3038
project_wt = self.setup_simple_branch(
3039
'project', ['README', 'dir/', 'dir/file.c'],
3041
lib_wt = self.setup_simple_branch(
3042
'lib1', ['README', 'Makefile', 'foo.c'], root_id)
3044
return project_wt, lib_wt
3046
def do_merge_into(self, location, merge_as):
3047
"""Helper for using MergeIntoMerger.
3049
:param location: location of directory to merge from, either the
3050
location of a branch or of a path inside a branch.
3051
:param merge_as: the path in a tree to add the new directory as.
3052
:returns: the conflicts from 'do_merge'.
3054
operation = cleanup.OperationWithCleanups(self._merge_into)
3055
return operation.run(location, merge_as)
3057
def _merge_into(self, op, location, merge_as):
3058
# Open and lock the various tree and branch objects
3059
wt, subdir_relpath = WorkingTree.open_containing(merge_as)
3060
op.add_cleanup(wt.lock_write().unlock)
3061
branch_to_merge, subdir_to_merge = _mod_branch.Branch.open_containing(
3063
op.add_cleanup(branch_to_merge.lock_read().unlock)
3064
other_tree = branch_to_merge.basis_tree()
3065
op.add_cleanup(other_tree.lock_read().unlock)
3067
merger = _mod_merge.MergeIntoMerger(this_tree=wt, other_tree=other_tree,
3068
other_branch=branch_to_merge, target_subdir=subdir_relpath,
3069
source_subpath=subdir_to_merge)
3070
merger.set_base_revision(_mod_revision.NULL_REVISION, branch_to_merge)
3071
conflicts = merger.do_merge()
3072
merger.set_pending()
3075
def assertTreeEntriesEqual(self, expected_entries, tree):
3076
"""Assert that 'tree' contains the expected inventory entries.
3078
:param expected_entries: sequence of (path, file-id) pairs.
3080
files = [(path, ie.file_id) for path, ie in tree.iter_entries_by_dir()]
3081
self.assertEqual(expected_entries, files)
3084
class TestMergeInto(TestMergeIntoBase):
3086
def test_newdir_with_unique_roots(self):
3087
"""Merge a branch with a unique root into a new directory."""
3088
project_wt, lib_wt = self.setup_two_branches()
3089
self.do_merge_into('lib1', 'project/lib1')
3090
project_wt.lock_read()
3091
self.addCleanup(project_wt.unlock)
3092
# The r1-lib1 revision should be merged into this one
3093
self.assertEqual(['r1-project', 'r1-lib1'], project_wt.get_parent_ids())
3094
self.assertTreeEntriesEqual(
3095
[('', 'project-root-id'),
3096
('README', 'project-README-id'),
3097
('dir', 'project-dir-id'),
3098
('lib1', 'lib1-root-id'),
3099
('dir/file.c', 'project-file.c-id'),
3100
('lib1/Makefile', 'lib1-Makefile-id'),
3101
('lib1/README', 'lib1-README-id'),
3102
('lib1/foo.c', 'lib1-foo.c-id'),
3105
def test_subdir(self):
3106
"""Merge a branch into a subdirectory of an existing directory."""
3107
project_wt, lib_wt = self.setup_two_branches()
3108
self.do_merge_into('lib1', 'project/dir/lib1')
3109
project_wt.lock_read()
3110
self.addCleanup(project_wt.unlock)
3111
# The r1-lib1 revision should be merged into this one
3112
self.assertEqual(['r1-project', 'r1-lib1'], project_wt.get_parent_ids())
3113
self.assertTreeEntriesEqual(
3114
[('', 'project-root-id'),
3115
('README', 'project-README-id'),
3116
('dir', 'project-dir-id'),
3117
('dir/file.c', 'project-file.c-id'),
3118
('dir/lib1', 'lib1-root-id'),
3119
('dir/lib1/Makefile', 'lib1-Makefile-id'),
3120
('dir/lib1/README', 'lib1-README-id'),
3121
('dir/lib1/foo.c', 'lib1-foo.c-id'),
3124
def test_newdir_with_repeat_roots(self):
3125
"""If the file-id of the dir to be merged already exists a new ID will
3126
be allocated to let the merge happen.
3128
project_wt, lib_wt = self.setup_two_branches(custom_root_ids=False)
3129
root_id = project_wt.path2id('')
3130
self.do_merge_into('lib1', 'project/lib1')
3131
project_wt.lock_read()
3132
self.addCleanup(project_wt.unlock)
3133
# The r1-lib1 revision should be merged into this one
3134
self.assertEqual(['r1-project', 'r1-lib1'], project_wt.get_parent_ids())
3135
new_lib1_id = project_wt.path2id('lib1')
3136
self.assertNotEqual(None, new_lib1_id)
3137
self.assertTreeEntriesEqual(
3139
('README', 'project-README-id'),
3140
('dir', 'project-dir-id'),
3141
('lib1', new_lib1_id),
3142
('dir/file.c', 'project-file.c-id'),
3143
('lib1/Makefile', 'lib1-Makefile-id'),
3144
('lib1/README', 'lib1-README-id'),
3145
('lib1/foo.c', 'lib1-foo.c-id'),
3148
def test_name_conflict(self):
3149
"""When the target directory name already exists a conflict is
3150
generated and the original directory is renamed to foo.moved.
3152
dest_wt = self.setup_simple_branch('dest', ['dir/', 'dir/file.txt'])
3153
src_wt = self.setup_simple_branch('src', ['README'])
3154
conflicts = self.do_merge_into('src', 'dest/dir')
3155
self.assertEqual(1, conflicts)
3157
self.addCleanup(dest_wt.unlock)
3158
# The r1-lib1 revision should be merged into this one
3159
self.assertEqual(['r1-dest', 'r1-src'], dest_wt.get_parent_ids())
3160
self.assertTreeEntriesEqual(
3161
[('', 'dest-root-id'),
3162
('dir', 'src-root-id'),
3163
('dir.moved', 'dest-dir-id'),
3164
('dir/README', 'src-README-id'),
3165
('dir.moved/file.txt', 'dest-file.txt-id'),
3168
def test_file_id_conflict(self):
3169
"""A conflict is generated if the merge-into adds a file (or other
3170
inventory entry) with a file-id that already exists in the target tree.
3172
dest_wt = self.setup_simple_branch('dest', ['file.txt'])
3173
# Make a second tree with a file-id that will clash with file.txt in
3175
src_wt = self.make_branch_and_tree('src')
3176
self.build_tree(['src/README'])
3177
src_wt.add(['README'], ids=['dest-file.txt-id'])
3178
src_wt.commit("Rev 1 of src.", rev_id='r1-src')
3179
conflicts = self.do_merge_into('src', 'dest/dir')
3180
# This is an edge case that shouldn't happen to users very often. So
3181
# we don't care really about the exact presentation of the conflict,
3182
# just that there is one.
3183
self.assertEqual(1, conflicts)
3185
def test_only_subdir(self):
3186
"""When the location points to just part of a tree, merge just that
3189
dest_wt = self.setup_simple_branch('dest')
3190
src_wt = self.setup_simple_branch(
3191
'src', ['hello.txt', 'dir/', 'dir/foo.c'])
3192
conflicts = self.do_merge_into('src/dir', 'dest/dir')
3194
self.addCleanup(dest_wt.unlock)
3195
# The r1-lib1 revision should NOT be merged into this one (this is a
3197
self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3198
self.assertTreeEntriesEqual(
3199
[('', 'dest-root-id'),
3200
('dir', 'src-dir-id'),
3201
('dir/foo.c', 'src-foo.c-id'),
3204
def test_only_file(self):
3205
"""An edge case: merge just one file, not a whole dir."""
3206
dest_wt = self.setup_simple_branch('dest')
3207
two_file_wt = self.setup_simple_branch(
3208
'two-file', ['file1.txt', 'file2.txt'])
3209
conflicts = self.do_merge_into('two-file/file1.txt', 'dest/file1.txt')
3211
self.addCleanup(dest_wt.unlock)
3212
# The r1-lib1 revision should NOT be merged into this one
3213
self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3214
self.assertTreeEntriesEqual(
3215
[('', 'dest-root-id'), ('file1.txt', 'two-file-file1.txt-id')],
3218
def test_no_such_source_path(self):
3219
"""PathNotInTree is raised if the specified path in the source tree
3222
dest_wt = self.setup_simple_branch('dest')
3223
two_file_wt = self.setup_simple_branch('src', ['dir/'])
3224
self.assertRaises(_mod_merge.PathNotInTree, self.do_merge_into,
3225
'src/no-such-dir', 'dest/foo')
3227
self.addCleanup(dest_wt.unlock)
3228
# The dest tree is unmodified.
3229
self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3230
self.assertTreeEntriesEqual([('', 'dest-root-id')], dest_wt)
3232
def test_no_such_target_path(self):
3233
"""PathNotInTree is also raised if the specified path in the target
3234
tree does not exist.
3236
dest_wt = self.setup_simple_branch('dest')
3237
two_file_wt = self.setup_simple_branch('src', ['file.txt'])
3238
self.assertRaises(_mod_merge.PathNotInTree, self.do_merge_into,
3239
'src', 'dest/no-such-dir/foo')
3241
self.addCleanup(dest_wt.unlock)
3242
# The dest tree is unmodified.
3243
self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3244
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