236
282
self.assertEqual(tree_b.conflicts(),
237
283
[conflicts.ContentsConflict('file',
238
284
file_id='file-id')])
240
286
def test_merge_type_registry(self):
241
287
merge_type_option = option.Option.OPTIONS['merge-type']
242
self.assertFalse('merge4' in [x[0] for x in
288
self.assertFalse('merge4' in [x[0] for x in
243
289
merge_type_option.iter_switches()])
244
290
registry = _mod_merge.get_merge_type_registry()
245
291
registry.register_lazy('merge4', 'bzrlib.merge', 'Merge4Merger',
246
292
'time-travelling merge')
247
self.assertTrue('merge4' in [x[0] for x in
293
self.assertTrue('merge4' in [x[0] for x in
248
294
merge_type_option.iter_switches()])
249
295
registry.remove('merge4')
250
self.assertFalse('merge4' in [x[0] for x in
296
self.assertFalse('merge4' in [x[0] for x in
251
297
merge_type_option.iter_switches()])
299
def test_merge_other_moves_we_deleted(self):
300
tree_a = self.make_branch_and_tree('A')
302
self.addCleanup(tree_a.unlock)
303
self.build_tree(['A/a'])
305
tree_a.commit('1', rev_id='rev-1')
307
tree_a.rename_one('a', 'b')
309
bzrdir_b = tree_a.bzrdir.sprout('B', revision_id='rev-1')
310
tree_b = bzrdir_b.open_workingtree()
312
self.addCleanup(tree_b.unlock)
316
tree_b.merge_from_branch(tree_a.branch)
317
except AttributeError:
318
self.fail('tried to join a path when name was None')
320
def test_merge_uncommitted_otherbasis_ancestor_of_thisbasis(self):
321
tree_a = self.make_branch_and_tree('a')
322
self.build_tree(['a/file_1', 'a/file_2'])
323
tree_a.add(['file_1'])
324
tree_a.commit('commit 1')
325
tree_a.add(['file_2'])
326
tree_a.commit('commit 2')
327
tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
328
tree_b.rename_one('file_1', 'renamed')
329
merger = _mod_merge.Merger.from_uncommitted(tree_a, tree_b)
330
merger.merge_type = _mod_merge.Merge3Merger
332
self.assertEqual(tree_a.get_parent_ids(), [tree_b.last_revision()])
334
def test_merge_uncommitted_otherbasis_ancestor_of_thisbasis_weave(self):
335
tree_a = self.make_branch_and_tree('a')
336
self.build_tree(['a/file_1', 'a/file_2'])
337
tree_a.add(['file_1'])
338
tree_a.commit('commit 1')
339
tree_a.add(['file_2'])
340
tree_a.commit('commit 2')
341
tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
342
tree_b.rename_one('file_1', 'renamed')
343
merger = _mod_merge.Merger.from_uncommitted(tree_a, tree_b)
344
merger.merge_type = _mod_merge.WeaveMerger
346
self.assertEqual(tree_a.get_parent_ids(), [tree_b.last_revision()])
348
def prepare_cherrypick(self):
349
"""Prepare a pair of trees for cherrypicking tests.
351
Both trees have a file, 'file'.
352
rev1 sets content to 'a'.
355
A full merge of rev2b and rev3b into this_tree would add both 'b' and
356
'c'. A successful cherrypick of rev2b-rev3b into this_tree will add
359
this_tree = self.make_branch_and_tree('this')
360
self.build_tree_contents([('this/file', "a\n")])
361
this_tree.add('file')
362
this_tree.commit('rev1')
363
other_tree = this_tree.bzrdir.sprout('other').open_workingtree()
364
self.build_tree_contents([('other/file', "a\nb\n")])
365
other_tree.commit('rev2b', rev_id='rev2b')
366
self.build_tree_contents([('other/file', "c\na\nb\n")])
367
other_tree.commit('rev3b', rev_id='rev3b')
368
this_tree.lock_write()
369
self.addCleanup(this_tree.unlock)
370
return this_tree, other_tree
372
def test_weave_cherrypick(self):
373
this_tree, other_tree = self.prepare_cherrypick()
374
merger = _mod_merge.Merger.from_revision_ids(None,
375
this_tree, 'rev3b', 'rev2b', other_tree.branch)
376
merger.merge_type = _mod_merge.WeaveMerger
378
self.assertFileEqual('c\na\n', 'this/file')
380
def test_weave_cannot_reverse_cherrypick(self):
381
this_tree, other_tree = self.prepare_cherrypick()
382
merger = _mod_merge.Merger.from_revision_ids(None,
383
this_tree, 'rev2b', 'rev3b', other_tree.branch)
384
merger.merge_type = _mod_merge.WeaveMerger
385
self.assertRaises(errors.CannotReverseCherrypick, merger.do_merge)
387
def test_merge3_can_reverse_cherrypick(self):
388
this_tree, other_tree = self.prepare_cherrypick()
389
merger = _mod_merge.Merger.from_revision_ids(None,
390
this_tree, 'rev2b', 'rev3b', other_tree.branch)
391
merger.merge_type = _mod_merge.Merge3Merger
394
def test_merge3_will_detect_cherrypick(self):
395
this_tree = self.make_branch_and_tree('this')
396
self.build_tree_contents([('this/file', "a\n")])
397
this_tree.add('file')
398
this_tree.commit('rev1')
399
other_tree = this_tree.bzrdir.sprout('other').open_workingtree()
400
self.build_tree_contents([('other/file', "a\nb\n")])
401
other_tree.commit('rev2b', rev_id='rev2b')
402
self.build_tree_contents([('other/file', "a\nb\nc\n")])
403
other_tree.commit('rev3b', rev_id='rev3b')
404
this_tree.lock_write()
405
self.addCleanup(this_tree.unlock)
407
merger = _mod_merge.Merger.from_revision_ids(None,
408
this_tree, 'rev3b', 'rev2b', other_tree.branch)
409
merger.merge_type = _mod_merge.Merge3Merger
411
self.assertFileEqual('a\n'
415
'>>>>>>> MERGE-SOURCE\n',
418
def test_merge_reverse_revision_range(self):
419
tree = self.make_branch_and_tree(".")
421
self.addCleanup(tree.unlock)
422
self.build_tree(['a'])
424
first_rev = tree.commit("added a")
425
merger = _mod_merge.Merger.from_revision_ids(None, tree,
426
_mod_revision.NULL_REVISION,
428
merger.merge_type = _mod_merge.Merge3Merger
429
merger.interesting_files = 'a'
430
conflict_count = merger.do_merge()
431
self.assertEqual(0, conflict_count)
433
self.assertPathDoesNotExist("a")
435
self.assertPathExists("a")
437
def test_make_merger(self):
438
this_tree = self.make_branch_and_tree('this')
439
this_tree.commit('rev1', rev_id='rev1')
440
other_tree = this_tree.bzrdir.sprout('other').open_workingtree()
441
this_tree.commit('rev2', rev_id='rev2a')
442
other_tree.commit('rev2', rev_id='rev2b')
443
this_tree.lock_write()
444
self.addCleanup(this_tree.unlock)
445
merger = _mod_merge.Merger.from_revision_ids(None,
446
this_tree, 'rev2b', other_branch=other_tree.branch)
447
merger.merge_type = _mod_merge.Merge3Merger
448
tree_merger = merger.make_merger()
449
self.assertIs(_mod_merge.Merge3Merger, tree_merger.__class__)
450
self.assertEqual('rev2b',
451
tree_merger.other_tree.get_revision_id())
452
self.assertEqual('rev1',
453
tree_merger.base_tree.get_revision_id())
454
self.assertEqual(other_tree.branch, tree_merger.other_branch)
456
def test_make_preview_transform(self):
457
this_tree = self.make_branch_and_tree('this')
458
self.build_tree_contents([('this/file', '1\n')])
459
this_tree.add('file', 'file-id')
460
this_tree.commit('rev1', rev_id='rev1')
461
other_tree = this_tree.bzrdir.sprout('other').open_workingtree()
462
self.build_tree_contents([('this/file', '1\n2a\n')])
463
this_tree.commit('rev2', rev_id='rev2a')
464
self.build_tree_contents([('other/file', '2b\n1\n')])
465
other_tree.commit('rev2', rev_id='rev2b')
466
this_tree.lock_write()
467
self.addCleanup(this_tree.unlock)
468
merger = _mod_merge.Merger.from_revision_ids(None,
469
this_tree, 'rev2b', other_branch=other_tree.branch)
470
merger.merge_type = _mod_merge.Merge3Merger
471
tree_merger = merger.make_merger()
472
tt = tree_merger.make_preview_transform()
473
self.addCleanup(tt.finalize)
474
preview_tree = tt.get_preview_tree()
475
tree_file = this_tree.get_file('file-id')
477
self.assertEqual('1\n2a\n', tree_file.read())
480
preview_file = preview_tree.get_file('file-id')
482
self.assertEqual('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', '1\n')])
489
this_tree.add('file', 'file-id')
490
this_tree.commit('rev1', rev_id='rev1')
491
other_tree = this_tree.bzrdir.sprout('other').open_workingtree()
492
self.build_tree_contents([('this/file', '1\n2a\n')])
493
this_tree.commit('rev2', rev_id='rev2a')
494
self.build_tree_contents([('other/file', '2b\n1\n')])
495
other_tree.commit('rev2', rev_id='rev2b')
496
this_tree.lock_write()
497
self.addCleanup(this_tree.unlock)
498
merger = _mod_merge.Merger.from_revision_ids(None,
499
this_tree, '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
tree_file = this_tree.get_file('file-id')
505
self.assertEqual('2b\n1\n2a\n', tree_file.read())
509
def test_merge_require_tree_root(self):
510
tree = self.make_branch_and_tree(".")
512
self.addCleanup(tree.unlock)
513
self.build_tree(['a'])
515
first_rev = tree.commit("added a")
516
old_root_id = tree.get_root_id()
517
merger = _mod_merge.Merger.from_revision_ids(None, tree,
518
_mod_revision.NULL_REVISION,
520
merger.merge_type = _mod_merge.Merge3Merger
521
conflict_count = merger.do_merge()
522
self.assertEqual(0, conflict_count)
523
self.assertEquals(set([old_root_id]), tree.all_file_ids())
524
tree.set_parent_ids([])
526
def test_merge_add_into_deleted_root(self):
527
# Yes, people actually do this. And report bugs if it breaks.
528
source = self.make_branch_and_tree('source', format='rich-root-pack')
529
self.build_tree(['source/foo/'])
530
source.add('foo', 'foo-id')
531
source.commit('Add foo')
532
target = source.bzrdir.sprout('target').open_workingtree()
533
subtree = target.extract('foo-id')
534
subtree.commit('Delete root')
535
self.build_tree(['source/bar'])
536
source.add('bar', 'bar-id')
537
source.commit('Add bar')
538
subtree.merge_from_branch(source.branch)
540
def test_merge_joined_branch(self):
541
source = self.make_branch_and_tree('source', format='rich-root-pack')
542
self.build_tree(['source/foo'])
544
source.commit('Add foo')
545
target = self.make_branch_and_tree('target', format='rich-root-pack')
546
self.build_tree(['target/bla'])
548
target.commit('Add bla')
549
nested = source.bzrdir.sprout('target/subtree').open_workingtree()
550
target.subsume(nested)
551
target.commit('Join nested')
552
self.build_tree(['source/bar'])
554
source.commit('Add bar')
555
target.merge_from_branch(source.branch)
556
target.commit('Merge source')
559
class TestPlanMerge(TestCaseWithMemoryTransport):
562
TestCaseWithMemoryTransport.setUp(self)
563
mapper = versionedfile.PrefixMapper()
564
factory = knit.make_file_factory(True, mapper)
565
self.vf = factory(self.get_transport())
566
self.plan_merge_vf = versionedfile._PlanMergeVersionedFile('root')
567
self.plan_merge_vf.fallback_versionedfiles.append(self.vf)
569
def add_version(self, key, parents, text):
570
self.vf.add_lines(key, parents, [c+'\n' for c in text])
572
def add_rev(self, prefix, revision_id, parents, text):
573
self.add_version((prefix, revision_id), [(prefix, p) for p in parents],
576
def add_uncommitted_version(self, key, parents, text):
577
self.plan_merge_vf.add_lines(key, parents,
578
[c+'\n' for c in text])
580
def setup_plan_merge(self):
581
self.add_rev('root', 'A', [], 'abc')
582
self.add_rev('root', 'B', ['A'], 'acehg')
583
self.add_rev('root', 'C', ['A'], 'fabg')
584
return _PlanMerge('B', 'C', self.plan_merge_vf, ('root',))
586
def setup_plan_merge_uncommitted(self):
587
self.add_version(('root', 'A'), [], 'abc')
588
self.add_uncommitted_version(('root', 'B:'), [('root', 'A')], 'acehg')
589
self.add_uncommitted_version(('root', 'C:'), [('root', 'A')], 'fabg')
590
return _PlanMerge('B:', 'C:', self.plan_merge_vf, ('root',))
592
def test_base_from_plan(self):
593
self.setup_plan_merge()
594
plan = self.plan_merge_vf.plan_merge('B', 'C')
595
pwm = versionedfile.PlanWeaveMerge(plan)
596
self.assertEqual(['a\n', 'b\n', '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', 'C')),
604
def test_plan_merge(self):
605
self.setup_plan_merge()
606
plan = self.plan_merge_vf.plan_merge('B', 'C')
609
('unchanged', 'a\n'),
618
def test_plan_merge_cherrypick(self):
619
self.add_rev('root', 'A', [], 'abc')
620
self.add_rev('root', 'B', ['A'], 'abcde')
621
self.add_rev('root', 'C', ['A'], 'abcefg')
622
self.add_rev('root', 'D', ['A', 'B', 'C'], 'abcdegh')
623
my_plan = _PlanMerge('B', 'D', self.plan_merge_vf, ('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('root', 'A', [], 'abc')
638
self.add_rev('root', 'B', [], 'xyz')
639
my_plan = _PlanMerge('A', 'B', self.plan_merge_vf, ('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('root', 'A', [], 'abc')
670
# Introduces a new line B
671
self.add_rev('root', 'B', ['A'], 'aBbc')
672
# Introduces a new line C
673
self.add_rev('root', 'C', ['A'], 'abCc')
674
# Introduce new line D
675
self.add_rev('root', 'D', ['B'], 'DaBbc')
676
# Merges B and C by just incorporating both
677
self.add_rev('root', 'E', ['B', 'C'], 'aBbCc')
678
# Introduce new line F
679
self.add_rev('root', 'F', ['C'], 'abCcF')
680
# Merge D & E by just combining the texts
681
self.add_rev('root', 'G', ['D', 'E'], 'DaBbCc')
682
# Merge F & E by just combining the texts
683
self.add_rev('root', 'H', ['F', 'E'], 'aBbCcF')
684
# Merge G & H by just combining texts
685
self.add_rev('root', 'I', ['G', 'H'], 'DaBbCcF')
686
# Merge G & H but supersede an old line in B
687
self.add_rev('root', 'J', ['H', 'G'], 'DaJbCcF')
688
plan = self.plan_merge_vf.plan_merge('I', 'J')
690
('unchanged', 'D\n'),
691
('unchanged', 'a\n'),
694
('unchanged', 'b\n'),
695
('unchanged', 'C\n'),
696
('unchanged', 'c\n'),
697
('unchanged', '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('root', 'A', [], 'abc')
719
self.add_rev('root', 'B', ['A'], 'aBbc')
720
self.add_rev('root', 'C', ['A'], 'abCc')
721
self.add_rev('root', 'D', ['B'], 'DaBbc')
722
self.add_rev('root', 'E', ['B', 'C'], 'aBbCc')
723
self.add_rev('root', 'F', ['C'], 'abCcF')
724
self.add_rev('root', 'G', ['D', 'E'], 'DaBbCc')
725
self.add_rev('root', 'H', ['F', 'E'], 'aBbCcF')
726
self.add_rev('root', 'Q', ['E'], 'aBbCc')
727
self.add_rev('root', 'I', ['G', 'Q', 'H'], 'DaBbCcF')
728
# Merge G & H but supersede an old line in B
729
self.add_rev('root', 'J', ['H', 'Q', 'G'], 'DaJbCcF')
730
plan = self.plan_merge_vf.plan_merge('I', 'J')
732
('unchanged', 'D\n'),
733
('unchanged', 'a\n'),
736
('unchanged', 'b\n'),
737
('unchanged', 'C\n'),
738
('unchanged', 'c\n'),
739
('unchanged', '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('root', 'A', [], 'abc')
759
self.add_rev('root', 'B', [], 'def')
760
self.add_rev('root', 'D', ['A'], 'Dabc')
761
self.add_rev('root', 'E', ['A', 'B'], 'abcdef')
762
self.add_rev('root', 'F', ['B'], 'defF')
763
self.add_rev('root', 'G', ['D', 'E'], 'Dabcdef')
764
self.add_rev('root', 'H', ['F', 'E'], 'abcdefF')
765
self.add_rev('root', 'Q', ['E'], 'abcdef')
766
self.add_rev('root', 'I', ['G', 'Q', 'H'], 'DabcdefF')
767
# Merge G & H but supersede an old line in B
768
self.add_rev('root', 'J', ['H', 'Q', 'G'], 'DabcdJfF')
769
plan = self.plan_merge_vf.plan_merge('I', 'J')
771
('unchanged', 'D\n'),
772
('unchanged', 'a\n'),
773
('unchanged', 'b\n'),
774
('unchanged', 'c\n'),
775
('unchanged', 'd\n'),
778
('unchanged', 'f\n'),
779
('unchanged', '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:', 'C:')
787
('unchanged', 'a\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('root', 'A', [], 'abcdef')
805
self.add_rev('root', 'B', ['A'], 'abwxcdef')
806
self.add_rev('root', 'C', ['A'], '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('root', 'D', ['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('root', 'E', ['C', 'B'],
816
# Same thing for the lines in C
817
self.add_rev('root', 'F', ['C'], 'abpqcdef')
818
plan = self.plan_merge_vf.plan_merge('D', 'E')
820
('unchanged', 'a\n'),
821
('unchanged', 'b\n'),
828
('unchanged', 'c\n'),
829
('unchanged', 'd\n'),
830
('unchanged', 'e\n'),
831
('unchanged', 'f\n')],
833
plan = self.plan_merge_vf.plan_merge('E', 'D')
834
# Going in the opposite direction shows the effect of the opposite plan
836
('unchanged', 'a\n'),
837
('unchanged', 'b\n'),
842
('killed-both', 'w\n'),
843
('killed-both', 'x\n'),
846
('unchanged', 'c\n'),
847
('unchanged', 'd\n'),
848
('unchanged', 'e\n'),
849
('unchanged', '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('root', 'XX', [], 'qrs')
878
self.add_rev('root', 'A', ['XX'], 'abcdef')
879
self.add_rev('root', 'B', ['A'], 'axcdef')
880
self.add_rev('root', 'C', ['B'], 'axcdefg')
881
self.add_rev('root', 'D', ['B'], 'haxcdef')
882
self.add_rev('root', 'E', ['A'], 'abcdyf')
883
# Simple combining of all texts
884
self.add_rev('root', 'F', ['C', 'D', 'E'], 'haxcdyfg')
885
# combine and supersede 'x'
886
self.add_rev('root', 'G', ['C', 'D', 'E'], 'hazcdyfg')
887
plan = self.plan_merge_vf.plan_merge('F', 'G')
889
('unchanged', 'h\n'),
890
('unchanged', 'a\n'),
891
('killed-base', 'b\n'),
894
('unchanged', 'c\n'),
895
('unchanged', 'd\n'),
896
('killed-base', 'e\n'),
897
('unchanged', 'y\n'),
898
('unchanged', 'f\n'),
899
('unchanged', 'g\n')],
901
plan = self.plan_merge_vf.plan_lca_merge('F', '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', 'h\n'),
908
('unchanged', 'a\n'),
909
('conflicted-a', 'x\n'),
911
('unchanged', 'c\n'),
912
('unchanged', 'd\n'),
913
('unchanged', 'y\n'),
914
('unchanged', 'f\n'),
915
('unchanged', '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('root', 'XX', [], 'qrs')
934
self.add_rev('root', 'A', ['XX'], 'abcdef')
935
self.add_rev('root', 'B', ['A'], 'abcdgef')
936
self.add_rev('root', 'C', ['A'], 'abcdhef')
937
self.add_rev('root', 'D', ['B', 'C'], 'abcdghef')
938
self.add_rev('root', 'E', ['C', 'B'], 'abcdhgef')
939
plan = list(self.plan_merge_vf.plan_merge('D', 'E'))
941
('unchanged', 'a\n'),
942
('unchanged', 'b\n'),
943
('unchanged', 'c\n'),
944
('unchanged', 'd\n'),
946
('unchanged', 'g\n'),
948
('unchanged', 'e\n'),
949
('unchanged', 'f\n'),
951
pwm = versionedfile.PlanWeaveMerge(plan)
952
self.assertEqualDiff('\n'.join('abcdghef') + '\n',
953
''.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('E', 'D'))
958
('unchanged', 'a\n'),
959
('unchanged', 'b\n'),
960
('unchanged', 'c\n'),
961
('unchanged', 'd\n'),
963
('unchanged', 'h\n'),
965
('unchanged', 'e\n'),
966
('unchanged', 'f\n'),
968
pwm = versionedfile.PlanWeaveMerge(plan)
969
self.assertEqualDiff('\n'.join('abcdhgef') + '\n',
970
''.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('D', 'E'))
976
('unchanged', 'a\n'),
977
('unchanged', 'b\n'),
978
('unchanged', 'c\n'),
979
('unchanged', 'd\n'),
980
('conflicted-b', 'h\n'),
981
('unchanged', 'g\n'),
982
('conflicted-a', 'h\n'),
983
('unchanged', 'e\n'),
984
('unchanged', 'f\n'),
986
pwm = versionedfile.PlanWeaveMerge(plan)
987
self.assertEqualDiff('\n'.join('abcdgef') + '\n',
988
''.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('E', 'D'))
993
('unchanged', 'a\n'),
994
('unchanged', 'b\n'),
995
('unchanged', 'c\n'),
996
('unchanged', 'd\n'),
997
('conflicted-b', 'g\n'),
998
('unchanged', 'h\n'),
999
('conflicted-a', 'g\n'),
1000
('unchanged', 'e\n'),
1001
('unchanged', 'f\n'),
1003
pwm = versionedfile.PlanWeaveMerge(plan)
1004
self.assertEqualDiff('\n'.join('abcdhef') + '\n',
1005
''.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.iteritems():
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', 'a\n'),
1082
('killed-a', 'c\n'),
1085
('killed-b', 'f\n'),
1086
('killed-b', 'g\n'),
1089
('unchanged', 'a\n'),
1091
('killed-a', 'c\n'),
1094
('killed-b', 'f\n'),
1095
('killed-b', 'i\n'),
1098
('unchanged', 'a\n'),
1100
('killed-a', 'c\n'),
1102
('unchanged', 'f\n'),
1103
('killed-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('root', 'COMMON', [], 'abc')
1110
self.add_rev('root', 'THIS', ['COMMON'], 'abcd')
1111
self.add_rev('root', 'BASE', ['COMMON'], 'eabc')
1112
self.add_rev('root', 'OTHER', ['BASE'], 'eafb')
1114
def test_plan_merge_with_base(self):
1115
self.setup_merge_with_base()
1116
plan = self.plan_merge_vf.plan_merge('THIS', 'OTHER', 'BASE')
1117
self.assertEqual([('unchanged', 'a\n'),
1119
('unchanged', 'b\n'),
1120
('killed-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', 'C')
1129
('unchanged', 'a\n'),
1130
('killed-b', 'c\n'),
1133
('killed-a', 'b\n'),
1134
('unchanged', '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:', 'C:')
1142
('unchanged', 'a\n'),
1143
('killed-b', 'c\n'),
1146
('killed-a', 'b\n'),
1147
('unchanged', '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('THIS', 'OTHER', 'BASE')
1153
self.assertEqual([('unchanged', 'a\n'),
1155
('unchanged', 'b\n'),
1156
('killed-b', 'c\n'),
1160
def test_plan_lca_merge_with_criss_cross(self):
1161
self.add_version(('root', 'ROOT'), [], 'abc')
1162
# each side makes a change
1163
self.add_version(('root', 'REV1'), [('root', 'ROOT')], 'abcd')
1164
self.add_version(('root', 'REV2'), [('root', 'ROOT')], 'abce')
1165
# both sides merge, discarding others' changes
1166
self.add_version(('root', 'LCA1'),
1167
[('root', 'REV1'), ('root', 'REV2')], 'abcd')
1168
self.add_version(('root', 'LCA2'),
1169
[('root', 'REV1'), ('root', 'REV2')], 'fabce')
1170
plan = self.plan_merge_vf.plan_lca_merge('LCA1', 'LCA2')
1171
self.assertEqual([('new-b', 'f\n'),
1172
('unchanged', 'a\n'),
1173
('unchanged', 'b\n'),
1174
('unchanged', 'c\n'),
1175
('conflicted-a', 'd\n'),
1176
('conflicted-b', 'e\n'),
1179
def test_plan_lca_merge_with_null(self):
1180
self.add_version(('root', 'A'), [], 'ab')
1181
self.add_version(('root', 'B'), [], 'bc')
1182
plan = self.plan_merge_vf.plan_lca_merge('A', 'B')
1183
self.assertEqual([('new-a', 'a\n'),
1184
('unchanged', 'b\n'),
1188
def test_plan_merge_with_delete_and_change(self):
1189
self.add_rev('root', 'C', [], 'a')
1190
self.add_rev('root', 'A', ['C'], 'b')
1191
self.add_rev('root', 'B', ['C'], '')
1192
plan = self.plan_merge_vf.plan_merge('A', 'B')
1193
self.assertEqual([('killed-both', 'a\n'),
1197
def test_plan_merge_with_move_and_change(self):
1198
self.add_rev('root', 'C', [], 'abcd')
1199
self.add_rev('root', 'A', ['C'], 'acbd')
1200
self.add_rev('root', 'B', ['C'], 'aBcd')
1201
plan = self.plan_merge_vf.plan_merge('A', 'B')
1202
self.assertEqual([('unchanged', 'a\n'),
1204
('killed-b', 'b\n'),
1206
('killed-a', 'c\n'),
1207
('unchanged', '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('A-id', None,
1245
[('add', ('', None, 'directory', None))])
1246
builder.build_snapshot('C-id', ['A-id'], [])
1247
builder.build_snapshot('B-id', ['A-id'], [])
1250
def setup_criss_cross_graph(self):
1251
"""Create a 5-node graph with a criss-cross.
1253
:return: A BranchBuilder
1260
builder = self.setup_simple_graph()
1261
builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
1262
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1265
def make_Merger(self, builder, other_revision_id,
1266
interesting_files=None, interesting_ids=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(None,
1272
mem_tree, other_revision_id)
1273
merger.set_interesting_files(interesting_files)
1274
# It seems there is no matching function for set_interesting_ids
1275
merger.interesting_ids = interesting_ids
1276
merger.merge_type = _mod_merge.Merge3Merger
1280
class TestMergerInMemory(TestMergerBase):
1282
def test_cache_trees_with_revision_ids_None(self):
1283
merger = self.make_Merger(self.setup_simple_graph(), 'C-id')
1284
original_cache = dict(merger._cached_trees)
1285
merger.cache_trees_with_revision_ids([None])
1286
self.assertEqual(original_cache, merger._cached_trees)
1288
def test_cache_trees_with_revision_ids_no_revision_id(self):
1289
merger = self.make_Merger(self.setup_simple_graph(), 'C-id')
1290
original_cache = dict(merger._cached_trees)
1291
tree = self.make_branch_and_memory_tree('tree')
1292
merger.cache_trees_with_revision_ids([tree])
1293
self.assertEqual(original_cache, merger._cached_trees)
1295
def test_cache_trees_with_revision_ids_having_revision_id(self):
1296
merger = self.make_Merger(self.setup_simple_graph(), 'C-id')
1297
original_cache = dict(merger._cached_trees)
1298
tree = merger.this_branch.repository.revision_tree('B-id')
1299
original_cache['B-id'] = tree
1300
merger.cache_trees_with_revision_ids([tree])
1301
self.assertEqual(original_cache, merger._cached_trees)
1303
def test_find_base(self):
1304
merger = self.make_Merger(self.setup_simple_graph(), 'C-id')
1305
self.assertEqual('A-id', merger.base_rev_id)
1306
self.assertFalse(merger._is_criss_cross)
1307
self.assertIs(None, merger._lca_trees)
1309
def test_find_base_criss_cross(self):
1310
builder = self.setup_criss_cross_graph()
1311
merger = self.make_Merger(builder, 'E-id')
1312
self.assertEqual('A-id', merger.base_rev_id)
1313
self.assertTrue(merger._is_criss_cross)
1314
self.assertEqual(['B-id', 'C-id'], [t.get_revision_id()
1315
for t in merger._lca_trees])
1316
# If we swap the order, we should get a different lca order
1317
builder.build_snapshot('F-id', ['E-id'], [])
1318
merger = self.make_Merger(builder, 'D-id')
1319
self.assertEqual(['C-id', 'B-id'], [t.get_revision_id()
1320
for t in merger._lca_trees])
1322
def test_find_base_triple_criss_cross(self):
1325
# B C F # F is merged into both branches
1332
builder = self.setup_criss_cross_graph()
1333
builder.build_snapshot('F-id', ['A-id'], [])
1334
builder.build_snapshot('H-id', ['E-id', 'F-id'], [])
1335
builder.build_snapshot('G-id', ['D-id', 'F-id'], [])
1336
merger = self.make_Merger(builder, 'H-id')
1337
self.assertEqual(['B-id', 'C-id', 'F-id'],
1338
[t.get_revision_id() for t in merger._lca_trees])
1340
def test_find_base_new_root_criss_cross(self):
1347
builder = self.get_builder()
1348
builder.build_snapshot('A-id', None,
1349
[('add', ('', None, 'directory', None))])
1350
builder.build_snapshot('B-id', [],
1351
[('add', ('', None, 'directory', None))])
1352
builder.build_snapshot('D-id', ['A-id', 'B-id'], [])
1353
builder.build_snapshot('C-id', ['A-id', 'B-id'], [])
1354
merger = self.make_Merger(builder, 'D-id')
1355
self.assertEqual('A-id', merger.base_rev_id)
1356
self.assertTrue(merger._is_criss_cross)
1357
self.assertEqual(['A-id', 'B-id'], [t.get_revision_id()
1358
for t in merger._lca_trees])
1360
def test_no_criss_cross_passed_to_merge_type(self):
1361
class LCATreesMerger(LoggingMerger):
1362
supports_lca_trees = True
1364
merger = self.make_Merger(self.setup_simple_graph(), 'C-id')
1365
merger.merge_type = LCATreesMerger
1366
merge_obj = merger.make_merger()
1367
self.assertIsInstance(merge_obj, LCATreesMerger)
1368
self.assertFalse('lca_trees' in merge_obj.kwargs)
1370
def test_criss_cross_passed_to_merge_type(self):
1371
merger = self.make_Merger(self.setup_criss_cross_graph(), 'E-id')
1372
merger.merge_type = _mod_merge.Merge3Merger
1373
merge_obj = merger.make_merger()
1374
self.assertEqual(['B-id', 'C-id'], [t.get_revision_id()
1375
for t in merger._lca_trees])
1377
def test_criss_cross_not_supported_merge_type(self):
1378
merger = self.make_Merger(self.setup_criss_cross_graph(), 'E-id')
1379
# We explicitly do not define supports_lca_trees
1380
merger.merge_type = LoggingMerger
1381
merge_obj = merger.make_merger()
1382
self.assertIsInstance(merge_obj, LoggingMerger)
1383
self.assertFalse('lca_trees' in merge_obj.kwargs)
1385
def test_criss_cross_unsupported_merge_type(self):
1386
class UnsupportedLCATreesMerger(LoggingMerger):
1387
supports_lca_trees = False
1389
merger = self.make_Merger(self.setup_criss_cross_graph(), 'E-id')
1390
merger.merge_type = UnsupportedLCATreesMerger
1391
merge_obj = merger.make_merger()
1392
self.assertIsInstance(merge_obj, UnsupportedLCATreesMerger)
1393
self.assertFalse('lca_trees' in merge_obj.kwargs)
1396
class TestMergerEntriesLCA(TestMergerBase):
1398
def make_merge_obj(self, builder, other_revision_id,
1399
interesting_files=None, interesting_ids=None):
1400
merger = self.make_Merger(builder, other_revision_id,
1401
interesting_files=interesting_files,
1402
interesting_ids=interesting_ids)
1403
return merger.make_merger()
1405
def test_simple(self):
1406
builder = self.get_builder()
1407
builder.build_snapshot('A-id', None,
1408
[('add', (u'', 'a-root-id', 'directory', None)),
1409
('add', (u'a', 'a-id', 'file', 'a\nb\nc\n'))])
1410
builder.build_snapshot('C-id', ['A-id'],
1411
[('modify', ('a-id', 'a\nb\nC\nc\n'))])
1412
builder.build_snapshot('B-id', ['A-id'],
1413
[('modify', ('a-id', 'a\nB\nb\nc\n'))])
1414
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1415
[('modify', ('a-id', 'a\nB\nb\nC\nc\nE\n'))])
1416
builder.build_snapshot('D-id', ['B-id', 'C-id'],
1417
[('modify', ('a-id', 'a\nB\nb\nC\nc\n'))])
1418
merge_obj = self.make_merge_obj(builder, 'E-id')
1420
self.assertEqual(['B-id', 'C-id'], [t.get_revision_id()
1421
for t in merge_obj._lca_trees])
1422
self.assertEqual('A-id', merge_obj.base_tree.get_revision_id())
1423
entries = list(merge_obj._entries_lca())
1425
# (file_id, changed, parents, names, executable)
1426
# BASE, lca1, lca2, OTHER, THIS
1427
root_id = 'a-root-id'
1428
self.assertEqual([('a-id', True,
1429
((root_id, [root_id, root_id]), root_id, root_id),
1430
((u'a', [u'a', u'a']), u'a', u'a'),
1431
((False, [False, False]), False, False)),
1434
def test_not_in_base(self):
1435
# LCAs all have the same last-modified revision for the file, as do
1436
# the tips, but the base has something different
1437
# A base, doesn't have the file
1439
# B C B introduces 'foo', C introduces 'bar'
1441
# D E D and E now both have 'foo' and 'bar'
1443
# F G the files are now in F, G, D and E, but not in A
1446
builder = self.get_builder()
1447
builder.build_snapshot('A-id', None,
1448
[('add', (u'', 'a-root-id', 'directory', None))])
1449
builder.build_snapshot('B-id', ['A-id'],
1450
[('add', (u'foo', 'foo-id', 'file', 'a\nb\nc\n'))])
1451
builder.build_snapshot('C-id', ['A-id'],
1452
[('add', (u'bar', 'bar-id', 'file', 'd\ne\nf\n'))])
1453
builder.build_snapshot('D-id', ['B-id', 'C-id'],
1454
[('add', (u'bar', 'bar-id', 'file', 'd\ne\nf\n'))])
1455
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1456
[('add', (u'foo', 'foo-id', 'file', 'a\nb\nc\n'))])
1457
builder.build_snapshot('G-id', ['E-id', 'D-id'],
1458
[('modify', (u'bar-id', 'd\ne\nf\nG\n'))])
1459
builder.build_snapshot('F-id', ['D-id', 'E-id'], [])
1460
merge_obj = self.make_merge_obj(builder, 'G-id')
1462
self.assertEqual(['D-id', 'E-id'], [t.get_revision_id()
1463
for t in merge_obj._lca_trees])
1464
self.assertEqual('A-id', merge_obj.base_tree.get_revision_id())
1465
entries = list(merge_obj._entries_lca())
1466
root_id = 'a-root-id'
1467
self.assertEqual([('bar-id', True,
1468
((None, [root_id, root_id]), root_id, root_id),
1469
((None, [u'bar', u'bar']), u'bar', u'bar'),
1470
((None, [False, False]), False, False)),
1473
def test_not_in_this(self):
1474
builder = self.get_builder()
1475
builder.build_snapshot('A-id', None,
1476
[('add', (u'', 'a-root-id', 'directory', None)),
1477
('add', (u'a', 'a-id', 'file', 'a\nb\nc\n'))])
1478
builder.build_snapshot('B-id', ['A-id'],
1479
[('modify', ('a-id', 'a\nB\nb\nc\n'))])
1480
builder.build_snapshot('C-id', ['A-id'],
1481
[('modify', ('a-id', 'a\nb\nC\nc\n'))])
1482
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1483
[('modify', ('a-id', 'a\nB\nb\nC\nc\nE\n'))])
1484
builder.build_snapshot('D-id', ['B-id', 'C-id'],
1485
[('unversion', 'a-id')])
1486
merge_obj = self.make_merge_obj(builder, 'E-id')
1488
self.assertEqual(['B-id', 'C-id'], [t.get_revision_id()
1489
for t in merge_obj._lca_trees])
1490
self.assertEqual('A-id', merge_obj.base_tree.get_revision_id())
1492
entries = list(merge_obj._entries_lca())
1493
root_id = 'a-root-id'
1494
self.assertEqual([('a-id', True,
1495
((root_id, [root_id, root_id]), root_id, None),
1496
((u'a', [u'a', u'a']), u'a', None),
1497
((False, [False, False]), False, None)),
1500
def test_file_not_in_one_lca(self):
1503
# B C # B no file, C introduces a file
1505
# D E # D and E both have the file, unchanged from C
1506
builder = self.get_builder()
1507
builder.build_snapshot('A-id', None,
1508
[('add', (u'', 'a-root-id', 'directory', None))])
1509
builder.build_snapshot('B-id', ['A-id'], [])
1510
builder.build_snapshot('C-id', ['A-id'],
1511
[('add', (u'a', 'a-id', 'file', 'a\nb\nc\n'))])
1512
builder.build_snapshot('E-id', ['C-id', 'B-id'], []) # Inherited from C
1513
builder.build_snapshot('D-id', ['B-id', 'C-id'], # Merged from C
1514
[('add', (u'a', 'a-id', 'file', 'a\nb\nc\n'))])
1515
merge_obj = self.make_merge_obj(builder, 'E-id')
1517
self.assertEqual(['B-id', 'C-id'], [t.get_revision_id()
1518
for t in merge_obj._lca_trees])
1519
self.assertEqual('A-id', merge_obj.base_tree.get_revision_id())
1521
entries = list(merge_obj._entries_lca())
1522
self.assertEqual([], entries)
1524
def test_not_in_other(self):
1525
builder = self.get_builder()
1526
builder.build_snapshot('A-id', None,
1527
[('add', (u'', 'a-root-id', 'directory', None)),
1528
('add', (u'a', 'a-id', 'file', 'a\nb\nc\n'))])
1529
builder.build_snapshot('B-id', ['A-id'], [])
1530
builder.build_snapshot('C-id', ['A-id'], [])
1531
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1532
[('unversion', 'a-id')])
1533
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1534
merge_obj = self.make_merge_obj(builder, 'E-id')
1536
entries = list(merge_obj._entries_lca())
1537
root_id = 'a-root-id'
1538
self.assertEqual([('a-id', True,
1539
((root_id, [root_id, root_id]), None, root_id),
1540
((u'a', [u'a', u'a']), None, u'a'),
1541
((False, [False, False]), None, False)),
1544
def test_not_in_other_or_lca(self):
1545
# A base, introduces 'foo'
1547
# B C B nothing, C deletes foo
1549
# D E D restores foo (same as B), E leaves it deleted
1551
# A => B, no changes
1552
# A => C, delete foo (C should supersede B)
1553
# C => D, restore foo
1554
# C => E, no changes
1555
# D would then win 'cleanly' and no record would be given
1556
builder = self.get_builder()
1557
builder.build_snapshot('A-id', None,
1558
[('add', (u'', 'a-root-id', 'directory', None)),
1559
('add', (u'foo', 'foo-id', 'file', 'content\n'))])
1560
builder.build_snapshot('B-id', ['A-id'], [])
1561
builder.build_snapshot('C-id', ['A-id'],
1562
[('unversion', 'foo-id')])
1563
builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
1564
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1565
merge_obj = self.make_merge_obj(builder, 'E-id')
1567
entries = list(merge_obj._entries_lca())
1568
self.assertEqual([], entries)
1570
def test_not_in_other_mod_in_lca1_not_in_lca2(self):
1571
# A base, introduces 'foo'
1573
# B C B changes 'foo', C deletes foo
1575
# D E D restores foo (same as B), E leaves it deleted (as C)
1577
# A => B, modified foo
1578
# A => C, delete foo, C does not supersede B
1579
# B => D, no changes
1580
# C => D, resolve in favor of B
1581
# B => E, resolve in favor of E
1582
# C => E, no changes
1583
# In this case, we have a conflict of how the changes were resolved. E
1584
# picked C and D picked B, so we should issue a conflict
1585
builder = self.get_builder()
1586
builder.build_snapshot('A-id', None,
1587
[('add', (u'', 'a-root-id', 'directory', None)),
1588
('add', (u'foo', 'foo-id', 'file', 'content\n'))])
1589
builder.build_snapshot('B-id', ['A-id'], [
1590
('modify', ('foo-id', 'new-content\n'))])
1591
builder.build_snapshot('C-id', ['A-id'],
1592
[('unversion', 'foo-id')])
1593
builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
1594
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1595
merge_obj = self.make_merge_obj(builder, 'E-id')
1597
entries = list(merge_obj._entries_lca())
1598
root_id = 'a-root-id'
1599
self.assertEqual([('foo-id', True,
1600
((root_id, [root_id, None]), None, root_id),
1601
((u'foo', [u'foo', None]), None, 'foo'),
1602
((False, [False, None]), None, False)),
1605
def test_only_in_one_lca(self):
1608
# B C B nothing, C add file
1610
# D E D still has nothing, E removes file
1613
# C => D, removed the file
1615
# C => E, removed the file
1616
# Thus D & E have identical changes, and this is a no-op
1619
# A => C, add file, thus C supersedes B
1620
# w/ C=BASE, D=THIS, E=OTHER we have 'happy convergence'
1621
builder = self.get_builder()
1622
builder.build_snapshot('A-id', None,
1623
[('add', (u'', 'a-root-id', 'directory', None))])
1624
builder.build_snapshot('B-id', ['A-id'], [])
1625
builder.build_snapshot('C-id', ['A-id'],
1626
[('add', (u'a', 'a-id', 'file', 'a\nb\nc\n'))])
1627
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1628
[('unversion', 'a-id')])
1629
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1630
merge_obj = self.make_merge_obj(builder, 'E-id')
1632
entries = list(merge_obj._entries_lca())
1633
self.assertEqual([], entries)
1635
def test_only_in_other(self):
1636
builder = self.get_builder()
1637
builder.build_snapshot('A-id', None,
1638
[('add', (u'', 'a-root-id', 'directory', None))])
1639
builder.build_snapshot('B-id', ['A-id'], [])
1640
builder.build_snapshot('C-id', ['A-id'], [])
1641
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1642
[('add', (u'a', 'a-id', 'file', 'a\nb\nc\n'))])
1643
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1644
merge_obj = self.make_merge_obj(builder, 'E-id')
1646
entries = list(merge_obj._entries_lca())
1647
root_id = 'a-root-id'
1648
self.assertEqual([('a-id', True,
1649
((None, [None, None]), root_id, None),
1650
((None, [None, None]), u'a', None),
1651
((None, [None, None]), False, None)),
1654
def test_one_lca_supersedes(self):
1655
# One LCA supersedes the other LCAs last modified value, but the
1656
# value is not the same as BASE.
1657
# A base, introduces 'foo', last mod A
1659
# B C B modifies 'foo' (mod B), C does nothing (mod A)
1661
# D E D does nothing (mod B), E updates 'foo' (mod E)
1663
# F G F updates 'foo' (mod F). G does nothing (mod E)
1665
# At this point, G should not be considered to modify 'foo', even
1666
# though its LCAs disagree. This is because the modification in E
1667
# completely supersedes the value in D.
1668
builder = self.get_builder()
1669
builder.build_snapshot('A-id', None,
1670
[('add', (u'', 'a-root-id', 'directory', None)),
1671
('add', (u'foo', 'foo-id', 'file', 'A content\n'))])
1672
builder.build_snapshot('C-id', ['A-id'], [])
1673
builder.build_snapshot('B-id', ['A-id'],
1674
[('modify', ('foo-id', 'B content\n'))])
1675
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1676
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1677
[('modify', ('foo-id', 'E content\n'))])
1678
builder.build_snapshot('G-id', ['E-id', 'D-id'], [])
1679
builder.build_snapshot('F-id', ['D-id', 'E-id'],
1680
[('modify', ('foo-id', 'F content\n'))])
1681
merge_obj = self.make_merge_obj(builder, 'G-id')
1683
self.assertEqual([], list(merge_obj._entries_lca()))
1685
def test_one_lca_supersedes_path(self):
1686
# Double-criss-cross merge, the ultimate base value is different from
1690
# B C B value 'bar', C = 'foo'
1692
# D E D = 'bar', E supersedes to 'bing'
1694
# F G F = 'bing', G supersedes to 'barry'
1696
# In this case, we technically should not care about the value 'bar' for
1697
# D, because it was clearly superseded by E's 'bing'. The
1698
# per-file/attribute graph would actually look like:
1707
# Because the other side of the merge never modifies the value, it just
1708
# takes the value from the merge.
1710
# ATM this fails because we will prune 'foo' from the LCAs, but we
1711
# won't prune 'bar'. This is getting far off into edge-case land, so we
1712
# aren't supporting it yet.
1714
builder = self.get_builder()
1715
builder.build_snapshot('A-id', None,
1716
[('add', (u'', 'a-root-id', 'directory', None)),
1717
('add', (u'foo', 'foo-id', 'file', 'A content\n'))])
1718
builder.build_snapshot('C-id', ['A-id'], [])
1719
builder.build_snapshot('B-id', ['A-id'],
1720
[('rename', ('foo', 'bar'))])
1721
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1722
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1723
[('rename', ('foo', 'bing'))]) # override to bing
1724
builder.build_snapshot('G-id', ['E-id', 'D-id'],
1725
[('rename', ('bing', 'barry'))]) # override to barry
1726
builder.build_snapshot('F-id', ['D-id', 'E-id'],
1727
[('rename', ('bar', 'bing'))]) # Merge in E's change
1728
merge_obj = self.make_merge_obj(builder, 'G-id')
1730
self.expectFailure("We don't do an actual heads() check on lca values,"
1731
" or use the per-attribute graph",
1732
self.assertEqual, [], list(merge_obj._entries_lca()))
1734
def test_one_lca_accidentally_pruned(self):
1735
# Another incorrect resolution from the same basic flaw:
1738
# B C B value 'bar', C = 'foo'
1740
# D E D = 'bar', E reverts to 'foo'
1742
# F G F = 'bing', G switches to 'bar'
1744
# 'bar' will not be seen as an interesting change, because 'foo' will
1745
# be pruned from the LCAs, even though it was newly introduced by E
1747
builder = self.get_builder()
1748
builder.build_snapshot('A-id', None,
1749
[('add', (u'', 'a-root-id', 'directory', None)),
1750
('add', (u'foo', 'foo-id', 'file', 'A content\n'))])
1751
builder.build_snapshot('C-id', ['A-id'], [])
1752
builder.build_snapshot('B-id', ['A-id'],
1753
[('rename', ('foo', 'bar'))])
1754
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1755
builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
1756
builder.build_snapshot('G-id', ['E-id', 'D-id'],
1757
[('rename', ('foo', 'bar'))])
1758
builder.build_snapshot('F-id', ['D-id', 'E-id'],
1759
[('rename', ('bar', 'bing'))]) # should end up conflicting
1760
merge_obj = self.make_merge_obj(builder, 'G-id')
1762
entries = list(merge_obj._entries_lca())
1763
root_id = 'a-root-id'
1764
self.expectFailure("We prune values from BASE even when relevant.",
1767
((root_id, [root_id, root_id]), root_id, root_id),
1768
((u'foo', [u'bar', u'foo']), u'bar', u'bing'),
1769
((False, [False, False]), False, False)),
1772
def test_both_sides_revert(self):
1773
# Both sides of a criss-cross revert the text to the lca
1774
# A base, introduces 'foo'
1776
# B C B modifies 'foo', C modifies 'foo'
1778
# D E D reverts to B, E reverts to C
1779
# This should conflict
1780
builder = self.get_builder()
1781
builder.build_snapshot('A-id', None,
1782
[('add', (u'', 'a-root-id', 'directory', None)),
1783
('add', (u'foo', 'foo-id', 'file', 'A content\n'))])
1784
builder.build_snapshot('B-id', ['A-id'],
1785
[('modify', ('foo-id', 'B content\n'))])
1786
builder.build_snapshot('C-id', ['A-id'],
1787
[('modify', ('foo-id', 'C content\n'))])
1788
builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
1789
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1790
merge_obj = self.make_merge_obj(builder, 'E-id')
1792
entries = list(merge_obj._entries_lca())
1793
root_id = 'a-root-id'
1794
self.assertEqual([('foo-id', True,
1795
((root_id, [root_id, root_id]), root_id, root_id),
1796
((u'foo', [u'foo', u'foo']), u'foo', u'foo'),
1797
((False, [False, False]), False, False)),
1800
def test_different_lca_resolve_one_side_updates_content(self):
1801
# Both sides converge, but then one side updates the text.
1802
# A base, introduces 'foo'
1804
# B C B modifies 'foo', C modifies 'foo'
1806
# D E D reverts to B, E reverts to C
1808
# F F updates to a new value
1809
# We need to emit an entry for 'foo', because D & E differed on the
1811
builder = self.get_builder()
1812
builder.build_snapshot('A-id', None,
1813
[('add', (u'', 'a-root-id', 'directory', None)),
1814
('add', (u'foo', 'foo-id', 'file', 'A content\n'))])
1815
builder.build_snapshot('B-id', ['A-id'],
1816
[('modify', ('foo-id', 'B content\n'))])
1817
builder.build_snapshot('C-id', ['A-id'],
1818
[('modify', ('foo-id', 'C content\n'))])
1819
builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
1820
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1821
builder.build_snapshot('F-id', ['D-id'],
1822
[('modify', ('foo-id', 'F content\n'))])
1823
merge_obj = self.make_merge_obj(builder, 'E-id')
1825
entries = list(merge_obj._entries_lca())
1826
root_id = 'a-root-id'
1827
self.assertEqual([('foo-id', True,
1828
((root_id, [root_id, root_id]), root_id, root_id),
1829
((u'foo', [u'foo', u'foo']), u'foo', u'foo'),
1830
((False, [False, False]), False, False)),
1833
def test_same_lca_resolution_one_side_updates_content(self):
1834
# Both sides converge, but then one side updates the text.
1835
# A base, introduces 'foo'
1837
# B C B modifies 'foo', C modifies 'foo'
1839
# D E D and E use C's value
1841
# F F updates to a new value
1842
# I think it is a bug that this conflicts, but we don't have a way to
1843
# detect otherwise. And because of:
1844
# test_different_lca_resolve_one_side_updates_content
1845
# We need to conflict.
1847
builder = self.get_builder()
1848
builder.build_snapshot('A-id', None,
1849
[('add', (u'', 'a-root-id', 'directory', None)),
1850
('add', (u'foo', 'foo-id', 'file', 'A content\n'))])
1851
builder.build_snapshot('B-id', ['A-id'],
1852
[('modify', ('foo-id', 'B content\n'))])
1853
builder.build_snapshot('C-id', ['A-id'],
1854
[('modify', ('foo-id', 'C content\n'))])
1855
builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
1856
builder.build_snapshot('D-id', ['B-id', 'C-id'],
1857
[('modify', ('foo-id', 'C content\n'))]) # Same as E
1858
builder.build_snapshot('F-id', ['D-id'],
1859
[('modify', ('foo-id', 'F content\n'))])
1860
merge_obj = self.make_merge_obj(builder, 'E-id')
1862
entries = list(merge_obj._entries_lca())
1863
self.expectFailure("We don't detect that LCA resolution was the"
1864
" same on both sides",
1865
self.assertEqual, [], entries)
1867
def test_only_path_changed(self):
1868
builder = self.get_builder()
1869
builder.build_snapshot('A-id', None,
1870
[('add', (u'', 'a-root-id', 'directory', None)),
1871
('add', (u'a', 'a-id', 'file', 'content\n'))])
1872
builder.build_snapshot('B-id', ['A-id'], [])
1873
builder.build_snapshot('C-id', ['A-id'], [])
1874
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1875
[('rename', (u'a', u'b'))])
1876
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1877
merge_obj = self.make_merge_obj(builder, 'E-id')
1878
entries = list(merge_obj._entries_lca())
1879
root_id = 'a-root-id'
1880
# The content was not changed, only the path
1881
self.assertEqual([('a-id', False,
1882
((root_id, [root_id, root_id]), root_id, root_id),
1883
((u'a', [u'a', u'a']), u'b', u'a'),
1884
((False, [False, False]), False, False)),
1887
def test_kind_changed(self):
1888
# Identical content, except 'D' changes a-id into a directory
1889
builder = self.get_builder()
1890
builder.build_snapshot('A-id', None,
1891
[('add', (u'', 'a-root-id', 'directory', None)),
1892
('add', (u'a', 'a-id', 'file', 'content\n'))])
1893
builder.build_snapshot('B-id', ['A-id'], [])
1894
builder.build_snapshot('C-id', ['A-id'], [])
1895
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1896
[('unversion', 'a-id'),
1898
('add', (u'a', 'a-id', 'directory', None))])
1899
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1900
merge_obj = self.make_merge_obj(builder, 'E-id')
1901
entries = list(merge_obj._entries_lca())
1902
root_id = 'a-root-id'
1903
# Only the kind was changed (content)
1904
self.assertEqual([('a-id', True,
1905
((root_id, [root_id, root_id]), root_id, root_id),
1906
((u'a', [u'a', u'a']), u'a', u'a'),
1907
((False, [False, False]), False, False)),
1910
def test_this_changed_kind(self):
1911
# Identical content, but THIS changes a file to a directory
1912
builder = self.get_builder()
1913
builder.build_snapshot('A-id', None,
1914
[('add', (u'', 'a-root-id', 'directory', None)),
1915
('add', (u'a', 'a-id', 'file', 'content\n'))])
1916
builder.build_snapshot('B-id', ['A-id'], [])
1917
builder.build_snapshot('C-id', ['A-id'], [])
1918
builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
1919
builder.build_snapshot('D-id', ['B-id', 'C-id'],
1920
[('unversion', 'a-id'),
1922
('add', (u'a', 'a-id', 'directory', None))])
1923
merge_obj = self.make_merge_obj(builder, 'E-id')
1924
entries = list(merge_obj._entries_lca())
1925
# Only the kind was changed (content)
1926
self.assertEqual([], entries)
1928
def test_interesting_files(self):
1929
# Two files modified, but we should filter one of them
1930
builder = self.get_builder()
1931
builder.build_snapshot('A-id', None,
1932
[('add', (u'', 'a-root-id', 'directory', None)),
1933
('add', (u'a', 'a-id', 'file', 'content\n')),
1934
('add', (u'b', 'b-id', 'file', 'content\n'))])
1935
builder.build_snapshot('B-id', ['A-id'], [])
1936
builder.build_snapshot('C-id', ['A-id'], [])
1937
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1938
[('modify', ('a-id', 'new-content\n')),
1939
('modify', ('b-id', 'new-content\n'))])
1940
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1941
merge_obj = self.make_merge_obj(builder, 'E-id',
1942
interesting_files=['b'])
1943
entries = list(merge_obj._entries_lca())
1944
root_id = 'a-root-id'
1945
self.assertEqual([('b-id', True,
1946
((root_id, [root_id, root_id]), root_id, root_id),
1947
((u'b', [u'b', u'b']), u'b', u'b'),
1948
((False, [False, False]), False, False)),
1951
def test_interesting_file_in_this(self):
1952
# This renamed the file, but it should still match the entry in other
1953
builder = self.get_builder()
1954
builder.build_snapshot('A-id', None,
1955
[('add', (u'', 'a-root-id', 'directory', None)),
1956
('add', (u'a', 'a-id', 'file', 'content\n')),
1957
('add', (u'b', 'b-id', 'file', 'content\n'))])
1958
builder.build_snapshot('B-id', ['A-id'], [])
1959
builder.build_snapshot('C-id', ['A-id'], [])
1960
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1961
[('modify', ('a-id', 'new-content\n')),
1962
('modify', ('b-id', 'new-content\n'))])
1963
builder.build_snapshot('D-id', ['B-id', 'C-id'],
1964
[('rename', ('b', 'c'))])
1965
merge_obj = self.make_merge_obj(builder, 'E-id',
1966
interesting_files=['c'])
1967
entries = list(merge_obj._entries_lca())
1968
root_id = 'a-root-id'
1969
self.assertEqual([('b-id', True,
1970
((root_id, [root_id, root_id]), root_id, root_id),
1971
((u'b', [u'b', u'b']), u'b', u'c'),
1972
((False, [False, False]), False, False)),
1975
def test_interesting_file_in_base(self):
1976
# This renamed the file, but it should still match the entry in BASE
1977
builder = self.get_builder()
1978
builder.build_snapshot('A-id', None,
1979
[('add', (u'', 'a-root-id', 'directory', None)),
1980
('add', (u'a', 'a-id', 'file', 'content\n')),
1981
('add', (u'c', 'c-id', 'file', 'content\n'))])
1982
builder.build_snapshot('B-id', ['A-id'],
1983
[('rename', ('c', 'b'))])
1984
builder.build_snapshot('C-id', ['A-id'],
1985
[('rename', ('c', 'b'))])
1986
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1987
[('modify', ('a-id', 'new-content\n')),
1988
('modify', ('c-id', 'new-content\n'))])
1989
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1990
merge_obj = self.make_merge_obj(builder, 'E-id',
1991
interesting_files=['c'])
1992
entries = list(merge_obj._entries_lca())
1993
root_id = 'a-root-id'
1994
self.assertEqual([('c-id', True,
1995
((root_id, [root_id, root_id]), root_id, root_id),
1996
((u'c', [u'b', u'b']), u'b', u'b'),
1997
((False, [False, False]), False, False)),
2000
def test_interesting_file_in_lca(self):
2001
# This renamed the file, but it should still match the entry in LCA
2002
builder = self.get_builder()
2003
builder.build_snapshot('A-id', None,
2004
[('add', (u'', 'a-root-id', 'directory', None)),
2005
('add', (u'a', 'a-id', 'file', 'content\n')),
2006
('add', (u'b', 'b-id', 'file', 'content\n'))])
2007
builder.build_snapshot('B-id', ['A-id'],
2008
[('rename', ('b', 'c'))])
2009
builder.build_snapshot('C-id', ['A-id'], [])
2010
builder.build_snapshot('E-id', ['C-id', 'B-id'],
2011
[('modify', ('a-id', 'new-content\n')),
2012
('modify', ('b-id', 'new-content\n'))])
2013
builder.build_snapshot('D-id', ['B-id', 'C-id'],
2014
[('rename', ('c', 'b'))])
2015
merge_obj = self.make_merge_obj(builder, 'E-id',
2016
interesting_files=['c'])
2017
entries = list(merge_obj._entries_lca())
2018
root_id = 'a-root-id'
2019
self.assertEqual([('b-id', True,
2020
((root_id, [root_id, root_id]), root_id, root_id),
2021
((u'b', [u'c', u'b']), u'b', u'b'),
2022
((False, [False, False]), False, False)),
2025
def test_interesting_ids(self):
2026
# Two files modified, but we should filter one of them
2027
builder = self.get_builder()
2028
builder.build_snapshot('A-id', None,
2029
[('add', (u'', 'a-root-id', 'directory', None)),
2030
('add', (u'a', 'a-id', 'file', 'content\n')),
2031
('add', (u'b', 'b-id', 'file', 'content\n'))])
2032
builder.build_snapshot('B-id', ['A-id'], [])
2033
builder.build_snapshot('C-id', ['A-id'], [])
2034
builder.build_snapshot('E-id', ['C-id', 'B-id'],
2035
[('modify', ('a-id', 'new-content\n')),
2036
('modify', ('b-id', 'new-content\n'))])
2037
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
2038
merge_obj = self.make_merge_obj(builder, 'E-id',
2039
interesting_ids=['b-id'])
2040
entries = list(merge_obj._entries_lca())
2041
root_id = 'a-root-id'
2042
self.assertEqual([('b-id', True,
2043
((root_id, [root_id, root_id]), root_id, root_id),
2044
((u'b', [u'b', u'b']), u'b', u'b'),
2045
((False, [False, False]), False, False)),
2050
class TestMergerEntriesLCAOnDisk(tests.TestCaseWithTransport):
2052
def get_builder(self):
2053
builder = self.make_branch_builder('path')
2054
builder.start_series()
2055
self.addCleanup(builder.finish_series)
2058
def get_wt_from_builder(self, builder):
2059
"""Get a real WorkingTree from the builder."""
2060
the_branch = builder.get_branch()
2061
wt = the_branch.bzrdir.create_workingtree()
2062
# Note: This is a little bit ugly, but we are holding the branch
2063
# write-locked as part of the build process, and we would like to
2064
# maintain that. So we just force the WT to re-use the same
2066
wt._branch = the_branch
2068
self.addCleanup(wt.unlock)
2071
def do_merge(self, builder, other_revision_id):
2072
wt = self.get_wt_from_builder(builder)
2073
merger = _mod_merge.Merger.from_revision_ids(None,
2074
wt, other_revision_id)
2075
merger.merge_type = _mod_merge.Merge3Merger
2076
return wt, merger.do_merge()
2078
def test_simple_lca(self):
2079
builder = self.get_builder()
2080
builder.build_snapshot('A-id', None,
2081
[('add', (u'', 'a-root-id', 'directory', None)),
2082
('add', (u'a', 'a-id', 'file', 'a\nb\nc\n'))])
2083
builder.build_snapshot('C-id', ['A-id'], [])
2084
builder.build_snapshot('B-id', ['A-id'], [])
2085
builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
2086
builder.build_snapshot('D-id', ['B-id', 'C-id'],
2087
[('modify', ('a-id', 'a\nb\nc\nd\ne\nf\n'))])
2088
wt, conflicts = self.do_merge(builder, 'E-id')
2089
self.assertEqual(0, conflicts)
2090
# The merge should have simply update the contents of 'a'
2091
self.assertEqual('a\nb\nc\nd\ne\nf\n', wt.get_file_text('a-id'))
2093
def test_conflict_without_lca(self):
2094
# This test would cause a merge conflict, unless we use the lca trees
2095
# to determine the real ancestry
2098
# B C Path renamed to 'bar' in B
2102
# D E Path at 'bar' in D and E
2104
# F Path at 'baz' in F, which supersedes 'bar' and 'foo'
2105
builder = self.get_builder()
2106
builder.build_snapshot('A-id', None,
2107
[('add', (u'', 'a-root-id', 'directory', None)),
2108
('add', (u'foo', 'foo-id', 'file', 'a\nb\nc\n'))])
2109
builder.build_snapshot('C-id', ['A-id'], [])
2110
builder.build_snapshot('B-id', ['A-id'],
2111
[('rename', ('foo', 'bar'))])
2112
builder.build_snapshot('E-id', ['C-id', 'B-id'], # merge the rename
2113
[('rename', ('foo', 'bar'))])
2114
builder.build_snapshot('F-id', ['E-id'],
2115
[('rename', ('bar', 'baz'))])
2116
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
2117
wt, conflicts = self.do_merge(builder, 'F-id')
2118
self.assertEqual(0, conflicts)
2119
# The merge should simply recognize that the final rename takes
2121
self.assertEqual('baz', wt.id2path('foo-id'))
2123
def test_other_deletes_lca_renames(self):
2124
# This test would cause a merge conflict, unless we use the lca trees
2125
# to determine the real ancestry
2128
# B C Path renamed to 'bar' in B
2132
# D E Path at 'bar' in D and E
2135
builder = self.get_builder()
2136
builder.build_snapshot('A-id', None,
2137
[('add', (u'', 'a-root-id', 'directory', None)),
2138
('add', (u'foo', 'foo-id', 'file', 'a\nb\nc\n'))])
2139
builder.build_snapshot('C-id', ['A-id'], [])
2140
builder.build_snapshot('B-id', ['A-id'],
2141
[('rename', ('foo', 'bar'))])
2142
builder.build_snapshot('E-id', ['C-id', 'B-id'], # merge the rename
2143
[('rename', ('foo', 'bar'))])
2144
builder.build_snapshot('F-id', ['E-id'],
2145
[('unversion', 'foo-id')])
2146
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
2147
wt, conflicts = self.do_merge(builder, 'F-id')
2148
self.assertEqual(0, conflicts)
2149
self.assertRaises(errors.NoSuchId, wt.id2path, 'foo-id')
2151
def test_executable_changes(self):
2160
# F Executable bit changed
2161
builder = self.get_builder()
2162
builder.build_snapshot('A-id', None,
2163
[('add', (u'', 'a-root-id', 'directory', None)),
2164
('add', (u'foo', 'foo-id', 'file', 'a\nb\nc\n'))])
2165
builder.build_snapshot('C-id', ['A-id'], [])
2166
builder.build_snapshot('B-id', ['A-id'], [])
2167
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
2168
builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
2169
# Have to use a real WT, because BranchBuilder doesn't support exec bit
2170
wt = self.get_wt_from_builder(builder)
2171
tt = transform.TreeTransform(wt)
2173
tt.set_executability(True, tt.trans_id_tree_file_id('foo-id'))
2178
self.assertTrue(wt.is_executable('foo-id'))
2179
wt.commit('F-id', rev_id='F-id')
2180
# Reset to D, so that we can merge F
2181
wt.set_parent_ids(['D-id'])
2182
wt.branch.set_last_revision_info(3, 'D-id')
2184
self.assertFalse(wt.is_executable('foo-id'))
2185
conflicts = wt.merge_from_branch(wt.branch, to_revision='F-id')
2186
self.assertEqual(0, conflicts)
2187
self.assertTrue(wt.is_executable('foo-id'))
2189
def test_create_symlink(self):
2190
self.requireFeature(features.SymlinkFeature)
2199
# F Add a symlink 'foo' => 'bar'
2200
# Have to use a real WT, because BranchBuilder and MemoryTree don't
2201
# have symlink support
2202
builder = self.get_builder()
2203
builder.build_snapshot('A-id', None,
2204
[('add', (u'', 'a-root-id', 'directory', None))])
2205
builder.build_snapshot('C-id', ['A-id'], [])
2206
builder.build_snapshot('B-id', ['A-id'], [])
2207
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
2208
builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
2209
# Have to use a real WT, because BranchBuilder doesn't support exec bit
2210
wt = self.get_wt_from_builder(builder)
2211
os.symlink('bar', 'path/foo')
2212
wt.add(['foo'], ['foo-id'])
2213
self.assertEqual('bar', wt.get_symlink_target('foo-id'))
2214
wt.commit('add symlink', rev_id='F-id')
2215
# Reset to D, so that we can merge F
2216
wt.set_parent_ids(['D-id'])
2217
wt.branch.set_last_revision_info(3, 'D-id')
2219
self.assertIs(None, wt.path2id('foo'))
2220
conflicts = wt.merge_from_branch(wt.branch, to_revision='F-id')
2221
self.assertEqual(0, conflicts)
2222
self.assertEqual('foo-id', wt.path2id('foo'))
2223
self.assertEqual('bar', wt.get_symlink_target('foo-id'))
2225
def test_both_sides_revert(self):
2226
# Both sides of a criss-cross revert the text to the lca
2227
# A base, introduces 'foo'
2229
# B C B modifies 'foo', C modifies 'foo'
2231
# D E D reverts to B, E reverts to C
2232
# This should conflict
2233
# This must be done with a real WorkingTree, because normally their
2234
# inventory contains "None" rather than a real sha1
2235
builder = self.get_builder()
2236
builder.build_snapshot('A-id', None,
2237
[('add', (u'', 'a-root-id', 'directory', None)),
2238
('add', (u'foo', 'foo-id', 'file', 'A content\n'))])
2239
builder.build_snapshot('B-id', ['A-id'],
2240
[('modify', ('foo-id', 'B content\n'))])
2241
builder.build_snapshot('C-id', ['A-id'],
2242
[('modify', ('foo-id', 'C content\n'))])
2243
builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
2244
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
2245
wt, conflicts = self.do_merge(builder, 'E-id')
2246
self.assertEqual(1, conflicts)
2247
self.assertEqualDiff('<<<<<<< TREE\n'
2251
'>>>>>>> MERGE-SOURCE\n',
2252
wt.get_file_text('foo-id'))
2254
def test_modified_symlink(self):
2255
self.requireFeature(features.SymlinkFeature)
2256
# A Create symlink foo => bar
2258
# B C B relinks foo => baz
2262
# D E D & E have foo => baz
2264
# F F changes it to bing
2266
# Merging D & F should result in F cleanly overriding D, because D's
2267
# value actually comes from B
2269
# Have to use a real WT, because BranchBuilder and MemoryTree don't
2270
# have symlink support
2271
wt = self.make_branch_and_tree('path')
2273
self.addCleanup(wt.unlock)
2274
os.symlink('bar', 'path/foo')
2275
wt.add(['foo'], ['foo-id'])
2276
wt.commit('add symlink', rev_id='A-id')
2277
os.remove('path/foo')
2278
os.symlink('baz', 'path/foo')
2279
wt.commit('foo => baz', rev_id='B-id')
2280
wt.set_last_revision('A-id')
2281
wt.branch.set_last_revision_info(1, 'A-id')
2283
wt.commit('C', rev_id='C-id')
2284
wt.merge_from_branch(wt.branch, 'B-id')
2285
self.assertEqual('baz', wt.get_symlink_target('foo-id'))
2286
wt.commit('E merges C & B', rev_id='E-id')
2287
os.remove('path/foo')
2288
os.symlink('bing', 'path/foo')
2289
wt.commit('F foo => bing', rev_id='F-id')
2290
wt.set_last_revision('B-id')
2291
wt.branch.set_last_revision_info(2, 'B-id')
2293
wt.merge_from_branch(wt.branch, 'C-id')
2294
wt.commit('D merges B & C', rev_id='D-id')
2295
conflicts = wt.merge_from_branch(wt.branch, to_revision='F-id')
2296
self.assertEqual(0, conflicts)
2297
self.assertEqual('bing', wt.get_symlink_target('foo-id'))
2299
def test_renamed_symlink(self):
2300
self.requireFeature(features.SymlinkFeature)
2301
# A Create symlink foo => bar
2303
# B C B renames foo => barry
2307
# D E D & E have barry
2309
# F F renames barry to blah
2311
# Merging D & F should result in F cleanly overriding D, because D's
2312
# value actually comes from B
2314
wt = self.make_branch_and_tree('path')
2316
self.addCleanup(wt.unlock)
2317
os.symlink('bar', 'path/foo')
2318
wt.add(['foo'], ['foo-id'])
2319
wt.commit('A add symlink', rev_id='A-id')
2320
wt.rename_one('foo', 'barry')
2321
wt.commit('B foo => barry', rev_id='B-id')
2322
wt.set_last_revision('A-id')
2323
wt.branch.set_last_revision_info(1, 'A-id')
2325
wt.commit('C', rev_id='C-id')
2326
wt.merge_from_branch(wt.branch, 'B-id')
2327
self.assertEqual('barry', wt.id2path('foo-id'))
2328
self.assertEqual('bar', wt.get_symlink_target('foo-id'))
2329
wt.commit('E merges C & B', rev_id='E-id')
2330
wt.rename_one('barry', 'blah')
2331
wt.commit('F barry => blah', rev_id='F-id')
2332
wt.set_last_revision('B-id')
2333
wt.branch.set_last_revision_info(2, 'B-id')
2335
wt.merge_from_branch(wt.branch, 'C-id')
2336
wt.commit('D merges B & C', rev_id='D-id')
2337
self.assertEqual('barry', wt.id2path('foo-id'))
2338
# Check the output of the Merger object directly
2339
merger = _mod_merge.Merger.from_revision_ids(None,
2341
merger.merge_type = _mod_merge.Merge3Merger
2342
merge_obj = merger.make_merger()
2343
root_id = wt.path2id('')
2344
entries = list(merge_obj._entries_lca())
2345
# No content change, just a path change
2346
self.assertEqual([('foo-id', False,
2347
((root_id, [root_id, root_id]), root_id, root_id),
2348
((u'foo', [u'barry', u'foo']), u'blah', u'barry'),
2349
((False, [False, False]), False, False)),
2351
conflicts = wt.merge_from_branch(wt.branch, to_revision='F-id')
2352
self.assertEqual(0, conflicts)
2353
self.assertEqual('blah', wt.id2path('foo-id'))
2355
def test_symlink_no_content_change(self):
2356
self.requireFeature(features.SymlinkFeature)
2357
# A Create symlink foo => bar
2359
# B C B relinks foo => baz
2363
# D E D & E have foo => baz
2365
# F F has foo => bing
2367
# Merging E into F should not cause a conflict, because E doesn't have
2368
# a content change relative to the LCAs (it does relative to A)
2369
wt = self.make_branch_and_tree('path')
2371
self.addCleanup(wt.unlock)
2372
os.symlink('bar', 'path/foo')
2373
wt.add(['foo'], ['foo-id'])
2374
wt.commit('add symlink', rev_id='A-id')
2375
os.remove('path/foo')
2376
os.symlink('baz', 'path/foo')
2377
wt.commit('foo => baz', rev_id='B-id')
2378
wt.set_last_revision('A-id')
2379
wt.branch.set_last_revision_info(1, 'A-id')
2381
wt.commit('C', rev_id='C-id')
2382
wt.merge_from_branch(wt.branch, 'B-id')
2383
self.assertEqual('baz', wt.get_symlink_target('foo-id'))
2384
wt.commit('E merges C & B', rev_id='E-id')
2385
wt.set_last_revision('B-id')
2386
wt.branch.set_last_revision_info(2, 'B-id')
2388
wt.merge_from_branch(wt.branch, 'C-id')
2389
wt.commit('D merges B & C', rev_id='D-id')
2390
os.remove('path/foo')
2391
os.symlink('bing', 'path/foo')
2392
wt.commit('F foo => bing', rev_id='F-id')
2394
# Check the output of the Merger object directly
2395
merger = _mod_merge.Merger.from_revision_ids(None,
2397
merger.merge_type = _mod_merge.Merge3Merger
2398
merge_obj = merger.make_merger()
2399
# Nothing interesting happened in OTHER relative to BASE
2400
self.assertEqual([], list(merge_obj._entries_lca()))
2401
# Now do a real merge, just to test the rest of the stack
2402
conflicts = wt.merge_from_branch(wt.branch, to_revision='E-id')
2403
self.assertEqual(0, conflicts)
2404
self.assertEqual('bing', wt.get_symlink_target('foo-id'))
2406
def test_symlink_this_changed_kind(self):
2407
self.requireFeature(features.SymlinkFeature)
2410
# B C B creates symlink foo => bar
2414
# D E D changes foo into a file, E has foo => bing
2416
# Mostly, this is trying to test that we don't try to os.readlink() on
2417
# a file, or when there is nothing there
2418
wt = self.make_branch_and_tree('path')
2420
self.addCleanup(wt.unlock)
2421
wt.commit('base', rev_id='A-id')
2422
os.symlink('bar', 'path/foo')
2423
wt.add(['foo'], ['foo-id'])
2424
wt.commit('add symlink foo => bar', rev_id='B-id')
2425
wt.set_last_revision('A-id')
2426
wt.branch.set_last_revision_info(1, 'A-id')
2428
wt.commit('C', rev_id='C-id')
2429
wt.merge_from_branch(wt.branch, 'B-id')
2430
self.assertEqual('bar', wt.get_symlink_target('foo-id'))
2431
os.remove('path/foo')
2432
# We have to change the link in E, or it won't try to do a comparison
2433
os.symlink('bing', 'path/foo')
2434
wt.commit('E merges C & B, overrides to bing', rev_id='E-id')
2435
wt.set_last_revision('B-id')
2436
wt.branch.set_last_revision_info(2, 'B-id')
2438
wt.merge_from_branch(wt.branch, 'C-id')
2439
os.remove('path/foo')
2440
self.build_tree_contents([('path/foo', 'file content\n')])
2441
# XXX: workaround, WT doesn't detect kind changes unless you do
2443
list(wt.iter_changes(wt.basis_tree()))
2444
wt.commit('D merges B & C, makes it a file', rev_id='D-id')
2446
merger = _mod_merge.Merger.from_revision_ids(None,
2448
merger.merge_type = _mod_merge.Merge3Merger
2449
merge_obj = merger.make_merger()
2450
entries = list(merge_obj._entries_lca())
2451
root_id = wt.path2id('')
2452
self.assertEqual([('foo-id', True,
2453
((None, [root_id, None]), root_id, root_id),
2454
((None, [u'foo', None]), u'foo', u'foo'),
2455
((None, [False, None]), False, False)),
2458
def test_symlink_all_wt(self):
2459
"""Check behavior if all trees are Working Trees."""
2460
self.requireFeature(features.SymlinkFeature)
2461
# The big issue is that entry.symlink_target is None for WorkingTrees.
2462
# So we need to make sure we handle that case correctly.
2465
# B C B relinks foo => baz
2467
# D E D & E have foo => baz
2469
# F F changes it to bing
2470
# Merging D & F should result in F cleanly overriding D, because D's
2471
# value actually comes from B
2473
wt = self.make_branch_and_tree('path')
2475
self.addCleanup(wt.unlock)
2476
os.symlink('bar', 'path/foo')
2477
wt.add(['foo'], ['foo-id'])
2478
wt.commit('add symlink', rev_id='A-id')
2479
os.remove('path/foo')
2480
os.symlink('baz', 'path/foo')
2481
wt.commit('foo => baz', rev_id='B-id')
2482
wt.set_last_revision('A-id')
2483
wt.branch.set_last_revision_info(1, 'A-id')
2485
wt.commit('C', rev_id='C-id')
2486
wt.merge_from_branch(wt.branch, 'B-id')
2487
self.assertEqual('baz', wt.get_symlink_target('foo-id'))
2488
wt.commit('E merges C & B', rev_id='E-id')
2489
os.remove('path/foo')
2490
os.symlink('bing', 'path/foo')
2491
wt.commit('F foo => bing', rev_id='F-id')
2492
wt.set_last_revision('B-id')
2493
wt.branch.set_last_revision_info(2, 'B-id')
2495
wt.merge_from_branch(wt.branch, 'C-id')
2496
wt.commit('D merges B & C', rev_id='D-id')
2497
wt_base = wt.bzrdir.sprout('base', 'A-id').open_workingtree()
2499
self.addCleanup(wt_base.unlock)
2500
wt_lca1 = wt.bzrdir.sprout('b-tree', 'B-id').open_workingtree()
2502
self.addCleanup(wt_lca1.unlock)
2503
wt_lca2 = wt.bzrdir.sprout('c-tree', 'C-id').open_workingtree()
2505
self.addCleanup(wt_lca2.unlock)
2506
wt_other = wt.bzrdir.sprout('other', 'F-id').open_workingtree()
2507
wt_other.lock_read()
2508
self.addCleanup(wt_other.unlock)
2509
merge_obj = _mod_merge.Merge3Merger(wt, wt, wt_base,
2510
wt_other, lca_trees=[wt_lca1, wt_lca2], do_merge=False)
2511
entries = list(merge_obj._entries_lca())
2512
root_id = wt.path2id('')
2513
self.assertEqual([('foo-id', True,
2514
((root_id, [root_id, root_id]), root_id, root_id),
2515
((u'foo', [u'foo', u'foo']), u'foo', u'foo'),
2516
((False, [False, False]), False, False)),
2519
def test_other_reverted_path_to_base(self):
2522
# B C Path at 'bar' in B
2529
builder = self.get_builder()
2530
builder.build_snapshot('A-id', None,
2531
[('add', (u'', 'a-root-id', 'directory', None)),
2532
('add', (u'foo', 'foo-id', 'file', 'a\nb\nc\n'))])
2533
builder.build_snapshot('C-id', ['A-id'], [])
2534
builder.build_snapshot('B-id', ['A-id'],
2535
[('rename', ('foo', 'bar'))])
2536
builder.build_snapshot('E-id', ['C-id', 'B-id'],
2537
[('rename', ('foo', 'bar'))]) # merge the rename
2538
builder.build_snapshot('F-id', ['E-id'],
2539
[('rename', ('bar', 'foo'))]) # Rename back to BASE
2540
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
2541
wt, conflicts = self.do_merge(builder, 'F-id')
2542
self.assertEqual(0, conflicts)
2543
self.assertEqual('foo', wt.id2path('foo-id'))
2545
def test_other_reverted_content_to_base(self):
2546
builder = self.get_builder()
2547
builder.build_snapshot('A-id', None,
2548
[('add', (u'', 'a-root-id', 'directory', None)),
2549
('add', (u'foo', 'foo-id', 'file', 'base content\n'))])
2550
builder.build_snapshot('C-id', ['A-id'], [])
2551
builder.build_snapshot('B-id', ['A-id'],
2552
[('modify', ('foo-id', 'B content\n'))])
2553
builder.build_snapshot('E-id', ['C-id', 'B-id'],
2554
[('modify', ('foo-id', 'B content\n'))]) # merge the content
2555
builder.build_snapshot('F-id', ['E-id'],
2556
[('modify', ('foo-id', 'base content\n'))]) # Revert back to BASE
2557
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
2558
wt, conflicts = self.do_merge(builder, 'F-id')
2559
self.assertEqual(0, conflicts)
2560
# TODO: We need to use the per-file graph to properly select a BASE
2561
# before this will work. Or at least use the LCA trees to find
2562
# the appropriate content base. (which is B, not A).
2563
self.assertEqual('base content\n', wt.get_file_text('foo-id'))
2565
def test_other_modified_content(self):
2566
builder = self.get_builder()
2567
builder.build_snapshot('A-id', None,
2568
[('add', (u'', 'a-root-id', 'directory', None)),
2569
('add', (u'foo', 'foo-id', 'file', 'base content\n'))])
2570
builder.build_snapshot('C-id', ['A-id'], [])
2571
builder.build_snapshot('B-id', ['A-id'],
2572
[('modify', ('foo-id', 'B content\n'))])
2573
builder.build_snapshot('E-id', ['C-id', 'B-id'],
2574
[('modify', ('foo-id', 'B content\n'))]) # merge the content
2575
builder.build_snapshot('F-id', ['E-id'],
2576
[('modify', ('foo-id', 'F content\n'))]) # Override B content
2577
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
2578
wt, conflicts = self.do_merge(builder, 'F-id')
2579
self.assertEqual(0, conflicts)
2580
self.assertEqual('F content\n', wt.get_file_text('foo-id'))
2582
def test_all_wt(self):
2583
"""Check behavior if all trees are Working Trees."""
2584
# The big issue is that entry.revision is None for WorkingTrees. (as is
2585
# entry.text_sha1, etc. So we need to make sure we handle that case
2587
# A Content of 'foo', path of 'a'
2589
# B C B modifies content, C renames 'a' => 'b'
2591
# D E E updates content, renames 'b' => 'c'
2592
builder = self.get_builder()
2593
builder.build_snapshot('A-id', None,
2594
[('add', (u'', 'a-root-id', 'directory', None)),
2595
('add', (u'a', 'a-id', 'file', 'base content\n')),
2596
('add', (u'foo', 'foo-id', 'file', 'base content\n'))])
2597
builder.build_snapshot('B-id', ['A-id'],
2598
[('modify', ('foo-id', 'B content\n'))])
2599
builder.build_snapshot('C-id', ['A-id'],
2600
[('rename', ('a', 'b'))])
2601
builder.build_snapshot('E-id', ['C-id', 'B-id'],
2602
[('rename', ('b', 'c')),
2603
('modify', ('foo-id', 'E content\n'))])
2604
builder.build_snapshot('D-id', ['B-id', 'C-id'],
2605
[('rename', ('a', 'b'))]) # merged change
2606
wt_this = self.get_wt_from_builder(builder)
2607
wt_base = wt_this.bzrdir.sprout('base', 'A-id').open_workingtree()
2609
self.addCleanup(wt_base.unlock)
2610
wt_lca1 = wt_this.bzrdir.sprout('b-tree', 'B-id').open_workingtree()
2612
self.addCleanup(wt_lca1.unlock)
2613
wt_lca2 = wt_this.bzrdir.sprout('c-tree', 'C-id').open_workingtree()
2615
self.addCleanup(wt_lca2.unlock)
2616
wt_other = wt_this.bzrdir.sprout('other', 'E-id').open_workingtree()
2617
wt_other.lock_read()
2618
self.addCleanup(wt_other.unlock)
2619
merge_obj = _mod_merge.Merge3Merger(wt_this, wt_this, wt_base,
2620
wt_other, lca_trees=[wt_lca1, wt_lca2], do_merge=False)
2621
entries = list(merge_obj._entries_lca())
2622
root_id = 'a-root-id'
2623
self.assertEqual([('a-id', False,
2624
((root_id, [root_id, root_id]), root_id, root_id),
2625
((u'a', [u'a', u'b']), u'c', u'b'),
2626
((False, [False, False]), False, False)),
2628
((root_id, [root_id, root_id]), root_id, root_id),
2629
((u'foo', [u'foo', u'foo']), u'foo', u'foo'),
2630
((False, [False, False]), False, False)),
2633
def test_nested_tree_unmodified(self):
2634
# Tested with a real WT, because BranchBuilder/MemoryTree don't handle
2636
wt = self.make_branch_and_tree('tree',
2637
format='dirstate-with-subtree')
2639
self.addCleanup(wt.unlock)
2640
sub_tree = self.make_branch_and_tree('tree/sub-tree',
2641
format='dirstate-with-subtree')
2642
wt.set_root_id('a-root-id')
2643
sub_tree.set_root_id('sub-tree-root')
2644
self.build_tree_contents([('tree/sub-tree/file', 'text1')])
2645
sub_tree.add('file')
2646
sub_tree.commit('foo', rev_id='sub-A-id')
2647
wt.add_reference(sub_tree)
2648
wt.commit('set text to 1', rev_id='A-id', recursive=None)
2649
# Now create a criss-cross merge in the parent, without modifying the
2651
wt.commit('B', rev_id='B-id', recursive=None)
2652
wt.set_last_revision('A-id')
2653
wt.branch.set_last_revision_info(1, 'A-id')
2654
wt.commit('C', rev_id='C-id', recursive=None)
2655
wt.merge_from_branch(wt.branch, to_revision='B-id')
2656
wt.commit('E', rev_id='E-id', recursive=None)
2657
wt.set_parent_ids(['B-id', 'C-id'])
2658
wt.branch.set_last_revision_info(2, 'B-id')
2659
wt.commit('D', rev_id='D-id', recursive=None)
2661
merger = _mod_merge.Merger.from_revision_ids(None,
2663
merger.merge_type = _mod_merge.Merge3Merger
2664
merge_obj = merger.make_merger()
2665
entries = list(merge_obj._entries_lca())
2666
self.assertEqual([], entries)
2668
def test_nested_tree_subtree_modified(self):
2669
# Tested with a real WT, because BranchBuilder/MemoryTree don't handle
2671
wt = self.make_branch_and_tree('tree',
2672
format='dirstate-with-subtree')
2674
self.addCleanup(wt.unlock)
2675
sub_tree = self.make_branch_and_tree('tree/sub',
2676
format='dirstate-with-subtree')
2677
wt.set_root_id('a-root-id')
2678
sub_tree.set_root_id('sub-tree-root')
2679
self.build_tree_contents([('tree/sub/file', 'text1')])
2680
sub_tree.add('file')
2681
sub_tree.commit('foo', rev_id='sub-A-id')
2682
wt.add_reference(sub_tree)
2683
wt.commit('set text to 1', rev_id='A-id', recursive=None)
2684
# Now create a criss-cross merge in the parent, without modifying the
2686
wt.commit('B', rev_id='B-id', recursive=None)
2687
wt.set_last_revision('A-id')
2688
wt.branch.set_last_revision_info(1, 'A-id')
2689
wt.commit('C', rev_id='C-id', recursive=None)
2690
wt.merge_from_branch(wt.branch, to_revision='B-id')
2691
self.build_tree_contents([('tree/sub/file', 'text2')])
2692
sub_tree.commit('modify contents', rev_id='sub-B-id')
2693
wt.commit('E', rev_id='E-id', recursive=None)
2694
wt.set_parent_ids(['B-id', 'C-id'])
2695
wt.branch.set_last_revision_info(2, 'B-id')
2696
wt.commit('D', rev_id='D-id', recursive=None)
2698
merger = _mod_merge.Merger.from_revision_ids(None,
2700
merger.merge_type = _mod_merge.Merge3Merger
2701
merge_obj = merger.make_merger()
2702
entries = list(merge_obj._entries_lca())
2703
# Nothing interesting about this sub-tree, because content changes are
2704
# computed at a higher level
2705
self.assertEqual([], entries)
2707
def test_nested_tree_subtree_renamed(self):
2708
# Tested with a real WT, because BranchBuilder/MemoryTree don't handle
2710
wt = self.make_branch_and_tree('tree',
2711
format='dirstate-with-subtree')
2713
self.addCleanup(wt.unlock)
2714
sub_tree = self.make_branch_and_tree('tree/sub',
2715
format='dirstate-with-subtree')
2716
wt.set_root_id('a-root-id')
2717
sub_tree.set_root_id('sub-tree-root')
2718
self.build_tree_contents([('tree/sub/file', 'text1')])
2719
sub_tree.add('file')
2720
sub_tree.commit('foo', rev_id='sub-A-id')
2721
wt.add_reference(sub_tree)
2722
wt.commit('set text to 1', rev_id='A-id', recursive=None)
2723
# Now create a criss-cross merge in the parent, without modifying the
2725
wt.commit('B', rev_id='B-id', recursive=None)
2726
wt.set_last_revision('A-id')
2727
wt.branch.set_last_revision_info(1, 'A-id')
2728
wt.commit('C', rev_id='C-id', recursive=None)
2729
wt.merge_from_branch(wt.branch, to_revision='B-id')
2730
wt.rename_one('sub', 'alt_sub')
2731
wt.commit('E', rev_id='E-id', recursive=None)
2732
wt.set_last_revision('B-id')
2734
wt.set_parent_ids(['B-id', 'C-id'])
2735
wt.branch.set_last_revision_info(2, 'B-id')
2736
wt.commit('D', rev_id='D-id', recursive=None)
2738
merger = _mod_merge.Merger.from_revision_ids(None,
2740
merger.merge_type = _mod_merge.Merge3Merger
2741
merge_obj = merger.make_merger()
2742
entries = list(merge_obj._entries_lca())
2743
root_id = 'a-root-id'
2744
self.assertEqual([('sub-tree-root', False,
2745
((root_id, [root_id, root_id]), root_id, root_id),
2746
((u'sub', [u'sub', u'sub']), u'alt_sub', u'sub'),
2747
((False, [False, False]), False, False)),
2750
def test_nested_tree_subtree_renamed_and_modified(self):
2751
# Tested with a real WT, because BranchBuilder/MemoryTree don't handle
2753
wt = self.make_branch_and_tree('tree',
2754
format='dirstate-with-subtree')
2756
self.addCleanup(wt.unlock)
2757
sub_tree = self.make_branch_and_tree('tree/sub',
2758
format='dirstate-with-subtree')
2759
wt.set_root_id('a-root-id')
2760
sub_tree.set_root_id('sub-tree-root')
2761
self.build_tree_contents([('tree/sub/file', 'text1')])
2762
sub_tree.add('file')
2763
sub_tree.commit('foo', rev_id='sub-A-id')
2764
wt.add_reference(sub_tree)
2765
wt.commit('set text to 1', rev_id='A-id', recursive=None)
2766
# Now create a criss-cross merge in the parent, without modifying the
2768
wt.commit('B', rev_id='B-id', recursive=None)
2769
wt.set_last_revision('A-id')
2770
wt.branch.set_last_revision_info(1, 'A-id')
2771
wt.commit('C', rev_id='C-id', recursive=None)
2772
wt.merge_from_branch(wt.branch, to_revision='B-id')
2773
self.build_tree_contents([('tree/sub/file', 'text2')])
2774
sub_tree.commit('modify contents', rev_id='sub-B-id')
2775
wt.rename_one('sub', 'alt_sub')
2776
wt.commit('E', rev_id='E-id', recursive=None)
2777
wt.set_last_revision('B-id')
2779
wt.set_parent_ids(['B-id', 'C-id'])
2780
wt.branch.set_last_revision_info(2, 'B-id')
2781
wt.commit('D', rev_id='D-id', recursive=None)
2783
merger = _mod_merge.Merger.from_revision_ids(None,
2785
merger.merge_type = _mod_merge.Merge3Merger
2786
merge_obj = merger.make_merger()
2787
entries = list(merge_obj._entries_lca())
2788
root_id = 'a-root-id'
2789
self.assertEqual([('sub-tree-root', False,
2790
((root_id, [root_id, root_id]), root_id, root_id),
2791
((u'sub', [u'sub', u'sub']), u'alt_sub', u'sub'),
2792
((False, [False, False]), False, False)),
2796
class TestLCAMultiWay(tests.TestCase):
2798
def assertLCAMultiWay(self, expected, base, lcas, other, this,
2799
allow_overriding_lca=True):
2800
self.assertEqual(expected, _mod_merge.Merge3Merger._lca_multi_way(
2801
(base, lcas), other, this,
2802
allow_overriding_lca=allow_overriding_lca))
2804
def test_other_equal_equal_lcas(self):
2805
"""Test when OTHER=LCA and all LCAs are identical."""
2806
self.assertLCAMultiWay('this',
2807
'bval', ['bval', 'bval'], 'bval', 'bval')
2808
self.assertLCAMultiWay('this',
2809
'bval', ['lcaval', 'lcaval'], 'lcaval', 'bval')
2810
self.assertLCAMultiWay('this',
2811
'bval', ['lcaval', 'lcaval', 'lcaval'], 'lcaval', 'bval')
2812
self.assertLCAMultiWay('this',
2813
'bval', ['lcaval', 'lcaval', 'lcaval'], 'lcaval', 'tval')
2814
self.assertLCAMultiWay('this',
2815
'bval', ['lcaval', 'lcaval', 'lcaval'], 'lcaval', None)
2817
def test_other_equal_this(self):
2818
"""Test when other and this are identical."""
2819
self.assertLCAMultiWay('this',
2820
'bval', ['bval', 'bval'], 'oval', 'oval')
2821
self.assertLCAMultiWay('this',
2822
'bval', ['lcaval', 'lcaval'], 'oval', 'oval')
2823
self.assertLCAMultiWay('this',
2824
'bval', ['cval', 'dval'], 'oval', 'oval')
2825
self.assertLCAMultiWay('this',
2826
'bval', [None, 'lcaval'], 'oval', 'oval')
2827
self.assertLCAMultiWay('this',
2828
None, [None, 'lcaval'], 'oval', 'oval')
2829
self.assertLCAMultiWay('this',
2830
None, ['lcaval', 'lcaval'], 'oval', 'oval')
2831
self.assertLCAMultiWay('this',
2832
None, ['cval', 'dval'], 'oval', 'oval')
2833
self.assertLCAMultiWay('this',
2834
None, ['cval', 'dval'], None, None)
2835
self.assertLCAMultiWay('this',
2836
None, ['cval', 'dval', 'eval', 'fval'], 'oval', 'oval')
2838
def test_no_lcas(self):
2839
self.assertLCAMultiWay('this',
2840
'bval', [], 'bval', 'tval')
2841
self.assertLCAMultiWay('other',
2842
'bval', [], 'oval', 'bval')
2843
self.assertLCAMultiWay('conflict',
2844
'bval', [], 'oval', 'tval')
2845
self.assertLCAMultiWay('this',
2846
'bval', [], 'oval', 'oval')
2848
def test_lca_supersedes_other_lca(self):
2849
"""If one lca == base, the other lca takes precedence"""
2850
self.assertLCAMultiWay('this',
2851
'bval', ['bval', 'lcaval'], 'lcaval', 'tval')
2852
self.assertLCAMultiWay('this',
2853
'bval', ['bval', 'lcaval'], 'lcaval', 'bval')
2854
# This is actually considered a 'revert' because the 'lcaval' in LCAS
2855
# supersedes the BASE val (in the other LCA) but then OTHER reverts it
2857
self.assertLCAMultiWay('other',
2858
'bval', ['bval', 'lcaval'], 'bval', 'lcaval')
2859
self.assertLCAMultiWay('conflict',
2860
'bval', ['bval', 'lcaval'], 'bval', 'tval')
2862
def test_other_and_this_pick_different_lca(self):
2863
# OTHER and THIS resolve the lca conflict in different ways
2864
self.assertLCAMultiWay('conflict',
2865
'bval', ['lca1val', 'lca2val'], 'lca1val', 'lca2val')
2866
self.assertLCAMultiWay('conflict',
2867
'bval', ['lca1val', 'lca2val', 'lca3val'], 'lca1val', 'lca2val')
2868
self.assertLCAMultiWay('conflict',
2869
'bval', ['lca1val', 'lca2val', 'bval'], 'lca1val', 'lca2val')
2871
def test_other_in_lca(self):
2872
# OTHER takes a value of one of the LCAs, THIS takes a new value, which
2873
# theoretically supersedes both LCA values and 'wins'
2874
self.assertLCAMultiWay('this',
2875
'bval', ['lca1val', 'lca2val'], 'lca1val', 'newval')
2876
self.assertLCAMultiWay('this',
2877
'bval', ['lca1val', 'lca2val', 'lca3val'], 'lca1val', 'newval')
2878
self.assertLCAMultiWay('conflict',
2879
'bval', ['lca1val', 'lca2val'], 'lca1val', 'newval',
2880
allow_overriding_lca=False)
2881
self.assertLCAMultiWay('conflict',
2882
'bval', ['lca1val', 'lca2val', 'lca3val'], 'lca1val', 'newval',
2883
allow_overriding_lca=False)
2884
# THIS reverted back to BASE, but that is an explicit supersede of all
2886
self.assertLCAMultiWay('this',
2887
'bval', ['lca1val', 'lca2val', 'lca3val'], 'lca1val', 'bval')
2888
self.assertLCAMultiWay('this',
2889
'bval', ['lca1val', 'lca2val', 'bval'], 'lca1val', 'bval')
2890
self.assertLCAMultiWay('conflict',
2891
'bval', ['lca1val', 'lca2val', 'lca3val'], 'lca1val', 'bval',
2892
allow_overriding_lca=False)
2893
self.assertLCAMultiWay('conflict',
2894
'bval', ['lca1val', 'lca2val', 'bval'], 'lca1val', 'bval',
2895
allow_overriding_lca=False)
2897
def test_this_in_lca(self):
2898
# THIS takes a value of one of the LCAs, OTHER takes a new value, which
2899
# theoretically supersedes both LCA values and 'wins'
2900
self.assertLCAMultiWay('other',
2901
'bval', ['lca1val', 'lca2val'], 'oval', 'lca1val')
2902
self.assertLCAMultiWay('other',
2903
'bval', ['lca1val', 'lca2val'], 'oval', 'lca2val')
2904
self.assertLCAMultiWay('conflict',
2905
'bval', ['lca1val', 'lca2val'], 'oval', 'lca1val',
2906
allow_overriding_lca=False)
2907
self.assertLCAMultiWay('conflict',
2908
'bval', ['lca1val', 'lca2val'], 'oval', 'lca2val',
2909
allow_overriding_lca=False)
2910
# OTHER reverted back to BASE, but that is an explicit supersede of all
2912
self.assertLCAMultiWay('other',
2913
'bval', ['lca1val', 'lca2val', 'lca3val'], 'bval', 'lca3val')
2914
self.assertLCAMultiWay('conflict',
2915
'bval', ['lca1val', 'lca2val', 'lca3val'], 'bval', 'lca3val',
2916
allow_overriding_lca=False)
2918
def test_all_differ(self):
2919
self.assertLCAMultiWay('conflict',
2920
'bval', ['lca1val', 'lca2val'], 'oval', 'tval')
2921
self.assertLCAMultiWay('conflict',
2922
'bval', ['lca1val', 'lca2val', 'lca2val'], 'oval', 'tval')
2923
self.assertLCAMultiWay('conflict',
2924
'bval', ['lca1val', 'lca2val', 'lca3val'], 'oval', 'tval')
2927
class TestConfigurableFileMerger(tests.TestCaseWithTransport):
2930
super(TestConfigurableFileMerger, self).setUp()
2933
def get_merger_factory(self):
2934
# Allows the inner methods to access the test attributes
2937
class FooMerger(_mod_merge.ConfigurableFileMerger):
2939
default_files = ['bar']
2941
def merge_text(self, params):
2942
calls.append('merge_text')
2943
return ('not_applicable', None)
2945
def factory(merger):
2946
result = FooMerger(merger)
2947
# Make sure we start with a clean slate
2948
self.assertEqual(None, result.affected_files)
2949
# Track the original merger
2950
self.merger = result
2955
def _install_hook(self, factory):
2956
_mod_merge.Merger.hooks.install_named_hook('merge_file_content',
2957
factory, 'test factory')
2959
def make_builder(self):
2960
builder = test_merge_core.MergeBuilder(self.test_base_dir)
2961
self.addCleanup(builder.cleanup)
2964
def make_text_conflict(self, file_name='bar'):
2965
factory = self.get_merger_factory()
2966
self._install_hook(factory)
2967
builder = self.make_builder()
2968
builder.add_file('bar-id', builder.tree_root, file_name, 'text1', True)
2969
builder.change_contents('bar-id', other='text4', this='text3')
2972
def make_kind_change(self):
2973
factory = self.get_merger_factory()
2974
self._install_hook(factory)
2975
builder = self.make_builder()
2976
builder.add_file('bar-id', builder.tree_root, 'bar', 'text1', True,
2978
builder.add_dir('bar-dir', builder.tree_root, 'bar-id',
2979
base=False, other=False)
2982
def test_uses_this_branch(self):
2983
builder = self.make_text_conflict()
2984
tt = builder.make_preview_transform()
2985
self.addCleanup(tt.finalize)
2987
def test_affected_files_cached(self):
2988
"""Ensures that the config variable is cached"""
2989
builder = self.make_text_conflict()
2990
conflicts = builder.merge()
2991
# The hook should set the variable
2992
self.assertEqual(['bar'], self.merger.affected_files)
2993
self.assertEqual(1, len(conflicts))
2995
def test_hook_called_for_text_conflicts(self):
2996
builder = self.make_text_conflict()
2997
conflicts = builder.merge()
2998
# The hook should call the merge_text() method
2999
self.assertEqual(['merge_text'], self.calls)
3001
def test_hook_not_called_for_kind_change(self):
3002
builder = self.make_kind_change()
3003
conflicts = builder.merge()
3004
# The hook should not call the merge_text() method
3005
self.assertEqual([], self.calls)
3007
def test_hook_not_called_for_other_files(self):
3008
builder = self.make_text_conflict('foobar')
3009
conflicts = builder.merge()
3010
# The hook should not call the merge_text() method
3011
self.assertEqual([], self.calls)
3014
class TestMergeIntoBase(tests.TestCaseWithTransport):
3016
def setup_simple_branch(self, relpath, shape=None, root_id=None):
3017
"""One commit, containing tree specified by optional shape.
3019
Default is empty tree (just root entry).
3022
root_id = '%s-root-id' % (relpath,)
3023
wt = self.make_branch_and_tree(relpath)
3024
wt.set_root_id(root_id)
3025
if shape is not None:
3026
adjusted_shape = [relpath + '/' + elem for elem in shape]
3027
self.build_tree(adjusted_shape)
3028
ids = ['%s-%s-id' % (relpath, basename(elem.rstrip('/')))
3030
wt.add(shape, ids=ids)
3031
rev_id = 'r1-%s' % (relpath,)
3032
wt.commit("Initial commit of %s" % (relpath,), rev_id=rev_id)
3033
self.assertEqual(root_id, wt.path2id(''))
3036
def setup_two_branches(self, custom_root_ids=True):
3037
"""Setup 2 branches, one will be a library, the other a project."""
3041
root_id = inventory.ROOT_ID
3042
project_wt = self.setup_simple_branch(
3043
'project', ['README', 'dir/', 'dir/file.c'],
3045
lib_wt = self.setup_simple_branch(
3046
'lib1', ['README', 'Makefile', 'foo.c'], root_id)
3048
return project_wt, lib_wt
3050
def do_merge_into(self, location, merge_as):
3051
"""Helper for using MergeIntoMerger.
3053
:param location: location of directory to merge from, either the
3054
location of a branch or of a path inside a branch.
3055
:param merge_as: the path in a tree to add the new directory as.
3056
:returns: the conflicts from 'do_merge'.
3058
operation = cleanup.OperationWithCleanups(self._merge_into)
3059
return operation.run(location, merge_as)
3061
def _merge_into(self, op, location, merge_as):
3062
# Open and lock the various tree and branch objects
3063
wt, subdir_relpath = WorkingTree.open_containing(merge_as)
3064
op.add_cleanup(wt.lock_write().unlock)
3065
branch_to_merge, subdir_to_merge = _mod_branch.Branch.open_containing(
3067
op.add_cleanup(branch_to_merge.lock_read().unlock)
3068
other_tree = branch_to_merge.basis_tree()
3069
op.add_cleanup(other_tree.lock_read().unlock)
3071
merger = _mod_merge.MergeIntoMerger(this_tree=wt, other_tree=other_tree,
3072
other_branch=branch_to_merge, target_subdir=subdir_relpath,
3073
source_subpath=subdir_to_merge)
3074
merger.set_base_revision(_mod_revision.NULL_REVISION, branch_to_merge)
3075
conflicts = merger.do_merge()
3076
merger.set_pending()
3079
def assertTreeEntriesEqual(self, expected_entries, tree):
3080
"""Assert that 'tree' contains the expected inventory entries.
3082
:param expected_entries: sequence of (path, file-id) pairs.
3084
files = [(path, ie.file_id) for path, ie in tree.iter_entries_by_dir()]
3085
self.assertEqual(expected_entries, files)
3088
class TestMergeInto(TestMergeIntoBase):
3090
def test_newdir_with_unique_roots(self):
3091
"""Merge a branch with a unique root into a new directory."""
3092
project_wt, lib_wt = self.setup_two_branches()
3093
self.do_merge_into('lib1', 'project/lib1')
3094
project_wt.lock_read()
3095
self.addCleanup(project_wt.unlock)
3096
# The r1-lib1 revision should be merged into this one
3097
self.assertEqual(['r1-project', 'r1-lib1'], project_wt.get_parent_ids())
3098
self.assertTreeEntriesEqual(
3099
[('', 'project-root-id'),
3100
('README', 'project-README-id'),
3101
('dir', 'project-dir-id'),
3102
('lib1', 'lib1-root-id'),
3103
('dir/file.c', 'project-file.c-id'),
3104
('lib1/Makefile', 'lib1-Makefile-id'),
3105
('lib1/README', 'lib1-README-id'),
3106
('lib1/foo.c', 'lib1-foo.c-id'),
3109
def test_subdir(self):
3110
"""Merge a branch into a subdirectory of an existing directory."""
3111
project_wt, lib_wt = self.setup_two_branches()
3112
self.do_merge_into('lib1', 'project/dir/lib1')
3113
project_wt.lock_read()
3114
self.addCleanup(project_wt.unlock)
3115
# The r1-lib1 revision should be merged into this one
3116
self.assertEqual(['r1-project', 'r1-lib1'], project_wt.get_parent_ids())
3117
self.assertTreeEntriesEqual(
3118
[('', 'project-root-id'),
3119
('README', 'project-README-id'),
3120
('dir', 'project-dir-id'),
3121
('dir/file.c', 'project-file.c-id'),
3122
('dir/lib1', 'lib1-root-id'),
3123
('dir/lib1/Makefile', 'lib1-Makefile-id'),
3124
('dir/lib1/README', 'lib1-README-id'),
3125
('dir/lib1/foo.c', 'lib1-foo.c-id'),
3128
def test_newdir_with_repeat_roots(self):
3129
"""If the file-id of the dir to be merged already exists a new ID will
3130
be allocated to let the merge happen.
3132
project_wt, lib_wt = self.setup_two_branches(custom_root_ids=False)
3133
root_id = project_wt.path2id('')
3134
self.do_merge_into('lib1', 'project/lib1')
3135
project_wt.lock_read()
3136
self.addCleanup(project_wt.unlock)
3137
# The r1-lib1 revision should be merged into this one
3138
self.assertEqual(['r1-project', 'r1-lib1'], project_wt.get_parent_ids())
3139
new_lib1_id = project_wt.path2id('lib1')
3140
self.assertNotEqual(None, new_lib1_id)
3141
self.assertTreeEntriesEqual(
3143
('README', 'project-README-id'),
3144
('dir', 'project-dir-id'),
3145
('lib1', new_lib1_id),
3146
('dir/file.c', 'project-file.c-id'),
3147
('lib1/Makefile', 'lib1-Makefile-id'),
3148
('lib1/README', 'lib1-README-id'),
3149
('lib1/foo.c', 'lib1-foo.c-id'),
3152
def test_name_conflict(self):
3153
"""When the target directory name already exists a conflict is
3154
generated and the original directory is renamed to foo.moved.
3156
dest_wt = self.setup_simple_branch('dest', ['dir/', 'dir/file.txt'])
3157
src_wt = self.setup_simple_branch('src', ['README'])
3158
conflicts = self.do_merge_into('src', 'dest/dir')
3159
self.assertEqual(1, conflicts)
3161
self.addCleanup(dest_wt.unlock)
3162
# The r1-lib1 revision should be merged into this one
3163
self.assertEqual(['r1-dest', 'r1-src'], dest_wt.get_parent_ids())
3164
self.assertTreeEntriesEqual(
3165
[('', 'dest-root-id'),
3166
('dir', 'src-root-id'),
3167
('dir.moved', 'dest-dir-id'),
3168
('dir/README', 'src-README-id'),
3169
('dir.moved/file.txt', 'dest-file.txt-id'),
3172
def test_file_id_conflict(self):
3173
"""A conflict is generated if the merge-into adds a file (or other
3174
inventory entry) with a file-id that already exists in the target tree.
3176
dest_wt = self.setup_simple_branch('dest', ['file.txt'])
3177
# Make a second tree with a file-id that will clash with file.txt in
3179
src_wt = self.make_branch_and_tree('src')
3180
self.build_tree(['src/README'])
3181
src_wt.add(['README'], ids=['dest-file.txt-id'])
3182
src_wt.commit("Rev 1 of src.", rev_id='r1-src')
3183
conflicts = self.do_merge_into('src', 'dest/dir')
3184
# This is an edge case that shouldn't happen to users very often. So
3185
# we don't care really about the exact presentation of the conflict,
3186
# just that there is one.
3187
self.assertEqual(1, conflicts)
3189
def test_only_subdir(self):
3190
"""When the location points to just part of a tree, merge just that
3193
dest_wt = self.setup_simple_branch('dest')
3194
src_wt = self.setup_simple_branch(
3195
'src', ['hello.txt', 'dir/', 'dir/foo.c'])
3196
conflicts = self.do_merge_into('src/dir', 'dest/dir')
3198
self.addCleanup(dest_wt.unlock)
3199
# The r1-lib1 revision should NOT be merged into this one (this is a
3201
self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3202
self.assertTreeEntriesEqual(
3203
[('', 'dest-root-id'),
3204
('dir', 'src-dir-id'),
3205
('dir/foo.c', 'src-foo.c-id'),
3208
def test_only_file(self):
3209
"""An edge case: merge just one file, not a whole dir."""
3210
dest_wt = self.setup_simple_branch('dest')
3211
two_file_wt = self.setup_simple_branch(
3212
'two-file', ['file1.txt', 'file2.txt'])
3213
conflicts = self.do_merge_into('two-file/file1.txt', 'dest/file1.txt')
3215
self.addCleanup(dest_wt.unlock)
3216
# The r1-lib1 revision should NOT be merged into this one
3217
self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3218
self.assertTreeEntriesEqual(
3219
[('', 'dest-root-id'), ('file1.txt', 'two-file-file1.txt-id')],
3222
def test_no_such_source_path(self):
3223
"""PathNotInTree is raised if the specified path in the source tree
3226
dest_wt = self.setup_simple_branch('dest')
3227
two_file_wt = self.setup_simple_branch('src', ['dir/'])
3228
self.assertRaises(_mod_merge.PathNotInTree, self.do_merge_into,
3229
'src/no-such-dir', 'dest/foo')
3231
self.addCleanup(dest_wt.unlock)
3232
# The dest tree is unmodified.
3233
self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3234
self.assertTreeEntriesEqual([('', 'dest-root-id')], dest_wt)
3236
def test_no_such_target_path(self):
3237
"""PathNotInTree is also raised if the specified path in the target
3238
tree does not exist.
3240
dest_wt = self.setup_simple_branch('dest')
3241
two_file_wt = self.setup_simple_branch('src', ['file.txt'])
3242
self.assertRaises(_mod_merge.PathNotInTree, self.do_merge_into,
3243
'src', 'dest/no-such-dir/foo')
3245
self.addCleanup(dest_wt.unlock)
3246
# The dest tree is unmodified.
3247
self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3248
self.assertTreeEntriesEqual([('', 'dest-root-id')], dest_wt)
3251
class TestMergeHooks(TestCaseWithTransport):
3254
super(TestMergeHooks, self).setUp()
3255
self.tree_a = self.make_branch_and_tree('tree_a')
3256
self.build_tree_contents([('tree_a/file', 'content_1')])
3257
self.tree_a.add('file', 'file-id')
3258
self.tree_a.commit('added file')
3260
self.tree_b = self.tree_a.bzrdir.sprout('tree_b').open_workingtree()
3261
self.build_tree_contents([('tree_b/file', 'content_2')])
3262
self.tree_b.commit('modify file')
3264
def test_pre_merge_hook_inject_different_tree(self):
3265
tree_c = self.tree_b.bzrdir.sprout('tree_c').open_workingtree()
3266
self.build_tree_contents([('tree_c/file', 'content_3')])
3267
tree_c.commit("more content")
3269
def factory(merger):
3270
self.assertIsInstance(merger, _mod_merge.Merge3Merger)
3271
merger.other_tree = tree_c
3272
calls.append(merger)
3273
_mod_merge.Merger.hooks.install_named_hook('pre_merge',
3274
factory, 'test factory')
3275
self.tree_a.merge_from_branch(self.tree_b.branch)
3277
self.assertFileEqual("content_3", 'tree_a/file')
3278
self.assertLength(1, calls)
3280
def test_post_merge_hook_called(self):
3282
def factory(merger):
3283
self.assertIsInstance(merger, _mod_merge.Merge3Merger)
3284
calls.append(merger)
3285
_mod_merge.Merger.hooks.install_named_hook('post_merge',
3286
factory, 'test factory')
3288
self.tree_a.merge_from_branch(self.tree_b.branch)
3290
self.assertFileEqual("content_2", 'tree_a/file')
3291
self.assertLength(1, calls)