220
171
tree_z.commit('removed b')
221
172
merge_inner(tree_z.branch, tree_a, base_tree, this_tree=tree_z)
222
173
self.assertEqual([
223
MissingParent('Created directory', 'b', b'b-id'),
224
UnversionedParent('Versioned directory', 'b', b'b-id')],
174
conflicts.MissingParent('Created directory', 'b', 'b-id'),
175
conflicts.UnversionedParent('Versioned directory', 'b', 'b-id')],
225
176
tree_z.conflicts())
226
177
merge_inner(tree_a.branch, tree_z.basis_tree(), base_tree,
227
178
this_tree=tree_a)
228
179
self.assertEqual([
229
DeletingParent('Not deleting', 'b', b'b-id'),
230
UnversionedParent('Versioned directory', 'b', b'b-id')],
180
conflicts.DeletingParent('Not deleting', 'b', 'b-id'),
181
conflicts.UnversionedParent('Versioned directory', 'b', 'b-id')],
231
182
tree_a.conflicts())
233
184
def test_nested_merge(self):
235
'iter_changes doesn\'t work with changes in nested trees')
236
185
tree = self.make_branch_and_tree('tree',
237
format='development-subtree')
186
format='dirstate-with-subtree')
238
187
sub_tree = self.make_branch_and_tree('tree/sub-tree',
239
format='development-subtree')
240
sub_tree.set_root_id(b'sub-tree-root')
241
self.build_tree_contents([('tree/sub-tree/file', b'text1')])
188
format='dirstate-with-subtree')
189
sub_tree.set_root_id('sub-tree-root')
190
self.build_tree_contents([('tree/sub-tree/file', 'text1')])
242
191
sub_tree.add('file')
243
192
sub_tree.commit('foo')
244
193
tree.add_reference(sub_tree)
245
194
tree.commit('set text to 1')
246
tree2 = tree.controldir.sprout('tree2').open_workingtree()
195
tree2 = tree.bzrdir.sprout('tree2').open_workingtree()
247
196
# modify the file in the subtree
248
self.build_tree_contents([('tree2/sub-tree/file', b'text2')])
197
self.build_tree_contents([('tree2/sub-tree/file', 'text2')])
249
198
# and merge the changes from the diverged subtree into the containing
251
200
tree2.commit('changed file text')
252
201
tree.merge_from_branch(tree2.branch)
253
self.assertFileEqual(b'text2', 'tree/sub-tree/file')
202
self.assertFileEqual('text2', 'tree/sub-tree/file')
255
204
def test_merge_with_missing(self):
256
205
tree_a = self.make_branch_and_tree('tree_a')
257
self.build_tree_contents([('tree_a/file', b'content_1')])
206
self.build_tree_contents([('tree_a/file', 'content_1')])
258
207
tree_a.add('file')
259
208
tree_a.commit('commit base')
260
209
# basis_tree() is only guaranteed to be valid as long as it is actually
261
# the basis tree. This test commits to the tree after grabbing basis,
262
# so we go to the repository.
263
base_tree = tree_a.branch.repository.revision_tree(
264
tree_a.last_revision())
265
tree_b = tree_a.controldir.sprout('tree_b').open_workingtree()
266
self.build_tree_contents([('tree_a/file', b'content_2')])
210
# the basis tree. This mutates the tree after grabbing basis, so go to
212
base_tree = tree_a.branch.repository.revision_tree(tree_a.last_revision())
213
tree_b = tree_a.bzrdir.sprout('tree_b').open_workingtree()
214
self.build_tree_contents([('tree_a/file', 'content_2')])
267
215
tree_a.commit('commit other')
268
216
other_tree = tree_a.basis_tree()
269
# 'file' is now missing but isn't altered in any commit in b so no
270
# change should be applied.
271
217
os.unlink('tree_b/file')
272
218
merge_inner(tree_b.branch, other_tree, base_tree, this_tree=tree_b)
274
220
def test_merge_kind_change(self):
275
221
tree_a = self.make_branch_and_tree('tree_a')
276
self.build_tree_contents([('tree_a/file', b'content_1')])
277
tree_a.add('file', b'file-id')
222
self.build_tree_contents([('tree_a/file', 'content_1')])
223
tree_a.add('file', 'file-id')
278
224
tree_a.commit('added file')
279
tree_b = tree_a.controldir.sprout('tree_b').open_workingtree()
225
tree_b = tree_a.bzrdir.sprout('tree_b').open_workingtree()
280
226
os.unlink('tree_a/file')
281
227
self.build_tree(['tree_a/file/'])
282
228
tree_a.commit('changed file to directory')
283
229
tree_b.merge_from_branch(tree_a.branch)
284
230
self.assertEqual('directory', file_kind('tree_b/file'))
286
232
self.assertEqual('file', file_kind('tree_b/file'))
287
self.build_tree_contents([('tree_b/file', b'content_2')])
233
self.build_tree_contents([('tree_b/file', 'content_2')])
288
234
tree_b.commit('content change')
289
235
tree_b.merge_from_branch(tree_a.branch)
290
236
self.assertEqual(tree_b.conflicts(),
291
[ContentsConflict('file', file_id=b'file-id')])
237
[conflicts.ContentsConflict('file',
293
240
def test_merge_type_registry(self):
294
241
merge_type_option = option.Option.OPTIONS['merge-type']
295
self.assertFalse('merge4' in [x[0] for x in
296
merge_type_option.iter_switches()])
242
self.assertFalse('merge4' in [x[0] for x in
243
merge_type_option.iter_switches()])
297
244
registry = _mod_merge.get_merge_type_registry()
298
registry.register_lazy('merge4', 'breezy.merge', 'Merge4Merger',
245
registry.register_lazy('merge4', 'bzrlib.merge', 'Merge4Merger',
299
246
'time-travelling merge')
300
self.assertTrue('merge4' in [x[0] for x in
301
merge_type_option.iter_switches()])
247
self.assertTrue('merge4' in [x[0] for x in
248
merge_type_option.iter_switches()])
302
249
registry.remove('merge4')
303
self.assertFalse('merge4' in [x[0] for x in
304
merge_type_option.iter_switches()])
306
def test_merge_other_moves_we_deleted(self):
307
tree_a = self.make_branch_and_tree('A')
309
self.addCleanup(tree_a.unlock)
310
self.build_tree(['A/a'])
312
tree_a.commit('1', rev_id=b'rev-1')
314
tree_a.rename_one('a', 'b')
316
bzrdir_b = tree_a.controldir.sprout('B', revision_id=b'rev-1')
317
tree_b = bzrdir_b.open_workingtree()
319
self.addCleanup(tree_b.unlock)
323
tree_b.merge_from_branch(tree_a.branch)
324
except AttributeError:
325
self.fail('tried to join a path when name was None')
327
def test_merge_uncommitted_otherbasis_ancestor_of_thisbasis(self):
328
tree_a = self.make_branch_and_tree('a')
329
self.build_tree(['a/file_1', 'a/file_2'])
330
tree_a.add(['file_1'])
331
tree_a.commit('commit 1')
332
tree_a.add(['file_2'])
333
tree_a.commit('commit 2')
334
tree_b = tree_a.controldir.sprout('b').open_workingtree()
335
tree_b.rename_one('file_1', 'renamed')
336
merger = _mod_merge.Merger.from_uncommitted(tree_a, tree_b)
337
merger.merge_type = _mod_merge.Merge3Merger
339
self.assertEqual(tree_a.get_parent_ids(), [tree_b.last_revision()])
341
def test_merge_uncommitted_otherbasis_ancestor_of_thisbasis_weave(self):
342
tree_a = self.make_branch_and_tree('a')
343
self.build_tree(['a/file_1', 'a/file_2'])
344
tree_a.add(['file_1'])
345
tree_a.commit('commit 1')
346
tree_a.add(['file_2'])
347
tree_a.commit('commit 2')
348
tree_b = tree_a.controldir.sprout('b').open_workingtree()
349
tree_b.rename_one('file_1', 'renamed')
350
merger = _mod_merge.Merger.from_uncommitted(tree_a, tree_b)
351
merger.merge_type = _mod_merge.WeaveMerger
353
self.assertEqual(tree_a.get_parent_ids(), [tree_b.last_revision()])
355
def prepare_cherrypick(self):
356
"""Prepare a pair of trees for cherrypicking tests.
358
Both trees have a file, 'file'.
359
rev1 sets content to 'a'.
362
A full merge of rev2b and rev3b into this_tree would add both 'b' and
363
'c'. A successful cherrypick of rev2b-rev3b into this_tree will add
366
this_tree = self.make_branch_and_tree('this')
367
self.build_tree_contents([('this/file', b"a\n")])
368
this_tree.add('file')
369
this_tree.commit('rev1')
370
other_tree = this_tree.controldir.sprout('other').open_workingtree()
371
self.build_tree_contents([('other/file', b"a\nb\n")])
372
other_tree.commit('rev2b', rev_id=b'rev2b')
373
self.build_tree_contents([('other/file', b"c\na\nb\n")])
374
other_tree.commit('rev3b', rev_id=b'rev3b')
375
this_tree.lock_write()
376
self.addCleanup(this_tree.unlock)
377
return this_tree, other_tree
379
def test_weave_cherrypick(self):
380
this_tree, other_tree = self.prepare_cherrypick()
381
merger = _mod_merge.Merger.from_revision_ids(
382
this_tree, b'rev3b', b'rev2b', other_tree.branch)
383
merger.merge_type = _mod_merge.WeaveMerger
385
self.assertFileEqual(b'c\na\n', 'this/file')
387
def test_weave_cannot_reverse_cherrypick(self):
388
this_tree, other_tree = self.prepare_cherrypick()
389
merger = _mod_merge.Merger.from_revision_ids(
390
this_tree, b'rev2b', b'rev3b', other_tree.branch)
391
merger.merge_type = _mod_merge.WeaveMerger
392
self.assertRaises(errors.CannotReverseCherrypick, merger.do_merge)
394
def test_merge3_can_reverse_cherrypick(self):
395
this_tree, other_tree = self.prepare_cherrypick()
396
merger = _mod_merge.Merger.from_revision_ids(
397
this_tree, b'rev2b', b'rev3b', other_tree.branch)
398
merger.merge_type = _mod_merge.Merge3Merger
401
def test_merge3_will_detect_cherrypick(self):
402
this_tree = self.make_branch_and_tree('this')
403
self.build_tree_contents([('this/file', b"a\n")])
404
this_tree.add('file')
405
this_tree.commit('rev1')
406
other_tree = this_tree.controldir.sprout('other').open_workingtree()
407
self.build_tree_contents([('other/file', b"a\nb\n")])
408
other_tree.commit('rev2b', rev_id=b'rev2b')
409
self.build_tree_contents([('other/file', b"a\nb\nc\n")])
410
other_tree.commit('rev3b', rev_id=b'rev3b')
411
this_tree.lock_write()
412
self.addCleanup(this_tree.unlock)
414
merger = _mod_merge.Merger.from_revision_ids(
415
this_tree, b'rev3b', b'rev2b', other_tree.branch)
416
merger.merge_type = _mod_merge.Merge3Merger
418
self.assertFileEqual(b'a\n'
422
b'>>>>>>> MERGE-SOURCE\n',
425
def test_merge_reverse_revision_range(self):
426
tree = self.make_branch_and_tree(".")
428
self.addCleanup(tree.unlock)
429
self.build_tree(['a'])
431
first_rev = tree.commit("added a")
432
merger = _mod_merge.Merger.from_revision_ids(tree,
433
_mod_revision.NULL_REVISION,
435
merger.merge_type = _mod_merge.Merge3Merger
436
merger.interesting_files = 'a'
437
conflict_count = merger.do_merge()
438
self.assertEqual(0, conflict_count)
440
self.assertPathDoesNotExist("a")
442
self.assertPathExists("a")
444
def test_make_merger(self):
445
this_tree = self.make_branch_and_tree('this')
446
this_tree.commit('rev1', rev_id=b'rev1')
447
other_tree = this_tree.controldir.sprout('other').open_workingtree()
448
this_tree.commit('rev2', rev_id=b'rev2a')
449
other_tree.commit('rev2', rev_id=b'rev2b')
450
this_tree.lock_write()
451
self.addCleanup(this_tree.unlock)
452
merger = _mod_merge.Merger.from_revision_ids(
453
this_tree, b'rev2b', other_branch=other_tree.branch)
454
merger.merge_type = _mod_merge.Merge3Merger
455
tree_merger = merger.make_merger()
456
self.assertIs(_mod_merge.Merge3Merger, tree_merger.__class__)
457
self.assertEqual(b'rev2b',
458
tree_merger.other_tree.get_revision_id())
459
self.assertEqual(b'rev1',
460
tree_merger.base_tree.get_revision_id())
461
self.assertEqual(other_tree.branch, tree_merger.other_branch)
463
def test_make_preview_transform(self):
464
this_tree = self.make_branch_and_tree('this')
465
self.build_tree_contents([('this/file', b'1\n')])
466
this_tree.add('file', b'file-id')
467
this_tree.commit('rev1', rev_id=b'rev1')
468
other_tree = this_tree.controldir.sprout('other').open_workingtree()
469
self.build_tree_contents([('this/file', b'1\n2a\n')])
470
this_tree.commit('rev2', rev_id=b'rev2a')
471
self.build_tree_contents([('other/file', b'2b\n1\n')])
472
other_tree.commit('rev2', rev_id=b'rev2b')
473
this_tree.lock_write()
474
self.addCleanup(this_tree.unlock)
475
merger = _mod_merge.Merger.from_revision_ids(
476
this_tree, b'rev2b', other_branch=other_tree.branch)
477
merger.merge_type = _mod_merge.Merge3Merger
478
tree_merger = merger.make_merger()
479
with tree_merger.make_preview_transform() as tt:
480
preview_tree = tt.get_preview_tree()
481
with this_tree.get_file('file') as tree_file:
482
self.assertEqual(b'1\n2a\n', tree_file.read())
483
with preview_tree.get_file('file') as preview_file:
484
self.assertEqual(b'2b\n1\n2a\n', preview_file.read())
486
def test_do_merge(self):
487
this_tree = self.make_branch_and_tree('this')
488
self.build_tree_contents([('this/file', b'1\n')])
489
this_tree.add('file', b'file-id')
490
this_tree.commit('rev1', rev_id=b'rev1')
491
other_tree = this_tree.controldir.sprout('other').open_workingtree()
492
self.build_tree_contents([('this/file', b'1\n2a\n')])
493
this_tree.commit('rev2', rev_id=b'rev2a')
494
self.build_tree_contents([('other/file', b'2b\n1\n')])
495
other_tree.commit('rev2', rev_id=b'rev2b')
496
this_tree.lock_write()
497
self.addCleanup(this_tree.unlock)
498
merger = _mod_merge.Merger.from_revision_ids(
499
this_tree, b'rev2b', other_branch=other_tree.branch)
500
merger.merge_type = _mod_merge.Merge3Merger
501
tree_merger = merger.make_merger()
502
tt = tree_merger.do_merge()
503
with this_tree.get_file('file') as tree_file:
504
self.assertEqual(b'2b\n1\n2a\n', tree_file.read())
506
def test_merge_require_tree_root(self):
507
tree = self.make_branch_and_tree(".")
509
self.addCleanup(tree.unlock)
510
self.build_tree(['a'])
512
first_rev = tree.commit("added a")
513
old_root_id = tree.path2id('')
514
merger = _mod_merge.Merger.from_revision_ids(tree,
515
_mod_revision.NULL_REVISION,
517
merger.merge_type = _mod_merge.Merge3Merger
518
conflict_count = merger.do_merge()
519
self.assertEqual(0, conflict_count)
520
self.assertEqual({''}, set(tree.all_versioned_paths()))
521
tree.set_parent_ids([])
523
def test_merge_add_into_deleted_root(self):
524
# Yes, people actually do this. And report bugs if it breaks.
525
source = self.make_branch_and_tree('source', format='rich-root-pack')
526
self.build_tree(['source/foo/'])
527
source.add('foo', b'foo-id')
528
source.commit('Add foo')
529
target = source.controldir.sprout('target').open_workingtree()
530
subtree = target.extract('foo')
531
subtree.commit('Delete root')
532
self.build_tree(['source/bar'])
533
source.add('bar', b'bar-id')
534
source.commit('Add bar')
535
subtree.merge_from_branch(source.branch)
537
def test_merge_joined_branch(self):
538
source = self.make_branch_and_tree('source', format='rich-root-pack')
539
self.build_tree(['source/foo'])
541
source.commit('Add foo')
542
target = self.make_branch_and_tree('target', format='rich-root-pack')
543
self.build_tree(['target/bla'])
545
target.commit('Add bla')
546
nested = source.controldir.sprout('target/subtree').open_workingtree()
547
target.subsume(nested)
548
target.commit('Join nested')
549
self.build_tree(['source/bar'])
551
source.commit('Add bar')
552
target.merge_from_branch(source.branch)
553
target.commit('Merge source')
556
class TestPlanMerge(TestCaseWithMemoryTransport):
559
super(TestPlanMerge, self).setUp()
560
mapper = versionedfile.PrefixMapper()
561
factory = knit.make_file_factory(True, mapper)
562
self.vf = factory(self.get_transport())
563
self.plan_merge_vf = versionedfile._PlanMergeVersionedFile(b'root')
564
self.plan_merge_vf.fallback_versionedfiles.append(self.vf)
566
def add_version(self, key, parents, text):
568
key, parents, [bytes([c]) + b'\n' for c in bytearray(text)])
570
def add_rev(self, prefix, revision_id, parents, text):
571
self.add_version((prefix, revision_id), [(prefix, p) for p in parents],
574
def add_uncommitted_version(self, key, parents, text):
575
self.plan_merge_vf.add_lines(key, parents,
576
[bytes([c]) + b'\n' for c in bytearray(text)])
578
def setup_plan_merge(self):
579
self.add_rev(b'root', b'A', [], b'abc')
580
self.add_rev(b'root', b'B', [b'A'], b'acehg')
581
self.add_rev(b'root', b'C', [b'A'], b'fabg')
582
return _PlanMerge(b'B', b'C', self.plan_merge_vf, (b'root',))
584
def setup_plan_merge_uncommitted(self):
585
self.add_version((b'root', b'A'), [], b'abc')
586
self.add_uncommitted_version(
587
(b'root', b'B:'), [(b'root', b'A')], b'acehg')
588
self.add_uncommitted_version(
589
(b'root', b'C:'), [(b'root', b'A')], b'fabg')
590
return _PlanMerge(b'B:', b'C:', self.plan_merge_vf, (b'root',))
592
def test_base_from_plan(self):
593
self.setup_plan_merge()
594
plan = self.plan_merge_vf.plan_merge(b'B', b'C')
595
pwm = versionedfile.PlanWeaveMerge(plan)
596
self.assertEqual([b'a\n', b'b\n', b'c\n'], pwm.base_from_plan())
598
def test_unique_lines(self):
599
plan = self.setup_plan_merge()
600
self.assertEqual(plan._unique_lines(
601
plan._get_matching_blocks(b'B', b'C')),
604
def test_plan_merge(self):
605
self.setup_plan_merge()
606
plan = self.plan_merge_vf.plan_merge(b'B', b'C')
609
('unchanged', b'a\n'),
610
('killed-a', b'b\n'),
611
('killed-b', b'c\n'),
618
def test_plan_merge_cherrypick(self):
619
self.add_rev(b'root', b'A', [], b'abc')
620
self.add_rev(b'root', b'B', [b'A'], b'abcde')
621
self.add_rev(b'root', b'C', [b'A'], b'abcefg')
622
self.add_rev(b'root', b'D', [b'A', b'B', b'C'], b'abcdegh')
623
my_plan = _PlanMerge(b'B', b'D', self.plan_merge_vf, (b'root',))
624
# We shortcut when one text supersedes the other in the per-file graph.
625
# We don't actually need to compare the texts at this point.
634
list(my_plan.plan_merge()))
636
def test_plan_merge_no_common_ancestor(self):
637
self.add_rev(b'root', b'A', [], b'abc')
638
self.add_rev(b'root', b'B', [], b'xyz')
639
my_plan = _PlanMerge(b'A', b'B', self.plan_merge_vf, (b'root',))
647
list(my_plan.plan_merge()))
649
def test_plan_merge_tail_ancestors(self):
650
# The graph looks like this:
651
# A # Common to all ancestors
653
# B C # Ancestors of E, only common to one side
655
# D E F # D, F are unique to G, H respectively
656
# |/ \| # E is the LCA for G & H, and the unique LCA for
661
# I J # criss-cross merge of G, H
663
# In this situation, a simple pruning of ancestors of E will leave D &
664
# F "dangling", which looks like they introduce lines different from
665
# the ones in E, but in actuality C&B introduced the lines, and they
666
# are already present in E
668
# Introduce the base text
669
self.add_rev(b'root', b'A', [], b'abc')
670
# Introduces a new line B
671
self.add_rev(b'root', b'B', [b'A'], b'aBbc')
672
# Introduces a new line C
673
self.add_rev(b'root', b'C', [b'A'], b'abCc')
674
# Introduce new line D
675
self.add_rev(b'root', b'D', [b'B'], b'DaBbc')
676
# Merges B and C by just incorporating both
677
self.add_rev(b'root', b'E', [b'B', b'C'], b'aBbCc')
678
# Introduce new line F
679
self.add_rev(b'root', b'F', [b'C'], b'abCcF')
680
# Merge D & E by just combining the texts
681
self.add_rev(b'root', b'G', [b'D', b'E'], b'DaBbCc')
682
# Merge F & E by just combining the texts
683
self.add_rev(b'root', b'H', [b'F', b'E'], b'aBbCcF')
684
# Merge G & H by just combining texts
685
self.add_rev(b'root', b'I', [b'G', b'H'], b'DaBbCcF')
686
# Merge G & H but supersede an old line in B
687
self.add_rev(b'root', b'J', [b'H', b'G'], b'DaJbCcF')
688
plan = self.plan_merge_vf.plan_merge(b'I', b'J')
690
('unchanged', b'D\n'),
691
('unchanged', b'a\n'),
692
('killed-b', b'B\n'),
694
('unchanged', b'b\n'),
695
('unchanged', b'C\n'),
696
('unchanged', b'c\n'),
697
('unchanged', b'F\n')],
700
def test_plan_merge_tail_triple_ancestors(self):
701
# The graph looks like this:
702
# A # Common to all ancestors
704
# B C # Ancestors of E, only common to one side
706
# D E F # D, F are unique to G, H respectively
707
# |/|\| # E is the LCA for G & H, and the unique LCA for
709
# |\ /| # Q is just an extra node which is merged into both
712
# I J # criss-cross merge of G, H
714
# This is the same as the test_plan_merge_tail_ancestors, except we add
715
# a third LCA that doesn't add new lines, but will trigger our more
716
# involved ancestry logic
718
self.add_rev(b'root', b'A', [], b'abc')
719
self.add_rev(b'root', b'B', [b'A'], b'aBbc')
720
self.add_rev(b'root', b'C', [b'A'], b'abCc')
721
self.add_rev(b'root', b'D', [b'B'], b'DaBbc')
722
self.add_rev(b'root', b'E', [b'B', b'C'], b'aBbCc')
723
self.add_rev(b'root', b'F', [b'C'], b'abCcF')
724
self.add_rev(b'root', b'G', [b'D', b'E'], b'DaBbCc')
725
self.add_rev(b'root', b'H', [b'F', b'E'], b'aBbCcF')
726
self.add_rev(b'root', b'Q', [b'E'], b'aBbCc')
727
self.add_rev(b'root', b'I', [b'G', b'Q', b'H'], b'DaBbCcF')
728
# Merge G & H but supersede an old line in B
729
self.add_rev(b'root', b'J', [b'H', b'Q', b'G'], b'DaJbCcF')
730
plan = self.plan_merge_vf.plan_merge(b'I', b'J')
732
('unchanged', b'D\n'),
733
('unchanged', b'a\n'),
734
('killed-b', b'B\n'),
736
('unchanged', b'b\n'),
737
('unchanged', b'C\n'),
738
('unchanged', b'c\n'),
739
('unchanged', b'F\n')],
742
def test_plan_merge_2_tail_triple_ancestors(self):
743
# The graph looks like this:
744
# A B # 2 tails going back to NULL
746
# D E F # D, is unique to G, F to H
747
# |/|\| # E is the LCA for G & H, and the unique LCA for
749
# |\ /| # Q is just an extra node which is merged into both
752
# I J # criss-cross merge of G, H (and Q)
755
# This is meant to test after hitting a 3-way LCA, and multiple tail
756
# ancestors (only have NULL_REVISION in common)
758
self.add_rev(b'root', b'A', [], b'abc')
759
self.add_rev(b'root', b'B', [], b'def')
760
self.add_rev(b'root', b'D', [b'A'], b'Dabc')
761
self.add_rev(b'root', b'E', [b'A', b'B'], b'abcdef')
762
self.add_rev(b'root', b'F', [b'B'], b'defF')
763
self.add_rev(b'root', b'G', [b'D', b'E'], b'Dabcdef')
764
self.add_rev(b'root', b'H', [b'F', b'E'], b'abcdefF')
765
self.add_rev(b'root', b'Q', [b'E'], b'abcdef')
766
self.add_rev(b'root', b'I', [b'G', b'Q', b'H'], b'DabcdefF')
767
# Merge G & H but supersede an old line in B
768
self.add_rev(b'root', b'J', [b'H', b'Q', b'G'], b'DabcdJfF')
769
plan = self.plan_merge_vf.plan_merge(b'I', b'J')
771
('unchanged', b'D\n'),
772
('unchanged', b'a\n'),
773
('unchanged', b'b\n'),
774
('unchanged', b'c\n'),
775
('unchanged', b'd\n'),
776
('killed-b', b'e\n'),
778
('unchanged', b'f\n'),
779
('unchanged', b'F\n')],
782
def test_plan_merge_uncommitted_files(self):
783
self.setup_plan_merge_uncommitted()
784
plan = self.plan_merge_vf.plan_merge(b'B:', b'C:')
787
('unchanged', b'a\n'),
788
('killed-a', b'b\n'),
789
('killed-b', b'c\n'),
796
def test_plan_merge_insert_order(self):
797
"""Weave merges are sensitive to the order of insertion.
799
Specifically for overlapping regions, it effects which region gets put
800
'first'. And when a user resolves an overlapping merge, if they use the
801
same ordering, then the lines match the parents, if they don't only
802
*some* of the lines match.
804
self.add_rev(b'root', b'A', [], b'abcdef')
805
self.add_rev(b'root', b'B', [b'A'], b'abwxcdef')
806
self.add_rev(b'root', b'C', [b'A'], b'abyzcdef')
807
# Merge, and resolve the conflict by adding *both* sets of lines
808
# If we get the ordering wrong, these will look like new lines in D,
809
# rather than carried over from B, C
810
self.add_rev(b'root', b'D', [b'B', b'C'],
812
# Supersede the lines in B and delete the lines in C, which will
813
# conflict if they are treated as being in D
814
self.add_rev(b'root', b'E', [b'C', b'B'],
816
# Same thing for the lines in C
817
self.add_rev(b'root', b'F', [b'C'], b'abpqcdef')
818
plan = self.plan_merge_vf.plan_merge(b'D', b'E')
820
('unchanged', b'a\n'),
821
('unchanged', b'b\n'),
822
('killed-b', b'w\n'),
823
('killed-b', b'x\n'),
824
('killed-b', b'y\n'),
825
('killed-b', b'z\n'),
828
('unchanged', b'c\n'),
829
('unchanged', b'd\n'),
830
('unchanged', b'e\n'),
831
('unchanged', b'f\n')],
833
plan = self.plan_merge_vf.plan_merge(b'E', b'D')
834
# Going in the opposite direction shows the effect of the opposite plan
836
('unchanged', b'a\n'),
837
('unchanged', b'b\n'),
840
('killed-a', b'y\n'),
841
('killed-a', b'z\n'),
842
('killed-both', b'w\n'),
843
('killed-both', b'x\n'),
846
('unchanged', b'c\n'),
847
('unchanged', b'd\n'),
848
('unchanged', b'e\n'),
849
('unchanged', b'f\n')],
852
def test_plan_merge_criss_cross(self):
853
# This is specificly trying to trigger problems when using limited
854
# ancestry and weaves. The ancestry graph looks like:
855
# XX unused ancestor, should not show up in the weave
859
# B \ Introduces a line 'foo'
861
# C D E C & D both have 'foo', E has different changes
865
# F G All of C, D, E are merged into F and G, so they are
866
# all common ancestors.
868
# The specific issue with weaves:
869
# B introduced a text ('foo') that is present in both C and D.
870
# If we do not include B (because it isn't an ancestor of E), then
871
# the A=>C and A=>D look like both sides independently introduce the
872
# text ('foo'). If F does not modify the text, it would still appear
873
# to have deleted on of the versions from C or D. If G then modifies
874
# 'foo', it should appear as superseding the value in F (since it
875
# came from B), rather than conflict because of the resolution during
877
self.add_rev(b'root', b'XX', [], b'qrs')
878
self.add_rev(b'root', b'A', [b'XX'], b'abcdef')
879
self.add_rev(b'root', b'B', [b'A'], b'axcdef')
880
self.add_rev(b'root', b'C', [b'B'], b'axcdefg')
881
self.add_rev(b'root', b'D', [b'B'], b'haxcdef')
882
self.add_rev(b'root', b'E', [b'A'], b'abcdyf')
883
# Simple combining of all texts
884
self.add_rev(b'root', b'F', [b'C', b'D', b'E'], b'haxcdyfg')
885
# combine and supersede 'x'
886
self.add_rev(b'root', b'G', [b'C', b'D', b'E'], b'hazcdyfg')
887
plan = self.plan_merge_vf.plan_merge(b'F', b'G')
889
('unchanged', b'h\n'),
890
('unchanged', b'a\n'),
891
('killed-base', b'b\n'),
892
('killed-b', b'x\n'),
894
('unchanged', b'c\n'),
895
('unchanged', b'd\n'),
896
('killed-base', b'e\n'),
897
('unchanged', b'y\n'),
898
('unchanged', b'f\n'),
899
('unchanged', b'g\n')],
901
plan = self.plan_merge_vf.plan_lca_merge(b'F', b'G')
902
# This is one of the main differences between plan_merge and
903
# plan_lca_merge. plan_lca_merge generates a conflict for 'x => z',
904
# because 'x' was not present in one of the bases. However, in this
905
# case it is spurious because 'x' does not exist in the global base A.
907
('unchanged', b'h\n'),
908
('unchanged', b'a\n'),
909
('conflicted-a', b'x\n'),
911
('unchanged', b'c\n'),
912
('unchanged', b'd\n'),
913
('unchanged', b'y\n'),
914
('unchanged', b'f\n'),
915
('unchanged', b'g\n')],
918
def test_criss_cross_flip_flop(self):
919
# This is specificly trying to trigger problems when using limited
920
# ancestry and weaves. The ancestry graph looks like:
921
# XX unused ancestor, should not show up in the weave
925
# B C B & C both introduce a new line
929
# D E B & C are both merged, so both are common ancestors
930
# In the process of merging, both sides order the new
933
self.add_rev(b'root', b'XX', [], b'qrs')
934
self.add_rev(b'root', b'A', [b'XX'], b'abcdef')
935
self.add_rev(b'root', b'B', [b'A'], b'abcdgef')
936
self.add_rev(b'root', b'C', [b'A'], b'abcdhef')
937
self.add_rev(b'root', b'D', [b'B', b'C'], b'abcdghef')
938
self.add_rev(b'root', b'E', [b'C', b'B'], b'abcdhgef')
939
plan = list(self.plan_merge_vf.plan_merge(b'D', b'E'))
941
('unchanged', b'a\n'),
942
('unchanged', b'b\n'),
943
('unchanged', b'c\n'),
944
('unchanged', b'd\n'),
946
('unchanged', b'g\n'),
947
('killed-b', b'h\n'),
948
('unchanged', b'e\n'),
949
('unchanged', b'f\n'),
951
pwm = versionedfile.PlanWeaveMerge(plan)
952
self.assertEqualDiff(b'a\nb\nc\nd\ng\nh\ne\nf\n',
953
b''.join(pwm.base_from_plan()))
954
# Reversing the order reverses the merge plan, and final order of 'hg'
956
plan = list(self.plan_merge_vf.plan_merge(b'E', b'D'))
958
('unchanged', b'a\n'),
959
('unchanged', b'b\n'),
960
('unchanged', b'c\n'),
961
('unchanged', b'd\n'),
963
('unchanged', b'h\n'),
964
('killed-b', b'g\n'),
965
('unchanged', b'e\n'),
966
('unchanged', b'f\n'),
968
pwm = versionedfile.PlanWeaveMerge(plan)
969
self.assertEqualDiff(b'a\nb\nc\nd\nh\ng\ne\nf\n',
970
b''.join(pwm.base_from_plan()))
971
# This is where lca differs, in that it (fairly correctly) determines
972
# that there is a conflict because both sides resolved the merge
974
plan = list(self.plan_merge_vf.plan_lca_merge(b'D', b'E'))
976
('unchanged', b'a\n'),
977
('unchanged', b'b\n'),
978
('unchanged', b'c\n'),
979
('unchanged', b'd\n'),
980
('conflicted-b', b'h\n'),
981
('unchanged', b'g\n'),
982
('conflicted-a', b'h\n'),
983
('unchanged', b'e\n'),
984
('unchanged', b'f\n'),
986
pwm = versionedfile.PlanWeaveMerge(plan)
987
self.assertEqualDiff(b'a\nb\nc\nd\ng\ne\nf\n',
988
b''.join(pwm.base_from_plan()))
989
# Reversing it changes what line is doubled, but still gives a
991
plan = list(self.plan_merge_vf.plan_lca_merge(b'E', b'D'))
993
('unchanged', b'a\n'),
994
('unchanged', b'b\n'),
995
('unchanged', b'c\n'),
996
('unchanged', b'd\n'),
997
('conflicted-b', b'g\n'),
998
('unchanged', b'h\n'),
999
('conflicted-a', b'g\n'),
1000
('unchanged', b'e\n'),
1001
('unchanged', b'f\n'),
1003
pwm = versionedfile.PlanWeaveMerge(plan)
1004
self.assertEqualDiff(b'a\nb\nc\nd\nh\ne\nf\n',
1005
b''.join(pwm.base_from_plan()))
1007
def assertRemoveExternalReferences(self, filtered_parent_map,
1008
child_map, tails, parent_map):
1009
"""Assert results for _PlanMerge._remove_external_references."""
1010
(act_filtered_parent_map, act_child_map,
1011
act_tails) = _PlanMerge._remove_external_references(parent_map)
1013
# The parent map *should* preserve ordering, but the ordering of
1014
# children is not strictly defined
1015
# child_map = dict((k, sorted(children))
1016
# for k, children in child_map.iteritems())
1017
# act_child_map = dict(k, sorted(children)
1018
# for k, children in act_child_map.iteritems())
1019
self.assertEqual(filtered_parent_map, act_filtered_parent_map)
1020
self.assertEqual(child_map, act_child_map)
1021
self.assertEqual(sorted(tails), sorted(act_tails))
1023
def test__remove_external_references(self):
1024
# First, nothing to remove
1025
self.assertRemoveExternalReferences({3: [2], 2: [1], 1: []},
1026
{1: [2], 2: [3], 3: []}, [1], {3: [2], 2: [1], 1: []})
1027
# The reverse direction
1028
self.assertRemoveExternalReferences({1: [2], 2: [3], 3: []},
1029
{3: [2], 2: [1], 1: []}, [3], {1: [2], 2: [3], 3: []})
1031
self.assertRemoveExternalReferences({3: [2], 2: [1], 1: []},
1032
{1: [2], 2: [3], 3: []}, [1], {3: [2, 4], 2: [1, 5], 1: [6]})
1034
self.assertRemoveExternalReferences(
1035
{4: [2, 3], 3: [], 2: [1], 1: []},
1036
{1: [2], 2: [4], 3: [4], 4: []},
1038
{4: [2, 3], 3: [5], 2: [1], 1: [6]})
1040
self.assertRemoveExternalReferences(
1041
{1: [3], 2: [3, 4], 3: [], 4: []},
1042
{1: [], 2: [], 3: [1, 2], 4: [2]},
1044
{1: [3], 2: [3, 4], 3: [5], 4: []})
1046
def assertPruneTails(self, pruned_map, tails, parent_map):
1048
for key, parent_keys in parent_map.items():
1049
child_map.setdefault(key, [])
1050
for pkey in parent_keys:
1051
child_map.setdefault(pkey, []).append(key)
1052
_PlanMerge._prune_tails(parent_map, child_map, tails)
1053
self.assertEqual(pruned_map, parent_map)
1055
def test__prune_tails(self):
1056
# Nothing requested to prune
1057
self.assertPruneTails({1: [], 2: [], 3: []}, [],
1058
{1: [], 2: [], 3: []})
1059
# Prune a single entry
1060
self.assertPruneTails({1: [], 3: []}, [2],
1061
{1: [], 2: [], 3: []})
1063
self.assertPruneTails({1: []}, [3],
1064
{1: [], 2: [3], 3: []})
1065
# Prune a chain with a diamond
1066
self.assertPruneTails({1: []}, [5],
1067
{1: [], 2: [3, 4], 3: [5], 4: [5], 5: []})
1068
# Prune a partial chain
1069
self.assertPruneTails({1: [6], 6: []}, [5],
1070
{1: [2, 6], 2: [3, 4], 3: [5], 4: [5], 5: [],
1072
# Prune a chain with multiple tips, that pulls out intermediates
1073
self.assertPruneTails({1: [3], 3: []}, [4, 5],
1074
{1: [2, 3], 2: [4, 5], 3: [], 4: [], 5: []})
1075
self.assertPruneTails({1: [3], 3: []}, [5, 4],
1076
{1: [2, 3], 2: [4, 5], 3: [], 4: [], 5: []})
1078
def test_subtract_plans(self):
1080
('unchanged', b'a\n'),
1082
('killed-a', b'c\n'),
1085
('killed-b', b'f\n'),
1086
('killed-b', b'g\n'),
1089
('unchanged', b'a\n'),
1091
('killed-a', b'c\n'),
1094
('killed-b', b'f\n'),
1095
('killed-b', b'i\n'),
1098
('unchanged', b'a\n'),
1100
('killed-a', b'c\n'),
1102
('unchanged', b'f\n'),
1103
('killed-b', b'i\n'),
1105
self.assertEqual(subtracted_plan,
1106
list(_PlanMerge._subtract_plans(old_plan, new_plan)))
1108
def setup_merge_with_base(self):
1109
self.add_rev(b'root', b'COMMON', [], b'abc')
1110
self.add_rev(b'root', b'THIS', [b'COMMON'], b'abcd')
1111
self.add_rev(b'root', b'BASE', [b'COMMON'], b'eabc')
1112
self.add_rev(b'root', b'OTHER', [b'BASE'], b'eafb')
1114
def test_plan_merge_with_base(self):
1115
self.setup_merge_with_base()
1116
plan = self.plan_merge_vf.plan_merge(b'THIS', b'OTHER', b'BASE')
1117
self.assertEqual([('unchanged', b'a\n'),
1119
('unchanged', b'b\n'),
1120
('killed-b', b'c\n'),
1124
def test_plan_lca_merge(self):
1125
self.setup_plan_merge()
1126
plan = self.plan_merge_vf.plan_lca_merge(b'B', b'C')
1129
('unchanged', b'a\n'),
1130
('killed-b', b'c\n'),
1133
('killed-a', b'b\n'),
1134
('unchanged', b'g\n')],
1137
def test_plan_lca_merge_uncommitted_files(self):
1138
self.setup_plan_merge_uncommitted()
1139
plan = self.plan_merge_vf.plan_lca_merge(b'B:', b'C:')
1142
('unchanged', b'a\n'),
1143
('killed-b', b'c\n'),
1146
('killed-a', b'b\n'),
1147
('unchanged', b'g\n')],
1150
def test_plan_lca_merge_with_base(self):
1151
self.setup_merge_with_base()
1152
plan = self.plan_merge_vf.plan_lca_merge(b'THIS', b'OTHER', b'BASE')
1153
self.assertEqual([('unchanged', b'a\n'),
1155
('unchanged', b'b\n'),
1156
('killed-b', b'c\n'),
1160
def test_plan_lca_merge_with_criss_cross(self):
1161
self.add_version((b'root', b'ROOT'), [], b'abc')
1162
# each side makes a change
1163
self.add_version((b'root', b'REV1'), [(b'root', b'ROOT')], b'abcd')
1164
self.add_version((b'root', b'REV2'), [(b'root', b'ROOT')], b'abce')
1165
# both sides merge, discarding others' changes
1166
self.add_version((b'root', b'LCA1'),
1167
[(b'root', b'REV1'), (b'root', b'REV2')], b'abcd')
1168
self.add_version((b'root', b'LCA2'),
1169
[(b'root', b'REV1'), (b'root', b'REV2')], b'fabce')
1170
plan = self.plan_merge_vf.plan_lca_merge(b'LCA1', b'LCA2')
1171
self.assertEqual([('new-b', b'f\n'),
1172
('unchanged', b'a\n'),
1173
('unchanged', b'b\n'),
1174
('unchanged', b'c\n'),
1175
('conflicted-a', b'd\n'),
1176
('conflicted-b', b'e\n'),
1179
def test_plan_lca_merge_with_null(self):
1180
self.add_version((b'root', b'A'), [], b'ab')
1181
self.add_version((b'root', b'B'), [], b'bc')
1182
plan = self.plan_merge_vf.plan_lca_merge(b'A', b'B')
1183
self.assertEqual([('new-a', b'a\n'),
1184
('unchanged', b'b\n'),
1188
def test_plan_merge_with_delete_and_change(self):
1189
self.add_rev(b'root', b'C', [], b'a')
1190
self.add_rev(b'root', b'A', [b'C'], b'b')
1191
self.add_rev(b'root', b'B', [b'C'], b'')
1192
plan = self.plan_merge_vf.plan_merge(b'A', b'B')
1193
self.assertEqual([('killed-both', b'a\n'),
1197
def test_plan_merge_with_move_and_change(self):
1198
self.add_rev(b'root', b'C', [], b'abcd')
1199
self.add_rev(b'root', b'A', [b'C'], b'acbd')
1200
self.add_rev(b'root', b'B', [b'C'], b'aBcd')
1201
plan = self.plan_merge_vf.plan_merge(b'A', b'B')
1202
self.assertEqual([('unchanged', b'a\n'),
1204
('killed-b', b'b\n'),
1206
('killed-a', b'c\n'),
1207
('unchanged', b'd\n'),
1211
class LoggingMerger(object):
1212
# These seem to be the required attributes
1213
requires_base = False
1214
supports_reprocess = False
1215
supports_show_base = False
1216
supports_cherrypick = False
1217
# We intentionally do not define supports_lca_trees
1219
def __init__(self, *args, **kwargs):
1221
self.kwargs = kwargs
1224
class TestMergerBase(TestCaseWithMemoryTransport):
1225
"""Common functionality for Merger tests that don't write to disk."""
1227
def get_builder(self):
1228
builder = self.make_branch_builder('path')
1229
builder.start_series()
1230
self.addCleanup(builder.finish_series)
1233
def setup_simple_graph(self):
1234
"""Create a simple 3-node graph.
1236
:return: A BranchBuilder
1243
builder = self.get_builder()
1244
builder.build_snapshot(None,
1245
[('add', ('', None, 'directory', None))],
1246
revision_id=b'A-id')
1247
builder.build_snapshot([b'A-id'], [], revision_id=b'C-id')
1248
builder.build_snapshot([b'A-id'], [], revision_id=b'B-id')
1251
def setup_criss_cross_graph(self):
1252
"""Create a 5-node graph with a criss-cross.
1254
:return: A BranchBuilder
1261
builder = self.setup_simple_graph()
1262
builder.build_snapshot([b'C-id', b'B-id'], [], revision_id=b'E-id')
1263
builder.build_snapshot([b'B-id', b'C-id'], [], revision_id=b'D-id')
1266
def make_Merger(self, builder, other_revision_id, interesting_files=None):
1267
"""Make a Merger object from a branch builder"""
1268
mem_tree = memorytree.MemoryTree.create_on_branch(builder.get_branch())
1269
mem_tree.lock_write()
1270
self.addCleanup(mem_tree.unlock)
1271
merger = _mod_merge.Merger.from_revision_ids(
1272
mem_tree, other_revision_id)
1273
merger.set_interesting_files(interesting_files)
1274
merger.merge_type = _mod_merge.Merge3Merger
1278
class TestMergerInMemory(TestMergerBase):
1280
def test_cache_trees_with_revision_ids_None(self):
1281
merger = self.make_Merger(self.setup_simple_graph(), b'C-id')
1282
original_cache = dict(merger._cached_trees)
1283
merger.cache_trees_with_revision_ids([None])
1284
self.assertEqual(original_cache, merger._cached_trees)
1286
def test_cache_trees_with_revision_ids_no_revision_id(self):
1287
merger = self.make_Merger(self.setup_simple_graph(), b'C-id')
1288
original_cache = dict(merger._cached_trees)
1289
tree = self.make_branch_and_memory_tree('tree')
1290
merger.cache_trees_with_revision_ids([tree])
1291
self.assertEqual(original_cache, merger._cached_trees)
1293
def test_cache_trees_with_revision_ids_having_revision_id(self):
1294
merger = self.make_Merger(self.setup_simple_graph(), b'C-id')
1295
original_cache = dict(merger._cached_trees)
1296
tree = merger.this_branch.repository.revision_tree(b'B-id')
1297
original_cache[b'B-id'] = tree
1298
merger.cache_trees_with_revision_ids([tree])
1299
self.assertEqual(original_cache, merger._cached_trees)
1301
def test_find_base(self):
1302
merger = self.make_Merger(self.setup_simple_graph(), b'C-id')
1303
self.assertEqual(b'A-id', merger.base_rev_id)
1304
self.assertFalse(merger._is_criss_cross)
1305
self.assertIs(None, merger._lca_trees)
1307
def test_find_base_criss_cross(self):
1308
builder = self.setup_criss_cross_graph()
1309
merger = self.make_Merger(builder, b'E-id')
1310
self.assertEqual(b'A-id', merger.base_rev_id)
1311
self.assertTrue(merger._is_criss_cross)
1312
self.assertEqual([b'B-id', b'C-id'], [t.get_revision_id()
1313
for t in merger._lca_trees])
1314
# If we swap the order, we should get a different lca order
1315
builder.build_snapshot([b'E-id'], [], revision_id=b'F-id')
1316
merger = self.make_Merger(builder, b'D-id')
1317
self.assertEqual([b'C-id', b'B-id'], [t.get_revision_id()
1318
for t in merger._lca_trees])
1320
def test_find_base_triple_criss_cross(self):
1323
# B C F # F is merged into both branches
1330
builder = self.setup_criss_cross_graph()
1331
builder.build_snapshot([b'A-id'], [], revision_id=b'F-id')
1332
builder.build_snapshot([b'E-id', b'F-id'], [], revision_id=b'H-id')
1333
builder.build_snapshot([b'D-id', b'F-id'], [], revision_id=b'G-id')
1334
merger = self.make_Merger(builder, b'H-id')
1335
self.assertEqual([b'B-id', b'C-id', b'F-id'],
1336
[t.get_revision_id() for t in merger._lca_trees])
1338
def test_find_base_new_root_criss_cross(self):
1344
builder = self.get_builder()
1345
builder.build_snapshot(None,
1346
[('add', ('', None, 'directory', None))],
1347
revision_id=b'A-id')
1348
builder.build_snapshot([],
1349
[('add', ('', None, 'directory', None))],
1350
revision_id=b'B-id')
1351
builder.build_snapshot([b'A-id', b'B-id'], [], revision_id=b'D-id')
1352
builder.build_snapshot([b'A-id', b'B-id'], [], revision_id=b'C-id')
1353
merger = self.make_Merger(builder, b'D-id')
1354
self.assertEqual(b'A-id', merger.base_rev_id)
1355
self.assertTrue(merger._is_criss_cross)
1356
self.assertEqual([b'A-id', b'B-id'], [t.get_revision_id()
1357
for t in merger._lca_trees])
1359
def test_no_criss_cross_passed_to_merge_type(self):
1360
class LCATreesMerger(LoggingMerger):
1361
supports_lca_trees = True
1363
merger = self.make_Merger(self.setup_simple_graph(), b'C-id')
1364
merger.merge_type = LCATreesMerger
1365
merge_obj = merger.make_merger()
1366
self.assertIsInstance(merge_obj, LCATreesMerger)
1367
self.assertFalse('lca_trees' in merge_obj.kwargs)
1369
def test_criss_cross_passed_to_merge_type(self):
1370
merger = self.make_Merger(self.setup_criss_cross_graph(), b'E-id')
1371
merger.merge_type = _mod_merge.Merge3Merger
1372
merge_obj = merger.make_merger()
1373
self.assertEqual([b'B-id', b'C-id'], [t.get_revision_id()
1374
for t in merger._lca_trees])
1376
def test_criss_cross_not_supported_merge_type(self):
1377
merger = self.make_Merger(self.setup_criss_cross_graph(), b'E-id')
1378
# We explicitly do not define supports_lca_trees
1379
merger.merge_type = LoggingMerger
1380
merge_obj = merger.make_merger()
1381
self.assertIsInstance(merge_obj, LoggingMerger)
1382
self.assertFalse('lca_trees' in merge_obj.kwargs)
1384
def test_criss_cross_unsupported_merge_type(self):
1385
class UnsupportedLCATreesMerger(LoggingMerger):
1386
supports_lca_trees = False
1388
merger = self.make_Merger(self.setup_criss_cross_graph(), b'E-id')
1389
merger.merge_type = UnsupportedLCATreesMerger
1390
merge_obj = merger.make_merger()
1391
self.assertIsInstance(merge_obj, UnsupportedLCATreesMerger)
1392
self.assertFalse('lca_trees' in merge_obj.kwargs)
1395
class TestMergerEntriesLCA(TestMergerBase):
1397
def make_merge_obj(self, builder, other_revision_id,
1398
interesting_files=None):
1399
merger = self.make_Merger(builder, other_revision_id,
1400
interesting_files=interesting_files)
1401
return merger.make_merger()
1403
def test_simple(self):
1404
builder = self.get_builder()
1405
builder.build_snapshot(None,
1406
[('add', (u'', b'a-root-id', 'directory', None)),
1407
('add', (u'a', b'a-id', 'file', b'a\nb\nc\n'))],
1408
revision_id=b'A-id')
1409
builder.build_snapshot([b'A-id'],
1410
[('modify', ('a', b'a\nb\nC\nc\n'))],
1411
revision_id=b'C-id')
1412
builder.build_snapshot([b'A-id'],
1413
[('modify', ('a', b'a\nB\nb\nc\n'))],
1414
revision_id=b'B-id')
1415
builder.build_snapshot([b'C-id', b'B-id'],
1416
[('modify', ('a', b'a\nB\nb\nC\nc\nE\n'))],
1417
revision_id=b'E-id')
1418
builder.build_snapshot([b'B-id', b'C-id'],
1419
[('modify', ('a', b'a\nB\nb\nC\nc\n'))],
1420
revision_id=b'D-id', )
1421
merge_obj = self.make_merge_obj(builder, b'E-id')
1423
self.assertEqual([b'B-id', b'C-id'], [t.get_revision_id()
1424
for t in merge_obj._lca_trees])
1425
self.assertEqual(b'A-id', merge_obj.base_tree.get_revision_id())
1426
entries = list(merge_obj._entries_lca())
1428
# (file_id, changed, parents, names, executable)
1429
# BASE, lca1, lca2, OTHER, THIS
1430
root_id = b'a-root-id'
1431
self.assertEqual([(b'a-id', True,
1432
((u'a', [u'a', u'a']), u'a', u'a'),
1433
((root_id, [root_id, root_id]), root_id, root_id),
1434
((u'a', [u'a', u'a']), u'a', u'a'),
1435
((False, [False, False]), False, False),
1439
def test_not_in_base(self):
1440
# LCAs all have the same last-modified revision for the file, as do
1441
# the tips, but the base has something different
1442
# A base, doesn't have the file
1444
# B C B introduces 'foo', C introduces 'bar'
1446
# D E D and E now both have 'foo' and 'bar'
1448
# F G the files are now in F, G, D and E, but not in A
1451
builder = self.get_builder()
1452
builder.build_snapshot(None,
1453
[('add', (u'', b'a-root-id', 'directory', None))],
1454
revision_id=b'A-id')
1455
builder.build_snapshot([b'A-id'],
1456
[('add', (u'foo', b'foo-id', 'file', b'a\nb\nc\n'))],
1457
revision_id=b'B-id')
1458
builder.build_snapshot([b'A-id'],
1459
[('add', (u'bar', b'bar-id', 'file', b'd\ne\nf\n'))],
1460
revision_id=b'C-id')
1461
builder.build_snapshot([b'B-id', b'C-id'],
1462
[('add', (u'bar', b'bar-id', 'file', b'd\ne\nf\n'))],
1463
revision_id=b'D-id')
1464
builder.build_snapshot([b'C-id', b'B-id'],
1465
[('add', (u'foo', b'foo-id', 'file', b'a\nb\nc\n'))],
1466
revision_id=b'E-id')
1467
builder.build_snapshot([b'E-id', b'D-id'],
1468
[('modify', (u'bar', b'd\ne\nf\nG\n'))],
1469
revision_id=b'G-id')
1470
builder.build_snapshot([b'D-id', b'E-id'], [], revision_id=b'F-id')
1471
merge_obj = self.make_merge_obj(builder, b'G-id')
1473
self.assertEqual([b'D-id', b'E-id'], [t.get_revision_id()
1474
for t in merge_obj._lca_trees])
1475
self.assertEqual(b'A-id', merge_obj.base_tree.get_revision_id())
1476
entries = list(merge_obj._entries_lca())
1477
root_id = b'a-root-id'
1478
self.assertEqual([(b'bar-id', True,
1479
((None, [u'bar', u'bar']), u'bar', u'bar'),
1480
((None, [root_id, root_id]), root_id, root_id),
1481
((None, [u'bar', u'bar']), u'bar', u'bar'),
1482
((None, [False, False]), False, False),
1486
def test_not_in_this(self):
1487
builder = self.get_builder()
1488
builder.build_snapshot(None,
1489
[('add', (u'', b'a-root-id', 'directory', None)),
1490
('add', (u'a', b'a-id', 'file', b'a\nb\nc\n'))],
1491
revision_id=b'A-id')
1492
builder.build_snapshot([b'A-id'],
1493
[('modify', ('a', b'a\nB\nb\nc\n'))],
1494
revision_id=b'B-id')
1495
builder.build_snapshot([b'A-id'],
1496
[('modify', ('a', b'a\nb\nC\nc\n'))],
1497
revision_id=b'C-id')
1498
builder.build_snapshot([b'C-id', b'B-id'],
1499
[('modify', ('a', b'a\nB\nb\nC\nc\nE\n'))],
1500
revision_id=b'E-id')
1501
builder.build_snapshot([b'B-id', b'C-id'],
1502
[('unversion', 'a')],
1503
revision_id=b'D-id')
1504
merge_obj = self.make_merge_obj(builder, b'E-id')
1506
self.assertEqual([b'B-id', b'C-id'], [t.get_revision_id()
1507
for t in merge_obj._lca_trees])
1508
self.assertEqual(b'A-id', merge_obj.base_tree.get_revision_id())
1510
entries = list(merge_obj._entries_lca())
1511
root_id = b'a-root-id'
1512
self.assertEqual([(b'a-id', True,
1513
((u'a', [u'a', u'a']), u'a', None),
1514
((root_id, [root_id, root_id]), root_id, None),
1515
((u'a', [u'a', u'a']), u'a', None),
1516
((False, [False, False]), False, None),
1520
def test_file_not_in_one_lca(self):
1523
# B C # B no file, C introduces a file
1525
# D E # D and E both have the file, unchanged from C
1526
builder = self.get_builder()
1527
builder.build_snapshot(None,
1528
[('add', (u'', b'a-root-id', 'directory', None))],
1529
revision_id=b'A-id')
1530
builder.build_snapshot([b'A-id'], [], revision_id=b'B-id')
1531
builder.build_snapshot([b'A-id'],
1532
[('add', (u'a', b'a-id', 'file', b'a\nb\nc\n'))],
1533
revision_id=b'C-id')
1534
builder.build_snapshot([b'C-id', b'B-id'],
1535
[], revision_id=b'E-id') # Inherited from C
1536
builder.build_snapshot([b'B-id', b'C-id'], # Merged from C
1537
[('add', (u'a', b'a-id', 'file', b'a\nb\nc\n'))],
1538
revision_id=b'D-id')
1539
merge_obj = self.make_merge_obj(builder, b'E-id')
1541
self.assertEqual([b'B-id', b'C-id'], [t.get_revision_id()
1542
for t in merge_obj._lca_trees])
1543
self.assertEqual(b'A-id', merge_obj.base_tree.get_revision_id())
1545
entries = list(merge_obj._entries_lca())
1546
self.assertEqual([], entries)
1548
def test_not_in_other(self):
1549
builder = self.get_builder()
1550
builder.build_snapshot(None,
1551
[('add', (u'', b'a-root-id', 'directory', None)),
1552
('add', (u'a', b'a-id', 'file', b'a\nb\nc\n'))],
1553
revision_id=b'A-id')
1554
builder.build_snapshot([b'A-id'], [], revision_id=b'B-id')
1555
builder.build_snapshot([b'A-id'], [], revision_id=b'C-id')
1556
builder.build_snapshot(
1558
[('unversion', 'a')], revision_id=b'E-id')
1559
builder.build_snapshot([b'B-id', b'C-id'], [], revision_id=b'D-id')
1560
merge_obj = self.make_merge_obj(builder, b'E-id')
1562
entries = list(merge_obj._entries_lca())
1563
root_id = b'a-root-id'
1564
self.assertEqual([(b'a-id', True,
1565
((u'a', [u'a', u'a']), None, u'a'),
1566
((root_id, [root_id, root_id]), None, root_id),
1567
((u'a', [u'a', u'a']), None, u'a'),
1568
((False, [False, False]), None, False),
1572
def test_not_in_other_or_lca(self):
1573
# A base, introduces 'foo'
1575
# B C B nothing, C deletes foo
1577
# D E D restores foo (same as B), E leaves it deleted
1579
# A => B, no changes
1580
# A => C, delete foo (C should supersede B)
1581
# C => D, restore foo
1582
# C => E, no changes
1583
# D would then win 'cleanly' and no record would be given
1584
builder = self.get_builder()
1585
builder.build_snapshot(None,
1586
[('add', (u'', b'a-root-id', 'directory', None)),
1587
('add', (u'foo', b'foo-id', 'file', b'content\n'))],
1588
revision_id=b'A-id')
1589
builder.build_snapshot([b'A-id'], [], revision_id=b'B-id')
1590
builder.build_snapshot([b'A-id'],
1591
[('unversion', 'foo')], revision_id=b'C-id')
1592
builder.build_snapshot([b'C-id', b'B-id'], [], revision_id=b'E-id')
1593
builder.build_snapshot([b'B-id', b'C-id'], [], revision_id=b'D-id')
1594
merge_obj = self.make_merge_obj(builder, b'E-id')
1596
entries = list(merge_obj._entries_lca())
1597
self.assertEqual([], entries)
1599
def test_not_in_other_mod_in_lca1_not_in_lca2(self):
1600
# A base, introduces 'foo'
1602
# B C B changes 'foo', C deletes foo
1604
# D E D restores foo (same as B), E leaves it deleted (as C)
1606
# A => B, modified foo
1607
# A => C, delete foo, C does not supersede B
1608
# B => D, no changes
1609
# C => D, resolve in favor of B
1610
# B => E, resolve in favor of E
1611
# C => E, no changes
1612
# In this case, we have a conflict of how the changes were resolved. E
1613
# picked C and D picked B, so we should issue a conflict
1614
builder = self.get_builder()
1615
builder.build_snapshot(None,
1616
[('add', (u'', b'a-root-id', 'directory', None)),
1617
('add', (u'foo', b'foo-id', 'file', b'content\n'))],
1618
revision_id=b'A-id')
1619
builder.build_snapshot([b'A-id'], [
1620
('modify', ('foo', b'new-content\n'))],
1621
revision_id=b'B-id')
1622
builder.build_snapshot([b'A-id'],
1623
[('unversion', 'foo')],
1624
revision_id=b'C-id')
1625
builder.build_snapshot([b'C-id', b'B-id'], [], revision_id=b'E-id')
1626
builder.build_snapshot([b'B-id', b'C-id'], [], revision_id=b'D-id')
1627
merge_obj = self.make_merge_obj(builder, b'E-id')
1629
entries = list(merge_obj._entries_lca())
1630
root_id = b'a-root-id'
1631
self.assertEqual([(b'foo-id', True,
1632
((u'foo', [u'foo', None]), None, u'foo'),
1633
((root_id, [root_id, None]), None, root_id),
1634
((u'foo', [u'foo', None]), None, 'foo'),
1635
((False, [False, None]), None, False),
1639
def test_only_in_one_lca(self):
1642
# B C B nothing, C add file
1644
# D E D still has nothing, E removes file
1647
# C => D, removed the file
1649
# C => E, removed the file
1650
# Thus D & E have identical changes, and this is a no-op
1653
# A => C, add file, thus C supersedes B
1654
# w/ C=BASE, D=THIS, E=OTHER we have 'happy convergence'
1655
builder = self.get_builder()
1656
builder.build_snapshot(None,
1657
[('add', (u'', b'a-root-id', 'directory', None))],
1658
revision_id=b'A-id')
1659
builder.build_snapshot([b'A-id'], [], revision_id=b'B-id')
1660
builder.build_snapshot([b'A-id'],
1661
[('add', (u'a', b'a-id', 'file', b'a\nb\nc\n'))],
1662
revision_id=b'C-id')
1663
builder.build_snapshot([b'C-id', b'B-id'],
1664
[('unversion', 'a')],
1665
revision_id=b'E-id')
1666
builder.build_snapshot([b'B-id', b'C-id'], [], revision_id=b'D-id')
1667
merge_obj = self.make_merge_obj(builder, b'E-id')
1669
entries = list(merge_obj._entries_lca())
1670
self.assertEqual([], entries)
1672
def test_only_in_other(self):
1673
builder = self.get_builder()
1674
builder.build_snapshot(None,
1675
[('add', (u'', b'a-root-id', 'directory', None))],
1676
revision_id=b'A-id')
1677
builder.build_snapshot([b'A-id'], [], revision_id=b'B-id')
1678
builder.build_snapshot([b'A-id'], [], revision_id=b'C-id')
1679
builder.build_snapshot([b'C-id', b'B-id'],
1680
[('add', (u'a', b'a-id', 'file', b'a\nb\nc\n'))],
1681
revision_id=b'E-id')
1682
builder.build_snapshot([b'B-id', b'C-id'], [], revision_id=b'D-id')
1683
merge_obj = self.make_merge_obj(builder, b'E-id')
1685
entries = list(merge_obj._entries_lca())
1686
root_id = b'a-root-id'
1687
self.assertEqual([(b'a-id', True,
1688
((None, [None, None]), u'a', None),
1689
((None, [None, None]), root_id, None),
1690
((None, [None, None]), u'a', None),
1691
((None, [None, None]), False, None),
1695
def test_one_lca_supersedes(self):
1696
# One LCA supersedes the other LCAs last modified value, but the
1697
# value is not the same as BASE.
1698
# A base, introduces 'foo', last mod A
1700
# B C B modifies 'foo' (mod B), C does nothing (mod A)
1702
# D E D does nothing (mod B), E updates 'foo' (mod E)
1704
# F G F updates 'foo' (mod F). G does nothing (mod E)
1706
# At this point, G should not be considered to modify 'foo', even
1707
# though its LCAs disagree. This is because the modification in E
1708
# completely supersedes the value in D.
1709
builder = self.get_builder()
1710
builder.build_snapshot(None,
1711
[('add', (u'', b'a-root-id', 'directory', None)),
1712
('add', (u'foo', b'foo-id', 'file', b'A content\n'))],
1713
revision_id=b'A-id')
1714
builder.build_snapshot([b'A-id'], [], revision_id=b'C-id')
1715
builder.build_snapshot([b'A-id'],
1716
[('modify', ('foo', b'B content\n'))],
1717
revision_id=b'B-id')
1718
builder.build_snapshot([b'B-id', b'C-id'], [], revision_id=b'D-id')
1719
builder.build_snapshot([b'C-id', b'B-id'],
1720
[('modify', ('foo', b'E content\n'))],
1721
revision_id=b'E-id')
1722
builder.build_snapshot([b'E-id', b'D-id'], [], revision_id=b'G-id')
1723
builder.build_snapshot([b'D-id', b'E-id'],
1724
[('modify', ('foo', b'F content\n'))],
1725
revision_id=b'F-id')
1726
merge_obj = self.make_merge_obj(builder, b'G-id')
1728
self.assertEqual([], list(merge_obj._entries_lca()))
1730
def test_one_lca_supersedes_path(self):
1731
# Double-criss-cross merge, the ultimate base value is different from
1735
# B C B value 'bar', C = 'foo'
1737
# D E D = 'bar', E supersedes to 'bing'
1739
# F G F = 'bing', G supersedes to 'barry'
1741
# In this case, we technically should not care about the value 'bar' for
1742
# D, because it was clearly superseded by E's 'bing'. The
1743
# per-file/attribute graph would actually look like:
1752
# Because the other side of the merge never modifies the value, it just
1753
# takes the value from the merge.
1755
# ATM this fails because we will prune 'foo' from the LCAs, but we
1756
# won't prune 'bar'. This is getting far off into edge-case land, so we
1757
# aren't supporting it yet.
1759
builder = self.get_builder()
1760
builder.build_snapshot(None,
1761
[('add', (u'', b'a-root-id', 'directory', None)),
1762
('add', (u'foo', b'foo-id', 'file', b'A content\n'))],
1763
revision_id=b'A-id')
1764
builder.build_snapshot([b'A-id'], [], revision_id=b'C-id')
1765
builder.build_snapshot([b'A-id'],
1766
[('rename', ('foo', 'bar'))],
1767
revision_id=b'B-id')
1768
builder.build_snapshot([b'B-id', b'C-id'], [], revision_id=b'D-id')
1769
builder.build_snapshot([b'C-id', b'B-id'],
1770
[('rename', ('foo', 'bing'))],
1771
revision_id=b'E-id') # override to bing
1772
builder.build_snapshot([b'E-id', b'D-id'],
1773
[('rename', ('bing', 'barry'))],
1774
revision_id=b'G-id') # override to barry
1775
builder.build_snapshot([b'D-id', b'E-id'],
1776
[('rename', ('bar', 'bing'))],
1777
revision_id=b'F-id') # Merge in E's change
1778
merge_obj = self.make_merge_obj(builder, b'G-id')
1780
self.expectFailure("We don't do an actual heads() check on lca values,"
1781
" or use the per-attribute graph",
1782
self.assertEqual, [], list(merge_obj._entries_lca()))
1784
def test_one_lca_accidentally_pruned(self):
1785
# Another incorrect resolution from the same basic flaw:
1788
# B C B value 'bar', C = 'foo'
1790
# D E D = 'bar', E reverts to 'foo'
1792
# F G F = 'bing', G switches to 'bar'
1794
# 'bar' will not be seen as an interesting change, because 'foo' will
1795
# be pruned from the LCAs, even though it was newly introduced by E
1797
builder = self.get_builder()
1798
builder.build_snapshot(None,
1799
[('add', (u'', b'a-root-id', 'directory', None)),
1800
('add', (u'foo', b'foo-id', 'file', b'A content\n'))],
1801
revision_id=b'A-id')
1802
builder.build_snapshot([b'A-id'], [], revision_id=b'C-id')
1803
builder.build_snapshot([b'A-id'],
1804
[('rename', ('foo', 'bar'))],
1805
revision_id=b'B-id')
1806
builder.build_snapshot([b'B-id', b'C-id'], [], revision_id=b'D-id')
1807
builder.build_snapshot([b'C-id', b'B-id'], [], revision_id=b'E-id')
1808
builder.build_snapshot([b'E-id', b'D-id'],
1809
[('rename', ('foo', 'bar'))],
1810
revision_id=b'G-id')
1811
builder.build_snapshot([b'D-id', b'E-id'],
1812
[('rename', ('bar', 'bing'))],
1813
revision_id=b'F-id') # should end up conflicting
1814
merge_obj = self.make_merge_obj(builder, b'G-id')
1816
entries = list(merge_obj._entries_lca())
1817
root_id = b'a-root-id'
1818
self.expectFailure("We prune values from BASE even when relevant.",
1821
((root_id, [root_id, root_id]), root_id, root_id),
1822
((u'foo', [u'bar', u'foo']), u'bar', u'bing'),
1823
((False, [False, False]), False, False),
1827
def test_both_sides_revert(self):
1828
# Both sides of a criss-cross revert the text to the lca
1829
# A base, introduces 'foo'
1831
# B C B modifies 'foo', C modifies 'foo'
1833
# D E D reverts to B, E reverts to C
1834
# This should conflict
1835
builder = self.get_builder()
1836
builder.build_snapshot(None,
1837
[('add', (u'', b'a-root-id', 'directory', None)),
1838
('add', (u'foo', b'foo-id', 'file', b'A content\n'))],
1839
revision_id=b'A-id')
1840
builder.build_snapshot([b'A-id'],
1841
[('modify', ('foo', b'B content\n'))],
1842
revision_id=b'B-id')
1843
builder.build_snapshot([b'A-id'],
1844
[('modify', ('foo', b'C content\n'))],
1845
revision_id=b'C-id')
1846
builder.build_snapshot([b'C-id', b'B-id'], [], revision_id=b'E-id')
1847
builder.build_snapshot([b'B-id', b'C-id'], [], revision_id=b'D-id')
1848
merge_obj = self.make_merge_obj(builder, b'E-id')
1850
entries = list(merge_obj._entries_lca())
1851
root_id = b'a-root-id'
1852
self.assertEqual([(b'foo-id', True,
1853
((u'foo', [u'foo', u'foo']), u'foo', u'foo'),
1854
((root_id, [root_id, root_id]), root_id, root_id),
1855
((u'foo', [u'foo', u'foo']), u'foo', u'foo'),
1856
((False, [False, False]), False, False),
1860
def test_different_lca_resolve_one_side_updates_content(self):
1861
# Both sides converge, but then one side updates the text.
1862
# A base, introduces 'foo'
1864
# B C B modifies 'foo', C modifies 'foo'
1866
# D E D reverts to B, E reverts to C
1868
# F F updates to a new value
1869
# We need to emit an entry for 'foo', because D & E differed on the
1871
builder = self.get_builder()
1872
builder.build_snapshot(None,
1873
[('add', (u'', b'a-root-id', 'directory', None)),
1874
('add', (u'foo', b'foo-id', 'file', b'A content\n'))],
1875
revision_id=b'A-id')
1876
builder.build_snapshot([b'A-id'],
1877
[('modify', ('foo', b'B content\n'))],
1878
revision_id=b'B-id')
1879
builder.build_snapshot([b'A-id'],
1880
[('modify', ('foo', b'C content\n'))],
1881
revision_id=b'C-id', )
1882
builder.build_snapshot([b'C-id', b'B-id'], [], revision_id=b'E-id')
1883
builder.build_snapshot([b'B-id', b'C-id'], [], revision_id=b'D-id')
1884
builder.build_snapshot([b'D-id'],
1885
[('modify', ('foo', b'F content\n'))],
1886
revision_id=b'F-id')
1887
merge_obj = self.make_merge_obj(builder, b'E-id')
1889
entries = list(merge_obj._entries_lca())
1890
root_id = b'a-root-id'
1891
self.assertEqual([(b'foo-id', True,
1892
((u'foo', [u'foo', u'foo']), u'foo', u'foo'),
1893
((root_id, [root_id, root_id]), root_id, root_id),
1894
((u'foo', [u'foo', u'foo']), u'foo', u'foo'),
1895
((False, [False, False]), False, False),
1899
def test_same_lca_resolution_one_side_updates_content(self):
1900
# Both sides converge, but then one side updates the text.
1901
# A base, introduces 'foo'
1903
# B C B modifies 'foo', C modifies 'foo'
1905
# D E D and E use C's value
1907
# F F updates to a new value
1908
# I think it is a bug that this conflicts, but we don't have a way to
1909
# detect otherwise. And because of:
1910
# test_different_lca_resolve_one_side_updates_content
1911
# We need to conflict.
1913
builder = self.get_builder()
1914
builder.build_snapshot(None,
1915
[('add', (u'', b'a-root-id', 'directory', None)),
1916
('add', (u'foo', b'foo-id', 'file', b'A content\n'))],
1917
revision_id=b'A-id')
1918
builder.build_snapshot([b'A-id'],
1919
[('modify', ('foo', b'B content\n'))],
1920
revision_id=b'B-id')
1921
builder.build_snapshot([b'A-id'],
1922
[('modify', ('foo', b'C content\n'))],
1923
revision_id=b'C-id')
1924
builder.build_snapshot([b'C-id', b'B-id'], [], revision_id=b'E-id')
1925
builder.build_snapshot([b'B-id', b'C-id'],
1926
[('modify', ('foo', b'C content\n'))],
1927
revision_id=b'D-id') # Same as E
1928
builder.build_snapshot([b'D-id'],
1929
[('modify', ('foo', b'F content\n'))],
1930
revision_id=b'F-id')
1931
merge_obj = self.make_merge_obj(builder, b'E-id')
1933
entries = list(merge_obj._entries_lca())
1934
self.expectFailure("We don't detect that LCA resolution was the"
1935
" same on both sides",
1936
self.assertEqual, [], entries)
1938
def test_only_path_changed(self):
1939
builder = self.get_builder()
1940
builder.build_snapshot(None,
1941
[('add', (u'', b'a-root-id', 'directory', None)),
1942
('add', (u'a', b'a-id', 'file', b'content\n'))],
1943
revision_id=b'A-id')
1944
builder.build_snapshot([b'A-id'], [], revision_id=b'B-id')
1945
builder.build_snapshot([b'A-id'], [], revision_id=b'C-id')
1946
builder.build_snapshot([b'C-id', b'B-id'],
1947
[('rename', (u'a', u'b'))],
1948
revision_id=b'E-id')
1949
builder.build_snapshot([b'B-id', b'C-id'], [], revision_id=b'D-id')
1950
merge_obj = self.make_merge_obj(builder, b'E-id')
1951
entries = list(merge_obj._entries_lca())
1952
root_id = b'a-root-id'
1953
# The content was not changed, only the path
1954
self.assertEqual([(b'a-id', False,
1955
((u'a', [u'a', u'a']), u'b', u'a'),
1956
((root_id, [root_id, root_id]), root_id, root_id),
1957
((u'a', [u'a', u'a']), u'b', u'a'),
1958
((False, [False, False]), False, False),
1962
def test_kind_changed(self):
1963
# Identical content, except 'D' changes a-id into a directory
1964
builder = self.get_builder()
1965
builder.build_snapshot(None,
1966
[('add', (u'', b'a-root-id', 'directory', None)),
1967
('add', (u'a', b'a-id', 'file', b'content\n'))],
1968
revision_id=b'A-id')
1969
builder.build_snapshot([b'A-id'], [], revision_id=b'B-id')
1970
builder.build_snapshot([b'A-id'], [], revision_id=b'C-id')
1971
builder.build_snapshot([b'C-id', b'B-id'],
1972
[('unversion', 'a'),
1974
('add', (u'a', b'a-id', 'directory', None))],
1975
revision_id=b'E-id')
1976
builder.build_snapshot([b'B-id', b'C-id'], [], revision_id=b'D-id')
1977
merge_obj = self.make_merge_obj(builder, b'E-id')
1978
entries = list(merge_obj._entries_lca())
1979
root_id = b'a-root-id'
1980
# Only the kind was changed (content)
1981
self.assertEqual([(b'a-id', True,
1982
((u'a', [u'a', u'a']), u'a', u'a'),
1983
((root_id, [root_id, root_id]), root_id, root_id),
1984
((u'a', [u'a', u'a']), u'a', u'a'),
1985
((False, [False, False]), False, False),
1989
def test_this_changed_kind(self):
1990
# Identical content, but THIS changes a file to a directory
1991
builder = self.get_builder()
1992
builder.build_snapshot(None,
1993
[('add', (u'', b'a-root-id', 'directory', None)),
1994
('add', (u'a', b'a-id', 'file', b'content\n'))],
1995
revision_id=b'A-id')
1996
builder.build_snapshot([b'A-id'], [], revision_id=b'B-id')
1997
builder.build_snapshot([b'A-id'], [], revision_id=b'C-id')
1998
builder.build_snapshot([b'C-id', b'B-id'], [], revision_id=b'E-id')
1999
builder.build_snapshot([b'B-id', b'C-id'],
2000
[('unversion', 'a'),
2002
('add', (u'a', b'a-id', 'directory', None))],
2003
revision_id=b'D-id')
2004
merge_obj = self.make_merge_obj(builder, b'E-id')
2005
entries = list(merge_obj._entries_lca())
2006
# Only the kind was changed (content)
2007
self.assertEqual([], entries)
2009
def test_interesting_files(self):
2010
# Two files modified, but we should filter one of them
2011
builder = self.get_builder()
2012
builder.build_snapshot(None,
2013
[('add', (u'', b'a-root-id', 'directory', None)),
2014
('add', (u'a', b'a-id', 'file', b'content\n')),
2015
('add', (u'b', b'b-id', 'file', b'content\n'))],
2016
revision_id=b'A-id')
2017
builder.build_snapshot([b'A-id'], [], revision_id=b'B-id')
2018
builder.build_snapshot([b'A-id'], [], revision_id=b'C-id')
2019
builder.build_snapshot([b'C-id', b'B-id'],
2020
[('modify', ('a', b'new-content\n')),
2021
('modify', ('b', b'new-content\n'))],
2022
revision_id=b'E-id')
2023
builder.build_snapshot([b'B-id', b'C-id'], [], revision_id=b'D-id')
2024
merge_obj = self.make_merge_obj(builder, b'E-id',
2025
interesting_files=['b'])
2026
entries = list(merge_obj._entries_lca())
2027
root_id = b'a-root-id'
2028
self.assertEqual([(b'b-id', True,
2029
((u'b', [u'b', u'b']), u'b', u'b'),
2030
((root_id, [root_id, root_id]), root_id, root_id),
2031
((u'b', [u'b', u'b']), u'b', u'b'),
2032
((False, [False, False]), False, False),
2036
def test_interesting_file_in_this(self):
2037
# This renamed the file, but it should still match the entry in other
2038
builder = self.get_builder()
2039
builder.build_snapshot(None,
2040
[('add', (u'', b'a-root-id', 'directory', None)),
2041
('add', (u'a', b'a-id', 'file', b'content\n')),
2042
('add', (u'b', b'b-id', 'file', b'content\n'))],
2043
revision_id=b'A-id')
2044
builder.build_snapshot([b'A-id'], [], revision_id=b'B-id')
2045
builder.build_snapshot([b'A-id'], [], revision_id=b'C-id')
2046
builder.build_snapshot([b'C-id', b'B-id'],
2047
[('modify', ('a', b'new-content\n')),
2048
('modify', ('b', b'new-content\n'))],
2049
revision_id=b'E-id')
2050
builder.build_snapshot([b'B-id', b'C-id'],
2051
[('rename', ('b', 'c'))],
2052
revision_id=b'D-id')
2053
merge_obj = self.make_merge_obj(builder, b'E-id',
2054
interesting_files=['c'])
2055
entries = list(merge_obj._entries_lca())
2056
root_id = b'a-root-id'
2057
self.assertEqual([(b'b-id', True,
2058
((u'b', [u'b', u'b']), u'b', u'c'),
2059
((root_id, [root_id, root_id]), root_id, root_id),
2060
((u'b', [u'b', u'b']), u'b', u'c'),
2061
((False, [False, False]), False, False),
2065
def test_interesting_file_in_base(self):
2066
# This renamed the file, but it should still match the entry in BASE
2067
builder = self.get_builder()
2068
builder.build_snapshot(None,
2069
[('add', (u'', b'a-root-id', 'directory', None)),
2070
('add', (u'a', b'a-id', 'file', b'content\n')),
2071
('add', (u'c', b'c-id', 'file', b'content\n'))],
2072
revision_id=b'A-id')
2073
builder.build_snapshot([b'A-id'],
2074
[('rename', ('c', 'b'))],
2075
revision_id=b'B-id')
2076
builder.build_snapshot([b'A-id'],
2077
[('rename', ('c', 'b'))],
2078
revision_id=b'C-id')
2079
builder.build_snapshot([b'C-id', b'B-id'],
2080
[('modify', ('a', b'new-content\n')),
2081
('modify', ('b', b'new-content\n'))],
2082
revision_id=b'E-id')
2083
builder.build_snapshot([b'B-id', b'C-id'], [], revision_id=b'D-id')
2084
merge_obj = self.make_merge_obj(builder, b'E-id',
2085
interesting_files=['c'])
2086
entries = list(merge_obj._entries_lca())
2087
root_id = b'a-root-id'
2088
self.assertEqual([(b'c-id', True,
2089
((u'c', [u'b', u'b']), u'b', u'b'),
2090
((root_id, [root_id, root_id]), root_id, root_id),
2091
((u'c', [u'b', u'b']), u'b', u'b'),
2092
((False, [False, False]), False, False),
2096
def test_interesting_file_in_lca(self):
2097
# This renamed the file, but it should still match the entry in LCA
2098
builder = self.get_builder()
2099
builder.build_snapshot(None,
2100
[('add', (u'', b'a-root-id', 'directory', None)),
2101
('add', (u'a', b'a-id', 'file', b'content\n')),
2102
('add', (u'b', b'b-id', 'file', b'content\n'))],
2103
revision_id=b'A-id')
2104
builder.build_snapshot([b'A-id'],
2105
[('rename', ('b', 'c'))], revision_id=b'B-id')
2106
builder.build_snapshot([b'A-id'], [], revision_id=b'C-id')
2107
builder.build_snapshot([b'C-id', b'B-id'],
2108
[('modify', ('a', b'new-content\n')),
2109
('modify', ('b', b'new-content\n'))],
2110
revision_id=b'E-id')
2111
builder.build_snapshot([b'B-id', b'C-id'],
2112
[('rename', ('c', 'b'))], revision_id=b'D-id')
2113
merge_obj = self.make_merge_obj(builder, b'E-id',
2114
interesting_files=['c'])
2115
entries = list(merge_obj._entries_lca())
2116
root_id = b'a-root-id'
2117
self.assertEqual([(b'b-id', True,
2118
((u'b', [u'c', u'b']), u'b', u'b'),
2119
((root_id, [root_id, root_id]), root_id, root_id),
2120
((u'b', [u'c', u'b']), u'b', u'b'),
2121
((False, [False, False]), False, False),
2125
def test_interesting_files(self):
2126
# Two files modified, but we should filter one of them
2127
builder = self.get_builder()
2128
builder.build_snapshot(None,
2129
[('add', (u'', b'a-root-id', 'directory', None)),
2130
('add', (u'a', b'a-id', 'file', b'content\n')),
2131
('add', (u'b', b'b-id', 'file', b'content\n'))],
2132
revision_id=b'A-id')
2133
builder.build_snapshot([b'A-id'], [], revision_id=b'B-id')
2134
builder.build_snapshot([b'A-id'], [], revision_id=b'C-id')
2135
builder.build_snapshot([b'C-id', b'B-id'],
2136
[('modify', ('a', b'new-content\n')),
2137
('modify', ('b', b'new-content\n'))], revision_id=b'E-id')
2138
builder.build_snapshot([b'B-id', b'C-id'], [], revision_id=b'D-id')
2139
merge_obj = self.make_merge_obj(builder, b'E-id',
2140
interesting_files=['b'])
2141
entries = list(merge_obj._entries_lca())
2142
root_id = b'a-root-id'
2143
self.assertEqual([(b'b-id', True,
2144
((u'b', [u'b', u'b']), u'b', u'b'),
2145
((root_id, [root_id, root_id]), root_id, root_id),
2146
((u'b', [u'b', u'b']), u'b', u'b'),
2147
((False, [False, False]), False, False),
2152
class TestMergerEntriesLCAOnDisk(tests.TestCaseWithTransport):
2154
def get_builder(self):
2155
builder = self.make_branch_builder('path')
2156
builder.start_series()
2157
self.addCleanup(builder.finish_series)
2160
def get_wt_from_builder(self, builder):
2161
"""Get a real WorkingTree from the builder."""
2162
the_branch = builder.get_branch()
2163
wt = the_branch.controldir.create_workingtree()
2164
# Note: This is a little bit ugly, but we are holding the branch
2165
# write-locked as part of the build process, and we would like to
2166
# maintain that. So we just force the WT to re-use the same
2168
wt._branch = the_branch
2170
self.addCleanup(wt.unlock)
2173
def do_merge(self, builder, other_revision_id):
2174
wt = self.get_wt_from_builder(builder)
2175
merger = _mod_merge.Merger.from_revision_ids(
2176
wt, other_revision_id)
2177
merger.merge_type = _mod_merge.Merge3Merger
2178
return wt, merger.do_merge()
2180
def test_simple_lca(self):
2181
builder = self.get_builder()
2182
builder.build_snapshot(None,
2183
[('add', (u'', b'a-root-id', 'directory', None)),
2184
('add', (u'a', b'a-id', 'file', b'a\nb\nc\n'))],
2185
revision_id=b'A-id')
2186
builder.build_snapshot([b'A-id'], [], revision_id=b'C-id')
2187
builder.build_snapshot([b'A-id'], [], revision_id=b'B-id')
2188
builder.build_snapshot([b'C-id', b'B-id'], [], revision_id=b'E-id')
2189
builder.build_snapshot([b'B-id', b'C-id'],
2190
[('modify', ('a', b'a\nb\nc\nd\ne\nf\n'))],
2191
revision_id=b'D-id')
2192
wt, conflicts = self.do_merge(builder, b'E-id')
2193
self.assertEqual(0, conflicts)
2194
# The merge should have simply update the contents of 'a'
2195
self.assertEqual(b'a\nb\nc\nd\ne\nf\n', wt.get_file_text('a'))
2197
def test_conflict_without_lca(self):
2198
# This test would cause a merge conflict, unless we use the lca trees
2199
# to determine the real ancestry
2202
# B C Path renamed to 'bar' in B
2206
# D E Path at 'bar' in D and E
2208
# F Path at 'baz' in F, which supersedes 'bar' and 'foo'
2209
builder = self.get_builder()
2210
builder.build_snapshot(None,
2211
[('add', (u'', b'a-root-id', 'directory', None)),
2212
('add', (u'foo', b'foo-id', 'file', b'a\nb\nc\n'))],
2213
revision_id=b'A-id')
2214
builder.build_snapshot([b'A-id'], [], revision_id=b'C-id')
2215
builder.build_snapshot([b'A-id'],
2216
[('rename', ('foo', 'bar'))], revision_id=b'B-id', )
2217
builder.build_snapshot([b'C-id', b'B-id'], # merge the rename
2218
[('rename', ('foo', 'bar'))], revision_id=b'E-id')
2219
builder.build_snapshot([b'E-id'],
2220
[('rename', ('bar', 'baz'))], revision_id=b'F-id')
2221
builder.build_snapshot([b'B-id', b'C-id'], [], revision_id=b'D-id')
2222
wt, conflicts = self.do_merge(builder, b'F-id')
2223
self.assertEqual(0, conflicts)
2224
# The merge should simply recognize that the final rename takes
2226
self.assertEqual('baz', wt.id2path(b'foo-id'))
2228
def test_other_deletes_lca_renames(self):
2229
# This test would cause a merge conflict, unless we use the lca trees
2230
# to determine the real ancestry
2233
# B C Path renamed to 'bar' in B
2237
# D E Path at 'bar' in D and E
2240
builder = self.get_builder()
2241
builder.build_snapshot(None,
2242
[('add', (u'', b'a-root-id', 'directory', None)),
2243
('add', (u'foo', b'foo-id', 'file', b'a\nb\nc\n'))],
2244
revision_id=b'A-id')
2245
builder.build_snapshot([b'A-id'], [], revision_id=b'C-id')
2246
builder.build_snapshot([b'A-id'],
2247
[('rename', ('foo', 'bar'))], revision_id=b'B-id')
2248
builder.build_snapshot([b'C-id', b'B-id'], # merge the rename
2249
[('rename', ('foo', 'bar'))], revision_id=b'E-id')
2250
builder.build_snapshot([b'E-id'],
2251
[('unversion', 'bar')], revision_id=b'F-id')
2252
builder.build_snapshot([b'B-id', b'C-id'], [], revision_id=b'D-id')
2253
wt, conflicts = self.do_merge(builder, b'F-id')
2254
self.assertEqual(0, conflicts)
2255
self.assertRaises(errors.NoSuchId, wt.id2path, b'foo-id')
2257
def test_executable_changes(self):
2266
# F Executable bit changed
2267
builder = self.get_builder()
2268
builder.build_snapshot(None,
2269
[('add', (u'', b'a-root-id', 'directory', None)),
2270
('add', (u'foo', b'foo-id', 'file', b'a\nb\nc\n'))],
2271
revision_id=b'A-id')
2272
builder.build_snapshot([b'A-id'], [], revision_id=b'C-id')
2273
builder.build_snapshot([b'A-id'], [], revision_id=b'B-id')
2274
builder.build_snapshot([b'B-id', b'C-id'], [], revision_id=b'D-id')
2275
builder.build_snapshot([b'C-id', b'B-id'], [], revision_id=b'E-id')
2276
# Have to use a real WT, because BranchBuilder doesn't support exec bit
2277
wt = self.get_wt_from_builder(builder)
2278
with wt.transform() as tt:
2279
tt.set_executability(True, tt.trans_id_tree_path('foo'))
2281
self.assertTrue(wt.is_executable('foo'))
2282
wt.commit('F-id', rev_id=b'F-id')
2283
# Reset to D, so that we can merge F
2284
wt.set_parent_ids([b'D-id'])
2285
wt.branch.set_last_revision_info(3, b'D-id')
2287
self.assertFalse(wt.is_executable('foo'))
2288
conflicts = wt.merge_from_branch(wt.branch, to_revision=b'F-id')
2289
self.assertEqual(0, conflicts)
2290
self.assertTrue(wt.is_executable('foo'))
2292
def test_create_symlink(self):
2293
self.requireFeature(features.SymlinkFeature)
2302
# F Add a symlink 'foo' => 'bar'
2303
# Have to use a real WT, because BranchBuilder and MemoryTree don't
2304
# have symlink support
2305
builder = self.get_builder()
2306
builder.build_snapshot(None,
2307
[('add', (u'', b'a-root-id', 'directory', None))],
2308
revision_id=b'A-id')
2309
builder.build_snapshot([b'A-id'], [], revision_id=b'C-id')
2310
builder.build_snapshot([b'A-id'], [], revision_id=b'B-id')
2311
builder.build_snapshot([b'B-id', b'C-id'], [], revision_id=b'D-id')
2312
builder.build_snapshot([b'C-id', b'B-id'], [], revision_id=b'E-id')
2313
# Have to use a real WT, because BranchBuilder doesn't support exec bit
2314
wt = self.get_wt_from_builder(builder)
2315
os.symlink('bar', 'path/foo')
2316
wt.add(['foo'], [b'foo-id'])
2317
self.assertEqual('bar', wt.get_symlink_target('foo'))
2318
wt.commit('add symlink', rev_id=b'F-id')
2319
# Reset to D, so that we can merge F
2320
wt.set_parent_ids([b'D-id'])
2321
wt.branch.set_last_revision_info(3, b'D-id')
2323
self.assertFalse(wt.is_versioned('foo'))
2324
conflicts = wt.merge_from_branch(wt.branch, to_revision=b'F-id')
2325
self.assertEqual(0, conflicts)
2326
self.assertEqual(b'foo-id', wt.path2id('foo'))
2327
self.assertEqual('bar', wt.get_symlink_target('foo'))
2329
def test_both_sides_revert(self):
2330
# Both sides of a criss-cross revert the text to the lca
2331
# A base, introduces 'foo'
2333
# B C B modifies 'foo', C modifies 'foo'
2335
# D E D reverts to B, E reverts to C
2336
# This should conflict
2337
# This must be done with a real WorkingTree, because normally their
2338
# inventory contains "None" rather than a real sha1
2339
builder = self.get_builder()
2340
builder.build_snapshot(None,
2341
[('add', (u'', b'a-root-id', 'directory', None)),
2342
('add', (u'foo', b'foo-id', 'file', b'A content\n'))],
2343
revision_id=b'A-id')
2344
builder.build_snapshot([b'A-id'],
2345
[('modify', ('foo', b'B content\n'))],
2346
revision_id=b'B-id')
2347
builder.build_snapshot([b'A-id'],
2348
[('modify', ('foo', b'C content\n'))],
2349
revision_id=b'C-id')
2350
builder.build_snapshot([b'C-id', b'B-id'], [],
2351
revision_id=b'E-id')
2352
builder.build_snapshot([b'B-id', b'C-id'], [],
2353
revision_id=b'D-id')
2354
wt, conflicts = self.do_merge(builder, b'E-id')
2355
self.assertEqual(1, conflicts)
2356
self.assertEqualDiff(b'<<<<<<< TREE\n'
2360
b'>>>>>>> MERGE-SOURCE\n',
2361
wt.get_file_text('foo'))
2363
def test_modified_symlink(self):
2364
self.requireFeature(features.SymlinkFeature)
2365
# A Create symlink foo => bar
2367
# B C B relinks foo => baz
2371
# D E D & E have foo => baz
2373
# F F changes it to bing
2375
# Merging D & F should result in F cleanly overriding D, because D's
2376
# value actually comes from B
2378
# Have to use a real WT, because BranchBuilder and MemoryTree don't
2379
# have symlink support
2380
wt = self.make_branch_and_tree('path')
2382
self.addCleanup(wt.unlock)
2383
os.symlink('bar', 'path/foo')
2384
wt.add(['foo'], [b'foo-id'])
2385
wt.commit('add symlink', rev_id=b'A-id')
2386
os.remove('path/foo')
2387
os.symlink('baz', 'path/foo')
2388
wt.commit('foo => baz', rev_id=b'B-id')
2389
wt.set_last_revision(b'A-id')
2390
wt.branch.set_last_revision_info(1, b'A-id')
2392
wt.commit('C', rev_id=b'C-id')
2393
wt.merge_from_branch(wt.branch, b'B-id')
2394
self.assertEqual('baz', wt.get_symlink_target('foo'))
2395
wt.commit('E merges C & B', rev_id=b'E-id')
2396
os.remove('path/foo')
2397
os.symlink('bing', 'path/foo')
2398
wt.commit('F foo => bing', rev_id=b'F-id')
2399
wt.set_last_revision(b'B-id')
2400
wt.branch.set_last_revision_info(2, b'B-id')
2402
wt.merge_from_branch(wt.branch, b'C-id')
2403
wt.commit('D merges B & C', rev_id=b'D-id')
2404
conflicts = wt.merge_from_branch(wt.branch, to_revision=b'F-id')
2405
self.assertEqual(0, conflicts)
2406
self.assertEqual('bing', wt.get_symlink_target('foo'))
2408
def test_renamed_symlink(self):
2409
self.requireFeature(features.SymlinkFeature)
2410
# A Create symlink foo => bar
2412
# B C B renames foo => barry
2416
# D E D & E have barry
2418
# F F renames barry to blah
2420
# Merging D & F should result in F cleanly overriding D, because D's
2421
# value actually comes from B
2423
wt = self.make_branch_and_tree('path')
2425
self.addCleanup(wt.unlock)
2426
os.symlink('bar', 'path/foo')
2427
wt.add(['foo'], [b'foo-id'])
2428
wt.commit('A add symlink', rev_id=b'A-id')
2429
wt.rename_one('foo', 'barry')
2430
wt.commit('B foo => barry', rev_id=b'B-id')
2431
wt.set_last_revision(b'A-id')
2432
wt.branch.set_last_revision_info(1, b'A-id')
2434
wt.commit('C', rev_id=b'C-id')
2435
wt.merge_from_branch(wt.branch, b'B-id')
2436
self.assertEqual('barry', wt.id2path(b'foo-id'))
2437
self.assertEqual('bar', wt.get_symlink_target('barry'))
2438
wt.commit('E merges C & B', rev_id=b'E-id')
2439
wt.rename_one('barry', 'blah')
2440
wt.commit('F barry => blah', rev_id=b'F-id')
2441
wt.set_last_revision(b'B-id')
2442
wt.branch.set_last_revision_info(2, b'B-id')
2444
wt.merge_from_branch(wt.branch, b'C-id')
2445
wt.commit('D merges B & C', rev_id=b'D-id')
2446
self.assertEqual('barry', wt.id2path(b'foo-id'))
2447
# Check the output of the Merger object directly
2448
merger = _mod_merge.Merger.from_revision_ids(wt, b'F-id')
2449
merger.merge_type = _mod_merge.Merge3Merger
2450
merge_obj = merger.make_merger()
2451
root_id = wt.path2id('')
2452
entries = list(merge_obj._entries_lca())
2453
# No content change, just a path change
2454
self.assertEqual([(b'foo-id', False,
2455
((u'foo', [u'barry', u'foo']), u'blah', u'barry'),
2456
((root_id, [root_id, root_id]), root_id, root_id),
2457
((u'foo', [u'barry', u'foo']), u'blah', u'barry'),
2458
((False, [False, False]), False, False),
2461
conflicts = wt.merge_from_branch(wt.branch, to_revision=b'F-id')
2462
self.assertEqual(0, conflicts)
2463
self.assertEqual('blah', wt.id2path(b'foo-id'))
2465
def test_symlink_no_content_change(self):
2466
self.requireFeature(features.SymlinkFeature)
2467
# A Create symlink foo => bar
2469
# B C B relinks foo => baz
2473
# D E D & E have foo => baz
2475
# F F has foo => bing
2477
# Merging E into F should not cause a conflict, because E doesn't have
2478
# a content change relative to the LCAs (it does relative to A)
2479
wt = self.make_branch_and_tree('path')
2481
self.addCleanup(wt.unlock)
2482
os.symlink('bar', 'path/foo')
2483
wt.add(['foo'], [b'foo-id'])
2484
wt.commit('add symlink', rev_id=b'A-id')
2485
os.remove('path/foo')
2486
os.symlink('baz', 'path/foo')
2487
wt.commit('foo => baz', rev_id=b'B-id')
2488
wt.set_last_revision(b'A-id')
2489
wt.branch.set_last_revision_info(1, b'A-id')
2491
wt.commit('C', rev_id=b'C-id')
2492
wt.merge_from_branch(wt.branch, b'B-id')
2493
self.assertEqual('baz', wt.get_symlink_target('foo'))
2494
wt.commit('E merges C & B', rev_id=b'E-id')
2495
wt.set_last_revision(b'B-id')
2496
wt.branch.set_last_revision_info(2, b'B-id')
2498
wt.merge_from_branch(wt.branch, b'C-id')
2499
wt.commit('D merges B & C', rev_id=b'D-id')
2500
os.remove('path/foo')
2501
os.symlink('bing', 'path/foo')
2502
wt.commit('F foo => bing', rev_id=b'F-id')
2504
# Check the output of the Merger object directly
2505
merger = _mod_merge.Merger.from_revision_ids(wt, b'E-id')
2506
merger.merge_type = _mod_merge.Merge3Merger
2507
merge_obj = merger.make_merger()
2508
# Nothing interesting happened in OTHER relative to BASE
2509
self.assertEqual([], list(merge_obj._entries_lca()))
2510
# Now do a real merge, just to test the rest of the stack
2511
conflicts = wt.merge_from_branch(wt.branch, to_revision=b'E-id')
2512
self.assertEqual(0, conflicts)
2513
self.assertEqual('bing', wt.get_symlink_target('foo'))
2515
def test_symlink_this_changed_kind(self):
2516
self.requireFeature(features.SymlinkFeature)
2519
# B C B creates symlink foo => bar
2523
# D E D changes foo into a file, E has foo => bing
2525
# Mostly, this is trying to test that we don't try to os.readlink() on
2526
# a file, or when there is nothing there
2527
wt = self.make_branch_and_tree('path')
2529
self.addCleanup(wt.unlock)
2530
wt.commit('base', rev_id=b'A-id')
2531
os.symlink('bar', 'path/foo')
2532
wt.add(['foo'], [b'foo-id'])
2533
wt.commit('add symlink foo => bar', rev_id=b'B-id')
2534
wt.set_last_revision(b'A-id')
2535
wt.branch.set_last_revision_info(1, b'A-id')
2537
wt.commit('C', rev_id=b'C-id')
2538
wt.merge_from_branch(wt.branch, b'B-id')
2539
self.assertEqual('bar', wt.get_symlink_target('foo'))
2540
os.remove('path/foo')
2541
# We have to change the link in E, or it won't try to do a comparison
2542
os.symlink('bing', 'path/foo')
2543
wt.commit('E merges C & B, overrides to bing', rev_id=b'E-id')
2544
wt.set_last_revision(b'B-id')
2545
wt.branch.set_last_revision_info(2, b'B-id')
2547
wt.merge_from_branch(wt.branch, b'C-id')
2548
os.remove('path/foo')
2549
self.build_tree_contents([('path/foo', b'file content\n')])
2550
# XXX: workaround, WT doesn't detect kind changes unless you do
2552
list(wt.iter_changes(wt.basis_tree()))
2553
wt.commit('D merges B & C, makes it a file', rev_id=b'D-id')
2555
merger = _mod_merge.Merger.from_revision_ids(wt, b'E-id')
2556
merger.merge_type = _mod_merge.Merge3Merger
2557
merge_obj = merger.make_merger()
2558
entries = list(merge_obj._entries_lca())
2559
root_id = wt.path2id('')
2560
self.assertEqual([(b'foo-id', True,
2561
((None, [u'foo', None]), u'foo', u'foo'),
2562
((None, [root_id, None]), root_id, root_id),
2563
((None, [u'foo', None]), u'foo', u'foo'),
2564
((None, [False, None]), False, False),
2568
def test_symlink_all_wt(self):
2569
"""Check behavior if all trees are Working Trees."""
2570
self.requireFeature(features.SymlinkFeature)
2571
# The big issue is that entry.symlink_target is None for WorkingTrees.
2572
# So we need to make sure we handle that case correctly.
2575
# B C B relinks foo => baz
2577
# D E D & E have foo => baz
2579
# F F changes it to bing
2580
# Merging D & F should result in F cleanly overriding D, because D's
2581
# value actually comes from B
2583
wt = self.make_branch_and_tree('path')
2585
self.addCleanup(wt.unlock)
2586
os.symlink('bar', 'path/foo')
2587
wt.add(['foo'], [b'foo-id'])
2588
wt.commit('add symlink', rev_id=b'A-id')
2589
os.remove('path/foo')
2590
os.symlink('baz', 'path/foo')
2591
wt.commit('foo => baz', rev_id=b'B-id')
2592
wt.set_last_revision(b'A-id')
2593
wt.branch.set_last_revision_info(1, b'A-id')
2595
wt.commit('C', rev_id=b'C-id')
2596
wt.merge_from_branch(wt.branch, b'B-id')
2597
self.assertEqual('baz', wt.get_symlink_target('foo'))
2598
wt.commit('E merges C & B', rev_id=b'E-id')
2599
os.remove('path/foo')
2600
os.symlink('bing', 'path/foo')
2601
wt.commit('F foo => bing', rev_id=b'F-id')
2602
wt.set_last_revision(b'B-id')
2603
wt.branch.set_last_revision_info(2, b'B-id')
2605
wt.merge_from_branch(wt.branch, b'C-id')
2606
wt.commit('D merges B & C', rev_id=b'D-id')
2607
wt_base = wt.controldir.sprout('base', b'A-id').open_workingtree()
2609
self.addCleanup(wt_base.unlock)
2610
wt_lca1 = wt.controldir.sprout('b-tree', b'B-id').open_workingtree()
2612
self.addCleanup(wt_lca1.unlock)
2613
wt_lca2 = wt.controldir.sprout('c-tree', b'C-id').open_workingtree()
2615
self.addCleanup(wt_lca2.unlock)
2616
wt_other = wt.controldir.sprout('other', b'F-id').open_workingtree()
2617
wt_other.lock_read()
2618
self.addCleanup(wt_other.unlock)
2619
merge_obj = _mod_merge.Merge3Merger(wt, wt, wt_base,
2620
wt_other, lca_trees=[wt_lca1, wt_lca2], do_merge=False)
2621
entries = list(merge_obj._entries_lca())
2622
root_id = wt.path2id('')
2623
self.assertEqual([(b'foo-id', True,
2624
((u'foo', [u'foo', u'foo']), u'foo', u'foo'),
2625
((root_id, [root_id, root_id]), root_id, root_id),
2626
((u'foo', [u'foo', u'foo']), u'foo', u'foo'),
2627
((False, [False, False]), False, False),
2631
def test_other_reverted_path_to_base(self):
2634
# B C Path at 'bar' in B
2641
builder = self.get_builder()
2642
builder.build_snapshot(None,
2643
[('add', (u'', b'a-root-id', 'directory', None)),
2644
('add', (u'foo', b'foo-id', 'file', b'a\nb\nc\n'))],
2645
revision_id=b'A-id')
2646
builder.build_snapshot([b'A-id'], [], revision_id=b'C-id')
2647
builder.build_snapshot([b'A-id'],
2648
[('rename', ('foo', 'bar'))], revision_id=b'B-id')
2649
builder.build_snapshot([b'C-id', b'B-id'],
2650
[('rename', ('foo', 'bar'))], revision_id=b'E-id') # merge the rename
2651
builder.build_snapshot([b'E-id'],
2652
[('rename', ('bar', 'foo'))], revision_id=b'F-id') # Rename back to BASE
2653
builder.build_snapshot([b'B-id', b'C-id'], [], revision_id=b'D-id')
2654
wt, conflicts = self.do_merge(builder, b'F-id')
2655
self.assertEqual(0, conflicts)
2656
self.assertEqual('foo', wt.id2path(b'foo-id'))
2658
def test_other_reverted_content_to_base(self):
2659
builder = self.get_builder()
2660
builder.build_snapshot(None,
2661
[('add', (u'', b'a-root-id', 'directory', None)),
2662
('add', (u'foo', b'foo-id', 'file', b'base content\n'))],
2663
revision_id=b'A-id')
2664
builder.build_snapshot([b'A-id'], [], revision_id=b'C-id')
2665
builder.build_snapshot([b'A-id'],
2666
[('modify', ('foo', b'B content\n'))],
2667
revision_id=b'B-id')
2668
builder.build_snapshot([b'C-id', b'B-id'],
2669
[('modify', ('foo', b'B content\n'))],
2670
revision_id=b'E-id') # merge the content
2671
builder.build_snapshot([b'E-id'],
2672
[('modify', ('foo', b'base content\n'))],
2673
revision_id=b'F-id') # Revert back to BASE
2674
builder.build_snapshot([b'B-id', b'C-id'], [], revision_id=b'D-id')
2675
wt, conflicts = self.do_merge(builder, b'F-id')
2676
self.assertEqual(0, conflicts)
2677
# TODO: We need to use the per-file graph to properly select a BASE
2678
# before this will work. Or at least use the LCA trees to find
2679
# the appropriate content base. (which is B, not A).
2680
self.assertEqual(b'base content\n', wt.get_file_text('foo'))
2682
def test_other_modified_content(self):
2683
builder = self.get_builder()
2684
builder.build_snapshot(None,
2685
[('add', (u'', b'a-root-id', 'directory', None)),
2686
('add', (u'foo', b'foo-id', 'file', b'base content\n'))],
2687
revision_id=b'A-id')
2688
builder.build_snapshot([b'A-id'], [], revision_id=b'C-id')
2689
builder.build_snapshot([b'A-id'],
2690
[('modify', ('foo', b'B content\n'))],
2691
revision_id=b'B-id')
2692
builder.build_snapshot([b'C-id', b'B-id'],
2693
[('modify', ('foo', b'B content\n'))],
2694
revision_id=b'E-id') # merge the content
2695
builder.build_snapshot([b'E-id'],
2696
[('modify', ('foo', b'F content\n'))],
2697
revision_id=b'F-id') # Override B content
2698
builder.build_snapshot([b'B-id', b'C-id'], [], revision_id=b'D-id')
2699
wt, conflicts = self.do_merge(builder, b'F-id')
2700
self.assertEqual(0, conflicts)
2701
self.assertEqual(b'F content\n', wt.get_file_text('foo'))
2703
def test_all_wt(self):
2704
"""Check behavior if all trees are Working Trees."""
2705
# The big issue is that entry.revision is None for WorkingTrees. (as is
2706
# entry.text_sha1, etc. So we need to make sure we handle that case
2708
# A Content of 'foo', path of 'a'
2710
# B C B modifies content, C renames 'a' => 'b'
2712
# D E E updates content, renames 'b' => 'c'
2713
builder = self.get_builder()
2714
builder.build_snapshot(None,
2715
[('add', (u'', b'a-root-id', 'directory', None)),
2716
('add', (u'a', b'a-id', 'file', b'base content\n')),
2717
('add', (u'foo', b'foo-id', 'file', b'base content\n'))],
2718
revision_id=b'A-id')
2719
builder.build_snapshot([b'A-id'],
2720
[('modify', ('foo', b'B content\n'))],
2721
revision_id=b'B-id')
2722
builder.build_snapshot([b'A-id'],
2723
[('rename', ('a', 'b'))],
2724
revision_id=b'C-id')
2725
builder.build_snapshot([b'C-id', b'B-id'],
2726
[('rename', ('b', 'c')),
2727
('modify', ('foo', b'E content\n'))],
2728
revision_id=b'E-id')
2729
builder.build_snapshot([b'B-id', b'C-id'],
2730
[('rename', ('a', 'b'))], revision_id=b'D-id') # merged change
2731
wt_this = self.get_wt_from_builder(builder)
2732
wt_base = wt_this.controldir.sprout('base', b'A-id').open_workingtree()
2734
self.addCleanup(wt_base.unlock)
2735
wt_lca1 = wt_this.controldir.sprout(
2736
'b-tree', b'B-id').open_workingtree()
2738
self.addCleanup(wt_lca1.unlock)
2739
wt_lca2 = wt_this.controldir.sprout(
2740
'c-tree', b'C-id').open_workingtree()
2742
self.addCleanup(wt_lca2.unlock)
2743
wt_other = wt_this.controldir.sprout(
2744
'other', b'E-id').open_workingtree()
2745
wt_other.lock_read()
2746
self.addCleanup(wt_other.unlock)
2747
merge_obj = _mod_merge.Merge3Merger(wt_this, wt_this, wt_base,
2748
wt_other, lca_trees=[wt_lca1, wt_lca2], do_merge=False)
2749
entries = list(merge_obj._entries_lca())
2750
root_id = b'a-root-id'
2751
self.assertEqual([(b'a-id', False,
2752
((u'a', [u'a', u'b']), u'c', u'b'),
2753
((root_id, [root_id, root_id]), root_id, root_id),
2754
((u'a', [u'a', u'b']), u'c', u'b'),
2755
((False, [False, False]), False, False),
2758
((u'foo', [u'foo', u'foo']), u'foo', u'foo'),
2759
((root_id, [root_id, root_id]), root_id, root_id),
2760
((u'foo', [u'foo', u'foo']), u'foo', u'foo'),
2761
((False, [False, False]), False, False),
2765
def test_nested_tree_unmodified(self):
2766
# Tested with a real WT, because BranchBuilder/MemoryTree don't handle
2768
wt = self.make_branch_and_tree('tree',
2769
format='development-subtree')
2771
self.addCleanup(wt.unlock)
2772
sub_tree = self.make_branch_and_tree('tree/sub-tree',
2773
format='development-subtree')
2774
wt.set_root_id(b'a-root-id')
2775
sub_tree.set_root_id(b'sub-tree-root')
2776
self.build_tree_contents([('tree/sub-tree/file', b'text1')])
2777
sub_tree.add('file')
2778
sub_tree.commit('foo', rev_id=b'sub-A-id')
2779
wt.add_reference(sub_tree)
2780
wt.commit('set text to 1', rev_id=b'A-id', recursive=None)
2781
# Now create a criss-cross merge in the parent, without modifying the
2783
wt.commit('B', rev_id=b'B-id', recursive=None)
2784
wt.set_last_revision(b'A-id')
2785
wt.branch.set_last_revision_info(1, b'A-id')
2786
wt.commit('C', rev_id=b'C-id', recursive=None)
2787
wt.merge_from_branch(wt.branch, to_revision=b'B-id')
2788
wt.commit('E', rev_id=b'E-id', recursive=None)
2789
wt.set_parent_ids([b'B-id', b'C-id'])
2790
wt.branch.set_last_revision_info(2, b'B-id')
2791
wt.commit('D', rev_id=b'D-id', recursive=None)
2793
merger = _mod_merge.Merger.from_revision_ids(wt, b'E-id')
2794
merger.merge_type = _mod_merge.Merge3Merger
2795
merge_obj = merger.make_merger()
2796
entries = list(merge_obj._entries_lca())
2797
self.assertEqual([], entries)
2799
def test_nested_tree_subtree_modified(self):
2800
# Tested with a real WT, because BranchBuilder/MemoryTree don't handle
2802
wt = self.make_branch_and_tree('tree',
2803
format='development-subtree')
2805
self.addCleanup(wt.unlock)
2806
sub_tree = self.make_branch_and_tree('tree/sub',
2807
format='development-subtree')
2808
wt.set_root_id(b'a-root-id')
2809
sub_tree.set_root_id(b'sub-tree-root')
2810
self.build_tree_contents([('tree/sub/file', b'text1')])
2811
sub_tree.add('file')
2812
sub_tree.commit('foo', rev_id=b'sub-A-id')
2813
wt.add_reference(sub_tree)
2814
wt.commit('set text to 1', rev_id=b'A-id', recursive=None)
2815
# Now create a criss-cross merge in the parent, without modifying the
2817
wt.commit('B', rev_id=b'B-id', recursive=None)
2818
wt.set_last_revision(b'A-id')
2819
wt.branch.set_last_revision_info(1, b'A-id')
2820
wt.commit('C', rev_id=b'C-id', recursive=None)
2821
wt.merge_from_branch(wt.branch, to_revision=b'B-id')
2822
self.build_tree_contents([('tree/sub/file', b'text2')])
2823
sub_tree.commit('modify contents', rev_id=b'sub-B-id')
2824
wt.commit('E', rev_id=b'E-id', recursive=None)
2825
wt.set_parent_ids([b'B-id', b'C-id'])
2826
wt.branch.set_last_revision_info(2, b'B-id')
2827
wt.commit('D', rev_id=b'D-id', recursive=None)
2829
merger = _mod_merge.Merger.from_revision_ids(wt, b'E-id')
2830
merger.merge_type = _mod_merge.Merge3Merger
2831
merge_obj = merger.make_merger()
2832
entries = list(merge_obj._entries_lca())
2833
# Nothing interesting about this sub-tree, because content changes are
2834
# computed at a higher level
2835
self.assertEqual([], entries)
2837
def test_nested_tree_subtree_renamed(self):
2838
# Tested with a real WT, because BranchBuilder/MemoryTree don't handle
2840
wt = self.make_branch_and_tree('tree',
2841
format='development-subtree')
2843
self.addCleanup(wt.unlock)
2844
sub_tree = self.make_branch_and_tree('tree/sub',
2845
format='development-subtree')
2846
wt.set_root_id(b'a-root-id')
2847
sub_tree.set_root_id(b'sub-tree-root')
2848
self.build_tree_contents([('tree/sub/file', b'text1')])
2849
sub_tree.add('file')
2850
sub_tree.commit('foo', rev_id=b'sub-A-id')
2851
wt.add_reference(sub_tree)
2852
wt.commit('set text to 1', rev_id=b'A-id', recursive=None)
2853
# Now create a criss-cross merge in the parent, without modifying the
2855
wt.commit('B', rev_id=b'B-id', recursive=None)
2856
wt.set_last_revision(b'A-id')
2857
wt.branch.set_last_revision_info(1, b'A-id')
2858
wt.commit('C', rev_id=b'C-id', recursive=None)
2859
wt.merge_from_branch(wt.branch, to_revision=b'B-id')
2860
wt.rename_one('sub', 'alt_sub')
2861
wt.commit('E', rev_id=b'E-id', recursive=None)
2862
wt.set_last_revision(b'B-id')
2864
wt.set_parent_ids([b'B-id', b'C-id'])
2865
wt.branch.set_last_revision_info(2, b'B-id')
2866
wt.commit('D', rev_id=b'D-id', recursive=None)
2868
merger = _mod_merge.Merger.from_revision_ids(wt, b'E-id')
2869
merger.merge_type = _mod_merge.Merge3Merger
2870
merge_obj = merger.make_merger()
2871
entries = list(merge_obj._entries_lca())
2872
root_id = b'a-root-id'
2873
self.assertEqual([(b'sub-tree-root', False,
2874
((u'sub', [u'sub', u'sub']), u'alt_sub', u'sub'),
2875
((root_id, [root_id, root_id]), root_id, root_id),
2876
((u'sub', [u'sub', u'sub']), u'alt_sub', u'sub'),
2877
((False, [False, False]), False, False),
2881
def test_nested_tree_subtree_renamed_and_modified(self):
2882
# Tested with a real WT, because BranchBuilder/MemoryTree don't handle
2884
wt = self.make_branch_and_tree('tree',
2885
format='development-subtree')
2887
self.addCleanup(wt.unlock)
2888
sub_tree = self.make_branch_and_tree('tree/sub',
2889
format='development-subtree')
2890
wt.set_root_id(b'a-root-id')
2891
sub_tree.set_root_id(b'sub-tree-root')
2892
self.build_tree_contents([('tree/sub/file', b'text1')])
2893
sub_tree.add('file')
2894
sub_tree.commit('foo', rev_id=b'sub-A-id')
2895
wt.add_reference(sub_tree)
2896
wt.commit('set text to 1', rev_id=b'A-id', recursive=None)
2897
# Now create a criss-cross merge in the parent, without modifying the
2899
wt.commit('B', rev_id=b'B-id', recursive=None)
2900
wt.set_last_revision(b'A-id')
2901
wt.branch.set_last_revision_info(1, b'A-id')
2902
wt.commit('C', rev_id=b'C-id', recursive=None)
2903
wt.merge_from_branch(wt.branch, to_revision=b'B-id')
2904
self.build_tree_contents([('tree/sub/file', b'text2')])
2905
sub_tree.commit('modify contents', rev_id=b'sub-B-id')
2906
wt.rename_one('sub', 'alt_sub')
2907
wt.commit('E', rev_id=b'E-id', recursive=None)
2908
wt.set_last_revision(b'B-id')
2910
wt.set_parent_ids([b'B-id', b'C-id'])
2911
wt.branch.set_last_revision_info(2, b'B-id')
2912
wt.commit('D', rev_id=b'D-id', recursive=None)
2914
merger = _mod_merge.Merger.from_revision_ids(wt, b'E-id')
2915
merger.merge_type = _mod_merge.Merge3Merger
2916
merge_obj = merger.make_merger()
2917
entries = list(merge_obj._entries_lca())
2918
root_id = b'a-root-id'
2919
self.assertEqual([(b'sub-tree-root', False,
2920
((u'sub', [u'sub', u'sub']), u'alt_sub', u'sub'),
2921
((root_id, [root_id, root_id]), root_id, root_id),
2922
((u'sub', [u'sub', u'sub']), u'alt_sub', u'sub'),
2923
((False, [False, False]), False, False),
2928
class TestLCAMultiWay(tests.TestCase):
2930
def assertLCAMultiWay(self, expected, base, lcas, other, this,
2931
allow_overriding_lca=True):
2932
self.assertEqual(expected, _mod_merge.Merge3Merger._lca_multi_way(
2933
(base, lcas), other, this,
2934
allow_overriding_lca=allow_overriding_lca))
2936
def test_other_equal_equal_lcas(self):
2937
"""Test when OTHER=LCA and all LCAs are identical."""
2938
self.assertLCAMultiWay('this',
2939
'bval', ['bval', 'bval'], 'bval', 'bval')
2940
self.assertLCAMultiWay('this',
2941
'bval', ['lcaval', 'lcaval'], 'lcaval', 'bval')
2942
self.assertLCAMultiWay('this',
2943
'bval', ['lcaval', 'lcaval', 'lcaval'], 'lcaval', 'bval')
2944
self.assertLCAMultiWay('this',
2945
'bval', ['lcaval', 'lcaval', 'lcaval'], 'lcaval', 'tval')
2946
self.assertLCAMultiWay('this',
2947
'bval', ['lcaval', 'lcaval', 'lcaval'], 'lcaval', None)
2949
def test_other_equal_this(self):
2950
"""Test when other and this are identical."""
2951
self.assertLCAMultiWay('this',
2952
'bval', ['bval', 'bval'], 'oval', 'oval')
2953
self.assertLCAMultiWay('this',
2954
'bval', ['lcaval', 'lcaval'], 'oval', 'oval')
2955
self.assertLCAMultiWay('this',
2956
'bval', ['cval', 'dval'], 'oval', 'oval')
2957
self.assertLCAMultiWay('this',
2958
'bval', [None, 'lcaval'], 'oval', 'oval')
2959
self.assertLCAMultiWay('this',
2960
None, [None, 'lcaval'], 'oval', 'oval')
2961
self.assertLCAMultiWay('this',
2962
None, ['lcaval', 'lcaval'], 'oval', 'oval')
2963
self.assertLCAMultiWay('this',
2964
None, ['cval', 'dval'], 'oval', 'oval')
2965
self.assertLCAMultiWay('this',
2966
None, ['cval', 'dval'], None, None)
2967
self.assertLCAMultiWay('this',
2968
None, ['cval', 'dval', 'eval', 'fval'], 'oval', 'oval')
2970
def test_no_lcas(self):
2971
self.assertLCAMultiWay('this',
2972
'bval', [], 'bval', 'tval')
2973
self.assertLCAMultiWay('other',
2974
'bval', [], 'oval', 'bval')
2975
self.assertLCAMultiWay('conflict',
2976
'bval', [], 'oval', 'tval')
2977
self.assertLCAMultiWay('this',
2978
'bval', [], 'oval', 'oval')
2980
def test_lca_supersedes_other_lca(self):
2981
"""If one lca == base, the other lca takes precedence"""
2982
self.assertLCAMultiWay('this',
2983
'bval', ['bval', 'lcaval'], 'lcaval', 'tval')
2984
self.assertLCAMultiWay('this',
2985
'bval', ['bval', 'lcaval'], 'lcaval', 'bval')
2986
# This is actually considered a 'revert' because the 'lcaval' in LCAS
2987
# supersedes the BASE val (in the other LCA) but then OTHER reverts it
2989
self.assertLCAMultiWay('other',
2990
'bval', ['bval', 'lcaval'], 'bval', 'lcaval')
2991
self.assertLCAMultiWay('conflict',
2992
'bval', ['bval', 'lcaval'], 'bval', 'tval')
2994
def test_other_and_this_pick_different_lca(self):
2995
# OTHER and THIS resolve the lca conflict in different ways
2996
self.assertLCAMultiWay('conflict',
2997
'bval', ['lca1val', 'lca2val'], 'lca1val', 'lca2val')
2998
self.assertLCAMultiWay('conflict',
2999
'bval', ['lca1val', 'lca2val', 'lca3val'], 'lca1val', 'lca2val')
3000
self.assertLCAMultiWay('conflict',
3001
'bval', ['lca1val', 'lca2val', 'bval'], 'lca1val', 'lca2val')
3003
def test_other_in_lca(self):
3004
# OTHER takes a value of one of the LCAs, THIS takes a new value, which
3005
# theoretically supersedes both LCA values and 'wins'
3006
self.assertLCAMultiWay(
3007
'this', 'bval', ['lca1val', 'lca2val'], 'lca1val', 'newval')
3008
self.assertLCAMultiWay(
3009
'this', 'bval', ['lca1val', 'lca2val', 'lca3val'], 'lca1val',
3011
self.assertLCAMultiWay('conflict',
3013
'lca2val'], 'lca1val', 'newval',
3014
allow_overriding_lca=False)
3015
self.assertLCAMultiWay('conflict',
3016
'bval', ['lca1val', 'lca2val',
3017
'lca3val'], 'lca1val', 'newval',
3018
allow_overriding_lca=False)
3019
# THIS reverted back to BASE, but that is an explicit supersede of all
3021
self.assertLCAMultiWay(
3022
'this', 'bval', ['lca1val', 'lca2val', 'lca3val'], 'lca1val',
3024
self.assertLCAMultiWay(
3025
'this', 'bval', ['lca1val', 'lca2val', 'bval'], 'lca1val', 'bval')
3026
self.assertLCAMultiWay('conflict',
3027
'bval', ['lca1val', 'lca2val',
3028
'lca3val'], 'lca1val', 'bval',
3029
allow_overriding_lca=False)
3030
self.assertLCAMultiWay('conflict',
3031
'bval', ['lca1val', 'lca2val',
3032
'bval'], 'lca1val', 'bval',
3033
allow_overriding_lca=False)
3035
def test_this_in_lca(self):
3036
# THIS takes a value of one of the LCAs, OTHER takes a new value, which
3037
# theoretically supersedes both LCA values and 'wins'
3038
self.assertLCAMultiWay(
3039
'other', 'bval', ['lca1val', 'lca2val'], 'oval', 'lca1val')
3040
self.assertLCAMultiWay(
3041
'other', 'bval', ['lca1val', 'lca2val'], 'oval', 'lca2val')
3042
self.assertLCAMultiWay('conflict',
3044
'lca2val'], 'oval', 'lca1val',
3045
allow_overriding_lca=False)
3046
self.assertLCAMultiWay('conflict',
3048
'lca2val'], 'oval', 'lca2val',
3049
allow_overriding_lca=False)
3050
# OTHER reverted back to BASE, but that is an explicit supersede of all
3052
self.assertLCAMultiWay(
3053
'other', 'bval', ['lca1val', 'lca2val', 'lca3val'], 'bval',
3055
self.assertLCAMultiWay(
3056
'conflict', 'bval', ['lca1val', 'lca2val', 'lca3val'],
3057
'bval', 'lca3val', allow_overriding_lca=False)
3059
def test_all_differ(self):
3060
self.assertLCAMultiWay(
3061
'conflict', 'bval', ['lca1val', 'lca2val'], 'oval', 'tval')
3062
self.assertLCAMultiWay(
3063
'conflict', 'bval', ['lca1val', 'lca2val', 'lca2val'], 'oval',
3065
self.assertLCAMultiWay(
3066
'conflict', 'bval', ['lca1val', 'lca2val', 'lca3val'], 'oval',
3070
class TestConfigurableFileMerger(tests.TestCaseWithTransport):
3073
super(TestConfigurableFileMerger, self).setUp()
3076
def get_merger_factory(self):
3077
# Allows the inner methods to access the test attributes
3080
class FooMerger(_mod_merge.ConfigurableFileMerger):
3082
default_files = ['bar']
3084
def merge_text(self, params):
3085
calls.append('merge_text')
3086
return ('not_applicable', None)
3088
def factory(merger):
3089
result = FooMerger(merger)
3090
# Make sure we start with a clean slate
3091
self.assertEqual(None, result.affected_files)
3092
# Track the original merger
3093
self.merger = result
3098
def _install_hook(self, factory):
3099
_mod_merge.Merger.hooks.install_named_hook('merge_file_content',
3100
factory, 'test factory')
3102
def make_builder(self):
3103
builder = test_merge_core.MergeBuilder(self.test_base_dir)
3104
self.addCleanup(builder.cleanup)
3107
def make_text_conflict(self, file_name='bar'):
3108
factory = self.get_merger_factory()
3109
self._install_hook(factory)
3110
builder = self.make_builder()
3111
builder.add_file(b'bar-id', builder.tree_root,
3112
file_name, b'text1', True)
3113
builder.change_contents(b'bar-id', other=b'text4', this=b'text3')
3116
def make_kind_change(self):
3117
factory = self.get_merger_factory()
3118
self._install_hook(factory)
3119
builder = self.make_builder()
3120
builder.add_file(b'bar-id', builder.tree_root, 'bar', b'text1', True,
3122
builder.add_dir(b'bar-dir', builder.tree_root, 'bar-id',
3123
base=False, other=False)
3126
def test_uses_this_branch(self):
3127
builder = self.make_text_conflict()
3128
with builder.make_preview_transform() as tt:
3131
def test_affected_files_cached(self):
3132
"""Ensures that the config variable is cached"""
3133
builder = self.make_text_conflict()
3134
conflicts = builder.merge()
3135
# The hook should set the variable
3136
self.assertEqual(['bar'], self.merger.affected_files)
3137
self.assertEqual(1, len(conflicts))
3139
def test_hook_called_for_text_conflicts(self):
3140
builder = self.make_text_conflict()
3142
# The hook should call the merge_text() method
3143
self.assertEqual(['merge_text'], self.calls)
3145
def test_hook_not_called_for_kind_change(self):
3146
builder = self.make_kind_change()
3148
# The hook should not call the merge_text() method
3149
self.assertEqual([], self.calls)
3151
def test_hook_not_called_for_other_files(self):
3152
builder = self.make_text_conflict('foobar')
3154
# The hook should not call the merge_text() method
3155
self.assertEqual([], self.calls)
3158
class TestMergeIntoBase(tests.TestCaseWithTransport):
3160
def setup_simple_branch(self, relpath, shape=None, root_id=None):
3161
"""One commit, containing tree specified by optional shape.
3163
Default is empty tree (just root entry).
3166
root_id = b'%s-root-id' % (relpath.encode('ascii'),)
3167
wt = self.make_branch_and_tree(relpath)
3168
wt.set_root_id(root_id)
3169
if shape is not None:
3170
adjusted_shape = [relpath + '/' + elem for elem in shape]
3171
self.build_tree(adjusted_shape)
3173
(b'%s-%s-id' % (relpath.encode('utf-8'),
3174
basename(elem.rstrip('/')).encode('ascii')))
3176
wt.add(shape, ids=ids)
3177
rev_id = b'r1-%s' % (relpath.encode('utf-8'),)
3178
wt.commit("Initial commit of %s" % (relpath,), rev_id=rev_id)
3179
self.assertEqual(root_id, wt.path2id(''))
3182
def setup_two_branches(self, custom_root_ids=True):
3183
"""Setup 2 branches, one will be a library, the other a project."""
3187
root_id = inventory.ROOT_ID
3188
project_wt = self.setup_simple_branch(
3189
'project', ['README', 'dir/', 'dir/file.c'],
3191
lib_wt = self.setup_simple_branch(
3192
'lib1', ['README', 'Makefile', 'foo.c'], root_id)
3194
return project_wt, lib_wt
3196
def do_merge_into(self, location, merge_as):
3197
"""Helper for using MergeIntoMerger.
3199
:param location: location of directory to merge from, either the
3200
location of a branch or of a path inside a branch.
3201
:param merge_as: the path in a tree to add the new directory as.
3202
:returns: the conflicts from 'do_merge'.
3204
with contextlib.ExitStack() as stack:
3205
# Open and lock the various tree and branch objects
3206
wt, subdir_relpath = WorkingTree.open_containing(merge_as)
3207
stack.enter_context(wt.lock_write())
3208
branch_to_merge, subdir_to_merge = _mod_branch.Branch.open_containing(
3210
stack.enter_context(branch_to_merge.lock_read())
3211
other_tree = branch_to_merge.basis_tree()
3212
stack.enter_context(other_tree.lock_read())
3214
merger = _mod_merge.MergeIntoMerger(
3215
this_tree=wt, other_tree=other_tree, other_branch=branch_to_merge,
3216
target_subdir=subdir_relpath, source_subpath=subdir_to_merge)
3217
merger.set_base_revision(_mod_revision.NULL_REVISION, branch_to_merge)
3218
conflicts = merger.do_merge()
3219
merger.set_pending()
3222
def assertTreeEntriesEqual(self, expected_entries, tree):
3223
"""Assert that 'tree' contains the expected inventory entries.
3225
:param expected_entries: sequence of (path, file-id) pairs.
3227
files = [(path, ie.file_id) for path, ie in tree.iter_entries_by_dir()]
3228
self.assertEqual(expected_entries, files)
3231
class TestMergeInto(TestMergeIntoBase):
3233
def test_newdir_with_unique_roots(self):
3234
"""Merge a branch with a unique root into a new directory."""
3235
project_wt, lib_wt = self.setup_two_branches()
3236
self.do_merge_into('lib1', 'project/lib1')
3237
project_wt.lock_read()
3238
self.addCleanup(project_wt.unlock)
3239
# The r1-lib1 revision should be merged into this one
3240
self.assertEqual([b'r1-project', b'r1-lib1'],
3241
project_wt.get_parent_ids())
3242
self.assertTreeEntriesEqual(
3243
[('', b'project-root-id'),
3244
('README', b'project-README-id'),
3245
('dir', b'project-dir-id'),
3246
('lib1', b'lib1-root-id'),
3247
('dir/file.c', b'project-file.c-id'),
3248
('lib1/Makefile', b'lib1-Makefile-id'),
3249
('lib1/README', b'lib1-README-id'),
3250
('lib1/foo.c', b'lib1-foo.c-id'),
3253
def test_subdir(self):
3254
"""Merge a branch into a subdirectory of an existing directory."""
3255
project_wt, lib_wt = self.setup_two_branches()
3256
self.do_merge_into('lib1', 'project/dir/lib1')
3257
project_wt.lock_read()
3258
self.addCleanup(project_wt.unlock)
3259
# The r1-lib1 revision should be merged into this one
3260
self.assertEqual([b'r1-project', b'r1-lib1'],
3261
project_wt.get_parent_ids())
3262
self.assertTreeEntriesEqual(
3263
[('', b'project-root-id'),
3264
('README', b'project-README-id'),
3265
('dir', b'project-dir-id'),
3266
('dir/file.c', b'project-file.c-id'),
3267
('dir/lib1', b'lib1-root-id'),
3268
('dir/lib1/Makefile', b'lib1-Makefile-id'),
3269
('dir/lib1/README', b'lib1-README-id'),
3270
('dir/lib1/foo.c', b'lib1-foo.c-id'),
3273
def test_newdir_with_repeat_roots(self):
3274
"""If the file-id of the dir to be merged already exists a new ID will
3275
be allocated to let the merge happen.
3277
project_wt, lib_wt = self.setup_two_branches(custom_root_ids=False)
3278
root_id = project_wt.path2id('')
3279
self.do_merge_into('lib1', 'project/lib1')
3280
project_wt.lock_read()
3281
self.addCleanup(project_wt.unlock)
3282
# The r1-lib1 revision should be merged into this one
3283
self.assertEqual([b'r1-project', b'r1-lib1'],
3284
project_wt.get_parent_ids())
3285
new_lib1_id = project_wt.path2id('lib1')
3286
self.assertNotEqual(None, new_lib1_id)
3287
self.assertTreeEntriesEqual(
3289
('README', b'project-README-id'),
3290
('dir', b'project-dir-id'),
3291
('lib1', new_lib1_id),
3292
('dir/file.c', b'project-file.c-id'),
3293
('lib1/Makefile', b'lib1-Makefile-id'),
3294
('lib1/README', b'lib1-README-id'),
3295
('lib1/foo.c', b'lib1-foo.c-id'),
3298
def test_name_conflict(self):
3299
"""When the target directory name already exists a conflict is
3300
generated and the original directory is renamed to foo.moved.
3302
dest_wt = self.setup_simple_branch('dest', ['dir/', 'dir/file.txt'])
3303
self.setup_simple_branch('src', ['README'])
3304
conflicts = self.do_merge_into('src', 'dest/dir')
3305
self.assertEqual(1, conflicts)
3307
self.addCleanup(dest_wt.unlock)
3308
# The r1-lib1 revision should be merged into this one
3309
self.assertEqual([b'r1-dest', b'r1-src'], dest_wt.get_parent_ids())
3310
self.assertTreeEntriesEqual(
3311
[('', b'dest-root-id'),
3312
('dir', b'src-root-id'),
3313
('dir.moved', b'dest-dir-id'),
3314
('dir/README', b'src-README-id'),
3315
('dir.moved/file.txt', b'dest-file.txt-id'),
3318
def test_file_id_conflict(self):
3319
"""A conflict is generated if the merge-into adds a file (or other
3320
inventory entry) with a file-id that already exists in the target tree.
3322
self.setup_simple_branch('dest', ['file.txt'])
3323
# Make a second tree with a file-id that will clash with file.txt in
3325
src_wt = self.make_branch_and_tree('src')
3326
self.build_tree(['src/README'])
3327
src_wt.add(['README'], ids=[b'dest-file.txt-id'])
3328
src_wt.commit("Rev 1 of src.", rev_id=b'r1-src')
3329
conflicts = self.do_merge_into('src', 'dest/dir')
3330
# This is an edge case that shouldn't happen to users very often. So
3331
# we don't care really about the exact presentation of the conflict,
3332
# just that there is one.
3333
self.assertEqual(1, conflicts)
3335
def test_only_subdir(self):
3336
"""When the location points to just part of a tree, merge just that
3339
dest_wt = self.setup_simple_branch('dest')
3340
self.setup_simple_branch('src', ['hello.txt', 'dir/', 'dir/foo.c'])
3341
self.do_merge_into('src/dir', 'dest/dir')
3343
self.addCleanup(dest_wt.unlock)
3344
# The r1-lib1 revision should NOT be merged into this one (this is a
3346
self.assertEqual([b'r1-dest'], dest_wt.get_parent_ids())
3347
self.assertTreeEntriesEqual(
3348
[('', b'dest-root-id'),
3349
('dir', b'src-dir-id'),
3350
('dir/foo.c', b'src-foo.c-id'),
3353
def test_only_file(self):
3354
"""An edge case: merge just one file, not a whole dir."""
3355
dest_wt = self.setup_simple_branch('dest')
3356
self.setup_simple_branch('two-file', ['file1.txt', 'file2.txt'])
3357
self.do_merge_into('two-file/file1.txt', 'dest/file1.txt')
3359
self.addCleanup(dest_wt.unlock)
3360
# The r1-lib1 revision should NOT be merged into this one
3361
self.assertEqual([b'r1-dest'], dest_wt.get_parent_ids())
3362
self.assertTreeEntriesEqual(
3363
[('', b'dest-root-id'), ('file1.txt', b'two-file-file1.txt-id')],
3366
def test_no_such_source_path(self):
3367
"""PathNotInTree is raised if the specified path in the source tree
3370
dest_wt = self.setup_simple_branch('dest')
3371
self.setup_simple_branch('src', ['dir/'])
3372
self.assertRaises(_mod_merge.PathNotInTree, self.do_merge_into,
3373
'src/no-such-dir', 'dest/foo')
3375
self.addCleanup(dest_wt.unlock)
3376
# The dest tree is unmodified.
3377
self.assertEqual([b'r1-dest'], dest_wt.get_parent_ids())
3378
self.assertTreeEntriesEqual([('', b'dest-root-id')], dest_wt)
3380
def test_no_such_target_path(self):
3381
"""PathNotInTree is also raised if the specified path in the target
3382
tree does not exist.
3384
dest_wt = self.setup_simple_branch('dest')
3385
self.setup_simple_branch('src', ['file.txt'])
3386
self.assertRaises(_mod_merge.PathNotInTree, self.do_merge_into,
3387
'src', 'dest/no-such-dir/foo')
3389
self.addCleanup(dest_wt.unlock)
3390
# The dest tree is unmodified.
3391
self.assertEqual([b'r1-dest'], dest_wt.get_parent_ids())
3392
self.assertTreeEntriesEqual([('', b'dest-root-id')], dest_wt)
3395
class TestMergeHooks(TestCaseWithTransport):
3398
super(TestMergeHooks, self).setUp()
3399
self.tree_a = self.make_branch_and_tree('tree_a')
3400
self.build_tree_contents([('tree_a/file', b'content_1')])
3401
self.tree_a.add('file', b'file-id')
3402
self.tree_a.commit('added file')
3404
self.tree_b = self.tree_a.controldir.sprout(
3405
'tree_b').open_workingtree()
3406
self.build_tree_contents([('tree_b/file', b'content_2')])
3407
self.tree_b.commit('modify file')
3409
def test_pre_merge_hook_inject_different_tree(self):
3410
tree_c = self.tree_b.controldir.sprout('tree_c').open_workingtree()
3411
self.build_tree_contents([('tree_c/file', b'content_3')])
3412
tree_c.commit("more content")
3415
def factory(merger):
3416
self.assertIsInstance(merger, _mod_merge.Merge3Merger)
3417
merger.other_tree = tree_c
3418
calls.append(merger)
3419
_mod_merge.Merger.hooks.install_named_hook('pre_merge',
3420
factory, 'test factory')
3421
self.tree_a.merge_from_branch(self.tree_b.branch)
3423
self.assertFileEqual(b"content_3", 'tree_a/file')
3424
self.assertLength(1, calls)
3426
def test_post_merge_hook_called(self):
3429
def factory(merger):
3430
self.assertIsInstance(merger, _mod_merge.Merge3Merger)
3431
calls.append(merger)
3432
_mod_merge.Merger.hooks.install_named_hook('post_merge',
3433
factory, 'test factory')
3435
self.tree_a.merge_from_branch(self.tree_b.branch)
3437
self.assertFileEqual(b"content_2", 'tree_a/file')
3438
self.assertLength(1, calls)
250
self.assertFalse('merge4' in [x[0] for x in
251
merge_type_option.iter_switches()])