193
complex_shortcut = {'a':[NULL_REVISION], 'b':['a'], 'c':['b'], 'd':['c'],
194
'e':['d'], 'f':['d'], 'g':['f'], 'h':['f'],
195
'i':['e', 'g'], 'j':['g'], 'k':['j'],
196
'l':['k'], 'm':['i', 'l'], 'n':['l', 'h']}
236
complex_shortcut2 = {'a':[NULL_REVISION], 'b':['a'], 'c':['b'], 'd':['c'],
237
'e':['d'], 'f':['e'], 'g':['f'], 'h':['d'], 'i':['g'],
238
'j':['h'], 'k':['h', 'i'], 'l':['k'], 'm':['l'], 'n':['m'],
239
'o':['n'], 'p':['o'], 'q':['p'], 'r':['q'], 's':['r'],
240
't':['i', 's'], 'u':['s', 'j'],
195
complex_shortcut = {'d':[NULL_REVISION],
196
'x':['d'], 'y':['x'],
197
'e':['y'], 'f':['d'], 'g':['f', 'i'], 'h':['f'],
198
'i':['e'], 'j':['g'], 'k':['j'],
199
'l':['k'], 'm':['i', 's'], 'n':['s', 'h'],
200
'o':['l'], 'p':['o'], 'q':['p'],
201
'r':['q'], 's':['r'],
243
# Graph where different walkers will race to find the common and uncommon
286
# x is found to be common right away, but is the start of a long series of
288
# o is actually common, but the i-j shortcut makes it look like it is actually
289
# unique to j at first, you have to traverse all of x->o to find it.
290
# q,m gives the walker from j a common point to stop searching, as does p,f.
291
# k-n exists so that the second pass still has nodes that are worth searching,
292
# rather than instantly cancelling the extra walker.
294
racing_shortcuts = {'a':[NULL_REVISION], 'b':['a'], 'c':['b'], 'd':['c'],
295
'e':['d'], 'f':['e'], 'g':['f'], 'h':['g'], 'i':['h', 'o'], 'j':['i', 'y'],
296
'k':['d'], 'l':['k'], 'm':['l'], 'n':['m'], 'o':['n', 'g'], 'p':['f'],
297
'q':['p', 'm'], 'r':['o'], 's':['r'], 't':['s'], 'u':['t'], 'v':['u'],
298
'w':['v'], 'x':['w'], 'y':['x'], 'z':['x', 'q']}
301
# A graph with multiple nodes unique to one side.
340
multiple_interesting_unique = {'a':[NULL_REVISION], 'b':['a'], 'c':['b'],
341
'd':['c'], 'e':['d'], 'f':['d'], 'g':['e'], 'h':['e'], 'i':['f'],
342
'j':['g'], 'k':['g'], 'l':['h'], 'm':['i'], 'n':['k', 'l'],
343
'o':['m'], 'p':['m', 'l'], 'q':['n', 'o'], 'r':['q'], 's':['r'],
344
't':['s'], 'u':['t'], 'v':['u'], 'w':['v'], 'x':['w'],
345
'y':['j', 'x'], 'z':['x', 'p']}
348
204
# Shortcut with extra root
349
205
# We have a long history shortcut, and an extra root, which is why we can't
350
206
# stop searchers based on seeing NULL_REVISION
1215
968
self.assertRaises(StopIteration, search.next)
1216
969
self.assertEqual(set(['head', 'ghost', NULL_REVISION]), search.seen)
1217
970
result = search.get_result()
1218
self.assertEqual(('search', set(['ghost', 'head']), set(['ghost']), 2),
971
self.assertEqual((set(['ghost', 'head']), set(['ghost']), 2),
1219
972
result.get_recipe())
1220
973
self.assertEqual(set(['head', NULL_REVISION]), result.get_keys())
1223
class TestFindUniqueAncestors(TestGraphBase):
1225
def assertFindUniqueAncestors(self, graph, expected, node, common):
1226
actual = graph.find_unique_ancestors(node, common)
1227
self.assertEqual(expected, sorted(actual))
1229
def test_empty_set(self):
1230
graph = self.make_graph(ancestry_1)
1231
self.assertFindUniqueAncestors(graph, [], 'rev1', ['rev1'])
1232
self.assertFindUniqueAncestors(graph, [], 'rev2b', ['rev2b'])
1233
self.assertFindUniqueAncestors(graph, [], 'rev3', ['rev1', 'rev3'])
1235
def test_single_node(self):
1236
graph = self.make_graph(ancestry_1)
1237
self.assertFindUniqueAncestors(graph, ['rev2a'], 'rev2a', ['rev1'])
1238
self.assertFindUniqueAncestors(graph, ['rev2b'], 'rev2b', ['rev1'])
1239
self.assertFindUniqueAncestors(graph, ['rev3'], 'rev3', ['rev2a'])
1241
def test_minimal_ancestry(self):
1242
graph = self.make_breaking_graph(extended_history_shortcut,
1243
[NULL_REVISION, 'a', 'b'])
1244
self.assertFindUniqueAncestors(graph, ['e'], 'e', ['d'])
1246
graph = self.make_breaking_graph(extended_history_shortcut,
1248
self.assertFindUniqueAncestors(graph, ['f'], 'f', ['a', 'd'])
1250
graph = self.make_breaking_graph(complex_shortcut,
1252
self.assertFindUniqueAncestors(graph, ['h'], 'h', ['i'])
1253
self.assertFindUniqueAncestors(graph, ['e', 'g', 'i'], 'i', ['h'])
1254
self.assertFindUniqueAncestors(graph, ['h'], 'h', ['g'])
1255
self.assertFindUniqueAncestors(graph, ['h'], 'h', ['j'])
1257
def test_in_ancestry(self):
1258
graph = self.make_graph(ancestry_1)
1259
self.assertFindUniqueAncestors(graph, [], 'rev1', ['rev3'])
1260
self.assertFindUniqueAncestors(graph, [], 'rev2b', ['rev4'])
1262
def test_multiple_revisions(self):
1263
graph = self.make_graph(ancestry_1)
1264
self.assertFindUniqueAncestors(graph,
1265
['rev4'], 'rev4', ['rev3', 'rev2b'])
1266
self.assertFindUniqueAncestors(graph,
1267
['rev2a', 'rev3', 'rev4'], 'rev4', ['rev2b'])
1269
def test_complex_shortcut(self):
1270
graph = self.make_graph(complex_shortcut)
1271
self.assertFindUniqueAncestors(graph,
1272
['h', 'n'], 'n', ['m'])
1273
self.assertFindUniqueAncestors(graph,
1274
['e', 'i', 'm'], 'm', ['n'])
1276
def test_complex_shortcut2(self):
1277
graph = self.make_graph(complex_shortcut2)
1278
self.assertFindUniqueAncestors(graph,
1279
['j', 'u'], 'u', ['t'])
1280
self.assertFindUniqueAncestors(graph,
1283
def test_multiple_interesting_unique(self):
1284
graph = self.make_graph(multiple_interesting_unique)
1285
self.assertFindUniqueAncestors(graph,
1286
['j', 'y'], 'y', ['z'])
1287
self.assertFindUniqueAncestors(graph,
1288
['p', 'z'], 'z', ['y'])
1290
def test_racing_shortcuts(self):
1291
graph = self.make_graph(racing_shortcuts)
1292
self.assertFindUniqueAncestors(graph,
1293
['p', 'q', 'z'], 'z', ['y'])
1294
self.assertFindUniqueAncestors(graph,
1295
['h', 'i', 'j', 'y'], 'j', ['z'])
1298
class TestGraphFindDistanceToNull(TestGraphBase):
1299
"""Test an api that should be able to compute a revno"""
1301
def assertFindDistance(self, revno, graph, target_id, known_ids):
1302
"""Assert the output of Graph.find_distance_to_null()"""
1303
actual = graph.find_distance_to_null(target_id, known_ids)
1304
self.assertEqual(revno, actual)
1306
def test_nothing_known(self):
1307
graph = self.make_graph(ancestry_1)
1308
self.assertFindDistance(0, graph, NULL_REVISION, [])
1309
self.assertFindDistance(1, graph, 'rev1', [])
1310
self.assertFindDistance(2, graph, 'rev2a', [])
1311
self.assertFindDistance(2, graph, 'rev2b', [])
1312
self.assertFindDistance(3, graph, 'rev3', [])
1313
self.assertFindDistance(4, graph, 'rev4', [])
1315
def test_rev_is_ghost(self):
1316
graph = self.make_graph(ancestry_1)
1317
e = self.assertRaises(errors.GhostRevisionsHaveNoRevno,
1318
graph.find_distance_to_null, 'rev_missing', [])
1319
self.assertEqual('rev_missing', e.revision_id)
1320
self.assertEqual('rev_missing', e.ghost_revision_id)
1322
def test_ancestor_is_ghost(self):
1323
graph = self.make_graph({'rev':['parent']})
1324
e = self.assertRaises(errors.GhostRevisionsHaveNoRevno,
1325
graph.find_distance_to_null, 'rev', [])
1326
self.assertEqual('rev', e.revision_id)
1327
self.assertEqual('parent', e.ghost_revision_id)
1329
def test_known_in_ancestry(self):
1330
graph = self.make_graph(ancestry_1)
1331
self.assertFindDistance(2, graph, 'rev2a', [('rev1', 1)])
1332
self.assertFindDistance(3, graph, 'rev3', [('rev2a', 2)])
1334
def test_known_in_ancestry_limits(self):
1335
graph = self.make_breaking_graph(ancestry_1, ['rev1'])
1336
self.assertFindDistance(4, graph, 'rev4', [('rev3', 3)])
1338
def test_target_is_ancestor(self):
1339
graph = self.make_graph(ancestry_1)
1340
self.assertFindDistance(2, graph, 'rev2a', [('rev3', 3)])
1342
def test_target_is_ancestor_limits(self):
1343
"""We shouldn't search all history if we run into ourselves"""
1344
graph = self.make_breaking_graph(ancestry_1, ['rev1'])
1345
self.assertFindDistance(3, graph, 'rev3', [('rev4', 4)])
1347
def test_target_parallel_to_known_limits(self):
1348
# Even though the known revision isn't part of the other ancestry, they
1349
# eventually converge
1350
graph = self.make_breaking_graph(with_tail, ['a'])
1351
self.assertFindDistance(6, graph, 'f', [('g', 6)])
1352
self.assertFindDistance(7, graph, 'h', [('g', 6)])
1353
self.assertFindDistance(8, graph, 'i', [('g', 6)])
1354
self.assertFindDistance(6, graph, 'g', [('i', 8)])
1357
class TestFindMergeOrder(TestGraphBase):
1359
def assertMergeOrder(self, expected, graph, tip, base_revisions):
1360
self.assertEqual(expected, graph.find_merge_order(tip, base_revisions))
1362
def test_parents(self):
1363
graph = self.make_graph(ancestry_1)
1364
self.assertMergeOrder(['rev3', 'rev2b'], graph, 'rev4',
1366
self.assertMergeOrder(['rev3', 'rev2b'], graph, 'rev4',
1369
def test_ancestors(self):
1370
graph = self.make_graph(ancestry_1)
1371
self.assertMergeOrder(['rev1', 'rev2b'], graph, 'rev4',
1373
self.assertMergeOrder(['rev1', 'rev2b'], graph, 'rev4',
1376
def test_shortcut_one_ancestor(self):
1377
# When we have enough info, we can stop searching
1378
graph = self.make_breaking_graph(ancestry_1, ['rev3', 'rev2b', 'rev4'])
1379
# Single ancestors shortcut right away
1380
self.assertMergeOrder(['rev3'], graph, 'rev4', ['rev3'])
1382
def test_shortcut_after_one_ancestor(self):
1383
graph = self.make_breaking_graph(ancestry_1, ['rev2a', 'rev2b'])
1384
self.assertMergeOrder(['rev3', 'rev1'], graph, 'rev4', ['rev1', 'rev3'])
1387
976
class TestCachingParentsProvider(tests.TestCase):
1388
"""These tests run with:
1390
self.inst_pp, a recording parents provider with a graph of a->b, and b is a
1392
self.caching_pp, a CachingParentsProvider layered on inst_pp.
1395
978
def setUp(self):
1396
979
super(TestCachingParentsProvider, self).setUp()
1431
1015
# Use sorted because we don't care about the order, just that each is
1432
1016
# only present 1 time.
1433
1017
self.assertEqual(['a', 'b'], sorted(self.inst_pp.calls))
1435
def test_note_missing_key(self):
1436
"""After noting that a key is missing it is cached."""
1437
self.caching_pp.note_missing_key('b')
1438
self.assertEqual({}, self.caching_pp.get_parent_map(['b']))
1439
self.assertEqual([], self.inst_pp.calls)
1440
self.assertEqual(set(['b']), self.caching_pp.missing_keys)
1443
class TestCachingParentsProviderExtras(tests.TestCaseWithTransport):
1444
"""Test the behaviour when parents are provided that were not requested."""
1447
super(TestCachingParentsProviderExtras, self).setUp()
1448
class ExtraParentsProvider(object):
1450
def get_parent_map(self, keys):
1451
return {'rev1': [], 'rev2': ['rev1',]}
1453
self.inst_pp = InstrumentedParentsProvider(ExtraParentsProvider())
1454
self.caching_pp = _mod_graph.CachingParentsProvider(
1455
get_parent_map=self.inst_pp.get_parent_map)
1457
def test_uncached(self):
1458
self.caching_pp.disable_cache()
1459
self.assertEqual({'rev1': []},
1460
self.caching_pp.get_parent_map(['rev1']))
1461
self.assertEqual(['rev1'], self.inst_pp.calls)
1462
self.assertIs(None, self.caching_pp._cache)
1464
def test_cache_initially_empty(self):
1465
self.assertEqual({}, self.caching_pp._cache)
1467
def test_cached(self):
1468
self.assertEqual({'rev1': []},
1469
self.caching_pp.get_parent_map(['rev1']))
1470
self.assertEqual(['rev1'], self.inst_pp.calls)
1471
self.assertEqual({'rev1': [], 'rev2': ['rev1']},
1472
self.caching_pp._cache)
1473
self.assertEqual({'rev1': []},
1474
self.caching_pp.get_parent_map(['rev1']))
1475
self.assertEqual(['rev1'], self.inst_pp.calls)
1477
def test_disable_cache_clears_cache(self):
1478
# Put something in the cache
1479
self.caching_pp.get_parent_map(['rev1'])
1480
self.assertEqual(2, len(self.caching_pp._cache))
1481
self.caching_pp.disable_cache()
1482
self.assertIs(None, self.caching_pp._cache)
1484
def test_enable_cache_raises(self):
1485
e = self.assertRaises(AssertionError, self.caching_pp.enable_cache)
1486
self.assertEqual('Cache enabled when already enabled.', str(e))
1488
def test_cache_misses(self):
1489
self.caching_pp.get_parent_map(['rev3'])
1490
self.caching_pp.get_parent_map(['rev3'])
1491
self.assertEqual(['rev3'], self.inst_pp.calls)
1493
def test_no_cache_misses(self):
1494
self.caching_pp.disable_cache()
1495
self.caching_pp.enable_cache(cache_misses=False)
1496
self.caching_pp.get_parent_map(['rev3'])
1497
self.caching_pp.get_parent_map(['rev3'])
1498
self.assertEqual(['rev3', 'rev3'], self.inst_pp.calls)
1500
def test_cache_extras(self):
1501
self.assertEqual({}, self.caching_pp.get_parent_map(['rev3']))
1502
self.assertEqual({'rev2': ['rev1']},
1503
self.caching_pp.get_parent_map(['rev2']))
1504
self.assertEqual(['rev3'], self.inst_pp.calls)
1507
class TestCollapseLinearRegions(tests.TestCase):
1509
def assertCollapsed(self, collapsed, original):
1510
self.assertEqual(collapsed,
1511
_mod_graph.collapse_linear_regions(original))
1513
def test_collapse_nothing(self):
1514
d = {1:[2, 3], 2:[], 3:[]}
1515
self.assertCollapsed(d, d)
1516
d = {1:[2], 2:[3, 4], 3:[5], 4:[5], 5:[]}
1517
self.assertCollapsed(d, d)
1519
def test_collapse_chain(self):
1520
# Any time we have a linear chain, we should be able to collapse
1521
d = {1:[2], 2:[3], 3:[4], 4:[5], 5:[]}
1522
self.assertCollapsed({1:[5], 5:[]}, d)
1523
d = {5:[4], 4:[3], 3:[2], 2:[1], 1:[]}
1524
self.assertCollapsed({5:[1], 1:[]}, d)
1525
d = {5:[3], 3:[4], 4:[1], 1:[2], 2:[]}
1526
self.assertCollapsed({5:[2], 2:[]}, d)
1528
def test_collapse_with_multiple_children(self):
1539
# 4 and 5 cannot be removed because 6 has 2 children
1540
# 2 and 3 cannot be removed because 1 has 2 parents
1541
d = {1:[2, 3], 2:[4], 4:[6], 3:[5], 5:[6], 6:[7], 7:[]}
1542
self.assertCollapsed(d, d)
1545
class TestPendingAncestryResultGetKeys(TestCaseWithMemoryTransport):
1546
"""Tests for bzrlib.graph.PendingAncestryResult."""
1548
def test_get_keys(self):
1549
builder = self.make_branch_builder('b')
1550
builder.start_series()
1551
builder.build_snapshot('rev-1', None, [
1552
('add', ('', 'root-id', 'directory', ''))])
1553
builder.build_snapshot('rev-2', ['rev-1'], [])
1554
builder.finish_series()
1555
repo = builder.get_branch().repository
1557
self.addCleanup(repo.unlock)
1558
result = _mod_graph.PendingAncestryResult(['rev-2'], repo)
1559
self.assertEqual(set(['rev-1', 'rev-2']), set(result.get_keys()))
1561
def test_get_keys_excludes_ghosts(self):
1562
builder = self.make_branch_builder('b')
1563
builder.start_series()
1564
builder.build_snapshot('rev-1', None, [
1565
('add', ('', 'root-id', 'directory', ''))])
1566
builder.build_snapshot('rev-2', ['rev-1', 'ghost'], [])
1567
builder.finish_series()
1568
repo = builder.get_branch().repository
1570
self.addCleanup(repo.unlock)
1571
result = _mod_graph.PendingAncestryResult(['rev-2'], repo)
1572
self.assertEqual(sorted(['rev-1', 'rev-2']), sorted(result.get_keys()))
1574
def test_get_keys_excludes_null(self):
1575
# Make a 'graph' with an iter_ancestry that returns NULL_REVISION
1576
# somewhere other than the last element, which can happen in real
1578
class StubGraph(object):
1579
def iter_ancestry(self, keys):
1580
return [(NULL_REVISION, ()), ('foo', (NULL_REVISION,))]
1581
result = _mod_graph.PendingAncestryResult(['rev-3'], None)
1582
result_keys = result._get_keys(StubGraph())
1583
# Only the non-null keys from the ancestry appear.
1584
self.assertEqual(set(['foo']), set(result_keys))
1587
class TestPendingAncestryResultRefine(TestGraphBase):
1589
def test_refine(self):
1590
# Used when pulling from a stacked repository, so test some revisions
1591
# being satisfied from the stacking branch.
1592
g = self.make_graph(
1593
{"tip":["mid"], "mid":["base"], "tag":["base"],
1594
"base":[NULL_REVISION], NULL_REVISION:[]})
1595
result = _mod_graph.PendingAncestryResult(['tip', 'tag'], None)
1596
result = result.refine(set(['tip']), set(['mid']))
1597
self.assertEqual(set(['mid', 'tag']), result.heads)
1598
result = result.refine(set(['mid', 'tag', 'base']),
1599
set([NULL_REVISION]))
1600
self.assertEqual(set([NULL_REVISION]), result.heads)
1601
self.assertTrue(result.is_empty())
1604
class TestSearchResultRefine(TestGraphBase):
1606
def test_refine(self):
1607
# Used when pulling from a stacked repository, so test some revisions
1608
# being satisfied from the stacking branch.
1609
g = self.make_graph(
1610
{"tip":["mid"], "mid":["base"], "tag":["base"],
1611
"base":[NULL_REVISION], NULL_REVISION:[]})
1612
result = _mod_graph.SearchResult(set(['tip', 'tag']),
1613
set([NULL_REVISION]), 4, set(['tip', 'mid', 'tag', 'base']))
1614
result = result.refine(set(['tip']), set(['mid']))
1615
recipe = result.get_recipe()
1616
# We should be starting from tag (original head) and mid (seen ref)
1617
self.assertEqual(set(['mid', 'tag']), recipe[1])
1618
# We should be stopping at NULL (original stop) and tip (seen head)
1619
self.assertEqual(set([NULL_REVISION, 'tip']), recipe[2])
1620
self.assertEqual(3, recipe[3])
1621
result = result.refine(set(['mid', 'tag', 'base']),
1622
set([NULL_REVISION]))
1623
recipe = result.get_recipe()
1624
# We should be starting from nothing (NULL was known as a cut point)
1625
self.assertEqual(set([]), recipe[1])
1626
# We should be stopping at NULL (original stop) and tip (seen head) and
1627
# tag (seen head) and mid(seen mid-point head). We could come back and
1628
# define this as not including mid, for minimal results, but it is
1629
# still 'correct' to include mid, and simpler/easier.
1630
self.assertEqual(set([NULL_REVISION, 'tip', 'tag', 'mid']), recipe[2])
1631
self.assertEqual(0, recipe[3])
1632
self.assertTrue(result.is_empty())