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'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'b-id'),
219
conflicts.UnversionedParent('Versioned directory', 'b', b'b-id')],
221
merge_inner(tree_a.branch, tree_z.basis_tree(), base_tree,
224
conflicts.DeletingParent('Not deleting', 'b', b'b-id'),
225
conflicts.UnversionedParent('Versioned directory', 'b', 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(b'sub-tree-root')
236
self.build_tree_contents([('tree/sub-tree/file', b'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', b'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', b'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', b'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', b'content_1')])
271
tree_a.add('file', b'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', b'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',
286
file_id=b'file-id')])
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=b'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', b"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', b"a\nb\n")])
367
other_tree.commit('rev2b', rev_id=b'rev2b')
368
self.build_tree_contents([('other/file', b"c\na\nb\n")])
369
other_tree.commit('rev3b', rev_id=b'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', b"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', b"a\nb\n")])
403
other_tree.commit('rev2b', rev_id=b'rev2b')
404
self.build_tree_contents([('other/file', b"a\nb\nc\n")])
405
other_tree.commit('rev3b', rev_id=b'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=b'rev1')
442
other_tree = this_tree.controldir.sprout('other').open_workingtree()
443
this_tree.commit('rev2', rev_id=b'rev2a')
444
other_tree.commit('rev2', rev_id=b'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', b'1\n')])
461
this_tree.add('file', b'file-id')
462
this_tree.commit('rev1', rev_id=b'rev1')
463
other_tree = this_tree.controldir.sprout('other').open_workingtree()
464
self.build_tree_contents([('this/file', b'1\n2a\n')])
465
this_tree.commit('rev2', rev_id=b'rev2a')
466
self.build_tree_contents([('other/file', b'2b\n1\n')])
467
other_tree.commit('rev2', rev_id=b'rev2b')
468
this_tree.lock_write()
469
self.addCleanup(this_tree.unlock)
470
merger = _mod_merge.Merger.from_revision_ids(
471
this_tree, b'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')
479
self.assertEqual('1\n2a\n', tree_file.read())
482
preview_file = preview_tree.get_file('file')
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', b'1\n')])
491
this_tree.add('file', b'file-id')
492
this_tree.commit('rev1', rev_id=b'rev1')
493
other_tree = this_tree.controldir.sprout('other').open_workingtree()
494
self.build_tree_contents([('this/file', b'1\n2a\n')])
495
this_tree.commit('rev2', rev_id=b'rev2a')
496
self.build_tree_contents([('other/file', b'2b\n1\n')])
497
other_tree.commit('rev2', rev_id=b'rev2b')
498
this_tree.lock_write()
499
self.addCleanup(this_tree.unlock)
500
merger = _mod_merge.Merger.from_revision_ids(
501
this_tree, b'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')
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({''}, set(tree.all_versioned_paths()))
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', b'foo-id')
533
source.commit('Add foo')
534
target = source.controldir.sprout('target').open_workingtree()
535
subtree = target.extract('foo', b'foo-id')
536
subtree.commit('Delete root')
537
self.build_tree(['source/bar'])
538
source.add('bar', b'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(None,
1247
[('add', ('', None, 'directory', None))],
1248
revision_id='A-id' )
1249
builder.build_snapshot(['A-id'], [], revision_id='C-id')
1250
builder.build_snapshot(['A-id'], [], revision_id='B-id')
1253
def setup_criss_cross_graph(self):
1254
"""Create a 5-node graph with a criss-cross.
1256
:return: A BranchBuilder
1263
builder = self.setup_simple_graph()
1264
builder.build_snapshot(['C-id', 'B-id'], [], revision_id='E-id')
1265
builder.build_snapshot(['B-id', 'C-id'], [], revision_id='D-id')
1268
def make_Merger(self, builder, other_revision_id, interesting_files=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
merger.merge_type = _mod_merge.Merge3Merger
1280
class TestMergerInMemory(TestMergerBase):
1282
def test_cache_trees_with_revision_ids_None(self):
1283
merger = self.make_Merger(self.setup_simple_graph(), 'C-id')
1284
original_cache = dict(merger._cached_trees)
1285
merger.cache_trees_with_revision_ids([None])
1286
self.assertEqual(original_cache, merger._cached_trees)
1288
def test_cache_trees_with_revision_ids_no_revision_id(self):
1289
merger = self.make_Merger(self.setup_simple_graph(), 'C-id')
1290
original_cache = dict(merger._cached_trees)
1291
tree = self.make_branch_and_memory_tree('tree')
1292
merger.cache_trees_with_revision_ids([tree])
1293
self.assertEqual(original_cache, merger._cached_trees)
1295
def test_cache_trees_with_revision_ids_having_revision_id(self):
1296
merger = self.make_Merger(self.setup_simple_graph(), 'C-id')
1297
original_cache = dict(merger._cached_trees)
1298
tree = merger.this_branch.repository.revision_tree('B-id')
1299
original_cache['B-id'] = tree
1300
merger.cache_trees_with_revision_ids([tree])
1301
self.assertEqual(original_cache, merger._cached_trees)
1303
def test_find_base(self):
1304
merger = self.make_Merger(self.setup_simple_graph(), 'C-id')
1305
self.assertEqual('A-id', merger.base_rev_id)
1306
self.assertFalse(merger._is_criss_cross)
1307
self.assertIs(None, merger._lca_trees)
1309
def test_find_base_criss_cross(self):
1310
builder = self.setup_criss_cross_graph()
1311
merger = self.make_Merger(builder, 'E-id')
1312
self.assertEqual('A-id', merger.base_rev_id)
1313
self.assertTrue(merger._is_criss_cross)
1314
self.assertEqual(['B-id', 'C-id'], [t.get_revision_id()
1315
for t in merger._lca_trees])
1316
# If we swap the order, we should get a different lca order
1317
builder.build_snapshot(['E-id'], [], revision_id='F-id')
1318
merger = self.make_Merger(builder, 'D-id')
1319
self.assertEqual(['C-id', 'B-id'], [t.get_revision_id()
1320
for t in merger._lca_trees])
1322
def test_find_base_triple_criss_cross(self):
1325
# B C F # F is merged into both branches
1332
builder = self.setup_criss_cross_graph()
1333
builder.build_snapshot(['A-id'], [], revision_id='F-id')
1334
builder.build_snapshot(['E-id', 'F-id'], [], revision_id='H-id')
1335
builder.build_snapshot(['D-id', 'F-id'], [], revision_id='G-id')
1336
merger = self.make_Merger(builder, 'H-id')
1337
self.assertEqual(['B-id', 'C-id', 'F-id'],
1338
[t.get_revision_id() for t in merger._lca_trees])
1340
def test_find_base_new_root_criss_cross(self):
1346
builder = self.get_builder()
1347
builder.build_snapshot(None,
1348
[('add', ('', None, 'directory', None))],
1350
builder.build_snapshot([],
1351
[('add', ('', None, 'directory', None))],
1353
builder.build_snapshot(['A-id', 'B-id'], [], revision_id='D-id')
1354
builder.build_snapshot(['A-id', 'B-id'], [], revision_id='C-id')
1355
merger = self.make_Merger(builder, 'D-id')
1356
self.assertEqual('A-id', merger.base_rev_id)
1357
self.assertTrue(merger._is_criss_cross)
1358
self.assertEqual(['A-id', 'B-id'], [t.get_revision_id()
1359
for t in merger._lca_trees])
1361
def test_no_criss_cross_passed_to_merge_type(self):
1362
class LCATreesMerger(LoggingMerger):
1363
supports_lca_trees = True
1365
merger = self.make_Merger(self.setup_simple_graph(), 'C-id')
1366
merger.merge_type = LCATreesMerger
1367
merge_obj = merger.make_merger()
1368
self.assertIsInstance(merge_obj, LCATreesMerger)
1369
self.assertFalse('lca_trees' in merge_obj.kwargs)
1371
def test_criss_cross_passed_to_merge_type(self):
1372
merger = self.make_Merger(self.setup_criss_cross_graph(), 'E-id')
1373
merger.merge_type = _mod_merge.Merge3Merger
1374
merge_obj = merger.make_merger()
1375
self.assertEqual(['B-id', 'C-id'], [t.get_revision_id()
1376
for t in merger._lca_trees])
1378
def test_criss_cross_not_supported_merge_type(self):
1379
merger = self.make_Merger(self.setup_criss_cross_graph(), 'E-id')
1380
# We explicitly do not define supports_lca_trees
1381
merger.merge_type = LoggingMerger
1382
merge_obj = merger.make_merger()
1383
self.assertIsInstance(merge_obj, LoggingMerger)
1384
self.assertFalse('lca_trees' in merge_obj.kwargs)
1386
def test_criss_cross_unsupported_merge_type(self):
1387
class UnsupportedLCATreesMerger(LoggingMerger):
1388
supports_lca_trees = False
1390
merger = self.make_Merger(self.setup_criss_cross_graph(), 'E-id')
1391
merger.merge_type = UnsupportedLCATreesMerger
1392
merge_obj = merger.make_merger()
1393
self.assertIsInstance(merge_obj, UnsupportedLCATreesMerger)
1394
self.assertFalse('lca_trees' in merge_obj.kwargs)
1397
class TestMergerEntriesLCA(TestMergerBase):
1399
def make_merge_obj(self, builder, other_revision_id,
1400
interesting_files=None):
1401
merger = self.make_Merger(builder, other_revision_id,
1402
interesting_files=interesting_files)
1403
return merger.make_merger()
1405
def test_simple(self):
1406
builder = self.get_builder()
1407
builder.build_snapshot(None,
1408
[('add', (u'', 'a-root-id', 'directory', None)),
1409
('add', (u'a', 'a-id', 'file', 'a\nb\nc\n'))],
1411
builder.build_snapshot(['A-id'],
1412
[('modify', ('a', 'a\nb\nC\nc\n'))],
1414
builder.build_snapshot(['A-id'],
1415
[('modify', ('a', 'a\nB\nb\nc\n'))],
1417
builder.build_snapshot(['C-id', 'B-id'],
1418
[('modify', ('a', 'a\nB\nb\nC\nc\nE\n'))],
1420
builder.build_snapshot(['B-id', 'C-id'],
1421
[('modify', ('a', 'a\nB\nb\nC\nc\n'))],
1422
revision_id='D-id', )
1423
merge_obj = self.make_merge_obj(builder, 'E-id')
1425
self.assertEqual(['B-id', 'C-id'], [t.get_revision_id()
1426
for t in merge_obj._lca_trees])
1427
self.assertEqual('A-id', merge_obj.base_tree.get_revision_id())
1428
entries = list(merge_obj._entries_lca())
1430
# (file_id, changed, parents, names, executable)
1431
# BASE, lca1, lca2, OTHER, THIS
1432
root_id = 'a-root-id'
1433
self.assertEqual([('a-id', True,
1434
((u'a', [u'a', u'a']), u'a', u'a'),
1435
((root_id, [root_id, root_id]), root_id, root_id),
1436
((u'a', [u'a', u'a']), u'a', u'a'),
1437
((False, [False, False]), False, False)),
1440
def test_not_in_base(self):
1441
# LCAs all have the same last-modified revision for the file, as do
1442
# the tips, but the base has something different
1443
# A base, doesn't have the file
1445
# B C B introduces 'foo', C introduces 'bar'
1447
# D E D and E now both have 'foo' and 'bar'
1449
# F G the files are now in F, G, D and E, but not in A
1452
builder = self.get_builder()
1453
builder.build_snapshot(None,
1454
[('add', (u'', 'a-root-id', 'directory', None))],
1456
builder.build_snapshot(['A-id'],
1457
[('add', (u'foo', 'foo-id', 'file', 'a\nb\nc\n'))],
1459
builder.build_snapshot(['A-id'],
1460
[('add', (u'bar', 'bar-id', 'file', 'd\ne\nf\n'))],
1462
builder.build_snapshot(['B-id', 'C-id'],
1463
[('add', (u'bar', 'bar-id', 'file', 'd\ne\nf\n'))],
1465
builder.build_snapshot(['C-id', 'B-id'],
1466
[('add', (u'foo', 'foo-id', 'file', 'a\nb\nc\n'))],
1468
builder.build_snapshot(['E-id', 'D-id'],
1469
[('modify', (u'bar', 'd\ne\nf\nG\n'))],
1471
builder.build_snapshot(['D-id', 'E-id'], [], revision_id='F-id')
1472
merge_obj = self.make_merge_obj(builder, 'G-id')
1474
self.assertEqual(['D-id', 'E-id'], [t.get_revision_id()
1475
for t in merge_obj._lca_trees])
1476
self.assertEqual('A-id', merge_obj.base_tree.get_revision_id())
1477
entries = list(merge_obj._entries_lca())
1478
root_id = 'a-root-id'
1479
self.assertEqual([('bar-id', True,
1480
((None, [u'bar', u'bar']), u'bar', u'bar'),
1481
((None, [root_id, root_id]), root_id, root_id),
1482
((None, [u'bar', u'bar']), u'bar', u'bar'),
1483
((None, [False, False]), False, False)),
1486
def test_not_in_this(self):
1487
builder = self.get_builder()
1488
builder.build_snapshot(None,
1489
[('add', (u'', 'a-root-id', 'directory', None)),
1490
('add', (u'a', 'a-id', 'file', 'a\nb\nc\n'))],
1492
builder.build_snapshot(['A-id'],
1493
[('modify', ('a', 'a\nB\nb\nc\n'))],
1495
builder.build_snapshot(['A-id'],
1496
[('modify', ('a', 'a\nb\nC\nc\n'))],
1498
builder.build_snapshot(['C-id', 'B-id'],
1499
[('modify', ('a', 'a\nB\nb\nC\nc\nE\n'))],
1501
builder.build_snapshot(['B-id', 'C-id'],
1502
[('unversion', 'a')],
1504
merge_obj = self.make_merge_obj(builder, 'E-id')
1506
self.assertEqual(['B-id', 'C-id'], [t.get_revision_id()
1507
for t in merge_obj._lca_trees])
1508
self.assertEqual('A-id', merge_obj.base_tree.get_revision_id())
1510
entries = list(merge_obj._entries_lca())
1511
root_id = 'a-root-id'
1512
self.assertEqual([('a-id', True,
1513
((u'a', [u'a', u'a']), u'a', None),
1514
((root_id, [root_id, root_id]), root_id, None),
1515
((u'a', [u'a', u'a']), u'a', None),
1516
((False, [False, False]), False, None)),
1519
def test_file_not_in_one_lca(self):
1522
# B C # B no file, C introduces a file
1524
# D E # D and E both have the file, unchanged from C
1525
builder = self.get_builder()
1526
builder.build_snapshot(None,
1527
[('add', (u'', 'a-root-id', 'directory', None))],
1529
builder.build_snapshot(['A-id'], [], revision_id='B-id')
1530
builder.build_snapshot(['A-id'],
1531
[('add', (u'a', 'a-id', 'file', 'a\nb\nc\n'))],
1533
builder.build_snapshot(['C-id', 'B-id'],
1534
[], revision_id='E-id') # Inherited from C
1535
builder.build_snapshot(['B-id', 'C-id'], # Merged from C
1536
[('add', (u'a', 'a-id', 'file', 'a\nb\nc\n'))],
1538
merge_obj = self.make_merge_obj(builder, 'E-id')
1540
self.assertEqual(['B-id', 'C-id'], [t.get_revision_id()
1541
for t in merge_obj._lca_trees])
1542
self.assertEqual('A-id', merge_obj.base_tree.get_revision_id())
1544
entries = list(merge_obj._entries_lca())
1545
self.assertEqual([], entries)
1547
def test_not_in_other(self):
1548
builder = self.get_builder()
1549
builder.build_snapshot(None,
1550
[('add', (u'', 'a-root-id', 'directory', None)),
1551
('add', (u'a', 'a-id', 'file', 'a\nb\nc\n'))],
1553
builder.build_snapshot(['A-id'], [], revision_id='B-id')
1554
builder.build_snapshot(['A-id'], [], revision_id='C-id')
1555
builder.build_snapshot(
1557
[('unversion', 'a')], revision_id='E-id')
1558
builder.build_snapshot(['B-id', 'C-id'], [], revision_id='D-id')
1559
merge_obj = self.make_merge_obj(builder, 'E-id')
1561
entries = list(merge_obj._entries_lca())
1562
root_id = 'a-root-id'
1563
self.assertEqual([('a-id', True,
1564
((u'a', [u'a', u'a']), None, u'a'),
1565
((root_id, [root_id, root_id]), None, root_id),
1566
((u'a', [u'a', u'a']), None, u'a'),
1567
((False, [False, False]), None, False)),
1570
def test_not_in_other_or_lca(self):
1571
# A base, introduces 'foo'
1573
# B C B nothing, C deletes foo
1575
# D E D restores foo (same as B), E leaves it deleted
1577
# A => B, no changes
1578
# A => C, delete foo (C should supersede B)
1579
# C => D, restore foo
1580
# C => E, no changes
1581
# D would then win 'cleanly' and no record would be given
1582
builder = self.get_builder()
1583
builder.build_snapshot(None,
1584
[('add', (u'', 'a-root-id', 'directory', None)),
1585
('add', (u'foo', 'foo-id', 'file', 'content\n'))],
1587
builder.build_snapshot(['A-id'], [], revision_id='B-id')
1588
builder.build_snapshot(['A-id'],
1589
[('unversion', 'foo')], revision_id='C-id')
1590
builder.build_snapshot(['C-id', 'B-id'], [], revision_id='E-id')
1591
builder.build_snapshot(['B-id', 'C-id'], [], revision_id='D-id')
1592
merge_obj = self.make_merge_obj(builder, 'E-id')
1594
entries = list(merge_obj._entries_lca())
1595
self.assertEqual([], entries)
1597
def test_not_in_other_mod_in_lca1_not_in_lca2(self):
1598
# A base, introduces 'foo'
1600
# B C B changes 'foo', C deletes foo
1602
# D E D restores foo (same as B), E leaves it deleted (as C)
1604
# A => B, modified foo
1605
# A => C, delete foo, C does not supersede B
1606
# B => D, no changes
1607
# C => D, resolve in favor of B
1608
# B => E, resolve in favor of E
1609
# C => E, no changes
1610
# In this case, we have a conflict of how the changes were resolved. E
1611
# picked C and D picked B, so we should issue a conflict
1612
builder = self.get_builder()
1613
builder.build_snapshot(None,
1614
[('add', (u'', 'a-root-id', 'directory', None)),
1615
('add', (u'foo', 'foo-id', 'file', 'content\n'))],
1617
builder.build_snapshot(['A-id'], [
1618
('modify', ('foo', 'new-content\n'))],
1620
builder.build_snapshot(['A-id'],
1621
[('unversion', 'foo')],
1623
builder.build_snapshot(['C-id', 'B-id'], [], revision_id='E-id')
1624
builder.build_snapshot(['B-id', 'C-id'], [], revision_id='D-id')
1625
merge_obj = self.make_merge_obj(builder, 'E-id')
1627
entries = list(merge_obj._entries_lca())
1628
root_id = 'a-root-id'
1629
self.assertEqual([('foo-id', True,
1630
((u'foo', [u'foo', None]), None, u'foo'),
1631
((root_id, [root_id, None]), None, root_id),
1632
((u'foo', [u'foo', None]), None, 'foo'),
1633
((False, [False, None]), None, False)),
1636
def test_only_in_one_lca(self):
1639
# B C B nothing, C add file
1641
# D E D still has nothing, E removes file
1644
# C => D, removed the file
1646
# C => E, removed the file
1647
# Thus D & E have identical changes, and this is a no-op
1650
# A => C, add file, thus C supersedes B
1651
# w/ C=BASE, D=THIS, E=OTHER we have 'happy convergence'
1652
builder = self.get_builder()
1653
builder.build_snapshot(None,
1654
[('add', (u'', 'a-root-id', 'directory', None))],
1656
builder.build_snapshot(['A-id'], [], revision_id='B-id')
1657
builder.build_snapshot(['A-id'],
1658
[('add', (u'a', 'a-id', 'file', 'a\nb\nc\n'))],
1660
builder.build_snapshot(['C-id', 'B-id'],
1661
[('unversion', 'a')],
1663
builder.build_snapshot(['B-id', 'C-id'], [], revision_id='D-id')
1664
merge_obj = self.make_merge_obj(builder, 'E-id')
1666
entries = list(merge_obj._entries_lca())
1667
self.assertEqual([], entries)
1669
def test_only_in_other(self):
1670
builder = self.get_builder()
1671
builder.build_snapshot(None,
1672
[('add', (u'', 'a-root-id', 'directory', None))],
1674
builder.build_snapshot(['A-id'], [], revision_id='B-id')
1675
builder.build_snapshot(['A-id'], [], revision_id='C-id')
1676
builder.build_snapshot(['C-id', 'B-id'],
1677
[('add', (u'a', 'a-id', 'file', 'a\nb\nc\n'))],
1679
builder.build_snapshot(['B-id', 'C-id'], [], revision_id='D-id')
1680
merge_obj = self.make_merge_obj(builder, 'E-id')
1682
entries = list(merge_obj._entries_lca())
1683
root_id = 'a-root-id'
1684
self.assertEqual([('a-id', True,
1685
((None, [None, None]), u'a', None),
1686
((None, [None, None]), root_id, None),
1687
((None, [None, None]), u'a', None),
1688
((None, [None, None]), False, None)),
1691
def test_one_lca_supersedes(self):
1692
# One LCA supersedes the other LCAs last modified value, but the
1693
# value is not the same as BASE.
1694
# A base, introduces 'foo', last mod A
1696
# B C B modifies 'foo' (mod B), C does nothing (mod A)
1698
# D E D does nothing (mod B), E updates 'foo' (mod E)
1700
# F G F updates 'foo' (mod F). G does nothing (mod E)
1702
# At this point, G should not be considered to modify 'foo', even
1703
# though its LCAs disagree. This is because the modification in E
1704
# completely supersedes the value in D.
1705
builder = self.get_builder()
1706
builder.build_snapshot(None,
1707
[('add', (u'', 'a-root-id', 'directory', None)),
1708
('add', (u'foo', 'foo-id', 'file', 'A content\n'))],
1710
builder.build_snapshot(['A-id'], [], revision_id='C-id')
1711
builder.build_snapshot(['A-id'],
1712
[('modify', ('foo', 'B content\n'))],
1714
builder.build_snapshot(['B-id', 'C-id'], [], revision_id='D-id')
1715
builder.build_snapshot(['C-id', 'B-id'],
1716
[('modify', ('foo', 'E content\n'))],
1718
builder.build_snapshot(['E-id', 'D-id'], [], revision_id='G-id')
1719
builder.build_snapshot(['D-id', 'E-id'],
1720
[('modify', ('foo', 'F content\n'))],
1722
merge_obj = self.make_merge_obj(builder, 'G-id')
1724
self.assertEqual([], list(merge_obj._entries_lca()))
1726
def test_one_lca_supersedes_path(self):
1727
# Double-criss-cross merge, the ultimate base value is different from
1731
# B C B value 'bar', C = 'foo'
1733
# D E D = 'bar', E supersedes to 'bing'
1735
# F G F = 'bing', G supersedes to 'barry'
1737
# In this case, we technically should not care about the value 'bar' for
1738
# D, because it was clearly superseded by E's 'bing'. The
1739
# per-file/attribute graph would actually look like:
1748
# Because the other side of the merge never modifies the value, it just
1749
# takes the value from the merge.
1751
# ATM this fails because we will prune 'foo' from the LCAs, but we
1752
# won't prune 'bar'. This is getting far off into edge-case land, so we
1753
# aren't supporting it yet.
1755
builder = self.get_builder()
1756
builder.build_snapshot(None,
1757
[('add', (u'', 'a-root-id', 'directory', None)),
1758
('add', (u'foo', 'foo-id', 'file', 'A content\n'))],
1760
builder.build_snapshot(['A-id'], [], revision_id='C-id')
1761
builder.build_snapshot(['A-id'],
1762
[('rename', ('foo', 'bar'))],
1764
builder.build_snapshot(['B-id', 'C-id'], [], revision_id='D-id')
1765
builder.build_snapshot(['C-id', 'B-id'],
1766
[('rename', ('foo', 'bing'))],
1767
revision_id='E-id') # override to bing
1768
builder.build_snapshot(['E-id', 'D-id'],
1769
[('rename', ('bing', 'barry'))],
1770
revision_id='G-id') # override to barry
1771
builder.build_snapshot(['D-id', 'E-id'],
1772
[('rename', ('bar', 'bing'))],
1773
revision_id='F-id') # Merge in E's change
1774
merge_obj = self.make_merge_obj(builder, 'G-id')
1776
self.expectFailure("We don't do an actual heads() check on lca values,"
1777
" or use the per-attribute graph",
1778
self.assertEqual, [], list(merge_obj._entries_lca()))
1780
def test_one_lca_accidentally_pruned(self):
1781
# Another incorrect resolution from the same basic flaw:
1784
# B C B value 'bar', C = 'foo'
1786
# D E D = 'bar', E reverts to 'foo'
1788
# F G F = 'bing', G switches to 'bar'
1790
# 'bar' will not be seen as an interesting change, because 'foo' will
1791
# be pruned from the LCAs, even though it was newly introduced by E
1793
builder = self.get_builder()
1794
builder.build_snapshot(None,
1795
[('add', (u'', 'a-root-id', 'directory', None)),
1796
('add', (u'foo', 'foo-id', 'file', 'A content\n'))],
1798
builder.build_snapshot(['A-id'], [], revision_id='C-id')
1799
builder.build_snapshot(['A-id'],
1800
[('rename', ('foo', 'bar'))],
1802
builder.build_snapshot(['B-id', 'C-id'], [], revision_id='D-id')
1803
builder.build_snapshot(['C-id', 'B-id'], [], revision_id='E-id')
1804
builder.build_snapshot(['E-id', 'D-id'],
1805
[('rename', ('foo', 'bar'))],
1807
builder.build_snapshot(['D-id', 'E-id'],
1808
[('rename', ('bar', 'bing'))],
1809
revision_id='F-id') # should end up conflicting
1810
merge_obj = self.make_merge_obj(builder, 'G-id')
1812
entries = list(merge_obj._entries_lca())
1813
root_id = 'a-root-id'
1814
self.expectFailure("We prune values from BASE even when relevant.",
1817
((root_id, [root_id, root_id]), root_id, root_id),
1818
((u'foo', [u'bar', u'foo']), u'bar', u'bing'),
1819
((False, [False, False]), False, False)),
1822
def test_both_sides_revert(self):
1823
# Both sides of a criss-cross revert the text to the lca
1824
# A base, introduces 'foo'
1826
# B C B modifies 'foo', C modifies 'foo'
1828
# D E D reverts to B, E reverts to C
1829
# This should conflict
1830
builder = self.get_builder()
1831
builder.build_snapshot(None,
1832
[('add', (u'', 'a-root-id', 'directory', None)),
1833
('add', (u'foo', 'foo-id', 'file', 'A content\n'))],
1835
builder.build_snapshot(['A-id'],
1836
[('modify', ('foo', 'B content\n'))],
1838
builder.build_snapshot(['A-id'],
1839
[('modify', ('foo', 'C content\n'))],
1841
builder.build_snapshot(['C-id', 'B-id'], [], revision_id='E-id')
1842
builder.build_snapshot(['B-id', 'C-id'], [], revision_id='D-id')
1843
merge_obj = self.make_merge_obj(builder, 'E-id')
1845
entries = list(merge_obj._entries_lca())
1846
root_id = 'a-root-id'
1847
self.assertEqual([('foo-id', True,
1848
((u'foo', [u'foo', u'foo']), u'foo', u'foo'),
1849
((root_id, [root_id, root_id]), root_id, root_id),
1850
((u'foo', [u'foo', u'foo']), u'foo', u'foo'),
1851
((False, [False, False]), False, False)),
1854
def test_different_lca_resolve_one_side_updates_content(self):
1855
# Both sides converge, but then one side updates the text.
1856
# A base, introduces 'foo'
1858
# B C B modifies 'foo', C modifies 'foo'
1860
# D E D reverts to B, E reverts to C
1862
# F F updates to a new value
1863
# We need to emit an entry for 'foo', because D & E differed on the
1865
builder = self.get_builder()
1866
builder.build_snapshot(None,
1867
[('add', (u'', 'a-root-id', 'directory', None)),
1868
('add', (u'foo', 'foo-id', 'file', 'A content\n'))],
1870
builder.build_snapshot(['A-id'],
1871
[('modify', ('foo', 'B content\n'))],
1873
builder.build_snapshot(['A-id'],
1874
[('modify', ('foo', 'C content\n'))],
1875
revision_id='C-id', )
1876
builder.build_snapshot(['C-id', 'B-id'], [], revision_id='E-id')
1877
builder.build_snapshot(['B-id', 'C-id'], [], revision_id='D-id')
1878
builder.build_snapshot(['D-id'],
1879
[('modify', ('foo', 'F content\n'))],
1881
merge_obj = self.make_merge_obj(builder, 'E-id')
1883
entries = list(merge_obj._entries_lca())
1884
root_id = 'a-root-id'
1885
self.assertEqual([('foo-id', True,
1886
((u'foo', [u'foo', u'foo']), u'foo', u'foo'),
1887
((root_id, [root_id, root_id]), root_id, root_id),
1888
((u'foo', [u'foo', u'foo']), u'foo', u'foo'),
1889
((False, [False, False]), False, False)),
1892
def test_same_lca_resolution_one_side_updates_content(self):
1893
# Both sides converge, but then one side updates the text.
1894
# A base, introduces 'foo'
1896
# B C B modifies 'foo', C modifies 'foo'
1898
# D E D and E use C's value
1900
# F F updates to a new value
1901
# I think it is a bug that this conflicts, but we don't have a way to
1902
# detect otherwise. And because of:
1903
# test_different_lca_resolve_one_side_updates_content
1904
# We need to conflict.
1906
builder = self.get_builder()
1907
builder.build_snapshot(None,
1908
[('add', (u'', 'a-root-id', 'directory', None)),
1909
('add', (u'foo', 'foo-id', 'file', 'A content\n'))],
1911
builder.build_snapshot(['A-id'],
1912
[('modify', ('foo', 'B content\n'))],
1914
builder.build_snapshot(['A-id'],
1915
[('modify', ('foo', 'C content\n'))],
1917
builder.build_snapshot(['C-id', 'B-id'], [], revision_id='E-id')
1918
builder.build_snapshot(['B-id', 'C-id'],
1919
[('modify', ('foo', 'C content\n'))],
1920
revision_id='D-id') # Same as E
1921
builder.build_snapshot(['D-id'],
1922
[('modify', ('foo', 'F content\n'))],
1924
merge_obj = self.make_merge_obj(builder, 'E-id')
1926
entries = list(merge_obj._entries_lca())
1927
self.expectFailure("We don't detect that LCA resolution was the"
1928
" same on both sides",
1929
self.assertEqual, [], entries)
1931
def test_only_path_changed(self):
1932
builder = self.get_builder()
1933
builder.build_snapshot(None,
1934
[('add', (u'', 'a-root-id', 'directory', None)),
1935
('add', (u'a', 'a-id', 'file', 'content\n'))],
1937
builder.build_snapshot(['A-id'], [], revision_id='B-id')
1938
builder.build_snapshot(['A-id'], [], revision_id='C-id')
1939
builder.build_snapshot(['C-id', 'B-id'],
1940
[('rename', (u'a', u'b'))],
1942
builder.build_snapshot(['B-id', 'C-id'], [], revision_id='D-id')
1943
merge_obj = self.make_merge_obj(builder, 'E-id')
1944
entries = list(merge_obj._entries_lca())
1945
root_id = 'a-root-id'
1946
# The content was not changed, only the path
1947
self.assertEqual([('a-id', False,
1948
((u'a', [u'a', u'a']), u'b', u'a'),
1949
((root_id, [root_id, root_id]), root_id, root_id),
1950
((u'a', [u'a', u'a']), u'b', u'a'),
1951
((False, [False, False]), False, False)),
1954
def test_kind_changed(self):
1955
# Identical content, except 'D' changes a-id into a directory
1956
builder = self.get_builder()
1957
builder.build_snapshot(None,
1958
[('add', (u'', 'a-root-id', 'directory', None)),
1959
('add', (u'a', 'a-id', 'file', 'content\n'))],
1961
builder.build_snapshot(['A-id'], [], revision_id='B-id')
1962
builder.build_snapshot(['A-id'], [], revision_id='C-id')
1963
builder.build_snapshot(['C-id', 'B-id'],
1964
[('unversion', 'a'),
1966
('add', (u'a', 'a-id', 'directory', None))],
1968
builder.build_snapshot(['B-id', 'C-id'], [], revision_id='D-id')
1969
merge_obj = self.make_merge_obj(builder, 'E-id')
1970
entries = list(merge_obj._entries_lca())
1971
root_id = 'a-root-id'
1972
# Only the kind was changed (content)
1973
self.assertEqual([('a-id', True,
1974
((u'a', [u'a', u'a']), u'a', u'a'),
1975
((root_id, [root_id, root_id]), root_id, root_id),
1976
((u'a', [u'a', u'a']), u'a', u'a'),
1977
((False, [False, False]), False, False)),
1980
def test_this_changed_kind(self):
1981
# Identical content, but THIS changes a file to a directory
1982
builder = self.get_builder()
1983
builder.build_snapshot(None,
1984
[('add', (u'', 'a-root-id', 'directory', None)),
1985
('add', (u'a', 'a-id', 'file', 'content\n'))],
1987
builder.build_snapshot(['A-id'], [], revision_id='B-id')
1988
builder.build_snapshot(['A-id'], [], revision_id='C-id')
1989
builder.build_snapshot(['C-id', 'B-id'], [], revision_id='E-id')
1990
builder.build_snapshot(['B-id', 'C-id'],
1991
[('unversion', 'a'),
1993
('add', (u'a', 'a-id', 'directory', None))],
1995
merge_obj = self.make_merge_obj(builder, 'E-id')
1996
entries = list(merge_obj._entries_lca())
1997
# Only the kind was changed (content)
1998
self.assertEqual([], entries)
2000
def test_interesting_files(self):
2001
# Two files modified, but we should filter one of them
2002
builder = self.get_builder()
2003
builder.build_snapshot(None,
2004
[('add', (u'', 'a-root-id', 'directory', None)),
2005
('add', (u'a', 'a-id', 'file', 'content\n')),
2006
('add', (u'b', 'b-id', 'file', 'content\n'))],
2008
builder.build_snapshot(['A-id'], [], revision_id='B-id')
2009
builder.build_snapshot(['A-id'], [], revision_id='C-id')
2010
builder.build_snapshot(['C-id', 'B-id'],
2011
[('modify', ('a', 'new-content\n')),
2012
('modify', ('b', 'new-content\n'))],
2014
builder.build_snapshot(['B-id', 'C-id'], [], revision_id='D-id')
2015
merge_obj = self.make_merge_obj(builder, 'E-id',
2016
interesting_files=['b'])
2017
entries = list(merge_obj._entries_lca())
2018
root_id = 'a-root-id'
2019
self.assertEqual([('b-id', True,
2020
((u'b', [u'b', u'b']), u'b', u'b'),
2021
((root_id, [root_id, root_id]), root_id, root_id),
2022
((u'b', [u'b', u'b']), u'b', u'b'),
2023
((False, [False, False]), False, False)),
2026
def test_interesting_file_in_this(self):
2027
# This renamed the file, but it should still match the entry in other
2028
builder = self.get_builder()
2029
builder.build_snapshot(None,
2030
[('add', (u'', 'a-root-id', 'directory', None)),
2031
('add', (u'a', 'a-id', 'file', 'content\n')),
2032
('add', (u'b', 'b-id', 'file', 'content\n'))],
2034
builder.build_snapshot(['A-id'], [], revision_id='B-id')
2035
builder.build_snapshot(['A-id'], [], revision_id='C-id')
2036
builder.build_snapshot(['C-id', 'B-id'],
2037
[('modify', ('a', 'new-content\n')),
2038
('modify', ('b', 'new-content\n'))],
2040
builder.build_snapshot(['B-id', 'C-id'],
2041
[('rename', ('b', 'c'))],
2043
merge_obj = self.make_merge_obj(builder, 'E-id',
2044
interesting_files=['c'])
2045
entries = list(merge_obj._entries_lca())
2046
root_id = 'a-root-id'
2047
self.assertEqual([('b-id', True,
2048
((u'b', [u'b', u'b']), u'b', u'c'),
2049
((root_id, [root_id, root_id]), root_id, root_id),
2050
((u'b', [u'b', u'b']), u'b', u'c'),
2051
((False, [False, False]), False, False)),
2054
def test_interesting_file_in_base(self):
2055
# This renamed the file, but it should still match the entry in BASE
2056
builder = self.get_builder()
2057
builder.build_snapshot(None,
2058
[('add', (u'', 'a-root-id', 'directory', None)),
2059
('add', (u'a', 'a-id', 'file', 'content\n')),
2060
('add', (u'c', 'c-id', 'file', 'content\n'))],
2062
builder.build_snapshot(['A-id'],
2063
[('rename', ('c', 'b'))],
2065
builder.build_snapshot(['A-id'],
2066
[('rename', ('c', 'b'))],
2068
builder.build_snapshot(['C-id', 'B-id'],
2069
[('modify', ('a', 'new-content\n')),
2070
('modify', ('b', 'new-content\n'))],
2072
builder.build_snapshot(['B-id', 'C-id'], [], revision_id='D-id')
2073
merge_obj = self.make_merge_obj(builder, 'E-id',
2074
interesting_files=['c'])
2075
entries = list(merge_obj._entries_lca())
2076
root_id = 'a-root-id'
2077
self.assertEqual([('c-id', True,
2078
((u'c', [u'b', u'b']), u'b', u'b'),
2079
((root_id, [root_id, root_id]), root_id, root_id),
2080
((u'c', [u'b', u'b']), u'b', u'b'),
2081
((False, [False, False]), False, False)),
2084
def test_interesting_file_in_lca(self):
2085
# This renamed the file, but it should still match the entry in LCA
2086
builder = self.get_builder()
2087
builder.build_snapshot(None,
2088
[('add', (u'', 'a-root-id', 'directory', None)),
2089
('add', (u'a', 'a-id', 'file', 'content\n')),
2090
('add', (u'b', 'b-id', 'file', 'content\n'))],
2092
builder.build_snapshot(['A-id'],
2093
[('rename', ('b', 'c'))], revision_id='B-id')
2094
builder.build_snapshot(['A-id'], [], revision_id='C-id')
2095
builder.build_snapshot(['C-id', 'B-id'],
2096
[('modify', ('a', 'new-content\n')),
2097
('modify', ('b', 'new-content\n'))],
2099
builder.build_snapshot(['B-id', 'C-id'],
2100
[('rename', ('c', 'b'))], revision_id='D-id')
2101
merge_obj = self.make_merge_obj(builder, 'E-id',
2102
interesting_files=['c'])
2103
entries = list(merge_obj._entries_lca())
2104
root_id = 'a-root-id'
2105
self.assertEqual([('b-id', True,
2106
((u'b', [u'c', u'b']), u'b', u'b'),
2107
((root_id, [root_id, root_id]), root_id, root_id),
2108
((u'b', [u'c', u'b']), u'b', u'b'),
2109
((False, [False, False]), False, False)),
2112
def test_interesting_files(self):
2113
# Two files modified, but we should filter one of them
2114
builder = self.get_builder()
2115
builder.build_snapshot(None,
2116
[('add', (u'', 'a-root-id', 'directory', None)),
2117
('add', (u'a', 'a-id', 'file', 'content\n')),
2118
('add', (u'b', 'b-id', 'file', 'content\n'))],
2120
builder.build_snapshot(['A-id'], [], revision_id='B-id')
2121
builder.build_snapshot(['A-id'], [], revision_id='C-id')
2122
builder.build_snapshot(['C-id', 'B-id'],
2123
[('modify', ('a', 'new-content\n')),
2124
('modify', ('b', 'new-content\n'))], revision_id='E-id')
2125
builder.build_snapshot(['B-id', 'C-id'], [], revision_id='D-id')
2126
merge_obj = self.make_merge_obj(builder, 'E-id',
2127
interesting_files=['b'])
2128
entries = list(merge_obj._entries_lca())
2129
root_id = 'a-root-id'
2130
self.assertEqual([('b-id', True,
2131
((u'b', [u'b', u'b']), u'b', u'b'),
2132
((root_id, [root_id, root_id]), root_id, root_id),
2133
((u'b', [u'b', u'b']), u'b', u'b'),
2134
((False, [False, False]), False, False)),
2139
class TestMergerEntriesLCAOnDisk(tests.TestCaseWithTransport):
2141
def get_builder(self):
2142
builder = self.make_branch_builder('path')
2143
builder.start_series()
2144
self.addCleanup(builder.finish_series)
2147
def get_wt_from_builder(self, builder):
2148
"""Get a real WorkingTree from the builder."""
2149
the_branch = builder.get_branch()
2150
wt = the_branch.controldir.create_workingtree()
2151
# Note: This is a little bit ugly, but we are holding the branch
2152
# write-locked as part of the build process, and we would like to
2153
# maintain that. So we just force the WT to re-use the same
2155
wt._branch = the_branch
2157
self.addCleanup(wt.unlock)
2160
def do_merge(self, builder, other_revision_id):
2161
wt = self.get_wt_from_builder(builder)
2162
merger = _mod_merge.Merger.from_revision_ids(
2163
wt, other_revision_id)
2164
merger.merge_type = _mod_merge.Merge3Merger
2165
return wt, merger.do_merge()
2167
def test_simple_lca(self):
2168
builder = self.get_builder()
2169
builder.build_snapshot(None,
2170
[('add', (u'', 'a-root-id', 'directory', None)),
2171
('add', (u'a', 'a-id', 'file', 'a\nb\nc\n'))],
2173
builder.build_snapshot(['A-id'], [], revision_id='C-id')
2174
builder.build_snapshot(['A-id'], [], revision_id='B-id')
2175
builder.build_snapshot(['C-id', 'B-id'], [], revision_id='E-id')
2176
builder.build_snapshot(['B-id', 'C-id'],
2177
[('modify', ('a', 'a\nb\nc\nd\ne\nf\n'))],
2179
wt, conflicts = self.do_merge(builder, 'E-id')
2180
self.assertEqual(0, conflicts)
2181
# The merge should have simply update the contents of 'a'
2182
self.assertEqual('a\nb\nc\nd\ne\nf\n', wt.get_file_text('a'))
2184
def test_conflict_without_lca(self):
2185
# This test would cause a merge conflict, unless we use the lca trees
2186
# to determine the real ancestry
2189
# B C Path renamed to 'bar' in B
2193
# D E Path at 'bar' in D and E
2195
# F Path at 'baz' in F, which supersedes 'bar' and 'foo'
2196
builder = self.get_builder()
2197
builder.build_snapshot(None,
2198
[('add', (u'', 'a-root-id', 'directory', None)),
2199
('add', (u'foo', 'foo-id', 'file', 'a\nb\nc\n'))],
2201
builder.build_snapshot(['A-id'], [], revision_id='C-id')
2202
builder.build_snapshot(['A-id'],
2203
[('rename', ('foo', 'bar'))], revision_id='B-id', )
2204
builder.build_snapshot(['C-id', 'B-id'], # merge the rename
2205
[('rename', ('foo', 'bar'))], revision_id='E-id')
2206
builder.build_snapshot(['E-id'],
2207
[('rename', ('bar', 'baz'))], revision_id='F-id')
2208
builder.build_snapshot(['B-id', 'C-id'], [], revision_id='D-id')
2209
wt, conflicts = self.do_merge(builder, 'F-id')
2210
self.assertEqual(0, conflicts)
2211
# The merge should simply recognize that the final rename takes
2213
self.assertEqual('baz', wt.id2path(b'foo-id'))
2215
def test_other_deletes_lca_renames(self):
2216
# This test would cause a merge conflict, unless we use the lca trees
2217
# to determine the real ancestry
2220
# B C Path renamed to 'bar' in B
2224
# D E Path at 'bar' in D and E
2227
builder = self.get_builder()
2228
builder.build_snapshot(None,
2229
[('add', (u'', 'a-root-id', 'directory', None)),
2230
('add', (u'foo', 'foo-id', 'file', 'a\nb\nc\n'))],
2232
builder.build_snapshot(['A-id'], [], revision_id='C-id')
2233
builder.build_snapshot(['A-id'],
2234
[('rename', ('foo', 'bar'))], revision_id='B-id')
2235
builder.build_snapshot(['C-id', 'B-id'], # merge the rename
2236
[('rename', ('foo', 'bar'))], revision_id='E-id')
2237
builder.build_snapshot(['E-id'],
2238
[('unversion', 'bar')], revision_id='F-id')
2239
builder.build_snapshot(['B-id', 'C-id'], [], revision_id='D-id')
2240
wt, conflicts = self.do_merge(builder, 'F-id')
2241
self.assertEqual(0, conflicts)
2242
self.assertRaises(errors.NoSuchId, wt.id2path, 'foo-id')
2244
def test_executable_changes(self):
2253
# F Executable bit changed
2254
builder = self.get_builder()
2255
builder.build_snapshot(None,
2256
[('add', (u'', 'a-root-id', 'directory', None)),
2257
('add', (u'foo', 'foo-id', 'file', 'a\nb\nc\n'))],
2259
builder.build_snapshot(['A-id'], [], revision_id='C-id')
2260
builder.build_snapshot(['A-id'], [], revision_id='B-id')
2261
builder.build_snapshot(['B-id', 'C-id'], [], revision_id='D-id')
2262
builder.build_snapshot(['C-id', 'B-id'], [], revision_id='E-id')
2263
# Have to use a real WT, because BranchBuilder doesn't support exec bit
2264
wt = self.get_wt_from_builder(builder)
2265
tt = transform.TreeTransform(wt)
2267
tt.set_executability(True, tt.trans_id_tree_path('foo'))
2272
self.assertTrue(wt.is_executable('foo'))
2273
wt.commit('F-id', rev_id=b'F-id')
2274
# Reset to D, so that we can merge F
2275
wt.set_parent_ids(['D-id'])
2276
wt.branch.set_last_revision_info(3, 'D-id')
2278
self.assertFalse(wt.is_executable('foo'))
2279
conflicts = wt.merge_from_branch(wt.branch, to_revision='F-id')
2280
self.assertEqual(0, conflicts)
2281
self.assertTrue(wt.is_executable('foo'))
2283
def test_create_symlink(self):
2284
self.requireFeature(features.SymlinkFeature)
2293
# F Add a symlink 'foo' => 'bar'
2294
# Have to use a real WT, because BranchBuilder and MemoryTree don't
2295
# have symlink support
2296
builder = self.get_builder()
2297
builder.build_snapshot(None,
2298
[('add', (u'', 'a-root-id', 'directory', None))],
2300
builder.build_snapshot(['A-id'], [], revision_id='C-id')
2301
builder.build_snapshot(['A-id'], [], revision_id='B-id')
2302
builder.build_snapshot(['B-id', 'C-id'], [], revision_id='D-id')
2303
builder.build_snapshot(['C-id', 'B-id'], [], revision_id='E-id')
2304
# Have to use a real WT, because BranchBuilder doesn't support exec bit
2305
wt = self.get_wt_from_builder(builder)
2306
os.symlink('bar', 'path/foo')
2307
wt.add(['foo'], [b'foo-id'])
2308
self.assertEqual('bar', wt.get_symlink_target('foo'))
2309
wt.commit('add symlink', rev_id=b'F-id')
2310
# Reset to D, so that we can merge F
2311
wt.set_parent_ids([b'D-id'])
2312
wt.branch.set_last_revision_info(3, b'D-id')
2314
self.assertFalse(wt.is_versioned('foo'))
2315
conflicts = wt.merge_from_branch(wt.branch, to_revision=b'F-id')
2316
self.assertEqual(0, conflicts)
2317
self.assertEqual(b'foo-id', wt.path2id('foo'))
2318
self.assertEqual('bar', wt.get_symlink_target('foo'))
2320
def test_both_sides_revert(self):
2321
# Both sides of a criss-cross revert the text to the lca
2322
# A base, introduces 'foo'
2324
# B C B modifies 'foo', C modifies 'foo'
2326
# D E D reverts to B, E reverts to C
2327
# This should conflict
2328
# This must be done with a real WorkingTree, because normally their
2329
# inventory contains "None" rather than a real sha1
2330
builder = self.get_builder()
2331
builder.build_snapshot(None,
2332
[('add', (u'', 'a-root-id', 'directory', None)),
2333
('add', (u'foo', 'foo-id', 'file', 'A content\n'))],
2335
builder.build_snapshot(['A-id'],
2336
[('modify', ('foo', 'B content\n'))],
2338
builder.build_snapshot(['A-id'],
2339
[('modify', ('foo', 'C content\n'))],
2341
builder.build_snapshot(['C-id', 'B-id'], [],
2343
builder.build_snapshot(['B-id', 'C-id'], [],
2345
wt, conflicts = self.do_merge(builder, 'E-id')
2346
self.assertEqual(1, conflicts)
2347
self.assertEqualDiff('<<<<<<< TREE\n'
2351
'>>>>>>> MERGE-SOURCE\n',
2352
wt.get_file_text('foo'))
2354
def test_modified_symlink(self):
2355
self.requireFeature(features.SymlinkFeature)
2356
# A Create symlink foo => bar
2358
# B C B relinks foo => baz
2362
# D E D & E have foo => baz
2364
# F F changes it to bing
2366
# Merging D & F should result in F cleanly overriding D, because D's
2367
# value actually comes from B
2369
# Have to use a real WT, because BranchBuilder and MemoryTree don't
2370
# have symlink support
2371
wt = self.make_branch_and_tree('path')
2373
self.addCleanup(wt.unlock)
2374
os.symlink('bar', 'path/foo')
2375
wt.add(['foo'], [b'foo-id'])
2376
wt.commit('add symlink', rev_id=b'A-id')
2377
os.remove('path/foo')
2378
os.symlink('baz', 'path/foo')
2379
wt.commit('foo => baz', rev_id=b'B-id')
2380
wt.set_last_revision(b'A-id')
2381
wt.branch.set_last_revision_info(1, 'A-id')
2383
wt.commit('C', rev_id=b'C-id')
2384
wt.merge_from_branch(wt.branch, b'B-id')
2385
self.assertEqual('baz', wt.get_symlink_target('foo'))
2386
wt.commit('E merges C & B', rev_id=b'E-id')
2387
os.remove('path/foo')
2388
os.symlink('bing', 'path/foo')
2389
wt.commit('F foo => bing', rev_id=b'F-id')
2390
wt.set_last_revision('B-id')
2391
wt.branch.set_last_revision_info(2, b'B-id')
2393
wt.merge_from_branch(wt.branch, b'C-id')
2394
wt.commit('D merges B & C', rev_id=b'D-id')
2395
conflicts = wt.merge_from_branch(wt.branch, to_revision=b'F-id')
2396
self.assertEqual(0, conflicts)
2397
self.assertEqual('bing', wt.get_symlink_target('foo'))
2399
def test_renamed_symlink(self):
2400
self.requireFeature(features.SymlinkFeature)
2401
# A Create symlink foo => bar
2403
# B C B renames foo => barry
2407
# D E D & E have barry
2409
# F F renames barry to blah
2411
# Merging D & F should result in F cleanly overriding D, because D's
2412
# value actually comes from B
2414
wt = self.make_branch_and_tree('path')
2416
self.addCleanup(wt.unlock)
2417
os.symlink('bar', 'path/foo')
2418
wt.add(['foo'], [b'foo-id'])
2419
wt.commit('A add symlink', rev_id=b'A-id')
2420
wt.rename_one('foo', 'barry')
2421
wt.commit('B foo => barry', rev_id=b'B-id')
2422
wt.set_last_revision('A-id')
2423
wt.branch.set_last_revision_info(1, b'A-id')
2425
wt.commit('C', rev_id=b'C-id')
2426
wt.merge_from_branch(wt.branch, b'B-id')
2427
self.assertEqual('barry', wt.id2path(b'foo-id'))
2428
self.assertEqual('bar', wt.get_symlink_target('barry'))
2429
wt.commit('E merges C & B', rev_id=b'E-id')
2430
wt.rename_one('barry', 'blah')
2431
wt.commit('F barry => blah', rev_id=b'F-id')
2432
wt.set_last_revision(b'B-id')
2433
wt.branch.set_last_revision_info(2, b'B-id')
2435
wt.merge_from_branch(wt.branch, b'C-id')
2436
wt.commit('D merges B & C', rev_id=b'D-id')
2437
self.assertEqual('barry', wt.id2path(b'foo-id'))
2438
# Check the output of the Merger object directly
2439
merger = _mod_merge.Merger.from_revision_ids(wt, b'F-id')
2440
merger.merge_type = _mod_merge.Merge3Merger
2441
merge_obj = merger.make_merger()
2442
root_id = wt.path2id('')
2443
entries = list(merge_obj._entries_lca())
2444
# No content change, just a path change
2445
self.assertEqual([(b'foo-id', False,
2446
((u'foo', [u'barry', u'foo']), u'blah', u'barry'),
2447
((root_id, [root_id, root_id]), root_id, root_id),
2448
((u'foo', [u'barry', u'foo']), u'blah', u'barry'),
2449
((False, [False, False]), False, False)),
2451
conflicts = wt.merge_from_branch(wt.branch, to_revision=b'F-id')
2452
self.assertEqual(0, conflicts)
2453
self.assertEqual('blah', wt.id2path(b'foo-id'))
2455
def test_symlink_no_content_change(self):
2456
self.requireFeature(features.SymlinkFeature)
2457
# A Create symlink foo => bar
2459
# B C B relinks foo => baz
2463
# D E D & E have foo => baz
2465
# F F has foo => bing
2467
# Merging E into F should not cause a conflict, because E doesn't have
2468
# a content change relative to the LCAs (it does relative to A)
2469
wt = self.make_branch_and_tree('path')
2471
self.addCleanup(wt.unlock)
2472
os.symlink('bar', 'path/foo')
2473
wt.add(['foo'], [b'foo-id'])
2474
wt.commit('add symlink', rev_id=b'A-id')
2475
os.remove('path/foo')
2476
os.symlink('baz', 'path/foo')
2477
wt.commit('foo => baz', rev_id=b'B-id')
2478
wt.set_last_revision(b'A-id')
2479
wt.branch.set_last_revision_info(1, b'A-id')
2481
wt.commit('C', rev_id=b'C-id')
2482
wt.merge_from_branch(wt.branch, b'B-id')
2483
self.assertEqual('baz', wt.get_symlink_target('foo'))
2484
wt.commit('E merges C & B', rev_id=b'E-id')
2485
wt.set_last_revision(b'B-id')
2486
wt.branch.set_last_revision_info(2, b'B-id')
2488
wt.merge_from_branch(wt.branch, b'C-id')
2489
wt.commit('D merges B & C', rev_id=b'D-id')
2490
os.remove('path/foo')
2491
os.symlink('bing', 'path/foo')
2492
wt.commit('F foo => bing', rev_id=b'F-id')
2494
# Check the output of the Merger object directly
2495
merger = _mod_merge.Merger.from_revision_ids(wt, b'E-id')
2496
merger.merge_type = _mod_merge.Merge3Merger
2497
merge_obj = merger.make_merger()
2498
# Nothing interesting happened in OTHER relative to BASE
2499
self.assertEqual([], list(merge_obj._entries_lca()))
2500
# Now do a real merge, just to test the rest of the stack
2501
conflicts = wt.merge_from_branch(wt.branch, to_revision=b'E-id')
2502
self.assertEqual(0, conflicts)
2503
self.assertEqual('bing', wt.get_symlink_target('foo'))
2505
def test_symlink_this_changed_kind(self):
2506
self.requireFeature(features.SymlinkFeature)
2509
# B C B creates symlink foo => bar
2513
# D E D changes foo into a file, E has foo => bing
2515
# Mostly, this is trying to test that we don't try to os.readlink() on
2516
# a file, or when there is nothing there
2517
wt = self.make_branch_and_tree('path')
2519
self.addCleanup(wt.unlock)
2520
wt.commit('base', rev_id=b'A-id')
2521
os.symlink('bar', 'path/foo')
2522
wt.add(['foo'], [b'foo-id'])
2523
wt.commit('add symlink foo => bar', rev_id=b'B-id')
2524
wt.set_last_revision(b'A-id')
2525
wt.branch.set_last_revision_info(1, b'A-id')
2527
wt.commit('C', rev_id=b'C-id')
2528
wt.merge_from_branch(wt.branch, b'B-id')
2529
self.assertEqual('bar', wt.get_symlink_target('foo'))
2530
os.remove('path/foo')
2531
# We have to change the link in E, or it won't try to do a comparison
2532
os.symlink('bing', 'path/foo')
2533
wt.commit('E merges C & B, overrides to bing', rev_id=b'E-id')
2534
wt.set_last_revision('B-id')
2535
wt.branch.set_last_revision_info(2, b'B-id')
2537
wt.merge_from_branch(wt.branch, b'C-id')
2538
os.remove('path/foo')
2539
self.build_tree_contents([('path/foo', b'file content\n')])
2540
# XXX: workaround, WT doesn't detect kind changes unless you do
2542
list(wt.iter_changes(wt.basis_tree()))
2543
wt.commit('D merges B & C, makes it a file', rev_id=b'D-id')
2545
merger = _mod_merge.Merger.from_revision_ids(wt, b'E-id')
2546
merger.merge_type = _mod_merge.Merge3Merger
2547
merge_obj = merger.make_merger()
2548
entries = list(merge_obj._entries_lca())
2549
root_id = wt.path2id('')
2550
self.assertEqual([(b'foo-id', True,
2551
((None, [u'foo', None]), u'foo', u'foo'),
2552
((None, [root_id, None]), root_id, root_id),
2553
((None, [u'foo', None]), u'foo', u'foo'),
2554
((None, [False, None]), False, False)),
2557
def test_symlink_all_wt(self):
2558
"""Check behavior if all trees are Working Trees."""
2559
self.requireFeature(features.SymlinkFeature)
2560
# The big issue is that entry.symlink_target is None for WorkingTrees.
2561
# So we need to make sure we handle that case correctly.
2564
# B C B relinks foo => baz
2566
# D E D & E have foo => baz
2568
# F F changes it to bing
2569
# Merging D & F should result in F cleanly overriding D, because D's
2570
# value actually comes from B
2572
wt = self.make_branch_and_tree('path')
2574
self.addCleanup(wt.unlock)
2575
os.symlink('bar', 'path/foo')
2576
wt.add(['foo'], [b'foo-id'])
2577
wt.commit('add symlink', rev_id=b'A-id')
2578
os.remove('path/foo')
2579
os.symlink('baz', 'path/foo')
2580
wt.commit('foo => baz', rev_id=b'B-id')
2581
wt.set_last_revision(b'A-id')
2582
wt.branch.set_last_revision_info(1, b'A-id')
2584
wt.commit('C', rev_id=b'C-id')
2585
wt.merge_from_branch(wt.branch, b'B-id')
2586
self.assertEqual('baz', wt.get_symlink_target('foo'))
2587
wt.commit('E merges C & B', rev_id=b'E-id')
2588
os.remove('path/foo')
2589
os.symlink('bing', 'path/foo')
2590
wt.commit('F foo => bing', rev_id=b'F-id')
2591
wt.set_last_revision('B-id')
2592
wt.branch.set_last_revision_info(2, b'B-id')
2594
wt.merge_from_branch(wt.branch, b'C-id')
2595
wt.commit('D merges B & C', rev_id=b'D-id')
2596
wt_base = wt.controldir.sprout('base', b'A-id').open_workingtree()
2598
self.addCleanup(wt_base.unlock)
2599
wt_lca1 = wt.controldir.sprout('b-tree', b'B-id').open_workingtree()
2601
self.addCleanup(wt_lca1.unlock)
2602
wt_lca2 = wt.controldir.sprout('c-tree', b'C-id').open_workingtree()
2604
self.addCleanup(wt_lca2.unlock)
2605
wt_other = wt.controldir.sprout('other', b'F-id').open_workingtree()
2606
wt_other.lock_read()
2607
self.addCleanup(wt_other.unlock)
2608
merge_obj = _mod_merge.Merge3Merger(wt, wt, wt_base,
2609
wt_other, lca_trees=[wt_lca1, wt_lca2], do_merge=False)
2610
entries = list(merge_obj._entries_lca())
2611
root_id = wt.path2id('')
2612
self.assertEqual([(b'foo-id', True,
2613
((u'foo', [u'foo', u'foo']), u'foo', u'foo'),
2614
((root_id, [root_id, root_id]), root_id, root_id),
2615
((u'foo', [u'foo', u'foo']), u'foo', u'foo'),
2616
((False, [False, False]), False, False)),
2619
def test_other_reverted_path_to_base(self):
2622
# B C Path at 'bar' in B
2629
builder = self.get_builder()
2630
builder.build_snapshot(None,
2631
[('add', (u'', b'a-root-id', 'directory', None)),
2632
('add', (u'foo', b'foo-id', 'file', 'a\nb\nc\n'))],
2633
revision_id=b'A-id')
2634
builder.build_snapshot([b'A-id'], [], revision_id=b'C-id')
2635
builder.build_snapshot([b'A-id'],
2636
[('rename', ('foo', 'bar'))], revision_id=b'B-id')
2637
builder.build_snapshot([b'C-id', b'B-id'],
2638
[('rename', ('foo', 'bar'))], revision_id=b'E-id') # merge the rename
2639
builder.build_snapshot([b'E-id'],
2640
[('rename', ('bar', 'foo'))], revision_id=b'F-id') # Rename back to BASE
2641
builder.build_snapshot([b'B-id', b'C-id'], [], revision_id=b'D-id')
2642
wt, conflicts = self.do_merge(builder, b'F-id')
2643
self.assertEqual(0, conflicts)
2644
self.assertEqual('foo', wt.id2path(b'foo-id'))
2646
def test_other_reverted_content_to_base(self):
2647
builder = self.get_builder()
2648
builder.build_snapshot(None,
2649
[('add', (u'', b'a-root-id', 'directory', None)),
2650
('add', (u'foo', b'foo-id', 'file', b'base content\n'))],
2651
revision_id=b'A-id')
2652
builder.build_snapshot([b'A-id'], [], revision_id=b'C-id')
2653
builder.build_snapshot([b'A-id'],
2654
[('modify', ('foo', b'B content\n'))],
2655
revision_id=b'B-id')
2656
builder.build_snapshot([b'C-id', b'B-id'],
2657
[('modify', ('foo', b'B content\n'))],
2658
revision_id=b'E-id') # merge the content
2659
builder.build_snapshot([b'E-id'],
2660
[('modify', ('foo', b'base content\n'))],
2661
revision_id=b'F-id') # Revert back to BASE
2662
builder.build_snapshot([b'B-id', b'C-id'], [], revision_id=b'D-id')
2663
wt, conflicts = self.do_merge(builder, b'F-id')
2664
self.assertEqual(0, conflicts)
2665
# TODO: We need to use the per-file graph to properly select a BASE
2666
# before this will work. Or at least use the LCA trees to find
2667
# the appropriate content base. (which is B, not A).
2668
self.assertEqual('base content\n', wt.get_file_text('foo'))
2670
def test_other_modified_content(self):
2671
builder = self.get_builder()
2672
builder.build_snapshot(None,
2673
[('add', (u'', b'a-root-id', 'directory', None)),
2674
('add', (u'foo', b'foo-id', 'file', b'base content\n'))],
2675
revision_id=b'A-id')
2676
builder.build_snapshot([b'A-id'], [], revision_id=b'C-id')
2677
builder.build_snapshot([b'A-id'],
2678
[('modify', ('foo', b'B content\n'))],
2679
revision_id=b'B-id')
2680
builder.build_snapshot([b'C-id', b'B-id'],
2681
[('modify', ('foo', b'B content\n'))],
2682
revision_id=b'E-id') # merge the content
2683
builder.build_snapshot([b'E-id'],
2684
[('modify', ('foo', b'F content\n'))],
2685
revision_id=b'F-id') # Override B content
2686
builder.build_snapshot([b'B-id', b'C-id'], [], revision_id=b'D-id')
2687
wt, conflicts = self.do_merge(builder, b'F-id')
2688
self.assertEqual(0, conflicts)
2689
self.assertEqual(b'F content\n', wt.get_file_text('foo'))
2691
def test_all_wt(self):
2692
"""Check behavior if all trees are Working Trees."""
2693
# The big issue is that entry.revision is None for WorkingTrees. (as is
2694
# entry.text_sha1, etc. So we need to make sure we handle that case
2696
# A Content of 'foo', path of 'a'
2698
# B C B modifies content, C renames 'a' => 'b'
2700
# D E E updates content, renames 'b' => 'c'
2701
builder = self.get_builder()
2702
builder.build_snapshot(None,
2703
[('add', (u'', 'a-root-id', 'directory', None)),
2704
('add', (u'a', 'a-id', 'file', 'base content\n')),
2705
('add', (u'foo', 'foo-id', 'file', 'base content\n'))],
2707
builder.build_snapshot(['A-id'],
2708
[('modify', ('foo', 'B content\n'))],
2710
builder.build_snapshot(['A-id'],
2711
[('rename', ('a', 'b'))],
2713
builder.build_snapshot(['C-id', 'B-id'],
2714
[('rename', ('b', 'c')),
2715
('modify', ('foo', 'E content\n'))],
2717
builder.build_snapshot(['B-id', 'C-id'],
2718
[('rename', ('a', 'b'))], revision_id='D-id') # merged change
2719
wt_this = self.get_wt_from_builder(builder)
2720
wt_base = wt_this.controldir.sprout('base', 'A-id').open_workingtree()
2722
self.addCleanup(wt_base.unlock)
2723
wt_lca1 = wt_this.controldir.sprout('b-tree', 'B-id').open_workingtree()
2725
self.addCleanup(wt_lca1.unlock)
2726
wt_lca2 = wt_this.controldir.sprout('c-tree', 'C-id').open_workingtree()
2728
self.addCleanup(wt_lca2.unlock)
2729
wt_other = wt_this.controldir.sprout('other', 'E-id').open_workingtree()
2730
wt_other.lock_read()
2731
self.addCleanup(wt_other.unlock)
2732
merge_obj = _mod_merge.Merge3Merger(wt_this, wt_this, wt_base,
2733
wt_other, lca_trees=[wt_lca1, wt_lca2], do_merge=False)
2734
entries = list(merge_obj._entries_lca())
2735
root_id = 'a-root-id'
2736
self.assertEqual([('a-id', False,
2737
((u'a', [u'a', u'b']), u'c', u'b'),
2738
((root_id, [root_id, root_id]), root_id, root_id),
2739
((u'a', [u'a', u'b']), u'c', u'b'),
2740
((False, [False, False]), False, False)),
2742
((u'foo', [u'foo', u'foo']), u'foo', u'foo'),
2743
((root_id, [root_id, root_id]), root_id, root_id),
2744
((u'foo', [u'foo', u'foo']), u'foo', u'foo'),
2745
((False, [False, False]), False, False)),
2748
def test_nested_tree_unmodified(self):
2749
# Tested with a real WT, because BranchBuilder/MemoryTree don't handle
2751
wt = self.make_branch_and_tree('tree',
2752
format='development-subtree')
2754
self.addCleanup(wt.unlock)
2755
sub_tree = self.make_branch_and_tree('tree/sub-tree',
2756
format='development-subtree')
2757
wt.set_root_id(b'a-root-id')
2758
sub_tree.set_root_id(b'sub-tree-root')
2759
self.build_tree_contents([('tree/sub-tree/file', b'text1')])
2760
sub_tree.add('file')
2761
sub_tree.commit('foo', rev_id=b'sub-A-id')
2762
wt.add_reference(sub_tree)
2763
wt.commit('set text to 1', rev_id=b'A-id', recursive=None)
2764
# Now create a criss-cross merge in the parent, without modifying the
2766
wt.commit('B', rev_id=b'B-id', recursive=None)
2767
wt.set_last_revision('A-id')
2768
wt.branch.set_last_revision_info(1, 'A-id')
2769
wt.commit('C', rev_id=b'C-id', recursive=None)
2770
wt.merge_from_branch(wt.branch, to_revision='B-id')
2771
wt.commit('E', rev_id=b'E-id', recursive=None)
2772
wt.set_parent_ids(['B-id', 'C-id'])
2773
wt.branch.set_last_revision_info(2, 'B-id')
2774
wt.commit('D', rev_id=b'D-id', recursive=None)
2776
merger = _mod_merge.Merger.from_revision_ids(wt, 'E-id')
2777
merger.merge_type = _mod_merge.Merge3Merger
2778
merge_obj = merger.make_merger()
2779
entries = list(merge_obj._entries_lca())
2780
self.assertEqual([], entries)
2782
def test_nested_tree_subtree_modified(self):
2783
# Tested with a real WT, because BranchBuilder/MemoryTree don't handle
2785
wt = self.make_branch_and_tree('tree',
2786
format='development-subtree')
2788
self.addCleanup(wt.unlock)
2789
sub_tree = self.make_branch_and_tree('tree/sub',
2790
format='development-subtree')
2791
wt.set_root_id(b'a-root-id')
2792
sub_tree.set_root_id(b'sub-tree-root')
2793
self.build_tree_contents([('tree/sub/file', b'text1')])
2794
sub_tree.add('file')
2795
sub_tree.commit('foo', rev_id=b'sub-A-id')
2796
wt.add_reference(sub_tree)
2797
wt.commit('set text to 1', rev_id=b'A-id', recursive=None)
2798
# Now create a criss-cross merge in the parent, without modifying the
2800
wt.commit('B', rev_id=b'B-id', recursive=None)
2801
wt.set_last_revision('A-id')
2802
wt.branch.set_last_revision_info(1, 'A-id')
2803
wt.commit('C', rev_id=b'C-id', recursive=None)
2804
wt.merge_from_branch(wt.branch, to_revision='B-id')
2805
self.build_tree_contents([('tree/sub/file', b'text2')])
2806
sub_tree.commit('modify contents', rev_id=b'sub-B-id')
2807
wt.commit('E', rev_id=b'E-id', recursive=None)
2808
wt.set_parent_ids(['B-id', 'C-id'])
2809
wt.branch.set_last_revision_info(2, 'B-id')
2810
wt.commit('D', rev_id=b'D-id', recursive=None)
2812
merger = _mod_merge.Merger.from_revision_ids(wt, 'E-id')
2813
merger.merge_type = _mod_merge.Merge3Merger
2814
merge_obj = merger.make_merger()
2815
entries = list(merge_obj._entries_lca())
2816
# Nothing interesting about this sub-tree, because content changes are
2817
# computed at a higher level
2818
self.assertEqual([], entries)
2820
def test_nested_tree_subtree_renamed(self):
2821
# Tested with a real WT, because BranchBuilder/MemoryTree don't handle
2823
wt = self.make_branch_and_tree('tree',
2824
format='development-subtree')
2826
self.addCleanup(wt.unlock)
2827
sub_tree = self.make_branch_and_tree('tree/sub',
2828
format='development-subtree')
2829
wt.set_root_id(b'a-root-id')
2830
sub_tree.set_root_id(b'sub-tree-root')
2831
self.build_tree_contents([('tree/sub/file', b'text1')])
2832
sub_tree.add('file')
2833
sub_tree.commit('foo', rev_id=b'sub-A-id')
2834
wt.add_reference(sub_tree)
2835
wt.commit('set text to 1', rev_id=b'A-id', recursive=None)
2836
# Now create a criss-cross merge in the parent, without modifying the
2838
wt.commit('B', rev_id=b'B-id', recursive=None)
2839
wt.set_last_revision('A-id')
2840
wt.branch.set_last_revision_info(1, 'A-id')
2841
wt.commit('C', rev_id=b'C-id', recursive=None)
2842
wt.merge_from_branch(wt.branch, to_revision='B-id')
2843
wt.rename_one('sub', 'alt_sub')
2844
wt.commit('E', rev_id=b'E-id', recursive=None)
2845
wt.set_last_revision('B-id')
2847
wt.set_parent_ids(['B-id', 'C-id'])
2848
wt.branch.set_last_revision_info(2, 'B-id')
2849
wt.commit('D', rev_id=b'D-id', recursive=None)
2851
merger = _mod_merge.Merger.from_revision_ids(wt, 'E-id')
2852
merger.merge_type = _mod_merge.Merge3Merger
2853
merge_obj = merger.make_merger()
2854
entries = list(merge_obj._entries_lca())
2855
root_id = 'a-root-id'
2856
self.assertEqual([('sub-tree-root', False,
2857
((u'sub', [u'sub', u'sub']), u'alt_sub', u'sub'),
2858
((root_id, [root_id, root_id]), root_id, root_id),
2859
((u'sub', [u'sub', u'sub']), u'alt_sub', u'sub'),
2860
((False, [False, False]), False, False)),
2863
def test_nested_tree_subtree_renamed_and_modified(self):
2864
# Tested with a real WT, because BranchBuilder/MemoryTree don't handle
2866
wt = self.make_branch_and_tree('tree',
2867
format='development-subtree')
2869
self.addCleanup(wt.unlock)
2870
sub_tree = self.make_branch_and_tree('tree/sub',
2871
format='development-subtree')
2872
wt.set_root_id(b'a-root-id')
2873
sub_tree.set_root_id(b'sub-tree-root')
2874
self.build_tree_contents([('tree/sub/file', b'text1')])
2875
sub_tree.add('file')
2876
sub_tree.commit('foo', rev_id=b'sub-A-id')
2877
wt.add_reference(sub_tree)
2878
wt.commit('set text to 1', rev_id=b'A-id', recursive=None)
2879
# Now create a criss-cross merge in the parent, without modifying the
2881
wt.commit('B', rev_id=b'B-id', recursive=None)
2882
wt.set_last_revision('A-id')
2883
wt.branch.set_last_revision_info(1, 'A-id')
2884
wt.commit('C', rev_id=b'C-id', recursive=None)
2885
wt.merge_from_branch(wt.branch, to_revision='B-id')
2886
self.build_tree_contents([('tree/sub/file', b'text2')])
2887
sub_tree.commit('modify contents', rev_id=b'sub-B-id')
2888
wt.rename_one('sub', 'alt_sub')
2889
wt.commit('E', rev_id=b'E-id', recursive=None)
2890
wt.set_last_revision('B-id')
2892
wt.set_parent_ids(['B-id', 'C-id'])
2893
wt.branch.set_last_revision_info(2, 'B-id')
2894
wt.commit('D', rev_id=b'D-id', recursive=None)
2896
merger = _mod_merge.Merger.from_revision_ids(wt, 'E-id')
2897
merger.merge_type = _mod_merge.Merge3Merger
2898
merge_obj = merger.make_merger()
2899
entries = list(merge_obj._entries_lca())
2900
root_id = 'a-root-id'
2901
self.assertEqual([('sub-tree-root', False,
2902
((u'sub', [u'sub', u'sub']), u'alt_sub', u'sub'),
2903
((root_id, [root_id, root_id]), root_id, root_id),
2904
((u'sub', [u'sub', u'sub']), u'alt_sub', u'sub'),
2905
((False, [False, False]), False, False)),
2909
class TestLCAMultiWay(tests.TestCase):
2911
def assertLCAMultiWay(self, expected, base, lcas, other, this,
2912
allow_overriding_lca=True):
2913
self.assertEqual(expected, _mod_merge.Merge3Merger._lca_multi_way(
2914
(base, lcas), other, this,
2915
allow_overriding_lca=allow_overriding_lca))
2917
def test_other_equal_equal_lcas(self):
2918
"""Test when OTHER=LCA and all LCAs are identical."""
2919
self.assertLCAMultiWay('this',
2920
'bval', ['bval', 'bval'], 'bval', 'bval')
2921
self.assertLCAMultiWay('this',
2922
'bval', ['lcaval', 'lcaval'], 'lcaval', 'bval')
2923
self.assertLCAMultiWay('this',
2924
'bval', ['lcaval', 'lcaval', 'lcaval'], 'lcaval', 'bval')
2925
self.assertLCAMultiWay('this',
2926
'bval', ['lcaval', 'lcaval', 'lcaval'], 'lcaval', 'tval')
2927
self.assertLCAMultiWay('this',
2928
'bval', ['lcaval', 'lcaval', 'lcaval'], 'lcaval', None)
2930
def test_other_equal_this(self):
2931
"""Test when other and this are identical."""
2932
self.assertLCAMultiWay('this',
2933
'bval', ['bval', 'bval'], 'oval', 'oval')
2934
self.assertLCAMultiWay('this',
2935
'bval', ['lcaval', 'lcaval'], 'oval', 'oval')
2936
self.assertLCAMultiWay('this',
2937
'bval', ['cval', 'dval'], 'oval', 'oval')
2938
self.assertLCAMultiWay('this',
2939
'bval', [None, 'lcaval'], 'oval', 'oval')
2940
self.assertLCAMultiWay('this',
2941
None, [None, 'lcaval'], 'oval', 'oval')
2942
self.assertLCAMultiWay('this',
2943
None, ['lcaval', 'lcaval'], 'oval', 'oval')
2944
self.assertLCAMultiWay('this',
2945
None, ['cval', 'dval'], 'oval', 'oval')
2946
self.assertLCAMultiWay('this',
2947
None, ['cval', 'dval'], None, None)
2948
self.assertLCAMultiWay('this',
2949
None, ['cval', 'dval', 'eval', 'fval'], 'oval', 'oval')
2951
def test_no_lcas(self):
2952
self.assertLCAMultiWay('this',
2953
'bval', [], 'bval', 'tval')
2954
self.assertLCAMultiWay('other',
2955
'bval', [], 'oval', 'bval')
2956
self.assertLCAMultiWay('conflict',
2957
'bval', [], 'oval', 'tval')
2958
self.assertLCAMultiWay('this',
2959
'bval', [], 'oval', 'oval')
2961
def test_lca_supersedes_other_lca(self):
2962
"""If one lca == base, the other lca takes precedence"""
2963
self.assertLCAMultiWay('this',
2964
'bval', ['bval', 'lcaval'], 'lcaval', 'tval')
2965
self.assertLCAMultiWay('this',
2966
'bval', ['bval', 'lcaval'], 'lcaval', 'bval')
2967
# This is actually considered a 'revert' because the 'lcaval' in LCAS
2968
# supersedes the BASE val (in the other LCA) but then OTHER reverts it
2970
self.assertLCAMultiWay('other',
2971
'bval', ['bval', 'lcaval'], 'bval', 'lcaval')
2972
self.assertLCAMultiWay('conflict',
2973
'bval', ['bval', 'lcaval'], 'bval', 'tval')
2975
def test_other_and_this_pick_different_lca(self):
2976
# OTHER and THIS resolve the lca conflict in different ways
2977
self.assertLCAMultiWay('conflict',
2978
'bval', ['lca1val', 'lca2val'], 'lca1val', 'lca2val')
2979
self.assertLCAMultiWay('conflict',
2980
'bval', ['lca1val', 'lca2val', 'lca3val'], 'lca1val', 'lca2val')
2981
self.assertLCAMultiWay('conflict',
2982
'bval', ['lca1val', 'lca2val', 'bval'], 'lca1val', 'lca2val')
2984
def test_other_in_lca(self):
2985
# OTHER takes a value of one of the LCAs, THIS takes a new value, which
2986
# theoretically supersedes both LCA values and 'wins'
2987
self.assertLCAMultiWay('this',
2988
'bval', ['lca1val', 'lca2val'], 'lca1val', 'newval')
2989
self.assertLCAMultiWay('this',
2990
'bval', ['lca1val', 'lca2val', 'lca3val'], 'lca1val', 'newval')
2991
self.assertLCAMultiWay('conflict',
2992
'bval', ['lca1val', 'lca2val'], 'lca1val', 'newval',
2993
allow_overriding_lca=False)
2994
self.assertLCAMultiWay('conflict',
2995
'bval', ['lca1val', 'lca2val', 'lca3val'], 'lca1val', 'newval',
2996
allow_overriding_lca=False)
2997
# THIS reverted back to BASE, but that is an explicit supersede of all
2999
self.assertLCAMultiWay('this',
3000
'bval', ['lca1val', 'lca2val', 'lca3val'], 'lca1val', 'bval')
3001
self.assertLCAMultiWay('this',
3002
'bval', ['lca1val', 'lca2val', 'bval'], 'lca1val', 'bval')
3003
self.assertLCAMultiWay('conflict',
3004
'bval', ['lca1val', 'lca2val', 'lca3val'], 'lca1val', 'bval',
3005
allow_overriding_lca=False)
3006
self.assertLCAMultiWay('conflict',
3007
'bval', ['lca1val', 'lca2val', 'bval'], 'lca1val', 'bval',
3008
allow_overriding_lca=False)
3010
def test_this_in_lca(self):
3011
# THIS takes a value of one of the LCAs, OTHER takes a new value, which
3012
# theoretically supersedes both LCA values and 'wins'
3013
self.assertLCAMultiWay('other',
3014
'bval', ['lca1val', 'lca2val'], 'oval', 'lca1val')
3015
self.assertLCAMultiWay('other',
3016
'bval', ['lca1val', 'lca2val'], 'oval', 'lca2val')
3017
self.assertLCAMultiWay('conflict',
3018
'bval', ['lca1val', 'lca2val'], 'oval', 'lca1val',
3019
allow_overriding_lca=False)
3020
self.assertLCAMultiWay('conflict',
3021
'bval', ['lca1val', 'lca2val'], 'oval', 'lca2val',
3022
allow_overriding_lca=False)
3023
# OTHER reverted back to BASE, but that is an explicit supersede of all
3025
self.assertLCAMultiWay('other',
3026
'bval', ['lca1val', 'lca2val', 'lca3val'], 'bval', 'lca3val')
3027
self.assertLCAMultiWay('conflict',
3028
'bval', ['lca1val', 'lca2val', 'lca3val'], 'bval', 'lca3val',
3029
allow_overriding_lca=False)
3031
def test_all_differ(self):
3032
self.assertLCAMultiWay('conflict',
3033
'bval', ['lca1val', 'lca2val'], 'oval', 'tval')
3034
self.assertLCAMultiWay('conflict',
3035
'bval', ['lca1val', 'lca2val', 'lca2val'], 'oval', 'tval')
3036
self.assertLCAMultiWay('conflict',
3037
'bval', ['lca1val', 'lca2val', 'lca3val'], 'oval', 'tval')
3040
class TestConfigurableFileMerger(tests.TestCaseWithTransport):
3043
super(TestConfigurableFileMerger, self).setUp()
3046
def get_merger_factory(self):
3047
# Allows the inner methods to access the test attributes
3050
class FooMerger(_mod_merge.ConfigurableFileMerger):
3052
default_files = ['bar']
3054
def merge_text(self, params):
3055
calls.append('merge_text')
3056
return ('not_applicable', None)
3058
def factory(merger):
3059
result = FooMerger(merger)
3060
# Make sure we start with a clean slate
3061
self.assertEqual(None, result.affected_files)
3062
# Track the original merger
3063
self.merger = result
3068
def _install_hook(self, factory):
3069
_mod_merge.Merger.hooks.install_named_hook('merge_file_content',
3070
factory, 'test factory')
3072
def make_builder(self):
3073
builder = test_merge_core.MergeBuilder(self.test_base_dir)
3074
self.addCleanup(builder.cleanup)
3077
def make_text_conflict(self, file_name='bar'):
3078
factory = self.get_merger_factory()
3079
self._install_hook(factory)
3080
builder = self.make_builder()
3081
builder.add_file('bar-id', builder.tree_root, file_name, 'text1', True)
3082
builder.change_contents('bar-id', other='text4', this='text3')
3085
def make_kind_change(self):
3086
factory = self.get_merger_factory()
3087
self._install_hook(factory)
3088
builder = self.make_builder()
3089
builder.add_file('bar-id', builder.tree_root, 'bar', 'text1', True,
3091
builder.add_dir('bar-dir', builder.tree_root, 'bar-id',
3092
base=False, other=False)
3095
def test_uses_this_branch(self):
3096
builder = self.make_text_conflict()
3097
tt = builder.make_preview_transform()
3098
self.addCleanup(tt.finalize)
3100
def test_affected_files_cached(self):
3101
"""Ensures that the config variable is cached"""
3102
builder = self.make_text_conflict()
3103
conflicts = builder.merge()
3104
# The hook should set the variable
3105
self.assertEqual(['bar'], self.merger.affected_files)
3106
self.assertEqual(1, len(conflicts))
3108
def test_hook_called_for_text_conflicts(self):
3109
builder = self.make_text_conflict()
3110
conflicts = builder.merge()
3111
# The hook should call the merge_text() method
3112
self.assertEqual(['merge_text'], self.calls)
3114
def test_hook_not_called_for_kind_change(self):
3115
builder = self.make_kind_change()
3116
conflicts = builder.merge()
3117
# The hook should not call the merge_text() method
3118
self.assertEqual([], self.calls)
3120
def test_hook_not_called_for_other_files(self):
3121
builder = self.make_text_conflict('foobar')
3122
conflicts = builder.merge()
3123
# The hook should not call the merge_text() method
3124
self.assertEqual([], self.calls)
3127
class TestMergeIntoBase(tests.TestCaseWithTransport):
3129
def setup_simple_branch(self, relpath, shape=None, root_id=None):
3130
"""One commit, containing tree specified by optional shape.
3132
Default is empty tree (just root entry).
3135
root_id = '%s-root-id' % (relpath,)
3136
wt = self.make_branch_and_tree(relpath)
3137
wt.set_root_id(root_id)
3138
if shape is not None:
3139
adjusted_shape = [relpath + '/' + elem for elem in shape]
3140
self.build_tree(adjusted_shape)
3141
ids = ['%s-%s-id' % (relpath, basename(elem.rstrip('/')))
3143
wt.add(shape, ids=ids)
3144
rev_id = 'r1-%s' % (relpath,)
3145
wt.commit("Initial commit of %s" % (relpath,), rev_id=rev_id)
3146
self.assertEqual(root_id, wt.path2id(''))
3149
def setup_two_branches(self, custom_root_ids=True):
3150
"""Setup 2 branches, one will be a library, the other a project."""
3154
root_id = inventory.ROOT_ID
3155
project_wt = self.setup_simple_branch(
3156
'project', ['README', 'dir/', 'dir/file.c'],
3158
lib_wt = self.setup_simple_branch(
3159
'lib1', ['README', 'Makefile', 'foo.c'], root_id)
3161
return project_wt, lib_wt
3163
def do_merge_into(self, location, merge_as):
3164
"""Helper for using MergeIntoMerger.
3166
:param location: location of directory to merge from, either the
3167
location of a branch or of a path inside a branch.
3168
:param merge_as: the path in a tree to add the new directory as.
3169
:returns: the conflicts from 'do_merge'.
3171
operation = cleanup.OperationWithCleanups(self._merge_into)
3172
return operation.run(location, merge_as)
3174
def _merge_into(self, op, location, merge_as):
3175
# Open and lock the various tree and branch objects
3176
wt, subdir_relpath = WorkingTree.open_containing(merge_as)
3177
op.add_cleanup(wt.lock_write().unlock)
3178
branch_to_merge, subdir_to_merge = _mod_branch.Branch.open_containing(
3180
op.add_cleanup(branch_to_merge.lock_read().unlock)
3181
other_tree = branch_to_merge.basis_tree()
3182
op.add_cleanup(other_tree.lock_read().unlock)
3184
merger = _mod_merge.MergeIntoMerger(this_tree=wt, other_tree=other_tree,
3185
other_branch=branch_to_merge, target_subdir=subdir_relpath,
3186
source_subpath=subdir_to_merge)
3187
merger.set_base_revision(_mod_revision.NULL_REVISION, branch_to_merge)
3188
conflicts = merger.do_merge()
3189
merger.set_pending()
3192
def assertTreeEntriesEqual(self, expected_entries, tree):
3193
"""Assert that 'tree' contains the expected inventory entries.
3195
:param expected_entries: sequence of (path, file-id) pairs.
3197
files = [(path, ie.file_id) for path, ie in tree.iter_entries_by_dir()]
3198
self.assertEqual(expected_entries, files)
3201
class TestMergeInto(TestMergeIntoBase):
3203
def test_newdir_with_unique_roots(self):
3204
"""Merge a branch with a unique root into a new directory."""
3205
project_wt, lib_wt = self.setup_two_branches()
3206
self.do_merge_into('lib1', 'project/lib1')
3207
project_wt.lock_read()
3208
self.addCleanup(project_wt.unlock)
3209
# The r1-lib1 revision should be merged into this one
3210
self.assertEqual(['r1-project', 'r1-lib1'], project_wt.get_parent_ids())
3211
self.assertTreeEntriesEqual(
3212
[('', 'project-root-id'),
3213
('README', 'project-README-id'),
3214
('dir', 'project-dir-id'),
3215
('lib1', 'lib1-root-id'),
3216
('dir/file.c', 'project-file.c-id'),
3217
('lib1/Makefile', 'lib1-Makefile-id'),
3218
('lib1/README', 'lib1-README-id'),
3219
('lib1/foo.c', 'lib1-foo.c-id'),
3222
def test_subdir(self):
3223
"""Merge a branch into a subdirectory of an existing directory."""
3224
project_wt, lib_wt = self.setup_two_branches()
3225
self.do_merge_into('lib1', 'project/dir/lib1')
3226
project_wt.lock_read()
3227
self.addCleanup(project_wt.unlock)
3228
# The r1-lib1 revision should be merged into this one
3229
self.assertEqual(['r1-project', 'r1-lib1'], project_wt.get_parent_ids())
3230
self.assertTreeEntriesEqual(
3231
[('', 'project-root-id'),
3232
('README', 'project-README-id'),
3233
('dir', 'project-dir-id'),
3234
('dir/file.c', 'project-file.c-id'),
3235
('dir/lib1', 'lib1-root-id'),
3236
('dir/lib1/Makefile', 'lib1-Makefile-id'),
3237
('dir/lib1/README', 'lib1-README-id'),
3238
('dir/lib1/foo.c', 'lib1-foo.c-id'),
3241
def test_newdir_with_repeat_roots(self):
3242
"""If the file-id of the dir to be merged already exists a new ID will
3243
be allocated to let the merge happen.
3245
project_wt, lib_wt = self.setup_two_branches(custom_root_ids=False)
3246
root_id = project_wt.path2id('')
3247
self.do_merge_into('lib1', 'project/lib1')
3248
project_wt.lock_read()
3249
self.addCleanup(project_wt.unlock)
3250
# The r1-lib1 revision should be merged into this one
3251
self.assertEqual(['r1-project', 'r1-lib1'], project_wt.get_parent_ids())
3252
new_lib1_id = project_wt.path2id('lib1')
3253
self.assertNotEqual(None, new_lib1_id)
3254
self.assertTreeEntriesEqual(
3256
('README', 'project-README-id'),
3257
('dir', 'project-dir-id'),
3258
('lib1', new_lib1_id),
3259
('dir/file.c', 'project-file.c-id'),
3260
('lib1/Makefile', 'lib1-Makefile-id'),
3261
('lib1/README', 'lib1-README-id'),
3262
('lib1/foo.c', 'lib1-foo.c-id'),
3265
def test_name_conflict(self):
3266
"""When the target directory name already exists a conflict is
3267
generated and the original directory is renamed to foo.moved.
3269
dest_wt = self.setup_simple_branch('dest', ['dir/', 'dir/file.txt'])
3270
src_wt = self.setup_simple_branch('src', ['README'])
3271
conflicts = self.do_merge_into('src', 'dest/dir')
3272
self.assertEqual(1, conflicts)
3274
self.addCleanup(dest_wt.unlock)
3275
# The r1-lib1 revision should be merged into this one
3276
self.assertEqual(['r1-dest', 'r1-src'], dest_wt.get_parent_ids())
3277
self.assertTreeEntriesEqual(
3278
[('', 'dest-root-id'),
3279
('dir', 'src-root-id'),
3280
('dir.moved', 'dest-dir-id'),
3281
('dir/README', 'src-README-id'),
3282
('dir.moved/file.txt', 'dest-file.txt-id'),
3285
def test_file_id_conflict(self):
3286
"""A conflict is generated if the merge-into adds a file (or other
3287
inventory entry) with a file-id that already exists in the target tree.
3289
dest_wt = self.setup_simple_branch('dest', ['file.txt'])
3290
# Make a second tree with a file-id that will clash with file.txt in
3292
src_wt = self.make_branch_and_tree('src')
3293
self.build_tree(['src/README'])
3294
src_wt.add(['README'], ids=[b'dest-file.txt-id'])
3295
src_wt.commit("Rev 1 of src.", rev_id=b'r1-src')
3296
conflicts = self.do_merge_into('src', 'dest/dir')
3297
# This is an edge case that shouldn't happen to users very often. So
3298
# we don't care really about the exact presentation of the conflict,
3299
# just that there is one.
3300
self.assertEqual(1, conflicts)
3302
def test_only_subdir(self):
3303
"""When the location points to just part of a tree, merge just that
3306
dest_wt = self.setup_simple_branch('dest')
3307
src_wt = self.setup_simple_branch(
3308
'src', ['hello.txt', 'dir/', 'dir/foo.c'])
3309
conflicts = self.do_merge_into('src/dir', 'dest/dir')
3311
self.addCleanup(dest_wt.unlock)
3312
# The r1-lib1 revision should NOT be merged into this one (this is a
3314
self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3315
self.assertTreeEntriesEqual(
3316
[('', 'dest-root-id'),
3317
('dir', 'src-dir-id'),
3318
('dir/foo.c', 'src-foo.c-id'),
3321
def test_only_file(self):
3322
"""An edge case: merge just one file, not a whole dir."""
3323
dest_wt = self.setup_simple_branch('dest')
3324
two_file_wt = self.setup_simple_branch(
3325
'two-file', ['file1.txt', 'file2.txt'])
3326
conflicts = self.do_merge_into('two-file/file1.txt', 'dest/file1.txt')
3328
self.addCleanup(dest_wt.unlock)
3329
# The r1-lib1 revision should NOT be merged into this one
3330
self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3331
self.assertTreeEntriesEqual(
3332
[('', 'dest-root-id'), ('file1.txt', 'two-file-file1.txt-id')],
3335
def test_no_such_source_path(self):
3336
"""PathNotInTree is raised if the specified path in the source tree
3339
dest_wt = self.setup_simple_branch('dest')
3340
two_file_wt = self.setup_simple_branch('src', ['dir/'])
3341
self.assertRaises(_mod_merge.PathNotInTree, self.do_merge_into,
3342
'src/no-such-dir', 'dest/foo')
3344
self.addCleanup(dest_wt.unlock)
3345
# The dest tree is unmodified.
3346
self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3347
self.assertTreeEntriesEqual([('', 'dest-root-id')], dest_wt)
3349
def test_no_such_target_path(self):
3350
"""PathNotInTree is also raised if the specified path in the target
3351
tree does not exist.
3353
dest_wt = self.setup_simple_branch('dest')
3354
two_file_wt = self.setup_simple_branch('src', ['file.txt'])
3355
self.assertRaises(_mod_merge.PathNotInTree, self.do_merge_into,
3356
'src', 'dest/no-such-dir/foo')
3358
self.addCleanup(dest_wt.unlock)
3359
# The dest tree is unmodified.
3360
self.assertEqual(['r1-dest'], dest_wt.get_parent_ids())
3361
self.assertTreeEntriesEqual([('', 'dest-root-id')], dest_wt)
3364
class TestMergeHooks(TestCaseWithTransport):
3367
super(TestMergeHooks, self).setUp()
3368
self.tree_a = self.make_branch_and_tree('tree_a')
3369
self.build_tree_contents([('tree_a/file', b'content_1')])
3370
self.tree_a.add('file', b'file-id')
3371
self.tree_a.commit('added file')
3373
self.tree_b = self.tree_a.controldir.sprout('tree_b').open_workingtree()
3374
self.build_tree_contents([('tree_b/file', b'content_2')])
3375
self.tree_b.commit('modify file')
3377
def test_pre_merge_hook_inject_different_tree(self):
3378
tree_c = self.tree_b.controldir.sprout('tree_c').open_workingtree()
3379
self.build_tree_contents([('tree_c/file', b'content_3')])
3380
tree_c.commit("more content")
3382
def factory(merger):
3383
self.assertIsInstance(merger, _mod_merge.Merge3Merger)
3384
merger.other_tree = tree_c
3385
calls.append(merger)
3386
_mod_merge.Merger.hooks.install_named_hook('pre_merge',
3387
factory, 'test factory')
3388
self.tree_a.merge_from_branch(self.tree_b.branch)
3390
self.assertFileEqual("content_3", 'tree_a/file')
3391
self.assertLength(1, calls)
3393
def test_post_merge_hook_called(self):
3395
def factory(merger):
3396
self.assertIsInstance(merger, _mod_merge.Merge3Merger)
3397
calls.append(merger)
3398
_mod_merge.Merger.hooks.install_named_hook('post_merge',
3399
factory, 'test factory')
3401
self.tree_a.merge_from_branch(self.tree_b.branch)
3403
self.assertFileEqual("content_2", 'tree_a/file')
3404
self.assertLength(1, calls)