1
# Copyright (C) 2005-2012, 2016 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
branch as _mod_branch,
27
revision as _mod_revision,
36
from ..conflicts import ConflictList, TextConflict
37
from ..errors import UnrelatedBranches, NoCommits
38
from ..merge import transform_tree, merge_inner, _PlanMerge
39
from ..osutils import basename, pathjoin, file_kind
42
TestCaseWithMemoryTransport,
43
TestCaseWithTransport,
46
from ..workingtree import WorkingTree
49
class TestMerge(TestCaseWithTransport):
50
"""Test appending more than one revision"""
52
def test_pending(self):
53
wt = self.make_branch_and_tree('.')
54
rev_a = wt.commit("lala!")
55
self.assertEqual([rev_a], wt.get_parent_ids())
56
self.assertRaises(errors.PointlessMerge, wt.merge_from_branch,
58
self.assertEqual([rev_a], wt.get_parent_ids())
62
wt = self.make_branch_and_tree('.')
66
wt.merge_from_branch(wt.branch, wt.branch.get_rev_id(2),
67
wt.branch.get_rev_id(1))
69
def test_nocommits(self):
70
wt = self.test_pending()
71
wt2 = self.make_branch_and_tree('branch2')
72
self.assertRaises(NoCommits, wt.merge_from_branch, wt2.branch)
75
def test_unrelated(self):
76
wt, wt2 = self.test_nocommits()
78
self.assertRaises(UnrelatedBranches, wt.merge_from_branch, wt2.branch)
81
def test_merge_one_file(self):
82
"""Do a partial merge of a tree which should not affect tree parents."""
83
wt1 = self.make_branch_and_tree('branch1')
84
tip = wt1.commit('empty commit')
85
wt2 = self.make_branch_and_tree('branch2')
87
with file('branch1/foo', 'wb') as f:
89
with file('branch1/bar', 'wb') as f:
93
wt1.commit('add foobar')
94
self.run_bzr('merge ../branch1/baz', retcode=3, working_dir='branch2')
95
self.run_bzr('merge ../branch1/foo', working_dir='branch2')
96
self.assertPathExists('branch2/foo')
97
self.assertPathDoesNotExist('branch2/bar')
98
wt2 = WorkingTree.open('branch2')
99
self.assertEqual([tip], wt2.get_parent_ids())
101
def test_pending_with_null(self):
102
"""When base is forced to revno 0, parent_ids are set"""
103
wt2 = self.test_unrelated()
104
wt1 = WorkingTree.open('.')
106
br1.fetch(wt2.branch)
107
# merge all of branch 2 into branch 1 even though they
109
wt1.merge_from_branch(wt2.branch, wt2.last_revision(), 'null:')
110
self.assertEqual([br1.last_revision(), wt2.branch.last_revision()],
111
wt1.get_parent_ids())
112
return (wt1, wt2.branch)
114
def test_two_roots(self):
115
"""Merge base is sane when two unrelated branches are merged"""
116
wt1, br2 = self.test_pending_with_null()
120
last = wt1.branch.last_revision()
121
last2 = br2.last_revision()
122
graph = wt1.branch.repository.get_graph()
123
self.assertEqual(last2, graph.find_unique_lca(last, last2))
127
def test_merge_into_null_tree(self):
128
wt = self.make_branch_and_tree('tree')
129
null_tree = wt.basis_tree()
130
self.build_tree(['tree/file'])
132
wt.commit('tree with root')
133
merger = _mod_merge.Merge3Merger(null_tree, null_tree, null_tree, wt,
134
this_branch=wt.branch,
136
with merger.make_preview_transform() as tt:
137
self.assertEqual([], tt.find_conflicts())
138
preview = tt.get_preview_tree()
139
self.assertEqual(wt.get_root_id(), preview.get_root_id())
141
def test_merge_unrelated_retains_root(self):
142
wt = self.make_branch_and_tree('tree')
143
other_tree = self.make_branch_and_tree('other')
144
self.addCleanup(other_tree.lock_read().unlock)
145
merger = _mod_merge.Merge3Merger(wt, wt, wt.basis_tree(), other_tree,
146
this_branch=wt.branch,
148
with transform.TransformPreview(wt) as merger.tt:
149
merger._compute_transform()
150
new_root_id = merger.tt.final_file_id(merger.tt.root)
151
self.assertEqual(wt.get_root_id(), new_root_id)
153
def test_create_rename(self):
154
"""Rename an inventory entry while creating the file"""
155
tree =self.make_branch_and_tree('.')
156
with file('name1', 'wb') as f: f.write('Hello')
158
tree.commit(message="hello")
159
tree.rename_one('name1', 'name2')
161
transform_tree(tree, tree.branch.basis_tree())
163
def test_layered_rename(self):
164
"""Rename both child and parent at same time"""
165
tree =self.make_branch_and_tree('.')
168
filename = pathjoin('dirname1', 'name1')
169
with file(filename, 'wb') as f: f.write('Hello')
171
tree.commit(message="hello")
172
filename2 = pathjoin('dirname1', 'name2')
173
tree.rename_one(filename, filename2)
174
tree.rename_one('dirname1', 'dirname2')
175
transform_tree(tree, tree.branch.basis_tree())
177
def test_ignore_zero_merge_inner(self):
178
# Test that merge_inner's ignore zero parameter is effective
179
tree_a =self.make_branch_and_tree('a')
180
tree_a.commit(message="hello")
181
dir_b = tree_a.controldir.sprout('b')
182
tree_b = dir_b.open_workingtree()
184
self.addCleanup(tree_b.unlock)
185
tree_a.commit(message="hello again")
186
merge_inner(tree_b.branch, tree_a, tree_b.basis_tree(),
187
this_tree=tree_b, ignore_zero=True)
188
self.assertTrue('All changes applied successfully.\n' not in
191
merge_inner(tree_b.branch, tree_a, tree_b.basis_tree(),
192
this_tree=tree_b, ignore_zero=False)
193
self.assertTrue('All changes applied successfully.\n' in self.get_log())
195
def test_merge_inner_conflicts(self):
196
tree_a = self.make_branch_and_tree('a')
197
tree_a.set_conflicts(ConflictList([TextConflict('patha')]))
198
merge_inner(tree_a.branch, tree_a, tree_a, this_tree=tree_a)
199
self.assertEqual(1, len(tree_a.conflicts()))
201
def test_rmdir_conflict(self):
202
tree_a = self.make_branch_and_tree('a')
203
self.build_tree(['a/b/'])
204
tree_a.add('b', 'b-id')
205
tree_a.commit('added b')
206
# basis_tree() is only guaranteed to be valid as long as it is actually
207
# the basis tree. This mutates the tree after grabbing basis, so go to
209
base_tree = tree_a.branch.repository.revision_tree(tree_a.last_revision())
210
tree_z = tree_a.controldir.sprout('z').open_workingtree()
211
self.build_tree(['a/b/c'])
213
tree_a.commit('added c')
215
tree_z.commit('removed b')
216
merge_inner(tree_z.branch, tree_a, base_tree, this_tree=tree_z)
218
conflicts.MissingParent('Created directory', 'b', 'b-id'),
219
conflicts.UnversionedParent('Versioned directory', 'b', 'b-id')],
221
merge_inner(tree_a.branch, tree_z.basis_tree(), base_tree,
224
conflicts.DeletingParent('Not deleting', 'b', 'b-id'),
225
conflicts.UnversionedParent('Versioned directory', 'b', 'b-id')],
228
def test_nested_merge(self):
230
'iter_changes doesn\'t work with changes in nested trees')
231
tree = self.make_branch_and_tree('tree',
232
format='development-subtree')
233
sub_tree = self.make_branch_and_tree('tree/sub-tree',
234
format='development-subtree')
235
sub_tree.set_root_id('sub-tree-root')
236
self.build_tree_contents([('tree/sub-tree/file', 'text1')])
238
sub_tree.commit('foo')
239
tree.add_reference(sub_tree)
240
tree.commit('set text to 1')
241
tree2 = tree.controldir.sprout('tree2').open_workingtree()
242
# modify the file in the subtree
243
self.build_tree_contents([('tree2/sub-tree/file', 'text2')])
244
# and merge the changes from the diverged subtree into the containing
246
tree2.commit('changed file text')
247
tree.merge_from_branch(tree2.branch)
248
self.assertFileEqual('text2', 'tree/sub-tree/file')
250
def test_merge_with_missing(self):
251
tree_a = self.make_branch_and_tree('tree_a')
252
self.build_tree_contents([('tree_a/file', 'content_1')])
254
tree_a.commit('commit base')
255
# basis_tree() is only guaranteed to be valid as long as it is actually
256
# the basis tree. This test commits to the tree after grabbing basis,
257
# so we go to the repository.
258
base_tree = tree_a.branch.repository.revision_tree(tree_a.last_revision())
259
tree_b = tree_a.controldir.sprout('tree_b').open_workingtree()
260
self.build_tree_contents([('tree_a/file', 'content_2')])
261
tree_a.commit('commit other')
262
other_tree = tree_a.basis_tree()
263
# 'file' is now missing but isn't altered in any commit in b so no
264
# change should be applied.
265
os.unlink('tree_b/file')
266
merge_inner(tree_b.branch, other_tree, base_tree, this_tree=tree_b)
268
def test_merge_kind_change(self):
269
tree_a = self.make_branch_and_tree('tree_a')
270
self.build_tree_contents([('tree_a/file', 'content_1')])
271
tree_a.add('file', 'file-id')
272
tree_a.commit('added file')
273
tree_b = tree_a.controldir.sprout('tree_b').open_workingtree()
274
os.unlink('tree_a/file')
275
self.build_tree(['tree_a/file/'])
276
tree_a.commit('changed file to directory')
277
tree_b.merge_from_branch(tree_a.branch)
278
self.assertEqual('directory', file_kind('tree_b/file'))
280
self.assertEqual('file', file_kind('tree_b/file'))
281
self.build_tree_contents([('tree_b/file', 'content_2')])
282
tree_b.commit('content change')
283
tree_b.merge_from_branch(tree_a.branch)
284
self.assertEqual(tree_b.conflicts(),
285
[conflicts.ContentsConflict('file',
288
def test_merge_type_registry(self):
289
merge_type_option = option.Option.OPTIONS['merge-type']
290
self.assertFalse('merge4' in [x[0] for x in
291
merge_type_option.iter_switches()])
292
registry = _mod_merge.get_merge_type_registry()
293
registry.register_lazy('merge4', 'breezy.merge', 'Merge4Merger',
294
'time-travelling merge')
295
self.assertTrue('merge4' in [x[0] for x in
296
merge_type_option.iter_switches()])
297
registry.remove('merge4')
298
self.assertFalse('merge4' in [x[0] for x in
299
merge_type_option.iter_switches()])
301
def test_merge_other_moves_we_deleted(self):
302
tree_a = self.make_branch_and_tree('A')
304
self.addCleanup(tree_a.unlock)
305
self.build_tree(['A/a'])
307
tree_a.commit('1', rev_id='rev-1')
309
tree_a.rename_one('a', 'b')
311
bzrdir_b = tree_a.controldir.sprout('B', revision_id='rev-1')
312
tree_b = bzrdir_b.open_workingtree()
314
self.addCleanup(tree_b.unlock)
318
tree_b.merge_from_branch(tree_a.branch)
319
except AttributeError:
320
self.fail('tried to join a path when name was None')
322
def test_merge_uncommitted_otherbasis_ancestor_of_thisbasis(self):
323
tree_a = self.make_branch_and_tree('a')
324
self.build_tree(['a/file_1', 'a/file_2'])
325
tree_a.add(['file_1'])
326
tree_a.commit('commit 1')
327
tree_a.add(['file_2'])
328
tree_a.commit('commit 2')
329
tree_b = tree_a.controldir.sprout('b').open_workingtree()
330
tree_b.rename_one('file_1', 'renamed')
331
merger = _mod_merge.Merger.from_uncommitted(tree_a, tree_b)
332
merger.merge_type = _mod_merge.Merge3Merger
334
self.assertEqual(tree_a.get_parent_ids(), [tree_b.last_revision()])
336
def test_merge_uncommitted_otherbasis_ancestor_of_thisbasis_weave(self):
337
tree_a = self.make_branch_and_tree('a')
338
self.build_tree(['a/file_1', 'a/file_2'])
339
tree_a.add(['file_1'])
340
tree_a.commit('commit 1')
341
tree_a.add(['file_2'])
342
tree_a.commit('commit 2')
343
tree_b = tree_a.controldir.sprout('b').open_workingtree()
344
tree_b.rename_one('file_1', 'renamed')
345
merger = _mod_merge.Merger.from_uncommitted(tree_a, tree_b)
346
merger.merge_type = _mod_merge.WeaveMerger
348
self.assertEqual(tree_a.get_parent_ids(), [tree_b.last_revision()])
350
def prepare_cherrypick(self):
351
"""Prepare a pair of trees for cherrypicking tests.
353
Both trees have a file, 'file'.
354
rev1 sets content to 'a'.
357
A full merge of rev2b and rev3b into this_tree would add both 'b' and
358
'c'. A successful cherrypick of rev2b-rev3b into this_tree will add
361
this_tree = self.make_branch_and_tree('this')
362
self.build_tree_contents([('this/file', "a\n")])
363
this_tree.add('file')
364
this_tree.commit('rev1')
365
other_tree = this_tree.controldir.sprout('other').open_workingtree()
366
self.build_tree_contents([('other/file', "a\nb\n")])
367
other_tree.commit('rev2b', rev_id='rev2b')
368
self.build_tree_contents([('other/file', "c\na\nb\n")])
369
other_tree.commit('rev3b', rev_id='rev3b')
370
this_tree.lock_write()
371
self.addCleanup(this_tree.unlock)
372
return this_tree, other_tree
374
def test_weave_cherrypick(self):
375
this_tree, other_tree = self.prepare_cherrypick()
376
merger = _mod_merge.Merger.from_revision_ids(
377
this_tree, 'rev3b', 'rev2b', other_tree.branch)
378
merger.merge_type = _mod_merge.WeaveMerger
380
self.assertFileEqual('c\na\n', 'this/file')
382
def test_weave_cannot_reverse_cherrypick(self):
383
this_tree, other_tree = self.prepare_cherrypick()
384
merger = _mod_merge.Merger.from_revision_ids(
385
this_tree, 'rev2b', 'rev3b', other_tree.branch)
386
merger.merge_type = _mod_merge.WeaveMerger
387
self.assertRaises(errors.CannotReverseCherrypick, merger.do_merge)
389
def test_merge3_can_reverse_cherrypick(self):
390
this_tree, other_tree = self.prepare_cherrypick()
391
merger = _mod_merge.Merger.from_revision_ids(
392
this_tree, 'rev2b', 'rev3b', other_tree.branch)
393
merger.merge_type = _mod_merge.Merge3Merger
396
def test_merge3_will_detect_cherrypick(self):
397
this_tree = self.make_branch_and_tree('this')
398
self.build_tree_contents([('this/file', "a\n")])
399
this_tree.add('file')
400
this_tree.commit('rev1')
401
other_tree = this_tree.controldir.sprout('other').open_workingtree()
402
self.build_tree_contents([('other/file', "a\nb\n")])
403
other_tree.commit('rev2b', rev_id='rev2b')
404
self.build_tree_contents([('other/file', "a\nb\nc\n")])
405
other_tree.commit('rev3b', rev_id='rev3b')
406
this_tree.lock_write()
407
self.addCleanup(this_tree.unlock)
409
merger = _mod_merge.Merger.from_revision_ids(
410
this_tree, 'rev3b', 'rev2b', other_tree.branch)
411
merger.merge_type = _mod_merge.Merge3Merger
413
self.assertFileEqual('a\n'
417
'>>>>>>> MERGE-SOURCE\n',
420
def test_merge_reverse_revision_range(self):
421
tree = self.make_branch_and_tree(".")
423
self.addCleanup(tree.unlock)
424
self.build_tree(['a'])
426
first_rev = tree.commit("added a")
427
merger = _mod_merge.Merger.from_revision_ids(tree,
428
_mod_revision.NULL_REVISION,
430
merger.merge_type = _mod_merge.Merge3Merger
431
merger.interesting_files = 'a'
432
conflict_count = merger.do_merge()
433
self.assertEqual(0, conflict_count)
435
self.assertPathDoesNotExist("a")
437
self.assertPathExists("a")
439
def test_make_merger(self):
440
this_tree = self.make_branch_and_tree('this')
441
this_tree.commit('rev1', rev_id='rev1')
442
other_tree = this_tree.controldir.sprout('other').open_workingtree()
443
this_tree.commit('rev2', rev_id='rev2a')
444
other_tree.commit('rev2', rev_id='rev2b')
445
this_tree.lock_write()
446
self.addCleanup(this_tree.unlock)
447
merger = _mod_merge.Merger.from_revision_ids(
448
this_tree, 'rev2b', other_branch=other_tree.branch)
449
merger.merge_type = _mod_merge.Merge3Merger
450
tree_merger = merger.make_merger()
451
self.assertIs(_mod_merge.Merge3Merger, tree_merger.__class__)
452
self.assertEqual('rev2b',
453
tree_merger.other_tree.get_revision_id())
454
self.assertEqual('rev1',
455
tree_merger.base_tree.get_revision_id())
456
self.assertEqual(other_tree.branch, tree_merger.other_branch)
458
def test_make_preview_transform(self):
459
this_tree = self.make_branch_and_tree('this')
460
self.build_tree_contents([('this/file', '1\n')])
461
this_tree.add('file', 'file-id')
462
this_tree.commit('rev1', rev_id='rev1')
463
other_tree = this_tree.controldir.sprout('other').open_workingtree()
464
self.build_tree_contents([('this/file', '1\n2a\n')])
465
this_tree.commit('rev2', rev_id='rev2a')
466
self.build_tree_contents([('other/file', '2b\n1\n')])
467
other_tree.commit('rev2', rev_id='rev2b')
468
this_tree.lock_write()
469
self.addCleanup(this_tree.unlock)
470
merger = _mod_merge.Merger.from_revision_ids(
471
this_tree, 'rev2b', other_branch=other_tree.branch)
472
merger.merge_type = _mod_merge.Merge3Merger
473
tree_merger = merger.make_merger()
474
tt = tree_merger.make_preview_transform()
475
self.addCleanup(tt.finalize)
476
preview_tree = tt.get_preview_tree()
477
tree_file = this_tree.get_file('file-id')
479
self.assertEqual('1\n2a\n', tree_file.read())
482
preview_file = preview_tree.get_file('file-id')
484
self.assertEqual('2b\n1\n2a\n', preview_file.read())
488
def test_do_merge(self):
489
this_tree = self.make_branch_and_tree('this')
490
self.build_tree_contents([('this/file', '1\n')])
491
this_tree.add('file', 'file-id')
492
this_tree.commit('rev1', rev_id='rev1')
493
other_tree = this_tree.controldir.sprout('other').open_workingtree()
494
self.build_tree_contents([('this/file', '1\n2a\n')])
495
this_tree.commit('rev2', rev_id='rev2a')
496
self.build_tree_contents([('other/file', '2b\n1\n')])
497
other_tree.commit('rev2', rev_id='rev2b')
498
this_tree.lock_write()
499
self.addCleanup(this_tree.unlock)
500
merger = _mod_merge.Merger.from_revision_ids(
501
this_tree, 'rev2b', other_branch=other_tree.branch)
502
merger.merge_type = _mod_merge.Merge3Merger
503
tree_merger = merger.make_merger()
504
tt = tree_merger.do_merge()
505
tree_file = this_tree.get_file('file-id')
507
self.assertEqual('2b\n1\n2a\n', tree_file.read())
511
def test_merge_require_tree_root(self):
512
tree = self.make_branch_and_tree(".")
514
self.addCleanup(tree.unlock)
515
self.build_tree(['a'])
517
first_rev = tree.commit("added a")
518
old_root_id = tree.get_root_id()
519
merger = _mod_merge.Merger.from_revision_ids(tree,
520
_mod_revision.NULL_REVISION,
522
merger.merge_type = _mod_merge.Merge3Merger
523
conflict_count = merger.do_merge()
524
self.assertEqual(0, conflict_count)
525
self.assertEqual({old_root_id}, tree.all_file_ids())
526
tree.set_parent_ids([])
528
def test_merge_add_into_deleted_root(self):
529
# Yes, people actually do this. And report bugs if it breaks.
530
source = self.make_branch_and_tree('source', format='rich-root-pack')
531
self.build_tree(['source/foo/'])
532
source.add('foo', 'foo-id')
533
source.commit('Add foo')
534
target = source.controldir.sprout('target').open_workingtree()
535
subtree = target.extract('foo-id')
536
subtree.commit('Delete root')
537
self.build_tree(['source/bar'])
538
source.add('bar', 'bar-id')
539
source.commit('Add bar')
540
subtree.merge_from_branch(source.branch)
542
def test_merge_joined_branch(self):
543
source = self.make_branch_and_tree('source', format='rich-root-pack')
544
self.build_tree(['source/foo'])
546
source.commit('Add foo')
547
target = self.make_branch_and_tree('target', format='rich-root-pack')
548
self.build_tree(['target/bla'])
550
target.commit('Add bla')
551
nested = source.controldir.sprout('target/subtree').open_workingtree()
552
target.subsume(nested)
553
target.commit('Join nested')
554
self.build_tree(['source/bar'])
556
source.commit('Add bar')
557
target.merge_from_branch(source.branch)
558
target.commit('Merge source')
561
class TestPlanMerge(TestCaseWithMemoryTransport):
564
super(TestPlanMerge, self).setUp()
565
mapper = versionedfile.PrefixMapper()
566
factory = knit.make_file_factory(True, mapper)
567
self.vf = factory(self.get_transport())
568
self.plan_merge_vf = versionedfile._PlanMergeVersionedFile('root')
569
self.plan_merge_vf.fallback_versionedfiles.append(self.vf)
571
def add_version(self, key, parents, text):
572
self.vf.add_lines(key, parents, [c+'\n' for c in text])
574
def add_rev(self, prefix, revision_id, parents, text):
575
self.add_version((prefix, revision_id), [(prefix, p) for p in parents],
578
def add_uncommitted_version(self, key, parents, text):
579
self.plan_merge_vf.add_lines(key, parents,
580
[c+'\n' for c in text])
582
def setup_plan_merge(self):
583
self.add_rev('root', 'A', [], 'abc')
584
self.add_rev('root', 'B', ['A'], 'acehg')
585
self.add_rev('root', 'C', ['A'], 'fabg')
586
return _PlanMerge('B', 'C', self.plan_merge_vf, ('root',))
588
def setup_plan_merge_uncommitted(self):
589
self.add_version(('root', 'A'), [], 'abc')
590
self.add_uncommitted_version(('root', 'B:'), [('root', 'A')], 'acehg')
591
self.add_uncommitted_version(('root', 'C:'), [('root', 'A')], 'fabg')
592
return _PlanMerge('B:', 'C:', self.plan_merge_vf, ('root',))
594
def test_base_from_plan(self):
595
self.setup_plan_merge()
596
plan = self.plan_merge_vf.plan_merge('B', 'C')
597
pwm = versionedfile.PlanWeaveMerge(plan)
598
self.assertEqual(['a\n', 'b\n', 'c\n'], pwm.base_from_plan())
600
def test_unique_lines(self):
601
plan = self.setup_plan_merge()
602
self.assertEqual(plan._unique_lines(
603
plan._get_matching_blocks('B', 'C')),
606
def test_plan_merge(self):
607
self.setup_plan_merge()
608
plan = self.plan_merge_vf.plan_merge('B', 'C')
611
('unchanged', 'a\n'),
620
def test_plan_merge_cherrypick(self):
621
self.add_rev('root', 'A', [], 'abc')
622
self.add_rev('root', 'B', ['A'], 'abcde')
623
self.add_rev('root', 'C', ['A'], 'abcefg')
624
self.add_rev('root', 'D', ['A', 'B', 'C'], 'abcdegh')
625
my_plan = _PlanMerge('B', 'D', self.plan_merge_vf, ('root',))
626
# We shortcut when one text supersedes the other in the per-file graph.
627
# We don't actually need to compare the texts at this point.
636
list(my_plan.plan_merge()))
638
def test_plan_merge_no_common_ancestor(self):
639
self.add_rev('root', 'A', [], 'abc')
640
self.add_rev('root', 'B', [], 'xyz')
641
my_plan = _PlanMerge('A', 'B', self.plan_merge_vf, ('root',))
649
list(my_plan.plan_merge()))
651
def test_plan_merge_tail_ancestors(self):
652
# The graph looks like this:
653
# A # Common to all ancestors
655
# B C # Ancestors of E, only common to one side
657
# D E F # D, F are unique to G, H respectively
658
# |/ \| # E is the LCA for G & H, and the unique LCA for
663
# I J # criss-cross merge of G, H
665
# In this situation, a simple pruning of ancestors of E will leave D &
666
# F "dangling", which looks like they introduce lines different from
667
# the ones in E, but in actuality C&B introduced the lines, and they
668
# are already present in E
670
# Introduce the base text
671
self.add_rev('root', 'A', [], 'abc')
672
# Introduces a new line B
673
self.add_rev('root', 'B', ['A'], 'aBbc')
674
# Introduces a new line C
675
self.add_rev('root', 'C', ['A'], 'abCc')
676
# Introduce new line D
677
self.add_rev('root', 'D', ['B'], 'DaBbc')
678
# Merges B and C by just incorporating both
679
self.add_rev('root', 'E', ['B', 'C'], 'aBbCc')
680
# Introduce new line F
681
self.add_rev('root', 'F', ['C'], 'abCcF')
682
# Merge D & E by just combining the texts
683
self.add_rev('root', 'G', ['D', 'E'], 'DaBbCc')
684
# Merge F & E by just combining the texts
685
self.add_rev('root', 'H', ['F', 'E'], 'aBbCcF')
686
# Merge G & H by just combining texts
687
self.add_rev('root', 'I', ['G', 'H'], 'DaBbCcF')
688
# Merge G & H but supersede an old line in B
689
self.add_rev('root', 'J', ['H', 'G'], 'DaJbCcF')
690
plan = self.plan_merge_vf.plan_merge('I', 'J')
692
('unchanged', 'D\n'),
693
('unchanged', 'a\n'),
696
('unchanged', 'b\n'),
697
('unchanged', 'C\n'),
698
('unchanged', 'c\n'),
699
('unchanged', 'F\n')],
702
def test_plan_merge_tail_triple_ancestors(self):
703
# The graph looks like this:
704
# A # Common to all ancestors
706
# B C # Ancestors of E, only common to one side
708
# D E F # D, F are unique to G, H respectively
709
# |/|\| # E is the LCA for G & H, and the unique LCA for
711
# |\ /| # Q is just an extra node which is merged into both
714
# I J # criss-cross merge of G, H
716
# This is the same as the test_plan_merge_tail_ancestors, except we add
717
# a third LCA that doesn't add new lines, but will trigger our more
718
# involved ancestry logic
720
self.add_rev('root', 'A', [], 'abc')
721
self.add_rev('root', 'B', ['A'], 'aBbc')
722
self.add_rev('root', 'C', ['A'], 'abCc')
723
self.add_rev('root', 'D', ['B'], 'DaBbc')
724
self.add_rev('root', 'E', ['B', 'C'], 'aBbCc')
725
self.add_rev('root', 'F', ['C'], 'abCcF')
726
self.add_rev('root', 'G', ['D', 'E'], 'DaBbCc')
727
self.add_rev('root', 'H', ['F', 'E'], 'aBbCcF')
728
self.add_rev('root', 'Q', ['E'], 'aBbCc')
729
self.add_rev('root', 'I', ['G', 'Q', 'H'], 'DaBbCcF')
730
# Merge G & H but supersede an old line in B
731
self.add_rev('root', 'J', ['H', 'Q', 'G'], 'DaJbCcF')
732
plan = self.plan_merge_vf.plan_merge('I', 'J')
734
('unchanged', 'D\n'),
735
('unchanged', 'a\n'),
738
('unchanged', 'b\n'),
739
('unchanged', 'C\n'),
740
('unchanged', 'c\n'),
741
('unchanged', 'F\n')],
744
def test_plan_merge_2_tail_triple_ancestors(self):
745
# The graph looks like this:
746
# A B # 2 tails going back to NULL
748
# D E F # D, is unique to G, F to H
749
# |/|\| # E is the LCA for G & H, and the unique LCA for
751
# |\ /| # Q is just an extra node which is merged into both
754
# I J # criss-cross merge of G, H (and Q)
757
# This is meant to test after hitting a 3-way LCA, and multiple tail
758
# ancestors (only have NULL_REVISION in common)
760
self.add_rev('root', 'A', [], 'abc')
761
self.add_rev('root', 'B', [], 'def')
762
self.add_rev('root', 'D', ['A'], 'Dabc')
763
self.add_rev('root', 'E', ['A', 'B'], 'abcdef')
764
self.add_rev('root', 'F', ['B'], 'defF')
765
self.add_rev('root', 'G', ['D', 'E'], 'Dabcdef')
766
self.add_rev('root', 'H', ['F', 'E'], 'abcdefF')
767
self.add_rev('root', 'Q', ['E'], 'abcdef')
768
self.add_rev('root', 'I', ['G', 'Q', 'H'], 'DabcdefF')
769
# Merge G & H but supersede an old line in B
770
self.add_rev('root', 'J', ['H', 'Q', 'G'], 'DabcdJfF')
771
plan = self.plan_merge_vf.plan_merge('I', 'J')
773
('unchanged', 'D\n'),
774
('unchanged', 'a\n'),
775
('unchanged', 'b\n'),
776
('unchanged', 'c\n'),
777
('unchanged', 'd\n'),
780
('unchanged', 'f\n'),
781
('unchanged', 'F\n')],
784
def test_plan_merge_uncommitted_files(self):
785
self.setup_plan_merge_uncommitted()
786
plan = self.plan_merge_vf.plan_merge('B:', 'C:')
789
('unchanged', 'a\n'),
798
def test_plan_merge_insert_order(self):
799
"""Weave merges are sensitive to the order of insertion.
801
Specifically for overlapping regions, it effects which region gets put
802
'first'. And when a user resolves an overlapping merge, if they use the
803
same ordering, then the lines match the parents, if they don't only
804
*some* of the lines match.
806
self.add_rev('root', 'A', [], 'abcdef')
807
self.add_rev('root', 'B', ['A'], 'abwxcdef')
808
self.add_rev('root', 'C', ['A'], 'abyzcdef')
809
# Merge, and resolve the conflict by adding *both* sets of lines
810
# If we get the ordering wrong, these will look like new lines in D,
811
# rather than carried over from B, C
812
self.add_rev('root', 'D', ['B', 'C'],
814
# Supersede the lines in B and delete the lines in C, which will
815
# conflict if they are treated as being in D
816
self.add_rev('root', 'E', ['C', 'B'],
818
# Same thing for the lines in C
819
self.add_rev('root', 'F', ['C'], 'abpqcdef')
820
plan = self.plan_merge_vf.plan_merge('D', 'E')
822
('unchanged', 'a\n'),
823
('unchanged', 'b\n'),
830
('unchanged', 'c\n'),
831
('unchanged', 'd\n'),
832
('unchanged', 'e\n'),
833
('unchanged', 'f\n')],
835
plan = self.plan_merge_vf.plan_merge('E', 'D')
836
# Going in the opposite direction shows the effect of the opposite plan
838
('unchanged', 'a\n'),
839
('unchanged', 'b\n'),
844
('killed-both', 'w\n'),
845
('killed-both', 'x\n'),
848
('unchanged', 'c\n'),
849
('unchanged', 'd\n'),
850
('unchanged', 'e\n'),
851
('unchanged', 'f\n')],
854
def test_plan_merge_criss_cross(self):
855
# This is specificly trying to trigger problems when using limited
856
# ancestry and weaves. The ancestry graph looks like:
857
# XX unused ancestor, should not show up in the weave
861
# B \ Introduces a line 'foo'
863
# C D E C & D both have 'foo', E has different changes
867
# F G All of C, D, E are merged into F and G, so they are
868
# all common ancestors.
870
# The specific issue with weaves:
871
# B introduced a text ('foo') that is present in both C and D.
872
# If we do not include B (because it isn't an ancestor of E), then
873
# the A=>C and A=>D look like both sides independently introduce the
874
# text ('foo'). If F does not modify the text, it would still appear
875
# to have deleted on of the versions from C or D. If G then modifies
876
# 'foo', it should appear as superseding the value in F (since it
877
# came from B), rather than conflict because of the resolution during
879
self.add_rev('root', 'XX', [], 'qrs')
880
self.add_rev('root', 'A', ['XX'], 'abcdef')
881
self.add_rev('root', 'B', ['A'], 'axcdef')
882
self.add_rev('root', 'C', ['B'], 'axcdefg')
883
self.add_rev('root', 'D', ['B'], 'haxcdef')
884
self.add_rev('root', 'E', ['A'], 'abcdyf')
885
# Simple combining of all texts
886
self.add_rev('root', 'F', ['C', 'D', 'E'], 'haxcdyfg')
887
# combine and supersede 'x'
888
self.add_rev('root', 'G', ['C', 'D', 'E'], 'hazcdyfg')
889
plan = self.plan_merge_vf.plan_merge('F', 'G')
891
('unchanged', 'h\n'),
892
('unchanged', 'a\n'),
893
('killed-base', 'b\n'),
896
('unchanged', 'c\n'),
897
('unchanged', 'd\n'),
898
('killed-base', 'e\n'),
899
('unchanged', 'y\n'),
900
('unchanged', 'f\n'),
901
('unchanged', 'g\n')],
903
plan = self.plan_merge_vf.plan_lca_merge('F', 'G')
904
# This is one of the main differences between plan_merge and
905
# plan_lca_merge. plan_lca_merge generates a conflict for 'x => z',
906
# because 'x' was not present in one of the bases. However, in this
907
# case it is spurious because 'x' does not exist in the global base A.
909
('unchanged', 'h\n'),
910
('unchanged', 'a\n'),
911
('conflicted-a', 'x\n'),
913
('unchanged', 'c\n'),
914
('unchanged', 'd\n'),
915
('unchanged', 'y\n'),
916
('unchanged', 'f\n'),
917
('unchanged', 'g\n')],
920
def test_criss_cross_flip_flop(self):
921
# This is specificly trying to trigger problems when using limited
922
# ancestry and weaves. The ancestry graph looks like:
923
# XX unused ancestor, should not show up in the weave
927
# B C B & C both introduce a new line
931
# D E B & C are both merged, so both are common ancestors
932
# In the process of merging, both sides order the new
935
self.add_rev('root', 'XX', [], 'qrs')
936
self.add_rev('root', 'A', ['XX'], 'abcdef')
937
self.add_rev('root', 'B', ['A'], 'abcdgef')
938
self.add_rev('root', 'C', ['A'], 'abcdhef')
939
self.add_rev('root', 'D', ['B', 'C'], 'abcdghef')
940
self.add_rev('root', 'E', ['C', 'B'], 'abcdhgef')
941
plan = list(self.plan_merge_vf.plan_merge('D', 'E'))
943
('unchanged', 'a\n'),
944
('unchanged', 'b\n'),
945
('unchanged', 'c\n'),
946
('unchanged', 'd\n'),
948
('unchanged', 'g\n'),
950
('unchanged', 'e\n'),
951
('unchanged', 'f\n'),
953
pwm = versionedfile.PlanWeaveMerge(plan)
954
self.assertEqualDiff('\n'.join('abcdghef') + '\n',
955
''.join(pwm.base_from_plan()))
956
# Reversing the order reverses the merge plan, and final order of 'hg'
958
plan = list(self.plan_merge_vf.plan_merge('E', 'D'))
960
('unchanged', 'a\n'),
961
('unchanged', 'b\n'),
962
('unchanged', 'c\n'),
963
('unchanged', 'd\n'),
965
('unchanged', 'h\n'),
967
('unchanged', 'e\n'),
968
('unchanged', 'f\n'),
970
pwm = versionedfile.PlanWeaveMerge(plan)
971
self.assertEqualDiff('\n'.join('abcdhgef') + '\n',
972
''.join(pwm.base_from_plan()))
973
# This is where lca differs, in that it (fairly correctly) determines
974
# that there is a conflict because both sides resolved the merge
976
plan = list(self.plan_merge_vf.plan_lca_merge('D', 'E'))
978
('unchanged', 'a\n'),
979
('unchanged', 'b\n'),
980
('unchanged', 'c\n'),
981
('unchanged', 'd\n'),
982
('conflicted-b', 'h\n'),
983
('unchanged', 'g\n'),
984
('conflicted-a', 'h\n'),
985
('unchanged', 'e\n'),
986
('unchanged', 'f\n'),
988
pwm = versionedfile.PlanWeaveMerge(plan)
989
self.assertEqualDiff('\n'.join('abcdgef') + '\n',
990
''.join(pwm.base_from_plan()))
991
# Reversing it changes what line is doubled, but still gives a
993
plan = list(self.plan_merge_vf.plan_lca_merge('E', 'D'))
995
('unchanged', 'a\n'),
996
('unchanged', 'b\n'),
997
('unchanged', 'c\n'),
998
('unchanged', 'd\n'),
999
('conflicted-b', 'g\n'),
1000
('unchanged', 'h\n'),
1001
('conflicted-a', 'g\n'),
1002
('unchanged', 'e\n'),
1003
('unchanged', 'f\n'),
1005
pwm = versionedfile.PlanWeaveMerge(plan)
1006
self.assertEqualDiff('\n'.join('abcdhef') + '\n',
1007
''.join(pwm.base_from_plan()))
1009
def assertRemoveExternalReferences(self, filtered_parent_map,
1010
child_map, tails, parent_map):
1011
"""Assert results for _PlanMerge._remove_external_references."""
1012
(act_filtered_parent_map, act_child_map,
1013
act_tails) = _PlanMerge._remove_external_references(parent_map)
1015
# The parent map *should* preserve ordering, but the ordering of
1016
# children is not strictly defined
1017
# child_map = dict((k, sorted(children))
1018
# for k, children in child_map.iteritems())
1019
# act_child_map = dict(k, sorted(children)
1020
# for k, children in act_child_map.iteritems())
1021
self.assertEqual(filtered_parent_map, act_filtered_parent_map)
1022
self.assertEqual(child_map, act_child_map)
1023
self.assertEqual(sorted(tails), sorted(act_tails))
1025
def test__remove_external_references(self):
1026
# First, nothing to remove
1027
self.assertRemoveExternalReferences({3: [2], 2: [1], 1: []},
1028
{1: [2], 2: [3], 3: []}, [1], {3: [2], 2: [1], 1: []})
1029
# The reverse direction
1030
self.assertRemoveExternalReferences({1: [2], 2: [3], 3: []},
1031
{3: [2], 2: [1], 1: []}, [3], {1: [2], 2: [3], 3: []})
1033
self.assertRemoveExternalReferences({3: [2], 2: [1], 1: []},
1034
{1: [2], 2: [3], 3: []}, [1], {3: [2, 4], 2: [1, 5], 1: [6]})
1036
self.assertRemoveExternalReferences(
1037
{4: [2, 3], 3: [], 2: [1], 1: []},
1038
{1: [2], 2: [4], 3: [4], 4: []},
1040
{4: [2, 3], 3: [5], 2: [1], 1: [6]})
1042
self.assertRemoveExternalReferences(
1043
{1: [3], 2: [3, 4], 3: [], 4: []},
1044
{1: [], 2: [], 3: [1, 2], 4: [2]},
1046
{1: [3], 2: [3, 4], 3: [5], 4: []})
1048
def assertPruneTails(self, pruned_map, tails, parent_map):
1050
for key, parent_keys in parent_map.items():
1051
child_map.setdefault(key, [])
1052
for pkey in parent_keys:
1053
child_map.setdefault(pkey, []).append(key)
1054
_PlanMerge._prune_tails(parent_map, child_map, tails)
1055
self.assertEqual(pruned_map, parent_map)
1057
def test__prune_tails(self):
1058
# Nothing requested to prune
1059
self.assertPruneTails({1: [], 2: [], 3: []}, [],
1060
{1: [], 2: [], 3: []})
1061
# Prune a single entry
1062
self.assertPruneTails({1: [], 3: []}, [2],
1063
{1: [], 2: [], 3: []})
1065
self.assertPruneTails({1: []}, [3],
1066
{1: [], 2: [3], 3: []})
1067
# Prune a chain with a diamond
1068
self.assertPruneTails({1: []}, [5],
1069
{1: [], 2: [3, 4], 3: [5], 4: [5], 5: []})
1070
# Prune a partial chain
1071
self.assertPruneTails({1: [6], 6:[]}, [5],
1072
{1: [2, 6], 2: [3, 4], 3: [5], 4: [5], 5: [],
1074
# Prune a chain with multiple tips, that pulls out intermediates
1075
self.assertPruneTails({1:[3], 3:[]}, [4, 5],
1076
{1: [2, 3], 2: [4, 5], 3: [], 4:[], 5:[]})
1077
self.assertPruneTails({1:[3], 3:[]}, [5, 4],
1078
{1: [2, 3], 2: [4, 5], 3: [], 4:[], 5:[]})
1080
def test_subtract_plans(self):
1082
('unchanged', 'a\n'),
1084
('killed-a', 'c\n'),
1087
('killed-b', 'f\n'),
1088
('killed-b', 'g\n'),
1091
('unchanged', 'a\n'),
1093
('killed-a', 'c\n'),
1096
('killed-b', 'f\n'),
1097
('killed-b', 'i\n'),
1100
('unchanged', 'a\n'),
1102
('killed-a', 'c\n'),
1104
('unchanged', 'f\n'),
1105
('killed-b', 'i\n'),
1107
self.assertEqual(subtracted_plan,
1108
list(_PlanMerge._subtract_plans(old_plan, new_plan)))
1110
def setup_merge_with_base(self):
1111
self.add_rev('root', 'COMMON', [], 'abc')
1112
self.add_rev('root', 'THIS', ['COMMON'], 'abcd')
1113
self.add_rev('root', 'BASE', ['COMMON'], 'eabc')
1114
self.add_rev('root', 'OTHER', ['BASE'], 'eafb')
1116
def test_plan_merge_with_base(self):
1117
self.setup_merge_with_base()
1118
plan = self.plan_merge_vf.plan_merge('THIS', 'OTHER', 'BASE')
1119
self.assertEqual([('unchanged', 'a\n'),
1121
('unchanged', 'b\n'),
1122
('killed-b', 'c\n'),
1126
def test_plan_lca_merge(self):
1127
self.setup_plan_merge()
1128
plan = self.plan_merge_vf.plan_lca_merge('B', 'C')
1131
('unchanged', 'a\n'),
1132
('killed-b', 'c\n'),
1135
('killed-a', 'b\n'),
1136
('unchanged', 'g\n')],
1139
def test_plan_lca_merge_uncommitted_files(self):
1140
self.setup_plan_merge_uncommitted()
1141
plan = self.plan_merge_vf.plan_lca_merge('B:', 'C:')
1144
('unchanged', 'a\n'),
1145
('killed-b', 'c\n'),
1148
('killed-a', 'b\n'),
1149
('unchanged', 'g\n')],
1152
def test_plan_lca_merge_with_base(self):
1153
self.setup_merge_with_base()
1154
plan = self.plan_merge_vf.plan_lca_merge('THIS', 'OTHER', 'BASE')
1155
self.assertEqual([('unchanged', 'a\n'),
1157
('unchanged', 'b\n'),
1158
('killed-b', 'c\n'),
1162
def test_plan_lca_merge_with_criss_cross(self):
1163
self.add_version(('root', 'ROOT'), [], 'abc')
1164
# each side makes a change
1165
self.add_version(('root', 'REV1'), [('root', 'ROOT')], 'abcd')
1166
self.add_version(('root', 'REV2'), [('root', 'ROOT')], 'abce')
1167
# both sides merge, discarding others' changes
1168
self.add_version(('root', 'LCA1'),
1169
[('root', 'REV1'), ('root', 'REV2')], 'abcd')
1170
self.add_version(('root', 'LCA2'),
1171
[('root', 'REV1'), ('root', 'REV2')], 'fabce')
1172
plan = self.plan_merge_vf.plan_lca_merge('LCA1', 'LCA2')
1173
self.assertEqual([('new-b', 'f\n'),
1174
('unchanged', 'a\n'),
1175
('unchanged', 'b\n'),
1176
('unchanged', 'c\n'),
1177
('conflicted-a', 'd\n'),
1178
('conflicted-b', 'e\n'),
1181
def test_plan_lca_merge_with_null(self):
1182
self.add_version(('root', 'A'), [], 'ab')
1183
self.add_version(('root', 'B'), [], 'bc')
1184
plan = self.plan_merge_vf.plan_lca_merge('A', 'B')
1185
self.assertEqual([('new-a', 'a\n'),
1186
('unchanged', 'b\n'),
1190
def test_plan_merge_with_delete_and_change(self):
1191
self.add_rev('root', 'C', [], 'a')
1192
self.add_rev('root', 'A', ['C'], 'b')
1193
self.add_rev('root', 'B', ['C'], '')
1194
plan = self.plan_merge_vf.plan_merge('A', 'B')
1195
self.assertEqual([('killed-both', 'a\n'),
1199
def test_plan_merge_with_move_and_change(self):
1200
self.add_rev('root', 'C', [], 'abcd')
1201
self.add_rev('root', 'A', ['C'], 'acbd')
1202
self.add_rev('root', 'B', ['C'], 'aBcd')
1203
plan = self.plan_merge_vf.plan_merge('A', 'B')
1204
self.assertEqual([('unchanged', 'a\n'),
1206
('killed-b', 'b\n'),
1208
('killed-a', 'c\n'),
1209
('unchanged', 'd\n'),
1213
class LoggingMerger(object):
1214
# These seem to be the required attributes
1215
requires_base = False
1216
supports_reprocess = False
1217
supports_show_base = False
1218
supports_cherrypick = False
1219
# We intentionally do not define supports_lca_trees
1221
def __init__(self, *args, **kwargs):
1223
self.kwargs = kwargs
1226
class TestMergerBase(TestCaseWithMemoryTransport):
1227
"""Common functionality for Merger tests that don't write to disk."""
1229
def get_builder(self):
1230
builder = self.make_branch_builder('path')
1231
builder.start_series()
1232
self.addCleanup(builder.finish_series)
1235
def setup_simple_graph(self):
1236
"""Create a simple 3-node graph.
1238
:return: A BranchBuilder
1245
builder = self.get_builder()
1246
builder.build_snapshot('A-id', None,
1247
[('add', ('', None, 'directory', None))])
1248
builder.build_snapshot('C-id', ['A-id'], [])
1249
builder.build_snapshot('B-id', ['A-id'], [])
1252
def setup_criss_cross_graph(self):
1253
"""Create a 5-node graph with a criss-cross.
1255
:return: A BranchBuilder
1262
builder = self.setup_simple_graph()
1263
builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
1264
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1267
def make_Merger(self, builder, other_revision_id,
1268
interesting_files=None, interesting_ids=None):
1269
"""Make a Merger object from a branch builder"""
1270
mem_tree = memorytree.MemoryTree.create_on_branch(builder.get_branch())
1271
mem_tree.lock_write()
1272
self.addCleanup(mem_tree.unlock)
1273
merger = _mod_merge.Merger.from_revision_ids(
1274
mem_tree, other_revision_id)
1275
merger.set_interesting_files(interesting_files)
1276
# It seems there is no matching function for set_interesting_ids
1277
merger.interesting_ids = interesting_ids
1278
merger.merge_type = _mod_merge.Merge3Merger
1282
class TestMergerInMemory(TestMergerBase):
1284
def test_cache_trees_with_revision_ids_None(self):
1285
merger = self.make_Merger(self.setup_simple_graph(), 'C-id')
1286
original_cache = dict(merger._cached_trees)
1287
merger.cache_trees_with_revision_ids([None])
1288
self.assertEqual(original_cache, merger._cached_trees)
1290
def test_cache_trees_with_revision_ids_no_revision_id(self):
1291
merger = self.make_Merger(self.setup_simple_graph(), 'C-id')
1292
original_cache = dict(merger._cached_trees)
1293
tree = self.make_branch_and_memory_tree('tree')
1294
merger.cache_trees_with_revision_ids([tree])
1295
self.assertEqual(original_cache, merger._cached_trees)
1297
def test_cache_trees_with_revision_ids_having_revision_id(self):
1298
merger = self.make_Merger(self.setup_simple_graph(), 'C-id')
1299
original_cache = dict(merger._cached_trees)
1300
tree = merger.this_branch.repository.revision_tree('B-id')
1301
original_cache['B-id'] = tree
1302
merger.cache_trees_with_revision_ids([tree])
1303
self.assertEqual(original_cache, merger._cached_trees)
1305
def test_find_base(self):
1306
merger = self.make_Merger(self.setup_simple_graph(), 'C-id')
1307
self.assertEqual('A-id', merger.base_rev_id)
1308
self.assertFalse(merger._is_criss_cross)
1309
self.assertIs(None, merger._lca_trees)
1311
def test_find_base_criss_cross(self):
1312
builder = self.setup_criss_cross_graph()
1313
merger = self.make_Merger(builder, 'E-id')
1314
self.assertEqual('A-id', merger.base_rev_id)
1315
self.assertTrue(merger._is_criss_cross)
1316
self.assertEqual(['B-id', 'C-id'], [t.get_revision_id()
1317
for t in merger._lca_trees])
1318
# If we swap the order, we should get a different lca order
1319
builder.build_snapshot('F-id', ['E-id'], [])
1320
merger = self.make_Merger(builder, 'D-id')
1321
self.assertEqual(['C-id', 'B-id'], [t.get_revision_id()
1322
for t in merger._lca_trees])
1324
def test_find_base_triple_criss_cross(self):
1327
# B C F # F is merged into both branches
1334
builder = self.setup_criss_cross_graph()
1335
builder.build_snapshot('F-id', ['A-id'], [])
1336
builder.build_snapshot('H-id', ['E-id', 'F-id'], [])
1337
builder.build_snapshot('G-id', ['D-id', 'F-id'], [])
1338
merger = self.make_Merger(builder, 'H-id')
1339
self.assertEqual(['B-id', 'C-id', 'F-id'],
1340
[t.get_revision_id() for t in merger._lca_trees])
1342
def test_find_base_new_root_criss_cross(self):
1349
builder = self.get_builder()
1350
builder.build_snapshot('A-id', None,
1351
[('add', ('', None, 'directory', None))])
1352
builder.build_snapshot('B-id', [],
1353
[('add', ('', None, 'directory', None))])
1354
builder.build_snapshot('D-id', ['A-id', 'B-id'], [])
1355
builder.build_snapshot('C-id', ['A-id', 'B-id'], [])
1356
merger = self.make_Merger(builder, 'D-id')
1357
self.assertEqual('A-id', merger.base_rev_id)
1358
self.assertTrue(merger._is_criss_cross)
1359
self.assertEqual(['A-id', 'B-id'], [t.get_revision_id()
1360
for t in merger._lca_trees])
1362
def test_no_criss_cross_passed_to_merge_type(self):
1363
class LCATreesMerger(LoggingMerger):
1364
supports_lca_trees = True
1366
merger = self.make_Merger(self.setup_simple_graph(), 'C-id')
1367
merger.merge_type = LCATreesMerger
1368
merge_obj = merger.make_merger()
1369
self.assertIsInstance(merge_obj, LCATreesMerger)
1370
self.assertFalse('lca_trees' in merge_obj.kwargs)
1372
def test_criss_cross_passed_to_merge_type(self):
1373
merger = self.make_Merger(self.setup_criss_cross_graph(), 'E-id')
1374
merger.merge_type = _mod_merge.Merge3Merger
1375
merge_obj = merger.make_merger()
1376
self.assertEqual(['B-id', 'C-id'], [t.get_revision_id()
1377
for t in merger._lca_trees])
1379
def test_criss_cross_not_supported_merge_type(self):
1380
merger = self.make_Merger(self.setup_criss_cross_graph(), 'E-id')
1381
# We explicitly do not define supports_lca_trees
1382
merger.merge_type = LoggingMerger
1383
merge_obj = merger.make_merger()
1384
self.assertIsInstance(merge_obj, LoggingMerger)
1385
self.assertFalse('lca_trees' in merge_obj.kwargs)
1387
def test_criss_cross_unsupported_merge_type(self):
1388
class UnsupportedLCATreesMerger(LoggingMerger):
1389
supports_lca_trees = False
1391
merger = self.make_Merger(self.setup_criss_cross_graph(), 'E-id')
1392
merger.merge_type = UnsupportedLCATreesMerger
1393
merge_obj = merger.make_merger()
1394
self.assertIsInstance(merge_obj, UnsupportedLCATreesMerger)
1395
self.assertFalse('lca_trees' in merge_obj.kwargs)
1398
class TestMergerEntriesLCA(TestMergerBase):
1400
def make_merge_obj(self, builder, other_revision_id,
1401
interesting_files=None, interesting_ids=None):
1402
merger = self.make_Merger(builder, other_revision_id,
1403
interesting_files=interesting_files,
1404
interesting_ids=interesting_ids)
1405
return merger.make_merger()
1407
def test_simple(self):
1408
builder = self.get_builder()
1409
builder.build_snapshot('A-id', None,
1410
[('add', (u'', 'a-root-id', 'directory', None)),
1411
('add', (u'a', 'a-id', 'file', 'a\nb\nc\n'))])
1412
builder.build_snapshot('C-id', ['A-id'],
1413
[('modify', ('a-id', 'a\nb\nC\nc\n'))])
1414
builder.build_snapshot('B-id', ['A-id'],
1415
[('modify', ('a-id', 'a\nB\nb\nc\n'))])
1416
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1417
[('modify', ('a-id', 'a\nB\nb\nC\nc\nE\n'))])
1418
builder.build_snapshot('D-id', ['B-id', 'C-id'],
1419
[('modify', ('a-id', 'a\nB\nb\nC\nc\n'))])
1420
merge_obj = self.make_merge_obj(builder, 'E-id')
1422
self.assertEqual(['B-id', 'C-id'], [t.get_revision_id()
1423
for t in merge_obj._lca_trees])
1424
self.assertEqual('A-id', merge_obj.base_tree.get_revision_id())
1425
entries = list(merge_obj._entries_lca())
1427
# (file_id, changed, parents, names, executable)
1428
# BASE, lca1, lca2, OTHER, THIS
1429
root_id = 'a-root-id'
1430
self.assertEqual([('a-id', True,
1431
((root_id, [root_id, root_id]), root_id, root_id),
1432
((u'a', [u'a', u'a']), u'a', u'a'),
1433
((False, [False, False]), False, False)),
1436
def test_not_in_base(self):
1437
# LCAs all have the same last-modified revision for the file, as do
1438
# the tips, but the base has something different
1439
# A base, doesn't have the file
1441
# B C B introduces 'foo', C introduces 'bar'
1443
# D E D and E now both have 'foo' and 'bar'
1445
# F G the files are now in F, G, D and E, but not in A
1448
builder = self.get_builder()
1449
builder.build_snapshot('A-id', None,
1450
[('add', (u'', 'a-root-id', 'directory', None))])
1451
builder.build_snapshot('B-id', ['A-id'],
1452
[('add', (u'foo', 'foo-id', 'file', 'a\nb\nc\n'))])
1453
builder.build_snapshot('C-id', ['A-id'],
1454
[('add', (u'bar', 'bar-id', 'file', 'd\ne\nf\n'))])
1455
builder.build_snapshot('D-id', ['B-id', 'C-id'],
1456
[('add', (u'bar', 'bar-id', 'file', 'd\ne\nf\n'))])
1457
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1458
[('add', (u'foo', 'foo-id', 'file', 'a\nb\nc\n'))])
1459
builder.build_snapshot('G-id', ['E-id', 'D-id'],
1460
[('modify', (u'bar-id', 'd\ne\nf\nG\n'))])
1461
builder.build_snapshot('F-id', ['D-id', 'E-id'], [])
1462
merge_obj = self.make_merge_obj(builder, 'G-id')
1464
self.assertEqual(['D-id', 'E-id'], [t.get_revision_id()
1465
for t in merge_obj._lca_trees])
1466
self.assertEqual('A-id', merge_obj.base_tree.get_revision_id())
1467
entries = list(merge_obj._entries_lca())
1468
root_id = 'a-root-id'
1469
self.assertEqual([('bar-id', True,
1470
((None, [root_id, root_id]), root_id, root_id),
1471
((None, [u'bar', u'bar']), u'bar', u'bar'),
1472
((None, [False, False]), False, False)),
1475
def test_not_in_this(self):
1476
builder = self.get_builder()
1477
builder.build_snapshot('A-id', None,
1478
[('add', (u'', 'a-root-id', 'directory', None)),
1479
('add', (u'a', 'a-id', 'file', 'a\nb\nc\n'))])
1480
builder.build_snapshot('B-id', ['A-id'],
1481
[('modify', ('a-id', 'a\nB\nb\nc\n'))])
1482
builder.build_snapshot('C-id', ['A-id'],
1483
[('modify', ('a-id', 'a\nb\nC\nc\n'))])
1484
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1485
[('modify', ('a-id', 'a\nB\nb\nC\nc\nE\n'))])
1486
builder.build_snapshot('D-id', ['B-id', 'C-id'],
1487
[('unversion', 'a-id')])
1488
merge_obj = self.make_merge_obj(builder, 'E-id')
1490
self.assertEqual(['B-id', 'C-id'], [t.get_revision_id()
1491
for t in merge_obj._lca_trees])
1492
self.assertEqual('A-id', merge_obj.base_tree.get_revision_id())
1494
entries = list(merge_obj._entries_lca())
1495
root_id = 'a-root-id'
1496
self.assertEqual([('a-id', True,
1497
((root_id, [root_id, root_id]), root_id, None),
1498
((u'a', [u'a', u'a']), u'a', None),
1499
((False, [False, False]), False, None)),
1502
def test_file_not_in_one_lca(self):
1505
# B C # B no file, C introduces a file
1507
# D E # D and E both have the file, unchanged from C
1508
builder = self.get_builder()
1509
builder.build_snapshot('A-id', None,
1510
[('add', (u'', 'a-root-id', 'directory', None))])
1511
builder.build_snapshot('B-id', ['A-id'], [])
1512
builder.build_snapshot('C-id', ['A-id'],
1513
[('add', (u'a', 'a-id', 'file', 'a\nb\nc\n'))])
1514
builder.build_snapshot('E-id', ['C-id', 'B-id'], []) # Inherited from C
1515
builder.build_snapshot('D-id', ['B-id', 'C-id'], # Merged from C
1516
[('add', (u'a', 'a-id', 'file', 'a\nb\nc\n'))])
1517
merge_obj = self.make_merge_obj(builder, 'E-id')
1519
self.assertEqual(['B-id', 'C-id'], [t.get_revision_id()
1520
for t in merge_obj._lca_trees])
1521
self.assertEqual('A-id', merge_obj.base_tree.get_revision_id())
1523
entries = list(merge_obj._entries_lca())
1524
self.assertEqual([], entries)
1526
def test_not_in_other(self):
1527
builder = self.get_builder()
1528
builder.build_snapshot('A-id', None,
1529
[('add', (u'', 'a-root-id', 'directory', None)),
1530
('add', (u'a', 'a-id', 'file', 'a\nb\nc\n'))])
1531
builder.build_snapshot('B-id', ['A-id'], [])
1532
builder.build_snapshot('C-id', ['A-id'], [])
1533
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1534
[('unversion', 'a-id')])
1535
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1536
merge_obj = self.make_merge_obj(builder, 'E-id')
1538
entries = list(merge_obj._entries_lca())
1539
root_id = 'a-root-id'
1540
self.assertEqual([('a-id', True,
1541
((root_id, [root_id, root_id]), None, root_id),
1542
((u'a', [u'a', u'a']), None, u'a'),
1543
((False, [False, False]), None, False)),
1546
def test_not_in_other_or_lca(self):
1547
# A base, introduces 'foo'
1549
# B C B nothing, C deletes foo
1551
# D E D restores foo (same as B), E leaves it deleted
1553
# A => B, no changes
1554
# A => C, delete foo (C should supersede B)
1555
# C => D, restore foo
1556
# C => E, no changes
1557
# D would then win 'cleanly' and no record would be given
1558
builder = self.get_builder()
1559
builder.build_snapshot('A-id', None,
1560
[('add', (u'', 'a-root-id', 'directory', None)),
1561
('add', (u'foo', 'foo-id', 'file', 'content\n'))])
1562
builder.build_snapshot('B-id', ['A-id'], [])
1563
builder.build_snapshot('C-id', ['A-id'],
1564
[('unversion', 'foo-id')])
1565
builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
1566
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1567
merge_obj = self.make_merge_obj(builder, 'E-id')
1569
entries = list(merge_obj._entries_lca())
1570
self.assertEqual([], entries)
1572
def test_not_in_other_mod_in_lca1_not_in_lca2(self):
1573
# A base, introduces 'foo'
1575
# B C B changes 'foo', C deletes foo
1577
# D E D restores foo (same as B), E leaves it deleted (as C)
1579
# A => B, modified foo
1580
# A => C, delete foo, C does not supersede B
1581
# B => D, no changes
1582
# C => D, resolve in favor of B
1583
# B => E, resolve in favor of E
1584
# C => E, no changes
1585
# In this case, we have a conflict of how the changes were resolved. E
1586
# picked C and D picked B, so we should issue a conflict
1587
builder = self.get_builder()
1588
builder.build_snapshot('A-id', None,
1589
[('add', (u'', 'a-root-id', 'directory', None)),
1590
('add', (u'foo', 'foo-id', 'file', 'content\n'))])
1591
builder.build_snapshot('B-id', ['A-id'], [
1592
('modify', ('foo-id', 'new-content\n'))])
1593
builder.build_snapshot('C-id', ['A-id'],
1594
[('unversion', 'foo-id')])
1595
builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
1596
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1597
merge_obj = self.make_merge_obj(builder, 'E-id')
1599
entries = list(merge_obj._entries_lca())
1600
root_id = 'a-root-id'
1601
self.assertEqual([('foo-id', True,
1602
((root_id, [root_id, None]), None, root_id),
1603
((u'foo', [u'foo', None]), None, 'foo'),
1604
((False, [False, None]), None, False)),
1607
def test_only_in_one_lca(self):
1610
# B C B nothing, C add file
1612
# D E D still has nothing, E removes file
1615
# C => D, removed the file
1617
# C => E, removed the file
1618
# Thus D & E have identical changes, and this is a no-op
1621
# A => C, add file, thus C supersedes B
1622
# w/ C=BASE, D=THIS, E=OTHER we have 'happy convergence'
1623
builder = self.get_builder()
1624
builder.build_snapshot('A-id', None,
1625
[('add', (u'', 'a-root-id', 'directory', None))])
1626
builder.build_snapshot('B-id', ['A-id'], [])
1627
builder.build_snapshot('C-id', ['A-id'],
1628
[('add', (u'a', 'a-id', 'file', 'a\nb\nc\n'))])
1629
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1630
[('unversion', 'a-id')])
1631
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1632
merge_obj = self.make_merge_obj(builder, 'E-id')
1634
entries = list(merge_obj._entries_lca())
1635
self.assertEqual([], entries)
1637
def test_only_in_other(self):
1638
builder = self.get_builder()
1639
builder.build_snapshot('A-id', None,
1640
[('add', (u'', 'a-root-id', 'directory', None))])
1641
builder.build_snapshot('B-id', ['A-id'], [])
1642
builder.build_snapshot('C-id', ['A-id'], [])
1643
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1644
[('add', (u'a', 'a-id', 'file', 'a\nb\nc\n'))])
1645
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1646
merge_obj = self.make_merge_obj(builder, 'E-id')
1648
entries = list(merge_obj._entries_lca())
1649
root_id = 'a-root-id'
1650
self.assertEqual([('a-id', True,
1651
((None, [None, None]), root_id, None),
1652
((None, [None, None]), u'a', None),
1653
((None, [None, None]), False, None)),
1656
def test_one_lca_supersedes(self):
1657
# One LCA supersedes the other LCAs last modified value, but the
1658
# value is not the same as BASE.
1659
# A base, introduces 'foo', last mod A
1661
# B C B modifies 'foo' (mod B), C does nothing (mod A)
1663
# D E D does nothing (mod B), E updates 'foo' (mod E)
1665
# F G F updates 'foo' (mod F). G does nothing (mod E)
1667
# At this point, G should not be considered to modify 'foo', even
1668
# though its LCAs disagree. This is because the modification in E
1669
# completely supersedes the value in D.
1670
builder = self.get_builder()
1671
builder.build_snapshot('A-id', None,
1672
[('add', (u'', 'a-root-id', 'directory', None)),
1673
('add', (u'foo', 'foo-id', 'file', 'A content\n'))])
1674
builder.build_snapshot('C-id', ['A-id'], [])
1675
builder.build_snapshot('B-id', ['A-id'],
1676
[('modify', ('foo-id', 'B content\n'))])
1677
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1678
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1679
[('modify', ('foo-id', 'E content\n'))])
1680
builder.build_snapshot('G-id', ['E-id', 'D-id'], [])
1681
builder.build_snapshot('F-id', ['D-id', 'E-id'],
1682
[('modify', ('foo-id', 'F content\n'))])
1683
merge_obj = self.make_merge_obj(builder, 'G-id')
1685
self.assertEqual([], list(merge_obj._entries_lca()))
1687
def test_one_lca_supersedes_path(self):
1688
# Double-criss-cross merge, the ultimate base value is different from
1692
# B C B value 'bar', C = 'foo'
1694
# D E D = 'bar', E supersedes to 'bing'
1696
# F G F = 'bing', G supersedes to 'barry'
1698
# In this case, we technically should not care about the value 'bar' for
1699
# D, because it was clearly superseded by E's 'bing'. The
1700
# per-file/attribute graph would actually look like:
1709
# Because the other side of the merge never modifies the value, it just
1710
# takes the value from the merge.
1712
# ATM this fails because we will prune 'foo' from the LCAs, but we
1713
# won't prune 'bar'. This is getting far off into edge-case land, so we
1714
# aren't supporting it yet.
1716
builder = self.get_builder()
1717
builder.build_snapshot('A-id', None,
1718
[('add', (u'', 'a-root-id', 'directory', None)),
1719
('add', (u'foo', 'foo-id', 'file', 'A content\n'))])
1720
builder.build_snapshot('C-id', ['A-id'], [])
1721
builder.build_snapshot('B-id', ['A-id'],
1722
[('rename', ('foo', 'bar'))])
1723
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1724
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1725
[('rename', ('foo', 'bing'))]) # override to bing
1726
builder.build_snapshot('G-id', ['E-id', 'D-id'],
1727
[('rename', ('bing', 'barry'))]) # override to barry
1728
builder.build_snapshot('F-id', ['D-id', 'E-id'],
1729
[('rename', ('bar', 'bing'))]) # Merge in E's change
1730
merge_obj = self.make_merge_obj(builder, 'G-id')
1732
self.expectFailure("We don't do an actual heads() check on lca values,"
1733
" or use the per-attribute graph",
1734
self.assertEqual, [], list(merge_obj._entries_lca()))
1736
def test_one_lca_accidentally_pruned(self):
1737
# Another incorrect resolution from the same basic flaw:
1740
# B C B value 'bar', C = 'foo'
1742
# D E D = 'bar', E reverts to 'foo'
1744
# F G F = 'bing', G switches to 'bar'
1746
# 'bar' will not be seen as an interesting change, because 'foo' will
1747
# be pruned from the LCAs, even though it was newly introduced by E
1749
builder = self.get_builder()
1750
builder.build_snapshot('A-id', None,
1751
[('add', (u'', 'a-root-id', 'directory', None)),
1752
('add', (u'foo', 'foo-id', 'file', 'A content\n'))])
1753
builder.build_snapshot('C-id', ['A-id'], [])
1754
builder.build_snapshot('B-id', ['A-id'],
1755
[('rename', ('foo', 'bar'))])
1756
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1757
builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
1758
builder.build_snapshot('G-id', ['E-id', 'D-id'],
1759
[('rename', ('foo', 'bar'))])
1760
builder.build_snapshot('F-id', ['D-id', 'E-id'],
1761
[('rename', ('bar', 'bing'))]) # should end up conflicting
1762
merge_obj = self.make_merge_obj(builder, 'G-id')
1764
entries = list(merge_obj._entries_lca())
1765
root_id = 'a-root-id'
1766
self.expectFailure("We prune values from BASE even when relevant.",
1769
((root_id, [root_id, root_id]), root_id, root_id),
1770
((u'foo', [u'bar', u'foo']), u'bar', u'bing'),
1771
((False, [False, False]), False, False)),
1774
def test_both_sides_revert(self):
1775
# Both sides of a criss-cross revert the text to the lca
1776
# A base, introduces 'foo'
1778
# B C B modifies 'foo', C modifies 'foo'
1780
# D E D reverts to B, E reverts to C
1781
# This should conflict
1782
builder = self.get_builder()
1783
builder.build_snapshot('A-id', None,
1784
[('add', (u'', 'a-root-id', 'directory', None)),
1785
('add', (u'foo', 'foo-id', 'file', 'A content\n'))])
1786
builder.build_snapshot('B-id', ['A-id'],
1787
[('modify', ('foo-id', 'B content\n'))])
1788
builder.build_snapshot('C-id', ['A-id'],
1789
[('modify', ('foo-id', 'C content\n'))])
1790
builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
1791
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1792
merge_obj = self.make_merge_obj(builder, 'E-id')
1794
entries = list(merge_obj._entries_lca())
1795
root_id = 'a-root-id'
1796
self.assertEqual([('foo-id', True,
1797
((root_id, [root_id, root_id]), root_id, root_id),
1798
((u'foo', [u'foo', u'foo']), u'foo', u'foo'),
1799
((False, [False, False]), False, False)),
1802
def test_different_lca_resolve_one_side_updates_content(self):
1803
# Both sides converge, but then one side updates the text.
1804
# A base, introduces 'foo'
1806
# B C B modifies 'foo', C modifies 'foo'
1808
# D E D reverts to B, E reverts to C
1810
# F F updates to a new value
1811
# We need to emit an entry for 'foo', because D & E differed on the
1813
builder = self.get_builder()
1814
builder.build_snapshot('A-id', None,
1815
[('add', (u'', 'a-root-id', 'directory', None)),
1816
('add', (u'foo', 'foo-id', 'file', 'A content\n'))])
1817
builder.build_snapshot('B-id', ['A-id'],
1818
[('modify', ('foo-id', 'B content\n'))])
1819
builder.build_snapshot('C-id', ['A-id'],
1820
[('modify', ('foo-id', 'C content\n'))])
1821
builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
1822
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1823
builder.build_snapshot('F-id', ['D-id'],
1824
[('modify', ('foo-id', 'F content\n'))])
1825
merge_obj = self.make_merge_obj(builder, 'E-id')
1827
entries = list(merge_obj._entries_lca())
1828
root_id = 'a-root-id'
1829
self.assertEqual([('foo-id', True,
1830
((root_id, [root_id, root_id]), root_id, root_id),
1831
((u'foo', [u'foo', u'foo']), u'foo', u'foo'),
1832
((False, [False, False]), False, False)),
1835
def test_same_lca_resolution_one_side_updates_content(self):
1836
# Both sides converge, but then one side updates the text.
1837
# A base, introduces 'foo'
1839
# B C B modifies 'foo', C modifies 'foo'
1841
# D E D and E use C's value
1843
# F F updates to a new value
1844
# I think it is a bug that this conflicts, but we don't have a way to
1845
# detect otherwise. And because of:
1846
# test_different_lca_resolve_one_side_updates_content
1847
# We need to conflict.
1849
builder = self.get_builder()
1850
builder.build_snapshot('A-id', None,
1851
[('add', (u'', 'a-root-id', 'directory', None)),
1852
('add', (u'foo', 'foo-id', 'file', 'A content\n'))])
1853
builder.build_snapshot('B-id', ['A-id'],
1854
[('modify', ('foo-id', 'B content\n'))])
1855
builder.build_snapshot('C-id', ['A-id'],
1856
[('modify', ('foo-id', 'C content\n'))])
1857
builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
1858
builder.build_snapshot('D-id', ['B-id', 'C-id'],
1859
[('modify', ('foo-id', 'C content\n'))]) # Same as E
1860
builder.build_snapshot('F-id', ['D-id'],
1861
[('modify', ('foo-id', 'F content\n'))])
1862
merge_obj = self.make_merge_obj(builder, 'E-id')
1864
entries = list(merge_obj._entries_lca())
1865
self.expectFailure("We don't detect that LCA resolution was the"
1866
" same on both sides",
1867
self.assertEqual, [], entries)
1869
def test_only_path_changed(self):
1870
builder = self.get_builder()
1871
builder.build_snapshot('A-id', None,
1872
[('add', (u'', 'a-root-id', 'directory', None)),
1873
('add', (u'a', 'a-id', 'file', 'content\n'))])
1874
builder.build_snapshot('B-id', ['A-id'], [])
1875
builder.build_snapshot('C-id', ['A-id'], [])
1876
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1877
[('rename', (u'a', u'b'))])
1878
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1879
merge_obj = self.make_merge_obj(builder, 'E-id')
1880
entries = list(merge_obj._entries_lca())
1881
root_id = 'a-root-id'
1882
# The content was not changed, only the path
1883
self.assertEqual([('a-id', False,
1884
((root_id, [root_id, root_id]), root_id, root_id),
1885
((u'a', [u'a', u'a']), u'b', u'a'),
1886
((False, [False, False]), False, False)),
1889
def test_kind_changed(self):
1890
# Identical content, except 'D' changes a-id into a directory
1891
builder = self.get_builder()
1892
builder.build_snapshot('A-id', None,
1893
[('add', (u'', 'a-root-id', 'directory', None)),
1894
('add', (u'a', 'a-id', 'file', 'content\n'))])
1895
builder.build_snapshot('B-id', ['A-id'], [])
1896
builder.build_snapshot('C-id', ['A-id'], [])
1897
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1898
[('unversion', 'a-id'),
1900
('add', (u'a', 'a-id', 'directory', None))])
1901
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1902
merge_obj = self.make_merge_obj(builder, 'E-id')
1903
entries = list(merge_obj._entries_lca())
1904
root_id = 'a-root-id'
1905
# Only the kind was changed (content)
1906
self.assertEqual([('a-id', True,
1907
((root_id, [root_id, root_id]), root_id, root_id),
1908
((u'a', [u'a', u'a']), u'a', u'a'),
1909
((False, [False, False]), False, False)),
1912
def test_this_changed_kind(self):
1913
# Identical content, but THIS changes a file to a directory
1914
builder = self.get_builder()
1915
builder.build_snapshot('A-id', None,
1916
[('add', (u'', 'a-root-id', 'directory', None)),
1917
('add', (u'a', 'a-id', 'file', 'content\n'))])
1918
builder.build_snapshot('B-id', ['A-id'], [])
1919
builder.build_snapshot('C-id', ['A-id'], [])
1920
builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
1921
builder.build_snapshot('D-id', ['B-id', 'C-id'],
1922
[('unversion', 'a-id'),
1924
('add', (u'a', 'a-id', 'directory', None))])
1925
merge_obj = self.make_merge_obj(builder, 'E-id')
1926
entries = list(merge_obj._entries_lca())
1927
# Only the kind was changed (content)
1928
self.assertEqual([], entries)
1930
def test_interesting_files(self):
1931
# Two files modified, but we should filter one of them
1932
builder = self.get_builder()
1933
builder.build_snapshot('A-id', None,
1934
[('add', (u'', 'a-root-id', 'directory', None)),
1935
('add', (u'a', 'a-id', 'file', 'content\n')),
1936
('add', (u'b', 'b-id', 'file', 'content\n'))])
1937
builder.build_snapshot('B-id', ['A-id'], [])
1938
builder.build_snapshot('C-id', ['A-id'], [])
1939
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1940
[('modify', ('a-id', 'new-content\n')),
1941
('modify', ('b-id', 'new-content\n'))])
1942
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1943
merge_obj = self.make_merge_obj(builder, 'E-id',
1944
interesting_files=['b'])
1945
entries = list(merge_obj._entries_lca())
1946
root_id = 'a-root-id'
1947
self.assertEqual([('b-id', True,
1948
((root_id, [root_id, root_id]), root_id, root_id),
1949
((u'b', [u'b', u'b']), u'b', u'b'),
1950
((False, [False, False]), False, False)),
1953
def test_interesting_file_in_this(self):
1954
# This renamed the file, but it should still match the entry in other
1955
builder = self.get_builder()
1956
builder.build_snapshot('A-id', None,
1957
[('add', (u'', 'a-root-id', 'directory', None)),
1958
('add', (u'a', 'a-id', 'file', 'content\n')),
1959
('add', (u'b', 'b-id', 'file', 'content\n'))])
1960
builder.build_snapshot('B-id', ['A-id'], [])
1961
builder.build_snapshot('C-id', ['A-id'], [])
1962
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1963
[('modify', ('a-id', 'new-content\n')),
1964
('modify', ('b-id', 'new-content\n'))])
1965
builder.build_snapshot('D-id', ['B-id', 'C-id'],
1966
[('rename', ('b', 'c'))])
1967
merge_obj = self.make_merge_obj(builder, 'E-id',
1968
interesting_files=['c'])
1969
entries = list(merge_obj._entries_lca())
1970
root_id = 'a-root-id'
1971
self.assertEqual([('b-id', True,
1972
((root_id, [root_id, root_id]), root_id, root_id),
1973
((u'b', [u'b', u'b']), u'b', u'c'),
1974
((False, [False, False]), False, False)),
1977
def test_interesting_file_in_base(self):
1978
# This renamed the file, but it should still match the entry in BASE
1979
builder = self.get_builder()
1980
builder.build_snapshot('A-id', None,
1981
[('add', (u'', 'a-root-id', 'directory', None)),
1982
('add', (u'a', 'a-id', 'file', 'content\n')),
1983
('add', (u'c', 'c-id', 'file', 'content\n'))])
1984
builder.build_snapshot('B-id', ['A-id'],
1985
[('rename', ('c', 'b'))])
1986
builder.build_snapshot('C-id', ['A-id'],
1987
[('rename', ('c', 'b'))])
1988
builder.build_snapshot('E-id', ['C-id', 'B-id'],
1989
[('modify', ('a-id', 'new-content\n')),
1990
('modify', ('c-id', 'new-content\n'))])
1991
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
1992
merge_obj = self.make_merge_obj(builder, 'E-id',
1993
interesting_files=['c'])
1994
entries = list(merge_obj._entries_lca())
1995
root_id = 'a-root-id'
1996
self.assertEqual([('c-id', True,
1997
((root_id, [root_id, root_id]), root_id, root_id),
1998
((u'c', [u'b', u'b']), u'b', u'b'),
1999
((False, [False, False]), False, False)),
2002
def test_interesting_file_in_lca(self):
2003
# This renamed the file, but it should still match the entry in LCA
2004
builder = self.get_builder()
2005
builder.build_snapshot('A-id', None,
2006
[('add', (u'', 'a-root-id', 'directory', None)),
2007
('add', (u'a', 'a-id', 'file', 'content\n')),
2008
('add', (u'b', 'b-id', 'file', 'content\n'))])
2009
builder.build_snapshot('B-id', ['A-id'],
2010
[('rename', ('b', 'c'))])
2011
builder.build_snapshot('C-id', ['A-id'], [])
2012
builder.build_snapshot('E-id', ['C-id', 'B-id'],
2013
[('modify', ('a-id', 'new-content\n')),
2014
('modify', ('b-id', 'new-content\n'))])
2015
builder.build_snapshot('D-id', ['B-id', 'C-id'],
2016
[('rename', ('c', 'b'))])
2017
merge_obj = self.make_merge_obj(builder, 'E-id',
2018
interesting_files=['c'])
2019
entries = list(merge_obj._entries_lca())
2020
root_id = 'a-root-id'
2021
self.assertEqual([('b-id', True,
2022
((root_id, [root_id, root_id]), root_id, root_id),
2023
((u'b', [u'c', u'b']), u'b', u'b'),
2024
((False, [False, False]), False, False)),
2027
def test_interesting_ids(self):
2028
# Two files modified, but we should filter one of them
2029
builder = self.get_builder()
2030
builder.build_snapshot('A-id', None,
2031
[('add', (u'', 'a-root-id', 'directory', None)),
2032
('add', (u'a', 'a-id', 'file', 'content\n')),
2033
('add', (u'b', 'b-id', 'file', 'content\n'))])
2034
builder.build_snapshot('B-id', ['A-id'], [])
2035
builder.build_snapshot('C-id', ['A-id'], [])
2036
builder.build_snapshot('E-id', ['C-id', 'B-id'],
2037
[('modify', ('a-id', 'new-content\n')),
2038
('modify', ('b-id', 'new-content\n'))])
2039
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
2040
merge_obj = self.make_merge_obj(builder, 'E-id',
2041
interesting_ids=['b-id'])
2042
entries = list(merge_obj._entries_lca())
2043
root_id = 'a-root-id'
2044
self.assertEqual([('b-id', True,
2045
((root_id, [root_id, root_id]), root_id, root_id),
2046
((u'b', [u'b', u'b']), u'b', u'b'),
2047
((False, [False, False]), False, False)),
2052
class TestMergerEntriesLCAOnDisk(tests.TestCaseWithTransport):
2054
def get_builder(self):
2055
builder = self.make_branch_builder('path')
2056
builder.start_series()
2057
self.addCleanup(builder.finish_series)
2060
def get_wt_from_builder(self, builder):
2061
"""Get a real WorkingTree from the builder."""
2062
the_branch = builder.get_branch()
2063
wt = the_branch.controldir.create_workingtree()
2064
# Note: This is a little bit ugly, but we are holding the branch
2065
# write-locked as part of the build process, and we would like to
2066
# maintain that. So we just force the WT to re-use the same
2068
wt._branch = the_branch
2070
self.addCleanup(wt.unlock)
2073
def do_merge(self, builder, other_revision_id):
2074
wt = self.get_wt_from_builder(builder)
2075
merger = _mod_merge.Merger.from_revision_ids(
2076
wt, other_revision_id)
2077
merger.merge_type = _mod_merge.Merge3Merger
2078
return wt, merger.do_merge()
2080
def test_simple_lca(self):
2081
builder = self.get_builder()
2082
builder.build_snapshot('A-id', None,
2083
[('add', (u'', 'a-root-id', 'directory', None)),
2084
('add', (u'a', 'a-id', 'file', 'a\nb\nc\n'))])
2085
builder.build_snapshot('C-id', ['A-id'], [])
2086
builder.build_snapshot('B-id', ['A-id'], [])
2087
builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
2088
builder.build_snapshot('D-id', ['B-id', 'C-id'],
2089
[('modify', ('a-id', 'a\nb\nc\nd\ne\nf\n'))])
2090
wt, conflicts = self.do_merge(builder, 'E-id')
2091
self.assertEqual(0, conflicts)
2092
# The merge should have simply update the contents of 'a'
2093
self.assertEqual('a\nb\nc\nd\ne\nf\n', wt.get_file_text('a-id'))
2095
def test_conflict_without_lca(self):
2096
# This test would cause a merge conflict, unless we use the lca trees
2097
# to determine the real ancestry
2100
# B C Path renamed to 'bar' in B
2104
# D E Path at 'bar' in D and E
2106
# F Path at 'baz' in F, which supersedes 'bar' and 'foo'
2107
builder = self.get_builder()
2108
builder.build_snapshot('A-id', None,
2109
[('add', (u'', 'a-root-id', 'directory', None)),
2110
('add', (u'foo', 'foo-id', 'file', 'a\nb\nc\n'))])
2111
builder.build_snapshot('C-id', ['A-id'], [])
2112
builder.build_snapshot('B-id', ['A-id'],
2113
[('rename', ('foo', 'bar'))])
2114
builder.build_snapshot('E-id', ['C-id', 'B-id'], # merge the rename
2115
[('rename', ('foo', 'bar'))])
2116
builder.build_snapshot('F-id', ['E-id'],
2117
[('rename', ('bar', 'baz'))])
2118
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
2119
wt, conflicts = self.do_merge(builder, 'F-id')
2120
self.assertEqual(0, conflicts)
2121
# The merge should simply recognize that the final rename takes
2123
self.assertEqual('baz', wt.id2path('foo-id'))
2125
def test_other_deletes_lca_renames(self):
2126
# This test would cause a merge conflict, unless we use the lca trees
2127
# to determine the real ancestry
2130
# B C Path renamed to 'bar' in B
2134
# D E Path at 'bar' in D and E
2137
builder = self.get_builder()
2138
builder.build_snapshot('A-id', None,
2139
[('add', (u'', 'a-root-id', 'directory', None)),
2140
('add', (u'foo', 'foo-id', 'file', 'a\nb\nc\n'))])
2141
builder.build_snapshot('C-id', ['A-id'], [])
2142
builder.build_snapshot('B-id', ['A-id'],
2143
[('rename', ('foo', 'bar'))])
2144
builder.build_snapshot('E-id', ['C-id', 'B-id'], # merge the rename
2145
[('rename', ('foo', 'bar'))])
2146
builder.build_snapshot('F-id', ['E-id'],
2147
[('unversion', 'foo-id')])
2148
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
2149
wt, conflicts = self.do_merge(builder, 'F-id')
2150
self.assertEqual(0, conflicts)
2151
self.assertRaises(errors.NoSuchId, wt.id2path, 'foo-id')
2153
def test_executable_changes(self):
2162
# F Executable bit changed
2163
builder = self.get_builder()
2164
builder.build_snapshot('A-id', None,
2165
[('add', (u'', 'a-root-id', 'directory', None)),
2166
('add', (u'foo', 'foo-id', 'file', 'a\nb\nc\n'))])
2167
builder.build_snapshot('C-id', ['A-id'], [])
2168
builder.build_snapshot('B-id', ['A-id'], [])
2169
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
2170
builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
2171
# Have to use a real WT, because BranchBuilder doesn't support exec bit
2172
wt = self.get_wt_from_builder(builder)
2173
tt = transform.TreeTransform(wt)
2175
tt.set_executability(True, tt.trans_id_tree_file_id('foo-id'))
2180
self.assertTrue(wt.is_executable('foo-id'))
2181
wt.commit('F-id', rev_id='F-id')
2182
# Reset to D, so that we can merge F
2183
wt.set_parent_ids(['D-id'])
2184
wt.branch.set_last_revision_info(3, 'D-id')
2186
self.assertFalse(wt.is_executable('foo-id'))
2187
conflicts = wt.merge_from_branch(wt.branch, to_revision='F-id')
2188
self.assertEqual(0, conflicts)
2189
self.assertTrue(wt.is_executable('foo-id'))
2191
def test_create_symlink(self):
2192
self.requireFeature(features.SymlinkFeature)
2201
# F Add a symlink 'foo' => 'bar'
2202
# Have to use a real WT, because BranchBuilder and MemoryTree don't
2203
# have symlink support
2204
builder = self.get_builder()
2205
builder.build_snapshot('A-id', None,
2206
[('add', (u'', 'a-root-id', 'directory', None))])
2207
builder.build_snapshot('C-id', ['A-id'], [])
2208
builder.build_snapshot('B-id', ['A-id'], [])
2209
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
2210
builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
2211
# Have to use a real WT, because BranchBuilder doesn't support exec bit
2212
wt = self.get_wt_from_builder(builder)
2213
os.symlink('bar', 'path/foo')
2214
wt.add(['foo'], ['foo-id'])
2215
self.assertEqual('bar', wt.get_symlink_target('foo-id'))
2216
wt.commit('add symlink', rev_id='F-id')
2217
# Reset to D, so that we can merge F
2218
wt.set_parent_ids(['D-id'])
2219
wt.branch.set_last_revision_info(3, 'D-id')
2221
self.assertIs(None, wt.path2id('foo'))
2222
conflicts = wt.merge_from_branch(wt.branch, to_revision='F-id')
2223
self.assertEqual(0, conflicts)
2224
self.assertEqual('foo-id', wt.path2id('foo'))
2225
self.assertEqual('bar', wt.get_symlink_target('foo-id'))
2227
def test_both_sides_revert(self):
2228
# Both sides of a criss-cross revert the text to the lca
2229
# A base, introduces 'foo'
2231
# B C B modifies 'foo', C modifies 'foo'
2233
# D E D reverts to B, E reverts to C
2234
# This should conflict
2235
# This must be done with a real WorkingTree, because normally their
2236
# inventory contains "None" rather than a real sha1
2237
builder = self.get_builder()
2238
builder.build_snapshot('A-id', None,
2239
[('add', (u'', 'a-root-id', 'directory', None)),
2240
('add', (u'foo', 'foo-id', 'file', 'A content\n'))])
2241
builder.build_snapshot('B-id', ['A-id'],
2242
[('modify', ('foo-id', 'B content\n'))])
2243
builder.build_snapshot('C-id', ['A-id'],
2244
[('modify', ('foo-id', 'C content\n'))])
2245
builder.build_snapshot('E-id', ['C-id', 'B-id'], [])
2246
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
2247
wt, conflicts = self.do_merge(builder, 'E-id')
2248
self.assertEqual(1, conflicts)
2249
self.assertEqualDiff('<<<<<<< TREE\n'
2253
'>>>>>>> MERGE-SOURCE\n',
2254
wt.get_file_text('foo-id'))
2256
def test_modified_symlink(self):
2257
self.requireFeature(features.SymlinkFeature)
2258
# A Create symlink foo => bar
2260
# B C B relinks foo => baz
2264
# D E D & E have foo => baz
2266
# F F changes it to bing
2268
# Merging D & F should result in F cleanly overriding D, because D's
2269
# value actually comes from B
2271
# Have to use a real WT, because BranchBuilder and MemoryTree don't
2272
# have symlink support
2273
wt = self.make_branch_and_tree('path')
2275
self.addCleanup(wt.unlock)
2276
os.symlink('bar', 'path/foo')
2277
wt.add(['foo'], ['foo-id'])
2278
wt.commit('add symlink', rev_id='A-id')
2279
os.remove('path/foo')
2280
os.symlink('baz', 'path/foo')
2281
wt.commit('foo => baz', rev_id='B-id')
2282
wt.set_last_revision('A-id')
2283
wt.branch.set_last_revision_info(1, 'A-id')
2285
wt.commit('C', rev_id='C-id')
2286
wt.merge_from_branch(wt.branch, 'B-id')
2287
self.assertEqual('baz', wt.get_symlink_target('foo-id'))
2288
wt.commit('E merges C & B', rev_id='E-id')
2289
os.remove('path/foo')
2290
os.symlink('bing', 'path/foo')
2291
wt.commit('F foo => bing', rev_id='F-id')
2292
wt.set_last_revision('B-id')
2293
wt.branch.set_last_revision_info(2, 'B-id')
2295
wt.merge_from_branch(wt.branch, 'C-id')
2296
wt.commit('D merges B & C', rev_id='D-id')
2297
conflicts = wt.merge_from_branch(wt.branch, to_revision='F-id')
2298
self.assertEqual(0, conflicts)
2299
self.assertEqual('bing', wt.get_symlink_target('foo-id'))
2301
def test_renamed_symlink(self):
2302
self.requireFeature(features.SymlinkFeature)
2303
# A Create symlink foo => bar
2305
# B C B renames foo => barry
2309
# D E D & E have barry
2311
# F F renames barry to blah
2313
# Merging D & F should result in F cleanly overriding D, because D's
2314
# value actually comes from B
2316
wt = self.make_branch_and_tree('path')
2318
self.addCleanup(wt.unlock)
2319
os.symlink('bar', 'path/foo')
2320
wt.add(['foo'], ['foo-id'])
2321
wt.commit('A add symlink', rev_id='A-id')
2322
wt.rename_one('foo', 'barry')
2323
wt.commit('B foo => barry', rev_id='B-id')
2324
wt.set_last_revision('A-id')
2325
wt.branch.set_last_revision_info(1, 'A-id')
2327
wt.commit('C', rev_id='C-id')
2328
wt.merge_from_branch(wt.branch, 'B-id')
2329
self.assertEqual('barry', wt.id2path('foo-id'))
2330
self.assertEqual('bar', wt.get_symlink_target('foo-id'))
2331
wt.commit('E merges C & B', rev_id='E-id')
2332
wt.rename_one('barry', 'blah')
2333
wt.commit('F barry => blah', rev_id='F-id')
2334
wt.set_last_revision('B-id')
2335
wt.branch.set_last_revision_info(2, 'B-id')
2337
wt.merge_from_branch(wt.branch, 'C-id')
2338
wt.commit('D merges B & C', rev_id='D-id')
2339
self.assertEqual('barry', wt.id2path('foo-id'))
2340
# Check the output of the Merger object directly
2341
merger = _mod_merge.Merger.from_revision_ids(wt, 'F-id')
2342
merger.merge_type = _mod_merge.Merge3Merger
2343
merge_obj = merger.make_merger()
2344
root_id = wt.path2id('')
2345
entries = list(merge_obj._entries_lca())
2346
# No content change, just a path change
2347
self.assertEqual([('foo-id', False,
2348
((root_id, [root_id, root_id]), root_id, root_id),
2349
((u'foo', [u'barry', u'foo']), u'blah', u'barry'),
2350
((False, [False, False]), False, False)),
2352
conflicts = wt.merge_from_branch(wt.branch, to_revision='F-id')
2353
self.assertEqual(0, conflicts)
2354
self.assertEqual('blah', wt.id2path('foo-id'))
2356
def test_symlink_no_content_change(self):
2357
self.requireFeature(features.SymlinkFeature)
2358
# A Create symlink foo => bar
2360
# B C B relinks foo => baz
2364
# D E D & E have foo => baz
2366
# F F has foo => bing
2368
# Merging E into F should not cause a conflict, because E doesn't have
2369
# a content change relative to the LCAs (it does relative to A)
2370
wt = self.make_branch_and_tree('path')
2372
self.addCleanup(wt.unlock)
2373
os.symlink('bar', 'path/foo')
2374
wt.add(['foo'], ['foo-id'])
2375
wt.commit('add symlink', rev_id='A-id')
2376
os.remove('path/foo')
2377
os.symlink('baz', 'path/foo')
2378
wt.commit('foo => baz', rev_id='B-id')
2379
wt.set_last_revision('A-id')
2380
wt.branch.set_last_revision_info(1, 'A-id')
2382
wt.commit('C', rev_id='C-id')
2383
wt.merge_from_branch(wt.branch, 'B-id')
2384
self.assertEqual('baz', wt.get_symlink_target('foo-id'))
2385
wt.commit('E merges C & B', rev_id='E-id')
2386
wt.set_last_revision('B-id')
2387
wt.branch.set_last_revision_info(2, 'B-id')
2389
wt.merge_from_branch(wt.branch, 'C-id')
2390
wt.commit('D merges B & C', rev_id='D-id')
2391
os.remove('path/foo')
2392
os.symlink('bing', 'path/foo')
2393
wt.commit('F foo => bing', rev_id='F-id')
2395
# Check the output of the Merger object directly
2396
merger = _mod_merge.Merger.from_revision_ids(wt, 'E-id')
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(wt, 'E-id')
2447
merger.merge_type = _mod_merge.Merge3Merger
2448
merge_obj = merger.make_merger()
2449
entries = list(merge_obj._entries_lca())
2450
root_id = wt.path2id('')
2451
self.assertEqual([('foo-id', True,
2452
((None, [root_id, None]), root_id, root_id),
2453
((None, [u'foo', None]), u'foo', u'foo'),
2454
((None, [False, None]), False, False)),
2457
def test_symlink_all_wt(self):
2458
"""Check behavior if all trees are Working Trees."""
2459
self.requireFeature(features.SymlinkFeature)
2460
# The big issue is that entry.symlink_target is None for WorkingTrees.
2461
# So we need to make sure we handle that case correctly.
2464
# B C B relinks foo => baz
2466
# D E D & E have foo => baz
2468
# F F changes it to bing
2469
# Merging D & F should result in F cleanly overriding D, because D's
2470
# value actually comes from B
2472
wt = self.make_branch_and_tree('path')
2474
self.addCleanup(wt.unlock)
2475
os.symlink('bar', 'path/foo')
2476
wt.add(['foo'], ['foo-id'])
2477
wt.commit('add symlink', rev_id='A-id')
2478
os.remove('path/foo')
2479
os.symlink('baz', 'path/foo')
2480
wt.commit('foo => baz', rev_id='B-id')
2481
wt.set_last_revision('A-id')
2482
wt.branch.set_last_revision_info(1, 'A-id')
2484
wt.commit('C', rev_id='C-id')
2485
wt.merge_from_branch(wt.branch, 'B-id')
2486
self.assertEqual('baz', wt.get_symlink_target('foo-id'))
2487
wt.commit('E merges C & B', rev_id='E-id')
2488
os.remove('path/foo')
2489
os.symlink('bing', 'path/foo')
2490
wt.commit('F foo => bing', rev_id='F-id')
2491
wt.set_last_revision('B-id')
2492
wt.branch.set_last_revision_info(2, 'B-id')
2494
wt.merge_from_branch(wt.branch, 'C-id')
2495
wt.commit('D merges B & C', rev_id='D-id')
2496
wt_base = wt.controldir.sprout('base', 'A-id').open_workingtree()
2498
self.addCleanup(wt_base.unlock)
2499
wt_lca1 = wt.controldir.sprout('b-tree', 'B-id').open_workingtree()
2501
self.addCleanup(wt_lca1.unlock)
2502
wt_lca2 = wt.controldir.sprout('c-tree', 'C-id').open_workingtree()
2504
self.addCleanup(wt_lca2.unlock)
2505
wt_other = wt.controldir.sprout('other', 'F-id').open_workingtree()
2506
wt_other.lock_read()
2507
self.addCleanup(wt_other.unlock)
2508
merge_obj = _mod_merge.Merge3Merger(wt, wt, wt_base,
2509
wt_other, lca_trees=[wt_lca1, wt_lca2], do_merge=False)
2510
entries = list(merge_obj._entries_lca())
2511
root_id = wt.path2id('')
2512
self.assertEqual([('foo-id', True,
2513
((root_id, [root_id, root_id]), root_id, root_id),
2514
((u'foo', [u'foo', u'foo']), u'foo', u'foo'),
2515
((False, [False, False]), False, False)),
2518
def test_other_reverted_path_to_base(self):
2521
# B C Path at 'bar' in B
2528
builder = self.get_builder()
2529
builder.build_snapshot('A-id', None,
2530
[('add', (u'', 'a-root-id', 'directory', None)),
2531
('add', (u'foo', 'foo-id', 'file', 'a\nb\nc\n'))])
2532
builder.build_snapshot('C-id', ['A-id'], [])
2533
builder.build_snapshot('B-id', ['A-id'],
2534
[('rename', ('foo', 'bar'))])
2535
builder.build_snapshot('E-id', ['C-id', 'B-id'],
2536
[('rename', ('foo', 'bar'))]) # merge the rename
2537
builder.build_snapshot('F-id', ['E-id'],
2538
[('rename', ('bar', 'foo'))]) # Rename back to BASE
2539
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
2540
wt, conflicts = self.do_merge(builder, 'F-id')
2541
self.assertEqual(0, conflicts)
2542
self.assertEqual('foo', wt.id2path('foo-id'))
2544
def test_other_reverted_content_to_base(self):
2545
builder = self.get_builder()
2546
builder.build_snapshot('A-id', None,
2547
[('add', (u'', 'a-root-id', 'directory', None)),
2548
('add', (u'foo', 'foo-id', 'file', 'base content\n'))])
2549
builder.build_snapshot('C-id', ['A-id'], [])
2550
builder.build_snapshot('B-id', ['A-id'],
2551
[('modify', ('foo-id', 'B content\n'))])
2552
builder.build_snapshot('E-id', ['C-id', 'B-id'],
2553
[('modify', ('foo-id', 'B content\n'))]) # merge the content
2554
builder.build_snapshot('F-id', ['E-id'],
2555
[('modify', ('foo-id', 'base content\n'))]) # Revert back to BASE
2556
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
2557
wt, conflicts = self.do_merge(builder, 'F-id')
2558
self.assertEqual(0, conflicts)
2559
# TODO: We need to use the per-file graph to properly select a BASE
2560
# before this will work. Or at least use the LCA trees to find
2561
# the appropriate content base. (which is B, not A).
2562
self.assertEqual('base content\n', wt.get_file_text('foo-id'))
2564
def test_other_modified_content(self):
2565
builder = self.get_builder()
2566
builder.build_snapshot('A-id', None,
2567
[('add', (u'', 'a-root-id', 'directory', None)),
2568
('add', (u'foo', 'foo-id', 'file', 'base content\n'))])
2569
builder.build_snapshot('C-id', ['A-id'], [])
2570
builder.build_snapshot('B-id', ['A-id'],
2571
[('modify', ('foo-id', 'B content\n'))])
2572
builder.build_snapshot('E-id', ['C-id', 'B-id'],
2573
[('modify', ('foo-id', 'B content\n'))]) # merge the content
2574
builder.build_snapshot('F-id', ['E-id'],
2575
[('modify', ('foo-id', 'F content\n'))]) # Override B content
2576
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
2577
wt, conflicts = self.do_merge(builder, 'F-id')
2578
self.assertEqual(0, conflicts)
2579
self.assertEqual('F content\n', wt.get_file_text('foo-id'))
2581
def test_all_wt(self):
2582
"""Check behavior if all trees are Working Trees."""
2583
# The big issue is that entry.revision is None for WorkingTrees. (as is
2584
# entry.text_sha1, etc. So we need to make sure we handle that case
2586
# A Content of 'foo', path of 'a'
2588
# B C B modifies content, C renames 'a' => 'b'
2590
# D E E updates content, renames 'b' => 'c'
2591
builder = self.get_builder()
2592
builder.build_snapshot('A-id', None,
2593
[('add', (u'', 'a-root-id', 'directory', None)),
2594
('add', (u'a', 'a-id', 'file', 'base content\n')),
2595
('add', (u'foo', 'foo-id', 'file', 'base content\n'))])
2596
builder.build_snapshot('B-id', ['A-id'],
2597
[('modify', ('foo-id', 'B content\n'))])
2598
builder.build_snapshot('C-id', ['A-id'],
2599
[('rename', ('a', 'b'))])
2600
builder.build_snapshot('E-id', ['C-id', 'B-id'],
2601
[('rename', ('b', 'c')),
2602
('modify', ('foo-id', 'E content\n'))])
2603
builder.build_snapshot('D-id', ['B-id', 'C-id'],
2604
[('rename', ('a', 'b'))]) # merged change
2605
wt_this = self.get_wt_from_builder(builder)
2606
wt_base = wt_this.controldir.sprout('base', 'A-id').open_workingtree()
2608
self.addCleanup(wt_base.unlock)
2609
wt_lca1 = wt_this.controldir.sprout('b-tree', 'B-id').open_workingtree()
2611
self.addCleanup(wt_lca1.unlock)
2612
wt_lca2 = wt_this.controldir.sprout('c-tree', 'C-id').open_workingtree()
2614
self.addCleanup(wt_lca2.unlock)
2615
wt_other = wt_this.controldir.sprout('other', 'E-id').open_workingtree()
2616
wt_other.lock_read()
2617
self.addCleanup(wt_other.unlock)
2618
merge_obj = _mod_merge.Merge3Merger(wt_this, wt_this, wt_base,
2619
wt_other, lca_trees=[wt_lca1, wt_lca2], do_merge=False)
2620
entries = list(merge_obj._entries_lca())
2621
root_id = 'a-root-id'
2622
self.assertEqual([('a-id', False,
2623
((root_id, [root_id, root_id]), root_id, root_id),
2624
((u'a', [u'a', u'b']), u'c', u'b'),
2625
((False, [False, False]), False, False)),
2627
((root_id, [root_id, root_id]), root_id, root_id),
2628
((u'foo', [u'foo', u'foo']), u'foo', u'foo'),
2629
((False, [False, False]), False, False)),
2632
def test_nested_tree_unmodified(self):
2633
# Tested with a real WT, because BranchBuilder/MemoryTree don't handle
2635
wt = self.make_branch_and_tree('tree',
2636
format='development-subtree')
2638
self.addCleanup(wt.unlock)
2639
sub_tree = self.make_branch_and_tree('tree/sub-tree',
2640
format='development-subtree')
2641
wt.set_root_id('a-root-id')
2642
sub_tree.set_root_id('sub-tree-root')
2643
self.build_tree_contents([('tree/sub-tree/file', 'text1')])
2644
sub_tree.add('file')
2645
sub_tree.commit('foo', rev_id='sub-A-id')
2646
wt.add_reference(sub_tree)
2647
wt.commit('set text to 1', rev_id='A-id', recursive=None)
2648
# Now create a criss-cross merge in the parent, without modifying the
2650
wt.commit('B', rev_id='B-id', recursive=None)
2651
wt.set_last_revision('A-id')
2652
wt.branch.set_last_revision_info(1, 'A-id')
2653
wt.commit('C', rev_id='C-id', recursive=None)
2654
wt.merge_from_branch(wt.branch, to_revision='B-id')
2655
wt.commit('E', rev_id='E-id', recursive=None)
2656
wt.set_parent_ids(['B-id', 'C-id'])
2657
wt.branch.set_last_revision_info(2, 'B-id')
2658
wt.commit('D', rev_id='D-id', recursive=None)
2660
merger = _mod_merge.Merger.from_revision_ids(wt, 'E-id')
2661
merger.merge_type = _mod_merge.Merge3Merger
2662
merge_obj = merger.make_merger()
2663
entries = list(merge_obj._entries_lca())
2664
self.assertEqual([], entries)
2666
def test_nested_tree_subtree_modified(self):
2667
# Tested with a real WT, because BranchBuilder/MemoryTree don't handle
2669
wt = self.make_branch_and_tree('tree',
2670
format='development-subtree')
2672
self.addCleanup(wt.unlock)
2673
sub_tree = self.make_branch_and_tree('tree/sub',
2674
format='development-subtree')
2675
wt.set_root_id('a-root-id')
2676
sub_tree.set_root_id('sub-tree-root')
2677
self.build_tree_contents([('tree/sub/file', 'text1')])
2678
sub_tree.add('file')
2679
sub_tree.commit('foo', rev_id='sub-A-id')
2680
wt.add_reference(sub_tree)
2681
wt.commit('set text to 1', rev_id='A-id', recursive=None)
2682
# Now create a criss-cross merge in the parent, without modifying the
2684
wt.commit('B', rev_id='B-id', recursive=None)
2685
wt.set_last_revision('A-id')
2686
wt.branch.set_last_revision_info(1, 'A-id')
2687
wt.commit('C', rev_id='C-id', recursive=None)
2688
wt.merge_from_branch(wt.branch, to_revision='B-id')
2689
self.build_tree_contents([('tree/sub/file', 'text2')])
2690
sub_tree.commit('modify contents', rev_id='sub-B-id')
2691
wt.commit('E', rev_id='E-id', recursive=None)
2692
wt.set_parent_ids(['B-id', 'C-id'])
2693
wt.branch.set_last_revision_info(2, 'B-id')
2694
wt.commit('D', rev_id='D-id', recursive=None)
2696
merger = _mod_merge.Merger.from_revision_ids(wt, 'E-id')
2697
merger.merge_type = _mod_merge.Merge3Merger
2698
merge_obj = merger.make_merger()
2699
entries = list(merge_obj._entries_lca())
2700
# Nothing interesting about this sub-tree, because content changes are
2701
# computed at a higher level
2702
self.assertEqual([], entries)
2704
def test_nested_tree_subtree_renamed(self):
2705
# Tested with a real WT, because BranchBuilder/MemoryTree don't handle
2707
wt = self.make_branch_and_tree('tree',
2708
format='development-subtree')
2710
self.addCleanup(wt.unlock)
2711
sub_tree = self.make_branch_and_tree('tree/sub',
2712
format='development-subtree')
2713
wt.set_root_id('a-root-id')
2714
sub_tree.set_root_id('sub-tree-root')
2715
self.build_tree_contents([('tree/sub/file', 'text1')])
2716
sub_tree.add('file')
2717
sub_tree.commit('foo', rev_id='sub-A-id')
2718
wt.add_reference(sub_tree)
2719
wt.commit('set text to 1', rev_id='A-id', recursive=None)
2720
# Now create a criss-cross merge in the parent, without modifying the
2722
wt.commit('B', rev_id='B-id', recursive=None)
2723
wt.set_last_revision('A-id')
2724
wt.branch.set_last_revision_info(1, 'A-id')
2725
wt.commit('C', rev_id='C-id', recursive=None)
2726
wt.merge_from_branch(wt.branch, to_revision='B-id')
2727
wt.rename_one('sub', 'alt_sub')
2728
wt.commit('E', rev_id='E-id', recursive=None)
2729
wt.set_last_revision('B-id')
2731
wt.set_parent_ids(['B-id', 'C-id'])
2732
wt.branch.set_last_revision_info(2, 'B-id')
2733
wt.commit('D', rev_id='D-id', recursive=None)
2735
merger = _mod_merge.Merger.from_revision_ids(wt, 'E-id')
2736
merger.merge_type = _mod_merge.Merge3Merger
2737
merge_obj = merger.make_merger()
2738
entries = list(merge_obj._entries_lca())
2739
root_id = 'a-root-id'
2740
self.assertEqual([('sub-tree-root', False,
2741
((root_id, [root_id, root_id]), root_id, root_id),
2742
((u'sub', [u'sub', u'sub']), u'alt_sub', u'sub'),
2743
((False, [False, False]), False, False)),
2746
def test_nested_tree_subtree_renamed_and_modified(self):
2747
# Tested with a real WT, because BranchBuilder/MemoryTree don't handle
2749
wt = self.make_branch_and_tree('tree',
2750
format='development-subtree')
2752
self.addCleanup(wt.unlock)
2753
sub_tree = self.make_branch_and_tree('tree/sub',
2754
format='development-subtree')
2755
wt.set_root_id('a-root-id')
2756
sub_tree.set_root_id('sub-tree-root')
2757
self.build_tree_contents([('tree/sub/file', 'text1')])
2758
sub_tree.add('file')
2759
sub_tree.commit('foo', rev_id='sub-A-id')
2760
wt.add_reference(sub_tree)
2761
wt.commit('set text to 1', rev_id='A-id', recursive=None)
2762
# Now create a criss-cross merge in the parent, without modifying the
2764
wt.commit('B', rev_id='B-id', recursive=None)
2765
wt.set_last_revision('A-id')
2766
wt.branch.set_last_revision_info(1, 'A-id')
2767
wt.commit('C', rev_id='C-id', recursive=None)
2768
wt.merge_from_branch(wt.branch, to_revision='B-id')
2769
self.build_tree_contents([('tree/sub/file', 'text2')])
2770
sub_tree.commit('modify contents', rev_id='sub-B-id')
2771
wt.rename_one('sub', 'alt_sub')
2772
wt.commit('E', rev_id='E-id', recursive=None)
2773
wt.set_last_revision('B-id')
2775
wt.set_parent_ids(['B-id', 'C-id'])
2776
wt.branch.set_last_revision_info(2, 'B-id')
2777
wt.commit('D', rev_id='D-id', recursive=None)
2779
merger = _mod_merge.Merger.from_revision_ids(wt, 'E-id')
2780
merger.merge_type = _mod_merge.Merge3Merger
2781
merge_obj = merger.make_merger()
2782
entries = list(merge_obj._entries_lca())
2783
root_id = 'a-root-id'
2784
self.assertEqual([('sub-tree-root', False,
2785
((root_id, [root_id, root_id]), root_id, root_id),
2786
((u'sub', [u'sub', u'sub']), u'alt_sub', u'sub'),
2787
((False, [False, False]), False, False)),
2791
class TestLCAMultiWay(tests.TestCase):
2793
def assertLCAMultiWay(self, expected, base, lcas, other, this,
2794
allow_overriding_lca=True):
2795
self.assertEqual(expected, _mod_merge.Merge3Merger._lca_multi_way(
2796
(base, lcas), other, this,
2797
allow_overriding_lca=allow_overriding_lca))
2799
def test_other_equal_equal_lcas(self):
2800
"""Test when OTHER=LCA and all LCAs are identical."""
2801
self.assertLCAMultiWay('this',
2802
'bval', ['bval', 'bval'], 'bval', 'bval')
2803
self.assertLCAMultiWay('this',
2804
'bval', ['lcaval', 'lcaval'], 'lcaval', 'bval')
2805
self.assertLCAMultiWay('this',
2806
'bval', ['lcaval', 'lcaval', 'lcaval'], 'lcaval', 'bval')
2807
self.assertLCAMultiWay('this',
2808
'bval', ['lcaval', 'lcaval', 'lcaval'], 'lcaval', 'tval')
2809
self.assertLCAMultiWay('this',
2810
'bval', ['lcaval', 'lcaval', 'lcaval'], 'lcaval', None)
2812
def test_other_equal_this(self):
2813
"""Test when other and this are identical."""
2814
self.assertLCAMultiWay('this',
2815
'bval', ['bval', 'bval'], 'oval', 'oval')
2816
self.assertLCAMultiWay('this',
2817
'bval', ['lcaval', 'lcaval'], 'oval', 'oval')
2818
self.assertLCAMultiWay('this',
2819
'bval', ['cval', 'dval'], 'oval', 'oval')
2820
self.assertLCAMultiWay('this',
2821
'bval', [None, 'lcaval'], 'oval', 'oval')
2822
self.assertLCAMultiWay('this',
2823
None, [None, 'lcaval'], 'oval', 'oval')
2824
self.assertLCAMultiWay('this',
2825
None, ['lcaval', 'lcaval'], 'oval', 'oval')
2826
self.assertLCAMultiWay('this',
2827
None, ['cval', 'dval'], 'oval', 'oval')
2828
self.assertLCAMultiWay('this',
2829
None, ['cval', 'dval'], None, None)
2830
self.assertLCAMultiWay('this',
2831
None, ['cval', 'dval', 'eval', 'fval'], 'oval', 'oval')
2833
def test_no_lcas(self):
2834
self.assertLCAMultiWay('this',
2835
'bval', [], 'bval', 'tval')
2836
self.assertLCAMultiWay('other',
2837
'bval', [], 'oval', 'bval')
2838
self.assertLCAMultiWay('conflict',
2839
'bval', [], 'oval', 'tval')
2840
self.assertLCAMultiWay('this',
2841
'bval', [], 'oval', 'oval')
2843
def test_lca_supersedes_other_lca(self):
2844
"""If one lca == base, the other lca takes precedence"""
2845
self.assertLCAMultiWay('this',
2846
'bval', ['bval', 'lcaval'], 'lcaval', 'tval')
2847
self.assertLCAMultiWay('this',
2848
'bval', ['bval', 'lcaval'], 'lcaval', 'bval')
2849
# This is actually considered a 'revert' because the 'lcaval' in LCAS
2850
# supersedes the BASE val (in the other LCA) but then OTHER reverts it
2852
self.assertLCAMultiWay('other',
2853
'bval', ['bval', 'lcaval'], 'bval', 'lcaval')
2854
self.assertLCAMultiWay('conflict',
2855
'bval', ['bval', 'lcaval'], 'bval', 'tval')
2857
def test_other_and_this_pick_different_lca(self):
2858
# OTHER and THIS resolve the lca conflict in different ways
2859
self.assertLCAMultiWay('conflict',
2860
'bval', ['lca1val', 'lca2val'], 'lca1val', 'lca2val')
2861
self.assertLCAMultiWay('conflict',
2862
'bval', ['lca1val', 'lca2val', 'lca3val'], 'lca1val', 'lca2val')
2863
self.assertLCAMultiWay('conflict',
2864
'bval', ['lca1val', 'lca2val', 'bval'], 'lca1val', 'lca2val')
2866
def test_other_in_lca(self):
2867
# OTHER takes a value of one of the LCAs, THIS takes a new value, which
2868
# theoretically supersedes both LCA values and 'wins'
2869
self.assertLCAMultiWay('this',
2870
'bval', ['lca1val', 'lca2val'], 'lca1val', 'newval')
2871
self.assertLCAMultiWay('this',
2872
'bval', ['lca1val', 'lca2val', 'lca3val'], 'lca1val', 'newval')
2873
self.assertLCAMultiWay('conflict',
2874
'bval', ['lca1val', 'lca2val'], 'lca1val', 'newval',
2875
allow_overriding_lca=False)
2876
self.assertLCAMultiWay('conflict',
2877
'bval', ['lca1val', 'lca2val', 'lca3val'], 'lca1val', 'newval',
2878
allow_overriding_lca=False)
2879
# THIS reverted back to BASE, but that is an explicit supersede of all
2881
self.assertLCAMultiWay('this',
2882
'bval', ['lca1val', 'lca2val', 'lca3val'], 'lca1val', 'bval')
2883
self.assertLCAMultiWay('this',
2884
'bval', ['lca1val', 'lca2val', 'bval'], 'lca1val', 'bval')
2885
self.assertLCAMultiWay('conflict',
2886
'bval', ['lca1val', 'lca2val', 'lca3val'], 'lca1val', 'bval',
2887
allow_overriding_lca=False)
2888
self.assertLCAMultiWay('conflict',
2889
'bval', ['lca1val', 'lca2val', 'bval'], 'lca1val', 'bval',
2890
allow_overriding_lca=False)
2892
def test_this_in_lca(self):
2893
# THIS takes a value of one of the LCAs, OTHER takes a new value, which
2894
# theoretically supersedes both LCA values and 'wins'
2895
self.assertLCAMultiWay('other',
2896
'bval', ['lca1val', 'lca2val'], 'oval', 'lca1val')
2897
self.assertLCAMultiWay('other',
2898
'bval', ['lca1val', 'lca2val'], 'oval', 'lca2val')
2899
self.assertLCAMultiWay('conflict',
2900
'bval', ['lca1val', 'lca2val'], 'oval', 'lca1val',
2901
allow_overriding_lca=False)
2902
self.assertLCAMultiWay('conflict',
2903
'bval', ['lca1val', 'lca2val'], 'oval', 'lca2val',
2904
allow_overriding_lca=False)
2905
# OTHER reverted back to BASE, but that is an explicit supersede of all
2907
self.assertLCAMultiWay('other',
2908
'bval', ['lca1val', 'lca2val', 'lca3val'], 'bval', 'lca3val')
2909
self.assertLCAMultiWay('conflict',
2910
'bval', ['lca1val', 'lca2val', 'lca3val'], 'bval', 'lca3val',
2911
allow_overriding_lca=False)
2913
def test_all_differ(self):
2914
self.assertLCAMultiWay('conflict',
2915
'bval', ['lca1val', 'lca2val'], 'oval', 'tval')
2916
self.assertLCAMultiWay('conflict',
2917
'bval', ['lca1val', 'lca2val', 'lca2val'], 'oval', 'tval')
2918
self.assertLCAMultiWay('conflict',
2919
'bval', ['lca1val', 'lca2val', 'lca3val'], 'oval', 'tval')
2922
class TestConfigurableFileMerger(tests.TestCaseWithTransport):
2925
super(TestConfigurableFileMerger, self).setUp()
2928
def get_merger_factory(self):
2929
# Allows the inner methods to access the test attributes
2932
class FooMerger(_mod_merge.ConfigurableFileMerger):
2934
default_files = ['bar']
2936
def merge_text(self, params):
2937
calls.append('merge_text')
2938
return ('not_applicable', None)
2940
def factory(merger):
2941
result = FooMerger(merger)
2942
# Make sure we start with a clean slate
2943
self.assertEqual(None, result.affected_files)
2944
# Track the original merger
2945
self.merger = result
2950
def _install_hook(self, factory):
2951
_mod_merge.Merger.hooks.install_named_hook('merge_file_content',
2952
factory, 'test factory')
2954
def make_builder(self):
2955
builder = test_merge_core.MergeBuilder(self.test_base_dir)
2956
self.addCleanup(builder.cleanup)
2959
def make_text_conflict(self, file_name='bar'):
2960
factory = self.get_merger_factory()
2961
self._install_hook(factory)
2962
builder = self.make_builder()
2963
builder.add_file('bar-id', builder.tree_root, file_name, 'text1', True)
2964
builder.change_contents('bar-id', other='text4', this='text3')
2967
def make_kind_change(self):
2968
factory = self.get_merger_factory()
2969
self._install_hook(factory)
2970
builder = self.make_builder()
2971
builder.add_file('bar-id', builder.tree_root, 'bar', 'text1', True,
2973
builder.add_dir('bar-dir', builder.tree_root, 'bar-id',
2974
base=False, other=False)
2977
def test_uses_this_branch(self):
2978
builder = self.make_text_conflict()
2979
tt = builder.make_preview_transform()
2980
self.addCleanup(tt.finalize)
2982
def test_affected_files_cached(self):
2983
"""Ensures that the config variable is cached"""
2984
builder = self.make_text_conflict()
2985
conflicts = builder.merge()
2986
# The hook should set the variable
2987
self.assertEqual(['bar'], self.merger.affected_files)
2988
self.assertEqual(1, len(conflicts))
2990
def test_hook_called_for_text_conflicts(self):
2991
builder = self.make_text_conflict()
2992
conflicts = builder.merge()
2993
# The hook should call the merge_text() method
2994
self.assertEqual(['merge_text'], self.calls)
2996
def test_hook_not_called_for_kind_change(self):
2997
builder = self.make_kind_change()
2998
conflicts = builder.merge()
2999
# The hook should not call the merge_text() method
3000
self.assertEqual([], self.calls)
3002
def test_hook_not_called_for_other_files(self):
3003
builder = self.make_text_conflict('foobar')
3004
conflicts = builder.merge()
3005
# The hook should not call the merge_text() method
3006
self.assertEqual([], self.calls)
3009
class TestMergeIntoBase(tests.TestCaseWithTransport):
3011
def setup_simple_branch(self, relpath, shape=None, root_id=None):
3012
"""One commit, containing tree specified by optional shape.
3014
Default is empty tree (just root entry).
3017
root_id = '%s-root-id' % (relpath,)
3018
wt = self.make_branch_and_tree(relpath)
3019
wt.set_root_id(root_id)
3020
if shape is not None:
3021
adjusted_shape = [relpath + '/' + elem for elem in shape]
3022
self.build_tree(adjusted_shape)
3023
ids = ['%s-%s-id' % (relpath, basename(elem.rstrip('/')))
3025
wt.add(shape, ids=ids)
3026
rev_id = 'r1-%s' % (relpath,)
3027
wt.commit("Initial commit of %s" % (relpath,), rev_id=rev_id)
3028
self.assertEqual(root_id, wt.path2id(''))
3031
def setup_two_branches(self, custom_root_ids=True):
3032
"""Setup 2 branches, one will be a library, the other a project."""
3036
root_id = inventory.ROOT_ID
3037
project_wt = self.setup_simple_branch(
3038
'project', ['README', 'dir/', 'dir/file.c'],
3040
lib_wt = self.setup_simple_branch(
3041
'lib1', ['README', 'Makefile', 'foo.c'], root_id)
3043
return project_wt, lib_wt
3045
def do_merge_into(self, location, merge_as):
3046
"""Helper for using MergeIntoMerger.
3048
:param location: location of directory to merge from, either the
3049
location of a branch or of a path inside a branch.
3050
:param merge_as: the path in a tree to add the new directory as.
3051
:returns: the conflicts from 'do_merge'.
3053
operation = cleanup.OperationWithCleanups(self._merge_into)
3054
return operation.run(location, merge_as)
3056
def _merge_into(self, op, location, merge_as):
3057
# Open and lock the various tree and branch objects
3058
wt, subdir_relpath = WorkingTree.open_containing(merge_as)
3059
op.add_cleanup(wt.lock_write().unlock)
3060
branch_to_merge, subdir_to_merge = _mod_branch.Branch.open_containing(
3062
op.add_cleanup(branch_to_merge.lock_read().unlock)
3063
other_tree = branch_to_merge.basis_tree()
3064
op.add_cleanup(other_tree.lock_read().unlock)
3066
merger = _mod_merge.MergeIntoMerger(this_tree=wt, other_tree=other_tree,
3067
other_branch=branch_to_merge, target_subdir=subdir_relpath,
3068
source_subpath=subdir_to_merge)
3069
merger.set_base_revision(_mod_revision.NULL_REVISION, branch_to_merge)
3070
conflicts = merger.do_merge()
3071
merger.set_pending()
3074
def assertTreeEntriesEqual(self, expected_entries, tree):
3075
"""Assert that 'tree' contains the expected inventory entries.
3077
:param expected_entries: sequence of (path, file-id) pairs.
3079
files = [(path, ie.file_id) for path, ie in tree.iter_entries_by_dir()]
3080
self.assertEqual(expected_entries, files)
3083
class TestMergeInto(TestMergeIntoBase):
3085
def test_newdir_with_unique_roots(self):
3086
"""Merge a branch with a unique root into a new directory."""
3087
project_wt, lib_wt = self.setup_two_branches()
3088
self.do_merge_into('lib1', 'project/lib1')
3089
project_wt.lock_read()
3090
self.addCleanup(project_wt.unlock)
3091
# The r1-lib1 revision should be merged into this one
3092
self.assertEqual(['r1-project', 'r1-lib1'], project_wt.get_parent_ids())
3093
self.assertTreeEntriesEqual(
3094
[('', 'project-root-id'),
3095
('README', 'project-README-id'),
3096
('dir', 'project-dir-id'),
3097
('lib1', 'lib1-root-id'),
3098
('dir/file.c', 'project-file.c-id'),
3099
('lib1/Makefile', 'lib1-Makefile-id'),
3100
('lib1/README', 'lib1-README-id'),
3101
('lib1/foo.c', 'lib1-foo.c-id'),
3104
def test_subdir(self):
3105
"""Merge a branch into a subdirectory of an existing directory."""
3106
project_wt, lib_wt = self.setup_two_branches()
3107
self.do_merge_into('lib1', 'project/dir/lib1')
3108
project_wt.lock_read()
3109
self.addCleanup(project_wt.unlock)
3110
# The r1-lib1 revision should be merged into this one
3111
self.assertEqual(['r1-project', 'r1-lib1'], project_wt.get_parent_ids())
3112
self.assertTreeEntriesEqual(
3113
[('', 'project-root-id'),
3114
('README', 'project-README-id'),
3115
('dir', 'project-dir-id'),
3116
('dir/file.c', 'project-file.c-id'),
3117
('dir/lib1', 'lib1-root-id'),
3118
('dir/lib1/Makefile', 'lib1-Makefile-id'),
3119
('dir/lib1/README', 'lib1-README-id'),
3120
('dir/lib1/foo.c', 'lib1-foo.c-id'),
3123
def test_newdir_with_repeat_roots(self):
3124
"""If the file-id of the dir to be merged already exists a new ID will
3125
be allocated to let the merge happen.
3127
project_wt, lib_wt = self.setup_two_branches(custom_root_ids=False)
3128
root_id = project_wt.path2id('')
3129
self.do_merge_into('lib1', 'project/lib1')
3130
project_wt.lock_read()
3131
self.addCleanup(project_wt.unlock)
3132
# The r1-lib1 revision should be merged into this one
3133
self.assertEqual(['r1-project', 'r1-lib1'], project_wt.get_parent_ids())
3134
new_lib1_id = project_wt.path2id('lib1')
3135
self.assertNotEqual(None, new_lib1_id)
3136
self.assertTreeEntriesEqual(
3138
('README', 'project-README-id'),
3139
('dir', 'project-dir-id'),
3140
('lib1', new_lib1_id),
3141
('dir/file.c', 'project-file.c-id'),
3142
('lib1/Makefile', 'lib1-Makefile-id'),
3143
('lib1/README', 'lib1-README-id'),
3144
('lib1/foo.c', 'lib1-foo.c-id'),
3147
def test_name_conflict(self):
3148
"""When the target directory name already exists a conflict is
3149
generated and the original directory is renamed to foo.moved.
3151
dest_wt = self.setup_simple_branch('dest', ['dir/', 'dir/file.txt'])
3152
src_wt = self.setup_simple_branch('src', ['README'])
3153
conflicts = self.do_merge_into('src', 'dest/dir')
3154
self.assertEqual(1, conflicts)
3156
self.addCleanup(dest_wt.unlock)
3157
# The r1-lib1 revision should be merged into this one
3158
self.assertEqual(['r1-dest', 'r1-src'], dest_wt.get_parent_ids())
3159
self.assertTreeEntriesEqual(
3160
[('', 'dest-root-id'),
3161
('dir', 'src-root-id'),
3162
('dir.moved', 'dest-dir-id'),
3163
('dir/README', 'src-README-id'),
3164
('dir.moved/file.txt', 'dest-file.txt-id'),
3167
def test_file_id_conflict(self):
3168
"""A conflict is generated if the merge-into adds a file (or other
3169
inventory entry) with a file-id that already exists in the target tree.
3171
dest_wt = self.setup_simple_branch('dest', ['file.txt'])
3172
# Make a second tree with a file-id that will clash with file.txt in
3174
src_wt = self.make_branch_and_tree('src')
3175
self.build_tree(['src/README'])
3176
src_wt.add(['README'], ids=['dest-file.txt-id'])
3177
src_wt.commit("Rev 1 of src.", rev_id='r1-src')
3178
conflicts = self.do_merge_into('src', 'dest/dir')
3179
# This is an edge case that shouldn't happen to users very often. So
3180
# we don't care really about the exact presentation of the conflict,
3181
# just that there is one.
3182
self.assertEqual(1, conflicts)
3184
def test_only_subdir(self):
3185
"""When the location points to just part of a tree, merge just that
3188
dest_wt = self.setup_simple_branch('dest')
3189
src_wt = self.setup_simple_branch(
3190
'src', ['hello.txt', 'dir/', 'dir/foo.c'])
3191
conflicts = self.do_merge_into('src/dir', 'dest/dir')
3193
self.addCleanup(dest_wt.unlock)
3194
# The r1-lib1 revision should NOT be merged into this one (this is a
3196
self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3197
self.assertTreeEntriesEqual(
3198
[('', 'dest-root-id'),
3199
('dir', 'src-dir-id'),
3200
('dir/foo.c', 'src-foo.c-id'),
3203
def test_only_file(self):
3204
"""An edge case: merge just one file, not a whole dir."""
3205
dest_wt = self.setup_simple_branch('dest')
3206
two_file_wt = self.setup_simple_branch(
3207
'two-file', ['file1.txt', 'file2.txt'])
3208
conflicts = self.do_merge_into('two-file/file1.txt', 'dest/file1.txt')
3210
self.addCleanup(dest_wt.unlock)
3211
# The r1-lib1 revision should NOT be merged into this one
3212
self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3213
self.assertTreeEntriesEqual(
3214
[('', 'dest-root-id'), ('file1.txt', 'two-file-file1.txt-id')],
3217
def test_no_such_source_path(self):
3218
"""PathNotInTree is raised if the specified path in the source tree
3221
dest_wt = self.setup_simple_branch('dest')
3222
two_file_wt = self.setup_simple_branch('src', ['dir/'])
3223
self.assertRaises(_mod_merge.PathNotInTree, self.do_merge_into,
3224
'src/no-such-dir', 'dest/foo')
3226
self.addCleanup(dest_wt.unlock)
3227
# The dest tree is unmodified.
3228
self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3229
self.assertTreeEntriesEqual([('', 'dest-root-id')], dest_wt)
3231
def test_no_such_target_path(self):
3232
"""PathNotInTree is also raised if the specified path in the target
3233
tree does not exist.
3235
dest_wt = self.setup_simple_branch('dest')
3236
two_file_wt = self.setup_simple_branch('src', ['file.txt'])
3237
self.assertRaises(_mod_merge.PathNotInTree, self.do_merge_into,
3238
'src', 'dest/no-such-dir/foo')
3240
self.addCleanup(dest_wt.unlock)
3241
# The dest tree is unmodified.
3242
self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3243
self.assertTreeEntriesEqual([('', 'dest-root-id')], dest_wt)
3246
class TestMergeHooks(TestCaseWithTransport):
3249
super(TestMergeHooks, self).setUp()
3250
self.tree_a = self.make_branch_and_tree('tree_a')
3251
self.build_tree_contents([('tree_a/file', 'content_1')])
3252
self.tree_a.add('file', 'file-id')
3253
self.tree_a.commit('added file')
3255
self.tree_b = self.tree_a.controldir.sprout('tree_b').open_workingtree()
3256
self.build_tree_contents([('tree_b/file', 'content_2')])
3257
self.tree_b.commit('modify file')
3259
def test_pre_merge_hook_inject_different_tree(self):
3260
tree_c = self.tree_b.controldir.sprout('tree_c').open_workingtree()
3261
self.build_tree_contents([('tree_c/file', 'content_3')])
3262
tree_c.commit("more content")
3264
def factory(merger):
3265
self.assertIsInstance(merger, _mod_merge.Merge3Merger)
3266
merger.other_tree = tree_c
3267
calls.append(merger)
3268
_mod_merge.Merger.hooks.install_named_hook('pre_merge',
3269
factory, 'test factory')
3270
self.tree_a.merge_from_branch(self.tree_b.branch)
3272
self.assertFileEqual("content_3", 'tree_a/file')
3273
self.assertLength(1, calls)
3275
def test_post_merge_hook_called(self):
3277
def factory(merger):
3278
self.assertIsInstance(merger, _mod_merge.Merge3Merger)
3279
calls.append(merger)
3280
_mod_merge.Merger.hooks.install_named_hook('post_merge',
3281
factory, 'test factory')
3283
self.tree_a.merge_from_branch(self.tree_b.branch)
3285
self.assertFileEqual("content_2", 'tree_a/file')
3286
self.assertLength(1, calls)